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>
6 #include "fbtft.h"
7
8 /*****************************************************************************
9 *
10 * void (*write_reg)(struct fbtft_par *par, int len, ...);
11 *
12 *****************************************************************************/
13
14 #define define_fbtft_write_reg(func, type, modifier) \
15 void func(struct fbtft_par *par, int len, ...) \
16 { \
17 va_list args; \
18 int i, ret; \
19 int offset = 0; \
20 type *buf = (type *)par->buf; \
21 \
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); \
26 } \
27 va_end(args); \
28 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par, par->info->device, type, buf, len, "%s: ", __func__); \
29 } \
30 \
31 va_start(args, len); \
32 \
33 if (par->startbyte) { \
34 *(u8 *)par->buf = par->startbyte; \
35 buf = (type *)(par->buf + 1); \
36 offset = 1; \
37 } \
38 \
39 *buf = modifier((type)va_arg(args, unsigned int)); \
40 ret = fbtft_write_buf_dc(par, par->buf, sizeof(type) + offset, 0); \
41 if (ret < 0) \
42 goto out; \
43 len--; \
44 \
45 if (par->startbyte) \
46 *(u8 *)par->buf = par->startbyte | 0x2; \
47 \
48 if (len) { \
49 i = len; \
50 while (i--) \
51 *buf++ = modifier((type)va_arg(args, unsigned int)); \
52 fbtft_write_buf_dc(par, par->buf, \
53 len * (sizeof(type) + offset), 1); \
54 } \
55 out: \
56 va_end(args); \
57 } \
58 EXPORT_SYMBOL(func);
59
60 define_fbtft_write_reg(fbtft_write_reg8_bus8, u8, )
define_fbtft_write_reg(fbtft_write_reg16_bus8,u16,cpu_to_be16)61 define_fbtft_write_reg(fbtft_write_reg16_bus8, u16, cpu_to_be16)
62 define_fbtft_write_reg(fbtft_write_reg16_bus16, u16, )
63
64 void fbtft_write_reg8_bus9(struct fbtft_par *par, int len, ...)
65 {
66 va_list args;
67 int i, ret;
68 int pad = 0;
69 u16 *buf = (u16 *)par->buf;
70
71 if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) {
72 va_start(args, len);
73 for (i = 0; i < len; i++)
74 *(((u8 *)buf) + i) = (u8)va_arg(args, unsigned int);
75 va_end(args);
76 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par,
77 par->info->device, u8, buf, len, "%s: ", __func__);
78 }
79 if (len <= 0)
80 return;
81
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)
85 */
86 pad = (len % 4) ? 4 - (len % 4) : 0;
87 for (i = 0; i < pad; i++)
88 *buf++ = 0x000;
89 }
90
91 va_start(args, len);
92 *buf++ = (u8)va_arg(args, unsigned int);
93 i = len - 1;
94 while (i--) {
95 *buf = (u8)va_arg(args, unsigned int);
96 *buf++ |= 0x100; /* dc=1 */
97 }
98 va_end(args);
99 ret = par->fbtftops.write(par, par->buf, (len + pad) * sizeof(u16));
100 if (ret < 0) {
101 dev_err(par->info->device,
102 "write() failed and returned %d\n", ret);
103 return;
104 }
105 }
106 EXPORT_SYMBOL(fbtft_write_reg8_bus9);
107
108 /*****************************************************************************
109 *
110 * int (*write_vmem)(struct fbtft_par *par);
111 *
112 *****************************************************************************/
113
114 /* 16 bit pixel over 8-bit databus */
fbtft_write_vmem16_bus8(struct fbtft_par * par,size_t offset,size_t len)115 int fbtft_write_vmem16_bus8(struct fbtft_par *par, size_t offset, size_t len)
116 {
117 u16 *vmem16;
118 __be16 *txbuf16 = par->txbuf.buf;
119 size_t remain;
120 size_t to_copy;
121 size_t tx_array_size;
122 int i;
123 int ret = 0;
124 size_t startbyte_size = 0;
125
126 fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s(offset=%zu, len=%zu)\n",
127 __func__, offset, len);
128
129 remain = len / 2;
130 vmem16 = (u16 *)(par->info->screen_buffer + offset);
131
132 if (par->gpio.dc != -1)
133 gpio_set_value(par->gpio.dc, 1);
134
135 /* non buffered write */
136 if (!par->txbuf.buf)
137 return par->fbtftops.write(par, vmem16, len);
138
139 /* buffered write */
140 tx_array_size = par->txbuf.len / 2;
141
142 if (par->startbyte) {
143 txbuf16 = par->txbuf.buf + 1;
144 tx_array_size -= 2;
145 *(u8 *)(par->txbuf.buf) = par->startbyte | 0x2;
146 startbyte_size = 1;
147 }
148
149 while (remain) {
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);
153
154 for (i = 0; i < to_copy; i++)
155 txbuf16[i] = cpu_to_be16(vmem16[i]);
156
157 vmem16 = vmem16 + to_copy;
158 ret = par->fbtftops.write(par, par->txbuf.buf,
159 startbyte_size + to_copy * 2);
160 if (ret < 0)
161 return ret;
162 remain -= to_copy;
163 }
164
165 return ret;
166 }
167 EXPORT_SYMBOL(fbtft_write_vmem16_bus8);
168
169 /* 16 bit pixel over 9-bit SPI bus: dc + high byte, dc + low byte */
fbtft_write_vmem16_bus9(struct fbtft_par * par,size_t offset,size_t len)170 int fbtft_write_vmem16_bus9(struct fbtft_par *par, size_t offset, size_t len)
171 {
172 u8 *vmem8;
173 u16 *txbuf16 = par->txbuf.buf;
174 size_t remain;
175 size_t to_copy;
176 size_t tx_array_size;
177 int i;
178 int ret = 0;
179
180 fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s(offset=%zu, len=%zu)\n",
181 __func__, offset, len);
182
183 if (!par->txbuf.buf) {
184 dev_err(par->info->device, "%s: txbuf.buf is NULL\n", __func__);
185 return -1;
186 }
187
188 remain = len;
189 vmem8 = par->info->screen_buffer + offset;
190
191 tx_array_size = par->txbuf.len / 2;
192
193 while (remain) {
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);
197
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];
202 }
203 #else
204 for (i = 0; i < to_copy; i++)
205 txbuf16[i] = 0x0100 | vmem8[i];
206 #endif
207 vmem8 = vmem8 + to_copy;
208 ret = par->fbtftops.write(par, par->txbuf.buf, to_copy * 2);
209 if (ret < 0)
210 return ret;
211 remain -= to_copy;
212 }
213
214 return ret;
215 }
216 EXPORT_SYMBOL(fbtft_write_vmem16_bus9);
217
fbtft_write_vmem8_bus8(struct fbtft_par * par,size_t offset,size_t len)218 int fbtft_write_vmem8_bus8(struct fbtft_par *par, size_t offset, size_t len)
219 {
220 dev_err(par->info->device, "%s: function not implemented\n", __func__);
221 return -1;
222 }
223 EXPORT_SYMBOL(fbtft_write_vmem8_bus8);
224
225 /* 16 bit pixel over 16-bit databus */
fbtft_write_vmem16_bus16(struct fbtft_par * par,size_t offset,size_t len)226 int fbtft_write_vmem16_bus16(struct fbtft_par *par, size_t offset, size_t len)
227 {
228 u16 *vmem16;
229
230 fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s(offset=%zu, len=%zu)\n",
231 __func__, offset, len);
232
233 vmem16 = (u16 *)(par->info->screen_buffer + offset);
234
235 /* no need for buffered write with 16-bit bus */
236 return fbtft_write_buf_dc(par, vmem16, len, 1);
237 }
238 EXPORT_SYMBOL(fbtft_write_vmem16_bus16);
239