drm/panel: Add panel driver for Waveshare DSI touchscreens
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / panel / panel-waveshare-dsi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright © 2023 Raspberry Pi Ltd
4  *
5  * Based on panel-raspberrypi-touchscreen by Broadcom
6  */
7
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/fb.h>
12 #include <linux/i2c.h>
13 #include <linux/media-bus-format.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/of_graph.h>
18 #include <linux/pm.h>
19
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>
24
25 #define WS_DSI_DRIVER_NAME "ws-ts-dsi"
26
27 struct ws_panel {
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;
33 };
34
35 /* 2.8inch 480x640
36  * https://www.waveshare.com/product/raspberry-pi/displays/2.8inch-dsi-lcd.htm
37  */
38 static const struct drm_display_mode ws_panel_2_8_mode = {
39         .clock = 50000,
40         .hdisplay = 480,
41         .hsync_start = 480 + 150,
42         .hsync_end = 480 + 150 + 50,
43         .htotal = 480 + 150 + 50 + 150,
44         .vdisplay = 640,
45         .vsync_start = 640 + 150,
46         .vsync_end = 640 + 150 + 50,
47         .vtotal = 640 + 150 + 50 + 150,
48 };
49
50 /* 3.4inch 800x800 Round
51  * https://www.waveshare.com/product/displays/lcd-oled/3.4inch-dsi-lcd-c.htm
52  */
53 static const struct drm_display_mode ws_panel_3_4_mode = {
54         .clock = 50000,
55         .hdisplay = 800,
56         .hsync_start = 800 + 32,
57         .hsync_end = 800 + 32 + 6,
58         .htotal = 800 + 32 + 6 + 120,
59         .vdisplay = 800,
60         .vsync_start = 800 + 8,
61         .vsync_end = 800 + 8 + 4,
62         .vtotal = 800 + 8 + 4 + 16,
63 };
64
65 /* 4.0inch 480x800
66  * https://www.waveshare.com/product/raspberry-pi/displays/4inch-dsi-lcd.htm
67  */
68 static const struct drm_display_mode ws_panel_4_0_mode = {
69         .clock = 50000,
70         .hdisplay = 480,
71         .hsync_start = 480 + 150,
72         .hsync_end = 480 + 150 + 100,
73         .htotal = 480 + 150 + 100 + 150,
74         .vdisplay = 800,
75         .vsync_start = 800 + 20,
76         .vsync_end = 800 + 20 + 100,
77         .vtotal = 800 + 20 + 100 + 20,
78 };
79
80 /* 7.0inch C 1024x600
81  * https://www.waveshare.com/product/raspberry-pi/displays/lcd-oled/7inch-dsi-lcd-c-with-case-a.htm
82  */
83 static const struct drm_display_mode ws_panel_7_0_c_mode = {
84         .clock = 50000,
85         .hdisplay = 1024,
86         .hsync_start = 1024 + 100,
87         .hsync_end = 1024 + 100 + 100,
88         .htotal = 1024 + 100 + 100 + 100,
89         .vdisplay = 600,
90         .vsync_start = 600 + 10,
91         .vsync_end = 600 + 10 + 10,
92         .vtotal = 600 + 10 + 10 + 10,
93 };
94
95 /* 7.9inch 400x1280
96  * https://www.waveshare.com/product/raspberry-pi/displays/7.9inch-dsi-lcd.htm
97  */
98 static const struct drm_display_mode ws_panel_7_9_mode = {
99         .clock = 50000,
100         .hdisplay = 400,
101         .hsync_start = 400 + 40,
102         .hsync_end = 400 + 40 + 30,
103         .htotal = 400 + 40 + 30 + 40,
104         .vdisplay = 1280,
105         .vsync_start = 1280 + 20,
106         .vsync_end = 1280 + 20 + 10,
107         .vtotal = 1280 + 20 + 10 + 20,
108 };
109
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
113  */
114 static const struct drm_display_mode ws_panel_10_1_mode = {
115         .clock = 76800,
116         .hdisplay = 1280,
117         .hsync_start = 1280 + 40,
118         .hsync_end = 1280 + 40 + 20,
119         .htotal = 1280 + 40 + 20 + 40,
120         .vdisplay = 800,
121         .vsync_start = 800 + 40,
122         .vsync_end = 800 + 40 + 48,
123         .vtotal = 800 + 40 + 48 + 40,
124 };
125
126 /* 11.9inch 320x1480
127  * https://www.waveshare.com/product/raspberry-pi/displays/11.9inch-dsi-lcd.htm
128  */
129 static const struct drm_display_mode ws_panel_11_9_mode = {
130         .clock = 50000,
131         .hdisplay = 320,
132         .hsync_start = 320 + 60,
133         .hsync_end = 320 + 60 + 60,
134         .htotal = 320 + 60 + 60 + 120,
135         .vdisplay = 1480,
136         .vsync_start = 1480 + 60,
137         .vsync_end = 1480 + 60 + 60,
138         .vtotal = 1480 + 60 + 60 + 60,
139 };
140
141 static struct ws_panel *panel_to_ts(struct drm_panel *panel)
142 {
143         return container_of(panel, struct ws_panel, base);
144 }
145
146 static void ws_panel_i2c_write(struct ws_panel *ts, u8 reg, u8 val)
147 {
148         int ret;
149
150         ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
151         if (ret)
152                 dev_err(&ts->i2c->dev, "I2C write failed: %d\n", ret);
153 }
154
155 static int ws_panel_disable(struct drm_panel *panel)
156 {
157         struct ws_panel *ts = panel_to_ts(panel);
158
159         ws_panel_i2c_write(ts, 0xad, 0x00);
160
161         return 0;
162 }
163
164 static int ws_panel_unprepare(struct drm_panel *panel)
165 {
166         return 0;
167 }
168
169 static int ws_panel_prepare(struct drm_panel *panel)
170 {
171         return 0;
172 }
173
174 static int ws_panel_enable(struct drm_panel *panel)
175 {
176         struct ws_panel *ts = panel_to_ts(panel);
177
178         ws_panel_i2c_write(ts, 0xad, 0x01);
179
180         return 0;
181 }
182
183 static int ws_panel_get_modes(struct drm_panel *panel,
184                               struct drm_connector *connector)
185 {
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;
189
190         mode = drm_mode_duplicate(connector->dev, ts->mode);
191         if (!mode) {
192                 dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
193                         ts->mode->hdisplay,
194                         ts->mode->vdisplay,
195                         drm_mode_vrefresh(ts->mode));
196         }
197
198         mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
199
200         drm_mode_set_name(mode);
201
202         drm_mode_probed_add(connector, mode);
203
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,
208                                          &bus_format, 1);
209
210         /*
211          * TODO: Remove once all drm drivers call
212          * drm_connector_set_orientation_from_panel()
213          */
214         drm_connector_set_panel_orientation(connector, ts->orientation);
215
216         return 1;
217 }
218
219 static enum drm_panel_orientation ws_panel_get_orientation(struct drm_panel *panel)
220 {
221         struct ws_panel *ts = panel_to_ts(panel);
222
223         return ts->orientation;
224 }
225
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,
233 };
234
235 static int ws_panel_bl_update_status(struct backlight_device *bl)
236 {
237         struct ws_panel *ts = bl_get_data(bl);
238
239         ws_panel_i2c_write(ts, 0xab, 0xff - backlight_get_brightness(bl));
240         ws_panel_i2c_write(ts, 0xaa, 0x01);
241
242         return 0;
243 }
244
245 static const struct backlight_ops ws_panel_bl_ops = {
246         .update_status = ws_panel_bl_update_status,
247 };
248
249 static struct backlight_device *
250 ws_panel_create_backlight(struct ws_panel *ts)
251 {
252         struct device *dev = ts->base.dev;
253         const struct backlight_properties props = {
254                 .type = BACKLIGHT_RAW,
255                 .brightness = 255,
256                 .max_brightness = 255,
257         };
258
259         return devm_backlight_device_register(dev, dev_name(dev), dev, ts,
260                                               &ws_panel_bl_ops, &props);
261 }
262
263 static int ws_panel_probe(struct i2c_client *i2c)
264 {
265         struct device *dev = &i2c->dev;
266         struct ws_panel *ts;
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,
271                 .channel = 0,
272                 .node = NULL,
273         };
274         int ret;
275
276         ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
277         if (!ts)
278                 return -ENOMEM;
279
280         ts->mode = of_device_get_match_data(dev);
281         if (!ts->mode)
282                 return -EINVAL;
283
284         i2c_set_clientdata(i2c, ts);
285
286         ts->i2c = i2c;
287
288         ws_panel_i2c_write(ts, 0xc0, 0x01);
289         ws_panel_i2c_write(ts, 0xc2, 0x01);
290         ws_panel_i2c_write(ts, 0xac, 0x01);
291
292         ret = of_drm_get_panel_orientation(dev->of_node, &ts->orientation);
293         if (ret) {
294                 dev_err(dev, "%pOF: failed to get orientation %d\n", dev->of_node, ret);
295                 return ret;
296         }
297
298         /* Look up the DSI host.  It needs to probe before we do. */
299         endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
300         if (!endpoint)
301                 return -ENODEV;
302
303         dsi_host_node = of_graph_get_remote_port_parent(endpoint);
304         if (!dsi_host_node)
305                 goto error;
306
307         host = of_find_mipi_dsi_host_by_node(dsi_host_node);
308         of_node_put(dsi_host_node);
309         if (!host) {
310                 of_node_put(endpoint);
311                 return -EPROBE_DEFER;
312         }
313
314         info.node = of_graph_get_remote_port(endpoint);
315         if (!info.node)
316                 goto error;
317
318         of_node_put(endpoint);
319
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",
323                         PTR_ERR(ts->dsi));
324                 return PTR_ERR(ts->dsi);
325         }
326
327         drm_panel_init(&ts->base, dev, &ws_panel_funcs,
328                        DRM_MODE_CONNECTOR_DSI);
329
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);
334                 return ret;
335         }
336
337         /* This appears last, as it's what will unblock the DSI host
338          * driver's component bind function.
339          */
340         drm_panel_add(&ts->base);
341
342         ts->dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
343                            MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
344                            MIPI_DSI_MODE_LPM);
345         ts->dsi->format = MIPI_DSI_FMT_RGB888;
346         ts->dsi->lanes = 2;
347
348         ret = devm_mipi_dsi_attach(dev, ts->dsi);
349
350         if (ret)
351                 dev_err(dev, "failed to attach dsi to host: %d\n", ret);
352
353         return 0;
354
355 error:
356         of_node_put(endpoint);
357         return -ENODEV;
358 }
359
360 static void ws_panel_remove(struct i2c_client *i2c)
361 {
362         struct ws_panel *ts = i2c_get_clientdata(i2c);
363
364         drm_panel_remove(&ts->base);
365 }
366
367 static const struct of_device_id ws_panel_of_ids[] = {
368         {
369                 .compatible = "waveshare,2.8inch-panel",
370                 .data = &ws_panel_2_8_mode,
371         }, {
372                 .compatible = "waveshare,3.4inch-panel",
373                 .data = &ws_panel_3_4_mode,
374         }, {
375                 .compatible = "waveshare,4.0inch-panel",
376                 .data = &ws_panel_4_0_mode,
377         }, {
378                 .compatible = "waveshare,7.0inch-c-panel",
379                 .data = &ws_panel_7_0_c_mode,
380         }, {
381                 .compatible = "waveshare,7.9inch-panel",
382                 .data = &ws_panel_7_9_mode,
383         }, {
384                 .compatible = "waveshare,8.0inch-panel",
385                 .data = &ws_panel_10_1_mode,
386         }, {
387                 .compatible = "waveshare,10.1inch-panel",
388                 .data = &ws_panel_10_1_mode,
389         }, {
390                 .compatible = "waveshare,11.9inch-panel",
391                 .data = &ws_panel_11_9_mode,
392         }, {
393                 /* sentinel */
394         }
395 };
396 MODULE_DEVICE_TABLE(of, ws_panel_of_ids);
397
398 static struct i2c_driver ws_panel_driver = {
399         .driver = {
400                 .name = "ws_touchscreen",
401                 .of_match_table = ws_panel_of_ids,
402         },
403         .probe = ws_panel_probe,
404         .remove = ws_panel_remove,
405 };
406 module_i2c_driver(ws_panel_driver);
407
408 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
409 MODULE_DESCRIPTION("Waveshare DSI panel driver");
410 MODULE_LICENSE("GPL");