hwmon: (emc1403) Add support for conversion interval configuration
[linux-2.6-block.git] / drivers / hwmon / emc1403.c
CommitLineData
873e65bc 1// SPDX-License-Identifier: GPL-2.0-only
dac6831e
KT
2/*
3 * emc1403.c - SMSC Thermal Driver
4 *
5 * Copyright (C) 2008 Intel Corp
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
dac6831e 9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dac6831e
KT
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>
4cab259f 21#include <linux/regmap.h>
6a815781 22#include <linux/util_macros.h>
dac6831e
KT
23
24#define THERMAL_PID_REG 0xfd
25#define THERMAL_SMSC_ID_REG 0xfe
26#define THERMAL_REVISION_REG 0xff
27
be7f5c4d
JG
28enum emc1403_chip { emc1402, emc1403, emc1404 };
29
dac6831e 30struct thermal_data {
1094360d 31 enum emc1403_chip chip;
4cab259f 32 struct regmap *regmap;
dac6831e 33 struct mutex mutex;
dac6831e
KT
34};
35
1094360d 36static ssize_t power_state_show(struct device *dev, struct device_attribute *attr, char *buf)
dac6831e 37{
454aee17 38 struct thermal_data *data = dev_get_drvdata(dev);
4cab259f 39 unsigned int val;
454aee17 40 int retval;
dac6831e 41
1094360d 42 retval = regmap_read(data->regmap, 0x03, &val);
dac6831e
KT
43 if (retval < 0)
44 return retval;
1094360d 45 return sprintf(buf, "%d\n", !!(val & BIT(6)));
dac6831e
KT
46}
47
1094360d
GR
48static ssize_t power_state_store(struct device *dev, struct device_attribute *attr,
49 const char *buf, size_t count)
dac6831e 50{
454aee17 51 struct thermal_data *data = dev_get_drvdata(dev);
960f12f4
AC
52 unsigned long val;
53 int retval;
54
179c4fdb 55 if (kstrtoul(buf, 10, &val))
960f12f4
AC
56 return -EINVAL;
57
1094360d
GR
58 retval = regmap_update_bits(data->regmap, 0x03, BIT(6),
59 val ? BIT(6) : 0);
960f12f4 60 if (retval < 0)
4cab259f
GR
61 return retval;
62 return count;
960f12f4
AC
63}
64
1094360d 65static DEVICE_ATTR_RW(power_state);
be7f5c4d
JG
66
67static struct attribute *emc1403_attrs[] = {
1094360d 68 &dev_attr_power_state.attr,
0011ddfe
GR
69 NULL
70};
1094360d 71ATTRIBUTE_GROUPS(emc1403);
03f49f64 72
dac6831e
KT
73static int emc1403_detect(struct i2c_client *client,
74 struct i2c_board_info *info)
75{
76 int id;
7a1b76f2 77 /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
dac6831e
KT
78
79 id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
80 if (id != 0x5d)
81 return -ENODEV;
82
7a1b76f2
JL
83 id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
84 switch (id) {
be7f5c4d 85 case 0x20:
f2f394db 86 strscpy(info->type, "emc1402", I2C_NAME_SIZE);
be7f5c4d 87 break;
7a1b76f2 88 case 0x21:
f2f394db 89 strscpy(info->type, "emc1403", I2C_NAME_SIZE);
7a1b76f2 90 break;
be7f5c4d 91 case 0x22:
f2f394db 92 strscpy(info->type, "emc1422", I2C_NAME_SIZE);
be7f5c4d 93 break;
7a1b76f2 94 case 0x23:
f2f394db 95 strscpy(info->type, "emc1423", I2C_NAME_SIZE);
7a1b76f2 96 break;
0011ddfe 97 case 0x25:
f2f394db 98 strscpy(info->type, "emc1404", I2C_NAME_SIZE);
0011ddfe
GR
99 break;
100 case 0x27:
f2f394db 101 strscpy(info->type, "emc1424", I2C_NAME_SIZE);
0011ddfe 102 break;
9350163a
DCC
103 case 0x60:
104 strscpy(info->type, "emc1442", I2C_NAME_SIZE);
105 break;
7a1b76f2 106 default:
dac6831e 107 return -ENODEV;
7a1b76f2 108 }
dac6831e
KT
109
110 id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
3a18e139 111 if (id < 0x01 || id > 0x04)
dac6831e
KT
112 return -ENODEV;
113
dac6831e
KT
114 return 0;
115}
116
4cab259f
GR
117static bool emc1403_regmap_is_volatile(struct device *dev, unsigned int reg)
118{
119 switch (reg) {
120 case 0x00: /* internal diode high byte */
121 case 0x01: /* external diode 1 high byte */
122 case 0x02: /* status */
123 case 0x10: /* external diode 1 low byte */
124 case 0x1b: /* external diode fault */
125 case 0x23: /* external diode 2 high byte */
126 case 0x24: /* external diode 2 low byte */
127 case 0x29: /* internal diode low byte */
128 case 0x2a: /* externl diode 3 high byte */
129 case 0x2b: /* external diode 3 low byte */
130 case 0x35: /* high limit status */
131 case 0x36: /* low limit status */
132 case 0x37: /* therm limit status */
133 return true;
134 default:
135 return false;
136 }
137}
138
034b44b4 139static const struct regmap_config emc1403_regmap_config = {
4cab259f
GR
140 .reg_bits = 8,
141 .val_bits = 8,
9c440cf0 142 .cache_type = REGCACHE_MAPLE,
4cab259f
GR
143 .volatile_reg = emc1403_regmap_is_volatile,
144};
145
1094360d 146enum emc1403_reg_map {temp_min, temp_max, temp_crit, temp_input};
67487038 147
1094360d
GR
148static u8 ema1403_temp_map[] = {
149 [hwmon_temp_min] = temp_min,
150 [hwmon_temp_max] = temp_max,
151 [hwmon_temp_crit] = temp_crit,
152 [hwmon_temp_input] = temp_input,
153};
154
155static u8 emc1403_temp_regs[][4] = {
156 [0] = {
157 [temp_min] = 0x06,
158 [temp_max] = 0x05,
159 [temp_crit] = 0x20,
160 [temp_input] = 0x00,
161 },
162 [1] = {
163 [temp_min] = 0x08,
164 [temp_max] = 0x07,
165 [temp_crit] = 0x19,
166 [temp_input] = 0x01,
167 },
168 [2] = {
169 [temp_min] = 0x16,
170 [temp_max] = 0x15,
171 [temp_crit] = 0x1a,
172 [temp_input] = 0x23,
173 },
174 [3] = {
175 [temp_min] = 0x2d,
176 [temp_max] = 0x2c,
177 [temp_crit] = 0x30,
178 [temp_input] = 0x2a,
179 },
180};
181
e77b204a
GR
182static s8 emc1403_temp_regs_low[][4] = {
183 [0] = {
184 [temp_min] = -1,
185 [temp_max] = -1,
186 [temp_crit] = -1,
187 [temp_input] = 0x29,
188 },
189 [1] = {
190 [temp_min] = 0x14,
191 [temp_max] = 0x13,
192 [temp_crit] = -1,
193 [temp_input] = 0x10,
194 },
195 [2] = {
196 [temp_min] = 0x18,
197 [temp_max] = 0x17,
198 [temp_crit] = -1,
199 [temp_input] = 0x24,
200 },
201 [3] = {
202 [temp_min] = 0x2f,
203 [temp_max] = 0x2e,
204 [temp_crit] = -1,
205 [temp_input] = 0x2b,
206 },
207};
208
1094360d
GR
209static int __emc1403_get_temp(struct thermal_data *data, int channel,
210 enum emc1403_reg_map map, long *val)
dac6831e 211{
1094360d
GR
212 unsigned int regval;
213 int ret;
e77b204a 214 s8 reg;
dac6831e 215
1094360d
GR
216 ret = regmap_read(data->regmap, emc1403_temp_regs[channel][map], &regval);
217 if (ret < 0)
218 return ret;
219 *val = regval * 1000;
dac6831e 220
e77b204a
GR
221 reg = emc1403_temp_regs_low[channel][map];
222 if (reg >= 0) {
223 ret = regmap_read(data->regmap, reg, &regval);
224 if (ret < 0)
225 return ret;
226 *val += (regval >> 5) * 125;
227 }
1094360d
GR
228 return 0;
229}
4cab259f 230
1094360d
GR
231static int emc1403_get_temp(struct thermal_data *data, int channel,
232 enum emc1403_reg_map map, long *val)
233{
234 int ret;
235
236 mutex_lock(&data->mutex);
237 ret = __emc1403_get_temp(data, channel, map, val);
238 mutex_unlock(&data->mutex);
239
240 return ret;
241}
242
243static int emc1403_get_hyst(struct thermal_data *data, int channel,
244 enum emc1403_reg_map map, long *val)
245{
246 int hyst, ret;
247 long limit;
248
249 mutex_lock(&data->mutex);
250 ret = __emc1403_get_temp(data, channel, map, &limit);
251 if (ret < 0)
252 goto unlock;
253 ret = regmap_read(data->regmap, 0x21, &hyst);
254 if (ret < 0)
255 goto unlock;
256 if (map == temp_min)
257 *val = limit + hyst * 1000;
258 else
259 *val = limit - hyst * 1000;
260unlock:
261 mutex_unlock(&data->mutex);
262 return ret;
263}
264
265static int emc1403_temp_read(struct thermal_data *data, u32 attr, int channel, long *val)
266{
267 unsigned int regval;
268 int ret;
269
270 switch (attr) {
271 case hwmon_temp_min:
272 case hwmon_temp_max:
273 case hwmon_temp_crit:
274 case hwmon_temp_input:
275 ret = emc1403_get_temp(data, channel, ema1403_temp_map[attr], val);
276 break;
277 case hwmon_temp_min_hyst:
278 ret = emc1403_get_hyst(data, channel, temp_min, val);
279 break;
280 case hwmon_temp_max_hyst:
281 ret = emc1403_get_hyst(data, channel, temp_max, val);
282 break;
283 case hwmon_temp_crit_hyst:
284 ret = emc1403_get_hyst(data, channel, temp_crit, val);
285 break;
286 case hwmon_temp_min_alarm:
287 if (data->chip == emc1402) {
288 ret = regmap_read(data->regmap, 0x02, &regval);
289 if (ret < 0)
290 break;
291 *val = !!(regval & BIT(5 - 2 * channel));
292 } else {
293 ret = regmap_read(data->regmap, 0x36, &regval);
294 if (ret < 0)
295 break;
296 *val = !!(regval & BIT(channel));
297 }
298 break;
299 case hwmon_temp_max_alarm:
300 if (data->chip == emc1402) {
301 ret = regmap_read(data->regmap, 0x02, &regval);
302 if (ret < 0)
303 break;
304 *val = !!(regval & BIT(6 - 2 * channel));
305 } else {
306 ret = regmap_read(data->regmap, 0x35, &regval);
307 if (ret < 0)
308 break;
309 *val = !!(regval & BIT(channel));
310 }
311 break;
312 case hwmon_temp_crit_alarm:
313 if (data->chip == emc1402) {
314 ret = regmap_read(data->regmap, 0x02, &regval);
315 if (ret < 0)
316 break;
317 *val = !!(regval & BIT(channel));
318 } else {
319 ret = regmap_read(data->regmap, 0x37, &regval);
320 if (ret < 0)
321 break;
322 *val = !!(regval & BIT(channel));
323 }
324 break;
325 case hwmon_temp_fault:
326 ret = regmap_read(data->regmap, 0x1b, &regval);
327 if (ret < 0)
328 break;
329 *val = !!(regval & BIT(channel));
330 break;
331 default:
332 return -EOPNOTSUPP;
333 }
334 return ret;
335}
336
6a815781
GR
337static int emc1403_get_convrate(struct thermal_data *data, long *val)
338{
339 unsigned int convrate;
340 int ret;
341
342 ret = regmap_read(data->regmap, 0x04, &convrate);
343 if (ret < 0)
344 return ret;
345 if (convrate > 10)
346 convrate = 4;
347
348 *val = 16000 >> convrate;
349 return 0;
350}
351
352static int emc1403_chip_read(struct thermal_data *data, u32 attr, long *val)
353{
354 switch (attr) {
355 case hwmon_chip_update_interval:
356 return emc1403_get_convrate(data, val);
357 default:
358 return -EOPNOTSUPP;
359 }
360}
361
1094360d
GR
362static int emc1403_read(struct device *dev, enum hwmon_sensor_types type,
363 u32 attr, int channel, long *val)
364{
365 struct thermal_data *data = dev_get_drvdata(dev);
dac6831e 366
1094360d
GR
367 switch (type) {
368 case hwmon_temp:
369 return emc1403_temp_read(data, attr, channel, val);
6a815781
GR
370 case hwmon_chip:
371 return emc1403_chip_read(data, attr, val);
1094360d
GR
372 default:
373 return -EOPNOTSUPP;
be7f5c4d 374 }
1094360d 375}
0011ddfe 376
1094360d
GR
377static int emc1403_set_hyst(struct thermal_data *data, long val)
378{
379 int hyst, ret;
380 long limit;
03f49f64 381
1094360d 382 val = clamp_val(val, 0, 255000);
dac6831e 383
1094360d
GR
384 mutex_lock(&data->mutex);
385 ret = __emc1403_get_temp(data, 0, temp_crit, &limit);
386 if (ret < 0)
387 goto unlock;
388
389 hyst = limit - val;
390 hyst = clamp_val(DIV_ROUND_CLOSEST(hyst, 1000), 0, 255);
391 ret = regmap_write(data->regmap, 0x21, hyst);
392unlock:
393 mutex_unlock(&data->mutex);
394 return ret;
dac6831e
KT
395}
396
1094360d
GR
397static int emc1403_set_temp(struct thermal_data *data, int channel,
398 enum emc1403_reg_map map, long val)
399{
400 unsigned int regval;
401 int ret;
e77b204a
GR
402 u8 regh;
403 s8 regl;
1094360d 404
e77b204a
GR
405 regh = emc1403_temp_regs[channel][map];
406 regl = emc1403_temp_regs_low[channel][map];
1094360d
GR
407
408 mutex_lock(&data->mutex);
e77b204a
GR
409 if (regl >= 0) {
410 val = clamp_val(val, 0, 255875);
411 regval = DIV_ROUND_CLOSEST(val, 125);
412 ret = regmap_write(data->regmap, regh, regval >> 3);
413 if (ret < 0)
414 goto unlock;
415 ret = regmap_write(data->regmap, regl, (regval & 0x07) << 5);
416 } else {
417 val = clamp_val(val, 0, 255000);
418 regval = DIV_ROUND_CLOSEST(val, 1000);
419 ret = regmap_write(data->regmap, regh, regval);
420 }
421unlock:
1094360d
GR
422 mutex_unlock(&data->mutex);
423 return ret;
424}
425
426static int emc1403_temp_write(struct thermal_data *data, u32 attr, int channel, long val)
427{
428 switch (attr) {
429 case hwmon_temp_min:
430 case hwmon_temp_max:
431 case hwmon_temp_crit:
432 return emc1403_set_temp(data, channel, ema1403_temp_map[attr], val);
433 case hwmon_temp_crit_hyst:
434 return emc1403_set_hyst(data, val);
435 default:
436 return -EOPNOTSUPP;
437 }
438}
439
6a815781
GR
440/* Lookup table for temperature conversion times in msec */
441static const u16 ina3221_conv_time[] = {
442 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 62, 31, 16
443};
444
445static int emc1403_set_convrate(struct thermal_data *data, unsigned int interval)
446{
447 int convrate;
448
449 convrate = find_closest_descending(interval, ina3221_conv_time,
450 ARRAY_SIZE(ina3221_conv_time));
451 return regmap_write(data->regmap, 0x04, convrate);
452}
453
454static int emc1403_chip_write(struct thermal_data *data, u32 attr, long val)
455{
456 switch (attr) {
457 case hwmon_chip_update_interval:
458 return emc1403_set_convrate(data, clamp_val(val, 0, 100000));
459 default:
460 return -EOPNOTSUPP;
461 }
462}
463
1094360d
GR
464static int emc1403_write(struct device *dev, enum hwmon_sensor_types type,
465 u32 attr, int channel, long val)
466{
467 struct thermal_data *data = dev_get_drvdata(dev);
468
469 switch (type) {
470 case hwmon_temp:
471 return emc1403_temp_write(data, attr, channel, val);
6a815781
GR
472 case hwmon_chip:
473 return emc1403_chip_write(data, attr, val);
1094360d
GR
474 default:
475 return -EOPNOTSUPP;
476 }
477}
478
479static umode_t emc1403_temp_is_visible(const void *_data, u32 attr, int channel)
480{
481 const struct thermal_data *data = _data;
482
483 if (data->chip == emc1402 && channel > 1)
484 return 0;
485 if (data->chip == emc1403 && channel > 2)
486 return 0;
487
488 switch (attr) {
489 case hwmon_temp_input:
490 case hwmon_temp_min_alarm:
491 case hwmon_temp_max_alarm:
492 case hwmon_temp_crit_alarm:
493 case hwmon_temp_fault:
494 case hwmon_temp_min_hyst:
495 case hwmon_temp_max_hyst:
496 return 0444;
497 case hwmon_temp_min:
498 case hwmon_temp_max:
499 case hwmon_temp_crit:
500 return 0644;
501 case hwmon_temp_crit_hyst:
502 if (channel == 0)
503 return 0644;
504 return 0444;
505 default:
506 return 0;
507 }
508}
509
6a815781
GR
510static umode_t emc1403_chip_is_visible(const void *_data, u32 attr)
511{
512 switch (attr) {
513 case hwmon_chip_update_interval:
514 return 0644;
515 default:
516 return 0;
517 }
518}
519
1094360d
GR
520static umode_t emc1403_is_visible(const void *data, enum hwmon_sensor_types type,
521 u32 attr, int channel)
522{
523 switch (type) {
524 case hwmon_temp:
525 return emc1403_temp_is_visible(data, attr, channel);
6a815781
GR
526 case hwmon_chip:
527 return emc1403_chip_is_visible(data, attr);
1094360d
GR
528 default:
529 return 0;
530 }
531}
532
533static const struct hwmon_channel_info * const emc1403_info[] = {
6a815781 534 HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
1094360d
GR
535 HWMON_CHANNEL_INFO(temp,
536 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
537 HWMON_T_CRIT | HWMON_T_MIN_HYST | HWMON_T_MAX_HYST |
538 HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
539 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM,
540 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
541 HWMON_T_CRIT | HWMON_T_MIN_HYST | HWMON_T_MAX_HYST |
542 HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
543 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT,
544 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
545 HWMON_T_CRIT | HWMON_T_MIN_HYST | HWMON_T_MAX_HYST |
546 HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
547 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT,
548 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
549 HWMON_T_CRIT | HWMON_T_MIN_HYST | HWMON_T_MAX_HYST |
550 HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
551 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT
552 ),
553 NULL
554};
555
556static const struct hwmon_ops emc1403_hwmon_ops = {
557 .is_visible = emc1403_is_visible,
558 .read = emc1403_read,
559 .write = emc1403_write,
560};
561
562static const struct hwmon_chip_info emc1403_chip_info = {
563 .ops = &emc1403_hwmon_ops,
564 .info = emc1403_info,
dac6831e
KT
565};
566
be7f5c4d 567/* Last digit of chip name indicates number of channels */
dac6831e 568static const struct i2c_device_id emc1403_idtable[] = {
be7f5c4d
JG
569 { "emc1402", emc1402 },
570 { "emc1403", emc1403 },
571 { "emc1404", emc1404 },
51585bef
GR
572 { "emc1412", emc1402 },
573 { "emc1413", emc1403 },
574 { "emc1414", emc1404 },
be7f5c4d
JG
575 { "emc1422", emc1402 },
576 { "emc1423", emc1403 },
577 { "emc1424", emc1404 },
9350163a 578 { "emc1442", emc1402 },
dac6831e
KT
579 { }
580};
581MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
582
1094360d
GR
583static int emc1403_probe(struct i2c_client *client)
584{
585 struct thermal_data *data;
586 struct device *hwmon_dev;
587 const struct i2c_device_id *id = i2c_match_id(emc1403_idtable, client);
588
589 data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
590 GFP_KERNEL);
591 if (!data)
592 return -ENOMEM;
593
594 data->chip = id->driver_data;
595 data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
596 if (IS_ERR(data->regmap))
597 return PTR_ERR(data->regmap);
598
599 mutex_init(&data->mutex);
600
601 hwmon_dev = devm_hwmon_device_register_with_info(&client->dev,
602 client->name, data,
603 &emc1403_chip_info,
604 emc1403_groups);
605 return PTR_ERR_OR_ZERO(hwmon_dev);
606}
607
608static const unsigned short emc1403_address_list[] = {
609 0x18, 0x1c, 0x29, 0x3c, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
610};
611
dac6831e
KT
612static struct i2c_driver sensor_emc1403 = {
613 .class = I2C_CLASS_HWMON,
614 .driver = {
615 .name = "emc1403",
616 },
617 .detect = emc1403_detect,
1975d167 618 .probe = emc1403_probe,
dac6831e
KT
619 .id_table = emc1403_idtable,
620 .address_list = emc1403_address_list,
621};
622
f0967eea 623module_i2c_driver(sensor_emc1403);
dac6831e
KT
624
625MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
626MODULE_DESCRIPTION("emc1403 Thermal Driver");
627MODULE_LICENSE("GPL v2");