1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 NVIDIA Corporation
6 #include <linux/delay.h>
7 #include <linux/gpio/consumer.h>
8 #include <linux/module.h>
10 #include <linux/regulator/consumer.h>
12 #include <video/mipi_display.h>
14 #include <drm/drm_crtc.h>
15 #include <drm/drm_device.h>
16 #include <drm/drm_mipi_dsi.h>
17 #include <drm/drm_panel.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 regulator *supply;
30 const struct drm_display_mode *mode;
33 static inline struct sharp_panel *to_sharp_panel(struct drm_panel *panel)
35 return container_of(panel, struct sharp_panel, base);
38 static void sharp_wait_frames(struct sharp_panel *sharp, unsigned int frames)
40 unsigned int refresh = drm_mode_vrefresh(sharp->mode);
42 if (WARN_ON(frames > refresh))
45 msleep(1000 / (refresh / frames));
48 static int sharp_panel_write(struct sharp_panel *sharp, u16 offset, u8 value)
50 u8 payload[3] = { offset >> 8, offset & 0xff, value };
51 struct mipi_dsi_device *dsi = sharp->link1;
54 err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
56 dev_err(&dsi->dev, "failed to write %02x to %04x: %zd\n",
61 err = mipi_dsi_dcs_nop(dsi);
63 dev_err(&dsi->dev, "failed to send DCS nop: %zd\n", err);
72 static __maybe_unused int sharp_panel_read(struct sharp_panel *sharp,
73 u16 offset, u8 *value)
77 cpu_to_be16s(&offset);
79 err = mipi_dsi_generic_read(sharp->link1, &offset, sizeof(offset),
80 value, sizeof(*value));
82 dev_err(&sharp->link1->dev, "failed to read from %04x: %zd\n",
88 static int sharp_panel_disable(struct drm_panel *panel)
90 struct sharp_panel *sharp = to_sharp_panel(panel);
95 sharp->enabled = false;
100 static int sharp_panel_unprepare(struct drm_panel *panel)
102 struct sharp_panel *sharp = to_sharp_panel(panel);
105 if (!sharp->prepared)
108 sharp_wait_frames(sharp, 4);
110 err = mipi_dsi_dcs_set_display_off(sharp->link1);
112 dev_err(panel->dev, "failed to set display off: %d\n", err);
114 err = mipi_dsi_dcs_enter_sleep_mode(sharp->link1);
116 dev_err(panel->dev, "failed to enter sleep mode: %d\n", err);
120 regulator_disable(sharp->supply);
122 sharp->prepared = false;
127 static int sharp_setup_symmetrical_split(struct mipi_dsi_device *left,
128 struct mipi_dsi_device *right,
129 const struct drm_display_mode *mode)
133 err = mipi_dsi_dcs_set_column_address(left, 0, mode->hdisplay / 2 - 1);
135 dev_err(&left->dev, "failed to set column address: %d\n", err);
139 err = mipi_dsi_dcs_set_page_address(left, 0, mode->vdisplay - 1);
141 dev_err(&left->dev, "failed to set page address: %d\n", err);
145 err = mipi_dsi_dcs_set_column_address(right, mode->hdisplay / 2,
148 dev_err(&right->dev, "failed to set column address: %d\n", err);
152 err = mipi_dsi_dcs_set_page_address(right, 0, mode->vdisplay - 1);
154 dev_err(&right->dev, "failed to set page address: %d\n", err);
161 static int sharp_panel_prepare(struct drm_panel *panel)
163 struct sharp_panel *sharp = to_sharp_panel(panel);
164 u8 format = MIPI_DCS_PIXEL_FMT_24BIT;
170 err = regulator_enable(sharp->supply);
175 * According to the datasheet, the panel needs around 10 ms to fully
176 * power up. At least another 120 ms is required before exiting sleep
177 * mode to make sure the panel is ready. Throw in another 20 ms for
182 err = mipi_dsi_dcs_exit_sleep_mode(sharp->link1);
184 dev_err(panel->dev, "failed to exit sleep mode: %d\n", err);
189 * The MIPI DCS specification mandates this delay only between the
190 * exit_sleep_mode and enter_sleep_mode commands, so it isn't strictly
197 /* set left-right mode */
198 err = sharp_panel_write(sharp, 0x1000, 0x2a);
200 dev_err(panel->dev, "failed to set left-right mode: %d\n", err);
204 /* enable command mode */
205 err = sharp_panel_write(sharp, 0x1001, 0x01);
207 dev_err(panel->dev, "failed to enable command mode: %d\n", err);
211 err = mipi_dsi_dcs_set_pixel_format(sharp->link1, format);
213 dev_err(panel->dev, "failed to set pixel format: %d\n", err);
218 * TODO: The device supports both left-right and even-odd split
219 * configurations, but this driver currently supports only the left-
220 * right split. To support a different mode a mechanism needs to be
221 * put in place to communicate the configuration back to the DSI host
224 err = sharp_setup_symmetrical_split(sharp->link1, sharp->link2,
227 dev_err(panel->dev, "failed to set up symmetrical split: %d\n",
232 err = mipi_dsi_dcs_set_display_on(sharp->link1);
234 dev_err(panel->dev, "failed to set display on: %d\n", err);
238 sharp->prepared = true;
240 /* wait for 6 frames before continuing */
241 sharp_wait_frames(sharp, 6);
246 regulator_disable(sharp->supply);
250 static int sharp_panel_enable(struct drm_panel *panel)
252 struct sharp_panel *sharp = to_sharp_panel(panel);
257 sharp->enabled = true;
262 static const struct drm_display_mode default_mode = {
265 .hsync_start = 2560 + 128,
266 .hsync_end = 2560 + 128 + 64,
267 .htotal = 2560 + 128 + 64 + 64,
269 .vsync_start = 1600 + 4,
270 .vsync_end = 1600 + 4 + 8,
271 .vtotal = 1600 + 4 + 8 + 32,
275 static int sharp_panel_get_modes(struct drm_panel *panel,
276 struct drm_connector *connector)
278 struct drm_display_mode *mode;
280 mode = drm_mode_duplicate(connector->dev, &default_mode);
282 dev_err(panel->dev, "failed to add mode %ux%ux@%u\n",
283 default_mode.hdisplay, default_mode.vdisplay,
284 default_mode.vrefresh);
288 drm_mode_set_name(mode);
290 drm_mode_probed_add(connector, mode);
292 connector->display_info.width_mm = 217;
293 connector->display_info.height_mm = 136;
298 static const struct drm_panel_funcs sharp_panel_funcs = {
299 .disable = sharp_panel_disable,
300 .unprepare = sharp_panel_unprepare,
301 .prepare = sharp_panel_prepare,
302 .enable = sharp_panel_enable,
303 .get_modes = sharp_panel_get_modes,
306 static const struct of_device_id sharp_of_match[] = {
307 { .compatible = "sharp,lq101r1sx01", },
310 MODULE_DEVICE_TABLE(of, sharp_of_match);
312 static int sharp_panel_add(struct sharp_panel *sharp)
316 sharp->mode = &default_mode;
318 sharp->supply = devm_regulator_get(&sharp->link1->dev, "power");
319 if (IS_ERR(sharp->supply))
320 return PTR_ERR(sharp->supply);
322 drm_panel_init(&sharp->base, &sharp->link1->dev, &sharp_panel_funcs,
323 DRM_MODE_CONNECTOR_DSI);
325 ret = drm_panel_of_backlight(&sharp->base);
329 return drm_panel_add(&sharp->base);
332 static void sharp_panel_del(struct sharp_panel *sharp)
335 drm_panel_remove(&sharp->base);
338 put_device(&sharp->link2->dev);
341 static int sharp_panel_probe(struct mipi_dsi_device *dsi)
343 struct mipi_dsi_device *secondary = NULL;
344 struct sharp_panel *sharp;
345 struct device_node *np;
349 dsi->format = MIPI_DSI_FMT_RGB888;
350 dsi->mode_flags = MIPI_DSI_MODE_LPM;
353 np = of_parse_phandle(dsi->dev.of_node, "link2", 0);
355 secondary = of_find_mipi_dsi_device_by_node(np);
359 return -EPROBE_DEFER;
362 /* register a panel for only the DSI-LINK1 interface */
364 sharp = devm_kzalloc(&dsi->dev, sizeof(*sharp), GFP_KERNEL);
366 put_device(&secondary->dev);
370 mipi_dsi_set_drvdata(dsi, sharp);
372 sharp->link2 = secondary;
375 err = sharp_panel_add(sharp);
377 put_device(&secondary->dev);
382 err = mipi_dsi_attach(dsi);
385 sharp_panel_del(sharp);
393 static int sharp_panel_remove(struct mipi_dsi_device *dsi)
395 struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
398 /* only detach from host for the DSI-LINK2 interface */
400 mipi_dsi_detach(dsi);
404 err = drm_panel_disable(&sharp->base);
406 dev_err(&dsi->dev, "failed to disable panel: %d\n", err);
408 err = mipi_dsi_detach(dsi);
410 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
412 sharp_panel_del(sharp);
417 static void sharp_panel_shutdown(struct mipi_dsi_device *dsi)
419 struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
421 /* nothing to do for DSI-LINK2 */
425 drm_panel_disable(&sharp->base);
428 static struct mipi_dsi_driver sharp_panel_driver = {
430 .name = "panel-sharp-lq101r1sx01",
431 .of_match_table = sharp_of_match,
433 .probe = sharp_panel_probe,
434 .remove = sharp_panel_remove,
435 .shutdown = sharp_panel_shutdown,
437 module_mipi_dsi_driver(sharp_panel_driver);
440 MODULE_DESCRIPTION("Sharp LQ101R1SX01 panel driver");
441 MODULE_LICENSE("GPL v2");