xref: /openbmc/linux/drivers/hwmon/adc128d818.c (revision 2c6467d2)
1 /*
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 #include <linux/bitops.h>
31 #include <linux/of.h>
32 
33 /* Addresses to scan
34  * The chip also supports addresses 0x35..0x37. Don't scan those addresses
35  * since they are also used by some EEPROMs, which may result in false
36  * positives.
37  */
38 static const unsigned short normal_i2c[] = {
39 	0x1d, 0x1e, 0x1f, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
40 
41 /* registers */
42 #define ADC128_REG_IN_MAX(nr)		(0x2a + (nr) * 2)
43 #define ADC128_REG_IN_MIN(nr)		(0x2b + (nr) * 2)
44 #define ADC128_REG_IN(nr)		(0x20 + (nr))
45 
46 #define ADC128_REG_TEMP			0x27
47 #define ADC128_REG_TEMP_MAX		0x38
48 #define ADC128_REG_TEMP_HYST		0x39
49 
50 #define ADC128_REG_CONFIG		0x00
51 #define ADC128_REG_ALARM		0x01
52 #define ADC128_REG_MASK			0x03
53 #define ADC128_REG_CONV_RATE		0x07
54 #define ADC128_REG_ONESHOT		0x09
55 #define ADC128_REG_SHUTDOWN		0x0a
56 #define ADC128_REG_CONFIG_ADV		0x0b
57 #define ADC128_REG_BUSY_STATUS		0x0c
58 
59 #define ADC128_REG_MAN_ID		0x3e
60 #define ADC128_REG_DEV_ID		0x3f
61 
62 /* No. of voltage entries in adc128_attrs */
63 #define ADC128_ATTR_NUM_VOLT		(8 * 4)
64 
65 /* Voltage inputs visible per operation mode */
66 static const u8 num_inputs[] = { 7, 8, 4, 6 };
67 
68 struct adc128_data {
69 	struct i2c_client *client;
70 	struct regulator *regulator;
71 	int vref;		/* Reference voltage in mV */
72 	struct mutex update_lock;
73 	u8 mode;		/* Operation mode */
74 	bool valid;		/* true if following fields are valid */
75 	unsigned long last_updated;	/* In jiffies */
76 
77 	u16 in[3][8];		/* Register value, normalized to 12 bit
78 				 * 0: input voltage
79 				 * 1: min limit
80 				 * 2: max limit
81 				 */
82 	s16 temp[3];		/* Register value, normalized to 9 bit
83 				 * 0: sensor 1: limit 2: hyst
84 				 */
85 	u8 alarms;		/* alarm register value */
86 };
87 
88 static struct adc128_data *adc128_update_device(struct device *dev)
89 {
90 	struct adc128_data *data = dev_get_drvdata(dev);
91 	struct i2c_client *client = data->client;
92 	struct adc128_data *ret = data;
93 	int i, rv;
94 
95 	mutex_lock(&data->update_lock);
96 
97 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
98 		for (i = 0; i < num_inputs[data->mode]; i++) {
99 			rv = i2c_smbus_read_word_swapped(client,
100 							 ADC128_REG_IN(i));
101 			if (rv < 0)
102 				goto abort;
103 			data->in[0][i] = rv >> 4;
104 
105 			rv = i2c_smbus_read_byte_data(client,
106 						      ADC128_REG_IN_MIN(i));
107 			if (rv < 0)
108 				goto abort;
109 			data->in[1][i] = rv << 4;
110 
111 			rv = i2c_smbus_read_byte_data(client,
112 						      ADC128_REG_IN_MAX(i));
113 			if (rv < 0)
114 				goto abort;
115 			data->in[2][i] = rv << 4;
116 		}
117 
118 		if (data->mode != 1) {
119 			rv = i2c_smbus_read_word_swapped(client,
120 							 ADC128_REG_TEMP);
121 			if (rv < 0)
122 				goto abort;
123 			data->temp[0] = rv >> 7;
124 
125 			rv = i2c_smbus_read_byte_data(client,
126 						      ADC128_REG_TEMP_MAX);
127 			if (rv < 0)
128 				goto abort;
129 			data->temp[1] = rv << 1;
130 
131 			rv = i2c_smbus_read_byte_data(client,
132 						      ADC128_REG_TEMP_HYST);
133 			if (rv < 0)
134 				goto abort;
135 			data->temp[2] = rv << 1;
136 		}
137 
138 		rv = i2c_smbus_read_byte_data(client, ADC128_REG_ALARM);
139 		if (rv < 0)
140 			goto abort;
141 		data->alarms |= rv;
142 
143 		data->last_updated = jiffies;
144 		data->valid = true;
145 	}
146 	goto done;
147 
148 abort:
149 	ret = ERR_PTR(rv);
150 	data->valid = false;
151 done:
152 	mutex_unlock(&data->update_lock);
153 	return ret;
154 }
155 
156 static ssize_t adc128_in_show(struct device *dev,
157 			      struct device_attribute *attr, char *buf)
158 {
159 	struct adc128_data *data = adc128_update_device(dev);
160 	int index = to_sensor_dev_attr_2(attr)->index;
161 	int nr = to_sensor_dev_attr_2(attr)->nr;
162 	int val;
163 
164 	if (IS_ERR(data))
165 		return PTR_ERR(data);
166 
167 	val = DIV_ROUND_CLOSEST(data->in[index][nr] * data->vref, 4095);
168 	return sprintf(buf, "%d\n", val);
169 }
170 
171 static ssize_t adc128_in_store(struct device *dev,
172 			       struct device_attribute *attr, const char *buf,
173 			       size_t count)
174 {
175 	struct adc128_data *data = dev_get_drvdata(dev);
176 	int index = to_sensor_dev_attr_2(attr)->index;
177 	int nr = to_sensor_dev_attr_2(attr)->nr;
178 	u8 reg, regval;
179 	long val;
180 	int err;
181 
182 	err = kstrtol(buf, 10, &val);
183 	if (err < 0)
184 		return err;
185 
186 	mutex_lock(&data->update_lock);
187 	/* 10 mV LSB on limit registers */
188 	regval = clamp_val(DIV_ROUND_CLOSEST(val, 10), 0, 255);
189 	data->in[index][nr] = regval << 4;
190 	reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr);
191 	i2c_smbus_write_byte_data(data->client, reg, regval);
192 	mutex_unlock(&data->update_lock);
193 
194 	return count;
195 }
196 
197 static ssize_t adc128_temp_show(struct device *dev,
198 				struct device_attribute *attr, char *buf)
199 {
200 	struct adc128_data *data = adc128_update_device(dev);
201 	int index = to_sensor_dev_attr(attr)->index;
202 	int temp;
203 
204 	if (IS_ERR(data))
205 		return PTR_ERR(data);
206 
207 	temp = sign_extend32(data->temp[index], 8);
208 	return sprintf(buf, "%d\n", temp * 500);/* 0.5 degrees C resolution */
209 }
210 
211 static ssize_t adc128_temp_store(struct device *dev,
212 				 struct device_attribute *attr,
213 				 const char *buf, size_t count)
214 {
215 	struct adc128_data *data = dev_get_drvdata(dev);
216 	int index = to_sensor_dev_attr(attr)->index;
217 	long val;
218 	int err;
219 	s8 regval;
220 
221 	err = kstrtol(buf, 10, &val);
222 	if (err < 0)
223 		return err;
224 
225 	mutex_lock(&data->update_lock);
226 	regval = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
227 	data->temp[index] = regval << 1;
228 	i2c_smbus_write_byte_data(data->client,
229 				  index == 1 ? ADC128_REG_TEMP_MAX
230 					     : ADC128_REG_TEMP_HYST,
231 				  regval);
232 	mutex_unlock(&data->update_lock);
233 
234 	return count;
235 }
236 
237 static ssize_t adc128_alarm_show(struct device *dev,
238 				 struct device_attribute *attr, char *buf)
239 {
240 	struct adc128_data *data = adc128_update_device(dev);
241 	int mask = 1 << to_sensor_dev_attr(attr)->index;
242 	u8 alarms;
243 
244 	if (IS_ERR(data))
245 		return PTR_ERR(data);
246 
247 	/*
248 	 * Clear an alarm after reporting it to user space. If it is still
249 	 * active, the next update sequence will set the alarm bit again.
250 	 */
251 	alarms = data->alarms;
252 	data->alarms &= ~mask;
253 
254 	return sprintf(buf, "%u\n", !!(alarms & mask));
255 }
256 
257 static umode_t adc128_is_visible(struct kobject *kobj,
258 				 struct attribute *attr, int index)
259 {
260 	struct device *dev = container_of(kobj, struct device, kobj);
261 	struct adc128_data *data = dev_get_drvdata(dev);
262 
263 	if (index < ADC128_ATTR_NUM_VOLT) {
264 		/* Voltage, visible according to num_inputs[] */
265 		if (index >= num_inputs[data->mode] * 4)
266 			return 0;
267 	} else {
268 		/* Temperature, visible if not in mode 1 */
269 		if (data->mode == 1)
270 			return 0;
271 	}
272 
273 	return attr->mode;
274 }
275 
276 static SENSOR_DEVICE_ATTR_2_RO(in0_input, adc128_in, 0, 0);
277 static SENSOR_DEVICE_ATTR_2_RW(in0_min, adc128_in, 0, 1);
278 static SENSOR_DEVICE_ATTR_2_RW(in0_max, adc128_in, 0, 2);
279 
280 static SENSOR_DEVICE_ATTR_2_RO(in1_input, adc128_in, 1, 0);
281 static SENSOR_DEVICE_ATTR_2_RW(in1_min, adc128_in, 1, 1);
282 static SENSOR_DEVICE_ATTR_2_RW(in1_max, adc128_in, 1, 2);
283 
284 static SENSOR_DEVICE_ATTR_2_RO(in2_input, adc128_in, 2, 0);
285 static SENSOR_DEVICE_ATTR_2_RW(in2_min, adc128_in, 2, 1);
286 static SENSOR_DEVICE_ATTR_2_RW(in2_max, adc128_in, 2, 2);
287 
288 static SENSOR_DEVICE_ATTR_2_RO(in3_input, adc128_in, 3, 0);
289 static SENSOR_DEVICE_ATTR_2_RW(in3_min, adc128_in, 3, 1);
290 static SENSOR_DEVICE_ATTR_2_RW(in3_max, adc128_in, 3, 2);
291 
292 static SENSOR_DEVICE_ATTR_2_RO(in4_input, adc128_in, 4, 0);
293 static SENSOR_DEVICE_ATTR_2_RW(in4_min, adc128_in, 4, 1);
294 static SENSOR_DEVICE_ATTR_2_RW(in4_max, adc128_in, 4, 2);
295 
296 static SENSOR_DEVICE_ATTR_2_RO(in5_input, adc128_in, 5, 0);
297 static SENSOR_DEVICE_ATTR_2_RW(in5_min, adc128_in, 5, 1);
298 static SENSOR_DEVICE_ATTR_2_RW(in5_max, adc128_in, 5, 2);
299 
300 static SENSOR_DEVICE_ATTR_2_RO(in6_input, adc128_in, 6, 0);
301 static SENSOR_DEVICE_ATTR_2_RW(in6_min, adc128_in, 6, 1);
302 static SENSOR_DEVICE_ATTR_2_RW(in6_max, adc128_in, 6, 2);
303 
304 static SENSOR_DEVICE_ATTR_2_RO(in7_input, adc128_in, 7, 0);
305 static SENSOR_DEVICE_ATTR_2_RW(in7_min, adc128_in, 7, 1);
306 static SENSOR_DEVICE_ATTR_2_RW(in7_max, adc128_in, 7, 2);
307 
308 static SENSOR_DEVICE_ATTR_RO(temp1_input, adc128_temp, 0);
309 static SENSOR_DEVICE_ATTR_RW(temp1_max, adc128_temp, 1);
310 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, adc128_temp, 2);
311 
312 static SENSOR_DEVICE_ATTR_RO(in0_alarm, adc128_alarm, 0);
313 static SENSOR_DEVICE_ATTR_RO(in1_alarm, adc128_alarm, 1);
314 static SENSOR_DEVICE_ATTR_RO(in2_alarm, adc128_alarm, 2);
315 static SENSOR_DEVICE_ATTR_RO(in3_alarm, adc128_alarm, 3);
316 static SENSOR_DEVICE_ATTR_RO(in4_alarm, adc128_alarm, 4);
317 static SENSOR_DEVICE_ATTR_RO(in5_alarm, adc128_alarm, 5);
318 static SENSOR_DEVICE_ATTR_RO(in6_alarm, adc128_alarm, 6);
319 static SENSOR_DEVICE_ATTR_RO(in7_alarm, adc128_alarm, 7);
320 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, adc128_alarm, 7);
321 
322 static struct attribute *adc128_attrs[] = {
323 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
324 	&sensor_dev_attr_in0_input.dev_attr.attr,
325 	&sensor_dev_attr_in0_max.dev_attr.attr,
326 	&sensor_dev_attr_in0_min.dev_attr.attr,
327 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
328 	&sensor_dev_attr_in1_input.dev_attr.attr,
329 	&sensor_dev_attr_in1_max.dev_attr.attr,
330 	&sensor_dev_attr_in1_min.dev_attr.attr,
331 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
332 	&sensor_dev_attr_in2_input.dev_attr.attr,
333 	&sensor_dev_attr_in2_max.dev_attr.attr,
334 	&sensor_dev_attr_in2_min.dev_attr.attr,
335 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
336 	&sensor_dev_attr_in3_input.dev_attr.attr,
337 	&sensor_dev_attr_in3_max.dev_attr.attr,
338 	&sensor_dev_attr_in3_min.dev_attr.attr,
339 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
340 	&sensor_dev_attr_in4_input.dev_attr.attr,
341 	&sensor_dev_attr_in4_max.dev_attr.attr,
342 	&sensor_dev_attr_in4_min.dev_attr.attr,
343 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
344 	&sensor_dev_attr_in5_input.dev_attr.attr,
345 	&sensor_dev_attr_in5_max.dev_attr.attr,
346 	&sensor_dev_attr_in5_min.dev_attr.attr,
347 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
348 	&sensor_dev_attr_in6_input.dev_attr.attr,
349 	&sensor_dev_attr_in6_max.dev_attr.attr,
350 	&sensor_dev_attr_in6_min.dev_attr.attr,
351 	&sensor_dev_attr_in7_alarm.dev_attr.attr,
352 	&sensor_dev_attr_in7_input.dev_attr.attr,
353 	&sensor_dev_attr_in7_max.dev_attr.attr,
354 	&sensor_dev_attr_in7_min.dev_attr.attr,
355 	&sensor_dev_attr_temp1_input.dev_attr.attr,
356 	&sensor_dev_attr_temp1_max.dev_attr.attr,
357 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
358 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
359 	NULL
360 };
361 
362 static const struct attribute_group adc128_group = {
363 	.attrs = adc128_attrs,
364 	.is_visible = adc128_is_visible,
365 };
366 __ATTRIBUTE_GROUPS(adc128);
367 
368 static int adc128_detect(struct i2c_client *client, struct i2c_board_info *info)
369 {
370 	int man_id, dev_id;
371 
372 	if (!i2c_check_functionality(client->adapter,
373 				     I2C_FUNC_SMBUS_BYTE_DATA |
374 				     I2C_FUNC_SMBUS_WORD_DATA))
375 		return -ENODEV;
376 
377 	man_id = i2c_smbus_read_byte_data(client, ADC128_REG_MAN_ID);
378 	dev_id = i2c_smbus_read_byte_data(client, ADC128_REG_DEV_ID);
379 	if (man_id != 0x01 || dev_id != 0x09)
380 		return -ENODEV;
381 
382 	/* Check unused bits for confirmation */
383 	if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG) & 0xf4)
384 		return -ENODEV;
385 	if (i2c_smbus_read_byte_data(client, ADC128_REG_CONV_RATE) & 0xfe)
386 		return -ENODEV;
387 	if (i2c_smbus_read_byte_data(client, ADC128_REG_ONESHOT) & 0xfe)
388 		return -ENODEV;
389 	if (i2c_smbus_read_byte_data(client, ADC128_REG_SHUTDOWN) & 0xfe)
390 		return -ENODEV;
391 	if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV) & 0xf8)
392 		return -ENODEV;
393 	if (i2c_smbus_read_byte_data(client, ADC128_REG_BUSY_STATUS) & 0xfc)
394 		return -ENODEV;
395 
396 	strlcpy(info->type, "adc128d818", I2C_NAME_SIZE);
397 
398 	return 0;
399 }
400 
401 static int adc128_init_client(struct adc128_data *data)
402 {
403 	struct i2c_client *client = data->client;
404 	int err;
405 
406 	/*
407 	 * Reset chip to defaults.
408 	 * This makes most other initializations unnecessary.
409 	 */
410 	err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x80);
411 	if (err)
412 		return err;
413 
414 	/* Set operation mode, if non-default */
415 	if (data->mode != 0) {
416 		err = i2c_smbus_write_byte_data(client,
417 						ADC128_REG_CONFIG_ADV,
418 						data->mode << 1);
419 		if (err)
420 			return err;
421 	}
422 
423 	/* Start monitoring */
424 	err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x01);
425 	if (err)
426 		return err;
427 
428 	/* If external vref is selected, configure the chip to use it */
429 	if (data->regulator) {
430 		err = i2c_smbus_write_byte_data(client,
431 						ADC128_REG_CONFIG_ADV, 0x01);
432 		if (err)
433 			return err;
434 	}
435 
436 	return 0;
437 }
438 
439 static int adc128_probe(struct i2c_client *client,
440 			const struct i2c_device_id *id)
441 {
442 	struct device *dev = &client->dev;
443 	struct regulator *regulator;
444 	struct device *hwmon_dev;
445 	struct adc128_data *data;
446 	int err, vref;
447 
448 	data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL);
449 	if (!data)
450 		return -ENOMEM;
451 
452 	/* vref is optional. If specified, is used as chip reference voltage */
453 	regulator = devm_regulator_get_optional(dev, "vref");
454 	if (!IS_ERR(regulator)) {
455 		data->regulator = regulator;
456 		err = regulator_enable(regulator);
457 		if (err < 0)
458 			return err;
459 		vref = regulator_get_voltage(regulator);
460 		if (vref < 0) {
461 			err = vref;
462 			goto error;
463 		}
464 		data->vref = DIV_ROUND_CLOSEST(vref, 1000);
465 	} else {
466 		data->vref = 2560;	/* 2.56V, in mV */
467 	}
468 
469 	/* Operation mode is optional. If unspecified, keep current mode */
470 	if (of_property_read_u8(dev->of_node, "ti,mode", &data->mode) == 0) {
471 		if (data->mode > 3) {
472 			dev_err(dev, "invalid operation mode %d\n",
473 				data->mode);
474 			err = -EINVAL;
475 			goto error;
476 		}
477 	} else {
478 		err = i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV);
479 		if (err < 0)
480 			goto error;
481 		data->mode = (err >> 1) & ADC128_REG_MASK;
482 	}
483 
484 	data->client = client;
485 	i2c_set_clientdata(client, data);
486 	mutex_init(&data->update_lock);
487 
488 	/* Initialize the chip */
489 	err = adc128_init_client(data);
490 	if (err < 0)
491 		goto error;
492 
493 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
494 							   data, adc128_groups);
495 	if (IS_ERR(hwmon_dev)) {
496 		err = PTR_ERR(hwmon_dev);
497 		goto error;
498 	}
499 
500 	return 0;
501 
502 error:
503 	if (data->regulator)
504 		regulator_disable(data->regulator);
505 	return err;
506 }
507 
508 static int adc128_remove(struct i2c_client *client)
509 {
510 	struct adc128_data *data = i2c_get_clientdata(client);
511 
512 	if (data->regulator)
513 		regulator_disable(data->regulator);
514 
515 	return 0;
516 }
517 
518 static const struct i2c_device_id adc128_id[] = {
519 	{ "adc128d818", 0 },
520 	{ }
521 };
522 MODULE_DEVICE_TABLE(i2c, adc128_id);
523 
524 static const struct of_device_id adc128_of_match[] = {
525 	{ .compatible = "ti,adc128d818" },
526 	{ },
527 };
528 MODULE_DEVICE_TABLE(of, adc128_of_match);
529 
530 static struct i2c_driver adc128_driver = {
531 	.class		= I2C_CLASS_HWMON,
532 	.driver = {
533 		.name	= "adc128d818",
534 		.of_match_table = of_match_ptr(adc128_of_match),
535 	},
536 	.probe		= adc128_probe,
537 	.remove		= adc128_remove,
538 	.id_table	= adc128_id,
539 	.detect		= adc128_detect,
540 	.address_list	= normal_i2c,
541 };
542 
543 module_i2c_driver(adc128_driver);
544 
545 MODULE_AUTHOR("Guenter Roeck");
546 MODULE_DESCRIPTION("Driver for ADC128D818");
547 MODULE_LICENSE("GPL");
548