xref: /linux/drivers/gpu/drm/panel/panel-novatek-nt39016.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Novatek NT39016 TFT LCD panel driver
4  *
5  * Copyright (C) 2017, Maarten ter Huurne <maarten@treewalker.org>
6  * Copyright (C) 2019, Paul Cercueil <paul@crapouillou.net>
7  */
8 
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.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/spi/spi.h>
18 
19 #include <drm/drm_modes.h>
20 #include <drm/drm_panel.h>
21 
22 enum nt39016_regs {
23 	NT39016_REG_SYSTEM,
24 	NT39016_REG_TIMING,
25 	NT39016_REG_OP,
26 	NT39016_REG_DATA_IN,
27 	NT39016_REG_SRC_TIMING_DELAY,
28 	NT39016_REG_GATE_TIMING_DELAY,
29 	NT39016_REG_RESERVED,
30 	NT39016_REG_INITIAL_FUNC,
31 	NT39016_REG_CONTRAST,
32 	NT39016_REG_BRIGHTNESS,
33 	NT39016_REG_HUE_SATURATION,
34 	NT39016_REG_RB_SUBCONTRAST,
35 	NT39016_REG_R_SUBBRIGHTNESS,
36 	NT39016_REG_B_SUBBRIGHTNESS,
37 	NT39016_REG_VCOMDC,
38 	NT39016_REG_VCOMAC,
39 	NT39016_REG_VGAM2,
40 	NT39016_REG_VGAM34,
41 	NT39016_REG_VGAM56,
42 	NT39016_REG_VCOMDC_TRIM = 0x1e,
43 	NT39016_REG_DISPLAY_MODE = 0x20,
44 };
45 
46 #define NT39016_SYSTEM_RESET_N	BIT(0)
47 #define NT39016_SYSTEM_STANDBY	BIT(1)
48 
49 struct nt39016_panel_info {
50 	const struct drm_display_mode *display_modes;
51 	unsigned int num_modes;
52 	u16 width_mm, height_mm;
53 	u32 bus_format, bus_flags;
54 };
55 
56 struct nt39016 {
57 	struct drm_panel drm_panel;
58 	struct regmap *map;
59 	struct regulator *supply;
60 	const struct nt39016_panel_info *panel_info;
61 
62 	struct gpio_desc *reset_gpio;
63 };
64 
to_nt39016(struct drm_panel * panel)65 static inline struct nt39016 *to_nt39016(struct drm_panel *panel)
66 {
67 	return container_of(panel, struct nt39016, drm_panel);
68 }
69 
70 #define RV(REG, VAL) { .reg = (REG), .def = (VAL), .delay_us = 2 }
71 static const struct reg_sequence nt39016_panel_regs[] = {
72 	RV(NT39016_REG_SYSTEM, 0x00),
73 	RV(NT39016_REG_TIMING, 0x00),
74 	RV(NT39016_REG_OP, 0x03),
75 	RV(NT39016_REG_DATA_IN, 0xCC),
76 	RV(NT39016_REG_SRC_TIMING_DELAY, 0x46),
77 	RV(NT39016_REG_GATE_TIMING_DELAY, 0x05),
78 	RV(NT39016_REG_RESERVED, 0x00),
79 	RV(NT39016_REG_INITIAL_FUNC, 0x00),
80 	RV(NT39016_REG_CONTRAST, 0x08),
81 	RV(NT39016_REG_BRIGHTNESS, 0x40),
82 	RV(NT39016_REG_HUE_SATURATION, 0x88),
83 	RV(NT39016_REG_RB_SUBCONTRAST, 0x88),
84 	RV(NT39016_REG_R_SUBBRIGHTNESS, 0x20),
85 	RV(NT39016_REG_B_SUBBRIGHTNESS, 0x20),
86 	RV(NT39016_REG_VCOMDC, 0x67),
87 	RV(NT39016_REG_VCOMAC, 0xA4),
88 	RV(NT39016_REG_VGAM2, 0x04),
89 	RV(NT39016_REG_VGAM34, 0x24),
90 	RV(NT39016_REG_VGAM56, 0x24),
91 	RV(NT39016_REG_DISPLAY_MODE, 0x00),
92 };
93 
94 #undef RV
95 
96 static const struct regmap_range nt39016_regmap_no_ranges[] = {
97 	regmap_reg_range(0x13, 0x1D),
98 	regmap_reg_range(0x1F, 0x1F),
99 };
100 
101 static const struct regmap_access_table nt39016_regmap_access_table = {
102 	.no_ranges = nt39016_regmap_no_ranges,
103 	.n_no_ranges = ARRAY_SIZE(nt39016_regmap_no_ranges),
104 };
105 
106 static const struct regmap_config nt39016_regmap_config = {
107 	.reg_bits = 6,
108 	.pad_bits = 2,
109 	.val_bits = 8,
110 
111 	.max_register = NT39016_REG_DISPLAY_MODE,
112 	.wr_table = &nt39016_regmap_access_table,
113 	.write_flag_mask = 0x02,
114 
115 	.cache_type = REGCACHE_FLAT,
116 };
117 
nt39016_prepare(struct drm_panel * drm_panel)118 static int nt39016_prepare(struct drm_panel *drm_panel)
119 {
120 	struct nt39016 *panel = to_nt39016(drm_panel);
121 	int err;
122 
123 	err = regulator_enable(panel->supply);
124 	if (err) {
125 		dev_err(drm_panel->dev, "Failed to enable power supply: %d\n", err);
126 		return err;
127 	}
128 
129 	/*
130 	 * Reset the NT39016.
131 	 * The documentation says the reset pulse should be at least 40 us to
132 	 * pass the glitch filter, but when testing I see some resets fail and
133 	 * some succeed when using a 70 us delay, so we use 100 us instead.
134 	 */
135 	gpiod_set_value_cansleep(panel->reset_gpio, 1);
136 	usleep_range(100, 1000);
137 	gpiod_set_value_cansleep(panel->reset_gpio, 0);
138 	udelay(2);
139 
140 	/* Init all registers. */
141 	err = regmap_multi_reg_write(panel->map, nt39016_panel_regs,
142 				     ARRAY_SIZE(nt39016_panel_regs));
143 	if (err) {
144 		dev_err(drm_panel->dev, "Failed to init registers: %d\n", err);
145 		goto err_disable_regulator;
146 	}
147 
148 	return 0;
149 
150 err_disable_regulator:
151 	regulator_disable(panel->supply);
152 	return err;
153 }
154 
nt39016_unprepare(struct drm_panel * drm_panel)155 static int nt39016_unprepare(struct drm_panel *drm_panel)
156 {
157 	struct nt39016 *panel = to_nt39016(drm_panel);
158 
159 	gpiod_set_value_cansleep(panel->reset_gpio, 1);
160 
161 	regulator_disable(panel->supply);
162 
163 	return 0;
164 }
165 
nt39016_enable(struct drm_panel * drm_panel)166 static int nt39016_enable(struct drm_panel *drm_panel)
167 {
168 	struct nt39016 *panel = to_nt39016(drm_panel);
169 	int ret;
170 
171 	ret = regmap_write(panel->map, NT39016_REG_SYSTEM,
172 			   NT39016_SYSTEM_RESET_N | NT39016_SYSTEM_STANDBY);
173 	if (ret) {
174 		dev_err(drm_panel->dev, "Unable to enable panel: %d\n", ret);
175 		return ret;
176 	}
177 
178 	if (drm_panel->backlight) {
179 		/* Wait for the picture to be ready before enabling backlight */
180 		msleep(150);
181 	}
182 
183 	return 0;
184 }
185 
nt39016_disable(struct drm_panel * drm_panel)186 static int nt39016_disable(struct drm_panel *drm_panel)
187 {
188 	struct nt39016 *panel = to_nt39016(drm_panel);
189 	int err;
190 
191 	err = regmap_write(panel->map, NT39016_REG_SYSTEM,
192 			   NT39016_SYSTEM_RESET_N);
193 	if (err) {
194 		dev_err(drm_panel->dev, "Unable to disable panel: %d\n", err);
195 		return err;
196 	}
197 
198 	return 0;
199 }
200 
nt39016_get_modes(struct drm_panel * drm_panel,struct drm_connector * connector)201 static int nt39016_get_modes(struct drm_panel *drm_panel,
202 			     struct drm_connector *connector)
203 {
204 	struct nt39016 *panel = to_nt39016(drm_panel);
205 	const struct nt39016_panel_info *panel_info = panel->panel_info;
206 	struct drm_display_mode *mode;
207 	unsigned int i;
208 
209 	for (i = 0; i < panel_info->num_modes; i++) {
210 		mode = drm_mode_duplicate(connector->dev,
211 					  &panel_info->display_modes[i]);
212 		if (!mode)
213 			return -ENOMEM;
214 
215 		drm_mode_set_name(mode);
216 
217 		mode->type = DRM_MODE_TYPE_DRIVER;
218 		if (panel_info->num_modes == 1)
219 			mode->type |= DRM_MODE_TYPE_PREFERRED;
220 
221 		drm_mode_probed_add(connector, mode);
222 	}
223 
224 	connector->display_info.bpc = 8;
225 	connector->display_info.width_mm = panel_info->width_mm;
226 	connector->display_info.height_mm = panel_info->height_mm;
227 
228 	drm_display_info_set_bus_formats(&connector->display_info,
229 					 &panel_info->bus_format, 1);
230 	connector->display_info.bus_flags = panel_info->bus_flags;
231 
232 	return panel_info->num_modes;
233 }
234 
235 static const struct drm_panel_funcs nt39016_funcs = {
236 	.prepare	= nt39016_prepare,
237 	.unprepare	= nt39016_unprepare,
238 	.enable		= nt39016_enable,
239 	.disable	= nt39016_disable,
240 	.get_modes	= nt39016_get_modes,
241 };
242 
nt39016_probe(struct spi_device * spi)243 static int nt39016_probe(struct spi_device *spi)
244 {
245 	struct device *dev = &spi->dev;
246 	struct nt39016 *panel;
247 	int err;
248 
249 	panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
250 	if (!panel)
251 		return -ENOMEM;
252 
253 	spi_set_drvdata(spi, panel);
254 
255 	panel->panel_info = of_device_get_match_data(dev);
256 	if (!panel->panel_info)
257 		return -EINVAL;
258 
259 	panel->supply = devm_regulator_get(dev, "power");
260 	if (IS_ERR(panel->supply))
261 		return dev_err_probe(dev, PTR_ERR(panel->supply),
262 				     "Failed to get power supply\n");
263 
264 	panel->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
265 	if (IS_ERR(panel->reset_gpio))
266 		return dev_err_probe(dev, PTR_ERR(panel->reset_gpio), "Failed to get reset GPIO\n");
267 
268 	spi->bits_per_word = 8;
269 	spi->mode = SPI_MODE_3 | SPI_3WIRE;
270 	err = spi_setup(spi);
271 	if (err) {
272 		dev_err(dev, "Failed to setup SPI\n");
273 		return err;
274 	}
275 
276 	panel->map = devm_regmap_init_spi(spi, &nt39016_regmap_config);
277 	if (IS_ERR(panel->map)) {
278 		dev_err(dev, "Failed to init regmap\n");
279 		return PTR_ERR(panel->map);
280 	}
281 
282 	drm_panel_init(&panel->drm_panel, dev, &nt39016_funcs,
283 		       DRM_MODE_CONNECTOR_DPI);
284 
285 	err = drm_panel_of_backlight(&panel->drm_panel);
286 	if (err)
287 		return dev_err_probe(dev, err, "Failed to get backlight handle\n");
288 
289 	drm_panel_add(&panel->drm_panel);
290 
291 	return 0;
292 }
293 
nt39016_remove(struct spi_device * spi)294 static void nt39016_remove(struct spi_device *spi)
295 {
296 	struct nt39016 *panel = spi_get_drvdata(spi);
297 
298 	drm_panel_remove(&panel->drm_panel);
299 
300 	nt39016_disable(&panel->drm_panel);
301 	nt39016_unprepare(&panel->drm_panel);
302 }
303 
304 static const struct drm_display_mode kd035g6_display_modes[] = {
305 	{	/* 60 Hz */
306 		.clock = 6000,
307 		.hdisplay = 320,
308 		.hsync_start = 320 + 10,
309 		.hsync_end = 320 + 10 + 50,
310 		.htotal = 320 + 10 + 50 + 20,
311 		.vdisplay = 240,
312 		.vsync_start = 240 + 5,
313 		.vsync_end = 240 + 5 + 1,
314 		.vtotal = 240 + 5 + 1 + 4,
315 		.flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
316 	},
317 	{	/* 50 Hz */
318 		.clock = 5400,
319 		.hdisplay = 320,
320 		.hsync_start = 320 + 42,
321 		.hsync_end = 320 + 42 + 50,
322 		.htotal = 320 + 42 + 50 + 20,
323 		.vdisplay = 240,
324 		.vsync_start = 240 + 5,
325 		.vsync_end = 240 + 5 + 1,
326 		.vtotal = 240 + 5 + 1 + 4,
327 		.flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
328 	},
329 };
330 
331 static const struct nt39016_panel_info kd035g6_info = {
332 	.display_modes = kd035g6_display_modes,
333 	.num_modes = ARRAY_SIZE(kd035g6_display_modes),
334 	.width_mm = 71,
335 	.height_mm = 53,
336 	.bus_format = MEDIA_BUS_FMT_RGB888_1X24,
337 	.bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
338 };
339 
340 static const struct of_device_id nt39016_of_match[] = {
341 	{ .compatible = "kingdisplay,kd035g6-54nt", .data = &kd035g6_info },
342 	{ /* sentinel */ }
343 };
344 MODULE_DEVICE_TABLE(of, nt39016_of_match);
345 
346 static struct spi_driver nt39016_driver = {
347 	.driver = {
348 		.name = "nt39016",
349 		.of_match_table = nt39016_of_match,
350 	},
351 	.probe = nt39016_probe,
352 	.remove = nt39016_remove,
353 };
354 
355 module_spi_driver(nt39016_driver);
356 
357 MODULE_AUTHOR("Maarten ter Huurne <maarten@treewalker.org>");
358 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
359 MODULE_DESCRIPTION("Novatek NT39016 TFT LCD panel driver");
360 MODULE_LICENSE("GPL v2");
361