blob: 68d0d549797c493a38ef93738da065e5b37a55e0 [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>
Dmitry Torokhovd48259a2015-07-06 14:29:29 -070027#include <linux/input/touchscreen.h>
Roger Quadros0dfc8d42014-05-18 22:46:43 -070028#include <linux/gpio.h>
Dmitry Torokhovcb4a5f02015-07-06 11:56:21 -070029#include <linux/gpio/consumer.h>
Dmitry Torokhovd48259a2015-07-06 14:29:29 -070030/*#include <linux/of.h>*/
Roger Quadrosa4054592014-07-28 10:05:39 -070031#include <linux/of_device.h>
Dmitry Torokhov28a74c02015-07-06 11:48:47 -070032#include <linux/platform_data/pixcir_i2c_ts.h>
Jianchun Bian36a281e2011-12-30 15:16:21 -080033
Roger Quadros36874c72014-07-28 10:01:07 -070034#define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
Roger Quadros62e65b72014-07-28 09:58:54 -070035
Jianchun Bian36a281e2011-12-30 15:16:21 -080036struct pixcir_i2c_ts_data {
37 struct i2c_client *client;
38 struct input_dev *input;
Dmitry Torokhovcb4a5f02015-07-06 11:56:21 -070039 struct gpio_desc *gpio_attb;
Roger Quadros40929162015-07-06 13:27:43 -070040 struct gpio_desc *gpio_reset;
Dmitry Torokhovd48259a2015-07-06 14:29:29 -070041 const struct pixcir_i2c_chip_data *chip;
Roger Quadros36874c72014-07-28 10:01:07 -070042 int max_fingers; /* Max fingers supported in this instance */
Dmitry Torokhovd48259a2015-07-06 14:29:29 -070043 bool running;
Jianchun Bian36a281e2011-12-30 15:16:21 -080044};
45
Roger Quadros62e65b72014-07-28 09:58:54 -070046struct pixcir_touch {
47 int x;
48 int y;
Roger Quadros36874c72014-07-28 10:01:07 -070049 int id;
Roger Quadros62e65b72014-07-28 09:58:54 -070050};
51
52struct pixcir_report_data {
53 int num_touches;
54 struct pixcir_touch touches[PIXCIR_MAX_SLOTS];
55};
56
57static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata,
58 struct pixcir_report_data *report)
Jianchun Bian36a281e2011-12-30 15:16:21 -080059{
Roger Quadros36874c72014-07-28 10:01:07 -070060 u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5];
61 u8 wrbuf[1] = { 0 };
Roger Quadros62e65b72014-07-28 09:58:54 -070062 u8 *bufptr;
Jianchun Bian36a281e2011-12-30 15:16:21 -080063 u8 touch;
Roger Quadros62e65b72014-07-28 09:58:54 -070064 int ret, i;
Roger Quadros36874c72014-07-28 10:01:07 -070065 int readsize;
Dmitry Torokhovd48259a2015-07-06 14:29:29 -070066 const struct pixcir_i2c_chip_data *chip = tsdata->chip;
Roger Quadros62e65b72014-07-28 09:58:54 -070067
68 memset(report, 0, sizeof(struct pixcir_report_data));
Jianchun Bian36a281e2011-12-30 15:16:21 -080069
Roger Quadros36874c72014-07-28 10:01:07 -070070 i = chip->has_hw_ids ? 1 : 0;
71 readsize = 2 + tsdata->max_fingers * (4 + i);
72 if (readsize > sizeof(rdbuf))
73 readsize = sizeof(rdbuf);
74
Jianchun Bian36a281e2011-12-30 15:16:21 -080075 ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
76 if (ret != sizeof(wrbuf)) {
77 dev_err(&tsdata->client->dev,
78 "%s: i2c_master_send failed(), ret=%d\n",
79 __func__, ret);
80 return;
81 }
82
Roger Quadros36874c72014-07-28 10:01:07 -070083 ret = i2c_master_recv(tsdata->client, rdbuf, readsize);
Jianchun Bian36a281e2011-12-30 15:16:21 -080084 if (ret != sizeof(rdbuf)) {
85 dev_err(&tsdata->client->dev,
86 "%s: i2c_master_recv failed(), ret=%d\n",
87 __func__, ret);
88 return;
89 }
90
Roger Quadros62e65b72014-07-28 09:58:54 -070091 touch = rdbuf[0] & 0x7;
Roger Quadros36874c72014-07-28 10:01:07 -070092 if (touch > tsdata->max_fingers)
93 touch = tsdata->max_fingers;
Jianchun Bian36a281e2011-12-30 15:16:21 -080094
Roger Quadros62e65b72014-07-28 09:58:54 -070095 report->num_touches = touch;
96 bufptr = &rdbuf[2];
Jianchun Bian36a281e2011-12-30 15:16:21 -080097
Roger Quadros62e65b72014-07-28 09:58:54 -070098 for (i = 0; i < touch; i++) {
99 report->touches[i].x = (bufptr[1] << 8) | bufptr[0];
100 report->touches[i].y = (bufptr[3] << 8) | bufptr[2];
Jianchun Bian36a281e2011-12-30 15:16:21 -0800101
Roger Quadros36874c72014-07-28 10:01:07 -0700102 if (chip->has_hw_ids) {
103 report->touches[i].id = bufptr[4];
104 bufptr = bufptr + 5;
105 } else {
106 bufptr = bufptr + 4;
107 }
Roger Quadros62e65b72014-07-28 09:58:54 -0700108 }
109}
110
111static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts,
112 struct pixcir_report_data *report)
113{
114 struct input_mt_pos pos[PIXCIR_MAX_SLOTS];
115 int slots[PIXCIR_MAX_SLOTS];
116 struct pixcir_touch *touch;
117 int n, i, slot;
118 struct device *dev = &ts->client->dev;
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700119 const struct pixcir_i2c_chip_data *chip = ts->chip;
Roger Quadros62e65b72014-07-28 09:58:54 -0700120
121 n = report->num_touches;
122 if (n > PIXCIR_MAX_SLOTS)
123 n = PIXCIR_MAX_SLOTS;
124
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700125 if (!ts->chip->has_hw_ids) {
Roger Quadros36874c72014-07-28 10:01:07 -0700126 for (i = 0; i < n; i++) {
127 touch = &report->touches[i];
128 pos[i].x = touch->x;
129 pos[i].y = touch->y;
130 }
131
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800132 input_mt_assign_slots(ts->input, slots, pos, n, 0);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800133 }
134
Roger Quadros62e65b72014-07-28 09:58:54 -0700135 for (i = 0; i < n; i++) {
136 touch = &report->touches[i];
Roger Quadros36874c72014-07-28 10:01:07 -0700137
138 if (chip->has_hw_ids) {
139 slot = input_mt_get_slot_by_key(ts->input, touch->id);
140 if (slot < 0) {
141 dev_dbg(dev, "no free slot for id 0x%x\n",
142 touch->id);
143 continue;
144 }
145 } else {
146 slot = slots[i];
147 }
Roger Quadros62e65b72014-07-28 09:58:54 -0700148
149 input_mt_slot(ts->input, slot);
150 input_mt_report_slot_state(ts->input,
151 MT_TOOL_FINGER, true);
152
153 input_event(ts->input, EV_ABS, ABS_MT_POSITION_X, touch->x);
154 input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y, touch->y);
155
156 dev_dbg(dev, "%d: slot %d, x %d, y %d\n",
157 i, slot, touch->x, touch->y);
158 }
159
160 input_mt_sync_frame(ts->input);
161 input_sync(ts->input);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800162}
163
164static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
165{
166 struct pixcir_i2c_ts_data *tsdata = dev_id;
Roger Quadros62e65b72014-07-28 09:58:54 -0700167 struct pixcir_report_data report;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800168
Roger Quadros3b36fbb2014-05-18 22:44:35 -0700169 while (tsdata->running) {
Roger Quadros62e65b72014-07-28 09:58:54 -0700170 /* parse packet */
171 pixcir_ts_parse(tsdata, &report);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800172
Roger Quadros62e65b72014-07-28 09:58:54 -0700173 /* report it */
174 pixcir_ts_report(tsdata, &report);
175
Dmitry Torokhov127520c2015-07-06 13:27:00 -0700176 if (gpiod_get_value_cansleep(tsdata->gpio_attb)) {
Roger Quadros62e65b72014-07-28 09:58:54 -0700177 if (report.num_touches) {
178 /*
179 * Last report with no finger up?
180 * Do it now then.
181 */
182 input_mt_sync_frame(tsdata->input);
183 input_sync(tsdata->input);
184 }
Jianchun Bian36a281e2011-12-30 15:16:21 -0800185 break;
Roger Quadros62e65b72014-07-28 09:58:54 -0700186 }
Jianchun Bian36a281e2011-12-30 15:16:21 -0800187
188 msleep(20);
189 }
190
191 return IRQ_HANDLED;
192}
193
Roger Quadros40929162015-07-06 13:27:43 -0700194static void pixcir_reset(struct pixcir_i2c_ts_data *tsdata)
195{
196 if (!IS_ERR_OR_NULL(tsdata->gpio_reset)) {
197 gpiod_set_value_cansleep(tsdata->gpio_reset, 1);
198 ndelay(100); /* datasheet section 1.2.3 says 80ns min. */
199 gpiod_set_value_cansleep(tsdata->gpio_reset, 0);
200 /* wait for controller ready. 100ms guess. */
201 msleep(100);
202 }
203}
204
Roger Quadros3b36fbb2014-05-18 22:44:35 -0700205static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
206 enum pixcir_power_mode mode)
207{
208 struct device *dev = &ts->client->dev;
209 int ret;
210
211 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
212 if (ret < 0) {
213 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
214 __func__, PIXCIR_REG_POWER_MODE, ret);
215 return ret;
216 }
217
218 ret &= ~PIXCIR_POWER_MODE_MASK;
219 ret |= mode;
220
221 /* Always AUTO_IDLE */
222 ret |= PIXCIR_POWER_ALLOW_IDLE;
223
224 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
225 if (ret < 0) {
226 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
227 __func__, PIXCIR_REG_POWER_MODE, ret);
228 return ret;
229 }
230
231 return 0;
232}
233
234/*
235 * Set the interrupt mode for the device i.e. ATTB line behaviour
236 *
237 * @polarity : 1 for active high, 0 for active low.
238 */
239static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
240 enum pixcir_int_mode mode, bool polarity)
241{
242 struct device *dev = &ts->client->dev;
243 int ret;
244
245 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
246 if (ret < 0) {
247 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
248 __func__, PIXCIR_REG_INT_MODE, ret);
249 return ret;
250 }
251
252 ret &= ~PIXCIR_INT_MODE_MASK;
253 ret |= mode;
254
255 if (polarity)
256 ret |= PIXCIR_INT_POL_HIGH;
257 else
258 ret &= ~PIXCIR_INT_POL_HIGH;
259
260 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
261 if (ret < 0) {
262 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
263 __func__, PIXCIR_REG_INT_MODE, ret);
264 return ret;
265 }
266
267 return 0;
268}
269
270/*
271 * Enable/disable interrupt generation
272 */
273static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
274{
275 struct device *dev = &ts->client->dev;
276 int ret;
277
278 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
279 if (ret < 0) {
280 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
281 __func__, PIXCIR_REG_INT_MODE, ret);
282 return ret;
283 }
284
285 if (enable)
286 ret |= PIXCIR_INT_ENABLE;
287 else
288 ret &= ~PIXCIR_INT_ENABLE;
289
290 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
291 if (ret < 0) {
292 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
293 __func__, PIXCIR_REG_INT_MODE, ret);
294 return ret;
295 }
296
297 return 0;
298}
299
300static int pixcir_start(struct pixcir_i2c_ts_data *ts)
301{
302 struct device *dev = &ts->client->dev;
303 int error;
304
305 /* LEVEL_TOUCH interrupt with active low polarity */
306 error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
307 if (error) {
308 dev_err(dev, "Failed to set interrupt mode: %d\n", error);
309 return error;
310 }
311
312 ts->running = true;
313 mb(); /* Update status before IRQ can fire */
314
315 /* enable interrupt generation */
316 error = pixcir_int_enable(ts, true);
317 if (error) {
318 dev_err(dev, "Failed to enable interrupt generation: %d\n",
319 error);
320 return error;
321 }
322
323 return 0;
324}
325
326static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
327{
328 int error;
329
330 /* Disable interrupt generation */
331 error = pixcir_int_enable(ts, false);
332 if (error) {
333 dev_err(&ts->client->dev,
334 "Failed to disable interrupt generation: %d\n",
335 error);
336 return error;
337 }
338
339 /* Exit ISR if running, no more report parsing */
340 ts->running = false;
341 mb(); /* update status before we synchronize irq */
342
343 /* Wait till running ISR is complete */
344 synchronize_irq(ts->client->irq);
345
346 return 0;
347}
348
349static int pixcir_input_open(struct input_dev *dev)
350{
351 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
352
353 return pixcir_start(ts);
354}
355
356static void pixcir_input_close(struct input_dev *dev)
357{
358 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
359
360 pixcir_stop(ts);
361}
362
Jingoo Han02b6a582014-11-02 00:04:14 -0700363static int __maybe_unused pixcir_i2c_ts_suspend(struct device *dev)
Jianchun Bian36a281e2011-12-30 15:16:21 -0800364{
365 struct i2c_client *client = to_i2c_client(dev);
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700366 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
367 struct input_dev *input = ts->input;
368 int ret = 0;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800369
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700370 mutex_lock(&input->mutex);
371
372 if (device_may_wakeup(&client->dev)) {
373 if (!input->users) {
374 ret = pixcir_start(ts);
375 if (ret) {
376 dev_err(dev, "Failed to start\n");
377 goto unlock;
378 }
379 }
380
Jianchun Bian36a281e2011-12-30 15:16:21 -0800381 enable_irq_wake(client->irq);
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700382 } else if (input->users) {
383 ret = pixcir_stop(ts);
384 }
Jianchun Bian36a281e2011-12-30 15:16:21 -0800385
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700386unlock:
387 mutex_unlock(&input->mutex);
388
389 return ret;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800390}
391
Jingoo Han02b6a582014-11-02 00:04:14 -0700392static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev)
Jianchun Bian36a281e2011-12-30 15:16:21 -0800393{
394 struct i2c_client *client = to_i2c_client(dev);
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700395 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
396 struct input_dev *input = ts->input;
397 int ret = 0;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800398
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700399 mutex_lock(&input->mutex);
400
401 if (device_may_wakeup(&client->dev)) {
Jianchun Bian36a281e2011-12-30 15:16:21 -0800402 disable_irq_wake(client->irq);
403
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700404 if (!input->users) {
405 ret = pixcir_stop(ts);
406 if (ret) {
407 dev_err(dev, "Failed to stop\n");
408 goto unlock;
409 }
410 }
411 } else if (input->users) {
412 ret = pixcir_start(ts);
413 }
414
415unlock:
416 mutex_unlock(&input->mutex);
417
418 return ret;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800419}
Jianchun Bian36a281e2011-12-30 15:16:21 -0800420
421static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
422 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
423
Roger Quadrosa4054592014-07-28 10:05:39 -0700424#ifdef CONFIG_OF
425static const struct of_device_id pixcir_of_match[];
426
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700427static int pixcir_parse_dt(struct device *dev,
428 struct pixcir_i2c_ts_data *tsdata)
Roger Quadrosa4054592014-07-28 10:05:39 -0700429{
Roger Quadrosa4054592014-07-28 10:05:39 -0700430 const struct of_device_id *match;
431
432 match = of_match_device(of_match_ptr(pixcir_of_match), dev);
433 if (!match)
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700434 return -EINVAL;
Roger Quadrosa4054592014-07-28 10:05:39 -0700435
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700436 tsdata->chip = (const struct pixcir_i2c_chip_data *)match->data;
437 if (!tsdata->chip)
438 return -EINVAL;
Roger Quadrosa4054592014-07-28 10:05:39 -0700439
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700440 return 0;
Roger Quadrosa4054592014-07-28 10:05:39 -0700441}
442#else
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700443static int pixcir_parse_dt(struct device *dev,
444 struct pixcir_i2c_ts_data *tsdata)
Roger Quadrosa4054592014-07-28 10:05:39 -0700445{
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700446 return -EINVAL;
Roger Quadrosa4054592014-07-28 10:05:39 -0700447}
448#endif
449
Bill Pemberton5298cc42012-11-23 21:38:25 -0800450static int pixcir_i2c_ts_probe(struct i2c_client *client,
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700451 const struct i2c_device_id *id)
Jianchun Bian36a281e2011-12-30 15:16:21 -0800452{
Jingoo Hanc838cb32013-12-05 19:21:10 -0800453 const struct pixcir_ts_platform_data *pdata =
454 dev_get_platdata(&client->dev);
Roger Quadrose9d47182014-05-18 22:43:42 -0700455 struct device *dev = &client->dev;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800456 struct pixcir_i2c_ts_data *tsdata;
457 struct input_dev *input;
458 int error;
459
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700460 tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
461 if (!tsdata)
462 return -ENOMEM;
Roger Quadrosa4054592014-07-28 10:05:39 -0700463
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700464 if (pdata) {
465 tsdata->chip = &pdata->chip;
466 } else if (dev->of_node) {
467 error = pixcir_parse_dt(dev, tsdata);
468 if (error)
469 return error;
470 } else {
Jianchun Bian36a281e2011-12-30 15:16:21 -0800471 dev_err(&client->dev, "platform data not defined\n");
472 return -EINVAL;
473 }
474
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700475 if (!tsdata->chip->max_fingers) {
476 dev_err(dev, "Invalid max_fingers in chip data\n");
Roger Quadros36874c72014-07-28 10:01:07 -0700477 return -EINVAL;
478 }
479
Roger Quadrose9d47182014-05-18 22:43:42 -0700480 input = devm_input_allocate_device(dev);
481 if (!input) {
482 dev_err(dev, "Failed to allocate input device\n");
483 return -ENOMEM;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800484 }
485
486 tsdata->client = client;
487 tsdata->input = input;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800488
489 input->name = client->name;
490 input->id.bustype = BUS_I2C;
Roger Quadros3b36fbb2014-05-18 22:44:35 -0700491 input->open = pixcir_input_open;
492 input->close = pixcir_input_close;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800493 input->dev.parent = &client->dev;
494
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700495 if (pdata) {
496 input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
497 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
498 } else {
499 input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
500 input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
501 touchscreen_parse_properties(input, true);
502 if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
503 !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
504 dev_err(dev, "Touchscreen size is not specified\n");
505 return -EINVAL;
506 }
507 }
Jianchun Bian36a281e2011-12-30 15:16:21 -0800508
Dmitry Torokhovd48259a2015-07-06 14:29:29 -0700509 tsdata->max_fingers = tsdata->chip->max_fingers;
Roger Quadros36874c72014-07-28 10:01:07 -0700510 if (tsdata->max_fingers > PIXCIR_MAX_SLOTS) {
511 tsdata->max_fingers = PIXCIR_MAX_SLOTS;
512 dev_info(dev, "Limiting maximum fingers to %d\n",
513 tsdata->max_fingers);
514 }
515
516 error = input_mt_init_slots(input, tsdata->max_fingers,
Roger Quadros62e65b72014-07-28 09:58:54 -0700517 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
518 if (error) {
519 dev_err(dev, "Error initializing Multi-Touch slots\n");
520 return error;
521 }
522
Jianchun Bian36a281e2011-12-30 15:16:21 -0800523 input_set_drvdata(input, tsdata);
524
Dmitry Torokhovcb4a5f02015-07-06 11:56:21 -0700525 tsdata->gpio_attb = devm_gpiod_get(dev, "attb", GPIOD_IN);
526 if (IS_ERR(tsdata->gpio_attb)) {
527 error = PTR_ERR(tsdata->gpio_attb);
528 dev_err(dev, "Failed to request ATTB gpio: %d\n", error);
Roger Quadros0dfc8d42014-05-18 22:46:43 -0700529 return error;
530 }
531
Roger Quadros40929162015-07-06 13:27:43 -0700532 tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset",
533 GPIOD_OUT_LOW);
534 if (IS_ERR(tsdata->gpio_reset)) {
535 error = PTR_ERR(tsdata->gpio_reset);
536 dev_err(dev, "Failed to request RESET gpio: %d\n", error);
537 return error;
538 }
539
Roger Quadrose9d47182014-05-18 22:43:42 -0700540 error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
541 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
542 client->name, tsdata);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800543 if (error) {
Roger Quadrose9d47182014-05-18 22:43:42 -0700544 dev_err(dev, "failed to request irq %d\n", client->irq);
545 return error;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800546 }
547
Roger Quadros40929162015-07-06 13:27:43 -0700548 pixcir_reset(tsdata);
549
Roger Quadros3b36fbb2014-05-18 22:44:35 -0700550 /* Always be in IDLE mode to save power, device supports auto wake */
551 error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
552 if (error) {
553 dev_err(dev, "Failed to set IDLE mode\n");
554 return error;
555 }
556
557 /* Stop device till opened */
558 error = pixcir_stop(tsdata);
559 if (error)
560 return error;
561
Jianchun Bian36a281e2011-12-30 15:16:21 -0800562 error = input_register_device(input);
563 if (error)
Roger Quadrose9d47182014-05-18 22:43:42 -0700564 return error;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800565
Roger Quadros7cdcb8d2014-05-18 22:49:20 -0700566 i2c_set_clientdata(client, tsdata);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800567 device_init_wakeup(&client->dev, 1);
568
569 return 0;
Jianchun Bian36a281e2011-12-30 15:16:21 -0800570}
571
Bill Pembertone2619cf2012-11-23 21:50:47 -0800572static int pixcir_i2c_ts_remove(struct i2c_client *client)
Jianchun Bian36a281e2011-12-30 15:16:21 -0800573{
Jianchun Bian36a281e2011-12-30 15:16:21 -0800574 device_init_wakeup(&client->dev, 0);
575
Jianchun Bian36a281e2011-12-30 15:16:21 -0800576 return 0;
577}
578
579static const struct i2c_device_id pixcir_i2c_ts_id[] = {
580 { "pixcir_ts", 0 },
Roger Quadrosa4054592014-07-28 10:05:39 -0700581 { "pixcir_tangoc", 0 },
Jianchun Bian36a281e2011-12-30 15:16:21 -0800582 { }
583};
584MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
585
Roger Quadrosa4054592014-07-28 10:05:39 -0700586#ifdef CONFIG_OF
587static const struct pixcir_i2c_chip_data pixcir_ts_data = {
588 .max_fingers = 2,
589 /* no hw id support */
590};
591
592static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
593 .max_fingers = 5,
594 .has_hw_ids = true,
595};
596
597static const struct of_device_id pixcir_of_match[] = {
598 { .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data },
599 { .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data },
600 { }
601};
602MODULE_DEVICE_TABLE(of, pixcir_of_match);
603#endif
604
Jianchun Bian36a281e2011-12-30 15:16:21 -0800605static struct i2c_driver pixcir_i2c_ts_driver = {
606 .driver = {
607 .owner = THIS_MODULE,
608 .name = "pixcir_ts",
609 .pm = &pixcir_dev_pm_ops,
Roger Quadrosa4054592014-07-28 10:05:39 -0700610 .of_match_table = of_match_ptr(pixcir_of_match),
Jianchun Bian36a281e2011-12-30 15:16:21 -0800611 },
612 .probe = pixcir_i2c_ts_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800613 .remove = pixcir_i2c_ts_remove,
Jianchun Bian36a281e2011-12-30 15:16:21 -0800614 .id_table = pixcir_i2c_ts_id,
615};
616
Dmitry Torokhov4a533832012-03-16 23:05:44 -0700617module_i2c_driver(pixcir_i2c_ts_driver);
Jianchun Bian36a281e2011-12-30 15:16:21 -0800618
619MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
620MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
621MODULE_LICENSE("GPL");