summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/panel/panel-sony-acx424akp.c
blob: 97a1b4790d3c2c2d738df2cb88e8c9a7e1053026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// SPDX-License-Identifier: GPL-2.0+
/*
 * MIPI-DSI Sony ACX424AKP panel driver. This is a 480x864
 * AMOLED panel with a command-only DSI interface.
 *
 * Copyright (C) Linaro Ltd. 2019
 * Author: Linus Walleij
 * Based on code and know-how from Marcus Lorentzon
 * Copyright (C) ST-Ericsson SA 2010
 */
#include <linux/backlight.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regulator/consumer.h>

#include <video/mipi_display.h>

#include <drm/drm_mipi_dsi.h>
#include <drm/drm_modes.h>
#include <drm/drm_panel.h>
#include <drm/drm_print.h>

#define ACX424_DCS_READ_ID1		0xDA
#define ACX424_DCS_READ_ID2		0xDB
#define ACX424_DCS_READ_ID3		0xDC
#define ACX424_DCS_SET_MDDI		0xAE

/*
 * Sony seems to use vendor ID 0x81
 */
#define DISPLAY_SONY_ACX424AKP_ID1	0x811b
#define DISPLAY_SONY_ACX424AKP_ID2	0x811a
/*
 * The third ID looks like a bug, vendor IDs begin at 0x80
 * and panel 00 ... seems like default values.
 */
#define DISPLAY_SONY_ACX424AKP_ID3	0x8000

struct acx424akp {
	struct drm_panel panel;
	struct device *dev;
	struct backlight_device *bl;
	struct regulator *supply;
	struct gpio_desc *reset_gpio;
	bool video_mode;
};

static const struct drm_display_mode sony_acx424akp_vid_mode = {
	.clock = 27234,
	.hdisplay = 480,
	.hsync_start = 480 + 15,
	.hsync_end = 480 + 15 + 0,
	.htotal = 480 + 15 + 0 + 15,
	.vdisplay = 864,
	.vsync_start = 864 + 14,
	.vsync_end = 864 + 14 + 1,
	.vtotal = 864 + 14 + 1 + 11,
	.width_mm = 48,
	.height_mm = 84,
	.flags = DRM_MODE_FLAG_PVSYNC,
};

/*
 * The timings are not very helpful as the display is used in
 * command mode using the maximum HS frequency.
 */
static const struct drm_display_mode sony_acx424akp_cmd_mode = {
	.clock = 35478,
	.hdisplay = 480,
	.hsync_start = 480 + 154,
	.hsync_end = 480 + 154 + 16,
	.htotal = 480 + 154 + 16 + 32,
	.vdisplay = 864,
	.vsync_start = 864 + 1,
	.vsync_end = 864 + 1 + 1,
	.vtotal = 864 + 1 + 1 + 1,
	/*
	 * Some desired refresh rate, experiments at the maximum "pixel"
	 * clock speed (HS clock 420 MHz) yields around 117Hz.
	 */
	.width_mm = 48,
	.height_mm = 84,
};

static inline struct acx424akp *panel_to_acx424akp(struct drm_panel *panel)
{
	return container_of(panel, struct acx424akp, panel);
}

#define FOSC			20 /* 20Mhz */
#define SCALE_FACTOR_NS_DIV_MHZ	1000

static int acx424akp_set_brightness(struct backlight_device *bl)
{
	struct acx424akp *acx = bl_get_data(bl);
	struct mipi_dsi_device *dsi = to_mipi_dsi_device(acx->dev);
	int period_ns = 1023;
	int duty_ns = bl->props.brightness;
	u8 pwm_ratio;
	u8 pwm_div;
	u8 par;
	int ret;

	/* Calculate the PWM duty cycle in n/256's */
	pwm_ratio = max(((duty_ns * 256) / period_ns) - 1, 1);
	pwm_div = max(1,
		      ((FOSC * period_ns) / 256) /
		      SCALE_FACTOR_NS_DIV_MHZ);

	/* Set up PWM dutycycle ONE byte (differs from the standard) */
	DRM_DEV_DEBUG(acx->dev, "calculated duty cycle %02x\n", pwm_ratio);
	ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
				 &pwm_ratio, 1);
	if (ret < 0) {
		DRM_DEV_ERROR(acx->dev,
			      "failed to set display PWM ratio (%d)\n",
			      ret);
		return ret;
	}

	/*
	 * Sequence to write PWMDIV:
	 *	address		data
	 *	0xF3		0xAA   CMD2 Unlock
	 *	0x00		0x01   Enter CMD2 page 0
	 *	0X7D		0x01   No reload MTP of CMD2 P1
	 *	0x22		PWMDIV
	 *	0x7F		0xAA   CMD2 page 1 lock
	 */
	par = 0xaa;
	ret = mipi_dsi_dcs_write(dsi, 0xf3, &par, 1);
	if (ret < 0) {
		DRM_DEV_ERROR(acx->dev,
			      "failed to unlock CMD 2 (%d)\n",
			      ret);
		return ret;
	}
	par = 0x01;
	ret = mipi_dsi_dcs_write(dsi, 0x00, &par, 1);
	if (ret < 0) {
		DRM_DEV_ERROR(acx->dev,
			      "failed to enter page 1 (%d)\n",
			      ret);
		return ret;
	}
	par = 0x01;
	ret = mipi_dsi_dcs_write(dsi, 0x7d, &par, 1);
	if (ret < 0) {
		DRM_DEV_ERROR(acx->dev,
			      "failed to disable MTP reload (%d)\n",
			      ret);
		return ret;
	}
	ret = mipi_dsi_dcs_write(dsi, 0x22, &pwm_div, 1);
	if (ret < 0) {
		DRM_DEV_ERROR(acx->dev,
			      "failed to set PWM divisor (%d)\n",
			      ret);
		return ret;
	}
	par = 0xaa;
	ret = mipi_dsi_dcs_write(dsi, 0x7f, &par, 1);
	if (ret < 0) {
		DRM_DEV_ERROR(acx->dev,
			      "failed to lock CMD 2 (%d)\n",
			      ret);
		return ret;
	}

	/* Enable backlight */
	par = 0x24;
	ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY,
				 &par, 1);
	if (ret < 0) {
		DRM_DEV_ERROR(acx->dev,
			      "failed to enable display backlight (%d)\n",
			      ret);
		return ret;
	}

	return 0;
}

static const struct backlight_ops acx424akp_bl_ops = {
	.update_status = acx424akp_set_brightness,
};

static int acx424akp_read_id(struct acx424akp *acx)
{
	struct mipi_dsi_device *dsi = to_mipi_dsi_device(acx->dev);
	u8 vendor, version, panel;
	u16 val;
	int ret;

	ret = mipi_dsi_dcs_read(dsi, ACX424_DCS_READ_ID1, &vendor, 1);
	if (ret < 0) {
		DRM_DEV_ERROR(acx->dev, "could not vendor ID byte\n");
		return ret;
	}
	ret = mipi_dsi_dcs_read(dsi, ACX424_DCS_READ_ID2, &version, 1);
	if (ret < 0) {
		DRM_DEV_ERROR(acx->dev, "could not read device version byte\n");
		return ret;
	}
	ret = mipi_dsi_dcs_read(dsi, ACX424_DCS_READ_ID3, &panel, 1);
	if (ret < 0) {
		DRM_DEV_ERROR(acx->dev, "could not read panel ID byte\n");
		return ret;
	}

	if (vendor == 0x00) {
		DRM_DEV_ERROR(acx->dev, "device vendor ID is zero\n");
		return -ENODEV;
	}

	val = (vendor << 8) | panel;
	switch (val) {
	case DISPLAY_SONY_ACX424AKP_ID1:
	case DISPLAY_SONY_ACX424AKP_ID2:
	case DISPLAY_SONY_ACX424AKP_ID3:
		DRM_DEV_INFO(acx->dev,
			     "MTP vendor: %02x, version: %02x, panel: %02x\n",
			     vendor, version, panel);
		break;
	default:
		DRM_DEV_INFO(acx->dev,
			     "unknown vendor: %02x, version: %02x, panel: %02x\n",
			     vendor, version, panel);
		break;
	}

	return 0;
}

static int acx424akp_power_on(struct acx424akp *acx)
{
	int ret;

	ret = regulator_enable(acx->supply);
	if (ret) {
		DRM_DEV_ERROR(acx->dev, "failed to enable supply (%d)\n", ret);
		return ret;
	}

	/* Assert RESET */
	gpiod_set_value_cansleep(acx->reset_gpio, 1);
	udelay(20);
	/* De-assert RESET */
	gpiod_set_value_cansleep(acx->reset_gpio, 0);
	usleep_range(11000, 20000);

	return 0;
}

static void acx424akp_power_off(struct acx424akp *acx)
{
	/* Assert RESET */
	gpiod_set_value_cansleep(acx->reset_gpio, 1);
	usleep_range(11000, 20000);

	regulator_disable(acx->supply);
}

static int acx424akp_prepare(struct drm_panel *panel)
{
	struct acx424akp *acx = panel_to_acx424akp(panel);
	struct mipi_dsi_device *dsi = to_mipi_dsi_device(acx->dev);
	const u8 mddi = 3;
	int ret;

	ret = acx424akp_power_on(acx);
	if (ret)
		return ret;

	ret = acx424akp_read_id(acx);
	if (ret) {
		DRM_DEV_ERROR(acx->dev, "failed to read panel ID (%d)\n", ret);
		goto err_power_off;
	}

	/* Enabe tearing mode: send TE (tearing effect) at VBLANK */
	ret = mipi_dsi_dcs_set_tear_on(dsi,
				       MIPI_DSI_DCS_TEAR_MODE_VBLANK);
	if (ret) {
		DRM_DEV_ERROR(acx->dev, "failed to enable vblank TE (%d)\n",
			      ret);
		goto err_power_off;
	}

	/*
	 * Set MDDI
	 *
	 * This presumably deactivates the Qualcomm MDDI interface and
	 * selects DSI, similar code is found in other drivers such as the
	 * Sharp LS043T1LE01 which makes us suspect that this panel may be
	 * using a Novatek NT35565 or similar display driver chip that shares
	 * this command. Due to the lack of documentation we cannot know for
	 * sure.
	 */
	ret = mipi_dsi_dcs_write(dsi, ACX424_DCS_SET_MDDI,
				 &mddi, sizeof(mddi));
	if (ret < 0) {
		DRM_DEV_ERROR(acx->dev, "failed to set MDDI (%d)\n", ret);
		goto err_power_off;
	}

	/* Exit sleep mode */
	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
	if (ret) {
		DRM_DEV_ERROR(acx->dev, "failed to exit sleep mode (%d)\n",
			      ret);
		goto err_power_off;
	}
	msleep(140);

	ret = mipi_dsi_dcs_set_display_on(dsi);
	if (ret) {
		DRM_DEV_ERROR(acx->dev, "failed to turn display on (%d)\n",
			      ret);
		goto err_power_off;
	}
	if (acx->video_mode) {
		/* In video mode turn peripheral on */
		ret = mipi_dsi_turn_on_peripheral(dsi);
		if (ret) {
			dev_err(acx->dev, "failed to turn on peripheral\n");
			goto err_power_off;
		}
	}

	acx->bl->props.power = FB_BLANK_NORMAL;

	return 0;

err_power_off:
	acx424akp_power_off(acx);
	return ret;
}

static int acx424akp_unprepare(struct drm_panel *panel)
{
	struct acx424akp *acx = panel_to_acx424akp(panel);
	struct mipi_dsi_device *dsi = to_mipi_dsi_device(acx->dev);
	u8 par;
	int ret;

	/* Disable backlight */
	par = 0x00;
	ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY,
				 &par, 1);
	if (ret) {
		DRM_DEV_ERROR(acx->dev,
			      "failed to disable display backlight (%d)\n",
			      ret);
		return ret;
	}

	ret = mipi_dsi_dcs_set_display_off(dsi);
	if (ret) {
		DRM_DEV_ERROR(acx->dev, "failed to turn display off (%d)\n",
			      ret);
		return ret;
	}

	/* Enter sleep mode */
	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
	if (ret) {
		DRM_DEV_ERROR(acx->dev, "failed to enter sleep mode (%d)\n",
			      ret);
		return ret;
	}
	msleep(85);

	acx424akp_power_off(acx);
	acx->bl->props.power = FB_BLANK_POWERDOWN;

	return 0;
}

static int acx424akp_enable(struct drm_panel *panel)
{
	struct acx424akp *acx = panel_to_acx424akp(panel);

	/*
	 * The backlight is on as long as the display is on
	 * so no use to call backlight_enable() here.
	 */
	acx->bl->props.power = FB_BLANK_UNBLANK;

	return 0;
}

static int acx424akp_disable(struct drm_panel *panel)
{
	struct acx424akp *acx = panel_to_acx424akp(panel);

	/*
	 * The backlight is on as long as the display is on
	 * so no use to call backlight_disable() here.
	 */
	acx->bl->props.power = FB_BLANK_NORMAL;

	return 0;
}

static int acx424akp_get_modes(struct drm_panel *panel,
			       struct drm_connector *connector)
{
	struct acx424akp *acx = panel_to_acx424akp(panel);
	struct drm_display_mode *mode;

	if (acx->video_mode)
		mode = drm_mode_duplicate(connector->dev,
					  &sony_acx424akp_vid_mode);
	else
		mode = drm_mode_duplicate(connector->dev,
					  &sony_acx424akp_cmd_mode);
	if (!mode) {
		DRM_ERROR("bad mode or failed to add mode\n");
		return -EINVAL;
	}
	drm_mode_set_name(mode);
	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;

	connector->display_info.width_mm = mode->width_mm;
	connector->display_info.height_mm = mode->height_mm;

	drm_mode_probed_add(connector, mode);

	return 1; /* Number of modes */
}

static const struct drm_panel_funcs acx424akp_drm_funcs = {
	.disable = acx424akp_disable,
	.unprepare = acx424akp_unprepare,
	.prepare = acx424akp_prepare,
	.enable = acx424akp_enable,
	.get_modes = acx424akp_get_modes,
};

static int acx424akp_probe(struct mipi_dsi_device *dsi)
{
	struct device *dev = &dsi->dev;
	struct acx424akp *acx;
	int ret;

	acx = devm_kzalloc(dev, sizeof(struct acx424akp), GFP_KERNEL);
	if (!acx)
		return -ENOMEM;
	acx->video_mode = of_property_read_bool(dev->of_node,
						"enforce-video-mode");

	mipi_dsi_set_drvdata(dsi, acx);
	acx->dev = dev;

	dsi->lanes = 2;
	dsi->format = MIPI_DSI_FMT_RGB888;
	/*
	 * FIXME: these come from the ST-Ericsson vendor driver for the
	 * HREF520 and seems to reflect limitations in the PLLs on that
	 * platform, if you have the datasheet, please cross-check the
	 * actual max rates.
	 */
	dsi->lp_rate = 19200000;
	dsi->hs_rate = 420160000;

	if (acx->video_mode)
		/* Burst mode using event for sync */
		dsi->mode_flags =
			MIPI_DSI_MODE_VIDEO |
			MIPI_DSI_MODE_VIDEO_BURST;
	else
		dsi->mode_flags =
			MIPI_DSI_CLOCK_NON_CONTINUOUS |
			MIPI_DSI_MODE_EOT_PACKET;

	acx->supply = devm_regulator_get(dev, "vddi");
	if (IS_ERR(acx->supply))
		return PTR_ERR(acx->supply);

	/* This asserts RESET by default */
	acx->reset_gpio = devm_gpiod_get_optional(dev, "reset",
						  GPIOD_OUT_HIGH);
	if (IS_ERR(acx->reset_gpio)) {
		ret = PTR_ERR(acx->reset_gpio);
		if (ret != -EPROBE_DEFER)
			DRM_DEV_ERROR(dev, "failed to request GPIO (%d)\n",
				      ret);
		return ret;
	}

	drm_panel_init(&acx->panel, dev, &acx424akp_drm_funcs,
		       DRM_MODE_CONNECTOR_DSI);

	acx->bl = devm_backlight_device_register(dev, "acx424akp", dev, acx,
						 &acx424akp_bl_ops, NULL);
	if (IS_ERR(acx->bl)) {
		DRM_DEV_ERROR(dev, "failed to register backlight device\n");
		return PTR_ERR(acx->bl);
	}
	acx->bl->props.max_brightness = 1023;
	acx->bl->props.brightness = 512;
	acx->bl->props.power = FB_BLANK_POWERDOWN;

	ret = drm_panel_add(&acx->panel);
	if (ret < 0)
		return ret;

	ret = mipi_dsi_attach(dsi);
	if (ret < 0) {
		drm_panel_remove(&acx->panel);
		return ret;
	}

	return 0;
}

static int acx424akp_remove(struct mipi_dsi_device *dsi)
{
	struct acx424akp *acx = mipi_dsi_get_drvdata(dsi);

	mipi_dsi_detach(dsi);
	drm_panel_remove(&acx->panel);

	return 0;
}

static const struct of_device_id acx424akp_of_match[] = {
	{ .compatible = "sony,acx424akp" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, acx424akp_of_match);

static struct mipi_dsi_driver acx424akp_driver = {
	.probe = acx424akp_probe,
	.remove = acx424akp_remove,
	.driver = {
		.name = "panel-sony-acx424akp",
		.of_match_table = acx424akp_of_match,
	},
};
module_mipi_dsi_driver(acx424akp_driver);

MODULE_AUTHOR("Linus Wallei <linus.walleij@linaro.org>");
MODULE_DESCRIPTION("MIPI-DSI Sony acx424akp Panel Driver");
MODULE_LICENSE("GPL v2");