1 /*
2 * cy8c4014/cy8c4024 touch pad driver
3 *
4 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/hrtimer.h>
20 #include <linux/i2c.h>
21 #include <linux/input.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/platform_device.h>
25 #include <linux/async.h>
26 #include <linux/gpio.h>
27 #include <asm/irq.h>
28 #include <linux/slab.h>
29 #include <linux/workqueue.h>
30 #include <linux/proc_fs.h>
31 #include <linux/input/mt.h>
32 #include "tp_suspend.h"
33 #include <linux/of_device.h>
34 #include <linux/of_gpio.h>
35 #include <linux/wakelock.h>
36 #include <linux/timer.h>
37 #include <linux/jiffies.h>
38
39 struct cy8c_chipdef {
40 int max_x;
41 int max_y;
42 int multi_touch_num;
43 u8 reg_base;
44 };
45
46 struct cy8c_touch {
47 struct i2c_client *client;
48 struct input_dev *input;
49 struct work_struct work;
50 struct workqueue_struct *wq;
51 int irq;
52 struct tp_device tp;
53 const struct cy8c_chipdef *cdef;
54 };
55
56 static const struct cy8c_chipdef cy8c4014_data = {
57 .max_x = 0xfe,
58 .max_y = 0xfe, /* only have x axis */
59 .multi_touch_num = 1,
60 .reg_base = 0x00,
61 };
62
i2c_cy8ctouch_read(struct cy8c_touch * ts,u8 * buf,u32 num)63 static u32 i2c_cy8ctouch_read(struct cy8c_touch *ts, u8 *buf, u32 num)
64 {
65 struct i2c_client *client = ts->client;
66 u8 reg = ts->cdef->reg_base;
67 struct i2c_msg xfer_msg[2];
68 int ret;
69
70 xfer_msg[0].addr = client->addr;
71 xfer_msg[0].len = 1;
72 xfer_msg[0].flags = 0;
73 xfer_msg[0].buf = ®
74
75 xfer_msg[1].addr = client->addr;
76 xfer_msg[1].len = num;
77 xfer_msg[1].flags = I2C_M_RD;
78 xfer_msg[1].buf = buf;
79
80 ret = i2c_transfer(client->adapter, xfer_msg, ARRAY_SIZE(xfer_msg));
81
82 if (ret != ARRAY_SIZE(xfer_msg))
83 return ret;
84
85 return 0;
86 }
87
cy8ctouch_irq(int irq,void * dev_id)88 static irqreturn_t cy8ctouch_irq(int irq, void *dev_id)
89 {
90 struct cy8c_touch *ts = (struct cy8c_touch *)dev_id;
91
92 disable_irq_nosync(ts->irq);
93 queue_work(ts->wq, &ts->work);
94
95 return IRQ_HANDLED;
96 }
97
cy8ctouch_ts_worker(struct work_struct * work)98 static void cy8ctouch_ts_worker(struct work_struct *work)
99 {
100 struct cy8c_touch *ts = container_of(work, struct cy8c_touch, work);
101 struct i2c_client *client = ts->client;
102 const struct cy8c_chipdef *cdef = ts->cdef;
103 int rc;
104 struct {
105 u8 cur_x;
106 u8 cur_y;
107 } pos;
108
109 rc = i2c_cy8ctouch_read(ts, (u8 *)&pos, sizeof(pos));
110 if (rc != 0) {
111 dev_err(&client->dev, "i2c read failed, ret = %d, line:%d\n",
112 rc, __LINE__);
113 enable_irq(ts->irq);
114 return;
115 }
116
117 if (pos.cur_x <= cdef->max_x && pos.cur_y <= cdef->max_y) {
118 /* finger down */
119 dev_dbg(&client->dev, "Touch down, position is %d : %d\n",
120 pos.cur_x, pos.cur_y);
121 input_report_abs(ts->input, ABS_X, pos.cur_x);
122 input_report_abs(ts->input, ABS_Y, pos.cur_y);
123 input_report_key(ts->input, BTN_TOUCH, 1);
124 input_sync(ts->input);
125 } else if (pos.cur_x == 0xff && pos.cur_y == 0xff) {
126 /* finger up */
127 dev_dbg(&client->dev, "Touch up, position is %d : %d\n",
128 pos.cur_x, pos.cur_y);
129 input_report_key(ts->input, BTN_TOUCH, 0);
130 input_sync(ts->input);
131 }
132 enable_irq(ts->irq);
133 }
134
cy8ctouch_init(struct i2c_client * client,struct cy8c_touch * ts)135 static int cy8ctouch_init(struct i2c_client *client, struct cy8c_touch *ts)
136 {
137 struct input_dev *input_device;
138 int rc = 0;
139
140 INIT_WORK(&ts->work, cy8ctouch_ts_worker);
141 ts->wq = create_singlethread_workqueue("cy8c_touchpad_wq");
142 if (!ts->wq) {
143 dev_err(&client->dev, "Create workqueue failed!\n");
144 return -ENOMEM;
145 }
146
147 input_device = devm_input_allocate_device(&ts->client->dev);
148 if (!input_device) {
149 dev_err(&client->dev, "Allocate input device failed!\n");
150 destroy_workqueue(ts->wq);
151 return -ENOMEM;
152 }
153
154 ts->input = input_device;
155 input_device->name = "cy8c_touchpad";
156 input_device->id.bustype = BUS_I2C;
157 input_device->dev.parent = &client->dev;
158 input_set_drvdata(input_device, ts);
159
160 __set_bit(EV_ABS, input_device->evbit);
161 __set_bit(EV_KEY, input_device->evbit);
162 __set_bit(BTN_TOUCH, input_device->keybit);
163
164 if (ts->cdef->max_x > 0) {
165 __set_bit(ABS_X, input_device->absbit);
166 input_set_abs_params(input_device,
167 ABS_X, 0, ts->cdef->max_x, 0, 0);
168 } else {
169 dev_err(&client->dev, "max_x is zero, touchpad don't have x axis\n");
170 }
171
172 if (ts->cdef->max_y > 0) {
173 __set_bit(ABS_Y, input_device->absbit);
174 input_set_abs_params(input_device,
175 ABS_Y, 0, ts->cdef->max_y, 0, 0);
176 } else {
177 dev_err(&client->dev, "max_y is zero, touchpad don't have y axis\n");
178 }
179
180 rc = input_register_device(input_device);
181 if (rc)
182 goto error_unreg_device;
183
184 return 0;
185
186 error_unreg_device:
187 destroy_workqueue(ts->wq);
188 return rc;
189 }
190
cy8ctouch_suspend(struct tp_device * tp_dev)191 static int cy8ctouch_suspend(struct tp_device *tp_dev)
192 {
193 struct cy8c_touch *ts = container_of(tp_dev, struct cy8c_touch, tp);
194
195 dev_info(&ts->client->dev, "[cy8ctouch] Enter %s\n", __func__);
196
197 return 0;
198 }
199
cy8ctouch_resume(struct tp_device * tp_dev)200 static int cy8ctouch_resume(struct tp_device *tp_dev)
201 {
202 struct cy8c_touch *ts = container_of(tp_dev, struct cy8c_touch, tp);
203
204 dev_info(&ts->client->dev, "[cy8ctouch] Enter %s\n", __func__);
205
206 return 0;
207 }
208
209 static const struct of_device_id of_cy8ctouch_dt_ids[] = {
210 {.compatible = "cypress,cy8c4014", .data = &cy8c4014_data},
211 {.compatible = "cypress,cy8c4024", .data = &cy8c4014_data},
212 { },
213 };
214 MODULE_DEVICE_TABLE(of, of_cy8ctouch_dt_ids);
215
216 static int
cy8ctouch_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)217 cy8ctouch_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
218 {
219 const struct of_device_id *of_dev_id;
220 struct device *dev = &client->dev;
221 struct cy8c_touch *ts;
222 int rc;
223
224 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
225 dev_err(&client->dev, "cy8ctouch I2C functionality not supported\n");
226 return -ENODEV;
227 }
228
229 of_dev_id = of_match_device(of_cy8ctouch_dt_ids, dev);
230 if (!of_dev_id)
231 return -EINVAL;
232
233 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
234 if (!ts)
235 return -ENOMEM;
236
237 ts->tp.tp_suspend = cy8ctouch_suspend;
238 ts->tp.tp_resume = cy8ctouch_resume;
239 tp_register_fb(&ts->tp);
240
241 ts->client = client;
242 i2c_set_clientdata(client, ts);
243
244 ts->cdef = of_dev_id->data;
245 rc = cy8ctouch_init(client, ts);
246 if (rc < 0) {
247 dev_err(&client->dev, "cy8ctouch init failed\n");
248 goto porbe_err_ret;
249 }
250
251 if (client->irq > 0) {
252 rc = devm_request_irq(dev, client->irq, cy8ctouch_irq, IRQF_TRIGGER_FALLING,
253 client->name, ts);
254 if (rc < 0) {
255 dev_err(&client->dev, "cy8ctouch_probe: request irq failed\n");
256 goto porbe_err_ret;
257 }
258 }
259
260 return 0;
261
262 porbe_err_ret:
263 return rc;
264 }
265
cy8ctouch_i2c_remove(struct i2c_client * client)266 static int cy8ctouch_i2c_remove(struct i2c_client *client)
267 {
268 struct cy8c_touch *ts = i2c_get_clientdata(client);
269
270 cancel_work_sync(&ts->work);
271 destroy_workqueue(ts->wq);
272
273 return 0;
274 }
275
276 static const struct i2c_device_id cy8ctouch_id[] = {
277 { "cy8c4014", 0 },
278 { "cy8c4024", 0 },
279 { },
280 };
281
282 static struct i2c_driver cy8ctouch_i2c_driver = {
283 .driver = {
284 .name = "cy8c_touchpad",
285 .of_match_table = of_match_ptr(of_cy8ctouch_dt_ids),
286 },
287 .probe = cy8ctouch_i2c_probe,
288 .remove = cy8ctouch_i2c_remove,
289 .id_table = cy8ctouch_id,
290 };
291
292 module_i2c_driver(cy8ctouch_i2c_driver);
293
294 MODULE_AUTHOR("Wenping Zhang <zwp@rock-chips.com>");
295 MODULE_DESCRIPTION("Cypress Cy8cxxxx touchpad driver");
296 MODULE_LICENSE("GPL v2");
297