1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 NVIDIA Corporation
6 #include <linux/backlight.h>
7 #include <linux/gpio/consumer.h>
8 #include <linux/module.h>
10 #include <linux/regulator/consumer.h>
13 #include <drm/drm_crtc.h>
14 #include <drm/drm_mipi_dsi.h>
15 #include <drm/drm_panel.h>
17 #include <video/mipi_display.h>
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;
25 struct backlight_device *backlight;
26 struct regulator *supply;
31 const struct drm_display_mode *mode;
34 static inline struct sharp_panel *to_sharp_panel(struct drm_panel *panel)
36 return container_of(panel, struct sharp_panel, base);
39 static void sharp_wait_frames(struct sharp_panel *sharp, unsigned int frames)
41 unsigned int refresh = drm_mode_vrefresh(sharp->mode);
43 if (WARN_ON(frames > refresh))
46 msleep(1000 / (refresh / frames));
49 static int sharp_panel_write(struct sharp_panel *sharp, u16 offset, u8 value)
51 u8 payload[3] = { offset >> 8, offset & 0xff, value };
52 struct mipi_dsi_device *dsi = sharp->link1;
55 err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
57 dev_err(&dsi->dev, "failed to write %02x to %04x: %zd\n",
62 err = mipi_dsi_dcs_nop(dsi);
64 dev_err(&dsi->dev, "failed to send DCS nop: %zd\n", err);
73 static __maybe_unused int sharp_panel_read(struct sharp_panel *sharp,
74 u16 offset, u8 *value)
78 cpu_to_be16s(&offset);
80 err = mipi_dsi_generic_read(sharp->link1, &offset, sizeof(offset),
81 value, sizeof(*value));
83 dev_err(&sharp->link1->dev, "failed to read from %04x: %zd\n",
89 static int sharp_panel_disable(struct drm_panel *panel)
91 struct sharp_panel *sharp = to_sharp_panel(panel);
96 backlight_disable(sharp->backlight);
98 sharp->enabled = false;
103 static int sharp_panel_unprepare(struct drm_panel *panel)
105 struct sharp_panel *sharp = to_sharp_panel(panel);
108 if (!sharp->prepared)
111 sharp_wait_frames(sharp, 4);
113 err = mipi_dsi_dcs_set_display_off(sharp->link1);
115 dev_err(panel->dev, "failed to set display off: %d\n", err);
117 err = mipi_dsi_dcs_enter_sleep_mode(sharp->link1);
119 dev_err(panel->dev, "failed to enter sleep mode: %d\n", err);
123 regulator_disable(sharp->supply);
125 sharp->prepared = false;
130 static int sharp_setup_symmetrical_split(struct mipi_dsi_device *left,
131 struct mipi_dsi_device *right,
132 const struct drm_display_mode *mode)
136 err = mipi_dsi_dcs_set_column_address(left, 0, mode->hdisplay / 2 - 1);
138 dev_err(&left->dev, "failed to set column address: %d\n", err);
142 err = mipi_dsi_dcs_set_page_address(left, 0, mode->vdisplay - 1);
144 dev_err(&left->dev, "failed to set page address: %d\n", err);
148 err = mipi_dsi_dcs_set_column_address(right, mode->hdisplay / 2,
151 dev_err(&right->dev, "failed to set column address: %d\n", err);
155 err = mipi_dsi_dcs_set_page_address(right, 0, mode->vdisplay - 1);
157 dev_err(&right->dev, "failed to set page address: %d\n", err);
164 static int sharp_panel_prepare(struct drm_panel *panel)
166 struct sharp_panel *sharp = to_sharp_panel(panel);
167 u8 format = MIPI_DCS_PIXEL_FMT_24BIT;
173 err = regulator_enable(sharp->supply);
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
185 err = mipi_dsi_dcs_exit_sleep_mode(sharp->link1);
187 dev_err(panel->dev, "failed to exit sleep mode: %d\n", err);
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
200 /* set left-right mode */
201 err = sharp_panel_write(sharp, 0x1000, 0x2a);
203 dev_err(panel->dev, "failed to set left-right mode: %d\n", err);
207 /* enable command mode */
208 err = sharp_panel_write(sharp, 0x1001, 0x01);
210 dev_err(panel->dev, "failed to enable command mode: %d\n", err);
214 err = mipi_dsi_dcs_set_pixel_format(sharp->link1, format);
216 dev_err(panel->dev, "failed to set pixel format: %d\n", err);
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
227 err = sharp_setup_symmetrical_split(sharp->link1, sharp->link2,
230 dev_err(panel->dev, "failed to set up symmetrical split: %d\n",
235 err = mipi_dsi_dcs_set_display_on(sharp->link1);
237 dev_err(panel->dev, "failed to set display on: %d\n", err);
241 sharp->prepared = true;
243 /* wait for 6 frames before continuing */
244 sharp_wait_frames(sharp, 6);
249 regulator_disable(sharp->supply);
253 static int sharp_panel_enable(struct drm_panel *panel)
255 struct sharp_panel *sharp = to_sharp_panel(panel);
260 backlight_enable(sharp->backlight);
262 sharp->enabled = true;
267 static const struct drm_display_mode default_mode = {
270 .hsync_start = 2560 + 128,
271 .hsync_end = 2560 + 128 + 64,
272 .htotal = 2560 + 128 + 64 + 64,
274 .vsync_start = 1600 + 4,
275 .vsync_end = 1600 + 4 + 8,
276 .vtotal = 1600 + 4 + 8 + 32,
280 static int sharp_panel_get_modes(struct drm_panel *panel)
282 struct drm_display_mode *mode;
284 mode = drm_mode_duplicate(panel->drm, &default_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);
292 drm_mode_set_name(mode);
294 drm_mode_probed_add(panel->connector, mode);
296 panel->connector->display_info.width_mm = 217;
297 panel->connector->display_info.height_mm = 136;
302 static 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,
310 static const struct of_device_id sharp_of_match[] = {
311 { .compatible = "sharp,lq101r1sx01", },
314 MODULE_DEVICE_TABLE(of, sharp_of_match);
316 static int sharp_panel_add(struct sharp_panel *sharp)
318 struct device *dev = &sharp->link1->dev;
320 sharp->mode = &default_mode;
322 sharp->supply = devm_regulator_get(&sharp->link1->dev, "power");
323 if (IS_ERR(sharp->supply))
324 return PTR_ERR(sharp->supply);
326 sharp->backlight = devm_of_find_backlight(dev);
328 if (IS_ERR(sharp->backlight))
329 return PTR_ERR(sharp->backlight);
331 drm_panel_init(&sharp->base);
332 sharp->base.funcs = &sharp_panel_funcs;
333 sharp->base.dev = &sharp->link1->dev;
335 return drm_panel_add(&sharp->base);
338 static void sharp_panel_del(struct sharp_panel *sharp)
341 drm_panel_remove(&sharp->base);
344 put_device(&sharp->link2->dev);
347 static int sharp_panel_probe(struct mipi_dsi_device *dsi)
349 struct mipi_dsi_device *secondary = NULL;
350 struct sharp_panel *sharp;
351 struct device_node *np;
355 dsi->format = MIPI_DSI_FMT_RGB888;
356 dsi->mode_flags = MIPI_DSI_MODE_LPM;
359 np = of_parse_phandle(dsi->dev.of_node, "link2", 0);
361 secondary = of_find_mipi_dsi_device_by_node(np);
365 return -EPROBE_DEFER;
368 /* register a panel for only the DSI-LINK1 interface */
370 sharp = devm_kzalloc(&dsi->dev, sizeof(*sharp), GFP_KERNEL);
372 put_device(&secondary->dev);
376 mipi_dsi_set_drvdata(dsi, sharp);
378 sharp->link2 = secondary;
381 err = sharp_panel_add(sharp);
383 put_device(&secondary->dev);
388 err = mipi_dsi_attach(dsi);
391 sharp_panel_del(sharp);
399 static int sharp_panel_remove(struct mipi_dsi_device *dsi)
401 struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
404 /* only detach from host for the DSI-LINK2 interface */
406 mipi_dsi_detach(dsi);
410 err = sharp_panel_disable(&sharp->base);
412 dev_err(&dsi->dev, "failed to disable panel: %d\n", err);
414 err = mipi_dsi_detach(dsi);
416 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
418 sharp_panel_del(sharp);
423 static void sharp_panel_shutdown(struct mipi_dsi_device *dsi)
425 struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
427 /* nothing to do for DSI-LINK2 */
431 sharp_panel_disable(&sharp->base);
434 static struct mipi_dsi_driver sharp_panel_driver = {
436 .name = "panel-sharp-lq101r1sx01",
437 .of_match_table = sharp_of_match,
439 .probe = sharp_panel_probe,
440 .remove = sharp_panel_remove,
441 .shutdown = sharp_panel_shutdown,
443 module_mipi_dsi_driver(sharp_panel_driver);
445 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
446 MODULE_DESCRIPTION("Sharp LQ101R1SX01 panel driver");
447 MODULE_LICENSE("GPL v2");