1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Driver for Goodix 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
20struct goodix_i2c_hid_timing_data {
21 unsigned int post_gpio_reset_delay_ms;
22 unsigned int post_power_delay_ms;
23};
24
25struct i2c_hid_of_goodix {
26 struct i2chid_ops ops;
27
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;
33};
34
35static int goodix_i2c_hid_power_up(struct i2chid_ops *ops)
36{
37 struct i2c_hid_of_goodix *ihid_goodix =
38 container_of(ops, struct i2c_hid_of_goodix, ops);
39 int ret;
40
41 /*
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.
45 */
46 if (ihid_goodix->no_reset_during_suspend)
47 gpiod_set_value_cansleep(desc: ihid_goodix->reset_gpio, value: 1);
48
49 ret = regulator_enable(regulator: ihid_goodix->vdd);
50 if (ret)
51 return ret;
52
53 ret = regulator_enable(regulator: ihid_goodix->vddio);
54 if (ret)
55 return ret;
56
57 if (ihid_goodix->timings->post_power_delay_ms)
58 msleep(msecs: ihid_goodix->timings->post_power_delay_ms);
59
60 gpiod_set_value_cansleep(desc: ihid_goodix->reset_gpio, value: 0);
61 if (ihid_goodix->timings->post_gpio_reset_delay_ms)
62 msleep(msecs: ihid_goodix->timings->post_gpio_reset_delay_ms);
63
64 return 0;
65}
66
67static void goodix_i2c_hid_power_down(struct i2chid_ops *ops)
68{
69 struct i2c_hid_of_goodix *ihid_goodix =
70 container_of(ops, struct i2c_hid_of_goodix, ops);
71
72 if (!ihid_goodix->no_reset_during_suspend)
73 gpiod_set_value_cansleep(desc: ihid_goodix->reset_gpio, value: 1);
74
75 regulator_disable(regulator: ihid_goodix->vddio);
76 regulator_disable(regulator: ihid_goodix->vdd);
77}
78
79static int i2c_hid_of_goodix_probe(struct i2c_client *client)
80{
81 struct i2c_hid_of_goodix *ihid_goodix;
82
83 ihid_goodix = devm_kzalloc(dev: &client->dev, size: sizeof(*ihid_goodix),
84 GFP_KERNEL);
85 if (!ihid_goodix)
86 return -ENOMEM;
87
88 ihid_goodix->ops.power_up = goodix_i2c_hid_power_up;
89 ihid_goodix->ops.power_down = goodix_i2c_hid_power_down;
90
91 /* Start out with reset asserted */
92 ihid_goodix->reset_gpio =
93 devm_gpiod_get_optional(dev: &client->dev, con_id: "reset", flags: GPIOD_OUT_HIGH);
94 if (IS_ERR(ptr: ihid_goodix->reset_gpio))
95 return PTR_ERR(ptr: ihid_goodix->reset_gpio);
96
97 ihid_goodix->vdd = devm_regulator_get(dev: &client->dev, id: "vdd");
98 if (IS_ERR(ptr: ihid_goodix->vdd))
99 return PTR_ERR(ptr: ihid_goodix->vdd);
100
101 ihid_goodix->vddio = devm_regulator_get(dev: &client->dev, id: "mainboard-vddio");
102 if (IS_ERR(ptr: ihid_goodix->vddio))
103 return PTR_ERR(ptr: ihid_goodix->vddio);
104
105 ihid_goodix->no_reset_during_suspend =
106 of_property_read_bool(np: client->dev.of_node, propname: "goodix,no-reset-during-suspend");
107
108 ihid_goodix->timings = device_get_match_data(dev: &client->dev);
109
110 return i2c_hid_core_probe(client, ops: &ihid_goodix->ops, hid_descriptor_address: 0x0001, quirks: 0);
111}
112
113static const struct goodix_i2c_hid_timing_data goodix_gt7375p_timing_data = {
114 .post_power_delay_ms = 10,
115 .post_gpio_reset_delay_ms = 180,
116};
117
118static const struct of_device_id goodix_i2c_hid_of_match[] = {
119 { .compatible = "goodix,gt7375p", .data = &goodix_gt7375p_timing_data },
120 { }
121};
122MODULE_DEVICE_TABLE(of, goodix_i2c_hid_of_match);
123
124static struct i2c_driver goodix_i2c_hid_ts_driver = {
125 .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),
130 },
131 .probe = i2c_hid_of_goodix_probe,
132 .remove = i2c_hid_core_remove,
133 .shutdown = i2c_hid_core_shutdown,
134};
135module_i2c_driver(goodix_i2c_hid_ts_driver);
136
137MODULE_AUTHOR("Douglas Anderson <dianders@chromium.org>");
138MODULE_DESCRIPTION("Goodix i2c-hid touchscreen driver");
139MODULE_LICENSE("GPL v2");
140

source code of linux/drivers/hid/i2c-hid/i2c-hid-of-goodix.c