1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Elan touchscreens that use the i2c-hid protocol.
5 * Copyright 2020 Google LLC
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
16 #include <linux/regulator/consumer.h>
20 struct elan_i2c_hid_chip_data {
21 unsigned int post_gpio_reset_delay_ms;
22 unsigned int post_power_delay_ms;
23 u16 hid_descriptor_address;
26 struct i2c_hid_of_elan {
27 struct i2chid_ops ops;
29 struct regulator *vcc33;
30 struct regulator *vccio;
31 struct gpio_desc *reset_gpio;
32 const struct elan_i2c_hid_chip_data *chip_data;
35 static int elan_i2c_hid_power_up(struct i2chid_ops *ops)
37 struct i2c_hid_of_elan *ihid_elan =
38 container_of(ops, struct i2c_hid_of_elan, ops);
41 ret = regulator_enable(ihid_elan->vcc33);
45 ret = regulator_enable(ihid_elan->vccio);
47 regulator_disable(ihid_elan->vcc33);
51 if (ihid_elan->chip_data->post_power_delay_ms)
52 msleep(ihid_elan->chip_data->post_power_delay_ms);
54 gpiod_set_value_cansleep(ihid_elan->reset_gpio, 0);
55 if (ihid_elan->chip_data->post_gpio_reset_delay_ms)
56 msleep(ihid_elan->chip_data->post_gpio_reset_delay_ms);
61 static void elan_i2c_hid_power_down(struct i2chid_ops *ops)
63 struct i2c_hid_of_elan *ihid_elan =
64 container_of(ops, struct i2c_hid_of_elan, ops);
66 gpiod_set_value_cansleep(ihid_elan->reset_gpio, 1);
67 regulator_disable(ihid_elan->vccio);
68 regulator_disable(ihid_elan->vcc33);
71 static int i2c_hid_of_elan_probe(struct i2c_client *client,
72 const struct i2c_device_id *id)
74 struct i2c_hid_of_elan *ihid_elan;
76 ihid_elan = devm_kzalloc(&client->dev, sizeof(*ihid_elan), GFP_KERNEL);
80 ihid_elan->ops.power_up = elan_i2c_hid_power_up;
81 ihid_elan->ops.power_down = elan_i2c_hid_power_down;
83 /* Start out with reset asserted */
84 ihid_elan->reset_gpio =
85 devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
86 if (IS_ERR(ihid_elan->reset_gpio))
87 return PTR_ERR(ihid_elan->reset_gpio);
89 ihid_elan->vccio = devm_regulator_get(&client->dev, "vccio");
90 if (IS_ERR(ihid_elan->vccio))
91 return PTR_ERR(ihid_elan->vccio);
93 ihid_elan->vcc33 = devm_regulator_get(&client->dev, "vcc33");
94 if (IS_ERR(ihid_elan->vcc33))
95 return PTR_ERR(ihid_elan->vcc33);
97 ihid_elan->chip_data = device_get_match_data(&client->dev);
99 return i2c_hid_core_probe(client, &ihid_elan->ops,
100 ihid_elan->chip_data->hid_descriptor_address, 0);
103 static const struct elan_i2c_hid_chip_data elan_ekth6915_chip_data = {
104 .post_power_delay_ms = 1,
105 .post_gpio_reset_delay_ms = 300,
106 .hid_descriptor_address = 0x0001,
109 static const struct of_device_id elan_i2c_hid_of_match[] = {
110 { .compatible = "elan,ekth6915", .data = &elan_ekth6915_chip_data },
113 MODULE_DEVICE_TABLE(of, elan_i2c_hid_of_match);
115 static struct i2c_driver elan_i2c_hid_ts_driver = {
117 .name = "i2c_hid_of_elan",
118 .pm = &i2c_hid_core_pm,
119 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
120 .of_match_table = of_match_ptr(elan_i2c_hid_of_match),
122 .probe = i2c_hid_of_elan_probe,
123 .remove = i2c_hid_core_remove,
124 .shutdown = i2c_hid_core_shutdown,
126 module_i2c_driver(elan_i2c_hid_ts_driver);
128 MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>");
129 MODULE_DESCRIPTION("Elan i2c-hid touchscreen driver");
130 MODULE_LICENSE("GPL");