1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/export.h>
3 #include <linux/errno.h>
4 #include <linux/gpio/consumer.h>
5 #include <linux/spi/spi.h>
8 /*****************************************************************************
10 * void (*write_reg)(struct fbtft_par *par, int len, ...);
12 *****************************************************************************/
14 #define define_fbtft_write_reg(func, buffer_type, data_type, modifier) \
15 void func(struct fbtft_par *par, int len, ...) \
20 buffer_type *buf = (buffer_type *)par->buf; \
22 if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) { \
23 va_start(args, len); \
24 for (i = 0; i < len; i++) { \
25 buf[i] = modifier((data_type)va_arg(args, \
29 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par, \
30 par->info->device, buffer_type, buf, len, \
34 va_start(args, len); \
36 if (par->startbyte) { \
37 *(u8 *)par->buf = par->startbyte; \
38 buf = (buffer_type *)(par->buf + 1); \
42 *buf = modifier((data_type)va_arg(args, unsigned int)); \
43 ret = fbtft_write_buf_dc(par, par->buf, sizeof(data_type) + offset, \
50 *(u8 *)par->buf = par->startbyte | 0x2; \
55 *buf++ = modifier((data_type)va_arg(args, \
57 fbtft_write_buf_dc(par, par->buf, \
58 len * (sizeof(data_type) + offset), 1); \
65 define_fbtft_write_reg(fbtft_write_reg8_bus8
, u8
, u8
, )
66 define_fbtft_write_reg(fbtft_write_reg16_bus8
, __be16
, u16
, cpu_to_be16
)
67 define_fbtft_write_reg(fbtft_write_reg16_bus16
, u16
, u16
, )
69 void fbtft_write_reg8_bus9(struct fbtft_par
*par
, int len
, ...)
74 u16
*buf
= (u16
*)par
->buf
;
76 if (unlikely(par
->debug
& DEBUG_WRITE_REGISTER
)) {
78 for (i
= 0; i
< len
; i
++)
79 *(((u8
*)buf
) + i
) = (u8
)va_arg(args
, unsigned int);
81 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER
, par
,
82 par
->info
->device
, u8
, buf
, len
, "%s: ",
88 if (par
->spi
&& (par
->spi
->bits_per_word
== 8)) {
89 /* we're emulating 9-bit, pad start of buffer with no-ops
90 * (assuming here that zero is a no-op)
92 pad
= (len
% 4) ? 4 - (len
% 4) : 0;
93 for (i
= 0; i
< pad
; i
++)
98 *buf
++ = (u8
)va_arg(args
, unsigned int);
101 *buf
= (u8
)va_arg(args
, unsigned int);
102 *buf
++ |= 0x100; /* dc=1 */
105 ret
= par
->fbtftops
.write(par
, par
->buf
, (len
+ pad
) * sizeof(u16
));
107 dev_err(par
->info
->device
,
108 "write() failed and returned %d\n", ret
);
112 EXPORT_SYMBOL(fbtft_write_reg8_bus9
);
114 /*****************************************************************************
116 * int (*write_vmem)(struct fbtft_par *par);
118 *****************************************************************************/
120 /* 16 bit pixel over 8-bit databus */
121 int fbtft_write_vmem16_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
)
124 __be16
*txbuf16
= par
->txbuf
.buf
;
127 size_t tx_array_size
;
130 size_t startbyte_size
= 0;
133 vmem16
= (u16
*)(par
->info
->screen_buffer
+ offset
);
135 gpiod_set_value(par
->gpio
.dc
, 1);
137 /* non buffered write */
139 return par
->fbtftops
.write(par
, vmem16
, len
);
142 tx_array_size
= par
->txbuf
.len
/ 2;
144 if (par
->startbyte
) {
145 txbuf16
= par
->txbuf
.buf
+ 1;
147 *(u8
*)(par
->txbuf
.buf
) = par
->startbyte
| 0x2;
152 to_copy
= min(tx_array_size
, remain
);
153 dev_dbg(par
->info
->device
, "to_copy=%zu, remain=%zu\n",
154 to_copy
, remain
- to_copy
);
156 for (i
= 0; i
< to_copy
; i
++)
157 txbuf16
[i
] = cpu_to_be16(vmem16
[i
]);
159 vmem16
= vmem16
+ to_copy
;
160 ret
= par
->fbtftops
.write(par
, par
->txbuf
.buf
,
161 startbyte_size
+ to_copy
* 2);
169 EXPORT_SYMBOL(fbtft_write_vmem16_bus8
);
171 /* 16 bit pixel over 9-bit SPI bus: dc + high byte, dc + low byte */
172 int fbtft_write_vmem16_bus9(struct fbtft_par
*par
, size_t offset
, size_t len
)
175 u16
*txbuf16
= par
->txbuf
.buf
;
178 size_t tx_array_size
;
182 if (!par
->txbuf
.buf
) {
183 dev_err(par
->info
->device
, "%s: txbuf.buf is NULL\n", __func__
);
188 vmem8
= par
->info
->screen_buffer
+ offset
;
190 tx_array_size
= par
->txbuf
.len
/ 2;
193 to_copy
= min(tx_array_size
, remain
);
194 dev_dbg(par
->info
->device
, "to_copy=%zu, remain=%zu\n",
195 to_copy
, remain
- to_copy
);
197 #ifdef __LITTLE_ENDIAN
198 for (i
= 0; i
< to_copy
; i
+= 2) {
199 txbuf16
[i
] = 0x0100 | vmem8
[i
+ 1];
200 txbuf16
[i
+ 1] = 0x0100 | vmem8
[i
];
203 for (i
= 0; i
< to_copy
; i
++)
204 txbuf16
[i
] = 0x0100 | vmem8
[i
];
206 vmem8
= vmem8
+ to_copy
;
207 ret
= par
->fbtftops
.write(par
, par
->txbuf
.buf
, to_copy
* 2);
215 EXPORT_SYMBOL(fbtft_write_vmem16_bus9
);
217 int fbtft_write_vmem8_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
)
219 dev_err(par
->info
->device
, "%s: function not implemented\n", __func__
);
222 EXPORT_SYMBOL(fbtft_write_vmem8_bus8
);
224 /* 16 bit pixel over 16-bit databus */
225 int fbtft_write_vmem16_bus16(struct fbtft_par
*par
, size_t offset
, size_t len
)
229 vmem16
= (u16
*)(par
->info
->screen_buffer
+ offset
);
231 /* no need for buffered write with 16-bit bus */
232 return fbtft_write_buf_dc(par
, vmem16
, len
, 1);
234 EXPORT_SYMBOL(fbtft_write_vmem16_bus16
);