xref: /linux/drivers/gpu/drm/panel/panel-samsung-sofef00.c (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Casey Connolly <casey.connolly@linaro.org>
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 #include <drm/drm_probe_helper.h>
20 
21 #include "panel-samsung-dsi.h"
22 
23 struct sofef00_panel {
24 	struct drm_panel panel;
25 	struct mipi_dsi_device *dsi;
26 	struct regulator_bulk_data *supplies;
27 	struct gpio_desc *reset_gpio;
28 };
29 
30 static const struct regulator_bulk_data sofef00_supplies[] = {
31 	{ .supply = "vddio" },
32 	{ .supply = "vci" },
33 	{ .supply = "poc" },
34 };
35 
36 static inline
37 struct sofef00_panel *to_sofef00_panel(struct drm_panel *panel)
38 {
39 	return container_of(panel, struct sofef00_panel, panel);
40 }
41 
42 static void sofef00_panel_reset(struct sofef00_panel *ctx)
43 {
44 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
45 	usleep_range(5000, 6000);
46 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
47 	usleep_range(2000, 3000);
48 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
49 	usleep_range(12000, 13000);
50 }
51 
52 static int sofef00_panel_on(struct sofef00_panel *ctx)
53 {
54 	struct mipi_dsi_device *dsi = ctx->dsi;
55 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
56 
57 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
58 
59 	mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx);
60 	mipi_dsi_usleep_range(&dsi_ctx, 10000, 11000);
61 
62 	samsung_dsi_test_key_on_lvl2(&dsi_ctx);
63 	mipi_dsi_dcs_set_tear_on_multi(&dsi_ctx, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
64 	samsung_dsi_test_key_off_lvl2(&dsi_ctx);
65 
66 	samsung_dsi_test_key_on_lvl2(&dsi_ctx);
67 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xb0, 0x07);
68 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xb6, 0x12);
69 	samsung_dsi_test_key_off_lvl2(&dsi_ctx);
70 
71 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x20);
72 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
73 
74 	return dsi_ctx.accum_err;
75 }
76 
77 static int sofef00_enable(struct drm_panel *panel)
78 {
79 	struct sofef00_panel *ctx = to_sofef00_panel(panel);
80 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = ctx->dsi };
81 
82 	mipi_dsi_dcs_set_display_on_multi(&dsi_ctx);
83 
84 	return dsi_ctx.accum_err;
85 }
86 
87 static int sofef00_panel_off(struct sofef00_panel *ctx)
88 {
89 	struct mipi_dsi_device *dsi = ctx->dsi;
90 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
91 
92 	mipi_dsi_dcs_set_display_off_multi(&dsi_ctx);
93 	mipi_dsi_msleep(&dsi_ctx, 40);
94 
95 	mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx);
96 	mipi_dsi_msleep(&dsi_ctx, 160);
97 
98 	return dsi_ctx.accum_err;
99 }
100 
101 static int sofef00_panel_prepare(struct drm_panel *panel)
102 {
103 	struct sofef00_panel *ctx = to_sofef00_panel(panel);
104 	int ret;
105 
106 	ret = regulator_bulk_enable(ARRAY_SIZE(sofef00_supplies), ctx->supplies);
107 	if (ret < 0)
108 		return ret;
109 
110 	sofef00_panel_reset(ctx);
111 
112 	ret = sofef00_panel_on(ctx);
113 	if (ret < 0) {
114 		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
115 		regulator_bulk_disable(ARRAY_SIZE(sofef00_supplies), ctx->supplies);
116 		return ret;
117 	}
118 
119 	return 0;
120 }
121 
122 static int sofef00_disable(struct drm_panel *panel)
123 {
124 	struct sofef00_panel *ctx = to_sofef00_panel(panel);
125 
126 	sofef00_panel_off(ctx);
127 
128 	return 0;
129 }
130 
131 static int sofef00_panel_unprepare(struct drm_panel *panel)
132 {
133 	struct sofef00_panel *ctx = to_sofef00_panel(panel);
134 
135 	regulator_bulk_disable(ARRAY_SIZE(sofef00_supplies), ctx->supplies);
136 
137 	return 0;
138 }
139 
140 static const struct drm_display_mode ams628nw01_panel_mode = {
141 	.clock = (1080 + 112 + 16 + 36) * (2280 + 36 + 8 + 12) * 60 / 1000,
142 
143 	.hdisplay = 1080,
144 	.hsync_start = 1080 + 112,
145 	.hsync_end = 1080 + 112 + 16,
146 	.htotal = 1080 + 112 + 16 + 36,
147 
148 	.vdisplay = 2280,
149 	.vsync_start = 2280 + 36,
150 	.vsync_end = 2280 + 36 + 8,
151 	.vtotal = 2280 + 36 + 8 + 12,
152 
153 	.width_mm = 68,
154 	.height_mm = 145,
155 
156 	.type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
157 };
158 
159 static int sofef00_panel_get_modes(struct drm_panel *panel, struct drm_connector *connector)
160 {
161 	return drm_connector_helper_get_modes_fixed(connector, &ams628nw01_panel_mode);
162 }
163 
164 static const struct drm_panel_funcs sofef00_panel_panel_funcs = {
165 	.prepare = sofef00_panel_prepare,
166 	.enable = sofef00_enable,
167 	.disable = sofef00_disable,
168 	.unprepare = sofef00_panel_unprepare,
169 	.get_modes = sofef00_panel_get_modes,
170 };
171 
172 static int sofef00_panel_bl_update_status(struct backlight_device *bl)
173 {
174 	struct mipi_dsi_device *dsi = bl_get_data(bl);
175 	struct sofef00_panel *ctx = mipi_dsi_get_drvdata(dsi);
176 	int err;
177 	u16 brightness = (u16)backlight_get_brightness(bl);
178 
179 	if (!ctx->panel.prepared)
180 		return 0;
181 
182 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
183 
184 	err = mipi_dsi_dcs_set_display_brightness_large(dsi, brightness);
185 	if (err < 0)
186 		return err;
187 
188 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
189 
190 	return 0;
191 }
192 
193 static const struct backlight_ops sofef00_panel_bl_ops = {
194 	.update_status = sofef00_panel_bl_update_status,
195 };
196 
197 static struct backlight_device *
198 sofef00_create_backlight(struct mipi_dsi_device *dsi)
199 {
200 	struct device *dev = &dsi->dev;
201 	const struct backlight_properties props = {
202 		.type = BACKLIGHT_PLATFORM,
203 		.brightness = 512,
204 		.max_brightness = 1023,
205 	};
206 
207 	return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
208 					      &sofef00_panel_bl_ops, &props);
209 }
210 
211 static int sofef00_panel_probe(struct mipi_dsi_device *dsi)
212 {
213 	struct device *dev = &dsi->dev;
214 	struct sofef00_panel *ctx;
215 	int ret;
216 
217 	ctx = devm_drm_panel_alloc(dev, struct sofef00_panel, panel,
218 				   &sofef00_panel_panel_funcs,
219 				   DRM_MODE_CONNECTOR_DSI);
220 	if (IS_ERR(ctx))
221 		return PTR_ERR(ctx);
222 
223 	ret = devm_regulator_bulk_get_const(dev,
224 					    ARRAY_SIZE(sofef00_supplies),
225 					    sofef00_supplies,
226 					    &ctx->supplies);
227 	if (ret)
228 		return dev_err_probe(dev, ret, "Failed to get regulators\n");
229 
230 	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
231 	if (IS_ERR(ctx->reset_gpio))
232 		return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
233 				     "Failed to get reset-gpios\n");
234 
235 	ctx->dsi = dsi;
236 	mipi_dsi_set_drvdata(dsi, ctx);
237 
238 	dsi->lanes = 4;
239 	dsi->format = MIPI_DSI_FMT_RGB888;
240 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO_BURST |
241 			  MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM;
242 
243 	ctx->panel.prepare_prev_first = true;
244 
245 	ctx->panel.backlight = sofef00_create_backlight(dsi);
246 	if (IS_ERR(ctx->panel.backlight))
247 		return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
248 				     "Failed to create backlight\n");
249 
250 	ret = devm_drm_panel_add(dev, &ctx->panel);
251 	if (ret)
252 		return ret;
253 
254 	ret = devm_mipi_dsi_attach(dev, dsi);
255 	if (ret < 0) {
256 		dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
257 		return ret;
258 	}
259 
260 	return 0;
261 }
262 
263 static const struct of_device_id sofef00_panel_of_match[] = {
264 	{ .compatible = "samsung,sofef00" }, /* legacy */
265 	{ .compatible = "samsung,sofef00-ams628nw01" },
266 	{ /* sentinel */ }
267 };
268 MODULE_DEVICE_TABLE(of, sofef00_panel_of_match);
269 
270 static struct mipi_dsi_driver sofef00_panel_driver = {
271 	.probe = sofef00_panel_probe,
272 	.driver = {
273 		.name = "panel-samsung-sofef00",
274 		.of_match_table = sofef00_panel_of_match,
275 	},
276 };
277 
278 module_mipi_dsi_driver(sofef00_panel_driver);
279 
280 MODULE_AUTHOR("Casey Connolly <casey.connolly@linaro.org>");
281 MODULE_DESCRIPTION("DRM driver for Samsung SOFEF00 DDIC");
282 MODULE_LICENSE("GPL v2");
283