2 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/backlight.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
14 #include <linux/regulator/consumer.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_mipi_dsi.h>
19 #include <drm/drm_panel.h>
21 #include <video/mipi_display.h>
23 struct innolux_panel {
24 struct drm_panel base;
25 struct mipi_dsi_device *link;
27 struct backlight_device *backlight;
28 struct regulator *supply;
29 struct gpio_desc *enable_gpio;
35 static inline struct innolux_panel *to_innolux_panel(struct drm_panel *panel)
37 return container_of(panel, struct innolux_panel, base);
40 static int innolux_panel_disable(struct drm_panel *panel)
42 struct innolux_panel *innolux = to_innolux_panel(panel);
44 if (!innolux->enabled)
47 innolux->backlight->props.power = FB_BLANK_POWERDOWN;
48 backlight_update_status(innolux->backlight);
50 innolux->enabled = false;
55 static int innolux_panel_unprepare(struct drm_panel *panel)
57 struct innolux_panel *innolux = to_innolux_panel(panel);
60 if (!innolux->prepared)
63 err = mipi_dsi_dcs_set_display_off(innolux->link);
65 DRM_DEV_ERROR(panel->dev, "failed to set display off: %d\n",
68 err = mipi_dsi_dcs_enter_sleep_mode(innolux->link);
70 DRM_DEV_ERROR(panel->dev, "failed to enter sleep mode: %d\n",
75 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
77 /* T8: 80ms - 1000ms */
80 err = regulator_disable(innolux->supply);
84 innolux->prepared = false;
89 static int innolux_panel_prepare(struct drm_panel *panel)
91 struct innolux_panel *innolux = to_innolux_panel(panel);
92 int err, regulator_err;
94 if (innolux->prepared)
97 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
99 err = regulator_enable(innolux->supply);
103 /* T2: 15ms - 1000ms */
104 usleep_range(15000, 16000);
106 gpiod_set_value_cansleep(innolux->enable_gpio, 1);
108 /* T4: 15ms - 1000ms */
109 usleep_range(15000, 16000);
111 err = mipi_dsi_dcs_exit_sleep_mode(innolux->link);
113 DRM_DEV_ERROR(panel->dev, "failed to exit sleep mode: %d\n",
118 /* T6: 120ms - 1000ms*/
121 err = mipi_dsi_dcs_set_display_on(innolux->link);
123 DRM_DEV_ERROR(panel->dev, "failed to set display on: %d\n",
129 usleep_range(5000, 6000);
131 innolux->prepared = true;
136 regulator_err = regulator_disable(innolux->supply);
138 DRM_DEV_ERROR(panel->dev, "failed to disable regulator: %d\n",
141 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
145 static int innolux_panel_enable(struct drm_panel *panel)
147 struct innolux_panel *innolux = to_innolux_panel(panel);
150 if (innolux->enabled)
153 innolux->backlight->props.power = FB_BLANK_UNBLANK;
154 ret = backlight_update_status(innolux->backlight);
156 DRM_DEV_ERROR(panel->drm->dev,
157 "Failed to enable backlight %d\n", ret);
161 innolux->enabled = true;
166 static const struct drm_display_mode default_mode = {
169 .hsync_start = 768 + 40,
170 .hsync_end = 768 + 40 + 40,
171 .htotal = 768 + 40 + 40 + 40,
173 .vsync_start = 1024 + 20,
174 .vsync_end = 1024 + 20 + 4,
175 .vtotal = 1024 + 20 + 4 + 20,
179 static int innolux_panel_get_modes(struct drm_panel *panel)
181 struct drm_display_mode *mode;
183 mode = drm_mode_duplicate(panel->drm, &default_mode);
185 DRM_DEV_ERROR(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
186 default_mode.hdisplay, default_mode.vdisplay,
187 default_mode.vrefresh);
191 drm_mode_set_name(mode);
193 drm_mode_probed_add(panel->connector, mode);
195 panel->connector->display_info.width_mm = 120;
196 panel->connector->display_info.height_mm = 160;
197 panel->connector->display_info.bpc = 8;
202 static const struct drm_panel_funcs innolux_panel_funcs = {
203 .disable = innolux_panel_disable,
204 .unprepare = innolux_panel_unprepare,
205 .prepare = innolux_panel_prepare,
206 .enable = innolux_panel_enable,
207 .get_modes = innolux_panel_get_modes,
210 static const struct of_device_id innolux_of_match[] = {
211 { .compatible = "innolux,p079zca", },
214 MODULE_DEVICE_TABLE(of, innolux_of_match);
216 static int innolux_panel_add(struct innolux_panel *innolux)
218 struct device *dev = &innolux->link->dev;
219 struct device_node *np;
222 innolux->supply = devm_regulator_get(dev, "power");
223 if (IS_ERR(innolux->supply))
224 return PTR_ERR(innolux->supply);
226 innolux->enable_gpio = devm_gpiod_get_optional(dev, "enable",
228 if (IS_ERR(innolux->enable_gpio)) {
229 err = PTR_ERR(innolux->enable_gpio);
230 dev_dbg(dev, "failed to get enable gpio: %d\n", err);
231 innolux->enable_gpio = NULL;
234 np = of_parse_phandle(dev->of_node, "backlight", 0);
236 innolux->backlight = of_find_backlight_by_node(np);
239 if (!innolux->backlight)
240 return -EPROBE_DEFER;
243 drm_panel_init(&innolux->base);
244 innolux->base.funcs = &innolux_panel_funcs;
245 innolux->base.dev = &innolux->link->dev;
247 err = drm_panel_add(&innolux->base);
254 put_device(&innolux->backlight->dev);
259 static void innolux_panel_del(struct innolux_panel *innolux)
261 if (innolux->base.dev)
262 drm_panel_remove(&innolux->base);
264 put_device(&innolux->backlight->dev);
267 static int innolux_panel_probe(struct mipi_dsi_device *dsi)
269 struct innolux_panel *innolux;
273 dsi->format = MIPI_DSI_FMT_RGB888;
274 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
277 innolux = devm_kzalloc(&dsi->dev, sizeof(*innolux), GFP_KERNEL);
281 mipi_dsi_set_drvdata(dsi, innolux);
285 err = innolux_panel_add(innolux);
289 err = mipi_dsi_attach(dsi);
293 static int innolux_panel_remove(struct mipi_dsi_device *dsi)
295 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi);
298 err = innolux_panel_unprepare(&innolux->base);
300 DRM_DEV_ERROR(&dsi->dev, "failed to unprepare panel: %d\n",
303 err = innolux_panel_disable(&innolux->base);
305 DRM_DEV_ERROR(&dsi->dev, "failed to disable panel: %d\n", err);
307 err = mipi_dsi_detach(dsi);
309 DRM_DEV_ERROR(&dsi->dev, "failed to detach from DSI host: %d\n",
312 drm_panel_detach(&innolux->base);
313 innolux_panel_del(innolux);
318 static void innolux_panel_shutdown(struct mipi_dsi_device *dsi)
320 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi);
322 innolux_panel_unprepare(&innolux->base);
323 innolux_panel_disable(&innolux->base);
326 static struct mipi_dsi_driver innolux_panel_driver = {
328 .name = "panel-innolux-p079zca",
329 .of_match_table = innolux_of_match,
331 .probe = innolux_panel_probe,
332 .remove = innolux_panel_remove,
333 .shutdown = innolux_panel_shutdown,
335 module_mipi_dsi_driver(innolux_panel_driver);
337 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
338 MODULE_DESCRIPTION("Innolux P079ZCA panel driver");
339 MODULE_LICENSE("GPL v2");