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_delay_ms;
22 	unsigned int post_power_delay_ms;
23 	u16 hid_descriptor_address;
24 };
25 
26 struct i2c_hid_of_elan {
27 	struct i2chid_ops ops;
28 
29 	struct regulator *vcc33;
30 	struct regulator *vccio;
31 	struct gpio_desc *reset_gpio;
32 	const struct elan_i2c_hid_chip_data *chip_data;
33 };
34 
elan_i2c_hid_power_up(struct i2chid_ops * ops)35 static int elan_i2c_hid_power_up(struct i2chid_ops *ops)
36 {
37 	struct i2c_hid_of_elan *ihid_elan =
38 		container_of(ops, struct i2c_hid_of_elan, ops);
39 	int ret;
40 
41 	ret = regulator_enable(ihid_elan->vcc33);
42 	if (ret)
43 		return ret;
44 
45 	ret = regulator_enable(ihid_elan->vccio);
46 	if (ret) {
47 		regulator_disable(ihid_elan->vcc33);
48 		return ret;
49 	}
50 
51 	if (ihid_elan->chip_data->post_power_delay_ms)
52 		msleep(ihid_elan->chip_data->post_power_delay_ms);
53 
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);
57 
58 	return 0;
59 }
60 
elan_i2c_hid_power_down(struct i2chid_ops * ops)61 static void elan_i2c_hid_power_down(struct i2chid_ops *ops)
62 {
63 	struct i2c_hid_of_elan *ihid_elan =
64 		container_of(ops, struct i2c_hid_of_elan, ops);
65 
66 	gpiod_set_value_cansleep(ihid_elan->reset_gpio, 1);
67 	regulator_disable(ihid_elan->vccio);
68 	regulator_disable(ihid_elan->vcc33);
69 }
70 
i2c_hid_of_elan_probe(struct i2c_client * client,const struct i2c_device_id * id)71 static int i2c_hid_of_elan_probe(struct i2c_client *client,
72 				 const struct i2c_device_id *id)
73 {
74 	struct i2c_hid_of_elan *ihid_elan;
75 
76 	ihid_elan = devm_kzalloc(&client->dev, sizeof(*ihid_elan), GFP_KERNEL);
77 	if (!ihid_elan)
78 		return -ENOMEM;
79 
80 	ihid_elan->ops.power_up = elan_i2c_hid_power_up;
81 	ihid_elan->ops.power_down = elan_i2c_hid_power_down;
82 
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);
88 
89 	ihid_elan->vccio = devm_regulator_get(&client->dev, "vccio");
90 	if (IS_ERR(ihid_elan->vccio))
91 		return PTR_ERR(ihid_elan->vccio);
92 
93 	ihid_elan->vcc33 = devm_regulator_get(&client->dev, "vcc33");
94 	if (IS_ERR(ihid_elan->vcc33))
95 		return PTR_ERR(ihid_elan->vcc33);
96 
97 	ihid_elan->chip_data = device_get_match_data(&client->dev);
98 
99 	return i2c_hid_core_probe(client, &ihid_elan->ops,
100 				  ihid_elan->chip_data->hid_descriptor_address, 0);
101 }
102 
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,
107 };
108 
109 static const struct of_device_id elan_i2c_hid_of_match[] = {
110 	{ .compatible = "elan,ekth6915", .data = &elan_ekth6915_chip_data },
111 	{ }
112 };
113 MODULE_DEVICE_TABLE(of, elan_i2c_hid_of_match);
114 
115 static struct i2c_driver elan_i2c_hid_ts_driver = {
116 	.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),
121 	},
122 	.probe		= i2c_hid_of_elan_probe,
123 	.remove		= i2c_hid_core_remove,
124 	.shutdown	= i2c_hid_core_shutdown,
125 };
126 module_i2c_driver(elan_i2c_hid_ts_driver);
127 
128 MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>");
129 MODULE_DESCRIPTION("Elan i2c-hid touchscreen driver");
130 MODULE_LICENSE("GPL");
131