1 /* 2 * ti-adc161s626.c - Texas Instruments ADC161S626 1-channel differential ADC 3 * 4 * ADC Devices Supported: 5 * adc141s626 - 14-bit ADC 6 * adc161s626 - 16-bit ADC 7 * 8 * Copyright (C) 2016 Matt Ranostay <mranostay@gmail.com> 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 #include <linux/module.h> 22 #include <linux/init.h> 23 #include <linux/err.h> 24 #include <linux/spi/spi.h> 25 #include <linux/iio/iio.h> 26 #include <linux/iio/trigger.h> 27 #include <linux/iio/buffer.h> 28 #include <linux/iio/trigger_consumer.h> 29 #include <linux/iio/triggered_buffer.h> 30 #include <linux/regulator/consumer.h> 31 32 #define TI_ADC_DRV_NAME "ti-adc161s626" 33 34 enum { 35 TI_ADC141S626, 36 TI_ADC161S626, 37 }; 38 39 static const struct iio_chan_spec ti_adc141s626_channels[] = { 40 { 41 .type = IIO_VOLTAGE, 42 .channel = 0, 43 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 44 BIT(IIO_CHAN_INFO_SCALE) | 45 BIT(IIO_CHAN_INFO_OFFSET), 46 .scan_index = 0, 47 .scan_type = { 48 .sign = 's', 49 .realbits = 14, 50 .storagebits = 16, 51 }, 52 }, 53 IIO_CHAN_SOFT_TIMESTAMP(1), 54 }; 55 56 static const struct iio_chan_spec ti_adc161s626_channels[] = { 57 { 58 .type = IIO_VOLTAGE, 59 .channel = 0, 60 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 61 BIT(IIO_CHAN_INFO_SCALE) | 62 BIT(IIO_CHAN_INFO_OFFSET), 63 .scan_index = 0, 64 .scan_type = { 65 .sign = 's', 66 .realbits = 16, 67 .storagebits = 16, 68 }, 69 }, 70 IIO_CHAN_SOFT_TIMESTAMP(1), 71 }; 72 73 struct ti_adc_data { 74 struct iio_dev *indio_dev; 75 struct spi_device *spi; 76 struct regulator *ref; 77 78 u8 read_size; 79 u8 shift; 80 81 u8 buffer[16] ____cacheline_aligned; 82 }; 83 84 static int ti_adc_read_measurement(struct ti_adc_data *data, 85 struct iio_chan_spec const *chan, int *val) 86 { 87 int ret; 88 89 switch (data->read_size) { 90 case 2: { 91 __be16 buf; 92 93 ret = spi_read(data->spi, (void *) &buf, 2); 94 if (ret) 95 return ret; 96 97 *val = be16_to_cpu(buf); 98 break; 99 } 100 case 3: { 101 __be32 buf; 102 103 ret = spi_read(data->spi, (void *) &buf, 3); 104 if (ret) 105 return ret; 106 107 *val = be32_to_cpu(buf) >> 8; 108 break; 109 } 110 default: 111 return -EINVAL; 112 } 113 114 *val = sign_extend32(*val >> data->shift, chan->scan_type.realbits - 1); 115 116 return 0; 117 } 118 119 static irqreturn_t ti_adc_trigger_handler(int irq, void *private) 120 { 121 struct iio_poll_func *pf = private; 122 struct iio_dev *indio_dev = pf->indio_dev; 123 struct ti_adc_data *data = iio_priv(indio_dev); 124 int ret; 125 126 ret = ti_adc_read_measurement(data, &indio_dev->channels[0], 127 (int *) &data->buffer); 128 if (!ret) 129 iio_push_to_buffers_with_timestamp(indio_dev, 130 data->buffer, 131 iio_get_time_ns(indio_dev)); 132 133 iio_trigger_notify_done(indio_dev->trig); 134 135 return IRQ_HANDLED; 136 } 137 138 static int ti_adc_read_raw(struct iio_dev *indio_dev, 139 struct iio_chan_spec const *chan, 140 int *val, int *val2, long mask) 141 { 142 struct ti_adc_data *data = iio_priv(indio_dev); 143 int ret; 144 145 switch (mask) { 146 case IIO_CHAN_INFO_RAW: 147 ret = iio_device_claim_direct_mode(indio_dev); 148 if (ret) 149 return ret; 150 151 ret = ti_adc_read_measurement(data, chan, val); 152 iio_device_release_direct_mode(indio_dev); 153 154 if (ret) 155 return ret; 156 157 return IIO_VAL_INT; 158 case IIO_CHAN_INFO_SCALE: 159 ret = regulator_get_voltage(data->ref); 160 if (ret < 0) 161 return ret; 162 163 *val = ret / 1000; 164 *val2 = chan->scan_type.realbits; 165 166 return IIO_VAL_FRACTIONAL_LOG2; 167 case IIO_CHAN_INFO_OFFSET: 168 *val = 1 << (chan->scan_type.realbits - 1); 169 return IIO_VAL_INT; 170 } 171 172 return 0; 173 } 174 175 static const struct iio_info ti_adc_info = { 176 .read_raw = ti_adc_read_raw, 177 }; 178 179 static int ti_adc_probe(struct spi_device *spi) 180 { 181 struct iio_dev *indio_dev; 182 struct ti_adc_data *data; 183 int ret; 184 185 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data)); 186 if (!indio_dev) 187 return -ENOMEM; 188 189 indio_dev->info = &ti_adc_info; 190 indio_dev->dev.parent = &spi->dev; 191 indio_dev->dev.of_node = spi->dev.of_node; 192 indio_dev->name = TI_ADC_DRV_NAME; 193 indio_dev->modes = INDIO_DIRECT_MODE; 194 spi_set_drvdata(spi, indio_dev); 195 196 data = iio_priv(indio_dev); 197 data->spi = spi; 198 199 switch (spi_get_device_id(spi)->driver_data) { 200 case TI_ADC141S626: 201 indio_dev->channels = ti_adc141s626_channels; 202 indio_dev->num_channels = ARRAY_SIZE(ti_adc141s626_channels); 203 data->shift = 0; 204 data->read_size = 2; 205 break; 206 case TI_ADC161S626: 207 indio_dev->channels = ti_adc161s626_channels; 208 indio_dev->num_channels = ARRAY_SIZE(ti_adc161s626_channels); 209 data->shift = 6; 210 data->read_size = 3; 211 break; 212 } 213 214 data->ref = devm_regulator_get(&spi->dev, "vdda"); 215 if (!IS_ERR(data->ref)) { 216 ret = regulator_enable(data->ref); 217 if (ret < 0) 218 return ret; 219 } 220 221 ret = iio_triggered_buffer_setup(indio_dev, NULL, 222 ti_adc_trigger_handler, NULL); 223 if (ret) 224 goto error_regulator_disable; 225 226 ret = iio_device_register(indio_dev); 227 if (ret) 228 goto error_unreg_buffer; 229 230 return 0; 231 232 error_unreg_buffer: 233 iio_triggered_buffer_cleanup(indio_dev); 234 235 error_regulator_disable: 236 regulator_disable(data->ref); 237 238 return ret; 239 } 240 241 static int ti_adc_remove(struct spi_device *spi) 242 { 243 struct iio_dev *indio_dev = spi_get_drvdata(spi); 244 struct ti_adc_data *data = iio_priv(indio_dev); 245 246 iio_device_unregister(indio_dev); 247 iio_triggered_buffer_cleanup(indio_dev); 248 regulator_disable(data->ref); 249 250 return 0; 251 } 252 253 static const struct of_device_id ti_adc_dt_ids[] = { 254 { .compatible = "ti,adc141s626", }, 255 { .compatible = "ti,adc161s626", }, 256 {} 257 }; 258 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids); 259 260 static const struct spi_device_id ti_adc_id[] = { 261 {"adc141s626", TI_ADC141S626}, 262 {"adc161s626", TI_ADC161S626}, 263 {}, 264 }; 265 MODULE_DEVICE_TABLE(spi, ti_adc_id); 266 267 static struct spi_driver ti_adc_driver = { 268 .driver = { 269 .name = TI_ADC_DRV_NAME, 270 .of_match_table = of_match_ptr(ti_adc_dt_ids), 271 }, 272 .probe = ti_adc_probe, 273 .remove = ti_adc_remove, 274 .id_table = ti_adc_id, 275 }; 276 module_spi_driver(ti_adc_driver); 277 278 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>"); 279 MODULE_DESCRIPTION("Texas Instruments ADC1x1S 1-channel differential ADC"); 280 MODULE_LICENSE("GPL"); 281