blob: 5ffd81f19d01c91f6416b273d5192436e2c62448 [file] [log] [blame]
Guenter Roeckb4c9c1a2014-01-24 22:25:33 -08001/*
2 * Driver for TI ADC128D818 System Monitor with Temperature Sensor
3 *
4 * Copyright (c) 2014 Guenter Roeck
5 *
6 * Derived from lm80.c
7 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
8 * and Philip Edelbrock <phil@netroedge.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/jiffies.h>
24#include <linux/i2c.h>
25#include <linux/hwmon.h>
26#include <linux/hwmon-sysfs.h>
27#include <linux/err.h>
28#include <linux/regulator/consumer.h>
29#include <linux/mutex.h>
30
31/* Addresses to scan
32 * The chip also supports addresses 0x35..0x37. Don't scan those addresses
33 * since they are also used by some EEPROMs, which may result in false
34 * positives.
35 */
36static const unsigned short normal_i2c[] = {
37 0x1d, 0x1e, 0x1f, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
38
39/* registers */
40#define ADC128_REG_IN_MAX(nr) (0x2a + (nr) * 2)
41#define ADC128_REG_IN_MIN(nr) (0x2b + (nr) * 2)
42#define ADC128_REG_IN(nr) (0x20 + (nr))
43
44#define ADC128_REG_TEMP 0x27
45#define ADC128_REG_TEMP_MAX 0x38
46#define ADC128_REG_TEMP_HYST 0x39
47
48#define ADC128_REG_CONFIG 0x00
49#define ADC128_REG_ALARM 0x01
50#define ADC128_REG_MASK 0x03
51#define ADC128_REG_CONV_RATE 0x07
52#define ADC128_REG_ONESHOT 0x09
53#define ADC128_REG_SHUTDOWN 0x0a
54#define ADC128_REG_CONFIG_ADV 0x0b
55#define ADC128_REG_BUSY_STATUS 0x0c
56
57#define ADC128_REG_MAN_ID 0x3e
58#define ADC128_REG_DEV_ID 0x3f
59
60struct adc128_data {
61 struct i2c_client *client;
62 struct regulator *regulator;
63 int vref; /* Reference voltage in mV */
64 struct mutex update_lock;
65 bool valid; /* true if following fields are valid */
66 unsigned long last_updated; /* In jiffies */
67
68 u16 in[3][7]; /* Register value, normalized to 12 bit
69 * 0: input voltage
70 * 1: min limit
71 * 2: max limit
72 */
73 s16 temp[3]; /* Register value, normalized to 9 bit
74 * 0: sensor 1: limit 2: hyst
75 */
76 u8 alarms; /* alarm register value */
77};
78
79static struct adc128_data *adc128_update_device(struct device *dev)
80{
81 struct adc128_data *data = dev_get_drvdata(dev);
82 struct i2c_client *client = data->client;
83 struct adc128_data *ret = data;
84 int i, rv;
85
86 mutex_lock(&data->update_lock);
87
88 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
89 for (i = 0; i < 7; i++) {
90 rv = i2c_smbus_read_word_swapped(client,
91 ADC128_REG_IN(i));
92 if (rv < 0)
93 goto abort;
94 data->in[0][i] = rv >> 4;
95
96 rv = i2c_smbus_read_byte_data(client,
97 ADC128_REG_IN_MIN(i));
98 if (rv < 0)
99 goto abort;
100 data->in[1][i] = rv << 4;
101
102 rv = i2c_smbus_read_byte_data(client,
103 ADC128_REG_IN_MAX(i));
104 if (rv < 0)
105 goto abort;
106 data->in[2][i] = rv << 4;
107 }
108
109 rv = i2c_smbus_read_word_swapped(client, ADC128_REG_TEMP);
110 if (rv < 0)
111 goto abort;
112 data->temp[0] = rv >> 7;
113
114 rv = i2c_smbus_read_byte_data(client, ADC128_REG_TEMP_MAX);
115 if (rv < 0)
116 goto abort;
117 data->temp[1] = rv << 1;
118
119 rv = i2c_smbus_read_byte_data(client, ADC128_REG_TEMP_HYST);
120 if (rv < 0)
121 goto abort;
122 data->temp[2] = rv << 1;
123
124 rv = i2c_smbus_read_byte_data(client, ADC128_REG_ALARM);
125 if (rv < 0)
126 goto abort;
127 data->alarms |= rv;
128
129 data->last_updated = jiffies;
130 data->valid = true;
131 }
132 goto done;
133
134abort:
135 ret = ERR_PTR(rv);
136 data->valid = false;
137done:
138 mutex_unlock(&data->update_lock);
139 return ret;
140}
141
142static ssize_t adc128_show_in(struct device *dev, struct device_attribute *attr,
143 char *buf)
144{
145 struct adc128_data *data = adc128_update_device(dev);
146 int index = to_sensor_dev_attr_2(attr)->index;
147 int nr = to_sensor_dev_attr_2(attr)->nr;
148 int val;
149
150 if (IS_ERR(data))
151 return PTR_ERR(data);
152
153 val = DIV_ROUND_CLOSEST(data->in[index][nr] * data->vref, 4095);
154 return sprintf(buf, "%d\n", val);
155}
156
157static ssize_t adc128_set_in(struct device *dev, struct device_attribute *attr,
158 const char *buf, size_t count)
159{
160 struct adc128_data *data = dev_get_drvdata(dev);
161 int index = to_sensor_dev_attr_2(attr)->index;
162 int nr = to_sensor_dev_attr_2(attr)->nr;
163 u8 reg, regval;
164 long val;
165 int err;
166
167 err = kstrtol(buf, 10, &val);
168 if (err < 0)
169 return err;
170
171 mutex_lock(&data->update_lock);
172 /* 10 mV LSB on limit registers */
173 regval = clamp_val(DIV_ROUND_CLOSEST(val, 10), 0, 255);
174 data->in[index][nr] = regval << 4;
175 reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr);
176 i2c_smbus_write_byte_data(data->client, reg, regval);
177 mutex_unlock(&data->update_lock);
178
179 return count;
180}
181
182static ssize_t adc128_show_temp(struct device *dev,
183 struct device_attribute *attr, char *buf)
184{
185 struct adc128_data *data = adc128_update_device(dev);
186 int index = to_sensor_dev_attr(attr)->index;
187 int temp;
188
189 if (IS_ERR(data))
190 return PTR_ERR(data);
191
192 temp = (data->temp[index] << 7) >> 7; /* sign extend */
193 return sprintf(buf, "%d\n", temp * 500);/* 0.5 degrees C resolution */
194}
195
196static ssize_t adc128_set_temp(struct device *dev,
197 struct device_attribute *attr,
198 const char *buf, size_t count)
199{
200 struct adc128_data *data = dev_get_drvdata(dev);
201 int index = to_sensor_dev_attr(attr)->index;
202 long val;
203 int err;
204 s8 regval;
205
206 err = kstrtol(buf, 10, &val);
207 if (err < 0)
208 return err;
209
210 mutex_lock(&data->update_lock);
211 regval = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
212 data->temp[index] = regval << 1;
213 i2c_smbus_write_byte_data(data->client,
214 index == 1 ? ADC128_REG_TEMP_MAX
215 : ADC128_REG_TEMP_HYST,
216 regval);
217 mutex_unlock(&data->update_lock);
218
219 return count;
220}
221
222static ssize_t adc128_show_alarm(struct device *dev,
223 struct device_attribute *attr, char *buf)
224{
225 struct adc128_data *data = adc128_update_device(dev);
226 int mask = 1 << to_sensor_dev_attr(attr)->index;
227 u8 alarms;
228
229 if (IS_ERR(data))
230 return PTR_ERR(data);
231
232 /*
233 * Clear an alarm after reporting it to user space. If it is still
234 * active, the next update sequence will set the alarm bit again.
235 */
236 alarms = data->alarms;
237 data->alarms &= ~mask;
238
239 return sprintf(buf, "%u\n", !!(alarms & mask));
240}
241
242static SENSOR_DEVICE_ATTR_2(in0_input, S_IWUSR | S_IRUGO,
243 adc128_show_in, adc128_set_in, 0, 0);
244static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO,
245 adc128_show_in, adc128_set_in, 0, 1);
246static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO,
247 adc128_show_in, adc128_set_in, 0, 2);
248
249static SENSOR_DEVICE_ATTR_2(in1_input, S_IWUSR | S_IRUGO,
250 adc128_show_in, adc128_set_in, 1, 0);
251static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO,
252 adc128_show_in, adc128_set_in, 1, 1);
253static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO,
254 adc128_show_in, adc128_set_in, 1, 2);
255
256static SENSOR_DEVICE_ATTR_2(in2_input, S_IWUSR | S_IRUGO,
257 adc128_show_in, adc128_set_in, 2, 0);
258static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO,
259 adc128_show_in, adc128_set_in, 2, 1);
260static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO,
261 adc128_show_in, adc128_set_in, 2, 2);
262
263static SENSOR_DEVICE_ATTR_2(in3_input, S_IWUSR | S_IRUGO,
264 adc128_show_in, adc128_set_in, 3, 0);
265static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO,
266 adc128_show_in, adc128_set_in, 3, 1);
267static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO,
268 adc128_show_in, adc128_set_in, 3, 2);
269
270static SENSOR_DEVICE_ATTR_2(in4_input, S_IWUSR | S_IRUGO,
271 adc128_show_in, adc128_set_in, 4, 0);
272static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO,
273 adc128_show_in, adc128_set_in, 4, 1);
274static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO,
275 adc128_show_in, adc128_set_in, 4, 2);
276
277static SENSOR_DEVICE_ATTR_2(in5_input, S_IWUSR | S_IRUGO,
278 adc128_show_in, adc128_set_in, 5, 0);
279static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO,
280 adc128_show_in, adc128_set_in, 5, 1);
281static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO,
282 adc128_show_in, adc128_set_in, 5, 2);
283
284static SENSOR_DEVICE_ATTR_2(in6_input, S_IWUSR | S_IRUGO,
285 adc128_show_in, adc128_set_in, 6, 0);
286static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO,
287 adc128_show_in, adc128_set_in, 6, 1);
288static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO,
289 adc128_show_in, adc128_set_in, 6, 2);
290
291static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adc128_show_temp, NULL, 0);
292static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
293 adc128_show_temp, adc128_set_temp, 1);
294static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
295 adc128_show_temp, adc128_set_temp, 2);
296
297static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, adc128_show_alarm, NULL, 0);
298static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, adc128_show_alarm, NULL, 1);
299static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, adc128_show_alarm, NULL, 2);
300static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, adc128_show_alarm, NULL, 3);
301static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, adc128_show_alarm, NULL, 4);
302static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, adc128_show_alarm, NULL, 5);
303static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, adc128_show_alarm, NULL, 6);
304static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, adc128_show_alarm, NULL, 7);
305
306static struct attribute *adc128_attrs[] = {
307 &sensor_dev_attr_in0_min.dev_attr.attr,
308 &sensor_dev_attr_in1_min.dev_attr.attr,
309 &sensor_dev_attr_in2_min.dev_attr.attr,
310 &sensor_dev_attr_in3_min.dev_attr.attr,
311 &sensor_dev_attr_in4_min.dev_attr.attr,
312 &sensor_dev_attr_in5_min.dev_attr.attr,
313 &sensor_dev_attr_in6_min.dev_attr.attr,
314 &sensor_dev_attr_in0_max.dev_attr.attr,
315 &sensor_dev_attr_in1_max.dev_attr.attr,
316 &sensor_dev_attr_in2_max.dev_attr.attr,
317 &sensor_dev_attr_in3_max.dev_attr.attr,
318 &sensor_dev_attr_in4_max.dev_attr.attr,
319 &sensor_dev_attr_in5_max.dev_attr.attr,
320 &sensor_dev_attr_in6_max.dev_attr.attr,
321 &sensor_dev_attr_in0_input.dev_attr.attr,
322 &sensor_dev_attr_in1_input.dev_attr.attr,
323 &sensor_dev_attr_in2_input.dev_attr.attr,
324 &sensor_dev_attr_in3_input.dev_attr.attr,
325 &sensor_dev_attr_in4_input.dev_attr.attr,
326 &sensor_dev_attr_in5_input.dev_attr.attr,
327 &sensor_dev_attr_in6_input.dev_attr.attr,
328 &sensor_dev_attr_temp1_input.dev_attr.attr,
329 &sensor_dev_attr_temp1_max.dev_attr.attr,
330 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
331 &sensor_dev_attr_in0_alarm.dev_attr.attr,
332 &sensor_dev_attr_in1_alarm.dev_attr.attr,
333 &sensor_dev_attr_in2_alarm.dev_attr.attr,
334 &sensor_dev_attr_in3_alarm.dev_attr.attr,
335 &sensor_dev_attr_in4_alarm.dev_attr.attr,
336 &sensor_dev_attr_in5_alarm.dev_attr.attr,
337 &sensor_dev_attr_in6_alarm.dev_attr.attr,
338 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
339 NULL
340};
341ATTRIBUTE_GROUPS(adc128);
342
343static int adc128_detect(struct i2c_client *client, struct i2c_board_info *info)
344{
345 int man_id, dev_id;
346
347 if (!i2c_check_functionality(client->adapter,
348 I2C_FUNC_SMBUS_BYTE_DATA |
349 I2C_FUNC_SMBUS_WORD_DATA))
350 return -ENODEV;
351
352 man_id = i2c_smbus_read_byte_data(client, ADC128_REG_MAN_ID);
353 dev_id = i2c_smbus_read_byte_data(client, ADC128_REG_DEV_ID);
354 if (man_id != 0x01 || dev_id != 0x09)
355 return -ENODEV;
356
357 /* Check unused bits for confirmation */
358 if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG) & 0xf4)
359 return -ENODEV;
360 if (i2c_smbus_read_byte_data(client, ADC128_REG_CONV_RATE) & 0xfe)
361 return -ENODEV;
362 if (i2c_smbus_read_byte_data(client, ADC128_REG_ONESHOT) & 0xfe)
363 return -ENODEV;
364 if (i2c_smbus_read_byte_data(client, ADC128_REG_SHUTDOWN) & 0xfe)
365 return -ENODEV;
366 if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV) & 0xf8)
367 return -ENODEV;
368 if (i2c_smbus_read_byte_data(client, ADC128_REG_BUSY_STATUS) & 0xfc)
369 return -ENODEV;
370
371 strlcpy(info->type, "adc128d818", I2C_NAME_SIZE);
372
373 return 0;
374}
375
376static int adc128_init_client(struct adc128_data *data)
377{
378 struct i2c_client *client = data->client;
379 int err;
380
381 /*
382 * Reset chip to defaults.
383 * This makes most other initializations unnecessary.
384 */
385 err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x80);
386 if (err)
387 return err;
388
389 /* Start monitoring */
390 err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x01);
391 if (err)
392 return err;
393
394 /* If external vref is selected, configure the chip to use it */
395 if (data->regulator) {
396 err = i2c_smbus_write_byte_data(client,
397 ADC128_REG_CONFIG_ADV, 0x01);
398 if (err)
399 return err;
400 }
401
402 return 0;
403}
404
405static int adc128_probe(struct i2c_client *client,
406 const struct i2c_device_id *id)
407{
408 struct device *dev = &client->dev;
409 struct regulator *regulator;
410 struct device *hwmon_dev;
411 struct adc128_data *data;
412 int err, vref;
413
414 data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL);
415 if (!data)
416 return -ENOMEM;
417
418 /* vref is optional. If specified, is used as chip reference voltage */
419 regulator = devm_regulator_get_optional(dev, "vref");
420 if (!IS_ERR(regulator)) {
421 data->regulator = regulator;
422 err = regulator_enable(regulator);
423 if (err < 0)
424 return err;
425 vref = regulator_get_voltage(regulator);
426 if (vref < 0) {
427 err = vref;
428 goto error;
429 }
430 data->vref = DIV_ROUND_CLOSEST(vref, 1000);
431 } else {
432 data->vref = 2560; /* 2.56V, in mV */
433 }
434
435 data->client = client;
436 i2c_set_clientdata(client, data);
437 mutex_init(&data->update_lock);
438
439 /* Initialize the chip */
440 err = adc128_init_client(data);
441 if (err < 0)
442 goto error;
443
444 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
445 data, adc128_groups);
446 if (IS_ERR(hwmon_dev)) {
447 err = PTR_ERR(hwmon_dev);
448 goto error;
449 }
450
451 return 0;
452
453error:
454 if (data->regulator)
455 regulator_disable(data->regulator);
456 return err;
457}
458
459static int adc128_remove(struct i2c_client *client)
460{
461 struct adc128_data *data = i2c_get_clientdata(client);
462
463 if (data->regulator)
464 regulator_disable(data->regulator);
465
466 return 0;
467}
468
469static const struct i2c_device_id adc128_id[] = {
470 { "adc128d818", 0 },
471 { }
472};
473MODULE_DEVICE_TABLE(i2c, adc128_id);
474
475static struct i2c_driver adc128_driver = {
476 .class = I2C_CLASS_HWMON,
477 .driver = {
478 .name = "adc128d818",
479 },
480 .probe = adc128_probe,
481 .remove = adc128_remove,
482 .id_table = adc128_id,
483 .detect = adc128_detect,
484 .address_list = normal_i2c,
485};
486
487module_i2c_driver(adc128_driver);
488
489MODULE_AUTHOR("Guenter Roeck");
490MODULE_DESCRIPTION("Driver for ADC128D818");
491MODULE_LICENSE("GPL");