1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * emc2103.c - Support for SMSC EMC2103
4*4882a593Smuzhiyun * Copyright (c) 2010 SMSC
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/module.h>
8*4882a593Smuzhiyun #include <linux/init.h>
9*4882a593Smuzhiyun #include <linux/slab.h>
10*4882a593Smuzhiyun #include <linux/jiffies.h>
11*4882a593Smuzhiyun #include <linux/i2c.h>
12*4882a593Smuzhiyun #include <linux/hwmon.h>
13*4882a593Smuzhiyun #include <linux/hwmon-sysfs.h>
14*4882a593Smuzhiyun #include <linux/err.h>
15*4882a593Smuzhiyun #include <linux/mutex.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /* Addresses scanned */
18*4882a593Smuzhiyun static const unsigned short normal_i2c[] = { 0x2E, I2C_CLIENT_END };
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun static const u8 REG_TEMP[4] = { 0x00, 0x02, 0x04, 0x06 };
21*4882a593Smuzhiyun static const u8 REG_TEMP_MIN[4] = { 0x3c, 0x38, 0x39, 0x3a };
22*4882a593Smuzhiyun static const u8 REG_TEMP_MAX[4] = { 0x34, 0x30, 0x31, 0x32 };
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #define REG_CONF1 0x20
25*4882a593Smuzhiyun #define REG_TEMP_MAX_ALARM 0x24
26*4882a593Smuzhiyun #define REG_TEMP_MIN_ALARM 0x25
27*4882a593Smuzhiyun #define REG_FAN_CONF1 0x42
28*4882a593Smuzhiyun #define REG_FAN_TARGET_LO 0x4c
29*4882a593Smuzhiyun #define REG_FAN_TARGET_HI 0x4d
30*4882a593Smuzhiyun #define REG_FAN_TACH_HI 0x4e
31*4882a593Smuzhiyun #define REG_FAN_TACH_LO 0x4f
32*4882a593Smuzhiyun #define REG_PRODUCT_ID 0xfd
33*4882a593Smuzhiyun #define REG_MFG_ID 0xfe
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /* equation 4 from datasheet: rpm = (3932160 * multipler) / count */
36*4882a593Smuzhiyun #define FAN_RPM_FACTOR 3932160
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun * 2103-2 and 2103-4's 3rd temperature sensor can be connected to two diodes
40*4882a593Smuzhiyun * in anti-parallel mode, and in this configuration both can be read
41*4882a593Smuzhiyun * independently (so we have 4 temperature inputs). The device can't
42*4882a593Smuzhiyun * detect if it's connected in this mode, so we have to manually enable
43*4882a593Smuzhiyun * it. Default is to leave the device in the state it's already in (-1).
44*4882a593Smuzhiyun * This parameter allows APD mode to be optionally forced on or off
45*4882a593Smuzhiyun */
46*4882a593Smuzhiyun static int apd = -1;
47*4882a593Smuzhiyun module_param(apd, bint, 0);
48*4882a593Smuzhiyun MODULE_PARM_DESC(apd, "Set to zero to disable anti-parallel diode mode");
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun struct temperature {
51*4882a593Smuzhiyun s8 degrees;
52*4882a593Smuzhiyun u8 fraction; /* 0-7 multiples of 0.125 */
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun struct emc2103_data {
56*4882a593Smuzhiyun struct i2c_client *client;
57*4882a593Smuzhiyun const struct attribute_group *groups[4];
58*4882a593Smuzhiyun struct mutex update_lock;
59*4882a593Smuzhiyun bool valid; /* registers are valid */
60*4882a593Smuzhiyun bool fan_rpm_control;
61*4882a593Smuzhiyun int temp_count; /* num of temp sensors */
62*4882a593Smuzhiyun unsigned long last_updated; /* in jiffies */
63*4882a593Smuzhiyun struct temperature temp[4]; /* internal + 3 external */
64*4882a593Smuzhiyun s8 temp_min[4]; /* no fractional part */
65*4882a593Smuzhiyun s8 temp_max[4]; /* no fractional part */
66*4882a593Smuzhiyun u8 temp_min_alarm;
67*4882a593Smuzhiyun u8 temp_max_alarm;
68*4882a593Smuzhiyun u8 fan_multiplier;
69*4882a593Smuzhiyun u16 fan_tach;
70*4882a593Smuzhiyun u16 fan_target;
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun
read_u8_from_i2c(struct i2c_client * client,u8 i2c_reg,u8 * output)73*4882a593Smuzhiyun static int read_u8_from_i2c(struct i2c_client *client, u8 i2c_reg, u8 *output)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun int status = i2c_smbus_read_byte_data(client, i2c_reg);
76*4882a593Smuzhiyun if (status < 0) {
77*4882a593Smuzhiyun dev_warn(&client->dev, "reg 0x%02x, err %d\n",
78*4882a593Smuzhiyun i2c_reg, status);
79*4882a593Smuzhiyun } else {
80*4882a593Smuzhiyun *output = status;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun return status;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
read_temp_from_i2c(struct i2c_client * client,u8 i2c_reg,struct temperature * temp)85*4882a593Smuzhiyun static void read_temp_from_i2c(struct i2c_client *client, u8 i2c_reg,
86*4882a593Smuzhiyun struct temperature *temp)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun u8 degrees, fractional;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun if (read_u8_from_i2c(client, i2c_reg, °rees) < 0)
91*4882a593Smuzhiyun return;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun if (read_u8_from_i2c(client, i2c_reg + 1, &fractional) < 0)
94*4882a593Smuzhiyun return;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun temp->degrees = degrees;
97*4882a593Smuzhiyun temp->fraction = (fractional & 0xe0) >> 5;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
read_fan_from_i2c(struct i2c_client * client,u16 * output,u8 hi_addr,u8 lo_addr)100*4882a593Smuzhiyun static void read_fan_from_i2c(struct i2c_client *client, u16 *output,
101*4882a593Smuzhiyun u8 hi_addr, u8 lo_addr)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun u8 high_byte, lo_byte;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun if (read_u8_from_i2c(client, hi_addr, &high_byte) < 0)
106*4882a593Smuzhiyun return;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun if (read_u8_from_i2c(client, lo_addr, &lo_byte) < 0)
109*4882a593Smuzhiyun return;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun *output = ((u16)high_byte << 5) | (lo_byte >> 3);
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
write_fan_target_to_i2c(struct i2c_client * client,u16 new_target)114*4882a593Smuzhiyun static void write_fan_target_to_i2c(struct i2c_client *client, u16 new_target)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun u8 high_byte = (new_target & 0x1fe0) >> 5;
117*4882a593Smuzhiyun u8 low_byte = (new_target & 0x001f) << 3;
118*4882a593Smuzhiyun i2c_smbus_write_byte_data(client, REG_FAN_TARGET_LO, low_byte);
119*4882a593Smuzhiyun i2c_smbus_write_byte_data(client, REG_FAN_TARGET_HI, high_byte);
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
read_fan_config_from_i2c(struct i2c_client * client)122*4882a593Smuzhiyun static void read_fan_config_from_i2c(struct i2c_client *client)
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun struct emc2103_data *data = i2c_get_clientdata(client);
126*4882a593Smuzhiyun u8 conf1;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun if (read_u8_from_i2c(client, REG_FAN_CONF1, &conf1) < 0)
129*4882a593Smuzhiyun return;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun data->fan_multiplier = 1 << ((conf1 & 0x60) >> 5);
132*4882a593Smuzhiyun data->fan_rpm_control = (conf1 & 0x80) != 0;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
emc2103_update_device(struct device * dev)135*4882a593Smuzhiyun static struct emc2103_data *emc2103_update_device(struct device *dev)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun struct emc2103_data *data = dev_get_drvdata(dev);
138*4882a593Smuzhiyun struct i2c_client *client = data->client;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun mutex_lock(&data->update_lock);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
143*4882a593Smuzhiyun || !data->valid) {
144*4882a593Smuzhiyun int i;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun for (i = 0; i < data->temp_count; i++) {
147*4882a593Smuzhiyun read_temp_from_i2c(client, REG_TEMP[i], &data->temp[i]);
148*4882a593Smuzhiyun read_u8_from_i2c(client, REG_TEMP_MIN[i],
149*4882a593Smuzhiyun &data->temp_min[i]);
150*4882a593Smuzhiyun read_u8_from_i2c(client, REG_TEMP_MAX[i],
151*4882a593Smuzhiyun &data->temp_max[i]);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun read_u8_from_i2c(client, REG_TEMP_MIN_ALARM,
155*4882a593Smuzhiyun &data->temp_min_alarm);
156*4882a593Smuzhiyun read_u8_from_i2c(client, REG_TEMP_MAX_ALARM,
157*4882a593Smuzhiyun &data->temp_max_alarm);
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun read_fan_from_i2c(client, &data->fan_tach,
160*4882a593Smuzhiyun REG_FAN_TACH_HI, REG_FAN_TACH_LO);
161*4882a593Smuzhiyun read_fan_from_i2c(client, &data->fan_target,
162*4882a593Smuzhiyun REG_FAN_TARGET_HI, REG_FAN_TARGET_LO);
163*4882a593Smuzhiyun read_fan_config_from_i2c(client);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun data->last_updated = jiffies;
166*4882a593Smuzhiyun data->valid = true;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun mutex_unlock(&data->update_lock);
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun return data;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun static ssize_t
temp_show(struct device * dev,struct device_attribute * da,char * buf)175*4882a593Smuzhiyun temp_show(struct device *dev, struct device_attribute *da, char *buf)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun int nr = to_sensor_dev_attr(da)->index;
178*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
179*4882a593Smuzhiyun int millidegrees = data->temp[nr].degrees * 1000
180*4882a593Smuzhiyun + data->temp[nr].fraction * 125;
181*4882a593Smuzhiyun return sprintf(buf, "%d\n", millidegrees);
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun static ssize_t
temp_min_show(struct device * dev,struct device_attribute * da,char * buf)185*4882a593Smuzhiyun temp_min_show(struct device *dev, struct device_attribute *da, char *buf)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun int nr = to_sensor_dev_attr(da)->index;
188*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
189*4882a593Smuzhiyun int millidegrees = data->temp_min[nr] * 1000;
190*4882a593Smuzhiyun return sprintf(buf, "%d\n", millidegrees);
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun static ssize_t
temp_max_show(struct device * dev,struct device_attribute * da,char * buf)194*4882a593Smuzhiyun temp_max_show(struct device *dev, struct device_attribute *da, char *buf)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun int nr = to_sensor_dev_attr(da)->index;
197*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
198*4882a593Smuzhiyun int millidegrees = data->temp_max[nr] * 1000;
199*4882a593Smuzhiyun return sprintf(buf, "%d\n", millidegrees);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun static ssize_t
temp_fault_show(struct device * dev,struct device_attribute * da,char * buf)203*4882a593Smuzhiyun temp_fault_show(struct device *dev, struct device_attribute *da, char *buf)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun int nr = to_sensor_dev_attr(da)->index;
206*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
207*4882a593Smuzhiyun bool fault = (data->temp[nr].degrees == -128);
208*4882a593Smuzhiyun return sprintf(buf, "%d\n", fault ? 1 : 0);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun static ssize_t
temp_min_alarm_show(struct device * dev,struct device_attribute * da,char * buf)212*4882a593Smuzhiyun temp_min_alarm_show(struct device *dev, struct device_attribute *da,
213*4882a593Smuzhiyun char *buf)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun int nr = to_sensor_dev_attr(da)->index;
216*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
217*4882a593Smuzhiyun bool alarm = data->temp_min_alarm & (1 << nr);
218*4882a593Smuzhiyun return sprintf(buf, "%d\n", alarm ? 1 : 0);
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun static ssize_t
temp_max_alarm_show(struct device * dev,struct device_attribute * da,char * buf)222*4882a593Smuzhiyun temp_max_alarm_show(struct device *dev, struct device_attribute *da,
223*4882a593Smuzhiyun char *buf)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun int nr = to_sensor_dev_attr(da)->index;
226*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
227*4882a593Smuzhiyun bool alarm = data->temp_max_alarm & (1 << nr);
228*4882a593Smuzhiyun return sprintf(buf, "%d\n", alarm ? 1 : 0);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
temp_min_store(struct device * dev,struct device_attribute * da,const char * buf,size_t count)231*4882a593Smuzhiyun static ssize_t temp_min_store(struct device *dev, struct device_attribute *da,
232*4882a593Smuzhiyun const char *buf, size_t count)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun int nr = to_sensor_dev_attr(da)->index;
235*4882a593Smuzhiyun struct emc2103_data *data = dev_get_drvdata(dev);
236*4882a593Smuzhiyun struct i2c_client *client = data->client;
237*4882a593Smuzhiyun long val;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun int result = kstrtol(buf, 10, &val);
240*4882a593Smuzhiyun if (result < 0)
241*4882a593Smuzhiyun return result;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun mutex_lock(&data->update_lock);
246*4882a593Smuzhiyun data->temp_min[nr] = val;
247*4882a593Smuzhiyun i2c_smbus_write_byte_data(client, REG_TEMP_MIN[nr], val);
248*4882a593Smuzhiyun mutex_unlock(&data->update_lock);
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun return count;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
temp_max_store(struct device * dev,struct device_attribute * da,const char * buf,size_t count)253*4882a593Smuzhiyun static ssize_t temp_max_store(struct device *dev, struct device_attribute *da,
254*4882a593Smuzhiyun const char *buf, size_t count)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun int nr = to_sensor_dev_attr(da)->index;
257*4882a593Smuzhiyun struct emc2103_data *data = dev_get_drvdata(dev);
258*4882a593Smuzhiyun struct i2c_client *client = data->client;
259*4882a593Smuzhiyun long val;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun int result = kstrtol(buf, 10, &val);
262*4882a593Smuzhiyun if (result < 0)
263*4882a593Smuzhiyun return result;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun mutex_lock(&data->update_lock);
268*4882a593Smuzhiyun data->temp_max[nr] = val;
269*4882a593Smuzhiyun i2c_smbus_write_byte_data(client, REG_TEMP_MAX[nr], val);
270*4882a593Smuzhiyun mutex_unlock(&data->update_lock);
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun return count;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun static ssize_t
fan1_input_show(struct device * dev,struct device_attribute * da,char * buf)276*4882a593Smuzhiyun fan1_input_show(struct device *dev, struct device_attribute *da, char *buf)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
279*4882a593Smuzhiyun int rpm = 0;
280*4882a593Smuzhiyun if (data->fan_tach != 0)
281*4882a593Smuzhiyun rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach;
282*4882a593Smuzhiyun return sprintf(buf, "%d\n", rpm);
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun static ssize_t
fan1_div_show(struct device * dev,struct device_attribute * da,char * buf)286*4882a593Smuzhiyun fan1_div_show(struct device *dev, struct device_attribute *da, char *buf)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
289*4882a593Smuzhiyun int fan_div = 8 / data->fan_multiplier;
290*4882a593Smuzhiyun return sprintf(buf, "%d\n", fan_div);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun /*
294*4882a593Smuzhiyun * Note: we also update the fan target here, because its value is
295*4882a593Smuzhiyun * determined in part by the fan clock divider. This follows the principle
296*4882a593Smuzhiyun * of least surprise; the user doesn't expect the fan target to change just
297*4882a593Smuzhiyun * because the divider changed.
298*4882a593Smuzhiyun */
fan1_div_store(struct device * dev,struct device_attribute * da,const char * buf,size_t count)299*4882a593Smuzhiyun static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
300*4882a593Smuzhiyun const char *buf, size_t count)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
303*4882a593Smuzhiyun struct i2c_client *client = data->client;
304*4882a593Smuzhiyun int new_range_bits, old_div = 8 / data->fan_multiplier;
305*4882a593Smuzhiyun long new_div;
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun int status = kstrtol(buf, 10, &new_div);
308*4882a593Smuzhiyun if (status < 0)
309*4882a593Smuzhiyun return status;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun if (new_div == old_div) /* No change */
312*4882a593Smuzhiyun return count;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun switch (new_div) {
315*4882a593Smuzhiyun case 1:
316*4882a593Smuzhiyun new_range_bits = 3;
317*4882a593Smuzhiyun break;
318*4882a593Smuzhiyun case 2:
319*4882a593Smuzhiyun new_range_bits = 2;
320*4882a593Smuzhiyun break;
321*4882a593Smuzhiyun case 4:
322*4882a593Smuzhiyun new_range_bits = 1;
323*4882a593Smuzhiyun break;
324*4882a593Smuzhiyun case 8:
325*4882a593Smuzhiyun new_range_bits = 0;
326*4882a593Smuzhiyun break;
327*4882a593Smuzhiyun default:
328*4882a593Smuzhiyun return -EINVAL;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun mutex_lock(&data->update_lock);
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1);
334*4882a593Smuzhiyun if (status < 0) {
335*4882a593Smuzhiyun dev_dbg(&client->dev, "reg 0x%02x, err %d\n",
336*4882a593Smuzhiyun REG_FAN_CONF1, status);
337*4882a593Smuzhiyun mutex_unlock(&data->update_lock);
338*4882a593Smuzhiyun return status;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun status &= 0x9F;
341*4882a593Smuzhiyun status |= (new_range_bits << 5);
342*4882a593Smuzhiyun i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun data->fan_multiplier = 8 / new_div;
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun /* update fan target if high byte is not disabled */
347*4882a593Smuzhiyun if ((data->fan_target & 0x1fe0) != 0x1fe0) {
348*4882a593Smuzhiyun u16 new_target = (data->fan_target * old_div) / new_div;
349*4882a593Smuzhiyun data->fan_target = min(new_target, (u16)0x1fff);
350*4882a593Smuzhiyun write_fan_target_to_i2c(client, data->fan_target);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun /* invalidate data to force re-read from hardware */
354*4882a593Smuzhiyun data->valid = false;
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun mutex_unlock(&data->update_lock);
357*4882a593Smuzhiyun return count;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun static ssize_t
fan1_target_show(struct device * dev,struct device_attribute * da,char * buf)361*4882a593Smuzhiyun fan1_target_show(struct device *dev, struct device_attribute *da, char *buf)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
364*4882a593Smuzhiyun int rpm = 0;
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun /* high byte of 0xff indicates disabled so return 0 */
367*4882a593Smuzhiyun if ((data->fan_target != 0) && ((data->fan_target & 0x1fe0) != 0x1fe0))
368*4882a593Smuzhiyun rpm = (FAN_RPM_FACTOR * data->fan_multiplier)
369*4882a593Smuzhiyun / data->fan_target;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun return sprintf(buf, "%d\n", rpm);
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
fan1_target_store(struct device * dev,struct device_attribute * da,const char * buf,size_t count)374*4882a593Smuzhiyun static ssize_t fan1_target_store(struct device *dev,
375*4882a593Smuzhiyun struct device_attribute *da, const char *buf,
376*4882a593Smuzhiyun size_t count)
377*4882a593Smuzhiyun {
378*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
379*4882a593Smuzhiyun struct i2c_client *client = data->client;
380*4882a593Smuzhiyun unsigned long rpm_target;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun int result = kstrtoul(buf, 10, &rpm_target);
383*4882a593Smuzhiyun if (result < 0)
384*4882a593Smuzhiyun return result;
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /* Datasheet states 16384 as maximum RPM target (table 3.2) */
387*4882a593Smuzhiyun rpm_target = clamp_val(rpm_target, 0, 16384);
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun mutex_lock(&data->update_lock);
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun if (rpm_target == 0)
392*4882a593Smuzhiyun data->fan_target = 0x1fff;
393*4882a593Smuzhiyun else
394*4882a593Smuzhiyun data->fan_target = clamp_val(
395*4882a593Smuzhiyun (FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target,
396*4882a593Smuzhiyun 0, 0x1fff);
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun write_fan_target_to_i2c(client, data->fan_target);
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun mutex_unlock(&data->update_lock);
401*4882a593Smuzhiyun return count;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun static ssize_t
fan1_fault_show(struct device * dev,struct device_attribute * da,char * buf)405*4882a593Smuzhiyun fan1_fault_show(struct device *dev, struct device_attribute *da, char *buf)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
408*4882a593Smuzhiyun bool fault = ((data->fan_tach & 0x1fe0) == 0x1fe0);
409*4882a593Smuzhiyun return sprintf(buf, "%d\n", fault ? 1 : 0);
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun static ssize_t
pwm1_enable_show(struct device * dev,struct device_attribute * da,char * buf)413*4882a593Smuzhiyun pwm1_enable_show(struct device *dev, struct device_attribute *da, char *buf)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun struct emc2103_data *data = emc2103_update_device(dev);
416*4882a593Smuzhiyun return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0);
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
pwm1_enable_store(struct device * dev,struct device_attribute * da,const char * buf,size_t count)419*4882a593Smuzhiyun static ssize_t pwm1_enable_store(struct device *dev,
420*4882a593Smuzhiyun struct device_attribute *da, const char *buf,
421*4882a593Smuzhiyun size_t count)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun struct emc2103_data *data = dev_get_drvdata(dev);
424*4882a593Smuzhiyun struct i2c_client *client = data->client;
425*4882a593Smuzhiyun long new_value;
426*4882a593Smuzhiyun u8 conf_reg;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun int result = kstrtol(buf, 10, &new_value);
429*4882a593Smuzhiyun if (result < 0)
430*4882a593Smuzhiyun return result;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun mutex_lock(&data->update_lock);
433*4882a593Smuzhiyun switch (new_value) {
434*4882a593Smuzhiyun case 0:
435*4882a593Smuzhiyun data->fan_rpm_control = false;
436*4882a593Smuzhiyun break;
437*4882a593Smuzhiyun case 3:
438*4882a593Smuzhiyun data->fan_rpm_control = true;
439*4882a593Smuzhiyun break;
440*4882a593Smuzhiyun default:
441*4882a593Smuzhiyun count = -EINVAL;
442*4882a593Smuzhiyun goto err;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
446*4882a593Smuzhiyun if (result < 0) {
447*4882a593Smuzhiyun count = result;
448*4882a593Smuzhiyun goto err;
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun if (data->fan_rpm_control)
452*4882a593Smuzhiyun conf_reg |= 0x80;
453*4882a593Smuzhiyun else
454*4882a593Smuzhiyun conf_reg &= ~0x80;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
457*4882a593Smuzhiyun err:
458*4882a593Smuzhiyun mutex_unlock(&data->update_lock);
459*4882a593Smuzhiyun return count;
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
463*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
464*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
465*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
466*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0);
467*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0);
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
470*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
471*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
472*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
473*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1);
474*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1);
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
477*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
478*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
479*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp3_fault, temp_fault, 2);
480*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2);
481*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2);
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
484*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
485*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
486*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp4_fault, temp_fault, 3);
487*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp4_min_alarm, temp_min_alarm, 3);
488*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, temp_max_alarm, 3);
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun static DEVICE_ATTR_RO(fan1_input);
491*4882a593Smuzhiyun static DEVICE_ATTR_RW(fan1_div);
492*4882a593Smuzhiyun static DEVICE_ATTR_RW(fan1_target);
493*4882a593Smuzhiyun static DEVICE_ATTR_RO(fan1_fault);
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun static DEVICE_ATTR_RW(pwm1_enable);
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun /* sensors present on all models */
498*4882a593Smuzhiyun static struct attribute *emc2103_attributes[] = {
499*4882a593Smuzhiyun &sensor_dev_attr_temp1_input.dev_attr.attr,
500*4882a593Smuzhiyun &sensor_dev_attr_temp1_min.dev_attr.attr,
501*4882a593Smuzhiyun &sensor_dev_attr_temp1_max.dev_attr.attr,
502*4882a593Smuzhiyun &sensor_dev_attr_temp1_fault.dev_attr.attr,
503*4882a593Smuzhiyun &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
504*4882a593Smuzhiyun &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
505*4882a593Smuzhiyun &sensor_dev_attr_temp2_input.dev_attr.attr,
506*4882a593Smuzhiyun &sensor_dev_attr_temp2_min.dev_attr.attr,
507*4882a593Smuzhiyun &sensor_dev_attr_temp2_max.dev_attr.attr,
508*4882a593Smuzhiyun &sensor_dev_attr_temp2_fault.dev_attr.attr,
509*4882a593Smuzhiyun &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
510*4882a593Smuzhiyun &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
511*4882a593Smuzhiyun &dev_attr_fan1_input.attr,
512*4882a593Smuzhiyun &dev_attr_fan1_div.attr,
513*4882a593Smuzhiyun &dev_attr_fan1_target.attr,
514*4882a593Smuzhiyun &dev_attr_fan1_fault.attr,
515*4882a593Smuzhiyun &dev_attr_pwm1_enable.attr,
516*4882a593Smuzhiyun NULL
517*4882a593Smuzhiyun };
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun /* extra temperature sensors only present on 2103-2 and 2103-4 */
520*4882a593Smuzhiyun static struct attribute *emc2103_attributes_temp3[] = {
521*4882a593Smuzhiyun &sensor_dev_attr_temp3_input.dev_attr.attr,
522*4882a593Smuzhiyun &sensor_dev_attr_temp3_min.dev_attr.attr,
523*4882a593Smuzhiyun &sensor_dev_attr_temp3_max.dev_attr.attr,
524*4882a593Smuzhiyun &sensor_dev_attr_temp3_fault.dev_attr.attr,
525*4882a593Smuzhiyun &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
526*4882a593Smuzhiyun &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
527*4882a593Smuzhiyun NULL
528*4882a593Smuzhiyun };
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun /* extra temperature sensors only present on 2103-2 and 2103-4 in APD mode */
531*4882a593Smuzhiyun static struct attribute *emc2103_attributes_temp4[] = {
532*4882a593Smuzhiyun &sensor_dev_attr_temp4_input.dev_attr.attr,
533*4882a593Smuzhiyun &sensor_dev_attr_temp4_min.dev_attr.attr,
534*4882a593Smuzhiyun &sensor_dev_attr_temp4_max.dev_attr.attr,
535*4882a593Smuzhiyun &sensor_dev_attr_temp4_fault.dev_attr.attr,
536*4882a593Smuzhiyun &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
537*4882a593Smuzhiyun &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
538*4882a593Smuzhiyun NULL
539*4882a593Smuzhiyun };
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun static const struct attribute_group emc2103_group = {
542*4882a593Smuzhiyun .attrs = emc2103_attributes,
543*4882a593Smuzhiyun };
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun static const struct attribute_group emc2103_temp3_group = {
546*4882a593Smuzhiyun .attrs = emc2103_attributes_temp3,
547*4882a593Smuzhiyun };
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun static const struct attribute_group emc2103_temp4_group = {
550*4882a593Smuzhiyun .attrs = emc2103_attributes_temp4,
551*4882a593Smuzhiyun };
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun static int
emc2103_probe(struct i2c_client * client)554*4882a593Smuzhiyun emc2103_probe(struct i2c_client *client)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun struct emc2103_data *data;
557*4882a593Smuzhiyun struct device *hwmon_dev;
558*4882a593Smuzhiyun int status, idx = 0;
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
561*4882a593Smuzhiyun return -EIO;
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun data = devm_kzalloc(&client->dev, sizeof(struct emc2103_data),
564*4882a593Smuzhiyun GFP_KERNEL);
565*4882a593Smuzhiyun if (!data)
566*4882a593Smuzhiyun return -ENOMEM;
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun i2c_set_clientdata(client, data);
569*4882a593Smuzhiyun data->client = client;
570*4882a593Smuzhiyun mutex_init(&data->update_lock);
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun /* 2103-2 and 2103-4 have 3 external diodes, 2103-1 has 1 */
573*4882a593Smuzhiyun status = i2c_smbus_read_byte_data(client, REG_PRODUCT_ID);
574*4882a593Smuzhiyun if (status == 0x24) {
575*4882a593Smuzhiyun /* 2103-1 only has 1 external diode */
576*4882a593Smuzhiyun data->temp_count = 2;
577*4882a593Smuzhiyun } else {
578*4882a593Smuzhiyun /* 2103-2 and 2103-4 have 3 or 4 external diodes */
579*4882a593Smuzhiyun status = i2c_smbus_read_byte_data(client, REG_CONF1);
580*4882a593Smuzhiyun if (status < 0) {
581*4882a593Smuzhiyun dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1,
582*4882a593Smuzhiyun status);
583*4882a593Smuzhiyun return status;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun /* detect current state of hardware */
587*4882a593Smuzhiyun data->temp_count = (status & 0x01) ? 4 : 3;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun /* force APD state if module parameter is set */
590*4882a593Smuzhiyun if (apd == 0) {
591*4882a593Smuzhiyun /* force APD mode off */
592*4882a593Smuzhiyun data->temp_count = 3;
593*4882a593Smuzhiyun status &= ~(0x01);
594*4882a593Smuzhiyun i2c_smbus_write_byte_data(client, REG_CONF1, status);
595*4882a593Smuzhiyun } else if (apd == 1) {
596*4882a593Smuzhiyun /* force APD mode on */
597*4882a593Smuzhiyun data->temp_count = 4;
598*4882a593Smuzhiyun status |= 0x01;
599*4882a593Smuzhiyun i2c_smbus_write_byte_data(client, REG_CONF1, status);
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun /* sysfs hooks */
604*4882a593Smuzhiyun data->groups[idx++] = &emc2103_group;
605*4882a593Smuzhiyun if (data->temp_count >= 3)
606*4882a593Smuzhiyun data->groups[idx++] = &emc2103_temp3_group;
607*4882a593Smuzhiyun if (data->temp_count == 4)
608*4882a593Smuzhiyun data->groups[idx++] = &emc2103_temp4_group;
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
611*4882a593Smuzhiyun client->name, data,
612*4882a593Smuzhiyun data->groups);
613*4882a593Smuzhiyun if (IS_ERR(hwmon_dev))
614*4882a593Smuzhiyun return PTR_ERR(hwmon_dev);
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun dev_info(&client->dev, "%s: sensor '%s'\n",
617*4882a593Smuzhiyun dev_name(hwmon_dev), client->name);
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun return 0;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun static const struct i2c_device_id emc2103_ids[] = {
623*4882a593Smuzhiyun { "emc2103", 0, },
624*4882a593Smuzhiyun { /* LIST END */ }
625*4882a593Smuzhiyun };
626*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, emc2103_ids);
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun /* Return 0 if detection is successful, -ENODEV otherwise */
629*4882a593Smuzhiyun static int
emc2103_detect(struct i2c_client * new_client,struct i2c_board_info * info)630*4882a593Smuzhiyun emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info)
631*4882a593Smuzhiyun {
632*4882a593Smuzhiyun struct i2c_adapter *adapter = new_client->adapter;
633*4882a593Smuzhiyun int manufacturer, product;
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
636*4882a593Smuzhiyun return -ENODEV;
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID);
639*4882a593Smuzhiyun if (manufacturer != 0x5D)
640*4882a593Smuzhiyun return -ENODEV;
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID);
643*4882a593Smuzhiyun if ((product != 0x24) && (product != 0x26))
644*4882a593Smuzhiyun return -ENODEV;
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun strlcpy(info->type, "emc2103", I2C_NAME_SIZE);
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun return 0;
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun static struct i2c_driver emc2103_driver = {
652*4882a593Smuzhiyun .class = I2C_CLASS_HWMON,
653*4882a593Smuzhiyun .driver = {
654*4882a593Smuzhiyun .name = "emc2103",
655*4882a593Smuzhiyun },
656*4882a593Smuzhiyun .probe_new = emc2103_probe,
657*4882a593Smuzhiyun .id_table = emc2103_ids,
658*4882a593Smuzhiyun .detect = emc2103_detect,
659*4882a593Smuzhiyun .address_list = normal_i2c,
660*4882a593Smuzhiyun };
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun module_i2c_driver(emc2103_driver);
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
665*4882a593Smuzhiyun MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
666*4882a593Smuzhiyun MODULE_LICENSE("GPL");
667