2 * Freescale i.MX28 LRADC driver
4 * Copyright (c) 2012 DENX Software Engineering, GmbH.
5 * Marek Vasut <marex@denx.de>
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.
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.
18 #include <linux/err.h>
19 #include <linux/interrupt.h>
20 #include <linux/device.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
24 #include <linux/of_device.h>
25 #include <linux/sysfs.h>
26 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/spinlock.h>
31 #include <linux/wait.h>
32 #include <linux/sched.h>
33 #include <linux/stmp_device.h>
34 #include <linux/bitops.h>
35 #include <linux/completion.h>
36 #include <linux/delay.h>
37 #include <linux/input.h>
38 #include <linux/clk.h>
40 #include <linux/iio/iio.h>
41 #include <linux/iio/buffer.h>
42 #include <linux/iio/trigger.h>
43 #include <linux/iio/trigger_consumer.h>
44 #include <linux/iio/triggered_buffer.h>
46 #define DRIVER_NAME "mxs-lradc"
48 #define LRADC_MAX_DELAY_CHANS 4
49 #define LRADC_MAX_MAPPED_CHANS 8
50 #define LRADC_MAX_TOTAL_CHANS 16
52 #define LRADC_DELAY_TIMER_HZ 2000
55 * Make this runtime configurable if necessary. Currently, if the buffered mode
56 * is enabled, the LRADC takes LRADC_DELAY_TIMER_LOOP samples of data before
57 * triggering IRQ. The sampling happens every (LRADC_DELAY_TIMER_PER / 2000)
58 * seconds. The result is that the samples arrive every 500mS.
60 #define LRADC_DELAY_TIMER_PER 200
61 #define LRADC_DELAY_TIMER_LOOP 5
64 * Once the pen touches the touchscreen, the touchscreen switches from
65 * IRQ-driven mode to polling mode to prevent interrupt storm. The polling
66 * is realized by worker thread, which is called every 20 or so milliseconds.
67 * This gives the touchscreen enough fluence and does not strain the system
70 #define LRADC_TS_SAMPLE_DELAY_MS 5
73 * The LRADC reads the following amount of samples from each touchscreen
74 * channel and the driver then computes avarage of these.
76 #define LRADC_TS_SAMPLE_AMOUNT 4
83 static const char * const mx23_lradc_irq_names[] = {
84 "mxs-lradc-touchscreen",
95 static const char * const mx28_lradc_irq_names[] = {
96 "mxs-lradc-touchscreen",
100 "mxs-lradc-channel1",
101 "mxs-lradc-channel2",
102 "mxs-lradc-channel3",
103 "mxs-lradc-channel4",
104 "mxs-lradc-channel5",
105 "mxs-lradc-channel6",
106 "mxs-lradc-channel7",
111 struct mxs_lradc_of_config {
113 const char * const *irq_name;
116 static const struct mxs_lradc_of_config mxs_lradc_of_config[] = {
118 .irq_count = ARRAY_SIZE(mx23_lradc_irq_names),
119 .irq_name = mx23_lradc_irq_names,
122 .irq_count = ARRAY_SIZE(mx28_lradc_irq_names),
123 .irq_name = mx28_lradc_irq_names,
128 MXS_LRADC_TOUCHSCREEN_NONE = 0,
129 MXS_LRADC_TOUCHSCREEN_4WIRE,
130 MXS_LRADC_TOUCHSCREEN_5WIRE,
134 * Touchscreen handling
136 enum lradc_ts_plate {
140 LRADC_SAMPLE_PRESSURE,
152 struct iio_trigger *trig;
156 struct completion completion;
159 * Touchscreen LRADC channels receives a private slot in the CTRL4
160 * register, the slot #7. Therefore only 7 slots instead of 8 in the
161 * CTRL4 register can be mapped to LRADC channels when using the
164 * Furthermore, certain LRADC channels are shared between touchscreen
165 * and/or touch-buttons and generic LRADC block. Therefore when using
166 * either of these, these channels are not available for the regular
167 * sampling. The shared channels are as follows:
169 * CH0 -- Touch button #0
170 * CH1 -- Touch button #1
171 * CH2 -- Touch screen XPUL
172 * CH3 -- Touch screen YPLL
173 * CH4 -- Touch screen XNUL
174 * CH5 -- Touch screen YNLR
175 * CH6 -- Touch screen WIPER (5-wire only)
177 * The bitfields below represents which parts of the LRADC block are
178 * switched into special mode of operation. These channels can not
179 * be sampled as regular LRADC channels. The driver will refuse any
180 * attempt to sample these channels.
182 #define CHAN_MASK_TOUCHBUTTON (0x3 << 0)
183 #define CHAN_MASK_TOUCHSCREEN_4WIRE (0xf << 2)
184 #define CHAN_MASK_TOUCHSCREEN_5WIRE (0x1f << 2)
185 enum mxs_lradc_ts use_touchscreen;
186 bool use_touchbutton;
188 struct input_dev *ts_input;
190 enum mxs_lradc_id soc;
191 enum lradc_ts_plate cur_plate; /* statemachine */
195 unsigned ts_pressure;
197 /* handle touchscreen's physical behaviour */
198 /* samples per coordinate */
199 unsigned over_sample_cnt;
200 /* time clocks between samples */
201 unsigned over_sample_delay;
202 /* time in clocks to wait after the plates where switched */
203 unsigned settling_delay;
206 #define LRADC_CTRL0 0x00
207 # define LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE (1 << 23)
208 # define LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE (1 << 22)
209 # define LRADC_CTRL0_MX28_YNNSW /* YM */ (1 << 21)
210 # define LRADC_CTRL0_MX28_YPNSW /* YP */ (1 << 20)
211 # define LRADC_CTRL0_MX28_YPPSW /* YP */ (1 << 19)
212 # define LRADC_CTRL0_MX28_XNNSW /* XM */ (1 << 18)
213 # define LRADC_CTRL0_MX28_XNPSW /* XM */ (1 << 17)
214 # define LRADC_CTRL0_MX28_XPPSW /* XP */ (1 << 16)
216 # define LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE (1 << 20)
217 # define LRADC_CTRL0_MX23_YM (1 << 19)
218 # define LRADC_CTRL0_MX23_XM (1 << 18)
219 # define LRADC_CTRL0_MX23_YP (1 << 17)
220 # define LRADC_CTRL0_MX23_XP (1 << 16)
222 # define LRADC_CTRL0_MX28_PLATE_MASK \
223 (LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE | \
224 LRADC_CTRL0_MX28_YNNSW | LRADC_CTRL0_MX28_YPNSW | \
225 LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_XNNSW | \
226 LRADC_CTRL0_MX28_XNPSW | LRADC_CTRL0_MX28_XPPSW)
228 # define LRADC_CTRL0_MX23_PLATE_MASK \
229 (LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE | \
230 LRADC_CTRL0_MX23_YM | LRADC_CTRL0_MX23_XM | \
231 LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_XP)
233 #define LRADC_CTRL1 0x10
234 #define LRADC_CTRL1_TOUCH_DETECT_IRQ_EN (1 << 24)
235 #define LRADC_CTRL1_LRADC_IRQ_EN(n) (1 << ((n) + 16))
236 #define LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK (0x1fff << 16)
237 #define LRADC_CTRL1_MX23_LRADC_IRQ_EN_MASK (0x01ff << 16)
238 #define LRADC_CTRL1_LRADC_IRQ_EN_OFFSET 16
239 #define LRADC_CTRL1_TOUCH_DETECT_IRQ (1 << 8)
240 #define LRADC_CTRL1_LRADC_IRQ(n) (1 << (n))
241 #define LRADC_CTRL1_MX28_LRADC_IRQ_MASK 0x1fff
242 #define LRADC_CTRL1_MX23_LRADC_IRQ_MASK 0x01ff
243 #define LRADC_CTRL1_LRADC_IRQ_OFFSET 0
245 #define LRADC_CTRL2 0x20
246 #define LRADC_CTRL2_TEMPSENSE_PWD (1 << 15)
248 #define LRADC_STATUS 0x40
249 #define LRADC_STATUS_TOUCH_DETECT_RAW (1 << 0)
251 #define LRADC_CH(n) (0x50 + (0x10 * (n)))
252 #define LRADC_CH_ACCUMULATE (1 << 29)
253 #define LRADC_CH_NUM_SAMPLES_MASK (0x1f << 24)
254 #define LRADC_CH_NUM_SAMPLES_OFFSET 24
255 #define LRADC_CH_NUM_SAMPLES(x) \
256 ((x) << LRADC_CH_NUM_SAMPLES_OFFSET)
257 #define LRADC_CH_VALUE_MASK 0x3ffff
258 #define LRADC_CH_VALUE_OFFSET 0
260 #define LRADC_DELAY(n) (0xd0 + (0x10 * (n)))
261 #define LRADC_DELAY_TRIGGER_LRADCS_MASK (0xff << 24)
262 #define LRADC_DELAY_TRIGGER_LRADCS_OFFSET 24
263 #define LRADC_DELAY_TRIGGER(x) \
264 (((x) << LRADC_DELAY_TRIGGER_LRADCS_OFFSET) & \
265 LRADC_DELAY_TRIGGER_LRADCS_MASK)
266 #define LRADC_DELAY_KICK (1 << 20)
267 #define LRADC_DELAY_TRIGGER_DELAYS_MASK (0xf << 16)
268 #define LRADC_DELAY_TRIGGER_DELAYS_OFFSET 16
269 #define LRADC_DELAY_TRIGGER_DELAYS(x) \
270 (((x) << LRADC_DELAY_TRIGGER_DELAYS_OFFSET) & \
271 LRADC_DELAY_TRIGGER_DELAYS_MASK)
272 #define LRADC_DELAY_LOOP_COUNT_MASK (0x1f << 11)
273 #define LRADC_DELAY_LOOP_COUNT_OFFSET 11
274 #define LRADC_DELAY_LOOP(x) \
275 (((x) << LRADC_DELAY_LOOP_COUNT_OFFSET) & \
276 LRADC_DELAY_LOOP_COUNT_MASK)
277 #define LRADC_DELAY_DELAY_MASK 0x7ff
278 #define LRADC_DELAY_DELAY_OFFSET 0
279 #define LRADC_DELAY_DELAY(x) \
280 (((x) << LRADC_DELAY_DELAY_OFFSET) & \
281 LRADC_DELAY_DELAY_MASK)
283 #define LRADC_CTRL4 0x140
284 #define LRADC_CTRL4_LRADCSELECT_MASK(n) (0xf << ((n) * 4))
285 #define LRADC_CTRL4_LRADCSELECT_OFFSET(n) ((n) * 4)
287 #define LRADC_RESOLUTION 12
288 #define LRADC_SINGLE_SAMPLE_MASK ((1 << LRADC_RESOLUTION) - 1)
290 static void mxs_lradc_reg_set(struct mxs_lradc *lradc, u32 val, u32 reg)
292 writel(val, lradc->base + reg + STMP_OFFSET_REG_SET);
295 static void mxs_lradc_reg_clear(struct mxs_lradc *lradc, u32 val, u32 reg)
297 writel(val, lradc->base + reg + STMP_OFFSET_REG_CLR);
300 static void mxs_lradc_reg_wrt(struct mxs_lradc *lradc, u32 val, u32 reg)
302 writel(val, lradc->base + reg);
305 static u32 mxs_lradc_plate_mask(struct mxs_lradc *lradc)
307 if (lradc->soc == IMX23_LRADC)
308 return LRADC_CTRL0_MX23_PLATE_MASK;
310 return LRADC_CTRL0_MX28_PLATE_MASK;
313 static u32 mxs_lradc_irq_en_mask(struct mxs_lradc *lradc)
315 if (lradc->soc == IMX23_LRADC)
316 return LRADC_CTRL1_MX23_LRADC_IRQ_EN_MASK;
318 return LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK;
321 static u32 mxs_lradc_irq_mask(struct mxs_lradc *lradc)
323 if (lradc->soc == IMX23_LRADC)
324 return LRADC_CTRL1_MX23_LRADC_IRQ_MASK;
326 return LRADC_CTRL1_MX28_LRADC_IRQ_MASK;
329 static u32 mxs_lradc_touch_detect_bit(struct mxs_lradc *lradc)
331 if (lradc->soc == IMX23_LRADC)
332 return LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE;
334 return LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE;
337 static u32 mxs_lradc_drive_x_plate(struct mxs_lradc *lradc)
339 if (lradc->soc == IMX23_LRADC)
340 return LRADC_CTRL0_MX23_XP | LRADC_CTRL0_MX23_XM;
342 return LRADC_CTRL0_MX28_XPPSW | LRADC_CTRL0_MX28_XNNSW;
345 static u32 mxs_lradc_drive_y_plate(struct mxs_lradc *lradc)
347 if (lradc->soc == IMX23_LRADC)
348 return LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_YM;
350 return LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_YNNSW;
353 static u32 mxs_lradc_drive_pressure(struct mxs_lradc *lradc)
355 if (lradc->soc == IMX23_LRADC)
356 return LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_XM;
358 return LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_XNNSW;
361 static bool mxs_lradc_check_touch_event(struct mxs_lradc *lradc)
363 return !!(readl(lradc->base + LRADC_STATUS) &
364 LRADC_STATUS_TOUCH_DETECT_RAW);
367 static void mxs_lradc_setup_ts_channel(struct mxs_lradc *lradc, unsigned ch)
370 * prepare for oversampling conversion
372 * from the datasheet:
373 * "The ACCUMULATE bit in the appropriate channel register
374 * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0;
375 * otherwise, the IRQs will not fire."
377 mxs_lradc_reg_wrt(lradc, LRADC_CH_ACCUMULATE |
378 LRADC_CH_NUM_SAMPLES(lradc->over_sample_cnt - 1),
381 /* from the datasheet:
382 * "Software must clear this register in preparation for a
383 * multi-cycle accumulation.
385 mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch));
387 /* prepare the delay/loop unit according to the oversampling count */
388 mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << ch) |
389 LRADC_DELAY_TRIGGER_DELAYS(0) |
390 LRADC_DELAY_LOOP(lradc->over_sample_cnt - 1) |
391 LRADC_DELAY_DELAY(lradc->over_sample_delay - 1),
394 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(2) |
395 LRADC_CTRL1_LRADC_IRQ(3) | LRADC_CTRL1_LRADC_IRQ(4) |
396 LRADC_CTRL1_LRADC_IRQ(5), LRADC_CTRL1);
398 /* wake us again, when the complete conversion is done */
399 mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(ch), LRADC_CTRL1);
401 * after changing the touchscreen plates setting
402 * the signals need some initial time to settle. Start the
403 * SoC's delay unit and start the conversion later
406 mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(0) | /* don't trigger ADC */
407 LRADC_DELAY_TRIGGER_DELAYS(1 << 3) | /* trigger DELAY unit#3 */
409 LRADC_DELAY_DELAY(lradc->settling_delay),
414 * Pressure detection is special:
415 * We want to do both required measurements for the pressure detection in
416 * one turn. Use the hardware features to chain both conversions and let the
417 * hardware report one interrupt if both conversions are done
419 static void mxs_lradc_setup_ts_pressure(struct mxs_lradc *lradc, unsigned ch1,
425 * prepare for oversampling conversion
427 * from the datasheet:
428 * "The ACCUMULATE bit in the appropriate channel register
429 * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0;
430 * otherwise, the IRQs will not fire."
432 reg = LRADC_CH_ACCUMULATE |
433 LRADC_CH_NUM_SAMPLES(lradc->over_sample_cnt - 1);
434 mxs_lradc_reg_wrt(lradc, reg, LRADC_CH(ch1));
435 mxs_lradc_reg_wrt(lradc, reg, LRADC_CH(ch2));
437 /* from the datasheet:
438 * "Software must clear this register in preparation for a
439 * multi-cycle accumulation.
441 mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch1));
442 mxs_lradc_reg_clear(lradc, LRADC_CH_VALUE_MASK, LRADC_CH(ch2));
444 /* prepare the delay/loop unit according to the oversampling count */
445 mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << ch1) |
446 LRADC_DELAY_TRIGGER(1 << ch2) | /* start both channels */
447 LRADC_DELAY_TRIGGER_DELAYS(0) |
448 LRADC_DELAY_LOOP(lradc->over_sample_cnt - 1) |
449 LRADC_DELAY_DELAY(lradc->over_sample_delay - 1),
452 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(2) |
453 LRADC_CTRL1_LRADC_IRQ(3) | LRADC_CTRL1_LRADC_IRQ(4) |
454 LRADC_CTRL1_LRADC_IRQ(5), LRADC_CTRL1);
456 /* wake us again, when the conversions are done */
457 mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(ch2), LRADC_CTRL1);
459 * after changing the touchscreen plates setting
460 * the signals need some initial time to settle. Start the
461 * SoC's delay unit and start the conversion later
464 mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(0) | /* don't trigger ADC */
465 LRADC_DELAY_TRIGGER_DELAYS(1 << 3) | /* trigger DELAY unit#3 */
467 LRADC_DELAY_DELAY(lradc->settling_delay), LRADC_DELAY(2));
470 static unsigned mxs_lradc_read_raw_channel(struct mxs_lradc *lradc,
474 unsigned num_samples, val;
476 reg = readl(lradc->base + LRADC_CH(channel));
477 if (reg & LRADC_CH_ACCUMULATE)
478 num_samples = lradc->over_sample_cnt;
482 val = (reg & LRADC_CH_VALUE_MASK) >> LRADC_CH_VALUE_OFFSET;
483 return val / num_samples;
486 static unsigned mxs_lradc_read_ts_pressure(struct mxs_lradc *lradc,
487 unsigned ch1, unsigned ch2)
490 unsigned pressure, m1, m2;
492 mask = LRADC_CTRL1_LRADC_IRQ(ch1) | LRADC_CTRL1_LRADC_IRQ(ch2);
493 reg = readl(lradc->base + LRADC_CTRL1) & mask;
495 while (reg != mask) {
496 reg = readl(lradc->base + LRADC_CTRL1) & mask;
497 dev_dbg(lradc->dev, "One channel is still busy: %X\n", reg);
500 m1 = mxs_lradc_read_raw_channel(lradc, ch1);
501 m2 = mxs_lradc_read_raw_channel(lradc, ch2);
504 dev_warn(lradc->dev, "Cannot calculate pressure\n");
505 return 1 << (LRADC_RESOLUTION - 1);
508 /* simply scale the value from 0 ... max ADC resolution */
510 pressure *= (1 << LRADC_RESOLUTION);
513 dev_dbg(lradc->dev, "Pressure = %u\n", pressure);
522 static int mxs_lradc_read_ts_channel(struct mxs_lradc *lradc)
527 reg = readl(lradc->base + LRADC_CTRL1);
529 /* only channels 3 to 5 are of interest here */
530 if (reg & LRADC_CTRL1_LRADC_IRQ(TS_CH_YP)) {
531 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(TS_CH_YP) |
532 LRADC_CTRL1_LRADC_IRQ(TS_CH_YP), LRADC_CTRL1);
533 val = mxs_lradc_read_raw_channel(lradc, TS_CH_YP);
534 } else if (reg & LRADC_CTRL1_LRADC_IRQ(TS_CH_XM)) {
535 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(TS_CH_XM) |
536 LRADC_CTRL1_LRADC_IRQ(TS_CH_XM), LRADC_CTRL1);
537 val = mxs_lradc_read_raw_channel(lradc, TS_CH_XM);
538 } else if (reg & LRADC_CTRL1_LRADC_IRQ(TS_CH_YM)) {
539 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(TS_CH_YM) |
540 LRADC_CTRL1_LRADC_IRQ(TS_CH_YM), LRADC_CTRL1);
541 val = mxs_lradc_read_raw_channel(lradc, TS_CH_YM);
546 mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2));
547 mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3));
553 * YP(open)--+-------------+
556 * YM(-)--+-------------+ |
561 * "weak+" means 200k Ohm VDDIO
564 static void mxs_lradc_setup_touch_detection(struct mxs_lradc *lradc)
567 * In order to detect a touch event the 'touch detect enable' bit
569 * - a weak pullup to the X+ connector
570 * - a strong ground at the Y- connector
572 mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
573 mxs_lradc_reg_set(lradc, mxs_lradc_touch_detect_bit(lradc),
578 * YP(meas)--+-------------+
581 * YM(open)--+-------------+ |
586 * (+) means here 1.85 V
589 static void mxs_lradc_prepare_x_pos(struct mxs_lradc *lradc)
591 mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
592 mxs_lradc_reg_set(lradc, mxs_lradc_drive_x_plate(lradc), LRADC_CTRL0);
594 lradc->cur_plate = LRADC_SAMPLE_X;
595 mxs_lradc_setup_ts_channel(lradc, TS_CH_YP);
599 * YP(+)--+-------------+
602 * YM(-)--+-------------+ |
607 * (+) means here 1.85 V
610 static void mxs_lradc_prepare_y_pos(struct mxs_lradc *lradc)
612 mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
613 mxs_lradc_reg_set(lradc, mxs_lradc_drive_y_plate(lradc), LRADC_CTRL0);
615 lradc->cur_plate = LRADC_SAMPLE_Y;
616 mxs_lradc_setup_ts_channel(lradc, TS_CH_XM);
620 * YP(+)--+-------------+
623 * YM(meas)--+-------------+ |
628 * (+) means here 1.85 V
631 static void mxs_lradc_prepare_pressure(struct mxs_lradc *lradc)
633 mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
634 mxs_lradc_reg_set(lradc, mxs_lradc_drive_pressure(lradc), LRADC_CTRL0);
636 lradc->cur_plate = LRADC_SAMPLE_PRESSURE;
637 mxs_lradc_setup_ts_pressure(lradc, TS_CH_XP, TS_CH_YM);
640 static void mxs_lradc_enable_touch_detection(struct mxs_lradc *lradc)
642 mxs_lradc_setup_touch_detection(lradc);
644 lradc->cur_plate = LRADC_TOUCH;
645 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ |
646 LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1);
647 mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1);
650 static void mxs_lradc_report_ts_event(struct mxs_lradc *lradc)
652 input_report_abs(lradc->ts_input, ABS_X, lradc->ts_x_pos);
653 input_report_abs(lradc->ts_input, ABS_Y, lradc->ts_y_pos);
654 input_report_abs(lradc->ts_input, ABS_PRESSURE, lradc->ts_pressure);
655 input_report_key(lradc->ts_input, BTN_TOUCH, 1);
656 input_sync(lradc->ts_input);
659 static void mxs_lradc_complete_touch_event(struct mxs_lradc *lradc)
661 mxs_lradc_setup_touch_detection(lradc);
662 lradc->cur_plate = LRADC_SAMPLE_VALID;
664 * start a dummy conversion to burn time to settle the signals
665 * note: we are not interested in the conversion's value
667 mxs_lradc_reg_wrt(lradc, 0, LRADC_CH(5));
668 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ(5), LRADC_CTRL1);
669 mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(5), LRADC_CTRL1);
670 mxs_lradc_reg_wrt(lradc, LRADC_DELAY_TRIGGER(1 << 5) |
671 LRADC_DELAY_KICK | LRADC_DELAY_DELAY(10), /* waste 5 ms */
676 * in order to avoid false measurements, report only samples where
677 * the surface is still touched after the position measurement
679 static void mxs_lradc_finish_touch_event(struct mxs_lradc *lradc, bool valid)
681 /* if it is still touched, report the sample */
682 if (valid && mxs_lradc_check_touch_event(lradc)) {
683 lradc->ts_valid = true;
684 mxs_lradc_report_ts_event(lradc);
687 /* if it is even still touched, continue with the next measurement */
688 if (mxs_lradc_check_touch_event(lradc)) {
689 mxs_lradc_prepare_y_pos(lradc);
693 if (lradc->ts_valid) {
694 /* signal the release */
695 lradc->ts_valid = false;
696 input_report_key(lradc->ts_input, BTN_TOUCH, 0);
697 input_sync(lradc->ts_input);
700 /* if it is released, wait for the next touch via IRQ */
701 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ, LRADC_CTRL1);
702 mxs_lradc_reg_set(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN, LRADC_CTRL1);
705 /* touchscreen's state machine */
706 static void mxs_lradc_handle_touch(struct mxs_lradc *lradc)
710 switch (lradc->cur_plate) {
713 * start with the Y-pos, because it uses nearly the same plate
714 * settings like the touch detection
716 if (mxs_lradc_check_touch_event(lradc)) {
717 mxs_lradc_reg_clear(lradc,
718 LRADC_CTRL1_TOUCH_DETECT_IRQ_EN,
720 mxs_lradc_prepare_y_pos(lradc);
722 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ,
727 val = mxs_lradc_read_ts_channel(lradc);
729 mxs_lradc_enable_touch_detection(lradc); /* re-start */
732 lradc->ts_y_pos = val;
733 mxs_lradc_prepare_x_pos(lradc);
737 val = mxs_lradc_read_ts_channel(lradc);
739 mxs_lradc_enable_touch_detection(lradc); /* re-start */
742 lradc->ts_x_pos = val;
743 mxs_lradc_prepare_pressure(lradc);
746 case LRADC_SAMPLE_PRESSURE:
748 mxs_lradc_read_ts_pressure(lradc, TS_CH_XP, TS_CH_YM);
749 mxs_lradc_complete_touch_event(lradc);
752 case LRADC_SAMPLE_VALID:
753 val = mxs_lradc_read_ts_channel(lradc); /* ignore the value */
754 mxs_lradc_finish_touch_event(lradc, 1);
762 static int mxs_lradc_read_raw(struct iio_dev *iio_dev,
763 const struct iio_chan_spec *chan,
764 int *val, int *val2, long m)
766 struct mxs_lradc *lradc = iio_priv(iio_dev);
769 if (m != IIO_CHAN_INFO_RAW)
772 /* Check for invalid channel */
773 if (chan->channel > LRADC_MAX_TOTAL_CHANS)
777 * See if there is no buffered operation in progess. If there is, simply
778 * bail out. This can be improved to support both buffered and raw IO at
779 * the same time, yet the code becomes horribly complicated. Therefore I
780 * applied KISS principle here.
782 ret = mutex_trylock(&lradc->lock);
786 INIT_COMPLETION(lradc->completion);
789 * No buffered operation in progress, map the channel and trigger it.
790 * Virtual channel 0 is always used here as the others are always not
791 * used if doing raw sampling.
793 if (lradc->soc == IMX28_LRADC)
794 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK,
796 mxs_lradc_reg_clear(lradc, 0xff, LRADC_CTRL0);
798 /* Clean the slot's previous content, then set new one. */
799 mxs_lradc_reg_clear(lradc, LRADC_CTRL4_LRADCSELECT_MASK(0), LRADC_CTRL4);
800 mxs_lradc_reg_set(lradc, chan->channel, LRADC_CTRL4);
802 mxs_lradc_reg_wrt(lradc, 0, LRADC_CH(0));
804 /* Enable the IRQ and start sampling the channel. */
805 mxs_lradc_reg_set(lradc, LRADC_CTRL1_LRADC_IRQ_EN(0), LRADC_CTRL1);
806 mxs_lradc_reg_set(lradc, 1 << 0, LRADC_CTRL0);
808 /* Wait for completion on the channel, 1 second max. */
809 ret = wait_for_completion_killable_timeout(&lradc->completion, HZ);
816 *val = readl(lradc->base + LRADC_CH(0)) & LRADC_CH_VALUE_MASK;
820 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_LRADC_IRQ_EN(0), LRADC_CTRL1);
822 mutex_unlock(&lradc->lock);
827 static const struct iio_info mxs_lradc_iio_info = {
828 .driver_module = THIS_MODULE,
829 .read_raw = mxs_lradc_read_raw,
832 static int mxs_lradc_ts_open(struct input_dev *dev)
834 struct mxs_lradc *lradc = input_get_drvdata(dev);
836 /* Enable the touch-detect circuitry. */
837 mxs_lradc_enable_touch_detection(lradc);
842 static void mxs_lradc_disable_ts(struct mxs_lradc *lradc)
844 /* stop all interrupts from firing */
845 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_TOUCH_DETECT_IRQ_EN |
846 LRADC_CTRL1_LRADC_IRQ_EN(2) | LRADC_CTRL1_LRADC_IRQ_EN(3) |
847 LRADC_CTRL1_LRADC_IRQ_EN(4) | LRADC_CTRL1_LRADC_IRQ_EN(5),
850 /* Power-down touchscreen touch-detect circuitry. */
851 mxs_lradc_reg_clear(lradc, mxs_lradc_plate_mask(lradc), LRADC_CTRL0);
854 static void mxs_lradc_ts_close(struct input_dev *dev)
856 struct mxs_lradc *lradc = input_get_drvdata(dev);
858 mxs_lradc_disable_ts(lradc);
861 static int mxs_lradc_ts_register(struct mxs_lradc *lradc)
863 struct input_dev *input;
864 struct device *dev = lradc->dev;
867 if (!lradc->use_touchscreen)
870 input = input_allocate_device();
872 dev_err(dev, "Failed to allocate TS device!\n");
876 input->name = DRIVER_NAME;
877 input->id.bustype = BUS_HOST;
878 input->dev.parent = dev;
879 input->open = mxs_lradc_ts_open;
880 input->close = mxs_lradc_ts_close;
882 __set_bit(EV_ABS, input->evbit);
883 __set_bit(EV_KEY, input->evbit);
884 __set_bit(BTN_TOUCH, input->keybit);
885 input_set_abs_params(input, ABS_X, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0);
886 input_set_abs_params(input, ABS_Y, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0);
887 input_set_abs_params(input, ABS_PRESSURE, 0, LRADC_SINGLE_SAMPLE_MASK,
890 lradc->ts_input = input;
891 input_set_drvdata(input, lradc);
892 ret = input_register_device(input);
894 input_free_device(lradc->ts_input);
899 static void mxs_lradc_ts_unregister(struct mxs_lradc *lradc)
901 if (!lradc->use_touchscreen)
904 mxs_lradc_disable_ts(lradc);
905 input_unregister_device(lradc->ts_input);
911 static irqreturn_t mxs_lradc_handle_irq(int irq, void *data)
913 struct iio_dev *iio = data;
914 struct mxs_lradc *lradc = iio_priv(iio);
915 unsigned long reg = readl(lradc->base + LRADC_CTRL1);
916 const uint32_t ts_irq_mask =
917 LRADC_CTRL1_TOUCH_DETECT_IRQ |
918 LRADC_CTRL1_LRADC_IRQ(2) |
919 LRADC_CTRL1_LRADC_IRQ(3) |
920 LRADC_CTRL1_LRADC_IRQ(4) |
921 LRADC_CTRL1_LRADC_IRQ(5);
923 if (!(reg & mxs_lradc_irq_mask(lradc)))
926 if (lradc->use_touchscreen && (reg & ts_irq_mask))
927 mxs_lradc_handle_touch(lradc);
929 if (iio_buffer_enabled(iio))
930 iio_trigger_poll(iio->trig, iio_get_time_ns());
931 else if (reg & LRADC_CTRL1_LRADC_IRQ(0))
932 complete(&lradc->completion);
934 mxs_lradc_reg_clear(lradc, reg & mxs_lradc_irq_mask(lradc), LRADC_CTRL1);
942 static irqreturn_t mxs_lradc_trigger_handler(int irq, void *p)
944 struct iio_poll_func *pf = p;
945 struct iio_dev *iio = pf->indio_dev;
946 struct mxs_lradc *lradc = iio_priv(iio);
947 const uint32_t chan_value = LRADC_CH_ACCUMULATE |
948 ((LRADC_DELAY_TIMER_LOOP - 1) << LRADC_CH_NUM_SAMPLES_OFFSET);
949 unsigned int i, j = 0;
951 for_each_set_bit(i, iio->active_scan_mask, LRADC_MAX_TOTAL_CHANS) {
952 lradc->buffer[j] = readl(lradc->base + LRADC_CH(j));
953 mxs_lradc_reg_wrt(lradc, chan_value, LRADC_CH(j));
954 lradc->buffer[j] &= LRADC_CH_VALUE_MASK;
955 lradc->buffer[j] /= LRADC_DELAY_TIMER_LOOP;
959 iio_push_to_buffers_with_timestamp(iio, lradc->buffer, pf->timestamp);
961 iio_trigger_notify_done(iio->trig);
966 static int mxs_lradc_configure_trigger(struct iio_trigger *trig, bool state)
968 struct iio_dev *iio = iio_trigger_get_drvdata(trig);
969 struct mxs_lradc *lradc = iio_priv(iio);
970 const uint32_t st = state ? STMP_OFFSET_REG_SET : STMP_OFFSET_REG_CLR;
972 mxs_lradc_reg_wrt(lradc, LRADC_DELAY_KICK, LRADC_DELAY(0) + st);
977 static const struct iio_trigger_ops mxs_lradc_trigger_ops = {
978 .owner = THIS_MODULE,
979 .set_trigger_state = &mxs_lradc_configure_trigger,
982 static int mxs_lradc_trigger_init(struct iio_dev *iio)
985 struct iio_trigger *trig;
986 struct mxs_lradc *lradc = iio_priv(iio);
988 trig = iio_trigger_alloc("%s-dev%i", iio->name, iio->id);
992 trig->dev.parent = lradc->dev;
993 iio_trigger_set_drvdata(trig, iio);
994 trig->ops = &mxs_lradc_trigger_ops;
996 ret = iio_trigger_register(trig);
998 iio_trigger_free(trig);
1007 static void mxs_lradc_trigger_remove(struct iio_dev *iio)
1009 struct mxs_lradc *lradc = iio_priv(iio);
1011 iio_trigger_unregister(lradc->trig);
1012 iio_trigger_free(lradc->trig);
1015 static int mxs_lradc_buffer_preenable(struct iio_dev *iio)
1017 struct mxs_lradc *lradc = iio_priv(iio);
1018 int ret = 0, chan, ofs = 0;
1019 unsigned long enable = 0;
1020 uint32_t ctrl4_set = 0;
1021 uint32_t ctrl4_clr = 0;
1022 uint32_t ctrl1_irq = 0;
1023 const uint32_t chan_value = LRADC_CH_ACCUMULATE |
1024 ((LRADC_DELAY_TIMER_LOOP - 1) << LRADC_CH_NUM_SAMPLES_OFFSET);
1025 const int len = bitmap_weight(iio->active_scan_mask, LRADC_MAX_TOTAL_CHANS);
1031 * Lock the driver so raw access can not be done during buffered
1032 * operation. This simplifies the code a lot.
1034 ret = mutex_trylock(&lradc->lock);
1038 lradc->buffer = kmalloc(len * sizeof(*lradc->buffer), GFP_KERNEL);
1039 if (!lradc->buffer) {
1044 if (lradc->soc == IMX28_LRADC)
1045 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK,
1047 mxs_lradc_reg_clear(lradc, 0xff, LRADC_CTRL0);
1049 for_each_set_bit(chan, iio->active_scan_mask, LRADC_MAX_TOTAL_CHANS) {
1050 ctrl4_set |= chan << LRADC_CTRL4_LRADCSELECT_OFFSET(ofs);
1051 ctrl4_clr |= LRADC_CTRL4_LRADCSELECT_MASK(ofs);
1052 ctrl1_irq |= LRADC_CTRL1_LRADC_IRQ_EN(ofs);
1053 mxs_lradc_reg_wrt(lradc, chan_value, LRADC_CH(ofs));
1054 bitmap_set(&enable, ofs, 1);
1058 mxs_lradc_reg_clear(lradc, LRADC_DELAY_TRIGGER_LRADCS_MASK |
1059 LRADC_DELAY_KICK, LRADC_DELAY(0));
1060 mxs_lradc_reg_clear(lradc, ctrl4_clr, LRADC_CTRL4);
1061 mxs_lradc_reg_set(lradc, ctrl4_set, LRADC_CTRL4);
1062 mxs_lradc_reg_set(lradc, ctrl1_irq, LRADC_CTRL1);
1063 mxs_lradc_reg_set(lradc, enable << LRADC_DELAY_TRIGGER_LRADCS_OFFSET,
1069 mutex_unlock(&lradc->lock);
1073 static int mxs_lradc_buffer_postdisable(struct iio_dev *iio)
1075 struct mxs_lradc *lradc = iio_priv(iio);
1077 mxs_lradc_reg_clear(lradc, LRADC_DELAY_TRIGGER_LRADCS_MASK |
1078 LRADC_DELAY_KICK, LRADC_DELAY(0));
1080 mxs_lradc_reg_clear(lradc, 0xff, LRADC_CTRL0);
1081 if (lradc->soc == IMX28_LRADC)
1082 mxs_lradc_reg_clear(lradc, LRADC_CTRL1_MX28_LRADC_IRQ_EN_MASK,
1085 kfree(lradc->buffer);
1086 mutex_unlock(&lradc->lock);
1091 static bool mxs_lradc_validate_scan_mask(struct iio_dev *iio,
1092 const unsigned long *mask)
1094 struct mxs_lradc *lradc = iio_priv(iio);
1095 const int map_chans = bitmap_weight(mask, LRADC_MAX_TOTAL_CHANS);
1097 unsigned long rsvd_mask = 0;
1099 if (lradc->use_touchbutton)
1100 rsvd_mask |= CHAN_MASK_TOUCHBUTTON;
1101 if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_4WIRE)
1102 rsvd_mask |= CHAN_MASK_TOUCHSCREEN_4WIRE;
1103 if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE)
1104 rsvd_mask |= CHAN_MASK_TOUCHSCREEN_5WIRE;
1106 if (lradc->use_touchbutton)
1108 if (lradc->use_touchscreen)
1111 /* Test for attempts to map channels with special mode of operation. */
1112 if (bitmap_intersects(mask, &rsvd_mask, LRADC_MAX_TOTAL_CHANS))
1115 /* Test for attempts to map more channels then available slots. */
1116 if (map_chans + rsvd_chans > LRADC_MAX_MAPPED_CHANS)
1122 static const struct iio_buffer_setup_ops mxs_lradc_buffer_ops = {
1123 .preenable = &mxs_lradc_buffer_preenable,
1124 .postenable = &iio_triggered_buffer_postenable,
1125 .predisable = &iio_triggered_buffer_predisable,
1126 .postdisable = &mxs_lradc_buffer_postdisable,
1127 .validate_scan_mask = &mxs_lradc_validate_scan_mask,
1131 * Driver initialization
1134 #define MXS_ADC_CHAN(idx, chan_type) { \
1135 .type = (chan_type), \
1137 .scan_index = (idx), \
1138 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1142 .realbits = LRADC_RESOLUTION, \
1143 .storagebits = 32, \
1147 static const struct iio_chan_spec mxs_lradc_chan_spec[] = {
1148 MXS_ADC_CHAN(0, IIO_VOLTAGE),
1149 MXS_ADC_CHAN(1, IIO_VOLTAGE),
1150 MXS_ADC_CHAN(2, IIO_VOLTAGE),
1151 MXS_ADC_CHAN(3, IIO_VOLTAGE),
1152 MXS_ADC_CHAN(4, IIO_VOLTAGE),
1153 MXS_ADC_CHAN(5, IIO_VOLTAGE),
1154 MXS_ADC_CHAN(6, IIO_VOLTAGE),
1155 MXS_ADC_CHAN(7, IIO_VOLTAGE), /* VBATT */
1156 MXS_ADC_CHAN(8, IIO_TEMP), /* Temp sense 0 */
1157 MXS_ADC_CHAN(9, IIO_TEMP), /* Temp sense 1 */
1158 MXS_ADC_CHAN(10, IIO_VOLTAGE), /* VDDIO */
1159 MXS_ADC_CHAN(11, IIO_VOLTAGE), /* VTH */
1160 MXS_ADC_CHAN(12, IIO_VOLTAGE), /* VDDA */
1161 MXS_ADC_CHAN(13, IIO_VOLTAGE), /* VDDD */
1162 MXS_ADC_CHAN(14, IIO_VOLTAGE), /* VBG */
1163 MXS_ADC_CHAN(15, IIO_VOLTAGE), /* VDD5V */
1166 static int mxs_lradc_hw_init(struct mxs_lradc *lradc)
1168 /* The ADC always uses DELAY CHANNEL 0. */
1169 const uint32_t adc_cfg =
1170 (1 << (LRADC_DELAY_TRIGGER_DELAYS_OFFSET + 0)) |
1171 (LRADC_DELAY_TIMER_PER << LRADC_DELAY_DELAY_OFFSET);
1173 int ret = stmp_reset_block(lradc->base);
1177 /* Configure DELAY CHANNEL 0 for generic ADC sampling. */
1178 mxs_lradc_reg_wrt(lradc, adc_cfg, LRADC_DELAY(0));
1180 /* Disable remaining DELAY CHANNELs */
1181 mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(1));
1182 mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2));
1183 mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3));
1185 /* Configure the touchscreen type */
1186 if (lradc->soc == IMX28_LRADC) {
1187 mxs_lradc_reg_clear(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
1190 if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE)
1191 mxs_lradc_reg_set(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
1195 /* Start internal temperature sensing. */
1196 mxs_lradc_reg_wrt(lradc, 0, LRADC_CTRL2);
1201 static void mxs_lradc_hw_stop(struct mxs_lradc *lradc)
1205 mxs_lradc_reg_clear(lradc, mxs_lradc_irq_en_mask(lradc), LRADC_CTRL1);
1207 for (i = 0; i < LRADC_MAX_DELAY_CHANS; i++)
1208 mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(i));
1211 static const struct of_device_id mxs_lradc_dt_ids[] = {
1212 { .compatible = "fsl,imx23-lradc", .data = (void *)IMX23_LRADC, },
1213 { .compatible = "fsl,imx28-lradc", .data = (void *)IMX28_LRADC, },
1216 MODULE_DEVICE_TABLE(of, mxs_lradc_dt_ids);
1218 static int mxs_lradc_probe_touchscreen(struct mxs_lradc *lradc,
1219 struct device_node *lradc_node)
1222 u32 ts_wires = 0, adapt;
1224 ret = of_property_read_u32(lradc_node, "fsl,lradc-touchscreen-wires",
1227 return -ENODEV; /* touchscreen feature disabled */
1231 lradc->use_touchscreen = MXS_LRADC_TOUCHSCREEN_4WIRE;
1234 if (lradc->soc == IMX28_LRADC) {
1235 lradc->use_touchscreen = MXS_LRADC_TOUCHSCREEN_5WIRE;
1238 /* fall through an error message for i.MX23 */
1241 "Unsupported number of touchscreen wires (%d)\n",
1246 lradc->over_sample_cnt = 4;
1247 ret = of_property_read_u32(lradc_node, "fsl,ave-ctrl", &adapt);
1249 lradc->over_sample_cnt = adapt;
1251 lradc->over_sample_delay = 2;
1252 ret = of_property_read_u32(lradc_node, "fsl,ave-delay", &adapt);
1254 lradc->over_sample_delay = adapt;
1256 lradc->settling_delay = 10;
1257 ret = of_property_read_u32(lradc_node, "fsl,settling", &adapt);
1259 lradc->settling_delay = adapt;
1264 static int mxs_lradc_probe(struct platform_device *pdev)
1266 const struct of_device_id *of_id =
1267 of_match_device(mxs_lradc_dt_ids, &pdev->dev);
1268 const struct mxs_lradc_of_config *of_cfg =
1269 &mxs_lradc_of_config[(enum mxs_lradc_id)of_id->data];
1270 struct device *dev = &pdev->dev;
1271 struct device_node *node = dev->of_node;
1272 struct mxs_lradc *lradc;
1273 struct iio_dev *iio;
1274 struct resource *iores;
1275 int ret = 0, touch_ret;
1278 /* Allocate the IIO device. */
1279 iio = devm_iio_device_alloc(dev, sizeof(*lradc));
1281 dev_err(dev, "Failed to allocate IIO device\n");
1285 lradc = iio_priv(iio);
1286 lradc->soc = (enum mxs_lradc_id)of_id->data;
1288 /* Grab the memory area */
1289 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1290 lradc->dev = &pdev->dev;
1291 lradc->base = devm_ioremap_resource(dev, iores);
1292 if (IS_ERR(lradc->base))
1293 return PTR_ERR(lradc->base);
1295 lradc->clk = devm_clk_get(&pdev->dev, NULL);
1296 if (IS_ERR(lradc->clk)) {
1297 dev_err(dev, "Failed to get the delay unit clock\n");
1298 return PTR_ERR(lradc->clk);
1300 ret = clk_prepare_enable(lradc->clk);
1302 dev_err(dev, "Failed to enable the delay unit clock\n");
1306 touch_ret = mxs_lradc_probe_touchscreen(lradc, node);
1308 /* Grab all IRQ sources */
1309 for (i = 0; i < of_cfg->irq_count; i++) {
1310 lradc->irq[i] = platform_get_irq(pdev, i);
1311 if (lradc->irq[i] < 0)
1314 ret = devm_request_irq(dev, lradc->irq[i],
1315 mxs_lradc_handle_irq, 0,
1316 of_cfg->irq_name[i], iio);
1321 platform_set_drvdata(pdev, iio);
1323 init_completion(&lradc->completion);
1324 mutex_init(&lradc->lock);
1326 iio->name = pdev->name;
1327 iio->dev.parent = &pdev->dev;
1328 iio->info = &mxs_lradc_iio_info;
1329 iio->modes = INDIO_DIRECT_MODE;
1330 iio->channels = mxs_lradc_chan_spec;
1331 iio->num_channels = ARRAY_SIZE(mxs_lradc_chan_spec);
1332 iio->masklength = LRADC_MAX_TOTAL_CHANS;
1334 ret = iio_triggered_buffer_setup(iio, &iio_pollfunc_store_time,
1335 &mxs_lradc_trigger_handler,
1336 &mxs_lradc_buffer_ops);
1340 ret = mxs_lradc_trigger_init(iio);
1344 /* Configure the hardware. */
1345 ret = mxs_lradc_hw_init(lradc);
1349 /* Register the touchscreen input device. */
1350 if (touch_ret == 0) {
1351 ret = mxs_lradc_ts_register(lradc);
1353 goto err_ts_register;
1356 /* Register IIO device. */
1357 ret = iio_device_register(iio);
1359 dev_err(dev, "Failed to register IIO device\n");
1366 mxs_lradc_ts_unregister(lradc);
1368 mxs_lradc_hw_stop(lradc);
1370 mxs_lradc_trigger_remove(iio);
1372 iio_triggered_buffer_cleanup(iio);
1376 static int mxs_lradc_remove(struct platform_device *pdev)
1378 struct iio_dev *iio = platform_get_drvdata(pdev);
1379 struct mxs_lradc *lradc = iio_priv(iio);
1381 iio_device_unregister(iio);
1382 mxs_lradc_ts_unregister(lradc);
1383 mxs_lradc_hw_stop(lradc);
1384 mxs_lradc_trigger_remove(iio);
1385 iio_triggered_buffer_cleanup(iio);
1387 clk_disable_unprepare(lradc->clk);
1391 static struct platform_driver mxs_lradc_driver = {
1393 .name = DRIVER_NAME,
1394 .owner = THIS_MODULE,
1395 .of_match_table = mxs_lradc_dt_ids,
1397 .probe = mxs_lradc_probe,
1398 .remove = mxs_lradc_remove,
1401 module_platform_driver(mxs_lradc_driver);
1403 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1404 MODULE_DESCRIPTION("Freescale i.MX28 LRADC driver");
1405 MODULE_LICENSE("GPL v2");
1406 MODULE_ALIAS("platform:" DRIVER_NAME);