2 * ADS1015 - Texas Instruments Analog-to-Digital Converter
4 * Copyright (c) 2016, Intel Corporation.
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.
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
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/init.h>
20 #include <linux/i2c.h>
21 #include <linux/regmap.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/mutex.h>
24 #include <linux/delay.h>
26 #include <linux/platform_data/ads1015.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/types.h>
30 #include <linux/iio/sysfs.h>
31 #include <linux/iio/buffer.h>
32 #include <linux/iio/triggered_buffer.h>
33 #include <linux/iio/trigger_consumer.h>
35 #define ADS1015_DRV_NAME "ads1015"
37 #define ADS1015_CONV_REG 0x00
38 #define ADS1015_CFG_REG 0x01
40 #define ADS1015_CFG_DR_SHIFT 5
41 #define ADS1015_CFG_MOD_SHIFT 8
42 #define ADS1015_CFG_PGA_SHIFT 9
43 #define ADS1015_CFG_MUX_SHIFT 12
45 #define ADS1015_CFG_DR_MASK GENMASK(7, 5)
46 #define ADS1015_CFG_MOD_MASK BIT(8)
47 #define ADS1015_CFG_PGA_MASK GENMASK(11, 9)
48 #define ADS1015_CFG_MUX_MASK GENMASK(14, 12)
50 /* device operating modes */
51 #define ADS1015_CONTINUOUS 0
52 #define ADS1015_SINGLESHOT 1
54 #define ADS1015_SLEEP_DELAY_MS 2000
55 #define ADS1015_DEFAULT_PGA 2
56 #define ADS1015_DEFAULT_DATA_RATE 4
57 #define ADS1015_DEFAULT_CHAN 0
64 enum ads1015_channels
{
65 ADS1015_AIN0_AIN1
= 0,
76 static const unsigned int ads1015_data_rate
[] = {
77 128, 250, 490, 920, 1600, 2400, 3300, 3300
80 static const unsigned int ads1115_data_rate
[] = {
81 8, 16, 32, 64, 128, 250, 475, 860
98 #define ADS1015_V_CHAN(_chan, _addr) { \
99 .type = IIO_VOLTAGE, \
103 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
104 BIT(IIO_CHAN_INFO_SCALE) | \
105 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
106 .scan_index = _addr, \
112 .endianness = IIO_CPU, \
114 .datasheet_name = "AIN"#_chan, \
117 #define ADS1015_V_DIFF_CHAN(_chan, _chan2, _addr) { \
118 .type = IIO_VOLTAGE, \
123 .channel2 = _chan2, \
124 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
125 BIT(IIO_CHAN_INFO_SCALE) | \
126 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
127 .scan_index = _addr, \
133 .endianness = IIO_CPU, \
135 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
138 #define ADS1115_V_CHAN(_chan, _addr) { \
139 .type = IIO_VOLTAGE, \
143 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
144 BIT(IIO_CHAN_INFO_SCALE) | \
145 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
146 .scan_index = _addr, \
151 .endianness = IIO_CPU, \
153 .datasheet_name = "AIN"#_chan, \
156 #define ADS1115_V_DIFF_CHAN(_chan, _chan2, _addr) { \
157 .type = IIO_VOLTAGE, \
162 .channel2 = _chan2, \
163 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
164 BIT(IIO_CHAN_INFO_SCALE) | \
165 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
166 .scan_index = _addr, \
171 .endianness = IIO_CPU, \
173 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
176 struct ads1015_data
{
177 struct regmap
*regmap
;
179 * Protects ADC ops, e.g: concurrent sysfs/buffered
180 * data reads, configuration updates
183 struct ads1015_channel_data channel_data
[ADS1015_CHANNELS
];
185 unsigned int *data_rate
;
188 static bool ads1015_is_writeable_reg(struct device
*dev
, unsigned int reg
)
190 return (reg
== ADS1015_CFG_REG
);
193 static const struct regmap_config ads1015_regmap_config
= {
196 .max_register
= ADS1015_CFG_REG
,
197 .writeable_reg
= ads1015_is_writeable_reg
,
200 static const struct iio_chan_spec ads1015_channels
[] = {
201 ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1
),
202 ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3
),
203 ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3
),
204 ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3
),
205 ADS1015_V_CHAN(0, ADS1015_AIN0
),
206 ADS1015_V_CHAN(1, ADS1015_AIN1
),
207 ADS1015_V_CHAN(2, ADS1015_AIN2
),
208 ADS1015_V_CHAN(3, ADS1015_AIN3
),
209 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP
),
212 static const struct iio_chan_spec ads1115_channels
[] = {
213 ADS1115_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1
),
214 ADS1115_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3
),
215 ADS1115_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3
),
216 ADS1115_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3
),
217 ADS1115_V_CHAN(0, ADS1015_AIN0
),
218 ADS1115_V_CHAN(1, ADS1015_AIN1
),
219 ADS1115_V_CHAN(2, ADS1015_AIN2
),
220 ADS1115_V_CHAN(3, ADS1015_AIN3
),
221 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP
),
224 static int ads1015_set_power_state(struct ads1015_data
*data
, bool on
)
227 struct device
*dev
= regmap_get_device(data
->regmap
);
230 ret
= pm_runtime_get_sync(dev
);
232 pm_runtime_put_noidle(dev
);
234 pm_runtime_mark_last_busy(dev
);
235 ret
= pm_runtime_put_autosuspend(dev
);
242 int ads1015_get_adc_result(struct ads1015_data
*data
, int chan
, int *val
)
244 int ret
, pga
, dr
, conv_time
;
247 if (chan
< 0 || chan
>= ADS1015_CHANNELS
)
250 pga
= data
->channel_data
[chan
].pga
;
251 dr
= data
->channel_data
[chan
].data_rate
;
253 ret
= regmap_update_bits_check(data
->regmap
, ADS1015_CFG_REG
,
254 ADS1015_CFG_MUX_MASK
|
255 ADS1015_CFG_PGA_MASK
,
256 chan
<< ADS1015_CFG_MUX_SHIFT
|
257 pga
<< ADS1015_CFG_PGA_SHIFT
,
263 conv_time
= DIV_ROUND_UP(USEC_PER_SEC
, data
->data_rate
[dr
]);
264 usleep_range(conv_time
, conv_time
+ 1);
267 return regmap_read(data
->regmap
, ADS1015_CONV_REG
, val
);
270 static irqreturn_t
ads1015_trigger_handler(int irq
, void *p
)
272 struct iio_poll_func
*pf
= p
;
273 struct iio_dev
*indio_dev
= pf
->indio_dev
;
274 struct ads1015_data
*data
= iio_priv(indio_dev
);
275 s16 buf
[8]; /* 1x s16 ADC val + 3x s16 padding + 4x s16 timestamp */
278 memset(buf
, 0, sizeof(buf
));
280 mutex_lock(&data
->lock
);
281 chan
= find_first_bit(indio_dev
->active_scan_mask
,
282 indio_dev
->masklength
);
283 ret
= ads1015_get_adc_result(data
, chan
, &res
);
285 mutex_unlock(&data
->lock
);
290 mutex_unlock(&data
->lock
);
292 iio_push_to_buffers_with_timestamp(indio_dev
, buf
,
293 iio_get_time_ns(indio_dev
));
296 iio_trigger_notify_done(indio_dev
->trig
);
301 static int ads1015_set_scale(struct ads1015_data
*data
, int chan
,
302 int scale
, int uscale
)
304 int i
, ret
, rindex
= -1;
306 for (i
= 0; i
< ARRAY_SIZE(ads1015_scale
); i
++)
307 if (ads1015_scale
[i
].scale
== scale
&&
308 ads1015_scale
[i
].uscale
== uscale
) {
315 ret
= regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
316 ADS1015_CFG_PGA_MASK
,
317 rindex
<< ADS1015_CFG_PGA_SHIFT
);
321 data
->channel_data
[chan
].pga
= rindex
;
326 static int ads1015_set_data_rate(struct ads1015_data
*data
, int chan
, int rate
)
328 int i
, ret
, rindex
= -1;
330 for (i
= 0; i
< ARRAY_SIZE(ads1015_data_rate
); i
++)
331 if (data
->data_rate
[i
] == rate
) {
338 ret
= regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
340 rindex
<< ADS1015_CFG_DR_SHIFT
);
344 data
->channel_data
[chan
].data_rate
= rindex
;
349 static int ads1015_read_raw(struct iio_dev
*indio_dev
,
350 struct iio_chan_spec
const *chan
, int *val
,
351 int *val2
, long mask
)
354 struct ads1015_data
*data
= iio_priv(indio_dev
);
356 mutex_lock(&indio_dev
->mlock
);
357 mutex_lock(&data
->lock
);
359 case IIO_CHAN_INFO_RAW
: {
360 int shift
= chan
->scan_type
.shift
;
362 if (iio_buffer_enabled(indio_dev
)) {
367 ret
= ads1015_set_power_state(data
, true);
371 ret
= ads1015_get_adc_result(data
, chan
->address
, val
);
373 ads1015_set_power_state(data
, false);
377 *val
= sign_extend32(*val
>> shift
, 15 - shift
);
379 ret
= ads1015_set_power_state(data
, false);
386 case IIO_CHAN_INFO_SCALE
:
387 idx
= data
->channel_data
[chan
->address
].pga
;
388 *val
= ads1015_scale
[idx
].scale
;
389 *val2
= ads1015_scale
[idx
].uscale
;
390 ret
= IIO_VAL_INT_PLUS_MICRO
;
392 case IIO_CHAN_INFO_SAMP_FREQ
:
393 idx
= data
->channel_data
[chan
->address
].data_rate
;
394 *val
= data
->data_rate
[idx
];
401 mutex_unlock(&data
->lock
);
402 mutex_unlock(&indio_dev
->mlock
);
407 static int ads1015_write_raw(struct iio_dev
*indio_dev
,
408 struct iio_chan_spec
const *chan
, int val
,
411 struct ads1015_data
*data
= iio_priv(indio_dev
);
414 mutex_lock(&data
->lock
);
416 case IIO_CHAN_INFO_SCALE
:
417 ret
= ads1015_set_scale(data
, chan
->address
, val
, val2
);
419 case IIO_CHAN_INFO_SAMP_FREQ
:
420 ret
= ads1015_set_data_rate(data
, chan
->address
, val
);
426 mutex_unlock(&data
->lock
);
431 static int ads1015_buffer_preenable(struct iio_dev
*indio_dev
)
433 return ads1015_set_power_state(iio_priv(indio_dev
), true);
436 static int ads1015_buffer_postdisable(struct iio_dev
*indio_dev
)
438 return ads1015_set_power_state(iio_priv(indio_dev
), false);
441 static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops
= {
442 .preenable
= ads1015_buffer_preenable
,
443 .postenable
= iio_triggered_buffer_postenable
,
444 .predisable
= iio_triggered_buffer_predisable
,
445 .postdisable
= ads1015_buffer_postdisable
,
446 .validate_scan_mask
= &iio_validate_scan_mask_onehot
,
449 static IIO_CONST_ATTR(scale_available
, "3 2 1 0.5 0.25 0.125");
451 static IIO_CONST_ATTR_NAMED(ads1015_sampling_frequency_available
,
452 sampling_frequency_available
, "128 250 490 920 1600 2400 3300");
453 static IIO_CONST_ATTR_NAMED(ads1115_sampling_frequency_available
,
454 sampling_frequency_available
, "8 16 32 64 128 250 475 860");
456 static struct attribute
*ads1015_attributes
[] = {
457 &iio_const_attr_scale_available
.dev_attr
.attr
,
458 &iio_const_attr_ads1015_sampling_frequency_available
.dev_attr
.attr
,
462 static const struct attribute_group ads1015_attribute_group
= {
463 .attrs
= ads1015_attributes
,
466 static struct attribute
*ads1115_attributes
[] = {
467 &iio_const_attr_scale_available
.dev_attr
.attr
,
468 &iio_const_attr_ads1115_sampling_frequency_available
.dev_attr
.attr
,
472 static const struct attribute_group ads1115_attribute_group
= {
473 .attrs
= ads1115_attributes
,
476 static const struct iio_info ads1015_info
= {
477 .driver_module
= THIS_MODULE
,
478 .read_raw
= ads1015_read_raw
,
479 .write_raw
= ads1015_write_raw
,
480 .attrs
= &ads1015_attribute_group
,
483 static const struct iio_info ads1115_info
= {
484 .driver_module
= THIS_MODULE
,
485 .read_raw
= ads1015_read_raw
,
486 .write_raw
= ads1015_write_raw
,
487 .attrs
= &ads1115_attribute_group
,
491 static int ads1015_get_channels_config_of(struct i2c_client
*client
)
493 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
494 struct ads1015_data
*data
= iio_priv(indio_dev
);
495 struct device_node
*node
;
497 if (!client
->dev
.of_node
||
498 !of_get_next_child(client
->dev
.of_node
, NULL
))
501 for_each_child_of_node(client
->dev
.of_node
, node
) {
503 unsigned int channel
;
504 unsigned int pga
= ADS1015_DEFAULT_PGA
;
505 unsigned int data_rate
= ADS1015_DEFAULT_DATA_RATE
;
507 if (of_property_read_u32(node
, "reg", &pval
)) {
508 dev_err(&client
->dev
, "invalid reg on %s\n",
514 if (channel
>= ADS1015_CHANNELS
) {
515 dev_err(&client
->dev
,
516 "invalid channel index %d on %s\n",
517 channel
, node
->full_name
);
521 if (!of_property_read_u32(node
, "ti,gain", &pval
)) {
524 dev_err(&client
->dev
, "invalid gain on %s\n",
531 if (!of_property_read_u32(node
, "ti,datarate", &pval
)) {
534 dev_err(&client
->dev
,
535 "invalid data_rate on %s\n",
542 data
->channel_data
[channel
].pga
= pga
;
543 data
->channel_data
[channel
].data_rate
= data_rate
;
550 static void ads1015_get_channels_config(struct i2c_client
*client
)
554 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
555 struct ads1015_data
*data
= iio_priv(indio_dev
);
556 struct ads1015_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
558 /* prefer platform data */
560 memcpy(data
->channel_data
, pdata
->channel_data
,
561 sizeof(data
->channel_data
));
566 if (!ads1015_get_channels_config_of(client
))
569 /* fallback on default configuration */
570 for (k
= 0; k
< ADS1015_CHANNELS
; ++k
) {
571 data
->channel_data
[k
].pga
= ADS1015_DEFAULT_PGA
;
572 data
->channel_data
[k
].data_rate
= ADS1015_DEFAULT_DATA_RATE
;
576 static int ads1015_probe(struct i2c_client
*client
,
577 const struct i2c_device_id
*id
)
579 struct iio_dev
*indio_dev
;
580 struct ads1015_data
*data
;
584 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
588 data
= iio_priv(indio_dev
);
589 i2c_set_clientdata(client
, indio_dev
);
591 mutex_init(&data
->lock
);
593 indio_dev
->dev
.parent
= &client
->dev
;
594 indio_dev
->dev
.of_node
= client
->dev
.of_node
;
595 indio_dev
->name
= ADS1015_DRV_NAME
;
596 indio_dev
->modes
= INDIO_DIRECT_MODE
;
598 if (client
->dev
.of_node
)
599 chip
= (enum chip_ids
)of_device_get_match_data(&client
->dev
);
601 chip
= id
->driver_data
;
604 indio_dev
->channels
= ads1015_channels
;
605 indio_dev
->num_channels
= ARRAY_SIZE(ads1015_channels
);
606 indio_dev
->info
= &ads1015_info
;
607 data
->data_rate
= (unsigned int *) &ads1015_data_rate
;
610 indio_dev
->channels
= ads1115_channels
;
611 indio_dev
->num_channels
= ARRAY_SIZE(ads1115_channels
);
612 indio_dev
->info
= &ads1115_info
;
613 data
->data_rate
= (unsigned int *) &ads1115_data_rate
;
617 /* we need to keep this ABI the same as used by hwmon ADS1015 driver */
618 ads1015_get_channels_config(client
);
620 data
->regmap
= devm_regmap_init_i2c(client
, &ads1015_regmap_config
);
621 if (IS_ERR(data
->regmap
)) {
622 dev_err(&client
->dev
, "Failed to allocate register map\n");
623 return PTR_ERR(data
->regmap
);
626 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
627 ads1015_trigger_handler
,
628 &ads1015_buffer_setup_ops
);
630 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
633 ret
= pm_runtime_set_active(&client
->dev
);
635 goto err_buffer_cleanup
;
636 pm_runtime_set_autosuspend_delay(&client
->dev
, ADS1015_SLEEP_DELAY_MS
);
637 pm_runtime_use_autosuspend(&client
->dev
);
638 pm_runtime_enable(&client
->dev
);
640 ret
= iio_device_register(indio_dev
);
642 dev_err(&client
->dev
, "Failed to register IIO device\n");
643 goto err_buffer_cleanup
;
649 iio_triggered_buffer_cleanup(indio_dev
);
654 static int ads1015_remove(struct i2c_client
*client
)
656 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
657 struct ads1015_data
*data
= iio_priv(indio_dev
);
659 iio_device_unregister(indio_dev
);
661 pm_runtime_disable(&client
->dev
);
662 pm_runtime_set_suspended(&client
->dev
);
663 pm_runtime_put_noidle(&client
->dev
);
665 iio_triggered_buffer_cleanup(indio_dev
);
667 /* power down single shot mode */
668 return regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
669 ADS1015_CFG_MOD_MASK
,
670 ADS1015_SINGLESHOT
<< ADS1015_CFG_MOD_SHIFT
);
674 static int ads1015_runtime_suspend(struct device
*dev
)
676 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
677 struct ads1015_data
*data
= iio_priv(indio_dev
);
679 return regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
680 ADS1015_CFG_MOD_MASK
,
681 ADS1015_SINGLESHOT
<< ADS1015_CFG_MOD_SHIFT
);
684 static int ads1015_runtime_resume(struct device
*dev
)
686 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
687 struct ads1015_data
*data
= iio_priv(indio_dev
);
689 return regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
690 ADS1015_CFG_MOD_MASK
,
691 ADS1015_CONTINUOUS
<< ADS1015_CFG_MOD_SHIFT
);
695 static const struct dev_pm_ops ads1015_pm_ops
= {
696 SET_RUNTIME_PM_OPS(ads1015_runtime_suspend
,
697 ads1015_runtime_resume
, NULL
)
700 static const struct i2c_device_id ads1015_id
[] = {
701 {"ads1015", ADS1015
},
702 {"ads1115", ADS1115
},
705 MODULE_DEVICE_TABLE(i2c
, ads1015_id
);
707 static const struct of_device_id ads1015_of_match
[] = {
709 .compatible
= "ti,ads1015",
710 .data
= (void *)ADS1015
713 .compatible
= "ti,ads1115",
714 .data
= (void *)ADS1115
718 MODULE_DEVICE_TABLE(of
, ads1015_of_match
);
720 static struct i2c_driver ads1015_driver
= {
722 .name
= ADS1015_DRV_NAME
,
723 .of_match_table
= ads1015_of_match
,
724 .pm
= &ads1015_pm_ops
,
726 .probe
= ads1015_probe
,
727 .remove
= ads1015_remove
,
728 .id_table
= ads1015_id
,
731 module_i2c_driver(ads1015_driver
);
733 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
734 MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver");
735 MODULE_LICENSE("GPL v2");