1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Goodix 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 goodix_i2c_hid_timing_data {
21 unsigned int post_gpio_reset_delay_ms;
22 unsigned int post_power_delay_ms;
25 struct i2c_hid_of_goodix {
26 struct i2chid_ops ops;
28 struct regulator *vdd;
29 struct notifier_block nb;
30 struct mutex regulator_mutex;
31 struct gpio_desc *reset_gpio;
32 const struct goodix_i2c_hid_timing_data *timings;
35 static void goodix_i2c_hid_deassert_reset(struct i2c_hid_of_goodix *ihid_goodix,
36 bool regulator_just_turned_on)
38 if (regulator_just_turned_on && ihid_goodix->timings->post_power_delay_ms)
39 msleep(ihid_goodix->timings->post_power_delay_ms);
41 gpiod_set_value_cansleep(ihid_goodix->reset_gpio, 0);
42 if (ihid_goodix->timings->post_gpio_reset_delay_ms)
43 msleep(ihid_goodix->timings->post_gpio_reset_delay_ms);
46 static int goodix_i2c_hid_power_up(struct i2chid_ops *ops)
48 struct i2c_hid_of_goodix *ihid_goodix =
49 container_of(ops, struct i2c_hid_of_goodix, ops);
51 return regulator_enable(ihid_goodix->vdd);
54 static void goodix_i2c_hid_power_down(struct i2chid_ops *ops)
56 struct i2c_hid_of_goodix *ihid_goodix =
57 container_of(ops, struct i2c_hid_of_goodix, ops);
59 regulator_disable(ihid_goodix->vdd);
62 static int ihid_goodix_vdd_notify(struct notifier_block *nb,
66 struct i2c_hid_of_goodix *ihid_goodix =
67 container_of(nb, struct i2c_hid_of_goodix, nb);
70 mutex_lock(&ihid_goodix->regulator_mutex);
73 case REGULATOR_EVENT_PRE_DISABLE:
74 gpiod_set_value_cansleep(ihid_goodix->reset_gpio, 1);
77 case REGULATOR_EVENT_ENABLE:
78 goodix_i2c_hid_deassert_reset(ihid_goodix, true);
81 case REGULATOR_EVENT_ABORT_DISABLE:
82 goodix_i2c_hid_deassert_reset(ihid_goodix, false);
90 mutex_unlock(&ihid_goodix->regulator_mutex);
95 static int i2c_hid_of_goodix_probe(struct i2c_client *client,
96 const struct i2c_device_id *id)
98 struct i2c_hid_of_goodix *ihid_goodix;
100 ihid_goodix = devm_kzalloc(&client->dev, sizeof(*ihid_goodix),
105 mutex_init(&ihid_goodix->regulator_mutex);
107 ihid_goodix->ops.power_up = goodix_i2c_hid_power_up;
108 ihid_goodix->ops.power_down = goodix_i2c_hid_power_down;
110 /* Start out with reset asserted */
111 ihid_goodix->reset_gpio =
112 devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
113 if (IS_ERR(ihid_goodix->reset_gpio))
114 return PTR_ERR(ihid_goodix->reset_gpio);
116 ihid_goodix->vdd = devm_regulator_get(&client->dev, "vdd");
117 if (IS_ERR(ihid_goodix->vdd))
118 return PTR_ERR(ihid_goodix->vdd);
120 ihid_goodix->timings = device_get_match_data(&client->dev);
123 * We need to control the "reset" line in lockstep with the regulator
124 * actually turning on an off instead of just when we make the request.
125 * This matters if the regulator is shared with another consumer.
126 * - If the regulator is off then we must assert reset. The reset
127 * line is active low and on some boards it could cause a current
129 * - If the regulator is on then we don't want reset asserted for very
130 * long. Holding the controller in reset apparently draws extra
133 mutex_lock(&ihid_goodix->regulator_mutex);
134 ihid_goodix->nb.notifier_call = ihid_goodix_vdd_notify;
135 ret = devm_regulator_register_notifier(ihid_goodix->vdd, &ihid_goodix->nb);
137 mutex_unlock(&ihid_goodix->regulator_mutex);
138 return dev_err_probe(&client->dev, ret,
139 "regulator notifier request failed\n");
143 * If someone else is holding the regulator on (or the regulator is
144 * an always-on one) we might never be told to deassert reset. Do it
145 * now. Here we'll assume that someone else might have _just
146 * barely_ turned the regulator on so we'll do the full
147 * "post_power_delay" just in case.
149 if (ihid_goodix->reset_gpio && regulator_is_enabled(ihid_goodix->vdd))
150 goodix_i2c_hid_deassert_reset(ihid_goodix, true);
151 mutex_unlock(&ihid_goodix->regulator_mutex);
153 return i2c_hid_core_probe(client, &ihid_goodix->ops, 0x0001, 0);
156 static const struct goodix_i2c_hid_timing_data goodix_gt7375p_timing_data = {
157 .post_power_delay_ms = 10,
158 .post_gpio_reset_delay_ms = 180,
161 static const struct of_device_id goodix_i2c_hid_of_match[] = {
162 { .compatible = "goodix,gt7375p", .data = &goodix_gt7375p_timing_data },
165 MODULE_DEVICE_TABLE(of, goodix_i2c_hid_of_match);
167 static struct i2c_driver goodix_i2c_hid_ts_driver = {
169 .name = "i2c_hid_of_goodix",
170 .pm = &i2c_hid_core_pm,
171 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
172 .of_match_table = of_match_ptr(goodix_i2c_hid_of_match),
174 .probe = i2c_hid_of_goodix_probe,
175 .remove = i2c_hid_core_remove,
176 .shutdown = i2c_hid_core_shutdown,
178 module_i2c_driver(goodix_i2c_hid_ts_driver);
181 MODULE_DESCRIPTION("Goodix i2c-hid touchscreen driver");
182 MODULE_LICENSE("GPL v2");