drm/client: Fix drm client endless Kconfig loop
[drm/drm-misc.git] / drivers / input / touchscreen / himax_hx83112b.c
blob896a145ddb2bc21c2e7c8447d5b6506e993eb1ad
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Driver for Himax hx83112b touchscreens
5 * Copyright (C) 2022 Job Noorman <job@noorman.info>
7 * HX83100A support
8 * Copyright (C) 2024 Felix Kaechele <felix@kaechele.ca>
10 * This code is based on "Himax Android Driver Sample Code for QCT platform":
12 * Copyright (C) 2017 Himax Corporation.
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/i2c.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/input/touchscreen.h>
22 #include <linux/interrupt.h>
23 #include <linux/kernel.h>
24 #include <linux/regmap.h>
26 #define HIMAX_MAX_POINTS 10
28 #define HIMAX_AHB_ADDR_BYTE_0 0x00
29 #define HIMAX_AHB_ADDR_RDATA_BYTE_0 0x08
30 #define HIMAX_AHB_ADDR_ACCESS_DIRECTION 0x0c
31 #define HIMAX_AHB_ADDR_INCR4 0x0d
32 #define HIMAX_AHB_ADDR_CONTI 0x13
33 #define HIMAX_AHB_ADDR_EVENT_STACK 0x30
35 #define HIMAX_AHB_CMD_ACCESS_DIRECTION_READ 0x00
36 #define HIMAX_AHB_CMD_INCR4 0x10
37 #define HIMAX_AHB_CMD_CONTI 0x31
39 #define HIMAX_REG_ADDR_ICID 0x900000d0
41 #define HX83100A_REG_FW_EVENT_STACK 0x90060000
43 #define HIMAX_INVALID_COORD 0xffff
45 struct himax_event_point {
46 __be16 x;
47 __be16 y;
48 } __packed;
50 struct himax_event {
51 struct himax_event_point points[HIMAX_MAX_POINTS];
52 u8 majors[HIMAX_MAX_POINTS];
53 u8 pad0[2];
54 u8 num_points;
55 u8 pad1[2];
56 u8 checksum_fix;
57 } __packed;
59 static_assert(sizeof(struct himax_event) == 56);
61 struct himax_ts_data;
62 struct himax_chip {
63 u32 id;
64 int (*check_id)(struct himax_ts_data *ts);
65 int (*read_events)(struct himax_ts_data *ts, struct himax_event *event,
66 size_t length);
69 struct himax_ts_data {
70 const struct himax_chip *chip;
71 struct gpio_desc *gpiod_rst;
72 struct input_dev *input_dev;
73 struct i2c_client *client;
74 struct regmap *regmap;
75 struct touchscreen_properties props;
78 static const struct regmap_config himax_regmap_config = {
79 .reg_bits = 8,
80 .val_bits = 32,
81 .val_format_endian = REGMAP_ENDIAN_LITTLE,
84 static int himax_bus_enable_burst(struct himax_ts_data *ts)
86 int error;
88 error = regmap_write(ts->regmap, HIMAX_AHB_ADDR_CONTI,
89 HIMAX_AHB_CMD_CONTI);
90 if (error)
91 return error;
93 error = regmap_write(ts->regmap, HIMAX_AHB_ADDR_INCR4,
94 HIMAX_AHB_CMD_INCR4);
95 if (error)
96 return error;
98 return 0;
101 static int himax_bus_read(struct himax_ts_data *ts, u32 address, void *dst,
102 size_t length)
104 int error;
106 if (length > 4) {
107 error = himax_bus_enable_burst(ts);
108 if (error)
109 return error;
112 error = regmap_write(ts->regmap, HIMAX_AHB_ADDR_BYTE_0, address);
113 if (error)
114 return error;
116 error = regmap_write(ts->regmap, HIMAX_AHB_ADDR_ACCESS_DIRECTION,
117 HIMAX_AHB_CMD_ACCESS_DIRECTION_READ);
118 if (error)
119 return error;
121 if (length > 4)
122 error = regmap_noinc_read(ts->regmap, HIMAX_AHB_ADDR_RDATA_BYTE_0,
123 dst, length);
124 else
125 error = regmap_read(ts->regmap, HIMAX_AHB_ADDR_RDATA_BYTE_0,
126 dst);
127 if (error)
128 return error;
130 return 0;
133 static void himax_reset(struct himax_ts_data *ts)
135 gpiod_set_value_cansleep(ts->gpiod_rst, 1);
137 /* Delay copied from downstream driver */
138 msleep(20);
139 gpiod_set_value_cansleep(ts->gpiod_rst, 0);
142 * The downstream driver doesn't contain this delay but is seems safer
143 * to include it. The range is just a guess that seems to work well.
145 usleep_range(1000, 1100);
148 static int himax_read_product_id(struct himax_ts_data *ts, u32 *product_id)
150 int error;
152 error = himax_bus_read(ts, HIMAX_REG_ADDR_ICID, product_id,
153 sizeof(*product_id));
154 if (error)
155 return error;
157 *product_id >>= 8;
158 return 0;
161 static int himax_check_product_id(struct himax_ts_data *ts)
163 int error;
164 u32 product_id;
166 error = himax_read_product_id(ts, &product_id);
167 if (error)
168 return error;
170 dev_dbg(&ts->client->dev, "Product id: %x\n", product_id);
172 if (product_id == ts->chip->id)
173 return 0;
175 dev_err(&ts->client->dev, "Unknown product id: %x\n",
176 product_id);
177 return -EINVAL;
180 static int himax_input_register(struct himax_ts_data *ts)
182 int error;
184 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
185 if (!ts->input_dev) {
186 dev_err(&ts->client->dev, "Failed to allocate input device\n");
187 return -ENOMEM;
190 ts->input_dev->name = "Himax Touchscreen";
192 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
193 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
194 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 200, 0, 0);
195 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 200, 0, 0);
197 touchscreen_parse_properties(ts->input_dev, true, &ts->props);
199 error = input_mt_init_slots(ts->input_dev, HIMAX_MAX_POINTS,
200 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
201 if (error) {
202 dev_err(&ts->client->dev,
203 "Failed to initialize MT slots: %d\n", error);
204 return error;
207 error = input_register_device(ts->input_dev);
208 if (error) {
209 dev_err(&ts->client->dev,
210 "Failed to register input device: %d\n", error);
211 return error;
214 return 0;
217 static u8 himax_event_get_num_points(const struct himax_event *event)
219 if (event->num_points == 0xff)
220 return 0;
221 else
222 return event->num_points & 0x0f;
225 static bool himax_process_event_point(struct himax_ts_data *ts,
226 const struct himax_event *event,
227 int point_index)
229 const struct himax_event_point *point = &event->points[point_index];
230 u16 x = be16_to_cpu(point->x);
231 u16 y = be16_to_cpu(point->y);
232 u8 w = event->majors[point_index];
234 if (x == HIMAX_INVALID_COORD || y == HIMAX_INVALID_COORD)
235 return false;
237 input_mt_slot(ts->input_dev, point_index);
238 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
239 touchscreen_report_pos(ts->input_dev, &ts->props, x, y, true);
240 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
241 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
242 return true;
245 static void himax_process_event(struct himax_ts_data *ts,
246 const struct himax_event *event)
248 int i;
249 int num_points_left = himax_event_get_num_points(event);
251 for (i = 0; i < HIMAX_MAX_POINTS && num_points_left > 0; i++) {
252 if (himax_process_event_point(ts, event, i))
253 num_points_left--;
256 input_mt_sync_frame(ts->input_dev);
257 input_sync(ts->input_dev);
260 static bool himax_verify_checksum(struct himax_ts_data *ts,
261 const struct himax_event *event)
263 u8 *data = (u8 *)event;
264 int i;
265 u16 checksum = 0;
267 for (i = 0; i < sizeof(*event); i++)
268 checksum += data[i];
270 if ((checksum & 0x00ff) != 0) {
271 dev_err(&ts->client->dev, "Wrong event checksum: %04x\n",
272 checksum);
273 return false;
276 return true;
279 static int himax_read_events(struct himax_ts_data *ts,
280 struct himax_event *event, size_t length)
282 return regmap_raw_read(ts->regmap, HIMAX_AHB_ADDR_EVENT_STACK, event,
283 length);
286 static int hx83100a_read_events(struct himax_ts_data *ts,
287 struct himax_event *event, size_t length)
289 return himax_bus_read(ts, HX83100A_REG_FW_EVENT_STACK, event, length);
292 static int himax_handle_input(struct himax_ts_data *ts)
294 int error;
295 struct himax_event event;
297 error = ts->chip->read_events(ts, &event, sizeof(event));
298 if (error) {
299 dev_err(&ts->client->dev, "Failed to read input event: %d\n",
300 error);
301 return error;
305 * Only process the current event when it has a valid checksum but
306 * don't consider it a fatal error when it doesn't.
308 if (himax_verify_checksum(ts, &event))
309 himax_process_event(ts, &event);
311 return 0;
314 static irqreturn_t himax_irq_handler(int irq, void *dev_id)
316 int error;
317 struct himax_ts_data *ts = dev_id;
319 error = himax_handle_input(ts);
320 if (error)
321 return IRQ_NONE;
323 return IRQ_HANDLED;
326 static int himax_probe(struct i2c_client *client)
328 int error;
329 struct device *dev = &client->dev;
330 struct himax_ts_data *ts;
332 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
333 dev_err(dev, "I2C check functionality failed\n");
334 return -ENXIO;
337 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
338 if (!ts)
339 return -ENOMEM;
341 i2c_set_clientdata(client, ts);
342 ts->client = client;
343 ts->chip = i2c_get_match_data(client);
345 ts->regmap = devm_regmap_init_i2c(client, &himax_regmap_config);
346 error = PTR_ERR_OR_ZERO(ts->regmap);
347 if (error) {
348 dev_err(dev, "Failed to initialize regmap: %d\n", error);
349 return error;
352 ts->gpiod_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
353 error = PTR_ERR_OR_ZERO(ts->gpiod_rst);
354 if (error) {
355 dev_err(dev, "Failed to get reset GPIO: %d\n", error);
356 return error;
359 himax_reset(ts);
361 if (ts->chip->check_id) {
362 error = himax_check_product_id(ts);
363 if (error)
364 return error;
367 error = himax_input_register(ts);
368 if (error)
369 return error;
371 error = devm_request_threaded_irq(dev, client->irq, NULL,
372 himax_irq_handler, IRQF_ONESHOT,
373 client->name, ts);
374 if (error)
375 return error;
377 return 0;
380 static int himax_suspend(struct device *dev)
382 struct himax_ts_data *ts = dev_get_drvdata(dev);
384 disable_irq(ts->client->irq);
385 return 0;
388 static int himax_resume(struct device *dev)
390 struct himax_ts_data *ts = dev_get_drvdata(dev);
392 enable_irq(ts->client->irq);
393 return 0;
396 static DEFINE_SIMPLE_DEV_PM_OPS(himax_pm_ops, himax_suspend, himax_resume);
398 static const struct himax_chip hx83100a_chip = {
399 .read_events = hx83100a_read_events,
402 static const struct himax_chip hx83112b_chip = {
403 .id = 0x83112b,
404 .check_id = himax_check_product_id,
405 .read_events = himax_read_events,
408 static const struct i2c_device_id himax_ts_id[] = {
409 { "hx83100a", (kernel_ulong_t)&hx83100a_chip },
410 { "hx83112b", (kernel_ulong_t)&hx83112b_chip },
411 { /* sentinel */ }
413 MODULE_DEVICE_TABLE(i2c, himax_ts_id);
415 #ifdef CONFIG_OF
416 static const struct of_device_id himax_of_match[] = {
417 { .compatible = "himax,hx83100a", .data = &hx83100a_chip },
418 { .compatible = "himax,hx83112b", .data = &hx83112b_chip },
419 { /* sentinel */ }
421 MODULE_DEVICE_TABLE(of, himax_of_match);
422 #endif
424 static struct i2c_driver himax_ts_driver = {
425 .probe = himax_probe,
426 .id_table = himax_ts_id,
427 .driver = {
428 .name = "Himax-hx83112b-TS",
429 .of_match_table = of_match_ptr(himax_of_match),
430 .pm = pm_sleep_ptr(&himax_pm_ops),
433 module_i2c_driver(himax_ts_driver);
435 MODULE_AUTHOR("Job Noorman <job@noorman.info>");
436 MODULE_DESCRIPTION("Himax hx83112b touchscreen driver");
437 MODULE_LICENSE("GPL");