]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/iio/adc/mxs-lradc.c
Merge branch 'sched-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.git] / drivers / staging / iio / adc / mxs-lradc.c
1 /*
2  * Freescale MXS LRADC driver
3  *
4  * Copyright (c) 2012 DENX Software Engineering, GmbH.
5  * Marek Vasut <marex@denx.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/bitops.h>
19 #include <linux/clk.h>
20 #include <linux/completion.h>
21 #include <linux/device.h>
22 #include <linux/err.h>
23 #include <linux/input.h>
24 #include <linux/interrupt.h>
25 #include <linux/io.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/mutex.h>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33 #include <linux/stmp_device.h>
34 #include <linux/sysfs.h>
35
36 #include <linux/iio/buffer.h>
37 #include <linux/iio/iio.h>
38 #include <linux/iio/trigger.h>
39 #include <linux/iio/trigger_consumer.h>
40 #include <linux/iio/triggered_buffer.h>
41 #include <linux/iio/sysfs.h>
42
43 #define DRIVER_NAME             "mxs-lradc"
44
45 #define LRADC_MAX_DELAY_CHANS   4
46 #define LRADC_MAX_MAPPED_CHANS  8
47 #define LRADC_MAX_TOTAL_CHANS   16
48
49 #define LRADC_DELAY_TIMER_HZ    2000
50
51 /*
52  * Make this runtime configurable if necessary. Currently, if the buffered mode
53  * is enabled, the LRADC takes LRADC_DELAY_TIMER_LOOP samples of data before
54  * triggering IRQ. The sampling happens every (LRADC_DELAY_TIMER_PER / 2000)
55  * seconds. The result is that the samples arrive every 500mS.
56  */
57 #define LRADC_DELAY_TIMER_PER   200
58 #define LRADC_DELAY_TIMER_LOOP  5
59
60 /*
61  * Once the pen touches the touchscreen, the touchscreen switches from
62  * IRQ-driven mode to polling mode to prevent interrupt storm. The polling
63  * is realized by worker thread, which is called every 20 or so milliseconds.
64  * This gives the touchscreen enough fluency and does not strain the system
65  * too much.
66  */
67 #define LRADC_TS_SAMPLE_DELAY_MS        5
68
69 /*
70  * The LRADC reads the following amount of samples from each touchscreen
71  * channel and the driver then computes average of these.
72  */
73 #define LRADC_TS_SAMPLE_AMOUNT          4
74
75 enum mxs_lradc_id {
76         IMX23_LRADC,
77         IMX28_LRADC,
78 };
79
80 static const char * const mx23_lradc_irq_names[] = {
81         "mxs-lradc-touchscreen",
82         "mxs-lradc-channel0",
83         "mxs-lradc-channel1",
84         "mxs-lradc-channel2",
85         "mxs-lradc-channel3",
86         "mxs-lradc-channel4",
87         "mxs-lradc-channel5",
88         "mxs-lradc-channel6",
89         "mxs-lradc-channel7",
90 };
91
92 static const char * const mx28_lradc_irq_names[] = {
93         "mxs-lradc-touchscreen",
94         "mxs-lradc-thresh0",
95         "mxs-lradc-thresh1",
96         "mxs-lradc-channel0",
97         "mxs-lradc-channel1",
98         "mxs-lradc-channel2",
99         "mxs-lradc-channel3",
100         "mxs-lradc-channel4",
101         "mxs-lradc-channel5",
102         "mxs-lradc-channel6",
103         "mxs-lradc-channel7",
104         "mxs-lradc-button0",
105         "mxs-lradc-button1",
106 };
107
108 struct mxs_lradc_of_config {
109         const int               irq_count;
110         const char * const      *irq_name;
111         const uint32_t          *vref_mv;
112 };
113
114 #define VREF_MV_BASE 1850
115
116 static const uint32_t mx23_vref_mv[LRADC_MAX_TOTAL_CHANS] = {
117         VREF_MV_BASE,           /* CH0 */
118         VREF_MV_BASE,           /* CH1 */
119         VREF_MV_BASE,           /* CH2 */
120         VREF_MV_BASE,           /* CH3 */
121         VREF_MV_BASE,           /* CH4 */
122         VREF_MV_BASE,           /* CH5 */
123         VREF_MV_BASE * 2,       /* CH6 VDDIO */
124         VREF_MV_BASE * 4,       /* CH7 VBATT */
125         VREF_MV_BASE,           /* CH8 Temp sense 0 */
126         VREF_MV_BASE,           /* CH9 Temp sense 1 */
127         VREF_MV_BASE,           /* CH10 */
128         VREF_MV_BASE,           /* CH11 */
129         VREF_MV_BASE,           /* CH12 USB_DP */
130         VREF_MV_BASE,           /* CH13 USB_DN */
131         VREF_MV_BASE,           /* CH14 VBG */
132         VREF_MV_BASE * 4,       /* CH15 VDD5V */
133 };
134
135 static const uint32_t mx28_vref_mv[LRADC_MAX_TOTAL_CHANS] = {
136         VREF_MV_BASE,           /* CH0 */
137         VREF_MV_BASE,           /* CH1 */
138         VREF_MV_BASE,           /* CH2 */
139         VREF_MV_BASE,           /* CH3 */
140         VREF_MV_BASE,           /* CH4 */
141         VREF_MV_BASE,           /* CH5 */
142         VREF_MV_BASE,           /* CH6 */
143         VREF_MV_BASE * 4,       /* CH7 VBATT */
144         VREF_MV_BASE,           /* CH8 Temp sense 0 */
145         VREF_MV_BASE,           /* CH9 Temp sense 1 */
146         VREF_MV_BASE * 2,       /* CH10 VDDIO */
147         VREF_MV_BASE,           /* CH11 VTH */
148         VREF_MV_BASE * 2,       /* CH12 VDDA */
149         VREF_MV_BASE,           /* CH13 VDDD */
150         VREF_MV_BASE,           /* CH14 VBG */
151         VREF_MV_BASE * 4,       /* CH15 VDD5V */
152 };
153
154 static const struct mxs_lradc_of_config mxs_lradc_of_config[] = {
155         [IMX23_LRADC] = {
156                 .irq_count      = ARRAY_SIZE(mx23_lradc_irq_names),
157                 .irq_name       = mx23_lradc_irq_names,
158                 .vref_mv        = mx23_vref_mv,
159         },
160         [IMX28_LRADC] = {
161                 .irq_count      = ARRAY_SIZE(mx28_lradc_irq_names),
162                 .irq_name       = mx28_lradc_irq_names,
163                 .vref_mv        = mx28_vref_mv,
164         },
165 };
166
167 enum mxs_lradc_ts {
168         MXS_LRADC_TOUCHSCREEN_NONE = 0,
169         MXS_LRADC_TOUCHSCREEN_4WIRE,
170         MXS_LRADC_TOUCHSCREEN_5WIRE,
171 };
172
173 /*
174  * Touchscreen handling
175  */
176 enum lradc_ts_plate {
177         LRADC_TOUCH = 0,
178         LRADC_SAMPLE_X,
179         LRADC_SAMPLE_Y,
180         LRADC_SAMPLE_PRESSURE,
181         LRADC_SAMPLE_VALID,
182 };
183
184 enum mxs_lradc_divbytwo {
185         MXS_LRADC_DIV_DISABLED = 0,
186         MXS_LRADC_DIV_ENABLED,
187 };
188
189 struct mxs_lradc_scale {
190         unsigned int            integer;
191         unsigned int            nano;
192 };
193
194 struct mxs_lradc {
195         struct device           *dev;
196         void __iomem            *base;
197         int                     irq[13];
198
199         struct clk              *clk;
200
201         uint32_t                *buffer;
202         struct iio_trigger      *trig;
203
204         struct mutex            lock;
205
206         struct completion       completion;
207
208         const uint32_t          *vref_mv;
209         struct mxs_lradc_scale  scale_avail[LRADC_MAX_TOTAL_CHANS][2];
210         unsigned long           is_divided;
211
212         /*
213          * When the touchscreen is enabled, we give it two private virtual
214          * channels: #6 and #7. This means that only 6 virtual channels (instead
215          * of 8) will be available for buffered capture.
216          */
217 #define TOUCHSCREEN_VCHANNEL1           7
218 #define TOUCHSCREEN_VCHANNEL2           6
219 #define BUFFER_VCHANS_LIMITED           0x3f
220 #define BUFFER_VCHANS_ALL               0xff
221         u8                      buffer_vchans;
222
223         /*
224          * Furthermore, certain LRADC channels are shared between touchscreen
225          * and/or touch-buttons and generic LRADC block. Therefore when using
226          * either of these, these channels are not available for the regular
227          * sampling. The shared channels are as follows:
228          *
229          * CH0 -- Touch button #0
230          * CH1 -- Touch button #1
231          * CH2 -- Touch screen XPUL
232          * CH3 -- Touch screen YPLL
233          * CH4 -- Touch screen XNUL
234          * CH5 -- Touch screen YNLR
235          * CH6 -- Touch screen WIPER (5-wire only)
236          *
237          * The bit fields below represents which parts of the LRADC block are
238          * switched into special mode of operation. These channels can not
239          * be sampled as regular LRADC channels. The driver will refuse any
240          * attempt to sample these channels.
241          */
242 #define CHAN_MASK_TOUCHBUTTON           (BIT(1) | BIT(0))
243 #define CHAN_MASK_TOUCHSCREEN_4WIRE     (0xf << 2)
244 #define CHAN_MASK_TOUCHSCREEN_5WIRE     (0x1f << 2)
245         enum mxs_lradc_ts       use_touchscreen;
246         bool                    use_touchbutton;
247
248         struct input_dev        *ts_input;
249
250         enum mxs_lradc_id       soc;
251         enum lradc_ts_plate     cur_plate; /* state machine */
252         bool                    ts_valid;
253         unsigned                ts_x_pos;
254         unsigned                ts_y_pos;
255         unsigned                ts_pressure;
256
257         /* handle touchscreen's physical behaviour */
258         /* samples per coordinate */
259         unsigned                over_sample_cnt;
260         /* time clocks between samples */
261         unsigned                over_sample_delay;
262         /* time in clocks to wait after the plates where switched */
263         unsigned                settling_delay;
264 };
265
266 #define LRADC_CTRL0                             0x00
267 # define LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE   BIT(23)
268 # define LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE     BIT(22)
269 # define LRADC_CTRL0_MX28_YNNSW /* YM */        BIT(21)
270 # define LRADC_CTRL0_MX28_YPNSW /* YP */        BIT(20)
271 # define LRADC_CTRL0_MX28_YPPSW /* YP */        BIT(19)
272 # define LRADC_CTRL0_MX28_XNNSW /* XM */        BIT(18)
273 # define LRADC_CTRL0_MX28_XNPSW /* XM */        BIT(17)
274 # define LRADC_CTRL0_MX28_XPPSW /* XP */        BIT(16)
275
276 # define LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE   BIT(20)
277 # define LRADC_CTRL0_MX23_YM                    BIT(19)
278 # define LRADC_CTRL0_MX23_XM                    BIT(18)
279 # define LRADC_CTRL0_MX23_YP                    BIT(17)
280 # define LRADC_CTRL0_MX23_XP                    BIT(16)
281
282 # define LRADC_CTRL0_MX28_PLATE_MASK \
283                 (LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE | \
284                 LRADC_CTRL0_MX28_YNNSW | LRADC_CTRL0_MX28_YPNSW | \
285                 LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_XNNSW | \
286                 LRADC_CTRL0_MX28_XNPSW | LRADC_CTRL0_MX28_XPPSW)
287
288 # define LRADC_CTRL0_MX23_PLATE_MASK \
289                 (LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE | \
290                 LRADC_CTRL0_MX23_YM | LRADC_CTRL0_MX23_XM | \
291                 LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_XP)
292
293 #define LRADC_CTRL1                             0x10
294 #define LRADC_CTRL1_TOUCH_DETECT_IRQ_EN         BIT(24)
295 #define LRADC_CTRL1_LRADC_IRQ_EN(n)             (1 << ((n) + 16))
296 #define LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK      (0x1fff << 16)
297 #define LRADC_CTRL1_MX23_LRADC_IRQ_EN_MASK      (0x01ff << 16)
298 #define LRADC_CTRL1_LRADC_IRQ_EN_OFFSET         16
299 #define LRADC_CTRL1_TOUCH_DETECT_IRQ            BIT(8)
300 #define LRADC_CTRL1_LRADC_IRQ(n)                (1 << (n))
301 #define LRADC_CTRL1_MX28_LRADC_IRQ_MASK         0x1fff
302 #define LRADC_CTRL1_MX23_LRADC_IRQ_MASK         0x01ff
303 #define LRADC_CTRL1_LRADC_IRQ_OFFSET            0
304
305 #define LRADC_CTRL2                             0x20
306 #define LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET        24
307 #define LRADC_CTRL2_TEMPSENSE_PWD               BIT(15)
308
309 #define LRADC_STATUS                            0x40
310 #define LRADC_STATUS_TOUCH_DETECT_RAW           BIT(0)
311
312 #define LRADC_CH(n)                             (0x50 + (0x10 * (n)))
313 #define LRADC_CH_ACCUMULATE                     BIT(29)
314 #define LRADC_CH_NUM_SAMPLES_MASK               (0x1f << 24)
315 #define LRADC_CH_NUM_SAMPLES_OFFSET             24
316 #define LRADC_CH_NUM_SAMPLES(x) \
317                                 ((x) << LRADC_CH_NUM_SAMPLES_OFFSET)
318 #define LRADC_CH_VALUE_MASK                     0x3ffff
319 #define LRADC_CH_VALUE_OFFSET                   0
320
321 #define LRADC_DELAY(n)                          (0xd0 + (0x10 * (n)))
322 #define LRADC_DELAY_TRIGGER_LRADCS_MASK         (0xff << 24)
323 #define LRADC_DELAY_TRIGGER_LRADCS_OFFSET       24
324 #define LRADC_DELAY_TRIGGER(x) \
325                                 (((x) << LRADC_DELAY_TRIGGER_LRADCS_OFFSET) & \
326                                 LRADC_DELAY_TRIGGER_LRADCS_MASK)
327 #define LRADC_DELAY_KICK                        (1 << 20)
328 #define LRADC_DELAY_TRIGGER_DELAYS_MASK         (0xf << 16)
329 #define LRADC_DELAY_TRIGGER_DELAYS_OFFSET       16
330 #define LRADC_DELAY_TRIGGER_DELAYS(x) \
331                                 (((x) << LRADC_DELAY_TRIGGER_DELAYS_OFFSET) & \
332                                 LRADC_DELAY_TRIGGER_DELAYS_MASK)
333 #define LRADC_DELAY_LOOP_COUNT_MASK             (0x1f << 11)
334 #define LRADC_DELAY_LOOP_COUNT_OFFSET           11
335 #define LRADC_DELAY_LOOP(x) \
336                                 (((x) << LRADC_DELAY_LOOP_COUNT_OFFSET) & \
337                                 LRADC_DELAY_LOOP_COUNT_MASK)
338 #define LRADC_DELAY_DELAY_MASK                  0x7ff
339 #define LRADC_DELAY_DELAY_OFFSET                0
340 #define LRADC_DELAY_DELAY(x) \
341                                 (((x) << LRADC_DELAY_DELAY_OFFSET) & \
342                                 LRADC_DELAY_DELAY_MASK)
343
344 #define LRADC_CTRL4                             0x140
345 #define LRADC_CTRL4_LRADCSELECT_MASK(n)         (0xf << ((n) * 4))
346 #define LRADC_CTRL4_LRADCSELECT_OFFSET(n)       ((n) * 4)
347 #define LRADC_CTRL4_LRADCSELECT(n, x) \
348                                 (((x) << LRADC_CTRL4_LRADCSELECT_OFFSET(n)) & \
349                                 LRADC_CTRL4_LRADCSELECT_MASK(n))
350
351 #define LRADC_RESOLUTION                        12
352 #define LRADC_SINGLE_SAMPLE_MASK                ((1 << LRADC_RESOLUTION) - 1)
353
354 static void mxs_lradc_reg_set(struct mxs_lradc *lradc, u32 val, u32 reg)
355 {
356         writel(val, lradc->base + reg + STMP_OFFSET_REG_SET);
357 }
358
359 static void mxs_lradc_reg_clear(struct mxs_lradc *lradc, u32 val, u32 reg)
360 {
361         writel(val, lradc->base + reg + STMP_OFFSET_REG_CLR);
362 }
363
364 static void mxs_lradc_reg_wrt(struct mxs_lradc *lradc, u32 val, u32 reg)
365 {
366         writel(val, lradc->base + reg);
367 }
368
369 static u32 mxs_lradc_plate_mask(struct mxs_lradc *lradc)
370 {
371         if (lradc->soc == IMX23_LRADC)
372                 return LRADC_CTRL0_MX23_PLATE_MASK;
373         return LRADC_CTRL0_MX28_PLATE_MASK;
374 }
375
376 static u32 mxs_lradc_irq_en_mask(struct mxs_lradc *lradc)
377 {
378         if (lradc->soc == IMX23_LRADC)
379                 return LRADC_CTRL1_MX23_LRADC_IRQ_EN_MASK;
380         return LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK;
381 }
382
383 static u32 mxs_lradc_irq_mask(struct mxs_lradc *lradc)
384 {
385         if (lradc->soc == IMX23_LRADC)
386                 return LRADC_CTRL1_MX23_LRADC_IRQ_MASK;
387         return LRADC_CTRL1_MX28_LRADC_IRQ_MASK;
388 }
389
390 static u32 mxs_lradc_touch_detect_bit(struct mxs_lradc *lradc)
391 {
392         if (lradc->soc == IMX23_LRADC)
393                 return LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE;
394         return LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE;
395 }
396
397 static u32 mxs_lradc_drive_x_plate(struct mxs_lradc *lradc)
398 {
399         if (lradc->soc == IMX23_LRADC)
400                 return LRADC_CTRL0_MX23_XP | LRADC_CTRL0_MX23_XM;
401         return LRADC_CTRL0_MX28_XPPSW | LRADC_CTRL0_MX28_XNNSW;
402 }
403
404 static u32 mxs_lradc_drive_y_plate(struct mxs_lradc *lradc)
405 {
406         if (lradc->soc == IMX23_LRADC)
407                 return LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_YM;
408         return LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_YNNSW;
409 }
410
411 static u32 mxs_lradc_drive_pressure(struct mxs_lradc *lradc)
412 {
413         if (lradc->soc == IMX23_LRADC)
414                 return LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_XM;
415         return LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_XNNSW;
416 }
417
418 static bool mxs_lradc_check_touch_event(struct mxs_lradc *lradc)
419 {
420         return !!(readl(lradc->base + LRADC_STATUS) &
421                                         LRADC_STATUS_TOUCH_DETECT_RAW);
422 }
423
424 static void mxs_lradc_map_channel(struct mxs_lradc *lradc, unsigned vch,
425                                   unsigned ch)
426 {
427         mxs_lradc_reg_clear(lradc, LRADC_CTRL4_LRADCSELECT_MASK(vch),
428                                 LRADC_CTRL4);
429         mxs_lradc_reg_set(lradc, LRADC_CTRL4_LRADCSELECT(vch, ch), LRADC_CTRL4);
430 }
431
432 static void mxs_lradc_setup_ts_channel(struct mxs_lradc *lradc, unsigned ch)
433 {
434         /*
435          * prepare for oversampling conversion
436          *
437          * from the datasheet:
438          * "The ACCUMULATE bit in the appropriate channel register
439          * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0;
440          * otherwise, the IRQs will not fire."
441          */
442         mxs_lradc_reg_wrt(lradc, LRADC_CH_ACCUMULATE |
443                         LRADC_CH_NUM_SAMPLES(lradc->over_sample_cnt - 1),
444                         LRADC_CH(ch));
445
446         /* from the datasheet:
447          * "Software must clear this register in preparation for a
448          * multi-cycle accumulation.
449          */
450         mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch));
451
452         /*
453          * prepare the delay/loop unit according to the oversampling count
454          *
455          * from the datasheet:
456          * "The DELAY fields in HW_LRADC_DELAY0, HW_LRADC_DELAY1,
457          * HW_LRADC_DELAY2, and HW_LRADC_DELAY3 must be non-zero; otherwise,
458          * the LRADC will not trigger the delay group."
459          */
460         mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << ch) |
461                 LRADC_DELAY_TRIGGER_DELAYS(0) |
462                 LRADC_DELAY_LOOP(lradc->over_sample_cnt - 1) |
463                 LRADC_DELAY_DELAY(lradc->over_sample_delay - 1),
464                         LRADC_DELAY(3));
465
466         mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(ch), LRADC_CTRL1);
467
468         /*
469          * after changing the touchscreen plates setting
470          * the signals need some initial time to settle. Start the
471          * SoC's delay unit and start the conversion later
472          * and automatically.
473          */
474         mxs_lradc_reg_wrt(lradc,
475                 LRADC_DELAY_TRIGGER(0) | /* don't trigger ADC */
476                 LRADC_DELAY_TRIGGER_DELAYS(BIT(3)) | /* trigger DELAY unit#3 */
477                 LRADC_DELAY_KICK |
478                 LRADC_DELAY_DELAY(lradc->settling_delay),
479                         LRADC_DELAY(2));
480 }
481
482 /*
483  * Pressure detection is special:
484  * We want to do both required measurements for the pressure detection in
485  * one turn. Use the hardware features to chain both conversions and let the
486  * hardware report one interrupt if both conversions are done
487  */
488 static void mxs_lradc_setup_ts_pressure(struct mxs_lradc *lradc, unsigned ch1,
489                                                         unsigned ch2)
490 {
491         u32 reg;
492
493         /*
494          * prepare for oversampling conversion
495          *
496          * from the datasheet:
497          * "The ACCUMULATE bit in the appropriate channel register
498          * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0;
499          * otherwise, the IRQs will not fire."
500          */
501         reg = LRADC_CH_ACCUMULATE |
502                 LRADC_CH_NUM_SAMPLES(lradc->over_sample_cnt - 1);
503         mxs_lradc_reg_wrt(lradc, reg, LRADC_CH(ch1));
504         mxs_lradc_reg_wrt(lradc, reg, LRADC_CH(ch2));
505
506         /* from the datasheet:
507          * "Software must clear this register in preparation for a
508          * multi-cycle accumulation.
509          */
510         mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch1));
511         mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch2));
512
513         /* prepare the delay/loop unit according to the oversampling count */
514         mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << ch1) |
515                 LRADC_DELAY_TRIGGER(1 << ch2) | /* start both channels */
516                 LRADC_DELAY_TRIGGER_DELAYS(0) |
517                 LRADC_DELAY_LOOP(lradc->over_sample_cnt - 1) |
518                 LRADC_DELAY_DELAY(lradc->over_sample_delay - 1),
519                                         LRADC_DELAY(3));
520
521         mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(ch2), LRADC_CTRL1);
522
523         /*
524          * after changing the touchscreen plates setting
525          * the signals need some initial time to settle. Start the
526          * SoC's delay unit and start the conversion later
527          * and automatically.
528          */
529         mxs_lradc_reg_wrt(lradc,
530                 LRADC_DELAY_TRIGGER(0) | /* don't trigger ADC */
531                 LRADC_DELAY_TRIGGER_DELAYS(BIT(3)) | /* trigger DELAY unit#3 */
532                 LRADC_DELAY_KICK |
533                 LRADC_DELAY_DELAY(lradc->settling_delay), LRADC_DELAY(2));
534 }
535
536 static unsigned mxs_lradc_read_raw_channel(struct mxs_lradc *lradc,
537                                                         unsigned channel)
538 {
539         u32 reg;
540         unsigned num_samples, val;
541
542         reg = readl(lradc->base + LRADC_CH(channel));
543         if (reg & LRADC_CH_ACCUMULATE)
544                 num_samples = lradc->over_sample_cnt;
545         else
546                 num_samples = 1;
547
548         val = (reg & LRADC_CH_VALUE_MASK) >> LRADC_CH_VALUE_OFFSET;
549         return val / num_samples;
550 }
551
552 static unsigned mxs_lradc_read_ts_pressure(struct mxs_lradc *lradc,
553                                                 unsigned ch1, unsigned ch2)
554 {
555         u32 reg, mask;
556         unsigned pressure, m1, m2;
557
558         mask = LRADC_CTRL1_LRADC_IRQ(ch1) | LRADC_CTRL1_LRADC_IRQ(ch2);
559         reg = readl(lradc->base + LRADC_CTRL1) & mask;
560
561         while (reg != mask) {
562                 reg = readl(lradc->base + LRADC_CTRL1) & mask;
563                 dev_dbg(lradc->dev, "One channel is still busy: %X\n", reg);
564         }
565
566         m1 = mxs_lradc_read_raw_channel(lradc, ch1);
567         m2 = mxs_lradc_read_raw_channel(lradc, ch2);
568
569         if (m2 == 0) {
570                 dev_warn(lradc->dev, "Cannot calculate pressure\n");
571                 return 1 << (LRADC_RESOLUTION - 1);
572         }
573
574         /* simply scale the value from 0 ... max ADC resolution */
575         pressure = m1;
576         pressure *= (1 << LRADC_RESOLUTION);
577         pressure /= m2;
578
579         dev_dbg(lradc->dev, "Pressure = %u\n", pressure);
580         return pressure;
581 }
582
583 #define TS_CH_XP 2
584 #define TS_CH_YP 3
585 #define TS_CH_XM 4
586 #define TS_CH_YM 5
587
588 /*
589  * YP(open)--+-------------+
590  *           |             |--+
591  *           |             |  |
592  *    YM(-)--+-------------+  |
593  *             +--------------+
594  *             |              |
595  *         XP(weak+)        XM(open)
596  *
597  * "weak+" means 200k Ohm VDDIO
598  * (-) means GND
599  */
600 static void mxs_lradc_setup_touch_detection(struct mxs_lradc *lradc)
601 {
602         /*
603          * In order to detect a touch event the 'touch detect enable' bit
604          * enables:
605          *  - a weak pullup to the X+ connector
606          *  - a strong ground at the Y- connector
607          */
608         mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
609         mxs_lradc_reg_set(lradc, mxs_lradc_touch_detect_bit(lradc),
610                                 LRADC_CTRL0);
611 }
612
613 /*
614  * YP(meas)--+-------------+
615  *           |             |--+
616  *           |             |  |
617  * YM(open)--+-------------+  |
618  *             +--------------+
619  *             |              |
620  *           XP(+)          XM(-)
621  *
622  * (+) means here 1.85 V
623  * (-) means here GND
624  */
625 static void mxs_lradc_prepare_x_pos(struct mxs_lradc *lradc)
626 {
627         mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
628         mxs_lradc_reg_set(lradc, mxs_lradc_drive_x_plate(lradc), LRADC_CTRL0);
629
630         lradc->cur_plate = LRADC_SAMPLE_X;
631         mxs_lradc_map_channel(lradc, TOUCHSCREEN_VCHANNEL1, TS_CH_YP);
632         mxs_lradc_setup_ts_channel(lradc, TOUCHSCREEN_VCHANNEL1);
633 }
634
635 /*
636  *   YP(+)--+-------------+
637  *          |             |--+
638  *          |             |  |
639  *   YM(-)--+-------------+  |
640  *            +--------------+
641  *            |              |
642  *         XP(open)        XM(meas)
643  *
644  * (+) means here 1.85 V
645  * (-) means here GND
646  */
647 static void mxs_lradc_prepare_y_pos(struct mxs_lradc *lradc)
648 {
649         mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
650         mxs_lradc_reg_set(lradc, mxs_lradc_drive_y_plate(lradc), LRADC_CTRL0);
651
652         lradc->cur_plate = LRADC_SAMPLE_Y;
653         mxs_lradc_map_channel(lradc, TOUCHSCREEN_VCHANNEL1, TS_CH_XM);
654         mxs_lradc_setup_ts_channel(lradc, TOUCHSCREEN_VCHANNEL1);
655 }
656
657 /*
658  *    YP(+)--+-------------+
659  *           |             |--+
660  *           |             |  |
661  * YM(meas)--+-------------+  |
662  *             +--------------+
663  *             |              |
664  *          XP(meas)        XM(-)
665  *
666  * (+) means here 1.85 V
667  * (-) means here GND
668  */
669 static void mxs_lradc_prepare_pressure(struct mxs_lradc *lradc)
670 {
671         mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
672         mxs_lradc_reg_set(lradc, mxs_lradc_drive_pressure(lradc), LRADC_CTRL0);
673
674         lradc->cur_plate = LRADC_SAMPLE_PRESSURE;
675         mxs_lradc_map_channel(lradc, TOUCHSCREEN_VCHANNEL1, TS_CH_YM);
676         mxs_lradc_map_channel(lradc, TOUCHSCREEN_VCHANNEL2, TS_CH_XP);
677         mxs_lradc_setup_ts_pressure(lradc, TOUCHSCREEN_VCHANNEL2,
678                                                 TOUCHSCREEN_VCHANNEL1);
679 }
680
681 static void mxs_lradc_enable_touch_detection(struct mxs_lradc *lradc)
682 {
683         mxs_lradc_setup_touch_detection(lradc);
684
685         lradc->cur_plate = LRADC_TOUCH;
686         mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ |
687                                 LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1);
688         mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1);
689 }
690
691 static void mxs_lradc_start_touch_event(struct mxs_lradc *lradc)
692 {
693         mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN,
694                                 LRADC_CTRL1);
695         mxs_lradc_reg_set(lradc,
696                 LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL1), LRADC_CTRL1);
697         /*
698          * start with the Y-pos, because it uses nearly the same plate
699          * settings like the touch detection
700          */
701         mxs_lradc_prepare_y_pos(lradc);
702 }
703
704 static void mxs_lradc_report_ts_event(struct mxs_lradc *lradc)
705 {
706         input_report_abs(lradc->ts_input, ABS_X, lradc->ts_x_pos);
707         input_report_abs(lradc->ts_input, ABS_Y, lradc->ts_y_pos);
708         input_report_abs(lradc->ts_input, ABS_PRESSURE, lradc->ts_pressure);
709         input_report_key(lradc->ts_input, BTN_TOUCH, 1);
710         input_sync(lradc->ts_input);
711 }
712
713 static void mxs_lradc_complete_touch_event(struct mxs_lradc *lradc)
714 {
715         mxs_lradc_setup_touch_detection(lradc);
716         lradc->cur_plate = LRADC_SAMPLE_VALID;
717         /*
718          * start a dummy conversion to burn time to settle the signals
719          * note: we are not interested in the conversion's value
720          */
721         mxs_lradc_reg_wrt(lradc, 0, LRADC_CH(TOUCHSCREEN_VCHANNEL1));
722         mxs_lradc_reg_clear(lradc,
723                 LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1) |
724                 LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL2), LRADC_CTRL1);
725         mxs_lradc_reg_wrt(lradc,
726                 LRADC_DELAY_TRIGGER(1 << TOUCHSCREEN_VCHANNEL1) |
727                 LRADC_DELAY_KICK | LRADC_DELAY_DELAY(10), /* waste 5 ms */
728                         LRADC_DELAY(2));
729 }
730
731 /*
732  * in order to avoid false measurements, report only samples where
733  * the surface is still touched after the position measurement
734  */
735 static void mxs_lradc_finish_touch_event(struct mxs_lradc *lradc, bool valid)
736 {
737         /* if it is still touched, report the sample */
738         if (valid && mxs_lradc_check_touch_event(lradc)) {
739                 lradc->ts_valid = true;
740                 mxs_lradc_report_ts_event(lradc);
741         }
742
743         /* if it is even still touched, continue with the next measurement */
744         if (mxs_lradc_check_touch_event(lradc)) {
745                 mxs_lradc_prepare_y_pos(lradc);
746                 return;
747         }
748
749         if (lradc->ts_valid) {
750                 /* signal the release */
751                 lradc->ts_valid = false;
752                 input_report_key(lradc->ts_input, BTN_TOUCH, 0);
753                 input_sync(lradc->ts_input);
754         }
755
756         /* if it is released, wait for the next touch via IRQ */
757         lradc->cur_plate = LRADC_TOUCH;
758         mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2));
759         mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3));
760         mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ |
761                 LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL1) |
762                 LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1), LRADC_CTRL1);
763         mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1);
764 }
765
766 /* touchscreen's state machine */
767 static void mxs_lradc_handle_touch(struct mxs_lradc *lradc)
768 {
769         switch (lradc->cur_plate) {
770         case LRADC_TOUCH:
771                 if (mxs_lradc_check_touch_event(lradc))
772                         mxs_lradc_start_touch_event(lradc);
773                 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ,
774                                         LRADC_CTRL1);
775                 return;
776
777         case LRADC_SAMPLE_Y:
778                 lradc->ts_y_pos = mxs_lradc_read_raw_channel(lradc,
779                                                         TOUCHSCREEN_VCHANNEL1);
780                 mxs_lradc_prepare_x_pos(lradc);
781                 return;
782
783         case LRADC_SAMPLE_X:
784                 lradc->ts_x_pos = mxs_lradc_read_raw_channel(lradc,
785                                                         TOUCHSCREEN_VCHANNEL1);
786                 mxs_lradc_prepare_pressure(lradc);
787                 return;
788
789         case LRADC_SAMPLE_PRESSURE:
790                 lradc->ts_pressure = mxs_lradc_read_ts_pressure(lradc,
791                                                         TOUCHSCREEN_VCHANNEL2,
792                                                         TOUCHSCREEN_VCHANNEL1);
793                 mxs_lradc_complete_touch_event(lradc);
794                 return;
795
796         case LRADC_SAMPLE_VALID:
797                 mxs_lradc_finish_touch_event(lradc, 1);
798                 break;
799         }
800 }
801
802 /*
803  * Raw I/O operations
804  */
805 static int mxs_lradc_read_single(struct iio_dev *iio_dev, int chan, int *val)
806 {
807         struct mxs_lradc *lradc = iio_priv(iio_dev);
808         int ret;
809
810         /*
811          * See if there is no buffered operation in progress. If there is, simply
812          * bail out. This can be improved to support both buffered and raw IO at
813          * the same time, yet the code becomes horribly complicated. Therefore I
814          * applied KISS principle here.
815          */
816         ret = mutex_trylock(&lradc->lock);
817         if (!ret)
818                 return -EBUSY;
819
820         reinit_completion(&lradc->completion);
821
822         /*
823          * No buffered operation in progress, map the channel and trigger it.
824          * Virtual channel 0 is always used here as the others are always not
825          * used if doing raw sampling.
826          */
827         if (lradc->soc == IMX28_LRADC)
828                 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(0),
829                         LRADC_CTRL1);
830         mxs_lradc_reg_clear(lradc, 0x1, LRADC_CTRL0);
831
832         /* Enable / disable the divider per requirement */
833         if (test_bit(chan, &lradc->is_divided))
834                 mxs_lradc_reg_set(lradc, 1 << LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET,
835                         LRADC_CTRL2);
836         else
837                 mxs_lradc_reg_clear(lradc,
838                         1 << LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET, LRADC_CTRL2);
839
840         /* Clean the slot's previous content, then set new one. */
841         mxs_lradc_reg_clear(lradc, LRADC_CTRL4_LRADCSELECT_MASK(0),
842                         LRADC_CTRL4);
843         mxs_lradc_reg_set(lradc, chan, LRADC_CTRL4);
844
845         mxs_lradc_reg_wrt(lradc, 0, LRADC_CH(0));
846
847         /* Enable the IRQ and start sampling the channel. */
848         mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(0), LRADC_CTRL1);
849         mxs_lradc_reg_set(lradc, BIT(0), LRADC_CTRL0);
850
851         /* Wait for completion on the channel, 1 second max. */
852         ret = wait_for_completion_killable_timeout(&lradc->completion, HZ);
853         if (!ret)
854                 ret = -ETIMEDOUT;
855         if (ret < 0)
856                 goto err;
857
858         /* Read the data. */
859         *val = readl(lradc->base + LRADC_CH(0)) & LRADC_CH_VALUE_MASK;
860         ret = IIO_VAL_INT;
861
862 err:
863         mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(0), LRADC_CTRL1);
864
865         mutex_unlock(&lradc->lock);
866
867         return ret;
868 }
869
870 static int mxs_lradc_read_temp(struct iio_dev *iio_dev, int *val)
871 {
872         int ret, min, max;
873
874         ret = mxs_lradc_read_single(iio_dev, 8, &min);
875         if (ret != IIO_VAL_INT)
876                 return ret;
877
878         ret = mxs_lradc_read_single(iio_dev, 9, &max);
879         if (ret != IIO_VAL_INT)
880                 return ret;
881
882         *val = max - min;
883
884         return IIO_VAL_INT;
885 }
886
887 static int mxs_lradc_read_raw(struct iio_dev *iio_dev,
888                         const struct iio_chan_spec *chan,
889                         int *val, int *val2, long m)
890 {
891         struct mxs_lradc *lradc = iio_priv(iio_dev);
892
893         switch (m) {
894         case IIO_CHAN_INFO_RAW:
895                 if (chan->type == IIO_TEMP)
896                         return mxs_lradc_read_temp(iio_dev, val);
897
898                 return mxs_lradc_read_single(iio_dev, chan->channel, val);
899
900         case IIO_CHAN_INFO_SCALE:
901                 if (chan->type == IIO_TEMP) {
902                         /* From the datasheet, we have to multiply by 1.012 and
903                          * divide by 4
904                          */
905                         *val = 0;
906                         *val2 = 253000;
907                         return IIO_VAL_INT_PLUS_MICRO;
908                 }
909
910                 *val = lradc->vref_mv[chan->channel];
911                 *val2 = chan->scan_type.realbits -
912                         test_bit(chan->channel, &lradc->is_divided);
913                 return IIO_VAL_FRACTIONAL_LOG2;
914
915         case IIO_CHAN_INFO_OFFSET:
916                 if (chan->type == IIO_TEMP) {
917                         /* The calculated value from the ADC is in Kelvin, we
918                          * want Celsius for hwmon so the offset is -273.15
919                          * The offset is applied before scaling so it is
920                          * actually -213.15 * 4 / 1.012 = -1079.644268
921                          */
922                         *val = -1079;
923                         *val2 = 644268;
924
925                         return IIO_VAL_INT_PLUS_MICRO;
926                 }
927
928                 return -EINVAL;
929
930         default:
931                 break;
932         }
933
934         return -EINVAL;
935 }
936
937 static int mxs_lradc_write_raw(struct iio_dev *iio_dev,
938                                const struct iio_chan_spec *chan,
939                                int val, int val2, long m)
940 {
941         struct mxs_lradc *lradc = iio_priv(iio_dev);
942         struct mxs_lradc_scale *scale_avail =
943                         lradc->scale_avail[chan->channel];
944         int ret;
945
946         ret = mutex_trylock(&lradc->lock);
947         if (!ret)
948                 return -EBUSY;
949
950         switch (m) {
951         case IIO_CHAN_INFO_SCALE:
952                 ret = -EINVAL;
953                 if (val == scale_avail[MXS_LRADC_DIV_DISABLED].integer &&
954                     val2 == scale_avail[MXS_LRADC_DIV_DISABLED].nano) {
955                         /* divider by two disabled */
956                         clear_bit(chan->channel, &lradc->is_divided);
957                         ret = 0;
958                 } else if (val == scale_avail[MXS_LRADC_DIV_ENABLED].integer &&
959                            val2 == scale_avail[MXS_LRADC_DIV_ENABLED].nano) {
960                         /* divider by two enabled */
961                         set_bit(chan->channel, &lradc->is_divided);
962                         ret = 0;
963                 }
964
965                 break;
966         default:
967                 ret = -EINVAL;
968                 break;
969         }
970
971         mutex_unlock(&lradc->lock);
972
973         return ret;
974 }
975
976 static int mxs_lradc_write_raw_get_fmt(struct iio_dev *iio_dev,
977                                        const struct iio_chan_spec *chan,
978                                        long m)
979 {
980         return IIO_VAL_INT_PLUS_NANO;
981 }
982
983 static ssize_t mxs_lradc_show_scale_available_ch(struct device *dev,
984                 struct device_attribute *attr,
985                 char *buf,
986                 int ch)
987 {
988         struct iio_dev *iio = dev_to_iio_dev(dev);
989         struct mxs_lradc *lradc = iio_priv(iio);
990         int i, len = 0;
991
992         for (i = 0; i < ARRAY_SIZE(lradc->scale_avail[ch]); i++)
993                 len += sprintf(buf + len, "%u.%09u ",
994                                lradc->scale_avail[ch][i].integer,
995                                lradc->scale_avail[ch][i].nano);
996
997         len += sprintf(buf + len, "\n");
998
999         return len;
1000 }
1001
1002 static ssize_t mxs_lradc_show_scale_available(struct device *dev,
1003                 struct device_attribute *attr,
1004                 char *buf)
1005 {
1006         struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
1007
1008         return mxs_lradc_show_scale_available_ch(dev, attr, buf,
1009                                                  iio_attr->address);
1010 }
1011
1012 #define SHOW_SCALE_AVAILABLE_ATTR(ch)                                   \
1013 static IIO_DEVICE_ATTR(in_voltage##ch##_scale_available, S_IRUGO,       \
1014                        mxs_lradc_show_scale_available, NULL, ch)
1015
1016 SHOW_SCALE_AVAILABLE_ATTR(0);
1017 SHOW_SCALE_AVAILABLE_ATTR(1);
1018 SHOW_SCALE_AVAILABLE_ATTR(2);
1019 SHOW_SCALE_AVAILABLE_ATTR(3);
1020 SHOW_SCALE_AVAILABLE_ATTR(4);
1021 SHOW_SCALE_AVAILABLE_ATTR(5);
1022 SHOW_SCALE_AVAILABLE_ATTR(6);
1023 SHOW_SCALE_AVAILABLE_ATTR(7);
1024 SHOW_SCALE_AVAILABLE_ATTR(10);
1025 SHOW_SCALE_AVAILABLE_ATTR(11);
1026 SHOW_SCALE_AVAILABLE_ATTR(12);
1027 SHOW_SCALE_AVAILABLE_ATTR(13);
1028 SHOW_SCALE_AVAILABLE_ATTR(14);
1029 SHOW_SCALE_AVAILABLE_ATTR(15);
1030
1031 static struct attribute *mxs_lradc_attributes[] = {
1032         &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
1033         &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
1034         &iio_dev_attr_in_voltage2_scale_available.dev_attr.attr,
1035         &iio_dev_attr_in_voltage3_scale_available.dev_attr.attr,
1036         &iio_dev_attr_in_voltage4_scale_available.dev_attr.attr,
1037         &iio_dev_attr_in_voltage5_scale_available.dev_attr.attr,
1038         &iio_dev_attr_in_voltage6_scale_available.dev_attr.attr,
1039         &iio_dev_attr_in_voltage7_scale_available.dev_attr.attr,
1040         &iio_dev_attr_in_voltage10_scale_available.dev_attr.attr,
1041         &iio_dev_attr_in_voltage11_scale_available.dev_attr.attr,
1042         &iio_dev_attr_in_voltage12_scale_available.dev_attr.attr,
1043         &iio_dev_attr_in_voltage13_scale_available.dev_attr.attr,
1044         &iio_dev_attr_in_voltage14_scale_available.dev_attr.attr,
1045         &iio_dev_attr_in_voltage15_scale_available.dev_attr.attr,
1046         NULL
1047 };
1048
1049 static const struct attribute_group mxs_lradc_attribute_group = {
1050         .attrs = mxs_lradc_attributes,
1051 };
1052
1053 static const struct iio_info mxs_lradc_iio_info = {
1054         .driver_module          = THIS_MODULE,
1055         .read_raw               = mxs_lradc_read_raw,
1056         .write_raw              = mxs_lradc_write_raw,
1057         .write_raw_get_fmt      = mxs_lradc_write_raw_get_fmt,
1058         .attrs                  = &mxs_lradc_attribute_group,
1059 };
1060
1061 static int mxs_lradc_ts_open(struct input_dev *dev)
1062 {
1063         struct mxs_lradc *lradc = input_get_drvdata(dev);
1064
1065         /* Enable the touch-detect circuitry. */
1066         mxs_lradc_enable_touch_detection(lradc);
1067
1068         return 0;
1069 }
1070
1071 static void mxs_lradc_disable_ts(struct mxs_lradc *lradc)
1072 {
1073         /* stop all interrupts from firing */
1074         mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN |
1075                 LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL1) |
1076                 LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL2), LRADC_CTRL1);
1077
1078         /* Power-down touchscreen touch-detect circuitry. */
1079         mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
1080 }
1081
1082 static void mxs_lradc_ts_close(struct input_dev *dev)
1083 {
1084         struct mxs_lradc *lradc = input_get_drvdata(dev);
1085
1086         mxs_lradc_disable_ts(lradc);
1087 }
1088
1089 static int mxs_lradc_ts_register(struct mxs_lradc *lradc)
1090 {
1091         struct input_dev *input;
1092         struct device *dev = lradc->dev;
1093         int ret;
1094
1095         if (!lradc->use_touchscreen)
1096                 return 0;
1097
1098         input = input_allocate_device();
1099         if (!input)
1100                 return -ENOMEM;
1101
1102         input->name = DRIVER_NAME;
1103         input->id.bustype = BUS_HOST;
1104         input->dev.parent = dev;
1105         input->open = mxs_lradc_ts_open;
1106         input->close = mxs_lradc_ts_close;
1107
1108         __set_bit(EV_ABS, input->evbit);
1109         __set_bit(EV_KEY, input->evbit);
1110         __set_bit(BTN_TOUCH, input->keybit);
1111         input_set_abs_params(input, ABS_X, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0);
1112         input_set_abs_params(input, ABS_Y, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0);
1113         input_set_abs_params(input, ABS_PRESSURE, 0, LRADC_SINGLE_SAMPLE_MASK,
1114                              0, 0);
1115
1116         lradc->ts_input = input;
1117         input_set_drvdata(input, lradc);
1118         ret = input_register_device(input);
1119         if (ret)
1120                 input_free_device(lradc->ts_input);
1121
1122         return ret;
1123 }
1124
1125 static void mxs_lradc_ts_unregister(struct mxs_lradc *lradc)
1126 {
1127         if (!lradc->use_touchscreen)
1128                 return;
1129
1130         mxs_lradc_disable_ts(lradc);
1131         input_unregister_device(lradc->ts_input);
1132 }
1133
1134 /*
1135  * IRQ Handling
1136  */
1137 static irqreturn_t mxs_lradc_handle_irq(int irq, void *data)
1138 {
1139         struct iio_dev *iio = data;
1140         struct mxs_lradc *lradc = iio_priv(iio);
1141         unsigned long reg = readl(lradc->base + LRADC_CTRL1);
1142         uint32_t clr_irq = mxs_lradc_irq_mask(lradc);
1143         const uint32_t ts_irq_mask =
1144                 LRADC_CTRL1_TOUCH_DETECT_IRQ |
1145                 LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1) |
1146                 LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL2);
1147
1148         if (!(reg & mxs_lradc_irq_mask(lradc)))
1149                 return IRQ_NONE;
1150
1151         if (lradc->use_touchscreen && (reg & ts_irq_mask)) {
1152                 mxs_lradc_handle_touch(lradc);
1153
1154                 /* Make sure we don't clear the next conversion's interrupt. */
1155                 clr_irq &= ~(LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1) |
1156                                 LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL2));
1157         }
1158
1159         if (iio_buffer_enabled(iio)) {
1160                 if (reg & lradc->buffer_vchans)
1161                         iio_trigger_poll(iio->trig);
1162         } else if (reg & LRADC_CTRL1_LRADC_IRQ(0)) {
1163                 complete(&lradc->completion);
1164         }
1165
1166         mxs_lradc_reg_clear(lradc, reg & clr_irq, LRADC_CTRL1);
1167
1168         return IRQ_HANDLED;
1169 }
1170
1171 /*
1172  * Trigger handling
1173  */
1174 static irqreturn_t mxs_lradc_trigger_handler(int irq, void *p)
1175 {
1176         struct iio_poll_func *pf = p;
1177         struct iio_dev *iio = pf->indio_dev;
1178         struct mxs_lradc *lradc = iio_priv(iio);
1179         const uint32_t chan_value = LRADC_CH_ACCUMULATE |
1180                 ((LRADC_DELAY_TIMER_LOOP - 1) << LRADC_CH_NUM_SAMPLES_OFFSET);
1181         unsigned int i, j = 0;
1182
1183         for_each_set_bit(i, iio->active_scan_mask, LRADC_MAX_TOTAL_CHANS) {
1184                 lradc->buffer[j] = readl(lradc->base + LRADC_CH(j));
1185                 mxs_lradc_reg_wrt(lradc, chan_value, LRADC_CH(j));
1186                 lradc->buffer[j] &= LRADC_CH_VALUE_MASK;
1187                 lradc->buffer[j] /= LRADC_DELAY_TIMER_LOOP;
1188                 j++;
1189         }
1190
1191         iio_push_to_buffers_with_timestamp(iio, lradc->buffer, pf->timestamp);
1192
1193         iio_trigger_notify_done(iio->trig);
1194
1195         return IRQ_HANDLED;
1196 }
1197
1198 static int mxs_lradc_configure_trigger(struct iio_trigger *trig, bool state)
1199 {
1200         struct iio_dev *iio = iio_trigger_get_drvdata(trig);
1201         struct mxs_lradc *lradc = iio_priv(iio);
1202         const uint32_t st = state ? STMP_OFFSET_REG_SET : STMP_OFFSET_REG_CLR;
1203
1204         mxs_lradc_reg_wrt(lradc, LRADC_DELAY_KICK, LRADC_DELAY(0) + st);
1205
1206         return 0;
1207 }
1208
1209 static const struct iio_trigger_ops mxs_lradc_trigger_ops = {
1210         .owner = THIS_MODULE,
1211         .set_trigger_state = &mxs_lradc_configure_trigger,
1212 };
1213
1214 static int mxs_lradc_trigger_init(struct iio_dev *iio)
1215 {
1216         int ret;
1217         struct iio_trigger *trig;
1218         struct mxs_lradc *lradc = iio_priv(iio);
1219
1220         trig = iio_trigger_alloc("%s-dev%i", iio->name, iio->id);
1221         if (trig == NULL)
1222                 return -ENOMEM;
1223
1224         trig->dev.parent = lradc->dev;
1225         iio_trigger_set_drvdata(trig, iio);
1226         trig->ops = &mxs_lradc_trigger_ops;
1227
1228         ret = iio_trigger_register(trig);
1229         if (ret) {
1230                 iio_trigger_free(trig);
1231                 return ret;
1232         }
1233
1234         lradc->trig = trig;
1235
1236         return 0;
1237 }
1238
1239 static void mxs_lradc_trigger_remove(struct iio_dev *iio)
1240 {
1241         struct mxs_lradc *lradc = iio_priv(iio);
1242
1243         iio_trigger_unregister(lradc->trig);
1244         iio_trigger_free(lradc->trig);
1245 }
1246
1247 static int mxs_lradc_buffer_preenable(struct iio_dev *iio)
1248 {
1249         struct mxs_lradc *lradc = iio_priv(iio);
1250         int ret = 0, chan, ofs = 0;
1251         unsigned long enable = 0;
1252         uint32_t ctrl4_set = 0;
1253         uint32_t ctrl4_clr = 0;
1254         uint32_t ctrl1_irq = 0;
1255         const uint32_t chan_value = LRADC_CH_ACCUMULATE |
1256                 ((LRADC_DELAY_TIMER_LOOP - 1) << LRADC_CH_NUM_SAMPLES_OFFSET);
1257         const int len = bitmap_weight(iio->active_scan_mask,
1258                         LRADC_MAX_TOTAL_CHANS);
1259
1260         if (!len)
1261                 return -EINVAL;
1262
1263         /*
1264          * Lock the driver so raw access can not be done during buffered
1265          * operation. This simplifies the code a lot.
1266          */
1267         ret = mutex_trylock(&lradc->lock);
1268         if (!ret)
1269                 return -EBUSY;
1270
1271         lradc->buffer = kmalloc_array(len, sizeof(*lradc->buffer), GFP_KERNEL);
1272         if (!lradc->buffer) {
1273                 ret = -ENOMEM;
1274                 goto err_mem;
1275         }
1276
1277         if (lradc->soc == IMX28_LRADC)
1278                 mxs_lradc_reg_clear(lradc,
1279                         lradc->buffer_vchans << LRADC_CTRL1_LRADC_IRQ_EN_OFFSET,
1280                         LRADC_CTRL1);
1281         mxs_lradc_reg_clear(lradc, lradc->buffer_vchans, LRADC_CTRL0);
1282
1283         for_each_set_bit(chan, iio->active_scan_mask, LRADC_MAX_TOTAL_CHANS) {
1284                 ctrl4_set |= chan << LRADC_CTRL4_LRADCSELECT_OFFSET(ofs);
1285                 ctrl4_clr |= LRADC_CTRL4_LRADCSELECT_MASK(ofs);
1286                 ctrl1_irq |= LRADC_CTRL1_LRADC_IRQ_EN(ofs);
1287                 mxs_lradc_reg_wrt(lradc, chan_value, LRADC_CH(ofs));
1288                 bitmap_set(&enable, ofs, 1);
1289                 ofs++;
1290         }
1291
1292         mxs_lradc_reg_clear(lradc, LRADC_DELAY_TRIGGER_LRADCS_MASK |
1293                                         LRADC_DELAY_KICK, LRADC_DELAY(0));
1294         mxs_lradc_reg_clear(lradc, ctrl4_clr, LRADC_CTRL4);
1295         mxs_lradc_reg_set(lradc, ctrl4_set, LRADC_CTRL4);
1296         mxs_lradc_reg_set(lradc, ctrl1_irq, LRADC_CTRL1);
1297         mxs_lradc_reg_set(lradc, enable << LRADC_DELAY_TRIGGER_LRADCS_OFFSET,
1298                                         LRADC_DELAY(0));
1299
1300         return 0;
1301
1302 err_mem:
1303         mutex_unlock(&lradc->lock);
1304         return ret;
1305 }
1306
1307 static int mxs_lradc_buffer_postdisable(struct iio_dev *iio)
1308 {
1309         struct mxs_lradc *lradc = iio_priv(iio);
1310
1311         mxs_lradc_reg_clear(lradc, LRADC_DELAY_TRIGGER_LRADCS_MASK |
1312                                         LRADC_DELAY_KICK, LRADC_DELAY(0));
1313
1314         mxs_lradc_reg_clear(lradc, lradc->buffer_vchans, LRADC_CTRL0);
1315         if (lradc->soc == IMX28_LRADC)
1316                 mxs_lradc_reg_clear(lradc,
1317                         lradc->buffer_vchans << LRADC_CTRL1_LRADC_IRQ_EN_OFFSET,
1318                         LRADC_CTRL1);
1319
1320         kfree(lradc->buffer);
1321         mutex_unlock(&lradc->lock);
1322
1323         return 0;
1324 }
1325
1326 static bool mxs_lradc_validate_scan_mask(struct iio_dev *iio,
1327                                         const unsigned long *mask)
1328 {
1329         struct mxs_lradc *lradc = iio_priv(iio);
1330         const int map_chans = bitmap_weight(mask, LRADC_MAX_TOTAL_CHANS);
1331         int rsvd_chans = 0;
1332         unsigned long rsvd_mask = 0;
1333
1334         if (lradc->use_touchbutton)
1335                 rsvd_mask |= CHAN_MASK_TOUCHBUTTON;
1336         if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_4WIRE)
1337                 rsvd_mask |= CHAN_MASK_TOUCHSCREEN_4WIRE;
1338         if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE)
1339                 rsvd_mask |= CHAN_MASK_TOUCHSCREEN_5WIRE;
1340
1341         if (lradc->use_touchbutton)
1342                 rsvd_chans++;
1343         if (lradc->use_touchscreen)
1344                 rsvd_chans += 2;
1345
1346         /* Test for attempts to map channels with special mode of operation. */
1347         if (bitmap_intersects(mask, &rsvd_mask, LRADC_MAX_TOTAL_CHANS))
1348                 return false;
1349
1350         /* Test for attempts to map more channels then available slots. */
1351         if (map_chans + rsvd_chans > LRADC_MAX_MAPPED_CHANS)
1352                 return false;
1353
1354         return true;
1355 }
1356
1357 static const struct iio_buffer_setup_ops mxs_lradc_buffer_ops = {
1358         .preenable = &mxs_lradc_buffer_preenable,
1359         .postenable = &iio_triggered_buffer_postenable,
1360         .predisable = &iio_triggered_buffer_predisable,
1361         .postdisable = &mxs_lradc_buffer_postdisable,
1362         .validate_scan_mask = &mxs_lradc_validate_scan_mask,
1363 };
1364
1365 /*
1366  * Driver initialization
1367  */
1368
1369 #define MXS_ADC_CHAN(idx, chan_type, name) {                    \
1370         .type = (chan_type),                                    \
1371         .indexed = 1,                                           \
1372         .scan_index = (idx),                                    \
1373         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |          \
1374                               BIT(IIO_CHAN_INFO_SCALE),         \
1375         .channel = (idx),                                       \
1376         .address = (idx),                                       \
1377         .scan_type = {                                          \
1378                 .sign = 'u',                                    \
1379                 .realbits = LRADC_RESOLUTION,                   \
1380                 .storagebits = 32,                              \
1381         },                                                      \
1382         .datasheet_name = (name),                               \
1383 }
1384
1385 static const struct iio_chan_spec mx23_lradc_chan_spec[] = {
1386         MXS_ADC_CHAN(0, IIO_VOLTAGE, "LRADC0"),
1387         MXS_ADC_CHAN(1, IIO_VOLTAGE, "LRADC1"),
1388         MXS_ADC_CHAN(2, IIO_VOLTAGE, "LRADC2"),
1389         MXS_ADC_CHAN(3, IIO_VOLTAGE, "LRADC3"),
1390         MXS_ADC_CHAN(4, IIO_VOLTAGE, "LRADC4"),
1391         MXS_ADC_CHAN(5, IIO_VOLTAGE, "LRADC5"),
1392         MXS_ADC_CHAN(6, IIO_VOLTAGE, "VDDIO"),
1393         MXS_ADC_CHAN(7, IIO_VOLTAGE, "VBATT"),
1394         /* Combined Temperature sensors */
1395         {
1396                 .type = IIO_TEMP,
1397                 .indexed = 1,
1398                 .scan_index = 8,
1399                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1400                                       BIT(IIO_CHAN_INFO_OFFSET) |
1401                                       BIT(IIO_CHAN_INFO_SCALE),
1402                 .channel = 8,
1403                 .scan_type = {.sign = 'u', .realbits = 18, .storagebits = 32,},
1404                 .datasheet_name = "TEMP_DIE",
1405         },
1406         /* Hidden channel to keep indexes */
1407         {
1408                 .type = IIO_TEMP,
1409                 .indexed = 1,
1410                 .scan_index = -1,
1411                 .channel = 9,
1412         },
1413         MXS_ADC_CHAN(10, IIO_VOLTAGE, NULL),
1414         MXS_ADC_CHAN(11, IIO_VOLTAGE, NULL),
1415         MXS_ADC_CHAN(12, IIO_VOLTAGE, "USB_DP"),
1416         MXS_ADC_CHAN(13, IIO_VOLTAGE, "USB_DN"),
1417         MXS_ADC_CHAN(14, IIO_VOLTAGE, "VBG"),
1418         MXS_ADC_CHAN(15, IIO_VOLTAGE, "VDD5V"),
1419 };
1420
1421 static const struct iio_chan_spec mx28_lradc_chan_spec[] = {
1422         MXS_ADC_CHAN(0, IIO_VOLTAGE, "LRADC0"),
1423         MXS_ADC_CHAN(1, IIO_VOLTAGE, "LRADC1"),
1424         MXS_ADC_CHAN(2, IIO_VOLTAGE, "LRADC2"),
1425         MXS_ADC_CHAN(3, IIO_VOLTAGE, "LRADC3"),
1426         MXS_ADC_CHAN(4, IIO_VOLTAGE, "LRADC4"),
1427         MXS_ADC_CHAN(5, IIO_VOLTAGE, "LRADC5"),
1428         MXS_ADC_CHAN(6, IIO_VOLTAGE, "LRADC6"),
1429         MXS_ADC_CHAN(7, IIO_VOLTAGE, "VBATT"),
1430         /* Combined Temperature sensors */
1431         {
1432                 .type = IIO_TEMP,
1433                 .indexed = 1,
1434                 .scan_index = 8,
1435                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1436                                       BIT(IIO_CHAN_INFO_OFFSET) |
1437                                       BIT(IIO_CHAN_INFO_SCALE),
1438                 .channel = 8,
1439                 .scan_type = {.sign = 'u', .realbits = 18, .storagebits = 32,},
1440                 .datasheet_name = "TEMP_DIE",
1441         },
1442         /* Hidden channel to keep indexes */
1443         {
1444                 .type = IIO_TEMP,
1445                 .indexed = 1,
1446                 .scan_index = -1,
1447                 .channel = 9,
1448         },
1449         MXS_ADC_CHAN(10, IIO_VOLTAGE, "VDDIO"),
1450         MXS_ADC_CHAN(11, IIO_VOLTAGE, "VTH"),
1451         MXS_ADC_CHAN(12, IIO_VOLTAGE, "VDDA"),
1452         MXS_ADC_CHAN(13, IIO_VOLTAGE, "VDDD"),
1453         MXS_ADC_CHAN(14, IIO_VOLTAGE, "VBG"),
1454         MXS_ADC_CHAN(15, IIO_VOLTAGE, "VDD5V"),
1455 };
1456
1457 static int mxs_lradc_hw_init(struct mxs_lradc *lradc)
1458 {
1459         /* The ADC always uses DELAY CHANNEL 0. */
1460         const uint32_t adc_cfg =
1461                 (1 << (LRADC_DELAY_TRIGGER_DELAYS_OFFSET + 0)) |
1462                 (LRADC_DELAY_TIMER_PER << LRADC_DELAY_DELAY_OFFSET);
1463
1464         int ret = stmp_reset_block(lradc->base);
1465
1466         if (ret)
1467                 return ret;
1468
1469         /* Configure DELAY CHANNEL 0 for generic ADC sampling. */
1470         mxs_lradc_reg_wrt(lradc, adc_cfg, LRADC_DELAY(0));
1471
1472         /* Disable remaining DELAY CHANNELs */
1473         mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(1));
1474         mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2));
1475         mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3));
1476
1477         /* Configure the touchscreen type */
1478         if (lradc->soc == IMX28_LRADC) {
1479                 mxs_lradc_reg_clear(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
1480                                                         LRADC_CTRL0);
1481
1482         if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE)
1483                 mxs_lradc_reg_set(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
1484                                 LRADC_CTRL0);
1485         }
1486
1487         /* Start internal temperature sensing. */
1488         mxs_lradc_reg_wrt(lradc, 0, LRADC_CTRL2);
1489
1490         return 0;
1491 }
1492
1493 static void mxs_lradc_hw_stop(struct mxs_lradc *lradc)
1494 {
1495         int i;
1496
1497         mxs_lradc_reg_clear(lradc, mxs_lradc_irq_en_mask(lradc), LRADC_CTRL1);
1498
1499         for (i = 0; i < LRADC_MAX_DELAY_CHANS; i++)
1500                 mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(i));
1501 }
1502
1503 static const struct of_device_id mxs_lradc_dt_ids[] = {
1504         { .compatible = "fsl,imx23-lradc", .data = (void *)IMX23_LRADC, },
1505         { .compatible = "fsl,imx28-lradc", .data = (void *)IMX28_LRADC, },
1506         { /* sentinel */ }
1507 };
1508 MODULE_DEVICE_TABLE(of, mxs_lradc_dt_ids);
1509
1510 static int mxs_lradc_probe_touchscreen(struct mxs_lradc *lradc,
1511                                                 struct device_node *lradc_node)
1512 {
1513         int ret;
1514         u32 ts_wires = 0, adapt;
1515
1516         ret = of_property_read_u32(lradc_node, "fsl,lradc-touchscreen-wires",
1517                                 &ts_wires);
1518         if (ret)
1519                 return -ENODEV; /* touchscreen feature disabled */
1520
1521         switch (ts_wires) {
1522         case 4:
1523                 lradc->use_touchscreen = MXS_LRADC_TOUCHSCREEN_4WIRE;
1524                 break;
1525         case 5:
1526                 if (lradc->soc == IMX28_LRADC) {
1527                         lradc->use_touchscreen = MXS_LRADC_TOUCHSCREEN_5WIRE;
1528                         break;
1529                 }
1530                 /* fall through an error message for i.MX23 */
1531         default:
1532                 dev_err(lradc->dev,
1533                         "Unsupported number of touchscreen wires (%d)\n",
1534                         ts_wires);
1535                 return -EINVAL;
1536         }
1537
1538         if (of_property_read_u32(lradc_node, "fsl,ave-ctrl", &adapt)) {
1539                 lradc->over_sample_cnt = 4;
1540         } else {
1541                 if (adapt < 1 || adapt > 32) {
1542                         dev_err(lradc->dev, "Invalid sample count (%u)\n",
1543                                 adapt);
1544                         return -EINVAL;
1545                 }
1546                 lradc->over_sample_cnt = adapt;
1547         }
1548
1549         if (of_property_read_u32(lradc_node, "fsl,ave-delay", &adapt)) {
1550                 lradc->over_sample_delay = 2;
1551         } else {
1552                 if (adapt < 2 || adapt > LRADC_DELAY_DELAY_MASK + 1) {
1553                         dev_err(lradc->dev, "Invalid sample delay (%u)\n",
1554                                 adapt);
1555                         return -EINVAL;
1556                 }
1557                 lradc->over_sample_delay = adapt;
1558         }
1559
1560         if (of_property_read_u32(lradc_node, "fsl,settling", &adapt)) {
1561                 lradc->settling_delay = 10;
1562         } else {
1563                 if (adapt < 1 || adapt > LRADC_DELAY_DELAY_MASK) {
1564                         dev_err(lradc->dev, "Invalid settling delay (%u)\n",
1565                                 adapt);
1566                         return -EINVAL;
1567                 }
1568                 lradc->settling_delay = adapt;
1569         }
1570
1571         return 0;
1572 }
1573
1574 static int mxs_lradc_probe(struct platform_device *pdev)
1575 {
1576         const struct of_device_id *of_id =
1577                 of_match_device(mxs_lradc_dt_ids, &pdev->dev);
1578         const struct mxs_lradc_of_config *of_cfg =
1579                 &mxs_lradc_of_config[(enum mxs_lradc_id)of_id->data];
1580         struct device *dev = &pdev->dev;
1581         struct device_node *node = dev->of_node;
1582         struct mxs_lradc *lradc;
1583         struct iio_dev *iio;
1584         struct resource *iores;
1585         int ret = 0, touch_ret;
1586         int i, s;
1587         uint64_t scale_uv;
1588
1589         /* Allocate the IIO device. */
1590         iio = devm_iio_device_alloc(dev, sizeof(*lradc));
1591         if (!iio) {
1592                 dev_err(dev, "Failed to allocate IIO device\n");
1593                 return -ENOMEM;
1594         }
1595
1596         lradc = iio_priv(iio);
1597         lradc->soc = (enum mxs_lradc_id)of_id->data;
1598
1599         /* Grab the memory area */
1600         iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1601         lradc->dev = &pdev->dev;
1602         lradc->base = devm_ioremap_resource(dev, iores);
1603         if (IS_ERR(lradc->base))
1604                 return PTR_ERR(lradc->base);
1605
1606         lradc->clk = devm_clk_get(&pdev->dev, NULL);
1607         if (IS_ERR(lradc->clk)) {
1608                 dev_err(dev, "Failed to get the delay unit clock\n");
1609                 return PTR_ERR(lradc->clk);
1610         }
1611         ret = clk_prepare_enable(lradc->clk);
1612         if (ret != 0) {
1613                 dev_err(dev, "Failed to enable the delay unit clock\n");
1614                 return ret;
1615         }
1616
1617         touch_ret = mxs_lradc_probe_touchscreen(lradc, node);
1618
1619         if (touch_ret == 0)
1620                 lradc->buffer_vchans = BUFFER_VCHANS_LIMITED;
1621         else
1622                 lradc->buffer_vchans = BUFFER_VCHANS_ALL;
1623
1624         /* Grab all IRQ sources */
1625         for (i = 0; i < of_cfg->irq_count; i++) {
1626                 lradc->irq[i] = platform_get_irq(pdev, i);
1627                 if (lradc->irq[i] < 0) {
1628                         ret = lradc->irq[i];
1629                         goto err_clk;
1630                 }
1631
1632                 ret = devm_request_irq(dev, lradc->irq[i],
1633                                         mxs_lradc_handle_irq, 0,
1634                                         of_cfg->irq_name[i], iio);
1635                 if (ret)
1636                         goto err_clk;
1637         }
1638
1639         lradc->vref_mv = of_cfg->vref_mv;
1640
1641         platform_set_drvdata(pdev, iio);
1642
1643         init_completion(&lradc->completion);
1644         mutex_init(&lradc->lock);
1645
1646         iio->name = pdev->name;
1647         iio->dev.parent = &pdev->dev;
1648         iio->info = &mxs_lradc_iio_info;
1649         iio->modes = INDIO_DIRECT_MODE;
1650         iio->masklength = LRADC_MAX_TOTAL_CHANS;
1651
1652         if (lradc->soc == IMX23_LRADC) {
1653                 iio->channels = mx23_lradc_chan_spec;
1654                 iio->num_channels = ARRAY_SIZE(mx23_lradc_chan_spec);
1655         } else {
1656                 iio->channels = mx28_lradc_chan_spec;
1657                 iio->num_channels = ARRAY_SIZE(mx28_lradc_chan_spec);
1658         }
1659
1660         ret = iio_triggered_buffer_setup(iio, &iio_pollfunc_store_time,
1661                                 &mxs_lradc_trigger_handler,
1662                                 &mxs_lradc_buffer_ops);
1663         if (ret)
1664                 goto err_clk;
1665
1666         ret = mxs_lradc_trigger_init(iio);
1667         if (ret)
1668                 goto err_trig;
1669
1670         /* Populate available ADC input ranges */
1671         for (i = 0; i < LRADC_MAX_TOTAL_CHANS; i++) {
1672                 for (s = 0; s < ARRAY_SIZE(lradc->scale_avail[i]); s++) {
1673                         /*
1674                          * [s=0] = optional divider by two disabled (default)
1675                          * [s=1] = optional divider by two enabled
1676                          *
1677                          * The scale is calculated by doing:
1678                          *   Vref >> (realbits - s)
1679                          * which multiplies by two on the second component
1680                          * of the array.
1681                          */
1682                         scale_uv = ((u64)lradc->vref_mv[i] * 100000000) >>
1683                                    (LRADC_RESOLUTION - s);
1684                         lradc->scale_avail[i][s].nano =
1685                                         do_div(scale_uv, 100000000) * 10;
1686                         lradc->scale_avail[i][s].integer = scale_uv;
1687                 }
1688         }
1689
1690         /* Configure the hardware. */
1691         ret = mxs_lradc_hw_init(lradc);
1692         if (ret)
1693                 goto err_dev;
1694
1695         /* Register the touchscreen input device. */
1696         if (touch_ret == 0) {
1697                 ret = mxs_lradc_ts_register(lradc);
1698                 if (ret)
1699                         goto err_ts_register;
1700         }
1701
1702         /* Register IIO device. */
1703         ret = iio_device_register(iio);
1704         if (ret) {
1705                 dev_err(dev, "Failed to register IIO device\n");
1706                 goto err_ts;
1707         }
1708
1709         return 0;
1710
1711 err_ts:
1712         mxs_lradc_ts_unregister(lradc);
1713 err_ts_register:
1714         mxs_lradc_hw_stop(lradc);
1715 err_dev:
1716         mxs_lradc_trigger_remove(iio);
1717 err_trig:
1718         iio_triggered_buffer_cleanup(iio);
1719 err_clk:
1720         clk_disable_unprepare(lradc->clk);
1721         return ret;
1722 }
1723
1724 static int mxs_lradc_remove(struct platform_device *pdev)
1725 {
1726         struct iio_dev *iio = platform_get_drvdata(pdev);
1727         struct mxs_lradc *lradc = iio_priv(iio);
1728
1729         iio_device_unregister(iio);
1730         mxs_lradc_ts_unregister(lradc);
1731         mxs_lradc_hw_stop(lradc);
1732         mxs_lradc_trigger_remove(iio);
1733         iio_triggered_buffer_cleanup(iio);
1734
1735         clk_disable_unprepare(lradc->clk);
1736         return 0;
1737 }
1738
1739 static struct platform_driver mxs_lradc_driver = {
1740         .driver = {
1741                 .name   = DRIVER_NAME,
1742                 .of_match_table = mxs_lradc_dt_ids,
1743         },
1744         .probe  = mxs_lradc_probe,
1745         .remove = mxs_lradc_remove,
1746 };
1747
1748 module_platform_driver(mxs_lradc_driver);
1749
1750 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1751 MODULE_DESCRIPTION("Freescale MXS LRADC driver");
1752 MODULE_LICENSE("GPL v2");
1753 MODULE_ALIAS("platform:" DRIVER_NAME);