blob: 006c08994156c457208043ae6609002fedd85415 [file] [log] [blame]
Flemmardd3990082013-04-25 20:53:01 -07001/* drivers/input/touchscreen/cy8c_cs.c
2 *
3 * Copyright (C) 2011 HTC Corporation.
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <linux/input/cy8c_cs.h>
17#include <linux/delay.h>
18#include <linux/earlysuspend.h>
19#include <linux/hrtimer.h>
20#include <linux/i2c.h>
21#include <linux/input.h>
22#include <linux/interrupt.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26#include <linux/gpio.h>
27#include <linux/workqueue.h>
28
29#define CY8C_I2C_RETRY_TIMES (10)
30#define CY8C_KEYLOCKTIME (1500)
31#define CY8C_KEYLOCKRESET (6)
32
33struct cy8c_cs_data {
34 struct i2c_client *client;
35 struct input_dev *input_dev;
36 struct workqueue_struct *cy8c_wq;
37 struct work_struct work;
38 struct early_suspend early_suspend;
39 int use_irq;
40 struct hrtimer timer;
41 uint16_t version;
42 struct infor id;
43 uint16_t intr;
44 uint8_t vk_id;
45 uint8_t debug_level;
46 int *keycode;
47 int (*power)(int on);
48 int (*reset)(void);
49 int func_support;
50 struct workqueue_struct *wq_raw;
51 struct delayed_work work_raw;
52};
53
54static struct cy8c_cs_data *private_cs;
55
56static irqreturn_t cy8c_cs_irq_handler(int, void *);
57static int disable_key;
58static int reset_cnt;
59
60extern int board_build_flag(void);
61#ifdef CONFIG_HAS_EARLYSUSPEND
62static void cy8c_cs_early_suspend(struct early_suspend *h);
63static void cy8c_cs_late_resume(struct early_suspend *h);
64#endif
65
66int i2c_cy8c_read(struct i2c_client *client, uint8_t addr, uint8_t *data, uint8_t length)
67{
68 int retry;
69
70 struct i2c_msg msg[] = {
71 {
72 .addr = client->addr,
73 .flags = 0,
74 .len = 1,
75 .buf = &addr,
76 },
77 {
78 .addr = client->addr,
79 .flags = I2C_M_RD,
80 .len = length,
81 .buf = data,
82 }
83 };
84
85 for (retry = 0; retry < CY8C_I2C_RETRY_TIMES; retry++) {
86 if (i2c_transfer(client->adapter, msg, 2) == 2)
87 break;
88 mdelay(10);
89 }
90 if (retry == CY8C_I2C_RETRY_TIMES) {
91 printk(KERN_INFO "[cap]i2c_read_block retry over %d\n",
92 CY8C_I2C_RETRY_TIMES);
93 return -EIO;
94 }
95 return 0;
96
97}
98
99int i2c_cy8c_write(struct i2c_client *client, uint8_t addr, uint8_t *data, uint8_t length)
100{
101 int retry, loop_i;
102 uint8_t buf[length + 1];
103
104 struct i2c_msg msg[] = {
105 {
106 .addr = client->addr,
107 .flags = 0,
108 .len = length + 1,
109 .buf = buf,
110 }
111 };
112
113 buf[0] = addr;
114 for (loop_i = 0; loop_i < length; loop_i++)
115 buf[loop_i + 1] = data[loop_i];
116
117 for (retry = 0; retry < CY8C_I2C_RETRY_TIMES; retry++) {
118 if (i2c_transfer(client->adapter, msg, 1) == 1)
119 break;
120 mdelay(10);
121 }
122
123 if (retry == CY8C_I2C_RETRY_TIMES) {
124 printk(KERN_ERR "[cap]i2c_write_block retry over %d\n",
125 CY8C_I2C_RETRY_TIMES);
126 return -EIO;
127 }
128 return 0;
129
130}
131
132int i2c_cy8c_write_byte_data(struct i2c_client *client, uint8_t addr, uint8_t value)
133{
134 return i2c_cy8c_write(client, addr, &value, 1);
135}
136
137static ssize_t diff(struct device *dev, struct device_attribute *attr, char *buf)
138{
139 int ret = 0, i;
140 char data[8] = {0};
141 struct cy8c_cs_data *cs;
142
143 pr_info("[cap] %s", __func__);
144
145 cs = private_cs;
146 ret = i2c_cy8c_write_byte_data(cs->client, CS_SELECT, CS_CMD_BASELINE);
147 if (ret < 0) {
148 pr_err("[cap] i2c Write baseline Err\n");
149 return ret;
150 }
151 msleep(100);
152 ret = i2c_cy8c_read(cs->client, CS_BL_HB, data, ARRAY_SIZE(data));
153 if (ret < 0) {
154 pr_err("[cap] i2c Read baseline Err\n");
155 return ret;
156 }
157
158 for (i = 0; i < 8 ; i += 2)
159 ret += sprintf(buf+ret, "BTN(%d)=%d, ", (i/2),
160 (data[i] << 8 | data[i+1]));
161 ret += sprintf(buf+ret, "\n");
162
163 return ret;
164}
165static DEVICE_ATTR(diff, S_IRUGO, diff, NULL);
166
167static ssize_t reset(struct device *dev, struct device_attribute *attr, char *buf)
168{
169 int ret = 0;
170 struct cy8c_cs_data *cs;
171 cs = private_cs;
172
173 pr_info("[cap] reset\n");
174 cs->reset();
175 ret = sprintf(buf, "Reset chip");
176 return ret;
177}
178static DEVICE_ATTR(reset, S_IRUGO, reset, NULL);
179
180static ssize_t stop_report(struct device *dev, struct device_attribute *attr,
181 const char *buf, size_t count)
182{
183 int err;
184 unsigned long i = 0;
185 err = strict_strtoul(buf, 10, &i);
186
187 if (disable_key < 2 || disable_key >= 0) {
188 disable_key = i;
189 pr_info("[cap] KEY Report %s!!\n", disable_key ?
190 "DISABLE" : "ENABLE");
191 } else
192 pr_info("[cap] Parameter Error\n");
193
194 return count;
195}
196
197static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
198 char *buf)
199{
200 return sprintf(buf, "[cap] disable_key = %d\n", disable_key);
201}
202static DEVICE_ATTR(diskey, (S_IWUSR|S_IRUGO), show_flag, stop_report);
203
204static int cy8c_printcs_raw(struct cy8c_cs_data *cs, char *buf)
205{
206 int ret = 0, pos = 0, i, j, cmd[4] = {CS_CMD_BTN1, CS_CMD_BTN2, CS_CMD_BTN3, CS_CMD_BTN4};
207 char data[6] = {0}, capstate[3][10] = {"BL", "Raw", "Dlt"};
208
209 for (i = 0; i < cs->id.config; i++) {
210 ret = i2c_cy8c_write_byte_data(cs->client, CS_SELECT, cmd[i]);
211 if (ret < 0) {
212 pr_err("[cap] i2c Write inform (%d_%#x) Err\n", i+1, cmd[i]);
213 return ret;
214 }
215 msleep(50);
216 ret = i2c_cy8c_read(cs->client, CS_BL_HB, data, ARRAY_SIZE(data));
217 if (ret < 0) {
218 pr_err("[cap] i2c Read inform (%d_%#x)) Err\n", i+1, cmd[i]);
219 return ret;
220 }
221 pos += sprintf(buf+pos, "BTN(%d)", i);
222 for (j = 0; j < 6 ; j += 2)
223 pos += sprintf(buf+pos, "%s=%d, ", capstate[j/2],
224 (data[j] << 8 | data[j+1]));
225 pos += sprintf(buf+pos, "\n");
226 memset(data, 0, sizeof(ARRAY_SIZE(data)));
227 }
228 return pos;
229}
230
231static ssize_t inform(struct device *dev, struct device_attribute *attr, char *buf)
232{
233 int ret = 0, pos = 0;
234 char data[2] = {0};
235 struct cy8c_cs_data *cs;
236
237 cs = private_cs;
238
239 if (cs->id.version < 0x10 && cs->id.version > 0x08)
240 cs->id.version = cs->id.version << 4;
241
242 if (cs->id.version >= 0x86) {
243 memset(data, 0, sizeof(ARRAY_SIZE(data)));
244 ret = i2c_cy8c_read(cs->client, CS_INT_STATUS, data, 2);
245 if (ret < 0) {
246 pr_err("[cap] i2c Read inform INT status Err\n");
247 return ret;
248 }
249 pos += sprintf(buf+pos, "Btn code = %x, INT status= %x\n", data[0], data[1]);
250 }
251 pos += cy8c_printcs_raw(cs, buf+pos);
252
253 return pos;
254}
255static DEVICE_ATTR(inform, S_IRUGO, inform, NULL);
256
257static ssize_t cs_vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
258{
259 char data[3] = {0};
260 int ret = 0;
261 struct cy8c_cs_data *cs;
262 cs = private_cs;
263
264 ret = i2c_cy8c_read(cs->client, CS_FW_VERSION, data, 2);
265 if (ret < 0) {
266 pr_err("[cap] i2c Read version Err\n");
267 return ret;
268 }
269 if (cs->id.chipid == CS_CHIPID)
270 sprintf(buf, "%s_V%x\n", CYPRESS_SS_NAME, data[0]);
271 else
272 sprintf(buf, "%s_V%x\n", CYPRESS_CS_NAME, data[0]);
273 ret += strlen(buf)+1;
274
275 return ret;
276}
277static DEVICE_ATTR(vendor, S_IRUGO, cs_vendor_show, NULL);
278
279static ssize_t cy8c_cs_gpio_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
281{
282 int ret = 0;
283 struct cy8c_cs_data *cs_data;
284 struct cy8c_i2c_cs_platform_data *pdata;
285
286 cs_data = private_cs;
287 pdata = cs_data->client->dev.platform_data;
288
289 ret = gpio_get_value(pdata->gpio_irq);
290 printk(KERN_DEBUG "[cap] GPIO_CS_INT_N=%d\n", pdata->gpio_irq);
291 sprintf(buf, "GPIO_CS_INT_N=%d\n", ret);
292 ret = strlen(buf) + 1;
293 return ret;
294}
295static DEVICE_ATTR(gpio, S_IRUGO, cy8c_cs_gpio_show, NULL);
296
297static ssize_t cy8c_cs_read_show(struct device *dev,
298 struct device_attribute *attr, char *buf)
299{
300 int ret = 0;
301 struct cy8c_cs_data *cs_data;
302 struct cy8c_i2c_cs_platform_data *pdata;
303
304 cs_data = private_cs;
305 pdata = cs_data->client->dev.platform_data;
306
307 ret = gpio_get_value(pdata->gpio_irq);
308 printk(KERN_DEBUG "GPIO_CS_INT_N=%d\n", pdata->gpio_irq);
309 ret = strlen(buf) + 1;
310 return ret;
311}
312static DEVICE_ATTR(read, S_IRUGO, cy8c_cs_read_show, NULL);
313
314static ssize_t debug_level_set(struct device *dev, struct device_attribute *attr,
315 const char *buf, size_t count)
316{
317 int i;
318 struct cy8c_cs_data *cs_data;
319 cs_data = private_cs;
320 if (sscanf(buf, "%d", &i) == 1 && i < 2) {
321 cs_data->debug_level = i;
322 pr_info("[cap] debug_level = %d\b", cs_data->debug_level);
323 } else
324 pr_info("[cap] Parameter Error\n");
325 return count;
326}
327
328static ssize_t debug_level_show(struct device *dev, struct device_attribute *attr, char *buf)
329{
330 struct cy8c_cs_data *cs_data;
331 cs_data = private_cs;
332 return sprintf(buf, "[cap] debug_level = %d\n", cs_data->debug_level);
333}
334DEVICE_ATTR(debug_level, (S_IWUSR|S_IRUGO), debug_level_show, debug_level_set);
335
336static struct kobject *android_touchkey_kobj;
337
338static int cy8c_touchkey_sysfs_init(void)
339{
340 int ret;
341 android_touchkey_kobj = kobject_create_and_add("android_key", NULL);
342 if (android_touchkey_kobj == NULL) {
343 printk(KERN_ERR "%s: subsystem_register failed\n", __func__);
344 ret = -ENOMEM;
345 return ret;
346 }
347 ret = sysfs_create_file(android_touchkey_kobj, &dev_attr_gpio.attr);
348 if (ret) {
349 printk(KERN_ERR "%s: sysfs_create_file gpio failed\n", __func__);
350 return ret;
351 }
352 ret = sysfs_create_file(android_touchkey_kobj, &dev_attr_read.attr);
353 if (ret) {
354 printk(KERN_ERR "%s: sysfs_create_file read failed\n", __func__);
355 return ret;
356 }
357 ret = sysfs_create_file(android_touchkey_kobj, &dev_attr_vendor.attr);
358 if (ret) {
359 printk(KERN_ERR "%s: sysfs_create_file vendor failed\n", __func__);
360 return ret;
361 }
362 ret = sysfs_create_file(android_touchkey_kobj, &dev_attr_inform.attr);
363 if (ret) {
364 printk(KERN_ERR "%s: sysfs_create_file inform failed\n", __func__);
365 return ret;
366 }
367 ret = sysfs_create_file(android_touchkey_kobj, &dev_attr_diff.attr);
368 if (ret) {
369 printk(KERN_ERR "%s: sysfs_create_file inform failed\n", __func__);
370 return ret;
371 }
372 ret = sysfs_create_file(android_touchkey_kobj, &dev_attr_debug_level.attr);
373 if (ret) {
374 printk(KERN_ERR "%s: sysfs_create_file debug_level failed\n", __func__);
375 return ret;
376 }
377 ret = sysfs_create_file(android_touchkey_kobj, &dev_attr_reset.attr);
378 if (ret) {
379 printk(KERN_ERR "%s: sysfs_create_file debug_level failed\n", __func__);
380 return ret;
381 }
382 ret = sysfs_create_file(android_touchkey_kobj, &dev_attr_diskey.attr);
383 if (ret) {
384 printk(KERN_ERR "%s: sysfs_create_file debug_level failed\n", __func__);
385 return ret;
386 }
387 return 0;
388}
389
390static void cy8c_touchkey_sysfs_deinit(void)
391{
392 sysfs_remove_file(android_touchkey_kobj, &dev_attr_gpio.attr);
393 sysfs_remove_file(android_touchkey_kobj, &dev_attr_read.attr);
394 sysfs_remove_file(android_touchkey_kobj, &dev_attr_vendor.attr);
395 sysfs_remove_file(android_touchkey_kobj, &dev_attr_inform.attr);
396 sysfs_remove_file(android_touchkey_kobj, &dev_attr_diff.attr);
397 sysfs_remove_file(android_touchkey_kobj, &dev_attr_debug_level.attr);
398 sysfs_remove_file(android_touchkey_kobj, &dev_attr_reset.attr);
399 sysfs_remove_file(android_touchkey_kobj, &dev_attr_diskey.attr);
400 kobject_del(android_touchkey_kobj);
401}
402
403static void cy8c_rawdata_print(struct work_struct *work)
404{
405 char buf[150] = {0};
406 int pos = 0;
407 struct cy8c_cs_data *cs = container_of(work, struct cy8c_cs_data,
408 work_raw.work);
409 pos += cy8c_printcs_raw(cs, buf+pos);
410 pos = strlen(buf)+1;
411 pr_info("[cap]%s\n", buf);
412
413 if (cs->vk_id) {
414 reset_cnt++;
415 if (reset_cnt % CY8C_KEYLOCKRESET == 0) {
416 pr_info("[cap] keylock reset\n");
417 cs->reset();
418 reset_cnt = 0;
419 cs->vk_id = 0;
420 }
421 queue_delayed_work(cs->wq_raw, &cs->work_raw,
422 msecs_to_jiffies(CY8C_KEYLOCKTIME-500));
423 }
424}
425
426static int cy8c_init_sensor(struct cy8c_cs_data *cs, struct cy8c_i2c_cs_platform_data *pdata)
427{
428 uint8_t ver[2] = {0}, chip[2] = {0};
429 int ret = 0;
430 pr_info("[cap] %s\n", __func__);
431
432 ret = i2c_cy8c_read(cs->client, CS_FW_CHIPID, chip, 1);
433 if (ret < 0) {
434 printk(KERN_ERR "[cap_err] Chip Read Err\n");
435 goto err_fw_get_fail;
436 }
437
438 ret = i2c_cy8c_read(cs->client, CS_FW_VERSION, ver, 1);
439 if (!ret)
440 cs->id.version = ver[0];
441 else {
442 printk(KERN_ERR "[cap_err] Ver Read Err\n");
443 goto err_fw_get_fail;
444 }
445
446 if (chip[0] == CS_CHIPID) {
447 cs->id.chipid = chip[0];
448 pr_info("[cap] CY8C_Smart_V%x\n", cs->id.version);
449 } else
450 pr_info("[cap] CY8C_Cap_V%x\n", cs->id.version);
451
452 ver[0] = 0;
453 ret = i2c_cy8c_read(cs->client, CS_FW_KEYCFG, ver, 1);
454 if (ret < 0) {
455 printk(KERN_ERR "[cap_err] Config Read Err\n");
456 goto err_fw_get_fail;
457 } else {
458 if ((ver[0] != 0) && (ver[0] == CS_KEY_3 || ver[0] == CS_KEY_4))
459 cs->id.config = ver[0];
460 else
461 cs->id.config = 0;
462 pr_info("[cap] config = %d\n", cs->id.config);
463 }
464 return 0;
465
466err_fw_get_fail:
467 return ret;
468}
469
470static void report_key_func(struct cy8c_cs_data *cs, uint8_t vk)
471{
472 int ret = 0;
473 if ((cs->debug_level & 0x01) || board_build_flag() > 0)
474 pr_info("[cap] vk = %x\n", vk);
475
476 if (vk) {
477 switch (vk) {
478 case 0x01:
479 input_report_key(cs->input_dev, cs->keycode[0], 1);
480 cs->vk_id = vk;
481 break;
482 case 0x02:
483 input_report_key(cs->input_dev, cs->keycode[1], 1);
484 cs->vk_id = vk;
485 break;
486 case 0x04:
487 input_report_key(cs->input_dev, cs->keycode[2], 1);
488 cs->vk_id = vk;
489 break;
490 case 0x08:
491 input_report_key(cs->input_dev, cs->keycode[3], 1);
492 cs->vk_id = vk;
493 break;
494 }
495#if defined(CONFIG_TOUCH_KEY_FILTER)
496 blocking_notifier_call_chain(&touchkey_notifier_list, 1, NULL);
497#endif
498 } else {
499 switch (cs->vk_id) {
500 case 0x01:
501 input_report_key(cs->input_dev, cs->keycode[0], 0);
502 break;
503 case 0x02:
504 input_report_key(cs->input_dev, cs->keycode[1], 0);
505 break;
506 case 0x04:
507 input_report_key(cs->input_dev, cs->keycode[2], 0);
508 break;
509 case 0x08:
510 input_report_key(cs->input_dev, cs->keycode[3], 0);
511 break;
512 }
513 cs->vk_id = 0;
514 }
515 input_sync(cs->input_dev);
516
517 if (cs->func_support & CS_FUNC_PRINTRAW) {
518 if (cs->vk_id) {
519 queue_delayed_work(cs->wq_raw, &cs->work_raw,
520 msecs_to_jiffies(CY8C_KEYLOCKTIME));
521 } else {
522 ret = cancel_delayed_work_sync(&cs->work_raw);
523 if (!ret)
524 cancel_delayed_work(&cs->work_raw);
525 }
526 }
527}
528
529static void cy8c_cs_work_func(struct work_struct *work)
530{
531 struct cy8c_cs_data *cs;
532 uint8_t buf[3] = {0};
533 static uint8_t pre_buf[3] = {0};
534
535 cs = container_of(work, struct cy8c_cs_data, work);
536
537 if (i2c_cy8c_read(cs->client, CS_STATUS, buf, 2) < 0) {
538 memset(buf, 0, sizeof(buf));
539 memset(pre_buf, 0, sizeof(pre_buf));
540 pr_err("[cap_err] %s i2c read fail", __func__);
541 goto enableirq;
542 }
543
544 if (!disable_key)
545 report_key_func(cs, buf[0]);
546
547 memcpy(pre_buf, buf, 2);
548enableirq:
549 if (!cs->use_irq)
550 hrtimer_start(&cs->timer, ktime_set(0, 20000000), HRTIMER_MODE_REL);
551 else
552 enable_irq(cs->client->irq);
553}
554
555#if 1
556static enum hrtimer_restart cy8c_cs_timer_func(struct hrtimer *timer)
557{
558 struct cy8c_cs_data *cs;
559
560 cs = container_of(timer, struct cy8c_cs_data, timer);
561 queue_work(cs->cy8c_wq, &cs->work);
562 return HRTIMER_NORESTART;
563}
564#endif
565#if 1
566static irqreturn_t cy8c_cs_irq_handler(int irq, void *dev_id)
567{
568 struct cy8c_cs_data *cs = dev_id;
569
570 disable_irq_nosync(cs->client->irq);
571 queue_work(cs->cy8c_wq, &cs->work);
572 return IRQ_HANDLED;
573}
574#endif
575
576static int cy8c_cs_probe(struct i2c_client *client,
577 const struct i2c_device_id *id)
578{
579 struct cy8c_cs_data *cs;
580 struct cy8c_i2c_cs_platform_data *pdata;
581 int ret = 0;
582
583 printk(KERN_DEBUG "[cap] %s: enter\n", __func__);
584
585 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
586 printk(KERN_ERR "[cap_err] need I2C_FUNC_I2C\n");
587 ret = -ENODEV;
588 goto err_check_functionality_failed;
589 }
590
591 cs = kzalloc(sizeof(struct cy8c_cs_data), GFP_KERNEL);
592 if (cs == NULL) {
593 printk(KERN_ERR "[cap_err] allocate cy8c_cs_data failed\n");
594 ret = -ENOMEM;
595 goto err_alloc_data_failed;
596 }
597
598 cs->client = client;
599 i2c_set_clientdata(client, cs);
600 pdata = client->dev.platform_data;
601
602 if (pdata) {
603 pdata->reset();
604 msleep(50);
605 cs->intr = pdata->gpio_irq;
606 }
607
608 if (cy8c_init_sensor(cs, pdata) < 0) {
609 pr_err("[cap_err] init failure, not probe up driver\n");
610 goto err_init_sensor_failed;
611 }
612 if (pdata) {
613 if (pdata->id.config != cs->id.config) {
614 pr_info("[cap] pdata ++\n");
615 pdata++;
616 }
617 }
618
619 cs->cy8c_wq = create_singlethread_workqueue("cypress_touchkey");
620 if (!cs->cy8c_wq) {
621 printk(KERN_ERR "[cap_err] create_singlethread_workqueue cy8c_wq fail\n");
622 goto err_create_wq_failed;
623 }
624 INIT_WORK(&cs->work, cy8c_cs_work_func);
625
626 cs->input_dev = input_allocate_device();
627 if (cs->input_dev == NULL) {
628 ret = -ENOMEM;
629 printk(KERN_ERR "[cap_err] Failed to allocate input device\n");
630 goto err_input_dev_alloc_failed;
631 }
632 cs->input_dev->name = "cy8c-touchkey";
633 cs->input_dev->id.product = cs->id.chipid;
634 cs->input_dev->id.version = cs->id.version;
635 cs->func_support = pdata->func_support;
636 cs->keycode = pdata->keycode;
637 cs->reset = pdata->reset;
638
639 set_bit(EV_SYN, cs->input_dev->evbit);
640 set_bit(EV_KEY, cs->input_dev->evbit);
641
642 set_bit(KEY_BACK, cs->input_dev->keybit);
643 set_bit(KEY_HOME, cs->input_dev->keybit);
644 set_bit(KEY_APP_SWITCH, cs->input_dev->keybit);
645 set_bit(KEY_MENU, cs->input_dev->keybit);
646
647 set_bit(KEY_SEARCH, cs->input_dev->keybit);
648 set_bit(KEY_WEIBO, cs->input_dev->keybit);
649
650 ret = input_register_device(cs->input_dev);
651 if (ret) {
652 printk(KERN_ERR "[cap_err] unable to register %s input device\n",
653 cs->input_dev->name);
654
655 goto err_input_register_device_failed;
656 }
657
658 private_cs = cs;
659
660 if (cs->func_support & CS_FUNC_PRINTRAW) {
661 pr_info("[cap]support_keylock(%x)\n", cs->func_support);
662 cs->wq_raw = create_singlethread_workqueue("CY8C_print_rawdata");
663 if (!cs->wq_raw) {
664 pr_err("[cap]allocate cy8c_cs_print_rawdata failed\n");
665 ret = -ENOMEM;
666 goto err_input_register_device_failed;
667 }
668 INIT_DELAYED_WORK(&cs->work_raw, cy8c_rawdata_print);
669 }
670
671#ifdef CONFIG_HAS_EARLYSUSPEND
672 cs->early_suspend.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING;
673 cs->early_suspend.suspend = cy8c_cs_early_suspend;
674 cs->early_suspend.resume = cy8c_cs_late_resume;
675 register_early_suspend(&cs->early_suspend);
676#endif
677 cy8c_touchkey_sysfs_init();
678
679 cs->use_irq = 1;
680 if (client->irq && cs->use_irq) {
681 ret = request_irq(client->irq, cy8c_cs_irq_handler,
682 IRQF_TRIGGER_FALLING,
683 cs->id.chipid == CS_CHIPID ? CYPRESS_SS_NAME : CYPRESS_CS_NAME,
684 cs);
685 if (ret < 0) {
686 dev_err(&client->dev, "[cap_err]request_irq failed\n");
687 printk(KERN_ERR "[cap_err] request_irq failed for gpio %d,"
688 " irq %d\n", cs->intr, client->irq);
689 }
690 }
691
692 if (!cs->use_irq) {
693 hrtimer_init(&cs->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
694 cs->timer.function = cy8c_cs_timer_func;
695 hrtimer_start(&cs->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
696 }
697
698 return 0;
699
700err_input_register_device_failed:
701 input_free_device(cs->input_dev);
702
703err_input_dev_alloc_failed:
704 destroy_workqueue(cs->cy8c_wq);
705err_init_sensor_failed:
706err_create_wq_failed:
707 kfree(cs);
708
709err_alloc_data_failed:
710err_check_functionality_failed:
711 return ret;
712}
713
714static int cy8c_cs_remove(struct i2c_client *client)
715{
716 struct cy8c_cs_data *cs = i2c_get_clientdata(client);
717
718 cy8c_touchkey_sysfs_deinit();
719
720 unregister_early_suspend(&cs->early_suspend);
721 free_irq(client->irq, cs);
722 input_unregister_device(cs->input_dev);
723
724 kfree(cs);
725
726 return 0;
727}
728
729static int cy8c_cs_suspend(struct i2c_client *client, pm_message_t mesg)
730{
731 int ret;
732 struct cy8c_cs_data *cs = i2c_get_clientdata(client);
733
734 pr_info("[cap] %s\n", __func__);
735
736 if (cs->func_support & CS_FUNC_PRINTRAW) {
737 ret = cancel_delayed_work_sync(&cs->work_raw);
738 if (!ret)
739 cancel_delayed_work(&cs->work_raw);
740 }
741 if (client->irq && cs->use_irq) {
742 disable_irq(client->irq);
743 ret = cancel_work_sync(&cs->work);
744 if (ret)
745 enable_irq(client->irq);
746 }
747 i2c_cy8c_write_byte_data(client, CS_MODE, CS_CMD_DSLEEP);
748 return 0;
749}
750
751static int cy8c_cs_resume(struct i2c_client *client)
752{
753 struct cy8c_cs_data *cs = i2c_get_clientdata(client);
754
755 pr_info("[cap] %s\n", __func__);
756 cs->reset();
757
758 msleep(50);
759
760 if (client->irq && cs->use_irq)
761 enable_irq(client->irq);
762 return 0;
763}
764
765#ifdef CONFIG_HAS_EARLYSUSPEND
766static void cy8c_cs_early_suspend(struct early_suspend *h)
767{
768 struct cy8c_cs_data *ts;
769 ts = container_of(h, struct cy8c_cs_data, early_suspend);
770 cy8c_cs_suspend(ts->client, PMSG_SUSPEND);
771}
772
773static void cy8c_cs_late_resume(struct early_suspend *h)
774{
775 struct cy8c_cs_data *ts;
776 ts = container_of(h, struct cy8c_cs_data, early_suspend);
777 cy8c_cs_resume(ts->client);
778}
779#endif
780
781static const struct i2c_device_id cy8c_cs_id[] = {
782 { CYPRESS_CS_NAME, 0 },
783};
784
785static struct i2c_driver cy8c_cs_driver = {
786 .probe = cy8c_cs_probe,
787 .remove = cy8c_cs_remove,
788 .id_table = cy8c_cs_id,
789#ifndef CONFIG_HAS_EARLYSUSPEND
790 .suspend = cy8c_cs_suspend,
791 .resume = cy8c_cs_resume,
792#endif
793 .driver = {
794 .name = CYPRESS_CS_NAME,
795 },
796};
797
798static int __init cy8c_cs_init(void)
799{
800 printk(KERN_INFO "[cap] %s: enter\n", __func__);
801 return i2c_add_driver(&cy8c_cs_driver);
802}
803
804static void __exit cy8c_cs_exit(void)
805{
806 i2c_del_driver(&cy8c_cs_driver);
807}
808
809module_init(cy8c_cs_init);
810module_exit(cy8c_cs_exit);
811
812MODULE_DESCRIPTION("cy8c_cs driver");
813MODULE_LICENSE("GPL");