drm/client: Fix drm client endless Kconfig loop
[drm/drm-misc.git] / drivers / input / touchscreen / da9052_tsi.c
blobc2d3252f84668c1bc0d39798038262e64fcb94eb
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * TSI driver for Dialog DA9052
5 * Copyright(c) 2012 Dialog Semiconductor Ltd.
7 * Author: David Dajun Chen <dchen@diasemi.com>
8 */
9 #include <linux/module.h>
10 #include <linux/input.h>
11 #include <linux/delay.h>
12 #include <linux/platform_device.h>
13 #include <linux/interrupt.h>
15 #include <linux/mfd/da9052/reg.h>
16 #include <linux/mfd/da9052/da9052.h>
18 #define TSI_PEN_DOWN_STATUS 0x40
20 struct da9052_tsi {
21 struct da9052 *da9052;
22 struct input_dev *dev;
23 struct delayed_work ts_pen_work;
24 bool stopped;
25 bool adc_on;
28 static void da9052_ts_adc_toggle(struct da9052_tsi *tsi, bool on)
30 da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 0, on);
31 tsi->adc_on = on;
34 static irqreturn_t da9052_ts_pendwn_irq(int irq, void *data)
36 struct da9052_tsi *tsi = data;
38 if (!tsi->stopped) {
39 /* Mask PEN_DOWN event and unmask TSI_READY event */
40 da9052_disable_irq_nosync(tsi->da9052, DA9052_IRQ_PENDOWN);
41 da9052_enable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
43 da9052_ts_adc_toggle(tsi, true);
45 schedule_delayed_work(&tsi->ts_pen_work, HZ / 50);
48 return IRQ_HANDLED;
51 static void da9052_ts_read(struct da9052_tsi *tsi)
53 struct input_dev *input = tsi->dev;
54 int ret;
55 u16 x, y, z;
56 u8 v;
58 ret = da9052_reg_read(tsi->da9052, DA9052_TSI_X_MSB_REG);
59 if (ret < 0)
60 return;
62 x = (u16) ret;
64 ret = da9052_reg_read(tsi->da9052, DA9052_TSI_Y_MSB_REG);
65 if (ret < 0)
66 return;
68 y = (u16) ret;
70 ret = da9052_reg_read(tsi->da9052, DA9052_TSI_Z_MSB_REG);
71 if (ret < 0)
72 return;
74 z = (u16) ret;
76 ret = da9052_reg_read(tsi->da9052, DA9052_TSI_LSB_REG);
77 if (ret < 0)
78 return;
80 v = (u8) ret;
82 x = ((x << 2) & 0x3fc) | (v & 0x3);
83 y = ((y << 2) & 0x3fc) | ((v & 0xc) >> 2);
84 z = ((z << 2) & 0x3fc) | ((v & 0x30) >> 4);
86 input_report_key(input, BTN_TOUCH, 1);
87 input_report_abs(input, ABS_X, x);
88 input_report_abs(input, ABS_Y, y);
89 input_report_abs(input, ABS_PRESSURE, z);
90 input_sync(input);
93 static irqreturn_t da9052_ts_datardy_irq(int irq, void *data)
95 struct da9052_tsi *tsi = data;
97 da9052_ts_read(tsi);
99 return IRQ_HANDLED;
102 static void da9052_ts_pen_work(struct work_struct *work)
104 struct da9052_tsi *tsi = container_of(work, struct da9052_tsi,
105 ts_pen_work.work);
106 if (!tsi->stopped) {
107 int ret = da9052_reg_read(tsi->da9052, DA9052_TSI_LSB_REG);
108 if (ret < 0 || (ret & TSI_PEN_DOWN_STATUS)) {
109 /* Pen is still DOWN (or read error) */
110 schedule_delayed_work(&tsi->ts_pen_work, HZ / 50);
111 } else {
112 struct input_dev *input = tsi->dev;
114 /* Pen UP */
115 da9052_ts_adc_toggle(tsi, false);
117 /* Report Pen UP */
118 input_report_key(input, BTN_TOUCH, 0);
119 input_report_abs(input, ABS_PRESSURE, 0);
120 input_sync(input);
123 * FIXME: Fixes the unhandled irq issue when quick
124 * pen down and pen up events occurs
126 ret = da9052_reg_update(tsi->da9052,
127 DA9052_EVENT_B_REG, 0xC0, 0xC0);
128 if (ret < 0)
129 return;
131 /* Mask TSI_READY event and unmask PEN_DOWN event */
132 da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
133 da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
138 static int da9052_ts_configure_gpio(struct da9052 *da9052)
140 int error;
142 error = da9052_reg_update(da9052, DA9052_GPIO_2_3_REG, 0x30, 0);
143 if (error < 0)
144 return error;
146 error = da9052_reg_update(da9052, DA9052_GPIO_4_5_REG, 0x33, 0);
147 if (error < 0)
148 return error;
150 error = da9052_reg_update(da9052, DA9052_GPIO_6_7_REG, 0x33, 0);
151 if (error < 0)
152 return error;
154 return 0;
157 static int da9052_configure_tsi(struct da9052_tsi *tsi)
159 int error;
161 error = da9052_ts_configure_gpio(tsi->da9052);
162 if (error)
163 return error;
165 /* Measure TSI sample every 1ms */
166 error = da9052_reg_update(tsi->da9052, DA9052_ADC_CONT_REG,
167 1 << 6, 1 << 6);
168 if (error < 0)
169 return error;
171 /* TSI_DELAY: 3 slots, TSI_SKIP: 0 slots, TSI_MODE: XYZP */
172 error = da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 0xFC, 0xC0);
173 if (error < 0)
174 return error;
176 /* Supply TSIRef through LD09 */
177 error = da9052_reg_write(tsi->da9052, DA9052_LDO9_REG, 0x59);
178 if (error < 0)
179 return error;
181 return 0;
184 static int da9052_ts_input_open(struct input_dev *input_dev)
186 struct da9052_tsi *tsi = input_get_drvdata(input_dev);
188 tsi->stopped = false;
189 mb();
191 /* Unmask PEN_DOWN event */
192 da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
194 /* Enable Pen Detect Circuit */
195 return da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG,
196 1 << 1, 1 << 1);
199 static void da9052_ts_input_close(struct input_dev *input_dev)
201 struct da9052_tsi *tsi = input_get_drvdata(input_dev);
203 tsi->stopped = true;
204 mb();
205 da9052_disable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
206 cancel_delayed_work_sync(&tsi->ts_pen_work);
208 if (tsi->adc_on) {
209 da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
210 da9052_ts_adc_toggle(tsi, false);
213 * If ADC was on that means that pendwn IRQ was disabled
214 * twice and we need to enable it to keep enable/disable
215 * counter balanced. IRQ is still off though.
217 da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
220 /* Disable Pen Detect Circuit */
221 da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 1, 0);
224 static int da9052_ts_probe(struct platform_device *pdev)
226 struct da9052 *da9052;
227 struct da9052_tsi *tsi;
228 struct input_dev *input_dev;
229 int error;
231 da9052 = dev_get_drvdata(pdev->dev.parent);
232 if (!da9052)
233 return -EINVAL;
235 tsi = kzalloc(sizeof(*tsi), GFP_KERNEL);
236 input_dev = input_allocate_device();
237 if (!tsi || !input_dev) {
238 error = -ENOMEM;
239 goto err_free_mem;
242 tsi->da9052 = da9052;
243 tsi->dev = input_dev;
244 tsi->stopped = true;
245 INIT_DELAYED_WORK(&tsi->ts_pen_work, da9052_ts_pen_work);
247 input_dev->id.version = 0x0101;
248 input_dev->id.vendor = 0x15B6;
249 input_dev->id.product = 0x9052;
250 input_dev->name = "Dialog DA9052 TouchScreen Driver";
251 input_dev->dev.parent = &pdev->dev;
252 input_dev->open = da9052_ts_input_open;
253 input_dev->close = da9052_ts_input_close;
255 __set_bit(EV_ABS, input_dev->evbit);
256 __set_bit(EV_KEY, input_dev->evbit);
257 __set_bit(BTN_TOUCH, input_dev->keybit);
259 input_set_abs_params(input_dev, ABS_X, 0, 1023, 0, 0);
260 input_set_abs_params(input_dev, ABS_Y, 0, 1023, 0, 0);
261 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 1023, 0, 0);
263 input_set_drvdata(input_dev, tsi);
265 /* Disable Pen Detect Circuit */
266 da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 1, 0);
268 /* Disable ADC */
269 da9052_ts_adc_toggle(tsi, false);
271 error = da9052_request_irq(tsi->da9052, DA9052_IRQ_PENDOWN,
272 "pendown-irq", da9052_ts_pendwn_irq, tsi);
273 if (error) {
274 dev_err(tsi->da9052->dev,
275 "Failed to register PENDWN IRQ: %d\n", error);
276 goto err_free_mem;
279 error = da9052_request_irq(tsi->da9052, DA9052_IRQ_TSIREADY,
280 "tsiready-irq", da9052_ts_datardy_irq, tsi);
281 if (error) {
282 dev_err(tsi->da9052->dev,
283 "Failed to register TSIRDY IRQ :%d\n", error);
284 goto err_free_pendwn_irq;
287 /* Mask PEN_DOWN and TSI_READY events */
288 da9052_disable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
289 da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
291 error = da9052_configure_tsi(tsi);
292 if (error)
293 goto err_free_datardy_irq;
295 error = input_register_device(tsi->dev);
296 if (error)
297 goto err_free_datardy_irq;
299 platform_set_drvdata(pdev, tsi);
301 return 0;
303 err_free_datardy_irq:
304 da9052_free_irq(tsi->da9052, DA9052_IRQ_TSIREADY, tsi);
305 err_free_pendwn_irq:
306 da9052_free_irq(tsi->da9052, DA9052_IRQ_PENDOWN, tsi);
307 err_free_mem:
308 kfree(tsi);
309 input_free_device(input_dev);
311 return error;
314 static void da9052_ts_remove(struct platform_device *pdev)
316 struct da9052_tsi *tsi = platform_get_drvdata(pdev);
318 da9052_reg_write(tsi->da9052, DA9052_LDO9_REG, 0x19);
320 da9052_free_irq(tsi->da9052, DA9052_IRQ_TSIREADY, tsi);
321 da9052_free_irq(tsi->da9052, DA9052_IRQ_PENDOWN, tsi);
323 input_unregister_device(tsi->dev);
324 kfree(tsi);
327 static struct platform_driver da9052_tsi_driver = {
328 .probe = da9052_ts_probe,
329 .remove = da9052_ts_remove,
330 .driver = {
331 .name = "da9052-tsi",
335 module_platform_driver(da9052_tsi_driver);
337 MODULE_DESCRIPTION("Touchscreen driver for Dialog Semiconductor DA9052");
338 MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
339 MODULE_LICENSE("GPL");
340 MODULE_ALIAS("platform:da9052-tsi");