hwmon: move from strlcpy with unused retval to strscpy
[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>
dac6831e
KT
22
23#define THERMAL_PID_REG 0xfd
24#define THERMAL_SMSC_ID_REG 0xfe
25#define THERMAL_REVISION_REG 0xff
26
be7f5c4d
JG
27enum emc1403_chip { emc1402, emc1403, emc1404 };
28
dac6831e 29struct thermal_data {
4cab259f 30 struct regmap *regmap;
dac6831e 31 struct mutex mutex;
4cab259f 32 const struct attribute_group *groups[4];
dac6831e
KT
33};
34
ae66d2d9
GR
35static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
36 char *buf)
dac6831e 37{
dac6831e 38 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
454aee17 39 struct thermal_data *data = dev_get_drvdata(dev);
4cab259f 40 unsigned int val;
454aee17 41 int retval;
dac6831e 42
4cab259f 43 retval = regmap_read(data->regmap, sda->index, &val);
dac6831e
KT
44 if (retval < 0)
45 return retval;
4cab259f 46 return sprintf(buf, "%d000\n", val);
dac6831e
KT
47}
48
ae66d2d9
GR
49static ssize_t bit_show(struct device *dev, struct device_attribute *attr,
50 char *buf)
dac6831e 51{
dac6831e 52 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
454aee17 53 struct thermal_data *data = dev_get_drvdata(dev);
4cab259f 54 unsigned int val;
454aee17 55 int retval;
dac6831e 56
4cab259f 57 retval = regmap_read(data->regmap, sda->nr, &val);
dac6831e
KT
58 if (retval < 0)
59 return retval;
4cab259f 60 return sprintf(buf, "%d\n", !!(val & sda->index));
dac6831e
KT
61}
62
ae66d2d9
GR
63static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
64 const char *buf, size_t count)
dac6831e
KT
65{
66 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
454aee17 67 struct thermal_data *data = dev_get_drvdata(dev);
dac6831e
KT
68 unsigned long val;
69 int retval;
70
179c4fdb 71 if (kstrtoul(buf, 10, &val))
dac6831e 72 return -EINVAL;
4cab259f
GR
73 retval = regmap_write(data->regmap, sda->index,
74 DIV_ROUND_CLOSEST(val, 1000));
dac6831e
KT
75 if (retval < 0)
76 return retval;
77 return count;
78}
79
ae66d2d9
GR
80static ssize_t bit_store(struct device *dev, struct device_attribute *attr,
81 const char *buf, size_t count)
960f12f4 82{
960f12f4 83 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
454aee17 84 struct thermal_data *data = dev_get_drvdata(dev);
960f12f4
AC
85 unsigned long val;
86 int retval;
87
179c4fdb 88 if (kstrtoul(buf, 10, &val))
960f12f4
AC
89 return -EINVAL;
90
4cab259f
GR
91 retval = regmap_update_bits(data->regmap, sda->nr, sda->index,
92 val ? sda->index : 0);
960f12f4 93 if (retval < 0)
4cab259f
GR
94 return retval;
95 return count;
960f12f4
AC
96}
97
54392ce4
GR
98static ssize_t show_hyst_common(struct device *dev,
99 struct device_attribute *attr, char *buf,
100 bool is_min)
dac6831e 101{
dac6831e 102 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
454aee17 103 struct thermal_data *data = dev_get_drvdata(dev);
4cab259f
GR
104 struct regmap *regmap = data->regmap;
105 unsigned int limit;
106 unsigned int hyst;
dac6831e 107 int retval;
dac6831e 108
4cab259f 109 retval = regmap_read(regmap, sda->index, &limit);
dac6831e
KT
110 if (retval < 0)
111 return retval;
112
4cab259f
GR
113 retval = regmap_read(regmap, 0x21, &hyst);
114 if (retval < 0)
115 return retval;
116
54392ce4
GR
117 return sprintf(buf, "%d000\n", is_min ? limit + hyst : limit - hyst);
118}
119
ae66d2d9
GR
120static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
121 char *buf)
54392ce4
GR
122{
123 return show_hyst_common(dev, attr, buf, false);
124}
125
ae66d2d9 126static ssize_t min_hyst_show(struct device *dev,
54392ce4
GR
127 struct device_attribute *attr, char *buf)
128{
129 return show_hyst_common(dev, attr, buf, true);
dac6831e
KT
130}
131
ae66d2d9
GR
132static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
133 const char *buf, size_t count)
dac6831e 134{
dac6831e 135 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
454aee17 136 struct thermal_data *data = dev_get_drvdata(dev);
4cab259f
GR
137 struct regmap *regmap = data->regmap;
138 unsigned int limit;
dac6831e
KT
139 int retval;
140 int hyst;
141 unsigned long val;
142
179c4fdb 143 if (kstrtoul(buf, 10, &val))
dac6831e
KT
144 return -EINVAL;
145
146 mutex_lock(&data->mutex);
4cab259f 147 retval = regmap_read(regmap, sda->index, &limit);
dac6831e
KT
148 if (retval < 0)
149 goto fail;
150
4cab259f 151 hyst = limit * 1000 - val;
ceeaa70c 152 hyst = clamp_val(DIV_ROUND_CLOSEST(hyst, 1000), 0, 255);
4cab259f
GR
153 retval = regmap_write(regmap, 0x21, hyst);
154 if (retval == 0)
dac6831e 155 retval = count;
dac6831e
KT
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
ae66d2d9
GR
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);
960f12f4 213
be7f5c4d 214static struct attribute *emc1402_attrs[] = {
dac6831e
KT
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,
54392ce4 219 &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
a9a74006 220 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
dac6831e 221 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
be7f5c4d 222
dac6831e
KT
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,
54392ce4 227 &sensor_dev_attr_temp2_min_hyst.dev_attr.attr,
a9a74006 228 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
be7f5c4d
JG
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
d8850c19 244 &sensor_dev_attr_temp2_fault.dev_attr.attr,
dac6831e
KT
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,
be7f5c4d 248
dac6831e
KT
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,
d8850c19 253 &sensor_dev_attr_temp3_fault.dev_attr.attr,
dac6831e
KT
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,
54392ce4 257 &sensor_dev_attr_temp3_min_hyst.dev_attr.attr,
a9a74006 258 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
dac6831e
KT
259 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
260 NULL
261};
0011ddfe
GR
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,
d8850c19 272 &sensor_dev_attr_temp4_fault.dev_attr.attr,
0011ddfe
GR
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,
54392ce4 276 &sensor_dev_attr_temp4_min_hyst.dev_attr.attr,
a9a74006 277 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
0011ddfe
GR
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};
dac6831e 285
03f49f64
GR
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[] = {
ae66d2d9
GR
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),
03f49f64
GR
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
dac6831e
KT
319static int emc1403_detect(struct i2c_client *client,
320 struct i2c_board_info *info)
321{
322 int id;
7a1b76f2 323 /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
dac6831e
KT
324
325 id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
326 if (id != 0x5d)
327 return -ENODEV;
328
7a1b76f2
JL
329 id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
330 switch (id) {
be7f5c4d 331 case 0x20:
f2f394db 332 strscpy(info->type, "emc1402", I2C_NAME_SIZE);
be7f5c4d 333 break;
7a1b76f2 334 case 0x21:
f2f394db 335 strscpy(info->type, "emc1403", I2C_NAME_SIZE);
7a1b76f2 336 break;
be7f5c4d 337 case 0x22:
f2f394db 338 strscpy(info->type, "emc1422", I2C_NAME_SIZE);
be7f5c4d 339 break;
7a1b76f2 340 case 0x23:
f2f394db 341 strscpy(info->type, "emc1423", I2C_NAME_SIZE);
7a1b76f2 342 break;
0011ddfe 343 case 0x25:
f2f394db 344 strscpy(info->type, "emc1404", I2C_NAME_SIZE);
0011ddfe
GR
345 break;
346 case 0x27:
f2f394db 347 strscpy(info->type, "emc1424", I2C_NAME_SIZE);
0011ddfe 348 break;
7a1b76f2 349 default:
dac6831e 350 return -ENODEV;
7a1b76f2 351 }
dac6831e
KT
352
353 id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
3a18e139 354 if (id < 0x01 || id > 0x04)
dac6831e
KT
355 return -ENODEV;
356
dac6831e
KT
357 return 0;
358}
359
4cab259f
GR
360static 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
034b44b4 382static const struct regmap_config emc1403_regmap_config = {
4cab259f
GR
383 .reg_bits = 8,
384 .val_bits = 8,
385 .cache_type = REGCACHE_RBTREE,
386 .volatile_reg = emc1403_regmap_is_volatile,
387};
388
67487038
SK
389static const struct i2c_device_id emc1403_idtable[];
390
391static int emc1403_probe(struct i2c_client *client)
dac6831e 392{
dac6831e 393 struct thermal_data *data;
454aee17 394 struct device *hwmon_dev;
67487038 395 const struct i2c_device_id *id = i2c_match_id(emc1403_idtable, client);
dac6831e 396
7b52eefe
GR
397 data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
398 GFP_KERNEL);
399 if (data == NULL)
dac6831e 400 return -ENOMEM;
dac6831e 401
4cab259f
GR
402 data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
403 if (IS_ERR(data->regmap))
404 return PTR_ERR(data->regmap);
405
dac6831e 406 mutex_init(&data->mutex);
dac6831e 407
be7f5c4d
JG
408 switch (id->driver_data) {
409 case emc1404:
410 data->groups[2] = &emc1404_group;
df561f66 411 fallthrough;
be7f5c4d
JG
412 case emc1403:
413 data->groups[1] = &emc1403_group;
df561f66 414 fallthrough;
be7f5c4d
JG
415 case emc1402:
416 data->groups[0] = &emc1402_group;
417 }
0011ddfe 418
03f49f64
GR
419 if (id->driver_data == emc1402)
420 data->groups[1] = &emc1402_alarm_group;
421
8759f904
JD
422 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
423 client->name, data,
424 data->groups);
454aee17
GR
425 if (IS_ERR(hwmon_dev))
426 return PTR_ERR(hwmon_dev);
dac6831e 427
0011ddfe 428 dev_info(&client->dev, "%s Thermal chip found\n", id->name);
dac6831e
KT
429 return 0;
430}
431
432static const unsigned short emc1403_address_list[] = {
be7f5c4d 433 0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
dac6831e
KT
434};
435
be7f5c4d 436/* Last digit of chip name indicates number of channels */
dac6831e 437static const struct i2c_device_id emc1403_idtable[] = {
be7f5c4d
JG
438 { "emc1402", emc1402 },
439 { "emc1403", emc1403 },
440 { "emc1404", emc1404 },
51585bef
GR
441 { "emc1412", emc1402 },
442 { "emc1413", emc1403 },
443 { "emc1414", emc1404 },
be7f5c4d
JG
444 { "emc1422", emc1402 },
445 { "emc1423", emc1403 },
446 { "emc1424", emc1404 },
dac6831e
KT
447 { }
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,
67487038 457 .probe_new = emc1403_probe,
dac6831e
KT
458 .id_table = emc1403_idtable,
459 .address_list = emc1403_address_list,
460};
461
f0967eea 462module_i2c_driver(sensor_emc1403);
dac6831e
KT
463
464MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
465MODULE_DESCRIPTION("emc1403 Thermal Driver");
466MODULE_LICENSE("GPL v2");