Linux Audio

Check our new training course

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