1c1ed18c1SDouglas Anderson // SPDX-License-Identifier: GPL-2.0
2c1ed18c1SDouglas Anderson /*
3c1ed18c1SDouglas Anderson * Driver for Goodix touchscreens that use the i2c-hid protocol.
4c1ed18c1SDouglas Anderson *
5c1ed18c1SDouglas Anderson * Copyright 2020 Google LLC
6c1ed18c1SDouglas Anderson */
7c1ed18c1SDouglas Anderson
8c1ed18c1SDouglas Anderson #include <linux/delay.h>
9c1ed18c1SDouglas Anderson #include <linux/device.h>
10c1ed18c1SDouglas Anderson #include <linux/gpio/consumer.h>
11c1ed18c1SDouglas Anderson #include <linux/i2c.h>
12c1ed18c1SDouglas Anderson #include <linux/kernel.h>
13c1ed18c1SDouglas Anderson #include <linux/module.h>
14c1ed18c1SDouglas Anderson #include <linux/of.h>
15c1ed18c1SDouglas Anderson #include <linux/pm.h>
16c1ed18c1SDouglas Anderson #include <linux/regulator/consumer.h>
17c1ed18c1SDouglas Anderson
18c1ed18c1SDouglas Anderson #include "i2c-hid.h"
19c1ed18c1SDouglas Anderson
20c1ed18c1SDouglas Anderson struct goodix_i2c_hid_timing_data {
21c1ed18c1SDouglas Anderson unsigned int post_gpio_reset_delay_ms;
22c1ed18c1SDouglas Anderson unsigned int post_power_delay_ms;
23c1ed18c1SDouglas Anderson };
24c1ed18c1SDouglas Anderson
25c1ed18c1SDouglas Anderson struct i2c_hid_of_goodix {
26c1ed18c1SDouglas Anderson struct i2chid_ops ops;
27c1ed18c1SDouglas Anderson
28c1ed18c1SDouglas Anderson struct regulator *vdd;
29eb16f59eSDouglas Anderson struct regulator *vddio;
30c1ed18c1SDouglas Anderson struct gpio_desc *reset_gpio;
317607f12bSFei Shao bool no_reset_during_suspend;
32c1ed18c1SDouglas Anderson const struct goodix_i2c_hid_timing_data *timings;
33c1ed18c1SDouglas Anderson };
34c1ed18c1SDouglas Anderson
goodix_i2c_hid_power_up(struct i2chid_ops * ops)3518eeef46SDouglas Anderson static int goodix_i2c_hid_power_up(struct i2chid_ops *ops)
3618eeef46SDouglas Anderson {
3718eeef46SDouglas Anderson struct i2c_hid_of_goodix *ihid_goodix =
3818eeef46SDouglas Anderson container_of(ops, struct i2c_hid_of_goodix, ops);
39557e05faSDouglas Anderson int ret;
4018eeef46SDouglas Anderson
417607f12bSFei Shao /*
427607f12bSFei Shao * We assert reset GPIO here (instead of during power-down) to ensure
437607f12bSFei Shao * the device will have a clean state after powering up, just like the
447607f12bSFei Shao * normal scenarios will have.
457607f12bSFei Shao */
467607f12bSFei Shao if (ihid_goodix->no_reset_during_suspend)
477607f12bSFei Shao gpiod_set_value_cansleep(ihid_goodix->reset_gpio, 1);
487607f12bSFei Shao
49557e05faSDouglas Anderson ret = regulator_enable(ihid_goodix->vdd);
50557e05faSDouglas Anderson if (ret)
51557e05faSDouglas Anderson return ret;
52557e05faSDouglas Anderson
53eb16f59eSDouglas Anderson ret = regulator_enable(ihid_goodix->vddio);
54eb16f59eSDouglas Anderson if (ret)
55eb16f59eSDouglas Anderson return ret;
56eb16f59eSDouglas Anderson
57557e05faSDouglas Anderson if (ihid_goodix->timings->post_power_delay_ms)
58557e05faSDouglas Anderson msleep(ihid_goodix->timings->post_power_delay_ms);
59557e05faSDouglas Anderson
60557e05faSDouglas Anderson gpiod_set_value_cansleep(ihid_goodix->reset_gpio, 0);
61557e05faSDouglas Anderson if (ihid_goodix->timings->post_gpio_reset_delay_ms)
62557e05faSDouglas Anderson msleep(ihid_goodix->timings->post_gpio_reset_delay_ms);
63557e05faSDouglas Anderson
64557e05faSDouglas Anderson return 0;
65c1ed18c1SDouglas Anderson }
66c1ed18c1SDouglas Anderson
goodix_i2c_hid_power_down(struct i2chid_ops * ops)67c1ed18c1SDouglas Anderson static void goodix_i2c_hid_power_down(struct i2chid_ops *ops)
68c1ed18c1SDouglas Anderson {
69c1ed18c1SDouglas Anderson struct i2c_hid_of_goodix *ihid_goodix =
70c1ed18c1SDouglas Anderson container_of(ops, struct i2c_hid_of_goodix, ops);
71c1ed18c1SDouglas Anderson
727607f12bSFei Shao if (!ihid_goodix->no_reset_during_suspend)
7318eeef46SDouglas Anderson gpiod_set_value_cansleep(ihid_goodix->reset_gpio, 1);
747607f12bSFei Shao
75eb16f59eSDouglas Anderson regulator_disable(ihid_goodix->vddio);
76557e05faSDouglas Anderson regulator_disable(ihid_goodix->vdd);
7718eeef46SDouglas Anderson }
7818eeef46SDouglas Anderson
i2c_hid_of_goodix_probe(struct i2c_client * client)79baf34f3bSStephen Kitt static int i2c_hid_of_goodix_probe(struct i2c_client *client)
80c1ed18c1SDouglas Anderson {
81c1ed18c1SDouglas Anderson struct i2c_hid_of_goodix *ihid_goodix;
82557e05faSDouglas Anderson
83c1ed18c1SDouglas Anderson ihid_goodix = devm_kzalloc(&client->dev, sizeof(*ihid_goodix),
84c1ed18c1SDouglas Anderson GFP_KERNEL);
85c1ed18c1SDouglas Anderson if (!ihid_goodix)
86c1ed18c1SDouglas Anderson return -ENOMEM;
87c1ed18c1SDouglas Anderson
88c1ed18c1SDouglas Anderson ihid_goodix->ops.power_up = goodix_i2c_hid_power_up;
89c1ed18c1SDouglas Anderson ihid_goodix->ops.power_down = goodix_i2c_hid_power_down;
90c1ed18c1SDouglas Anderson
91c1ed18c1SDouglas Anderson /* Start out with reset asserted */
92c1ed18c1SDouglas Anderson ihid_goodix->reset_gpio =
93c1ed18c1SDouglas Anderson devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
94c1ed18c1SDouglas Anderson if (IS_ERR(ihid_goodix->reset_gpio))
95c1ed18c1SDouglas Anderson return PTR_ERR(ihid_goodix->reset_gpio);
96c1ed18c1SDouglas Anderson
97c1ed18c1SDouglas Anderson ihid_goodix->vdd = devm_regulator_get(&client->dev, "vdd");
98c1ed18c1SDouglas Anderson if (IS_ERR(ihid_goodix->vdd))
99c1ed18c1SDouglas Anderson return PTR_ERR(ihid_goodix->vdd);
100c1ed18c1SDouglas Anderson
101eb16f59eSDouglas Anderson ihid_goodix->vddio = devm_regulator_get(&client->dev, "mainboard-vddio");
102eb16f59eSDouglas Anderson if (IS_ERR(ihid_goodix->vddio))
103eb16f59eSDouglas Anderson return PTR_ERR(ihid_goodix->vddio);
104eb16f59eSDouglas Anderson
1057607f12bSFei Shao ihid_goodix->no_reset_during_suspend =
1067607f12bSFei Shao of_property_read_bool(client->dev.of_node, "goodix,no-reset-during-suspend");
1077607f12bSFei Shao
108c1ed18c1SDouglas Anderson ihid_goodix->timings = device_get_match_data(&client->dev);
109c1ed18c1SDouglas Anderson
110b60d3c80SAlistair Francis return i2c_hid_core_probe(client, &ihid_goodix->ops, 0x0001, 0);
111c1ed18c1SDouglas Anderson }
112c1ed18c1SDouglas Anderson
113c1ed18c1SDouglas Anderson static const struct goodix_i2c_hid_timing_data goodix_gt7375p_timing_data = {
114c1ed18c1SDouglas Anderson .post_power_delay_ms = 10,
115c1ed18c1SDouglas Anderson .post_gpio_reset_delay_ms = 180,
116c1ed18c1SDouglas Anderson };
117c1ed18c1SDouglas Anderson
118c1ed18c1SDouglas Anderson static const struct of_device_id goodix_i2c_hid_of_match[] = {
119c1ed18c1SDouglas Anderson { .compatible = "goodix,gt7375p", .data = &goodix_gt7375p_timing_data },
120c1ed18c1SDouglas Anderson { }
121c1ed18c1SDouglas Anderson };
122c1ed18c1SDouglas Anderson MODULE_DEVICE_TABLE(of, goodix_i2c_hid_of_match);
123c1ed18c1SDouglas Anderson
124c1ed18c1SDouglas Anderson static struct i2c_driver goodix_i2c_hid_ts_driver = {
125c1ed18c1SDouglas Anderson .driver = {
126c1ed18c1SDouglas Anderson .name = "i2c_hid_of_goodix",
127c1ed18c1SDouglas Anderson .pm = &i2c_hid_core_pm,
128c1ed18c1SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
129c1ed18c1SDouglas Anderson .of_match_table = of_match_ptr(goodix_i2c_hid_of_match),
130c1ed18c1SDouglas Anderson },
131*e4b88075SUwe Kleine-König .probe = i2c_hid_of_goodix_probe,
132c1ed18c1SDouglas Anderson .remove = i2c_hid_core_remove,
133c1ed18c1SDouglas Anderson .shutdown = i2c_hid_core_shutdown,
134c1ed18c1SDouglas Anderson };
135c1ed18c1SDouglas Anderson module_i2c_driver(goodix_i2c_hid_ts_driver);
136c1ed18c1SDouglas Anderson
137c1ed18c1SDouglas Anderson MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>");
138c1ed18c1SDouglas Anderson MODULE_DESCRIPTION("Goodix i2c-hid touchscreen driver");
139c1ed18c1SDouglas Anderson MODULE_LICENSE("GPL v2");
140