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