1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for Elan touchscreens that use the i2c-hid protocol.
4  *
5  * Copyright 2020 Google LLC
6  */
7 
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>
14 #include <linux/of.h>
15 #include <linux/pm.h>
16 #include <linux/regulator/consumer.h>
17 
18 #include "i2c-hid.h"
19 
20 struct elan_i2c_hid_chip_data {
21 	unsigned int post_gpio_reset_on_delay_ms;
22 	unsigned int post_gpio_reset_off_delay_ms;
23 	unsigned int post_power_delay_ms;
24 	u16 hid_descriptor_address;
25 	const char *main_supply_name;
26 };
27 
28 struct i2c_hid_of_elan {
29 	struct i2chid_ops ops;
30 
31 	struct regulator *vcc33;
32 	struct regulator *vccio;
33 	struct gpio_desc *reset_gpio;
34 	const struct elan_i2c_hid_chip_data *chip_data;
35 };
36 
37 static int elan_i2c_hid_power_up(struct i2chid_ops *ops)
38 {
39 	struct i2c_hid_of_elan *ihid_elan =
40 		container_of(ops, struct i2c_hid_of_elan, ops);
41 	int ret;
42 
43 	if (ihid_elan->vcc33) {
44 		ret = regulator_enable(ihid_elan->vcc33);
45 		if (ret)
46 			return ret;
47 	}
48 
49 	ret = regulator_enable(ihid_elan->vccio);
50 	if (ret) {
51 		regulator_disable(ihid_elan->vcc33);
52 		return ret;
53 	}
54 
55 	if (ihid_elan->chip_data->post_power_delay_ms)
56 		msleep(ihid_elan->chip_data->post_power_delay_ms);
57 
58 	gpiod_set_value_cansleep(ihid_elan->reset_gpio, 0);
59 	if (ihid_elan->chip_data->post_gpio_reset_on_delay_ms)
60 		msleep(ihid_elan->chip_data->post_gpio_reset_on_delay_ms);
61 
62 	return 0;
63 }
64 
65 static void elan_i2c_hid_power_down(struct i2chid_ops *ops)
66 {
67 	struct i2c_hid_of_elan *ihid_elan =
68 		container_of(ops, struct i2c_hid_of_elan, ops);
69 
70 	gpiod_set_value_cansleep(ihid_elan->reset_gpio, 1);
71 	if (ihid_elan->chip_data->post_gpio_reset_off_delay_ms)
72 		msleep(ihid_elan->chip_data->post_gpio_reset_off_delay_ms);
73 
74 	regulator_disable(ihid_elan->vccio);
75 	if (ihid_elan->vcc33)
76 		regulator_disable(ihid_elan->vcc33);
77 }
78 
79 static int i2c_hid_of_elan_probe(struct i2c_client *client)
80 {
81 	struct i2c_hid_of_elan *ihid_elan;
82 
83 	ihid_elan = devm_kzalloc(&client->dev, sizeof(*ihid_elan), GFP_KERNEL);
84 	if (!ihid_elan)
85 		return -ENOMEM;
86 
87 	ihid_elan->ops.power_up = elan_i2c_hid_power_up;
88 	ihid_elan->ops.power_down = elan_i2c_hid_power_down;
89 
90 	/* Start out with reset asserted */
91 	ihid_elan->reset_gpio =
92 		devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
93 	if (IS_ERR(ihid_elan->reset_gpio))
94 		return PTR_ERR(ihid_elan->reset_gpio);
95 
96 	ihid_elan->vccio = devm_regulator_get(&client->dev, "vccio");
97 	if (IS_ERR(ihid_elan->vccio))
98 		return PTR_ERR(ihid_elan->vccio);
99 
100 	ihid_elan->chip_data = device_get_match_data(&client->dev);
101 
102 	if (ihid_elan->chip_data->main_supply_name) {
103 		ihid_elan->vcc33 = devm_regulator_get(&client->dev,
104 						      ihid_elan->chip_data->main_supply_name);
105 		if (IS_ERR(ihid_elan->vcc33))
106 			return PTR_ERR(ihid_elan->vcc33);
107 	}
108 
109 	return i2c_hid_core_probe(client, &ihid_elan->ops,
110 				  ihid_elan->chip_data->hid_descriptor_address, 0);
111 }
112 
113 static const struct elan_i2c_hid_chip_data elan_ekth6915_chip_data = {
114 	.post_power_delay_ms = 1,
115 	.post_gpio_reset_on_delay_ms = 300,
116 	.hid_descriptor_address = 0x0001,
117 	.main_supply_name = "vcc33",
118 };
119 
120 static const struct elan_i2c_hid_chip_data ilitek_ili9882t_chip_data = {
121 	.post_power_delay_ms = 1,
122 	.post_gpio_reset_on_delay_ms = 200,
123 	.post_gpio_reset_off_delay_ms = 65,
124 	.hid_descriptor_address = 0x0001,
125 	/*
126 	 * this touchscreen is tightly integrated with the panel and assumes
127 	 * that the relevant power rails (other than the IO rail) have already
128 	 * been turned on by the panel driver because we're a panel follower.
129 	 */
130 	.main_supply_name = NULL,
131 };
132 
133 static const struct of_device_id elan_i2c_hid_of_match[] = {
134 	{ .compatible = "elan,ekth6915", .data = &elan_ekth6915_chip_data },
135 	{ .compatible = "ilitek,ili9882t", .data = &ilitek_ili9882t_chip_data },
136 	{ }
137 };
138 MODULE_DEVICE_TABLE(of, elan_i2c_hid_of_match);
139 
140 static struct i2c_driver elan_i2c_hid_ts_driver = {
141 	.driver = {
142 		.name	= "i2c_hid_of_elan",
143 		.pm	= &i2c_hid_core_pm,
144 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
145 		.of_match_table = of_match_ptr(elan_i2c_hid_of_match),
146 	},
147 	.probe		= i2c_hid_of_elan_probe,
148 	.remove		= i2c_hid_core_remove,
149 	.shutdown	= i2c_hid_core_shutdown,
150 };
151 module_i2c_driver(elan_i2c_hid_ts_driver);
152 
153 MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>");
154 MODULE_DESCRIPTION("Elan i2c-hid touchscreen driver");
155 MODULE_LICENSE("GPL");
156