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
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>
32#include <linux/regmap.h>
33
34#define THERMAL_PID_REG 0xfd
35#define THERMAL_SMSC_ID_REG 0xfe
36#define THERMAL_REVISION_REG 0xff
37
38enum emc1403_chip { emc1402, emc1403, emc1404 };
39
40struct thermal_data {
41 struct regmap *regmap;
42 struct mutex mutex;
43 const struct attribute_group *groups[4];
44};
45
46static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
47 char *buf)
48{
49 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
50 struct thermal_data *data = dev_get_drvdata(dev);
51 unsigned int val;
52 int retval;
53
54 retval = regmap_read(data->regmap, sda->index, &val);
55 if (retval < 0)
56 return retval;
57 return sprintf(buf, "%d000\n", val);
58}
59
60static ssize_t bit_show(struct device *dev, struct device_attribute *attr,
61 char *buf)
62{
63 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
64 struct thermal_data *data = dev_get_drvdata(dev);
65 unsigned int val;
66 int retval;
67
68 retval = regmap_read(data->regmap, sda->nr, &val);
69 if (retval < 0)
70 return retval;
71 return sprintf(buf, "%d\n", !!(val & sda->index));
72}
73
74static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
75 const char *buf, size_t count)
76{
77 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
78 struct thermal_data *data = dev_get_drvdata(dev);
79 unsigned long val;
80 int retval;
81
82 if (kstrtoul(buf, 10, &val))
83 return -EINVAL;
84 retval = regmap_write(data->regmap, sda->index,
85 DIV_ROUND_CLOSEST(val, 1000));
86 if (retval < 0)
87 return retval;
88 return count;
89}
90
91static ssize_t bit_store(struct device *dev, struct device_attribute *attr,
92 const char *buf, size_t count)
93{
94 struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
95 struct thermal_data *data = dev_get_drvdata(dev);
96 unsigned long val;
97 int retval;
98
99 if (kstrtoul(buf, 10, &val))
100 return -EINVAL;
101
102 retval = regmap_update_bits(data->regmap, sda->nr, sda->index,
103 val ? sda->index : 0);
104 if (retval < 0)
105 return retval;
106 return count;
107}
108
109static ssize_t show_hyst_common(struct device *dev,
110 struct device_attribute *attr, char *buf,
111 bool is_min)
112{
113 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
114 struct thermal_data *data = dev_get_drvdata(dev);
115 struct regmap *regmap = data->regmap;
116 unsigned int limit;
117 unsigned int hyst;
118 int retval;
119
120 retval = regmap_read(regmap, sda->index, &limit);
121 if (retval < 0)
122 return retval;
123
124 retval = regmap_read(regmap, 0x21, &hyst);
125 if (retval < 0)
126 return retval;
127
128 return sprintf(buf, "%d000\n", is_min ? limit + hyst : limit - hyst);
129}
130
131static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
132 char *buf)
133{
134 return show_hyst_common(dev, attr, buf, false);
135}
136
137static ssize_t min_hyst_show(struct device *dev,
138 struct device_attribute *attr, char *buf)
139{
140 return show_hyst_common(dev, attr, buf, true);
141}
142
143static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
144 const char *buf, size_t count)
145{
146 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
147 struct thermal_data *data = dev_get_drvdata(dev);
148 struct regmap *regmap = data->regmap;
149 unsigned int limit;
150 int retval;
151 int hyst;
152 unsigned long val;
153
154 if (kstrtoul(buf, 10, &val))
155 return -EINVAL;
156
157 mutex_lock(&data->mutex);
158 retval = regmap_read(regmap, sda->index, &limit);
159 if (retval < 0)
160 goto fail;
161
162 hyst = limit * 1000 - val;
163 hyst = clamp_val(DIV_ROUND_CLOSEST(hyst, 1000), 0, 255);
164 retval = regmap_write(regmap, 0x21, hyst);
165 if (retval == 0)
166 retval = count;
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
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);
224
225static struct attribute *emc1402_attrs[] = {
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,
230 &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
231 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
232 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
233
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,
238 &sensor_dev_attr_temp2_min_hyst.dev_attr.attr,
239 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
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
255 &sensor_dev_attr_temp2_fault.dev_attr.attr,
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,
259
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,
264 &sensor_dev_attr_temp3_fault.dev_attr.attr,
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,
268 &sensor_dev_attr_temp3_min_hyst.dev_attr.attr,
269 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
270 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
271 NULL
272};
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,
283 &sensor_dev_attr_temp4_fault.dev_attr.attr,
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,
287 &sensor_dev_attr_temp4_min_hyst.dev_attr.attr,
288 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
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};
296
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[] = {
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),
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
330static int emc1403_detect(struct i2c_client *client,
331 struct i2c_board_info *info)
332{
333 int id;
334 /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
335
336 id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
337 if (id != 0x5d)
338 return -ENODEV;
339
340 id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
341 switch (id) {
342 case 0x20:
343 strlcpy(info->type, "emc1402", I2C_NAME_SIZE);
344 break;
345 case 0x21:
346 strlcpy(info->type, "emc1403", I2C_NAME_SIZE);
347 break;
348 case 0x22:
349 strlcpy(info->type, "emc1422", I2C_NAME_SIZE);
350 break;
351 case 0x23:
352 strlcpy(info->type, "emc1423", I2C_NAME_SIZE);
353 break;
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;
360 default:
361 return -ENODEV;
362 }
363
364 id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
365 if (id < 0x01 || id > 0x04)
366 return -ENODEV;
367
368 return 0;
369}
370
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
393static const struct regmap_config emc1403_regmap_config = {
394 .reg_bits = 8,
395 .val_bits = 8,
396 .cache_type = REGCACHE_RBTREE,
397 .volatile_reg = emc1403_regmap_is_volatile,
398};
399
400static int emc1403_probe(struct i2c_client *client,
401 const struct i2c_device_id *id)
402{
403 struct thermal_data *data;
404 struct device *hwmon_dev;
405
406 data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
407 GFP_KERNEL);
408 if (data == NULL)
409 return -ENOMEM;
410
411 data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
412 if (IS_ERR(data->regmap))
413 return PTR_ERR(data->regmap);
414
415 mutex_init(&data->mutex);
416
417 switch (id->driver_data) {
418 case emc1404:
419 data->groups[2] = &emc1404_group;
420 /* fall through */
421 case emc1403:
422 data->groups[1] = &emc1403_group;
423 /* fall through */
424 case emc1402:
425 data->groups[0] = &emc1402_group;
426 }
427
428 if (id->driver_data == emc1402)
429 data->groups[1] = &emc1402_alarm_group;
430
431 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
432 client->name, data,
433 data->groups);
434 if (IS_ERR(hwmon_dev))
435 return PTR_ERR(hwmon_dev);
436
437 dev_info(&client->dev, "%s Thermal chip found\n", id->name);
438 return 0;
439}
440
441static const unsigned short emc1403_address_list[] = {
442 0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
443};
444
445/* Last digit of chip name indicates number of channels */
446static const struct i2c_device_id emc1403_idtable[] = {
447 { "emc1402", emc1402 },
448 { "emc1403", emc1403 },
449 { "emc1404", emc1404 },
450 { "emc1412", emc1402 },
451 { "emc1413", emc1403 },
452 { "emc1414", emc1404 },
453 { "emc1422", emc1402 },
454 { "emc1423", emc1403 },
455 { "emc1424", emc1404 },
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,
467 .id_table = emc1403_idtable,
468 .address_list = emc1403_address_list,
469};
470
471module_i2c_driver(sensor_emc1403);
472
473MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
474MODULE_DESCRIPTION("emc1403 Thermal Driver");
475MODULE_LICENSE("GPL v2");
476