Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 2 | /* |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 3 | * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver |
| 4 | * |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 2012 Avionic Design GmbH |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 6 | * Copyright (C) 2016 Intel |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 7 | * |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 8 | * Datasheets: |
Alexander A. Klimov | 3593cd5 | 2020-07-04 21:27:43 +0200 | [diff] [blame] | 9 | * https://www.ti.com/lit/ds/symlink/adc081c021.pdf |
| 10 | * https://www.ti.com/lit/ds/symlink/adc101c021.pdf |
| 11 | * https://www.ti.com/lit/ds/symlink/adc121c021.pdf |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 12 | * |
| 13 | * The devices have a very similar interface and differ mostly in the number of |
| 14 | * bits handled. For the 8-bit and 10-bit models the least-significant 4 or 2 |
| 15 | * bits of value registers are reserved. |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/module.h> |
Jonathan Cameron | b087374 | 2020-06-28 13:36:46 +0100 | [diff] [blame] | 21 | #include <linux/mod_devicetable.h> |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 22 | |
| 23 | #include <linux/iio/iio.h> |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 24 | #include <linux/iio/buffer.h> |
| 25 | #include <linux/iio/trigger_consumer.h> |
| 26 | #include <linux/iio/triggered_buffer.h> |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 27 | #include <linux/regulator/consumer.h> |
| 28 | |
| 29 | struct adc081c { |
| 30 | struct i2c_client *i2c; |
| 31 | struct regulator *ref; |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 32 | |
| 33 | /* 8, 10 or 12 */ |
| 34 | int bits; |
Jonathan Cameron | 54f82df | 2020-07-22 16:50:56 +0100 | [diff] [blame] | 35 | |
| 36 | /* Ensure natural alignment of buffer elements */ |
| 37 | struct { |
| 38 | u16 channel; |
| 39 | s64 ts __aligned(8); |
| 40 | } scan; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | #define REG_CONV_RES 0x00 |
| 44 | |
| 45 | static int adc081c_read_raw(struct iio_dev *iio, |
| 46 | struct iio_chan_spec const *channel, int *value, |
| 47 | int *shift, long mask) |
| 48 | { |
| 49 | struct adc081c *adc = iio_priv(iio); |
| 50 | int err; |
| 51 | |
| 52 | switch (mask) { |
| 53 | case IIO_CHAN_INFO_RAW: |
| 54 | err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES); |
| 55 | if (err < 0) |
| 56 | return err; |
| 57 | |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 58 | *value = (err & 0xFFF) >> (12 - adc->bits); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 59 | return IIO_VAL_INT; |
| 60 | |
| 61 | case IIO_CHAN_INFO_SCALE: |
| 62 | err = regulator_get_voltage(adc->ref); |
| 63 | if (err < 0) |
| 64 | return err; |
| 65 | |
| 66 | *value = err / 1000; |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 67 | *shift = adc->bits; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 68 | |
| 69 | return IIO_VAL_FRACTIONAL_LOG2; |
| 70 | |
| 71 | default: |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | return -EINVAL; |
| 76 | } |
| 77 | |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 78 | #define ADCxx1C_CHAN(_bits) { \ |
| 79 | .type = IIO_VOLTAGE, \ |
| 80 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
| 81 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 82 | .scan_type = { \ |
| 83 | .sign = 'u', \ |
| 84 | .realbits = (_bits), \ |
| 85 | .storagebits = 16, \ |
| 86 | .shift = 12 - (_bits), \ |
| 87 | .endianness = IIO_CPU, \ |
| 88 | }, \ |
| 89 | } |
| 90 | |
| 91 | #define DEFINE_ADCxx1C_CHANNELS(_name, _bits) \ |
| 92 | static const struct iio_chan_spec _name ## _channels[] = { \ |
| 93 | ADCxx1C_CHAN((_bits)), \ |
| 94 | IIO_CHAN_SOFT_TIMESTAMP(1), \ |
| 95 | }; \ |
| 96 | |
| 97 | #define ADC081C_NUM_CHANNELS 2 |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 98 | |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 99 | struct adcxx1c_model { |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 100 | const struct iio_chan_spec* channels; |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 101 | int bits; |
| 102 | }; |
| 103 | |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 104 | #define ADCxx1C_MODEL(_name, _bits) \ |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 105 | { \ |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 106 | .channels = _name ## _channels, \ |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 107 | .bits = (_bits), \ |
| 108 | } |
| 109 | |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 110 | DEFINE_ADCxx1C_CHANNELS(adc081c, 8); |
| 111 | DEFINE_ADCxx1C_CHANNELS(adc101c, 10); |
| 112 | DEFINE_ADCxx1C_CHANNELS(adc121c, 12); |
| 113 | |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 114 | /* Model ids are indexes in _models array */ |
| 115 | enum adcxx1c_model_id { |
| 116 | ADC081C = 0, |
| 117 | ADC101C = 1, |
| 118 | ADC121C = 2, |
| 119 | }; |
| 120 | |
| 121 | static struct adcxx1c_model adcxx1c_models[] = { |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 122 | ADCxx1C_MODEL(adc081c, 8), |
| 123 | ADCxx1C_MODEL(adc101c, 10), |
| 124 | ADCxx1C_MODEL(adc121c, 12), |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 125 | }; |
| 126 | |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 127 | static const struct iio_info adc081c_info = { |
| 128 | .read_raw = adc081c_read_raw, |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 131 | static irqreturn_t adc081c_trigger_handler(int irq, void *p) |
| 132 | { |
| 133 | struct iio_poll_func *pf = p; |
| 134 | struct iio_dev *indio_dev = pf->indio_dev; |
| 135 | struct adc081c *data = iio_priv(indio_dev); |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 136 | int ret; |
| 137 | |
| 138 | ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES); |
| 139 | if (ret < 0) |
| 140 | goto out; |
Jonathan Cameron | 54f82df | 2020-07-22 16:50:56 +0100 | [diff] [blame] | 141 | data->scan.channel = ret; |
| 142 | iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 143 | iio_get_time_ns(indio_dev)); |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 144 | out: |
| 145 | iio_trigger_notify_done(indio_dev->trig); |
| 146 | return IRQ_HANDLED; |
| 147 | } |
| 148 | |
Jonathan Cameron | 6c100eb | 2021-05-16 18:25:17 +0100 | [diff] [blame] | 149 | static void adc081c_reg_disable(void *reg) |
| 150 | { |
| 151 | regulator_disable(reg); |
| 152 | } |
| 153 | |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 154 | static int adc081c_probe(struct i2c_client *client, |
| 155 | const struct i2c_device_id *id) |
| 156 | { |
| 157 | struct iio_dev *iio; |
| 158 | struct adc081c *adc; |
Dan O'Donovan | 7feae87 | 2016-05-27 18:37:30 +0100 | [diff] [blame] | 159 | struct adcxx1c_model *model; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 160 | int err; |
| 161 | |
| 162 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) |
Matt Ranostay | f8d9d3b | 2016-02-26 22:13:49 -0800 | [diff] [blame] | 163 | return -EOPNOTSUPP; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 164 | |
Jonathan Cameron | c458b7c | 2020-07-21 18:14:41 +0100 | [diff] [blame] | 165 | model = &adcxx1c_models[id->driver_data]; |
Dan O'Donovan | 7feae87 | 2016-05-27 18:37:30 +0100 | [diff] [blame] | 166 | |
Sachin Kamat | 99e94b6 | 2013-07-23 09:58:00 +0100 | [diff] [blame] | 167 | iio = devm_iio_device_alloc(&client->dev, sizeof(*adc)); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 168 | if (!iio) |
| 169 | return -ENOMEM; |
| 170 | |
| 171 | adc = iio_priv(iio); |
| 172 | adc->i2c = client; |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 173 | adc->bits = model->bits; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 174 | |
Sachin Kamat | 99e94b6 | 2013-07-23 09:58:00 +0100 | [diff] [blame] | 175 | adc->ref = devm_regulator_get(&client->dev, "vref"); |
| 176 | if (IS_ERR(adc->ref)) |
| 177 | return PTR_ERR(adc->ref); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 178 | |
| 179 | err = regulator_enable(adc->ref); |
| 180 | if (err < 0) |
Sachin Kamat | 99e94b6 | 2013-07-23 09:58:00 +0100 | [diff] [blame] | 181 | return err; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 182 | |
Jonathan Cameron | 6c100eb | 2021-05-16 18:25:17 +0100 | [diff] [blame] | 183 | err = devm_add_action_or_reset(&client->dev, adc081c_reg_disable, |
| 184 | adc->ref); |
| 185 | if (err) |
| 186 | return err; |
| 187 | |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 188 | iio->name = dev_name(&client->dev); |
| 189 | iio->modes = INDIO_DIRECT_MODE; |
| 190 | iio->info = &adc081c_info; |
| 191 | |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 192 | iio->channels = model->channels; |
| 193 | iio->num_channels = ADC081C_NUM_CHANNELS; |
| 194 | |
Jonathan Cameron | 6c100eb | 2021-05-16 18:25:17 +0100 | [diff] [blame] | 195 | err = devm_iio_triggered_buffer_setup(&client->dev, iio, NULL, |
| 196 | adc081c_trigger_handler, NULL); |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 197 | if (err < 0) { |
| 198 | dev_err(&client->dev, "iio triggered buffer setup failed\n"); |
Jonathan Cameron | 6c100eb | 2021-05-16 18:25:17 +0100 | [diff] [blame] | 199 | return err; |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 200 | } |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 201 | |
Jonathan Cameron | 6c100eb | 2021-05-16 18:25:17 +0100 | [diff] [blame] | 202 | return devm_iio_device_register(&client->dev, iio); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static const struct i2c_device_id adc081c_id[] = { |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 206 | { "adc081c", ADC081C }, |
| 207 | { "adc101c", ADC101C }, |
| 208 | { "adc121c", ADC121C }, |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 209 | { } |
| 210 | }; |
| 211 | MODULE_DEVICE_TABLE(i2c, adc081c_id); |
| 212 | |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 213 | static const struct of_device_id adc081c_of_match[] = { |
| 214 | { .compatible = "ti,adc081c" }, |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 215 | { .compatible = "ti,adc101c" }, |
| 216 | { .compatible = "ti,adc121c" }, |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 217 | { } |
| 218 | }; |
| 219 | MODULE_DEVICE_TABLE(of, adc081c_of_match); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 220 | |
| 221 | static struct i2c_driver adc081c_driver = { |
| 222 | .driver = { |
| 223 | .name = "adc081c", |
Jonathan Cameron | b087374 | 2020-06-28 13:36:46 +0100 | [diff] [blame] | 224 | .of_match_table = adc081c_of_match, |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 225 | }, |
| 226 | .probe = adc081c_probe, |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 227 | .id_table = adc081c_id, |
| 228 | }; |
| 229 | module_i2c_driver(adc081c_driver); |
| 230 | |
| 231 | MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 232 | MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver"); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 233 | MODULE_LICENSE("GPL v2"); |