blob: a5a83139aad05c7cc975ea14eafd9f0f1609cddf [file] [log] [blame]
Jianchun Bian36a281e2011-12-30 15:16:21 -08001/*
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>
Roger Quadros62e65b72014-07-28 09:58:54 -070026#include <linux/input/mt.h>
Roger Quadros0dfc8d42014-05-18 22:46:43 -070027#include <linux/gpio.h>
Dmitry Torokhovcb4a5f02015-07-06 11:56:21 -070028#include <linux/gpio/consumer.h>
Roger Quadrosa4054592014-07-28 10:05:39 -070029#include <linux/of.h>
Roger Quadrosa4054592014-07-28 10:05:39 -070030#include <linux/of_device.h>
Dmitry Torokhov28a74c02015-07-06 11:48:47 -070031#include <linux/platform_data/pixcir_i2c_ts.h>
Jianchun Bian36a281e2011-12-30 15:16:21 -080032
Roger Quadros36874c72014-07-28 10:01:07 -070033#define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
Roger Quadros62e65b72014-07-28 09:58:54 -070034
Jianchun Bian36a281e2011-12-30 15:16:21 -080035struct pixcir_i2c_ts_data {
36 struct i2c_client *client;
37 struct input_dev *input;
Dmitry Torokhovcb4a5f02015-07-06 11:56:21 -070038 struct gpio_desc *gpio_attb;
Roger Quadros40929162015-07-06 13:27:43 -070039 struct gpio_desc *gpio_reset;
Roger Quadros36874c72014-07-28 10:01:07 -070040 const struct pixcir_ts_platform_data *pdata;
Roger Quadros3b36fbb2014-05-18 22:44:35 -070041 bool running;
Roger Quadros36874c72014-07-28 10:01:07 -070042 int max_fingers; /* Max fingers supported in this instance */
Jianchun Bian36a281e2011-12-30 15:16:21 -080043};
44
Roger Quadros62e65b72014-07-28 09:58:54 -070045struct pixcir_touch {
46 int x;
47 int y;
Roger Quadros36874c72014-07-28 10:01:07 -070048 int id;
Roger Quadros62e65b72014-07-28 09:58:54 -070049};
50
51struct pixcir_report_data {
52 int num_touches;
53 struct pixcir_touch touches[PIXCIR_MAX_SLOTS];
54};
55
56static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata,
57 struct pixcir_report_data *report)
Jianchun Bian36a281e2011-12-30 15:16:21 -080058{
Roger Quadros36874c72014-07-28 10:01:07 -070059 u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5];
60 u8 wrbuf[1] = { 0 };
Roger Quadros62e65b72014-07-28 09:58:54 -070061 u8 *bufptr;
Jianchun Bian36a281e2011-12-30 15:16:21 -080062 u8 touch;
Roger Quadros62e65b72014-07-28 09:58:54 -070063 int ret, i;
Roger Quadros36874c72014-07-28 10:01:07 -070064 int readsize;
65 const struct pixcir_i2c_chip_data *chip = &tsdata->pdata->chip;
Roger Quadros62e65b72014-07-28 09:58:54 -070066
67 memset(report, 0, sizeof(struct pixcir_report_data));
Jianchun Bian36a281e2011-12-30 15:16:21 -080068
Roger Quadros36874c72014-07-28 10:01:07 -070069 i = chip->has_hw_ids ? 1 : 0;
70 readsize = 2 + tsdata->max_fingers * (4 + i);
71 if (readsize > sizeof(rdbuf))
72 readsize = sizeof(rdbuf);
73
Jianchun Bian36a281e2011-12-30 15:16:21 -080074 ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
75 if (ret != sizeof(wrbuf)) {
76 dev_err(&tsdata->client->dev,
77 "%s: i2c_master_send failed(), ret=%d\n",
78 __func__, ret);
79 return;
80 }
81
Roger Quadros36874c72014-07-28 10:01:07 -070082 ret = i2c_master_recv(tsdata->client, rdbuf, readsize);
Jianchun Bian36a281e2011-12-30 15:16:21 -080083 if (ret != sizeof(rdbuf)) {
84 dev_err(&tsdata->client->dev,
85 "%s: i2c_master_recv failed(), ret=%d\n",
86 __func__, ret);
87 return;
88 }
89
Roger Quadros62e65b72014-07-28 09:58:54 -070090 touch = rdbuf[0] & 0x7;
Roger Quadros36874c72014-07-28 10:01:07 -070091 if (touch > tsdata->max_fingers)
92 touch = tsdata->max_fingers;
Jianchun Bian36a281e2011-12-30 15:16:21 -080093
Roger Quadros62e65b72014-07-28 09:58:54 -070094 report->num_touches = touch;
95 bufptr = &rdbuf[2];
Jianchun Bian36a281e2011-12-30 15:16:21 -080096
Roger Quadros62e65b72014-07-28 09:58:54 -070097 for (i = 0; i < touch; i++) {
98 report->touches[i].x = (bufptr[1] << 8) | bufptr[0];
99 report->touches[i].y = (bufptr[3] << 8) | bufptr[2];
Jianchun Bian36a281e2011-12-30 15:16:21 -0800100
Roger Quadros36874c72014-07-28 10:01:07 -0700101 if (chip->has_hw_ids) {
102 report->touches[i].id = bufptr[4];
103 bufptr = bufptr + 5;
104 } else {
105 bufptr = bufptr + 4;
106 }
Roger Quadros62e65b72014-07-28 09:58:54 -0700107 }
108}
109
110static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts,
111 struct pixcir_report_data *report)
112{
113 struct input_mt_pos pos[PIXCIR_MAX_SLOTS];
114 int slots[PIXCIR_MAX_SLOTS];
115 struct pixcir_touch *touch;
116 int n, i, slot;
117 struct device *dev = &ts->client->dev;
Roger Quadros36874c72014-07-28 10:01:07 -0700118 const struct pixcir_i2c_chip_data *chip = &ts->pdata->chip;
Roger Quadros62e65b72014-07-28 09:58:54 -0700119
120 n = report->num_touches;
121 if (n > PIXCIR_MAX_SLOTS)
122 n = PIXCIR_MAX_SLOTS;
123
Roger Quadros36874c72014-07-28 10:01:07 -0700124 if (!chip->has_hw_ids) {
125 for (i = 0; i < n; i++) {
126 touch = &report->touches[i];
127 pos[i].x = touch->x;
128 pos[i].y = touch->y;
129 }
130
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800131 input_mt_assign_slots(ts->input, slots, pos, n, 0);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800132 }
133
Roger Quadros62e65b72014-07-28 09:58:54 -0700134 for (i = 0; i < n; i++) {
135 touch = &report->touches[i];
Roger Quadros36874c72014-07-28 10:01:07 -0700136
137 if (chip->has_hw_ids) {
138 slot = input_mt_get_slot_by_key(ts->input, touch->id);
139 if (slot < 0) {
140 dev_dbg(dev, "no free slot for id 0x%x\n",
141 touch->id);
142 continue;
143 }
144 } else {
145 slot = slots[i];
146 }
Roger Quadros62e65b72014-07-28 09:58:54 -0700147
148 input_mt_slot(ts->input, slot);
149 input_mt_report_slot_state(ts->input,
150 MT_TOOL_FINGER, true);
151
152 input_event(ts->input, EV_ABS, ABS_MT_POSITION_X, touch->x);
153 input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y, touch->y);
154
155 dev_dbg(dev, "%d: slot %d, x %d, y %d\n",
156 i, slot, touch->x, touch->y);
157 }
158
159 input_mt_sync_frame(ts->input);
160 input_sync(ts->input);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800161}
162
163static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
164{
165 struct pixcir_i2c_ts_data *tsdata = dev_id;
Roger Quadros62e65b72014-07-28 09:58:54 -0700166 struct pixcir_report_data report;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800167
Roger Quadros3b36fbb2014-05-18 22:44:35 -0700168 while (tsdata->running) {
Roger Quadros62e65b72014-07-28 09:58:54 -0700169 /* parse packet */
170 pixcir_ts_parse(tsdata, &report);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800171
Roger Quadros62e65b72014-07-28 09:58:54 -0700172 /* report it */
173 pixcir_ts_report(tsdata, &report);
174
Dmitry Torokhov127520c2015-07-06 13:27:00 -0700175 if (gpiod_get_value_cansleep(tsdata->gpio_attb)) {
Roger Quadros62e65b72014-07-28 09:58:54 -0700176 if (report.num_touches) {
177 /*
178 * Last report with no finger up?
179 * Do it now then.
180 */
181 input_mt_sync_frame(tsdata->input);
182 input_sync(tsdata->input);
183 }
Jianchun Bian36a281e2011-12-30 15:16:21 -0800184 break;
Roger Quadros62e65b72014-07-28 09:58:54 -0700185 }
Jianchun Bian36a281e2011-12-30 15:16:21 -0800186
187 msleep(20);
188 }
189
190 return IRQ_HANDLED;
191}
192
Roger Quadros40929162015-07-06 13:27:43 -0700193static void pixcir_reset(struct pixcir_i2c_ts_data *tsdata)
194{
195 if (!IS_ERR_OR_NULL(tsdata->gpio_reset)) {
196 gpiod_set_value_cansleep(tsdata->gpio_reset, 1);
197 ndelay(100); /* datasheet section 1.2.3 says 80ns min. */
198 gpiod_set_value_cansleep(tsdata->gpio_reset, 0);
199 /* wait for controller ready. 100ms guess. */
200 msleep(100);
201 }
202}
203
Roger Quadros3b36fbb2014-05-18 22:44:35 -0700204static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
205 enum pixcir_power_mode mode)
206{
207 struct device *dev = &ts->client->dev;
208 int ret;
209
210 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
211 if (ret < 0) {
212 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
213 __func__, PIXCIR_REG_POWER_MODE, ret);
214 return ret;
215 }
216
217 ret &= ~PIXCIR_POWER_MODE_MASK;
218 ret |= mode;
219
220 /* Always AUTO_IDLE */
221 ret |= PIXCIR_POWER_ALLOW_IDLE;
222
223 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
224 if (ret < 0) {
225 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
226 __func__, PIXCIR_REG_POWER_MODE, ret);
227 return ret;
228 }
229
230 return 0;
231}
232
233/*
234 * Set the interrupt mode for the device i.e. ATTB line behaviour
235 *
236 * @polarity : 1 for active high, 0 for active low.
237 */
238static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
239 enum pixcir_int_mode mode, bool polarity)
240{
241 struct device *dev = &ts->client->dev;
242 int ret;
243
244 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
245 if (ret < 0) {
246 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
247 __func__, PIXCIR_REG_INT_MODE, ret);
248 return ret;
249 }
250
251 ret &= ~PIXCIR_INT_MODE_MASK;
252 ret |= mode;
253
254 if (polarity)
255 ret |= PIXCIR_INT_POL_HIGH;
256 else
257 ret &= ~PIXCIR_INT_POL_HIGH;
258
259 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
260 if (ret < 0) {
261 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
262 __func__, PIXCIR_REG_INT_MODE, ret);
263 return ret;
264 }
265
266 return 0;
267}
268
269/*
270 * Enable/disable interrupt generation
271 */
272static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
273{
274 struct device *dev = &ts->client->dev;
275 int ret;
276
277 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
278 if (ret < 0) {
279 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
280 __func__, PIXCIR_REG_INT_MODE, ret);
281 return ret;
282 }
283
284 if (enable)
285 ret |= PIXCIR_INT_ENABLE;
286 else
287 ret &= ~PIXCIR_INT_ENABLE;
288
289 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
290 if (ret < 0) {
291 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
292 __func__, PIXCIR_REG_INT_MODE, ret);
293 return ret;
294 }
295
296 return 0;
297}
298
299static int pixcir_start(struct pixcir_i2c_ts_data *ts)
300{
301 struct device *dev = &ts->client->dev;
302 int error;
303
304 /* LEVEL_TOUCH interrupt with active low polarity */
305 error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
306 if (error) {
307 dev_err(dev, "Failed to set interrupt mode: %d\n", error);
308 return error;
309 }
310
311 ts->running = true;
312 mb(); /* Update status before IRQ can fire */
313
314 /* enable interrupt generation */
315 error = pixcir_int_enable(ts, true);
316 if (error) {
317 dev_err(dev, "Failed to enable interrupt generation: %d\n",
318 error);
319 return error;
320 }
321
322 return 0;
323}
324
325static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
326{
327 int error;
328
329 /* Disable interrupt generation */
330 error = pixcir_int_enable(ts, false);
331 if (error) {
332 dev_err(&ts->client->dev,
333 "Failed to disable interrupt generation: %d\n",
334 error);
335 return error;
336 }
337
338 /* Exit ISR if running, no more report parsing */
339 ts->running = false;
340 mb(); /* update status before we synchronize irq */
341
342 /* Wait till running ISR is complete */
343 synchronize_irq(ts->client->irq);
344
345 return 0;
346}
347
348static int pixcir_input_open(struct input_dev *dev)
349{
350 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
351
352 return pixcir_start(ts);
353}
354
355static void pixcir_input_close(struct input_dev *dev)
356{
357 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
358
359 pixcir_stop(ts);
360}
361
Jingoo Han02b6a582014-11-02 00:04:14 -0700362static int __maybe_unused pixcir_i2c_ts_suspend(struct device *dev)
Jianchun Bian36a281e2011-12-30 15:16:21 -0800363{
364 struct i2c_client *client = to_i2c_client(dev);
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700365 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
366 struct input_dev *input = ts->input;
367 int ret = 0;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800368
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700369 mutex_lock(&input->mutex);
370
371 if (device_may_wakeup(&client->dev)) {
372 if (!input->users) {
373 ret = pixcir_start(ts);
374 if (ret) {
375 dev_err(dev, "Failed to start\n");
376 goto unlock;
377 }
378 }
379
Jianchun Bian36a281e2011-12-30 15:16:21 -0800380 enable_irq_wake(client->irq);
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700381 } else if (input->users) {
382 ret = pixcir_stop(ts);
383 }
Jianchun Bian36a281e2011-12-30 15:16:21 -0800384
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700385unlock:
386 mutex_unlock(&input->mutex);
387
388 return ret;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800389}
390
Jingoo Han02b6a582014-11-02 00:04:14 -0700391static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev)
Jianchun Bian36a281e2011-12-30 15:16:21 -0800392{
393 struct i2c_client *client = to_i2c_client(dev);
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700394 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
395 struct input_dev *input = ts->input;
396 int ret = 0;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800397
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700398 mutex_lock(&input->mutex);
399
400 if (device_may_wakeup(&client->dev)) {
Jianchun Bian36a281e2011-12-30 15:16:21 -0800401 disable_irq_wake(client->irq);
402
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700403 if (!input->users) {
404 ret = pixcir_stop(ts);
405 if (ret) {
406 dev_err(dev, "Failed to stop\n");
407 goto unlock;
408 }
409 }
410 } else if (input->users) {
411 ret = pixcir_start(ts);
412 }
413
414unlock:
415 mutex_unlock(&input->mutex);
416
417 return ret;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800418}
Jianchun Bian36a281e2011-12-30 15:16:21 -0800419
420static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
421 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
422
Roger Quadrosa4054592014-07-28 10:05:39 -0700423#ifdef CONFIG_OF
424static const struct of_device_id pixcir_of_match[];
425
426static struct pixcir_ts_platform_data *pixcir_parse_dt(struct device *dev)
427{
428 struct pixcir_ts_platform_data *pdata;
429 struct device_node *np = dev->of_node;
430 const struct of_device_id *match;
431
432 match = of_match_device(of_match_ptr(pixcir_of_match), dev);
433 if (!match)
434 return ERR_PTR(-EINVAL);
435
436 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
437 if (!pdata)
438 return ERR_PTR(-ENOMEM);
439
440 pdata->chip = *(const struct pixcir_i2c_chip_data *)match->data;
441
Roger Quadrosa4054592014-07-28 10:05:39 -0700442 if (of_property_read_u32(np, "touchscreen-size-x", &pdata->x_max)) {
443 dev_err(dev, "Failed to get touchscreen-size-x property\n");
444 return ERR_PTR(-EINVAL);
445 }
446 pdata->x_max -= 1;
447
448 if (of_property_read_u32(np, "touchscreen-size-y", &pdata->y_max)) {
449 dev_err(dev, "Failed to get touchscreen-size-y property\n");
450 return ERR_PTR(-EINVAL);
451 }
452 pdata->y_max -= 1;
453
Dmitry Torokhovcb4a5f02015-07-06 11:56:21 -0700454 dev_dbg(dev, "%s: x %d, y %d\n", __func__,
455 pdata->x_max + 1, pdata->y_max + 1);
Roger Quadrosa4054592014-07-28 10:05:39 -0700456
457 return pdata;
458}
459#else
460static struct pixcir_ts_platform_data *pixcir_parse_dt(struct device *dev)
461{
462 return ERR_PTR(-EINVAL);
463}
464#endif
465
Bill Pemberton5298cc42012-11-23 21:38:25 -0800466static int pixcir_i2c_ts_probe(struct i2c_client *client,
Jianchun Bian36a281e2011-12-30 15:16:21 -0800467 const struct i2c_device_id *id)
468{
Jingoo Hanc838cb32013-12-05 19:21:10 -0800469 const struct pixcir_ts_platform_data *pdata =
470 dev_get_platdata(&client->dev);
Roger Quadrose9d47182014-05-18 22:43:42 -0700471 struct device *dev = &client->dev;
Roger Quadrosa4054592014-07-28 10:05:39 -0700472 struct device_node *np = dev->of_node;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800473 struct pixcir_i2c_ts_data *tsdata;
474 struct input_dev *input;
475 int error;
476
Roger Quadrosa4054592014-07-28 10:05:39 -0700477 if (np && !pdata) {
478 pdata = pixcir_parse_dt(dev);
479 if (IS_ERR(pdata))
480 return PTR_ERR(pdata);
481 }
482
Jianchun Bian36a281e2011-12-30 15:16:21 -0800483 if (!pdata) {
484 dev_err(&client->dev, "platform data not defined\n");
485 return -EINVAL;
486 }
487
Roger Quadros36874c72014-07-28 10:01:07 -0700488 if (!pdata->chip.max_fingers) {
489 dev_err(dev, "Invalid max_fingers in pdata\n");
490 return -EINVAL;
491 }
492
Roger Quadrose9d47182014-05-18 22:43:42 -0700493 tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
494 if (!tsdata)
495 return -ENOMEM;
496
497 input = devm_input_allocate_device(dev);
498 if (!input) {
499 dev_err(dev, "Failed to allocate input device\n");
500 return -ENOMEM;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800501 }
502
503 tsdata->client = client;
504 tsdata->input = input;
Roger Quadros36874c72014-07-28 10:01:07 -0700505 tsdata->pdata = pdata;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800506
507 input->name = client->name;
508 input->id.bustype = BUS_I2C;
Roger Quadros3b36fbb2014-05-18 22:44:35 -0700509 input->open = pixcir_input_open;
510 input->close = pixcir_input_close;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800511 input->dev.parent = &client->dev;
512
513 __set_bit(EV_KEY, input->evbit);
514 __set_bit(EV_ABS, input->evbit);
515 __set_bit(BTN_TOUCH, input->keybit);
516 input_set_abs_params(input, ABS_X, 0, pdata->x_max, 0, 0);
517 input_set_abs_params(input, ABS_Y, 0, pdata->y_max, 0, 0);
518 input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
519 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
520
Roger Quadros36874c72014-07-28 10:01:07 -0700521 tsdata->max_fingers = tsdata->pdata->chip.max_fingers;
522 if (tsdata->max_fingers > PIXCIR_MAX_SLOTS) {
523 tsdata->max_fingers = PIXCIR_MAX_SLOTS;
524 dev_info(dev, "Limiting maximum fingers to %d\n",
525 tsdata->max_fingers);
526 }
527
528 error = input_mt_init_slots(input, tsdata->max_fingers,
Roger Quadros62e65b72014-07-28 09:58:54 -0700529 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
530 if (error) {
531 dev_err(dev, "Error initializing Multi-Touch slots\n");
532 return error;
533 }
534
Jianchun Bian36a281e2011-12-30 15:16:21 -0800535 input_set_drvdata(input, tsdata);
536
Dmitry Torokhovcb4a5f02015-07-06 11:56:21 -0700537 tsdata->gpio_attb = devm_gpiod_get(dev, "attb", GPIOD_IN);
538 if (IS_ERR(tsdata->gpio_attb)) {
539 error = PTR_ERR(tsdata->gpio_attb);
540 dev_err(dev, "Failed to request ATTB gpio: %d\n", error);
Roger Quadros0dfc8d42014-05-18 22:46:43 -0700541 return error;
542 }
543
Roger Quadros40929162015-07-06 13:27:43 -0700544 tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset",
545 GPIOD_OUT_LOW);
546 if (IS_ERR(tsdata->gpio_reset)) {
547 error = PTR_ERR(tsdata->gpio_reset);
548 dev_err(dev, "Failed to request RESET gpio: %d\n", error);
549 return error;
550 }
551
Roger Quadrose9d47182014-05-18 22:43:42 -0700552 error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
553 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
554 client->name, tsdata);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800555 if (error) {
Roger Quadrose9d47182014-05-18 22:43:42 -0700556 dev_err(dev, "failed to request irq %d\n", client->irq);
557 return error;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800558 }
559
Roger Quadros40929162015-07-06 13:27:43 -0700560 pixcir_reset(tsdata);
561
Roger Quadros3b36fbb2014-05-18 22:44:35 -0700562 /* Always be in IDLE mode to save power, device supports auto wake */
563 error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
564 if (error) {
565 dev_err(dev, "Failed to set IDLE mode\n");
566 return error;
567 }
568
569 /* Stop device till opened */
570 error = pixcir_stop(tsdata);
571 if (error)
572 return error;
573
Jianchun Bian36a281e2011-12-30 15:16:21 -0800574 error = input_register_device(input);
575 if (error)
Roger Quadrose9d47182014-05-18 22:43:42 -0700576 return error;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800577
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700578 i2c_set_clientdata(client, tsdata);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800579 device_init_wakeup(&client->dev, 1);
580
581 return 0;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800582}
583
Bill Pembertone2619cf2012-11-23 21:50:47 -0800584static int pixcir_i2c_ts_remove(struct i2c_client *client)
Jianchun Bian36a281e2011-12-30 15:16:21 -0800585{
Jianchun Bian36a281e2011-12-30 15:16:21 -0800586 device_init_wakeup(&client->dev, 0);
587
Jianchun Bian36a281e2011-12-30 15:16:21 -0800588 return 0;
589}
590
591static const struct i2c_device_id pixcir_i2c_ts_id[] = {
592 { "pixcir_ts", 0 },
Roger Quadrosa4054592014-07-28 10:05:39 -0700593 { "pixcir_tangoc", 0 },
Jianchun Bian36a281e2011-12-30 15:16:21 -0800594 { }
595};
596MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
597
Roger Quadrosa4054592014-07-28 10:05:39 -0700598#ifdef CONFIG_OF
599static const struct pixcir_i2c_chip_data pixcir_ts_data = {
600 .max_fingers = 2,
601 /* no hw id support */
602};
603
604static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
605 .max_fingers = 5,
606 .has_hw_ids = true,
607};
608
609static const struct of_device_id pixcir_of_match[] = {
610 { .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data },
611 { .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data },
612 { }
613};
614MODULE_DEVICE_TABLE(of, pixcir_of_match);
615#endif
616
Jianchun Bian36a281e2011-12-30 15:16:21 -0800617static struct i2c_driver pixcir_i2c_ts_driver = {
618 .driver = {
619 .owner = THIS_MODULE,
620 .name = "pixcir_ts",
621 .pm = &pixcir_dev_pm_ops,
Roger Quadrosa4054592014-07-28 10:05:39 -0700622 .of_match_table = of_match_ptr(pixcir_of_match),
Jianchun Bian36a281e2011-12-30 15:16:21 -0800623 },
624 .probe = pixcir_i2c_ts_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800625 .remove = pixcir_i2c_ts_remove,
Jianchun Bian36a281e2011-12-30 15:16:21 -0800626 .id_table = pixcir_i2c_ts_id,
627};
628
Dmitry Torokhov4a533832012-03-16 23:05:44 -0700629module_i2c_driver(pixcir_i2c_ts_driver);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800630
631MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
632MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
633MODULE_LICENSE("GPL");