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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Novatek NT39016 TFT LCD panel driver
*
* Copyright (C) 2017, Maarten ter Huurne <maarten@treewalker.org>
* Copyright (C) 2019, Paul Cercueil <paul@crapouillou.net>
*/
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/gpio/consumer.h>
#include <linux/media-bus-format.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/spi/spi.h>
#include <drm/drm_modes.h>
#include <drm/drm_panel.h>
enum nt39016_regs {
NT39016_REG_SYSTEM,
NT39016_REG_TIMING,
NT39016_REG_OP,
NT39016_REG_DATA_IN,
NT39016_REG_SRC_TIMING_DELAY,
NT39016_REG_GATE_TIMING_DELAY,
NT39016_REG_RESERVED,
NT39016_REG_INITIAL_FUNC,
NT39016_REG_CONTRAST,
NT39016_REG_BRIGHTNESS,
NT39016_REG_HUE_SATURATION,
NT39016_REG_RB_SUBCONTRAST,
NT39016_REG_R_SUBBRIGHTNESS,
NT39016_REG_B_SUBBRIGHTNESS,
NT39016_REG_VCOMDC,
NT39016_REG_VCOMAC,
NT39016_REG_VGAM2,
NT39016_REG_VGAM34,
NT39016_REG_VGAM56,
NT39016_REG_VCOMDC_TRIM = 0x1e,
NT39016_REG_DISPLAY_MODE = 0x20,
};
#define NT39016_SYSTEM_RESET_N BIT(0)
#define NT39016_SYSTEM_STANDBY BIT(1)
struct nt39016_panel_info {
const struct drm_display_mode *display_modes;
unsigned int num_modes;
u16 width_mm, height_mm;
u32 bus_format, bus_flags;
};
struct nt39016 {
struct drm_panel drm_panel;
struct regmap *map;
struct regulator *supply;
const struct nt39016_panel_info *panel_info;
struct gpio_desc *reset_gpio;
};
static inline struct nt39016 *to_nt39016(struct drm_panel *panel)
{
return container_of(panel, struct nt39016, drm_panel);
}
#define RV(REG, VAL) { .reg = (REG), .def = (VAL), .delay_us = 2 }
static const struct reg_sequence nt39016_panel_regs[] = {
RV(NT39016_REG_SYSTEM, 0x00),
RV(NT39016_REG_TIMING, 0x00),
RV(NT39016_REG_OP, 0x03),
RV(NT39016_REG_DATA_IN, 0xCC),
RV(NT39016_REG_SRC_TIMING_DELAY, 0x46),
RV(NT39016_REG_GATE_TIMING_DELAY, 0x05),
RV(NT39016_REG_RESERVED, 0x00),
RV(NT39016_REG_INITIAL_FUNC, 0x00),
RV(NT39016_REG_CONTRAST, 0x08),
RV(NT39016_REG_BRIGHTNESS, 0x40),
RV(NT39016_REG_HUE_SATURATION, 0x88),
RV(NT39016_REG_RB_SUBCONTRAST, 0x88),
RV(NT39016_REG_R_SUBBRIGHTNESS, 0x20),
RV(NT39016_REG_B_SUBBRIGHTNESS, 0x20),
RV(NT39016_REG_VCOMDC, 0x67),
RV(NT39016_REG_VCOMAC, 0xA4),
RV(NT39016_REG_VGAM2, 0x04),
RV(NT39016_REG_VGAM34, 0x24),
RV(NT39016_REG_VGAM56, 0x24),
RV(NT39016_REG_DISPLAY_MODE, 0x00),
};
#undef RV
static const struct regmap_range nt39016_regmap_no_ranges[] = {
regmap_reg_range(0x13, 0x1D),
regmap_reg_range(0x1F, 0x1F),
};
static const struct regmap_access_table nt39016_regmap_access_table = {
.no_ranges = nt39016_regmap_no_ranges,
.n_no_ranges = ARRAY_SIZE(nt39016_regmap_no_ranges),
};
static const struct regmap_config nt39016_regmap_config = {
.reg_bits = 6,
.pad_bits = 2,
.val_bits = 8,
.max_register = NT39016_REG_DISPLAY_MODE,
.wr_table = &nt39016_regmap_access_table,
.write_flag_mask = 0x02,
.cache_type = REGCACHE_FLAT,
};
static int nt39016_prepare(struct drm_panel *drm_panel)
{
struct nt39016 *panel = to_nt39016(drm_panel);
int err;
err = regulator_enable(panel->supply);
if (err) {
dev_err(drm_panel->dev, "Failed to enable power supply: %d\n", err);
return err;
}
/*
* Reset the NT39016.
* The documentation says the reset pulse should be at least 40 us to
* pass the glitch filter, but when testing I see some resets fail and
* some succeed when using a 70 us delay, so we use 100 us instead.
*/
gpiod_set_value_cansleep(panel->reset_gpio, 1);
usleep_range(100, 1000);
gpiod_set_value_cansleep(panel->reset_gpio, 0);
udelay(2);
/* Init all registers. */
err = regmap_multi_reg_write(panel->map, nt39016_panel_regs,
ARRAY_SIZE(nt39016_panel_regs));
if (err) {
dev_err(drm_panel->dev, "Failed to init registers: %d\n", err);
goto err_disable_regulator;
}
return 0;
err_disable_regulator:
regulator_disable(panel->supply);
return err;
}
static int nt39016_unprepare(struct drm_panel *drm_panel)
{
struct nt39016 *panel = to_nt39016(drm_panel);
gpiod_set_value_cansleep(panel->reset_gpio, 1);
regulator_disable(panel->supply);
return 0;
}
static int nt39016_enable(struct drm_panel *drm_panel)
{
struct nt39016 *panel = to_nt39016(drm_panel);
int ret;
ret = regmap_write(panel->map, NT39016_REG_SYSTEM,
NT39016_SYSTEM_RESET_N | NT39016_SYSTEM_STANDBY);
if (ret) {
dev_err(drm_panel->dev, "Unable to enable panel: %d\n", ret);
return ret;
}
if (drm_panel->backlight) {
/* Wait for the picture to be ready before enabling backlight */
msleep(150);
}
return 0;
}
static int nt39016_disable(struct drm_panel *drm_panel)
{
struct nt39016 *panel = to_nt39016(drm_panel);
int err;
err = regmap_write(panel->map, NT39016_REG_SYSTEM,
NT39016_SYSTEM_RESET_N);
if (err) {
dev_err(drm_panel->dev, "Unable to disable panel: %d\n", err);
return err;
}
return 0;
}
static int nt39016_get_modes(struct drm_panel *drm_panel,
struct drm_connector *connector)
{
struct nt39016 *panel = to_nt39016(drm_panel);
const struct nt39016_panel_info *panel_info = panel->panel_info;
struct drm_display_mode *mode;
unsigned int i;
for (i = 0; i < panel_info->num_modes; i++) {
mode = drm_mode_duplicate(connector->dev,
&panel_info->display_modes[i]);
if (!mode)
return -ENOMEM;
drm_mode_set_name(mode);
mode->type = DRM_MODE_TYPE_DRIVER;
if (panel_info->num_modes == 1)
mode->type |= DRM_MODE_TYPE_PREFERRED;
drm_mode_probed_add(connector, mode);
}
connector->display_info.bpc = 8;
connector->display_info.width_mm = panel_info->width_mm;
connector->display_info.height_mm = panel_info->height_mm;
drm_display_info_set_bus_formats(&connector->display_info,
&panel_info->bus_format, 1);
connector->display_info.bus_flags = panel_info->bus_flags;
return panel_info->num_modes;
}
static const struct drm_panel_funcs nt39016_funcs = {
.prepare = nt39016_prepare,
.unprepare = nt39016_unprepare,
.enable = nt39016_enable,
.disable = nt39016_disable,
.get_modes = nt39016_get_modes,
};
static int nt39016_probe(struct spi_device *spi)
{
struct device *dev = &spi->dev;
struct nt39016 *panel;
int err;
panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
if (!panel)
return -ENOMEM;
spi_set_drvdata(spi, panel);
panel->panel_info = of_device_get_match_data(dev);
if (!panel->panel_info)
return -EINVAL;
panel->supply = devm_regulator_get(dev, "power");
if (IS_ERR(panel->supply))
return dev_err_probe(dev, PTR_ERR(panel->supply),
"Failed to get power supply\n");
panel->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
if (IS_ERR(panel->reset_gpio))
return dev_err_probe(dev, PTR_ERR(panel->reset_gpio), "Failed to get reset GPIO\n");
spi->bits_per_word = 8;
spi->mode = SPI_MODE_3 | SPI_3WIRE;
err = spi_setup(spi);
if (err) {
dev_err(dev, "Failed to setup SPI\n");
return err;
}
panel->map = devm_regmap_init_spi(spi, &nt39016_regmap_config);
if (IS_ERR(panel->map)) {
dev_err(dev, "Failed to init regmap\n");
return PTR_ERR(panel->map);
}
drm_panel_init(&panel->drm_panel, dev, &nt39016_funcs,
DRM_MODE_CONNECTOR_DPI);
err = drm_panel_of_backlight(&panel->drm_panel);
if (err)
return dev_err_probe(dev, err, "Failed to get backlight handle\n");
drm_panel_add(&panel->drm_panel);
return 0;
}
static void nt39016_remove(struct spi_device *spi)
{
struct nt39016 *panel = spi_get_drvdata(spi);
drm_panel_remove(&panel->drm_panel);
nt39016_disable(&panel->drm_panel);
nt39016_unprepare(&panel->drm_panel);
}
static const struct drm_display_mode kd035g6_display_modes[] = {
{ /* 60 Hz */
.clock = 6000,
.hdisplay = 320,
.hsync_start = 320 + 10,
.hsync_end = 320 + 10 + 50,
.htotal = 320 + 10 + 50 + 20,
.vdisplay = 240,
.vsync_start = 240 + 5,
.vsync_end = 240 + 5 + 1,
.vtotal = 240 + 5 + 1 + 4,
.flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
},
{ /* 50 Hz */
.clock = 5400,
.hdisplay = 320,
.hsync_start = 320 + 42,
.hsync_end = 320 + 42 + 50,
.htotal = 320 + 42 + 50 + 20,
.vdisplay = 240,
.vsync_start = 240 + 5,
.vsync_end = 240 + 5 + 1,
.vtotal = 240 + 5 + 1 + 4,
.flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
},
};
static const struct nt39016_panel_info kd035g6_info = {
.display_modes = kd035g6_display_modes,
.num_modes = ARRAY_SIZE(kd035g6_display_modes),
.width_mm = 71,
.height_mm = 53,
.bus_format = MEDIA_BUS_FMT_RGB888_1X24,
.bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
};
static const struct of_device_id nt39016_of_match[] = {
{ .compatible = "kingdisplay,kd035g6-54nt", .data = &kd035g6_info },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, nt39016_of_match);
static struct spi_driver nt39016_driver = {
.driver = {
.name = "nt39016",
.of_match_table = nt39016_of_match,
},
.probe = nt39016_probe,
.remove = nt39016_remove,
};
module_spi_driver(nt39016_driver);
MODULE_AUTHOR("Maarten ter Huurne <maarten@treewalker.org>");
MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
MODULE_LICENSE("GPL v2");
|