1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2eead75a2SAshish Jangam /*
3eead75a2SAshish Jangam * TSI driver for Dialog DA9052
4eead75a2SAshish Jangam *
5eead75a2SAshish Jangam * Copyright(c) 2012 Dialog Semiconductor Ltd.
6eead75a2SAshish Jangam *
7eead75a2SAshish Jangam * Author: David Dajun Chen <dchen@diasemi.com>
8eead75a2SAshish Jangam */
9eead75a2SAshish Jangam #include <linux/module.h>
10eead75a2SAshish Jangam #include <linux/input.h>
11eead75a2SAshish Jangam #include <linux/delay.h>
12eead75a2SAshish Jangam #include <linux/platform_device.h>
13eead75a2SAshish Jangam #include <linux/interrupt.h>
14eead75a2SAshish Jangam
15eead75a2SAshish Jangam #include <linux/mfd/da9052/reg.h>
16eead75a2SAshish Jangam #include <linux/mfd/da9052/da9052.h>
17eead75a2SAshish Jangam
18eead75a2SAshish Jangam #define TSI_PEN_DOWN_STATUS 0x40
19eead75a2SAshish Jangam
20eead75a2SAshish Jangam struct da9052_tsi {
21eead75a2SAshish Jangam struct da9052 *da9052;
22eead75a2SAshish Jangam struct input_dev *dev;
23eead75a2SAshish Jangam struct delayed_work ts_pen_work;
24eead75a2SAshish Jangam bool stopped;
25eead75a2SAshish Jangam bool adc_on;
26eead75a2SAshish Jangam };
27eead75a2SAshish Jangam
da9052_ts_adc_toggle(struct da9052_tsi * tsi,bool on)28eead75a2SAshish Jangam static void da9052_ts_adc_toggle(struct da9052_tsi *tsi, bool on)
29eead75a2SAshish Jangam {
30eead75a2SAshish Jangam da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 0, on);
31eead75a2SAshish Jangam tsi->adc_on = on;
32eead75a2SAshish Jangam }
33eead75a2SAshish Jangam
da9052_ts_pendwn_irq(int irq,void * data)34eead75a2SAshish Jangam static irqreturn_t da9052_ts_pendwn_irq(int irq, void *data)
35eead75a2SAshish Jangam {
36eead75a2SAshish Jangam struct da9052_tsi *tsi = data;
37eead75a2SAshish Jangam
38eead75a2SAshish Jangam if (!tsi->stopped) {
39eead75a2SAshish Jangam /* Mask PEN_DOWN event and unmask TSI_READY event */
408f520803SFabio Estevam da9052_disable_irq_nosync(tsi->da9052, DA9052_IRQ_PENDOWN);
418f520803SFabio Estevam da9052_enable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
42eead75a2SAshish Jangam
43eead75a2SAshish Jangam da9052_ts_adc_toggle(tsi, true);
44eead75a2SAshish Jangam
45eead75a2SAshish Jangam schedule_delayed_work(&tsi->ts_pen_work, HZ / 50);
46eead75a2SAshish Jangam }
47eead75a2SAshish Jangam
48eead75a2SAshish Jangam return IRQ_HANDLED;
49eead75a2SAshish Jangam }
50eead75a2SAshish Jangam
da9052_ts_read(struct da9052_tsi * tsi)51eead75a2SAshish Jangam static void da9052_ts_read(struct da9052_tsi *tsi)
52eead75a2SAshish Jangam {
53eead75a2SAshish Jangam struct input_dev *input = tsi->dev;
54eead75a2SAshish Jangam int ret;
55eead75a2SAshish Jangam u16 x, y, z;
56eead75a2SAshish Jangam u8 v;
57eead75a2SAshish Jangam
58eead75a2SAshish Jangam ret = da9052_reg_read(tsi->da9052, DA9052_TSI_X_MSB_REG);
59eead75a2SAshish Jangam if (ret < 0)
60eead75a2SAshish Jangam return;
61eead75a2SAshish Jangam
62eead75a2SAshish Jangam x = (u16) ret;
63eead75a2SAshish Jangam
64eead75a2SAshish Jangam ret = da9052_reg_read(tsi->da9052, DA9052_TSI_Y_MSB_REG);
65eead75a2SAshish Jangam if (ret < 0)
66eead75a2SAshish Jangam return;
67eead75a2SAshish Jangam
68eead75a2SAshish Jangam y = (u16) ret;
69eead75a2SAshish Jangam
70eead75a2SAshish Jangam ret = da9052_reg_read(tsi->da9052, DA9052_TSI_Z_MSB_REG);
71eead75a2SAshish Jangam if (ret < 0)
72eead75a2SAshish Jangam return;
73eead75a2SAshish Jangam
74eead75a2SAshish Jangam z = (u16) ret;
75eead75a2SAshish Jangam
76eead75a2SAshish Jangam ret = da9052_reg_read(tsi->da9052, DA9052_TSI_LSB_REG);
77eead75a2SAshish Jangam if (ret < 0)
78eead75a2SAshish Jangam return;
79eead75a2SAshish Jangam
80eead75a2SAshish Jangam v = (u8) ret;
81eead75a2SAshish Jangam
82eead75a2SAshish Jangam x = ((x << 2) & 0x3fc) | (v & 0x3);
83eead75a2SAshish Jangam y = ((y << 2) & 0x3fc) | ((v & 0xc) >> 2);
84eead75a2SAshish Jangam z = ((z << 2) & 0x3fc) | ((v & 0x30) >> 4);
85eead75a2SAshish Jangam
86eead75a2SAshish Jangam input_report_key(input, BTN_TOUCH, 1);
87eead75a2SAshish Jangam input_report_abs(input, ABS_X, x);
88eead75a2SAshish Jangam input_report_abs(input, ABS_Y, y);
89eead75a2SAshish Jangam input_report_abs(input, ABS_PRESSURE, z);
90eead75a2SAshish Jangam input_sync(input);
91eead75a2SAshish Jangam }
92eead75a2SAshish Jangam
da9052_ts_datardy_irq(int irq,void * data)93eead75a2SAshish Jangam static irqreturn_t da9052_ts_datardy_irq(int irq, void *data)
94eead75a2SAshish Jangam {
95eead75a2SAshish Jangam struct da9052_tsi *tsi = data;
96eead75a2SAshish Jangam
97eead75a2SAshish Jangam da9052_ts_read(tsi);
98eead75a2SAshish Jangam
99eead75a2SAshish Jangam return IRQ_HANDLED;
100eead75a2SAshish Jangam }
101eead75a2SAshish Jangam
da9052_ts_pen_work(struct work_struct * work)102eead75a2SAshish Jangam static void da9052_ts_pen_work(struct work_struct *work)
103eead75a2SAshish Jangam {
104eead75a2SAshish Jangam struct da9052_tsi *tsi = container_of(work, struct da9052_tsi,
105eead75a2SAshish Jangam ts_pen_work.work);
106eead75a2SAshish Jangam if (!tsi->stopped) {
107eead75a2SAshish Jangam int ret = da9052_reg_read(tsi->da9052, DA9052_TSI_LSB_REG);
108eead75a2SAshish Jangam if (ret < 0 || (ret & TSI_PEN_DOWN_STATUS)) {
109eead75a2SAshish Jangam /* Pen is still DOWN (or read error) */
110eead75a2SAshish Jangam schedule_delayed_work(&tsi->ts_pen_work, HZ / 50);
111eead75a2SAshish Jangam } else {
112eead75a2SAshish Jangam struct input_dev *input = tsi->dev;
113eead75a2SAshish Jangam
114eead75a2SAshish Jangam /* Pen UP */
115eead75a2SAshish Jangam da9052_ts_adc_toggle(tsi, false);
116eead75a2SAshish Jangam
117eead75a2SAshish Jangam /* Report Pen UP */
118eead75a2SAshish Jangam input_report_key(input, BTN_TOUCH, 0);
119eead75a2SAshish Jangam input_report_abs(input, ABS_PRESSURE, 0);
120eead75a2SAshish Jangam input_sync(input);
121eead75a2SAshish Jangam
122eead75a2SAshish Jangam /*
123eead75a2SAshish Jangam * FIXME: Fixes the unhandled irq issue when quick
124eead75a2SAshish Jangam * pen down and pen up events occurs
125eead75a2SAshish Jangam */
126eead75a2SAshish Jangam ret = da9052_reg_update(tsi->da9052,
127eead75a2SAshish Jangam DA9052_EVENT_B_REG, 0xC0, 0xC0);
128eead75a2SAshish Jangam if (ret < 0)
129eead75a2SAshish Jangam return;
130eead75a2SAshish Jangam
131eead75a2SAshish Jangam /* Mask TSI_READY event and unmask PEN_DOWN event */
1328f520803SFabio Estevam da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
1338f520803SFabio Estevam da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
134eead75a2SAshish Jangam }
135eead75a2SAshish Jangam }
136eead75a2SAshish Jangam }
137eead75a2SAshish Jangam
da9052_ts_configure_gpio(struct da9052 * da9052)1385298cc4cSBill Pemberton static int da9052_ts_configure_gpio(struct da9052 *da9052)
139eead75a2SAshish Jangam {
140eead75a2SAshish Jangam int error;
141eead75a2SAshish Jangam
142eead75a2SAshish Jangam error = da9052_reg_update(da9052, DA9052_GPIO_2_3_REG, 0x30, 0);
143eead75a2SAshish Jangam if (error < 0)
144eead75a2SAshish Jangam return error;
145eead75a2SAshish Jangam
146eead75a2SAshish Jangam error = da9052_reg_update(da9052, DA9052_GPIO_4_5_REG, 0x33, 0);
147eead75a2SAshish Jangam if (error < 0)
148eead75a2SAshish Jangam return error;
149eead75a2SAshish Jangam
150eead75a2SAshish Jangam error = da9052_reg_update(da9052, DA9052_GPIO_6_7_REG, 0x33, 0);
151eead75a2SAshish Jangam if (error < 0)
152eead75a2SAshish Jangam return error;
153eead75a2SAshish Jangam
154eead75a2SAshish Jangam return 0;
155eead75a2SAshish Jangam }
156eead75a2SAshish Jangam
da9052_configure_tsi(struct da9052_tsi * tsi)1575298cc4cSBill Pemberton static int da9052_configure_tsi(struct da9052_tsi *tsi)
158eead75a2SAshish Jangam {
159eead75a2SAshish Jangam int error;
160eead75a2SAshish Jangam
161eead75a2SAshish Jangam error = da9052_ts_configure_gpio(tsi->da9052);
162eead75a2SAshish Jangam if (error)
163eead75a2SAshish Jangam return error;
164eead75a2SAshish Jangam
165eead75a2SAshish Jangam /* Measure TSI sample every 1ms */
166eead75a2SAshish Jangam error = da9052_reg_update(tsi->da9052, DA9052_ADC_CONT_REG,
167eead75a2SAshish Jangam 1 << 6, 1 << 6);
168eead75a2SAshish Jangam if (error < 0)
169eead75a2SAshish Jangam return error;
170eead75a2SAshish Jangam
171eead75a2SAshish Jangam /* TSI_DELAY: 3 slots, TSI_SKIP: 0 slots, TSI_MODE: XYZP */
172eead75a2SAshish Jangam error = da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 0xFC, 0xC0);
173eead75a2SAshish Jangam if (error < 0)
174eead75a2SAshish Jangam return error;
175eead75a2SAshish Jangam
176eead75a2SAshish Jangam /* Supply TSIRef through LD09 */
177eead75a2SAshish Jangam error = da9052_reg_write(tsi->da9052, DA9052_LDO9_REG, 0x59);
178eead75a2SAshish Jangam if (error < 0)
179eead75a2SAshish Jangam return error;
180eead75a2SAshish Jangam
181eead75a2SAshish Jangam return 0;
182eead75a2SAshish Jangam }
183eead75a2SAshish Jangam
da9052_ts_input_open(struct input_dev * input_dev)184eead75a2SAshish Jangam static int da9052_ts_input_open(struct input_dev *input_dev)
185eead75a2SAshish Jangam {
186eead75a2SAshish Jangam struct da9052_tsi *tsi = input_get_drvdata(input_dev);
187eead75a2SAshish Jangam
188eead75a2SAshish Jangam tsi->stopped = false;
189eead75a2SAshish Jangam mb();
190eead75a2SAshish Jangam
191eead75a2SAshish Jangam /* Unmask PEN_DOWN event */
1928f520803SFabio Estevam da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
193eead75a2SAshish Jangam
194eead75a2SAshish Jangam /* Enable Pen Detect Circuit */
195eead75a2SAshish Jangam return da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG,
196eead75a2SAshish Jangam 1 << 1, 1 << 1);
197eead75a2SAshish Jangam }
198eead75a2SAshish Jangam
da9052_ts_input_close(struct input_dev * input_dev)199eead75a2SAshish Jangam static void da9052_ts_input_close(struct input_dev *input_dev)
200eead75a2SAshish Jangam {
201eead75a2SAshish Jangam struct da9052_tsi *tsi = input_get_drvdata(input_dev);
202eead75a2SAshish Jangam
203eead75a2SAshish Jangam tsi->stopped = true;
204eead75a2SAshish Jangam mb();
2058f520803SFabio Estevam da9052_disable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
206eead75a2SAshish Jangam cancel_delayed_work_sync(&tsi->ts_pen_work);
207eead75a2SAshish Jangam
208eead75a2SAshish Jangam if (tsi->adc_on) {
2098f520803SFabio Estevam da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
210eead75a2SAshish Jangam da9052_ts_adc_toggle(tsi, false);
211eead75a2SAshish Jangam
212eead75a2SAshish Jangam /*
213eead75a2SAshish Jangam * If ADC was on that means that pendwn IRQ was disabled
214eead75a2SAshish Jangam * twice and we need to enable it to keep enable/disable
215eead75a2SAshish Jangam * counter balanced. IRQ is still off though.
216eead75a2SAshish Jangam */
2178f520803SFabio Estevam da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
218eead75a2SAshish Jangam }
219eead75a2SAshish Jangam
220eead75a2SAshish Jangam /* Disable Pen Detect Circuit */
221eead75a2SAshish Jangam da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 1, 0);
222eead75a2SAshish Jangam }
223eead75a2SAshish Jangam
da9052_ts_probe(struct platform_device * pdev)2245298cc4cSBill Pemberton static int da9052_ts_probe(struct platform_device *pdev)
225eead75a2SAshish Jangam {
226eead75a2SAshish Jangam struct da9052 *da9052;
227eead75a2SAshish Jangam struct da9052_tsi *tsi;
228eead75a2SAshish Jangam struct input_dev *input_dev;
229eead75a2SAshish Jangam int error;
230eead75a2SAshish Jangam
231eead75a2SAshish Jangam da9052 = dev_get_drvdata(pdev->dev.parent);
232eead75a2SAshish Jangam if (!da9052)
233eead75a2SAshish Jangam return -EINVAL;
234eead75a2SAshish Jangam
235eead75a2SAshish Jangam tsi = kzalloc(sizeof(struct da9052_tsi), GFP_KERNEL);
236eead75a2SAshish Jangam input_dev = input_allocate_device();
237eead75a2SAshish Jangam if (!tsi || !input_dev) {
238eead75a2SAshish Jangam error = -ENOMEM;
239eead75a2SAshish Jangam goto err_free_mem;
240eead75a2SAshish Jangam }
241eead75a2SAshish Jangam
242eead75a2SAshish Jangam tsi->da9052 = da9052;
243eead75a2SAshish Jangam tsi->dev = input_dev;
244eead75a2SAshish Jangam tsi->stopped = true;
245eead75a2SAshish Jangam INIT_DELAYED_WORK(&tsi->ts_pen_work, da9052_ts_pen_work);
246eead75a2SAshish Jangam
247eead75a2SAshish Jangam input_dev->id.version = 0x0101;
248eead75a2SAshish Jangam input_dev->id.vendor = 0x15B6;
249eead75a2SAshish Jangam input_dev->id.product = 0x9052;
250eead75a2SAshish Jangam input_dev->name = "Dialog DA9052 TouchScreen Driver";
251eead75a2SAshish Jangam input_dev->dev.parent = &pdev->dev;
252eead75a2SAshish Jangam input_dev->open = da9052_ts_input_open;
253eead75a2SAshish Jangam input_dev->close = da9052_ts_input_close;
254eead75a2SAshish Jangam
255eead75a2SAshish Jangam __set_bit(EV_ABS, input_dev->evbit);
256eead75a2SAshish Jangam __set_bit(EV_KEY, input_dev->evbit);
257eead75a2SAshish Jangam __set_bit(BTN_TOUCH, input_dev->keybit);
258eead75a2SAshish Jangam
259eead75a2SAshish Jangam input_set_abs_params(input_dev, ABS_X, 0, 1023, 0, 0);
260eead75a2SAshish Jangam input_set_abs_params(input_dev, ABS_Y, 0, 1023, 0, 0);
261eead75a2SAshish Jangam input_set_abs_params(input_dev, ABS_PRESSURE, 0, 1023, 0, 0);
262eead75a2SAshish Jangam
263eead75a2SAshish Jangam input_set_drvdata(input_dev, tsi);
264eead75a2SAshish Jangam
265eead75a2SAshish Jangam /* Disable Pen Detect Circuit */
266eead75a2SAshish Jangam da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 1, 0);
267eead75a2SAshish Jangam
268eead75a2SAshish Jangam /* Disable ADC */
269eead75a2SAshish Jangam da9052_ts_adc_toggle(tsi, false);
270eead75a2SAshish Jangam
2718f520803SFabio Estevam error = da9052_request_irq(tsi->da9052, DA9052_IRQ_PENDOWN,
2728f520803SFabio Estevam "pendown-irq", da9052_ts_pendwn_irq, tsi);
273eead75a2SAshish Jangam if (error) {
274eead75a2SAshish Jangam dev_err(tsi->da9052->dev,
2758f520803SFabio Estevam "Failed to register PENDWN IRQ: %d\n", error);
276eead75a2SAshish Jangam goto err_free_mem;
277eead75a2SAshish Jangam }
278eead75a2SAshish Jangam
2798f520803SFabio Estevam error = da9052_request_irq(tsi->da9052, DA9052_IRQ_TSIREADY,
2808f520803SFabio Estevam "tsiready-irq", da9052_ts_datardy_irq, tsi);
281eead75a2SAshish Jangam if (error) {
282eead75a2SAshish Jangam dev_err(tsi->da9052->dev,
2838f520803SFabio Estevam "Failed to register TSIRDY IRQ :%d\n", error);
284eead75a2SAshish Jangam goto err_free_pendwn_irq;
285eead75a2SAshish Jangam }
286eead75a2SAshish Jangam
287eead75a2SAshish Jangam /* Mask PEN_DOWN and TSI_READY events */
2888f520803SFabio Estevam da9052_disable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
2898f520803SFabio Estevam da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
290eead75a2SAshish Jangam
291eead75a2SAshish Jangam error = da9052_configure_tsi(tsi);
292eead75a2SAshish Jangam if (error)
293eead75a2SAshish Jangam goto err_free_datardy_irq;
294eead75a2SAshish Jangam
295eead75a2SAshish Jangam error = input_register_device(tsi->dev);
296eead75a2SAshish Jangam if (error)
297eead75a2SAshish Jangam goto err_free_datardy_irq;
298eead75a2SAshish Jangam
299eead75a2SAshish Jangam platform_set_drvdata(pdev, tsi);
300eead75a2SAshish Jangam
301eead75a2SAshish Jangam return 0;
302eead75a2SAshish Jangam
303eead75a2SAshish Jangam err_free_datardy_irq:
3048f520803SFabio Estevam da9052_free_irq(tsi->da9052, DA9052_IRQ_TSIREADY, tsi);
305eead75a2SAshish Jangam err_free_pendwn_irq:
3068f520803SFabio Estevam da9052_free_irq(tsi->da9052, DA9052_IRQ_PENDOWN, tsi);
307eead75a2SAshish Jangam err_free_mem:
308eead75a2SAshish Jangam kfree(tsi);
309eead75a2SAshish Jangam input_free_device(input_dev);
310eead75a2SAshish Jangam
311eead75a2SAshish Jangam return error;
312eead75a2SAshish Jangam }
313eead75a2SAshish Jangam
da9052_ts_remove(struct platform_device * pdev)314e2619cf7SBill Pemberton static int da9052_ts_remove(struct platform_device *pdev)
315eead75a2SAshish Jangam {
316eead75a2SAshish Jangam struct da9052_tsi *tsi = platform_get_drvdata(pdev);
317eead75a2SAshish Jangam
318eead75a2SAshish Jangam da9052_reg_write(tsi->da9052, DA9052_LDO9_REG, 0x19);
319eead75a2SAshish Jangam
3208f520803SFabio Estevam da9052_free_irq(tsi->da9052, DA9052_IRQ_TSIREADY, tsi);
3218f520803SFabio Estevam da9052_free_irq(tsi->da9052, DA9052_IRQ_PENDOWN, tsi);
322eead75a2SAshish Jangam
323eead75a2SAshish Jangam input_unregister_device(tsi->dev);
324eead75a2SAshish Jangam kfree(tsi);
325eead75a2SAshish Jangam
326eead75a2SAshish Jangam return 0;
327eead75a2SAshish Jangam }
328eead75a2SAshish Jangam
329eead75a2SAshish Jangam static struct platform_driver da9052_tsi_driver = {
330eead75a2SAshish Jangam .probe = da9052_ts_probe,
3311cb0aa88SBill Pemberton .remove = da9052_ts_remove,
332eead75a2SAshish Jangam .driver = {
333eead75a2SAshish Jangam .name = "da9052-tsi",
334eead75a2SAshish Jangam },
335eead75a2SAshish Jangam };
336eead75a2SAshish Jangam
337eead75a2SAshish Jangam module_platform_driver(da9052_tsi_driver);
338eead75a2SAshish Jangam
339eead75a2SAshish Jangam MODULE_DESCRIPTION("Touchscreen driver for Dialog Semiconductor DA9052");
340eead75a2SAshish Jangam MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
341eead75a2SAshish Jangam MODULE_LICENSE("GPL");
342eead75a2SAshish Jangam MODULE_ALIAS("platform:da9052-tsi");
343