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 
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 
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 
71 static int i2c_hid_of_elan_probe(struct i2c_client *client)
72 {
73 	struct i2c_hid_of_elan *ihid_elan;
74 
75 	ihid_elan = devm_kzalloc(&client->dev, sizeof(*ihid_elan), GFP_KERNEL);
76 	if (!ihid_elan)
77 		return -ENOMEM;
78 
79 	ihid_elan->ops.power_up = elan_i2c_hid_power_up;
80 	ihid_elan->ops.power_down = elan_i2c_hid_power_down;
81 
82 	/* Start out with reset asserted */
83 	ihid_elan->reset_gpio =
84 		devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
85 	if (IS_ERR(ihid_elan->reset_gpio))
86 		return PTR_ERR(ihid_elan->reset_gpio);
87 
88 	ihid_elan->vccio = devm_regulator_get(&client->dev, "vccio");
89 	if (IS_ERR(ihid_elan->vccio))
90 		return PTR_ERR(ihid_elan->vccio);
91 
92 	ihid_elan->vcc33 = devm_regulator_get(&client->dev, "vcc33");
93 	if (IS_ERR(ihid_elan->vcc33))
94 		return PTR_ERR(ihid_elan->vcc33);
95 
96 	ihid_elan->chip_data = device_get_match_data(&client->dev);
97 
98 	return i2c_hid_core_probe(client, &ihid_elan->ops,
99 				  ihid_elan->chip_data->hid_descriptor_address, 0);
100 }
101 
102 static const struct elan_i2c_hid_chip_data elan_ekth6915_chip_data = {
103 	.post_power_delay_ms = 1,
104 	.post_gpio_reset_delay_ms = 300,
105 	.hid_descriptor_address = 0x0001,
106 };
107 
108 static const struct of_device_id elan_i2c_hid_of_match[] = {
109 	{ .compatible = "elan,ekth6915", .data = &elan_ekth6915_chip_data },
110 	{ }
111 };
112 MODULE_DEVICE_TABLE(of, elan_i2c_hid_of_match);
113 
114 static struct i2c_driver elan_i2c_hid_ts_driver = {
115 	.driver = {
116 		.name	= "i2c_hid_of_elan",
117 		.pm	= &i2c_hid_core_pm,
118 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
119 		.of_match_table = of_match_ptr(elan_i2c_hid_of_match),
120 	},
121 	.probe		= i2c_hid_of_elan_probe,
122 	.remove		= i2c_hid_core_remove,
123 	.shutdown	= i2c_hid_core_shutdown,
124 };
125 module_i2c_driver(elan_i2c_hid_ts_driver);
126 
127 MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>");
128 MODULE_DESCRIPTION("Elan i2c-hid touchscreen driver");
129 MODULE_LICENSE("GPL");
130