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 | |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/io.h> |
| 24 | #include <linux/iio/iio.h> |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 25 | #include <linux/of.h> |
| 26 | #include <linux/of_device.h> |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 27 | #include <linux/iio/machine.h> |
| 28 | #include <linux/iio/driver.h> |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 29 | |
| 30 | #include <linux/mfd/ti_am335x_tscadc.h> |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 31 | #include <linux/iio/buffer.h> |
| 32 | #include <linux/iio/kfifo_buf.h> |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 33 | |
| 34 | struct tiadc_device { |
| 35 | struct ti_tscadc_dev *mfd_tscadc; |
| 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]; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg) |
| 44 | { |
| 45 | return readl(adc->mfd_tscadc->tscadc_base + reg); |
| 46 | } |
| 47 | |
| 48 | static void tiadc_writel(struct tiadc_device *adc, unsigned int reg, |
| 49 | unsigned int val) |
| 50 | { |
| 51 | writel(val, adc->mfd_tscadc->tscadc_base + reg); |
| 52 | } |
| 53 | |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 54 | static u32 get_adc_step_mask(struct tiadc_device *adc_dev) |
| 55 | { |
| 56 | u32 step_en; |
| 57 | |
| 58 | step_en = ((1 << adc_dev->channels) - 1); |
| 59 | step_en <<= TOTAL_STEPS - adc_dev->channels + 1; |
| 60 | return step_en; |
| 61 | } |
| 62 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 63 | 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] | 64 | { |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 65 | return 1 << adc_dev->channel_step[chan]; |
| 66 | } |
| 67 | |
| 68 | static void tiadc_step_config(struct iio_dev *indio_dev) |
| 69 | { |
| 70 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 71 | unsigned int stepconfig; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 72 | int i, steps; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * There are 16 configurable steps and 8 analog input |
| 76 | * lines available which are shared between Touchscreen and ADC. |
| 77 | * |
| 78 | * Steps backwards i.e. from 16 towards 0 are used by ADC |
| 79 | * depending on number of input lines needed. |
| 80 | * Channel would represent which analog input |
| 81 | * needs to be given to ADC to digitalize data. |
| 82 | */ |
| 83 | |
| 84 | steps = TOTAL_STEPS - adc_dev->channels; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 85 | if (iio_buffer_enabled(indio_dev)) |
| 86 | stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1 |
| 87 | | STEPCONFIG_MODE_SWCNT; |
| 88 | else |
| 89 | stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 90 | |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 91 | for (i = 0; i < adc_dev->channels; i++) { |
| 92 | int chan; |
| 93 | |
| 94 | chan = adc_dev->channel_line[i]; |
| 95 | tiadc_writel(adc_dev, REG_STEPCONFIG(steps), |
| 96 | stepconfig | STEPCONFIG_INP(chan)); |
| 97 | tiadc_writel(adc_dev, REG_STEPDELAY(steps), |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 98 | STEPCONFIG_OPENDLY); |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 99 | adc_dev->channel_step[i] = steps; |
| 100 | steps++; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 101 | } |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 102 | } |
| 103 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 104 | static irqreturn_t tiadc_irq_h(int irq, void *private) |
| 105 | { |
| 106 | struct iio_dev *indio_dev = private; |
| 107 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 108 | unsigned int status, config; |
| 109 | status = tiadc_readl(adc_dev, REG_IRQSTATUS); |
| 110 | |
| 111 | /* |
| 112 | * ADC and touchscreen share the IRQ line. |
| 113 | * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only |
| 114 | */ |
| 115 | if (status & IRQENB_FIFO1OVRRUN) { |
| 116 | /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */ |
| 117 | config = tiadc_readl(adc_dev, REG_CTRL); |
| 118 | config &= ~(CNTRLREG_TSCSSENB); |
| 119 | tiadc_writel(adc_dev, REG_CTRL, config); |
| 120 | tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN |
| 121 | | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES); |
| 122 | tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB)); |
| 123 | return IRQ_HANDLED; |
| 124 | } else if (status & IRQENB_FIFO1THRES) { |
| 125 | /* Disable irq and wake worker thread */ |
| 126 | tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES); |
| 127 | return IRQ_WAKE_THREAD; |
| 128 | } |
| 129 | |
| 130 | return IRQ_NONE; |
| 131 | } |
| 132 | |
| 133 | static irqreturn_t tiadc_worker_h(int irq, void *private) |
| 134 | { |
| 135 | struct iio_dev *indio_dev = private; |
| 136 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 137 | int i, k, fifo1count, read; |
| 138 | u16 *data = adc_dev->data; |
| 139 | |
| 140 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); |
| 141 | for (k = 0; k < fifo1count; k = k + i) { |
| 142 | for (i = 0; i < (indio_dev->scan_bytes)/2; i++) { |
| 143 | read = tiadc_readl(adc_dev, REG_FIFO1); |
| 144 | data[i] = read & FIFOREAD_DATA_MASK; |
| 145 | } |
| 146 | iio_push_to_buffers(indio_dev, (u8 *) data); |
| 147 | } |
| 148 | |
| 149 | tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES); |
| 150 | tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES); |
| 151 | |
| 152 | return IRQ_HANDLED; |
| 153 | } |
| 154 | |
| 155 | static int tiadc_buffer_preenable(struct iio_dev *indio_dev) |
| 156 | { |
| 157 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 158 | int i, fifo1count, read; |
| 159 | |
| 160 | tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES | |
| 161 | IRQENB_FIFO1OVRRUN | |
| 162 | IRQENB_FIFO1UNDRFLW)); |
| 163 | |
| 164 | /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */ |
| 165 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); |
| 166 | for (i = 0; i < fifo1count; i++) |
| 167 | read = tiadc_readl(adc_dev, REG_FIFO1); |
| 168 | |
Lars-Peter Clausen | 24adaf7 | 2013-10-14 17:49:00 +0100 | [diff] [blame] | 169 | return 0; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static int tiadc_buffer_postenable(struct iio_dev *indio_dev) |
| 173 | { |
| 174 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 175 | struct iio_buffer *buffer = indio_dev->buffer; |
| 176 | unsigned int enb = 0; |
| 177 | u8 bit; |
| 178 | |
| 179 | tiadc_step_config(indio_dev); |
| 180 | for_each_set_bit(bit, buffer->scan_mask, adc_dev->channels) |
| 181 | enb |= (get_adc_step_bit(adc_dev, bit) << 1); |
| 182 | adc_dev->buffer_en_ch_steps = enb; |
| 183 | |
Sebastian Andrzej Siewior | 7e170c6 | 2013-12-19 16:28:29 +0100 | [diff] [blame] | 184 | am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb); |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 185 | |
| 186 | tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES |
| 187 | | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW); |
| 188 | tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES |
| 189 | | IRQENB_FIFO1OVRRUN); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int tiadc_buffer_predisable(struct iio_dev *indio_dev) |
| 195 | { |
| 196 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 197 | int fifo1count, i, read; |
| 198 | |
| 199 | tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES | |
| 200 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW)); |
| 201 | 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^] | 202 | adc_dev->buffer_en_ch_steps = 0; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 203 | |
| 204 | /* Flush FIFO of leftover data in the time it takes to disable adc */ |
| 205 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); |
| 206 | for (i = 0; i < fifo1count; i++) |
| 207 | read = tiadc_readl(adc_dev, REG_FIFO1); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int tiadc_buffer_postdisable(struct iio_dev *indio_dev) |
| 213 | { |
| 214 | tiadc_step_config(indio_dev); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = { |
| 220 | .preenable = &tiadc_buffer_preenable, |
| 221 | .postenable = &tiadc_buffer_postenable, |
| 222 | .predisable = &tiadc_buffer_predisable, |
| 223 | .postdisable = &tiadc_buffer_postdisable, |
| 224 | }; |
| 225 | |
Zubair Lutfullah | 98c08cf | 2013-09-22 09:20:00 +0100 | [diff] [blame] | 226 | static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev, |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 227 | irqreturn_t (*pollfunc_bh)(int irq, void *p), |
| 228 | irqreturn_t (*pollfunc_th)(int irq, void *p), |
| 229 | int irq, |
| 230 | unsigned long flags, |
| 231 | const struct iio_buffer_setup_ops *setup_ops) |
| 232 | { |
| 233 | int ret; |
| 234 | |
| 235 | indio_dev->buffer = iio_kfifo_allocate(indio_dev); |
| 236 | if (!indio_dev->buffer) |
| 237 | return -ENOMEM; |
| 238 | |
| 239 | ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh, |
| 240 | flags, indio_dev->name, indio_dev); |
| 241 | if (ret) |
| 242 | goto error_kfifo_free; |
| 243 | |
| 244 | indio_dev->setup_ops = setup_ops; |
| 245 | indio_dev->modes |= INDIO_BUFFER_HARDWARE; |
| 246 | |
| 247 | ret = iio_buffer_register(indio_dev, |
| 248 | indio_dev->channels, |
| 249 | indio_dev->num_channels); |
| 250 | if (ret) |
| 251 | goto error_free_irq; |
| 252 | |
| 253 | return 0; |
| 254 | |
| 255 | error_free_irq: |
| 256 | free_irq(irq, indio_dev); |
| 257 | error_kfifo_free: |
| 258 | iio_kfifo_free(indio_dev->buffer); |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev) |
| 263 | { |
| 264 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 265 | |
| 266 | free_irq(adc_dev->mfd_tscadc->irq, indio_dev); |
| 267 | iio_kfifo_free(indio_dev->buffer); |
| 268 | iio_buffer_unregister(indio_dev); |
| 269 | } |
| 270 | |
| 271 | |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 272 | static const char * const chan_name_ain[] = { |
| 273 | "AIN0", |
| 274 | "AIN1", |
| 275 | "AIN2", |
| 276 | "AIN3", |
| 277 | "AIN4", |
| 278 | "AIN5", |
| 279 | "AIN6", |
| 280 | "AIN7", |
| 281 | }; |
| 282 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 283 | static int tiadc_channel_init(struct iio_dev *indio_dev, int channels) |
| 284 | { |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 285 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 286 | struct iio_chan_spec *chan_array; |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 287 | struct iio_chan_spec *chan; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 288 | int i; |
| 289 | |
| 290 | indio_dev->num_channels = channels; |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 291 | chan_array = kcalloc(channels, |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 292 | sizeof(struct iio_chan_spec), GFP_KERNEL); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 293 | if (chan_array == NULL) |
| 294 | return -ENOMEM; |
| 295 | |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 296 | chan = chan_array; |
| 297 | for (i = 0; i < channels; i++, chan++) { |
| 298 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 299 | chan->type = IIO_VOLTAGE; |
| 300 | chan->indexed = 1; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 301 | chan->channel = adc_dev->channel_line[i]; |
Jonathan Cameron | 6c57252 | 2013-02-27 19:07:18 +0000 | [diff] [blame] | 302 | chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 303 | chan->datasheet_name = chan_name_ain[chan->channel]; |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 304 | chan->scan_index = i; |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 305 | chan->scan_type.sign = 'u'; |
| 306 | chan->scan_type.realbits = 12; |
Zubair Lutfullah | 0f6fc7d | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 307 | chan->scan_type.storagebits = 16; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | indio_dev->channels = chan_array; |
| 311 | |
Pantelis Antoniou | c80df48 | 2012-10-13 16:37:24 +0300 | [diff] [blame] | 312 | return 0; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static void tiadc_channels_remove(struct iio_dev *indio_dev) |
| 316 | { |
| 317 | kfree(indio_dev->channels); |
| 318 | } |
| 319 | |
| 320 | static int tiadc_read_raw(struct iio_dev *indio_dev, |
| 321 | struct iio_chan_spec const *chan, |
| 322 | int *val, int *val2, long mask) |
| 323 | { |
| 324 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 325 | int i, map_val; |
| 326 | unsigned int fifo1count, read, stepid; |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame] | 327 | bool found = false; |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 328 | u32 step_en; |
| 329 | unsigned long timeout = jiffies + usecs_to_jiffies |
| 330 | (IDLE_TIMEOUT * adc_dev->channels); |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 331 | |
| 332 | if (iio_buffer_enabled(indio_dev)) |
| 333 | return -EBUSY; |
| 334 | |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 335 | step_en = get_adc_step_mask(adc_dev); |
Sebastian Andrzej Siewior | 7e170c6 | 2013-12-19 16:28:29 +0100 | [diff] [blame] | 336 | am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en); |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 337 | |
| 338 | /* Wait for ADC sequencer to complete sampling */ |
| 339 | while (tiadc_readl(adc_dev, REG_ADCFSM) & SEQ_STATUS) { |
| 340 | if (time_after(jiffies, timeout)) |
| 341 | return -EAGAIN; |
Sebastian Andrzej Siewior | fb7f8ce | 2013-12-19 16:28:27 +0100 | [diff] [blame] | 342 | } |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 343 | map_val = chan->channel + TOTAL_CHANNELS; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 344 | |
| 345 | /* |
| 346 | * When the sub-system is first enabled, |
| 347 | * the sequencer will always start with the |
| 348 | * lowest step (1) and continue until step (16). |
| 349 | * For ex: If we have enabled 4 ADC channels and |
| 350 | * currently use only 1 out of them, the |
| 351 | * sequencer still configures all the 4 steps, |
| 352 | * leading to 3 unwanted data. |
| 353 | * Hence we need to flush out this data. |
| 354 | */ |
| 355 | |
| 356 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); |
| 357 | for (i = 0; i < fifo1count; i++) { |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 358 | read = tiadc_readl(adc_dev, REG_FIFO1); |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 359 | stepid = read & FIFOREAD_CHNLID_MASK; |
| 360 | stepid = stepid >> 0x10; |
| 361 | |
| 362 | if (stepid == map_val) { |
| 363 | read = read & FIFOREAD_DATA_MASK; |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame] | 364 | found = true; |
Zubair Lutfullah | 0f6fc7d | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 365 | *val = (u16) read; |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame] | 366 | } |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 367 | } |
Patil, Rachna | b1451e5 | 2013-07-20 17:27:00 +0100 | [diff] [blame] | 368 | |
Sebastian Andrzej Siewior | 1460c15 | 2013-05-29 18:49:55 +0200 | [diff] [blame] | 369 | if (found == false) |
| 370 | return -EBUSY; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 371 | return IIO_VAL_INT; |
| 372 | } |
| 373 | |
| 374 | static const struct iio_info tiadc_info = { |
| 375 | .read_raw = &tiadc_read_raw, |
Wei Yongjun | bc93aa7 | 2013-07-06 05:20:00 +0100 | [diff] [blame] | 376 | .driver_module = THIS_MODULE, |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 377 | }; |
| 378 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 379 | static int tiadc_probe(struct platform_device *pdev) |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 380 | { |
| 381 | struct iio_dev *indio_dev; |
| 382 | struct tiadc_device *adc_dev; |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 383 | struct device_node *node = pdev->dev.of_node; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 384 | struct property *prop; |
| 385 | const __be32 *cur; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 386 | int err; |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 387 | u32 val; |
| 388 | int channels = 0; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 389 | |
Sebastian Andrzej Siewior | 0ead4fb | 2013-05-21 17:49:22 +0200 | [diff] [blame] | 390 | if (!node) { |
| 391 | dev_err(&pdev->dev, "Could not find valid DT data.\n"); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 392 | return -EINVAL; |
| 393 | } |
| 394 | |
Sachin Kamat | a064813 | 2013-07-23 09:46:00 +0100 | [diff] [blame] | 395 | indio_dev = devm_iio_device_alloc(&pdev->dev, |
| 396 | sizeof(struct tiadc_device)); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 397 | if (indio_dev == NULL) { |
| 398 | dev_err(&pdev->dev, "failed to allocate iio device\n"); |
Sachin Kamat | a064813 | 2013-07-23 09:46:00 +0100 | [diff] [blame] | 399 | return -ENOMEM; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 400 | } |
| 401 | adc_dev = iio_priv(indio_dev); |
| 402 | |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 403 | adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev); |
| 404 | |
Sebastian Andrzej Siewior | 18926ed | 2013-05-29 17:39:02 +0200 | [diff] [blame] | 405 | of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) { |
| 406 | adc_dev->channel_line[channels] = val; |
| 407 | channels++; |
| 408 | } |
| 409 | adc_dev->channels = channels; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 410 | |
| 411 | indio_dev->dev.parent = &pdev->dev; |
| 412 | indio_dev->name = dev_name(&pdev->dev); |
| 413 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 414 | indio_dev->info = &tiadc_info; |
| 415 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 416 | tiadc_step_config(indio_dev); |
| 417 | tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 418 | |
| 419 | err = tiadc_channel_init(indio_dev, adc_dev->channels); |
| 420 | if (err < 0) |
Sachin Kamat | a064813 | 2013-07-23 09:46:00 +0100 | [diff] [blame] | 421 | return err; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 422 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 423 | err = tiadc_iio_buffered_hardware_setup(indio_dev, |
| 424 | &tiadc_worker_h, |
| 425 | &tiadc_irq_h, |
| 426 | adc_dev->mfd_tscadc->irq, |
| 427 | IRQF_SHARED, |
| 428 | &tiadc_buffer_setup_ops); |
| 429 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 430 | if (err) |
| 431 | goto err_free_channels; |
| 432 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 433 | err = iio_device_register(indio_dev); |
| 434 | if (err) |
| 435 | goto err_buffer_unregister; |
| 436 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 437 | platform_set_drvdata(pdev, indio_dev); |
| 438 | |
| 439 | return 0; |
| 440 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 441 | err_buffer_unregister: |
| 442 | tiadc_iio_buffered_hardware_remove(indio_dev); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 443 | err_free_channels: |
| 444 | tiadc_channels_remove(indio_dev); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 445 | return err; |
| 446 | } |
| 447 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 448 | static int tiadc_remove(struct platform_device *pdev) |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 449 | { |
| 450 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 451 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 452 | u32 step_en; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 453 | |
| 454 | iio_device_unregister(indio_dev); |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 455 | tiadc_iio_buffered_hardware_remove(indio_dev); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 456 | tiadc_channels_remove(indio_dev); |
| 457 | |
Patil, Rachna | abeccee | 2013-01-24 03:45:05 +0000 | [diff] [blame] | 458 | step_en = get_adc_step_mask(adc_dev); |
| 459 | am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en); |
| 460 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | #ifdef CONFIG_PM |
| 465 | static int tiadc_suspend(struct device *dev) |
| 466 | { |
| 467 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 468 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
Sebastian Andrzej Siewior | a9bce1b | 2013-06-05 16:13:47 +0200 | [diff] [blame] | 469 | struct ti_tscadc_dev *tscadc_dev; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 470 | unsigned int idle; |
| 471 | |
Sebastian Andrzej Siewior | a9bce1b | 2013-06-05 16:13:47 +0200 | [diff] [blame] | 472 | tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev)); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 473 | if (!device_may_wakeup(tscadc_dev->dev)) { |
| 474 | idle = tiadc_readl(adc_dev, REG_CTRL); |
| 475 | idle &= ~(CNTRLREG_TSCSSENB); |
| 476 | tiadc_writel(adc_dev, REG_CTRL, (idle | |
| 477 | CNTRLREG_POWERDOWN)); |
| 478 | } |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | static int tiadc_resume(struct device *dev) |
| 484 | { |
| 485 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 486 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
| 487 | unsigned int restore; |
| 488 | |
| 489 | /* Make sure ADC is powered up */ |
| 490 | restore = tiadc_readl(adc_dev, REG_CTRL); |
| 491 | restore &= ~(CNTRLREG_POWERDOWN); |
| 492 | tiadc_writel(adc_dev, REG_CTRL, restore); |
| 493 | |
Zubair Lutfullah | ca9a563 | 2013-09-19 07:24:00 +0100 | [diff] [blame] | 494 | tiadc_step_config(indio_dev); |
Sebastian Andrzej Siewior | 3954b7b | 2013-12-19 16:28:30 +0100 | [diff] [blame^] | 495 | am335x_tsc_se_set(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps); |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static const struct dev_pm_ops tiadc_pm_ops = { |
| 501 | .suspend = tiadc_suspend, |
| 502 | .resume = tiadc_resume, |
| 503 | }; |
| 504 | #define TIADC_PM_OPS (&tiadc_pm_ops) |
| 505 | #else |
| 506 | #define TIADC_PM_OPS NULL |
| 507 | #endif |
| 508 | |
Patil, Rachna | 6f39ac4 | 2013-01-24 03:45:11 +0000 | [diff] [blame] | 509 | static const struct of_device_id ti_adc_dt_ids[] = { |
| 510 | { .compatible = "ti,am3359-adc", }, |
| 511 | { } |
| 512 | }; |
| 513 | MODULE_DEVICE_TABLE(of, ti_adc_dt_ids); |
| 514 | |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 515 | static struct platform_driver tiadc_driver = { |
| 516 | .driver = { |
Sebastian Andrzej Siewior | 9f99928 | 2013-05-27 17:12:52 +0200 | [diff] [blame] | 517 | .name = "TI-am335x-adc", |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 518 | .owner = THIS_MODULE, |
| 519 | .pm = TIADC_PM_OPS, |
Sachin Kamat | de06b34 | 2013-10-21 10:27:00 +0100 | [diff] [blame] | 520 | .of_match_table = ti_adc_dt_ids, |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 521 | }, |
| 522 | .probe = tiadc_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 523 | .remove = tiadc_remove, |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 524 | }; |
Patil, Rachna | 5e53a69 | 2012-10-16 12:55:45 +0530 | [diff] [blame] | 525 | module_platform_driver(tiadc_driver); |
| 526 | |
| 527 | MODULE_DESCRIPTION("TI ADC controller driver"); |
| 528 | MODULE_AUTHOR("Rachna Patil <rachna@ti.com>"); |
| 529 | MODULE_LICENSE("GPL"); |