Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / hwmon / emc1403.c
blob314838272049940109a9fed32f69aa9e4517795b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * emc1403.c - SMSC Thermal Driver
5 * Copyright (C) 2008 Intel Corp
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
23 #define THERMAL_PID_REG 0xfd
24 #define THERMAL_SMSC_ID_REG 0xfe
25 #define THERMAL_REVISION_REG 0xff
27 enum emc1403_chip { emc1402, emc1403, emc1404 };
29 struct thermal_data {
30 struct regmap *regmap;
31 struct mutex mutex;
32 const struct attribute_group *groups[4];
35 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
36 char *buf)
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;
43 retval = regmap_read(data->regmap, sda->index, &val);
44 if (retval < 0)
45 return retval;
46 return sprintf(buf, "%d000\n", val);
49 static ssize_t bit_show(struct device *dev, struct device_attribute *attr,
50 char *buf)
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;
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));
63 static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
64 const char *buf, size_t count)
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;
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;
80 static ssize_t bit_store(struct device *dev, struct device_attribute *attr,
81 const char *buf, size_t count)
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;
88 if (kstrtoul(buf, 10, &val))
89 return -EINVAL;
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;
98 static ssize_t show_hyst_common(struct device *dev,
99 struct device_attribute *attr, char *buf,
100 bool is_min)
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;
109 retval = regmap_read(regmap, sda->index, &limit);
110 if (retval < 0)
111 return retval;
113 retval = regmap_read(regmap, 0x21, &hyst);
114 if (retval < 0)
115 return retval;
117 return sprintf(buf, "%d000\n", is_min ? limit + hyst : limit - hyst);
120 static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
121 char *buf)
123 return show_hyst_common(dev, attr, buf, false);
126 static ssize_t min_hyst_show(struct device *dev,
127 struct device_attribute *attr, char *buf)
129 return show_hyst_common(dev, attr, buf, true);
132 static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
133 const char *buf, size_t count)
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;
143 if (kstrtoul(buf, 10, &val))
144 return -EINVAL;
146 mutex_lock(&data->mutex);
147 retval = regmap_read(regmap, sda->index, &limit);
148 if (retval < 0)
149 goto fail;
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;
156 fail:
157 mutex_unlock(&data->mutex);
158 return retval;
162 * Sensors. We pass the actual i2c register to the methods.
165 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 0x06);
166 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 0x05);
167 static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, 0x20);
168 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0x00);
169 static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, bit, 0x36, 0x01);
170 static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, bit, 0x35, 0x01);
171 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, bit, 0x37, 0x01);
172 static SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, min_hyst, 0x06);
173 static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, hyst, 0x05);
174 static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0x20);
176 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, 0x08);
177 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, 0x07);
178 static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, 0x19);
179 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 0x01);
180 static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, bit, 0x1b, 0x02);
181 static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, bit, 0x36, 0x02);
182 static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, bit, 0x35, 0x02);
183 static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, bit, 0x37, 0x02);
184 static SENSOR_DEVICE_ATTR_RO(temp2_min_hyst, min_hyst, 0x08);
185 static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, hyst, 0x07);
186 static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, hyst, 0x19);
188 static SENSOR_DEVICE_ATTR_RW(temp3_min, temp, 0x16);
189 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp, 0x15);
190 static SENSOR_DEVICE_ATTR_RW(temp3_crit, temp, 0x1A);
191 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 0x23);
192 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, bit, 0x1b, 0x04);
193 static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, bit, 0x36, 0x04);
194 static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, bit, 0x35, 0x04);
195 static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, bit, 0x37, 0x04);
196 static SENSOR_DEVICE_ATTR_RO(temp3_min_hyst, min_hyst, 0x16);
197 static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, hyst, 0x15);
198 static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, hyst, 0x1A);
200 static SENSOR_DEVICE_ATTR_RW(temp4_min, temp, 0x2D);
201 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp, 0x2C);
202 static SENSOR_DEVICE_ATTR_RW(temp4_crit, temp, 0x30);
203 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 0x2A);
204 static SENSOR_DEVICE_ATTR_2_RO(temp4_fault, bit, 0x1b, 0x08);
205 static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, bit, 0x36, 0x08);
206 static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, bit, 0x35, 0x08);
207 static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, bit, 0x37, 0x08);
208 static SENSOR_DEVICE_ATTR_RO(temp4_min_hyst, min_hyst, 0x2D);
209 static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, hyst, 0x2C);
210 static SENSOR_DEVICE_ATTR_RO(temp4_crit_hyst, hyst, 0x30);
212 static SENSOR_DEVICE_ATTR_2_RW(power_state, bit, 0x03, 0x40);
214 static 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,
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,
231 &sensor_dev_attr_power_state.dev_attr.attr,
232 NULL
235 static const struct attribute_group emc1402_group = {
236 .attrs = emc1402_attrs,
239 static 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,
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,
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
263 static const struct attribute_group emc1403_group = {
264 .attrs = emc1403_attrs,
267 static 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
282 static const struct attribute_group emc1404_group = {
283 .attrs = emc1404_attrs,
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.
293 static 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),
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),
304 static 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,
315 static const struct attribute_group emc1402_alarm_group = {
316 .attrs = emc1402_alarm_attrs,
319 static int emc1403_detect(struct i2c_client *client,
320 struct i2c_board_info *info)
322 int id;
323 /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
325 id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
326 if (id != 0x5d)
327 return -ENODEV;
329 id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
330 switch (id) {
331 case 0x20:
332 strlcpy(info->type, "emc1402", I2C_NAME_SIZE);
333 break;
334 case 0x21:
335 strlcpy(info->type, "emc1403", I2C_NAME_SIZE);
336 break;
337 case 0x22:
338 strlcpy(info->type, "emc1422", I2C_NAME_SIZE);
339 break;
340 case 0x23:
341 strlcpy(info->type, "emc1423", I2C_NAME_SIZE);
342 break;
343 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;
349 default:
350 return -ENODEV;
353 id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
354 if (id < 0x01 || id > 0x04)
355 return -ENODEV;
357 return 0;
360 static bool emc1403_regmap_is_volatile(struct device *dev, unsigned int reg)
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;
382 static const struct regmap_config emc1403_regmap_config = {
383 .reg_bits = 8,
384 .val_bits = 8,
385 .cache_type = REGCACHE_RBTREE,
386 .volatile_reg = emc1403_regmap_is_volatile,
389 static const struct i2c_device_id emc1403_idtable[];
391 static int emc1403_probe(struct i2c_client *client)
393 struct thermal_data *data;
394 struct device *hwmon_dev;
395 const struct i2c_device_id *id = i2c_match_id(emc1403_idtable, client);
397 data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
398 GFP_KERNEL);
399 if (data == NULL)
400 return -ENOMEM;
402 data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
403 if (IS_ERR(data->regmap))
404 return PTR_ERR(data->regmap);
406 mutex_init(&data->mutex);
408 switch (id->driver_data) {
409 case emc1404:
410 data->groups[2] = &emc1404_group;
411 fallthrough;
412 case emc1403:
413 data->groups[1] = &emc1403_group;
414 fallthrough;
415 case emc1402:
416 data->groups[0] = &emc1402_group;
419 if (id->driver_data == emc1402)
420 data->groups[1] = &emc1402_alarm_group;
422 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
423 client->name, data,
424 data->groups);
425 if (IS_ERR(hwmon_dev))
426 return PTR_ERR(hwmon_dev);
428 dev_info(&client->dev, "%s Thermal chip found\n", id->name);
429 return 0;
432 static const unsigned short emc1403_address_list[] = {
433 0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
436 /* Last digit of chip name indicates number of channels */
437 static const struct i2c_device_id emc1403_idtable[] = {
438 { "emc1402", emc1402 },
439 { "emc1403", emc1403 },
440 { "emc1404", emc1404 },
441 { "emc1412", emc1402 },
442 { "emc1413", emc1403 },
443 { "emc1414", emc1404 },
444 { "emc1422", emc1402 },
445 { "emc1423", emc1403 },
446 { "emc1424", emc1404 },
449 MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
451 static struct i2c_driver sensor_emc1403 = {
452 .class = I2C_CLASS_HWMON,
453 .driver = {
454 .name = "emc1403",
456 .detect = emc1403_detect,
457 .probe_new = emc1403_probe,
458 .id_table = emc1403_idtable,
459 .address_list = emc1403_address_list,
462 module_i2c_driver(sensor_emc1403);
464 MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
465 MODULE_DESCRIPTION("emc1403 Thermal Driver");
466 MODULE_LICENSE("GPL v2");