Merge tag 'kbuild-v5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy...
[linux-2.6-block.git] / drivers / hwmon / emc1403.c
CommitLineData
dac6831e
KT
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dac6831e
KT
21 */
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/i2c.h>
27#include <linux/hwmon.h>
28#include <linux/hwmon-sysfs.h>
29#include <linux/err.h>
30#include <linux/sysfs.h>
31#include <linux/mutex.h>
4cab259f 32#include <linux/regmap.h>
dac6831e
KT
33
34#define THERMAL_PID_REG 0xfd
35#define THERMAL_SMSC_ID_REG 0xfe
36#define THERMAL_REVISION_REG 0xff
37
be7f5c4d
JG
38enum emc1403_chip { emc1402, emc1403, emc1404 };
39
dac6831e 40struct thermal_data {
4cab259f 41 struct regmap *regmap;
dac6831e 42 struct mutex mutex;
4cab259f 43 const struct attribute_group *groups[4];
dac6831e
KT
44};
45
ae66d2d9
GR
46static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
47 char *buf)
dac6831e 48{
dac6831e 49 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
454aee17 50 struct thermal_data *data = dev_get_drvdata(dev);
4cab259f 51 unsigned int val;
454aee17 52 int retval;
dac6831e 53
4cab259f 54 retval = regmap_read(data->regmap, sda->index, &val);
dac6831e
KT
55 if (retval < 0)
56 return retval;
4cab259f 57 return sprintf(buf, "%d000\n", val);
dac6831e
KT
58}
59
ae66d2d9
GR
60static ssize_t bit_show(struct device *dev, struct device_attribute *attr,
61 char *buf)
dac6831e 62{
dac6831e 63 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
454aee17 64 struct thermal_data *data = dev_get_drvdata(dev);
4cab259f 65 unsigned int val;
454aee17 66 int retval;
dac6831e 67
4cab259f 68 retval = regmap_read(data->regmap, sda->nr, &val);
dac6831e
KT
69 if (retval < 0)
70 return retval;
4cab259f 71 return sprintf(buf, "%d\n", !!(val & sda->index));
dac6831e
KT
72}
73
ae66d2d9
GR
74static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
75 const char *buf, size_t count)
dac6831e
KT
76{
77 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
454aee17 78 struct thermal_data *data = dev_get_drvdata(dev);
dac6831e
KT
79 unsigned long val;
80 int retval;
81
179c4fdb 82 if (kstrtoul(buf, 10, &val))
dac6831e 83 return -EINVAL;
4cab259f
GR
84 retval = regmap_write(data->regmap, sda->index,
85 DIV_ROUND_CLOSEST(val, 1000));
dac6831e
KT
86 if (retval < 0)
87 return retval;
88 return count;
89}
90
ae66d2d9
GR
91static ssize_t bit_store(struct device *dev, struct device_attribute *attr,
92 const char *buf, size_t count)
960f12f4 93{
960f12f4 94 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
454aee17 95 struct thermal_data *data = dev_get_drvdata(dev);
960f12f4
AC
96 unsigned long val;
97 int retval;
98
179c4fdb 99 if (kstrtoul(buf, 10, &val))
960f12f4
AC
100 return -EINVAL;
101
4cab259f
GR
102 retval = regmap_update_bits(data->regmap, sda->nr, sda->index,
103 val ? sda->index : 0);
960f12f4 104 if (retval < 0)
4cab259f
GR
105 return retval;
106 return count;
960f12f4
AC
107}
108
54392ce4
GR
109static ssize_t show_hyst_common(struct device *dev,
110 struct device_attribute *attr, char *buf,
111 bool is_min)
dac6831e 112{
dac6831e 113 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
454aee17 114 struct thermal_data *data = dev_get_drvdata(dev);
4cab259f
GR
115 struct regmap *regmap = data->regmap;
116 unsigned int limit;
117 unsigned int hyst;
dac6831e 118 int retval;
dac6831e 119
4cab259f 120 retval = regmap_read(regmap, sda->index, &limit);
dac6831e
KT
121 if (retval < 0)
122 return retval;
123
4cab259f
GR
124 retval = regmap_read(regmap, 0x21, &hyst);
125 if (retval < 0)
126 return retval;
127
54392ce4
GR
128 return sprintf(buf, "%d000\n", is_min ? limit + hyst : limit - hyst);
129}
130
ae66d2d9
GR
131static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
132 char *buf)
54392ce4
GR
133{
134 return show_hyst_common(dev, attr, buf, false);
135}
136
ae66d2d9 137static ssize_t min_hyst_show(struct device *dev,
54392ce4
GR
138 struct device_attribute *attr, char *buf)
139{
140 return show_hyst_common(dev, attr, buf, true);
dac6831e
KT
141}
142
ae66d2d9
GR
143static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
144 const char *buf, size_t count)
dac6831e 145{
dac6831e 146 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
454aee17 147 struct thermal_data *data = dev_get_drvdata(dev);
4cab259f
GR
148 struct regmap *regmap = data->regmap;
149 unsigned int limit;
dac6831e
KT
150 int retval;
151 int hyst;
152 unsigned long val;
153
179c4fdb 154 if (kstrtoul(buf, 10, &val))
dac6831e
KT
155 return -EINVAL;
156
157 mutex_lock(&data->mutex);
4cab259f 158 retval = regmap_read(regmap, sda->index, &limit);
dac6831e
KT
159 if (retval < 0)
160 goto fail;
161
4cab259f 162 hyst = limit * 1000 - val;
ceeaa70c 163 hyst = clamp_val(DIV_ROUND_CLOSEST(hyst, 1000), 0, 255);
4cab259f
GR
164 retval = regmap_write(regmap, 0x21, hyst);
165 if (retval == 0)
dac6831e 166 retval = count;
dac6831e
KT
167fail:
168 mutex_unlock(&data->mutex);
169 return retval;
170}
171
172/*
173 * Sensors. We pass the actual i2c register to the methods.
174 */
175
ae66d2d9
GR
176static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 0x06);
177static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 0x05);
178static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, 0x20);
179static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0x00);
180static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, bit, 0x36, 0x01);
181static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, bit, 0x35, 0x01);
182static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, bit, 0x37, 0x01);
183static SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, min_hyst, 0x06);
184static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, hyst, 0x05);
185static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0x20);
186
187static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, 0x08);
188static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, 0x07);
189static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, 0x19);
190static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 0x01);
191static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, bit, 0x1b, 0x02);
192static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, bit, 0x36, 0x02);
193static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, bit, 0x35, 0x02);
194static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, bit, 0x37, 0x02);
195static SENSOR_DEVICE_ATTR_RO(temp2_min_hyst, min_hyst, 0x08);
196static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, hyst, 0x07);
197static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, hyst, 0x19);
198
199static SENSOR_DEVICE_ATTR_RW(temp3_min, temp, 0x16);
200static SENSOR_DEVICE_ATTR_RW(temp3_max, temp, 0x15);
201static SENSOR_DEVICE_ATTR_RW(temp3_crit, temp, 0x1A);
202static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 0x23);
203static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, bit, 0x1b, 0x04);
204static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, bit, 0x36, 0x04);
205static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, bit, 0x35, 0x04);
206static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, bit, 0x37, 0x04);
207static SENSOR_DEVICE_ATTR_RO(temp3_min_hyst, min_hyst, 0x16);
208static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, hyst, 0x15);
209static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, hyst, 0x1A);
210
211static SENSOR_DEVICE_ATTR_RW(temp4_min, temp, 0x2D);
212static SENSOR_DEVICE_ATTR_RW(temp4_max, temp, 0x2C);
213static SENSOR_DEVICE_ATTR_RW(temp4_crit, temp, 0x30);
214static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 0x2A);
215static SENSOR_DEVICE_ATTR_2_RO(temp4_fault, bit, 0x1b, 0x08);
216static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, bit, 0x36, 0x08);
217static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, bit, 0x35, 0x08);
218static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, bit, 0x37, 0x08);
219static SENSOR_DEVICE_ATTR_RO(temp4_min_hyst, min_hyst, 0x2D);
220static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, hyst, 0x2C);
221static SENSOR_DEVICE_ATTR_RO(temp4_crit_hyst, hyst, 0x30);
222
223static SENSOR_DEVICE_ATTR_2_RW(power_state, bit, 0x03, 0x40);
960f12f4 224
be7f5c4d 225static struct attribute *emc1402_attrs[] = {
dac6831e
KT
226 &sensor_dev_attr_temp1_min.dev_attr.attr,
227 &sensor_dev_attr_temp1_max.dev_attr.attr,
228 &sensor_dev_attr_temp1_crit.dev_attr.attr,
229 &sensor_dev_attr_temp1_input.dev_attr.attr,
54392ce4 230 &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
a9a74006 231 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
dac6831e 232 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
be7f5c4d 233
dac6831e
KT
234 &sensor_dev_attr_temp2_min.dev_attr.attr,
235 &sensor_dev_attr_temp2_max.dev_attr.attr,
236 &sensor_dev_attr_temp2_crit.dev_attr.attr,
237 &sensor_dev_attr_temp2_input.dev_attr.attr,
54392ce4 238 &sensor_dev_attr_temp2_min_hyst.dev_attr.attr,
a9a74006 239 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
be7f5c4d
JG
240 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
241
242 &sensor_dev_attr_power_state.dev_attr.attr,
243 NULL
244};
245
246static const struct attribute_group emc1402_group = {
247 .attrs = emc1402_attrs,
248};
249
250static struct attribute *emc1403_attrs[] = {
251 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
252 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
253 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
254
d8850c19 255 &sensor_dev_attr_temp2_fault.dev_attr.attr,
dac6831e
KT
256 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
257 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
258 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
be7f5c4d 259
dac6831e
KT
260 &sensor_dev_attr_temp3_min.dev_attr.attr,
261 &sensor_dev_attr_temp3_max.dev_attr.attr,
262 &sensor_dev_attr_temp3_crit.dev_attr.attr,
263 &sensor_dev_attr_temp3_input.dev_attr.attr,
d8850c19 264 &sensor_dev_attr_temp3_fault.dev_attr.attr,
dac6831e
KT
265 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
266 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
267 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
54392ce4 268 &sensor_dev_attr_temp3_min_hyst.dev_attr.attr,
a9a74006 269 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
dac6831e
KT
270 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
271 NULL
272};
0011ddfe
GR
273
274static const struct attribute_group emc1403_group = {
275 .attrs = emc1403_attrs,
276};
277
278static struct attribute *emc1404_attrs[] = {
279 &sensor_dev_attr_temp4_min.dev_attr.attr,
280 &sensor_dev_attr_temp4_max.dev_attr.attr,
281 &sensor_dev_attr_temp4_crit.dev_attr.attr,
282 &sensor_dev_attr_temp4_input.dev_attr.attr,
d8850c19 283 &sensor_dev_attr_temp4_fault.dev_attr.attr,
0011ddfe
GR
284 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
285 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
286 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
54392ce4 287 &sensor_dev_attr_temp4_min_hyst.dev_attr.attr,
a9a74006 288 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
0011ddfe
GR
289 &sensor_dev_attr_temp4_crit_hyst.dev_attr.attr,
290 NULL
291};
292
293static const struct attribute_group emc1404_group = {
294 .attrs = emc1404_attrs,
295};
dac6831e 296
03f49f64
GR
297/*
298 * EMC14x2 uses a different register and different bits to report alarm and
299 * fault status. For simplicity, provide a separate attribute group for this
300 * chip series.
301 * Since we can not re-use the same attribute names, create a separate attribute
302 * array.
303 */
304static struct sensor_device_attribute_2 emc1402_alarms[] = {
ae66d2d9
GR
305 SENSOR_ATTR_2_RO(temp1_min_alarm, bit, 0x02, 0x20),
306 SENSOR_ATTR_2_RO(temp1_max_alarm, bit, 0x02, 0x40),
307 SENSOR_ATTR_2_RO(temp1_crit_alarm, bit, 0x02, 0x01),
308
309 SENSOR_ATTR_2_RO(temp2_fault, bit, 0x02, 0x04),
310 SENSOR_ATTR_2_RO(temp2_min_alarm, bit, 0x02, 0x08),
311 SENSOR_ATTR_2_RO(temp2_max_alarm, bit, 0x02, 0x10),
312 SENSOR_ATTR_2_RO(temp2_crit_alarm, bit, 0x02, 0x02),
03f49f64
GR
313};
314
315static struct attribute *emc1402_alarm_attrs[] = {
316 &emc1402_alarms[0].dev_attr.attr,
317 &emc1402_alarms[1].dev_attr.attr,
318 &emc1402_alarms[2].dev_attr.attr,
319 &emc1402_alarms[3].dev_attr.attr,
320 &emc1402_alarms[4].dev_attr.attr,
321 &emc1402_alarms[5].dev_attr.attr,
322 &emc1402_alarms[6].dev_attr.attr,
323 NULL,
324};
325
326static const struct attribute_group emc1402_alarm_group = {
327 .attrs = emc1402_alarm_attrs,
328};
329
dac6831e
KT
330static int emc1403_detect(struct i2c_client *client,
331 struct i2c_board_info *info)
332{
333 int id;
7a1b76f2 334 /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
dac6831e
KT
335
336 id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
337 if (id != 0x5d)
338 return -ENODEV;
339
7a1b76f2
JL
340 id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
341 switch (id) {
be7f5c4d
JG
342 case 0x20:
343 strlcpy(info->type, "emc1402", I2C_NAME_SIZE);
344 break;
7a1b76f2
JL
345 case 0x21:
346 strlcpy(info->type, "emc1403", I2C_NAME_SIZE);
347 break;
be7f5c4d
JG
348 case 0x22:
349 strlcpy(info->type, "emc1422", I2C_NAME_SIZE);
350 break;
7a1b76f2
JL
351 case 0x23:
352 strlcpy(info->type, "emc1423", I2C_NAME_SIZE);
353 break;
0011ddfe
GR
354 case 0x25:
355 strlcpy(info->type, "emc1404", I2C_NAME_SIZE);
356 break;
357 case 0x27:
358 strlcpy(info->type, "emc1424", I2C_NAME_SIZE);
359 break;
7a1b76f2 360 default:
dac6831e 361 return -ENODEV;
7a1b76f2 362 }
dac6831e
KT
363
364 id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
3a18e139 365 if (id < 0x01 || id > 0x04)
dac6831e
KT
366 return -ENODEV;
367
dac6831e
KT
368 return 0;
369}
370
4cab259f
GR
371static bool emc1403_regmap_is_volatile(struct device *dev, unsigned int reg)
372{
373 switch (reg) {
374 case 0x00: /* internal diode high byte */
375 case 0x01: /* external diode 1 high byte */
376 case 0x02: /* status */
377 case 0x10: /* external diode 1 low byte */
378 case 0x1b: /* external diode fault */
379 case 0x23: /* external diode 2 high byte */
380 case 0x24: /* external diode 2 low byte */
381 case 0x29: /* internal diode low byte */
382 case 0x2a: /* externl diode 3 high byte */
383 case 0x2b: /* external diode 3 low byte */
384 case 0x35: /* high limit status */
385 case 0x36: /* low limit status */
386 case 0x37: /* therm limit status */
387 return true;
388 default:
389 return false;
390 }
391}
392
034b44b4 393static const struct regmap_config emc1403_regmap_config = {
4cab259f
GR
394 .reg_bits = 8,
395 .val_bits = 8,
396 .cache_type = REGCACHE_RBTREE,
397 .volatile_reg = emc1403_regmap_is_volatile,
398};
399
dac6831e
KT
400static int emc1403_probe(struct i2c_client *client,
401 const struct i2c_device_id *id)
402{
dac6831e 403 struct thermal_data *data;
454aee17 404 struct device *hwmon_dev;
dac6831e 405
7b52eefe
GR
406 data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
407 GFP_KERNEL);
408 if (data == NULL)
dac6831e 409 return -ENOMEM;
dac6831e 410
4cab259f
GR
411 data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
412 if (IS_ERR(data->regmap))
413 return PTR_ERR(data->regmap);
414
dac6831e 415 mutex_init(&data->mutex);
dac6831e 416
be7f5c4d
JG
417 switch (id->driver_data) {
418 case emc1404:
419 data->groups[2] = &emc1404_group;
ffb32432 420 /* fall through */
be7f5c4d
JG
421 case emc1403:
422 data->groups[1] = &emc1403_group;
ffb32432 423 /* fall through */
be7f5c4d
JG
424 case emc1402:
425 data->groups[0] = &emc1402_group;
426 }
0011ddfe 427
03f49f64
GR
428 if (id->driver_data == emc1402)
429 data->groups[1] = &emc1402_alarm_group;
430
8759f904
JD
431 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
432 client->name, data,
433 data->groups);
454aee17
GR
434 if (IS_ERR(hwmon_dev))
435 return PTR_ERR(hwmon_dev);
dac6831e 436
0011ddfe 437 dev_info(&client->dev, "%s Thermal chip found\n", id->name);
dac6831e
KT
438 return 0;
439}
440
441static const unsigned short emc1403_address_list[] = {
be7f5c4d 442 0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
dac6831e
KT
443};
444
be7f5c4d 445/* Last digit of chip name indicates number of channels */
dac6831e 446static const struct i2c_device_id emc1403_idtable[] = {
be7f5c4d
JG
447 { "emc1402", emc1402 },
448 { "emc1403", emc1403 },
449 { "emc1404", emc1404 },
51585bef
GR
450 { "emc1412", emc1402 },
451 { "emc1413", emc1403 },
452 { "emc1414", emc1404 },
be7f5c4d
JG
453 { "emc1422", emc1402 },
454 { "emc1423", emc1403 },
455 { "emc1424", emc1404 },
dac6831e
KT
456 { }
457};
458MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
459
460static struct i2c_driver sensor_emc1403 = {
461 .class = I2C_CLASS_HWMON,
462 .driver = {
463 .name = "emc1403",
464 },
465 .detect = emc1403_detect,
466 .probe = emc1403_probe,
dac6831e
KT
467 .id_table = emc1403_idtable,
468 .address_list = emc1403_address_list,
469};
470
f0967eea 471module_i2c_driver(sensor_emc1403);
dac6831e
KT
472
473MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
474MODULE_DESCRIPTION("emc1403 Thermal Driver");
475MODULE_LICENSE("GPL v2");