xref: /linux/drivers/input/touchscreen/goodix_berlin_spi.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Goodix Berlin Touchscreen Driver
4  *
5  * Copyright (C) 2020 - 2021 Goodix, Inc.
6  * Copyright (C) 2023 Linaro Ltd.
7  *
8  * Based on goodix_ts_berlin driver.
9  */
10 #include <linux/unaligned.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/spi/spi.h>
15 #include <linux/input.h>
16 
17 #include "goodix_berlin.h"
18 
19 #define GOODIX_BERLIN_SPI_TRANS_PREFIX_LEN	1
20 #define GOODIX_BERLIN_REGISTER_WIDTH		4
21 #define GOODIX_BERLIN_SPI_READ_DUMMY_LEN	3
22 #define GOODIX_BERLIN_SPI_READ_PREFIX_LEN	(GOODIX_BERLIN_SPI_TRANS_PREFIX_LEN + \
23 						 GOODIX_BERLIN_REGISTER_WIDTH + \
24 						 GOODIX_BERLIN_SPI_READ_DUMMY_LEN)
25 #define GOODIX_BERLIN_SPI_WRITE_PREFIX_LEN	(GOODIX_BERLIN_SPI_TRANS_PREFIX_LEN + \
26 						 GOODIX_BERLIN_REGISTER_WIDTH)
27 
28 #define GOODIX_BERLIN_SPI_WRITE_FLAG		0xF0
29 #define GOODIX_BERLIN_SPI_READ_FLAG		0xF1
30 
31 static int goodix_berlin_spi_read(void *context, const void *reg_buf,
32 				  size_t reg_size, void *val_buf,
33 				  size_t val_size)
34 {
35 	struct spi_device *spi = context;
36 	struct spi_transfer xfers;
37 	struct spi_message spi_msg;
38 	const u32 *reg = reg_buf; /* reg is stored as native u32 at start of buffer */
39 	int error;
40 
41 	if (reg_size != GOODIX_BERLIN_REGISTER_WIDTH)
42 		return -EINVAL;
43 
44 	u8 *buf __free(kfree) =
45 		kzalloc(GOODIX_BERLIN_SPI_READ_PREFIX_LEN + val_size,
46 			GFP_KERNEL);
47 	if (!buf)
48 		return -ENOMEM;
49 
50 	spi_message_init(&spi_msg);
51 	memset(&xfers, 0, sizeof(xfers));
52 
53 	/* buffer format: 0xF1 + addr(4bytes) + dummy(3bytes) + data */
54 	buf[0] = GOODIX_BERLIN_SPI_READ_FLAG;
55 	put_unaligned_be32(*reg, buf + GOODIX_BERLIN_SPI_TRANS_PREFIX_LEN);
56 	memset(buf + GOODIX_BERLIN_SPI_TRANS_PREFIX_LEN + GOODIX_BERLIN_REGISTER_WIDTH,
57 	       0xff, GOODIX_BERLIN_SPI_READ_DUMMY_LEN);
58 
59 	xfers.tx_buf = buf;
60 	xfers.rx_buf = buf;
61 	xfers.len = GOODIX_BERLIN_SPI_READ_PREFIX_LEN + val_size;
62 	xfers.cs_change = 0;
63 	spi_message_add_tail(&xfers, &spi_msg);
64 
65 	error = spi_sync(spi, &spi_msg);
66 	if (error < 0) {
67 		dev_err(&spi->dev, "spi transfer error, %d", error);
68 		return error;
69 	}
70 
71 	memcpy(val_buf, buf + GOODIX_BERLIN_SPI_READ_PREFIX_LEN, val_size);
72 	return error;
73 }
74 
75 static int goodix_berlin_spi_write(void *context, const void *data,
76 				   size_t count)
77 {
78 	unsigned int len = count - GOODIX_BERLIN_REGISTER_WIDTH;
79 	struct spi_device *spi = context;
80 	struct spi_transfer xfers;
81 	struct spi_message spi_msg;
82 	const u32 *reg = data; /* reg is stored as native u32 at start of buffer */
83 	int error;
84 
85 	u8 *buf __free(kfree) =
86 		kzalloc(GOODIX_BERLIN_SPI_WRITE_PREFIX_LEN + len, GFP_KERNEL);
87 	if (!buf)
88 		return -ENOMEM;
89 
90 	spi_message_init(&spi_msg);
91 	memset(&xfers, 0, sizeof(xfers));
92 
93 	buf[0] = GOODIX_BERLIN_SPI_WRITE_FLAG;
94 	put_unaligned_be32(*reg, buf + GOODIX_BERLIN_SPI_TRANS_PREFIX_LEN);
95 	memcpy(buf + GOODIX_BERLIN_SPI_WRITE_PREFIX_LEN,
96 	       data + GOODIX_BERLIN_REGISTER_WIDTH, len);
97 
98 	xfers.tx_buf = buf;
99 	xfers.len = GOODIX_BERLIN_SPI_WRITE_PREFIX_LEN + len;
100 	xfers.cs_change = 0;
101 	spi_message_add_tail(&xfers, &spi_msg);
102 
103 	error = spi_sync(spi, &spi_msg);
104 	if (error < 0) {
105 		dev_err(&spi->dev, "spi transfer error, %d", error);
106 		return error;
107 	}
108 
109 	return 0;
110 }
111 
112 static const struct regmap_config goodix_berlin_spi_regmap_conf = {
113 	.reg_bits = 32,
114 	.val_bits = 8,
115 	.read = goodix_berlin_spi_read,
116 	.write = goodix_berlin_spi_write,
117 };
118 
119 /* vendor & product left unassigned here, should probably be updated from fw info */
120 static const struct input_id goodix_berlin_spi_input_id = {
121 	.bustype = BUS_SPI,
122 };
123 
124 static int goodix_berlin_spi_probe(struct spi_device *spi)
125 {
126 	struct regmap_config regmap_config;
127 	struct regmap *regmap;
128 	size_t max_size;
129 	int error = 0;
130 
131 	spi->mode = SPI_MODE_0;
132 	spi->bits_per_word = 8;
133 	error = spi_setup(spi);
134 	if (error)
135 		return error;
136 
137 	max_size = spi_max_transfer_size(spi);
138 
139 	regmap_config = goodix_berlin_spi_regmap_conf;
140 	regmap_config.max_raw_read = max_size - GOODIX_BERLIN_SPI_READ_PREFIX_LEN;
141 	regmap_config.max_raw_write = max_size - GOODIX_BERLIN_SPI_WRITE_PREFIX_LEN;
142 
143 	regmap = devm_regmap_init(&spi->dev, NULL, spi, &regmap_config);
144 	if (IS_ERR(regmap))
145 		return PTR_ERR(regmap);
146 
147 	error = goodix_berlin_probe(&spi->dev, spi->irq,
148 				    &goodix_berlin_spi_input_id, regmap);
149 	if (error)
150 		return error;
151 
152 	return 0;
153 }
154 
155 static const struct spi_device_id goodix_berlin_spi_ids[] = {
156 	{ "gt9916" },
157 	{ },
158 };
159 MODULE_DEVICE_TABLE(spi, goodix_berlin_spi_ids);
160 
161 static const struct of_device_id goodix_berlin_spi_of_match[] = {
162 	{ .compatible = "goodix,gt9916", },
163 	{ }
164 };
165 MODULE_DEVICE_TABLE(of, goodix_berlin_spi_of_match);
166 
167 static struct spi_driver goodix_berlin_spi_driver = {
168 	.driver = {
169 		.name = "goodix-berlin-spi",
170 		.of_match_table = goodix_berlin_spi_of_match,
171 		.pm = pm_sleep_ptr(&goodix_berlin_pm_ops),
172 		.dev_groups = goodix_berlin_groups,
173 	},
174 	.probe = goodix_berlin_spi_probe,
175 	.id_table = goodix_berlin_spi_ids,
176 };
177 module_spi_driver(goodix_berlin_spi_driver);
178 
179 MODULE_LICENSE("GPL");
180 MODULE_DESCRIPTION("Goodix Berlin SPI Touchscreen driver");
181 MODULE_AUTHOR("Neil Armstrong <neil.armstrong@linaro.org>");
182