of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / staging / fbtft / fbtft-bus.c
blob58449ad84f46c17f842cc32e04ee5c18cf2b58c0
1 #include <linux/export.h>
2 #include <linux/errno.h>
3 #include <linux/gpio.h>
4 #include <linux/spi/spi.h>
5 #include "fbtft.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, ...) \
15 { \
16 va_list args; \
17 int i, ret; \
18 int offset = 0; \
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); \
25 } \
26 va_end(args); \
27 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par, par->info->device, type, buf, len, "%s: ", __func__); \
28 } \
30 va_start(args, len); \
32 if (par->startbyte) { \
33 *(u8 *)par->buf = par->startbyte; \
34 buf = (type *)(par->buf + 1); \
35 offset = 1; \
36 } \
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); \
42 if (ret < 0) { \
43 va_end(args); \
44 dev_err(par->info->device, "%s: write() failed and returned %d\n", __func__, ret); \
45 return; \
46 } \
47 len--; \
49 if (par->startbyte) \
50 *(u8 *)par->buf = par->startbyte | 0x2; \
52 if (len) { \
53 i = len; \
54 while (i--) { \
55 *buf++ = modifier((type)va_arg(args, unsigned int)); \
56 } \
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)); \
61 if (ret < 0) { \
62 va_end(args); \
63 dev_err(par->info->device, "%s: write() failed and returned %d\n", __func__, ret); \
64 return; \
65 } \
66 } \
67 va_end(args); \
68 } \
69 EXPORT_SYMBOL(func);
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, ...)
77 va_list args;
78 int i, ret;
79 int pad = 0;
80 u16 *buf = (u16 *)par->buf;
82 if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) {
83 va_start(args, len);
84 for (i = 0; i < len; i++)
85 *(((u8 *)buf) + i) = (u8)va_arg(args, unsigned int);
86 va_end(args);
87 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par,
88 par->info->device, u8, buf, len, "%s: ", __func__);
90 if (len <= 0)
91 return;
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++)
98 *buf++ = 0x000;
101 va_start(args, len);
102 *buf++ = (u8)va_arg(args, unsigned int);
103 i = len - 1;
104 while (i--) {
105 *buf = (u8)va_arg(args, unsigned int);
106 *buf++ |= 0x100; /* dc=1 */
108 va_end(args);
109 ret = par->fbtftops.write(par, par->buf, (len + pad) * sizeof(u16));
110 if (ret < 0) {
111 dev_err(par->info->device,
112 "write() failed and returned %d\n", ret);
113 return;
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)
127 u16 *vmem16;
128 u16 *txbuf16 = (u16 *)par->txbuf.buf;
129 size_t remain;
130 size_t to_copy;
131 size_t tx_array_size;
132 int i;
133 int ret = 0;
134 size_t startbyte_size = 0;
136 fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s(offset=%zu, len=%zu)\n",
137 __func__, offset, len);
139 remain = len / 2;
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 */
146 if (!par->txbuf.buf)
147 return par->fbtftops.write(par, vmem16, len);
149 /* buffered write */
150 tx_array_size = par->txbuf.len / 2;
152 if (par->startbyte) {
153 txbuf16 = (u16 *)(par->txbuf.buf + 1);
154 tx_array_size -= 2;
155 *(u8 *)(par->txbuf.buf) = par->startbyte | 0x2;
156 startbyte_size = 1;
159 while (remain) {
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);
170 if (ret < 0)
171 return ret;
172 remain -= to_copy;
175 return ret;
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)
182 u8 *vmem8;
183 u16 *txbuf16 = par->txbuf.buf;
184 size_t remain;
185 size_t to_copy;
186 size_t tx_array_size;
187 int i;
188 int ret = 0;
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__);
195 return -1;
198 remain = len;
199 vmem8 = par->info->screen_buffer + offset;
201 tx_array_size = par->txbuf.len / 2;
203 while (remain) {
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];
213 #else
214 for (i = 0; i < to_copy; i++)
215 txbuf16[i] = 0x0100 | vmem8[i];
216 #endif
217 vmem8 = vmem8 + to_copy;
218 ret = par->fbtftops.write(par, par->txbuf.buf, to_copy * 2);
219 if (ret < 0)
220 return ret;
221 remain -= to_copy;
224 return ret;
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__);
231 return -1;
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)
238 u16 *vmem16;
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);