xref: /linux/drivers/gpu/drm/panel/panel-sharp-ls043t1le01.c (revision 25489a4f556414445d342951615178368ee45cde)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 Red Hat
4  * Copyright (C) 2015 Sony Mobile Communications Inc.
5  * Author: Werner Johansson <werner.johansson@sonymobile.com>
6  *
7  * Based on AUO panel driver by Rob Clark <robdclark@gmail.com>
8  */
9 
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_nt_panel {
24 	struct drm_panel base;
25 	struct mipi_dsi_device *dsi;
26 
27 	struct regulator *supply;
28 	struct gpio_desc *reset_gpio;
29 };
30 
31 static inline struct sharp_nt_panel *to_sharp_nt_panel(struct drm_panel *panel)
32 {
33 	return container_of(panel, struct sharp_nt_panel, base);
34 }
35 
36 static int sharp_nt_panel_init(struct sharp_nt_panel *sharp_nt)
37 {
38 	struct mipi_dsi_device *dsi = sharp_nt->dsi;
39 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
40 
41 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
42 
43 	mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx);
44 
45 	mipi_dsi_msleep(&dsi_ctx, 120);
46 
47 	/* Novatek two-lane operation */
48 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xae,  0x03);
49 
50 	/* Set both MCU and RGB I/F to 24bpp */
51 	mipi_dsi_dcs_set_pixel_format_multi(&dsi_ctx,
52 					    MIPI_DCS_PIXEL_FMT_24BIT |
53 					    (MIPI_DCS_PIXEL_FMT_24BIT << 4));
54 
55 	return dsi_ctx.accum_err;
56 }
57 
58 static int sharp_nt_panel_on(struct sharp_nt_panel *sharp_nt)
59 {
60 	struct mipi_dsi_device *dsi = sharp_nt->dsi;
61 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
62 
63 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
64 
65 	mipi_dsi_dcs_set_display_on_multi(&dsi_ctx);
66 
67 	return dsi_ctx.accum_err;
68 }
69 
70 static int sharp_nt_panel_off(struct sharp_nt_panel *sharp_nt)
71 {
72 	struct mipi_dsi_device *dsi = sharp_nt->dsi;
73 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
74 
75 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
76 
77 	mipi_dsi_dcs_set_display_off_multi(&dsi_ctx);
78 
79 	mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx);
80 
81 	return dsi_ctx.accum_err;
82 }
83 
84 static int sharp_nt_panel_unprepare(struct drm_panel *panel)
85 {
86 	struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
87 	int ret;
88 
89 	ret = sharp_nt_panel_off(sharp_nt);
90 	if (ret < 0) {
91 		dev_err(panel->dev, "failed to set panel off: %d\n", ret);
92 		return ret;
93 	}
94 
95 	regulator_disable(sharp_nt->supply);
96 	if (sharp_nt->reset_gpio)
97 		gpiod_set_value(sharp_nt->reset_gpio, 0);
98 
99 	return 0;
100 }
101 
102 static int sharp_nt_panel_prepare(struct drm_panel *panel)
103 {
104 	struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
105 	int ret;
106 
107 	ret = regulator_enable(sharp_nt->supply);
108 	if (ret < 0)
109 		return ret;
110 
111 	msleep(20);
112 
113 	if (sharp_nt->reset_gpio) {
114 		gpiod_set_value(sharp_nt->reset_gpio, 1);
115 		msleep(1);
116 		gpiod_set_value(sharp_nt->reset_gpio, 0);
117 		msleep(1);
118 		gpiod_set_value(sharp_nt->reset_gpio, 1);
119 		msleep(10);
120 	}
121 
122 	ret = sharp_nt_panel_init(sharp_nt);
123 	if (ret < 0) {
124 		dev_err(panel->dev, "failed to init panel: %d\n", ret);
125 		goto poweroff;
126 	}
127 
128 	ret = sharp_nt_panel_on(sharp_nt);
129 	if (ret < 0) {
130 		dev_err(panel->dev, "failed to set panel on: %d\n", ret);
131 		goto poweroff;
132 	}
133 
134 	return 0;
135 
136 poweroff:
137 	regulator_disable(sharp_nt->supply);
138 	if (sharp_nt->reset_gpio)
139 		gpiod_set_value(sharp_nt->reset_gpio, 0);
140 	return ret;
141 }
142 
143 static const struct drm_display_mode default_mode = {
144 	.clock = (540 + 48 + 32 + 80) * (960 + 3 + 10 + 15) * 60 / 1000,
145 	.hdisplay = 540,
146 	.hsync_start = 540 + 48,
147 	.hsync_end = 540 + 48 + 32,
148 	.htotal = 540 + 48 + 32 + 80,
149 	.vdisplay = 960,
150 	.vsync_start = 960 + 3,
151 	.vsync_end = 960 + 3 + 10,
152 	.vtotal = 960 + 3 + 10 + 15,
153 };
154 
155 static int sharp_nt_panel_get_modes(struct drm_panel *panel,
156 				    struct drm_connector *connector)
157 {
158 	struct drm_display_mode *mode;
159 
160 	mode = drm_mode_duplicate(connector->dev, &default_mode);
161 	if (!mode) {
162 		dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
163 			default_mode.hdisplay, default_mode.vdisplay,
164 			drm_mode_vrefresh(&default_mode));
165 		return -ENOMEM;
166 	}
167 
168 	drm_mode_set_name(mode);
169 
170 	drm_mode_probed_add(connector, mode);
171 
172 	connector->display_info.width_mm = 54;
173 	connector->display_info.height_mm = 95;
174 
175 	return 1;
176 }
177 
178 static const struct drm_panel_funcs sharp_nt_panel_funcs = {
179 	.unprepare = sharp_nt_panel_unprepare,
180 	.prepare = sharp_nt_panel_prepare,
181 	.get_modes = sharp_nt_panel_get_modes,
182 };
183 
184 static int sharp_nt_panel_add(struct sharp_nt_panel *sharp_nt)
185 {
186 	struct device *dev = &sharp_nt->dsi->dev;
187 	int ret;
188 
189 	sharp_nt->supply = devm_regulator_get(dev, "avdd");
190 	if (IS_ERR(sharp_nt->supply))
191 		return PTR_ERR(sharp_nt->supply);
192 
193 	sharp_nt->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
194 	if (IS_ERR(sharp_nt->reset_gpio)) {
195 		dev_err(dev, "cannot get reset-gpios %ld\n",
196 			PTR_ERR(sharp_nt->reset_gpio));
197 		sharp_nt->reset_gpio = NULL;
198 	} else {
199 		gpiod_set_value(sharp_nt->reset_gpio, 0);
200 	}
201 
202 	drm_panel_init(&sharp_nt->base, &sharp_nt->dsi->dev,
203 		       &sharp_nt_panel_funcs, DRM_MODE_CONNECTOR_DSI);
204 
205 	ret = drm_panel_of_backlight(&sharp_nt->base);
206 	if (ret)
207 		return ret;
208 
209 	drm_panel_add(&sharp_nt->base);
210 
211 	return 0;
212 }
213 
214 static void sharp_nt_panel_del(struct sharp_nt_panel *sharp_nt)
215 {
216 	if (sharp_nt->base.dev)
217 		drm_panel_remove(&sharp_nt->base);
218 }
219 
220 static int sharp_nt_panel_probe(struct mipi_dsi_device *dsi)
221 {
222 	struct sharp_nt_panel *sharp_nt;
223 	int ret;
224 
225 	dsi->lanes = 2;
226 	dsi->format = MIPI_DSI_FMT_RGB888;
227 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
228 			MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
229 			MIPI_DSI_MODE_VIDEO_HSE |
230 			MIPI_DSI_CLOCK_NON_CONTINUOUS |
231 			MIPI_DSI_MODE_NO_EOT_PACKET;
232 
233 	sharp_nt = devm_kzalloc(&dsi->dev, sizeof(*sharp_nt), GFP_KERNEL);
234 	if (!sharp_nt)
235 		return -ENOMEM;
236 
237 	mipi_dsi_set_drvdata(dsi, sharp_nt);
238 
239 	sharp_nt->dsi = dsi;
240 
241 	ret = sharp_nt_panel_add(sharp_nt);
242 	if (ret < 0)
243 		return ret;
244 
245 	ret = mipi_dsi_attach(dsi);
246 	if (ret < 0) {
247 		sharp_nt_panel_del(sharp_nt);
248 		return ret;
249 	}
250 
251 	return 0;
252 }
253 
254 static void sharp_nt_panel_remove(struct mipi_dsi_device *dsi)
255 {
256 	struct sharp_nt_panel *sharp_nt = mipi_dsi_get_drvdata(dsi);
257 	int ret;
258 
259 	ret = mipi_dsi_detach(dsi);
260 	if (ret < 0)
261 		dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
262 
263 	sharp_nt_panel_del(sharp_nt);
264 }
265 
266 static const struct of_device_id sharp_nt_of_match[] = {
267 	{ .compatible = "sharp,ls043t1le01-qhd", },
268 	{ }
269 };
270 MODULE_DEVICE_TABLE(of, sharp_nt_of_match);
271 
272 static struct mipi_dsi_driver sharp_nt_panel_driver = {
273 	.driver = {
274 		.name = "panel-sharp-ls043t1le01-qhd",
275 		.of_match_table = sharp_nt_of_match,
276 	},
277 	.probe = sharp_nt_panel_probe,
278 	.remove = sharp_nt_panel_remove,
279 };
280 module_mipi_dsi_driver(sharp_nt_panel_driver);
281 
282 MODULE_AUTHOR("Werner Johansson <werner.johansson@sonymobile.com>");
283 MODULE_DESCRIPTION("Sharp LS043T1LE01 NT35565-based qHD (540x960) video mode panel driver");
284 MODULE_LICENSE("GPL v2");
285