blob: 0bf14067c1678978eb86ba984eb650ed13a3d267 [file] [log] [blame]
Hans de Goedea93ad652015-03-21 20:40:45 -07001/*
2 * Driver for ChipOne icn8318 i2c touchscreen controller
3 *
4 * Copyright (c) 2015 Red Hat Inc.
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 * Red Hat authors:
12 * Hans de Goede <hdegoede@redhat.com>
13 */
14
15#include <linux/gpio/consumer.h>
16#include <linux/interrupt.h>
17#include <linux/i2c.h>
18#include <linux/input.h>
19#include <linux/input/mt.h>
Hans de Goede50dd9f62016-07-15 14:28:11 -070020#include <linux/input/touchscreen.h>
Hans de Goedea93ad652015-03-21 20:40:45 -070021#include <linux/module.h>
22#include <linux/of.h>
23
24#define ICN8318_REG_POWER 4
25#define ICN8318_REG_TOUCHDATA 16
26
27#define ICN8318_POWER_ACTIVE 0
28#define ICN8318_POWER_MONITOR 1
29#define ICN8318_POWER_HIBERNATE 2
30
31#define ICN8318_MAX_TOUCHES 5
32
33struct icn8318_touch {
34 __u8 slot;
35 __be16 x;
36 __be16 y;
37 __u8 pressure; /* Seems more like finger width then pressure really */
38 __u8 event;
39/* The difference between 2 and 3 is unclear */
40#define ICN8318_EVENT_NO_DATA 1 /* No finger seen yet since wakeup */
41#define ICN8318_EVENT_UPDATE1 2 /* New or updated coordinates */
42#define ICN8318_EVENT_UPDATE2 3 /* New or updated coordinates */
43#define ICN8318_EVENT_END 4 /* Finger lifted */
44} __packed;
45
46struct icn8318_touch_data {
47 __u8 softbutton;
48 __u8 touch_count;
49 struct icn8318_touch touches[ICN8318_MAX_TOUCHES];
50} __packed;
51
52struct icn8318_data {
53 struct i2c_client *client;
54 struct input_dev *input;
55 struct gpio_desc *wake_gpio;
Hans de Goede50dd9f62016-07-15 14:28:11 -070056 struct touchscreen_properties prop;
Hans de Goedea93ad652015-03-21 20:40:45 -070057};
58
59static int icn8318_read_touch_data(struct i2c_client *client,
60 struct icn8318_touch_data *touch_data)
61{
62 u8 reg = ICN8318_REG_TOUCHDATA;
63 struct i2c_msg msg[2] = {
64 {
65 .addr = client->addr,
66 .len = 1,
67 .buf = &reg
68 },
69 {
70 .addr = client->addr,
71 .flags = I2C_M_RD,
72 .len = sizeof(struct icn8318_touch_data),
73 .buf = (u8 *)touch_data
74 }
75 };
76
77 return i2c_transfer(client->adapter, msg, 2);
78}
79
80static inline bool icn8318_touch_active(u8 event)
81{
82 return (event == ICN8318_EVENT_UPDATE1) ||
83 (event == ICN8318_EVENT_UPDATE2);
84}
85
86static irqreturn_t icn8318_irq(int irq, void *dev_id)
87{
88 struct icn8318_data *data = dev_id;
89 struct device *dev = &data->client->dev;
90 struct icn8318_touch_data touch_data;
Hans de Goede50dd9f62016-07-15 14:28:11 -070091 int i, ret;
Hans de Goedea93ad652015-03-21 20:40:45 -070092
93 ret = icn8318_read_touch_data(data->client, &touch_data);
94 if (ret < 0) {
95 dev_err(dev, "Error reading touch data: %d\n", ret);
96 return IRQ_HANDLED;
97 }
98
99 if (touch_data.softbutton) {
100 /*
101 * Other data is invalid when a softbutton is pressed.
102 * This needs some extra devicetree bindings to map the icn8318
103 * softbutton codes to evdev codes. Currently no known devices
104 * use this.
105 */
106 return IRQ_HANDLED;
107 }
108
109 if (touch_data.touch_count > ICN8318_MAX_TOUCHES) {
110 dev_warn(dev, "Too much touches %d > %d\n",
111 touch_data.touch_count, ICN8318_MAX_TOUCHES);
112 touch_data.touch_count = ICN8318_MAX_TOUCHES;
113 }
114
115 for (i = 0; i < touch_data.touch_count; i++) {
116 struct icn8318_touch *touch = &touch_data.touches[i];
117 bool act = icn8318_touch_active(touch->event);
118
119 input_mt_slot(data->input, touch->slot);
120 input_mt_report_slot_state(data->input, MT_TOOL_FINGER, act);
121 if (!act)
122 continue;
123
Hans de Goede50dd9f62016-07-15 14:28:11 -0700124 touchscreen_report_pos(data->input, &data->prop,
125 be16_to_cpu(touch->x),
126 be16_to_cpu(touch->y), true);
Hans de Goedea93ad652015-03-21 20:40:45 -0700127 }
128
129 input_mt_sync_frame(data->input);
130 input_sync(data->input);
131
132 return IRQ_HANDLED;
133}
134
135static int icn8318_start(struct input_dev *dev)
136{
137 struct icn8318_data *data = input_get_drvdata(dev);
138
139 enable_irq(data->client->irq);
140 gpiod_set_value_cansleep(data->wake_gpio, 1);
141
142 return 0;
143}
144
145static void icn8318_stop(struct input_dev *dev)
146{
147 struct icn8318_data *data = input_get_drvdata(dev);
148
149 disable_irq(data->client->irq);
150 i2c_smbus_write_byte_data(data->client, ICN8318_REG_POWER,
151 ICN8318_POWER_HIBERNATE);
152 gpiod_set_value_cansleep(data->wake_gpio, 0);
153}
154
155#ifdef CONFIG_PM_SLEEP
156static int icn8318_suspend(struct device *dev)
157{
158 struct icn8318_data *data = i2c_get_clientdata(to_i2c_client(dev));
159
160 mutex_lock(&data->input->mutex);
161 if (data->input->users)
162 icn8318_stop(data->input);
163 mutex_unlock(&data->input->mutex);
164
165 return 0;
166}
167
168static int icn8318_resume(struct device *dev)
169{
170 struct icn8318_data *data = i2c_get_clientdata(to_i2c_client(dev));
171
172 mutex_lock(&data->input->mutex);
173 if (data->input->users)
174 icn8318_start(data->input);
175 mutex_unlock(&data->input->mutex);
176
177 return 0;
178}
179#endif
180
181static SIMPLE_DEV_PM_OPS(icn8318_pm_ops, icn8318_suspend, icn8318_resume);
182
183static int icn8318_probe(struct i2c_client *client,
184 const struct i2c_device_id *id)
185{
186 struct device *dev = &client->dev;
Hans de Goedea93ad652015-03-21 20:40:45 -0700187 struct icn8318_data *data;
188 struct input_dev *input;
Hans de Goedea93ad652015-03-21 20:40:45 -0700189 int error;
190
191 if (!client->irq) {
192 dev_err(dev, "Error no irq specified\n");
193 return -EINVAL;
194 }
195
196 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
197 if (!data)
198 return -ENOMEM;
199
200 data->wake_gpio = devm_gpiod_get(dev, "wake", GPIOD_OUT_LOW);
201 if (IS_ERR(data->wake_gpio)) {
202 error = PTR_ERR(data->wake_gpio);
203 if (error != -EPROBE_DEFER)
204 dev_err(dev, "Error getting wake gpio: %d\n", error);
205 return error;
206 }
207
Hans de Goedea93ad652015-03-21 20:40:45 -0700208 input = devm_input_allocate_device(dev);
209 if (!input)
210 return -ENOMEM;
211
212 input->name = client->name;
213 input->id.bustype = BUS_I2C;
214 input->open = icn8318_start;
215 input->close = icn8318_stop;
216 input->dev.parent = dev;
217
Hans de Goede50dd9f62016-07-15 14:28:11 -0700218 input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
219 input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
220
221 touchscreen_parse_properties(input, true, &data->prop);
222 if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
223 !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
224 dev_err(dev, "Error touchscreen-size-x and/or -y missing\n");
225 return -EINVAL;
Hans de Goedea93ad652015-03-21 20:40:45 -0700226 }
227
228 error = input_mt_init_slots(input, ICN8318_MAX_TOUCHES,
229 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
230 if (error)
231 return error;
232
233 data->client = client;
234 data->input = input;
235 input_set_drvdata(input, data);
236
237 error = devm_request_threaded_irq(dev, client->irq, NULL, icn8318_irq,
238 IRQF_ONESHOT, client->name, data);
239 if (error) {
240 dev_err(dev, "Error requesting irq: %d\n", error);
241 return error;
242 }
243
244 /* Stop device till opened */
245 icn8318_stop(data->input);
246
247 error = input_register_device(input);
248 if (error)
249 return error;
250
251 i2c_set_clientdata(client, data);
252
253 return 0;
254}
255
256static const struct of_device_id icn8318_of_match[] = {
257 { .compatible = "chipone,icn8318" },
258 { }
259};
260MODULE_DEVICE_TABLE(of, icn8318_of_match);
261
262/* This is useless for OF-enabled devices, but it is needed by I2C subsystem */
263static const struct i2c_device_id icn8318_i2c_id[] = {
264 { },
265};
266MODULE_DEVICE_TABLE(i2c, icn8318_i2c_id);
267
268static struct i2c_driver icn8318_driver = {
269 .driver = {
Hans de Goedea93ad652015-03-21 20:40:45 -0700270 .name = "chipone_icn8318",
271 .pm = &icn8318_pm_ops,
272 .of_match_table = icn8318_of_match,
273 },
274 .probe = icn8318_probe,
275 .id_table = icn8318_i2c_id,
276};
277
278module_i2c_driver(icn8318_driver);
279
280MODULE_DESCRIPTION("ChipOne icn8318 I2C Touchscreen Driver");
281MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
282MODULE_LICENSE("GPL");