xref: /openbmc/linux/drivers/gpu/drm/panel/panel-samsung-ld9040.c (revision 4984dd069f2995f239f075199ee8c0d9f020bcd9)
1 /*
2  * ld9040 AMOLED LCD drm_panel driver.
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd
5  * Derived from drivers/video/backlight/ld9040.c
6  *
7  * Andrzej Hajda <a.hajda@samsung.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12 */
13 
14 #include <linux/delay.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/spi/spi.h>
20 
21 #include <video/mipi_display.h>
22 #include <video/of_videomode.h>
23 #include <video/videomode.h>
24 
25 #include <drm/drm_modes.h>
26 #include <drm/drm_panel.h>
27 #include <drm/drm_print.h>
28 
29 /* Manufacturer Command Set */
30 #define MCS_MANPWR		0xb0
31 #define MCS_ELVSS_ON		0xb1
32 #define MCS_USER_SETTING	0xf0
33 #define MCS_DISPCTL		0xf2
34 #define MCS_POWER_CTRL		0xf4
35 #define MCS_GTCON		0xf7
36 #define MCS_PANEL_CONDITION	0xf8
37 #define MCS_GAMMA_SET1		0xf9
38 #define MCS_GAMMA_CTRL		0xfb
39 
40 /* array of gamma tables for gamma value 2.2 */
41 static u8 const ld9040_gammas[25][22] = {
42 	{ 0xf9, 0x00, 0x13, 0xb2, 0xba, 0xd2, 0x00, 0x30, 0x00, 0xaf, 0xc0,
43 	  0xb8, 0xcd, 0x00, 0x3d, 0x00, 0xa8, 0xb8, 0xb7, 0xcd, 0x00, 0x44 },
44 	{ 0xf9, 0x00, 0x13, 0xb9, 0xb9, 0xd0, 0x00, 0x3c, 0x00, 0xaf, 0xbf,
45 	  0xb6, 0xcb, 0x00, 0x4b, 0x00, 0xa8, 0xb9, 0xb5, 0xcc, 0x00, 0x52 },
46 	{ 0xf9, 0x00, 0x13, 0xba, 0xb9, 0xcd, 0x00, 0x41, 0x00, 0xb0, 0xbe,
47 	  0xb5, 0xc9, 0x00, 0x51, 0x00, 0xa9, 0xb9, 0xb5, 0xca, 0x00, 0x57 },
48 	{ 0xf9, 0x00, 0x13, 0xb9, 0xb8, 0xcd, 0x00, 0x46, 0x00, 0xb1, 0xbc,
49 	  0xb5, 0xc8, 0x00, 0x56, 0x00, 0xaa, 0xb8, 0xb4, 0xc9, 0x00, 0x5d },
50 	{ 0xf9, 0x00, 0x13, 0xba, 0xb8, 0xcb, 0x00, 0x4b, 0x00, 0xb3, 0xbc,
51 	  0xb4, 0xc7, 0x00, 0x5c, 0x00, 0xac, 0xb8, 0xb4, 0xc8, 0x00, 0x62 },
52 	{ 0xf9, 0x00, 0x13, 0xbb, 0xb7, 0xca, 0x00, 0x4f, 0x00, 0xb4, 0xbb,
53 	  0xb3, 0xc7, 0x00, 0x60, 0x00, 0xad, 0xb8, 0xb4, 0xc7, 0x00, 0x67 },
54 	{ 0xf9, 0x00, 0x47, 0xba, 0xb6, 0xca, 0x00, 0x53, 0x00, 0xb5, 0xbb,
55 	  0xb3, 0xc6, 0x00, 0x65, 0x00, 0xae, 0xb8, 0xb3, 0xc7, 0x00, 0x6c },
56 	{ 0xf9, 0x00, 0x71, 0xbb, 0xb5, 0xc8, 0x00, 0x57, 0x00, 0xb5, 0xbb,
57 	  0xb0, 0xc5, 0x00, 0x6a, 0x00, 0xae, 0xb9, 0xb1, 0xc6, 0x00, 0x70 },
58 	{ 0xf9, 0x00, 0x7b, 0xbb, 0xb4, 0xc8, 0x00, 0x5b, 0x00, 0xb5, 0xba,
59 	  0xb1, 0xc4, 0x00, 0x6e, 0x00, 0xae, 0xb9, 0xb0, 0xc5, 0x00, 0x75 },
60 	{ 0xf9, 0x00, 0x82, 0xba, 0xb4, 0xc7, 0x00, 0x5f, 0x00, 0xb5, 0xba,
61 	  0xb0, 0xc3, 0x00, 0x72, 0x00, 0xae, 0xb8, 0xb0, 0xc3, 0x00, 0x7a },
62 	{ 0xf9, 0x00, 0x89, 0xba, 0xb3, 0xc8, 0x00, 0x62, 0x00, 0xb6, 0xba,
63 	  0xaf, 0xc3, 0x00, 0x76, 0x00, 0xaf, 0xb7, 0xae, 0xc4, 0x00, 0x7e },
64 	{ 0xf9, 0x00, 0x8b, 0xb9, 0xb3, 0xc7, 0x00, 0x65, 0x00, 0xb7, 0xb8,
65 	  0xaf, 0xc3, 0x00, 0x7a, 0x00, 0x80, 0xb6, 0xae, 0xc4, 0x00, 0x81 },
66 	{ 0xf9, 0x00, 0x93, 0xba, 0xb3, 0xc5, 0x00, 0x69, 0x00, 0xb8, 0xb9,
67 	  0xae, 0xc1, 0x00, 0x7f, 0x00, 0xb0, 0xb6, 0xae, 0xc3, 0x00, 0x85 },
68 	{ 0xf9, 0x00, 0x97, 0xba, 0xb2, 0xc5, 0x00, 0x6c, 0x00, 0xb8, 0xb8,
69 	  0xae, 0xc1, 0x00, 0x82, 0x00, 0xb0, 0xb6, 0xae, 0xc2, 0x00, 0x89 },
70 	{ 0xf9, 0x00, 0x9a, 0xba, 0xb1, 0xc4, 0x00, 0x6f, 0x00, 0xb8, 0xb8,
71 	  0xad, 0xc0, 0x00, 0x86, 0x00, 0xb0, 0xb7, 0xad, 0xc0, 0x00, 0x8d },
72 	{ 0xf9, 0x00, 0x9c, 0xb9, 0xb0, 0xc4, 0x00, 0x72, 0x00, 0xb8, 0xb8,
73 	  0xac, 0xbf, 0x00, 0x8a, 0x00, 0xb0, 0xb6, 0xac, 0xc0, 0x00, 0x91 },
74 	{ 0xf9, 0x00, 0x9e, 0xba, 0xb0, 0xc2, 0x00, 0x75, 0x00, 0xb9, 0xb8,
75 	  0xab, 0xbe, 0x00, 0x8e, 0x00, 0xb0, 0xb6, 0xac, 0xbf, 0x00, 0x94 },
76 	{ 0xf9, 0x00, 0xa0, 0xb9, 0xaf, 0xc3, 0x00, 0x77, 0x00, 0xb9, 0xb7,
77 	  0xab, 0xbe, 0x00, 0x90, 0x00, 0xb0, 0xb6, 0xab, 0xbf, 0x00, 0x97 },
78 	{ 0xf9, 0x00, 0xa2, 0xb9, 0xaf, 0xc2, 0x00, 0x7a, 0x00, 0xb9, 0xb7,
79 	  0xaa, 0xbd, 0x00, 0x94, 0x00, 0xb0, 0xb5, 0xab, 0xbf, 0x00, 0x9a },
80 	{ 0xf9, 0x00, 0xa4, 0xb9, 0xaf, 0xc1, 0x00, 0x7d, 0x00, 0xb9, 0xb6,
81 	  0xaa, 0xbb, 0x00, 0x97, 0x00, 0xb1, 0xb5, 0xaa, 0xbf, 0x00, 0x9d },
82 	{ 0xf9, 0x00, 0xa4, 0xb8, 0xb0, 0xbf, 0x00, 0x80, 0x00, 0xb8, 0xb6,
83 	  0xaa, 0xbc, 0x00, 0x9a, 0x00, 0xb0, 0xb5, 0xab, 0xbd, 0x00, 0xa0 },
84 	{ 0xf9, 0x00, 0xa8, 0xb8, 0xae, 0xbe, 0x00, 0x84, 0x00, 0xb9, 0xb7,
85 	  0xa8, 0xbc, 0x00, 0x9d, 0x00, 0xb2, 0xb5, 0xaa, 0xbc, 0x00, 0xa4 },
86 	{ 0xf9, 0x00, 0xa9, 0xb6, 0xad, 0xbf, 0x00, 0x86, 0x00, 0xb8, 0xb5,
87 	  0xa8, 0xbc, 0x00, 0xa0, 0x00, 0xb3, 0xb3, 0xa9, 0xbc, 0x00, 0xa7 },
88 	{ 0xf9, 0x00, 0xa9, 0xb7, 0xae, 0xbd, 0x00, 0x89, 0x00, 0xb7, 0xb6,
89 	  0xa8, 0xba, 0x00, 0xa4, 0x00, 0xb1, 0xb4, 0xaa, 0xbb, 0x00, 0xaa },
90 	{ 0xf9, 0x00, 0xa7, 0xb4, 0xae, 0xbf, 0x00, 0x91, 0x00, 0xb2, 0xb4,
91 	  0xaa, 0xbb, 0x00, 0xac, 0x00, 0xb3, 0xb1, 0xaa, 0xbc, 0x00, 0xb3 },
92 };
93 
94 struct ld9040 {
95 	struct device *dev;
96 	struct drm_panel panel;
97 
98 	struct regulator_bulk_data supplies[2];
99 	struct gpio_desc *reset_gpio;
100 	u32 power_on_delay;
101 	u32 reset_delay;
102 	struct videomode vm;
103 	u32 width_mm;
104 	u32 height_mm;
105 
106 	int brightness;
107 
108 	/* This field is tested by functions directly accessing bus before
109 	 * transfer, transfer is skipped if it is set. In case of transfer
110 	 * failure or unexpected response the field is set to error value.
111 	 * Such construct allows to eliminate many checks in higher level
112 	 * functions.
113 	 */
114 	int error;
115 };
116 
117 static inline struct ld9040 *panel_to_ld9040(struct drm_panel *panel)
118 {
119 	return container_of(panel, struct ld9040, panel);
120 }
121 
122 static int ld9040_clear_error(struct ld9040 *ctx)
123 {
124 	int ret = ctx->error;
125 
126 	ctx->error = 0;
127 	return ret;
128 }
129 
130 static int ld9040_spi_write_word(struct ld9040 *ctx, u16 data)
131 {
132 	struct spi_device *spi = to_spi_device(ctx->dev);
133 	struct spi_transfer xfer = {
134 		.len		= 2,
135 		.tx_buf		= &data,
136 	};
137 	struct spi_message msg;
138 
139 	spi_message_init(&msg);
140 	spi_message_add_tail(&xfer, &msg);
141 
142 	return spi_sync(spi, &msg);
143 }
144 
145 static void ld9040_dcs_write(struct ld9040 *ctx, const u8 *data, size_t len)
146 {
147 	int ret = 0;
148 
149 	if (ctx->error < 0 || len == 0)
150 		return;
151 
152 	dev_dbg(ctx->dev, "writing dcs seq: %*ph\n", (int)len, data);
153 	ret = ld9040_spi_write_word(ctx, *data);
154 
155 	while (!ret && --len) {
156 		++data;
157 		ret = ld9040_spi_write_word(ctx, *data | 0x100);
158 	}
159 
160 	if (ret) {
161 		dev_err(ctx->dev, "error %d writing dcs seq: %*ph\n", ret,
162 			(int)len, data);
163 		ctx->error = ret;
164 	}
165 
166 	usleep_range(300, 310);
167 }
168 
169 #define ld9040_dcs_write_seq_static(ctx, seq...) \
170 ({\
171 	static const u8 d[] = { seq };\
172 	ld9040_dcs_write(ctx, d, ARRAY_SIZE(d));\
173 })
174 
175 static void ld9040_brightness_set(struct ld9040 *ctx)
176 {
177 	ld9040_dcs_write(ctx, ld9040_gammas[ctx->brightness],
178 			 ARRAY_SIZE(ld9040_gammas[ctx->brightness]));
179 
180 	ld9040_dcs_write_seq_static(ctx, MCS_GAMMA_CTRL, 0x02, 0x5a);
181 }
182 
183 static void ld9040_init(struct ld9040 *ctx)
184 {
185 	ld9040_dcs_write_seq_static(ctx, MCS_USER_SETTING, 0x5a, 0x5a);
186 	ld9040_dcs_write_seq_static(ctx, MCS_PANEL_CONDITION,
187 		0x05, 0x65, 0x96, 0x71, 0x7d, 0x19, 0x3b, 0x0d,
188 		0x19, 0x7e, 0x0d, 0xe2, 0x00, 0x00, 0x7e, 0x7d,
189 		0x07, 0x07, 0x20, 0x20, 0x20, 0x02, 0x02);
190 	ld9040_dcs_write_seq_static(ctx, MCS_DISPCTL,
191 		0x02, 0x08, 0x08, 0x10, 0x10);
192 	ld9040_dcs_write_seq_static(ctx, MCS_MANPWR, 0x04);
193 	ld9040_dcs_write_seq_static(ctx, MCS_POWER_CTRL,
194 		0x0a, 0x87, 0x25, 0x6a, 0x44, 0x02, 0x88);
195 	ld9040_dcs_write_seq_static(ctx, MCS_ELVSS_ON, 0x0d, 0x00, 0x16);
196 	ld9040_dcs_write_seq_static(ctx, MCS_GTCON, 0x09, 0x00, 0x00);
197 	ld9040_brightness_set(ctx);
198 	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_EXIT_SLEEP_MODE);
199 	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_ON);
200 }
201 
202 static int ld9040_power_on(struct ld9040 *ctx)
203 {
204 	int ret;
205 
206 	ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
207 	if (ret < 0)
208 		return ret;
209 
210 	msleep(ctx->power_on_delay);
211 	gpiod_set_value(ctx->reset_gpio, 0);
212 	msleep(ctx->reset_delay);
213 	gpiod_set_value(ctx->reset_gpio, 1);
214 	msleep(ctx->reset_delay);
215 
216 	return 0;
217 }
218 
219 static int ld9040_power_off(struct ld9040 *ctx)
220 {
221 	return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
222 }
223 
224 static int ld9040_disable(struct drm_panel *panel)
225 {
226 	return 0;
227 }
228 
229 static int ld9040_unprepare(struct drm_panel *panel)
230 {
231 	struct ld9040 *ctx = panel_to_ld9040(panel);
232 
233 	msleep(120);
234 	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_OFF);
235 	ld9040_dcs_write_seq_static(ctx, MIPI_DCS_ENTER_SLEEP_MODE);
236 	msleep(40);
237 
238 	ld9040_clear_error(ctx);
239 
240 	return ld9040_power_off(ctx);
241 }
242 
243 static int ld9040_prepare(struct drm_panel *panel)
244 {
245 	struct ld9040 *ctx = panel_to_ld9040(panel);
246 	int ret;
247 
248 	ret = ld9040_power_on(ctx);
249 	if (ret < 0)
250 		return ret;
251 
252 	ld9040_init(ctx);
253 
254 	ret = ld9040_clear_error(ctx);
255 
256 	if (ret < 0)
257 		ld9040_unprepare(panel);
258 
259 	return ret;
260 }
261 
262 static int ld9040_enable(struct drm_panel *panel)
263 {
264 	return 0;
265 }
266 
267 static int ld9040_get_modes(struct drm_panel *panel)
268 {
269 	struct drm_connector *connector = panel->connector;
270 	struct ld9040 *ctx = panel_to_ld9040(panel);
271 	struct drm_display_mode *mode;
272 
273 	mode = drm_mode_create(connector->dev);
274 	if (!mode) {
275 		DRM_ERROR("failed to create a new display mode\n");
276 		return 0;
277 	}
278 
279 	drm_display_mode_from_videomode(&ctx->vm, mode);
280 	mode->width_mm = ctx->width_mm;
281 	mode->height_mm = ctx->height_mm;
282 	connector->display_info.width_mm = mode->width_mm;
283 	connector->display_info.height_mm = mode->height_mm;
284 
285 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
286 	drm_mode_probed_add(connector, mode);
287 
288 	return 1;
289 }
290 
291 static const struct drm_panel_funcs ld9040_drm_funcs = {
292 	.disable = ld9040_disable,
293 	.unprepare = ld9040_unprepare,
294 	.prepare = ld9040_prepare,
295 	.enable = ld9040_enable,
296 	.get_modes = ld9040_get_modes,
297 };
298 
299 static int ld9040_parse_dt(struct ld9040 *ctx)
300 {
301 	struct device *dev = ctx->dev;
302 	struct device_node *np = dev->of_node;
303 	int ret;
304 
305 	ret = of_get_videomode(np, &ctx->vm, 0);
306 	if (ret < 0)
307 		return ret;
308 
309 	of_property_read_u32(np, "power-on-delay", &ctx->power_on_delay);
310 	of_property_read_u32(np, "reset-delay", &ctx->reset_delay);
311 	of_property_read_u32(np, "panel-width-mm", &ctx->width_mm);
312 	of_property_read_u32(np, "panel-height-mm", &ctx->height_mm);
313 
314 	return 0;
315 }
316 
317 static int ld9040_probe(struct spi_device *spi)
318 {
319 	struct device *dev = &spi->dev;
320 	struct ld9040 *ctx;
321 	int ret;
322 
323 	ctx = devm_kzalloc(dev, sizeof(struct ld9040), GFP_KERNEL);
324 	if (!ctx)
325 		return -ENOMEM;
326 
327 	spi_set_drvdata(spi, ctx);
328 
329 	ctx->dev = dev;
330 	ctx->brightness = ARRAY_SIZE(ld9040_gammas) - 1;
331 
332 	ret = ld9040_parse_dt(ctx);
333 	if (ret < 0)
334 		return ret;
335 
336 	ctx->supplies[0].supply = "vdd3";
337 	ctx->supplies[1].supply = "vci";
338 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
339 				      ctx->supplies);
340 	if (ret < 0)
341 		return ret;
342 
343 	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
344 	if (IS_ERR(ctx->reset_gpio)) {
345 		dev_err(dev, "cannot get reset-gpios %ld\n",
346 			PTR_ERR(ctx->reset_gpio));
347 		return PTR_ERR(ctx->reset_gpio);
348 	}
349 
350 	spi->bits_per_word = 9;
351 	ret = spi_setup(spi);
352 	if (ret < 0) {
353 		dev_err(dev, "spi setup failed.\n");
354 		return ret;
355 	}
356 
357 	drm_panel_init(&ctx->panel);
358 	ctx->panel.dev = dev;
359 	ctx->panel.funcs = &ld9040_drm_funcs;
360 
361 	return drm_panel_add(&ctx->panel);
362 }
363 
364 static int ld9040_remove(struct spi_device *spi)
365 {
366 	struct ld9040 *ctx = spi_get_drvdata(spi);
367 
368 	ld9040_power_off(ctx);
369 	drm_panel_remove(&ctx->panel);
370 
371 	return 0;
372 }
373 
374 static const struct of_device_id ld9040_of_match[] = {
375 	{ .compatible = "samsung,ld9040" },
376 	{ }
377 };
378 MODULE_DEVICE_TABLE(of, ld9040_of_match);
379 
380 static struct spi_driver ld9040_driver = {
381 	.probe = ld9040_probe,
382 	.remove = ld9040_remove,
383 	.driver = {
384 		.name = "panel-samsung-ld9040",
385 		.of_match_table = ld9040_of_match,
386 	},
387 };
388 module_spi_driver(ld9040_driver);
389 
390 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
391 MODULE_DESCRIPTION("ld9040 LCD Driver");
392 MODULE_LICENSE("GPL v2");
393