1 // SPDX-License-Identifier: GPL-2.0
3 * Asia Better Technology Ltd. Y030XX067A IPS LCD panel driver
5 * Copyright (C) 2020, Paul Cercueil <paul@crapouillou.net>
6 * Copyright (C) 2020, Christophe Branchereau <cbranchereau@gmail.com>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/media-bus-format.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/spi/spi.h>
19 #include <drm/drm_modes.h>
20 #include <drm/drm_panel.h>
22 #define REG00_VBRT_CTRL(val) (val)
24 #define REG01_COM_DC(val) (val)
26 #define REG02_DA_CONTRAST(val) (val)
27 #define REG02_VESA_SEL(val) ((val) << 5)
28 #define REG02_COMDC_SW BIT(7)
30 #define REG03_VPOSITION(val) (val)
31 #define REG03_BSMOUNT BIT(5)
32 #define REG03_COMTST BIT(6)
33 #define REG03_HPOSITION1 BIT(7)
35 #define REG04_HPOSITION1(val) (val)
37 #define REG05_CLIP BIT(0)
38 #define REG05_NVM_VREFRESH BIT(1)
39 #define REG05_SLFR BIT(2)
40 #define REG05_SLBRCHARGE(val) ((val) << 3)
41 #define REG05_PRECHARGE_LEVEL(val) ((val) << 6)
43 #define REG06_TEST5 BIT(0)
44 #define REG06_SLDWN BIT(1)
45 #define REG06_SLRGT BIT(2)
46 #define REG06_TEST2 BIT(3)
47 #define REG06_XPSAVE BIT(4)
48 #define REG06_GAMMA_SEL(val) ((val) << 5)
49 #define REG06_NT BIT(7)
51 #define REG07_TEST1 BIT(0)
52 #define REG07_HDVD_POL BIT(1)
53 #define REG07_CK_POL BIT(2)
54 #define REG07_TEST3 BIT(3)
55 #define REG07_TEST4 BIT(4)
56 #define REG07_480_LINEMASK BIT(5)
57 #define REG07_AMPTST(val) ((val) << 6)
59 #define REG08_SLHRC(val) (val)
60 #define REG08_CLOCK_DIV(val) ((val) << 2)
61 #define REG08_PANEL(val) ((val) << 5)
63 #define REG09_SUB_BRIGHT_R(val) (val)
64 #define REG09_NW_NB BIT(6)
65 #define REG09_IPCON BIT(7)
67 #define REG0A_SUB_BRIGHT_B(val) (val)
68 #define REG0A_PAIR BIT(6)
69 #define REG0A_DE_SEL BIT(7)
71 #define REG0B_MBK_POSITION(val) (val)
72 #define REG0B_HD_FREERUN BIT(4)
73 #define REG0B_VD_FREERUN BIT(5)
74 #define REG0B_YUV2BIN(val) ((val) << 6)
76 #define REG0C_CONTRAST_R(val) (val)
77 #define REG0C_DOUBLEREAD BIT(7)
79 #define REG0D_CONTRAST_G(val) (val)
80 #define REG0D_RGB_YUV BIT(7)
82 #define REG0E_CONTRAST_B(val) (val)
83 #define REG0E_PIXELCOLORDRIVE BIT(7)
85 #define REG0F_ASPECT BIT(0)
86 #define REG0F_OVERSCAN(val) ((val) << 1)
87 #define REG0F_FRAMEWIDTH(val) ((val) << 3)
89 #define REG10_BRIGHT(val) (val)
91 #define REG11_SIG_GAIN(val) (val)
92 #define REG11_SIGC_CNTL BIT(6)
93 #define REG11_SIGC_POL BIT(7)
95 #define REG12_COLOR(val) (val)
96 #define REG12_PWCKSEL(val) ((val) << 6)
98 #define REG13_4096LEVEL_CNTL(val) (val)
99 #define REG13_SL4096(val) ((val) << 4)
100 #define REG13_LIMITER_CONTROL BIT(7)
102 #define REG14_PANEL_TEST(val) (val)
104 #define REG15_NVM_LINK0 BIT(0)
105 #define REG15_NVM_LINK1 BIT(1)
106 #define REG15_NVM_LINK2 BIT(2)
107 #define REG15_NVM_LINK3 BIT(3)
108 #define REG15_NVM_LINK4 BIT(4)
109 #define REG15_NVM_LINK5 BIT(5)
110 #define REG15_NVM_LINK6 BIT(6)
111 #define REG15_NVM_LINK7 BIT(7)
113 struct y030xx067a_info {
114 const struct drm_display_mode *display_modes;
115 unsigned int num_modes;
116 u16 width_mm, height_mm;
117 u32 bus_format, bus_flags;
121 struct drm_panel panel;
122 struct spi_device *spi;
125 const struct y030xx067a_info *panel_info;
127 struct regulator *supply;
128 struct gpio_desc *reset_gpio;
131 static inline struct y030xx067a *to_y030xx067a(struct drm_panel *panel)
133 return container_of(panel, struct y030xx067a, panel);
136 static const struct reg_sequence y030xx067a_init_sequence[] = {
137 { 0x00, REG00_VBRT_CTRL(0x7f) },
138 { 0x01, REG01_COM_DC(0x3c) },
139 { 0x02, REG02_VESA_SEL(0x3) | REG02_DA_CONTRAST(0x1f) },
140 { 0x03, REG03_VPOSITION(0x0a) },
141 { 0x04, REG04_HPOSITION1(0xd2) },
142 { 0x05, REG05_CLIP | REG05_NVM_VREFRESH | REG05_SLBRCHARGE(0x2) },
143 { 0x06, REG06_XPSAVE | REG06_NT },
145 { 0x08, REG08_PANEL(0x1) | REG08_CLOCK_DIV(0x2) },
146 { 0x09, REG09_SUB_BRIGHT_R(0x20) },
147 { 0x0a, REG0A_SUB_BRIGHT_B(0x20) },
148 { 0x0b, REG0B_HD_FREERUN | REG0B_VD_FREERUN },
149 { 0x0c, REG0C_CONTRAST_R(0x10) },
150 { 0x0d, REG0D_CONTRAST_G(0x10) },
151 { 0x0e, REG0E_CONTRAST_B(0x10) },
153 { 0x10, REG10_BRIGHT(0x7f) },
154 { 0x11, REG11_SIGC_CNTL | REG11_SIG_GAIN(0x3f) },
155 { 0x12, REG12_COLOR(0x20) | REG12_PWCKSEL(0x1) },
156 { 0x13, REG13_4096LEVEL_CNTL(0x8) },
161 static int y030xx067a_prepare(struct drm_panel *panel)
163 struct y030xx067a *priv = to_y030xx067a(panel);
164 struct device *dev = &priv->spi->dev;
167 err = regulator_enable(priv->supply);
169 dev_err(dev, "Failed to enable power supply: %d\n", err);
174 gpiod_set_value_cansleep(priv->reset_gpio, 1);
175 usleep_range(1000, 20000);
176 gpiod_set_value_cansleep(priv->reset_gpio, 0);
177 usleep_range(1000, 20000);
179 err = regmap_multi_reg_write(priv->map, y030xx067a_init_sequence,
180 ARRAY_SIZE(y030xx067a_init_sequence));
182 dev_err(dev, "Failed to init registers: %d\n", err);
183 goto err_disable_regulator;
190 err_disable_regulator:
191 regulator_disable(priv->supply);
195 static int y030xx067a_unprepare(struct drm_panel *panel)
197 struct y030xx067a *priv = to_y030xx067a(panel);
199 gpiod_set_value_cansleep(priv->reset_gpio, 1);
200 regulator_disable(priv->supply);
205 static int y030xx067a_get_modes(struct drm_panel *panel,
206 struct drm_connector *connector)
208 struct y030xx067a *priv = to_y030xx067a(panel);
209 const struct y030xx067a_info *panel_info = priv->panel_info;
210 struct drm_display_mode *mode;
213 for (i = 0; i < panel_info->num_modes; i++) {
214 mode = drm_mode_duplicate(connector->dev,
215 &panel_info->display_modes[i]);
219 drm_mode_set_name(mode);
221 mode->type = DRM_MODE_TYPE_DRIVER;
222 if (panel_info->num_modes == 1)
223 mode->type |= DRM_MODE_TYPE_PREFERRED;
225 drm_mode_probed_add(connector, mode);
228 connector->display_info.bpc = 8;
229 connector->display_info.width_mm = panel_info->width_mm;
230 connector->display_info.height_mm = panel_info->height_mm;
232 drm_display_info_set_bus_formats(&connector->display_info,
233 &panel_info->bus_format, 1);
234 connector->display_info.bus_flags = panel_info->bus_flags;
236 return panel_info->num_modes;
239 static const struct drm_panel_funcs y030xx067a_funcs = {
240 .prepare = y030xx067a_prepare,
241 .unprepare = y030xx067a_unprepare,
242 .get_modes = y030xx067a_get_modes,
245 static const struct regmap_config y030xx067a_regmap_config = {
248 .max_register = 0x15,
251 static int y030xx067a_probe(struct spi_device *spi)
253 struct device *dev = &spi->dev;
254 struct y030xx067a *priv;
257 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
262 spi_set_drvdata(spi, priv);
264 priv->map = devm_regmap_init_spi(spi, &y030xx067a_regmap_config);
265 if (IS_ERR(priv->map)) {
266 dev_err(dev, "Unable to init regmap\n");
267 return PTR_ERR(priv->map);
270 priv->panel_info = of_device_get_match_data(dev);
271 if (!priv->panel_info)
274 priv->supply = devm_regulator_get(dev, "power");
275 if (IS_ERR(priv->supply)) {
276 dev_err(dev, "Failed to get power supply\n");
277 return PTR_ERR(priv->supply);
280 priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
281 if (IS_ERR(priv->reset_gpio)) {
282 dev_err(dev, "Failed to get reset GPIO\n");
283 return PTR_ERR(priv->reset_gpio);
286 drm_panel_init(&priv->panel, dev, &y030xx067a_funcs,
287 DRM_MODE_CONNECTOR_DPI);
289 err = drm_panel_of_backlight(&priv->panel);
293 drm_panel_add(&priv->panel);
298 static int y030xx067a_remove(struct spi_device *spi)
300 struct y030xx067a *priv = spi_get_drvdata(spi);
302 drm_panel_remove(&priv->panel);
303 drm_panel_disable(&priv->panel);
304 drm_panel_unprepare(&priv->panel);
309 static const struct drm_display_mode y030xx067a_modes[] = {
313 .hsync_start = 320 + 10,
314 .hsync_end = 320 + 10 + 37,
315 .htotal = 320 + 10 + 37 + 33,
317 .vsync_start = 480 + 84,
318 .vsync_end = 480 + 84 + 20,
319 .vtotal = 480 + 84 + 20 + 16,
320 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
325 .hsync_start = 320 + 10,
326 .hsync_end = 320 + 10 + 37,
327 .htotal = 320 + 10 + 37 + 33,
329 .vsync_start = 480 + 84,
330 .vsync_end = 480 + 84 + 20,
331 .vtotal = 480 + 84 + 20 + 16,
332 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
336 static const struct y030xx067a_info y030xx067a_info = {
337 .display_modes = y030xx067a_modes,
338 .num_modes = ARRAY_SIZE(y030xx067a_modes),
341 .bus_format = MEDIA_BUS_FMT_RGB888_3X8_DELTA,
342 .bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE | DRM_BUS_FLAG_DE_LOW,
345 static const struct of_device_id y030xx067a_of_match[] = {
346 { .compatible = "abt,y030xx067a", .data = &y030xx067a_info },
349 MODULE_DEVICE_TABLE(of, y030xx067a_of_match);
351 static struct spi_driver y030xx067a_driver = {
353 .name = "abt-y030xx067a",
354 .of_match_table = y030xx067a_of_match,
356 .probe = y030xx067a_probe,
357 .remove = y030xx067a_remove,
359 module_spi_driver(y030xx067a_driver);
361 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
362 MODULE_AUTHOR("Christophe Branchereau <cbranchereau@gmail.com>");
363 MODULE_LICENSE("GPL v2");