1 #include <linux/module.h>
3 #include <linux/interrupt.h>
4 #include <linux/slab.h>
5 #include <linux/input.h>
6 #include <linux/input/mt.h>
7 #include <linux/input/touchscreen.h>
8 #include <linux/delay.h>
9 #include <linux/workqueue.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/of_device.h>
12 #include <asm/unaligned.h>
14 #define ILI210X_TOUCHES 2
15 #define ILI251X_TOUCHES 10
16 #define DEFAULT_POLL_PERIOD 20
18 /* Touchscreen commands */
19 #define REG_TOUCHDATA 0x10
20 #define REG_PANEL_INFO 0x20
21 #define REG_FIRMWARE_VERSION 0x40
22 #define REG_CALIBRATE 0xcc
24 struct firmware_version {
36 struct i2c_client *client;
37 struct input_dev *input;
38 unsigned int poll_period;
39 struct delayed_work dwork;
40 struct gpio_desc *reset_gpio;
41 struct touchscreen_properties prop;
42 enum ili2xxx_model model;
43 unsigned int max_touches;
46 static int ili210x_read_reg(struct i2c_client *client, u8 reg, void *buf,
49 struct ili210x *priv = i2c_get_clientdata(client);
50 struct i2c_msg msg[2] = {
65 if (priv->model == MODEL_ILI251X) {
66 if (i2c_transfer(client->adapter, msg, 1) != 1) {
67 dev_err(&client->dev, "i2c transfer failed\n");
71 usleep_range(5000, 5500);
73 if (i2c_transfer(client->adapter, msg + 1, 1) != 1) {
74 dev_err(&client->dev, "i2c transfer failed\n");
78 if (i2c_transfer(client->adapter, msg, 2) != 2) {
79 dev_err(&client->dev, "i2c transfer failed\n");
87 static int ili210x_read(struct i2c_client *client, void *buf, size_t len)
89 struct i2c_msg msg = {
96 if (i2c_transfer(client->adapter, &msg, 1) != 1) {
97 dev_err(&client->dev, "i2c transfer failed\n");
104 static bool ili210x_touchdata_to_coords(struct ili210x *priv, u8 *touchdata,
106 unsigned int *x, unsigned int *y)
108 if (finger >= ILI210X_TOUCHES)
111 if (touchdata[0] & BIT(finger))
114 *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0);
115 *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2);
120 static bool ili251x_touchdata_to_coords(struct ili210x *priv, u8 *touchdata,
122 unsigned int *x, unsigned int *y)
124 if (finger >= ILI251X_TOUCHES)
127 *x = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0);
128 if (!(*x & BIT(15))) /* Touch indication */
132 *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2);
137 static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata)
139 struct input_dev *input = priv->input;
141 bool contact = false, touch = false;
142 unsigned int x = 0, y = 0;
144 for (i = 0; i < priv->max_touches; i++) {
145 if (priv->model == MODEL_ILI210X) {
146 touch = ili210x_touchdata_to_coords(priv, touchdata,
148 } else if (priv->model == MODEL_ILI251X) {
149 touch = ili251x_touchdata_to_coords(priv, touchdata,
155 input_mt_slot(input, i);
156 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
159 touchscreen_report_pos(input, &priv->prop, x, y,
163 input_mt_report_pointer_emulation(input, false);
166 if (priv->model == MODEL_ILI210X)
167 contact = touchdata[0] & 0xf3;
172 static void ili210x_work(struct work_struct *work)
174 struct ili210x *priv = container_of(work, struct ili210x,
176 struct i2c_client *client = priv->client;
177 u8 touchdata[64] = { 0 };
181 if (priv->model == MODEL_ILI210X) {
182 error = ili210x_read_reg(client, REG_TOUCHDATA,
183 touchdata, sizeof(touchdata));
184 } else if (priv->model == MODEL_ILI251X) {
185 error = ili210x_read_reg(client, REG_TOUCHDATA,
187 if (!error && touchdata[0] == 2)
188 error = ili210x_read(client, &touchdata[31], 20);
192 dev_err(&client->dev,
193 "Unable to get touchdata, err = %d\n", error);
197 touch = ili210x_report_events(priv, touchdata);
200 schedule_delayed_work(&priv->dwork,
201 msecs_to_jiffies(priv->poll_period));
204 static irqreturn_t ili210x_irq(int irq, void *irq_data)
206 struct ili210x *priv = irq_data;
208 schedule_delayed_work(&priv->dwork, 0);
213 static ssize_t ili210x_calibrate(struct device *dev,
214 struct device_attribute *attr,
215 const char *buf, size_t count)
217 struct i2c_client *client = to_i2c_client(dev);
218 struct ili210x *priv = i2c_get_clientdata(client);
219 unsigned long calibrate;
221 u8 cmd = REG_CALIBRATE;
223 if (kstrtoul(buf, 10, &calibrate))
230 rc = i2c_master_send(priv->client, &cmd, sizeof(cmd));
231 if (rc != sizeof(cmd))
237 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate);
239 static struct attribute *ili210x_attributes[] = {
240 &dev_attr_calibrate.attr,
244 static const struct attribute_group ili210x_attr_group = {
245 .attrs = ili210x_attributes,
248 static void ili210x_power_down(void *data)
250 struct gpio_desc *reset_gpio = data;
252 gpiod_set_value_cansleep(reset_gpio, 1);
255 static void ili210x_cancel_work(void *data)
257 struct ili210x *priv = data;
259 cancel_delayed_work_sync(&priv->dwork);
262 static int ili210x_i2c_probe(struct i2c_client *client,
263 const struct i2c_device_id *id)
265 struct device *dev = &client->dev;
266 struct ili210x *priv;
267 struct gpio_desc *reset_gpio;
268 struct input_dev *input;
269 struct firmware_version firmware;
270 enum ili2xxx_model model;
273 model = (enum ili2xxx_model)id->driver_data;
275 dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
277 if (client->irq <= 0) {
278 dev_err(dev, "No IRQ!\n");
282 reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
283 if (IS_ERR(reset_gpio))
284 return PTR_ERR(reset_gpio);
287 error = devm_add_action_or_reset(dev, ili210x_power_down,
292 usleep_range(50, 100);
293 gpiod_set_value_cansleep(reset_gpio, 0);
297 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
301 input = devm_input_allocate_device(dev);
305 priv->client = client;
307 priv->poll_period = DEFAULT_POLL_PERIOD;
308 INIT_DELAYED_WORK(&priv->dwork, ili210x_work);
309 priv->reset_gpio = reset_gpio;
311 if (model == MODEL_ILI210X)
312 priv->max_touches = ILI210X_TOUCHES;
313 if (model == MODEL_ILI251X)
314 priv->max_touches = ILI251X_TOUCHES;
316 i2c_set_clientdata(client, priv);
318 /* Get firmware version */
319 error = ili210x_read_reg(client, REG_FIRMWARE_VERSION,
320 &firmware, sizeof(firmware));
322 dev_err(dev, "Failed to get firmware version, err: %d\n",
327 /* Setup input device */
328 input->name = "ILI210x Touchscreen";
329 input->id.bustype = BUS_I2C;
330 input->dev.parent = dev;
333 input_set_abs_params(input, ABS_MT_POSITION_X, 0, 0xffff, 0, 0);
334 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 0xffff, 0, 0);
335 touchscreen_parse_properties(input, true, &priv->prop);
336 input_mt_init_slots(input, priv->max_touches, INPUT_MT_DIRECT);
338 error = devm_add_action(dev, ili210x_cancel_work, priv);
342 error = devm_request_irq(dev, client->irq, ili210x_irq, 0,
345 dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
350 error = devm_device_add_group(dev, &ili210x_attr_group);
352 dev_err(dev, "Unable to create sysfs attributes, err: %d\n",
357 error = input_register_device(priv->input);
359 dev_err(dev, "Cannot register input device, err: %d\n", error);
363 device_init_wakeup(dev, 1);
366 "ILI210x initialized (IRQ: %d), firmware version %d.%d.%d",
367 client->irq, firmware.id, firmware.major, firmware.minor);
372 static int __maybe_unused ili210x_i2c_suspend(struct device *dev)
374 struct i2c_client *client = to_i2c_client(dev);
376 if (device_may_wakeup(&client->dev))
377 enable_irq_wake(client->irq);
382 static int __maybe_unused ili210x_i2c_resume(struct device *dev)
384 struct i2c_client *client = to_i2c_client(dev);
386 if (device_may_wakeup(&client->dev))
387 disable_irq_wake(client->irq);
392 static SIMPLE_DEV_PM_OPS(ili210x_i2c_pm,
393 ili210x_i2c_suspend, ili210x_i2c_resume);
395 static const struct i2c_device_id ili210x_i2c_id[] = {
396 { "ili210x", MODEL_ILI210X },
397 { "ili251x", MODEL_ILI251X },
400 MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
402 static const struct of_device_id ili210x_dt_ids[] = {
403 { .compatible = "ilitek,ili210x", .data = (void *)MODEL_ILI210X },
404 { .compatible = "ilitek,ili251x", .data = (void *)MODEL_ILI251X },
407 MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
409 static struct i2c_driver ili210x_ts_driver = {
411 .name = "ili210x_i2c",
412 .pm = &ili210x_i2c_pm,
413 .of_match_table = ili210x_dt_ids,
415 .id_table = ili210x_i2c_id,
416 .probe = ili210x_i2c_probe,
419 module_i2c_driver(ili210x_ts_driver);
422 MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
423 MODULE_LICENSE("GPL");