1 #include <linux/export.h>
2 #include <linux/errno.h>
3 #include <linux/gpio.h>
4 #include <linux/spi/spi.h>
7 /*****************************************************************************
9 * void (*write_reg)(struct fbtft_par *par, int len, ...);
11 *****************************************************************************/
13 #define define_fbtft_write_reg(func, type, modifier) \
14 void func(struct fbtft_par *par, int len, ...) \
19 type *buf = (type *)par->buf; \
21 if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) { \
22 va_start(args, len); \
23 for (i = 0; i < len; i++) { \
24 buf[i] = (type)va_arg(args, unsigned int); \
27 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par, par->info->device, type, buf, len, "%s: ", __func__); \
30 va_start(args, len); \
32 if (par->startbyte) { \
33 *(u8 *)par->buf = par->startbyte; \
34 buf = (type *)(par->buf + 1); \
38 *buf = modifier((type)va_arg(args, unsigned int)); \
39 if (par->gpio.dc != -1) \
40 gpio_set_value(par->gpio.dc, 0); \
41 ret = par->fbtftops.write(par, par->buf, sizeof(type) + offset); \
44 dev_err(par->info->device, "%s: write() failed and returned %d\n", __func__, ret); \
50 *(u8 *)par->buf = par->startbyte | 0x2; \
55 *buf++ = modifier((type)va_arg(args, unsigned int)); \
57 if (par->gpio.dc != -1) \
58 gpio_set_value(par->gpio.dc, 1); \
59 ret = par->fbtftops.write(par, par->buf, \
60 len * (sizeof(type) + offset)); \
63 dev_err(par->info->device, "%s: write() failed and returned %d\n", __func__, ret); \
71 define_fbtft_write_reg(fbtft_write_reg8_bus8
, u8
, )
72 define_fbtft_write_reg(fbtft_write_reg16_bus8
, u16
, cpu_to_be16
)
73 define_fbtft_write_reg(fbtft_write_reg16_bus16
, u16
, )
75 void fbtft_write_reg8_bus9(struct fbtft_par
*par
, int len
, ...)
80 u16
*buf
= (u16
*)par
->buf
;
82 if (unlikely(par
->debug
& DEBUG_WRITE_REGISTER
)) {
84 for (i
= 0; i
< len
; i
++)
85 *(((u8
*)buf
) + i
) = (u8
)va_arg(args
, unsigned int);
87 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER
, par
,
88 par
->info
->device
, u8
, buf
, len
, "%s: ", __func__
);
93 if (par
->spi
&& (par
->spi
->bits_per_word
== 8)) {
94 /* we're emulating 9-bit, pad start of buffer with no-ops
95 (assuming here that zero is a no-op) */
96 pad
= (len
% 4) ? 4 - (len
% 4) : 0;
97 for (i
= 0; i
< pad
; i
++)
102 *buf
++ = (u8
)va_arg(args
, unsigned int);
105 *buf
= (u8
)va_arg(args
, unsigned int);
106 *buf
++ |= 0x100; /* dc=1 */
109 ret
= par
->fbtftops
.write(par
, par
->buf
, (len
+ pad
) * sizeof(u16
));
111 dev_err(par
->info
->device
,
112 "write() failed and returned %d\n", ret
);
116 EXPORT_SYMBOL(fbtft_write_reg8_bus9
);
118 /*****************************************************************************
120 * int (*write_vmem)(struct fbtft_par *par);
122 *****************************************************************************/
124 /* 16 bit pixel over 8-bit databus */
125 int fbtft_write_vmem16_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
)
128 u16
*txbuf16
= (u16
*)par
->txbuf
.buf
;
131 size_t tx_array_size
;
134 size_t startbyte_size
= 0;
136 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
137 __func__
, offset
, len
);
140 vmem16
= (u16
*)(par
->info
->screen_buffer
+ offset
);
142 if (par
->gpio
.dc
!= -1)
143 gpio_set_value(par
->gpio
.dc
, 1);
145 /* non buffered write */
147 return par
->fbtftops
.write(par
, vmem16
, len
);
150 tx_array_size
= par
->txbuf
.len
/ 2;
152 if (par
->startbyte
) {
153 txbuf16
= (u16
*)(par
->txbuf
.buf
+ 1);
155 *(u8
*)(par
->txbuf
.buf
) = par
->startbyte
| 0x2;
160 to_copy
= remain
> tx_array_size
? tx_array_size
: remain
;
161 dev_dbg(par
->info
->device
, " to_copy=%zu, remain=%zu\n",
162 to_copy
, remain
- to_copy
);
164 for (i
= 0; i
< to_copy
; i
++)
165 txbuf16
[i
] = cpu_to_be16(vmem16
[i
]);
167 vmem16
= vmem16
+ to_copy
;
168 ret
= par
->fbtftops
.write(par
, par
->txbuf
.buf
,
169 startbyte_size
+ to_copy
* 2);
177 EXPORT_SYMBOL(fbtft_write_vmem16_bus8
);
179 /* 16 bit pixel over 9-bit SPI bus: dc + high byte, dc + low byte */
180 int fbtft_write_vmem16_bus9(struct fbtft_par
*par
, size_t offset
, size_t len
)
183 u16
*txbuf16
= par
->txbuf
.buf
;
186 size_t tx_array_size
;
190 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
191 __func__
, offset
, len
);
193 if (!par
->txbuf
.buf
) {
194 dev_err(par
->info
->device
, "%s: txbuf.buf is NULL\n", __func__
);
199 vmem8
= par
->info
->screen_buffer
+ offset
;
201 tx_array_size
= par
->txbuf
.len
/ 2;
204 to_copy
= remain
> tx_array_size
? tx_array_size
: remain
;
205 dev_dbg(par
->info
->device
, " to_copy=%zu, remain=%zu\n",
206 to_copy
, remain
- to_copy
);
208 #ifdef __LITTLE_ENDIAN
209 for (i
= 0; i
< to_copy
; i
+= 2) {
210 txbuf16
[i
] = 0x0100 | vmem8
[i
+ 1];
211 txbuf16
[i
+ 1] = 0x0100 | vmem8
[i
];
214 for (i
= 0; i
< to_copy
; i
++)
215 txbuf16
[i
] = 0x0100 | vmem8
[i
];
217 vmem8
= vmem8
+ to_copy
;
218 ret
= par
->fbtftops
.write(par
, par
->txbuf
.buf
, to_copy
* 2);
226 EXPORT_SYMBOL(fbtft_write_vmem16_bus9
);
228 int fbtft_write_vmem8_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
)
230 dev_err(par
->info
->device
, "%s: function not implemented\n", __func__
);
233 EXPORT_SYMBOL(fbtft_write_vmem8_bus8
);
235 /* 16 bit pixel over 16-bit databus */
236 int fbtft_write_vmem16_bus16(struct fbtft_par
*par
, size_t offset
, size_t len
)
240 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
241 __func__
, offset
, len
);
243 vmem16
= (u16
*)(par
->info
->screen_buffer
+ offset
);
245 if (par
->gpio
.dc
!= -1)
246 gpio_set_value(par
->gpio
.dc
, 1);
248 /* no need for buffered write with 16-bit bus */
249 return par
->fbtftops
.write(par
, vmem16
, len
);
251 EXPORT_SYMBOL(fbtft_write_vmem16_bus16
);