This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathili9488.c
403 lines (327 loc) · 10.1 KB
/
ili9488.c
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (C) 2019, Elkami
*/
#include <linux/backlight.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/fb.h>
#include <linux/of.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/gpio/consumer.h>
#include <linux/gpio/machine.h>
#include <linux/regulator/consumer.h>
#include <linux/pinctrl/pinctrl-state.h>
#include <linux/pinctrl/consumer.h>
#include <linux/pinctrl/devinfo.h>
#include <linux/pinctrl/machine.h>
#include <linux/pinctrl/pinconf-generic.h>
#include <linux/pinctrl/pinconf.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinmux.h>
#include <drm/drm_mipi_dsi.h>
#include <drm/drm_modes.h>
#include <drm/drm_panel.h>
#include <video/mipi_display.h>
#define WIDTH_MM 45
#define HEIGHT_MM 33
#define GPIO_HIGH 1
#define GPIO_LOW 0
struct ili9488c {
struct drm_panel panel;
struct mipi_dsi_device *dsi;
struct backlight_device *backlight;
struct regulator *power;
struct gpio_desc *reset;
};
static inline struct ili9488c *panel_to_ili9488c(struct drm_panel *panel)
{
return container_of(panel, struct ili9488c, panel);
}
void static ili9488c_sendCmd(struct ili9488c* ctx, u8 cmd, const void *data, size_t len)
{
int ret = mipi_dsi_dcs_write(ctx->dsi, cmd, data, len);
if (ret < 0)
printk(KERN_ERR "ili9488 send %02x command failed. Size = %ld\n", cmd, len);
else
printk(KERN_INFO "ili9488 send %02x command with success. Size = %ld\n", cmd, len);
}
static int ili9488c_prepare(struct drm_panel *panel)
{
printk(KERN_INFO "ili9488c_prepare\n");
struct ili9488c* ctx = panel_to_ili9488c(panel);
int ret = regulator_enable(ctx->power); /* Power the panel */
if (ret)
{
printk(KERN_ERR "ili9488c_prepare - return regulator_enable\n");
return ret;
}
msleep(5);
/* And reset it */
gpiod_set_value(ctx->reset, GPIO_HIGH);
msleep(1);
gpiod_set_value(ctx->reset, GPIO_LOW);
msleep(10);
gpiod_set_value(ctx->reset, GPIO_HIGH);
msleep(120);
// Init register
{
u8 buf[] = { 0xFF, 0x98, 0x06 };
ili9488c_sendCmd(ctx, 0xFF, buf, sizeof(buf));
}
{
u8 buf[] = { 0x00, 0x04, 0x0E, 0x08, 0x17, 0x0A, 0x40, 0x79, 0x4D, 0x07, 0x0E, 0x0A, 0x1A, 0x1D, 0x0F };
ili9488c_sendCmd(ctx, 0xE0, buf, sizeof(buf));
}
{
u8 buf[] = { 0x00, 0x1B, 0x1F, 0x02, 0x10, 0x05, 0x32, 0x34, 0x43, 0x02, 0x0A, 0x09, 0x33, 0x37, 0x0F };
ili9488c_sendCmd(ctx, 0xE1, buf, sizeof(buf));
}
{
u8 buf[] = { 0x41 };
ili9488c_sendCmd(ctx, 0xC1, buf, sizeof(buf));
}
{
u8 buf[] = { 0x00, 0x22, 0x80 };
ili9488c_sendCmd(ctx, 0xC5, buf, sizeof(buf));
}
{
u8 buf[] = { 0x48 };
ili9488c_sendCmd(ctx, 0x36, buf, sizeof(buf));
}
{
u8 buf[] = { 0x70 };
ili9488c_sendCmd(ctx, 0x3A, buf, sizeof(buf));
}
{
u8 buf[] = { 0x00 };
ili9488c_sendCmd(ctx, 0xB0, buf, sizeof(buf));
}
{
u8 buf[] = { 0xB0 };
ili9488c_sendCmd(ctx, 0xB1, buf, sizeof(buf));
}
{
u8 buf[] = { 0x02 };
ili9488c_sendCmd(ctx, 0xB4, buf, sizeof(buf));
}
{
u8 buf[] = { 0x02, 0x02, 0x3B };
ili9488c_sendCmd(ctx, 0xB6, buf, sizeof(buf));
}
{
u8 buf[] = { 0x00 };
ili9488c_sendCmd(ctx, 0xE9, buf, sizeof(buf));
}
{
u8 buf[] = { 0xA9, 0x51, 0x2c, 0x82 };
ili9488c_sendCmd(ctx, 0xF7, buf, sizeof(buf));
}
{
u8 buf[] = { 0x00, 0x00, 0x01, 0x3F };
ili9488c_sendCmd(ctx, 0x2A, buf, sizeof(buf));
}
{
u8 buf[] = { 0x00, 0x00, 0x00, 0xEF };
ili9488c_sendCmd(ctx, 0x2B, buf, sizeof(buf));
}
{
u8 buf[] = { 0x00 };
ili9488c_sendCmd(ctx, 0x11, buf, sizeof(buf));
msleep(120);
}
{
u8 buf[] = { 0x00 };
ili9488c_sendCmd(ctx, 0x29, buf, sizeof(buf));
msleep(25);
}
{
u8 buf[] = { 0x00 };
ili9488c_sendCmd(ctx, 0x2C, buf, sizeof(buf));
}
// TEARING NOT USE here
/*ret = mipi_dsi_dcs_set_tear_on(ctx->dsi, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
if (ret)
{
printk(KERN_ERR "ili9488c_prepare - mipi_dsi_dcs_set_tear_on failed\n");
return ret;
}*/
ret = mipi_dsi_dcs_exit_sleep_mode(ctx->dsi);
if (ret)
{
printk(KERN_ERR "ili9488c_prepare - mipi_dsi_dcs_exit_sleep_mode failed\n");
return ret;
}
printk(KERN_ERR "ili9488c_prepare - success\n");
return 0;
}
static int ili9488c_enable(struct drm_panel *panel)
{
printk(KERN_INFO "ili9488c_enable\n");
struct ili9488c *ctx = panel_to_ili9488c(panel);
msleep(120);
mipi_dsi_dcs_set_display_on(ctx->dsi);
// No backlight managnement
//backlight_enable(ctx->backlight);
return 0;
}
static int ili9488c_disable(struct drm_panel *panel)
{
printk(KERN_INFO "ili9488c_disable\n");
struct ili9488c *ctx = panel_to_ili9488c(panel);
// No backlight managnement
//backlight_disable(ctx->backlight);
return mipi_dsi_dcs_set_display_off(ctx->dsi);
}
static int ili9488c_unprepare(struct drm_panel *panel)
{
printk(KERN_INFO "ili9488c_unprepare\n");
struct ili9488c *ctx = panel_to_ili9488c(panel);
mipi_dsi_dcs_enter_sleep_mode(ctx->dsi);
regulator_disable(ctx->power);
gpiod_set_value(ctx->reset, GPIO_LOW);
return 0;
}
static const struct drm_display_mode driver_drm_default_mode = {
.clock = 6250, //In kHz. htotal (360) * vtotal (248) * frame_rate (70) / 1000; (source: https://github.com/freedreno/freedreno/wiki/DSI-Panel-Driver-Porting)
.vrefresh = 70, //Hz
/* From Linux source code documentation (https://elixir.bootlin.com/linux/v4.10.14/source/include/drm/drm_modes.h)
* The horizontal and vertical timings are defined per the following diagram.
*
* Active Front Sync Back
* Region Porch Porch
* <-----------------------><----------------><-------------><-------------->
* //////////////////////|
* ////////////////////// |
* ////////////////////// |.................. ................
* _______________
* <----- [hv]display ----->
* <------------- [hv]sync_start ------------>
* <--------------------- [hv]sync_end --------------------->
* <-------------------------------- [hv]total ----------------------------->*/
.hdisplay = 320,
.hsync_start = 320 + 10, /*HAdr + HFP*/
.hsync_end = 320 + 10 + 10, /*HAdr + HFP + Hsync*/
.htotal = 320 + 10 + 10 + 20, /*HAdr + HFP + Hsync + HBP*/
.vdisplay = 240,
.vsync_start = 240 + 4, /*VAdr + VFP*/
.vsync_end = 240 + 4 + 2, /*VAdr + VFP + Vsync*/
.vtotal = 240 + 4 + 2 + 2, /*VAdr + VFP + Vsync + VBP*/
.width_mm = WIDTH_MM,
.height_mm = HEIGHT_MM,
};
static int ili9488c_get_modes(struct drm_panel *panel)
{
printk(KERN_INFO "ili9488c_get_modes\n");
struct drm_connector *connector = panel->connector;
struct ili9488c *ctx = panel_to_ili9488c(panel);
struct drm_display_mode *mode;
mode = drm_mode_duplicate(panel->drm, &driver_drm_default_mode);
if (!mode)
{
dev_err(&ctx->dsi->dev, "failed to add mode %ux%ux@%u\n", driver_drm_default_mode.hdisplay, driver_drm_default_mode.vdisplay, driver_drm_default_mode.vrefresh);
return -ENOMEM;
}
drm_mode_set_name(mode);
mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
drm_mode_probed_add(connector, mode);
panel->connector->display_info.width_mm = WIDTH_MM;
panel->connector->display_info.height_mm = HEIGHT_MM;
return 1;
}
static const struct drm_panel_funcs ili9488c_funcs = {
.prepare = ili9488c_prepare,
.unprepare = ili9488c_unprepare,
.enable = ili9488c_enable,
.disable = ili9488c_disable,
.get_modes = ili9488c_get_modes,
};
static int ili9488c_dsi_probe(struct mipi_dsi_device *dsi)
{
printk(KERN_INFO "ili9488 driver loaded - v1.2.0.3\n");
int ret;
struct ili9488c* ctx = devm_kzalloc(&dsi->dev, sizeof(*ctx), GFP_KERNEL);
if (!ctx)
return -ENOMEM;
mipi_dsi_set_drvdata(dsi, ctx);
ctx->dsi = dsi;
drm_panel_init(&ctx->panel);
ctx->panel.dev = &dsi->dev;
ctx->panel.funcs = &ili9488c_funcs;
ctx->power = devm_regulator_get(&dsi->dev, "power");
if (IS_ERR(ctx->power))
{
printk(KERN_ERR "ili9488c_dsi_probe - Couldn't get our power regulator\n");
return PTR_ERR(ctx->power);
}
// reset GPIO
ctx->reset = devm_gpiod_get(&dsi->dev, "reset", GPIOD_OUT_HIGH);
if (IS_ERR(ctx->reset))
printk(KERN_ERR "ili9488c_dsi_probe - Couldn't get our reset GPIO\n");
else
printk(KERN_INFO "ili9488c_dsi_probe - Get reset GPIO - OK\n");
// Handle GPIO 32
struct gpio_desc* descGpio32 = devm_gpiod_get(&dsi->dev, "enable", GPIOD_OUT_HIGH);
if (IS_ERR(descGpio32))
{
printk(KERN_ERR "ili9488c_dsi_probe - Couldn't get our GPIO32 directly\n");
}
else
{
printk(KERN_INFO "ili9488c_dsi_probe - Get GPIO 32 - OK\n");
gpiod_set_value(descGpio32, GPIO_HIGH);
printk(KERN_INFO "ili9488c_dsi_probe - Set GPIO32 to high level\n");
}
// No backlight managnement
/*np = of_parse_phandle(dsi->dev.of_node, "backlight", 0);
if (np) {
ctx->backlight = of_find_backlight_by_node(np);
of_node_put(np);
if (!ctx->backlight)
return -EPROBE_DEFER;
}
*/
ret = drm_panel_add(&ctx->panel);
if (ret < 0)
{
printk(KERN_ERR "ili9488c_dsi_probe - drm_panel_add failed\n");
return ret;
}
dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_EOT_PACKET | MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
dsi->format = MIPI_DSI_FMT_RGB888;
dsi->lanes = 1;
printk(KERN_INFO "ili9488c_dsi_probe success\n");
return mipi_dsi_attach(dsi);
}
static int ili9488c_dsi_remove(struct mipi_dsi_device *dsi)
{
printk(KERN_INFO "ili9488c_dsi_remove\n");
struct ili9488c *ctx = mipi_dsi_get_drvdata(dsi);
mipi_dsi_detach(dsi);
drm_panel_remove(&ctx->panel);
// No backlight management
/*if (ctx->backlight)
put_device(&ctx->backlight->dev);*/
return 0;
}
static const struct of_device_id ili9488c_of_match[] = {
{ .compatible = "ilitek,ili9488" },
{ }
};
MODULE_DEVICE_TABLE(of, ili9488c_of_match);
static struct mipi_dsi_driver ili9488c_dsi_driver = {
.probe = ili9488c_dsi_probe,
.remove = ili9488c_dsi_remove,
.driver = {
.name = "ili9488",
.of_match_table = ili9488c_of_match,
},
};
module_mipi_dsi_driver(ili9488c_dsi_driver);
MODULE_AUTHOR("Elkami");
MODULE_DESCRIPTION("Ilitek ILI9488 Controller Driver");
MODULE_LICENSE("GPL v2");