xref: /linux/drivers/input/touchscreen/ipaq-micro-ts.c (revision ffd01c3bcc1af4d8c3e7949152af0d9fe3d1fda5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * h3600 atmel micro companion support, touchscreen subdevice
5  * Author : Alessandro Gardich <gremlin@gremlin.it>
6  * Author : Dmitry Artamonow <mad_soft@inbox.ru>
7  * Author : Linus Walleij <linus.walleij@linaro.org>
8  */
9 
10 #include <asm/byteorder.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/pm.h>
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/input.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/mfd/ipaq-micro.h>
21 
22 struct touchscreen_data {
23 	struct input_dev *input;
24 	struct ipaq_micro *micro;
25 };
26 
27 static void micro_ts_receive(void *data, int len, unsigned char *msg)
28 {
29 	struct touchscreen_data *ts = data;
30 
31 	if (len == 4) {
32 		input_report_abs(ts->input, ABS_X,
33 				 be16_to_cpup((__be16 *) &msg[2]));
34 		input_report_abs(ts->input, ABS_Y,
35 				 be16_to_cpup((__be16 *) &msg[0]));
36 		input_report_key(ts->input, BTN_TOUCH, 1);
37 		input_sync(ts->input);
38 	} else if (len == 0) {
39 		input_report_abs(ts->input, ABS_X, 0);
40 		input_report_abs(ts->input, ABS_Y, 0);
41 		input_report_key(ts->input, BTN_TOUCH, 0);
42 		input_sync(ts->input);
43 	}
44 }
45 
46 static void micro_ts_toggle_receive(struct touchscreen_data *ts, bool enable)
47 {
48 	struct ipaq_micro *micro = ts->micro;
49 
50 	guard(spinlock_irq)(&micro->lock);
51 
52 	if (enable) {
53 		micro->ts = micro_ts_receive;
54 		micro->ts_data = ts;
55 	} else {
56 		micro->ts = NULL;
57 		micro->ts_data = NULL;
58 	}
59 }
60 
61 static int micro_ts_open(struct input_dev *input)
62 {
63 	struct touchscreen_data *ts = input_get_drvdata(input);
64 
65 	micro_ts_toggle_receive(ts, true);
66 
67 	return 0;
68 }
69 
70 static void micro_ts_close(struct input_dev *input)
71 {
72 	struct touchscreen_data *ts = input_get_drvdata(input);
73 
74 	micro_ts_toggle_receive(ts, false);
75 }
76 
77 static int micro_ts_probe(struct platform_device *pdev)
78 {
79 	struct ipaq_micro *micro = dev_get_drvdata(pdev->dev.parent);
80 	struct touchscreen_data *ts;
81 	int error;
82 
83 	ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
84 	if (!ts)
85 		return -ENOMEM;
86 
87 	ts->micro = micro;
88 
89 	ts->input = devm_input_allocate_device(&pdev->dev);
90 	if (!ts->input) {
91 		dev_err(&pdev->dev, "failed to allocate input device\n");
92 		return -ENOMEM;
93 	}
94 
95 	ts->input->name = "ipaq micro ts";
96 	ts->input->open = micro_ts_open;
97 	ts->input->close = micro_ts_close;
98 
99 	input_set_drvdata(ts->input, ts);
100 
101 	input_set_capability(ts->input, EV_KEY, BTN_TOUCH);
102 	input_set_capability(ts->input, EV_ABS, ABS_X);
103 	input_set_capability(ts->input, EV_ABS, ABS_Y);
104 	input_set_abs_params(ts->input, ABS_X, 0, 1023, 0, 0);
105 	input_set_abs_params(ts->input, ABS_Y, 0, 1023, 0, 0);
106 
107 	error = input_register_device(ts->input);
108 	if (error) {
109 		dev_err(&pdev->dev, "error registering touch input\n");
110 		return error;
111 	}
112 
113 	platform_set_drvdata(pdev, ts);
114 
115 	dev_info(&pdev->dev, "iPAQ micro touchscreen\n");
116 
117 	return 0;
118 }
119 
120 static int micro_ts_suspend(struct device *dev)
121 {
122 	struct touchscreen_data *ts = dev_get_drvdata(dev);
123 
124 	micro_ts_toggle_receive(ts, false);
125 
126 	return 0;
127 }
128 
129 static int micro_ts_resume(struct device *dev)
130 {
131 	struct touchscreen_data *ts = dev_get_drvdata(dev);
132 	struct input_dev *input = ts->input;
133 
134 	guard(mutex)(&input->mutex);
135 
136 	if (input_device_enabled(input))
137 		micro_ts_toggle_receive(ts, true);
138 
139 	return 0;
140 }
141 
142 static DEFINE_SIMPLE_DEV_PM_OPS(micro_ts_dev_pm_ops,
143 				micro_ts_suspend, micro_ts_resume);
144 
145 static struct platform_driver micro_ts_device_driver = {
146 	.driver	= {
147 		.name	= "ipaq-micro-ts",
148 		.pm	= pm_sleep_ptr(&micro_ts_dev_pm_ops),
149 	},
150 	.probe	= micro_ts_probe,
151 };
152 module_platform_driver(micro_ts_device_driver);
153 
154 MODULE_LICENSE("GPL");
155 MODULE_DESCRIPTION("driver for iPAQ Atmel micro touchscreen");
156 MODULE_ALIAS("platform:ipaq-micro-ts");
157