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/init.h>
19 #include <linux/i2c.h>
20 #include <linux/regmap.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/mutex.h>
23 #include <linux/delay.h>
25 #include <linux/i2c/ads1015.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/types.h>
29 #include <linux/iio/sysfs.h>
30 #include <linux/iio/buffer.h>
31 #include <linux/iio/triggered_buffer.h>
32 #include <linux/iio/trigger_consumer.h>
34 #define ADS1015_DRV_NAME "ads1015"
36 #define ADS1015_CONV_REG 0x00
37 #define ADS1015_CFG_REG 0x01
39 #define ADS1015_CFG_DR_SHIFT 5
40 #define ADS1015_CFG_MOD_SHIFT 8
41 #define ADS1015_CFG_PGA_SHIFT 9
42 #define ADS1015_CFG_MUX_SHIFT 12
44 #define ADS1015_CFG_DR_MASK GENMASK(7, 5)
45 #define ADS1015_CFG_MOD_MASK BIT(8)
46 #define ADS1015_CFG_PGA_MASK GENMASK(11, 9)
47 #define ADS1015_CFG_MUX_MASK GENMASK(14, 12)
49 /* device operating modes */
50 #define ADS1015_CONTINUOUS 0
51 #define ADS1015_SINGLESHOT 1
53 #define ADS1015_SLEEP_DELAY_MS 2000
54 #define ADS1015_DEFAULT_PGA 2
55 #define ADS1015_DEFAULT_DATA_RATE 4
56 #define ADS1015_DEFAULT_CHAN 0
58 enum ads1015_channels
{
59 ADS1015_AIN0_AIN1
= 0,
70 static const unsigned int ads1015_data_rate
[] = {
71 128, 250, 490, 920, 1600, 2400, 3300, 3300
88 #define ADS1015_V_CHAN(_chan, _addr) { \
89 .type = IIO_VOLTAGE, \
93 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
94 BIT(IIO_CHAN_INFO_SCALE) | \
95 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
96 .scan_index = _addr, \
102 .endianness = IIO_CPU, \
106 #define ADS1015_V_DIFF_CHAN(_chan, _chan2, _addr) { \
107 .type = IIO_VOLTAGE, \
112 .channel2 = _chan2, \
113 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
114 BIT(IIO_CHAN_INFO_SCALE) | \
115 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
116 .scan_index = _addr, \
122 .endianness = IIO_CPU, \
126 struct ads1015_data
{
127 struct regmap
*regmap
;
129 * Protects ADC ops, e.g: concurrent sysfs/buffered
130 * data reads, configuration updates
133 struct ads1015_channel_data channel_data
[ADS1015_CHANNELS
];
136 static bool ads1015_is_writeable_reg(struct device
*dev
, unsigned int reg
)
138 return (reg
== ADS1015_CFG_REG
);
141 static const struct regmap_config ads1015_regmap_config
= {
144 .max_register
= ADS1015_CFG_REG
,
145 .writeable_reg
= ads1015_is_writeable_reg
,
148 static const struct iio_chan_spec ads1015_channels
[] = {
149 ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1
),
150 ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3
),
151 ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3
),
152 ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3
),
153 ADS1015_V_CHAN(0, ADS1015_AIN0
),
154 ADS1015_V_CHAN(1, ADS1015_AIN1
),
155 ADS1015_V_CHAN(2, ADS1015_AIN2
),
156 ADS1015_V_CHAN(3, ADS1015_AIN3
),
157 IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP
),
160 static int ads1015_set_power_state(struct ads1015_data
*data
, bool on
)
163 struct device
*dev
= regmap_get_device(data
->regmap
);
166 ret
= pm_runtime_get_sync(dev
);
168 pm_runtime_put_noidle(dev
);
170 pm_runtime_mark_last_busy(dev
);
171 ret
= pm_runtime_put_autosuspend(dev
);
178 int ads1015_get_adc_result(struct ads1015_data
*data
, int chan
, int *val
)
180 int ret
, pga
, dr
, conv_time
;
183 if (chan
< 0 || chan
>= ADS1015_CHANNELS
)
186 pga
= data
->channel_data
[chan
].pga
;
187 dr
= data
->channel_data
[chan
].data_rate
;
189 ret
= regmap_update_bits_check(data
->regmap
, ADS1015_CFG_REG
,
190 ADS1015_CFG_MUX_MASK
|
191 ADS1015_CFG_PGA_MASK
,
192 chan
<< ADS1015_CFG_MUX_SHIFT
|
193 pga
<< ADS1015_CFG_PGA_SHIFT
,
199 conv_time
= DIV_ROUND_UP(USEC_PER_SEC
, ads1015_data_rate
[dr
]);
200 usleep_range(conv_time
, conv_time
+ 1);
203 return regmap_read(data
->regmap
, ADS1015_CONV_REG
, val
);
206 static irqreturn_t
ads1015_trigger_handler(int irq
, void *p
)
208 struct iio_poll_func
*pf
= p
;
209 struct iio_dev
*indio_dev
= pf
->indio_dev
;
210 struct ads1015_data
*data
= iio_priv(indio_dev
);
211 s16 buf
[8]; /* 1x s16 ADC val + 3x s16 padding + 4x s16 timestamp */
214 memset(buf
, 0, sizeof(buf
));
216 mutex_lock(&data
->lock
);
217 chan
= find_first_bit(indio_dev
->active_scan_mask
,
218 indio_dev
->masklength
);
219 ret
= ads1015_get_adc_result(data
, chan
, &res
);
221 mutex_unlock(&data
->lock
);
226 mutex_unlock(&data
->lock
);
228 iio_push_to_buffers_with_timestamp(indio_dev
, buf
, iio_get_time_ns());
231 iio_trigger_notify_done(indio_dev
->trig
);
236 static int ads1015_set_scale(struct ads1015_data
*data
, int chan
,
237 int scale
, int uscale
)
239 int i
, ret
, rindex
= -1;
241 for (i
= 0; i
< ARRAY_SIZE(ads1015_scale
); i
++)
242 if (ads1015_scale
[i
].scale
== scale
&&
243 ads1015_scale
[i
].uscale
== uscale
) {
250 ret
= regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
251 ADS1015_CFG_PGA_MASK
,
252 rindex
<< ADS1015_CFG_PGA_SHIFT
);
256 data
->channel_data
[chan
].pga
= rindex
;
261 static int ads1015_set_data_rate(struct ads1015_data
*data
, int chan
, int rate
)
263 int i
, ret
, rindex
= -1;
265 for (i
= 0; i
< ARRAY_SIZE(ads1015_data_rate
); i
++)
266 if (ads1015_data_rate
[i
] == rate
) {
273 ret
= regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
275 rindex
<< ADS1015_CFG_DR_SHIFT
);
279 data
->channel_data
[chan
].data_rate
= rindex
;
284 static int ads1015_read_raw(struct iio_dev
*indio_dev
,
285 struct iio_chan_spec
const *chan
, int *val
,
286 int *val2
, long mask
)
289 struct ads1015_data
*data
= iio_priv(indio_dev
);
291 mutex_lock(&indio_dev
->mlock
);
292 mutex_lock(&data
->lock
);
294 case IIO_CHAN_INFO_RAW
:
295 if (iio_buffer_enabled(indio_dev
)) {
300 ret
= ads1015_set_power_state(data
, true);
304 ret
= ads1015_get_adc_result(data
, chan
->address
, val
);
306 ads1015_set_power_state(data
, false);
310 /* 12 bit res, D0 is bit 4 in conversion register */
311 *val
= sign_extend32(*val
>> 4, 11);
313 ret
= ads1015_set_power_state(data
, false);
319 case IIO_CHAN_INFO_SCALE
:
320 idx
= data
->channel_data
[chan
->address
].pga
;
321 *val
= ads1015_scale
[idx
].scale
;
322 *val2
= ads1015_scale
[idx
].uscale
;
323 ret
= IIO_VAL_INT_PLUS_MICRO
;
325 case IIO_CHAN_INFO_SAMP_FREQ
:
326 idx
= data
->channel_data
[chan
->address
].data_rate
;
327 *val
= ads1015_data_rate
[idx
];
334 mutex_unlock(&data
->lock
);
335 mutex_unlock(&indio_dev
->mlock
);
340 static int ads1015_write_raw(struct iio_dev
*indio_dev
,
341 struct iio_chan_spec
const *chan
, int val
,
344 struct ads1015_data
*data
= iio_priv(indio_dev
);
347 mutex_lock(&data
->lock
);
349 case IIO_CHAN_INFO_SCALE
:
350 ret
= ads1015_set_scale(data
, chan
->address
, val
, val2
);
352 case IIO_CHAN_INFO_SAMP_FREQ
:
353 ret
= ads1015_set_data_rate(data
, chan
->address
, val
);
359 mutex_unlock(&data
->lock
);
364 static int ads1015_buffer_preenable(struct iio_dev
*indio_dev
)
366 return ads1015_set_power_state(iio_priv(indio_dev
), true);
369 static int ads1015_buffer_postdisable(struct iio_dev
*indio_dev
)
371 return ads1015_set_power_state(iio_priv(indio_dev
), false);
374 static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops
= {
375 .preenable
= ads1015_buffer_preenable
,
376 .postenable
= iio_triggered_buffer_postenable
,
377 .predisable
= iio_triggered_buffer_predisable
,
378 .postdisable
= ads1015_buffer_postdisable
,
379 .validate_scan_mask
= &iio_validate_scan_mask_onehot
,
382 static IIO_CONST_ATTR(scale_available
, "3 2 1 0.5 0.25 0.125");
383 static IIO_CONST_ATTR(sampling_frequency_available
,
384 "128 250 490 920 1600 2400 3300");
386 static struct attribute
*ads1015_attributes
[] = {
387 &iio_const_attr_scale_available
.dev_attr
.attr
,
388 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
392 static const struct attribute_group ads1015_attribute_group
= {
393 .attrs
= ads1015_attributes
,
396 static const struct iio_info ads1015_info
= {
397 .driver_module
= THIS_MODULE
,
398 .read_raw
= ads1015_read_raw
,
399 .write_raw
= ads1015_write_raw
,
400 .attrs
= &ads1015_attribute_group
,
404 static int ads1015_get_channels_config_of(struct i2c_client
*client
)
406 struct ads1015_data
*data
= i2c_get_clientdata(client
);
407 struct device_node
*node
;
409 if (!client
->dev
.of_node
||
410 !of_get_next_child(client
->dev
.of_node
, NULL
))
413 for_each_child_of_node(client
->dev
.of_node
, node
) {
415 unsigned int channel
;
416 unsigned int pga
= ADS1015_DEFAULT_PGA
;
417 unsigned int data_rate
= ADS1015_DEFAULT_DATA_RATE
;
419 if (of_property_read_u32(node
, "reg", &pval
)) {
420 dev_err(&client
->dev
, "invalid reg on %s\n",
426 if (channel
>= ADS1015_CHANNELS
) {
427 dev_err(&client
->dev
,
428 "invalid channel index %d on %s\n",
429 channel
, node
->full_name
);
433 if (!of_property_read_u32(node
, "ti,gain", &pval
)) {
436 dev_err(&client
->dev
, "invalid gain on %s\n",
442 if (!of_property_read_u32(node
, "ti,datarate", &pval
)) {
445 dev_err(&client
->dev
,
446 "invalid data_rate on %s\n",
452 data
->channel_data
[channel
].pga
= pga
;
453 data
->channel_data
[channel
].data_rate
= data_rate
;
460 static void ads1015_get_channels_config(struct i2c_client
*client
)
464 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
465 struct ads1015_data
*data
= iio_priv(indio_dev
);
466 struct ads1015_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
468 /* prefer platform data */
470 memcpy(data
->channel_data
, pdata
->channel_data
,
471 sizeof(data
->channel_data
));
476 if (!ads1015_get_channels_config_of(client
))
479 /* fallback on default configuration */
480 for (k
= 0; k
< ADS1015_CHANNELS
; ++k
) {
481 data
->channel_data
[k
].pga
= ADS1015_DEFAULT_PGA
;
482 data
->channel_data
[k
].data_rate
= ADS1015_DEFAULT_DATA_RATE
;
486 static int ads1015_probe(struct i2c_client
*client
,
487 const struct i2c_device_id
*id
)
489 struct iio_dev
*indio_dev
;
490 struct ads1015_data
*data
;
493 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
497 data
= iio_priv(indio_dev
);
498 i2c_set_clientdata(client
, indio_dev
);
500 mutex_init(&data
->lock
);
502 indio_dev
->dev
.parent
= &client
->dev
;
503 indio_dev
->info
= &ads1015_info
;
504 indio_dev
->name
= ADS1015_DRV_NAME
;
505 indio_dev
->channels
= ads1015_channels
;
506 indio_dev
->num_channels
= ARRAY_SIZE(ads1015_channels
);
507 indio_dev
->modes
= INDIO_DIRECT_MODE
;
509 /* we need to keep this ABI the same as used by hwmon ADS1015 driver */
510 ads1015_get_channels_config(client
);
512 data
->regmap
= devm_regmap_init_i2c(client
, &ads1015_regmap_config
);
513 if (IS_ERR(data
->regmap
)) {
514 dev_err(&client
->dev
, "Failed to allocate register map\n");
515 return PTR_ERR(data
->regmap
);
518 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
519 ads1015_trigger_handler
,
520 &ads1015_buffer_setup_ops
);
522 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
525 ret
= pm_runtime_set_active(&client
->dev
);
527 goto err_buffer_cleanup
;
528 pm_runtime_set_autosuspend_delay(&client
->dev
, ADS1015_SLEEP_DELAY_MS
);
529 pm_runtime_use_autosuspend(&client
->dev
);
530 pm_runtime_enable(&client
->dev
);
532 ret
= iio_device_register(indio_dev
);
534 dev_err(&client
->dev
, "Failed to register IIO device\n");
535 goto err_buffer_cleanup
;
541 iio_triggered_buffer_cleanup(indio_dev
);
546 static int ads1015_remove(struct i2c_client
*client
)
548 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
549 struct ads1015_data
*data
= iio_priv(indio_dev
);
551 iio_device_unregister(indio_dev
);
553 pm_runtime_disable(&client
->dev
);
554 pm_runtime_set_suspended(&client
->dev
);
555 pm_runtime_put_noidle(&client
->dev
);
557 iio_triggered_buffer_cleanup(indio_dev
);
559 /* power down single shot mode */
560 return regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
561 ADS1015_CFG_MOD_MASK
,
562 ADS1015_SINGLESHOT
<< ADS1015_CFG_MOD_SHIFT
);
566 static int ads1015_runtime_suspend(struct device
*dev
)
568 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
569 struct ads1015_data
*data
= iio_priv(indio_dev
);
571 return regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
572 ADS1015_CFG_MOD_MASK
,
573 ADS1015_SINGLESHOT
<< ADS1015_CFG_MOD_SHIFT
);
576 static int ads1015_runtime_resume(struct device
*dev
)
578 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
579 struct ads1015_data
*data
= iio_priv(indio_dev
);
581 return regmap_update_bits(data
->regmap
, ADS1015_CFG_REG
,
582 ADS1015_CFG_MOD_MASK
,
583 ADS1015_CONTINUOUS
<< ADS1015_CFG_MOD_SHIFT
);
587 static const struct dev_pm_ops ads1015_pm_ops
= {
588 SET_RUNTIME_PM_OPS(ads1015_runtime_suspend
,
589 ads1015_runtime_resume
, NULL
)
592 static const struct i2c_device_id ads1015_id
[] = {
596 MODULE_DEVICE_TABLE(i2c
, ads1015_id
);
598 static struct i2c_driver ads1015_driver
= {
600 .name
= ADS1015_DRV_NAME
,
601 .pm
= &ads1015_pm_ops
,
603 .probe
= ads1015_probe
,
604 .remove
= ads1015_remove
,
605 .id_table
= ads1015_id
,
608 module_i2c_driver(ads1015_driver
);
610 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
611 MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver");
612 MODULE_LICENSE("GPL v2");