Commit | Line | Data |
---|---|---|
dda2af74 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
885c316d MD |
2 | /* |
3 | * Touch Screen driver for Renesas MIGO-R Platform | |
4 | * | |
5 | * Copyright (c) 2008 Magnus Damm | |
6 | * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>, | |
7 | * Kenati Technologies Pvt Ltd. | |
885c316d MD |
8 | */ |
9 | #include <linux/module.h> | |
10 | #include <linux/kernel.h> | |
11 | #include <linux/input.h> | |
12 | #include <linux/interrupt.h> | |
d72e64ec | 13 | #include <linux/pm.h> |
5a0e3ad6 | 14 | #include <linux/slab.h> |
885c316d MD |
15 | #include <asm/io.h> |
16 | #include <linux/i2c.h> | |
17 | #include <linux/timer.h> | |
18 | ||
19 | #define EVENT_PENDOWN 1 | |
20 | #define EVENT_REPEAT 2 | |
21 | #define EVENT_PENUP 3 | |
22 | ||
23 | struct migor_ts_priv { | |
24 | struct i2c_client *client; | |
25 | struct input_dev *input; | |
885c316d MD |
26 | int irq; |
27 | }; | |
28 | ||
29 | static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11, | |
30 | 0x01, 0x06, 0x07, }; | |
31 | static const u_int8_t migor_ts_dis_seq[17] = { }; | |
32 | ||
468792eb | 33 | static irqreturn_t migor_ts_isr(int irq, void *dev_id) |
885c316d | 34 | { |
468792eb | 35 | struct migor_ts_priv *priv = dev_id; |
885c316d MD |
36 | unsigned short xpos, ypos; |
37 | unsigned char event; | |
38 | u_int8_t buf[16]; | |
39 | ||
468792eb DT |
40 | /* |
41 | * The touch screen controller chip is hooked up to the CPU | |
42 | * using I2C and a single interrupt line. The interrupt line | |
43 | * is pulled low whenever someone taps the screen. To deassert | |
44 | * the interrupt line we need to acknowledge the interrupt by | |
45 | * communicating with the controller over the slow i2c bus. | |
46 | * | |
47 | * Since I2C bus controller may sleep we are using threaded | |
48 | * IRQ here. | |
49 | */ | |
50 | ||
885c316d MD |
51 | memset(buf, 0, sizeof(buf)); |
52 | ||
53 | /* Set Index 0 */ | |
54 | buf[0] = 0; | |
55 | if (i2c_master_send(priv->client, buf, 1) != 1) { | |
56 | dev_err(&priv->client->dev, "Unable to write i2c index\n"); | |
57 | goto out; | |
58 | } | |
59 | ||
60 | /* Now do Page Read */ | |
61 | if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) { | |
62 | dev_err(&priv->client->dev, "Unable to read i2c page\n"); | |
63 | goto out; | |
64 | } | |
65 | ||
66 | ypos = ((buf[9] & 0x03) << 8 | buf[8]); | |
67 | xpos = ((buf[11] & 0x03) << 8 | buf[10]); | |
68 | event = buf[12]; | |
69 | ||
468792eb DT |
70 | switch (event) { |
71 | case EVENT_PENDOWN: | |
72 | case EVENT_REPEAT: | |
885c316d MD |
73 | input_report_key(priv->input, BTN_TOUCH, 1); |
74 | input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/ | |
75 | input_report_abs(priv->input, ABS_Y, xpos); | |
76 | input_sync(priv->input); | |
468792eb DT |
77 | break; |
78 | ||
79 | case EVENT_PENUP: | |
885c316d MD |
80 | input_report_key(priv->input, BTN_TOUCH, 0); |
81 | input_sync(priv->input); | |
468792eb | 82 | break; |
885c316d | 83 | } |
885c316d | 84 | |
468792eb | 85 | out: |
885c316d MD |
86 | return IRQ_HANDLED; |
87 | } | |
88 | ||
885c316d MD |
89 | static int migor_ts_open(struct input_dev *dev) |
90 | { | |
91 | struct migor_ts_priv *priv = input_get_drvdata(dev); | |
92 | struct i2c_client *client = priv->client; | |
93 | int count; | |
94 | ||
95 | /* enable controller */ | |
96 | count = i2c_master_send(client, migor_ts_ena_seq, | |
97 | sizeof(migor_ts_ena_seq)); | |
98 | if (count != sizeof(migor_ts_ena_seq)) { | |
99 | dev_err(&client->dev, "Unable to enable touchscreen.\n"); | |
100 | return -ENXIO; | |
101 | } | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
106 | static void migor_ts_close(struct input_dev *dev) | |
107 | { | |
108 | struct migor_ts_priv *priv = input_get_drvdata(dev); | |
109 | struct i2c_client *client = priv->client; | |
110 | ||
111 | disable_irq(priv->irq); | |
112 | ||
885c316d MD |
113 | /* disable controller */ |
114 | i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq)); | |
115 | ||
116 | enable_irq(priv->irq); | |
117 | } | |
118 | ||
ff3de863 | 119 | static int migor_ts_probe(struct i2c_client *client) |
885c316d MD |
120 | { |
121 | struct migor_ts_priv *priv; | |
122 | struct input_dev *input; | |
123 | int error; | |
124 | ||
125 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
885c316d | 126 | input = input_allocate_device(); |
ff4d0492 DT |
127 | if (!priv || !input) { |
128 | dev_err(&client->dev, "failed to allocate memory\n"); | |
885c316d | 129 | error = -ENOMEM; |
ff4d0492 | 130 | goto err_free_mem; |
885c316d MD |
131 | } |
132 | ||
ff4d0492 DT |
133 | priv->client = client; |
134 | priv->input = input; | |
135 | priv->irq = client->irq; | |
136 | ||
885c316d | 137 | input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
ff4d0492 DT |
138 | |
139 | __set_bit(BTN_TOUCH, input->keybit); | |
885c316d MD |
140 | |
141 | input_set_abs_params(input, ABS_X, 95, 955, 0, 0); | |
142 | input_set_abs_params(input, ABS_Y, 85, 935, 0, 0); | |
143 | ||
b6ce9ad0 | 144 | input->name = client->name; |
885c316d MD |
145 | input->id.bustype = BUS_I2C; |
146 | input->dev.parent = &client->dev; | |
147 | ||
148 | input->open = migor_ts_open; | |
149 | input->close = migor_ts_close; | |
150 | ||
151 | input_set_drvdata(input, priv); | |
152 | ||
468792eb DT |
153 | error = request_threaded_irq(priv->irq, NULL, migor_ts_isr, |
154 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, | |
155 | client->name, priv); | |
885c316d MD |
156 | if (error) { |
157 | dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); | |
ff4d0492 | 158 | goto err_free_mem; |
885c316d MD |
159 | } |
160 | ||
ff4d0492 DT |
161 | error = input_register_device(input); |
162 | if (error) | |
163 | goto err_free_irq; | |
164 | ||
ec208612 | 165 | i2c_set_clientdata(client, priv); |
0935ade0 | 166 | device_init_wakeup(&client->dev, 1); |
ff4d0492 | 167 | |
885c316d MD |
168 | return 0; |
169 | ||
ff4d0492 DT |
170 | err_free_irq: |
171 | free_irq(priv->irq, priv); | |
172 | err_free_mem: | |
885c316d MD |
173 | input_free_device(input); |
174 | kfree(priv); | |
885c316d MD |
175 | return error; |
176 | } | |
177 | ||
ed5c2f5f | 178 | static void migor_ts_remove(struct i2c_client *client) |
885c316d | 179 | { |
ec208612 | 180 | struct migor_ts_priv *priv = i2c_get_clientdata(client); |
885c316d MD |
181 | |
182 | free_irq(priv->irq, priv); | |
183 | input_unregister_device(priv->input); | |
184 | kfree(priv); | |
185 | ||
186 | dev_set_drvdata(&client->dev, NULL); | |
885c316d MD |
187 | } |
188 | ||
3a841b3e | 189 | static int migor_ts_suspend(struct device *dev) |
0935ade0 | 190 | { |
d72e64ec | 191 | struct i2c_client *client = to_i2c_client(dev); |
ec208612 | 192 | struct migor_ts_priv *priv = i2c_get_clientdata(client); |
0935ade0 MD |
193 | |
194 | if (device_may_wakeup(&client->dev)) | |
195 | enable_irq_wake(priv->irq); | |
196 | ||
197 | return 0; | |
198 | } | |
199 | ||
3a841b3e | 200 | static int migor_ts_resume(struct device *dev) |
0935ade0 | 201 | { |
d72e64ec | 202 | struct i2c_client *client = to_i2c_client(dev); |
ec208612 | 203 | struct migor_ts_priv *priv = i2c_get_clientdata(client); |
0935ade0 MD |
204 | |
205 | if (device_may_wakeup(&client->dev)) | |
206 | disable_irq_wake(priv->irq); | |
207 | ||
208 | return 0; | |
209 | } | |
210 | ||
3a841b3e | 211 | static DEFINE_SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume); |
d72e64ec | 212 | |
b6ce9ad0 | 213 | static const struct i2c_device_id migor_ts_id[] = { |
5852f2af | 214 | { "migor_ts" }, |
b6ce9ad0 MD |
215 | { } |
216 | }; | |
8f4decc3 | 217 | MODULE_DEVICE_TABLE(i2c, migor_ts_id); |
b6ce9ad0 | 218 | |
885c316d MD |
219 | static struct i2c_driver migor_ts_driver = { |
220 | .driver = { | |
221 | .name = "migor_ts", | |
3a841b3e | 222 | .pm = pm_sleep_ptr(&migor_ts_pm), |
885c316d | 223 | }, |
d8bde56d | 224 | .probe = migor_ts_probe, |
885c316d | 225 | .remove = migor_ts_remove, |
b6ce9ad0 | 226 | .id_table = migor_ts_id, |
885c316d MD |
227 | }; |
228 | ||
1b92c1cf | 229 | module_i2c_driver(migor_ts_driver); |
885c316d MD |
230 | |
231 | MODULE_DESCRIPTION("MigoR Touchscreen driver"); | |
232 | MODULE_AUTHOR("Magnus Damm <damm@opensource.se>"); | |
233 | MODULE_LICENSE("GPL"); |