Lines Matching +full:adc +full:- +full:ts +full:- +full:wires

1 // SPDX-License-Identifier: GPL-2.0-or-later
19 #include <linux/mfd/mxs-lradc.h>
25 "mxs-lradc-touchscreen",
26 "mxs-lradc-channel6",
27 "mxs-lradc-channel7",
91 static bool mxs_lradc_check_touch_event(struct mxs_lradc_ts *ts) in mxs_lradc_check_touch_event() argument
93 return !!(readl(ts->base + LRADC_STATUS) & in mxs_lradc_check_touch_event()
97 static void mxs_lradc_map_ts_channel(struct mxs_lradc_ts *ts, unsigned int vch, in mxs_lradc_map_ts_channel() argument
101 ts->base + LRADC_CTRL4 + STMP_OFFSET_REG_CLR); in mxs_lradc_map_ts_channel()
103 ts->base + LRADC_CTRL4 + STMP_OFFSET_REG_SET); in mxs_lradc_map_ts_channel()
106 static void mxs_lradc_setup_ts_channel(struct mxs_lradc_ts *ts, unsigned int ch) in mxs_lradc_setup_ts_channel() argument
117 LRADC_CH_NUM_SAMPLES(ts->over_sample_cnt - 1), in mxs_lradc_setup_ts_channel()
118 ts->base + LRADC_CH(ch)); in mxs_lradc_setup_ts_channel()
122 * multi-cycle accumulation. in mxs_lradc_setup_ts_channel()
125 ts->base + LRADC_CH(ch) + STMP_OFFSET_REG_CLR); in mxs_lradc_setup_ts_channel()
132 * HW_LRADC_DELAY2, and HW_LRADC_DELAY3 must be non-zero; otherwise, in mxs_lradc_setup_ts_channel()
136 LRADC_DELAY_LOOP(ts->over_sample_cnt - 1) | in mxs_lradc_setup_ts_channel()
137 LRADC_DELAY_DELAY(ts->over_sample_delay - 1), in mxs_lradc_setup_ts_channel()
138 ts->base + LRADC_DELAY(3)); in mxs_lradc_setup_ts_channel()
141 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); in mxs_lradc_setup_ts_channel()
150 LRADC_DELAY_KICK | LRADC_DELAY_DELAY(ts->settling_delay), in mxs_lradc_setup_ts_channel()
151 ts->base + LRADC_DELAY(2)); in mxs_lradc_setup_ts_channel()
160 static void mxs_lradc_setup_ts_pressure(struct mxs_lradc_ts *ts, in mxs_lradc_setup_ts_pressure() argument
174 LRADC_CH_NUM_SAMPLES(ts->over_sample_cnt - 1); in mxs_lradc_setup_ts_pressure()
175 writel(reg, ts->base + LRADC_CH(ch1)); in mxs_lradc_setup_ts_pressure()
176 writel(reg, ts->base + LRADC_CH(ch2)); in mxs_lradc_setup_ts_pressure()
180 * multi-cycle accumulation. in mxs_lradc_setup_ts_pressure()
183 ts->base + LRADC_CH(ch1) + STMP_OFFSET_REG_CLR); in mxs_lradc_setup_ts_pressure()
185 ts->base + LRADC_CH(ch2) + STMP_OFFSET_REG_CLR); in mxs_lradc_setup_ts_pressure()
190 LRADC_DELAY_LOOP(ts->over_sample_cnt - 1) | in mxs_lradc_setup_ts_pressure()
191 LRADC_DELAY_DELAY(ts->over_sample_delay - 1), in mxs_lradc_setup_ts_pressure()
192 ts->base + LRADC_DELAY(3)); in mxs_lradc_setup_ts_pressure()
195 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); in mxs_lradc_setup_ts_pressure()
204 LRADC_DELAY_KICK | LRADC_DELAY_DELAY(ts->settling_delay), in mxs_lradc_setup_ts_pressure()
205 ts->base + LRADC_DELAY(2)); in mxs_lradc_setup_ts_pressure()
208 static unsigned int mxs_lradc_ts_read_raw_channel(struct mxs_lradc_ts *ts, in mxs_lradc_ts_read_raw_channel() argument
214 reg = readl(ts->base + LRADC_CH(channel)); in mxs_lradc_ts_read_raw_channel()
216 num_samples = ts->over_sample_cnt; in mxs_lradc_ts_read_raw_channel()
224 static unsigned int mxs_lradc_read_ts_pressure(struct mxs_lradc_ts *ts, in mxs_lradc_read_ts_pressure() argument
231 reg = readl(ts->base + LRADC_CTRL1) & mask; in mxs_lradc_read_ts_pressure()
234 reg = readl(ts->base + LRADC_CTRL1) & mask; in mxs_lradc_read_ts_pressure()
235 dev_dbg(ts->dev, "One channel is still busy: %X\n", reg); in mxs_lradc_read_ts_pressure()
238 m1 = mxs_lradc_ts_read_raw_channel(ts, ch1); in mxs_lradc_read_ts_pressure()
239 m2 = mxs_lradc_ts_read_raw_channel(ts, ch2); in mxs_lradc_read_ts_pressure()
242 dev_warn(ts->dev, "Cannot calculate pressure\n"); in mxs_lradc_read_ts_pressure()
243 return 1 << (LRADC_RESOLUTION - 1); in mxs_lradc_read_ts_pressure()
246 /* simply scale the value from 0 ... max ADC resolution */ in mxs_lradc_read_ts_pressure()
251 dev_dbg(ts->dev, "Pressure = %u\n", pressure); in mxs_lradc_read_ts_pressure()
261 * YP(open)--+-------------+
262 * | |--+
264 * YM(-)--+-------------+ |
265 * +--------------+
270 * (-) means GND
272 static void mxs_lradc_setup_touch_detection(struct mxs_lradc_ts *ts) in mxs_lradc_setup_touch_detection() argument
274 struct mxs_lradc *lradc = ts->lradc; in mxs_lradc_setup_touch_detection()
279 * - a weak pullup to the X+ connector in mxs_lradc_setup_touch_detection()
280 * - a strong ground at the Y- connector in mxs_lradc_setup_touch_detection()
282 writel(info[lradc->soc].mask, in mxs_lradc_setup_touch_detection()
283 ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); in mxs_lradc_setup_touch_detection()
284 writel(info[lradc->soc].bit, in mxs_lradc_setup_touch_detection()
285 ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET); in mxs_lradc_setup_touch_detection()
289 * YP(meas)--+-------------+
290 * | |--+
292 * YM(open)--+-------------+ |
293 * +--------------+
295 * XP(+) XM(-)
298 * (-) means here GND
300 static void mxs_lradc_prepare_x_pos(struct mxs_lradc_ts *ts) in mxs_lradc_prepare_x_pos() argument
302 struct mxs_lradc *lradc = ts->lradc; in mxs_lradc_prepare_x_pos()
304 writel(info[lradc->soc].mask, in mxs_lradc_prepare_x_pos()
305 ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); in mxs_lradc_prepare_x_pos()
306 writel(info[lradc->soc].x_plate, in mxs_lradc_prepare_x_pos()
307 ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET); in mxs_lradc_prepare_x_pos()
309 ts->cur_plate = LRADC_SAMPLE_X; in mxs_lradc_prepare_x_pos()
310 mxs_lradc_map_ts_channel(ts, TOUCHSCREEN_VCHANNEL1, TS_CH_YP); in mxs_lradc_prepare_x_pos()
311 mxs_lradc_setup_ts_channel(ts, TOUCHSCREEN_VCHANNEL1); in mxs_lradc_prepare_x_pos()
315 * YP(+)--+-------------+
316 * | |--+
318 * YM(-)--+-------------+ |
319 * +--------------+
324 * (-) means here GND
326 static void mxs_lradc_prepare_y_pos(struct mxs_lradc_ts *ts) in mxs_lradc_prepare_y_pos() argument
328 struct mxs_lradc *lradc = ts->lradc; in mxs_lradc_prepare_y_pos()
330 writel(info[lradc->soc].mask, in mxs_lradc_prepare_y_pos()
331 ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); in mxs_lradc_prepare_y_pos()
332 writel(info[lradc->soc].y_plate, in mxs_lradc_prepare_y_pos()
333 ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET); in mxs_lradc_prepare_y_pos()
335 ts->cur_plate = LRADC_SAMPLE_Y; in mxs_lradc_prepare_y_pos()
336 mxs_lradc_map_ts_channel(ts, TOUCHSCREEN_VCHANNEL1, TS_CH_XM); in mxs_lradc_prepare_y_pos()
337 mxs_lradc_setup_ts_channel(ts, TOUCHSCREEN_VCHANNEL1); in mxs_lradc_prepare_y_pos()
341 * YP(+)--+-------------+
342 * | |--+
344 * YM(meas)--+-------------+ |
345 * +--------------+
347 * XP(meas) XM(-)
350 * (-) means here GND
352 static void mxs_lradc_prepare_pressure(struct mxs_lradc_ts *ts) in mxs_lradc_prepare_pressure() argument
354 struct mxs_lradc *lradc = ts->lradc; in mxs_lradc_prepare_pressure()
356 writel(info[lradc->soc].mask, in mxs_lradc_prepare_pressure()
357 ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); in mxs_lradc_prepare_pressure()
358 writel(info[lradc->soc].pressure, in mxs_lradc_prepare_pressure()
359 ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET); in mxs_lradc_prepare_pressure()
361 ts->cur_plate = LRADC_SAMPLE_PRESSURE; in mxs_lradc_prepare_pressure()
362 mxs_lradc_map_ts_channel(ts, TOUCHSCREEN_VCHANNEL1, TS_CH_YM); in mxs_lradc_prepare_pressure()
363 mxs_lradc_map_ts_channel(ts, TOUCHSCREEN_VCHANNEL2, TS_CH_XP); in mxs_lradc_prepare_pressure()
364 mxs_lradc_setup_ts_pressure(ts, TOUCHSCREEN_VCHANNEL2, in mxs_lradc_prepare_pressure()
368 static void mxs_lradc_enable_touch_detection(struct mxs_lradc_ts *ts) in mxs_lradc_enable_touch_detection() argument
370 mxs_lradc_setup_touch_detection(ts); in mxs_lradc_enable_touch_detection()
372 ts->cur_plate = LRADC_TOUCH; in mxs_lradc_enable_touch_detection()
374 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); in mxs_lradc_enable_touch_detection()
376 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET); in mxs_lradc_enable_touch_detection()
379 static void mxs_lradc_start_touch_event(struct mxs_lradc_ts *ts) in mxs_lradc_start_touch_event() argument
382 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); in mxs_lradc_start_touch_event()
384 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET); in mxs_lradc_start_touch_event()
386 * start with the Y-pos, because it uses nearly the same plate in mxs_lradc_start_touch_event()
389 mxs_lradc_prepare_y_pos(ts); in mxs_lradc_start_touch_event()
392 static void mxs_lradc_report_ts_event(struct mxs_lradc_ts *ts) in mxs_lradc_report_ts_event() argument
394 input_report_abs(ts->ts_input, ABS_X, ts->ts_x_pos); in mxs_lradc_report_ts_event()
395 input_report_abs(ts->ts_input, ABS_Y, ts->ts_y_pos); in mxs_lradc_report_ts_event()
396 input_report_abs(ts->ts_input, ABS_PRESSURE, ts->ts_pressure); in mxs_lradc_report_ts_event()
397 input_report_key(ts->ts_input, BTN_TOUCH, 1); in mxs_lradc_report_ts_event()
398 input_sync(ts->ts_input); in mxs_lradc_report_ts_event()
401 static void mxs_lradc_complete_touch_event(struct mxs_lradc_ts *ts) in mxs_lradc_complete_touch_event() argument
403 mxs_lradc_setup_touch_detection(ts); in mxs_lradc_complete_touch_event()
404 ts->cur_plate = LRADC_SAMPLE_VALID; in mxs_lradc_complete_touch_event()
409 writel(0, ts->base + LRADC_CH(TOUCHSCREEN_VCHANNEL1)); in mxs_lradc_complete_touch_event()
412 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); in mxs_lradc_complete_touch_event()
415 ts->base + LRADC_DELAY(2)); in mxs_lradc_complete_touch_event()
422 static void mxs_lradc_finish_touch_event(struct mxs_lradc_ts *ts, bool valid) in mxs_lradc_finish_touch_event() argument
425 if (valid && mxs_lradc_check_touch_event(ts)) { in mxs_lradc_finish_touch_event()
426 ts->ts_valid = true; in mxs_lradc_finish_touch_event()
427 mxs_lradc_report_ts_event(ts); in mxs_lradc_finish_touch_event()
431 if (mxs_lradc_check_touch_event(ts)) { in mxs_lradc_finish_touch_event()
432 mxs_lradc_prepare_y_pos(ts); in mxs_lradc_finish_touch_event()
436 if (ts->ts_valid) { in mxs_lradc_finish_touch_event()
438 ts->ts_valid = false; in mxs_lradc_finish_touch_event()
439 input_report_key(ts->ts_input, BTN_TOUCH, 0); in mxs_lradc_finish_touch_event()
440 input_sync(ts->ts_input); in mxs_lradc_finish_touch_event()
444 ts->cur_plate = LRADC_TOUCH; in mxs_lradc_finish_touch_event()
445 writel(0, ts->base + LRADC_DELAY(2)); in mxs_lradc_finish_touch_event()
446 writel(0, ts->base + LRADC_DELAY(3)); in mxs_lradc_finish_touch_event()
450 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); in mxs_lradc_finish_touch_event()
452 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET); in mxs_lradc_finish_touch_event()
456 static void mxs_lradc_handle_touch(struct mxs_lradc_ts *ts) in mxs_lradc_handle_touch() argument
458 switch (ts->cur_plate) { in mxs_lradc_handle_touch()
460 if (mxs_lradc_check_touch_event(ts)) in mxs_lradc_handle_touch()
461 mxs_lradc_start_touch_event(ts); in mxs_lradc_handle_touch()
463 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); in mxs_lradc_handle_touch()
467 ts->ts_y_pos = in mxs_lradc_handle_touch()
468 mxs_lradc_ts_read_raw_channel(ts, TOUCHSCREEN_VCHANNEL1); in mxs_lradc_handle_touch()
469 mxs_lradc_prepare_x_pos(ts); in mxs_lradc_handle_touch()
473 ts->ts_x_pos = in mxs_lradc_handle_touch()
474 mxs_lradc_ts_read_raw_channel(ts, TOUCHSCREEN_VCHANNEL1); in mxs_lradc_handle_touch()
475 mxs_lradc_prepare_pressure(ts); in mxs_lradc_handle_touch()
479 ts->ts_pressure = in mxs_lradc_handle_touch()
480 mxs_lradc_read_ts_pressure(ts, in mxs_lradc_handle_touch()
483 mxs_lradc_complete_touch_event(ts); in mxs_lradc_handle_touch()
487 mxs_lradc_finish_touch_event(ts, 1); in mxs_lradc_handle_touch()
495 struct mxs_lradc_ts *ts = data; in mxs_lradc_ts_handle_irq() local
496 struct mxs_lradc *lradc = ts->lradc; in mxs_lradc_ts_handle_irq()
497 unsigned long reg = readl(ts->base + LRADC_CTRL1); in mxs_lradc_ts_handle_irq()
509 spin_lock_irqsave(&ts->lock, flags); in mxs_lradc_ts_handle_irq()
510 mxs_lradc_handle_touch(ts); in mxs_lradc_ts_handle_irq()
511 spin_unlock_irqrestore(&ts->lock, flags); in mxs_lradc_ts_handle_irq()
516 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); in mxs_lradc_ts_handle_irq()
524 struct mxs_lradc_ts *ts = input_get_drvdata(dev); in mxs_lradc_ts_open() local
526 /* Enable the touch-detect circuitry. */ in mxs_lradc_ts_open()
527 mxs_lradc_enable_touch_detection(ts); in mxs_lradc_ts_open()
532 static void mxs_lradc_ts_stop(struct mxs_lradc_ts *ts) in mxs_lradc_ts_stop() argument
535 struct mxs_lradc *lradc = ts->lradc; in mxs_lradc_ts_stop()
541 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); in mxs_lradc_ts_stop()
543 /* Power-down touchscreen touch-detect circuitry. */ in mxs_lradc_ts_stop()
544 writel(info[lradc->soc].mask, in mxs_lradc_ts_stop()
545 ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); in mxs_lradc_ts_stop()
547 writel(lradc->buffer_vchans << LRADC_CTRL1_LRADC_IRQ_EN_OFFSET, in mxs_lradc_ts_stop()
548 ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR); in mxs_lradc_ts_stop()
551 writel(0, ts->base + LRADC_DELAY(i)); in mxs_lradc_ts_stop()
556 struct mxs_lradc_ts *ts = input_get_drvdata(dev); in mxs_lradc_ts_close() local
558 mxs_lradc_ts_stop(ts); in mxs_lradc_ts_close()
561 static void mxs_lradc_ts_hw_init(struct mxs_lradc_ts *ts) in mxs_lradc_ts_hw_init() argument
563 struct mxs_lradc *lradc = ts->lradc; in mxs_lradc_ts_hw_init()
566 if (lradc->soc == IMX28_LRADC) { in mxs_lradc_ts_hw_init()
568 ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR); in mxs_lradc_ts_hw_init()
570 if (lradc->touchscreen_wire == MXS_LRADC_TOUCHSCREEN_5WIRE) in mxs_lradc_ts_hw_init()
572 ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET); in mxs_lradc_ts_hw_init()
576 static int mxs_lradc_ts_register(struct mxs_lradc_ts *ts) in mxs_lradc_ts_register() argument
579 struct device *dev = ts->dev; in mxs_lradc_ts_register()
583 return -ENOMEM; in mxs_lradc_ts_register()
585 input->name = "mxs-lradc-ts"; in mxs_lradc_ts_register()
586 input->id.bustype = BUS_HOST; in mxs_lradc_ts_register()
587 input->open = mxs_lradc_ts_open; in mxs_lradc_ts_register()
588 input->close = mxs_lradc_ts_close; in mxs_lradc_ts_register()
590 __set_bit(INPUT_PROP_DIRECT, input->propbit); in mxs_lradc_ts_register()
597 ts->ts_input = input; in mxs_lradc_ts_register()
598 input_set_drvdata(input, ts); in mxs_lradc_ts_register()
605 struct device *dev = &pdev->dev; in mxs_lradc_ts_probe()
606 struct device_node *node = dev->parent->of_node; in mxs_lradc_ts_probe()
607 struct mxs_lradc *lradc = dev_get_drvdata(dev->parent); in mxs_lradc_ts_probe()
608 struct mxs_lradc_ts *ts; in mxs_lradc_ts_probe() local
612 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL); in mxs_lradc_ts_probe()
613 if (!ts) in mxs_lradc_ts_probe()
614 return -ENOMEM; in mxs_lradc_ts_probe()
616 platform_set_drvdata(pdev, ts); in mxs_lradc_ts_probe()
618 ts->lradc = lradc; in mxs_lradc_ts_probe()
619 ts->dev = dev; in mxs_lradc_ts_probe()
620 spin_lock_init(&ts->lock); in mxs_lradc_ts_probe()
622 ts->base = devm_platform_ioremap_resource(pdev, 0); in mxs_lradc_ts_probe()
623 if (IS_ERR(ts->base)) in mxs_lradc_ts_probe()
624 return PTR_ERR(ts->base); in mxs_lradc_ts_probe()
626 ret = of_property_read_u32(node, "fsl,lradc-touchscreen-wires", in mxs_lradc_ts_probe()
631 if (of_property_read_u32(node, "fsl,ave-ctrl", &adapt)) { in mxs_lradc_ts_probe()
632 ts->over_sample_cnt = 4; in mxs_lradc_ts_probe()
635 ts->over_sample_cnt = adapt; in mxs_lradc_ts_probe()
637 dev_err(ts->dev, "Invalid sample count (%u)\n", in mxs_lradc_ts_probe()
639 return -EINVAL; in mxs_lradc_ts_probe()
643 if (of_property_read_u32(node, "fsl,ave-delay", &adapt)) { in mxs_lradc_ts_probe()
644 ts->over_sample_delay = 2; in mxs_lradc_ts_probe()
647 ts->over_sample_delay = adapt; in mxs_lradc_ts_probe()
649 dev_err(ts->dev, "Invalid sample delay (%u)\n", in mxs_lradc_ts_probe()
651 return -EINVAL; in mxs_lradc_ts_probe()
656 ts->settling_delay = 10; in mxs_lradc_ts_probe()
659 ts->settling_delay = adapt; in mxs_lradc_ts_probe()
661 dev_err(ts->dev, "Invalid settling delay (%u)\n", in mxs_lradc_ts_probe()
663 return -EINVAL; in mxs_lradc_ts_probe()
667 ret = stmp_reset_block(ts->base); in mxs_lradc_ts_probe()
671 mxs_lradc_ts_hw_init(ts); in mxs_lradc_ts_probe()
680 mxs_lradc_ts_stop(ts); in mxs_lradc_ts_probe()
684 0, mxs_lradc_ts_irq_names[i], ts); in mxs_lradc_ts_probe()
689 return mxs_lradc_ts_register(ts); in mxs_lradc_ts_probe()
694 .name = "mxs-lradc-ts",
703 MODULE_ALIAS("platform:mxs-lradc-ts");