1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation.
5  *
6  * h3600 atmel micro companion support, touchscreen subdevice
7  * Author : Alessandro Gardich <gremlin@gremlin.it>
8  * Author : Dmitry Artamonow <mad_soft@inbox.ru>
9  * Author : Linus Walleij <linus.walleij@linaro.org>
10  *
11  */
12 
13 #include <asm/byteorder.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/pm.h>
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/input.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/mfd/ipaq-micro.h>
24 
25 struct touchscreen_data {
26 	struct input_dev *input;
27 	struct ipaq_micro *micro;
28 };
29 
30 static void micro_ts_receive(void *data, int len, unsigned char *msg)
31 {
32 	struct touchscreen_data *ts = data;
33 
34 	if (len == 4) {
35 		input_report_abs(ts->input, ABS_X,
36 				 be16_to_cpup((__be16 *) &msg[2]));
37 		input_report_abs(ts->input, ABS_Y,
38 				 be16_to_cpup((__be16 *) &msg[0]));
39 		input_report_key(ts->input, BTN_TOUCH, 1);
40 		input_sync(ts->input);
41 	} else if (len == 0) {
42 		input_report_abs(ts->input, ABS_X, 0);
43 		input_report_abs(ts->input, ABS_Y, 0);
44 		input_report_key(ts->input, BTN_TOUCH, 0);
45 		input_sync(ts->input);
46 	}
47 }
48 
49 static int micro_ts_probe(struct platform_device *pdev)
50 {
51 	struct touchscreen_data *ts;
52 	int ret;
53 
54 	ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
55 	if (!ts)
56 		return -ENOMEM;
57 	ts->micro = dev_get_drvdata(pdev->dev.parent);
58 
59 	platform_set_drvdata(pdev, ts);
60 
61 	ts->input = devm_input_allocate_device(&pdev->dev);
62 	if (!ts->input) {
63 		dev_err(&pdev->dev, "failed to allocate input device\n");
64 		return -ENOMEM;
65 	}
66 
67 	input_set_capability(ts->input, EV_KEY, BTN_TOUCH);
68 	input_set_capability(ts->input, EV_ABS, ABS_X);
69 	input_set_capability(ts->input, EV_ABS, ABS_Y);
70 	input_set_abs_params(ts->input, ABS_X, 0, 1023, 0, 0);
71 	input_set_abs_params(ts->input, ABS_Y, 0, 1023, 0, 0);
72 
73 	ts->input->name = "ipaq micro ts";
74 
75 	ret = input_register_device(ts->input);
76 	if (ret) {
77 		dev_err(&pdev->dev, "error registering touch input\n");
78 		return ret;
79 	}
80 
81 	spin_lock_irq(&ts->micro->lock);
82 	ts->micro->ts = micro_ts_receive;
83 	ts->micro->ts_data = ts;
84 	spin_unlock_irq(&ts->micro->lock);
85 
86 	dev_info(&pdev->dev, "iPAQ micro touchscreen\n");
87 	return 0;
88 }
89 
90 static int micro_ts_remove(struct platform_device *pdev)
91 {
92 	struct touchscreen_data *ts = platform_get_drvdata(pdev);
93 
94 	spin_lock_irq(&ts->micro->lock);
95 	ts->micro->ts = NULL;
96 	ts->micro->ts_data = NULL;
97 	spin_unlock_irq(&ts->micro->lock);
98 
99 	return 0;
100 }
101 
102 #ifdef CONFIG_PM_SLEEP
103 static int micro_ts_suspend(struct device *dev)
104 {
105 	struct touchscreen_data *ts = dev_get_drvdata(dev);
106 
107 	spin_lock_irq(&ts->micro->lock);
108 	ts->micro->ts = NULL;
109 	ts->micro->ts_data = NULL;
110 	spin_unlock_irq(&ts->micro->lock);
111 	return 0;
112 }
113 
114 static int micro_ts_resume(struct device *dev)
115 {
116 	struct touchscreen_data *ts = dev_get_drvdata(dev);
117 
118 	spin_lock_irq(&ts->micro->lock);
119 	ts->micro->ts = micro_ts_receive;
120 	ts->micro->ts_data = ts;
121 	spin_unlock_irq(&ts->micro->lock);
122 	return 0;
123 }
124 #endif
125 
126 static const struct dev_pm_ops micro_ts_dev_pm_ops = {
127 	SET_SYSTEM_SLEEP_PM_OPS(micro_ts_suspend, micro_ts_resume)
128 };
129 
130 static struct platform_driver micro_ts_device_driver = {
131 	.driver	= {
132 		.name	= "ipaq-micro-ts",
133 		.pm	= &micro_ts_dev_pm_ops,
134 	},
135 	.probe	= micro_ts_probe,
136 	.remove	= micro_ts_remove,
137 };
138 module_platform_driver(micro_ts_device_driver);
139 
140 MODULE_LICENSE("GPL");
141 MODULE_DESCRIPTION("driver for iPAQ Atmel micro touchscreen");
142 MODULE_ALIAS("platform:ipaq-micro-ts");
143