1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * BOE BF060Y8M-AJ0 5.99" MIPI-DSI OLED Panel on SW43404 DriverIC
4  *
5  * Copyright (c) 2020 AngeloGioacchino Del Regno
6  *                    <angelogioacchino.delregno@somainline.org>
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 #include <video/mipi_display.h>
16 #include <drm/drm_mipi_dsi.h>
17 #include <drm/drm_modes.h>
18 #include <drm/drm_panel.h>
19 
20 #define DCS_ALLOW_HBM_RANGE		0x0c
21 #define DCS_DISALLOW_HBM_RANGE		0x08
22 
23 enum boe_bf060y8m_aj0_supplies {
24 	BF060Y8M_VREG_VCC,
25 	BF060Y8M_VREG_VDDIO,
26 	BF060Y8M_VREG_VCI,
27 	BF060Y8M_VREG_EL_VDD,
28 	BF060Y8M_VREG_EL_VSS,
29 	BF060Y8M_VREG_MAX
30 };
31 
32 struct boe_bf060y8m_aj0 {
33 	struct drm_panel panel;
34 	struct mipi_dsi_device *dsi;
35 	struct regulator_bulk_data vregs[BF060Y8M_VREG_MAX];
36 	struct gpio_desc *reset_gpio;
37 	bool prepared;
38 };
39 
40 static inline
41 struct boe_bf060y8m_aj0 *to_boe_bf060y8m_aj0(struct drm_panel *panel)
42 {
43 	return container_of(panel, struct boe_bf060y8m_aj0, panel);
44 }
45 
46 static void boe_bf060y8m_aj0_reset(struct boe_bf060y8m_aj0 *boe)
47 {
48 	gpiod_set_value_cansleep(boe->reset_gpio, 0);
49 	usleep_range(2000, 3000);
50 	gpiod_set_value_cansleep(boe->reset_gpio, 1);
51 	usleep_range(15000, 16000);
52 	gpiod_set_value_cansleep(boe->reset_gpio, 0);
53 	usleep_range(5000, 6000);
54 }
55 
56 static int boe_bf060y8m_aj0_on(struct boe_bf060y8m_aj0 *boe)
57 {
58 	struct mipi_dsi_device *dsi = boe->dsi;
59 	struct device *dev = &dsi->dev;
60 	int ret;
61 
62 	mipi_dsi_dcs_write_seq(dsi, 0xb0, 0xa5, 0x00);
63 	mipi_dsi_dcs_write_seq(dsi, 0xb2, 0x00, 0x4c);
64 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_3D_CONTROL, 0x10);
65 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_POWER_SAVE, DCS_ALLOW_HBM_RANGE);
66 	mipi_dsi_dcs_write_seq(dsi, 0xf8,
67 			       0x00, 0x08, 0x10, 0x00, 0x22, 0x00, 0x00, 0x2d);
68 
69 	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
70 	if (ret < 0) {
71 		dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
72 		return ret;
73 	}
74 	msleep(30);
75 
76 	mipi_dsi_dcs_write_seq(dsi, 0xb0, 0xa5, 0x00);
77 	mipi_dsi_dcs_write_seq(dsi, 0xc0,
78 			       0x08, 0x48, 0x65, 0x33, 0x33, 0x33,
79 			       0x2a, 0x31, 0x39, 0x20, 0x09);
80 	mipi_dsi_dcs_write_seq(dsi, 0xc1, 0x00, 0x00, 0x00, 0x1f, 0x1f,
81 			       0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
82 			       0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f);
83 	mipi_dsi_dcs_write_seq(dsi, 0xe2, 0x20, 0x04, 0x10, 0x12, 0x92,
84 			       0x4f, 0x8f, 0x44, 0x84, 0x83, 0x83, 0x83,
85 			       0x5c, 0x5c, 0x5c);
86 	mipi_dsi_dcs_write_seq(dsi, 0xde, 0x01, 0x2c, 0x00, 0x77, 0x3e);
87 
88 	msleep(30);
89 
90 	ret = mipi_dsi_dcs_set_display_on(dsi);
91 	if (ret < 0) {
92 		dev_err(dev, "Failed to set display on: %d\n", ret);
93 		return ret;
94 	}
95 	msleep(50);
96 
97 	return 0;
98 }
99 
100 static int boe_bf060y8m_aj0_off(struct boe_bf060y8m_aj0 *boe)
101 {
102 	struct mipi_dsi_device *dsi = boe->dsi;
103 	struct device *dev = &dsi->dev;
104 	int ret;
105 
106 	/* OFF commands sent in HS mode */
107 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
108 	ret = mipi_dsi_dcs_set_display_off(dsi);
109 	if (ret < 0) {
110 		dev_err(dev, "Failed to set display off: %d\n", ret);
111 		return ret;
112 	}
113 	msleep(20);
114 
115 	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
116 	if (ret < 0) {
117 		dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
118 		return ret;
119 	}
120 	usleep_range(1000, 2000);
121 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
122 
123 	return 0;
124 }
125 
126 static int boe_bf060y8m_aj0_prepare(struct drm_panel *panel)
127 {
128 	struct boe_bf060y8m_aj0 *boe = to_boe_bf060y8m_aj0(panel);
129 	struct device *dev = &boe->dsi->dev;
130 	int ret;
131 
132 	if (boe->prepared)
133 		return 0;
134 
135 	/*
136 	 * Enable EL Driving Voltage first - doing that at the beginning
137 	 * or at the end of the power sequence doesn't matter, so enable
138 	 * it here to avoid yet another usleep at the end.
139 	 */
140 	ret = regulator_enable(boe->vregs[BF060Y8M_VREG_EL_VDD].consumer);
141 	if (ret)
142 		return ret;
143 	ret = regulator_enable(boe->vregs[BF060Y8M_VREG_EL_VSS].consumer);
144 	if (ret)
145 		goto err_elvss;
146 
147 	ret = regulator_enable(boe->vregs[BF060Y8M_VREG_VCC].consumer);
148 	if (ret)
149 		goto err_vcc;
150 	usleep_range(1000, 2000);
151 	ret = regulator_enable(boe->vregs[BF060Y8M_VREG_VDDIO].consumer);
152 	if (ret)
153 		goto err_vddio;
154 	usleep_range(500, 1000);
155 	ret = regulator_enable(boe->vregs[BF060Y8M_VREG_VCI].consumer);
156 	if (ret)
157 		goto err_vci;
158 	usleep_range(2000, 3000);
159 
160 	boe_bf060y8m_aj0_reset(boe);
161 
162 	ret = boe_bf060y8m_aj0_on(boe);
163 	if (ret < 0) {
164 		dev_err(dev, "Failed to initialize panel: %d\n", ret);
165 		gpiod_set_value_cansleep(boe->reset_gpio, 1);
166 		return ret;
167 	}
168 
169 	boe->prepared = true;
170 	return 0;
171 
172 err_vci:
173 	regulator_disable(boe->vregs[BF060Y8M_VREG_VDDIO].consumer);
174 err_vddio:
175 	regulator_disable(boe->vregs[BF060Y8M_VREG_VCC].consumer);
176 err_vcc:
177 	regulator_disable(boe->vregs[BF060Y8M_VREG_EL_VSS].consumer);
178 err_elvss:
179 	regulator_disable(boe->vregs[BF060Y8M_VREG_EL_VDD].consumer);
180 	return ret;
181 }
182 
183 static int boe_bf060y8m_aj0_unprepare(struct drm_panel *panel)
184 {
185 	struct boe_bf060y8m_aj0 *boe = to_boe_bf060y8m_aj0(panel);
186 	struct device *dev = &boe->dsi->dev;
187 	int ret;
188 
189 	if (!boe->prepared)
190 		return 0;
191 
192 	ret = boe_bf060y8m_aj0_off(boe);
193 	if (ret < 0)
194 		dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
195 
196 	gpiod_set_value_cansleep(boe->reset_gpio, 1);
197 	ret = regulator_bulk_disable(ARRAY_SIZE(boe->vregs), boe->vregs);
198 
199 	boe->prepared = false;
200 	return 0;
201 }
202 
203 static const struct drm_display_mode boe_bf060y8m_aj0_mode = {
204 	.clock = 165268,
205 	.hdisplay = 1080,
206 	.hsync_start = 1080 + 36,
207 	.hsync_end = 1080 + 36 + 24,
208 	.htotal = 1080 + 36 + 24 + 96,
209 	.vdisplay = 2160,
210 	.vsync_start = 2160 + 16,
211 	.vsync_end = 2160 + 16 + 1,
212 	.vtotal = 2160 + 16 + 1 + 15,
213 	.width_mm = 68,   /* 68.04 mm */
214 	.height_mm = 136, /* 136.08 mm */
215 };
216 
217 static int boe_bf060y8m_aj0_get_modes(struct drm_panel *panel,
218 				      struct drm_connector *connector)
219 {
220 	struct drm_display_mode *mode;
221 
222 	mode = drm_mode_duplicate(connector->dev, &boe_bf060y8m_aj0_mode);
223 	if (!mode)
224 		return -ENOMEM;
225 
226 	drm_mode_set_name(mode);
227 
228 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
229 	connector->display_info.width_mm = mode->width_mm;
230 	connector->display_info.height_mm = mode->height_mm;
231 	drm_mode_probed_add(connector, mode);
232 
233 	return 1;
234 }
235 
236 static const struct drm_panel_funcs boe_bf060y8m_aj0_panel_funcs = {
237 	.prepare = boe_bf060y8m_aj0_prepare,
238 	.unprepare = boe_bf060y8m_aj0_unprepare,
239 	.get_modes = boe_bf060y8m_aj0_get_modes,
240 };
241 
242 static int boe_bf060y8m_aj0_bl_update_status(struct backlight_device *bl)
243 {
244 	struct mipi_dsi_device *dsi = bl_get_data(bl);
245 	u16 brightness = backlight_get_brightness(bl);
246 	int ret;
247 
248 	ret = mipi_dsi_dcs_set_display_brightness(dsi, brightness);
249 	if (ret < 0)
250 		return ret;
251 
252 	return 0;
253 }
254 
255 static int boe_bf060y8m_aj0_bl_get_brightness(struct backlight_device *bl)
256 {
257 	struct mipi_dsi_device *dsi = bl_get_data(bl);
258 	u16 brightness;
259 	int ret;
260 
261 	ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness);
262 	if (ret < 0)
263 		return ret;
264 
265 	return brightness & 0xff;
266 }
267 
268 static const struct backlight_ops boe_bf060y8m_aj0_bl_ops = {
269 	.update_status = boe_bf060y8m_aj0_bl_update_status,
270 	.get_brightness = boe_bf060y8m_aj0_bl_get_brightness,
271 };
272 
273 static struct backlight_device *
274 boe_bf060y8m_aj0_create_backlight(struct mipi_dsi_device *dsi)
275 {
276 	struct device *dev = &dsi->dev;
277 	const struct backlight_properties props = {
278 		.type = BACKLIGHT_RAW,
279 		.brightness = 127,
280 		.max_brightness = 255,
281 		.scale = BACKLIGHT_SCALE_NON_LINEAR,
282 	};
283 
284 	return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
285 					      &boe_bf060y8m_aj0_bl_ops, &props);
286 }
287 
288 static int boe_bf060y8m_aj0_init_vregs(struct boe_bf060y8m_aj0 *boe,
289 				       struct device *dev)
290 {
291 	struct regulator *vreg;
292 	int ret;
293 
294 	boe->vregs[BF060Y8M_VREG_VCC].supply = "vcc";
295 	boe->vregs[BF060Y8M_VREG_VDDIO].supply = "vddio";
296 	boe->vregs[BF060Y8M_VREG_VCI].supply = "vci";
297 	boe->vregs[BF060Y8M_VREG_EL_VDD].supply = "elvdd";
298 	boe->vregs[BF060Y8M_VREG_EL_VSS].supply = "elvss";
299 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(boe->vregs),
300 				      boe->vregs);
301 	if (ret < 0) {
302 		dev_err(dev, "Failed to get regulators: %d\n", ret);
303 		return ret;
304 	}
305 
306 	vreg = boe->vregs[BF060Y8M_VREG_VCC].consumer;
307 	ret = regulator_is_supported_voltage(vreg, 2700000, 3600000);
308 	if (!ret)
309 		return ret;
310 
311 	vreg = boe->vregs[BF060Y8M_VREG_VDDIO].consumer;
312 	ret = regulator_is_supported_voltage(vreg, 1620000, 1980000);
313 	if (!ret)
314 		return ret;
315 
316 	vreg = boe->vregs[BF060Y8M_VREG_VCI].consumer;
317 	ret = regulator_is_supported_voltage(vreg, 2600000, 3600000);
318 	if (!ret)
319 		return ret;
320 
321 	vreg = boe->vregs[BF060Y8M_VREG_EL_VDD].consumer;
322 	ret = regulator_is_supported_voltage(vreg, 4400000, 4800000);
323 	if (!ret)
324 		return ret;
325 
326 	/* ELVSS is negative: -5.00V to -1.40V */
327 	vreg = boe->vregs[BF060Y8M_VREG_EL_VSS].consumer;
328 	ret = regulator_is_supported_voltage(vreg, 1400000, 5000000);
329 	if (!ret)
330 		return ret;
331 
332 	/*
333 	 * Set min/max rated current, known only for VCI and VDDIO and,
334 	 * in case of failure, just go on gracefully, as this step is not
335 	 * guaranteed to succeed on all regulator HW but do a debug print
336 	 * to inform the developer during debugging.
337 	 * In any case, these two supplies are also optional, so they may
338 	 * be fixed-regulator which, at the time of writing, does not
339 	 * support fake current limiting.
340 	 */
341 	vreg = boe->vregs[BF060Y8M_VREG_VDDIO].consumer;
342 	ret = regulator_set_current_limit(vreg, 1500, 2500);
343 	if (ret)
344 		dev_dbg(dev, "Current limit cannot be set on %s: %d\n",
345 			boe->vregs[1].supply, ret);
346 
347 	vreg = boe->vregs[BF060Y8M_VREG_VCI].consumer;
348 	ret = regulator_set_current_limit(vreg, 20000, 40000);
349 	if (ret)
350 		dev_dbg(dev, "Current limit cannot be set on %s: %d\n",
351 			boe->vregs[2].supply, ret);
352 
353 	return 0;
354 }
355 
356 static int boe_bf060y8m_aj0_probe(struct mipi_dsi_device *dsi)
357 {
358 	struct device *dev = &dsi->dev;
359 	struct boe_bf060y8m_aj0 *boe;
360 	int ret;
361 
362 	boe = devm_kzalloc(dev, sizeof(*boe), GFP_KERNEL);
363 	if (!boe)
364 		return -ENOMEM;
365 
366 	ret = boe_bf060y8m_aj0_init_vregs(boe, dev);
367 	if (ret)
368 		return dev_err_probe(dev, ret,
369 				     "Failed to initialize supplies.\n");
370 
371 	boe->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
372 	if (IS_ERR(boe->reset_gpio))
373 		return dev_err_probe(dev, PTR_ERR(boe->reset_gpio),
374 				     "Failed to get reset-gpios\n");
375 
376 	boe->dsi = dsi;
377 	mipi_dsi_set_drvdata(dsi, boe);
378 
379 	dsi->lanes = 4;
380 	dsi->format = MIPI_DSI_FMT_RGB888;
381 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_NO_EOT_PACKET |
382 			  MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
383 			  MIPI_DSI_CLOCK_NON_CONTINUOUS |
384 			  MIPI_DSI_MODE_LPM;
385 
386 	drm_panel_init(&boe->panel, dev, &boe_bf060y8m_aj0_panel_funcs,
387 		       DRM_MODE_CONNECTOR_DSI);
388 
389 	boe->panel.backlight = boe_bf060y8m_aj0_create_backlight(dsi);
390 	if (IS_ERR(boe->panel.backlight))
391 		return dev_err_probe(dev, PTR_ERR(boe->panel.backlight),
392 				     "Failed to create backlight\n");
393 
394 	drm_panel_add(&boe->panel);
395 
396 	ret = mipi_dsi_attach(dsi);
397 	if (ret < 0) {
398 		dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
399 		return ret;
400 	}
401 
402 	return 0;
403 }
404 
405 static void boe_bf060y8m_aj0_remove(struct mipi_dsi_device *dsi)
406 {
407 	struct boe_bf060y8m_aj0 *boe = mipi_dsi_get_drvdata(dsi);
408 	int ret;
409 
410 	ret = mipi_dsi_detach(dsi);
411 	if (ret < 0)
412 		dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
413 
414 	drm_panel_remove(&boe->panel);
415 }
416 
417 static const struct of_device_id boe_bf060y8m_aj0_of_match[] = {
418 	{ .compatible = "boe,bf060y8m-aj0" },
419 	{ /* sentinel */ }
420 };
421 MODULE_DEVICE_TABLE(of, boe_bf060y8m_aj0_of_match);
422 
423 static struct mipi_dsi_driver boe_bf060y8m_aj0_driver = {
424 	.probe = boe_bf060y8m_aj0_probe,
425 	.remove = boe_bf060y8m_aj0_remove,
426 	.driver = {
427 		.name = "panel-sw43404-boe-fhd-amoled",
428 		.of_match_table = boe_bf060y8m_aj0_of_match,
429 	},
430 };
431 module_mipi_dsi_driver(boe_bf060y8m_aj0_driver);
432 
433 MODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@somainline.org>");
434 MODULE_DESCRIPTION("BOE BF060Y8M-AJ0 MIPI-DSI OLED panel");
435 MODULE_LICENSE("GPL v2");
436