1/*
2 * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
3 *
4 * Copyright 2010-2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/i2c.h>
12#include <linux/interrupt.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/regulator/consumer.h>
17#include <linux/slab.h>
18#include <linux/sysfs.h>
19
20#include <linux/iio/iio.h>
21#include <linux/iio/sysfs.h>
22#include <linux/iio/events.h>
23
24#include <linux/platform_data/ad7291.h>
25
26/*
27 * Simplified handling
28 *
29 * If no events enabled - single polled channel read
30 * If event enabled direct reads disable unless channel
31 * is in the read mask.
32 *
33 * The noise-delayed bit as per datasheet suggestion is always enabled.
34 */
35
36/*
37 * AD7291 registers definition
38 */
39#define AD7291_COMMAND			0x00
40#define AD7291_VOLTAGE			0x01
41#define AD7291_T_SENSE			0x02
42#define AD7291_T_AVERAGE		0x03
43#define AD7291_DATA_HIGH(x)		((x) * 3 + 0x4)
44#define AD7291_DATA_LOW(x)		((x) * 3 + 0x5)
45#define AD7291_HYST(x)			((x) * 3 + 0x6)
46#define AD7291_VOLTAGE_ALERT_STATUS	0x1F
47#define AD7291_T_ALERT_STATUS		0x20
48
49#define AD7291_BITS			12
50#define AD7291_VOLTAGE_LIMIT_COUNT	8
51
52
53/*
54 * AD7291 command
55 */
56#define AD7291_AUTOCYCLE		BIT(0)
57#define AD7291_RESET			BIT(1)
58#define AD7291_ALERT_CLEAR		BIT(2)
59#define AD7291_ALERT_POLARITY		BIT(3)
60#define AD7291_EXT_REF			BIT(4)
61#define AD7291_NOISE_DELAY		BIT(5)
62#define AD7291_T_SENSE_MASK		BIT(7)
63#define AD7291_VOLTAGE_MASK		GENMASK(15, 8)
64#define AD7291_VOLTAGE_OFFSET		8
65
66/*
67 * AD7291 value masks
68 */
69#define AD7291_VALUE_MASK		GENMASK(11, 0)
70
71/*
72 * AD7291 alert register bits
73 */
74#define AD7291_T_LOW			BIT(0)
75#define AD7291_T_HIGH			BIT(1)
76#define AD7291_T_AVG_LOW		BIT(2)
77#define AD7291_T_AVG_HIGH		BIT(3)
78#define AD7291_V_LOW(x)			BIT((x) * 2)
79#define AD7291_V_HIGH(x)		BIT((x) * 2 + 1)
80
81
82struct ad7291_chip_info {
83	struct i2c_client	*client;
84	struct regulator	*reg;
85	u16			command;
86	u16			c_mask;	/* Active voltage channels for events */
87	struct mutex		state_lock;
88};
89
90static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
91{
92	struct i2c_client *client = chip->client;
93	int ret = 0;
94
95	ret = i2c_smbus_read_word_swapped(client, reg);
96	if (ret < 0) {
97		dev_err(&client->dev, "I2C read error\n");
98		return ret;
99	}
100
101	*data = ret;
102
103	return 0;
104}
105
106static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
107{
108	return i2c_smbus_write_word_swapped(chip->client, reg, data);
109}
110
111static irqreturn_t ad7291_event_handler(int irq, void *private)
112{
113	struct iio_dev *indio_dev = private;
114	struct ad7291_chip_info *chip = iio_priv(private);
115	u16 t_status, v_status;
116	u16 command;
117	int i;
118	s64 timestamp = iio_get_time_ns();
119
120	if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
121		return IRQ_HANDLED;
122
123	if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
124		return IRQ_HANDLED;
125
126	if (!(t_status || v_status))
127		return IRQ_HANDLED;
128
129	command = chip->command | AD7291_ALERT_CLEAR;
130	ad7291_i2c_write(chip, AD7291_COMMAND, command);
131
132	command = chip->command & ~AD7291_ALERT_CLEAR;
133	ad7291_i2c_write(chip, AD7291_COMMAND, command);
134
135	/* For now treat t_sense and t_sense_average the same */
136	if ((t_status & AD7291_T_LOW) || (t_status & AD7291_T_AVG_LOW))
137		iio_push_event(indio_dev,
138			       IIO_UNMOD_EVENT_CODE(IIO_TEMP,
139						    0,
140						    IIO_EV_TYPE_THRESH,
141						    IIO_EV_DIR_FALLING),
142			       timestamp);
143	if ((t_status & AD7291_T_HIGH) || (t_status & AD7291_T_AVG_HIGH))
144		iio_push_event(indio_dev,
145			       IIO_UNMOD_EVENT_CODE(IIO_TEMP,
146						    0,
147						    IIO_EV_TYPE_THRESH,
148						    IIO_EV_DIR_RISING),
149			       timestamp);
150
151	for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
152		if (v_status & AD7291_V_LOW(i))
153			iio_push_event(indio_dev,
154				       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
155							    i,
156							    IIO_EV_TYPE_THRESH,
157							    IIO_EV_DIR_FALLING),
158				       timestamp);
159		if (v_status & AD7291_V_HIGH(i))
160			iio_push_event(indio_dev,
161				       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
162							    i,
163							    IIO_EV_TYPE_THRESH,
164							    IIO_EV_DIR_RISING),
165				       timestamp);
166	}
167
168	return IRQ_HANDLED;
169}
170
171static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan,
172					 enum iio_event_direction dir,
173					 enum iio_event_info info)
174{
175	unsigned int offset;
176
177	switch (chan->type) {
178	case IIO_VOLTAGE:
179		offset = chan->channel;
180		break;
181	case IIO_TEMP:
182		offset = AD7291_VOLTAGE_OFFSET;
183		break;
184	default:
185	    return 0;
186	}
187
188	switch (info) {
189	case IIO_EV_INFO_VALUE:
190		if (dir == IIO_EV_DIR_FALLING)
191			return AD7291_DATA_HIGH(offset);
192		else
193			return AD7291_DATA_LOW(offset);
194	case IIO_EV_INFO_HYSTERESIS:
195		return AD7291_HYST(offset);
196	default:
197		break;
198	}
199	return 0;
200}
201
202static int ad7291_read_event_value(struct iio_dev *indio_dev,
203				   const struct iio_chan_spec *chan,
204				   enum iio_event_type type,
205				   enum iio_event_direction dir,
206				   enum iio_event_info info,
207				   int *val, int *val2)
208{
209	struct ad7291_chip_info *chip = iio_priv(indio_dev);
210	int ret;
211	u16 uval;
212
213	ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
214			      &uval);
215	if (ret < 0)
216		return ret;
217
218	if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
219		*val = uval & AD7291_VALUE_MASK;
220
221	else
222		*val = sign_extend32(uval, 11);
223
224	return IIO_VAL_INT;
225}
226
227static int ad7291_write_event_value(struct iio_dev *indio_dev,
228				    const struct iio_chan_spec *chan,
229				    enum iio_event_type type,
230				    enum iio_event_direction dir,
231				    enum iio_event_info info,
232				    int val, int val2)
233{
234	struct ad7291_chip_info *chip = iio_priv(indio_dev);
235
236	if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) {
237		if (val > AD7291_VALUE_MASK || val < 0)
238			return -EINVAL;
239	} else {
240		if (val > 2047 || val < -2048)
241			return -EINVAL;
242	}
243
244	return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
245				val);
246}
247
248static int ad7291_read_event_config(struct iio_dev *indio_dev,
249				    const struct iio_chan_spec *chan,
250				    enum iio_event_type type,
251				    enum iio_event_direction dir)
252{
253	struct ad7291_chip_info *chip = iio_priv(indio_dev);
254	/*
255	 * To be enabled the channel must simply be on. If any are enabled
256	 * we are in continuous sampling mode
257	 */
258
259	switch (chan->type) {
260	case IIO_VOLTAGE:
261		return !!(chip->c_mask & BIT(15 - chan->channel));
262	case IIO_TEMP:
263		/* always on */
264		return 1;
265	default:
266		return -EINVAL;
267	}
268
269}
270
271static int ad7291_write_event_config(struct iio_dev *indio_dev,
272				     const struct iio_chan_spec *chan,
273				     enum iio_event_type type,
274				     enum iio_event_direction dir,
275				     int state)
276{
277	int ret = 0;
278	struct ad7291_chip_info *chip = iio_priv(indio_dev);
279	unsigned int mask;
280	u16 regval;
281
282	mutex_lock(&chip->state_lock);
283	regval = chip->command;
284	/*
285	 * To be enabled the channel must simply be on. If any are enabled
286	 * use continuous sampling mode.
287	 * Possible to disable temp as well but that makes single read tricky.
288	 */
289
290	mask = BIT(15 - chan->channel);
291
292	switch (chan->type) {
293	case IIO_VOLTAGE:
294		if ((!state) && (chip->c_mask & mask))
295			chip->c_mask &= ~mask;
296		else if (state && (!(chip->c_mask & mask)))
297			chip->c_mask |= mask;
298		else
299			break;
300
301		regval &= ~AD7291_AUTOCYCLE;
302		regval |= chip->c_mask;
303		if (chip->c_mask) /* Enable autocycle? */
304			regval |= AD7291_AUTOCYCLE;
305
306		ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
307		if (ret < 0)
308			goto error_ret;
309
310		chip->command = regval;
311		break;
312	default:
313		ret = -EINVAL;
314	}
315
316error_ret:
317	mutex_unlock(&chip->state_lock);
318	return ret;
319}
320
321static int ad7291_read_raw(struct iio_dev *indio_dev,
322			   struct iio_chan_spec const *chan,
323			   int *val,
324			   int *val2,
325			   long mask)
326{
327	int ret;
328	struct ad7291_chip_info *chip = iio_priv(indio_dev);
329	u16 regval;
330
331	switch (mask) {
332	case IIO_CHAN_INFO_RAW:
333		switch (chan->type) {
334		case IIO_VOLTAGE:
335			mutex_lock(&chip->state_lock);
336			/* If in autocycle mode drop through */
337			if (chip->command & AD7291_AUTOCYCLE) {
338				mutex_unlock(&chip->state_lock);
339				return -EBUSY;
340			}
341			/* Enable this channel alone */
342			regval = chip->command & (~AD7291_VOLTAGE_MASK);
343			regval |= BIT(15 - chan->channel);
344			ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
345			if (ret < 0) {
346				mutex_unlock(&chip->state_lock);
347				return ret;
348			}
349			/* Read voltage */
350			ret = i2c_smbus_read_word_swapped(chip->client,
351							  AD7291_VOLTAGE);
352			if (ret < 0) {
353				mutex_unlock(&chip->state_lock);
354				return ret;
355			}
356			*val = ret & AD7291_VALUE_MASK;
357			mutex_unlock(&chip->state_lock);
358			return IIO_VAL_INT;
359		case IIO_TEMP:
360			/* Assumes tsense bit of command register always set */
361			ret = i2c_smbus_read_word_swapped(chip->client,
362							  AD7291_T_SENSE);
363			if (ret < 0)
364				return ret;
365			*val = sign_extend32(ret, 11);
366			return IIO_VAL_INT;
367		default:
368			return -EINVAL;
369		}
370	case IIO_CHAN_INFO_AVERAGE_RAW:
371		ret = i2c_smbus_read_word_swapped(chip->client,
372						  AD7291_T_AVERAGE);
373			if (ret < 0)
374				return ret;
375			*val = sign_extend32(ret, 11);
376			return IIO_VAL_INT;
377	case IIO_CHAN_INFO_SCALE:
378		switch (chan->type) {
379		case IIO_VOLTAGE:
380			if (chip->reg) {
381				int vref;
382
383				vref = regulator_get_voltage(chip->reg);
384				if (vref < 0)
385					return vref;
386				*val = vref / 1000;
387			} else {
388				*val = 2500;
389			}
390			*val2 = AD7291_BITS;
391			return IIO_VAL_FRACTIONAL_LOG2;
392		case IIO_TEMP:
393			/*
394			 * One LSB of the ADC corresponds to 0.25 deg C.
395			 * The temperature reading is in 12-bit twos
396			 * complement format
397			 */
398			*val = 250;
399			return IIO_VAL_INT;
400		default:
401			return -EINVAL;
402		}
403	default:
404		return -EINVAL;
405	}
406}
407
408static const struct iio_event_spec ad7291_events[] = {
409	{
410		.type = IIO_EV_TYPE_THRESH,
411		.dir = IIO_EV_DIR_RISING,
412		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
413			BIT(IIO_EV_INFO_ENABLE),
414	}, {
415		.type = IIO_EV_TYPE_THRESH,
416		.dir = IIO_EV_DIR_FALLING,
417		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
418			BIT(IIO_EV_INFO_ENABLE),
419	}, {
420		.type = IIO_EV_TYPE_THRESH,
421		.dir = IIO_EV_DIR_EITHER,
422		.mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
423	},
424};
425
426#define AD7291_VOLTAGE_CHAN(_chan)					\
427{									\
428	.type = IIO_VOLTAGE,						\
429	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
430	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),		\
431	.indexed = 1,							\
432	.channel = _chan,						\
433	.event_spec = ad7291_events,					\
434	.num_event_specs = ARRAY_SIZE(ad7291_events),			\
435}
436
437static const struct iio_chan_spec ad7291_channels[] = {
438	AD7291_VOLTAGE_CHAN(0),
439	AD7291_VOLTAGE_CHAN(1),
440	AD7291_VOLTAGE_CHAN(2),
441	AD7291_VOLTAGE_CHAN(3),
442	AD7291_VOLTAGE_CHAN(4),
443	AD7291_VOLTAGE_CHAN(5),
444	AD7291_VOLTAGE_CHAN(6),
445	AD7291_VOLTAGE_CHAN(7),
446	{
447		.type = IIO_TEMP,
448		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
449				BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
450				BIT(IIO_CHAN_INFO_SCALE),
451		.indexed = 1,
452		.channel = 0,
453		.event_spec = ad7291_events,
454		.num_event_specs = ARRAY_SIZE(ad7291_events),
455	}
456};
457
458static const struct iio_info ad7291_info = {
459	.read_raw = &ad7291_read_raw,
460	.read_event_config = &ad7291_read_event_config,
461	.write_event_config = &ad7291_write_event_config,
462	.read_event_value = &ad7291_read_event_value,
463	.write_event_value = &ad7291_write_event_value,
464	.driver_module = THIS_MODULE,
465};
466
467static int ad7291_probe(struct i2c_client *client,
468			const struct i2c_device_id *id)
469{
470	struct ad7291_platform_data *pdata = client->dev.platform_data;
471	struct ad7291_chip_info *chip;
472	struct iio_dev *indio_dev;
473	int ret;
474
475	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
476	if (!indio_dev)
477		return -ENOMEM;
478	chip = iio_priv(indio_dev);
479
480	if (pdata && pdata->use_external_ref) {
481		chip->reg = devm_regulator_get(&client->dev, "vref");
482		if (IS_ERR(chip->reg))
483			return PTR_ERR(chip->reg);
484
485		ret = regulator_enable(chip->reg);
486		if (ret)
487			return ret;
488	}
489
490	mutex_init(&chip->state_lock);
491	/* this is only used for device removal purposes */
492	i2c_set_clientdata(client, indio_dev);
493
494	chip->client = client;
495
496	chip->command = AD7291_NOISE_DELAY |
497			AD7291_T_SENSE_MASK | /* Tsense always enabled */
498			AD7291_ALERT_POLARITY; /* set irq polarity low level */
499
500	if (pdata && pdata->use_external_ref)
501		chip->command |= AD7291_EXT_REF;
502
503	indio_dev->name = id->name;
504	indio_dev->channels = ad7291_channels;
505	indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
506
507	indio_dev->dev.parent = &client->dev;
508	indio_dev->info = &ad7291_info;
509	indio_dev->modes = INDIO_DIRECT_MODE;
510
511	ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
512	if (ret) {
513		ret = -EIO;
514		goto error_disable_reg;
515	}
516
517	ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
518	if (ret) {
519		ret = -EIO;
520		goto error_disable_reg;
521	}
522
523	if (client->irq > 0) {
524		ret = request_threaded_irq(client->irq,
525					   NULL,
526					   &ad7291_event_handler,
527					   IRQF_TRIGGER_LOW | IRQF_ONESHOT,
528					   id->name,
529					   indio_dev);
530		if (ret)
531			goto error_disable_reg;
532	}
533
534	ret = iio_device_register(indio_dev);
535	if (ret)
536		goto error_unreg_irq;
537
538	return 0;
539
540error_unreg_irq:
541	if (client->irq)
542		free_irq(client->irq, indio_dev);
543error_disable_reg:
544	if (chip->reg)
545		regulator_disable(chip->reg);
546
547	return ret;
548}
549
550static int ad7291_remove(struct i2c_client *client)
551{
552	struct iio_dev *indio_dev = i2c_get_clientdata(client);
553	struct ad7291_chip_info *chip = iio_priv(indio_dev);
554
555	iio_device_unregister(indio_dev);
556
557	if (client->irq)
558		free_irq(client->irq, indio_dev);
559
560	if (chip->reg)
561		regulator_disable(chip->reg);
562
563	return 0;
564}
565
566static const struct i2c_device_id ad7291_id[] = {
567	{ "ad7291", 0 },
568	{}
569};
570
571MODULE_DEVICE_TABLE(i2c, ad7291_id);
572
573static struct i2c_driver ad7291_driver = {
574	.driver = {
575		.name = KBUILD_MODNAME,
576	},
577	.probe = ad7291_probe,
578	.remove = ad7291_remove,
579	.id_table = ad7291_id,
580};
581module_i2c_driver(ad7291_driver);
582
583MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
584MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
585MODULE_LICENSE("GPL v2");
586