xref: /linux/drivers/gpu/drm/panel/panel-sharp-lq101r1sx01.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
1 /*
2  * Copyright (C) 2014 NVIDIA Corporation
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 version 2 as
6  * published by the Free Software Foundation.
7  */
8 
9 #include <linux/backlight.h>
10 #include <linux/delay.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 <video/mipi_display.h>
17 
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_device.h>
20 #include <drm/drm_mipi_dsi.h>
21 #include <drm/drm_panel.h>
22 
23 struct sharp_panel {
24 	struct drm_panel base;
25 	/* the datasheet refers to them as DSI-LINK1 and DSI-LINK2 */
26 	struct mipi_dsi_device *link1;
27 	struct mipi_dsi_device *link2;
28 
29 	struct backlight_device *backlight;
30 	struct regulator *supply;
31 
32 	bool prepared;
33 	bool enabled;
34 
35 	const struct drm_display_mode *mode;
36 };
37 
38 static inline struct sharp_panel *to_sharp_panel(struct drm_panel *panel)
39 {
40 	return container_of(panel, struct sharp_panel, base);
41 }
42 
43 static void sharp_wait_frames(struct sharp_panel *sharp, unsigned int frames)
44 {
45 	unsigned int refresh = drm_mode_vrefresh(sharp->mode);
46 
47 	if (WARN_ON(frames > refresh))
48 		return;
49 
50 	msleep(1000 / (refresh / frames));
51 }
52 
53 static int sharp_panel_write(struct sharp_panel *sharp, u16 offset, u8 value)
54 {
55 	u8 payload[3] = { offset >> 8, offset & 0xff, value };
56 	struct mipi_dsi_device *dsi = sharp->link1;
57 	ssize_t err;
58 
59 	err = mipi_dsi_generic_write(dsi, payload, sizeof(payload));
60 	if (err < 0) {
61 		dev_err(&dsi->dev, "failed to write %02x to %04x: %zd\n",
62 			value, offset, err);
63 		return err;
64 	}
65 
66 	err = mipi_dsi_dcs_nop(dsi);
67 	if (err < 0) {
68 		dev_err(&dsi->dev, "failed to send DCS nop: %zd\n", err);
69 		return err;
70 	}
71 
72 	usleep_range(10, 20);
73 
74 	return 0;
75 }
76 
77 static __maybe_unused int sharp_panel_read(struct sharp_panel *sharp,
78 					   u16 offset, u8 *value)
79 {
80 	ssize_t err;
81 
82 	cpu_to_be16s(&offset);
83 
84 	err = mipi_dsi_generic_read(sharp->link1, &offset, sizeof(offset),
85 				    value, sizeof(*value));
86 	if (err < 0)
87 		dev_err(&sharp->link1->dev, "failed to read from %04x: %zd\n",
88 			offset, err);
89 
90 	return err;
91 }
92 
93 static int sharp_panel_disable(struct drm_panel *panel)
94 {
95 	struct sharp_panel *sharp = to_sharp_panel(panel);
96 
97 	if (!sharp->enabled)
98 		return 0;
99 
100 	backlight_disable(sharp->backlight);
101 
102 	sharp->enabled = false;
103 
104 	return 0;
105 }
106 
107 static int sharp_panel_unprepare(struct drm_panel *panel)
108 {
109 	struct sharp_panel *sharp = to_sharp_panel(panel);
110 	int err;
111 
112 	if (!sharp->prepared)
113 		return 0;
114 
115 	sharp_wait_frames(sharp, 4);
116 
117 	err = mipi_dsi_dcs_set_display_off(sharp->link1);
118 	if (err < 0)
119 		dev_err(panel->dev, "failed to set display off: %d\n", err);
120 
121 	err = mipi_dsi_dcs_enter_sleep_mode(sharp->link1);
122 	if (err < 0)
123 		dev_err(panel->dev, "failed to enter sleep mode: %d\n", err);
124 
125 	msleep(120);
126 
127 	regulator_disable(sharp->supply);
128 
129 	sharp->prepared = false;
130 
131 	return 0;
132 }
133 
134 static int sharp_setup_symmetrical_split(struct mipi_dsi_device *left,
135 					 struct mipi_dsi_device *right,
136 					 const struct drm_display_mode *mode)
137 {
138 	int err;
139 
140 	err = mipi_dsi_dcs_set_column_address(left, 0, mode->hdisplay / 2 - 1);
141 	if (err < 0) {
142 		dev_err(&left->dev, "failed to set column address: %d\n", err);
143 		return err;
144 	}
145 
146 	err = mipi_dsi_dcs_set_page_address(left, 0, mode->vdisplay - 1);
147 	if (err < 0) {
148 		dev_err(&left->dev, "failed to set page address: %d\n", err);
149 		return err;
150 	}
151 
152 	err = mipi_dsi_dcs_set_column_address(right, mode->hdisplay / 2,
153 					      mode->hdisplay - 1);
154 	if (err < 0) {
155 		dev_err(&right->dev, "failed to set column address: %d\n", err);
156 		return err;
157 	}
158 
159 	err = mipi_dsi_dcs_set_page_address(right, 0, mode->vdisplay - 1);
160 	if (err < 0) {
161 		dev_err(&right->dev, "failed to set page address: %d\n", err);
162 		return err;
163 	}
164 
165 	return 0;
166 }
167 
168 static int sharp_panel_prepare(struct drm_panel *panel)
169 {
170 	struct sharp_panel *sharp = to_sharp_panel(panel);
171 	u8 format = MIPI_DCS_PIXEL_FMT_24BIT;
172 	int err;
173 
174 	if (sharp->prepared)
175 		return 0;
176 
177 	err = regulator_enable(sharp->supply);
178 	if (err < 0)
179 		return err;
180 
181 	/*
182 	 * According to the datasheet, the panel needs around 10 ms to fully
183 	 * power up. At least another 120 ms is required before exiting sleep
184 	 * mode to make sure the panel is ready. Throw in another 20 ms for
185 	 * good measure.
186 	 */
187 	msleep(150);
188 
189 	err = mipi_dsi_dcs_exit_sleep_mode(sharp->link1);
190 	if (err < 0) {
191 		dev_err(panel->dev, "failed to exit sleep mode: %d\n", err);
192 		goto poweroff;
193 	}
194 
195 	/*
196 	 * The MIPI DCS specification mandates this delay only between the
197 	 * exit_sleep_mode and enter_sleep_mode commands, so it isn't strictly
198 	 * necessary here.
199 	 */
200 	/*
201 	msleep(120);
202 	*/
203 
204 	/* set left-right mode */
205 	err = sharp_panel_write(sharp, 0x1000, 0x2a);
206 	if (err < 0) {
207 		dev_err(panel->dev, "failed to set left-right mode: %d\n", err);
208 		goto poweroff;
209 	}
210 
211 	/* enable command mode */
212 	err = sharp_panel_write(sharp, 0x1001, 0x01);
213 	if (err < 0) {
214 		dev_err(panel->dev, "failed to enable command mode: %d\n", err);
215 		goto poweroff;
216 	}
217 
218 	err = mipi_dsi_dcs_set_pixel_format(sharp->link1, format);
219 	if (err < 0) {
220 		dev_err(panel->dev, "failed to set pixel format: %d\n", err);
221 		goto poweroff;
222 	}
223 
224 	/*
225 	 * TODO: The device supports both left-right and even-odd split
226 	 * configurations, but this driver currently supports only the left-
227 	 * right split. To support a different mode a mechanism needs to be
228 	 * put in place to communicate the configuration back to the DSI host
229 	 * controller.
230 	 */
231 	err = sharp_setup_symmetrical_split(sharp->link1, sharp->link2,
232 					    sharp->mode);
233 	if (err < 0) {
234 		dev_err(panel->dev, "failed to set up symmetrical split: %d\n",
235 			err);
236 		goto poweroff;
237 	}
238 
239 	err = mipi_dsi_dcs_set_display_on(sharp->link1);
240 	if (err < 0) {
241 		dev_err(panel->dev, "failed to set display on: %d\n", err);
242 		goto poweroff;
243 	}
244 
245 	sharp->prepared = true;
246 
247 	/* wait for 6 frames before continuing */
248 	sharp_wait_frames(sharp, 6);
249 
250 	return 0;
251 
252 poweroff:
253 	regulator_disable(sharp->supply);
254 	return err;
255 }
256 
257 static int sharp_panel_enable(struct drm_panel *panel)
258 {
259 	struct sharp_panel *sharp = to_sharp_panel(panel);
260 
261 	if (sharp->enabled)
262 		return 0;
263 
264 	backlight_enable(sharp->backlight);
265 
266 	sharp->enabled = true;
267 
268 	return 0;
269 }
270 
271 static const struct drm_display_mode default_mode = {
272 	.clock = 278000,
273 	.hdisplay = 2560,
274 	.hsync_start = 2560 + 128,
275 	.hsync_end = 2560 + 128 + 64,
276 	.htotal = 2560 + 128 + 64 + 64,
277 	.vdisplay = 1600,
278 	.vsync_start = 1600 + 4,
279 	.vsync_end = 1600 + 4 + 8,
280 	.vtotal = 1600 + 4 + 8 + 32,
281 	.vrefresh = 60,
282 };
283 
284 static int sharp_panel_get_modes(struct drm_panel *panel)
285 {
286 	struct drm_display_mode *mode;
287 
288 	mode = drm_mode_duplicate(panel->drm, &default_mode);
289 	if (!mode) {
290 		dev_err(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
291 			default_mode.hdisplay, default_mode.vdisplay,
292 			default_mode.vrefresh);
293 		return -ENOMEM;
294 	}
295 
296 	drm_mode_set_name(mode);
297 
298 	drm_mode_probed_add(panel->connector, mode);
299 
300 	panel->connector->display_info.width_mm = 217;
301 	panel->connector->display_info.height_mm = 136;
302 
303 	return 1;
304 }
305 
306 static const struct drm_panel_funcs sharp_panel_funcs = {
307 	.disable = sharp_panel_disable,
308 	.unprepare = sharp_panel_unprepare,
309 	.prepare = sharp_panel_prepare,
310 	.enable = sharp_panel_enable,
311 	.get_modes = sharp_panel_get_modes,
312 };
313 
314 static const struct of_device_id sharp_of_match[] = {
315 	{ .compatible = "sharp,lq101r1sx01", },
316 	{ }
317 };
318 MODULE_DEVICE_TABLE(of, sharp_of_match);
319 
320 static int sharp_panel_add(struct sharp_panel *sharp)
321 {
322 	struct device *dev = &sharp->link1->dev;
323 
324 	sharp->mode = &default_mode;
325 
326 	sharp->supply = devm_regulator_get(&sharp->link1->dev, "power");
327 	if (IS_ERR(sharp->supply))
328 		return PTR_ERR(sharp->supply);
329 
330 	sharp->backlight = devm_of_find_backlight(dev);
331 
332 	if (IS_ERR(sharp->backlight))
333 		return PTR_ERR(sharp->backlight);
334 
335 	drm_panel_init(&sharp->base);
336 	sharp->base.funcs = &sharp_panel_funcs;
337 	sharp->base.dev = &sharp->link1->dev;
338 
339 	return drm_panel_add(&sharp->base);
340 }
341 
342 static void sharp_panel_del(struct sharp_panel *sharp)
343 {
344 	if (sharp->base.dev)
345 		drm_panel_remove(&sharp->base);
346 
347 	if (sharp->link2)
348 		put_device(&sharp->link2->dev);
349 }
350 
351 static int sharp_panel_probe(struct mipi_dsi_device *dsi)
352 {
353 	struct mipi_dsi_device *secondary = NULL;
354 	struct sharp_panel *sharp;
355 	struct device_node *np;
356 	int err;
357 
358 	dsi->lanes = 4;
359 	dsi->format = MIPI_DSI_FMT_RGB888;
360 	dsi->mode_flags = MIPI_DSI_MODE_LPM;
361 
362 	/* Find DSI-LINK1 */
363 	np = of_parse_phandle(dsi->dev.of_node, "link2", 0);
364 	if (np) {
365 		secondary = of_find_mipi_dsi_device_by_node(np);
366 		of_node_put(np);
367 
368 		if (!secondary)
369 			return -EPROBE_DEFER;
370 	}
371 
372 	/* register a panel for only the DSI-LINK1 interface */
373 	if (secondary) {
374 		sharp = devm_kzalloc(&dsi->dev, sizeof(*sharp), GFP_KERNEL);
375 		if (!sharp) {
376 			put_device(&secondary->dev);
377 			return -ENOMEM;
378 		}
379 
380 		mipi_dsi_set_drvdata(dsi, sharp);
381 
382 		sharp->link2 = secondary;
383 		sharp->link1 = dsi;
384 
385 		err = sharp_panel_add(sharp);
386 		if (err < 0) {
387 			put_device(&secondary->dev);
388 			return err;
389 		}
390 	}
391 
392 	err = mipi_dsi_attach(dsi);
393 	if (err < 0) {
394 		if (secondary)
395 			sharp_panel_del(sharp);
396 
397 		return err;
398 	}
399 
400 	return 0;
401 }
402 
403 static int sharp_panel_remove(struct mipi_dsi_device *dsi)
404 {
405 	struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
406 	int err;
407 
408 	/* only detach from host for the DSI-LINK2 interface */
409 	if (!sharp) {
410 		mipi_dsi_detach(dsi);
411 		return 0;
412 	}
413 
414 	err = sharp_panel_disable(&sharp->base);
415 	if (err < 0)
416 		dev_err(&dsi->dev, "failed to disable panel: %d\n", err);
417 
418 	err = mipi_dsi_detach(dsi);
419 	if (err < 0)
420 		dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
421 
422 	sharp_panel_del(sharp);
423 
424 	return 0;
425 }
426 
427 static void sharp_panel_shutdown(struct mipi_dsi_device *dsi)
428 {
429 	struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi);
430 
431 	/* nothing to do for DSI-LINK2 */
432 	if (!sharp)
433 		return;
434 
435 	sharp_panel_disable(&sharp->base);
436 }
437 
438 static struct mipi_dsi_driver sharp_panel_driver = {
439 	.driver = {
440 		.name = "panel-sharp-lq101r1sx01",
441 		.of_match_table = sharp_of_match,
442 	},
443 	.probe = sharp_panel_probe,
444 	.remove = sharp_panel_remove,
445 	.shutdown = sharp_panel_shutdown,
446 };
447 module_mipi_dsi_driver(sharp_panel_driver);
448 
449 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
450 MODULE_DESCRIPTION("Sharp LQ101R1SX01 panel driver");
451 MODULE_LICENSE("GPL v2");
452