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