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 regulator *vddio;
30 struct gpio_desc *reset_gpio;
31 bool no_reset_during_suspend;
32 const struct goodix_i2c_hid_timing_data *timings;
35 static int goodix_i2c_hid_power_up(struct i2chid_ops *ops)
37 struct i2c_hid_of_goodix *ihid_goodix =
38 container_of(ops, struct i2c_hid_of_goodix, ops);
42 * We assert reset GPIO here (instead of during power-down) to ensure
43 * the device will have a clean state after powering up, just like the
44 * normal scenarios will have.
46 if (ihid_goodix->no_reset_during_suspend)
47 gpiod_set_value_cansleep(ihid_goodix->reset_gpio, 1);
49 ret = regulator_enable(ihid_goodix->vdd);
53 ret = regulator_enable(ihid_goodix->vddio);
57 if (ihid_goodix->timings->post_power_delay_ms)
58 msleep(ihid_goodix->timings->post_power_delay_ms);
60 gpiod_set_value_cansleep(ihid_goodix->reset_gpio, 0);
61 if (ihid_goodix->timings->post_gpio_reset_delay_ms)
62 msleep(ihid_goodix->timings->post_gpio_reset_delay_ms);
67 static void goodix_i2c_hid_power_down(struct i2chid_ops *ops)
69 struct i2c_hid_of_goodix *ihid_goodix =
70 container_of(ops, struct i2c_hid_of_goodix, ops);
72 if (!ihid_goodix->no_reset_during_suspend)
73 gpiod_set_value_cansleep(ihid_goodix->reset_gpio, 1);
75 regulator_disable(ihid_goodix->vddio);
76 regulator_disable(ihid_goodix->vdd);
79 static int i2c_hid_of_goodix_probe(struct i2c_client *client)
81 struct i2c_hid_of_goodix *ihid_goodix;
83 ihid_goodix = devm_kzalloc(&client->dev, sizeof(*ihid_goodix),
88 ihid_goodix->ops.power_up = goodix_i2c_hid_power_up;
89 ihid_goodix->ops.power_down = goodix_i2c_hid_power_down;
91 /* Start out with reset asserted */
92 ihid_goodix->reset_gpio =
93 devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
94 if (IS_ERR(ihid_goodix->reset_gpio))
95 return PTR_ERR(ihid_goodix->reset_gpio);
97 ihid_goodix->vdd = devm_regulator_get(&client->dev, "vdd");
98 if (IS_ERR(ihid_goodix->vdd))
99 return PTR_ERR(ihid_goodix->vdd);
101 ihid_goodix->vddio = devm_regulator_get(&client->dev, "mainboard-vddio");
102 if (IS_ERR(ihid_goodix->vddio))
103 return PTR_ERR(ihid_goodix->vddio);
105 ihid_goodix->no_reset_during_suspend =
106 of_property_read_bool(client->dev.of_node, "goodix,no-reset-during-suspend");
108 ihid_goodix->timings = device_get_match_data(&client->dev);
110 return i2c_hid_core_probe(client, &ihid_goodix->ops, 0x0001, 0);
113 static const struct goodix_i2c_hid_timing_data goodix_gt7375p_timing_data = {
114 .post_power_delay_ms = 10,
115 .post_gpio_reset_delay_ms = 180,
118 static const struct of_device_id goodix_i2c_hid_of_match[] = {
119 { .compatible = "goodix,gt7375p", .data = &goodix_gt7375p_timing_data },
122 MODULE_DEVICE_TABLE(of, goodix_i2c_hid_of_match);
124 static struct i2c_driver goodix_i2c_hid_ts_driver = {
126 .name = "i2c_hid_of_goodix",
127 .pm = &i2c_hid_core_pm,
128 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
129 .of_match_table = of_match_ptr(goodix_i2c_hid_of_match),
131 .probe = i2c_hid_of_goodix_probe,
132 .remove = i2c_hid_core_remove,
133 .shutdown = i2c_hid_core_shutdown,
135 module_i2c_driver(goodix_i2c_hid_ts_driver);
137 MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>");
138 MODULE_DESCRIPTION("Goodix i2c-hid touchscreen driver");
139 MODULE_LICENSE("GPL v2");