-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
mipi_dbi_spi.c
574 lines (489 loc) · 14.3 KB
/
mipi_dbi_spi.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
* Copyright 2023 NXP
* Copyright 2024 TiaC Systems
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT zephyr_mipi_dbi_spi
#include <zephyr/drivers/mipi_dbi.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(mipi_dbi_spi, CONFIG_MIPI_DBI_LOG_LEVEL);
struct mipi_dbi_spi_config {
/* SPI hardware used to send data */
const struct device *spi_dev;
/* Command/Data gpio */
const struct gpio_dt_spec cmd_data;
/* Reset GPIO */
const struct gpio_dt_spec reset;
/* Minimum transfer bits */
const uint8_t xfr_min_bits;
};
struct mipi_dbi_spi_data {
struct k_mutex lock;
/* Used for 3 wire mode */
uint16_t spi_byte;
};
/* Expands to 1 if the node does not have the `write-only` property */
#define _WRITE_ONLY_ABSENT(n) (!DT_INST_PROP(n, write_only)) |
/* This macro will evaluate to 1 if any of the nodes with zephyr,mipi-dbi-spi
* lack a `write-only` property. The intention here is to allow the entire
* command_read function to be optimized out when it is not needed.
*/
#define MIPI_DBI_SPI_READ_REQUIRED DT_INST_FOREACH_STATUS_OKAY(_WRITE_ONLY_ABSENT) 0
uint32_t var = MIPI_DBI_SPI_READ_REQUIRED;
/* Expands to 1 if the node does reflect the enum in `xfr-min-bits` property */
#define _XFR_8BITS(n) (DT_INST_PROP(n, xfr_min_bits) == MIPI_DBI_SPI_XFR_8BIT) |
#define _XFR_16BITS(n) (DT_INST_PROP(n, xfr_min_bits) == MIPI_DBI_SPI_XFR_16BIT) |
/* This macros will evaluate to 1 if any of the nodes with zephyr,mipi-dbi-spi
* have the `xfr-min-bits` property to corresponding enum value. The intention
* here is to allow the write helper functions to be optimized out when not all
* minimum transfer bits will be needed.
*/
#define MIPI_DBI_SPI_WRITE_8BIT_REQUIRED DT_INST_FOREACH_STATUS_OKAY(_XFR_8BITS) 0
#define MIPI_DBI_SPI_WRITE_16BIT_REQUIRED DT_INST_FOREACH_STATUS_OKAY(_XFR_16BITS) 0
/* In Type C mode 1 MIPI BIT communication, the 9th bit of the word
* (first bit sent in each word) indicates if the word is a command or
* data. Typically 0 indicates a command and 1 indicates data, but some
* displays may vary.
* Index starts from 0 so that BIT(8) means 9th bit.
*/
#define MIPI_DBI_DC_BIT BIT(8)
static inline int
mipi_dbi_spi_write_helper_3wire(const struct device *dev,
const struct mipi_dbi_config *dbi_config,
bool cmd_present, uint8_t cmd,
const uint8_t *data_buf, size_t len)
{
const struct mipi_dbi_spi_config *config = dev->config;
struct mipi_dbi_spi_data *data = dev->data;
struct spi_buf buffer;
struct spi_buf_set buf_set = {
.buffers = &buffer,
.count = 1,
};
int ret = 0;
/*
* 9 bit word mode must be used, as the command/data bit
* is stored before the data word.
*/
if ((dbi_config->config.operation & SPI_WORD_SIZE_MASK)
!= SPI_WORD_SET(9)) {
return -ENOTSUP;
}
buffer.buf = &data->spi_byte;
buffer.len = 2;
/* Send command */
if (cmd_present) {
data->spi_byte = cmd;
ret = spi_write(config->spi_dev, &dbi_config->config, &buf_set);
if (ret < 0) {
goto out;
}
}
/* Write data, byte by byte */
for (size_t i = 0; i < len; i++) {
data->spi_byte = MIPI_DBI_DC_BIT | data_buf[i];
ret = spi_write(config->spi_dev, &dbi_config->config, &buf_set);
if (ret < 0) {
goto out;
}
}
out:
return ret;
}
#if MIPI_DBI_SPI_WRITE_8BIT_REQUIRED
static inline int
mipi_dbi_spi_write_helper_4wire_8bit(const struct device *dev,
const struct mipi_dbi_config *dbi_config,
bool cmd_present, uint8_t cmd,
const uint8_t *data_buf, size_t len)
{
const struct mipi_dbi_spi_config *config = dev->config;
struct spi_buf buffer;
struct spi_buf_set buf_set = {
.buffers = &buffer,
.count = 1,
};
int ret = 0;
/*
* 4 wire mode is much simpler. We just toggle the
* command/data GPIO to indicate if we are sending
* a command or data
*/
buffer.buf = &cmd;
buffer.len = sizeof(cmd);
if (cmd_present) {
/* Set CD pin low for command */
gpio_pin_set_dt(&config->cmd_data, 0);
ret = spi_write(config->spi_dev, &dbi_config->config, &buf_set);
if (ret < 0) {
goto out;
}
}
if (len > 0) {
buffer.buf = (void *)data_buf;
buffer.len = len;
/* Set CD pin high for data */
gpio_pin_set_dt(&config->cmd_data, 1);
ret = spi_write(config->spi_dev, &dbi_config->config, &buf_set);
if (ret < 0) {
goto out;
}
}
out:
return ret;
}
#endif /* MIPI_DBI_SPI_WRITE_8BIT_REQUIRED */
#if MIPI_DBI_SPI_WRITE_16BIT_REQUIRED
static inline int
mipi_dbi_spi_write_helper_4wire_16bit(const struct device *dev,
const struct mipi_dbi_config *dbi_config,
bool cmd_present, uint8_t cmd,
const uint8_t *data_buf, size_t len)
{
const struct mipi_dbi_spi_config *config = dev->config;
struct spi_buf buffer;
struct spi_buf_set buf_set = {
.buffers = &buffer,
.count = 1,
};
uint16_t data16;
int ret = 0;
/*
* 4 wire mode with toggle the command/data GPIO
* to indicate if we are sending a command or data
* but send 16-bit blocks (with bit stuffing).
*/
if (cmd_present) {
data16 = sys_cpu_to_be16(cmd);
buffer.buf = &data16;
buffer.len = sizeof(data16);
/* Set CD pin low for command */
gpio_pin_set_dt(&config->cmd_data, 0);
ret = spi_write(config->spi_dev, &dbi_config->config,
&buf_set);
if (ret < 0) {
goto out;
}
/* Set CD pin high for data, if there are any */
if (len > 0) {
gpio_pin_set_dt(&config->cmd_data, 1);
}
/* iterate command data */
for (int i = 0; i < len; i++) {
data16 = sys_cpu_to_be16(data_buf[i]);
ret = spi_write(config->spi_dev, &dbi_config->config,
&buf_set);
if (ret < 0) {
goto out;
}
}
} else {
int stuffing = len % sizeof(data16);
/* Set CD pin high for data, if there are any */
if (len > 0) {
gpio_pin_set_dt(&config->cmd_data, 1);
}
/* pass through generic device data */
if (len - stuffing > 0) {
buffer.buf = (void *)data_buf;
buffer.len = len - stuffing;
ret = spi_write(config->spi_dev, &dbi_config->config,
&buf_set);
if (ret < 0) {
goto out;
}
}
/* iterate remaining data with stuffing */
for (int i = len - stuffing; i < len; i++) {
data16 = sys_cpu_to_be16(data_buf[i]);
buffer.buf = &data16;
buffer.len = sizeof(data16);
ret = spi_write(config->spi_dev, &dbi_config->config,
&buf_set);
if (ret < 0) {
goto out;
}
}
}
out:
return ret;
}
#endif /* MIPI_DBI_SPI_WRITE_16BIT_REQUIRED */
static int mipi_dbi_spi_write_helper(const struct device *dev,
const struct mipi_dbi_config *dbi_config,
bool cmd_present, uint8_t cmd,
const uint8_t *data_buf, size_t len)
{
const struct mipi_dbi_spi_config *config = dev->config;
struct mipi_dbi_spi_data *data = dev->data;
int ret = 0;
ret = k_mutex_lock(&data->lock, K_FOREVER);
if (ret < 0) {
return ret;
}
if (dbi_config->mode == MIPI_DBI_MODE_SPI_3WIRE &&
IS_ENABLED(CONFIG_MIPI_DBI_SPI_3WIRE)) {
ret = mipi_dbi_spi_write_helper_3wire(dev, dbi_config,
cmd_present, cmd,
data_buf, len);
goto out;
}
if (dbi_config->mode == MIPI_DBI_MODE_SPI_4WIRE) {
#if MIPI_DBI_SPI_WRITE_8BIT_REQUIRED
if (config->xfr_min_bits == MIPI_DBI_SPI_XFR_8BIT) {
ret = mipi_dbi_spi_write_helper_4wire_8bit(
dev, dbi_config,
cmd_present, cmd,
data_buf, len);
goto out;
}
#endif
#if MIPI_DBI_SPI_WRITE_16BIT_REQUIRED
if (config->xfr_min_bits == MIPI_DBI_SPI_XFR_16BIT) {
ret = mipi_dbi_spi_write_helper_4wire_16bit(
dev, dbi_config,
cmd_present, cmd,
data_buf, len);
goto out;
}
#endif
}
/* Otherwise, unsupported mode */
ret = -ENOTSUP;
out:
k_mutex_unlock(&data->lock);
return ret;
}
static int mipi_dbi_spi_command_write(const struct device *dev,
const struct mipi_dbi_config *dbi_config,
uint8_t cmd, const uint8_t *data_buf,
size_t len)
{
return mipi_dbi_spi_write_helper(dev, dbi_config, true, cmd,
data_buf, len);
}
static int mipi_dbi_spi_write_display(const struct device *dev,
const struct mipi_dbi_config *dbi_config,
const uint8_t *framebuf,
struct display_buffer_descriptor *desc,
enum display_pixel_format pixfmt)
{
ARG_UNUSED(pixfmt);
return mipi_dbi_spi_write_helper(dev, dbi_config, false, 0x0,
framebuf, desc->buf_size);
}
#if MIPI_DBI_SPI_READ_REQUIRED
static inline int
mipi_dbi_spi_read_helper_3wire(const struct device *dev,
const struct mipi_dbi_config *dbi_config,
uint8_t *cmds, size_t num_cmds,
uint8_t *response, size_t len)
{
const struct mipi_dbi_spi_config *config = dev->config;
struct mipi_dbi_spi_data *data = dev->data;
struct spi_config tmp_config;
struct spi_buf buffer;
struct spi_buf_set buf_set = {
.buffers = &buffer,
.count = 1,
};
int ret = 0;
/*
* We have to emulate 3 wire mode by packing the data/command
* bit into the upper bit of the SPI transfer, switch SPI to
* 9 bit mode, and write the transfer.
*/
if ((dbi_config->config.operation & SPI_WORD_SIZE_MASK)
!= SPI_WORD_SET(9)) {
return -ENOTSUP;
}
memcpy(&tmp_config, &dbi_config->config, sizeof(tmp_config));
tmp_config.operation &= ~SPI_WORD_SIZE_MASK;
tmp_config.operation |= SPI_WORD_SET(9);
buffer.buf = &data->spi_byte;
buffer.len = 1;
/* Send each command */
for (size_t i = 0; i < num_cmds; i++) {
data->spi_byte = cmds[i];
ret = spi_write(config->spi_dev, &tmp_config, &buf_set);
if (ret < 0) {
goto out;
}
}
/* Now, we can switch to 8 bit mode, and read data */
buffer.buf = (void *)response;
buffer.len = len;
ret = spi_read(config->spi_dev, &dbi_config->config, &buf_set);
out:
spi_release(config->spi_dev, &tmp_config); /* Really necessary here? */
return ret;
}
static inline int
mipi_dbi_spi_read_helper_4wire(const struct device *dev,
const struct mipi_dbi_config *dbi_config,
uint8_t *cmds, size_t num_cmds,
uint8_t *response, size_t len)
{
const struct mipi_dbi_spi_config *config = dev->config;
struct spi_config tmp_config;
struct spi_buf buffer;
struct spi_buf_set buf_set = {
.buffers = &buffer,
.count = 1,
};
int ret = 0;
/*
* 4 wire mode is much simpler. We just toggle the
* command/data GPIO to indicate if we are sending
* a command or data. Note that since some SPI displays
* require CS to be held low for the entire read sequence,
* we set SPI_HOLD_ON_CS
*/
memcpy(&tmp_config, &dbi_config->config, sizeof(tmp_config));
tmp_config.operation |= SPI_HOLD_ON_CS;
if (num_cmds > 0) {
buffer.buf = cmds;
buffer.len = num_cmds;
/* Set CD pin low for command */
gpio_pin_set_dt(&config->cmd_data, 0);
ret = spi_write(config->spi_dev, &tmp_config, &buf_set);
if (ret < 0) {
goto out;
}
}
if (len > 0) {
buffer.buf = (void *)response;
buffer.len = len;
/* Set CD pin high for data */
gpio_pin_set_dt(&config->cmd_data, 1);
ret = spi_read(config->spi_dev, &tmp_config, &buf_set);
if (ret < 0) {
goto out;
}
}
out:
spi_release(config->spi_dev, &tmp_config);
return ret;
}
static int mipi_dbi_spi_command_read(const struct device *dev,
const struct mipi_dbi_config *dbi_config,
uint8_t *cmds, size_t num_cmds,
uint8_t *response, size_t len)
{
struct mipi_dbi_spi_data *data = dev->data;
int ret = 0;
ret = k_mutex_lock(&data->lock, K_FOREVER);
if (ret < 0) {
return ret;
}
if (dbi_config->mode == MIPI_DBI_MODE_SPI_3WIRE &&
IS_ENABLED(CONFIG_MIPI_DBI_SPI_3WIRE)) {
ret = mipi_dbi_spi_read_helper_3wire(dev, dbi_config,
cmds, num_cmds,
response, len);
if (ret < 0) {
goto out;
}
} else if (dbi_config->mode == MIPI_DBI_MODE_SPI_4WIRE) {
ret = mipi_dbi_spi_read_helper_4wire(dev, dbi_config,
cmds, num_cmds,
response, len);
if (ret < 0) {
goto out;
}
} else {
/* Otherwise, unsupported mode */
ret = -ENOTSUP;
}
out:
k_mutex_unlock(&data->lock);
return ret;
}
#endif /* MIPI_DBI_SPI_READ_REQUIRED */
static inline bool mipi_dbi_has_pin(const struct gpio_dt_spec *spec)
{
return spec->port != NULL;
}
static int mipi_dbi_spi_reset(const struct device *dev, k_timeout_t delay)
{
const struct mipi_dbi_spi_config *config = dev->config;
int ret;
if (!mipi_dbi_has_pin(&config->reset)) {
return -ENOTSUP;
}
ret = gpio_pin_set_dt(&config->reset, 1);
if (ret < 0) {
return ret;
}
k_sleep(delay);
return gpio_pin_set_dt(&config->reset, 0);
}
static int mipi_dbi_spi_release(const struct device *dev,
const struct mipi_dbi_config *dbi_config)
{
const struct mipi_dbi_spi_config *config = dev->config;
return spi_release(config->spi_dev, &dbi_config->config);
}
static int mipi_dbi_spi_init(const struct device *dev)
{
const struct mipi_dbi_spi_config *config = dev->config;
struct mipi_dbi_spi_data *data = dev->data;
int ret;
if (!device_is_ready(config->spi_dev)) {
LOG_ERR("SPI device is not ready");
return -ENODEV;
}
if (mipi_dbi_has_pin(&config->cmd_data)) {
if (!gpio_is_ready_dt(&config->cmd_data)) {
return -ENODEV;
}
ret = gpio_pin_configure_dt(&config->cmd_data, GPIO_OUTPUT);
if (ret < 0) {
LOG_ERR("Could not configure command/data GPIO (%d)", ret);
return ret;
}
}
if (mipi_dbi_has_pin(&config->reset)) {
if (!gpio_is_ready_dt(&config->reset)) {
return -ENODEV;
}
ret = gpio_pin_configure_dt(&config->reset, GPIO_OUTPUT_INACTIVE);
if (ret < 0) {
LOG_ERR("Could not configure reset GPIO (%d)", ret);
return ret;
}
}
k_mutex_init(&data->lock);
return 0;
}
static DEVICE_API(mipi_dbi, mipi_dbi_spi_driver_api) = {
.reset = mipi_dbi_spi_reset,
.command_write = mipi_dbi_spi_command_write,
.write_display = mipi_dbi_spi_write_display,
.release = mipi_dbi_spi_release,
#if MIPI_DBI_SPI_READ_REQUIRED
.command_read = mipi_dbi_spi_command_read,
#endif
};
#define MIPI_DBI_SPI_INIT(n) \
static const struct mipi_dbi_spi_config \
mipi_dbi_spi_config_##n = { \
.spi_dev = DEVICE_DT_GET( \
DT_INST_PHANDLE(n, spi_dev)), \
.cmd_data = GPIO_DT_SPEC_INST_GET_OR(n, dc_gpios, {}), \
.reset = GPIO_DT_SPEC_INST_GET_OR(n, reset_gpios, {}), \
.xfr_min_bits = DT_INST_PROP(n, xfr_min_bits) \
}; \
static struct mipi_dbi_spi_data mipi_dbi_spi_data_##n; \
\
DEVICE_DT_INST_DEFINE(n, mipi_dbi_spi_init, NULL, \
&mipi_dbi_spi_data_##n, \
&mipi_dbi_spi_config_##n, \
POST_KERNEL, \
CONFIG_MIPI_DBI_INIT_PRIORITY, \
&mipi_dbi_spi_driver_api);
DT_INST_FOREACH_STATUS_OKAY(MIPI_DBI_SPI_INIT)