1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * TI tlc4541 ADC Driver 4 * 5 * Copyright (C) 2017 Phil Reid 6 * 7 * Datasheets can be found here: 8 * http://www.ti.com/lit/gpn/tlc3541 9 * http://www.ti.com/lit/gpn/tlc4541 10 * 11 * The tlc4541 requires 24 clock cycles to start a transfer. 12 * Conversion then takes 2.94us to complete before data is ready 13 * Data is returned MSB first. 14 */ 15 16 #include <linux/delay.h> 17 #include <linux/device.h> 18 #include <linux/err.h> 19 #include <linux/interrupt.h> 20 #include <linux/iio/iio.h> 21 #include <linux/iio/sysfs.h> 22 #include <linux/iio/buffer.h> 23 #include <linux/iio/trigger_consumer.h> 24 #include <linux/iio/triggered_buffer.h> 25 #include <linux/kernel.h> 26 #include <linux/module.h> 27 #include <linux/regulator/consumer.h> 28 #include <linux/slab.h> 29 #include <linux/spi/spi.h> 30 #include <linux/sysfs.h> 31 32 struct tlc4541_state { 33 struct spi_device *spi; 34 struct regulator *reg; 35 struct spi_transfer scan_single_xfer[3]; 36 struct spi_message scan_single_msg; 37 38 /* 39 * DMA (thus cache coherency maintenance) requires the 40 * transfer buffers to live in their own cache lines. 41 * 2 bytes data + 6 bytes padding + 8 bytes timestamp when 42 * call iio_push_to_buffers_with_timestamp. 43 */ 44 __be16 rx_buf[8] ____cacheline_aligned; 45 }; 46 47 struct tlc4541_chip_info { 48 const struct iio_chan_spec *channels; 49 unsigned int num_channels; 50 }; 51 52 enum tlc4541_id { 53 TLC3541, 54 TLC4541, 55 }; 56 57 #define TLC4541_V_CHAN(bits, bitshift) { \ 58 .type = IIO_VOLTAGE, \ 59 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 60 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 61 .scan_type = { \ 62 .sign = 'u', \ 63 .realbits = (bits), \ 64 .storagebits = 16, \ 65 .shift = (bitshift), \ 66 .endianness = IIO_BE, \ 67 }, \ 68 } 69 70 #define DECLARE_TLC4541_CHANNELS(name, bits, bitshift) \ 71 const struct iio_chan_spec name ## _channels[] = { \ 72 TLC4541_V_CHAN(bits, bitshift), \ 73 IIO_CHAN_SOFT_TIMESTAMP(1), \ 74 } 75 76 static DECLARE_TLC4541_CHANNELS(tlc3541, 14, 2); 77 static DECLARE_TLC4541_CHANNELS(tlc4541, 16, 0); 78 79 static const struct tlc4541_chip_info tlc4541_chip_info[] = { 80 [TLC3541] = { 81 .channels = tlc3541_channels, 82 .num_channels = ARRAY_SIZE(tlc3541_channels), 83 }, 84 [TLC4541] = { 85 .channels = tlc4541_channels, 86 .num_channels = ARRAY_SIZE(tlc4541_channels), 87 }, 88 }; 89 90 static irqreturn_t tlc4541_trigger_handler(int irq, void *p) 91 { 92 struct iio_poll_func *pf = p; 93 struct iio_dev *indio_dev = pf->indio_dev; 94 struct tlc4541_state *st = iio_priv(indio_dev); 95 int ret; 96 97 ret = spi_sync(st->spi, &st->scan_single_msg); 98 if (ret < 0) 99 goto done; 100 101 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf, 102 iio_get_time_ns(indio_dev)); 103 104 done: 105 iio_trigger_notify_done(indio_dev->trig); 106 return IRQ_HANDLED; 107 } 108 109 static int tlc4541_get_range(struct tlc4541_state *st) 110 { 111 int vref; 112 113 vref = regulator_get_voltage(st->reg); 114 if (vref < 0) 115 return vref; 116 117 vref /= 1000; 118 119 return vref; 120 } 121 122 static int tlc4541_read_raw(struct iio_dev *indio_dev, 123 struct iio_chan_spec const *chan, 124 int *val, 125 int *val2, 126 long m) 127 { 128 int ret = 0; 129 struct tlc4541_state *st = iio_priv(indio_dev); 130 131 switch (m) { 132 case IIO_CHAN_INFO_RAW: 133 ret = iio_device_claim_direct_mode(indio_dev); 134 if (ret) 135 return ret; 136 ret = spi_sync(st->spi, &st->scan_single_msg); 137 iio_device_release_direct_mode(indio_dev); 138 if (ret < 0) 139 return ret; 140 *val = be16_to_cpu(st->rx_buf[0]); 141 *val = *val >> chan->scan_type.shift; 142 *val &= GENMASK(chan->scan_type.realbits - 1, 0); 143 return IIO_VAL_INT; 144 case IIO_CHAN_INFO_SCALE: 145 ret = tlc4541_get_range(st); 146 if (ret < 0) 147 return ret; 148 *val = ret; 149 *val2 = chan->scan_type.realbits; 150 return IIO_VAL_FRACTIONAL_LOG2; 151 } 152 return -EINVAL; 153 } 154 155 static const struct iio_info tlc4541_info = { 156 .read_raw = &tlc4541_read_raw, 157 }; 158 159 static int tlc4541_probe(struct spi_device *spi) 160 { 161 struct tlc4541_state *st; 162 struct iio_dev *indio_dev; 163 const struct tlc4541_chip_info *info; 164 int ret; 165 int8_t device_init = 0; 166 167 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 168 if (indio_dev == NULL) 169 return -ENOMEM; 170 171 st = iio_priv(indio_dev); 172 173 spi_set_drvdata(spi, indio_dev); 174 175 st->spi = spi; 176 177 info = &tlc4541_chip_info[spi_get_device_id(spi)->driver_data]; 178 179 indio_dev->name = spi_get_device_id(spi)->name; 180 indio_dev->dev.parent = &spi->dev; 181 indio_dev->modes = INDIO_DIRECT_MODE; 182 indio_dev->channels = info->channels; 183 indio_dev->num_channels = info->num_channels; 184 indio_dev->info = &tlc4541_info; 185 186 /* perform reset */ 187 spi_write(spi, &device_init, 1); 188 189 /* Setup default message */ 190 st->scan_single_xfer[0].rx_buf = &st->rx_buf[0]; 191 st->scan_single_xfer[0].len = 3; 192 st->scan_single_xfer[1].delay.value = 3; 193 st->scan_single_xfer[1].delay.unit = SPI_DELAY_UNIT_NSECS; 194 st->scan_single_xfer[2].rx_buf = &st->rx_buf[0]; 195 st->scan_single_xfer[2].len = 2; 196 197 spi_message_init_with_transfers(&st->scan_single_msg, 198 st->scan_single_xfer, 3); 199 200 st->reg = devm_regulator_get(&spi->dev, "vref"); 201 if (IS_ERR(st->reg)) 202 return PTR_ERR(st->reg); 203 204 ret = regulator_enable(st->reg); 205 if (ret) 206 return ret; 207 208 ret = iio_triggered_buffer_setup(indio_dev, NULL, 209 &tlc4541_trigger_handler, NULL); 210 if (ret) 211 goto error_disable_reg; 212 213 ret = iio_device_register(indio_dev); 214 if (ret) 215 goto error_cleanup_buffer; 216 217 return 0; 218 219 error_cleanup_buffer: 220 iio_triggered_buffer_cleanup(indio_dev); 221 error_disable_reg: 222 regulator_disable(st->reg); 223 224 return ret; 225 } 226 227 static int tlc4541_remove(struct spi_device *spi) 228 { 229 struct iio_dev *indio_dev = spi_get_drvdata(spi); 230 struct tlc4541_state *st = iio_priv(indio_dev); 231 232 iio_device_unregister(indio_dev); 233 iio_triggered_buffer_cleanup(indio_dev); 234 regulator_disable(st->reg); 235 236 return 0; 237 } 238 239 #ifdef CONFIG_OF 240 static const struct of_device_id tlc4541_dt_ids[] = { 241 { .compatible = "ti,tlc3541", }, 242 { .compatible = "ti,tlc4541", }, 243 {} 244 }; 245 MODULE_DEVICE_TABLE(of, tlc4541_dt_ids); 246 #endif 247 248 static const struct spi_device_id tlc4541_id[] = { 249 {"tlc3541", TLC3541}, 250 {"tlc4541", TLC4541}, 251 {} 252 }; 253 MODULE_DEVICE_TABLE(spi, tlc4541_id); 254 255 static struct spi_driver tlc4541_driver = { 256 .driver = { 257 .name = "tlc4541", 258 .of_match_table = of_match_ptr(tlc4541_dt_ids), 259 }, 260 .probe = tlc4541_probe, 261 .remove = tlc4541_remove, 262 .id_table = tlc4541_id, 263 }; 264 module_spi_driver(tlc4541_driver); 265 266 MODULE_AUTHOR("Phil Reid <preid@electromag.com.au>"); 267 MODULE_DESCRIPTION("Texas Instruments TLC4541 ADC"); 268 MODULE_LICENSE("GPL v2"); 269