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