Input: pixcir_i2c_ts - initialize interrupt mode and power mode
[deliverable/linux.git] / drivers / input / touchscreen / pixcir_i2c_ts.c
CommitLineData
36a281e2
JB
1/*
2 * Driver for Pixcir I2C touchscreen controllers.
3 *
4 * Copyright (C) 2010-2011 Pixcir, Inc.
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/delay.h>
21#include <linux/module.h>
22#include <linux/interrupt.h>
23#include <linux/slab.h>
24#include <linux/i2c.h>
25#include <linux/input.h>
26#include <linux/input/pixcir_ts.h>
27
28struct pixcir_i2c_ts_data {
29 struct i2c_client *client;
30 struct input_dev *input;
31 const struct pixcir_ts_platform_data *chip;
3b36fbb0 32 bool running;
36a281e2
JB
33};
34
35static void pixcir_ts_poscheck(struct pixcir_i2c_ts_data *data)
36{
37 struct pixcir_i2c_ts_data *tsdata = data;
38 u8 rdbuf[10], wrbuf[1] = { 0 };
39 u8 touch;
40 int ret;
41
42 ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
43 if (ret != sizeof(wrbuf)) {
44 dev_err(&tsdata->client->dev,
45 "%s: i2c_master_send failed(), ret=%d\n",
46 __func__, ret);
47 return;
48 }
49
50 ret = i2c_master_recv(tsdata->client, rdbuf, sizeof(rdbuf));
51 if (ret != sizeof(rdbuf)) {
52 dev_err(&tsdata->client->dev,
53 "%s: i2c_master_recv failed(), ret=%d\n",
54 __func__, ret);
55 return;
56 }
57
58 touch = rdbuf[0];
59 if (touch) {
60 u16 posx1 = (rdbuf[3] << 8) | rdbuf[2];
61 u16 posy1 = (rdbuf[5] << 8) | rdbuf[4];
62 u16 posx2 = (rdbuf[7] << 8) | rdbuf[6];
63 u16 posy2 = (rdbuf[9] << 8) | rdbuf[8];
64
65 input_report_key(tsdata->input, BTN_TOUCH, 1);
66 input_report_abs(tsdata->input, ABS_X, posx1);
67 input_report_abs(tsdata->input, ABS_Y, posy1);
68
69 input_report_abs(tsdata->input, ABS_MT_POSITION_X, posx1);
70 input_report_abs(tsdata->input, ABS_MT_POSITION_Y, posy1);
71 input_mt_sync(tsdata->input);
72
73 if (touch == 2) {
74 input_report_abs(tsdata->input,
75 ABS_MT_POSITION_X, posx2);
76 input_report_abs(tsdata->input,
77 ABS_MT_POSITION_Y, posy2);
78 input_mt_sync(tsdata->input);
79 }
80 } else {
81 input_report_key(tsdata->input, BTN_TOUCH, 0);
82 }
83
84 input_sync(tsdata->input);
85}
86
87static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
88{
89 struct pixcir_i2c_ts_data *tsdata = dev_id;
90
3b36fbb0 91 while (tsdata->running) {
36a281e2
JB
92 pixcir_ts_poscheck(tsdata);
93
94 if (tsdata->chip->attb_read_val())
95 break;
96
97 msleep(20);
98 }
99
100 return IRQ_HANDLED;
101}
102
3b36fbb0
RQ
103static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
104 enum pixcir_power_mode mode)
105{
106 struct device *dev = &ts->client->dev;
107 int ret;
108
109 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
110 if (ret < 0) {
111 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
112 __func__, PIXCIR_REG_POWER_MODE, ret);
113 return ret;
114 }
115
116 ret &= ~PIXCIR_POWER_MODE_MASK;
117 ret |= mode;
118
119 /* Always AUTO_IDLE */
120 ret |= PIXCIR_POWER_ALLOW_IDLE;
121
122 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
123 if (ret < 0) {
124 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
125 __func__, PIXCIR_REG_POWER_MODE, ret);
126 return ret;
127 }
128
129 return 0;
130}
131
132/*
133 * Set the interrupt mode for the device i.e. ATTB line behaviour
134 *
135 * @polarity : 1 for active high, 0 for active low.
136 */
137static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
138 enum pixcir_int_mode mode, bool polarity)
139{
140 struct device *dev = &ts->client->dev;
141 int ret;
142
143 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
144 if (ret < 0) {
145 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
146 __func__, PIXCIR_REG_INT_MODE, ret);
147 return ret;
148 }
149
150 ret &= ~PIXCIR_INT_MODE_MASK;
151 ret |= mode;
152
153 if (polarity)
154 ret |= PIXCIR_INT_POL_HIGH;
155 else
156 ret &= ~PIXCIR_INT_POL_HIGH;
157
158 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
159 if (ret < 0) {
160 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
161 __func__, PIXCIR_REG_INT_MODE, ret);
162 return ret;
163 }
164
165 return 0;
166}
167
168/*
169 * Enable/disable interrupt generation
170 */
171static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
172{
173 struct device *dev = &ts->client->dev;
174 int ret;
175
176 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
177 if (ret < 0) {
178 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
179 __func__, PIXCIR_REG_INT_MODE, ret);
180 return ret;
181 }
182
183 if (enable)
184 ret |= PIXCIR_INT_ENABLE;
185 else
186 ret &= ~PIXCIR_INT_ENABLE;
187
188 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
189 if (ret < 0) {
190 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
191 __func__, PIXCIR_REG_INT_MODE, ret);
192 return ret;
193 }
194
195 return 0;
196}
197
198static int pixcir_start(struct pixcir_i2c_ts_data *ts)
199{
200 struct device *dev = &ts->client->dev;
201 int error;
202
203 /* LEVEL_TOUCH interrupt with active low polarity */
204 error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
205 if (error) {
206 dev_err(dev, "Failed to set interrupt mode: %d\n", error);
207 return error;
208 }
209
210 ts->running = true;
211 mb(); /* Update status before IRQ can fire */
212
213 /* enable interrupt generation */
214 error = pixcir_int_enable(ts, true);
215 if (error) {
216 dev_err(dev, "Failed to enable interrupt generation: %d\n",
217 error);
218 return error;
219 }
220
221 return 0;
222}
223
224static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
225{
226 int error;
227
228 /* Disable interrupt generation */
229 error = pixcir_int_enable(ts, false);
230 if (error) {
231 dev_err(&ts->client->dev,
232 "Failed to disable interrupt generation: %d\n",
233 error);
234 return error;
235 }
236
237 /* Exit ISR if running, no more report parsing */
238 ts->running = false;
239 mb(); /* update status before we synchronize irq */
240
241 /* Wait till running ISR is complete */
242 synchronize_irq(ts->client->irq);
243
244 return 0;
245}
246
247static int pixcir_input_open(struct input_dev *dev)
248{
249 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
250
251 return pixcir_start(ts);
252}
253
254static void pixcir_input_close(struct input_dev *dev)
255{
256 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
257
258 pixcir_stop(ts);
259}
260
36a281e2
JB
261#ifdef CONFIG_PM_SLEEP
262static int pixcir_i2c_ts_suspend(struct device *dev)
263{
264 struct i2c_client *client = to_i2c_client(dev);
265
266 if (device_may_wakeup(&client->dev))
267 enable_irq_wake(client->irq);
268
269 return 0;
270}
271
272static int pixcir_i2c_ts_resume(struct device *dev)
273{
274 struct i2c_client *client = to_i2c_client(dev);
275
276 if (device_may_wakeup(&client->dev))
277 disable_irq_wake(client->irq);
278
279 return 0;
280}
281#endif
282
283static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
284 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
285
5298cc4c 286static int pixcir_i2c_ts_probe(struct i2c_client *client,
36a281e2
JB
287 const struct i2c_device_id *id)
288{
c838cb3d
JH
289 const struct pixcir_ts_platform_data *pdata =
290 dev_get_platdata(&client->dev);
e9d4718d 291 struct device *dev = &client->dev;
36a281e2
JB
292 struct pixcir_i2c_ts_data *tsdata;
293 struct input_dev *input;
294 int error;
295
296 if (!pdata) {
297 dev_err(&client->dev, "platform data not defined\n");
298 return -EINVAL;
299 }
300
e9d4718d
RQ
301 tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
302 if (!tsdata)
303 return -ENOMEM;
304
305 input = devm_input_allocate_device(dev);
306 if (!input) {
307 dev_err(dev, "Failed to allocate input device\n");
308 return -ENOMEM;
36a281e2
JB
309 }
310
311 tsdata->client = client;
312 tsdata->input = input;
313 tsdata->chip = pdata;
314
315 input->name = client->name;
316 input->id.bustype = BUS_I2C;
3b36fbb0
RQ
317 input->open = pixcir_input_open;
318 input->close = pixcir_input_close;
36a281e2
JB
319 input->dev.parent = &client->dev;
320
321 __set_bit(EV_KEY, input->evbit);
322 __set_bit(EV_ABS, input->evbit);
323 __set_bit(BTN_TOUCH, input->keybit);
324 input_set_abs_params(input, ABS_X, 0, pdata->x_max, 0, 0);
325 input_set_abs_params(input, ABS_Y, 0, pdata->y_max, 0, 0);
326 input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
327 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
328
329 input_set_drvdata(input, tsdata);
330
e9d4718d
RQ
331 error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
332 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
333 client->name, tsdata);
36a281e2 334 if (error) {
e9d4718d
RQ
335 dev_err(dev, "failed to request irq %d\n", client->irq);
336 return error;
36a281e2
JB
337 }
338
3b36fbb0
RQ
339 /* Always be in IDLE mode to save power, device supports auto wake */
340 error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
341 if (error) {
342 dev_err(dev, "Failed to set IDLE mode\n");
343 return error;
344 }
345
346 /* Stop device till opened */
347 error = pixcir_stop(tsdata);
348 if (error)
349 return error;
350
36a281e2
JB
351 error = input_register_device(input);
352 if (error)
e9d4718d 353 return error;
36a281e2 354
36a281e2
JB
355 device_init_wakeup(&client->dev, 1);
356
357 return 0;
36a281e2
JB
358}
359
e2619cf7 360static int pixcir_i2c_ts_remove(struct i2c_client *client)
36a281e2 361{
36a281e2
JB
362 device_init_wakeup(&client->dev, 0);
363
36a281e2
JB
364 return 0;
365}
366
367static const struct i2c_device_id pixcir_i2c_ts_id[] = {
368 { "pixcir_ts", 0 },
369 { }
370};
371MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
372
373static struct i2c_driver pixcir_i2c_ts_driver = {
374 .driver = {
375 .owner = THIS_MODULE,
376 .name = "pixcir_ts",
377 .pm = &pixcir_dev_pm_ops,
378 },
379 .probe = pixcir_i2c_ts_probe,
1cb0aa88 380 .remove = pixcir_i2c_ts_remove,
36a281e2
JB
381 .id_table = pixcir_i2c_ts_id,
382};
383
4a533835 384module_i2c_driver(pixcir_i2c_ts_driver);
36a281e2
JB
385
386MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
387MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
388MODULE_LICENSE("GPL");
This page took 0.061799 seconds and 5 git commands to generate.