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 .driver_module = THIS_MODULE, 177 .read_raw = ti_adc_read_raw, 178 }; 179 180 static int ti_adc_probe(struct spi_device *spi) 181 { 182 struct iio_dev *indio_dev; 183 struct ti_adc_data *data; 184 int ret; 185 186 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data)); 187 if (!indio_dev) 188 return -ENOMEM; 189 190 indio_dev->info = &ti_adc_info; 191 indio_dev->dev.parent = &spi->dev; 192 indio_dev->dev.of_node = spi->dev.of_node; 193 indio_dev->name = TI_ADC_DRV_NAME; 194 indio_dev->modes = INDIO_DIRECT_MODE; 195 spi_set_drvdata(spi, indio_dev); 196 197 data = iio_priv(indio_dev); 198 data->spi = spi; 199 200 switch (spi_get_device_id(spi)->driver_data) { 201 case TI_ADC141S626: 202 indio_dev->channels = ti_adc141s626_channels; 203 indio_dev->num_channels = ARRAY_SIZE(ti_adc141s626_channels); 204 data->shift = 0; 205 data->read_size = 2; 206 break; 207 case TI_ADC161S626: 208 indio_dev->channels = ti_adc161s626_channels; 209 indio_dev->num_channels = ARRAY_SIZE(ti_adc161s626_channels); 210 data->shift = 6; 211 data->read_size = 3; 212 break; 213 } 214 215 data->ref = devm_regulator_get(&spi->dev, "vdda"); 216 if (!IS_ERR(data->ref)) { 217 ret = regulator_enable(data->ref); 218 if (ret < 0) 219 return ret; 220 } 221 222 ret = iio_triggered_buffer_setup(indio_dev, NULL, 223 ti_adc_trigger_handler, NULL); 224 if (ret) 225 goto error_regulator_disable; 226 227 ret = iio_device_register(indio_dev); 228 if (ret) 229 goto error_unreg_buffer; 230 231 return 0; 232 233 error_unreg_buffer: 234 iio_triggered_buffer_cleanup(indio_dev); 235 236 error_regulator_disable: 237 regulator_disable(data->ref); 238 239 return ret; 240 } 241 242 static int ti_adc_remove(struct spi_device *spi) 243 { 244 struct iio_dev *indio_dev = spi_get_drvdata(spi); 245 struct ti_adc_data *data = iio_priv(indio_dev); 246 247 iio_device_unregister(indio_dev); 248 iio_triggered_buffer_cleanup(indio_dev); 249 regulator_disable(data->ref); 250 251 return 0; 252 } 253 254 static const struct of_device_id ti_adc_dt_ids[] = { 255 { .compatible = "ti,adc141s626", }, 256 { .compatible = "ti,adc161s626", }, 257 {} 258 }; 259 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids); 260 261 static const struct spi_device_id ti_adc_id[] = { 262 {"adc141s626", TI_ADC141S626}, 263 {"adc161s626", TI_ADC161S626}, 264 {}, 265 }; 266 MODULE_DEVICE_TABLE(spi, ti_adc_id); 267 268 static struct spi_driver ti_adc_driver = { 269 .driver = { 270 .name = TI_ADC_DRV_NAME, 271 .of_match_table = of_match_ptr(ti_adc_dt_ids), 272 }, 273 .probe = ti_adc_probe, 274 .remove = ti_adc_remove, 275 .id_table = ti_adc_id, 276 }; 277 module_spi_driver(ti_adc_driver); 278 279 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>"); 280 MODULE_DESCRIPTION("Texas Instruments ADC1x1S 1-channel differential ADC"); 281 MODULE_LICENSE("GPL"); 282