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)
97 pad
= (len
% 4) ? 4 - (len
% 4) : 0;
98 for (i
= 0; i
< pad
; i
++)
103 *buf
++ = (u8
)va_arg(args
, unsigned int);
106 *buf
= (u8
)va_arg(args
, unsigned int);
107 *buf
++ |= 0x100; /* dc=1 */
110 ret
= par
->fbtftops
.write(par
, par
->buf
, (len
+ pad
) * sizeof(u16
));
112 dev_err(par
->info
->device
,
113 "write() failed and returned %d\n", ret
);
117 EXPORT_SYMBOL(fbtft_write_reg8_bus9
);
119 /*****************************************************************************
121 * int (*write_vmem)(struct fbtft_par *par);
123 *****************************************************************************/
125 /* 16 bit pixel over 8-bit databus */
126 int fbtft_write_vmem16_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
)
129 u16
*txbuf16
= par
->txbuf
.buf
;
132 size_t tx_array_size
;
135 size_t startbyte_size
= 0;
137 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
138 __func__
, offset
, len
);
141 vmem16
= (u16
*)(par
->info
->screen_buffer
+ offset
);
143 if (par
->gpio
.dc
!= -1)
144 gpio_set_value(par
->gpio
.dc
, 1);
146 /* non buffered write */
148 return par
->fbtftops
.write(par
, vmem16
, len
);
151 tx_array_size
= par
->txbuf
.len
/ 2;
153 if (par
->startbyte
) {
154 txbuf16
= par
->txbuf
.buf
+ 1;
156 *(u8
*)(par
->txbuf
.buf
) = par
->startbyte
| 0x2;
161 to_copy
= min(tx_array_size
, remain
);
162 dev_dbg(par
->info
->device
, " to_copy=%zu, remain=%zu\n",
163 to_copy
, remain
- to_copy
);
165 for (i
= 0; i
< to_copy
; i
++)
166 txbuf16
[i
] = cpu_to_be16(vmem16
[i
]);
168 vmem16
= vmem16
+ to_copy
;
169 ret
= par
->fbtftops
.write(par
, par
->txbuf
.buf
,
170 startbyte_size
+ to_copy
* 2);
178 EXPORT_SYMBOL(fbtft_write_vmem16_bus8
);
180 /* 16 bit pixel over 9-bit SPI bus: dc + high byte, dc + low byte */
181 int fbtft_write_vmem16_bus9(struct fbtft_par
*par
, size_t offset
, size_t len
)
184 u16
*txbuf16
= par
->txbuf
.buf
;
187 size_t tx_array_size
;
191 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
192 __func__
, offset
, len
);
194 if (!par
->txbuf
.buf
) {
195 dev_err(par
->info
->device
, "%s: txbuf.buf is NULL\n", __func__
);
200 vmem8
= par
->info
->screen_buffer
+ offset
;
202 tx_array_size
= par
->txbuf
.len
/ 2;
205 to_copy
= min(tx_array_size
, remain
);
206 dev_dbg(par
->info
->device
, " to_copy=%zu, remain=%zu\n",
207 to_copy
, remain
- to_copy
);
209 #ifdef __LITTLE_ENDIAN
210 for (i
= 0; i
< to_copy
; i
+= 2) {
211 txbuf16
[i
] = 0x0100 | vmem8
[i
+ 1];
212 txbuf16
[i
+ 1] = 0x0100 | vmem8
[i
];
215 for (i
= 0; i
< to_copy
; i
++)
216 txbuf16
[i
] = 0x0100 | vmem8
[i
];
218 vmem8
= vmem8
+ to_copy
;
219 ret
= par
->fbtftops
.write(par
, par
->txbuf
.buf
, to_copy
* 2);
227 EXPORT_SYMBOL(fbtft_write_vmem16_bus9
);
229 int fbtft_write_vmem8_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
)
231 dev_err(par
->info
->device
, "%s: function not implemented\n", __func__
);
234 EXPORT_SYMBOL(fbtft_write_vmem8_bus8
);
236 /* 16 bit pixel over 16-bit databus */
237 int fbtft_write_vmem16_bus16(struct fbtft_par
*par
, size_t offset
, size_t len
)
241 fbtft_par_dbg(DEBUG_WRITE_VMEM
, par
, "%s(offset=%zu, len=%zu)\n",
242 __func__
, offset
, len
);
244 vmem16
= (u16
*)(par
->info
->screen_buffer
+ offset
);
246 if (par
->gpio
.dc
!= -1)
247 gpio_set_value(par
->gpio
.dc
, 1);
249 /* no need for buffered write with 16-bit bus */
250 return par
->fbtftops
.write(par
, vmem16
, len
);
252 EXPORT_SYMBOL(fbtft_write_vmem16_bus16
);