667a719a808c4068b4485efe717b80aa7076cdeb
[openwrt/staging/linusw.git] /
1 From 4d5992d474e75e58ceb0f3fe02e0f949c1255dcd Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Wed, 26 Jan 2022 16:02:31 +0000
4 Subject: [PATCH 0346/1085] drm/panel: Add panel driver for TDO Y17B based
5 panels
6
7 The Top DisplayOptoelectronics (TDO) T17B driver chip is used
8 in the TL040HDS20CT panel (found in the Pimoroni HyperPixel4
9 Square display) and potentially other displays.
10 The driver chip supports SPI for configuration and DPI
11 video data.
12
13 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
14 ---
15 drivers/gpu/drm/panel/Kconfig | 11 +
16 drivers/gpu/drm/panel/Makefile | 1 +
17 drivers/gpu/drm/panel/panel-tdo-y17p.c | 277 +++++++++++++++++++++++++
18 3 files changed, 289 insertions(+)
19 create mode 100644 drivers/gpu/drm/panel/panel-tdo-y17p.c
20
21 --- a/drivers/gpu/drm/panel/Kconfig
22 +++ b/drivers/gpu/drm/panel/Kconfig
23 @@ -767,6 +767,17 @@ config DRM_PANEL_TDO_TL070WSH30
24 24 bit RGB per pixel. It provides a MIPI DSI interface to
25 the host, a built-in LED backlight and touch controller.
26
27 +config DRM_PANEL_TPO_Y17P
28 + tristate "TDO Y17P-based panels"
29 + depends on OF && SPI
30 + select DRM_KMS_HELPER
31 + depends on DRM_GEM_CMA_HELPER
32 + depends on BACKLIGHT_CLASS_DEVICE
33 + select DRM_MIPI_DBI
34 + help
35 + Say Y if you want to enable support for panels based on the
36 + TDO Y17P controller.
37 +
38 config DRM_PANEL_TPO_TD028TTEC1
39 tristate "Toppoly (TPO) TD028TTEC1 panel driver"
40 depends on OF && SPI
41 --- a/drivers/gpu/drm/panel/Makefile
42 +++ b/drivers/gpu/drm/panel/Makefile
43 @@ -77,6 +77,7 @@ obj-$(CONFIG_DRM_PANEL_SONY_TD4353_JDI)
44 obj-$(CONFIG_DRM_PANEL_SONY_TULIP_TRULY_NT35521) += panel-sony-tulip-truly-nt35521.o
45 obj-$(CONFIG_DRM_PANEL_STARTEK_KD070FHFID015) += panel-startek-kd070fhfid015.o
46 obj-$(CONFIG_DRM_PANEL_TDO_TL070WSH30) += panel-tdo-tl070wsh30.o
47 +obj-$(CONFIG_DRM_PANEL_TPO_Y17P) += panel-tdo-y17p.o
48 obj-$(CONFIG_DRM_PANEL_TPO_TD028TTEC1) += panel-tpo-td028ttec1.o
49 obj-$(CONFIG_DRM_PANEL_TPO_TD043MTEA1) += panel-tpo-td043mtea1.o
50 obj-$(CONFIG_DRM_PANEL_TPO_TPG110) += panel-tpo-tpg110.o
51 --- /dev/null
52 +++ b/drivers/gpu/drm/panel/panel-tdo-y17p.c
53 @@ -0,0 +1,277 @@
54 +// SPDX-License-Identifier: GPL-2.0-only
55 +/*
56 + * TDO Y17P TFT LCD drm_panel driver.
57 + *
58 + * SPI configured DPI display controller
59 + * Copyright (C) 2022 Raspberry Pi Ltd
60 + *
61 + * Derived from drivers/drm/gpu/panel/panel-sitronix-st7789v.c
62 + * Copyright (C) 2017 Free Electrons
63 + */
64 +
65 +#include <drm/drm_modes.h>
66 +#include <drm/drm_panel.h>
67 +
68 +#include <linux/bitops.h>
69 +#include <linux/gpio/consumer.h>
70 +#include <linux/media-bus-format.h>
71 +#include <linux/module.h>
72 +#include <linux/of_device.h>
73 +#include <linux/regmap.h>
74 +#include <linux/regulator/consumer.h>
75 +#include <linux/spi/spi.h>
76 +
77 +#include <video/mipi_display.h>
78 +#include <video/of_videomode.h>
79 +#include <video/videomode.h>
80 +
81 +struct tdo_y17p {
82 + struct drm_panel panel;
83 + struct spi_device *spi;
84 + struct gpio_desc *reset;
85 + struct regulator *power;
86 + u32 bus_format;
87 +};
88 +
89 +static const u16 panel_init[] = {
90 + 0x0ff, 0x1ff, 0x198, 0x106, 0x104, 0x101, 0x008, 0x110,
91 + 0x021, 0x109, 0x030, 0x102, 0x031, 0x100, 0x040, 0x110,
92 + 0x041, 0x155, 0x042, 0x102, 0x043, 0x109, 0x044, 0x107,
93 + 0x050, 0x178, 0x051, 0x178, 0x052, 0x100, 0x053, 0x16d,
94 + 0x060, 0x107, 0x061, 0x100, 0x062, 0x108, 0x063, 0x100,
95 + 0x0a0, 0x100, 0x0a1, 0x107, 0x0a2, 0x10c, 0x0a3, 0x10b,
96 + 0x0a4, 0x103, 0x0a5, 0x107, 0x0a6, 0x106, 0x0a7, 0x104,
97 + 0x0a8, 0x108, 0x0a9, 0x10c, 0x0aa, 0x113, 0x0ab, 0x106,
98 + 0x0ac, 0x10d, 0x0ad, 0x119, 0x0ae, 0x110, 0x0af, 0x100,
99 + 0x0c0, 0x100, 0x0c1, 0x107, 0x0c2, 0x10c, 0x0c3, 0x10b,
100 + 0x0c4, 0x103, 0x0c5, 0x107, 0x0c6, 0x107, 0x0c7, 0x104,
101 + 0x0c8, 0x108, 0x0c9, 0x10c, 0x0ca, 0x113, 0x0cb, 0x106,
102 + 0x0cc, 0x10d, 0x0cd, 0x118, 0x0ce, 0x110, 0x0cf, 0x100,
103 + 0x0ff, 0x1ff, 0x198, 0x106, 0x104, 0x106, 0x000, 0x120,
104 + 0x001, 0x10a, 0x002, 0x100, 0x003, 0x100, 0x004, 0x101,
105 + 0x005, 0x101, 0x006, 0x198, 0x007, 0x106, 0x008, 0x101,
106 + 0x009, 0x180, 0x00a, 0x100, 0x00b, 0x100, 0x00c, 0x101,
107 + 0x00d, 0x101, 0x00e, 0x100, 0x00f, 0x100, 0x010, 0x1f0,
108 + 0x011, 0x1f4, 0x012, 0x101, 0x013, 0x100, 0x014, 0x100,
109 + 0x015, 0x1c0, 0x016, 0x108, 0x017, 0x100, 0x018, 0x100,
110 + 0x019, 0x100, 0x01a, 0x100, 0x01b, 0x100, 0x01c, 0x100,
111 + 0x01d, 0x100, 0x020, 0x101, 0x021, 0x123, 0x022, 0x145,
112 + 0x023, 0x167, 0x024, 0x101, 0x025, 0x123, 0x026, 0x145,
113 + 0x027, 0x167, 0x030, 0x111, 0x031, 0x111, 0x032, 0x100,
114 + 0x033, 0x1ee, 0x034, 0x1ff, 0x035, 0x1bb, 0x036, 0x1aa,
115 + 0x037, 0x1dd, 0x038, 0x1cc, 0x039, 0x166, 0x03a, 0x177,
116 + 0x03b, 0x122, 0x03c, 0x122, 0x03d, 0x122, 0x03e, 0x122,
117 + 0x03f, 0x122, 0x040, 0x122, 0x052, 0x110, 0x053, 0x110,
118 + 0x0ff, 0x1ff, 0x198, 0x106, 0x104, 0x107, 0x018, 0x11d,
119 + 0x017, 0x122, 0x002, 0x177, 0x026, 0x1b2, 0x0e1, 0x179,
120 + 0x0ff, 0x1ff, 0x198, 0x106, 0x104, 0x100, 0x03a, 0x160,
121 + 0x035, 0x100, 0x011, 0x100,
122 +};
123 +
124 +#define NUM_INIT_REGS ARRAY_SIZE(panel_init)
125 +
126 +static inline struct tdo_y17p *panel_to_tdo_y17p(struct drm_panel *panel)
127 +{
128 + return container_of(panel, struct tdo_y17p, panel);
129 +}
130 +
131 +static int tdo_y17p_write_msg(struct tdo_y17p *ctx, const u16 *msg, unsigned int len)
132 +{
133 + struct spi_transfer xfer = { };
134 + struct spi_message spi;
135 +
136 + spi_message_init(&spi);
137 +
138 + xfer.tx_buf = msg;
139 + xfer.bits_per_word = 9;
140 + xfer.len = sizeof(u16) * len;
141 +
142 + spi_message_add_tail(&xfer, &spi);
143 + return spi_sync(ctx->spi, &spi);
144 +}
145 +
146 +static const struct drm_display_mode tdo_y17pe_720x720_mode = {
147 + .clock = 36720,
148 + .hdisplay = 720,
149 + .hsync_start = 720 + 20,
150 + .hsync_end = 720 + 20 + 20,
151 + .htotal = 720 + 20 + 20 + 40,
152 + .vdisplay = 720,
153 + .vsync_start = 720 + 15,
154 + .vsync_end = 720 + 15 + 15,
155 + .vtotal = 720 + 15 + 15 + 15,
156 + .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
157 +};
158 +
159 +static int tdo_y17p_get_modes(struct drm_panel *panel,
160 + struct drm_connector *connector)
161 +{
162 + struct tdo_y17p *ctx = panel_to_tdo_y17p(panel);
163 + struct drm_display_mode *mode;
164 +
165 + mode = drm_mode_duplicate(connector->dev, &tdo_y17pe_720x720_mode);
166 + if (!mode) {
167 + dev_err(panel->dev, "failed to add mode %ux%ux@%u\n",
168 + tdo_y17pe_720x720_mode.hdisplay,
169 + tdo_y17pe_720x720_mode.vdisplay,
170 + drm_mode_vrefresh(&tdo_y17pe_720x720_mode));
171 + return -ENOMEM;
172 + }
173 +
174 + drm_mode_set_name(mode);
175 +
176 + mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
177 + drm_mode_probed_add(connector, mode);
178 +
179 + connector->display_info.width_mm = 72;
180 + connector->display_info.height_mm = 72;
181 + drm_display_info_set_bus_formats(&connector->display_info,
182 + &ctx->bus_format, 1);
183 + connector->display_info.bus_flags = 0;
184 +
185 + return 1;
186 +}
187 +
188 +static int tdo_y17p_prepare(struct drm_panel *panel)
189 +{
190 + struct tdo_y17p *ctx = panel_to_tdo_y17p(panel);
191 + int ret;
192 +
193 + ret = regulator_enable(ctx->power);
194 + if (ret)
195 + return ret;
196 +
197 + ret = tdo_y17p_write_msg(ctx, panel_init, NUM_INIT_REGS);
198 +
199 + msleep(120);
200 +
201 + return ret;
202 +}
203 +
204 +static int tdo_y17p_enable(struct drm_panel *panel)
205 +{
206 + struct tdo_y17p *ctx = panel_to_tdo_y17p(panel);
207 + const u16 msg[] = { MIPI_DCS_SET_DISPLAY_ON };
208 + int ret;
209 +
210 + ret = tdo_y17p_write_msg(ctx, msg, ARRAY_SIZE(msg));
211 +
212 + return ret;
213 +}
214 +
215 +static int tdo_y17p_disable(struct drm_panel *panel)
216 +{
217 + struct tdo_y17p *ctx = panel_to_tdo_y17p(panel);
218 + const u16 msg[] = { MIPI_DCS_SET_DISPLAY_OFF };
219 + int ret;
220 +
221 + ret = tdo_y17p_write_msg(ctx, msg, ARRAY_SIZE(msg));
222 +
223 + return ret;
224 +}
225 +
226 +static int tdo_y17p_unprepare(struct drm_panel *panel)
227 +{
228 + struct tdo_y17p *ctx = panel_to_tdo_y17p(panel);
229 + const u16 msg[] = { MIPI_DCS_ENTER_SLEEP_MODE };
230 + int ret;
231 +
232 + ret = tdo_y17p_write_msg(ctx, msg, ARRAY_SIZE(msg));
233 +
234 + return ret;
235 +}
236 +
237 +static const struct drm_panel_funcs tdo_y17p_drm_funcs = {
238 + .disable = tdo_y17p_disable,
239 + .enable = tdo_y17p_enable,
240 + .get_modes = tdo_y17p_get_modes,
241 + .prepare = tdo_y17p_prepare,
242 + .unprepare = tdo_y17p_unprepare,
243 +};
244 +
245 +static const struct of_device_id tdo_y17p_of_match[] = {
246 + { .compatible = "tdo,tl040hds20ct",
247 + .data = (void *)MEDIA_BUS_FMT_BGR888_1X24,
248 + }, {
249 + .compatible = "pimoroni,hyperpixel4square",
250 + .data = (void *)MEDIA_BUS_FMT_BGR666_1X24_CPADHI,
251 + }, {
252 + .compatible = "tdo,y17p",
253 + .data = (void *)MEDIA_BUS_FMT_BGR888_1X24,
254 + }, {
255 + /* sentinel */
256 + }
257 +};
258 +MODULE_DEVICE_TABLE(of, tdo_y17p_of_match);
259 +
260 +static int tdo_y17p_probe(struct spi_device *spi)
261 +{
262 + const struct of_device_id *id;
263 + struct tdo_y17p *ctx;
264 + int ret;
265 +
266 + ctx = devm_kzalloc(&spi->dev, sizeof(*ctx), GFP_KERNEL);
267 + if (!ctx)
268 + return -ENOMEM;
269 +
270 + id = of_match_node(tdo_y17p_of_match, spi->dev.of_node);
271 + if (!id)
272 + return -ENODEV;
273 +
274 + ctx->bus_format = (u32)(uintptr_t)id->data;
275 +
276 + spi_set_drvdata(spi, ctx);
277 + ctx->spi = spi;
278 +
279 + drm_panel_init(&ctx->panel, &spi->dev, &tdo_y17p_drm_funcs,
280 + DRM_MODE_CONNECTOR_DPI);
281 +
282 + ctx->power = devm_regulator_get(&spi->dev, "power");
283 + if (IS_ERR(ctx->power))
284 + return PTR_ERR(ctx->power);
285 +
286 + ctx->reset = devm_gpiod_get_optional(&spi->dev, "reset", GPIOD_OUT_LOW);
287 + if (IS_ERR(ctx->reset)) {
288 + dev_err(&spi->dev, "Couldn't get our reset line\n");
289 + return PTR_ERR(ctx->reset);
290 + }
291 +
292 + ret = drm_panel_of_backlight(&ctx->panel);
293 + if (ret)
294 + return ret;
295 +
296 + drm_panel_add(&ctx->panel);
297 +
298 + return 0;
299 +}
300 +
301 +static void tdo_y17p_remove(struct spi_device *spi)
302 +{
303 + struct tdo_y17p *ctx = spi_get_drvdata(spi);
304 +
305 + drm_panel_remove(&ctx->panel);
306 +}
307 +
308 +static const struct spi_device_id tdo_y17p_ids[] = {
309 + { "tl040hds20ct", 0 },
310 + { "hyperpixel4square", 0 },
311 + { "y17p", 0 },
312 + { /* sentinel */ }
313 +};
314 +
315 +MODULE_DEVICE_TABLE(spi, tdo_y17p_ids);
316 +
317 +static struct spi_driver tdo_y17p_driver = {
318 + .probe = tdo_y17p_probe,
319 + .remove = tdo_y17p_remove,
320 + .driver = {
321 + .name = "tdo_y17p",
322 + .of_match_table = tdo_y17p_of_match,
323 + },
324 + .id_table = tdo_y17p_ids,
325 +};
326 +module_spi_driver(tdo_y17p_driver);
327 +
328 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
329 +MODULE_DESCRIPTION("TDO Y17P LCD panel driver");
330 +MODULE_LICENSE("GPL v2");