Merge drm/drm-next into drm-misc-next
[sfrench/cifs-2.6.git] / drivers / gpu / drm / panel / panel-sharp-lq101r1sx01.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2014 NVIDIA Corporation
4  */
5
6 #include <linux/backlight.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/regulator/consumer.h>
12
13 #include <video/mipi_display.h>
14
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_device.h>
17 #include <drm/drm_mipi_dsi.h>
18 #include <drm/drm_panel.h>
19
20 struct sharp_panel {
21         struct drm_panel base;
22         /* the datasheet refers to them as DSI-LINK1 and DSI-LINK2 */
23         struct mipi_dsi_device *link1;
24         struct mipi_dsi_device *link2;
25
26         struct backlight_device *backlight;
27         struct regulator *supply;
28
29         bool prepared;
30         bool enabled;
31
32         const struct drm_display_mode *mode;
33 };
34
35 static inline struct sharp_panel *to_sharp_panel(struct drm_panel *panel)
36 {
37         return container_of(panel, struct sharp_panel, base);
38 }
39
40 static void sharp_wait_frames(struct sharp_panel *sharp, unsigned int frames)
41 {
42         unsigned int refresh = drm_mode_vrefresh(sharp->mode);
43
44         if (WARN_ON(frames > refresh))
45                 return;
46
47         msleep(1000 / (refresh / frames));
48 }
49
50 static int sharp_panel_write(struct sharp_panel *sharp, u16 offset, u8 value)
51 {
52         u8 payload[3] = { offset >> 8, offset & 0xff, value };
53         struct mipi_dsi_device *dsi = sharp->link1;
54         ssize_t err;
55
56         err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
57         if (err < 0) {
58                 dev_err(&dsi->dev, "failed to write %02x to %04x: %zd\n",
59                         value, offset, err);
60                 return err;
61         }
62
63         err = mipi_dsi_dcs_nop(dsi);
64         if (err < 0) {
65                 dev_err(&dsi->dev, "failed to send DCS nop: %zd\n", err);
66                 return err;
67         }
68
69         usleep_range(10, 20);
70
71         return 0;
72 }
73
74 static __maybe_unused int sharp_panel_read(struct sharp_panel *sharp,
75                                            u16 offset, u8 *value)
76 {
77         ssize_t err;
78
79         cpu_to_be16s(&offset);
80
81         err = mipi_dsi_generic_read(sharp->link1, &offset, sizeof(offset),
82                                     value, sizeof(*value));
83         if (err < 0)
84                 dev_err(&sharp->link1->dev, "failed to read from %04x: %zd\n",
85                         offset, err);
86
87         return err;
88 }
89
90 static int sharp_panel_disable(struct drm_panel *panel)
91 {
92         struct sharp_panel *sharp = to_sharp_panel(panel);
93
94         if (!sharp->enabled)
95                 return 0;
96
97         backlight_disable(sharp->backlight);
98
99         sharp->enabled = false;
100
101         return 0;
102 }
103
104 static int sharp_panel_unprepare(struct drm_panel *panel)
105 {
106         struct sharp_panel *sharp = to_sharp_panel(panel);
107         int err;
108
109         if (!sharp->prepared)
110                 return 0;
111
112         sharp_wait_frames(sharp, 4);
113
114         err = mipi_dsi_dcs_set_display_off(sharp->link1);
115         if (err < 0)
116                 dev_err(panel->dev, "failed to set display off: %d\n", err);
117
118         err = mipi_dsi_dcs_enter_sleep_mode(sharp->link1);
119         if (err < 0)
120                 dev_err(panel->dev, "failed to enter sleep mode: %d\n", err);
121
122         msleep(120);
123
124         regulator_disable(sharp->supply);
125
126         sharp->prepared = false;
127
128         return 0;
129 }
130
131 static int sharp_setup_symmetrical_split(struct mipi_dsi_device *left,
132                                          struct mipi_dsi_device *right,
133                                          const struct drm_display_mode *mode)
134 {
135         int err;
136
137         err = mipi_dsi_dcs_set_column_address(left, 0, mode->hdisplay / 2 - 1);
138         if (err < 0) {
139                 dev_err(&left->dev, "failed to set column address: %d\n", err);
140                 return err;
141         }
142
143         err = mipi_dsi_dcs_set_page_address(left, 0, mode->vdisplay - 1);
144         if (err < 0) {
145                 dev_err(&left->dev, "failed to set page address: %d\n", err);
146                 return err;
147         }
148
149         err = mipi_dsi_dcs_set_column_address(right, mode->hdisplay / 2,
150                                               mode->hdisplay - 1);
151         if (err < 0) {
152                 dev_err(&right->dev, "failed to set column address: %d\n", err);
153                 return err;
154         }
155
156         err = mipi_dsi_dcs_set_page_address(right, 0, mode->vdisplay - 1);
157         if (err < 0) {
158                 dev_err(&right->dev, "failed to set page address: %d\n", err);
159                 return err;
160         }
161
162         return 0;
163 }
164
165 static int sharp_panel_prepare(struct drm_panel *panel)
166 {
167         struct sharp_panel *sharp = to_sharp_panel(panel);
168         u8 format = MIPI_DCS_PIXEL_FMT_24BIT;
169         int err;
170
171         if (sharp->prepared)
172                 return 0;
173
174         err = regulator_enable(sharp->supply);
175         if (err < 0)
176                 return err;
177
178         /*
179          * According to the datasheet, the panel needs around 10 ms to fully
180          * power up. At least another 120 ms is required before exiting sleep
181          * mode to make sure the panel is ready. Throw in another 20 ms for
182          * good measure.
183          */
184         msleep(150);
185
186         err = mipi_dsi_dcs_exit_sleep_mode(sharp->link1);
187         if (err < 0) {
188                 dev_err(panel->dev, "failed to exit sleep mode: %d\n", err);
189                 goto poweroff;
190         }
191
192         /*
193          * The MIPI DCS specification mandates this delay only between the
194          * exit_sleep_mode and enter_sleep_mode commands, so it isn't strictly
195          * necessary here.
196          */
197         /*
198         msleep(120);
199         */
200
201         /* set left-right mode */
202         err = sharp_panel_write(sharp, 0x1000, 0x2a);
203         if (err < 0) {
204                 dev_err(panel->dev, "failed to set left-right mode: %d\n", err);
205                 goto poweroff;
206         }
207
208         /* enable command mode */
209         err = sharp_panel_write(sharp, 0x1001, 0x01);
210         if (err < 0) {
211                 dev_err(panel->dev, "failed to enable command mode: %d\n", err);
212                 goto poweroff;
213         }
214
215         err = mipi_dsi_dcs_set_pixel_format(sharp->link1, format);
216         if (err < 0) {
217                 dev_err(panel->dev, "failed to set pixel format: %d\n", err);
218                 goto poweroff;
219         }
220
221         /*
222          * TODO: The device supports both left-right and even-odd split
223          * configurations, but this driver currently supports only the left-
224          * right split. To support a different mode a mechanism needs to be
225          * put in place to communicate the configuration back to the DSI host
226          * controller.
227          */
228         err = sharp_setup_symmetrical_split(sharp->link1, sharp->link2,
229                                             sharp->mode);
230         if (err < 0) {
231                 dev_err(panel->dev, "failed to set up symmetrical split: %d\n",
232                         err);
233                 goto poweroff;
234         }
235
236         err = mipi_dsi_dcs_set_display_on(sharp->link1);
237         if (err < 0) {
238                 dev_err(panel->dev, "failed to set display on: %d\n", err);
239                 goto poweroff;
240         }
241
242         sharp->prepared = true;
243
244         /* wait for 6 frames before continuing */
245         sharp_wait_frames(sharp, 6);
246
247         return 0;
248
249 poweroff:
250         regulator_disable(sharp->supply);
251         return err;
252 }
253
254 static int sharp_panel_enable(struct drm_panel *panel)
255 {
256         struct sharp_panel *sharp = to_sharp_panel(panel);
257
258         if (sharp->enabled)
259                 return 0;
260
261         backlight_enable(sharp->backlight);
262
263         sharp->enabled = true;
264
265         return 0;
266 }
267
268 static const struct drm_display_mode default_mode = {
269         .clock = 278000,
270         .hdisplay = 2560,
271         .hsync_start = 2560 + 128,
272         .hsync_end = 2560 + 128 + 64,
273         .htotal = 2560 + 128 + 64 + 64,
274         .vdisplay = 1600,
275         .vsync_start = 1600 + 4,
276         .vsync_end = 1600 + 4 + 8,
277         .vtotal = 1600 + 4 + 8 + 32,
278         .vrefresh = 60,
279 };
280
281 static int sharp_panel_get_modes(struct drm_panel *panel)
282 {
283         struct drm_display_mode *mode;
284
285         mode = drm_mode_duplicate(panel->drm, &default_mode);
286         if (!mode) {
287                 dev_err(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
288                         default_mode.hdisplay, default_mode.vdisplay,
289                         default_mode.vrefresh);
290                 return -ENOMEM;
291         }
292
293         drm_mode_set_name(mode);
294
295         drm_mode_probed_add(panel->connector, mode);
296
297         panel->connector->display_info.width_mm = 217;
298         panel->connector->display_info.height_mm = 136;
299
300         return 1;
301 }
302
303 static const struct drm_panel_funcs sharp_panel_funcs = {
304         .disable = sharp_panel_disable,
305         .unprepare = sharp_panel_unprepare,
306         .prepare = sharp_panel_prepare,
307         .enable = sharp_panel_enable,
308         .get_modes = sharp_panel_get_modes,
309 };
310
311 static const struct of_device_id sharp_of_match[] = {
312         { .compatible = "sharp,lq101r1sx01", },
313         { }
314 };
315 MODULE_DEVICE_TABLE(of, sharp_of_match);
316
317 static int sharp_panel_add(struct sharp_panel *sharp)
318 {
319         struct device *dev = &sharp->link1->dev;
320
321         sharp->mode = &default_mode;
322
323         sharp->supply = devm_regulator_get(&sharp->link1->dev, "power");
324         if (IS_ERR(sharp->supply))
325                 return PTR_ERR(sharp->supply);
326
327         sharp->backlight = devm_of_find_backlight(dev);
328
329         if (IS_ERR(sharp->backlight))
330                 return PTR_ERR(sharp->backlight);
331
332         drm_panel_init(&sharp->base, &sharp->link1->dev, &sharp_panel_funcs,
333                        DRM_MODE_CONNECTOR_DSI);
334
335         return drm_panel_add(&sharp->base);
336 }
337
338 static void sharp_panel_del(struct sharp_panel *sharp)
339 {
340         if (sharp->base.dev)
341                 drm_panel_remove(&sharp->base);
342
343         if (sharp->link2)
344                 put_device(&sharp->link2->dev);
345 }
346
347 static int sharp_panel_probe(struct mipi_dsi_device *dsi)
348 {
349         struct mipi_dsi_device *secondary = NULL;
350         struct sharp_panel *sharp;
351         struct device_node *np;
352         int err;
353
354         dsi->lanes = 4;
355         dsi->format = MIPI_DSI_FMT_RGB888;
356         dsi->mode_flags = MIPI_DSI_MODE_LPM;
357
358         /* Find DSI-LINK1 */
359         np = of_parse_phandle(dsi->dev.of_node, "link2", 0);
360         if (np) {
361                 secondary = of_find_mipi_dsi_device_by_node(np);
362                 of_node_put(np);
363
364                 if (!secondary)
365                         return -EPROBE_DEFER;
366         }
367
368         /* register a panel for only the DSI-LINK1 interface */
369         if (secondary) {
370                 sharp = devm_kzalloc(&dsi->dev, sizeof(*sharp), GFP_KERNEL);
371                 if (!sharp) {
372                         put_device(&secondary->dev);
373                         return -ENOMEM;
374                 }
375
376                 mipi_dsi_set_drvdata(dsi, sharp);
377
378                 sharp->link2 = secondary;
379                 sharp->link1 = dsi;
380
381                 err = sharp_panel_add(sharp);
382                 if (err < 0) {
383                         put_device(&secondary->dev);
384                         return err;
385                 }
386         }
387
388         err = mipi_dsi_attach(dsi);
389         if (err < 0) {
390                 if (secondary)
391                         sharp_panel_del(sharp);
392
393                 return err;
394         }
395
396         return 0;
397 }
398
399 static int sharp_panel_remove(struct mipi_dsi_device *dsi)
400 {
401         struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
402         int err;
403
404         /* only detach from host for the DSI-LINK2 interface */
405         if (!sharp) {
406                 mipi_dsi_detach(dsi);
407                 return 0;
408         }
409
410         err = sharp_panel_disable(&sharp->base);
411         if (err < 0)
412                 dev_err(&dsi->dev, "failed to disable panel: %d\n", err);
413
414         err = mipi_dsi_detach(dsi);
415         if (err < 0)
416                 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
417
418         sharp_panel_del(sharp);
419
420         return 0;
421 }
422
423 static void sharp_panel_shutdown(struct mipi_dsi_device *dsi)
424 {
425         struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
426
427         /* nothing to do for DSI-LINK2 */
428         if (!sharp)
429                 return;
430
431         sharp_panel_disable(&sharp->base);
432 }
433
434 static struct mipi_dsi_driver sharp_panel_driver = {
435         .driver = {
436                 .name = "panel-sharp-lq101r1sx01",
437                 .of_match_table = sharp_of_match,
438         },
439         .probe = sharp_panel_probe,
440         .remove = sharp_panel_remove,
441         .shutdown = sharp_panel_shutdown,
442 };
443 module_mipi_dsi_driver(sharp_panel_driver);
444
445 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
446 MODULE_DESCRIPTION("Sharp LQ101R1SX01 panel driver");
447 MODULE_LICENSE("GPL v2");