Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 1 | /* |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 2 | * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver |
| 3 | * |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 4 | * Copyright (C) 2012 Avionic Design GmbH |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 5 | * Copyright (C) 2016 Intel |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 10 | * |
| 11 | * Datasheets: |
| 12 | * http://www.ti.com/lit/ds/symlink/adc081c021.pdf |
| 13 | * http://www.ti.com/lit/ds/symlink/adc101c021.pdf |
| 14 | * http://www.ti.com/lit/ds/symlink/adc121c021.pdf |
| 15 | * |
| 16 | * The devices have a very similar interface and differ mostly in the number of |
| 17 | * bits handled. For the 8-bit and 10-bit models the least-significant 4 or 2 |
| 18 | * bits of value registers are reserved. |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/i2c.h> |
| 23 | #include <linux/module.h> |
Sachin Kamat | 1f100e8 | 2013-10-18 13:04:00 +0100 | [diff] [blame] | 24 | #include <linux/of.h> |
Dan O'Donovan | 7feae87 | 2016-05-27 18:37:30 +0100 | [diff] [blame] | 25 | #include <linux/acpi.h> |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 26 | |
| 27 | #include <linux/iio/iio.h> |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 28 | #include <linux/iio/buffer.h> |
| 29 | #include <linux/iio/trigger_consumer.h> |
| 30 | #include <linux/iio/triggered_buffer.h> |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 31 | #include <linux/regulator/consumer.h> |
| 32 | |
| 33 | struct adc081c { |
| 34 | struct i2c_client *i2c; |
| 35 | struct regulator *ref; |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 36 | |
| 37 | /* 8, 10 or 12 */ |
| 38 | int bits; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #define REG_CONV_RES 0x00 |
| 42 | |
| 43 | static int adc081c_read_raw(struct iio_dev *iio, |
| 44 | struct iio_chan_spec const *channel, int *value, |
| 45 | int *shift, long mask) |
| 46 | { |
| 47 | struct adc081c *adc = iio_priv(iio); |
| 48 | int err; |
| 49 | |
| 50 | switch (mask) { |
| 51 | case IIO_CHAN_INFO_RAW: |
| 52 | err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES); |
| 53 | if (err < 0) |
| 54 | return err; |
| 55 | |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 56 | *value = (err & 0xFFF) >> (12 - adc->bits); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 57 | return IIO_VAL_INT; |
| 58 | |
| 59 | case IIO_CHAN_INFO_SCALE: |
| 60 | err = regulator_get_voltage(adc->ref); |
| 61 | if (err < 0) |
| 62 | return err; |
| 63 | |
| 64 | *value = err / 1000; |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 65 | *shift = adc->bits; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 66 | |
| 67 | return IIO_VAL_FRACTIONAL_LOG2; |
| 68 | |
| 69 | default: |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | return -EINVAL; |
| 74 | } |
| 75 | |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 76 | #define ADCxx1C_CHAN(_bits) { \ |
| 77 | .type = IIO_VOLTAGE, \ |
| 78 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
| 79 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 80 | .scan_type = { \ |
| 81 | .sign = 'u', \ |
| 82 | .realbits = (_bits), \ |
| 83 | .storagebits = 16, \ |
| 84 | .shift = 12 - (_bits), \ |
| 85 | .endianness = IIO_CPU, \ |
| 86 | }, \ |
| 87 | } |
| 88 | |
| 89 | #define DEFINE_ADCxx1C_CHANNELS(_name, _bits) \ |
| 90 | static const struct iio_chan_spec _name ## _channels[] = { \ |
| 91 | ADCxx1C_CHAN((_bits)), \ |
| 92 | IIO_CHAN_SOFT_TIMESTAMP(1), \ |
| 93 | }; \ |
| 94 | |
| 95 | #define ADC081C_NUM_CHANNELS 2 |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 96 | |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 97 | struct adcxx1c_model { |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 98 | const struct iio_chan_spec* channels; |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 99 | int bits; |
| 100 | }; |
| 101 | |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 102 | #define ADCxx1C_MODEL(_name, _bits) \ |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 103 | { \ |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 104 | .channels = _name ## _channels, \ |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 105 | .bits = (_bits), \ |
| 106 | } |
| 107 | |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 108 | DEFINE_ADCxx1C_CHANNELS(adc081c, 8); |
| 109 | DEFINE_ADCxx1C_CHANNELS(adc101c, 10); |
| 110 | DEFINE_ADCxx1C_CHANNELS(adc121c, 12); |
| 111 | |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 112 | /* Model ids are indexes in _models array */ |
| 113 | enum adcxx1c_model_id { |
| 114 | ADC081C = 0, |
| 115 | ADC101C = 1, |
| 116 | ADC121C = 2, |
| 117 | }; |
| 118 | |
| 119 | static struct adcxx1c_model adcxx1c_models[] = { |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 120 | ADCxx1C_MODEL(adc081c, 8), |
| 121 | ADCxx1C_MODEL(adc101c, 10), |
| 122 | ADCxx1C_MODEL(adc121c, 12), |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 123 | }; |
| 124 | |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 125 | static const struct iio_info adc081c_info = { |
| 126 | .read_raw = adc081c_read_raw, |
| 127 | .driver_module = THIS_MODULE, |
| 128 | }; |
| 129 | |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 130 | static irqreturn_t adc081c_trigger_handler(int irq, void *p) |
| 131 | { |
| 132 | struct iio_poll_func *pf = p; |
| 133 | struct iio_dev *indio_dev = pf->indio_dev; |
| 134 | struct adc081c *data = iio_priv(indio_dev); |
| 135 | u16 buf[8]; /* 2 bytes data + 6 bytes padding + 8 bytes timestamp */ |
| 136 | int ret; |
| 137 | |
| 138 | ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES); |
| 139 | if (ret < 0) |
| 140 | goto out; |
| 141 | buf[0] = ret; |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 142 | iio_push_to_buffers_with_timestamp(indio_dev, buf, |
| 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 | |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 149 | static int adc081c_probe(struct i2c_client *client, |
| 150 | const struct i2c_device_id *id) |
| 151 | { |
| 152 | struct iio_dev *iio; |
| 153 | struct adc081c *adc; |
Dan O'Donovan | 7feae87 | 2016-05-27 18:37:30 +0100 | [diff] [blame] | 154 | struct adcxx1c_model *model; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 155 | int err; |
| 156 | |
| 157 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) |
Matt Ranostay | f8d9d3b | 2016-02-26 22:13:49 -0800 | [diff] [blame] | 158 | return -EOPNOTSUPP; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 159 | |
Dan O'Donovan | 7feae87 | 2016-05-27 18:37:30 +0100 | [diff] [blame] | 160 | if (ACPI_COMPANION(&client->dev)) { |
| 161 | const struct acpi_device_id *ad_id; |
| 162 | |
| 163 | ad_id = acpi_match_device(client->dev.driver->acpi_match_table, |
| 164 | &client->dev); |
| 165 | if (!ad_id) |
| 166 | return -ENODEV; |
| 167 | model = &adcxx1c_models[ad_id->driver_data]; |
| 168 | } else { |
| 169 | model = &adcxx1c_models[id->driver_data]; |
| 170 | } |
| 171 | |
Sachin Kamat | 99e94b6 | 2013-07-23 09:58:00 +0100 | [diff] [blame] | 172 | iio = devm_iio_device_alloc(&client->dev, sizeof(*adc)); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 173 | if (!iio) |
| 174 | return -ENOMEM; |
| 175 | |
| 176 | adc = iio_priv(iio); |
| 177 | adc->i2c = client; |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 178 | adc->bits = model->bits; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 179 | |
Sachin Kamat | 99e94b6 | 2013-07-23 09:58:00 +0100 | [diff] [blame] | 180 | adc->ref = devm_regulator_get(&client->dev, "vref"); |
| 181 | if (IS_ERR(adc->ref)) |
| 182 | return PTR_ERR(adc->ref); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 183 | |
| 184 | err = regulator_enable(adc->ref); |
| 185 | if (err < 0) |
Sachin Kamat | 99e94b6 | 2013-07-23 09:58:00 +0100 | [diff] [blame] | 186 | return err; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 187 | |
| 188 | iio->dev.parent = &client->dev; |
Matt Ranostay | b541eaf | 2016-07-02 17:26:33 -0700 | [diff] [blame] | 189 | iio->dev.of_node = client->dev.of_node; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 190 | iio->name = dev_name(&client->dev); |
| 191 | iio->modes = INDIO_DIRECT_MODE; |
| 192 | iio->info = &adc081c_info; |
| 193 | |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 194 | iio->channels = model->channels; |
| 195 | iio->num_channels = ADC081C_NUM_CHANNELS; |
| 196 | |
| 197 | err = iio_triggered_buffer_setup(iio, NULL, adc081c_trigger_handler, NULL); |
| 198 | if (err < 0) { |
| 199 | dev_err(&client->dev, "iio triggered buffer setup failed\n"); |
| 200 | goto err_regulator_disable; |
| 201 | } |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 202 | |
| 203 | err = iio_device_register(iio); |
| 204 | if (err < 0) |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 205 | goto err_buffer_cleanup; |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 206 | |
| 207 | i2c_set_clientdata(client, iio); |
| 208 | |
| 209 | return 0; |
| 210 | |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 211 | err_buffer_cleanup: |
| 212 | iio_triggered_buffer_cleanup(iio); |
| 213 | err_regulator_disable: |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 214 | regulator_disable(adc->ref); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 215 | |
| 216 | return err; |
| 217 | } |
| 218 | |
| 219 | static int adc081c_remove(struct i2c_client *client) |
| 220 | { |
| 221 | struct iio_dev *iio = i2c_get_clientdata(client); |
| 222 | struct adc081c *adc = iio_priv(iio); |
| 223 | |
| 224 | iio_device_unregister(iio); |
Crestez Dan Leonard | 08e05d1 | 2016-04-11 17:24:27 +0300 | [diff] [blame] | 225 | iio_triggered_buffer_cleanup(iio); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 226 | regulator_disable(adc->ref); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static const struct i2c_device_id adc081c_id[] = { |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 232 | { "adc081c", ADC081C }, |
| 233 | { "adc101c", ADC101C }, |
| 234 | { "adc121c", ADC121C }, |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 235 | { } |
| 236 | }; |
| 237 | MODULE_DEVICE_TABLE(i2c, adc081c_id); |
| 238 | |
| 239 | #ifdef CONFIG_OF |
| 240 | static const struct of_device_id adc081c_of_match[] = { |
| 241 | { .compatible = "ti,adc081c" }, |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 242 | { .compatible = "ti,adc101c" }, |
| 243 | { .compatible = "ti,adc121c" }, |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 244 | { } |
| 245 | }; |
| 246 | MODULE_DEVICE_TABLE(of, adc081c_of_match); |
| 247 | #endif |
| 248 | |
Dan O'Donovan | 7feae87 | 2016-05-27 18:37:30 +0100 | [diff] [blame] | 249 | #ifdef CONFIG_ACPI |
| 250 | static const struct acpi_device_id adc081c_acpi_match[] = { |
| 251 | { "ADC081C", ADC081C }, |
| 252 | { "ADC101C", ADC101C }, |
| 253 | { "ADC121C", ADC121C }, |
| 254 | { } |
| 255 | }; |
| 256 | MODULE_DEVICE_TABLE(acpi, adc081c_acpi_match); |
| 257 | #endif |
| 258 | |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 259 | static struct i2c_driver adc081c_driver = { |
| 260 | .driver = { |
| 261 | .name = "adc081c", |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 262 | .of_match_table = of_match_ptr(adc081c_of_match), |
Dan O'Donovan | 7feae87 | 2016-05-27 18:37:30 +0100 | [diff] [blame] | 263 | .acpi_match_table = ACPI_PTR(adc081c_acpi_match), |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 264 | }, |
| 265 | .probe = adc081c_probe, |
| 266 | .remove = adc081c_remove, |
| 267 | .id_table = adc081c_id, |
| 268 | }; |
| 269 | module_i2c_driver(adc081c_driver); |
| 270 | |
| 271 | MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); |
Crestez Dan Leonard | a6b5ec8 | 2016-04-11 17:24:26 +0300 | [diff] [blame] | 272 | MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver"); |
Thierry Reding | bc0a409 | 2012-11-23 15:13:00 +0000 | [diff] [blame] | 273 | MODULE_LICENSE("GPL v2"); |