Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * max6639.c - Support for Maxim MAX6639
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * based on the initial MAX6639 support from semptian.net
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * by He Changqing <hechangqing@semptian.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/platform_data/max6639.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) /* Addresses to scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) /* The MAX6639 registers, valid channel numbers: 0, 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define MAX6639_REG_TEMP(ch)			(0x00 + (ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define MAX6639_REG_STATUS			0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define MAX6639_REG_OUTPUT_MASK			0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define MAX6639_REG_GCONFIG			0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define MAX6639_REG_TEMP_EXT(ch)		(0x05 + (ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define MAX6639_REG_ALERT_LIMIT(ch)		(0x08 + (ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define MAX6639_REG_OT_LIMIT(ch)		(0x0A + (ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define MAX6639_REG_THERM_LIMIT(ch)		(0x0C + (ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define MAX6639_REG_FAN_CONFIG1(ch)		(0x10 + (ch) * 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define MAX6639_REG_FAN_CONFIG2a(ch)		(0x11 + (ch) * 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define MAX6639_REG_FAN_CONFIG2b(ch)		(0x12 + (ch) * 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define MAX6639_REG_FAN_CONFIG3(ch)		(0x13 + (ch) * 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define MAX6639_REG_FAN_CNT(ch)			(0x20 + (ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define MAX6639_REG_TARGET_CNT(ch)		(0x22 + (ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define MAX6639_REG_FAN_PPR(ch)			(0x24 + (ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define MAX6639_REG_TARGTDUTY(ch)		(0x26 + (ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define MAX6639_REG_FAN_START_TEMP(ch)		(0x28 + (ch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define MAX6639_REG_DEVID			0x3D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define MAX6639_REG_MANUID			0x3E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define MAX6639_REG_DEVREV			0x3F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) /* Register bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define MAX6639_GCONFIG_STANDBY			0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define MAX6639_GCONFIG_POR			0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define MAX6639_GCONFIG_DISABLE_TIMEOUT		0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define MAX6639_GCONFIG_CH2_LOCAL		0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define MAX6639_GCONFIG_PWM_FREQ_HI		0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define MAX6639_FAN_CONFIG1_PWM			0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED	0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define FAN_FROM_REG(val, rpm_range)	((val) == 0 || (val) == 255 ? \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 				0 : (rpm_ranges[rpm_range] * 30) / (val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define TEMP_LIMIT_TO_REG(val)	clamp_val((val) / 1000, 0, 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * Client data (each client gets its own)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) struct max6639_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	char valid;		/* !=0 if following fields are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	unsigned long last_updated;	/* In jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	/* Register values sampled regularly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	u16 temp[2];		/* Temperature, in 1/8 C, 0..255 C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	bool temp_fault[2];	/* Detected temperature diode failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	u8 fan[2];		/* Register value: TACH count for fans >=30 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	u8 status;		/* Detected channel alarms and fan failures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/* Register values only written to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	u8 pwm[2];		/* Register value: Duty cycle 0..120 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	u8 temp_therm[2];	/* THERM Temperature, 0..255 C (->_max) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	u8 temp_alert[2];	/* ALERT Temperature, 0..255 C (->_crit) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	u8 temp_ot[2];		/* OT Temperature, 0..255 C (->_emergency) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	/* Register values initialized only once */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	u8 ppr;			/* Pulses per rotation 0..3 for 1..4 ppr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	u8 rpm_range;		/* Index in above rpm_ranges table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static struct max6639_data *max6639_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct max6639_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct max6639_data *ret = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	int status_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		dev_dbg(&client->dev, "Starting max6639 update\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		status_reg = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 						      MAX6639_REG_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		if (status_reg < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			ret = ERR_PTR(status_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		data->status = status_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			res = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 					MAX6639_REG_FAN_CNT(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			if (res < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				ret = ERR_PTR(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			data->fan[i] = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			res = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 					MAX6639_REG_TEMP_EXT(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			if (res < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				ret = ERR_PTR(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			data->temp[i] = res >> 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			data->temp_fault[i] = res & 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			res = i2c_smbus_read_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 					MAX6639_REG_TEMP(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			if (res < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				ret = ERR_PTR(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 				goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			data->temp[i] |= res << 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) abort:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static ssize_t temp_input_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			       struct device_attribute *dev_attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	long temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct max6639_data *data = max6639_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	temp = data->temp[attr->index] * 125;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return sprintf(buf, "%ld\n", temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static ssize_t temp_fault_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			       struct device_attribute *dev_attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct max6639_data *data = max6639_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return sprintf(buf, "%d\n", data->temp_fault[attr->index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static ssize_t temp_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			     struct device_attribute *dev_attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	struct max6639_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static ssize_t temp_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			      struct device_attribute *dev_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			      const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	struct max6639_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	res = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 				  MAX6639_REG_THERM_LIMIT(attr->index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 				  data->temp_therm[attr->index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static ssize_t temp_crit_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			      struct device_attribute *dev_attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	struct max6639_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static ssize_t temp_crit_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			       struct device_attribute *dev_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			       const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	struct max6639_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	res = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 				  MAX6639_REG_ALERT_LIMIT(attr->index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				  data->temp_alert[attr->index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static ssize_t temp_emergency_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				   struct device_attribute *dev_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 				   char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	struct max6639_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static ssize_t temp_emergency_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 				    struct device_attribute *dev_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				    const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct max6639_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	res = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 				  MAX6639_REG_OT_LIMIT(attr->index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 				  data->temp_ot[attr->index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static ssize_t pwm_show(struct device *dev, struct device_attribute *dev_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	struct max6639_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static ssize_t pwm_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			 struct device_attribute *dev_attr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			 size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct max6639_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	res = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	val = clamp_val(val, 0, 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	data->pwm[attr->index] = (u8)(val * 120 / 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				  MAX6639_REG_TARGTDUTY(attr->index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 				  data->pwm[attr->index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static ssize_t fan_input_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			      struct device_attribute *dev_attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct max6639_data *data = max6639_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		       data->rpm_range));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static ssize_t alarm_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			  struct device_attribute *dev_attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	struct max6639_data *data = max6639_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp_crit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp_crit, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static SENSOR_DEVICE_ATTR_RW(temp1_emergency, temp_emergency, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static SENSOR_DEVICE_ATTR_RW(temp2_emergency, temp_emergency, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static SENSOR_DEVICE_ATTR_RO(fan1_fault, alarm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static SENSOR_DEVICE_ATTR_RO(fan2_fault, alarm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static SENSOR_DEVICE_ATTR_RO(temp1_emergency_alarm, alarm, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static SENSOR_DEVICE_ATTR_RO(temp2_emergency_alarm, alarm, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static struct attribute *max6639_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	&sensor_dev_attr_temp2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	&sensor_dev_attr_temp2_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	&sensor_dev_attr_pwm1.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	&sensor_dev_attr_pwm2.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	&sensor_dev_attr_fan1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	&sensor_dev_attr_fan2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	&sensor_dev_attr_fan1_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	&sensor_dev_attr_fan2_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	&sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	&sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) ATTRIBUTE_GROUPS(max6639);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)  *  returns respective index in rpm_ranges table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)  *  1 by default on invalid range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static int rpm_range_to_reg(int range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		if (rpm_ranges[i] == range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	return 1; /* default: 4000 RPM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static int max6639_init_client(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			       struct max6639_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	struct max6639_platform_data *max6639_info =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	int rpm_range = 1; /* default: 4000 RPM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	/* Reset chip to default values, see below for GCONFIG setup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 				  MAX6639_GCONFIG_POR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	/* Fans pulse per revolution is 2 by default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	if (max6639_info && max6639_info->ppr > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 			max6639_info->ppr < 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		data->ppr = max6639_info->ppr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		data->ppr = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	data->ppr -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (max6639_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		rpm_range = rpm_range_to_reg(max6639_info->rpm_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	data->rpm_range = rpm_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		/* Set Fan pulse per revolution */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		err = i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 				MAX6639_REG_FAN_PPR(i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 				data->ppr << 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		/* Fans config PWM, RPM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		err = i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 			MAX6639_REG_FAN_CONFIG1(i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			MAX6639_FAN_CONFIG1_PWM | rpm_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		/* Fans PWM polarity high by default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		if (max6639_info && max6639_info->pwm_polarity == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			err = i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 				MAX6639_REG_FAN_CONFIG2a(i), 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			err = i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 				MAX6639_REG_FAN_CONFIG2a(i), 0x02);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		 * /THERM full speed enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		 * PWM frequency 25kHz, see also GCONFIG below
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		err = i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			MAX6639_REG_FAN_CONFIG3(i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		/* Max. temp. 80C/90C/100C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		data->temp_therm[i] = 80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		data->temp_alert[i] = 90;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		data->temp_ot[i] = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		err = i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 				MAX6639_REG_THERM_LIMIT(i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 				data->temp_therm[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		err = i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 				MAX6639_REG_ALERT_LIMIT(i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 				data->temp_alert[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		err = i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 				MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		/* PWM 120/120 (i.e. 100%) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		data->pwm[i] = 120;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		err = i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 				MAX6639_REG_TARGTDUTY(i), data->pwm[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	/* Start monitoring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		MAX6639_GCONFIG_PWM_FREQ_HI);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) /* Return 0 if detection is successful, -ENODEV otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) static int max6639_detect(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			  struct i2c_board_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	int dev_id, manu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	/* Actual detection via device and manufacturer ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	if (dev_id != 0x58 || manu_id != 0x4D)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	strlcpy(info->type, "max6639", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) static int max6639_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	struct max6639_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	/* Initialize the max6639 chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	err = max6639_init_client(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 							   data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 							   max6639_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) static int max6639_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	if (data < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	return i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 			MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) static int max6639_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	if (data < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	return i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) #endif /* CONFIG_PM_SLEEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) static const struct i2c_device_id max6639_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	{"max6639", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) MODULE_DEVICE_TABLE(i2c, max6639_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) static SIMPLE_DEV_PM_OPS(max6639_pm_ops, max6639_suspend, max6639_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) static struct i2c_driver max6639_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	.class = I2C_CLASS_HWMON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		   .name = "max6639",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		   .pm = &max6639_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		   },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	.probe_new = max6639_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	.id_table = max6639_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	.detect = max6639_detect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	.address_list = normal_i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) module_i2c_driver(max6639_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) MODULE_DESCRIPTION("max6639 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) MODULE_LICENSE("GPL");