1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * emc1403.c - SMSC Thermal Driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2008 Intel Corp
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/init.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/i2c.h>
16*4882a593Smuzhiyun #include <linux/hwmon.h>
17*4882a593Smuzhiyun #include <linux/hwmon-sysfs.h>
18*4882a593Smuzhiyun #include <linux/err.h>
19*4882a593Smuzhiyun #include <linux/sysfs.h>
20*4882a593Smuzhiyun #include <linux/mutex.h>
21*4882a593Smuzhiyun #include <linux/regmap.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #define THERMAL_PID_REG 0xfd
24*4882a593Smuzhiyun #define THERMAL_SMSC_ID_REG 0xfe
25*4882a593Smuzhiyun #define THERMAL_REVISION_REG 0xff
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun enum emc1403_chip { emc1402, emc1403, emc1404 };
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun struct thermal_data {
30*4882a593Smuzhiyun struct regmap *regmap;
31*4882a593Smuzhiyun struct mutex mutex;
32*4882a593Smuzhiyun const struct attribute_group *groups[4];
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun
temp_show(struct device * dev,struct device_attribute * attr,char * buf)35*4882a593Smuzhiyun static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
36*4882a593Smuzhiyun char *buf)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
39*4882a593Smuzhiyun struct thermal_data *data = dev_get_drvdata(dev);
40*4882a593Smuzhiyun unsigned int val;
41*4882a593Smuzhiyun int retval;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun retval = regmap_read(data->regmap, sda->index, &val);
44*4882a593Smuzhiyun if (retval < 0)
45*4882a593Smuzhiyun return retval;
46*4882a593Smuzhiyun return sprintf(buf, "%d000\n", val);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
bit_show(struct device * dev,struct device_attribute * attr,char * buf)49*4882a593Smuzhiyun static ssize_t bit_show(struct device *dev, struct device_attribute *attr,
50*4882a593Smuzhiyun char *buf)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
53*4882a593Smuzhiyun struct thermal_data *data = dev_get_drvdata(dev);
54*4882a593Smuzhiyun unsigned int val;
55*4882a593Smuzhiyun int retval;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun retval = regmap_read(data->regmap, sda->nr, &val);
58*4882a593Smuzhiyun if (retval < 0)
59*4882a593Smuzhiyun return retval;
60*4882a593Smuzhiyun return sprintf(buf, "%d\n", !!(val & sda->index));
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
temp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)63*4882a593Smuzhiyun static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
64*4882a593Smuzhiyun const char *buf, size_t count)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
67*4882a593Smuzhiyun struct thermal_data *data = dev_get_drvdata(dev);
68*4882a593Smuzhiyun unsigned long val;
69*4882a593Smuzhiyun int retval;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun if (kstrtoul(buf, 10, &val))
72*4882a593Smuzhiyun return -EINVAL;
73*4882a593Smuzhiyun retval = regmap_write(data->regmap, sda->index,
74*4882a593Smuzhiyun DIV_ROUND_CLOSEST(val, 1000));
75*4882a593Smuzhiyun if (retval < 0)
76*4882a593Smuzhiyun return retval;
77*4882a593Smuzhiyun return count;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
bit_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)80*4882a593Smuzhiyun static ssize_t bit_store(struct device *dev, struct device_attribute *attr,
81*4882a593Smuzhiyun const char *buf, size_t count)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
84*4882a593Smuzhiyun struct thermal_data *data = dev_get_drvdata(dev);
85*4882a593Smuzhiyun unsigned long val;
86*4882a593Smuzhiyun int retval;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun if (kstrtoul(buf, 10, &val))
89*4882a593Smuzhiyun return -EINVAL;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun retval = regmap_update_bits(data->regmap, sda->nr, sda->index,
92*4882a593Smuzhiyun val ? sda->index : 0);
93*4882a593Smuzhiyun if (retval < 0)
94*4882a593Smuzhiyun return retval;
95*4882a593Smuzhiyun return count;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
show_hyst_common(struct device * dev,struct device_attribute * attr,char * buf,bool is_min)98*4882a593Smuzhiyun static ssize_t show_hyst_common(struct device *dev,
99*4882a593Smuzhiyun struct device_attribute *attr, char *buf,
100*4882a593Smuzhiyun bool is_min)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
103*4882a593Smuzhiyun struct thermal_data *data = dev_get_drvdata(dev);
104*4882a593Smuzhiyun struct regmap *regmap = data->regmap;
105*4882a593Smuzhiyun unsigned int limit;
106*4882a593Smuzhiyun unsigned int hyst;
107*4882a593Smuzhiyun int retval;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun retval = regmap_read(regmap, sda->index, &limit);
110*4882a593Smuzhiyun if (retval < 0)
111*4882a593Smuzhiyun return retval;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun retval = regmap_read(regmap, 0x21, &hyst);
114*4882a593Smuzhiyun if (retval < 0)
115*4882a593Smuzhiyun return retval;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun return sprintf(buf, "%d000\n", is_min ? limit + hyst : limit - hyst);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
hyst_show(struct device * dev,struct device_attribute * attr,char * buf)120*4882a593Smuzhiyun static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
121*4882a593Smuzhiyun char *buf)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun return show_hyst_common(dev, attr, buf, false);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
min_hyst_show(struct device * dev,struct device_attribute * attr,char * buf)126*4882a593Smuzhiyun static ssize_t min_hyst_show(struct device *dev,
127*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun return show_hyst_common(dev, attr, buf, true);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
hyst_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)132*4882a593Smuzhiyun static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
133*4882a593Smuzhiyun const char *buf, size_t count)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
136*4882a593Smuzhiyun struct thermal_data *data = dev_get_drvdata(dev);
137*4882a593Smuzhiyun struct regmap *regmap = data->regmap;
138*4882a593Smuzhiyun unsigned int limit;
139*4882a593Smuzhiyun int retval;
140*4882a593Smuzhiyun int hyst;
141*4882a593Smuzhiyun unsigned long val;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun if (kstrtoul(buf, 10, &val))
144*4882a593Smuzhiyun return -EINVAL;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun mutex_lock(&data->mutex);
147*4882a593Smuzhiyun retval = regmap_read(regmap, sda->index, &limit);
148*4882a593Smuzhiyun if (retval < 0)
149*4882a593Smuzhiyun goto fail;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun hyst = limit * 1000 - val;
152*4882a593Smuzhiyun hyst = clamp_val(DIV_ROUND_CLOSEST(hyst, 1000), 0, 255);
153*4882a593Smuzhiyun retval = regmap_write(regmap, 0x21, hyst);
154*4882a593Smuzhiyun if (retval == 0)
155*4882a593Smuzhiyun retval = count;
156*4882a593Smuzhiyun fail:
157*4882a593Smuzhiyun mutex_unlock(&data->mutex);
158*4882a593Smuzhiyun return retval;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun * Sensors. We pass the actual i2c register to the methods.
163*4882a593Smuzhiyun */
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 0x06);
166*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 0x05);
167*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, 0x20);
168*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0x00);
169*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, bit, 0x36, 0x01);
170*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, bit, 0x35, 0x01);
171*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, bit, 0x37, 0x01);
172*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, min_hyst, 0x06);
173*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, hyst, 0x05);
174*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0x20);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, 0x08);
177*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, 0x07);
178*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, 0x19);
179*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 0x01);
180*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, bit, 0x1b, 0x02);
181*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, bit, 0x36, 0x02);
182*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, bit, 0x35, 0x02);
183*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, bit, 0x37, 0x02);
184*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp2_min_hyst, min_hyst, 0x08);
185*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, hyst, 0x07);
186*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, hyst, 0x19);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp3_min, temp, 0x16);
189*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp3_max, temp, 0x15);
190*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp3_crit, temp, 0x1A);
191*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 0x23);
192*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, bit, 0x1b, 0x04);
193*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, bit, 0x36, 0x04);
194*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, bit, 0x35, 0x04);
195*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, bit, 0x37, 0x04);
196*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp3_min_hyst, min_hyst, 0x16);
197*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, hyst, 0x15);
198*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, hyst, 0x1A);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp4_min, temp, 0x2D);
201*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp4_max, temp, 0x2C);
202*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RW(temp4_crit, temp, 0x30);
203*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 0x2A);
204*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp4_fault, bit, 0x1b, 0x08);
205*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, bit, 0x36, 0x08);
206*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, bit, 0x35, 0x08);
207*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, bit, 0x37, 0x08);
208*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp4_min_hyst, min_hyst, 0x2D);
209*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, hyst, 0x2C);
210*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_RO(temp4_crit_hyst, hyst, 0x30);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR_2_RW(power_state, bit, 0x03, 0x40);
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun static struct attribute *emc1402_attrs[] = {
215*4882a593Smuzhiyun &sensor_dev_attr_temp1_min.dev_attr.attr,
216*4882a593Smuzhiyun &sensor_dev_attr_temp1_max.dev_attr.attr,
217*4882a593Smuzhiyun &sensor_dev_attr_temp1_crit.dev_attr.attr,
218*4882a593Smuzhiyun &sensor_dev_attr_temp1_input.dev_attr.attr,
219*4882a593Smuzhiyun &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
220*4882a593Smuzhiyun &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
221*4882a593Smuzhiyun &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun &sensor_dev_attr_temp2_min.dev_attr.attr,
224*4882a593Smuzhiyun &sensor_dev_attr_temp2_max.dev_attr.attr,
225*4882a593Smuzhiyun &sensor_dev_attr_temp2_crit.dev_attr.attr,
226*4882a593Smuzhiyun &sensor_dev_attr_temp2_input.dev_attr.attr,
227*4882a593Smuzhiyun &sensor_dev_attr_temp2_min_hyst.dev_attr.attr,
228*4882a593Smuzhiyun &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
229*4882a593Smuzhiyun &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun &sensor_dev_attr_power_state.dev_attr.attr,
232*4882a593Smuzhiyun NULL
233*4882a593Smuzhiyun };
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun static const struct attribute_group emc1402_group = {
236*4882a593Smuzhiyun .attrs = emc1402_attrs,
237*4882a593Smuzhiyun };
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun static struct attribute *emc1403_attrs[] = {
240*4882a593Smuzhiyun &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
241*4882a593Smuzhiyun &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
242*4882a593Smuzhiyun &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun &sensor_dev_attr_temp2_fault.dev_attr.attr,
245*4882a593Smuzhiyun &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
246*4882a593Smuzhiyun &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
247*4882a593Smuzhiyun &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun &sensor_dev_attr_temp3_min.dev_attr.attr,
250*4882a593Smuzhiyun &sensor_dev_attr_temp3_max.dev_attr.attr,
251*4882a593Smuzhiyun &sensor_dev_attr_temp3_crit.dev_attr.attr,
252*4882a593Smuzhiyun &sensor_dev_attr_temp3_input.dev_attr.attr,
253*4882a593Smuzhiyun &sensor_dev_attr_temp3_fault.dev_attr.attr,
254*4882a593Smuzhiyun &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
255*4882a593Smuzhiyun &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
256*4882a593Smuzhiyun &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
257*4882a593Smuzhiyun &sensor_dev_attr_temp3_min_hyst.dev_attr.attr,
258*4882a593Smuzhiyun &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
259*4882a593Smuzhiyun &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
260*4882a593Smuzhiyun NULL
261*4882a593Smuzhiyun };
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun static const struct attribute_group emc1403_group = {
264*4882a593Smuzhiyun .attrs = emc1403_attrs,
265*4882a593Smuzhiyun };
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun static struct attribute *emc1404_attrs[] = {
268*4882a593Smuzhiyun &sensor_dev_attr_temp4_min.dev_attr.attr,
269*4882a593Smuzhiyun &sensor_dev_attr_temp4_max.dev_attr.attr,
270*4882a593Smuzhiyun &sensor_dev_attr_temp4_crit.dev_attr.attr,
271*4882a593Smuzhiyun &sensor_dev_attr_temp4_input.dev_attr.attr,
272*4882a593Smuzhiyun &sensor_dev_attr_temp4_fault.dev_attr.attr,
273*4882a593Smuzhiyun &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
274*4882a593Smuzhiyun &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
275*4882a593Smuzhiyun &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
276*4882a593Smuzhiyun &sensor_dev_attr_temp4_min_hyst.dev_attr.attr,
277*4882a593Smuzhiyun &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
278*4882a593Smuzhiyun &sensor_dev_attr_temp4_crit_hyst.dev_attr.attr,
279*4882a593Smuzhiyun NULL
280*4882a593Smuzhiyun };
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun static const struct attribute_group emc1404_group = {
283*4882a593Smuzhiyun .attrs = emc1404_attrs,
284*4882a593Smuzhiyun };
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun /*
287*4882a593Smuzhiyun * EMC14x2 uses a different register and different bits to report alarm and
288*4882a593Smuzhiyun * fault status. For simplicity, provide a separate attribute group for this
289*4882a593Smuzhiyun * chip series.
290*4882a593Smuzhiyun * Since we can not re-use the same attribute names, create a separate attribute
291*4882a593Smuzhiyun * array.
292*4882a593Smuzhiyun */
293*4882a593Smuzhiyun static struct sensor_device_attribute_2 emc1402_alarms[] = {
294*4882a593Smuzhiyun SENSOR_ATTR_2_RO(temp1_min_alarm, bit, 0x02, 0x20),
295*4882a593Smuzhiyun SENSOR_ATTR_2_RO(temp1_max_alarm, bit, 0x02, 0x40),
296*4882a593Smuzhiyun SENSOR_ATTR_2_RO(temp1_crit_alarm, bit, 0x02, 0x01),
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun SENSOR_ATTR_2_RO(temp2_fault, bit, 0x02, 0x04),
299*4882a593Smuzhiyun SENSOR_ATTR_2_RO(temp2_min_alarm, bit, 0x02, 0x08),
300*4882a593Smuzhiyun SENSOR_ATTR_2_RO(temp2_max_alarm, bit, 0x02, 0x10),
301*4882a593Smuzhiyun SENSOR_ATTR_2_RO(temp2_crit_alarm, bit, 0x02, 0x02),
302*4882a593Smuzhiyun };
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun static struct attribute *emc1402_alarm_attrs[] = {
305*4882a593Smuzhiyun &emc1402_alarms[0].dev_attr.attr,
306*4882a593Smuzhiyun &emc1402_alarms[1].dev_attr.attr,
307*4882a593Smuzhiyun &emc1402_alarms[2].dev_attr.attr,
308*4882a593Smuzhiyun &emc1402_alarms[3].dev_attr.attr,
309*4882a593Smuzhiyun &emc1402_alarms[4].dev_attr.attr,
310*4882a593Smuzhiyun &emc1402_alarms[5].dev_attr.attr,
311*4882a593Smuzhiyun &emc1402_alarms[6].dev_attr.attr,
312*4882a593Smuzhiyun NULL,
313*4882a593Smuzhiyun };
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun static const struct attribute_group emc1402_alarm_group = {
316*4882a593Smuzhiyun .attrs = emc1402_alarm_attrs,
317*4882a593Smuzhiyun };
318*4882a593Smuzhiyun
emc1403_detect(struct i2c_client * client,struct i2c_board_info * info)319*4882a593Smuzhiyun static int emc1403_detect(struct i2c_client *client,
320*4882a593Smuzhiyun struct i2c_board_info *info)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun int id;
323*4882a593Smuzhiyun /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
326*4882a593Smuzhiyun if (id != 0x5d)
327*4882a593Smuzhiyun return -ENODEV;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
330*4882a593Smuzhiyun switch (id) {
331*4882a593Smuzhiyun case 0x20:
332*4882a593Smuzhiyun strlcpy(info->type, "emc1402", I2C_NAME_SIZE);
333*4882a593Smuzhiyun break;
334*4882a593Smuzhiyun case 0x21:
335*4882a593Smuzhiyun strlcpy(info->type, "emc1403", I2C_NAME_SIZE);
336*4882a593Smuzhiyun break;
337*4882a593Smuzhiyun case 0x22:
338*4882a593Smuzhiyun strlcpy(info->type, "emc1422", I2C_NAME_SIZE);
339*4882a593Smuzhiyun break;
340*4882a593Smuzhiyun case 0x23:
341*4882a593Smuzhiyun strlcpy(info->type, "emc1423", I2C_NAME_SIZE);
342*4882a593Smuzhiyun break;
343*4882a593Smuzhiyun case 0x25:
344*4882a593Smuzhiyun strlcpy(info->type, "emc1404", I2C_NAME_SIZE);
345*4882a593Smuzhiyun break;
346*4882a593Smuzhiyun case 0x27:
347*4882a593Smuzhiyun strlcpy(info->type, "emc1424", I2C_NAME_SIZE);
348*4882a593Smuzhiyun break;
349*4882a593Smuzhiyun default:
350*4882a593Smuzhiyun return -ENODEV;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
354*4882a593Smuzhiyun if (id < 0x01 || id > 0x04)
355*4882a593Smuzhiyun return -ENODEV;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun return 0;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun
emc1403_regmap_is_volatile(struct device * dev,unsigned int reg)360*4882a593Smuzhiyun static bool emc1403_regmap_is_volatile(struct device *dev, unsigned int reg)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun switch (reg) {
363*4882a593Smuzhiyun case 0x00: /* internal diode high byte */
364*4882a593Smuzhiyun case 0x01: /* external diode 1 high byte */
365*4882a593Smuzhiyun case 0x02: /* status */
366*4882a593Smuzhiyun case 0x10: /* external diode 1 low byte */
367*4882a593Smuzhiyun case 0x1b: /* external diode fault */
368*4882a593Smuzhiyun case 0x23: /* external diode 2 high byte */
369*4882a593Smuzhiyun case 0x24: /* external diode 2 low byte */
370*4882a593Smuzhiyun case 0x29: /* internal diode low byte */
371*4882a593Smuzhiyun case 0x2a: /* externl diode 3 high byte */
372*4882a593Smuzhiyun case 0x2b: /* external diode 3 low byte */
373*4882a593Smuzhiyun case 0x35: /* high limit status */
374*4882a593Smuzhiyun case 0x36: /* low limit status */
375*4882a593Smuzhiyun case 0x37: /* therm limit status */
376*4882a593Smuzhiyun return true;
377*4882a593Smuzhiyun default:
378*4882a593Smuzhiyun return false;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun static const struct regmap_config emc1403_regmap_config = {
383*4882a593Smuzhiyun .reg_bits = 8,
384*4882a593Smuzhiyun .val_bits = 8,
385*4882a593Smuzhiyun .cache_type = REGCACHE_RBTREE,
386*4882a593Smuzhiyun .volatile_reg = emc1403_regmap_is_volatile,
387*4882a593Smuzhiyun };
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun static const struct i2c_device_id emc1403_idtable[];
390*4882a593Smuzhiyun
emc1403_probe(struct i2c_client * client)391*4882a593Smuzhiyun static int emc1403_probe(struct i2c_client *client)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun struct thermal_data *data;
394*4882a593Smuzhiyun struct device *hwmon_dev;
395*4882a593Smuzhiyun const struct i2c_device_id *id = i2c_match_id(emc1403_idtable, client);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
398*4882a593Smuzhiyun GFP_KERNEL);
399*4882a593Smuzhiyun if (data == NULL)
400*4882a593Smuzhiyun return -ENOMEM;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
403*4882a593Smuzhiyun if (IS_ERR(data->regmap))
404*4882a593Smuzhiyun return PTR_ERR(data->regmap);
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun mutex_init(&data->mutex);
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun switch (id->driver_data) {
409*4882a593Smuzhiyun case emc1404:
410*4882a593Smuzhiyun data->groups[2] = &emc1404_group;
411*4882a593Smuzhiyun fallthrough;
412*4882a593Smuzhiyun case emc1403:
413*4882a593Smuzhiyun data->groups[1] = &emc1403_group;
414*4882a593Smuzhiyun fallthrough;
415*4882a593Smuzhiyun case emc1402:
416*4882a593Smuzhiyun data->groups[0] = &emc1402_group;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun if (id->driver_data == emc1402)
420*4882a593Smuzhiyun data->groups[1] = &emc1402_alarm_group;
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
423*4882a593Smuzhiyun client->name, data,
424*4882a593Smuzhiyun data->groups);
425*4882a593Smuzhiyun if (IS_ERR(hwmon_dev))
426*4882a593Smuzhiyun return PTR_ERR(hwmon_dev);
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun dev_info(&client->dev, "%s Thermal chip found\n", id->name);
429*4882a593Smuzhiyun return 0;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun static const unsigned short emc1403_address_list[] = {
433*4882a593Smuzhiyun 0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
434*4882a593Smuzhiyun };
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun /* Last digit of chip name indicates number of channels */
437*4882a593Smuzhiyun static const struct i2c_device_id emc1403_idtable[] = {
438*4882a593Smuzhiyun { "emc1402", emc1402 },
439*4882a593Smuzhiyun { "emc1403", emc1403 },
440*4882a593Smuzhiyun { "emc1404", emc1404 },
441*4882a593Smuzhiyun { "emc1412", emc1402 },
442*4882a593Smuzhiyun { "emc1413", emc1403 },
443*4882a593Smuzhiyun { "emc1414", emc1404 },
444*4882a593Smuzhiyun { "emc1422", emc1402 },
445*4882a593Smuzhiyun { "emc1423", emc1403 },
446*4882a593Smuzhiyun { "emc1424", emc1404 },
447*4882a593Smuzhiyun { }
448*4882a593Smuzhiyun };
449*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun static struct i2c_driver sensor_emc1403 = {
452*4882a593Smuzhiyun .class = I2C_CLASS_HWMON,
453*4882a593Smuzhiyun .driver = {
454*4882a593Smuzhiyun .name = "emc1403",
455*4882a593Smuzhiyun },
456*4882a593Smuzhiyun .detect = emc1403_detect,
457*4882a593Smuzhiyun .probe_new = emc1403_probe,
458*4882a593Smuzhiyun .id_table = emc1403_idtable,
459*4882a593Smuzhiyun .address_list = emc1403_address_list,
460*4882a593Smuzhiyun };
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun module_i2c_driver(sensor_emc1403);
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
465*4882a593Smuzhiyun MODULE_DESCRIPTION("emc1403 Thermal Driver");
466*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
467