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
7 Waveshare sell a range of DSI panels of varying sizes, all
8 using a common MCU to control the panel and backlight.
10 Add a panel driver that supports these panels.
12 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
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
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.
26 +config DRM_PANEL_WAVESHARE_TOUCHSCREEN
27 + tristate "Waveshare touchscreen panels"
28 + depends on DRM_MIPI_DSI
30 + depends on BACKLIGHT_CLASS_DEVICE
32 + Say Y here if you want to enable support for the Waveshare
33 + DSI Touchscreens. To compile this driver as a module,
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
49 +++ b/drivers/gpu/drm/panel/panel-waveshare-dsi.c
51 +// SPDX-License-Identifier: GPL-2.0
53 + * Copyright © 2023 Raspberry Pi Ltd
55 + * Based on panel-raspberrypi-touchscreen by Broadcom
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>
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>
75 +#define WS_DSI_DRIVER_NAME "ws-ts-dsi"
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;
86 + * https://www.waveshare.com/product/raspberry-pi/displays/2.8inch-dsi-lcd.htm
88 +static const struct drm_display_mode ws_panel_2_8_mode = {
91 + .hsync_start = 480 + 150,
92 + .hsync_end = 480 + 150 + 50,
93 + .htotal = 480 + 150 + 50 + 150,
95 + .vsync_start = 640 + 150,
96 + .vsync_end = 640 + 150 + 50,
97 + .vtotal = 640 + 150 + 50 + 150,
100 +/* 3.4inch 800x800 Round
101 + * https://www.waveshare.com/product/displays/lcd-oled/3.4inch-dsi-lcd-c.htm
103 +static const struct drm_display_mode ws_panel_3_4_mode = {
106 + .hsync_start = 800 + 32,
107 + .hsync_end = 800 + 32 + 6,
108 + .htotal = 800 + 32 + 6 + 120,
110 + .vsync_start = 800 + 8,
111 + .vsync_end = 800 + 8 + 4,
112 + .vtotal = 800 + 8 + 4 + 16,
116 + * https://www.waveshare.com/product/raspberry-pi/displays/4inch-dsi-lcd.htm
118 +static const struct drm_display_mode ws_panel_4_0_mode = {
121 + .hsync_start = 480 + 150,
122 + .hsync_end = 480 + 150 + 100,
123 + .htotal = 480 + 150 + 100 + 150,
125 + .vsync_start = 800 + 20,
126 + .vsync_end = 800 + 20 + 100,
127 + .vtotal = 800 + 20 + 100 + 20,
130 +/* 7.0inch C 1024x600
131 + * https://www.waveshare.com/product/raspberry-pi/displays/lcd-oled/7inch-dsi-lcd-c-with-case-a.htm
133 +static const struct drm_display_mode ws_panel_7_0_c_mode = {
136 + .hsync_start = 1024 + 100,
137 + .hsync_end = 1024 + 100 + 100,
138 + .htotal = 1024 + 100 + 100 + 100,
140 + .vsync_start = 600 + 10,
141 + .vsync_end = 600 + 10 + 10,
142 + .vtotal = 600 + 10 + 10 + 10,
146 + * https://www.waveshare.com/product/raspberry-pi/displays/7.9inch-dsi-lcd.htm
148 +static const struct drm_display_mode ws_panel_7_9_mode = {
151 + .hsync_start = 400 + 40,
152 + .hsync_end = 400 + 40 + 30,
153 + .htotal = 400 + 40 + 30 + 40,
155 + .vsync_start = 1280 + 20,
156 + .vsync_end = 1280 + 20 + 10,
157 + .vtotal = 1280 + 20 + 10 + 20,
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
164 +static const struct drm_display_mode ws_panel_10_1_mode = {
167 + .hsync_start = 1280 + 40,
168 + .hsync_end = 1280 + 40 + 20,
169 + .htotal = 1280 + 40 + 20 + 40,
171 + .vsync_start = 800 + 40,
172 + .vsync_end = 800 + 40 + 48,
173 + .vtotal = 800 + 40 + 48 + 40,
176 +/* 11.9inch 320x1480
177 + * https://www.waveshare.com/product/raspberry-pi/displays/11.9inch-dsi-lcd.htm
179 +static const struct drm_display_mode ws_panel_11_9_mode = {
182 + .hsync_start = 320 + 60,
183 + .hsync_end = 320 + 60 + 60,
184 + .htotal = 320 + 60 + 60 + 120,
186 + .vsync_start = 1480 + 60,
187 + .vsync_end = 1480 + 60 + 60,
188 + .vtotal = 1480 + 60 + 60 + 60,
191 +static struct ws_panel *panel_to_ts(struct drm_panel *panel)
193 + return container_of(panel, struct ws_panel, base);
196 +static void ws_panel_i2c_write(struct ws_panel *ts, u8 reg, u8 val)
200 + ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
202 + dev_err(&ts->i2c->dev, "I2C write failed: %d\n", ret);
205 +static int ws_panel_disable(struct drm_panel *panel)
207 + struct ws_panel *ts = panel_to_ts(panel);
209 + ws_panel_i2c_write(ts, 0xad, 0x00);
214 +static int ws_panel_unprepare(struct drm_panel *panel)
219 +static int ws_panel_prepare(struct drm_panel *panel)
224 +static int ws_panel_enable(struct drm_panel *panel)
226 + struct ws_panel *ts = panel_to_ts(panel);
228 + ws_panel_i2c_write(ts, 0xad, 0x01);
233 +static int ws_panel_get_modes(struct drm_panel *panel,
234 + struct drm_connector *connector)
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;
240 + mode = drm_mode_duplicate(connector->dev, ts->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));
248 + mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
250 + drm_mode_set_name(mode);
252 + drm_mode_probed_add(connector, mode);
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,
261 + * TODO: Remove once all drm drivers call
262 + * drm_connector_set_orientation_from_panel()
264 + drm_connector_set_panel_orientation(connector, ts->orientation);
269 +static enum drm_panel_orientation ws_panel_get_orientation(struct drm_panel *panel)
271 + struct ws_panel *ts = panel_to_ts(panel);
273 + return ts->orientation;
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,
285 +static int ws_panel_bl_update_status(struct backlight_device *bl)
287 + struct ws_panel *ts = bl_get_data(bl);
289 + ws_panel_i2c_write(ts, 0xab, 0xff - backlight_get_brightness(bl));
290 + ws_panel_i2c_write(ts, 0xaa, 0x01);
295 +static const struct backlight_ops ws_panel_bl_ops = {
296 + .update_status = ws_panel_bl_update_status,
299 +static struct backlight_device *
300 +ws_panel_create_backlight(struct ws_panel *ts)
302 + struct device *dev = ts->base.dev;
303 + const struct backlight_properties props = {
304 + .type = BACKLIGHT_RAW,
306 + .max_brightness = 255,
309 + return devm_backlight_device_register(dev, dev_name(dev), dev, ts,
310 + &ws_panel_bl_ops, &props);
313 +static int ws_panel_probe(struct i2c_client *i2c,
314 + const struct i2c_device_id *id)
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,
327 + ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
331 + ts->mode = of_device_get_match_data(dev);
335 + i2c_set_clientdata(i2c, ts);
339 + ws_panel_i2c_write(ts, 0xc0, 0x01);
340 + ws_panel_i2c_write(ts, 0xc2, 0x01);
341 + ws_panel_i2c_write(ts, 0xac, 0x01);
343 + ret = of_drm_get_panel_orientation(dev->of_node, &ts->orientation);
345 + dev_err(dev, "%pOF: failed to get orientation %d\n", dev->of_node, ret);
349 + /* Look up the DSI host. It needs to probe before we do. */
350 + endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
354 + dsi_host_node = of_graph_get_remote_port_parent(endpoint);
355 + if (!dsi_host_node)
358 + host = of_find_mipi_dsi_host_by_node(dsi_host_node);
359 + of_node_put(dsi_host_node);
361 + of_node_put(endpoint);
362 + return -EPROBE_DEFER;
365 + info.node = of_graph_get_remote_port(endpoint);
369 + of_node_put(endpoint);
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",
375 + return PTR_ERR(ts->dsi);
378 + drm_panel_init(&ts->base, dev, &ws_panel_funcs,
379 + DRM_MODE_CONNECTOR_DSI);
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);
388 + /* This appears last, as it's what will unblock the DSI host
389 + * driver's component bind function.
391 + drm_panel_add(&ts->base);
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;
399 + ret = devm_mipi_dsi_attach(dev, ts->dsi);
402 + dev_err(dev, "failed to attach dsi to host: %d\n", ret);
407 + of_node_put(endpoint);
411 +static void ws_panel_remove(struct i2c_client *i2c)
413 + struct ws_panel *ts = i2c_get_clientdata(i2c);
415 + drm_panel_remove(&ts->base);
418 +static const struct of_device_id ws_panel_of_ids[] = {
420 + .compatible = "waveshare,2.8inch-panel",
421 + .data = &ws_panel_2_8_mode,
423 + .compatible = "waveshare,3.4inch-panel",
424 + .data = &ws_panel_3_4_mode,
426 + .compatible = "waveshare,4.0inch-panel",
427 + .data = &ws_panel_4_0_mode,
429 + .compatible = "waveshare,7.0inch-c-panel",
430 + .data = &ws_panel_7_0_c_mode,
432 + .compatible = "waveshare,7.9inch-panel",
433 + .data = &ws_panel_7_9_mode,
435 + .compatible = "waveshare,8.0inch-panel",
436 + .data = &ws_panel_10_1_mode,
438 + .compatible = "waveshare,10.1inch-panel",
439 + .data = &ws_panel_10_1_mode,
441 + .compatible = "waveshare,11.9inch-panel",
442 + .data = &ws_panel_11_9_mode,
447 +MODULE_DEVICE_TABLE(of, ws_panel_of_ids);
449 +static struct i2c_driver ws_panel_driver = {
451 + .name = "ws_touchscreen",
452 + .of_match_table = ws_panel_of_ids,
454 + .probe = ws_panel_probe,
455 + .remove = ws_panel_remove,
457 +module_i2c_driver(ws_panel_driver);
459 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
460 +MODULE_DESCRIPTION("Waveshare DSI panel driver");
461 +MODULE_LICENSE("GPL");