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