1 /*
2  * Copyright (c) 2024 Nicolas Goualard <nicolas.goualard@sfr.fr>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT chipsemi_chsc6x
8 
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/input/input.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/drivers/gpio.h>
13 #include <zephyr/logging/log.h>
14 
15 struct chsc6x_config {
16 	struct i2c_dt_spec i2c;
17 	const struct gpio_dt_spec int_gpio;
18 };
19 
20 struct chsc6x_data {
21 	const struct device *dev;
22 	struct k_work work;
23 	struct gpio_callback int_gpio_cb;
24 };
25 
26 #define CHSC6X_READ_ADDR             0
27 #define CHSC6X_READ_LENGTH           5
28 #define CHSC6X_OUTPUT_POINTS_PRESSED 0
29 #define CHSC6X_OUTPUT_COL            2
30 #define CHSC6X_OUTPUT_ROW            4
31 
32 LOG_MODULE_REGISTER(chsc6x, CONFIG_INPUT_LOG_LEVEL);
33 
chsc6x_process(const struct device * dev)34 static int chsc6x_process(const struct device *dev)
35 {
36 	uint8_t output[CHSC6X_READ_LENGTH];
37 	uint8_t row, col;
38 	bool is_pressed;
39 	int ret;
40 
41 	const struct chsc6x_config *cfg = dev->config;
42 
43 	ret = i2c_burst_read_dt(&cfg->i2c, CHSC6X_READ_ADDR, output, CHSC6X_READ_LENGTH);
44 	if (ret < 0) {
45 		LOG_ERR("Could not read data: %i", ret);
46 		return -ENODATA;
47 	}
48 
49 	is_pressed = output[CHSC6X_OUTPUT_POINTS_PRESSED];
50 	col = output[CHSC6X_OUTPUT_COL];
51 	row = output[CHSC6X_OUTPUT_ROW];
52 
53 	if (is_pressed) {
54 		input_report_abs(dev, INPUT_ABS_X, col, false, K_FOREVER);
55 		input_report_abs(dev, INPUT_ABS_Y, row, false, K_FOREVER);
56 		input_report_key(dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER);
57 	} else {
58 		input_report_key(dev, INPUT_BTN_TOUCH, 0, true, K_FOREVER);
59 	}
60 
61 	return 0;
62 }
63 
chsc6x_work_handler(struct k_work * work)64 static void chsc6x_work_handler(struct k_work *work)
65 {
66 	struct chsc6x_data *data = CONTAINER_OF(work, struct chsc6x_data, work);
67 
68 	chsc6x_process(data->dev);
69 }
70 
chsc6x_isr_handler(const struct device * dev,struct gpio_callback * cb,uint32_t mask)71 static void chsc6x_isr_handler(const struct device *dev, struct gpio_callback *cb, uint32_t mask)
72 {
73 	struct chsc6x_data *data = CONTAINER_OF(cb, struct chsc6x_data, int_gpio_cb);
74 
75 	k_work_submit(&data->work);
76 }
77 
chsc6x_chip_init(const struct device * dev)78 static int chsc6x_chip_init(const struct device *dev)
79 {
80 	const struct chsc6x_config *cfg = dev->config;
81 
82 	if (!i2c_is_ready_dt(&cfg->i2c)) {
83 		LOG_ERR("I2C bus %s not ready", cfg->i2c.bus->name);
84 		return -ENODEV;
85 	}
86 
87 	return 0;
88 }
89 
chsc6x_init(const struct device * dev)90 static int chsc6x_init(const struct device *dev)
91 {
92 	struct chsc6x_data *data = dev->data;
93 	int ret;
94 
95 	data->dev = dev;
96 
97 	k_work_init(&data->work, chsc6x_work_handler);
98 
99 	const struct chsc6x_config *config = dev->config;
100 
101 	if (!gpio_is_ready_dt(&config->int_gpio)) {
102 		LOG_ERR("GPIO port %s not ready", config->int_gpio.port->name);
103 		return -ENODEV;
104 	}
105 
106 	ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
107 	if (ret < 0) {
108 		LOG_ERR("Could not configure interrupt GPIO pin: %d", ret);
109 		return ret;
110 	}
111 
112 	ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
113 	if (ret < 0) {
114 		LOG_ERR("Could not configure interrupt GPIO interrupt: %d", ret);
115 		return ret;
116 	}
117 
118 	gpio_init_callback(&data->int_gpio_cb, chsc6x_isr_handler, BIT(config->int_gpio.pin));
119 
120 	ret = gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb);
121 	if (ret < 0) {
122 		LOG_ERR("Could not set gpio callback: %d", ret);
123 		return ret;
124 	}
125 
126 	return chsc6x_chip_init(dev);
127 };
128 
129 #define CHSC6X_DEFINE(index)                                                                       \
130 	static const struct chsc6x_config chsc6x_config_##index = {                                \
131 		.i2c = I2C_DT_SPEC_INST_GET(index),                                                \
132 		.int_gpio = GPIO_DT_SPEC_INST_GET(index, irq_gpios),                               \
133 	};                                                                                         \
134 	static struct chsc6x_data chsc6x_data_##index;                                             \
135 	DEVICE_DT_INST_DEFINE(index, chsc6x_init, NULL, &chsc6x_data_##index,                      \
136 			      &chsc6x_config_##index, POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY,     \
137 			      NULL);
138 
139 DT_INST_FOREACH_STATUS_OKAY(CHSC6X_DEFINE)
140