1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/export.h>
3 #include <linux/errno.h>
4 #include <linux/gpio.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, type, modifier) \
15 void func(struct fbtft_par *par, int len, ...) \
20 type *buf = (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] = (type)va_arg(args, unsigned int); \
28 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par, par->info->device, type, buf, len, "%s: ", __func__); \
31 va_start(args, len); \
33 if (par->startbyte) { \
34 *(u8 *)par->buf = par->startbyte; \
35 buf = (type *)(par->buf + 1); \
39 *buf = modifier((type)va_arg(args, unsigned int)); \
40 ret = fbtft_write_buf_dc(par, par->buf, sizeof(type) + offset, 0); \
46 *(u8 *)par->buf = par->startbyte | 0x2; \
51 *buf++ = modifier((type)va_arg(args, unsigned int)); \
52 fbtft_write_buf_dc(par, par->buf, \
53 len * (sizeof(type) + offset), 1); \
60 define_fbtft_write_reg(fbtft_write_reg8_bus8
, u8
, )
61 define_fbtft_write_reg(fbtft_write_reg16_bus8
, u16
, cpu_to_be16
)
62 define_fbtft_write_reg(fbtft_write_reg16_bus16
, u16
, )
64 void fbtft_write_reg8_bus9(struct fbtft_par
*par
, int len
, ...)
69 u16
*buf
= (u16
*)par
->buf
;
71 if (unlikely(par
->debug
& DEBUG_WRITE_REGISTER
)) {
73 for (i
= 0; i
< len
; i
++)
74 *(((u8
*)buf
) + i
) = (u8
)va_arg(args
, unsigned int);
76 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER
, par
,
77 par
->info
->device
, u8
, buf
, len
, "%s: ", __func__
);
82 if (par
->spi
&& (par
->spi
->bits_per_word
== 8)) {
83 /* we're emulating 9-bit, pad start of buffer with no-ops
84 * (assuming here that zero is a no-op)
86 pad
= (len
% 4) ? 4 - (len
% 4) : 0;
87 for (i
= 0; i
< pad
; i
++)
92 *buf
++ = (u8
)va_arg(args
, unsigned int);
95 *buf
= (u8
)va_arg(args
, unsigned int);
96 *buf
++ |= 0x100; /* dc=1 */
99 ret
= par
->fbtftops
.write(par
, par
->buf
, (len
+ pad
) * sizeof(u16
));
101 dev_err(par
->info
->device
,
102 "write() failed and returned %d\n", ret
);
106 EXPORT_SYMBOL(fbtft_write_reg8_bus9
);
108 /*****************************************************************************
110 * int (*write_vmem)(struct fbtft_par *par);
112 *****************************************************************************/
114 /* 16 bit pixel over 8-bit databus */
115 int fbtft_write_vmem16_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
)
118 __be16
*txbuf16
= par
->txbuf
.buf
;
121 size_t tx_array_size
;
124 size_t startbyte_size
= 0;
126 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
127 __func__
, offset
, len
);
130 vmem16
= (u16
*)(par
->info
->screen_buffer
+ offset
);
132 if (par
->gpio
.dc
!= -1)
133 gpio_set_value(par
->gpio
.dc
, 1);
135 /* non buffered write */
137 return par
->fbtftops
.write(par
, vmem16
, len
);
140 tx_array_size
= par
->txbuf
.len
/ 2;
142 if (par
->startbyte
) {
143 txbuf16
= par
->txbuf
.buf
+ 1;
145 *(u8
*)(par
->txbuf
.buf
) = par
->startbyte
| 0x2;
150 to_copy
= min(tx_array_size
, remain
);
151 dev_dbg(par
->info
->device
, " to_copy=%zu, remain=%zu\n",
152 to_copy
, remain
- to_copy
);
154 for (i
= 0; i
< to_copy
; i
++)
155 txbuf16
[i
] = cpu_to_be16(vmem16
[i
]);
157 vmem16
= vmem16
+ to_copy
;
158 ret
= par
->fbtftops
.write(par
, par
->txbuf
.buf
,
159 startbyte_size
+ to_copy
* 2);
167 EXPORT_SYMBOL(fbtft_write_vmem16_bus8
);
169 /* 16 bit pixel over 9-bit SPI bus: dc + high byte, dc + low byte */
170 int fbtft_write_vmem16_bus9(struct fbtft_par
*par
, size_t offset
, size_t len
)
173 u16
*txbuf16
= par
->txbuf
.buf
;
176 size_t tx_array_size
;
180 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
181 __func__
, offset
, len
);
183 if (!par
->txbuf
.buf
) {
184 dev_err(par
->info
->device
, "%s: txbuf.buf is NULL\n", __func__
);
189 vmem8
= par
->info
->screen_buffer
+ offset
;
191 tx_array_size
= par
->txbuf
.len
/ 2;
194 to_copy
= min(tx_array_size
, remain
);
195 dev_dbg(par
->info
->device
, " to_copy=%zu, remain=%zu\n",
196 to_copy
, remain
- to_copy
);
198 #ifdef __LITTLE_ENDIAN
199 for (i
= 0; i
< to_copy
; i
+= 2) {
200 txbuf16
[i
] = 0x0100 | vmem8
[i
+ 1];
201 txbuf16
[i
+ 1] = 0x0100 | vmem8
[i
];
204 for (i
= 0; i
< to_copy
; i
++)
205 txbuf16
[i
] = 0x0100 | vmem8
[i
];
207 vmem8
= vmem8
+ to_copy
;
208 ret
= par
->fbtftops
.write(par
, par
->txbuf
.buf
, to_copy
* 2);
216 EXPORT_SYMBOL(fbtft_write_vmem16_bus9
);
218 int fbtft_write_vmem8_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
)
220 dev_err(par
->info
->device
, "%s: function not implemented\n", __func__
);
223 EXPORT_SYMBOL(fbtft_write_vmem8_bus8
);
225 /* 16 bit pixel over 16-bit databus */
226 int fbtft_write_vmem16_bus16(struct fbtft_par
*par
, size_t offset
, size_t len
)
230 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
231 __func__
, offset
, len
);
233 vmem16
= (u16
*)(par
->info
->screen_buffer
+ offset
);
235 /* no need for buffered write with 16-bit bus */
236 return fbtft_write_buf_dc(par
, vmem16
, len
, 1);
238 EXPORT_SYMBOL(fbtft_write_vmem16_bus16
);