Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 1 | /* |
| 2 | * TI ADC MFD driver |
| 3 | * |
| 4 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation version 2. |
| 9 | * |
| 10 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 11 | * kind, whether express or implied; without even the implied warranty |
| 12 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/iio/iio.h> |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 24 | #include <linux/of.h> |
| 25 | #include <linux/of_device.h> |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 26 | #include <linux/iio/machine.h> |
| 27 | #include <linux/iio/driver.h> |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 28 | |
| 29 | #include <linux/mfd/ti_am335x_tscadc.h> |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 30 | #include <linux/iio/buffer.h> |
| 31 | #include <linux/iio/kfifo_buf.h> |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 32 | |
| 33 | struct tiadc_device { |
| 34 | struct ti_tscadc_dev *mfd_tscadc; |
Vignesh R | 90c43ec | 2016-08-17 17:43:00 +0530 | [diff] [blame^] | 35 | struct mutex fifo1_lock; /* to protect fifo access */ |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 36 | int channels; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 37 | u8 channel_line[8]; |
| 38 | u8 channel_step[8]; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 39 | int buffer_en_ch_steps; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 40 | u16 data[8]; |
Vignesh R | 5dc11e8 | 2015-03-31 16:42:37 +0530 | [diff] [blame] | 41 | u32 open_delay[8], sample_delay[8], step_avg[8]; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg) |
| 45 | { |
| 46 | return readl(adc->mfd_tscadc->tscadc_base + reg); |
| 47 | } |
| 48 | |
| 49 | static void tiadc_writel(struct tiadc_device *adc, unsigned int reg, |
| 50 | unsigned int val) |
| 51 | { |
| 52 | writel(val, adc->mfd_tscadc->tscadc_base + reg); |
| 53 | } |
| 54 | |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 55 | static u32 get_adc_step_mask(struct tiadc_device *adc_dev) |
| 56 | { |
| 57 | u32 step_en; |
| 58 | |
| 59 | step_en = ((1 << adc_dev->channels) - 1); |
| 60 | step_en <<= TOTAL_STEPS - adc_dev->channels + 1; |
| 61 | return step_en; |
| 62 | } |
| 63 | |
Sebastian Andrzej Siewior | 7ca6740 | 2013-12-19 16:28:31 +0100 | [diff] [blame] | 64 | static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev, |
| 65 | struct iio_chan_spec const *chan) |
| 66 | { |
| 67 | int i; |
| 68 | |
| 69 | for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) { |
| 70 | if (chan->channel == adc_dev->channel_line[i]) { |
| 71 | u32 step; |
| 72 | |
| 73 | step = adc_dev->channel_step[i]; |
| 74 | /* +1 for the charger */ |
| 75 | return 1 << (step + 1); |
| 76 | } |
| 77 | } |
| 78 | WARN_ON(1); |
| 79 | return 0; |
| 80 | } |
| 81 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 82 | static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan) |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 83 | { |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 84 | return 1 << adc_dev->channel_step[chan]; |
| 85 | } |
| 86 | |
| 87 | static void tiadc_step_config(struct iio_dev *indio_dev) |
| 88 | { |
| 89 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
Vignesh R | 5dc11e8 | 2015-03-31 16:42:37 +0530 | [diff] [blame] | 90 | struct device *dev = adc_dev->mfd_tscadc->dev; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 91 | unsigned int stepconfig; |
Brad Griffis | 3a59684 | 2015-02-03 11:41:58 -0800 | [diff] [blame] | 92 | int i, steps = 0; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * There are 16 configurable steps and 8 analog input |
| 96 | * lines available which are shared between Touchscreen and ADC. |
| 97 | * |
Brad Griffis | 3a59684 | 2015-02-03 11:41:58 -0800 | [diff] [blame] | 98 | * Steps forwards i.e. from 0 towards 16 are used by ADC |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 99 | * depending on number of input lines needed. |
| 100 | * Channel would represent which analog input |
| 101 | * needs to be given to ADC to digitalize data. |
| 102 | */ |
| 103 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 104 | |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 105 | for (i = 0; i < adc_dev->channels; i++) { |
| 106 | int chan; |
| 107 | |
| 108 | chan = adc_dev->channel_line[i]; |
Vignesh R | 5dc11e8 | 2015-03-31 16:42:37 +0530 | [diff] [blame] | 109 | |
| 110 | if (adc_dev->step_avg[i] > STEPCONFIG_AVG_16) { |
| 111 | dev_warn(dev, "chan %d step_avg truncating to %d\n", |
| 112 | chan, STEPCONFIG_AVG_16); |
| 113 | adc_dev->step_avg[i] = STEPCONFIG_AVG_16; |
| 114 | } |
| 115 | |
| 116 | if (adc_dev->step_avg[i]) |
| 117 | stepconfig = |
| 118 | STEPCONFIG_AVG(ffs(adc_dev->step_avg[i]) - 1) | |
| 119 | STEPCONFIG_FIFO1; |
| 120 | else |
| 121 | stepconfig = STEPCONFIG_FIFO1; |
| 122 | |
| 123 | if (iio_buffer_enabled(indio_dev)) |
| 124 | stepconfig |= STEPCONFIG_MODE_SWCNT; |
| 125 | |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 126 | tiadc_writel(adc_dev, REG_STEPCONFIG(steps), |
| 127 | stepconfig | STEPCONFIG_INP(chan)); |
Vignesh R | 5dc11e8 | 2015-03-31 16:42:37 +0530 | [diff] [blame] | 128 | |
| 129 | if (adc_dev->open_delay[i] > STEPDELAY_OPEN_MASK) { |
| 130 | dev_warn(dev, "chan %d open delay truncating to 0x3FFFF\n", |
| 131 | chan); |
| 132 | adc_dev->open_delay[i] = STEPDELAY_OPEN_MASK; |
| 133 | } |
| 134 | |
| 135 | if (adc_dev->sample_delay[i] > 0xFF) { |
| 136 | dev_warn(dev, "chan %d sample delay truncating to 0xFF\n", |
| 137 | chan); |
| 138 | adc_dev->sample_delay[i] = 0xFF; |
| 139 | } |
| 140 | |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 141 | tiadc_writel(adc_dev, REG_STEPDELAY(steps), |
Vignesh R | 5dc11e8 | 2015-03-31 16:42:37 +0530 | [diff] [blame] | 142 | STEPDELAY_OPEN(adc_dev->open_delay[i]) | |
| 143 | STEPDELAY_SAMPLE(adc_dev->sample_delay[i])); |
| 144 | |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 145 | adc_dev->channel_step[i] = steps; |
| 146 | steps++; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 147 | } |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 148 | } |
| 149 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 150 | static irqreturn_t tiadc_irq_h(int irq, void *private) |
| 151 | { |
| 152 | struct iio_dev *indio_dev = private; |
| 153 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 154 | unsigned int status, config; |
| 155 | status = tiadc_readl(adc_dev, REG_IRQSTATUS); |
| 156 | |
| 157 | /* |
| 158 | * ADC and touchscreen share the IRQ line. |
| 159 | * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only |
| 160 | */ |
| 161 | if (status & IRQENB_FIFO1OVRRUN) { |
| 162 | /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */ |
| 163 | config = tiadc_readl(adc_dev, REG_CTRL); |
| 164 | config &= ~(CNTRLREG_TSCSSENB); |
| 165 | tiadc_writel(adc_dev, REG_CTRL, config); |
| 166 | tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN |
| 167 | | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES); |
| 168 | tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB)); |
| 169 | return IRQ_HANDLED; |
| 170 | } else if (status & IRQENB_FIFO1THRES) { |
| 171 | /* Disable irq and wake worker thread */ |
| 172 | tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES); |
| 173 | return IRQ_WAKE_THREAD; |
| 174 | } |
| 175 | |
| 176 | return IRQ_NONE; |
| 177 | } |
| 178 | |
| 179 | static irqreturn_t tiadc_worker_h(int irq, void *private) |
| 180 | { |
| 181 | struct iio_dev *indio_dev = private; |
| 182 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 183 | int i, k, fifo1count, read; |
| 184 | u16 *data = adc_dev->data; |
| 185 | |
| 186 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); |
| 187 | for (k = 0; k < fifo1count; k = k + i) { |
| 188 | for (i = 0; i < (indio_dev->scan_bytes)/2; i++) { |
| 189 | read = tiadc_readl(adc_dev, REG_FIFO1); |
| 190 | data[i] = read & FIFOREAD_DATA_MASK; |
| 191 | } |
| 192 | iio_push_to_buffers(indio_dev, (u8 *) data); |
| 193 | } |
| 194 | |
| 195 | tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES); |
| 196 | tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES); |
| 197 | |
| 198 | return IRQ_HANDLED; |
| 199 | } |
| 200 | |
| 201 | static int tiadc_buffer_preenable(struct iio_dev *indio_dev) |
| 202 | { |
| 203 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 204 | int i, fifo1count, read; |
| 205 | |
| 206 | tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES | |
| 207 | IRQENB_FIFO1OVRRUN | |
| 208 | IRQENB_FIFO1UNDRFLW)); |
| 209 | |
| 210 | /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */ |
| 211 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); |
| 212 | for (i = 0; i < fifo1count; i++) |
| 213 | read = tiadc_readl(adc_dev, REG_FIFO1); |
| 214 | |
Lars-Peter Clausen | 24adaf7 | 2013-10-14 17:49:00 +0100 | [diff] [blame] | 215 | return 0; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static int tiadc_buffer_postenable(struct iio_dev *indio_dev) |
| 219 | { |
| 220 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 221 | unsigned int enb = 0; |
| 222 | u8 bit; |
| 223 | |
| 224 | tiadc_step_config(indio_dev); |
Octavian Purdila | 70dddee | 2015-03-02 21:03:05 +0200 | [diff] [blame] | 225 | for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels) |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 226 | enb |= (get_adc_step_bit(adc_dev, bit) << 1); |
| 227 | adc_dev->buffer_en_ch_steps = enb; |
| 228 | |
Sebastian Andrzej Siewior | 7e170c6 | 2013-12-19 16:28:29 +0100 | [diff] [blame] | 229 | am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb); |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 230 | |
| 231 | tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES |
| 232 | | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW); |
| 233 | tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES |
| 234 | | IRQENB_FIFO1OVRRUN); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int tiadc_buffer_predisable(struct iio_dev *indio_dev) |
| 240 | { |
| 241 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 242 | int fifo1count, i, read; |
| 243 | |
| 244 | tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES | |
| 245 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW)); |
| 246 | am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps); |
Sebastian Andrzej Siewior | 3954b7b | 2013-12-19 16:28:30 +0100 | [diff] [blame] | 247 | adc_dev->buffer_en_ch_steps = 0; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 248 | |
| 249 | /* Flush FIFO of leftover data in the time it takes to disable adc */ |
| 250 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); |
| 251 | for (i = 0; i < fifo1count; i++) |
| 252 | read = tiadc_readl(adc_dev, REG_FIFO1); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int tiadc_buffer_postdisable(struct iio_dev *indio_dev) |
| 258 | { |
| 259 | tiadc_step_config(indio_dev); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = { |
| 265 | .preenable = &tiadc_buffer_preenable, |
| 266 | .postenable = &tiadc_buffer_postenable, |
| 267 | .predisable = &tiadc_buffer_predisable, |
| 268 | .postdisable = &tiadc_buffer_postdisable, |
| 269 | }; |
| 270 | |
Zubair Lutfullah | 98c08cf | 2013-09-22 09:20:00 +0100 | [diff] [blame] | 271 | static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev, |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 272 | irqreturn_t (*pollfunc_bh)(int irq, void *p), |
| 273 | irqreturn_t (*pollfunc_th)(int irq, void *p), |
| 274 | int irq, |
| 275 | unsigned long flags, |
| 276 | const struct iio_buffer_setup_ops *setup_ops) |
| 277 | { |
Lars-Peter Clausen | fe26980 | 2013-10-24 10:41:00 +0100 | [diff] [blame] | 278 | struct iio_buffer *buffer; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 279 | int ret; |
| 280 | |
Karol Wrona | 7ab374a | 2014-12-19 18:39:24 +0100 | [diff] [blame] | 281 | buffer = iio_kfifo_allocate(); |
Lars-Peter Clausen | fe26980 | 2013-10-24 10:41:00 +0100 | [diff] [blame] | 282 | if (!buffer) |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 283 | return -ENOMEM; |
| 284 | |
Lars-Peter Clausen | fe26980 | 2013-10-24 10:41:00 +0100 | [diff] [blame] | 285 | iio_device_attach_buffer(indio_dev, buffer); |
| 286 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 287 | ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh, |
| 288 | flags, indio_dev->name, indio_dev); |
| 289 | if (ret) |
| 290 | goto error_kfifo_free; |
| 291 | |
| 292 | indio_dev->setup_ops = setup_ops; |
Jonathan Cameron | 9d0be85 | 2016-01-01 18:05:34 +0000 | [diff] [blame] | 293 | indio_dev->modes |= INDIO_BUFFER_SOFTWARE; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 294 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 295 | return 0; |
| 296 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 297 | error_kfifo_free: |
| 298 | iio_kfifo_free(indio_dev->buffer); |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev) |
| 303 | { |
| 304 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 305 | |
| 306 | free_irq(adc_dev->mfd_tscadc->irq, indio_dev); |
| 307 | iio_kfifo_free(indio_dev->buffer); |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 311 | static const char * const chan_name_ain[] = { |
| 312 | "AIN0", |
| 313 | "AIN1", |
| 314 | "AIN2", |
| 315 | "AIN3", |
| 316 | "AIN4", |
| 317 | "AIN5", |
| 318 | "AIN6", |
| 319 | "AIN7", |
| 320 | }; |
| 321 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 322 | static int tiadc_channel_init(struct iio_dev *indio_dev, int channels) |
| 323 | { |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 324 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 325 | struct iio_chan_spec *chan_array; |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 326 | struct iio_chan_spec *chan; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 327 | int i; |
| 328 | |
| 329 | indio_dev->num_channels = channels; |
Andrew F. Davis | fea89e2 | 2016-05-31 12:00:12 -0500 | [diff] [blame] | 330 | chan_array = kcalloc(channels, sizeof(*chan_array), GFP_KERNEL); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 331 | if (chan_array == NULL) |
| 332 | return -ENOMEM; |
| 333 | |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 334 | chan = chan_array; |
| 335 | for (i = 0; i < channels; i++, chan++) { |
| 336 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 337 | chan->type = IIO_VOLTAGE; |
| 338 | chan->indexed = 1; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 339 | chan->channel = adc_dev->channel_line[i]; |
Jonathan Cameron | 6c57252 | 2013-02-27 19:07:18 +0000 | [diff] [blame] | 340 | chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 341 | chan->datasheet_name = chan_name_ain[chan->channel]; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 342 | chan->scan_index = i; |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 343 | chan->scan_type.sign = 'u'; |
| 344 | chan->scan_type.realbits = 12; |
Zubair Lutfullah | 0f6fc7d | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 345 | chan->scan_type.storagebits = 16; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | indio_dev->channels = chan_array; |
| 349 | |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 350 | return 0; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static void tiadc_channels_remove(struct iio_dev *indio_dev) |
| 354 | { |
| 355 | kfree(indio_dev->channels); |
| 356 | } |
| 357 | |
| 358 | static int tiadc_read_raw(struct iio_dev *indio_dev, |
| 359 | struct iio_chan_spec const *chan, |
| 360 | int *val, int *val2, long mask) |
| 361 | { |
| 362 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
Vignesh R | 90c43ec | 2016-08-17 17:43:00 +0530 | [diff] [blame^] | 363 | int ret = IIO_VAL_INT; |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 364 | int i, map_val; |
| 365 | unsigned int fifo1count, read, stepid; |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame] | 366 | bool found = false; |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 367 | u32 step_en; |
Sebastian Andrzej Siewior | 7ca6740 | 2013-12-19 16:28:31 +0100 | [diff] [blame] | 368 | unsigned long timeout; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 369 | |
| 370 | if (iio_buffer_enabled(indio_dev)) |
| 371 | return -EBUSY; |
| 372 | |
Sebastian Andrzej Siewior | 7ca6740 | 2013-12-19 16:28:31 +0100 | [diff] [blame] | 373 | step_en = get_adc_chan_step_mask(adc_dev, chan); |
| 374 | if (!step_en) |
| 375 | return -EINVAL; |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 376 | |
Vignesh R | 90c43ec | 2016-08-17 17:43:00 +0530 | [diff] [blame^] | 377 | mutex_lock(&adc_dev->fifo1_lock); |
Sebastian Andrzej Siewior | 7ca6740 | 2013-12-19 16:28:31 +0100 | [diff] [blame] | 378 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); |
| 379 | while (fifo1count--) |
| 380 | tiadc_readl(adc_dev, REG_FIFO1); |
| 381 | |
Sebastian Andrzej Siewior | 7e170c6 | 2013-12-19 16:28:29 +0100 | [diff] [blame] | 382 | am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en); |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 383 | |
Sebastian Andrzej Siewior | 7ca6740 | 2013-12-19 16:28:31 +0100 | [diff] [blame] | 384 | timeout = jiffies + usecs_to_jiffies |
| 385 | (IDLE_TIMEOUT * adc_dev->channels); |
| 386 | /* Wait for Fifo threshold interrupt */ |
| 387 | while (1) { |
| 388 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); |
| 389 | if (fifo1count) |
| 390 | break; |
| 391 | |
| 392 | if (time_after(jiffies, timeout)) { |
| 393 | am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); |
Vignesh R | 90c43ec | 2016-08-17 17:43:00 +0530 | [diff] [blame^] | 394 | ret = -EAGAIN; |
| 395 | goto err_unlock; |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 396 | } |
Sebastian Andrzej Siewior | fb7f8ce | 2013-12-19 16:28:27 +0100 | [diff] [blame] | 397 | } |
Jan Kardell | baa3c65 | 2014-11-06 22:18:00 +0000 | [diff] [blame] | 398 | map_val = adc_dev->channel_step[chan->scan_index]; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 399 | |
| 400 | /* |
Sebastian Andrzej Siewior | 7ca6740 | 2013-12-19 16:28:31 +0100 | [diff] [blame] | 401 | * We check the complete FIFO. We programmed just one entry but in case |
| 402 | * something went wrong we left empty handed (-EAGAIN previously) and |
| 403 | * then the value apeared somehow in the FIFO we would have two entries. |
| 404 | * Therefore we read every item and keep only the latest version of the |
| 405 | * requested channel. |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 406 | */ |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 407 | for (i = 0; i < fifo1count; i++) { |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 408 | read = tiadc_readl(adc_dev, REG_FIFO1); |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 409 | stepid = read & FIFOREAD_CHNLID_MASK; |
| 410 | stepid = stepid >> 0x10; |
| 411 | |
| 412 | if (stepid == map_val) { |
| 413 | read = read & FIFOREAD_DATA_MASK; |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame] | 414 | found = true; |
Zubair Lutfullah | 0f6fc7d | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 415 | *val = (u16) read; |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame] | 416 | } |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 417 | } |
Sebastian Andrzej Siewior | 7ca6740 | 2013-12-19 16:28:31 +0100 | [diff] [blame] | 418 | am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 419 | |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame] | 420 | if (found == false) |
Vignesh R | 90c43ec | 2016-08-17 17:43:00 +0530 | [diff] [blame^] | 421 | ret = -EBUSY; |
| 422 | |
| 423 | err_unlock: |
| 424 | mutex_unlock(&adc_dev->fifo1_lock); |
| 425 | return ret; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static const struct iio_info tiadc_info = { |
| 429 | .read_raw = &tiadc_read_raw, |
Wei Yongjun | bc93aa7 | 2013-07-06 05:20:00 +0100 | [diff] [blame] | 430 | .driver_module = THIS_MODULE, |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 431 | }; |
| 432 | |
Vignesh R | dee1f55 | 2015-03-31 16:42:36 +0530 | [diff] [blame] | 433 | static int tiadc_parse_dt(struct platform_device *pdev, |
| 434 | struct tiadc_device *adc_dev) |
| 435 | { |
| 436 | struct device_node *node = pdev->dev.of_node; |
| 437 | struct property *prop; |
| 438 | const __be32 *cur; |
| 439 | int channels = 0; |
| 440 | u32 val; |
| 441 | |
| 442 | of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) { |
| 443 | adc_dev->channel_line[channels] = val; |
Vignesh R | 5dc11e8 | 2015-03-31 16:42:37 +0530 | [diff] [blame] | 444 | |
| 445 | /* Set Default values for optional DT parameters */ |
| 446 | adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY; |
| 447 | adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY; |
| 448 | adc_dev->step_avg[channels] = 16; |
| 449 | |
Vignesh R | dee1f55 | 2015-03-31 16:42:36 +0530 | [diff] [blame] | 450 | channels++; |
| 451 | } |
| 452 | |
Vignesh R | 5dc11e8 | 2015-03-31 16:42:37 +0530 | [diff] [blame] | 453 | of_property_read_u32_array(node, "ti,chan-step-avg", |
| 454 | adc_dev->step_avg, channels); |
| 455 | of_property_read_u32_array(node, "ti,chan-step-opendelay", |
| 456 | adc_dev->open_delay, channels); |
| 457 | of_property_read_u32_array(node, "ti,chan-step-sampledelay", |
| 458 | adc_dev->sample_delay, channels); |
| 459 | |
Vignesh R | dee1f55 | 2015-03-31 16:42:36 +0530 | [diff] [blame] | 460 | adc_dev->channels = channels; |
| 461 | return 0; |
| 462 | } |
| 463 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 464 | static int tiadc_probe(struct platform_device *pdev) |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 465 | { |
| 466 | struct iio_dev *indio_dev; |
| 467 | struct tiadc_device *adc_dev; |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 468 | struct device_node *node = pdev->dev.of_node; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 469 | int err; |
| 470 | |
Sebastian Andrzej Siewior | 0ead4fb | 2013-05-21 17:49:22 +0200 | [diff] [blame] | 471 | if (!node) { |
| 472 | dev_err(&pdev->dev, "Could not find valid DT data.\n"); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 473 | return -EINVAL; |
| 474 | } |
| 475 | |
Andrew F. Davis | fea89e2 | 2016-05-31 12:00:12 -0500 | [diff] [blame] | 476 | indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*indio_dev)); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 477 | if (indio_dev == NULL) { |
| 478 | dev_err(&pdev->dev, "failed to allocate iio device\n"); |
Sachin Kamat | a064813 | 2013-07-23 09:46:00 +0100 | [diff] [blame] | 479 | return -ENOMEM; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 480 | } |
| 481 | adc_dev = iio_priv(indio_dev); |
| 482 | |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 483 | adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev); |
Vignesh R | dee1f55 | 2015-03-31 16:42:36 +0530 | [diff] [blame] | 484 | tiadc_parse_dt(pdev, adc_dev); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 485 | |
| 486 | indio_dev->dev.parent = &pdev->dev; |
| 487 | indio_dev->name = dev_name(&pdev->dev); |
| 488 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 489 | indio_dev->info = &tiadc_info; |
| 490 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 491 | tiadc_step_config(indio_dev); |
| 492 | tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD); |
Vignesh R | 90c43ec | 2016-08-17 17:43:00 +0530 | [diff] [blame^] | 493 | mutex_init(&adc_dev->fifo1_lock); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 494 | |
| 495 | err = tiadc_channel_init(indio_dev, adc_dev->channels); |
| 496 | if (err < 0) |
Sachin Kamat | a064813 | 2013-07-23 09:46:00 +0100 | [diff] [blame] | 497 | return err; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 498 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 499 | err = tiadc_iio_buffered_hardware_setup(indio_dev, |
| 500 | &tiadc_worker_h, |
| 501 | &tiadc_irq_h, |
| 502 | adc_dev->mfd_tscadc->irq, |
| 503 | IRQF_SHARED, |
| 504 | &tiadc_buffer_setup_ops); |
| 505 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 506 | if (err) |
| 507 | goto err_free_channels; |
| 508 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 509 | err = iio_device_register(indio_dev); |
| 510 | if (err) |
| 511 | goto err_buffer_unregister; |
| 512 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 513 | platform_set_drvdata(pdev, indio_dev); |
| 514 | |
| 515 | return 0; |
| 516 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 517 | err_buffer_unregister: |
| 518 | tiadc_iio_buffered_hardware_remove(indio_dev); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 519 | err_free_channels: |
| 520 | tiadc_channels_remove(indio_dev); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 521 | return err; |
| 522 | } |
| 523 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 524 | static int tiadc_remove(struct platform_device *pdev) |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 525 | { |
| 526 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 527 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 528 | u32 step_en; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 529 | |
| 530 | iio_device_unregister(indio_dev); |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 531 | tiadc_iio_buffered_hardware_remove(indio_dev); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 532 | tiadc_channels_remove(indio_dev); |
| 533 | |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 534 | step_en = get_adc_step_mask(adc_dev); |
| 535 | am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en); |
| 536 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | |
Andrew F. Davis | 27aa832 | 2016-05-31 12:00:07 -0500 | [diff] [blame] | 540 | static int __maybe_unused tiadc_suspend(struct device *dev) |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 541 | { |
| 542 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 543 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
Sebastian Andrzej Siewior | a9bce1b | 2013-06-05 16:13:47 +0200 | [diff] [blame] | 544 | struct ti_tscadc_dev *tscadc_dev; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 545 | unsigned int idle; |
| 546 | |
Sebastian Andrzej Siewior | a9bce1b | 2013-06-05 16:13:47 +0200 | [diff] [blame] | 547 | tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev)); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 548 | if (!device_may_wakeup(tscadc_dev->dev)) { |
| 549 | idle = tiadc_readl(adc_dev, REG_CTRL); |
| 550 | idle &= ~(CNTRLREG_TSCSSENB); |
| 551 | tiadc_writel(adc_dev, REG_CTRL, (idle | |
| 552 | CNTRLREG_POWERDOWN)); |
| 553 | } |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
Andrew F. Davis | 27aa832 | 2016-05-31 12:00:07 -0500 | [diff] [blame] | 558 | static int __maybe_unused tiadc_resume(struct device *dev) |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 559 | { |
| 560 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 561 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 562 | unsigned int restore; |
| 563 | |
| 564 | /* Make sure ADC is powered up */ |
| 565 | restore = tiadc_readl(adc_dev, REG_CTRL); |
| 566 | restore &= ~(CNTRLREG_POWERDOWN); |
| 567 | tiadc_writel(adc_dev, REG_CTRL, restore); |
| 568 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 569 | tiadc_step_config(indio_dev); |
Sebastian Andrzej Siewior | 7ca6740 | 2013-12-19 16:28:31 +0100 | [diff] [blame] | 570 | am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, |
| 571 | adc_dev->buffer_en_ch_steps); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | |
Andrew F. Davis | 27aa832 | 2016-05-31 12:00:07 -0500 | [diff] [blame] | 575 | static SIMPLE_DEV_PM_OPS(tiadc_pm_ops, tiadc_suspend, tiadc_resume); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 576 | |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 577 | static const struct of_device_id ti_adc_dt_ids[] = { |
| 578 | { .compatible = "ti,am3359-adc", }, |
| 579 | { } |
| 580 | }; |
| 581 | MODULE_DEVICE_TABLE(of, ti_adc_dt_ids); |
| 582 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 583 | static struct platform_driver tiadc_driver = { |
| 584 | .driver = { |
Sebastian Andrzej Siewior | 9f99928 | 2013-05-27 17:12:52 +0200 | [diff] [blame] | 585 | .name = "TI-am335x-adc", |
Andrew F. Davis | 27aa832 | 2016-05-31 12:00:07 -0500 | [diff] [blame] | 586 | .pm = &tiadc_pm_ops, |
Sachin Kamat | de06b34 | 2013-10-21 10:27:00 +0100 | [diff] [blame] | 587 | .of_match_table = ti_adc_dt_ids, |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 588 | }, |
| 589 | .probe = tiadc_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 590 | .remove = tiadc_remove, |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 591 | }; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 592 | module_platform_driver(tiadc_driver); |
| 593 | |
| 594 | MODULE_DESCRIPTION("TI ADC controller driver"); |
| 595 | MODULE_AUTHOR("Rachna Patil <rachna@ti.com>"); |
| 596 | MODULE_LICENSE("GPL"); |