1 /*
2  * Copyright (c) 2020,2023 NXP
3  * Copyright (c) 2020 Mark Olsson <mark@markolsson.se>
4  * Copyright (c) 2020 Teslabs Engineering S.L.
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define DT_DRV_COMPAT focaltech_ft5336
10 
11 #include <zephyr/drivers/gpio.h>
12 #include <zephyr/drivers/i2c.h>
13 #include <zephyr/input/input.h>
14 #include <zephyr/pm/device.h>
15 #include <zephyr/pm/device_runtime.h>
16 #include <zephyr/sys/util.h>
17 
18 #include <zephyr/logging/log.h>
19 LOG_MODULE_REGISTER(ft5336, CONFIG_INPUT_LOG_LEVEL);
20 
21 /* FT5336 used registers */
22 #define REG_TD_STATUS		0x02U
23 #define REG_P1_XH		0x03U
24 #define REG_G_PMODE		0xA5U
25 
26 /* REG_TD_STATUS: Touch points. */
27 #define TOUCH_POINTS_POS	0U
28 #define TOUCH_POINTS_MSK	0x0FU
29 
30 /* REG_Pn_XH: Events. */
31 #define EVENT_POS		6U
32 #define EVENT_MSK		0x03U
33 
34 #define EVENT_PRESS_DOWN	0x00U
35 #define EVENT_LIFT_UP		0x01U
36 #define EVENT_CONTACT		0x02U
37 #define EVENT_NONE		0x03U
38 
39 /* REG_Pn_YH: Touch ID */
40 #define TOUCH_ID_POS		4U
41 #define TOUCH_ID_MSK		0x0FU
42 
43 #define TOUCH_ID_INVALID	0x0FU
44 
45 /* REG_Pn_XH and REG_Pn_YH: Position */
46 #define POSITION_H_MSK		0x0FU
47 
48 /* REG_G_PMODE: Power Consume Mode */
49 #define PMOD_HIBERNATE		0x03U
50 
51 /** FT5336 configuration (DT). */
52 struct ft5336_config {
53 	/** I2C bus. */
54 	struct i2c_dt_spec bus;
55 	struct gpio_dt_spec reset_gpio;
56 #ifdef CONFIG_INPUT_FT5336_INTERRUPT
57 	/** Interrupt GPIO information. */
58 	struct gpio_dt_spec int_gpio;
59 #endif
60 };
61 
62 /** FT5336 data. */
63 struct ft5336_data {
64 	/** Device pointer. */
65 	const struct device *dev;
66 	/** Work queue (for deferred read). */
67 	struct k_work work;
68 #ifdef CONFIG_INPUT_FT5336_INTERRUPT
69 	/** Interrupt GPIO callback. */
70 	struct gpio_callback int_gpio_cb;
71 #else
72 	/** Timer (polling mode). */
73 	struct k_timer timer;
74 #endif
75 	/** Last pressed state. */
76 	bool pressed_old;
77 };
78 
ft5336_process(const struct device * dev)79 static int ft5336_process(const struct device *dev)
80 {
81 	const struct ft5336_config *config = dev->config;
82 	struct ft5336_data *data = dev->data;
83 
84 	int r;
85 	uint8_t points;
86 	uint8_t coords[4U];
87 	uint16_t row, col;
88 	bool pressed;
89 
90 	/* obtain number of touch points */
91 	r = i2c_reg_read_byte_dt(&config->bus, REG_TD_STATUS, &points);
92 	if (r < 0) {
93 		return r;
94 	}
95 
96 	points = FIELD_GET(TOUCH_POINTS_MSK, points);
97 	if (points != 0) {
98 		/* Any number of touches still counts as one touch. All touch
99 		 * points except the first are ignored. Obtain first point
100 		 * X, Y coordinates from:
101 		 * REG_P1_XH, REG_P1_XL, REG_P1_YH, REG_P1_YL.
102 		 * We ignore the Event Flag because Zephyr only cares about
103 		 * pressed / not pressed and not press down / lift up
104 		 */
105 		r = i2c_burst_read_dt(&config->bus, REG_P1_XH, coords, sizeof(coords));
106 		if (r < 0) {
107 			return r;
108 		}
109 
110 		row = ((coords[0] & POSITION_H_MSK) << 8U) | coords[1];
111 		col = ((coords[2] & POSITION_H_MSK) << 8U) | coords[3];
112 
113 		uint8_t touch_id = FIELD_GET(TOUCH_ID_MSK, coords[2]);
114 
115 		if (touch_id != TOUCH_ID_INVALID) {
116 			pressed = true;
117 			LOG_DBG("points: %d, touch_id: %d, row: %d, col: %d",
118 				 points, touch_id, row, col);
119 		} else {
120 			pressed = false;
121 			LOG_WRN("bad TOUCH_ID: row: %d, col: %d", row, col);
122 		}
123 	} else  {
124 		/* no touch = no press */
125 		pressed = false;
126 	}
127 
128 	if (pressed) {
129 		input_report_abs(dev, INPUT_ABS_X, col, false, K_FOREVER);
130 		input_report_abs(dev, INPUT_ABS_Y, row, false, K_FOREVER);
131 		input_report_key(dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER);
132 	} else if (data->pressed_old && !pressed) {
133 		input_report_key(dev, INPUT_BTN_TOUCH, 0, true, K_FOREVER);
134 	}
135 	data->pressed_old = pressed;
136 
137 	return 0;
138 }
139 
ft5336_work_handler(struct k_work * work)140 static void ft5336_work_handler(struct k_work *work)
141 {
142 	struct ft5336_data *data = CONTAINER_OF(work, struct ft5336_data, work);
143 
144 	ft5336_process(data->dev);
145 }
146 
147 #ifdef CONFIG_INPUT_FT5336_INTERRUPT
ft5336_isr_handler(const struct device * dev,struct gpio_callback * cb,uint32_t pins)148 static void ft5336_isr_handler(const struct device *dev,
149 			       struct gpio_callback *cb, uint32_t pins)
150 {
151 	struct ft5336_data *data = CONTAINER_OF(cb, struct ft5336_data, int_gpio_cb);
152 
153 	k_work_submit(&data->work);
154 }
155 #else
ft5336_timer_handler(struct k_timer * timer)156 static void ft5336_timer_handler(struct k_timer *timer)
157 {
158 	struct ft5336_data *data = CONTAINER_OF(timer, struct ft5336_data, timer);
159 
160 	k_work_submit(&data->work);
161 }
162 #endif
163 
ft5336_init(const struct device * dev)164 static int ft5336_init(const struct device *dev)
165 {
166 	const struct ft5336_config *config = dev->config;
167 	struct ft5336_data *data = dev->data;
168 	int r;
169 
170 	if (!device_is_ready(config->bus.bus)) {
171 		LOG_ERR("I2C controller device not ready");
172 		return -ENODEV;
173 	}
174 
175 	data->dev = dev;
176 
177 	k_work_init(&data->work, ft5336_work_handler);
178 
179 	if (config->reset_gpio.port != NULL) {
180 		/* Enable reset GPIO and assert reset */
181 		r = gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT_ACTIVE);
182 		if (r < 0) {
183 			LOG_ERR("Could not enable reset GPIO");
184 			return r;
185 		}
186 		/*
187 		 * Datasheet requires reset be held low 1 ms, or
188 		 * 1 ms + 100us if powering on controller. Hold low for
189 		 * 5 ms to be safe.
190 		 */
191 		k_sleep(K_MSEC(5));
192 		/* Pull reset pin high to complete reset sequence */
193 		r = gpio_pin_set_dt(&config->reset_gpio, 0);
194 		if (r < 0) {
195 			return r;
196 		}
197 	}
198 
199 #ifdef CONFIG_INPUT_FT5336_INTERRUPT
200 	if (!gpio_is_ready_dt(&config->int_gpio)) {
201 		LOG_ERR("Interrupt GPIO controller device not ready");
202 		return -ENODEV;
203 	}
204 
205 	r = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
206 	if (r < 0) {
207 		LOG_ERR("Could not configure interrupt GPIO pin");
208 		return r;
209 	}
210 
211 	r = gpio_pin_interrupt_configure_dt(&config->int_gpio,
212 					    GPIO_INT_EDGE_TO_ACTIVE);
213 	if (r < 0) {
214 		LOG_ERR("Could not configure interrupt GPIO interrupt.");
215 		return r;
216 	}
217 
218 	gpio_init_callback(&data->int_gpio_cb, ft5336_isr_handler,
219 			   BIT(config->int_gpio.pin));
220 	r = gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb);
221 	if (r < 0) {
222 		LOG_ERR("Could not set gpio callback");
223 		return r;
224 	}
225 #else
226 	k_timer_init(&data->timer, ft5336_timer_handler, NULL);
227 	k_timer_start(&data->timer, K_MSEC(CONFIG_INPUT_FT5336_PERIOD),
228 		      K_MSEC(CONFIG_INPUT_FT5336_PERIOD));
229 #endif
230 
231 	r = pm_device_runtime_enable(dev);
232 	if (r < 0 && r != -ENOTSUP) {
233 		LOG_ERR("Failed to enable runtime power management");
234 		return r;
235 	}
236 
237 	return 0;
238 }
239 
240 #ifdef CONFIG_PM_DEVICE
ft5336_pm_action(const struct device * dev,enum pm_device_action action)241 static int ft5336_pm_action(const struct device *dev,
242 			    enum pm_device_action action)
243 {
244 	const struct ft5336_config *config = dev->config;
245 #ifndef CONFIG_INPUT_FT5336_INTERRUPT
246 	struct ft5336_data *data = dev->data;
247 #endif
248 	int ret;
249 
250 	if (config->reset_gpio.port == NULL) {
251 		return -ENOTSUP;
252 	}
253 
254 	switch (action) {
255 	case PM_DEVICE_ACTION_SUSPEND:
256 		ret = i2c_reg_write_byte_dt(&config->bus,
257 					    REG_G_PMODE, PMOD_HIBERNATE);
258 		if (ret < 0) {
259 			return ret;
260 		}
261 
262 #ifndef CONFIG_INPUT_FT5336_INTERRUPT
263 		k_timer_stop(&data->timer);
264 #endif
265 		break;
266 	case PM_DEVICE_ACTION_RESUME:
267 		ret = gpio_pin_set_dt(&config->reset_gpio, 1);
268 		if (ret < 0) {
269 			return ret;
270 		}
271 
272 		k_sleep(K_MSEC(5));
273 
274 		ret = gpio_pin_set_dt(&config->reset_gpio, 0);
275 		if (ret < 0) {
276 			return ret;
277 		}
278 
279 #ifndef CONFIG_INPUT_FT5336_INTERRUPT
280 		k_timer_start(&data->timer,
281 			      K_MSEC(CONFIG_INPUT_FT5336_PERIOD),
282 			      K_MSEC(CONFIG_INPUT_FT5336_PERIOD));
283 #endif
284 		break;
285 	default:
286 		return -ENOTSUP;
287 	}
288 
289 	return 0;
290 }
291 #endif
292 
293 #define FT5336_INIT(index)								\
294 	PM_DEVICE_DT_INST_DEFINE(n, ft5336_pm_action);					\
295 	static const struct ft5336_config ft5336_config_##index = {			\
296 		.bus = I2C_DT_SPEC_INST_GET(index),					\
297 		.reset_gpio = GPIO_DT_SPEC_INST_GET_OR(index, reset_gpios, {0}),	\
298 		IF_ENABLED(CONFIG_INPUT_FT5336_INTERRUPT,				\
299 		(.int_gpio = GPIO_DT_SPEC_INST_GET(index, int_gpios),))			\
300 	};										\
301 	static struct ft5336_data ft5336_data_##index;					\
302 	DEVICE_DT_INST_DEFINE(index, ft5336_init, PM_DEVICE_DT_INST_GET(n),		\
303 			      &ft5336_data_##index, &ft5336_config_##index,		\
304 			      POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, NULL);
305 
306 DT_INST_FOREACH_STATUS_OKAY(FT5336_INIT)
307