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