Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * DRM driver for the HX8357D LCD controller
4 *
5 * Copyright 2018 Broadcom
6 * Copyright 2018 David Lechner <david@lechnology.com>
7 * Copyright 2016 Noralf Trønnes
8 * Copyright (C) 2015 Adafruit Industries
9 * Copyright (C) 2013 Christian Vogelgsang
10 */
11
12#include <linux/backlight.h>
13#include <linux/delay.h>
14#include <linux/gpio/consumer.h>
15#include <linux/module.h>
16#include <linux/property.h>
17#include <linux/spi/spi.h>
18
19#include <drm/clients/drm_client_setup.h>
20#include <drm/drm_atomic_helper.h>
21#include <drm/drm_drv.h>
22#include <drm/drm_fbdev_dma.h>
23#include <drm/drm_gem_atomic_helper.h>
24#include <drm/drm_gem_dma_helper.h>
25#include <drm/drm_managed.h>
26#include <drm/drm_mipi_dbi.h>
27#include <drm/drm_modeset_helper.h>
28#include <drm/drm_print.h>
29#include <video/mipi_display.h>
30
31#define HX8357D_SETOSC 0xb0
32#define HX8357D_SETPOWER 0xb1
33#define HX8357D_SETRGB 0xb3
34#define HX8357D_SETCYC 0xb3
35#define HX8357D_SETCOM 0xb6
36#define HX8357D_SETEXTC 0xb9
37#define HX8357D_SETSTBA 0xc0
38#define HX8357D_SETPANEL 0xcc
39#define HX8357D_SETGAMMA 0xe0
40
41#define HX8357D_MADCTL_MY 0x80
42#define HX8357D_MADCTL_MX 0x40
43#define HX8357D_MADCTL_MV 0x20
44#define HX8357D_MADCTL_ML 0x10
45#define HX8357D_MADCTL_RGB 0x00
46#define HX8357D_MADCTL_BGR 0x08
47#define HX8357D_MADCTL_MH 0x04
48
49static void yx240qv29_enable(struct drm_simple_display_pipe *pipe,
50 struct drm_crtc_state *crtc_state,
51 struct drm_plane_state *plane_state)
52{
53 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
54 struct mipi_dbi *dbi = &dbidev->dbi;
55 u8 addr_mode;
56 int ret, idx;
57
58 if (!drm_dev_enter(pipe->crtc.dev, &idx))
59 return;
60
61 DRM_DEBUG_KMS("\n");
62
63 ret = mipi_dbi_poweron_conditional_reset(dbidev);
64 if (ret < 0)
65 goto out_exit;
66 if (ret == 1)
67 goto out_enable;
68
69 /* setextc */
70 mipi_dbi_command(dbi, HX8357D_SETEXTC, 0xFF, 0x83, 0x57);
71 msleep(150);
72
73 /* setRGB which also enables SDO */
74 mipi_dbi_command(dbi, HX8357D_SETRGB, 0x00, 0x00, 0x06, 0x06);
75
76 /* -1.52V */
77 mipi_dbi_command(dbi, HX8357D_SETCOM, 0x25);
78
79 /* Normal mode 70Hz, Idle mode 55 Hz */
80 mipi_dbi_command(dbi, HX8357D_SETOSC, 0x68);
81
82 /* Set Panel - BGR, Gate direction swapped */
83 mipi_dbi_command(dbi, HX8357D_SETPANEL, 0x05);
84
85 mipi_dbi_command(dbi, HX8357D_SETPOWER,
86 0x00, /* Not deep standby */
87 0x15, /* BT */
88 0x1C, /* VSPR */
89 0x1C, /* VSNR */
90 0x83, /* AP */
91 0xAA); /* FS */
92
93 mipi_dbi_command(dbi, HX8357D_SETSTBA,
94 0x50, /* OPON normal */
95 0x50, /* OPON idle */
96 0x01, /* STBA */
97 0x3C, /* STBA */
98 0x1E, /* STBA */
99 0x08); /* GEN */
100
101 mipi_dbi_command(dbi, HX8357D_SETCYC,
102 0x02, /* NW 0x02 */
103 0x40, /* RTN */
104 0x00, /* DIV */
105 0x2A, /* DUM */
106 0x2A, /* DUM */
107 0x0D, /* GDON */
108 0x78); /* GDOFF */
109
110 mipi_dbi_command(dbi, HX8357D_SETGAMMA,
111 0x02,
112 0x0A,
113 0x11,
114 0x1d,
115 0x23,
116 0x35,
117 0x41,
118 0x4b,
119 0x4b,
120 0x42,
121 0x3A,
122 0x27,
123 0x1B,
124 0x08,
125 0x09,
126 0x03,
127 0x02,
128 0x0A,
129 0x11,
130 0x1d,
131 0x23,
132 0x35,
133 0x41,
134 0x4b,
135 0x4b,
136 0x42,
137 0x3A,
138 0x27,
139 0x1B,
140 0x08,
141 0x09,
142 0x03,
143 0x00,
144 0x01);
145
146 /* 16 bit */
147 mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT,
148 MIPI_DCS_PIXEL_FMT_16BIT);
149
150 /* TE off */
151 mipi_dbi_command(dbi, MIPI_DCS_SET_TEAR_ON, 0x00);
152
153 /* tear line */
154 mipi_dbi_command(dbi, MIPI_DCS_SET_TEAR_SCANLINE, 0x00, 0x02);
155
156 /* Exit Sleep */
157 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
158 msleep(150);
159
160 /* display on */
161 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
162 usleep_range(5000, 7000);
163
164out_enable:
165 switch (dbidev->rotation) {
166 default:
167 addr_mode = HX8357D_MADCTL_MX | HX8357D_MADCTL_MY;
168 break;
169 case 90:
170 addr_mode = HX8357D_MADCTL_MV | HX8357D_MADCTL_MY;
171 break;
172 case 180:
173 addr_mode = 0;
174 break;
175 case 270:
176 addr_mode = HX8357D_MADCTL_MV | HX8357D_MADCTL_MX;
177 break;
178 }
179 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
180 mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
181out_exit:
182 drm_dev_exit(idx);
183}
184
185static const struct drm_simple_display_pipe_funcs hx8357d_pipe_funcs = {
186 DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(yx240qv29_enable),
187};
188
189static const struct drm_display_mode yx350hv15_mode = {
190 DRM_SIMPLE_MODE(320, 480, 60, 75),
191};
192
193DEFINE_DRM_GEM_DMA_FOPS(hx8357d_fops);
194
195static const struct drm_driver hx8357d_driver = {
196 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
197 .fops = &hx8357d_fops,
198 DRM_GEM_DMA_DRIVER_OPS_VMAP,
199 DRM_FBDEV_DMA_DRIVER_OPS,
200 .debugfs_init = mipi_dbi_debugfs_init,
201 .name = "hx8357d",
202 .desc = "HX8357D",
203 .major = 1,
204 .minor = 0,
205};
206
207static const struct of_device_id hx8357d_of_match[] = {
208 { .compatible = "adafruit,yx350hv15" },
209 { }
210};
211MODULE_DEVICE_TABLE(of, hx8357d_of_match);
212
213static const struct spi_device_id hx8357d_id[] = {
214 { "yx350hv15", 0 },
215 { }
216};
217MODULE_DEVICE_TABLE(spi, hx8357d_id);
218
219static int hx8357d_probe(struct spi_device *spi)
220{
221 struct device *dev = &spi->dev;
222 struct mipi_dbi_dev *dbidev;
223 struct drm_device *drm;
224 struct gpio_desc *dc;
225 u32 rotation = 0;
226 int ret;
227
228 dbidev = devm_drm_dev_alloc(dev, &hx8357d_driver,
229 struct mipi_dbi_dev, drm);
230 if (IS_ERR(dbidev))
231 return PTR_ERR(dbidev);
232
233 drm = &dbidev->drm;
234
235 dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
236 if (IS_ERR(dc))
237 return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
238
239 dbidev->backlight = devm_of_find_backlight(dev);
240 if (IS_ERR(dbidev->backlight))
241 return PTR_ERR(dbidev->backlight);
242
243 device_property_read_u32(dev, "rotation", &rotation);
244
245 ret = mipi_dbi_spi_init(spi, &dbidev->dbi, dc);
246 if (ret)
247 return ret;
248
249 ret = mipi_dbi_dev_init(dbidev, &hx8357d_pipe_funcs, &yx350hv15_mode, rotation);
250 if (ret)
251 return ret;
252
253 drm_mode_config_reset(drm);
254
255 ret = drm_dev_register(drm, 0);
256 if (ret)
257 return ret;
258
259 spi_set_drvdata(spi, drm);
260
261 drm_client_setup(drm, NULL);
262
263 return 0;
264}
265
266static void hx8357d_remove(struct spi_device *spi)
267{
268 struct drm_device *drm = spi_get_drvdata(spi);
269
270 drm_dev_unplug(drm);
271 drm_atomic_helper_shutdown(drm);
272}
273
274static void hx8357d_shutdown(struct spi_device *spi)
275{
276 drm_atomic_helper_shutdown(spi_get_drvdata(spi));
277}
278
279static struct spi_driver hx8357d_spi_driver = {
280 .driver = {
281 .name = "hx8357d",
282 .of_match_table = hx8357d_of_match,
283 },
284 .id_table = hx8357d_id,
285 .probe = hx8357d_probe,
286 .remove = hx8357d_remove,
287 .shutdown = hx8357d_shutdown,
288};
289module_spi_driver(hx8357d_spi_driver);
290
291MODULE_DESCRIPTION("HX8357D DRM driver");
292MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
293MODULE_LICENSE("GPL");