dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / panel / panel-sharp-lq101r1sx01.c
blob02fc0f5423d40e585dd8d12002c2bfc98a5c4717
1 /*
2 * Copyright (C) 2014 NVIDIA Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/backlight.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/regulator/consumer.h>
15 #include <drm/drmP.h>
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_mipi_dsi.h>
18 #include <drm/drm_panel.h>
20 #include <video/mipi_display.h>
22 struct sharp_panel {
23 struct drm_panel base;
24 /* the datasheet refers to them as DSI-LINK1 and DSI-LINK2 */
25 struct mipi_dsi_device *link1;
26 struct mipi_dsi_device *link2;
28 struct backlight_device *backlight;
29 struct regulator *supply;
31 bool prepared;
32 bool enabled;
34 const struct drm_display_mode *mode;
37 static inline struct sharp_panel *to_sharp_panel(struct drm_panel *panel)
39 return container_of(panel, struct sharp_panel, base);
42 static void sharp_wait_frames(struct sharp_panel *sharp, unsigned int frames)
44 unsigned int refresh = drm_mode_vrefresh(sharp->mode);
46 if (WARN_ON(frames > refresh))
47 return;
49 msleep(1000 / (refresh / frames));
52 static int sharp_panel_write(struct sharp_panel *sharp, u16 offset, u8 value)
54 u8 payload[3] = { offset >> 8, offset & 0xff, value };
55 struct mipi_dsi_device *dsi = sharp->link1;
56 ssize_t err;
58 err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
59 if (err < 0) {
60 dev_err(&dsi->dev, "failed to write %02x to %04x: %zd\n",
61 value, offset, err);
62 return err;
65 err = mipi_dsi_dcs_nop(dsi);
66 if (err < 0) {
67 dev_err(&dsi->dev, "failed to send DCS nop: %zd\n", err);
68 return err;
71 usleep_range(10, 20);
73 return 0;
76 static __maybe_unused int sharp_panel_read(struct sharp_panel *sharp,
77 u16 offset, u8 *value)
79 ssize_t err;
81 cpu_to_be16s(&offset);
83 err = mipi_dsi_generic_read(sharp->link1, &offset, sizeof(offset),
84 value, sizeof(*value));
85 if (err < 0)
86 dev_err(&sharp->link1->dev, "failed to read from %04x: %zd\n",
87 offset, err);
89 return err;
92 static int sharp_panel_disable(struct drm_panel *panel)
94 struct sharp_panel *sharp = to_sharp_panel(panel);
96 if (!sharp->enabled)
97 return 0;
99 backlight_disable(sharp->backlight);
101 sharp->enabled = false;
103 return 0;
106 static int sharp_panel_unprepare(struct drm_panel *panel)
108 struct sharp_panel *sharp = to_sharp_panel(panel);
109 int err;
111 if (!sharp->prepared)
112 return 0;
114 sharp_wait_frames(sharp, 4);
116 err = mipi_dsi_dcs_set_display_off(sharp->link1);
117 if (err < 0)
118 dev_err(panel->dev, "failed to set display off: %d\n", err);
120 err = mipi_dsi_dcs_enter_sleep_mode(sharp->link1);
121 if (err < 0)
122 dev_err(panel->dev, "failed to enter sleep mode: %d\n", err);
124 msleep(120);
126 regulator_disable(sharp->supply);
128 sharp->prepared = false;
130 return 0;
133 static int sharp_setup_symmetrical_split(struct mipi_dsi_device *left,
134 struct mipi_dsi_device *right,
135 const struct drm_display_mode *mode)
137 int err;
139 err = mipi_dsi_dcs_set_column_address(left, 0, mode->hdisplay / 2 - 1);
140 if (err < 0) {
141 dev_err(&left->dev, "failed to set column address: %d\n", err);
142 return err;
145 err = mipi_dsi_dcs_set_page_address(left, 0, mode->vdisplay - 1);
146 if (err < 0) {
147 dev_err(&left->dev, "failed to set page address: %d\n", err);
148 return err;
151 err = mipi_dsi_dcs_set_column_address(right, mode->hdisplay / 2,
152 mode->hdisplay - 1);
153 if (err < 0) {
154 dev_err(&right->dev, "failed to set column address: %d\n", err);
155 return err;
158 err = mipi_dsi_dcs_set_page_address(right, 0, mode->vdisplay - 1);
159 if (err < 0) {
160 dev_err(&right->dev, "failed to set page address: %d\n", err);
161 return err;
164 return 0;
167 static int sharp_panel_prepare(struct drm_panel *panel)
169 struct sharp_panel *sharp = to_sharp_panel(panel);
170 u8 format = MIPI_DCS_PIXEL_FMT_24BIT;
171 int err;
173 if (sharp->prepared)
174 return 0;
176 err = regulator_enable(sharp->supply);
177 if (err < 0)
178 return err;
181 * According to the datasheet, the panel needs around 10 ms to fully
182 * power up. At least another 120 ms is required before exiting sleep
183 * mode to make sure the panel is ready. Throw in another 20 ms for
184 * good measure.
186 msleep(150);
188 err = mipi_dsi_dcs_exit_sleep_mode(sharp->link1);
189 if (err < 0) {
190 dev_err(panel->dev, "failed to exit sleep mode: %d\n", err);
191 goto poweroff;
195 * The MIPI DCS specification mandates this delay only between the
196 * exit_sleep_mode and enter_sleep_mode commands, so it isn't strictly
197 * necessary here.
200 msleep(120);
203 /* set left-right mode */
204 err = sharp_panel_write(sharp, 0x1000, 0x2a);
205 if (err < 0) {
206 dev_err(panel->dev, "failed to set left-right mode: %d\n", err);
207 goto poweroff;
210 /* enable command mode */
211 err = sharp_panel_write(sharp, 0x1001, 0x01);
212 if (err < 0) {
213 dev_err(panel->dev, "failed to enable command mode: %d\n", err);
214 goto poweroff;
217 err = mipi_dsi_dcs_set_pixel_format(sharp->link1, format);
218 if (err < 0) {
219 dev_err(panel->dev, "failed to set pixel format: %d\n", err);
220 goto poweroff;
224 * TODO: The device supports both left-right and even-odd split
225 * configurations, but this driver currently supports only the left-
226 * right split. To support a different mode a mechanism needs to be
227 * put in place to communicate the configuration back to the DSI host
228 * controller.
230 err = sharp_setup_symmetrical_split(sharp->link1, sharp->link2,
231 sharp->mode);
232 if (err < 0) {
233 dev_err(panel->dev, "failed to set up symmetrical split: %d\n",
234 err);
235 goto poweroff;
238 err = mipi_dsi_dcs_set_display_on(sharp->link1);
239 if (err < 0) {
240 dev_err(panel->dev, "failed to set display on: %d\n", err);
241 goto poweroff;
244 sharp->prepared = true;
246 /* wait for 6 frames before continuing */
247 sharp_wait_frames(sharp, 6);
249 return 0;
251 poweroff:
252 regulator_disable(sharp->supply);
253 return err;
256 static int sharp_panel_enable(struct drm_panel *panel)
258 struct sharp_panel *sharp = to_sharp_panel(panel);
260 if (sharp->enabled)
261 return 0;
263 backlight_enable(sharp->backlight);
265 sharp->enabled = true;
267 return 0;
270 static const struct drm_display_mode default_mode = {
271 .clock = 278000,
272 .hdisplay = 2560,
273 .hsync_start = 2560 + 128,
274 .hsync_end = 2560 + 128 + 64,
275 .htotal = 2560 + 128 + 64 + 64,
276 .vdisplay = 1600,
277 .vsync_start = 1600 + 4,
278 .vsync_end = 1600 + 4 + 8,
279 .vtotal = 1600 + 4 + 8 + 32,
280 .vrefresh = 60,
283 static int sharp_panel_get_modes(struct drm_panel *panel)
285 struct drm_display_mode *mode;
287 mode = drm_mode_duplicate(panel->drm, &default_mode);
288 if (!mode) {
289 dev_err(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
290 default_mode.hdisplay, default_mode.vdisplay,
291 default_mode.vrefresh);
292 return -ENOMEM;
295 drm_mode_set_name(mode);
297 drm_mode_probed_add(panel->connector, mode);
299 panel->connector->display_info.width_mm = 217;
300 panel->connector->display_info.height_mm = 136;
302 return 1;
305 static const struct drm_panel_funcs sharp_panel_funcs = {
306 .disable = sharp_panel_disable,
307 .unprepare = sharp_panel_unprepare,
308 .prepare = sharp_panel_prepare,
309 .enable = sharp_panel_enable,
310 .get_modes = sharp_panel_get_modes,
313 static const struct of_device_id sharp_of_match[] = {
314 { .compatible = "sharp,lq101r1sx01", },
317 MODULE_DEVICE_TABLE(of, sharp_of_match);
319 static int sharp_panel_add(struct sharp_panel *sharp)
321 struct device *dev = &sharp->link1->dev;
323 sharp->mode = &default_mode;
325 sharp->supply = devm_regulator_get(&sharp->link1->dev, "power");
326 if (IS_ERR(sharp->supply))
327 return PTR_ERR(sharp->supply);
329 sharp->backlight = devm_of_find_backlight(dev);
331 if (IS_ERR(sharp->backlight))
332 return PTR_ERR(sharp->backlight);
334 drm_panel_init(&sharp->base);
335 sharp->base.funcs = &sharp_panel_funcs;
336 sharp->base.dev = &sharp->link1->dev;
338 return drm_panel_add(&sharp->base);
341 static void sharp_panel_del(struct sharp_panel *sharp)
343 if (sharp->base.dev)
344 drm_panel_remove(&sharp->base);
346 if (sharp->link2)
347 put_device(&sharp->link2->dev);
350 static int sharp_panel_probe(struct mipi_dsi_device *dsi)
352 struct mipi_dsi_device *secondary = NULL;
353 struct sharp_panel *sharp;
354 struct device_node *np;
355 int err;
357 dsi->lanes = 4;
358 dsi->format = MIPI_DSI_FMT_RGB888;
359 dsi->mode_flags = MIPI_DSI_MODE_LPM;
361 /* Find DSI-LINK1 */
362 np = of_parse_phandle(dsi->dev.of_node, "link2", 0);
363 if (np) {
364 secondary = of_find_mipi_dsi_device_by_node(np);
365 of_node_put(np);
367 if (!secondary)
368 return -EPROBE_DEFER;
371 /* register a panel for only the DSI-LINK1 interface */
372 if (secondary) {
373 sharp = devm_kzalloc(&dsi->dev, sizeof(*sharp), GFP_KERNEL);
374 if (!sharp) {
375 put_device(&secondary->dev);
376 return -ENOMEM;
379 mipi_dsi_set_drvdata(dsi, sharp);
381 sharp->link2 = secondary;
382 sharp->link1 = dsi;
384 err = sharp_panel_add(sharp);
385 if (err < 0) {
386 put_device(&secondary->dev);
387 return err;
391 err = mipi_dsi_attach(dsi);
392 if (err < 0) {
393 if (secondary)
394 sharp_panel_del(sharp);
396 return err;
399 return 0;
402 static int sharp_panel_remove(struct mipi_dsi_device *dsi)
404 struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
405 int err;
407 /* only detach from host for the DSI-LINK2 interface */
408 if (!sharp) {
409 mipi_dsi_detach(dsi);
410 return 0;
413 err = sharp_panel_disable(&sharp->base);
414 if (err < 0)
415 dev_err(&dsi->dev, "failed to disable panel: %d\n", err);
417 err = mipi_dsi_detach(dsi);
418 if (err < 0)
419 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
421 sharp_panel_del(sharp);
423 return 0;
426 static void sharp_panel_shutdown(struct mipi_dsi_device *dsi)
428 struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
430 /* nothing to do for DSI-LINK2 */
431 if (!sharp)
432 return;
434 sharp_panel_disable(&sharp->base);
437 static struct mipi_dsi_driver sharp_panel_driver = {
438 .driver = {
439 .name = "panel-sharp-lq101r1sx01",
440 .of_match_table = sharp_of_match,
442 .probe = sharp_panel_probe,
443 .remove = sharp_panel_remove,
444 .shutdown = sharp_panel_shutdown,
446 module_mipi_dsi_driver(sharp_panel_driver);
448 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
449 MODULE_DESCRIPTION("Sharp LQ101R1SX01 panel driver");
450 MODULE_LICENSE("GPL v2");