1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 NVIDIA Corporation
6 #include <linux/backlight.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/module.h>
11 #include <linux/regulator/consumer.h>
13 #include <video/mipi_display.h>
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>
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;
26 struct backlight_device *backlight;
27 struct regulator *supply;
32 const struct drm_display_mode *mode;
35 static inline struct sharp_panel *to_sharp_panel(struct drm_panel *panel)
37 return container_of(panel, struct sharp_panel, base);
40 static void sharp_wait_frames(struct sharp_panel *sharp, unsigned int frames)
42 unsigned int refresh = drm_mode_vrefresh(sharp->mode);
44 if (WARN_ON(frames > refresh))
47 msleep(1000 / (refresh / frames));
50 static int sharp_panel_write(struct sharp_panel *sharp, u16 offset, u8 value)
52 u8 payload[3] = { offset >> 8, offset & 0xff, value };
53 struct mipi_dsi_device *dsi = sharp->link1;
56 err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
58 dev_err(&dsi->dev, "failed to write %02x to %04x: %zd\n",
63 err = mipi_dsi_dcs_nop(dsi);
65 dev_err(&dsi->dev, "failed to send DCS nop: %zd\n", err);
74 static __maybe_unused int sharp_panel_read(struct sharp_panel *sharp,
75 u16 offset, u8 *value)
79 cpu_to_be16s(&offset);
81 err = mipi_dsi_generic_read(sharp->link1, &offset, sizeof(offset),
82 value, sizeof(*value));
84 dev_err(&sharp->link1->dev, "failed to read from %04x: %zd\n",
90 static int sharp_panel_disable(struct drm_panel *panel)
92 struct sharp_panel *sharp = to_sharp_panel(panel);
97 backlight_disable(sharp->backlight);
99 sharp->enabled = false;
104 static int sharp_panel_unprepare(struct drm_panel *panel)
106 struct sharp_panel *sharp = to_sharp_panel(panel);
109 if (!sharp->prepared)
112 sharp_wait_frames(sharp, 4);
114 err = mipi_dsi_dcs_set_display_off(sharp->link1);
116 dev_err(panel->dev, "failed to set display off: %d\n", err);
118 err = mipi_dsi_dcs_enter_sleep_mode(sharp->link1);
120 dev_err(panel->dev, "failed to enter sleep mode: %d\n", err);
124 regulator_disable(sharp->supply);
126 sharp->prepared = false;
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)
137 err = mipi_dsi_dcs_set_column_address(left, 0, mode->hdisplay / 2 - 1);
139 dev_err(&left->dev, "failed to set column address: %d\n", err);
143 err = mipi_dsi_dcs_set_page_address(left, 0, mode->vdisplay - 1);
145 dev_err(&left->dev, "failed to set page address: %d\n", err);
149 err = mipi_dsi_dcs_set_column_address(right, mode->hdisplay / 2,
152 dev_err(&right->dev, "failed to set column address: %d\n", err);
156 err = mipi_dsi_dcs_set_page_address(right, 0, mode->vdisplay - 1);
158 dev_err(&right->dev, "failed to set page address: %d\n", err);
165 static int sharp_panel_prepare(struct drm_panel *panel)
167 struct sharp_panel *sharp = to_sharp_panel(panel);
168 u8 format = MIPI_DCS_PIXEL_FMT_24BIT;
174 err = regulator_enable(sharp->supply);
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
186 err = mipi_dsi_dcs_exit_sleep_mode(sharp->link1);
188 dev_err(panel->dev, "failed to exit sleep mode: %d\n", err);
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
201 /* set left-right mode */
202 err = sharp_panel_write(sharp, 0x1000, 0x2a);
204 dev_err(panel->dev, "failed to set left-right mode: %d\n", err);
208 /* enable command mode */
209 err = sharp_panel_write(sharp, 0x1001, 0x01);
211 dev_err(panel->dev, "failed to enable command mode: %d\n", err);
215 err = mipi_dsi_dcs_set_pixel_format(sharp->link1, format);
217 dev_err(panel->dev, "failed to set pixel format: %d\n", err);
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
228 err = sharp_setup_symmetrical_split(sharp->link1, sharp->link2,
231 dev_err(panel->dev, "failed to set up symmetrical split: %d\n",
236 err = mipi_dsi_dcs_set_display_on(sharp->link1);
238 dev_err(panel->dev, "failed to set display on: %d\n", err);
242 sharp->prepared = true;
244 /* wait for 6 frames before continuing */
245 sharp_wait_frames(sharp, 6);
250 regulator_disable(sharp->supply);
254 static int sharp_panel_enable(struct drm_panel *panel)
256 struct sharp_panel *sharp = to_sharp_panel(panel);
261 backlight_enable(sharp->backlight);
263 sharp->enabled = true;
268 static const struct drm_display_mode default_mode = {
271 .hsync_start = 2560 + 128,
272 .hsync_end = 2560 + 128 + 64,
273 .htotal = 2560 + 128 + 64 + 64,
275 .vsync_start = 1600 + 4,
276 .vsync_end = 1600 + 4 + 8,
277 .vtotal = 1600 + 4 + 8 + 32,
281 static int sharp_panel_get_modes(struct drm_panel *panel)
283 struct drm_display_mode *mode;
285 mode = drm_mode_duplicate(panel->drm, &default_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);
293 drm_mode_set_name(mode);
295 drm_mode_probed_add(panel->connector, mode);
297 panel->connector->display_info.width_mm = 217;
298 panel->connector->display_info.height_mm = 136;
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,
311 static const struct of_device_id sharp_of_match[] = {
312 { .compatible = "sharp,lq101r1sx01", },
315 MODULE_DEVICE_TABLE(of, sharp_of_match);
317 static int sharp_panel_add(struct sharp_panel *sharp)
319 struct device *dev = &sharp->link1->dev;
321 sharp->mode = &default_mode;
323 sharp->supply = devm_regulator_get(&sharp->link1->dev, "power");
324 if (IS_ERR(sharp->supply))
325 return PTR_ERR(sharp->supply);
327 sharp->backlight = devm_of_find_backlight(dev);
329 if (IS_ERR(sharp->backlight))
330 return PTR_ERR(sharp->backlight);
332 drm_panel_init(&sharp->base);
333 sharp->base.funcs = &sharp_panel_funcs;
334 sharp->base.dev = &sharp->link1->dev;
336 return drm_panel_add(&sharp->base);
339 static void sharp_panel_del(struct sharp_panel *sharp)
342 drm_panel_remove(&sharp->base);
345 put_device(&sharp->link2->dev);
348 static int sharp_panel_probe(struct mipi_dsi_device *dsi)
350 struct mipi_dsi_device *secondary = NULL;
351 struct sharp_panel *sharp;
352 struct device_node *np;
356 dsi->format = MIPI_DSI_FMT_RGB888;
357 dsi->mode_flags = MIPI_DSI_MODE_LPM;
360 np = of_parse_phandle(dsi->dev.of_node, "link2", 0);
362 secondary = of_find_mipi_dsi_device_by_node(np);
366 return -EPROBE_DEFER;
369 /* register a panel for only the DSI-LINK1 interface */
371 sharp = devm_kzalloc(&dsi->dev, sizeof(*sharp), GFP_KERNEL);
373 put_device(&secondary->dev);
377 mipi_dsi_set_drvdata(dsi, sharp);
379 sharp->link2 = secondary;
382 err = sharp_panel_add(sharp);
384 put_device(&secondary->dev);
389 err = mipi_dsi_attach(dsi);
392 sharp_panel_del(sharp);
400 static int sharp_panel_remove(struct mipi_dsi_device *dsi)
402 struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
405 /* only detach from host for the DSI-LINK2 interface */
407 mipi_dsi_detach(dsi);
411 err = sharp_panel_disable(&sharp->base);
413 dev_err(&dsi->dev, "failed to disable panel: %d\n", err);
415 err = mipi_dsi_detach(dsi);
417 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
419 sharp_panel_del(sharp);
424 static void sharp_panel_shutdown(struct mipi_dsi_device *dsi)
426 struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
428 /* nothing to do for DSI-LINK2 */
432 sharp_panel_disable(&sharp->base);
435 static struct mipi_dsi_driver sharp_panel_driver = {
437 .name = "panel-sharp-lq101r1sx01",
438 .of_match_table = sharp_of_match,
440 .probe = sharp_panel_probe,
441 .remove = sharp_panel_remove,
442 .shutdown = sharp_panel_shutdown,
444 module_mipi_dsi_driver(sharp_panel_driver);
447 MODULE_DESCRIPTION("Sharp LQ101R1SX01 panel driver");
448 MODULE_LICENSE("GPL v2");