1 #include <linux/export.h>
2 #include <linux/errno.h>
3 #include <linux/gpio.h>
4 #include <linux/spi/spi.h>
10 /*****************************************************************************
12 * void (*write_reg)(struct fbtft_par *par, int len, ...);
14 *****************************************************************************/
16 #define define_fbtft_write_reg(func, type, modifier) \
17 void func(struct fbtft_par *par, int len, ...) \
22 type *buf = (type *)par->buf; \
24 if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) { \
25 va_start(args, len); \
26 for (i = 0; i < len; i++) { \
27 buf[i] = (type)va_arg(args, unsigned int); \
30 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par, par->info->device, type, buf, len, "%s: ", __func__); \
33 va_start(args, len); \
35 if (par->startbyte) { \
36 *(u8 *)par->buf = par->startbyte; \
37 buf = (type *)(par->buf + 1); \
41 *buf = modifier((type)va_arg(args, unsigned int)); \
42 if (par->gpio.dc != -1) \
43 gpio_set_value(par->gpio.dc, 0); \
44 ret = par->fbtftops.write(par, par->buf, sizeof(type)+offset); \
47 dev_err(par->info->device, "%s: write() failed and returned %d\n", __func__, ret); \
53 *(u8 *)par->buf = par->startbyte | 0x2; \
58 *buf++ = modifier((type)va_arg(args, unsigned int)); \
60 if (par->gpio.dc != -1) \
61 gpio_set_value(par->gpio.dc, 1); \
62 ret = par->fbtftops.write(par, par->buf, len * (sizeof(type)+offset)); \
65 dev_err(par->info->device, "%s: write() failed and returned %d\n", __func__, ret); \
73 define_fbtft_write_reg(fbtft_write_reg8_bus8
, u8
, )
74 define_fbtft_write_reg(fbtft_write_reg16_bus8
, u16
, cpu_to_be16
)
75 define_fbtft_write_reg(fbtft_write_reg16_bus16
, u16
, )
77 void fbtft_write_reg8_bus9(struct fbtft_par
*par
, int len
, ...)
82 u16
*buf
= (u16
*)par
->buf
;
84 if (unlikely(par
->debug
& DEBUG_WRITE_REGISTER
)) {
86 for (i
= 0; i
< len
; i
++)
87 *(((u8
*)buf
) + i
) = (u8
)va_arg(args
, unsigned int);
89 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER
, par
,
90 par
->info
->device
, u8
, buf
, len
, "%s: ", __func__
);
95 if (par
->spi
&& (par
->spi
->bits_per_word
== 8)) {
96 /* we're emulating 9-bit, pad start of buffer with no-ops
97 (assuming here that zero is a no-op) */
98 pad
= (len
% 4) ? 4 - (len
% 4) : 0;
99 for (i
= 0; i
< pad
; i
++)
104 *buf
++ = (u8
)va_arg(args
, unsigned int);
107 *buf
= (u8
)va_arg(args
, unsigned int);
108 *buf
++ |= 0x100; /* dc=1 */
111 ret
= par
->fbtftops
.write(par
, par
->buf
, (len
+ pad
) * sizeof(u16
));
113 dev_err(par
->info
->device
,
114 "write() failed and returned %d\n", ret
);
118 EXPORT_SYMBOL(fbtft_write_reg8_bus9
);
123 /*****************************************************************************
125 * int (*write_vmem)(struct fbtft_par *par);
127 *****************************************************************************/
129 /* 16 bit pixel over 8-bit databus */
130 int fbtft_write_vmem16_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
)
133 u16
*txbuf16
= (u16
*)par
->txbuf
.buf
;
136 size_t tx_array_size
;
139 size_t startbyte_size
= 0;
141 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
142 __func__
, offset
, len
);
145 vmem16
= (u16
*)(par
->info
->screen_base
+ offset
);
147 if (par
->gpio
.dc
!= -1)
148 gpio_set_value(par
->gpio
.dc
, 1);
150 /* non buffered write */
152 return par
->fbtftops
.write(par
, vmem16
, len
);
155 tx_array_size
= par
->txbuf
.len
/ 2;
157 if (par
->startbyte
) {
158 txbuf16
= (u16
*)(par
->txbuf
.buf
+ 1);
160 *(u8
*)(par
->txbuf
.buf
) = par
->startbyte
| 0x2;
165 to_copy
= remain
> tx_array_size
? tx_array_size
: remain
;
166 dev_dbg(par
->info
->device
, " to_copy=%zu, remain=%zu\n",
167 to_copy
, remain
- to_copy
);
169 for (i
= 0; i
< to_copy
; i
++)
170 txbuf16
[i
] = cpu_to_be16(vmem16
[i
]);
172 vmem16
= vmem16
+ to_copy
;
173 ret
= par
->fbtftops
.write(par
, par
->txbuf
.buf
,
174 startbyte_size
+ to_copy
* 2);
182 EXPORT_SYMBOL(fbtft_write_vmem16_bus8
);
184 /* 16 bit pixel over 9-bit SPI bus: dc + high byte, dc + low byte */
185 int fbtft_write_vmem16_bus9(struct fbtft_par
*par
, size_t offset
, size_t len
)
188 u16
*txbuf16
= par
->txbuf
.buf
;
191 size_t tx_array_size
;
195 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
196 __func__
, offset
, len
);
198 if (!par
->txbuf
.buf
) {
199 dev_err(par
->info
->device
, "%s: txbuf.buf is NULL\n", __func__
);
204 vmem8
= par
->info
->screen_base
+ offset
;
206 tx_array_size
= par
->txbuf
.len
/ 2;
209 to_copy
= remain
> tx_array_size
? tx_array_size
: remain
;
210 dev_dbg(par
->info
->device
, " to_copy=%zu, remain=%zu\n",
211 to_copy
, remain
- to_copy
);
213 #ifdef __LITTLE_ENDIAN
214 for (i
= 0; i
< to_copy
; i
+= 2) {
215 txbuf16
[i
] = 0x0100 | vmem8
[i
+1];
216 txbuf16
[i
+1] = 0x0100 | vmem8
[i
];
219 for (i
= 0; i
< to_copy
; i
++)
220 txbuf16
[i
] = 0x0100 | vmem8
[i
];
222 vmem8
= vmem8
+ to_copy
;
223 ret
= par
->fbtftops
.write(par
, par
->txbuf
.buf
, to_copy
*2);
231 EXPORT_SYMBOL(fbtft_write_vmem16_bus9
);
233 int fbtft_write_vmem8_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
)
235 dev_err(par
->info
->device
, "%s: function not implemented\n", __func__
);
238 EXPORT_SYMBOL(fbtft_write_vmem8_bus8
);
240 /* 16 bit pixel over 16-bit databus */
241 int fbtft_write_vmem16_bus16(struct fbtft_par
*par
, size_t offset
, size_t len
)
245 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
246 __func__
, offset
, len
);
248 vmem16
= (u16
*)(par
->info
->screen_base
+ offset
);
250 if (par
->gpio
.dc
!= -1)
251 gpio_set_value(par
->gpio
.dc
, 1);
253 /* no need for buffered write with 16-bit bus */
254 return par
->fbtftops
.write(par
, vmem16
, len
);
256 EXPORT_SYMBOL(fbtft_write_vmem16_bus16
);