1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright © 2023 Raspberry Pi Ltd
5 * Based on panel-raspberrypi-touchscreen by Broadcom
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/media-bus-format.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/of_graph.h>
20 #include <drm/drm_crtc.h>
21 #include <drm/drm_device.h>
22 #include <drm/drm_mipi_dsi.h>
23 #include <drm/drm_panel.h>
25 #define WS_DSI_DRIVER_NAME "ws-ts-dsi"
28 struct drm_panel base;
29 struct mipi_dsi_device *dsi;
30 struct i2c_client *i2c;
31 const struct drm_display_mode *mode;
32 enum drm_panel_orientation orientation;
36 * https://www.waveshare.com/product/raspberry-pi/displays/2.8inch-dsi-lcd.htm
38 static const struct drm_display_mode ws_panel_2_8_mode = {
41 .hsync_start = 480 + 150,
42 .hsync_end = 480 + 150 + 50,
43 .htotal = 480 + 150 + 50 + 150,
45 .vsync_start = 640 + 150,
46 .vsync_end = 640 + 150 + 50,
47 .vtotal = 640 + 150 + 50 + 150,
50 /* 3.4inch 800x800 Round
51 * https://www.waveshare.com/product/displays/lcd-oled/3.4inch-dsi-lcd-c.htm
53 static const struct drm_display_mode ws_panel_3_4_mode = {
56 .hsync_start = 800 + 32,
57 .hsync_end = 800 + 32 + 6,
58 .htotal = 800 + 32 + 6 + 120,
60 .vsync_start = 800 + 8,
61 .vsync_end = 800 + 8 + 4,
62 .vtotal = 800 + 8 + 4 + 16,
66 * https://www.waveshare.com/product/raspberry-pi/displays/4inch-dsi-lcd.htm
68 static const struct drm_display_mode ws_panel_4_0_mode = {
71 .hsync_start = 480 + 150,
72 .hsync_end = 480 + 150 + 100,
73 .htotal = 480 + 150 + 100 + 150,
75 .vsync_start = 800 + 20,
76 .vsync_end = 800 + 20 + 100,
77 .vtotal = 800 + 20 + 100 + 20,
81 * https://www.waveshare.com/product/raspberry-pi/displays/lcd-oled/7inch-dsi-lcd-c-with-case-a.htm
83 static const struct drm_display_mode ws_panel_7_0_c_mode = {
86 .hsync_start = 1024 + 100,
87 .hsync_end = 1024 + 100 + 100,
88 .htotal = 1024 + 100 + 100 + 100,
90 .vsync_start = 600 + 10,
91 .vsync_end = 600 + 10 + 10,
92 .vtotal = 600 + 10 + 10 + 10,
96 * https://www.waveshare.com/product/raspberry-pi/displays/7.9inch-dsi-lcd.htm
98 static const struct drm_display_mode ws_panel_7_9_mode = {
101 .hsync_start = 400 + 40,
102 .hsync_end = 400 + 40 + 30,
103 .htotal = 400 + 40 + 30 + 40,
105 .vsync_start = 1280 + 20,
106 .vsync_end = 1280 + 20 + 10,
107 .vtotal = 1280 + 20 + 10 + 20,
110 /* 8.0inch or 10.1inch 1280x800
111 * https://www.waveshare.com/product/raspberry-pi/displays/8inch-dsi-lcd-c.htm
112 * https://www.waveshare.com/product/raspberry-pi/displays/10.1inch-dsi-lcd-c.htm
114 static const struct drm_display_mode ws_panel_10_1_mode = {
117 .hsync_start = 1280 + 40,
118 .hsync_end = 1280 + 40 + 20,
119 .htotal = 1280 + 40 + 20 + 40,
121 .vsync_start = 800 + 40,
122 .vsync_end = 800 + 40 + 48,
123 .vtotal = 800 + 40 + 48 + 40,
127 * https://www.waveshare.com/product/raspberry-pi/displays/11.9inch-dsi-lcd.htm
129 static const struct drm_display_mode ws_panel_11_9_mode = {
132 .hsync_start = 320 + 60,
133 .hsync_end = 320 + 60 + 60,
134 .htotal = 320 + 60 + 60 + 120,
136 .vsync_start = 1480 + 60,
137 .vsync_end = 1480 + 60 + 60,
138 .vtotal = 1480 + 60 + 60 + 60,
141 static struct ws_panel *panel_to_ts(struct drm_panel *panel)
143 return container_of(panel, struct ws_panel, base);
146 static void ws_panel_i2c_write(struct ws_panel *ts, u8 reg, u8 val)
150 ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
152 dev_err(&ts->i2c->dev, "I2C write failed: %d\n", ret);
155 static int ws_panel_disable(struct drm_panel *panel)
157 struct ws_panel *ts = panel_to_ts(panel);
159 ws_panel_i2c_write(ts, 0xad, 0x00);
164 static int ws_panel_unprepare(struct drm_panel *panel)
169 static int ws_panel_prepare(struct drm_panel *panel)
174 static int ws_panel_enable(struct drm_panel *panel)
176 struct ws_panel *ts = panel_to_ts(panel);
178 ws_panel_i2c_write(ts, 0xad, 0x01);
183 static int ws_panel_get_modes(struct drm_panel *panel,
184 struct drm_connector *connector)
186 static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
187 struct ws_panel *ts = panel_to_ts(panel);
188 struct drm_display_mode *mode;
190 mode = drm_mode_duplicate(connector->dev, ts->mode);
192 dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
195 drm_mode_vrefresh(ts->mode));
198 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
200 drm_mode_set_name(mode);
202 drm_mode_probed_add(connector, mode);
204 connector->display_info.bpc = 8;
205 connector->display_info.width_mm = 154;
206 connector->display_info.height_mm = 86;
207 drm_display_info_set_bus_formats(&connector->display_info,
211 * TODO: Remove once all drm drivers call
212 * drm_connector_set_orientation_from_panel()
214 drm_connector_set_panel_orientation(connector, ts->orientation);
219 static enum drm_panel_orientation ws_panel_get_orientation(struct drm_panel *panel)
221 struct ws_panel *ts = panel_to_ts(panel);
223 return ts->orientation;
226 static const struct drm_panel_funcs ws_panel_funcs = {
227 .disable = ws_panel_disable,
228 .unprepare = ws_panel_unprepare,
229 .prepare = ws_panel_prepare,
230 .enable = ws_panel_enable,
231 .get_modes = ws_panel_get_modes,
232 .get_orientation = ws_panel_get_orientation,
235 static int ws_panel_bl_update_status(struct backlight_device *bl)
237 struct ws_panel *ts = bl_get_data(bl);
239 ws_panel_i2c_write(ts, 0xab, 0xff - backlight_get_brightness(bl));
240 ws_panel_i2c_write(ts, 0xaa, 0x01);
245 static const struct backlight_ops ws_panel_bl_ops = {
246 .update_status = ws_panel_bl_update_status,
249 static struct backlight_device *
250 ws_panel_create_backlight(struct ws_panel *ts)
252 struct device *dev = ts->base.dev;
253 const struct backlight_properties props = {
254 .type = BACKLIGHT_RAW,
256 .max_brightness = 255,
259 return devm_backlight_device_register(dev, dev_name(dev), dev, ts,
260 &ws_panel_bl_ops, &props);
263 static int ws_panel_probe(struct i2c_client *i2c)
265 struct device *dev = &i2c->dev;
267 struct device_node *endpoint, *dsi_host_node;
268 struct mipi_dsi_host *host;
269 struct mipi_dsi_device_info info = {
270 .type = WS_DSI_DRIVER_NAME,
276 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
280 ts->mode = of_device_get_match_data(dev);
284 i2c_set_clientdata(i2c, ts);
288 ws_panel_i2c_write(ts, 0xc0, 0x01);
289 ws_panel_i2c_write(ts, 0xc2, 0x01);
290 ws_panel_i2c_write(ts, 0xac, 0x01);
292 ret = of_drm_get_panel_orientation(dev->of_node, &ts->orientation);
294 dev_err(dev, "%pOF: failed to get orientation %d\n", dev->of_node, ret);
298 /* Look up the DSI host. It needs to probe before we do. */
299 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
303 dsi_host_node = of_graph_get_remote_port_parent(endpoint);
307 host = of_find_mipi_dsi_host_by_node(dsi_host_node);
308 of_node_put(dsi_host_node);
310 of_node_put(endpoint);
311 return -EPROBE_DEFER;
314 info.node = of_graph_get_remote_port(endpoint);
318 of_node_put(endpoint);
320 ts->dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
321 if (IS_ERR(ts->dsi)) {
322 dev_err(dev, "DSI device registration failed: %ld\n",
324 return PTR_ERR(ts->dsi);
327 drm_panel_init(&ts->base, dev, &ws_panel_funcs,
328 DRM_MODE_CONNECTOR_DSI);
330 ts->base.backlight = ws_panel_create_backlight(ts);
331 if (IS_ERR(ts->base.backlight)) {
332 ret = PTR_ERR(ts->base.backlight);
333 dev_err(dev, "Failed to create backlight: %d\n", ret);
337 /* This appears last, as it's what will unblock the DSI host
338 * driver's component bind function.
340 drm_panel_add(&ts->base);
342 ts->dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
343 MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
345 ts->dsi->format = MIPI_DSI_FMT_RGB888;
348 ret = devm_mipi_dsi_attach(dev, ts->dsi);
351 dev_err(dev, "failed to attach dsi to host: %d\n", ret);
356 of_node_put(endpoint);
360 static void ws_panel_remove(struct i2c_client *i2c)
362 struct ws_panel *ts = i2c_get_clientdata(i2c);
364 drm_panel_remove(&ts->base);
367 static const struct of_device_id ws_panel_of_ids[] = {
369 .compatible = "waveshare,2.8inch-panel",
370 .data = &ws_panel_2_8_mode,
372 .compatible = "waveshare,3.4inch-panel",
373 .data = &ws_panel_3_4_mode,
375 .compatible = "waveshare,4.0inch-panel",
376 .data = &ws_panel_4_0_mode,
378 .compatible = "waveshare,7.0inch-c-panel",
379 .data = &ws_panel_7_0_c_mode,
381 .compatible = "waveshare,7.9inch-panel",
382 .data = &ws_panel_7_9_mode,
384 .compatible = "waveshare,8.0inch-panel",
385 .data = &ws_panel_10_1_mode,
387 .compatible = "waveshare,10.1inch-panel",
388 .data = &ws_panel_10_1_mode,
390 .compatible = "waveshare,11.9inch-panel",
391 .data = &ws_panel_11_9_mode,
396 MODULE_DEVICE_TABLE(of, ws_panel_of_ids);
398 static struct i2c_driver ws_panel_driver = {
400 .name = "ws_touchscreen",
401 .of_match_table = ws_panel_of_ids,
403 .probe = ws_panel_probe,
404 .remove = ws_panel_remove,
406 module_i2c_driver(ws_panel_driver);
408 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
409 MODULE_DESCRIPTION("Waveshare DSI panel driver");
410 MODULE_LICENSE("GPL");