blob: 314838272049940109a9fed32f69aa9e4517795b [file] [log] [blame]
Thomas Gleixner873e65b2019-05-27 08:55:15 +02001// SPDX-License-Identifier: GPL-2.0-only
Kalhan Trisaldac68312010-05-27 19:58:56 +02002/*
3 * emc1403.c - SMSC Thermal Driver
4 *
5 * Copyright (C) 2008 Intel Corp
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
Kalhan Trisaldac68312010-05-27 19:58:56 +02009 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Kalhan Trisaldac68312010-05-27 19:58:56 +020010 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/i2c.h>
16#include <linux/hwmon.h>
17#include <linux/hwmon-sysfs.h>
18#include <linux/err.h>
19#include <linux/sysfs.h>
20#include <linux/mutex.h>
Guenter Roeck4cab2592014-05-12 10:44:48 -070021#include <linux/regmap.h>
Kalhan Trisaldac68312010-05-27 19:58:56 +020022
23#define THERMAL_PID_REG 0xfd
24#define THERMAL_SMSC_ID_REG 0xfe
25#define THERMAL_REVISION_REG 0xff
26
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +020027enum emc1403_chip { emc1402, emc1403, emc1404 };
28
Kalhan Trisaldac68312010-05-27 19:58:56 +020029struct thermal_data {
Guenter Roeck4cab2592014-05-12 10:44:48 -070030 struct regmap *regmap;
Kalhan Trisaldac68312010-05-27 19:58:56 +020031 struct mutex mutex;
Guenter Roeck4cab2592014-05-12 10:44:48 -070032 const struct attribute_group *groups[4];
Kalhan Trisaldac68312010-05-27 19:58:56 +020033};
34
Guenter Roeckae66d2d2018-12-10 14:02:05 -080035static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
36 char *buf)
Kalhan Trisaldac68312010-05-27 19:58:56 +020037{
Kalhan Trisaldac68312010-05-27 19:58:56 +020038 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
Guenter Roeck454aee12013-09-29 11:49:53 -070039 struct thermal_data *data = dev_get_drvdata(dev);
Guenter Roeck4cab2592014-05-12 10:44:48 -070040 unsigned int val;
Guenter Roeck454aee12013-09-29 11:49:53 -070041 int retval;
Kalhan Trisaldac68312010-05-27 19:58:56 +020042
Guenter Roeck4cab2592014-05-12 10:44:48 -070043 retval = regmap_read(data->regmap, sda->index, &val);
Kalhan Trisaldac68312010-05-27 19:58:56 +020044 if (retval < 0)
45 return retval;
Guenter Roeck4cab2592014-05-12 10:44:48 -070046 return sprintf(buf, "%d000\n", val);
Kalhan Trisaldac68312010-05-27 19:58:56 +020047}
48
Guenter Roeckae66d2d2018-12-10 14:02:05 -080049static ssize_t bit_show(struct device *dev, struct device_attribute *attr,
50 char *buf)
Kalhan Trisaldac68312010-05-27 19:58:56 +020051{
Kalhan Trisaldac68312010-05-27 19:58:56 +020052 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
Guenter Roeck454aee12013-09-29 11:49:53 -070053 struct thermal_data *data = dev_get_drvdata(dev);
Guenter Roeck4cab2592014-05-12 10:44:48 -070054 unsigned int val;
Guenter Roeck454aee12013-09-29 11:49:53 -070055 int retval;
Kalhan Trisaldac68312010-05-27 19:58:56 +020056
Guenter Roeck4cab2592014-05-12 10:44:48 -070057 retval = regmap_read(data->regmap, sda->nr, &val);
Kalhan Trisaldac68312010-05-27 19:58:56 +020058 if (retval < 0)
59 return retval;
Guenter Roeck4cab2592014-05-12 10:44:48 -070060 return sprintf(buf, "%d\n", !!(val & sda->index));
Kalhan Trisaldac68312010-05-27 19:58:56 +020061}
62
Guenter Roeckae66d2d2018-12-10 14:02:05 -080063static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
64 const char *buf, size_t count)
Kalhan Trisaldac68312010-05-27 19:58:56 +020065{
66 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
Guenter Roeck454aee12013-09-29 11:49:53 -070067 struct thermal_data *data = dev_get_drvdata(dev);
Kalhan Trisaldac68312010-05-27 19:58:56 +020068 unsigned long val;
69 int retval;
70
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +010071 if (kstrtoul(buf, 10, &val))
Kalhan Trisaldac68312010-05-27 19:58:56 +020072 return -EINVAL;
Guenter Roeck4cab2592014-05-12 10:44:48 -070073 retval = regmap_write(data->regmap, sda->index,
74 DIV_ROUND_CLOSEST(val, 1000));
Kalhan Trisaldac68312010-05-27 19:58:56 +020075 if (retval < 0)
76 return retval;
77 return count;
78}
79
Guenter Roeckae66d2d2018-12-10 14:02:05 -080080static ssize_t bit_store(struct device *dev, struct device_attribute *attr,
81 const char *buf, size_t count)
Alan Cox960f12f2010-08-14 21:08:49 +020082{
Alan Cox960f12f2010-08-14 21:08:49 +020083 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
Guenter Roeck454aee12013-09-29 11:49:53 -070084 struct thermal_data *data = dev_get_drvdata(dev);
Alan Cox960f12f2010-08-14 21:08:49 +020085 unsigned long val;
86 int retval;
87
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +010088 if (kstrtoul(buf, 10, &val))
Alan Cox960f12f2010-08-14 21:08:49 +020089 return -EINVAL;
90
Guenter Roeck4cab2592014-05-12 10:44:48 -070091 retval = regmap_update_bits(data->regmap, sda->nr, sda->index,
92 val ? sda->index : 0);
Alan Cox960f12f2010-08-14 21:08:49 +020093 if (retval < 0)
Guenter Roeck4cab2592014-05-12 10:44:48 -070094 return retval;
95 return count;
Alan Cox960f12f2010-08-14 21:08:49 +020096}
97
Guenter Roeck54392ce2014-05-12 11:35:06 -070098static ssize_t show_hyst_common(struct device *dev,
99 struct device_attribute *attr, char *buf,
100 bool is_min)
Kalhan Trisaldac68312010-05-27 19:58:56 +0200101{
Kalhan Trisaldac68312010-05-27 19:58:56 +0200102 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
Guenter Roeck454aee12013-09-29 11:49:53 -0700103 struct thermal_data *data = dev_get_drvdata(dev);
Guenter Roeck4cab2592014-05-12 10:44:48 -0700104 struct regmap *regmap = data->regmap;
105 unsigned int limit;
106 unsigned int hyst;
Kalhan Trisaldac68312010-05-27 19:58:56 +0200107 int retval;
Kalhan Trisaldac68312010-05-27 19:58:56 +0200108
Guenter Roeck4cab2592014-05-12 10:44:48 -0700109 retval = regmap_read(regmap, sda->index, &limit);
Kalhan Trisaldac68312010-05-27 19:58:56 +0200110 if (retval < 0)
111 return retval;
112
Guenter Roeck4cab2592014-05-12 10:44:48 -0700113 retval = regmap_read(regmap, 0x21, &hyst);
114 if (retval < 0)
115 return retval;
116
Guenter Roeck54392ce2014-05-12 11:35:06 -0700117 return sprintf(buf, "%d000\n", is_min ? limit + hyst : limit - hyst);
118}
119
Guenter Roeckae66d2d2018-12-10 14:02:05 -0800120static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
121 char *buf)
Guenter Roeck54392ce2014-05-12 11:35:06 -0700122{
123 return show_hyst_common(dev, attr, buf, false);
124}
125
Guenter Roeckae66d2d2018-12-10 14:02:05 -0800126static ssize_t min_hyst_show(struct device *dev,
Guenter Roeck54392ce2014-05-12 11:35:06 -0700127 struct device_attribute *attr, char *buf)
128{
129 return show_hyst_common(dev, attr, buf, true);
Kalhan Trisaldac68312010-05-27 19:58:56 +0200130}
131
Guenter Roeckae66d2d2018-12-10 14:02:05 -0800132static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
133 const char *buf, size_t count)
Kalhan Trisaldac68312010-05-27 19:58:56 +0200134{
Kalhan Trisaldac68312010-05-27 19:58:56 +0200135 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
Guenter Roeck454aee12013-09-29 11:49:53 -0700136 struct thermal_data *data = dev_get_drvdata(dev);
Guenter Roeck4cab2592014-05-12 10:44:48 -0700137 struct regmap *regmap = data->regmap;
138 unsigned int limit;
Kalhan Trisaldac68312010-05-27 19:58:56 +0200139 int retval;
140 int hyst;
141 unsigned long val;
142
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100143 if (kstrtoul(buf, 10, &val))
Kalhan Trisaldac68312010-05-27 19:58:56 +0200144 return -EINVAL;
145
146 mutex_lock(&data->mutex);
Guenter Roeck4cab2592014-05-12 10:44:48 -0700147 retval = regmap_read(regmap, sda->index, &limit);
Kalhan Trisaldac68312010-05-27 19:58:56 +0200148 if (retval < 0)
149 goto fail;
150
Guenter Roeck4cab2592014-05-12 10:44:48 -0700151 hyst = limit * 1000 - val;
Guenter Roeckceeaa702014-05-12 11:20:24 -0700152 hyst = clamp_val(DIV_ROUND_CLOSEST(hyst, 1000), 0, 255);
Guenter Roeck4cab2592014-05-12 10:44:48 -0700153 retval = regmap_write(regmap, 0x21, hyst);
154 if (retval == 0)
Kalhan Trisaldac68312010-05-27 19:58:56 +0200155 retval = count;
Kalhan Trisaldac68312010-05-27 19:58:56 +0200156fail:
157 mutex_unlock(&data->mutex);
158 return retval;
159}
160
161/*
162 * Sensors. We pass the actual i2c register to the methods.
163 */
164
Guenter Roeckae66d2d2018-12-10 14:02:05 -0800165static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 0x06);
166static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 0x05);
167static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, 0x20);
168static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0x00);
169static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, bit, 0x36, 0x01);
170static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, bit, 0x35, 0x01);
171static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, bit, 0x37, 0x01);
172static SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, min_hyst, 0x06);
173static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, hyst, 0x05);
174static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0x20);
Kalhan Trisaldac68312010-05-27 19:58:56 +0200175
Guenter Roeckae66d2d2018-12-10 14:02:05 -0800176static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, 0x08);
177static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, 0x07);
178static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, 0x19);
179static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 0x01);
180static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, bit, 0x1b, 0x02);
181static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, bit, 0x36, 0x02);
182static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, bit, 0x35, 0x02);
183static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, bit, 0x37, 0x02);
184static SENSOR_DEVICE_ATTR_RO(temp2_min_hyst, min_hyst, 0x08);
185static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, hyst, 0x07);
186static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, hyst, 0x19);
Kalhan Trisaldac68312010-05-27 19:58:56 +0200187
Guenter Roeckae66d2d2018-12-10 14:02:05 -0800188static SENSOR_DEVICE_ATTR_RW(temp3_min, temp, 0x16);
189static SENSOR_DEVICE_ATTR_RW(temp3_max, temp, 0x15);
190static SENSOR_DEVICE_ATTR_RW(temp3_crit, temp, 0x1A);
191static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 0x23);
192static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, bit, 0x1b, 0x04);
193static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, bit, 0x36, 0x04);
194static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, bit, 0x35, 0x04);
195static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, bit, 0x37, 0x04);
196static SENSOR_DEVICE_ATTR_RO(temp3_min_hyst, min_hyst, 0x16);
197static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, hyst, 0x15);
198static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, hyst, 0x1A);
Kalhan Trisaldac68312010-05-27 19:58:56 +0200199
Guenter Roeckae66d2d2018-12-10 14:02:05 -0800200static SENSOR_DEVICE_ATTR_RW(temp4_min, temp, 0x2D);
201static SENSOR_DEVICE_ATTR_RW(temp4_max, temp, 0x2C);
202static SENSOR_DEVICE_ATTR_RW(temp4_crit, temp, 0x30);
203static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 0x2A);
204static SENSOR_DEVICE_ATTR_2_RO(temp4_fault, bit, 0x1b, 0x08);
205static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, bit, 0x36, 0x08);
206static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, bit, 0x35, 0x08);
207static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, bit, 0x37, 0x08);
208static SENSOR_DEVICE_ATTR_RO(temp4_min_hyst, min_hyst, 0x2D);
209static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, hyst, 0x2C);
210static SENSOR_DEVICE_ATTR_RO(temp4_crit_hyst, hyst, 0x30);
Guenter Roeck0011ddf2013-09-29 13:20:53 -0700211
Guenter Roeckae66d2d2018-12-10 14:02:05 -0800212static SENSOR_DEVICE_ATTR_2_RW(power_state, bit, 0x03, 0x40);
Alan Cox960f12f2010-08-14 21:08:49 +0200213
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200214static struct attribute *emc1402_attrs[] = {
Kalhan Trisaldac68312010-05-27 19:58:56 +0200215 &sensor_dev_attr_temp1_min.dev_attr.attr,
216 &sensor_dev_attr_temp1_max.dev_attr.attr,
217 &sensor_dev_attr_temp1_crit.dev_attr.attr,
218 &sensor_dev_attr_temp1_input.dev_attr.attr,
Guenter Roeck54392ce2014-05-12 11:35:06 -0700219 &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
Guenter Roecka9a74002014-05-12 11:25:47 -0700220 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
Kalhan Trisaldac68312010-05-27 19:58:56 +0200221 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200222
Kalhan Trisaldac68312010-05-27 19:58:56 +0200223 &sensor_dev_attr_temp2_min.dev_attr.attr,
224 &sensor_dev_attr_temp2_max.dev_attr.attr,
225 &sensor_dev_attr_temp2_crit.dev_attr.attr,
226 &sensor_dev_attr_temp2_input.dev_attr.attr,
Guenter Roeck54392ce2014-05-12 11:35:06 -0700227 &sensor_dev_attr_temp2_min_hyst.dev_attr.attr,
Guenter Roecka9a74002014-05-12 11:25:47 -0700228 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200229 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
230
231 &sensor_dev_attr_power_state.dev_attr.attr,
232 NULL
233};
234
235static const struct attribute_group emc1402_group = {
236 .attrs = emc1402_attrs,
237};
238
239static struct attribute *emc1403_attrs[] = {
240 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
241 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
242 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
243
Guenter Roeckd8850c12014-05-12 10:48:02 -0700244 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Kalhan Trisaldac68312010-05-27 19:58:56 +0200245 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
246 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
247 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200248
Kalhan Trisaldac68312010-05-27 19:58:56 +0200249 &sensor_dev_attr_temp3_min.dev_attr.attr,
250 &sensor_dev_attr_temp3_max.dev_attr.attr,
251 &sensor_dev_attr_temp3_crit.dev_attr.attr,
252 &sensor_dev_attr_temp3_input.dev_attr.attr,
Guenter Roeckd8850c12014-05-12 10:48:02 -0700253 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Kalhan Trisaldac68312010-05-27 19:58:56 +0200254 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
255 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
256 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
Guenter Roeck54392ce2014-05-12 11:35:06 -0700257 &sensor_dev_attr_temp3_min_hyst.dev_attr.attr,
Guenter Roecka9a74002014-05-12 11:25:47 -0700258 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
Kalhan Trisaldac68312010-05-27 19:58:56 +0200259 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
Kalhan Trisaldac68312010-05-27 19:58:56 +0200260 NULL
261};
Guenter Roeck0011ddf2013-09-29 13:20:53 -0700262
263static const struct attribute_group emc1403_group = {
264 .attrs = emc1403_attrs,
265};
266
267static struct attribute *emc1404_attrs[] = {
268 &sensor_dev_attr_temp4_min.dev_attr.attr,
269 &sensor_dev_attr_temp4_max.dev_attr.attr,
270 &sensor_dev_attr_temp4_crit.dev_attr.attr,
271 &sensor_dev_attr_temp4_input.dev_attr.attr,
Guenter Roeckd8850c12014-05-12 10:48:02 -0700272 &sensor_dev_attr_temp4_fault.dev_attr.attr,
Guenter Roeck0011ddf2013-09-29 13:20:53 -0700273 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
274 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
275 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
Guenter Roeck54392ce2014-05-12 11:35:06 -0700276 &sensor_dev_attr_temp4_min_hyst.dev_attr.attr,
Guenter Roecka9a74002014-05-12 11:25:47 -0700277 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
Guenter Roeck0011ddf2013-09-29 13:20:53 -0700278 &sensor_dev_attr_temp4_crit_hyst.dev_attr.attr,
279 NULL
280};
281
282static const struct attribute_group emc1404_group = {
283 .attrs = emc1404_attrs,
284};
Kalhan Trisaldac68312010-05-27 19:58:56 +0200285
Guenter Roeck03f49f62014-05-12 11:03:47 -0700286/*
287 * EMC14x2 uses a different register and different bits to report alarm and
288 * fault status. For simplicity, provide a separate attribute group for this
289 * chip series.
290 * Since we can not re-use the same attribute names, create a separate attribute
291 * array.
292 */
293static struct sensor_device_attribute_2 emc1402_alarms[] = {
Guenter Roeckae66d2d2018-12-10 14:02:05 -0800294 SENSOR_ATTR_2_RO(temp1_min_alarm, bit, 0x02, 0x20),
295 SENSOR_ATTR_2_RO(temp1_max_alarm, bit, 0x02, 0x40),
296 SENSOR_ATTR_2_RO(temp1_crit_alarm, bit, 0x02, 0x01),
Guenter Roeck03f49f62014-05-12 11:03:47 -0700297
Guenter Roeckae66d2d2018-12-10 14:02:05 -0800298 SENSOR_ATTR_2_RO(temp2_fault, bit, 0x02, 0x04),
299 SENSOR_ATTR_2_RO(temp2_min_alarm, bit, 0x02, 0x08),
300 SENSOR_ATTR_2_RO(temp2_max_alarm, bit, 0x02, 0x10),
301 SENSOR_ATTR_2_RO(temp2_crit_alarm, bit, 0x02, 0x02),
Guenter Roeck03f49f62014-05-12 11:03:47 -0700302};
303
304static struct attribute *emc1402_alarm_attrs[] = {
305 &emc1402_alarms[0].dev_attr.attr,
306 &emc1402_alarms[1].dev_attr.attr,
307 &emc1402_alarms[2].dev_attr.attr,
308 &emc1402_alarms[3].dev_attr.attr,
309 &emc1402_alarms[4].dev_attr.attr,
310 &emc1402_alarms[5].dev_attr.attr,
311 &emc1402_alarms[6].dev_attr.attr,
312 NULL,
313};
314
315static const struct attribute_group emc1402_alarm_group = {
316 .attrs = emc1402_alarm_attrs,
317};
318
Kalhan Trisaldac68312010-05-27 19:58:56 +0200319static int emc1403_detect(struct i2c_client *client,
320 struct i2c_board_info *info)
321{
322 int id;
Jekyll Lai7a1b76f2011-01-12 21:55:12 +0100323 /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
Kalhan Trisaldac68312010-05-27 19:58:56 +0200324
325 id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
326 if (id != 0x5d)
327 return -ENODEV;
328
Jekyll Lai7a1b76f2011-01-12 21:55:12 +0100329 id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
330 switch (id) {
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200331 case 0x20:
332 strlcpy(info->type, "emc1402", I2C_NAME_SIZE);
333 break;
Jekyll Lai7a1b76f2011-01-12 21:55:12 +0100334 case 0x21:
335 strlcpy(info->type, "emc1403", I2C_NAME_SIZE);
336 break;
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200337 case 0x22:
338 strlcpy(info->type, "emc1422", I2C_NAME_SIZE);
339 break;
Jekyll Lai7a1b76f2011-01-12 21:55:12 +0100340 case 0x23:
341 strlcpy(info->type, "emc1423", I2C_NAME_SIZE);
342 break;
Guenter Roeck0011ddf2013-09-29 13:20:53 -0700343 case 0x25:
344 strlcpy(info->type, "emc1404", I2C_NAME_SIZE);
345 break;
346 case 0x27:
347 strlcpy(info->type, "emc1424", I2C_NAME_SIZE);
348 break;
Jekyll Lai7a1b76f2011-01-12 21:55:12 +0100349 default:
Kalhan Trisaldac68312010-05-27 19:58:56 +0200350 return -ENODEV;
Jekyll Lai7a1b76f2011-01-12 21:55:12 +0100351 }
Kalhan Trisaldac68312010-05-27 19:58:56 +0200352
353 id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
Josef Gajdusek3a18e132014-05-12 13:48:26 +0200354 if (id < 0x01 || id > 0x04)
Kalhan Trisaldac68312010-05-27 19:58:56 +0200355 return -ENODEV;
356
Kalhan Trisaldac68312010-05-27 19:58:56 +0200357 return 0;
358}
359
Guenter Roeck4cab2592014-05-12 10:44:48 -0700360static bool emc1403_regmap_is_volatile(struct device *dev, unsigned int reg)
361{
362 switch (reg) {
363 case 0x00: /* internal diode high byte */
364 case 0x01: /* external diode 1 high byte */
365 case 0x02: /* status */
366 case 0x10: /* external diode 1 low byte */
367 case 0x1b: /* external diode fault */
368 case 0x23: /* external diode 2 high byte */
369 case 0x24: /* external diode 2 low byte */
370 case 0x29: /* internal diode low byte */
371 case 0x2a: /* externl diode 3 high byte */
372 case 0x2b: /* external diode 3 low byte */
373 case 0x35: /* high limit status */
374 case 0x36: /* low limit status */
375 case 0x37: /* therm limit status */
376 return true;
377 default:
378 return false;
379 }
380}
381
Axel Lin034b44b2014-07-02 08:42:18 +0800382static const struct regmap_config emc1403_regmap_config = {
Guenter Roeck4cab2592014-05-12 10:44:48 -0700383 .reg_bits = 8,
384 .val_bits = 8,
385 .cache_type = REGCACHE_RBTREE,
386 .volatile_reg = emc1403_regmap_is_volatile,
387};
388
Stephen Kitt67487032020-08-13 18:02:22 +0200389static const struct i2c_device_id emc1403_idtable[];
390
391static int emc1403_probe(struct i2c_client *client)
Kalhan Trisaldac68312010-05-27 19:58:56 +0200392{
Kalhan Trisaldac68312010-05-27 19:58:56 +0200393 struct thermal_data *data;
Guenter Roeck454aee12013-09-29 11:49:53 -0700394 struct device *hwmon_dev;
Stephen Kitt67487032020-08-13 18:02:22 +0200395 const struct i2c_device_id *id = i2c_match_id(emc1403_idtable, client);
Kalhan Trisaldac68312010-05-27 19:58:56 +0200396
Guenter Roeck7b52eef2012-06-02 09:58:04 -0700397 data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
398 GFP_KERNEL);
399 if (data == NULL)
Kalhan Trisaldac68312010-05-27 19:58:56 +0200400 return -ENOMEM;
Kalhan Trisaldac68312010-05-27 19:58:56 +0200401
Guenter Roeck4cab2592014-05-12 10:44:48 -0700402 data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
403 if (IS_ERR(data->regmap))
404 return PTR_ERR(data->regmap);
405
Kalhan Trisaldac68312010-05-27 19:58:56 +0200406 mutex_init(&data->mutex);
Kalhan Trisaldac68312010-05-27 19:58:56 +0200407
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200408 switch (id->driver_data) {
409 case emc1404:
410 data->groups[2] = &emc1404_group;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500411 fallthrough;
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200412 case emc1403:
413 data->groups[1] = &emc1403_group;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500414 fallthrough;
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200415 case emc1402:
416 data->groups[0] = &emc1402_group;
417 }
Guenter Roeck0011ddf2013-09-29 13:20:53 -0700418
Guenter Roeck03f49f62014-05-12 11:03:47 -0700419 if (id->driver_data == emc1402)
420 data->groups[1] = &emc1402_alarm_group;
421
Jean Delvare8759f902014-05-12 11:44:51 +0200422 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
423 client->name, data,
424 data->groups);
Guenter Roeck454aee12013-09-29 11:49:53 -0700425 if (IS_ERR(hwmon_dev))
426 return PTR_ERR(hwmon_dev);
427
Guenter Roeck0011ddf2013-09-29 13:20:53 -0700428 dev_info(&client->dev, "%s Thermal chip found\n", id->name);
Guenter Roeck7b52eef2012-06-02 09:58:04 -0700429 return 0;
Kalhan Trisaldac68312010-05-27 19:58:56 +0200430}
431
432static const unsigned short emc1403_address_list[] = {
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200433 0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
Kalhan Trisaldac68312010-05-27 19:58:56 +0200434};
435
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200436/* Last digit of chip name indicates number of channels */
Kalhan Trisaldac68312010-05-27 19:58:56 +0200437static const struct i2c_device_id emc1403_idtable[] = {
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200438 { "emc1402", emc1402 },
439 { "emc1403", emc1403 },
440 { "emc1404", emc1404 },
Guenter Roeck51585be2014-05-12 11:46:12 -0700441 { "emc1412", emc1402 },
442 { "emc1413", emc1403 },
443 { "emc1414", emc1404 },
Josef Gajdusekbe7f5c42014-05-12 14:34:09 +0200444 { "emc1422", emc1402 },
445 { "emc1423", emc1403 },
446 { "emc1424", emc1404 },
Kalhan Trisaldac68312010-05-27 19:58:56 +0200447 { }
448};
449MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
450
451static struct i2c_driver sensor_emc1403 = {
452 .class = I2C_CLASS_HWMON,
453 .driver = {
454 .name = "emc1403",
455 },
456 .detect = emc1403_detect,
Stephen Kitt67487032020-08-13 18:02:22 +0200457 .probe_new = emc1403_probe,
Kalhan Trisaldac68312010-05-27 19:58:56 +0200458 .id_table = emc1403_idtable,
459 .address_list = emc1403_address_list,
460};
461
Axel Linf0967ee2012-01-20 15:38:18 +0800462module_i2c_driver(sensor_emc1403);
Kalhan Trisaldac68312010-05-27 19:58:56 +0200463
464MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
465MODULE_DESCRIPTION("emc1403 Thermal Driver");
466MODULE_LICENSE("GPL v2");