blob: 69d30c2639cc8634bea3ddb9121d05a1dbfbc250 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Thierry Reding1976dbc2014-08-01 14:26:35 +02002/*
3 * Copyright (C) 2014 NVIDIA Corporation
Thierry Reding1976dbc2014-08-01 14:26:35 +02004 */
5
6#include <linux/backlight.h>
7#include <linux/gpio/consumer.h>
8#include <linux/module.h>
9#include <linux/of.h>
10#include <linux/regulator/consumer.h>
11
12#include <drm/drmP.h>
13#include <drm/drm_crtc.h>
14#include <drm/drm_mipi_dsi.h>
15#include <drm/drm_panel.h>
16
17#include <video/mipi_display.h>
18
Thierry Reding1976dbc2014-08-01 14:26:35 +020019struct sharp_panel {
20 struct drm_panel base;
21 /* the datasheet refers to them as DSI-LINK1 and DSI-LINK2 */
22 struct mipi_dsi_device *link1;
23 struct mipi_dsi_device *link2;
24
25 struct backlight_device *backlight;
26 struct regulator *supply;
27
28 bool prepared;
29 bool enabled;
30
31 const struct drm_display_mode *mode;
32};
33
34static inline struct sharp_panel *to_sharp_panel(struct drm_panel *panel)
35{
36 return container_of(panel, struct sharp_panel, base);
37}
38
Thierry Reding45696192014-12-08 15:05:48 +010039static void sharp_wait_frames(struct sharp_panel *sharp, unsigned int frames)
40{
41 unsigned int refresh = drm_mode_vrefresh(sharp->mode);
42
43 if (WARN_ON(frames > refresh))
44 return;
45
46 msleep(1000 / (refresh / frames));
47}
48
Thierry Reding1976dbc2014-08-01 14:26:35 +020049static int sharp_panel_write(struct sharp_panel *sharp, u16 offset, u8 value)
50{
51 u8 payload[3] = { offset >> 8, offset & 0xff, value };
52 struct mipi_dsi_device *dsi = sharp->link1;
53 ssize_t err;
54
55 err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
56 if (err < 0) {
57 dev_err(&dsi->dev, "failed to write %02x to %04x: %zd\n",
58 value, offset, err);
59 return err;
60 }
61
62 err = mipi_dsi_dcs_nop(dsi);
63 if (err < 0) {
64 dev_err(&dsi->dev, "failed to send DCS nop: %zd\n", err);
65 return err;
66 }
67
68 usleep_range(10, 20);
69
70 return 0;
71}
72
73static __maybe_unused int sharp_panel_read(struct sharp_panel *sharp,
74 u16 offset, u8 *value)
75{
76 ssize_t err;
77
78 cpu_to_be16s(&offset);
79
80 err = mipi_dsi_generic_read(sharp->link1, &offset, sizeof(offset),
81 value, sizeof(*value));
82 if (err < 0)
83 dev_err(&sharp->link1->dev, "failed to read from %04x: %zd\n",
84 offset, err);
85
86 return err;
87}
88
89static int sharp_panel_disable(struct drm_panel *panel)
90{
91 struct sharp_panel *sharp = to_sharp_panel(panel);
92
93 if (!sharp->enabled)
94 return 0;
95
Meghana Madhyasthad593bfd2018-01-24 16:39:27 +000096 backlight_disable(sharp->backlight);
Thierry Reding1976dbc2014-08-01 14:26:35 +020097
98 sharp->enabled = false;
99
100 return 0;
101}
102
103static int sharp_panel_unprepare(struct drm_panel *panel)
104{
105 struct sharp_panel *sharp = to_sharp_panel(panel);
106 int err;
107
108 if (!sharp->prepared)
109 return 0;
110
Thierry Redingee920bc2014-12-08 15:38:17 +0100111 sharp_wait_frames(sharp, 4);
112
Thierry Reding1976dbc2014-08-01 14:26:35 +0200113 err = mipi_dsi_dcs_set_display_off(sharp->link1);
114 if (err < 0)
115 dev_err(panel->dev, "failed to set display off: %d\n", err);
116
117 err = mipi_dsi_dcs_enter_sleep_mode(sharp->link1);
118 if (err < 0)
119 dev_err(panel->dev, "failed to enter sleep mode: %d\n", err);
120
121 msleep(120);
122
123 regulator_disable(sharp->supply);
124
125 sharp->prepared = false;
126
127 return 0;
128}
129
130static int sharp_setup_symmetrical_split(struct mipi_dsi_device *left,
131 struct mipi_dsi_device *right,
132 const struct drm_display_mode *mode)
133{
134 int err;
135
136 err = mipi_dsi_dcs_set_column_address(left, 0, mode->hdisplay / 2 - 1);
137 if (err < 0) {
138 dev_err(&left->dev, "failed to set column address: %d\n", err);
139 return err;
140 }
141
142 err = mipi_dsi_dcs_set_page_address(left, 0, mode->vdisplay - 1);
143 if (err < 0) {
144 dev_err(&left->dev, "failed to set page address: %d\n", err);
145 return err;
146 }
147
148 err = mipi_dsi_dcs_set_column_address(right, mode->hdisplay / 2,
149 mode->hdisplay - 1);
150 if (err < 0) {
151 dev_err(&right->dev, "failed to set column address: %d\n", err);
152 return err;
153 }
154
155 err = mipi_dsi_dcs_set_page_address(right, 0, mode->vdisplay - 1);
156 if (err < 0) {
157 dev_err(&right->dev, "failed to set page address: %d\n", err);
158 return err;
159 }
160
161 return 0;
162}
163
164static int sharp_panel_prepare(struct drm_panel *panel)
165{
166 struct sharp_panel *sharp = to_sharp_panel(panel);
167 u8 format = MIPI_DCS_PIXEL_FMT_24BIT;
168 int err;
169
170 if (sharp->prepared)
171 return 0;
172
173 err = regulator_enable(sharp->supply);
174 if (err < 0)
175 return err;
176
Thierry Redingee920bc2014-12-08 15:38:17 +0100177 /*
178 * According to the datasheet, the panel needs around 10 ms to fully
179 * power up. At least another 120 ms is required before exiting sleep
180 * mode to make sure the panel is ready. Throw in another 20 ms for
181 * good measure.
182 */
183 msleep(150);
Thierry Reding1976dbc2014-08-01 14:26:35 +0200184
185 err = mipi_dsi_dcs_exit_sleep_mode(sharp->link1);
186 if (err < 0) {
187 dev_err(panel->dev, "failed to exit sleep mode: %d\n", err);
188 goto poweroff;
189 }
190
191 /*
192 * The MIPI DCS specification mandates this delay only between the
193 * exit_sleep_mode and enter_sleep_mode commands, so it isn't strictly
194 * necessary here.
195 */
196 /*
197 msleep(120);
198 */
199
200 /* set left-right mode */
201 err = sharp_panel_write(sharp, 0x1000, 0x2a);
202 if (err < 0) {
203 dev_err(panel->dev, "failed to set left-right mode: %d\n", err);
204 goto poweroff;
205 }
206
207 /* enable command mode */
208 err = sharp_panel_write(sharp, 0x1001, 0x01);
209 if (err < 0) {
210 dev_err(panel->dev, "failed to enable command mode: %d\n", err);
211 goto poweroff;
212 }
213
214 err = mipi_dsi_dcs_set_pixel_format(sharp->link1, format);
215 if (err < 0) {
216 dev_err(panel->dev, "failed to set pixel format: %d\n", err);
217 goto poweroff;
218 }
219
220 /*
221 * TODO: The device supports both left-right and even-odd split
222 * configurations, but this driver currently supports only the left-
223 * right split. To support a different mode a mechanism needs to be
224 * put in place to communicate the configuration back to the DSI host
225 * controller.
226 */
227 err = sharp_setup_symmetrical_split(sharp->link1, sharp->link2,
228 sharp->mode);
229 if (err < 0) {
230 dev_err(panel->dev, "failed to set up symmetrical split: %d\n",
231 err);
232 goto poweroff;
233 }
234
235 err = mipi_dsi_dcs_set_display_on(sharp->link1);
236 if (err < 0) {
237 dev_err(panel->dev, "failed to set display on: %d\n", err);
238 goto poweroff;
239 }
240
241 sharp->prepared = true;
242
Thierry Reding45696192014-12-08 15:05:48 +0100243 /* wait for 6 frames before continuing */
244 sharp_wait_frames(sharp, 6);
245
Thierry Reding1976dbc2014-08-01 14:26:35 +0200246 return 0;
247
248poweroff:
249 regulator_disable(sharp->supply);
250 return err;
251}
252
253static int sharp_panel_enable(struct drm_panel *panel)
254{
255 struct sharp_panel *sharp = to_sharp_panel(panel);
256
257 if (sharp->enabled)
258 return 0;
259
Meghana Madhyasthad593bfd2018-01-24 16:39:27 +0000260 backlight_enable(sharp->backlight);
Thierry Reding1976dbc2014-08-01 14:26:35 +0200261
262 sharp->enabled = true;
263
264 return 0;
265}
266
267static const struct drm_display_mode default_mode = {
268 .clock = 278000,
269 .hdisplay = 2560,
270 .hsync_start = 2560 + 128,
271 .hsync_end = 2560 + 128 + 64,
272 .htotal = 2560 + 128 + 64 + 64,
273 .vdisplay = 1600,
274 .vsync_start = 1600 + 4,
275 .vsync_end = 1600 + 4 + 8,
276 .vtotal = 1600 + 4 + 8 + 32,
277 .vrefresh = 60,
278};
279
280static int sharp_panel_get_modes(struct drm_panel *panel)
281{
282 struct drm_display_mode *mode;
283
284 mode = drm_mode_duplicate(panel->drm, &default_mode);
285 if (!mode) {
286 dev_err(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
287 default_mode.hdisplay, default_mode.vdisplay,
288 default_mode.vrefresh);
289 return -ENOMEM;
290 }
291
292 drm_mode_set_name(mode);
293
294 drm_mode_probed_add(panel->connector, mode);
295
296 panel->connector->display_info.width_mm = 217;
297 panel->connector->display_info.height_mm = 136;
298
299 return 1;
300}
301
302static const struct drm_panel_funcs sharp_panel_funcs = {
303 .disable = sharp_panel_disable,
304 .unprepare = sharp_panel_unprepare,
305 .prepare = sharp_panel_prepare,
306 .enable = sharp_panel_enable,
307 .get_modes = sharp_panel_get_modes,
308};
309
310static const struct of_device_id sharp_of_match[] = {
311 { .compatible = "sharp,lq101r1sx01", },
312 { }
313};
314MODULE_DEVICE_TABLE(of, sharp_of_match);
315
316static int sharp_panel_add(struct sharp_panel *sharp)
317{
Meghana Madhyasthac69f9452018-01-24 16:40:51 +0000318 struct device *dev = &sharp->link1->dev;
Thierry Reding1976dbc2014-08-01 14:26:35 +0200319
320 sharp->mode = &default_mode;
321
322 sharp->supply = devm_regulator_get(&sharp->link1->dev, "power");
323 if (IS_ERR(sharp->supply))
324 return PTR_ERR(sharp->supply);
325
Meghana Madhyasthac69f9452018-01-24 16:40:51 +0000326 sharp->backlight = devm_of_find_backlight(dev);
Thierry Reding1976dbc2014-08-01 14:26:35 +0200327
Meghana Madhyasthac69f9452018-01-24 16:40:51 +0000328 if (IS_ERR(sharp->backlight))
329 return PTR_ERR(sharp->backlight);
Thierry Reding1976dbc2014-08-01 14:26:35 +0200330
331 drm_panel_init(&sharp->base);
332 sharp->base.funcs = &sharp_panel_funcs;
333 sharp->base.dev = &sharp->link1->dev;
334
Meghana Madhyasthac69f9452018-01-24 16:40:51 +0000335 return drm_panel_add(&sharp->base);
Thierry Reding1976dbc2014-08-01 14:26:35 +0200336}
337
338static void sharp_panel_del(struct sharp_panel *sharp)
339{
340 if (sharp->base.dev)
341 drm_panel_remove(&sharp->base);
342
Thierry Reding1976dbc2014-08-01 14:26:35 +0200343 if (sharp->link2)
344 put_device(&sharp->link2->dev);
345}
346
347static 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
399static 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
Thierry Reding1976dbc2014-08-01 14:26:35 +0200418 sharp_panel_del(sharp);
419
420 return 0;
421}
422
423static 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
434static 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};
443module_mipi_dsi_driver(sharp_panel_driver);
444
445MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
446MODULE_DESCRIPTION("Sharp LQ101R1SX01 panel driver");
447MODULE_LICENSE("GPL v2");