rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * MAX1117/MAX1118/MAX1119 8-bit, dual-channel ADCs driver |
| 3 | * |
| 4 | * Copyright (c) 2017 Akinobu Mita <akinobu.mita@gmail.com> |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of version 2 of |
| 7 | * the GNU General Public License. See the file COPYING in the main |
| 8 | * directory of this archive for more details. |
| 9 | * |
| 10 | * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1117-MAX1119.pdf |
| 11 | * |
| 12 | * SPI interface connections |
| 13 | * |
| 14 | * SPI MAXIM |
| 15 | * Master Direction MAX1117/8/9 |
| 16 | * ------ --------- ----------- |
| 17 | * nCS --> CNVST |
| 18 | * SCK --> SCLK |
| 19 | * MISO <-- DOUT |
| 20 | * ------ --------- ----------- |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/spi/spi.h> |
| 25 | #include <linux/iio/iio.h> |
| 26 | #include <linux/iio/buffer.h> |
| 27 | #include <linux/iio/triggered_buffer.h> |
| 28 | #include <linux/iio/trigger_consumer.h> |
| 29 | #include <linux/regulator/consumer.h> |
| 30 | |
| 31 | enum max1118_id { |
| 32 | max1117, |
| 33 | max1118, |
| 34 | max1119, |
| 35 | }; |
| 36 | |
| 37 | struct max1118 { |
| 38 | struct spi_device *spi; |
| 39 | struct mutex lock; |
| 40 | struct regulator *reg; |
| 41 | /* Ensure natural alignment of buffer elements */ |
| 42 | struct { |
| 43 | u8 channels[2]; |
| 44 | s64 ts __aligned(8); |
| 45 | } scan; |
| 46 | |
| 47 | u8 data ____cacheline_aligned; |
| 48 | }; |
| 49 | |
| 50 | #define MAX1118_CHANNEL(ch) \ |
| 51 | { \ |
| 52 | .type = IIO_VOLTAGE, \ |
| 53 | .indexed = 1, \ |
| 54 | .channel = (ch), \ |
| 55 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 56 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
| 57 | .scan_index = ch, \ |
| 58 | .scan_type = { \ |
| 59 | .sign = 'u', \ |
| 60 | .realbits = 8, \ |
| 61 | .storagebits = 8, \ |
| 62 | }, \ |
| 63 | } |
| 64 | |
| 65 | static const struct iio_chan_spec max1118_channels[] = { |
| 66 | MAX1118_CHANNEL(0), |
| 67 | MAX1118_CHANNEL(1), |
| 68 | IIO_CHAN_SOFT_TIMESTAMP(2), |
| 69 | }; |
| 70 | |
| 71 | static int max1118_read(struct spi_device *spi, int channel) |
| 72 | { |
| 73 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 74 | struct max1118 *adc = iio_priv(indio_dev); |
| 75 | struct spi_transfer xfers[] = { |
| 76 | /* |
| 77 | * To select CH1 for conversion, CNVST pin must be brought high |
| 78 | * and low for a second time. |
| 79 | */ |
| 80 | { |
| 81 | .len = 0, |
| 82 | .delay_usecs = 1, /* > CNVST Low Time 100 ns */ |
| 83 | .cs_change = 1, |
| 84 | }, |
| 85 | /* |
| 86 | * The acquisition interval begins with the falling edge of |
| 87 | * CNVST. The total acquisition and conversion process takes |
| 88 | * <7.5us. |
| 89 | */ |
| 90 | { |
| 91 | .len = 0, |
| 92 | .delay_usecs = 8, |
| 93 | }, |
| 94 | { |
| 95 | .rx_buf = &adc->data, |
| 96 | .len = 1, |
| 97 | }, |
| 98 | }; |
| 99 | int ret; |
| 100 | |
| 101 | if (channel == 0) |
| 102 | ret = spi_sync_transfer(spi, xfers + 1, 2); |
| 103 | else |
| 104 | ret = spi_sync_transfer(spi, xfers, 3); |
| 105 | |
| 106 | if (ret) |
| 107 | return ret; |
| 108 | |
| 109 | return adc->data; |
| 110 | } |
| 111 | |
| 112 | static int max1118_get_vref_mV(struct spi_device *spi) |
| 113 | { |
| 114 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 115 | struct max1118 *adc = iio_priv(indio_dev); |
| 116 | const struct spi_device_id *id = spi_get_device_id(spi); |
| 117 | int vref_uV; |
| 118 | |
| 119 | switch (id->driver_data) { |
| 120 | case max1117: |
| 121 | return 2048; |
| 122 | case max1119: |
| 123 | return 4096; |
| 124 | case max1118: |
| 125 | vref_uV = regulator_get_voltage(adc->reg); |
| 126 | if (vref_uV < 0) |
| 127 | return vref_uV; |
| 128 | return vref_uV / 1000; |
| 129 | } |
| 130 | |
| 131 | return -ENODEV; |
| 132 | } |
| 133 | |
| 134 | static int max1118_read_raw(struct iio_dev *indio_dev, |
| 135 | struct iio_chan_spec const *chan, |
| 136 | int *val, int *val2, long mask) |
| 137 | { |
| 138 | struct max1118 *adc = iio_priv(indio_dev); |
| 139 | |
| 140 | switch (mask) { |
| 141 | case IIO_CHAN_INFO_RAW: |
| 142 | mutex_lock(&adc->lock); |
| 143 | *val = max1118_read(adc->spi, chan->channel); |
| 144 | mutex_unlock(&adc->lock); |
| 145 | if (*val < 0) |
| 146 | return *val; |
| 147 | |
| 148 | return IIO_VAL_INT; |
| 149 | case IIO_CHAN_INFO_SCALE: |
| 150 | *val = max1118_get_vref_mV(adc->spi); |
| 151 | if (*val < 0) |
| 152 | return *val; |
| 153 | *val2 = 8; |
| 154 | |
| 155 | return IIO_VAL_FRACTIONAL_LOG2; |
| 156 | } |
| 157 | |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | static const struct iio_info max1118_info = { |
| 162 | .read_raw = max1118_read_raw, |
| 163 | .driver_module = THIS_MODULE, |
| 164 | }; |
| 165 | |
| 166 | static irqreturn_t max1118_trigger_handler(int irq, void *p) |
| 167 | { |
| 168 | struct iio_poll_func *pf = p; |
| 169 | struct iio_dev *indio_dev = pf->indio_dev; |
| 170 | struct max1118 *adc = iio_priv(indio_dev); |
| 171 | int scan_index; |
| 172 | int i = 0; |
| 173 | |
| 174 | mutex_lock(&adc->lock); |
| 175 | |
| 176 | for_each_set_bit(scan_index, indio_dev->active_scan_mask, |
| 177 | indio_dev->masklength) { |
| 178 | const struct iio_chan_spec *scan_chan = |
| 179 | &indio_dev->channels[scan_index]; |
| 180 | int ret = max1118_read(adc->spi, scan_chan->channel); |
| 181 | |
| 182 | if (ret < 0) { |
| 183 | dev_warn(&adc->spi->dev, |
| 184 | "failed to get conversion data\n"); |
| 185 | goto out; |
| 186 | } |
| 187 | |
| 188 | adc->scan.channels[i] = ret; |
| 189 | i++; |
| 190 | } |
| 191 | iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan, |
| 192 | iio_get_time_ns(indio_dev)); |
| 193 | out: |
| 194 | mutex_unlock(&adc->lock); |
| 195 | |
| 196 | iio_trigger_notify_done(indio_dev->trig); |
| 197 | |
| 198 | return IRQ_HANDLED; |
| 199 | } |
| 200 | |
| 201 | static int max1118_probe(struct spi_device *spi) |
| 202 | { |
| 203 | struct iio_dev *indio_dev; |
| 204 | struct max1118 *adc; |
| 205 | const struct spi_device_id *id = spi_get_device_id(spi); |
| 206 | int ret; |
| 207 | |
| 208 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc)); |
| 209 | if (!indio_dev) |
| 210 | return -ENOMEM; |
| 211 | |
| 212 | adc = iio_priv(indio_dev); |
| 213 | adc->spi = spi; |
| 214 | mutex_init(&adc->lock); |
| 215 | |
| 216 | if (id->driver_data == max1118) { |
| 217 | adc->reg = devm_regulator_get(&spi->dev, "vref"); |
| 218 | if (IS_ERR(adc->reg)) { |
| 219 | dev_err(&spi->dev, "failed to get vref regulator\n"); |
| 220 | return PTR_ERR(adc->reg); |
| 221 | } |
| 222 | ret = regulator_enable(adc->reg); |
| 223 | if (ret) |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | spi_set_drvdata(spi, indio_dev); |
| 228 | |
| 229 | indio_dev->name = spi_get_device_id(spi)->name; |
| 230 | indio_dev->dev.parent = &spi->dev; |
| 231 | indio_dev->info = &max1118_info; |
| 232 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 233 | indio_dev->channels = max1118_channels; |
| 234 | indio_dev->num_channels = ARRAY_SIZE(max1118_channels); |
| 235 | |
| 236 | /* |
| 237 | * To reinitiate a conversion on CH0, it is necessary to allow for a |
| 238 | * conversion to be complete and all of the data to be read out. Once |
| 239 | * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go |
| 240 | * into AutoShutdown mode until the next conversion is initiated. |
| 241 | */ |
| 242 | max1118_read(spi, 0); |
| 243 | |
| 244 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 245 | max1118_trigger_handler, NULL); |
| 246 | if (ret) |
| 247 | goto err_reg_disable; |
| 248 | |
| 249 | ret = iio_device_register(indio_dev); |
| 250 | if (ret) |
| 251 | goto err_buffer_cleanup; |
| 252 | |
| 253 | return 0; |
| 254 | |
| 255 | err_buffer_cleanup: |
| 256 | iio_triggered_buffer_cleanup(indio_dev); |
| 257 | err_reg_disable: |
| 258 | if (id->driver_data == max1118) |
| 259 | regulator_disable(adc->reg); |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static int max1118_remove(struct spi_device *spi) |
| 265 | { |
| 266 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 267 | struct max1118 *adc = iio_priv(indio_dev); |
| 268 | const struct spi_device_id *id = spi_get_device_id(spi); |
| 269 | |
| 270 | iio_device_unregister(indio_dev); |
| 271 | iio_triggered_buffer_cleanup(indio_dev); |
| 272 | if (id->driver_data == max1118) |
| 273 | return regulator_disable(adc->reg); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static const struct spi_device_id max1118_id[] = { |
| 279 | { "max1117", max1117 }, |
| 280 | { "max1118", max1118 }, |
| 281 | { "max1119", max1119 }, |
| 282 | {} |
| 283 | }; |
| 284 | MODULE_DEVICE_TABLE(spi, max1118_id); |
| 285 | |
| 286 | #ifdef CONFIG_OF |
| 287 | |
| 288 | static const struct of_device_id max1118_dt_ids[] = { |
| 289 | { .compatible = "maxim,max1117" }, |
| 290 | { .compatible = "maxim,max1118" }, |
| 291 | { .compatible = "maxim,max1119" }, |
| 292 | {}, |
| 293 | }; |
| 294 | MODULE_DEVICE_TABLE(of, max1118_dt_ids); |
| 295 | |
| 296 | #endif |
| 297 | |
| 298 | static struct spi_driver max1118_spi_driver = { |
| 299 | .driver = { |
| 300 | .name = "max1118", |
| 301 | .of_match_table = of_match_ptr(max1118_dt_ids), |
| 302 | }, |
| 303 | .probe = max1118_probe, |
| 304 | .remove = max1118_remove, |
| 305 | .id_table = max1118_id, |
| 306 | }; |
| 307 | module_spi_driver(max1118_spi_driver); |
| 308 | |
| 309 | MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>"); |
| 310 | MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver"); |
| 311 | MODULE_LICENSE("GPL v2"); |