1 /*
2 * Copyright (c) 2025 Basalte bv
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT chipsemi_chsc5x
8
9 #include <zephyr/drivers/gpio.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/input/input.h>
12 #include <zephyr/input/input_touch.h>
13 #include <zephyr/logging/log.h>
14 #include <zephyr/pm/device.h>
15
16 LOG_MODULE_REGISTER(chsc5x, CONFIG_INPUT_LOG_LEVEL);
17
18 struct chsc5x_config {
19 struct input_touchscreen_common_config common;
20 struct i2c_dt_spec i2c;
21 const struct gpio_dt_spec int_gpio;
22 const struct gpio_dt_spec reset_gpio;
23 };
24
25 struct chsc5x_data {
26 const struct device *dev;
27 struct k_work work;
28 struct gpio_callback int_gpio_cb;
29 };
30
31 INPUT_TOUCH_STRUCT_CHECK(struct chsc5x_config);
32
33 enum {
34 CHSC5X_IC_TYPE_CHSC5472 = 0x00,
35 CHSC5X_IC_TYPE_CHSC5448 = 0x01,
36 CHSC5X_IC_TYPE_CHSC5448A = 0x02,
37 CHSC5X_IC_TYPE_CHSC5460 = 0x03,
38 CHSC5X_IC_TYPE_CHSC5468 = 0x04,
39 CHSC5X_IC_TYPE_CHSC5432 = 0x05,
40 CHSC5X_IC_TYPE_CHSC5816 = 0x10,
41 CHSC5X_IC_TYPE_CHSC1716 = 0x11,
42 };
43
44 #define CHSC5X_BASE_ADDR1 0x20
45 #define CHSC5X_BASE_ADDR2 0x00
46 #define CHSC5X_BASE_ADDR3 0x00
47 #define CHSC5X_ADDRESS_MODE 0x00
48 #define CHSC5X_ADDRESS_IC_TYPE 0x80
49 #define CHSC5X_ADDRESS_TOUCH_DATA 0x2C
50 #define CHSC5X_SIZE_TOUCH_DATA 8
51
52 #define CHSC5X_OFFSET_EVENT_TYPE 0x00
53 #define CHSC5X_OFFSET_FINGER_NUMBER 0x01
54 #define CHSC5X_OFFSET_X_COORDINATE 0x02
55 #define CHSC5X_OFFSET_Y_COORDINATE 0x03
56 #define CHSC5X_OFFSET_PRESSURE 0x04
57 #define CHSC5X_OFFSET_XY_COORDINATE 0x05
58 #define CHSC5X_OFFSET_TOUCH_EVENT 0x06
59
chsc5x_work_handler(struct k_work * work)60 static void chsc5x_work_handler(struct k_work *work)
61 {
62 struct chsc5x_data *data = CONTAINER_OF(work, struct chsc5x_data, work);
63 const struct device *dev = data->dev;
64 const struct chsc5x_config *cfg = dev->config;
65 uint16_t row, col;
66 bool is_pressed;
67 int ret;
68 const uint8_t write_buffer[] = {
69 CHSC5X_BASE_ADDR1,
70 CHSC5X_BASE_ADDR2,
71 CHSC5X_BASE_ADDR3,
72 CHSC5X_ADDRESS_TOUCH_DATA,
73 };
74 uint8_t read_buffer[CHSC5X_SIZE_TOUCH_DATA];
75
76 ret = i2c_write_read_dt(&cfg->i2c, write_buffer, sizeof(write_buffer), read_buffer,
77 sizeof(read_buffer));
78 if (ret < 0) {
79 LOG_ERR("Could not read data: %i", ret);
80 return;
81 }
82
83 is_pressed = (read_buffer[CHSC5X_OFFSET_TOUCH_EVENT] & 0x40) == 0U;
84 col = read_buffer[CHSC5X_OFFSET_X_COORDINATE] +
85 ((read_buffer[CHSC5X_OFFSET_XY_COORDINATE] & 0x0f) << 8);
86 row = read_buffer[CHSC5X_OFFSET_Y_COORDINATE] +
87 ((read_buffer[CHSC5X_OFFSET_XY_COORDINATE] & 0xf0) << 4);
88
89 if (is_pressed) {
90 input_touchscreen_report_pos(dev, col, row, K_FOREVER);
91 }
92
93 input_report_key(dev, INPUT_BTN_TOUCH, is_pressed, true, K_FOREVER);
94 }
95
chsc5x_isr_handler(const struct device * dev,struct gpio_callback * cb,uint32_t mask)96 static void chsc5x_isr_handler(const struct device *dev, struct gpio_callback *cb, uint32_t mask)
97 {
98 struct chsc5x_data *data = CONTAINER_OF(cb, struct chsc5x_data, int_gpio_cb);
99
100 k_work_submit(&data->work);
101 }
102
103 #if defined(CONFIG_INPUT_CHSC5X_VERIFY_IC_TYPE)
chsc5x_verify_ic(const struct device * dev)104 static int chsc5x_verify_ic(const struct device *dev)
105 {
106 const struct chsc5x_config *cfg = dev->config;
107 int ret;
108 const uint8_t write_buffer[] = {
109 CHSC5X_BASE_ADDR1,
110 CHSC5X_BASE_ADDR2,
111 CHSC5X_BASE_ADDR3,
112 CHSC5X_ADDRESS_IC_TYPE,
113 };
114 uint8_t read_buffer[4];
115
116 if (!i2c_is_ready_dt(&cfg->i2c)) {
117 LOG_ERR("I2C bus %s not ready", cfg->i2c.bus->name);
118 return -ENODEV;
119 }
120
121 ret = i2c_write_read_dt(&cfg->i2c, write_buffer, sizeof(write_buffer), read_buffer,
122 sizeof(read_buffer));
123 if (ret < 0) {
124 LOG_ERR("Could not read data: %i", ret);
125 return ret;
126 }
127
128 switch (read_buffer[0]) {
129 case CHSC5X_IC_TYPE_CHSC5472:
130 case CHSC5X_IC_TYPE_CHSC5448:
131 case CHSC5X_IC_TYPE_CHSC5448A:
132 case CHSC5X_IC_TYPE_CHSC5460:
133 case CHSC5X_IC_TYPE_CHSC5468:
134 case CHSC5X_IC_TYPE_CHSC5432:
135 case CHSC5X_IC_TYPE_CHSC5816:
136 case CHSC5X_IC_TYPE_CHSC1716:
137 break;
138 default:
139 LOG_ERR("CHSC5X wrong ic type: returned 0x%02x", read_buffer[0]);
140 return -ENODEV;
141 }
142
143 return 0;
144 }
145 #endif /* CONFIG_INPUT_CHSC5X_VERIFY_IC_TYPE */
146
chsc5x_reset(const struct device * dev)147 static int chsc5x_reset(const struct device *dev)
148 {
149 const struct chsc5x_config *config = dev->config;
150 int ret;
151
152 if (config->reset_gpio.port != NULL) {
153 if (!gpio_is_ready_dt(&config->reset_gpio)) {
154 LOG_ERR("GPIO port %s not ready", config->reset_gpio.port->name);
155 return -ENODEV;
156 }
157
158 ret = gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT_ACTIVE);
159 if (ret < 0) {
160 LOG_ERR("Could not configure reset GPIO (%d)", ret);
161 return ret;
162 }
163
164 k_usleep(500);
165
166 ret = gpio_pin_set_dt(&config->reset_gpio, 0);
167 if (ret < 0) {
168 LOG_ERR("Could not pull reset low (%d)", ret);
169 return ret;
170 }
171
172 k_msleep(1);
173 }
174
175 return 0;
176 }
177
178 #ifdef CONFIG_PM_DEVICE
chsc5x_pm_action(const struct device * dev,enum pm_device_action action)179 static int chsc5x_pm_action(const struct device *dev, enum pm_device_action action)
180 {
181 const struct chsc5x_config *config = dev->config;
182 int ret;
183
184 if (config->reset_gpio.port == NULL) {
185 return -ENOTSUP;
186 }
187
188 switch (action) {
189 case PM_DEVICE_ACTION_RESUME:
190 ret = chsc5x_reset(dev);
191 break;
192
193 case PM_DEVICE_ACTION_SUSPEND: {
194 const uint8_t write_buffer[] = {
195 CHSC5X_BASE_ADDR1,
196 CHSC5X_BASE_ADDR2,
197 CHSC5X_BASE_ADDR3,
198 CHSC5X_ADDRESS_MODE,
199 /* Fixed sequence with checksum */
200 0xF7, 0x16, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00,
201 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE9,
202 };
203
204 ret = i2c_write_dt(&config->i2c, write_buffer, sizeof(write_buffer));
205 break;
206 }
207
208 default:
209 ret = -ENOTSUP;
210 }
211
212 return ret;
213 }
214 #endif /* CONFIG_PM_DEVICE */
215
chsc5x_init(const struct device * dev)216 static int chsc5x_init(const struct device *dev)
217 {
218 const struct chsc5x_config *config = dev->config;
219 struct chsc5x_data *data = dev->data;
220 int ret;
221
222 data->dev = dev;
223
224 k_work_init(&data->work, chsc5x_work_handler);
225
226 ret = chsc5x_reset(dev);
227 if (ret < 0) {
228 LOG_ERR("Failed to reset (%d)", ret);
229 return ret;
230 }
231
232 if (!gpio_is_ready_dt(&config->int_gpio)) {
233 LOG_ERR("GPIO port %s not ready", config->int_gpio.port->name);
234 return -ENODEV;
235 }
236
237 ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
238 if (ret < 0) {
239 LOG_ERR("Could not configure interrupt GPIO pin: %d", ret);
240 return ret;
241 }
242
243 ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
244 if (ret < 0) {
245 LOG_ERR("Could not configure interrupt GPIO interrupt: %d", ret);
246 return ret;
247 }
248
249 gpio_init_callback(&data->int_gpio_cb, chsc5x_isr_handler, BIT(config->int_gpio.pin));
250
251 ret = gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb);
252 if (ret < 0) {
253 LOG_ERR("Could not set gpio callback: %d", ret);
254 return ret;
255 }
256
257 #if defined(CONFIG_INPUT_CHSC5X_VERIFY_IC_TYPE)
258 /* It takes about 94ms until chip is ready after reset. */
259 k_msleep(100);
260
261 ret = chsc5x_verify_ic(dev);
262 if (ret < 0) {
263 LOG_ERR("Failed to verify ic: %d", ret);
264 return ret;
265 }
266 #endif /* CONFIG_INPUT_CHSC5X_VERIFY_IC_TYPE */
267 return ret;
268 };
269
270 #define CHSC5X_DEFINE(index) \
271 PM_DEVICE_DT_INST_DEFINE(inst, chsc5x_pm_action); \
272 static const struct chsc5x_config chsc5x_config_##index = { \
273 .common = INPUT_TOUCH_DT_INST_COMMON_CONFIG_INIT(index), \
274 .i2c = I2C_DT_SPEC_INST_GET(index), \
275 .int_gpio = GPIO_DT_SPEC_INST_GET(index, int_gpios), \
276 .reset_gpio = GPIO_DT_SPEC_INST_GET_OR(index, reset_gpios, {0}), \
277 }; \
278 static struct chsc5x_data chsc5x_data_##index; \
279 DEVICE_DT_INST_DEFINE(index, chsc5x_init, PM_DEVICE_DT_INST_GET(inst), \
280 &chsc5x_data_##index, &chsc5x_config_##index, POST_KERNEL, \
281 CONFIG_INPUT_INIT_PRIORITY, NULL);
282
283 DT_INST_FOREACH_STATUS_OKAY(CHSC5X_DEFINE)
284