1 // SPDX-License-Identifier: GPL-2.0
3 * Novatek NT39016 TFT LCD panel driver
5 * Copyright (C) 2017, Maarten ter Huurne <maarten@treewalker.org>
6 * Copyright (C) 2019, Paul Cercueil <paul@crapouillou.net>
9 #include <linux/backlight.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/media-bus-format.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/spi/spi.h>
21 #include <drm/drm_modes.h>
22 #include <drm/drm_panel.h>
29 NT39016_REG_SRC_TIMING_DELAY
,
30 NT39016_REG_GATE_TIMING_DELAY
,
32 NT39016_REG_INITIAL_FUNC
,
34 NT39016_REG_BRIGHTNESS
,
35 NT39016_REG_HUE_SATURATION
,
36 NT39016_REG_RB_SUBCONTRAST
,
37 NT39016_REG_R_SUBBRIGHTNESS
,
38 NT39016_REG_B_SUBBRIGHTNESS
,
44 NT39016_REG_VCOMDC_TRIM
= 0x1e,
45 NT39016_REG_DISPLAY_MODE
= 0x20,
48 #define NT39016_SYSTEM_RESET_N BIT(0)
49 #define NT39016_SYSTEM_STANDBY BIT(1)
51 struct nt39016_panel_info
{
52 struct drm_display_mode display_mode
;
53 u16 width_mm
, height_mm
;
54 u32 bus_format
, bus_flags
;
58 struct drm_panel drm_panel
;
61 struct regulator
*supply
;
62 const struct nt39016_panel_info
*panel_info
;
64 struct gpio_desc
*reset_gpio
;
66 struct backlight_device
*backlight
;
69 static inline struct nt39016
*to_nt39016(struct drm_panel
*panel
)
71 return container_of(panel
, struct nt39016
, drm_panel
);
74 #define RV(REG, VAL) { .reg = (REG), .def = (VAL), .delay_us = 2 }
75 static const struct reg_sequence nt39016_panel_regs
[] = {
76 RV(NT39016_REG_SYSTEM
, 0x00),
77 RV(NT39016_REG_TIMING
, 0x00),
78 RV(NT39016_REG_OP
, 0x03),
79 RV(NT39016_REG_DATA_IN
, 0xCC),
80 RV(NT39016_REG_SRC_TIMING_DELAY
, 0x46),
81 RV(NT39016_REG_GATE_TIMING_DELAY
, 0x05),
82 RV(NT39016_REG_RESERVED
, 0x00),
83 RV(NT39016_REG_INITIAL_FUNC
, 0x00),
84 RV(NT39016_REG_CONTRAST
, 0x08),
85 RV(NT39016_REG_BRIGHTNESS
, 0x40),
86 RV(NT39016_REG_HUE_SATURATION
, 0x88),
87 RV(NT39016_REG_RB_SUBCONTRAST
, 0x88),
88 RV(NT39016_REG_R_SUBBRIGHTNESS
, 0x20),
89 RV(NT39016_REG_B_SUBBRIGHTNESS
, 0x20),
90 RV(NT39016_REG_VCOMDC
, 0x67),
91 RV(NT39016_REG_VCOMAC
, 0xA4),
92 RV(NT39016_REG_VGAM2
, 0x04),
93 RV(NT39016_REG_VGAM34
, 0x24),
94 RV(NT39016_REG_VGAM56
, 0x24),
95 RV(NT39016_REG_DISPLAY_MODE
, 0x00),
100 static const struct regmap_range nt39016_regmap_no_ranges
[] = {
101 regmap_reg_range(0x13, 0x1D),
102 regmap_reg_range(0x1F, 0x1F),
105 static const struct regmap_access_table nt39016_regmap_access_table
= {
106 .no_ranges
= nt39016_regmap_no_ranges
,
107 .n_no_ranges
= ARRAY_SIZE(nt39016_regmap_no_ranges
),
110 static const struct regmap_config nt39016_regmap_config
= {
115 .max_register
= NT39016_REG_DISPLAY_MODE
,
116 .wr_table
= &nt39016_regmap_access_table
,
117 .write_flag_mask
= 0x02,
119 .cache_type
= REGCACHE_FLAT
,
122 static int nt39016_prepare(struct drm_panel
*drm_panel
)
124 struct nt39016
*panel
= to_nt39016(drm_panel
);
127 err
= regulator_enable(panel
->supply
);
129 dev_err(panel
->dev
, "Failed to enable power supply: %d", err
);
135 * The documentation says the reset pulse should be at least 40 us to
136 * pass the glitch filter, but when testing I see some resets fail and
137 * some succeed when using a 70 us delay, so we use 100 us instead.
139 gpiod_set_value_cansleep(panel
->reset_gpio
, 1);
140 usleep_range(100, 1000);
141 gpiod_set_value_cansleep(panel
->reset_gpio
, 0);
144 /* Init all registers. */
145 err
= regmap_multi_reg_write(panel
->map
, nt39016_panel_regs
,
146 ARRAY_SIZE(nt39016_panel_regs
));
148 dev_err(panel
->dev
, "Failed to init registers: %d", err
);
149 goto err_disable_regulator
;
154 err_disable_regulator
:
155 regulator_disable(panel
->supply
);
159 static int nt39016_unprepare(struct drm_panel
*drm_panel
)
161 struct nt39016
*panel
= to_nt39016(drm_panel
);
163 gpiod_set_value_cansleep(panel
->reset_gpio
, 1);
165 regulator_disable(panel
->supply
);
170 static int nt39016_enable(struct drm_panel
*drm_panel
)
172 struct nt39016
*panel
= to_nt39016(drm_panel
);
175 ret
= regmap_write(panel
->map
, NT39016_REG_SYSTEM
,
176 NT39016_SYSTEM_RESET_N
| NT39016_SYSTEM_STANDBY
);
178 dev_err(panel
->dev
, "Unable to enable panel: %d", ret
);
182 if (panel
->backlight
) {
183 /* Wait for the picture to be ready before enabling backlight */
186 ret
= backlight_enable(panel
->backlight
);
192 static int nt39016_disable(struct drm_panel
*drm_panel
)
194 struct nt39016
*panel
= to_nt39016(drm_panel
);
197 backlight_disable(panel
->backlight
);
199 err
= regmap_write(panel
->map
, NT39016_REG_SYSTEM
,
200 NT39016_SYSTEM_RESET_N
);
202 dev_err(panel
->dev
, "Unable to disable panel: %d", err
);
209 static int nt39016_get_modes(struct drm_panel
*drm_panel
,
210 struct drm_connector
*connector
)
212 struct nt39016
*panel
= to_nt39016(drm_panel
);
213 const struct nt39016_panel_info
*panel_info
= panel
->panel_info
;
214 struct drm_display_mode
*mode
;
216 mode
= drm_mode_duplicate(connector
->dev
, &panel_info
->display_mode
);
220 drm_mode_set_name(mode
);
222 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
223 drm_mode_probed_add(connector
, mode
);
225 connector
->display_info
.bpc
= 8;
226 connector
->display_info
.width_mm
= panel_info
->width_mm
;
227 connector
->display_info
.height_mm
= panel_info
->height_mm
;
229 drm_display_info_set_bus_formats(&connector
->display_info
,
230 &panel_info
->bus_format
, 1);
231 connector
->display_info
.bus_flags
= panel_info
->bus_flags
;
236 static const struct drm_panel_funcs nt39016_funcs
= {
237 .prepare
= nt39016_prepare
,
238 .unprepare
= nt39016_unprepare
,
239 .enable
= nt39016_enable
,
240 .disable
= nt39016_disable
,
241 .get_modes
= nt39016_get_modes
,
244 static int nt39016_probe(struct spi_device
*spi
)
246 struct device
*dev
= &spi
->dev
;
247 struct nt39016
*panel
;
250 panel
= devm_kzalloc(dev
, sizeof(*panel
), GFP_KERNEL
);
255 spi_set_drvdata(spi
, panel
);
257 panel
->panel_info
= of_device_get_match_data(dev
);
258 if (!panel
->panel_info
)
261 panel
->supply
= devm_regulator_get(dev
, "power");
262 if (IS_ERR(panel
->supply
)) {
263 dev_err(dev
, "Failed to get power supply");
264 return PTR_ERR(panel
->supply
);
267 panel
->reset_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
268 if (IS_ERR(panel
->reset_gpio
)) {
269 dev_err(dev
, "Failed to get reset GPIO");
270 return PTR_ERR(panel
->reset_gpio
);
273 spi
->bits_per_word
= 8;
274 spi
->mode
= SPI_MODE_3
| SPI_3WIRE
;
275 err
= spi_setup(spi
);
277 dev_err(dev
, "Failed to setup SPI");
281 panel
->map
= devm_regmap_init_spi(spi
, &nt39016_regmap_config
);
282 if (IS_ERR(panel
->map
)) {
283 dev_err(dev
, "Failed to init regmap");
284 return PTR_ERR(panel
->map
);
287 panel
->backlight
= devm_of_find_backlight(dev
);
288 if (IS_ERR(panel
->backlight
)) {
289 err
= PTR_ERR(panel
->backlight
);
290 if (err
!= -EPROBE_DEFER
)
291 dev_err(dev
, "Failed to get backlight handle");
295 drm_panel_init(&panel
->drm_panel
, dev
, &nt39016_funcs
,
296 DRM_MODE_CONNECTOR_DPI
);
298 err
= drm_panel_add(&panel
->drm_panel
);
300 dev_err(dev
, "Failed to register panel");
307 static int nt39016_remove(struct spi_device
*spi
)
309 struct nt39016
*panel
= spi_get_drvdata(spi
);
311 drm_panel_remove(&panel
->drm_panel
);
313 nt39016_disable(&panel
->drm_panel
);
314 nt39016_unprepare(&panel
->drm_panel
);
319 static const struct nt39016_panel_info kd035g6_info
= {
323 .hsync_start
= 320 + 10,
324 .hsync_end
= 320 + 10 + 50,
325 .htotal
= 320 + 10 + 50 + 20,
327 .vsync_start
= 240 + 5,
328 .vsync_end
= 240 + 5 + 1,
329 .vtotal
= 240 + 5 + 1 + 4,
331 .flags
= DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_NVSYNC
,
335 .bus_format
= MEDIA_BUS_FMT_RGB888_1X24
,
336 .bus_flags
= DRM_BUS_FLAG_PIXDATA_NEGEDGE
,
339 static const struct of_device_id nt39016_of_match
[] = {
340 { .compatible
= "kingdisplay,kd035g6-54nt", .data
= &kd035g6_info
},
343 MODULE_DEVICE_TABLE(of
, nt39016_of_match
);
345 static struct spi_driver nt39016_driver
= {
348 .of_match_table
= nt39016_of_match
,
350 .probe
= nt39016_probe
,
351 .remove
= nt39016_remove
,
354 module_spi_driver(nt39016_driver
);
356 MODULE_AUTHOR("Maarten ter Huurne <maarten@treewalker.org>");
357 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
358 MODULE_LICENSE("GPL v2");