xref: /openbmc/linux/drivers/hwmon/emc1403.c (revision 1975d167)
1873e65bcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dac6831eSKalhan Trisal /*
3dac6831eSKalhan Trisal  * emc1403.c - SMSC Thermal Driver
4dac6831eSKalhan Trisal  *
5dac6831eSKalhan Trisal  * Copyright (C) 2008 Intel Corp
6dac6831eSKalhan Trisal  *
7dac6831eSKalhan Trisal  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8dac6831eSKalhan Trisal  *
9dac6831eSKalhan Trisal  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10dac6831eSKalhan Trisal  */
11dac6831eSKalhan Trisal 
12dac6831eSKalhan Trisal #include <linux/module.h>
13dac6831eSKalhan Trisal #include <linux/init.h>
14dac6831eSKalhan Trisal #include <linux/slab.h>
15dac6831eSKalhan Trisal #include <linux/i2c.h>
16dac6831eSKalhan Trisal #include <linux/hwmon.h>
17dac6831eSKalhan Trisal #include <linux/hwmon-sysfs.h>
18dac6831eSKalhan Trisal #include <linux/err.h>
19dac6831eSKalhan Trisal #include <linux/sysfs.h>
20dac6831eSKalhan Trisal #include <linux/mutex.h>
214cab259fSGuenter Roeck #include <linux/regmap.h>
22dac6831eSKalhan Trisal 
23dac6831eSKalhan Trisal #define THERMAL_PID_REG		0xfd
24dac6831eSKalhan Trisal #define THERMAL_SMSC_ID_REG	0xfe
25dac6831eSKalhan Trisal #define THERMAL_REVISION_REG	0xff
26dac6831eSKalhan Trisal 
27be7f5c4dSJosef Gajdusek enum emc1403_chip { emc1402, emc1403, emc1404 };
28be7f5c4dSJosef Gajdusek 
29dac6831eSKalhan Trisal struct thermal_data {
304cab259fSGuenter Roeck 	struct regmap *regmap;
31dac6831eSKalhan Trisal 	struct mutex mutex;
324cab259fSGuenter Roeck 	const struct attribute_group *groups[4];
33dac6831eSKalhan Trisal };
34dac6831eSKalhan Trisal 
temp_show(struct device * dev,struct device_attribute * attr,char * buf)35ae66d2d9SGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
36ae66d2d9SGuenter Roeck 			 char *buf)
37dac6831eSKalhan Trisal {
38dac6831eSKalhan Trisal 	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
39454aee17SGuenter Roeck 	struct thermal_data *data = dev_get_drvdata(dev);
404cab259fSGuenter Roeck 	unsigned int val;
41454aee17SGuenter Roeck 	int retval;
42dac6831eSKalhan Trisal 
434cab259fSGuenter Roeck 	retval = regmap_read(data->regmap, sda->index, &val);
44dac6831eSKalhan Trisal 	if (retval < 0)
45dac6831eSKalhan Trisal 		return retval;
464cab259fSGuenter Roeck 	return sprintf(buf, "%d000\n", val);
47dac6831eSKalhan Trisal }
48dac6831eSKalhan Trisal 
bit_show(struct device * dev,struct device_attribute * attr,char * buf)49ae66d2d9SGuenter Roeck static ssize_t bit_show(struct device *dev, struct device_attribute *attr,
50ae66d2d9SGuenter Roeck 			char *buf)
51dac6831eSKalhan Trisal {
52dac6831eSKalhan Trisal 	struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
53454aee17SGuenter Roeck 	struct thermal_data *data = dev_get_drvdata(dev);
544cab259fSGuenter Roeck 	unsigned int val;
55454aee17SGuenter Roeck 	int retval;
56dac6831eSKalhan Trisal 
574cab259fSGuenter Roeck 	retval = regmap_read(data->regmap, sda->nr, &val);
58dac6831eSKalhan Trisal 	if (retval < 0)
59dac6831eSKalhan Trisal 		return retval;
604cab259fSGuenter Roeck 	return sprintf(buf, "%d\n", !!(val & sda->index));
61dac6831eSKalhan Trisal }
62dac6831eSKalhan Trisal 
temp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)63ae66d2d9SGuenter Roeck static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
64ae66d2d9SGuenter Roeck 			  const char *buf, size_t count)
65dac6831eSKalhan Trisal {
66dac6831eSKalhan Trisal 	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
67454aee17SGuenter Roeck 	struct thermal_data *data = dev_get_drvdata(dev);
68dac6831eSKalhan Trisal 	unsigned long val;
69dac6831eSKalhan Trisal 	int retval;
70dac6831eSKalhan Trisal 
71179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val))
72dac6831eSKalhan Trisal 		return -EINVAL;
734cab259fSGuenter Roeck 	retval = regmap_write(data->regmap, sda->index,
74dac6831eSKalhan Trisal 			      DIV_ROUND_CLOSEST(val, 1000));
75dac6831eSKalhan Trisal 	if (retval < 0)
76dac6831eSKalhan Trisal 		return retval;
77dac6831eSKalhan Trisal 	return count;
78dac6831eSKalhan Trisal }
79dac6831eSKalhan Trisal 
bit_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)80ae66d2d9SGuenter Roeck static ssize_t bit_store(struct device *dev, struct device_attribute *attr,
81ae66d2d9SGuenter Roeck 			 const char *buf, size_t count)
82960f12f4SAlan Cox {
83960f12f4SAlan Cox 	struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
84454aee17SGuenter Roeck 	struct thermal_data *data = dev_get_drvdata(dev);
85960f12f4SAlan Cox 	unsigned long val;
86960f12f4SAlan Cox 	int retval;
87960f12f4SAlan Cox 
88179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val))
89960f12f4SAlan Cox 		return -EINVAL;
90960f12f4SAlan Cox 
914cab259fSGuenter Roeck 	retval = regmap_update_bits(data->regmap, sda->nr, sda->index,
924cab259fSGuenter Roeck 				    val ? sda->index : 0);
93960f12f4SAlan Cox 	if (retval < 0)
94960f12f4SAlan Cox 		return retval;
954cab259fSGuenter Roeck 	return count;
96960f12f4SAlan Cox }
97960f12f4SAlan Cox 
show_hyst_common(struct device * dev,struct device_attribute * attr,char * buf,bool is_min)9854392ce4SGuenter Roeck static ssize_t show_hyst_common(struct device *dev,
9954392ce4SGuenter Roeck 				struct device_attribute *attr, char *buf,
10054392ce4SGuenter Roeck 				bool is_min)
101dac6831eSKalhan Trisal {
102dac6831eSKalhan Trisal 	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
103454aee17SGuenter Roeck 	struct thermal_data *data = dev_get_drvdata(dev);
1044cab259fSGuenter Roeck 	struct regmap *regmap = data->regmap;
1054cab259fSGuenter Roeck 	unsigned int limit;
1064cab259fSGuenter Roeck 	unsigned int hyst;
107dac6831eSKalhan Trisal 	int retval;
108dac6831eSKalhan Trisal 
1094cab259fSGuenter Roeck 	retval = regmap_read(regmap, sda->index, &limit);
110dac6831eSKalhan Trisal 	if (retval < 0)
111dac6831eSKalhan Trisal 		return retval;
112dac6831eSKalhan Trisal 
1134cab259fSGuenter Roeck 	retval = regmap_read(regmap, 0x21, &hyst);
1144cab259fSGuenter Roeck 	if (retval < 0)
115dac6831eSKalhan Trisal 		return retval;
1164cab259fSGuenter Roeck 
11754392ce4SGuenter Roeck 	return sprintf(buf, "%d000\n", is_min ? limit + hyst : limit - hyst);
11854392ce4SGuenter Roeck }
11954392ce4SGuenter Roeck 
hyst_show(struct device * dev,struct device_attribute * attr,char * buf)120ae66d2d9SGuenter Roeck static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
121ae66d2d9SGuenter Roeck 			 char *buf)
12254392ce4SGuenter Roeck {
12354392ce4SGuenter Roeck 	return show_hyst_common(dev, attr, buf, false);
12454392ce4SGuenter Roeck }
12554392ce4SGuenter Roeck 
min_hyst_show(struct device * dev,struct device_attribute * attr,char * buf)126ae66d2d9SGuenter Roeck static ssize_t min_hyst_show(struct device *dev,
12754392ce4SGuenter Roeck 			     struct device_attribute *attr, char *buf)
12854392ce4SGuenter Roeck {
12954392ce4SGuenter Roeck 	return show_hyst_common(dev, attr, buf, true);
130dac6831eSKalhan Trisal }
131dac6831eSKalhan Trisal 
hyst_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)132ae66d2d9SGuenter Roeck static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
133ae66d2d9SGuenter Roeck 			  const char *buf, size_t count)
134dac6831eSKalhan Trisal {
135dac6831eSKalhan Trisal 	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
136454aee17SGuenter Roeck 	struct thermal_data *data = dev_get_drvdata(dev);
1374cab259fSGuenter Roeck 	struct regmap *regmap = data->regmap;
1384cab259fSGuenter Roeck 	unsigned int limit;
139dac6831eSKalhan Trisal 	int retval;
140dac6831eSKalhan Trisal 	int hyst;
141dac6831eSKalhan Trisal 	unsigned long val;
142dac6831eSKalhan Trisal 
143179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val))
144dac6831eSKalhan Trisal 		return -EINVAL;
145dac6831eSKalhan Trisal 
146dac6831eSKalhan Trisal 	mutex_lock(&data->mutex);
1474cab259fSGuenter Roeck 	retval = regmap_read(regmap, sda->index, &limit);
148dac6831eSKalhan Trisal 	if (retval < 0)
149dac6831eSKalhan Trisal 		goto fail;
150dac6831eSKalhan Trisal 
1514cab259fSGuenter Roeck 	hyst = limit * 1000 - val;
152ceeaa70cSGuenter Roeck 	hyst = clamp_val(DIV_ROUND_CLOSEST(hyst, 1000), 0, 255);
1534cab259fSGuenter Roeck 	retval = regmap_write(regmap, 0x21, hyst);
1544cab259fSGuenter Roeck 	if (retval == 0)
155dac6831eSKalhan Trisal 		retval = count;
156dac6831eSKalhan Trisal fail:
157dac6831eSKalhan Trisal 	mutex_unlock(&data->mutex);
158dac6831eSKalhan Trisal 	return retval;
159dac6831eSKalhan Trisal }
160dac6831eSKalhan Trisal 
161dac6831eSKalhan Trisal /*
162dac6831eSKalhan Trisal  *	Sensors. We pass the actual i2c register to the methods.
163dac6831eSKalhan Trisal  */
164dac6831eSKalhan Trisal 
165ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 0x06);
166ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 0x05);
167ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, 0x20);
168ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0x00);
169ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, bit, 0x36, 0x01);
170ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, bit, 0x35, 0x01);
171ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, bit, 0x37, 0x01);
172ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, min_hyst, 0x06);
173ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, hyst, 0x05);
174ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0x20);
175dac6831eSKalhan Trisal 
176ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, 0x08);
177ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, 0x07);
178ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, 0x19);
179ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 0x01);
180ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, bit, 0x1b, 0x02);
181ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, bit, 0x36, 0x02);
182ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, bit, 0x35, 0x02);
183ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, bit, 0x37, 0x02);
184ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_min_hyst, min_hyst, 0x08);
185ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, hyst, 0x07);
186ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, hyst, 0x19);
187dac6831eSKalhan Trisal 
188ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp3_min, temp, 0x16);
189ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp3_max, temp, 0x15);
190ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp3_crit, temp, 0x1A);
191ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 0x23);
192ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, bit, 0x1b, 0x04);
193ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, bit, 0x36, 0x04);
194ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, bit, 0x35, 0x04);
195ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, bit, 0x37, 0x04);
196ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_min_hyst, min_hyst, 0x16);
197ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, hyst, 0x15);
198ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, hyst, 0x1A);
199dac6831eSKalhan Trisal 
200ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp4_min, temp, 0x2D);
201ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp4_max, temp, 0x2C);
202ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp4_crit, temp, 0x30);
203ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 0x2A);
204ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp4_fault, bit, 0x1b, 0x08);
205ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, bit, 0x36, 0x08);
206ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, bit, 0x35, 0x08);
207ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, bit, 0x37, 0x08);
208ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_min_hyst, min_hyst, 0x2D);
209ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, hyst, 0x2C);
210ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_crit_hyst, hyst, 0x30);
2110011ddfeSGuenter Roeck 
212ae66d2d9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(power_state, bit, 0x03, 0x40);
213960f12f4SAlan Cox 
214be7f5c4dSJosef Gajdusek static struct attribute *emc1402_attrs[] = {
215dac6831eSKalhan Trisal 	&sensor_dev_attr_temp1_min.dev_attr.attr,
216dac6831eSKalhan Trisal 	&sensor_dev_attr_temp1_max.dev_attr.attr,
217dac6831eSKalhan Trisal 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
218dac6831eSKalhan Trisal 	&sensor_dev_attr_temp1_input.dev_attr.attr,
21954392ce4SGuenter Roeck 	&sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
220a9a74006SGuenter Roeck 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
221dac6831eSKalhan Trisal 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
222be7f5c4dSJosef Gajdusek 
223dac6831eSKalhan Trisal 	&sensor_dev_attr_temp2_min.dev_attr.attr,
224dac6831eSKalhan Trisal 	&sensor_dev_attr_temp2_max.dev_attr.attr,
225dac6831eSKalhan Trisal 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
226dac6831eSKalhan Trisal 	&sensor_dev_attr_temp2_input.dev_attr.attr,
22754392ce4SGuenter Roeck 	&sensor_dev_attr_temp2_min_hyst.dev_attr.attr,
228a9a74006SGuenter Roeck 	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
229be7f5c4dSJosef Gajdusek 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
230be7f5c4dSJosef Gajdusek 
231be7f5c4dSJosef Gajdusek 	&sensor_dev_attr_power_state.dev_attr.attr,
232be7f5c4dSJosef Gajdusek 	NULL
233be7f5c4dSJosef Gajdusek };
234be7f5c4dSJosef Gajdusek 
235be7f5c4dSJosef Gajdusek static const struct attribute_group emc1402_group = {
236be7f5c4dSJosef Gajdusek 		.attrs = emc1402_attrs,
237be7f5c4dSJosef Gajdusek };
238be7f5c4dSJosef Gajdusek 
239be7f5c4dSJosef Gajdusek static struct attribute *emc1403_attrs[] = {
240be7f5c4dSJosef Gajdusek 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
241be7f5c4dSJosef Gajdusek 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
242be7f5c4dSJosef Gajdusek 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
243be7f5c4dSJosef Gajdusek 
244d8850c19SGuenter Roeck 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
245dac6831eSKalhan Trisal 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
246dac6831eSKalhan Trisal 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
247dac6831eSKalhan Trisal 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
248be7f5c4dSJosef Gajdusek 
249dac6831eSKalhan Trisal 	&sensor_dev_attr_temp3_min.dev_attr.attr,
250dac6831eSKalhan Trisal 	&sensor_dev_attr_temp3_max.dev_attr.attr,
251dac6831eSKalhan Trisal 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
252dac6831eSKalhan Trisal 	&sensor_dev_attr_temp3_input.dev_attr.attr,
253d8850c19SGuenter Roeck 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
254dac6831eSKalhan Trisal 	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
255dac6831eSKalhan Trisal 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
256dac6831eSKalhan Trisal 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
25754392ce4SGuenter Roeck 	&sensor_dev_attr_temp3_min_hyst.dev_attr.attr,
258a9a74006SGuenter Roeck 	&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
259dac6831eSKalhan Trisal 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
260dac6831eSKalhan Trisal 	NULL
261dac6831eSKalhan Trisal };
2620011ddfeSGuenter Roeck 
2630011ddfeSGuenter Roeck static const struct attribute_group emc1403_group = {
2640011ddfeSGuenter Roeck 	.attrs = emc1403_attrs,
2650011ddfeSGuenter Roeck };
2660011ddfeSGuenter Roeck 
2670011ddfeSGuenter Roeck static struct attribute *emc1404_attrs[] = {
2680011ddfeSGuenter Roeck 	&sensor_dev_attr_temp4_min.dev_attr.attr,
2690011ddfeSGuenter Roeck 	&sensor_dev_attr_temp4_max.dev_attr.attr,
2700011ddfeSGuenter Roeck 	&sensor_dev_attr_temp4_crit.dev_attr.attr,
2710011ddfeSGuenter Roeck 	&sensor_dev_attr_temp4_input.dev_attr.attr,
272d8850c19SGuenter Roeck 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
2730011ddfeSGuenter Roeck 	&sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
2740011ddfeSGuenter Roeck 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
2750011ddfeSGuenter Roeck 	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
27654392ce4SGuenter Roeck 	&sensor_dev_attr_temp4_min_hyst.dev_attr.attr,
277a9a74006SGuenter Roeck 	&sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
2780011ddfeSGuenter Roeck 	&sensor_dev_attr_temp4_crit_hyst.dev_attr.attr,
2790011ddfeSGuenter Roeck 	NULL
2800011ddfeSGuenter Roeck };
2810011ddfeSGuenter Roeck 
2820011ddfeSGuenter Roeck static const struct attribute_group emc1404_group = {
2830011ddfeSGuenter Roeck 	.attrs = emc1404_attrs,
2840011ddfeSGuenter Roeck };
285dac6831eSKalhan Trisal 
28603f49f64SGuenter Roeck /*
28703f49f64SGuenter Roeck  * EMC14x2 uses a different register and different bits to report alarm and
28803f49f64SGuenter Roeck  * fault status. For simplicity, provide a separate attribute group for this
28903f49f64SGuenter Roeck  * chip series.
29003f49f64SGuenter Roeck  * Since we can not re-use the same attribute names, create a separate attribute
29103f49f64SGuenter Roeck  * array.
29203f49f64SGuenter Roeck  */
29303f49f64SGuenter Roeck static struct sensor_device_attribute_2 emc1402_alarms[] = {
294ae66d2d9SGuenter Roeck 	SENSOR_ATTR_2_RO(temp1_min_alarm, bit, 0x02, 0x20),
295ae66d2d9SGuenter Roeck 	SENSOR_ATTR_2_RO(temp1_max_alarm, bit, 0x02, 0x40),
296ae66d2d9SGuenter Roeck 	SENSOR_ATTR_2_RO(temp1_crit_alarm, bit, 0x02, 0x01),
29703f49f64SGuenter Roeck 
298ae66d2d9SGuenter Roeck 	SENSOR_ATTR_2_RO(temp2_fault, bit, 0x02, 0x04),
299ae66d2d9SGuenter Roeck 	SENSOR_ATTR_2_RO(temp2_min_alarm, bit, 0x02, 0x08),
300ae66d2d9SGuenter Roeck 	SENSOR_ATTR_2_RO(temp2_max_alarm, bit, 0x02, 0x10),
301ae66d2d9SGuenter Roeck 	SENSOR_ATTR_2_RO(temp2_crit_alarm, bit, 0x02, 0x02),
30203f49f64SGuenter Roeck };
30303f49f64SGuenter Roeck 
30403f49f64SGuenter Roeck static struct attribute *emc1402_alarm_attrs[] = {
30503f49f64SGuenter Roeck 	&emc1402_alarms[0].dev_attr.attr,
30603f49f64SGuenter Roeck 	&emc1402_alarms[1].dev_attr.attr,
30703f49f64SGuenter Roeck 	&emc1402_alarms[2].dev_attr.attr,
30803f49f64SGuenter Roeck 	&emc1402_alarms[3].dev_attr.attr,
30903f49f64SGuenter Roeck 	&emc1402_alarms[4].dev_attr.attr,
31003f49f64SGuenter Roeck 	&emc1402_alarms[5].dev_attr.attr,
31103f49f64SGuenter Roeck 	&emc1402_alarms[6].dev_attr.attr,
31203f49f64SGuenter Roeck 	NULL,
31303f49f64SGuenter Roeck };
31403f49f64SGuenter Roeck 
31503f49f64SGuenter Roeck static const struct attribute_group emc1402_alarm_group = {
31603f49f64SGuenter Roeck 	.attrs = emc1402_alarm_attrs,
31703f49f64SGuenter Roeck };
31803f49f64SGuenter Roeck 
emc1403_detect(struct i2c_client * client,struct i2c_board_info * info)319dac6831eSKalhan Trisal static int emc1403_detect(struct i2c_client *client,
320dac6831eSKalhan Trisal 			struct i2c_board_info *info)
321dac6831eSKalhan Trisal {
322dac6831eSKalhan Trisal 	int id;
3237a1b76f2SJekyll Lai 	/* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
324dac6831eSKalhan Trisal 
325dac6831eSKalhan Trisal 	id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
326dac6831eSKalhan Trisal 	if (id != 0x5d)
327dac6831eSKalhan Trisal 		return -ENODEV;
328dac6831eSKalhan Trisal 
3297a1b76f2SJekyll Lai 	id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
3307a1b76f2SJekyll Lai 	switch (id) {
331be7f5c4dSJosef Gajdusek 	case 0x20:
332f2f394dbSWolfram Sang 		strscpy(info->type, "emc1402", I2C_NAME_SIZE);
333be7f5c4dSJosef Gajdusek 		break;
3347a1b76f2SJekyll Lai 	case 0x21:
335f2f394dbSWolfram Sang 		strscpy(info->type, "emc1403", I2C_NAME_SIZE);
3367a1b76f2SJekyll Lai 		break;
337be7f5c4dSJosef Gajdusek 	case 0x22:
338f2f394dbSWolfram Sang 		strscpy(info->type, "emc1422", I2C_NAME_SIZE);
339be7f5c4dSJosef Gajdusek 		break;
3407a1b76f2SJekyll Lai 	case 0x23:
341f2f394dbSWolfram Sang 		strscpy(info->type, "emc1423", I2C_NAME_SIZE);
3427a1b76f2SJekyll Lai 		break;
3430011ddfeSGuenter Roeck 	case 0x25:
344f2f394dbSWolfram Sang 		strscpy(info->type, "emc1404", I2C_NAME_SIZE);
3450011ddfeSGuenter Roeck 		break;
3460011ddfeSGuenter Roeck 	case 0x27:
347f2f394dbSWolfram Sang 		strscpy(info->type, "emc1424", I2C_NAME_SIZE);
3480011ddfeSGuenter Roeck 		break;
3497a1b76f2SJekyll Lai 	default:
350dac6831eSKalhan Trisal 		return -ENODEV;
3517a1b76f2SJekyll Lai 	}
352dac6831eSKalhan Trisal 
353dac6831eSKalhan Trisal 	id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
3543a18e139SJosef Gajdusek 	if (id < 0x01 || id > 0x04)
355dac6831eSKalhan Trisal 		return -ENODEV;
356dac6831eSKalhan Trisal 
357dac6831eSKalhan Trisal 	return 0;
358dac6831eSKalhan Trisal }
359dac6831eSKalhan Trisal 
emc1403_regmap_is_volatile(struct device * dev,unsigned int reg)3604cab259fSGuenter Roeck static bool emc1403_regmap_is_volatile(struct device *dev, unsigned int reg)
3614cab259fSGuenter Roeck {
3624cab259fSGuenter Roeck 	switch (reg) {
3634cab259fSGuenter Roeck 	case 0x00:	/* internal diode high byte */
3644cab259fSGuenter Roeck 	case 0x01:	/* external diode 1 high byte */
3654cab259fSGuenter Roeck 	case 0x02:	/* status */
3664cab259fSGuenter Roeck 	case 0x10:	/* external diode 1 low byte */
3674cab259fSGuenter Roeck 	case 0x1b:	/* external diode fault */
3684cab259fSGuenter Roeck 	case 0x23:	/* external diode 2 high byte */
3694cab259fSGuenter Roeck 	case 0x24:	/* external diode 2 low byte */
3704cab259fSGuenter Roeck 	case 0x29:	/* internal diode low byte */
3714cab259fSGuenter Roeck 	case 0x2a:	/* externl diode 3 high byte */
3724cab259fSGuenter Roeck 	case 0x2b:	/* external diode 3 low byte */
3734cab259fSGuenter Roeck 	case 0x35:	/* high limit status */
3744cab259fSGuenter Roeck 	case 0x36:	/* low limit status */
3754cab259fSGuenter Roeck 	case 0x37:	/* therm limit status */
3764cab259fSGuenter Roeck 		return true;
3774cab259fSGuenter Roeck 	default:
3784cab259fSGuenter Roeck 		return false;
3794cab259fSGuenter Roeck 	}
3804cab259fSGuenter Roeck }
3814cab259fSGuenter Roeck 
382034b44b4SAxel Lin static const struct regmap_config emc1403_regmap_config = {
3834cab259fSGuenter Roeck 	.reg_bits = 8,
3844cab259fSGuenter Roeck 	.val_bits = 8,
3854cab259fSGuenter Roeck 	.cache_type = REGCACHE_RBTREE,
3864cab259fSGuenter Roeck 	.volatile_reg = emc1403_regmap_is_volatile,
3874cab259fSGuenter Roeck };
3884cab259fSGuenter Roeck 
38967487038SStephen Kitt static const struct i2c_device_id emc1403_idtable[];
39067487038SStephen Kitt 
emc1403_probe(struct i2c_client * client)39167487038SStephen Kitt static int emc1403_probe(struct i2c_client *client)
392dac6831eSKalhan Trisal {
393dac6831eSKalhan Trisal 	struct thermal_data *data;
394454aee17SGuenter Roeck 	struct device *hwmon_dev;
39567487038SStephen Kitt 	const struct i2c_device_id *id = i2c_match_id(emc1403_idtable, client);
396dac6831eSKalhan Trisal 
3977b52eefeSGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
3987b52eefeSGuenter Roeck 			    GFP_KERNEL);
3997b52eefeSGuenter Roeck 	if (data == NULL)
400dac6831eSKalhan Trisal 		return -ENOMEM;
401dac6831eSKalhan Trisal 
4024cab259fSGuenter Roeck 	data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
4034cab259fSGuenter Roeck 	if (IS_ERR(data->regmap))
4044cab259fSGuenter Roeck 		return PTR_ERR(data->regmap);
4054cab259fSGuenter Roeck 
406dac6831eSKalhan Trisal 	mutex_init(&data->mutex);
407dac6831eSKalhan Trisal 
408be7f5c4dSJosef Gajdusek 	switch (id->driver_data) {
409be7f5c4dSJosef Gajdusek 	case emc1404:
410be7f5c4dSJosef Gajdusek 		data->groups[2] = &emc1404_group;
411df561f66SGustavo A. R. Silva 		fallthrough;
412be7f5c4dSJosef Gajdusek 	case emc1403:
413be7f5c4dSJosef Gajdusek 		data->groups[1] = &emc1403_group;
414df561f66SGustavo A. R. Silva 		fallthrough;
415be7f5c4dSJosef Gajdusek 	case emc1402:
416be7f5c4dSJosef Gajdusek 		data->groups[0] = &emc1402_group;
417be7f5c4dSJosef Gajdusek 	}
4180011ddfeSGuenter Roeck 
41903f49f64SGuenter Roeck 	if (id->driver_data == emc1402)
42003f49f64SGuenter Roeck 		data->groups[1] = &emc1402_alarm_group;
42103f49f64SGuenter Roeck 
4228759f904SJean Delvare 	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
423454aee17SGuenter Roeck 							   client->name, data,
4240011ddfeSGuenter Roeck 							   data->groups);
425454aee17SGuenter Roeck 	if (IS_ERR(hwmon_dev))
426454aee17SGuenter Roeck 		return PTR_ERR(hwmon_dev);
427454aee17SGuenter Roeck 
4280011ddfeSGuenter Roeck 	dev_info(&client->dev, "%s Thermal chip found\n", id->name);
4297b52eefeSGuenter Roeck 	return 0;
430dac6831eSKalhan Trisal }
431dac6831eSKalhan Trisal 
432dac6831eSKalhan Trisal static const unsigned short emc1403_address_list[] = {
433be7f5c4dSJosef Gajdusek 	0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
434dac6831eSKalhan Trisal };
435dac6831eSKalhan Trisal 
436be7f5c4dSJosef Gajdusek /* Last digit of chip name indicates number of channels */
437dac6831eSKalhan Trisal static const struct i2c_device_id emc1403_idtable[] = {
438be7f5c4dSJosef Gajdusek 	{ "emc1402", emc1402 },
439be7f5c4dSJosef Gajdusek 	{ "emc1403", emc1403 },
440be7f5c4dSJosef Gajdusek 	{ "emc1404", emc1404 },
44151585befSGuenter Roeck 	{ "emc1412", emc1402 },
44251585befSGuenter Roeck 	{ "emc1413", emc1403 },
44351585befSGuenter Roeck 	{ "emc1414", emc1404 },
444be7f5c4dSJosef Gajdusek 	{ "emc1422", emc1402 },
445be7f5c4dSJosef Gajdusek 	{ "emc1423", emc1403 },
446be7f5c4dSJosef Gajdusek 	{ "emc1424", emc1404 },
447dac6831eSKalhan Trisal 	{ }
448dac6831eSKalhan Trisal };
449dac6831eSKalhan Trisal MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
450dac6831eSKalhan Trisal 
451dac6831eSKalhan Trisal static struct i2c_driver sensor_emc1403 = {
452dac6831eSKalhan Trisal 	.class = I2C_CLASS_HWMON,
453dac6831eSKalhan Trisal 	.driver = {
454dac6831eSKalhan Trisal 		.name = "emc1403",
455dac6831eSKalhan Trisal 	},
456dac6831eSKalhan Trisal 	.detect = emc1403_detect,
457*1975d167SUwe Kleine-König 	.probe = emc1403_probe,
458dac6831eSKalhan Trisal 	.id_table = emc1403_idtable,
459dac6831eSKalhan Trisal 	.address_list = emc1403_address_list,
460dac6831eSKalhan Trisal };
461dac6831eSKalhan Trisal 
462f0967eeaSAxel Lin module_i2c_driver(sensor_emc1403);
463dac6831eSKalhan Trisal 
464dac6831eSKalhan Trisal MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
465dac6831eSKalhan Trisal MODULE_DESCRIPTION("emc1403 Thermal Driver");
466dac6831eSKalhan Trisal MODULE_LICENSE("GPL v2");
467