• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ADS1015 - Texas Instruments Analog-to-Digital Converter
3  *
4  * Copyright (c) 2016, Intel Corporation.
5  *
6  * This file is subject to the terms and conditions of version 2 of
7  * the GNU General Public License.  See the file COPYING in the main
8  * directory of this archive for more details.
9  *
10  * IIO driver for ADS1015 ADC 7-bit I2C slave address:
11  *	* 0x48 - ADDR connected to Ground
12  *	* 0x49 - ADDR connected to Vdd
13  *	* 0x4A - ADDR connected to SDA
14  *	* 0x4B - ADDR connected to SCL
15  */
16 
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/init.h>
20 #include <linux/irq.h>
21 #include <linux/i2c.h>
22 #include <linux/regmap.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/mutex.h>
25 #include <linux/delay.h>
26 
27 #include <linux/platform_data/ads1015.h>
28 
29 #include <linux/iio/iio.h>
30 #include <linux/iio/types.h>
31 #include <linux/iio/sysfs.h>
32 #include <linux/iio/events.h>
33 #include <linux/iio/buffer.h>
34 #include <linux/iio/triggered_buffer.h>
35 #include <linux/iio/trigger_consumer.h>
36 
37 #define ADS1015_DRV_NAME "ads1015"
38 
39 #define ADS1015_CONV_REG	0x00
40 #define ADS1015_CFG_REG		0x01
41 #define ADS1015_LO_THRESH_REG	0x02
42 #define ADS1015_HI_THRESH_REG	0x03
43 
44 #define ADS1015_CFG_COMP_QUE_SHIFT	0
45 #define ADS1015_CFG_COMP_LAT_SHIFT	2
46 #define ADS1015_CFG_COMP_POL_SHIFT	3
47 #define ADS1015_CFG_COMP_MODE_SHIFT	4
48 #define ADS1015_CFG_DR_SHIFT	5
49 #define ADS1015_CFG_MOD_SHIFT	8
50 #define ADS1015_CFG_PGA_SHIFT	9
51 #define ADS1015_CFG_MUX_SHIFT	12
52 
53 #define ADS1015_CFG_COMP_QUE_MASK	GENMASK(1, 0)
54 #define ADS1015_CFG_COMP_LAT_MASK	BIT(2)
55 #define ADS1015_CFG_COMP_POL_MASK	BIT(3)
56 #define ADS1015_CFG_COMP_MODE_MASK	BIT(4)
57 #define ADS1015_CFG_DR_MASK	GENMASK(7, 5)
58 #define ADS1015_CFG_MOD_MASK	BIT(8)
59 #define ADS1015_CFG_PGA_MASK	GENMASK(11, 9)
60 #define ADS1015_CFG_MUX_MASK	GENMASK(14, 12)
61 
62 /* Comparator queue and disable field */
63 #define ADS1015_CFG_COMP_DISABLE	3
64 
65 /* Comparator polarity field */
66 #define ADS1015_CFG_COMP_POL_LOW	0
67 #define ADS1015_CFG_COMP_POL_HIGH	1
68 
69 /* Comparator mode field */
70 #define ADS1015_CFG_COMP_MODE_TRAD	0
71 #define ADS1015_CFG_COMP_MODE_WINDOW	1
72 
73 /* device operating modes */
74 #define ADS1015_CONTINUOUS	0
75 #define ADS1015_SINGLESHOT	1
76 
77 #define ADS1015_SLEEP_DELAY_MS		2000
78 #define ADS1015_DEFAULT_PGA		2
79 #define ADS1015_DEFAULT_DATA_RATE	4
80 #define ADS1015_DEFAULT_CHAN		0
81 
82 enum chip_ids {
83 	ADS1015,
84 	ADS1115,
85 };
86 
87 enum ads1015_channels {
88 	ADS1015_AIN0_AIN1 = 0,
89 	ADS1015_AIN0_AIN3,
90 	ADS1015_AIN1_AIN3,
91 	ADS1015_AIN2_AIN3,
92 	ADS1015_AIN0,
93 	ADS1015_AIN1,
94 	ADS1015_AIN2,
95 	ADS1015_AIN3,
96 	ADS1015_TIMESTAMP,
97 };
98 
99 static const unsigned int ads1015_data_rate[] = {
100 	128, 250, 490, 920, 1600, 2400, 3300, 3300
101 };
102 
103 static const unsigned int ads1115_data_rate[] = {
104 	8, 16, 32, 64, 128, 250, 475, 860
105 };
106 
107 /*
108  * Translation from PGA bits to full-scale positive and negative input voltage
109  * range in mV
110  */
111 static int ads1015_fullscale_range[] = {
112 	6144, 4096, 2048, 1024, 512, 256, 256, 256
113 };
114 
115 /*
116  * Translation from COMP_QUE field value to the number of successive readings
117  * exceed the threshold values before an interrupt is generated
118  */
119 static const int ads1015_comp_queue[] = { 1, 2, 4 };
120 
121 static const struct iio_event_spec ads1015_events[] = {
122 	{
123 		.type = IIO_EV_TYPE_THRESH,
124 		.dir = IIO_EV_DIR_RISING,
125 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
126 				BIT(IIO_EV_INFO_ENABLE),
127 	}, {
128 		.type = IIO_EV_TYPE_THRESH,
129 		.dir = IIO_EV_DIR_FALLING,
130 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
131 	}, {
132 		.type = IIO_EV_TYPE_THRESH,
133 		.dir = IIO_EV_DIR_EITHER,
134 		.mask_separate = BIT(IIO_EV_INFO_ENABLE) |
135 				BIT(IIO_EV_INFO_PERIOD),
136 	},
137 };
138 
139 #define ADS1015_V_CHAN(_chan, _addr) {				\
140 	.type = IIO_VOLTAGE,					\
141 	.indexed = 1,						\
142 	.address = _addr,					\
143 	.channel = _chan,					\
144 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
145 				BIT(IIO_CHAN_INFO_SCALE) |	\
146 				BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
147 	.scan_index = _addr,					\
148 	.scan_type = {						\
149 		.sign = 's',					\
150 		.realbits = 12,					\
151 		.storagebits = 16,				\
152 		.shift = 4,					\
153 		.endianness = IIO_CPU,				\
154 	},							\
155 	.event_spec = ads1015_events,				\
156 	.num_event_specs = ARRAY_SIZE(ads1015_events),		\
157 	.datasheet_name = "AIN"#_chan,				\
158 }
159 
160 #define ADS1015_V_DIFF_CHAN(_chan, _chan2, _addr) {		\
161 	.type = IIO_VOLTAGE,					\
162 	.differential = 1,					\
163 	.indexed = 1,						\
164 	.address = _addr,					\
165 	.channel = _chan,					\
166 	.channel2 = _chan2,					\
167 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
168 				BIT(IIO_CHAN_INFO_SCALE) |	\
169 				BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
170 	.scan_index = _addr,					\
171 	.scan_type = {						\
172 		.sign = 's',					\
173 		.realbits = 12,					\
174 		.storagebits = 16,				\
175 		.shift = 4,					\
176 		.endianness = IIO_CPU,				\
177 	},							\
178 	.event_spec = ads1015_events,				\
179 	.num_event_specs = ARRAY_SIZE(ads1015_events),		\
180 	.datasheet_name = "AIN"#_chan"-AIN"#_chan2,		\
181 }
182 
183 #define ADS1115_V_CHAN(_chan, _addr) {				\
184 	.type = IIO_VOLTAGE,					\
185 	.indexed = 1,						\
186 	.address = _addr,					\
187 	.channel = _chan,					\
188 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
189 				BIT(IIO_CHAN_INFO_SCALE) |	\
190 				BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
191 	.scan_index = _addr,					\
192 	.scan_type = {						\
193 		.sign = 's',					\
194 		.realbits = 16,					\
195 		.storagebits = 16,				\
196 		.endianness = IIO_CPU,				\
197 	},							\
198 	.event_spec = ads1015_events,				\
199 	.num_event_specs = ARRAY_SIZE(ads1015_events),		\
200 	.datasheet_name = "AIN"#_chan,				\
201 }
202 
203 #define ADS1115_V_DIFF_CHAN(_chan, _chan2, _addr) {		\
204 	.type = IIO_VOLTAGE,					\
205 	.differential = 1,					\
206 	.indexed = 1,						\
207 	.address = _addr,					\
208 	.channel = _chan,					\
209 	.channel2 = _chan2,					\
210 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
211 				BIT(IIO_CHAN_INFO_SCALE) |	\
212 				BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
213 	.scan_index = _addr,					\
214 	.scan_type = {						\
215 		.sign = 's',					\
216 		.realbits = 16,					\
217 		.storagebits = 16,				\
218 		.endianness = IIO_CPU,				\
219 	},							\
220 	.event_spec = ads1015_events,				\
221 	.num_event_specs = ARRAY_SIZE(ads1015_events),		\
222 	.datasheet_name = "AIN"#_chan"-AIN"#_chan2,		\
223 }
224 
225 struct ads1015_thresh_data {
226 	unsigned int comp_queue;
227 	int high_thresh;
228 	int low_thresh;
229 };
230 
231 struct ads1015_data {
232 	struct regmap *regmap;
233 	/*
234 	 * Protects ADC ops, e.g: concurrent sysfs/buffered
235 	 * data reads, configuration updates
236 	 */
237 	struct mutex lock;
238 	struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
239 
240 	unsigned int event_channel;
241 	unsigned int comp_mode;
242 	struct ads1015_thresh_data thresh_data[ADS1015_CHANNELS];
243 
244 	unsigned int *data_rate;
245 	/*
246 	 * Set to true when the ADC is switched to the continuous-conversion
247 	 * mode and exits from a power-down state.  This flag is used to avoid
248 	 * getting the stale result from the conversion register.
249 	 */
250 	bool conv_invalid;
251 };
252 
ads1015_event_channel_enabled(struct ads1015_data * data)253 static bool ads1015_event_channel_enabled(struct ads1015_data *data)
254 {
255 	return (data->event_channel != ADS1015_CHANNELS);
256 }
257 
ads1015_event_channel_enable(struct ads1015_data * data,int chan,int comp_mode)258 static void ads1015_event_channel_enable(struct ads1015_data *data, int chan,
259 					 int comp_mode)
260 {
261 	WARN_ON(ads1015_event_channel_enabled(data));
262 
263 	data->event_channel = chan;
264 	data->comp_mode = comp_mode;
265 }
266 
ads1015_event_channel_disable(struct ads1015_data * data,int chan)267 static void ads1015_event_channel_disable(struct ads1015_data *data, int chan)
268 {
269 	data->event_channel = ADS1015_CHANNELS;
270 }
271 
ads1015_is_writeable_reg(struct device * dev,unsigned int reg)272 static bool ads1015_is_writeable_reg(struct device *dev, unsigned int reg)
273 {
274 	switch (reg) {
275 	case ADS1015_CFG_REG:
276 	case ADS1015_LO_THRESH_REG:
277 	case ADS1015_HI_THRESH_REG:
278 		return true;
279 	default:
280 		return false;
281 	}
282 }
283 
284 static const struct regmap_config ads1015_regmap_config = {
285 	.reg_bits = 8,
286 	.val_bits = 16,
287 	.max_register = ADS1015_HI_THRESH_REG,
288 	.writeable_reg = ads1015_is_writeable_reg,
289 };
290 
291 static const struct iio_chan_spec ads1015_channels[] = {
292 	ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
293 	ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
294 	ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
295 	ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
296 	ADS1015_V_CHAN(0, ADS1015_AIN0),
297 	ADS1015_V_CHAN(1, ADS1015_AIN1),
298 	ADS1015_V_CHAN(2, ADS1015_AIN2),
299 	ADS1015_V_CHAN(3, ADS1015_AIN3),
300 	IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
301 };
302 
303 static const struct iio_chan_spec ads1115_channels[] = {
304 	ADS1115_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
305 	ADS1115_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
306 	ADS1115_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
307 	ADS1115_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
308 	ADS1115_V_CHAN(0, ADS1015_AIN0),
309 	ADS1115_V_CHAN(1, ADS1015_AIN1),
310 	ADS1115_V_CHAN(2, ADS1015_AIN2),
311 	ADS1115_V_CHAN(3, ADS1015_AIN3),
312 	IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
313 };
314 
315 #ifdef CONFIG_PM
ads1015_set_power_state(struct ads1015_data * data,bool on)316 static int ads1015_set_power_state(struct ads1015_data *data, bool on)
317 {
318 	int ret;
319 	struct device *dev = regmap_get_device(data->regmap);
320 
321 	if (on) {
322 		ret = pm_runtime_get_sync(dev);
323 		if (ret < 0)
324 			pm_runtime_put_noidle(dev);
325 	} else {
326 		pm_runtime_mark_last_busy(dev);
327 		ret = pm_runtime_put_autosuspend(dev);
328 	}
329 
330 	return ret < 0 ? ret : 0;
331 }
332 
333 #else /* !CONFIG_PM */
334 
ads1015_set_power_state(struct ads1015_data * data,bool on)335 static int ads1015_set_power_state(struct ads1015_data *data, bool on)
336 {
337 	return 0;
338 }
339 
340 #endif /* !CONFIG_PM */
341 
342 static
ads1015_get_adc_result(struct ads1015_data * data,int chan,int * val)343 int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val)
344 {
345 	int ret, pga, dr, dr_old, conv_time;
346 	unsigned int old, mask, cfg;
347 
348 	if (chan < 0 || chan >= ADS1015_CHANNELS)
349 		return -EINVAL;
350 
351 	ret = regmap_read(data->regmap, ADS1015_CFG_REG, &old);
352 	if (ret)
353 		return ret;
354 
355 	pga = data->channel_data[chan].pga;
356 	dr = data->channel_data[chan].data_rate;
357 	mask = ADS1015_CFG_MUX_MASK | ADS1015_CFG_PGA_MASK |
358 		ADS1015_CFG_DR_MASK;
359 	cfg = chan << ADS1015_CFG_MUX_SHIFT | pga << ADS1015_CFG_PGA_SHIFT |
360 		dr << ADS1015_CFG_DR_SHIFT;
361 
362 	if (ads1015_event_channel_enabled(data)) {
363 		mask |= ADS1015_CFG_COMP_QUE_MASK | ADS1015_CFG_COMP_MODE_MASK;
364 		cfg |= data->thresh_data[chan].comp_queue <<
365 				ADS1015_CFG_COMP_QUE_SHIFT |
366 			data->comp_mode <<
367 				ADS1015_CFG_COMP_MODE_SHIFT;
368 	}
369 
370 	cfg = (old & ~mask) | (cfg & mask);
371 	if (old != cfg) {
372 		ret = regmap_write(data->regmap, ADS1015_CFG_REG, cfg);
373 		if (ret)
374 			return ret;
375 		data->conv_invalid = true;
376 	}
377 	if (data->conv_invalid) {
378 		dr_old = (old & ADS1015_CFG_DR_MASK) >> ADS1015_CFG_DR_SHIFT;
379 		conv_time = DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr_old]);
380 		conv_time += DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr]);
381 		conv_time += conv_time / 10; /* 10% internal clock inaccuracy */
382 		usleep_range(conv_time, conv_time + 1);
383 		data->conv_invalid = false;
384 	}
385 
386 	return regmap_read(data->regmap, ADS1015_CONV_REG, val);
387 }
388 
ads1015_trigger_handler(int irq,void * p)389 static irqreturn_t ads1015_trigger_handler(int irq, void *p)
390 {
391 	struct iio_poll_func *pf = p;
392 	struct iio_dev *indio_dev = pf->indio_dev;
393 	struct ads1015_data *data = iio_priv(indio_dev);
394 	s16 buf[8]; /* 1x s16 ADC val + 3x s16 padding +  4x s16 timestamp */
395 	int chan, ret, res;
396 
397 	memset(buf, 0, sizeof(buf));
398 
399 	mutex_lock(&data->lock);
400 	chan = find_first_bit(indio_dev->active_scan_mask,
401 			      indio_dev->masklength);
402 	ret = ads1015_get_adc_result(data, chan, &res);
403 	if (ret < 0) {
404 		mutex_unlock(&data->lock);
405 		goto err;
406 	}
407 
408 	buf[0] = res;
409 	mutex_unlock(&data->lock);
410 
411 	iio_push_to_buffers_with_timestamp(indio_dev, buf,
412 					   iio_get_time_ns(indio_dev));
413 
414 err:
415 	iio_trigger_notify_done(indio_dev->trig);
416 
417 	return IRQ_HANDLED;
418 }
419 
ads1015_set_scale(struct ads1015_data * data,struct iio_chan_spec const * chan,int scale,int uscale)420 static int ads1015_set_scale(struct ads1015_data *data,
421 			     struct iio_chan_spec const *chan,
422 			     int scale, int uscale)
423 {
424 	int i;
425 	int fullscale = div_s64((scale * 1000000LL + uscale) <<
426 				(chan->scan_type.realbits - 1), 1000000);
427 
428 	for (i = 0; i < ARRAY_SIZE(ads1015_fullscale_range); i++) {
429 		if (ads1015_fullscale_range[i] == fullscale) {
430 			data->channel_data[chan->address].pga = i;
431 			return 0;
432 		}
433 	}
434 
435 	return -EINVAL;
436 }
437 
ads1015_set_data_rate(struct ads1015_data * data,int chan,int rate)438 static int ads1015_set_data_rate(struct ads1015_data *data, int chan, int rate)
439 {
440 	int i;
441 
442 	for (i = 0; i < ARRAY_SIZE(ads1015_data_rate); i++) {
443 		if (data->data_rate[i] == rate) {
444 			data->channel_data[chan].data_rate = i;
445 			return 0;
446 		}
447 	}
448 
449 	return -EINVAL;
450 }
451 
ads1015_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)452 static int ads1015_read_raw(struct iio_dev *indio_dev,
453 			    struct iio_chan_spec const *chan, int *val,
454 			    int *val2, long mask)
455 {
456 	int ret, idx;
457 	struct ads1015_data *data = iio_priv(indio_dev);
458 
459 	mutex_lock(&data->lock);
460 	switch (mask) {
461 	case IIO_CHAN_INFO_RAW: {
462 		int shift = chan->scan_type.shift;
463 
464 		ret = iio_device_claim_direct_mode(indio_dev);
465 		if (ret)
466 			break;
467 
468 		if (ads1015_event_channel_enabled(data) &&
469 				data->event_channel != chan->address) {
470 			ret = -EBUSY;
471 			goto release_direct;
472 		}
473 
474 		ret = ads1015_set_power_state(data, true);
475 		if (ret < 0)
476 			goto release_direct;
477 
478 		ret = ads1015_get_adc_result(data, chan->address, val);
479 		if (ret < 0) {
480 			ads1015_set_power_state(data, false);
481 			goto release_direct;
482 		}
483 
484 		*val = sign_extend32(*val >> shift, 15 - shift);
485 
486 		ret = ads1015_set_power_state(data, false);
487 		if (ret < 0)
488 			goto release_direct;
489 
490 		ret = IIO_VAL_INT;
491 release_direct:
492 		iio_device_release_direct_mode(indio_dev);
493 		break;
494 	}
495 	case IIO_CHAN_INFO_SCALE:
496 		idx = data->channel_data[chan->address].pga;
497 		*val = ads1015_fullscale_range[idx];
498 		*val2 = chan->scan_type.realbits - 1;
499 		ret = IIO_VAL_FRACTIONAL_LOG2;
500 		break;
501 	case IIO_CHAN_INFO_SAMP_FREQ:
502 		idx = data->channel_data[chan->address].data_rate;
503 		*val = data->data_rate[idx];
504 		ret = IIO_VAL_INT;
505 		break;
506 	default:
507 		ret = -EINVAL;
508 		break;
509 	}
510 	mutex_unlock(&data->lock);
511 
512 	return ret;
513 }
514 
ads1015_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)515 static int ads1015_write_raw(struct iio_dev *indio_dev,
516 			     struct iio_chan_spec const *chan, int val,
517 			     int val2, long mask)
518 {
519 	struct ads1015_data *data = iio_priv(indio_dev);
520 	int ret;
521 
522 	mutex_lock(&data->lock);
523 	switch (mask) {
524 	case IIO_CHAN_INFO_SCALE:
525 		ret = ads1015_set_scale(data, chan, val, val2);
526 		break;
527 	case IIO_CHAN_INFO_SAMP_FREQ:
528 		ret = ads1015_set_data_rate(data, chan->address, val);
529 		break;
530 	default:
531 		ret = -EINVAL;
532 		break;
533 	}
534 	mutex_unlock(&data->lock);
535 
536 	return ret;
537 }
538 
ads1015_read_event(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)539 static int ads1015_read_event(struct iio_dev *indio_dev,
540 	const struct iio_chan_spec *chan, enum iio_event_type type,
541 	enum iio_event_direction dir, enum iio_event_info info, int *val,
542 	int *val2)
543 {
544 	struct ads1015_data *data = iio_priv(indio_dev);
545 	int ret;
546 	unsigned int comp_queue;
547 	int period;
548 	int dr;
549 
550 	mutex_lock(&data->lock);
551 
552 	switch (info) {
553 	case IIO_EV_INFO_VALUE:
554 		*val = (dir == IIO_EV_DIR_RISING) ?
555 			data->thresh_data[chan->address].high_thresh :
556 			data->thresh_data[chan->address].low_thresh;
557 		ret = IIO_VAL_INT;
558 		break;
559 	case IIO_EV_INFO_PERIOD:
560 		dr = data->channel_data[chan->address].data_rate;
561 		comp_queue = data->thresh_data[chan->address].comp_queue;
562 		period = ads1015_comp_queue[comp_queue] *
563 			USEC_PER_SEC / data->data_rate[dr];
564 
565 		*val = period / USEC_PER_SEC;
566 		*val2 = period % USEC_PER_SEC;
567 		ret = IIO_VAL_INT_PLUS_MICRO;
568 		break;
569 	default:
570 		ret = -EINVAL;
571 		break;
572 	}
573 
574 	mutex_unlock(&data->lock);
575 
576 	return ret;
577 }
578 
ads1015_write_event(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)579 static int ads1015_write_event(struct iio_dev *indio_dev,
580 	const struct iio_chan_spec *chan, enum iio_event_type type,
581 	enum iio_event_direction dir, enum iio_event_info info, int val,
582 	int val2)
583 {
584 	struct ads1015_data *data = iio_priv(indio_dev);
585 	int realbits = chan->scan_type.realbits;
586 	int ret = 0;
587 	long long period;
588 	int i;
589 	int dr;
590 
591 	mutex_lock(&data->lock);
592 
593 	switch (info) {
594 	case IIO_EV_INFO_VALUE:
595 		if (val >= 1 << (realbits - 1) || val < -1 << (realbits - 1)) {
596 			ret = -EINVAL;
597 			break;
598 		}
599 		if (dir == IIO_EV_DIR_RISING)
600 			data->thresh_data[chan->address].high_thresh = val;
601 		else
602 			data->thresh_data[chan->address].low_thresh = val;
603 		break;
604 	case IIO_EV_INFO_PERIOD:
605 		dr = data->channel_data[chan->address].data_rate;
606 		period = val * USEC_PER_SEC + val2;
607 
608 		for (i = 0; i < ARRAY_SIZE(ads1015_comp_queue) - 1; i++) {
609 			if (period <= ads1015_comp_queue[i] *
610 					USEC_PER_SEC / data->data_rate[dr])
611 				break;
612 		}
613 		data->thresh_data[chan->address].comp_queue = i;
614 		break;
615 	default:
616 		ret = -EINVAL;
617 		break;
618 	}
619 
620 	mutex_unlock(&data->lock);
621 
622 	return ret;
623 }
624 
ads1015_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)625 static int ads1015_read_event_config(struct iio_dev *indio_dev,
626 	const struct iio_chan_spec *chan, enum iio_event_type type,
627 	enum iio_event_direction dir)
628 {
629 	struct ads1015_data *data = iio_priv(indio_dev);
630 	int ret = 0;
631 
632 	mutex_lock(&data->lock);
633 	if (data->event_channel == chan->address) {
634 		switch (dir) {
635 		case IIO_EV_DIR_RISING:
636 			ret = 1;
637 			break;
638 		case IIO_EV_DIR_EITHER:
639 			ret = (data->comp_mode == ADS1015_CFG_COMP_MODE_WINDOW);
640 			break;
641 		default:
642 			ret = -EINVAL;
643 			break;
644 		}
645 	}
646 	mutex_unlock(&data->lock);
647 
648 	return ret;
649 }
650 
ads1015_enable_event_config(struct ads1015_data * data,const struct iio_chan_spec * chan,int comp_mode)651 static int ads1015_enable_event_config(struct ads1015_data *data,
652 	const struct iio_chan_spec *chan, int comp_mode)
653 {
654 	int low_thresh = data->thresh_data[chan->address].low_thresh;
655 	int high_thresh = data->thresh_data[chan->address].high_thresh;
656 	int ret;
657 	unsigned int val;
658 
659 	if (ads1015_event_channel_enabled(data)) {
660 		if (data->event_channel != chan->address ||
661 			(data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD &&
662 				comp_mode == ADS1015_CFG_COMP_MODE_WINDOW))
663 			return -EBUSY;
664 
665 		return 0;
666 	}
667 
668 	if (comp_mode == ADS1015_CFG_COMP_MODE_TRAD) {
669 		low_thresh = max(-1 << (chan->scan_type.realbits - 1),
670 				high_thresh - 1);
671 	}
672 	ret = regmap_write(data->regmap, ADS1015_LO_THRESH_REG,
673 			low_thresh << chan->scan_type.shift);
674 	if (ret)
675 		return ret;
676 
677 	ret = regmap_write(data->regmap, ADS1015_HI_THRESH_REG,
678 			high_thresh << chan->scan_type.shift);
679 	if (ret)
680 		return ret;
681 
682 	ret = ads1015_set_power_state(data, true);
683 	if (ret < 0)
684 		return ret;
685 
686 	ads1015_event_channel_enable(data, chan->address, comp_mode);
687 
688 	ret = ads1015_get_adc_result(data, chan->address, &val);
689 	if (ret) {
690 		ads1015_event_channel_disable(data, chan->address);
691 		ads1015_set_power_state(data, false);
692 	}
693 
694 	return ret;
695 }
696 
ads1015_disable_event_config(struct ads1015_data * data,const struct iio_chan_spec * chan,int comp_mode)697 static int ads1015_disable_event_config(struct ads1015_data *data,
698 	const struct iio_chan_spec *chan, int comp_mode)
699 {
700 	int ret;
701 
702 	if (!ads1015_event_channel_enabled(data))
703 		return 0;
704 
705 	if (data->event_channel != chan->address)
706 		return 0;
707 
708 	if (data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD &&
709 			comp_mode == ADS1015_CFG_COMP_MODE_WINDOW)
710 		return 0;
711 
712 	ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
713 				ADS1015_CFG_COMP_QUE_MASK,
714 				ADS1015_CFG_COMP_DISABLE <<
715 					ADS1015_CFG_COMP_QUE_SHIFT);
716 	if (ret)
717 		return ret;
718 
719 	ads1015_event_channel_disable(data, chan->address);
720 
721 	return ads1015_set_power_state(data, false);
722 }
723 
ads1015_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int state)724 static int ads1015_write_event_config(struct iio_dev *indio_dev,
725 	const struct iio_chan_spec *chan, enum iio_event_type type,
726 	enum iio_event_direction dir, int state)
727 {
728 	struct ads1015_data *data = iio_priv(indio_dev);
729 	int ret;
730 	int comp_mode = (dir == IIO_EV_DIR_EITHER) ?
731 		ADS1015_CFG_COMP_MODE_WINDOW : ADS1015_CFG_COMP_MODE_TRAD;
732 
733 	mutex_lock(&data->lock);
734 
735 	/* Prevent from enabling both buffer and event at a time */
736 	ret = iio_device_claim_direct_mode(indio_dev);
737 	if (ret) {
738 		mutex_unlock(&data->lock);
739 		return ret;
740 	}
741 
742 	if (state)
743 		ret = ads1015_enable_event_config(data, chan, comp_mode);
744 	else
745 		ret = ads1015_disable_event_config(data, chan, comp_mode);
746 
747 	iio_device_release_direct_mode(indio_dev);
748 	mutex_unlock(&data->lock);
749 
750 	return ret;
751 }
752 
ads1015_event_handler(int irq,void * priv)753 static irqreturn_t ads1015_event_handler(int irq, void *priv)
754 {
755 	struct iio_dev *indio_dev = priv;
756 	struct ads1015_data *data = iio_priv(indio_dev);
757 	int val;
758 	int ret;
759 
760 	/* Clear the latched ALERT/RDY pin */
761 	ret = regmap_read(data->regmap, ADS1015_CONV_REG, &val);
762 	if (ret)
763 		return IRQ_HANDLED;
764 
765 	if (ads1015_event_channel_enabled(data)) {
766 		enum iio_event_direction dir;
767 		u64 code;
768 
769 		dir = data->comp_mode == ADS1015_CFG_COMP_MODE_TRAD ?
770 					IIO_EV_DIR_RISING : IIO_EV_DIR_EITHER;
771 		code = IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, data->event_channel,
772 					IIO_EV_TYPE_THRESH, dir);
773 		iio_push_event(indio_dev, code, iio_get_time_ns(indio_dev));
774 	}
775 
776 	return IRQ_HANDLED;
777 }
778 
ads1015_buffer_preenable(struct iio_dev * indio_dev)779 static int ads1015_buffer_preenable(struct iio_dev *indio_dev)
780 {
781 	struct ads1015_data *data = iio_priv(indio_dev);
782 
783 	/* Prevent from enabling both buffer and event at a time */
784 	if (ads1015_event_channel_enabled(data))
785 		return -EBUSY;
786 
787 	return ads1015_set_power_state(iio_priv(indio_dev), true);
788 }
789 
ads1015_buffer_postdisable(struct iio_dev * indio_dev)790 static int ads1015_buffer_postdisable(struct iio_dev *indio_dev)
791 {
792 	return ads1015_set_power_state(iio_priv(indio_dev), false);
793 }
794 
795 static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = {
796 	.preenable	= ads1015_buffer_preenable,
797 	.postenable	= iio_triggered_buffer_postenable,
798 	.predisable	= iio_triggered_buffer_predisable,
799 	.postdisable	= ads1015_buffer_postdisable,
800 	.validate_scan_mask = &iio_validate_scan_mask_onehot,
801 };
802 
803 static IIO_CONST_ATTR_NAMED(ads1015_scale_available, scale_available,
804 	"3 2 1 0.5 0.25 0.125");
805 static IIO_CONST_ATTR_NAMED(ads1115_scale_available, scale_available,
806 	"0.1875 0.125 0.0625 0.03125 0.015625 0.007813");
807 
808 static IIO_CONST_ATTR_NAMED(ads1015_sampling_frequency_available,
809 	sampling_frequency_available, "128 250 490 920 1600 2400 3300");
810 static IIO_CONST_ATTR_NAMED(ads1115_sampling_frequency_available,
811 	sampling_frequency_available, "8 16 32 64 128 250 475 860");
812 
813 static struct attribute *ads1015_attributes[] = {
814 	&iio_const_attr_ads1015_scale_available.dev_attr.attr,
815 	&iio_const_attr_ads1015_sampling_frequency_available.dev_attr.attr,
816 	NULL,
817 };
818 
819 static const struct attribute_group ads1015_attribute_group = {
820 	.attrs = ads1015_attributes,
821 };
822 
823 static struct attribute *ads1115_attributes[] = {
824 	&iio_const_attr_ads1115_scale_available.dev_attr.attr,
825 	&iio_const_attr_ads1115_sampling_frequency_available.dev_attr.attr,
826 	NULL,
827 };
828 
829 static const struct attribute_group ads1115_attribute_group = {
830 	.attrs = ads1115_attributes,
831 };
832 
833 static const struct iio_info ads1015_info = {
834 	.read_raw	= ads1015_read_raw,
835 	.write_raw	= ads1015_write_raw,
836 	.read_event_value = ads1015_read_event,
837 	.write_event_value = ads1015_write_event,
838 	.read_event_config = ads1015_read_event_config,
839 	.write_event_config = ads1015_write_event_config,
840 	.attrs          = &ads1015_attribute_group,
841 };
842 
843 static const struct iio_info ads1115_info = {
844 	.read_raw	= ads1015_read_raw,
845 	.write_raw	= ads1015_write_raw,
846 	.read_event_value = ads1015_read_event,
847 	.write_event_value = ads1015_write_event,
848 	.read_event_config = ads1015_read_event_config,
849 	.write_event_config = ads1015_write_event_config,
850 	.attrs          = &ads1115_attribute_group,
851 };
852 
853 #ifdef CONFIG_OF
ads1015_get_channels_config_of(struct i2c_client * client)854 static int ads1015_get_channels_config_of(struct i2c_client *client)
855 {
856 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
857 	struct ads1015_data *data = iio_priv(indio_dev);
858 	struct device_node *node;
859 
860 	if (!client->dev.of_node ||
861 	    !of_get_next_child(client->dev.of_node, NULL))
862 		return -EINVAL;
863 
864 	for_each_child_of_node(client->dev.of_node, node) {
865 		u32 pval;
866 		unsigned int channel;
867 		unsigned int pga = ADS1015_DEFAULT_PGA;
868 		unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
869 
870 		if (of_property_read_u32(node, "reg", &pval)) {
871 			dev_err(&client->dev, "invalid reg on %pOF\n",
872 				node);
873 			continue;
874 		}
875 
876 		channel = pval;
877 		if (channel >= ADS1015_CHANNELS) {
878 			dev_err(&client->dev,
879 				"invalid channel index %d on %pOF\n",
880 				channel, node);
881 			continue;
882 		}
883 
884 		if (!of_property_read_u32(node, "ti,gain", &pval)) {
885 			pga = pval;
886 			if (pga > 6) {
887 				dev_err(&client->dev, "invalid gain on %pOF\n",
888 					node);
889 				of_node_put(node);
890 				return -EINVAL;
891 			}
892 		}
893 
894 		if (!of_property_read_u32(node, "ti,datarate", &pval)) {
895 			data_rate = pval;
896 			if (data_rate > 7) {
897 				dev_err(&client->dev,
898 					"invalid data_rate on %pOF\n",
899 					node);
900 				of_node_put(node);
901 				return -EINVAL;
902 			}
903 		}
904 
905 		data->channel_data[channel].pga = pga;
906 		data->channel_data[channel].data_rate = data_rate;
907 	}
908 
909 	return 0;
910 }
911 #endif
912 
ads1015_get_channels_config(struct i2c_client * client)913 static void ads1015_get_channels_config(struct i2c_client *client)
914 {
915 	unsigned int k;
916 
917 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
918 	struct ads1015_data *data = iio_priv(indio_dev);
919 	struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
920 
921 	/* prefer platform data */
922 	if (pdata) {
923 		memcpy(data->channel_data, pdata->channel_data,
924 		       sizeof(data->channel_data));
925 		return;
926 	}
927 
928 #ifdef CONFIG_OF
929 	if (!ads1015_get_channels_config_of(client))
930 		return;
931 #endif
932 	/* fallback on default configuration */
933 	for (k = 0; k < ADS1015_CHANNELS; ++k) {
934 		data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
935 		data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
936 	}
937 }
938 
ads1015_set_conv_mode(struct ads1015_data * data,int mode)939 static int ads1015_set_conv_mode(struct ads1015_data *data, int mode)
940 {
941 	return regmap_update_bits(data->regmap, ADS1015_CFG_REG,
942 				  ADS1015_CFG_MOD_MASK,
943 				  mode << ADS1015_CFG_MOD_SHIFT);
944 }
945 
ads1015_probe(struct i2c_client * client,const struct i2c_device_id * id)946 static int ads1015_probe(struct i2c_client *client,
947 			 const struct i2c_device_id *id)
948 {
949 	struct iio_dev *indio_dev;
950 	struct ads1015_data *data;
951 	int ret;
952 	enum chip_ids chip;
953 	int i;
954 
955 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
956 	if (!indio_dev)
957 		return -ENOMEM;
958 
959 	data = iio_priv(indio_dev);
960 	i2c_set_clientdata(client, indio_dev);
961 
962 	mutex_init(&data->lock);
963 
964 	indio_dev->dev.parent = &client->dev;
965 	indio_dev->dev.of_node = client->dev.of_node;
966 	indio_dev->name = ADS1015_DRV_NAME;
967 	indio_dev->modes = INDIO_DIRECT_MODE;
968 
969 	if (client->dev.of_node)
970 		chip = (enum chip_ids)of_device_get_match_data(&client->dev);
971 	else
972 		chip = id->driver_data;
973 	switch (chip) {
974 	case ADS1015:
975 		indio_dev->channels = ads1015_channels;
976 		indio_dev->num_channels = ARRAY_SIZE(ads1015_channels);
977 		indio_dev->info = &ads1015_info;
978 		data->data_rate = (unsigned int *) &ads1015_data_rate;
979 		break;
980 	case ADS1115:
981 		indio_dev->channels = ads1115_channels;
982 		indio_dev->num_channels = ARRAY_SIZE(ads1115_channels);
983 		indio_dev->info = &ads1115_info;
984 		data->data_rate = (unsigned int *) &ads1115_data_rate;
985 		break;
986 	}
987 
988 	data->event_channel = ADS1015_CHANNELS;
989 	/*
990 	 * Set default lower and upper threshold to min and max value
991 	 * respectively.
992 	 */
993 	for (i = 0; i < ADS1015_CHANNELS; i++) {
994 		int realbits = indio_dev->channels[i].scan_type.realbits;
995 
996 		data->thresh_data[i].low_thresh = -1 << (realbits - 1);
997 		data->thresh_data[i].high_thresh = (1 << (realbits - 1)) - 1;
998 	}
999 
1000 	/* we need to keep this ABI the same as used by hwmon ADS1015 driver */
1001 	ads1015_get_channels_config(client);
1002 
1003 	data->regmap = devm_regmap_init_i2c(client, &ads1015_regmap_config);
1004 	if (IS_ERR(data->regmap)) {
1005 		dev_err(&client->dev, "Failed to allocate register map\n");
1006 		return PTR_ERR(data->regmap);
1007 	}
1008 
1009 	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
1010 					      ads1015_trigger_handler,
1011 					      &ads1015_buffer_setup_ops);
1012 	if (ret < 0) {
1013 		dev_err(&client->dev, "iio triggered buffer setup failed\n");
1014 		return ret;
1015 	}
1016 
1017 	if (client->irq) {
1018 		unsigned long irq_trig =
1019 			irqd_get_trigger_type(irq_get_irq_data(client->irq));
1020 		unsigned int cfg_comp_mask = ADS1015_CFG_COMP_QUE_MASK |
1021 			ADS1015_CFG_COMP_LAT_MASK | ADS1015_CFG_COMP_POL_MASK;
1022 		unsigned int cfg_comp =
1023 			ADS1015_CFG_COMP_DISABLE << ADS1015_CFG_COMP_QUE_SHIFT |
1024 			1 << ADS1015_CFG_COMP_LAT_SHIFT;
1025 
1026 		switch (irq_trig) {
1027 		case IRQF_TRIGGER_LOW:
1028 			cfg_comp |= ADS1015_CFG_COMP_POL_LOW <<
1029 					ADS1015_CFG_COMP_POL_SHIFT;
1030 			break;
1031 		case IRQF_TRIGGER_HIGH:
1032 			cfg_comp |= ADS1015_CFG_COMP_POL_HIGH <<
1033 					ADS1015_CFG_COMP_POL_SHIFT;
1034 			break;
1035 		default:
1036 			return -EINVAL;
1037 		}
1038 
1039 		ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
1040 					cfg_comp_mask, cfg_comp);
1041 		if (ret)
1042 			return ret;
1043 
1044 		ret = devm_request_threaded_irq(&client->dev, client->irq,
1045 						NULL, ads1015_event_handler,
1046 						irq_trig | IRQF_ONESHOT,
1047 						client->name, indio_dev);
1048 		if (ret)
1049 			return ret;
1050 	}
1051 
1052 	ret = ads1015_set_conv_mode(data, ADS1015_CONTINUOUS);
1053 	if (ret)
1054 		return ret;
1055 
1056 	data->conv_invalid = true;
1057 
1058 	ret = pm_runtime_set_active(&client->dev);
1059 	if (ret)
1060 		return ret;
1061 	pm_runtime_set_autosuspend_delay(&client->dev, ADS1015_SLEEP_DELAY_MS);
1062 	pm_runtime_use_autosuspend(&client->dev);
1063 	pm_runtime_enable(&client->dev);
1064 
1065 	ret = iio_device_register(indio_dev);
1066 	if (ret < 0) {
1067 		dev_err(&client->dev, "Failed to register IIO device\n");
1068 		return ret;
1069 	}
1070 
1071 	return 0;
1072 }
1073 
ads1015_remove(struct i2c_client * client)1074 static int ads1015_remove(struct i2c_client *client)
1075 {
1076 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1077 	struct ads1015_data *data = iio_priv(indio_dev);
1078 
1079 	iio_device_unregister(indio_dev);
1080 
1081 	pm_runtime_disable(&client->dev);
1082 	pm_runtime_set_suspended(&client->dev);
1083 	pm_runtime_put_noidle(&client->dev);
1084 
1085 	/* power down single shot mode */
1086 	return ads1015_set_conv_mode(data, ADS1015_SINGLESHOT);
1087 }
1088 
1089 #ifdef CONFIG_PM
ads1015_runtime_suspend(struct device * dev)1090 static int ads1015_runtime_suspend(struct device *dev)
1091 {
1092 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1093 	struct ads1015_data *data = iio_priv(indio_dev);
1094 
1095 	return ads1015_set_conv_mode(data, ADS1015_SINGLESHOT);
1096 }
1097 
ads1015_runtime_resume(struct device * dev)1098 static int ads1015_runtime_resume(struct device *dev)
1099 {
1100 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1101 	struct ads1015_data *data = iio_priv(indio_dev);
1102 	int ret;
1103 
1104 	ret = ads1015_set_conv_mode(data, ADS1015_CONTINUOUS);
1105 	if (!ret)
1106 		data->conv_invalid = true;
1107 
1108 	return ret;
1109 }
1110 #endif
1111 
1112 static const struct dev_pm_ops ads1015_pm_ops = {
1113 	SET_RUNTIME_PM_OPS(ads1015_runtime_suspend,
1114 			   ads1015_runtime_resume, NULL)
1115 };
1116 
1117 static const struct i2c_device_id ads1015_id[] = {
1118 	{"ads1015", ADS1015},
1119 	{"ads1115", ADS1115},
1120 	{}
1121 };
1122 MODULE_DEVICE_TABLE(i2c, ads1015_id);
1123 
1124 static const struct of_device_id ads1015_of_match[] = {
1125 	{
1126 		.compatible = "ti,ads1015",
1127 		.data = (void *)ADS1015
1128 	},
1129 	{
1130 		.compatible = "ti,ads1115",
1131 		.data = (void *)ADS1115
1132 	},
1133 	{}
1134 };
1135 MODULE_DEVICE_TABLE(of, ads1015_of_match);
1136 
1137 static struct i2c_driver ads1015_driver = {
1138 	.driver = {
1139 		.name = ADS1015_DRV_NAME,
1140 		.of_match_table = ads1015_of_match,
1141 		.pm = &ads1015_pm_ops,
1142 	},
1143 	.probe		= ads1015_probe,
1144 	.remove		= ads1015_remove,
1145 	.id_table	= ads1015_id,
1146 };
1147 
1148 module_i2c_driver(ads1015_driver);
1149 
1150 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1151 MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver");
1152 MODULE_LICENSE("GPL v2");
1153