1 /*
2  * Copyright 2024 Google LLC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT pixart_pat912x
8 
9 #include <stdint.h>
10 #include <stdlib.h>
11 
12 #include <zephyr/device.h>
13 #include <zephyr/drivers/gpio.h>
14 #include <zephyr/drivers/i2c.h>
15 #include <zephyr/input/input.h>
16 #include <zephyr/input/input_pat912x.h>
17 #include <zephyr/kernel.h>
18 #include <zephyr/logging/log.h>
19 #include <zephyr/pm/device.h>
20 #include <zephyr/pm/device_runtime.h>
21 #include <zephyr/sys/byteorder.h>
22 #include <zephyr/sys/util.h>
23 
24 LOG_MODULE_REGISTER(input_pat912x, CONFIG_INPUT_LOG_LEVEL);
25 
26 #define PAT912X_PRODUCT_ID1	0x00
27 #define PAT912X_PRODUCT_ID2	0x01
28 #define PAT912X_MOTION_STATUS	0x02
29 #define PAT912X_DELTA_X_LO	0x03
30 #define PAT912X_DELTA_Y_LO	0x04
31 #define PAT912X_OPERATION_MODE	0x05
32 #define PAT912X_CONFIGURATION	0x06
33 #define PAT912X_WRITE_PROTECT	0x09
34 #define PAT912X_SLEEP1		0x0a
35 #define PAT912X_SLEEP2		0x0b
36 #define PAT912X_RES_X		0x0d
37 #define PAT912X_RES_Y		0x0e
38 #define PAT912X_DELTA_XY_HI	0x12
39 #define PAT912X_SHUTTER		0x14
40 #define PAT912X_FRAME_AVG	0x17
41 #define PAT912X_ORIENTATION	0x19
42 
43 #define PRODUCT_ID_PAT9125EL 0x3191
44 
45 #define CONFIGURATION_RESET 0x97
46 #define CONFIGURATION_CLEAR 0x17
47 #define CONFIGURATION_PD_ENH BIT(3)
48 #define WRITE_PROTECT_ENABLE 0x00
49 #define WRITE_PROTECT_DISABLE 0x5a
50 #define MOTION_STATUS_MOTION BIT(7)
51 #define RES_SCALING_FACTOR 5
52 #define RES_MAX (UINT8_MAX * RES_SCALING_FACTOR)
53 #define OPERATION_MODE_SLEEP_1_EN BIT(4)
54 #define OPERATION_MODE_SLEEP_12_EN (BIT(4) | BIT(3))
55 
56 #define PAT912X_DATA_SIZE_BITS 12
57 
58 #define RESET_DELAY_MS 2
59 
60 struct pat912x_config {
61 	struct i2c_dt_spec i2c;
62 	struct gpio_dt_spec motion_gpio;
63 	int32_t axis_x;
64 	int32_t axis_y;
65 	int16_t res_x_cpi;
66 	int16_t res_y_cpi;
67 	bool invert_x;
68 	bool invert_y;
69 	bool sleep1_enable;
70 	bool sleep2_enable;
71 };
72 
73 struct pat912x_data {
74 	const struct device *dev;
75 	struct k_work motion_work;
76 	struct gpio_callback motion_cb;
77 };
78 
pat912x_motion_work_handler(struct k_work * work)79 static void pat912x_motion_work_handler(struct k_work *work)
80 {
81 	struct pat912x_data *data = CONTAINER_OF(
82 			work, struct pat912x_data, motion_work);
83 	const struct device *dev = data->dev;
84 	const struct pat912x_config *cfg = dev->config;
85 	int32_t x, y;
86 	uint8_t val;
87 	uint8_t xy[2];
88 	int ret;
89 
90 	ret = i2c_reg_read_byte_dt(&cfg->i2c, PAT912X_MOTION_STATUS, &val);
91 	if (ret < 0) {
92 		return;
93 	}
94 
95 	if ((val & MOTION_STATUS_MOTION) == 0x00) {
96 		return;
97 	}
98 
99 	ret = i2c_burst_read_dt(&cfg->i2c, PAT912X_DELTA_X_LO, xy, sizeof(xy));
100 	if (ret < 0) {
101 		return;
102 	}
103 	x = xy[0];
104 	y = xy[1];
105 
106 	ret = i2c_reg_read_byte_dt(&cfg->i2c, PAT912X_DELTA_XY_HI, &val);
107 	if (ret < 0) {
108 		return;
109 	}
110 	y |= (val << 8) & 0xf00;
111 	x |= (val << 4) & 0xf00;
112 
113 	x = sign_extend(x, PAT912X_DATA_SIZE_BITS - 1);
114 	y = sign_extend(y, PAT912X_DATA_SIZE_BITS - 1);
115 
116 	if (cfg->invert_x) {
117 		x *= -1;
118 	}
119 	if (cfg->invert_y) {
120 		y *= -1;
121 	}
122 
123 	LOG_DBG("x=%4d y=%4d", x, y);
124 
125 	if (cfg->axis_x >= 0) {
126 		bool sync = cfg->axis_y < 0;
127 
128 		input_report_rel(data->dev, cfg->axis_x, x, sync, K_FOREVER);
129 	}
130 
131 	if (cfg->axis_y >= 0) {
132 		input_report_rel(data->dev, cfg->axis_y, y, true, K_FOREVER);
133 	}
134 
135 	/* Trigger one more scan in case more data is available. */
136 	k_work_submit(&data->motion_work);
137 }
138 
pat912x_motion_handler(const struct device * gpio_dev,struct gpio_callback * cb,uint32_t pins)139 static void pat912x_motion_handler(const struct device *gpio_dev,
140 				   struct gpio_callback *cb,
141 				   uint32_t pins)
142 {
143 	struct pat912x_data *data = CONTAINER_OF(
144 			cb, struct pat912x_data, motion_cb);
145 
146 	k_work_submit(&data->motion_work);
147 }
148 
pat912x_set_resolution(const struct device * dev,int16_t res_x_cpi,int16_t res_y_cpi)149 int pat912x_set_resolution(const struct device *dev,
150 			   int16_t res_x_cpi, int16_t res_y_cpi)
151 {
152 	const struct pat912x_config *cfg = dev->config;
153 	int ret;
154 
155 	if (res_x_cpi >= 0) {
156 		if (!IN_RANGE(res_x_cpi, 0, RES_MAX)) {
157 			LOG_ERR("res_x_cpi out of range: %d", res_x_cpi);
158 			return -EINVAL;
159 		}
160 
161 		ret = i2c_reg_write_byte_dt(&cfg->i2c, PAT912X_RES_X,
162 					    res_x_cpi / RES_SCALING_FACTOR);
163 		if (ret < 0) {
164 			return ret;
165 		}
166 	}
167 
168 	if (res_y_cpi >= 0) {
169 		if (!IN_RANGE(res_y_cpi, 0, RES_MAX)) {
170 			LOG_ERR("res_y_cpi out of range: %d", res_y_cpi);
171 			return -EINVAL;
172 		}
173 
174 		ret = i2c_reg_write_byte_dt(&cfg->i2c, PAT912X_RES_Y,
175 					    res_y_cpi / RES_SCALING_FACTOR);
176 		if (ret < 0) {
177 			return ret;
178 		}
179 	}
180 
181 	return 0;
182 }
183 
pat912x_configure(const struct device * dev)184 static int pat912x_configure(const struct device *dev)
185 {
186 	const struct pat912x_config *cfg = dev->config;
187 	uint8_t id[2];
188 	int ret;
189 
190 	ret = i2c_burst_read_dt(&cfg->i2c, PAT912X_PRODUCT_ID1, id, sizeof(id));
191 	if (ret < 0) {
192 		return ret;
193 	}
194 
195 	if (sys_get_be16(id) != PRODUCT_ID_PAT9125EL) {
196 		LOG_ERR("Invalid product id: %04x", sys_get_be16(id));
197 		return -ENOTSUP;
198 	}
199 
200 	/* Software reset */
201 
202 	i2c_reg_write_byte_dt(&cfg->i2c, PAT912X_CONFIGURATION, CONFIGURATION_RESET);
203 	/* no ret value check, the device NACKs */
204 
205 	k_sleep(K_MSEC(RESET_DELAY_MS));
206 
207 	ret = i2c_reg_write_byte_dt(&cfg->i2c, PAT912X_CONFIGURATION, CONFIGURATION_CLEAR);
208 	if (ret < 0) {
209 		return ret;
210 	}
211 
212 	ret = pat912x_set_resolution(dev, cfg->res_x_cpi, cfg->res_y_cpi);
213 	if (ret < 0) {
214 		return ret;
215 	}
216 
217 	if (cfg->sleep1_enable && cfg->sleep2_enable) {
218 		ret = i2c_reg_update_byte_dt(&cfg->i2c,
219 					     PAT912X_OPERATION_MODE,
220 					     OPERATION_MODE_SLEEP_12_EN,
221 					     OPERATION_MODE_SLEEP_12_EN);
222 		if (ret < 0) {
223 			return ret;
224 		}
225 	} else if (cfg->sleep1_enable) {
226 		ret = i2c_reg_update_byte_dt(&cfg->i2c,
227 					     PAT912X_OPERATION_MODE,
228 					     OPERATION_MODE_SLEEP_12_EN,
229 					     OPERATION_MODE_SLEEP_1_EN);
230 		if (ret < 0) {
231 			return ret;
232 		}
233 	}
234 
235 	return 0;
236 }
237 
pat912x_init(const struct device * dev)238 static int pat912x_init(const struct device *dev)
239 {
240 	const struct pat912x_config *cfg = dev->config;
241 	struct pat912x_data *data = dev->data;
242 	int ret;
243 
244 	if (!i2c_is_ready_dt(&cfg->i2c)) {
245 		LOG_ERR("%s is not ready", cfg->i2c.bus->name);
246 		return -ENODEV;
247 	}
248 
249 	data->dev = dev;
250 
251 	k_work_init(&data->motion_work, pat912x_motion_work_handler);
252 
253 	if (!gpio_is_ready_dt(&cfg->motion_gpio)) {
254 		LOG_ERR("%s is not ready", cfg->motion_gpio.port->name);
255 		return -ENODEV;
256 	}
257 
258 	ret = gpio_pin_configure_dt(&cfg->motion_gpio, GPIO_INPUT);
259 	if (ret != 0) {
260 		LOG_ERR("Motion pin configuration failed: %d", ret);
261 		return ret;
262 	}
263 
264 	ret = gpio_pin_interrupt_configure_dt(&cfg->motion_gpio,
265 					      GPIO_INT_EDGE_TO_ACTIVE);
266 	if (ret != 0) {
267 		LOG_ERR("Motion interrupt configuration failed: %d", ret);
268 		return ret;
269 	}
270 
271 	gpio_init_callback(&data->motion_cb, pat912x_motion_handler,
272 			   BIT(cfg->motion_gpio.pin));
273 
274 	ret = pat912x_configure(dev);
275 	if (ret != 0) {
276 		LOG_ERR("Device configuration failed: %d", ret);
277 		return ret;
278 	}
279 
280 	ret = gpio_add_callback_dt(&cfg->motion_gpio, &data->motion_cb);
281 	if (ret < 0) {
282 		LOG_ERR("Could not set motion callback: %d", ret);
283 		return ret;
284 	}
285 
286 	/* Trigger an initial read to clear any pending motion status.*/
287 	k_work_submit(&data->motion_work);
288 
289 	ret = pm_device_runtime_enable(dev);
290 	if (ret < 0) {
291 		LOG_ERR("Failed to enable runtime power management");
292 		return ret;
293 	}
294 
295 	return 0;
296 }
297 
298 #ifdef CONFIG_PM_DEVICE
pat912x_pm_action(const struct device * dev,enum pm_device_action action)299 static int pat912x_pm_action(const struct device *dev,
300 			     enum pm_device_action action)
301 {
302 	const struct pat912x_config *cfg = dev->config;
303 	uint8_t val;
304 	int ret;
305 
306 	switch (action) {
307 	case PM_DEVICE_ACTION_SUSPEND:
308 		val = CONFIGURATION_PD_ENH;
309 		break;
310 	case PM_DEVICE_ACTION_RESUME:
311 		val = 0;
312 		break;
313 	default:
314 		return -ENOTSUP;
315 	}
316 
317 	ret = i2c_reg_update_byte_dt(&cfg->i2c, PAT912X_CONFIGURATION,
318 				     CONFIGURATION_PD_ENH, val);
319 	if (ret < 0) {
320 		return ret;
321 	}
322 
323 	return 0;
324 }
325 #endif
326 
327 #define PAT912X_INIT(n)								\
328 	BUILD_ASSERT(IN_RANGE(DT_INST_PROP_OR(n, res_x_cpi, 0), 0, RES_MAX),	\
329 		     "invalid res-x-cpi");					\
330 	BUILD_ASSERT(IN_RANGE(DT_INST_PROP_OR(n, res_y_cpi, 0), 0, RES_MAX),	\
331 		     "invalid res-y-cpi");					\
332 	BUILD_ASSERT(DT_INST_PROP(n, sleep1_enable) ||				\
333 		     !DT_INST_PROP(n, sleep2_enable),				\
334 		     "invalid sleep configuration");				\
335 										\
336 	static const struct pat912x_config pat912x_cfg_##n = {			\
337 		.i2c = I2C_DT_SPEC_INST_GET(n),					\
338 		.motion_gpio = GPIO_DT_SPEC_INST_GET(n, motion_gpios),		\
339 		.axis_x = DT_INST_PROP_OR(n, zephyr_axis_x, -1),		\
340 		.axis_y = DT_INST_PROP_OR(n, zephyr_axis_y, -1),		\
341 		.res_x_cpi = DT_INST_PROP_OR(n, res_x_cpi, -1),			\
342 		.res_y_cpi = DT_INST_PROP_OR(n, res_y_cpi, -1),			\
343 		.invert_x = DT_INST_PROP(n, invert_x),				\
344 		.invert_y = DT_INST_PROP(n, invert_y),				\
345 		.sleep1_enable = DT_INST_PROP(n, sleep1_enable),		\
346 		.sleep2_enable = DT_INST_PROP(n, sleep2_enable),		\
347 	};									\
348 										\
349 	static struct pat912x_data pat912x_data_##n;				\
350 										\
351 	PM_DEVICE_DT_INST_DEFINE(n, pat912x_pm_action);				\
352 										\
353 	DEVICE_DT_INST_DEFINE(n, pat912x_init, PM_DEVICE_DT_INST_GET(n),	\
354 			      &pat912x_data_##n, &pat912x_cfg_##n,		\
355 			      POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY,		\
356 			      NULL);
357 
358 DT_INST_FOREACH_STATUS_OKAY(PAT912X_INIT)
359