7c44509690020554d1e3729d33906b5966b04b2c
[openwrt/staging/ldir.git] /
1 From cd117c5e63682b561b29cac1794e2e4413ad6773 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Fri, 14 Apr 2023 13:50:08 +0100
4 Subject: [PATCH] drm/panel: Add panel driver for Waveshare DSI
5 touchscreens
6
7 Waveshare sell a range of DSI panels of varying sizes, all
8 using a common MCU to control the panel and backlight.
9
10 Add a panel driver that supports these panels.
11
12 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
13 ---
14 drivers/gpu/drm/panel/Kconfig | 10 +
15 drivers/gpu/drm/panel/Makefile | 1 +
16 drivers/gpu/drm/panel/panel-waveshare-dsi.c | 411 ++++++++++++++++++++
17 3 files changed, 422 insertions(+)
18 create mode 100644 drivers/gpu/drm/panel/panel-waveshare-dsi.c
19
20 --- a/drivers/gpu/drm/panel/Kconfig
21 +++ b/drivers/gpu/drm/panel/Kconfig
22 @@ -720,6 +720,16 @@ config DRM_PANEL_VISIONOX_RM69299
23 Say Y here if you want to enable support for Visionox
24 RM69299 DSI Video Mode panel.
25
26 +config DRM_PANEL_WAVESHARE_TOUCHSCREEN
27 + tristate "Waveshare touchscreen panels"
28 + depends on DRM_MIPI_DSI
29 + depends on I2C
30 + depends on BACKLIGHT_CLASS_DEVICE
31 + help
32 + Say Y here if you want to enable support for the Waveshare
33 + DSI Touchscreens. To compile this driver as a module,
34 + choose M here.
35 +
36 config DRM_PANEL_WIDECHIPS_WS2401
37 tristate "Widechips WS2401 DPI panel driver"
38 depends on SPI && GPIOLIB
39 --- a/drivers/gpu/drm/panel/Makefile
40 +++ b/drivers/gpu/drm/panel/Makefile
41 @@ -73,5 +73,6 @@ obj-$(CONFIG_DRM_PANEL_TPO_TD043MTEA1) +
42 obj-$(CONFIG_DRM_PANEL_TPO_TPG110) += panel-tpo-tpg110.o
43 obj-$(CONFIG_DRM_PANEL_TRULY_NT35597_WQXGA) += panel-truly-nt35597.o
44 obj-$(CONFIG_DRM_PANEL_VISIONOX_RM69299) += panel-visionox-rm69299.o
45 +obj-$(CONFIG_DRM_PANEL_WAVESHARE_TOUCHSCREEN) += panel-waveshare-dsi.o
46 obj-$(CONFIG_DRM_PANEL_WIDECHIPS_WS2401) += panel-widechips-ws2401.o
47 obj-$(CONFIG_DRM_PANEL_XINPENG_XPP055C272) += panel-xinpeng-xpp055c272.o
48 --- /dev/null
49 +++ b/drivers/gpu/drm/panel/panel-waveshare-dsi.c
50 @@ -0,0 +1,411 @@
51 +// SPDX-License-Identifier: GPL-2.0
52 +/*
53 + * Copyright © 2023 Raspberry Pi Ltd
54 + *
55 + * Based on panel-raspberrypi-touchscreen by Broadcom
56 + */
57 +
58 +#include <linux/backlight.h>
59 +#include <linux/delay.h>
60 +#include <linux/err.h>
61 +#include <linux/fb.h>
62 +#include <linux/i2c.h>
63 +#include <linux/media-bus-format.h>
64 +#include <linux/module.h>
65 +#include <linux/of.h>
66 +#include <linux/of_device.h>
67 +#include <linux/of_graph.h>
68 +#include <linux/pm.h>
69 +
70 +#include <drm/drm_crtc.h>
71 +#include <drm/drm_device.h>
72 +#include <drm/drm_mipi_dsi.h>
73 +#include <drm/drm_panel.h>
74 +
75 +#define WS_DSI_DRIVER_NAME "ws-ts-dsi"
76 +
77 +struct ws_panel {
78 + struct drm_panel base;
79 + struct mipi_dsi_device *dsi;
80 + struct i2c_client *i2c;
81 + const struct drm_display_mode *mode;
82 + enum drm_panel_orientation orientation;
83 +};
84 +
85 +/* 2.8inch 480x640
86 + * https://www.waveshare.com/product/raspberry-pi/displays/2.8inch-dsi-lcd.htm
87 + */
88 +static const struct drm_display_mode ws_panel_2_8_mode = {
89 + .clock = 50000,
90 + .hdisplay = 480,
91 + .hsync_start = 480 + 150,
92 + .hsync_end = 480 + 150 + 50,
93 + .htotal = 480 + 150 + 50 + 150,
94 + .vdisplay = 640,
95 + .vsync_start = 640 + 150,
96 + .vsync_end = 640 + 150 + 50,
97 + .vtotal = 640 + 150 + 50 + 150,
98 +};
99 +
100 +/* 3.4inch 800x800 Round
101 + * https://www.waveshare.com/product/displays/lcd-oled/3.4inch-dsi-lcd-c.htm
102 + */
103 +static const struct drm_display_mode ws_panel_3_4_mode = {
104 + .clock = 50000,
105 + .hdisplay = 800,
106 + .hsync_start = 800 + 32,
107 + .hsync_end = 800 + 32 + 6,
108 + .htotal = 800 + 32 + 6 + 120,
109 + .vdisplay = 800,
110 + .vsync_start = 800 + 8,
111 + .vsync_end = 800 + 8 + 4,
112 + .vtotal = 800 + 8 + 4 + 16,
113 +};
114 +
115 +/* 4.0inch 480x800
116 + * https://www.waveshare.com/product/raspberry-pi/displays/4inch-dsi-lcd.htm
117 + */
118 +static const struct drm_display_mode ws_panel_4_0_mode = {
119 + .clock = 50000,
120 + .hdisplay = 480,
121 + .hsync_start = 480 + 150,
122 + .hsync_end = 480 + 150 + 100,
123 + .htotal = 480 + 150 + 100 + 150,
124 + .vdisplay = 800,
125 + .vsync_start = 800 + 20,
126 + .vsync_end = 800 + 20 + 100,
127 + .vtotal = 800 + 20 + 100 + 20,
128 +};
129 +
130 +/* 7.0inch C 1024x600
131 + * https://www.waveshare.com/product/raspberry-pi/displays/lcd-oled/7inch-dsi-lcd-c-with-case-a.htm
132 + */
133 +static const struct drm_display_mode ws_panel_7_0_c_mode = {
134 + .clock = 50000,
135 + .hdisplay = 1024,
136 + .hsync_start = 1024 + 100,
137 + .hsync_end = 1024 + 100 + 100,
138 + .htotal = 1024 + 100 + 100 + 100,
139 + .vdisplay = 600,
140 + .vsync_start = 600 + 10,
141 + .vsync_end = 600 + 10 + 10,
142 + .vtotal = 600 + 10 + 10 + 10,
143 +};
144 +
145 +/* 7.9inch 400x1280
146 + * https://www.waveshare.com/product/raspberry-pi/displays/7.9inch-dsi-lcd.htm
147 + */
148 +static const struct drm_display_mode ws_panel_7_9_mode = {
149 + .clock = 50000,
150 + .hdisplay = 400,
151 + .hsync_start = 400 + 40,
152 + .hsync_end = 400 + 40 + 30,
153 + .htotal = 400 + 40 + 30 + 40,
154 + .vdisplay = 1280,
155 + .vsync_start = 1280 + 20,
156 + .vsync_end = 1280 + 20 + 10,
157 + .vtotal = 1280 + 20 + 10 + 20,
158 +};
159 +
160 +/* 8.0inch or 10.1inch 1280x800
161 + * https://www.waveshare.com/product/raspberry-pi/displays/8inch-dsi-lcd-c.htm
162 + * https://www.waveshare.com/product/raspberry-pi/displays/10.1inch-dsi-lcd-c.htm
163 + */
164 +static const struct drm_display_mode ws_panel_10_1_mode = {
165 + .clock = 76800,
166 + .hdisplay = 1280,
167 + .hsync_start = 1280 + 40,
168 + .hsync_end = 1280 + 40 + 20,
169 + .htotal = 1280 + 40 + 20 + 40,
170 + .vdisplay = 800,
171 + .vsync_start = 800 + 40,
172 + .vsync_end = 800 + 40 + 48,
173 + .vtotal = 800 + 40 + 48 + 40,
174 +};
175 +
176 +/* 11.9inch 320x1480
177 + * https://www.waveshare.com/product/raspberry-pi/displays/11.9inch-dsi-lcd.htm
178 + */
179 +static const struct drm_display_mode ws_panel_11_9_mode = {
180 + .clock = 50000,
181 + .hdisplay = 320,
182 + .hsync_start = 320 + 60,
183 + .hsync_end = 320 + 60 + 60,
184 + .htotal = 320 + 60 + 60 + 120,
185 + .vdisplay = 1480,
186 + .vsync_start = 1480 + 60,
187 + .vsync_end = 1480 + 60 + 60,
188 + .vtotal = 1480 + 60 + 60 + 60,
189 +};
190 +
191 +static struct ws_panel *panel_to_ts(struct drm_panel *panel)
192 +{
193 + return container_of(panel, struct ws_panel, base);
194 +}
195 +
196 +static void ws_panel_i2c_write(struct ws_panel *ts, u8 reg, u8 val)
197 +{
198 + int ret;
199 +
200 + ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
201 + if (ret)
202 + dev_err(&ts->i2c->dev, "I2C write failed: %d\n", ret);
203 +}
204 +
205 +static int ws_panel_disable(struct drm_panel *panel)
206 +{
207 + struct ws_panel *ts = panel_to_ts(panel);
208 +
209 + ws_panel_i2c_write(ts, 0xad, 0x00);
210 +
211 + return 0;
212 +}
213 +
214 +static int ws_panel_unprepare(struct drm_panel *panel)
215 +{
216 + return 0;
217 +}
218 +
219 +static int ws_panel_prepare(struct drm_panel *panel)
220 +{
221 + return 0;
222 +}
223 +
224 +static int ws_panel_enable(struct drm_panel *panel)
225 +{
226 + struct ws_panel *ts = panel_to_ts(panel);
227 +
228 + ws_panel_i2c_write(ts, 0xad, 0x01);
229 +
230 + return 0;
231 +}
232 +
233 +static int ws_panel_get_modes(struct drm_panel *panel,
234 + struct drm_connector *connector)
235 +{
236 + static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
237 + struct ws_panel *ts = panel_to_ts(panel);
238 + struct drm_display_mode *mode;
239 +
240 + mode = drm_mode_duplicate(connector->dev, ts->mode);
241 + if (!mode) {
242 + dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
243 + ts->mode->hdisplay,
244 + ts->mode->vdisplay,
245 + drm_mode_vrefresh(ts->mode));
246 + }
247 +
248 + mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
249 +
250 + drm_mode_set_name(mode);
251 +
252 + drm_mode_probed_add(connector, mode);
253 +
254 + connector->display_info.bpc = 8;
255 + connector->display_info.width_mm = 154;
256 + connector->display_info.height_mm = 86;
257 + drm_display_info_set_bus_formats(&connector->display_info,
258 + &bus_format, 1);
259 +
260 + /*
261 + * TODO: Remove once all drm drivers call
262 + * drm_connector_set_orientation_from_panel()
263 + */
264 + drm_connector_set_panel_orientation(connector, ts->orientation);
265 +
266 + return 1;
267 +}
268 +
269 +static enum drm_panel_orientation ws_panel_get_orientation(struct drm_panel *panel)
270 +{
271 + struct ws_panel *ts = panel_to_ts(panel);
272 +
273 + return ts->orientation;
274 +}
275 +
276 +static const struct drm_panel_funcs ws_panel_funcs = {
277 + .disable = ws_panel_disable,
278 + .unprepare = ws_panel_unprepare,
279 + .prepare = ws_panel_prepare,
280 + .enable = ws_panel_enable,
281 + .get_modes = ws_panel_get_modes,
282 + .get_orientation = ws_panel_get_orientation,
283 +};
284 +
285 +static int ws_panel_bl_update_status(struct backlight_device *bl)
286 +{
287 + struct ws_panel *ts = bl_get_data(bl);
288 +
289 + ws_panel_i2c_write(ts, 0xab, 0xff - backlight_get_brightness(bl));
290 + ws_panel_i2c_write(ts, 0xaa, 0x01);
291 +
292 + return 0;
293 +}
294 +
295 +static const struct backlight_ops ws_panel_bl_ops = {
296 + .update_status = ws_panel_bl_update_status,
297 +};
298 +
299 +static struct backlight_device *
300 +ws_panel_create_backlight(struct ws_panel *ts)
301 +{
302 + struct device *dev = ts->base.dev;
303 + const struct backlight_properties props = {
304 + .type = BACKLIGHT_RAW,
305 + .brightness = 255,
306 + .max_brightness = 255,
307 + };
308 +
309 + return devm_backlight_device_register(dev, dev_name(dev), dev, ts,
310 + &ws_panel_bl_ops, &props);
311 +}
312 +
313 +static int ws_panel_probe(struct i2c_client *i2c,
314 + const struct i2c_device_id *id)
315 +{
316 + struct device *dev = &i2c->dev;
317 + struct ws_panel *ts;
318 + struct device_node *endpoint, *dsi_host_node;
319 + struct mipi_dsi_host *host;
320 + struct mipi_dsi_device_info info = {
321 + .type = WS_DSI_DRIVER_NAME,
322 + .channel = 0,
323 + .node = NULL,
324 + };
325 + int ret;
326 +
327 + ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
328 + if (!ts)
329 + return -ENOMEM;
330 +
331 + ts->mode = of_device_get_match_data(dev);
332 + if (!ts->mode)
333 + return -EINVAL;
334 +
335 + i2c_set_clientdata(i2c, ts);
336 +
337 + ts->i2c = i2c;
338 +
339 + ws_panel_i2c_write(ts, 0xc0, 0x01);
340 + ws_panel_i2c_write(ts, 0xc2, 0x01);
341 + ws_panel_i2c_write(ts, 0xac, 0x01);
342 +
343 + ret = of_drm_get_panel_orientation(dev->of_node, &ts->orientation);
344 + if (ret) {
345 + dev_err(dev, "%pOF: failed to get orientation %d\n", dev->of_node, ret);
346 + return ret;
347 + }
348 +
349 + /* Look up the DSI host. It needs to probe before we do. */
350 + endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
351 + if (!endpoint)
352 + return -ENODEV;
353 +
354 + dsi_host_node = of_graph_get_remote_port_parent(endpoint);
355 + if (!dsi_host_node)
356 + goto error;
357 +
358 + host = of_find_mipi_dsi_host_by_node(dsi_host_node);
359 + of_node_put(dsi_host_node);
360 + if (!host) {
361 + of_node_put(endpoint);
362 + return -EPROBE_DEFER;
363 + }
364 +
365 + info.node = of_graph_get_remote_port(endpoint);
366 + if (!info.node)
367 + goto error;
368 +
369 + of_node_put(endpoint);
370 +
371 + ts->dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
372 + if (IS_ERR(ts->dsi)) {
373 + dev_err(dev, "DSI device registration failed: %ld\n",
374 + PTR_ERR(ts->dsi));
375 + return PTR_ERR(ts->dsi);
376 + }
377 +
378 + drm_panel_init(&ts->base, dev, &ws_panel_funcs,
379 + DRM_MODE_CONNECTOR_DSI);
380 +
381 + ts->base.backlight = ws_panel_create_backlight(ts);
382 + if (IS_ERR(ts->base.backlight)) {
383 + ret = PTR_ERR(ts->base.backlight);
384 + dev_err(dev, "Failed to create backlight: %d\n", ret);
385 + return ret;
386 + }
387 +
388 + /* This appears last, as it's what will unblock the DSI host
389 + * driver's component bind function.
390 + */
391 + drm_panel_add(&ts->base);
392 +
393 + ts->dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
394 + MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
395 + MIPI_DSI_MODE_LPM);
396 + ts->dsi->format = MIPI_DSI_FMT_RGB888;
397 + ts->dsi->lanes = 2;
398 +
399 + ret = devm_mipi_dsi_attach(dev, ts->dsi);
400 +
401 + if (ret)
402 + dev_err(dev, "failed to attach dsi to host: %d\n", ret);
403 +
404 + return 0;
405 +
406 +error:
407 + of_node_put(endpoint);
408 + return -ENODEV;
409 +}
410 +
411 +static void ws_panel_remove(struct i2c_client *i2c)
412 +{
413 + struct ws_panel *ts = i2c_get_clientdata(i2c);
414 +
415 + drm_panel_remove(&ts->base);
416 +}
417 +
418 +static const struct of_device_id ws_panel_of_ids[] = {
419 + {
420 + .compatible = "waveshare,2.8inch-panel",
421 + .data = &ws_panel_2_8_mode,
422 + }, {
423 + .compatible = "waveshare,3.4inch-panel",
424 + .data = &ws_panel_3_4_mode,
425 + }, {
426 + .compatible = "waveshare,4.0inch-panel",
427 + .data = &ws_panel_4_0_mode,
428 + }, {
429 + .compatible = "waveshare,7.0inch-c-panel",
430 + .data = &ws_panel_7_0_c_mode,
431 + }, {
432 + .compatible = "waveshare,7.9inch-panel",
433 + .data = &ws_panel_7_9_mode,
434 + }, {
435 + .compatible = "waveshare,8.0inch-panel",
436 + .data = &ws_panel_10_1_mode,
437 + }, {
438 + .compatible = "waveshare,10.1inch-panel",
439 + .data = &ws_panel_10_1_mode,
440 + }, {
441 + .compatible = "waveshare,11.9inch-panel",
442 + .data = &ws_panel_11_9_mode,
443 + }, {
444 + /* sentinel */
445 + }
446 +};
447 +MODULE_DEVICE_TABLE(of, ws_panel_of_ids);
448 +
449 +static struct i2c_driver ws_panel_driver = {
450 + .driver = {
451 + .name = "ws_touchscreen",
452 + .of_match_table = ws_panel_of_ids,
453 + },
454 + .probe = ws_panel_probe,
455 + .remove = ws_panel_remove,
456 +};
457 +module_i2c_driver(ws_panel_driver);
458 +
459 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
460 +MODULE_DESCRIPTION("Waveshare DSI panel driver");
461 +MODULE_LICENSE("GPL");