blob: 4c1b29eec2a5e34fd6f2c66ec39bb1999f6d8966 [file] [log] [blame]
Chris Zhong14c8f2e2017-03-24 08:51:32 +08001/*
2 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
3 *
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.
8 */
9
10#include <linux/backlight.h>
11#include <linux/gpio/consumer.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/regulator/consumer.h>
15
16#include <drm/drmP.h>
17#include <drm/drm_crtc.h>
18#include <drm/drm_mipi_dsi.h>
19#include <drm/drm_panel.h>
20
21#include <video/mipi_display.h>
22
23struct innolux_panel {
24 struct drm_panel base;
25 struct mipi_dsi_device *link;
26
27 struct backlight_device *backlight;
28 struct regulator *supply;
29 struct gpio_desc *enable_gpio;
30
31 bool prepared;
32 bool enabled;
33};
34
35static inline struct innolux_panel *to_innolux_panel(struct drm_panel *panel)
36{
37 return container_of(panel, struct innolux_panel, base);
38}
39
40static int innolux_panel_disable(struct drm_panel *panel)
41{
42 struct innolux_panel *innolux = to_innolux_panel(panel);
43 int err;
44
45 if (!innolux->enabled)
46 return 0;
47
Meghana Madhyasthad593bfd2018-01-24 16:39:27 +000048 backlight_disable(innolux->backlight);
Chris Zhong14c8f2e2017-03-24 08:51:32 +080049
50 err = mipi_dsi_dcs_set_display_off(innolux->link);
51 if (err < 0)
52 DRM_DEV_ERROR(panel->dev, "failed to set display off: %d\n",
53 err);
54
55 innolux->enabled = false;
56
57 return 0;
58}
59
60static int innolux_panel_unprepare(struct drm_panel *panel)
61{
62 struct innolux_panel *innolux = to_innolux_panel(panel);
63 int err;
64
65 if (!innolux->prepared)
66 return 0;
67
68 err = mipi_dsi_dcs_enter_sleep_mode(innolux->link);
69 if (err < 0) {
70 DRM_DEV_ERROR(panel->dev, "failed to enter sleep mode: %d\n",
71 err);
72 return err;
73 }
74
75 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
76
77 /* T8: 80ms - 1000ms */
78 msleep(80);
79
80 err = regulator_disable(innolux->supply);
81 if (err < 0)
82 return err;
83
84 innolux->prepared = false;
85
86 return 0;
87}
88
89static int innolux_panel_prepare(struct drm_panel *panel)
90{
91 struct innolux_panel *innolux = to_innolux_panel(panel);
92 int err, regulator_err;
93
94 if (innolux->prepared)
95 return 0;
96
97 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
98
99 err = regulator_enable(innolux->supply);
100 if (err < 0)
101 return err;
102
103 /* T2: 15ms - 1000ms */
104 usleep_range(15000, 16000);
105
106 gpiod_set_value_cansleep(innolux->enable_gpio, 1);
107
108 /* T4: 15ms - 1000ms */
109 usleep_range(15000, 16000);
110
111 err = mipi_dsi_dcs_exit_sleep_mode(innolux->link);
112 if (err < 0) {
113 DRM_DEV_ERROR(panel->dev, "failed to exit sleep mode: %d\n",
114 err);
115 goto poweroff;
116 }
117
118 /* T6: 120ms - 1000ms*/
119 msleep(120);
120
121 err = mipi_dsi_dcs_set_display_on(innolux->link);
122 if (err < 0) {
123 DRM_DEV_ERROR(panel->dev, "failed to set display on: %d\n",
124 err);
125 goto poweroff;
126 }
127
128 /* T7: 5ms */
129 usleep_range(5000, 6000);
130
131 innolux->prepared = true;
132
133 return 0;
134
135poweroff:
136 regulator_err = regulator_disable(innolux->supply);
137 if (regulator_err)
138 DRM_DEV_ERROR(panel->dev, "failed to disable regulator: %d\n",
139 regulator_err);
140
141 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
142 return err;
143}
144
145static int innolux_panel_enable(struct drm_panel *panel)
146{
147 struct innolux_panel *innolux = to_innolux_panel(panel);
148 int ret;
149
150 if (innolux->enabled)
151 return 0;
152
Meghana Madhyasthad593bfd2018-01-24 16:39:27 +0000153 ret = backlight_enable(innolux->backlight);
Chris Zhong14c8f2e2017-03-24 08:51:32 +0800154 if (ret) {
155 DRM_DEV_ERROR(panel->drm->dev,
156 "Failed to enable backlight %d\n", ret);
157 return ret;
158 }
159
160 innolux->enabled = true;
161
162 return 0;
163}
164
165static const struct drm_display_mode default_mode = {
166 .clock = 56900,
167 .hdisplay = 768,
168 .hsync_start = 768 + 40,
169 .hsync_end = 768 + 40 + 40,
170 .htotal = 768 + 40 + 40 + 40,
171 .vdisplay = 1024,
172 .vsync_start = 1024 + 20,
173 .vsync_end = 1024 + 20 + 4,
174 .vtotal = 1024 + 20 + 4 + 20,
175 .vrefresh = 60,
176};
177
178static int innolux_panel_get_modes(struct drm_panel *panel)
179{
180 struct drm_display_mode *mode;
181
182 mode = drm_mode_duplicate(panel->drm, &default_mode);
183 if (!mode) {
184 DRM_DEV_ERROR(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
185 default_mode.hdisplay, default_mode.vdisplay,
186 default_mode.vrefresh);
187 return -ENOMEM;
188 }
189
190 drm_mode_set_name(mode);
191
192 drm_mode_probed_add(panel->connector, mode);
193
194 panel->connector->display_info.width_mm = 120;
195 panel->connector->display_info.height_mm = 160;
196 panel->connector->display_info.bpc = 8;
197
198 return 1;
199}
200
201static const struct drm_panel_funcs innolux_panel_funcs = {
202 .disable = innolux_panel_disable,
203 .unprepare = innolux_panel_unprepare,
204 .prepare = innolux_panel_prepare,
205 .enable = innolux_panel_enable,
206 .get_modes = innolux_panel_get_modes,
207};
208
209static const struct of_device_id innolux_of_match[] = {
210 { .compatible = "innolux,p079zca", },
211 { }
212};
213MODULE_DEVICE_TABLE(of, innolux_of_match);
214
215static int innolux_panel_add(struct innolux_panel *innolux)
216{
217 struct device *dev = &innolux->link->dev;
218 struct device_node *np;
219 int err;
220
221 innolux->supply = devm_regulator_get(dev, "power");
222 if (IS_ERR(innolux->supply))
223 return PTR_ERR(innolux->supply);
224
225 innolux->enable_gpio = devm_gpiod_get_optional(dev, "enable",
226 GPIOD_OUT_HIGH);
227 if (IS_ERR(innolux->enable_gpio)) {
228 err = PTR_ERR(innolux->enable_gpio);
229 dev_dbg(dev, "failed to get enable gpio: %d\n", err);
230 innolux->enable_gpio = NULL;
231 }
232
233 np = of_parse_phandle(dev->of_node, "backlight", 0);
234 if (np) {
235 innolux->backlight = of_find_backlight_by_node(np);
236 of_node_put(np);
237
238 if (!innolux->backlight)
239 return -EPROBE_DEFER;
240 }
241
242 drm_panel_init(&innolux->base);
243 innolux->base.funcs = &innolux_panel_funcs;
244 innolux->base.dev = &innolux->link->dev;
245
246 err = drm_panel_add(&innolux->base);
247 if (err < 0)
248 goto put_backlight;
249
250 return 0;
251
252put_backlight:
253 put_device(&innolux->backlight->dev);
254
255 return err;
256}
257
258static void innolux_panel_del(struct innolux_panel *innolux)
259{
260 if (innolux->base.dev)
261 drm_panel_remove(&innolux->base);
262
263 put_device(&innolux->backlight->dev);
264}
265
266static int innolux_panel_probe(struct mipi_dsi_device *dsi)
267{
268 struct innolux_panel *innolux;
269 int err;
270
271 dsi->lanes = 4;
272 dsi->format = MIPI_DSI_FMT_RGB888;
273 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
274 MIPI_DSI_MODE_LPM;
275
276 innolux = devm_kzalloc(&dsi->dev, sizeof(*innolux), GFP_KERNEL);
277 if (!innolux)
278 return -ENOMEM;
279
280 mipi_dsi_set_drvdata(dsi, innolux);
281
282 innolux->link = dsi;
283
284 err = innolux_panel_add(innolux);
285 if (err < 0)
286 return err;
287
288 err = mipi_dsi_attach(dsi);
289 return err;
290}
291
292static int innolux_panel_remove(struct mipi_dsi_device *dsi)
293{
294 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi);
295 int err;
296
297 err = innolux_panel_unprepare(&innolux->base);
298 if (err < 0)
299 DRM_DEV_ERROR(&dsi->dev, "failed to unprepare panel: %d\n",
300 err);
301
302 err = innolux_panel_disable(&innolux->base);
303 if (err < 0)
304 DRM_DEV_ERROR(&dsi->dev, "failed to disable panel: %d\n", err);
305
306 err = mipi_dsi_detach(dsi);
307 if (err < 0)
308 DRM_DEV_ERROR(&dsi->dev, "failed to detach from DSI host: %d\n",
309 err);
310
311 drm_panel_detach(&innolux->base);
312 innolux_panel_del(innolux);
313
314 return 0;
315}
316
317static void innolux_panel_shutdown(struct mipi_dsi_device *dsi)
318{
319 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi);
320
321 innolux_panel_unprepare(&innolux->base);
322 innolux_panel_disable(&innolux->base);
323}
324
325static struct mipi_dsi_driver innolux_panel_driver = {
326 .driver = {
327 .name = "panel-innolux-p079zca",
328 .of_match_table = innolux_of_match,
329 },
330 .probe = innolux_panel_probe,
331 .remove = innolux_panel_remove,
332 .shutdown = innolux_panel_shutdown,
333};
334module_mipi_dsi_driver(innolux_panel_driver);
335
336MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
337MODULE_DESCRIPTION("Innolux P079ZCA panel driver");
338MODULE_LICENSE("GPL v2");