1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Caleb Connolly <caleb@connolly.tech>
3  * Generated with linux-mdss-dsi-panel-driver-generator from vendor device tree:
4  * Copyright (c) 2020, The Linux Foundation. All rights reserved.
5  */
6 
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/regulator/consumer.h>
12 #include <linux/backlight.h>
13 
14 #include <video/mipi_display.h>
15 
16 #include <drm/drm_mipi_dsi.h>
17 #include <drm/drm_modes.h>
18 #include <drm/drm_panel.h>
19 
20 struct sofef00_panel {
21 	struct drm_panel panel;
22 	struct mipi_dsi_device *dsi;
23 	struct regulator *supply;
24 	struct gpio_desc *reset_gpio;
25 	const struct drm_display_mode *mode;
26 	bool prepared;
27 };
28 
29 static inline
30 struct sofef00_panel *to_sofef00_panel(struct drm_panel *panel)
31 {
32 	return container_of(panel, struct sofef00_panel, panel);
33 }
34 
35 static void sofef00_panel_reset(struct sofef00_panel *ctx)
36 {
37 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
38 	usleep_range(5000, 6000);
39 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
40 	usleep_range(2000, 3000);
41 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
42 	usleep_range(12000, 13000);
43 }
44 
45 static int sofef00_panel_on(struct sofef00_panel *ctx)
46 {
47 	struct mipi_dsi_device *dsi = ctx->dsi;
48 	struct device *dev = &dsi->dev;
49 	int ret;
50 
51 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
52 
53 	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
54 	if (ret < 0) {
55 		dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
56 		return ret;
57 	}
58 	usleep_range(10000, 11000);
59 
60 	mipi_dsi_dcs_write_seq(dsi, 0xf0, 0x5a, 0x5a);
61 
62 	ret = mipi_dsi_dcs_set_tear_on(dsi, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
63 	if (ret < 0) {
64 		dev_err(dev, "Failed to set tear on: %d\n", ret);
65 		return ret;
66 	}
67 
68 	mipi_dsi_dcs_write_seq(dsi, 0xf0, 0xa5, 0xa5);
69 	mipi_dsi_dcs_write_seq(dsi, 0xf0, 0x5a, 0x5a);
70 	mipi_dsi_dcs_write_seq(dsi, 0xb0, 0x07);
71 	mipi_dsi_dcs_write_seq(dsi, 0xb6, 0x12);
72 	mipi_dsi_dcs_write_seq(dsi, 0xf0, 0xa5, 0xa5);
73 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x20);
74 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
75 
76 	ret = mipi_dsi_dcs_set_display_on(dsi);
77 	if (ret < 0) {
78 		dev_err(dev, "Failed to set display on: %d\n", ret);
79 		return ret;
80 	}
81 
82 	return 0;
83 }
84 
85 static int sofef00_panel_off(struct sofef00_panel *ctx)
86 {
87 	struct mipi_dsi_device *dsi = ctx->dsi;
88 	struct device *dev = &dsi->dev;
89 	int ret;
90 
91 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
92 
93 	ret = mipi_dsi_dcs_set_display_off(dsi);
94 	if (ret < 0) {
95 		dev_err(dev, "Failed to set display off: %d\n", ret);
96 		return ret;
97 	}
98 	msleep(40);
99 
100 	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
101 	if (ret < 0) {
102 		dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
103 		return ret;
104 	}
105 	msleep(160);
106 
107 	return 0;
108 }
109 
110 static int sofef00_panel_prepare(struct drm_panel *panel)
111 {
112 	struct sofef00_panel *ctx = to_sofef00_panel(panel);
113 	struct device *dev = &ctx->dsi->dev;
114 	int ret;
115 
116 	if (ctx->prepared)
117 		return 0;
118 
119 	ret = regulator_enable(ctx->supply);
120 	if (ret < 0) {
121 		dev_err(dev, "Failed to enable regulator: %d\n", ret);
122 		return ret;
123 	}
124 
125 	sofef00_panel_reset(ctx);
126 
127 	ret = sofef00_panel_on(ctx);
128 	if (ret < 0) {
129 		dev_err(dev, "Failed to initialize panel: %d\n", ret);
130 		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
131 		return ret;
132 	}
133 
134 	ctx->prepared = true;
135 	return 0;
136 }
137 
138 static int sofef00_panel_unprepare(struct drm_panel *panel)
139 {
140 	struct sofef00_panel *ctx = to_sofef00_panel(panel);
141 	struct device *dev = &ctx->dsi->dev;
142 	int ret;
143 
144 	if (!ctx->prepared)
145 		return 0;
146 
147 	ret = sofef00_panel_off(ctx);
148 	if (ret < 0)
149 		dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
150 
151 	regulator_disable(ctx->supply);
152 
153 	ctx->prepared = false;
154 	return 0;
155 }
156 
157 static const struct drm_display_mode enchilada_panel_mode = {
158 	.clock = (1080 + 112 + 16 + 36) * (2280 + 36 + 8 + 12) * 60 / 1000,
159 	.hdisplay = 1080,
160 	.hsync_start = 1080 + 112,
161 	.hsync_end = 1080 + 112 + 16,
162 	.htotal = 1080 + 112 + 16 + 36,
163 	.vdisplay = 2280,
164 	.vsync_start = 2280 + 36,
165 	.vsync_end = 2280 + 36 + 8,
166 	.vtotal = 2280 + 36 + 8 + 12,
167 	.width_mm = 68,
168 	.height_mm = 145,
169 };
170 
171 static const struct drm_display_mode fajita_panel_mode = {
172 	.clock = (1080 + 72 + 16 + 36) * (2340 + 32 + 4 + 18) * 60 / 1000,
173 	.hdisplay = 1080,
174 	.hsync_start = 1080 + 72,
175 	.hsync_end = 1080 + 72 + 16,
176 	.htotal = 1080 + 72 + 16 + 36,
177 	.vdisplay = 2340,
178 	.vsync_start = 2340 + 32,
179 	.vsync_end = 2340 + 32 + 4,
180 	.vtotal = 2340 + 32 + 4 + 18,
181 	.width_mm = 68,
182 	.height_mm = 145,
183 };
184 
185 static int sofef00_panel_get_modes(struct drm_panel *panel, struct drm_connector *connector)
186 {
187 	struct drm_display_mode *mode;
188 	struct sofef00_panel *ctx = to_sofef00_panel(panel);
189 
190 	mode = drm_mode_duplicate(connector->dev, ctx->mode);
191 	if (!mode)
192 		return -ENOMEM;
193 
194 	drm_mode_set_name(mode);
195 
196 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
197 	connector->display_info.width_mm = mode->width_mm;
198 	connector->display_info.height_mm = mode->height_mm;
199 	drm_mode_probed_add(connector, mode);
200 
201 	return 1;
202 }
203 
204 static const struct drm_panel_funcs sofef00_panel_panel_funcs = {
205 	.prepare = sofef00_panel_prepare,
206 	.unprepare = sofef00_panel_unprepare,
207 	.get_modes = sofef00_panel_get_modes,
208 };
209 
210 static int sofef00_panel_bl_update_status(struct backlight_device *bl)
211 {
212 	struct mipi_dsi_device *dsi = bl_get_data(bl);
213 	int err;
214 	u16 brightness = (u16)backlight_get_brightness(bl);
215 
216 	err = mipi_dsi_dcs_set_display_brightness_large(dsi, brightness);
217 	if (err < 0)
218 		return err;
219 
220 	return 0;
221 }
222 
223 static const struct backlight_ops sofef00_panel_bl_ops = {
224 	.update_status = sofef00_panel_bl_update_status,
225 };
226 
227 static struct backlight_device *
228 sofef00_create_backlight(struct mipi_dsi_device *dsi)
229 {
230 	struct device *dev = &dsi->dev;
231 	const struct backlight_properties props = {
232 		.type = BACKLIGHT_PLATFORM,
233 		.brightness = 1023,
234 		.max_brightness = 1023,
235 	};
236 
237 	return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
238 					      &sofef00_panel_bl_ops, &props);
239 }
240 
241 static int sofef00_panel_probe(struct mipi_dsi_device *dsi)
242 {
243 	struct device *dev = &dsi->dev;
244 	struct sofef00_panel *ctx;
245 	int ret;
246 
247 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
248 	if (!ctx)
249 		return -ENOMEM;
250 
251 	ctx->mode = of_device_get_match_data(dev);
252 
253 	if (!ctx->mode) {
254 		dev_err(dev, "Missing device mode\n");
255 		return -ENODEV;
256 	}
257 
258 	ctx->supply = devm_regulator_get(dev, "vddio");
259 	if (IS_ERR(ctx->supply))
260 		return dev_err_probe(dev, PTR_ERR(ctx->supply),
261 				     "Failed to get vddio regulator\n");
262 
263 	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
264 	if (IS_ERR(ctx->reset_gpio))
265 		return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
266 				     "Failed to get reset-gpios\n");
267 
268 	ctx->dsi = dsi;
269 	mipi_dsi_set_drvdata(dsi, ctx);
270 
271 	dsi->lanes = 4;
272 	dsi->format = MIPI_DSI_FMT_RGB888;
273 
274 	drm_panel_init(&ctx->panel, dev, &sofef00_panel_panel_funcs,
275 		       DRM_MODE_CONNECTOR_DSI);
276 
277 	ctx->panel.backlight = sofef00_create_backlight(dsi);
278 	if (IS_ERR(ctx->panel.backlight))
279 		return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
280 				     "Failed to create backlight\n");
281 
282 	drm_panel_add(&ctx->panel);
283 
284 	ret = mipi_dsi_attach(dsi);
285 	if (ret < 0) {
286 		dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
287 		drm_panel_remove(&ctx->panel);
288 		return ret;
289 	}
290 
291 	return 0;
292 }
293 
294 static void sofef00_panel_remove(struct mipi_dsi_device *dsi)
295 {
296 	struct sofef00_panel *ctx = mipi_dsi_get_drvdata(dsi);
297 	int ret;
298 
299 	ret = mipi_dsi_detach(dsi);
300 	if (ret < 0)
301 		dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
302 
303 	drm_panel_remove(&ctx->panel);
304 }
305 
306 static const struct of_device_id sofef00_panel_of_match[] = {
307 	{ // OnePlus 6 / enchilada
308 		.compatible = "samsung,sofef00",
309 		.data = &enchilada_panel_mode,
310 	},
311 	{ // OnePlus 6T / fajita
312 		.compatible = "samsung,s6e3fc2x01",
313 		.data = &fajita_panel_mode,
314 	},
315 	{ /* sentinel */ }
316 };
317 MODULE_DEVICE_TABLE(of, sofef00_panel_of_match);
318 
319 static struct mipi_dsi_driver sofef00_panel_driver = {
320 	.probe = sofef00_panel_probe,
321 	.remove = sofef00_panel_remove,
322 	.driver = {
323 		.name = "panel-oneplus6",
324 		.of_match_table = sofef00_panel_of_match,
325 	},
326 };
327 
328 module_mipi_dsi_driver(sofef00_panel_driver);
329 
330 MODULE_AUTHOR("Caleb Connolly <caleb@connolly.tech>");
331 MODULE_DESCRIPTION("DRM driver for Samsung AMOLED DSI panels found in OnePlus 6/6T phones");
332 MODULE_LICENSE("GPL v2");
333