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;
27 const struct drm_display_mode *mode;
30 static inline struct sharp_panel *to_sharp_panel(struct drm_panel *panel)
32 return container_of(panel, struct sharp_panel, base);
35 static void sharp_wait_frames(struct sharp_panel *sharp, unsigned int frames)
37 unsigned int refresh = drm_mode_vrefresh(sharp->mode);
39 if (WARN_ON(frames > refresh))
42 msleep(1000 / (refresh / frames));
45 static int sharp_panel_write(struct sharp_panel *sharp, u16 offset, u8 value)
47 u8 payload[3] = { offset >> 8, offset & 0xff, value };
48 struct mipi_dsi_device *dsi = sharp->link1;
51 err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
53 dev_err(&dsi->dev, "failed to write %02x to %04x: %zd\n",
58 err = mipi_dsi_dcs_nop(dsi);
60 dev_err(&dsi->dev, "failed to send DCS nop: %zd\n", err);
69 static __maybe_unused int sharp_panel_read(struct sharp_panel *sharp,
70 u16 offset, u8 *value)
74 cpu_to_be16s(&offset);
76 err = mipi_dsi_generic_read(sharp->link1, &offset, sizeof(offset),
77 value, sizeof(*value));
79 dev_err(&sharp->link1->dev, "failed to read from %04x: %zd\n",
85 static int sharp_panel_unprepare(struct drm_panel *panel)
87 struct sharp_panel *sharp = to_sharp_panel(panel);
90 sharp_wait_frames(sharp, 4);
92 err = mipi_dsi_dcs_set_display_off(sharp->link1);
94 dev_err(panel->dev, "failed to set display off: %d\n", err);
96 err = mipi_dsi_dcs_enter_sleep_mode(sharp->link1);
98 dev_err(panel->dev, "failed to enter sleep mode: %d\n", err);
102 regulator_disable(sharp->supply);
107 static int sharp_setup_symmetrical_split(struct mipi_dsi_device *left,
108 struct mipi_dsi_device *right,
109 const struct drm_display_mode *mode)
113 err = mipi_dsi_dcs_set_column_address(left, 0, mode->hdisplay / 2 - 1);
115 dev_err(&left->dev, "failed to set column address: %d\n", err);
119 err = mipi_dsi_dcs_set_page_address(left, 0, mode->vdisplay - 1);
121 dev_err(&left->dev, "failed to set page address: %d\n", err);
125 err = mipi_dsi_dcs_set_column_address(right, mode->hdisplay / 2,
128 dev_err(&right->dev, "failed to set column address: %d\n", err);
132 err = mipi_dsi_dcs_set_page_address(right, 0, mode->vdisplay - 1);
134 dev_err(&right->dev, "failed to set page address: %d\n", err);
141 static int sharp_panel_prepare(struct drm_panel *panel)
143 struct sharp_panel *sharp = to_sharp_panel(panel);
144 u8 format = MIPI_DCS_PIXEL_FMT_24BIT;
147 err = regulator_enable(sharp->supply);
152 * According to the datasheet, the panel needs around 10 ms to fully
153 * power up. At least another 120 ms is required before exiting sleep
154 * mode to make sure the panel is ready. Throw in another 20 ms for
159 err = mipi_dsi_dcs_exit_sleep_mode(sharp->link1);
161 dev_err(panel->dev, "failed to exit sleep mode: %d\n", err);
166 * The MIPI DCS specification mandates this delay only between the
167 * exit_sleep_mode and enter_sleep_mode commands, so it isn't strictly
174 /* set left-right mode */
175 err = sharp_panel_write(sharp, 0x1000, 0x2a);
177 dev_err(panel->dev, "failed to set left-right mode: %d\n", err);
181 /* enable command mode */
182 err = sharp_panel_write(sharp, 0x1001, 0x01);
184 dev_err(panel->dev, "failed to enable command mode: %d\n", err);
188 err = mipi_dsi_dcs_set_pixel_format(sharp->link1, format);
190 dev_err(panel->dev, "failed to set pixel format: %d\n", err);
195 * TODO: The device supports both left-right and even-odd split
196 * configurations, but this driver currently supports only the left-
197 * right split. To support a different mode a mechanism needs to be
198 * put in place to communicate the configuration back to the DSI host
201 err = sharp_setup_symmetrical_split(sharp->link1, sharp->link2,
204 dev_err(panel->dev, "failed to set up symmetrical split: %d\n",
209 err = mipi_dsi_dcs_set_display_on(sharp->link1);
211 dev_err(panel->dev, "failed to set display on: %d\n", err);
215 /* wait for 6 frames before continuing */
216 sharp_wait_frames(sharp, 6);
221 regulator_disable(sharp->supply);
225 static const struct drm_display_mode default_mode = {
228 .hsync_start = 2560 + 128,
229 .hsync_end = 2560 + 128 + 64,
230 .htotal = 2560 + 128 + 64 + 64,
232 .vsync_start = 1600 + 4,
233 .vsync_end = 1600 + 4 + 8,
234 .vtotal = 1600 + 4 + 8 + 32,
237 static int sharp_panel_get_modes(struct drm_panel *panel,
238 struct drm_connector *connector)
240 struct drm_display_mode *mode;
242 mode = drm_mode_duplicate(connector->dev, &default_mode);
244 dev_err(panel->dev, "failed to add mode %ux%ux@%u\n",
245 default_mode.hdisplay, default_mode.vdisplay,
246 drm_mode_vrefresh(&default_mode));
250 drm_mode_set_name(mode);
252 drm_mode_probed_add(connector, mode);
254 connector->display_info.width_mm = 217;
255 connector->display_info.height_mm = 136;
260 static const struct drm_panel_funcs sharp_panel_funcs = {
261 .unprepare = sharp_panel_unprepare,
262 .prepare = sharp_panel_prepare,
263 .get_modes = sharp_panel_get_modes,
266 static const struct of_device_id sharp_of_match[] = {
267 { .compatible = "sharp,lq101r1sx01", },
270 MODULE_DEVICE_TABLE(of, sharp_of_match);
272 static int sharp_panel_add(struct sharp_panel *sharp)
276 sharp->mode = &default_mode;
278 sharp->supply = devm_regulator_get(&sharp->link1->dev, "power");
279 if (IS_ERR(sharp->supply))
280 return PTR_ERR(sharp->supply);
282 drm_panel_init(&sharp->base, &sharp->link1->dev, &sharp_panel_funcs,
283 DRM_MODE_CONNECTOR_DSI);
285 ret = drm_panel_of_backlight(&sharp->base);
289 drm_panel_add(&sharp->base);
294 static void sharp_panel_del(struct sharp_panel *sharp)
297 drm_panel_remove(&sharp->base);
300 put_device(&sharp->link2->dev);
303 static int sharp_panel_probe(struct mipi_dsi_device *dsi)
305 struct mipi_dsi_device *secondary = NULL;
306 struct sharp_panel *sharp;
307 struct device_node *np;
311 dsi->format = MIPI_DSI_FMT_RGB888;
312 dsi->mode_flags = MIPI_DSI_MODE_LPM;
315 np = of_parse_phandle(dsi->dev.of_node, "link2", 0);
317 secondary = of_find_mipi_dsi_device_by_node(np);
321 return -EPROBE_DEFER;
324 /* register a panel for only the DSI-LINK1 interface */
326 sharp = devm_kzalloc(&dsi->dev, sizeof(*sharp), GFP_KERNEL);
328 put_device(&secondary->dev);
332 mipi_dsi_set_drvdata(dsi, sharp);
334 sharp->link2 = secondary;
337 err = sharp_panel_add(sharp);
339 put_device(&secondary->dev);
344 err = mipi_dsi_attach(dsi);
347 sharp_panel_del(sharp);
355 static void sharp_panel_remove(struct mipi_dsi_device *dsi)
357 struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
360 err = mipi_dsi_detach(dsi);
362 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
364 /* only detach from host for the DSI-LINK2 interface */
366 sharp_panel_del(sharp);
369 static struct mipi_dsi_driver sharp_panel_driver = {
371 .name = "panel-sharp-lq101r1sx01",
372 .of_match_table = sharp_of_match,
374 .probe = sharp_panel_probe,
375 .remove = sharp_panel_remove,
377 module_mipi_dsi_driver(sharp_panel_driver);
380 MODULE_DESCRIPTION("Sharp LQ101R1SX01 panel driver");
381 MODULE_LICENSE("GPL v2");