1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c296d5f9SThomas Petazzoni #include <linux/export.h>
3c296d5f9SThomas Petazzoni #include <linux/errno.h>
4c440eee1SNishad Kamdar #include <linux/gpio/consumer.h>
5c296d5f9SThomas Petazzoni #include <linux/spi/spi.h>
6c296d5f9SThomas Petazzoni #include "fbtft.h"
7c296d5f9SThomas Petazzoni
8c296d5f9SThomas Petazzoni /*****************************************************************************
9c296d5f9SThomas Petazzoni *
10c296d5f9SThomas Petazzoni * void (*write_reg)(struct fbtft_par *par, int len, ...);
11c296d5f9SThomas Petazzoni *
12c296d5f9SThomas Petazzoni *****************************************************************************/
13c296d5f9SThomas Petazzoni
148d8825b4SAlfonso Lima Astor #define define_fbtft_write_reg(func, buffer_type, data_type, modifier) \
15c296d5f9SThomas Petazzoni void func(struct fbtft_par *par, int len, ...) \
16c296d5f9SThomas Petazzoni { \
17c296d5f9SThomas Petazzoni va_list args; \
18c296d5f9SThomas Petazzoni int i, ret; \
19c296d5f9SThomas Petazzoni int offset = 0; \
208d8825b4SAlfonso Lima Astor buffer_type *buf = (buffer_type *)par->buf; \
21c296d5f9SThomas Petazzoni \
22c296d5f9SThomas Petazzoni if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) { \
23c296d5f9SThomas Petazzoni va_start(args, len); \
24c296d5f9SThomas Petazzoni for (i = 0; i < len; i++) { \
25cc1c0eeaSRenato Soma buf[i] = modifier((data_type)va_arg(args, \
26cc1c0eeaSRenato Soma unsigned int)); \
27c296d5f9SThomas Petazzoni } \
28c296d5f9SThomas Petazzoni va_end(args); \
29cc1c0eeaSRenato Soma fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par, \
30cc1c0eeaSRenato Soma par->info->device, buffer_type, buf, len, \
31cc1c0eeaSRenato Soma "%s: ", __func__); \
32c296d5f9SThomas Petazzoni } \
33c296d5f9SThomas Petazzoni \
34c296d5f9SThomas Petazzoni va_start(args, len); \
35c296d5f9SThomas Petazzoni \
36c296d5f9SThomas Petazzoni if (par->startbyte) { \
37c296d5f9SThomas Petazzoni *(u8 *)par->buf = par->startbyte; \
388d8825b4SAlfonso Lima Astor buf = (buffer_type *)(par->buf + 1); \
39c296d5f9SThomas Petazzoni offset = 1; \
40c296d5f9SThomas Petazzoni } \
41c296d5f9SThomas Petazzoni \
428d8825b4SAlfonso Lima Astor *buf = modifier((data_type)va_arg(args, unsigned int)); \
43cc1c0eeaSRenato Soma ret = fbtft_write_buf_dc(par, par->buf, sizeof(data_type) + offset, \
44cc1c0eeaSRenato Soma 0); \
45e70065fdSHeiner Kallweit if (ret < 0) \
46e70065fdSHeiner Kallweit goto out; \
47c296d5f9SThomas Petazzoni len--; \
48c296d5f9SThomas Petazzoni \
49c296d5f9SThomas Petazzoni if (par->startbyte) \
50c296d5f9SThomas Petazzoni *(u8 *)par->buf = par->startbyte | 0x2; \
51c296d5f9SThomas Petazzoni \
52c296d5f9SThomas Petazzoni if (len) { \
53c296d5f9SThomas Petazzoni i = len; \
54e70065fdSHeiner Kallweit while (i--) \
55cc1c0eeaSRenato Soma *buf++ = modifier((data_type)va_arg(args, \
56cc1c0eeaSRenato Soma unsigned int)); \
57e70065fdSHeiner Kallweit fbtft_write_buf_dc(par, par->buf, \
588d8825b4SAlfonso Lima Astor len * (sizeof(data_type) + offset), 1); \
59c296d5f9SThomas Petazzoni } \
60e70065fdSHeiner Kallweit out: \
61c296d5f9SThomas Petazzoni va_end(args); \
62c296d5f9SThomas Petazzoni } \
63c296d5f9SThomas Petazzoni EXPORT_SYMBOL(func);
64c296d5f9SThomas Petazzoni
658d8825b4SAlfonso Lima Astor define_fbtft_write_reg(fbtft_write_reg8_bus8, u8, u8, )
define_fbtft_write_reg(fbtft_write_reg16_bus8,__be16,u16,cpu_to_be16)668d8825b4SAlfonso Lima Astor define_fbtft_write_reg(fbtft_write_reg16_bus8, __be16, u16, cpu_to_be16)
678d8825b4SAlfonso Lima Astor define_fbtft_write_reg(fbtft_write_reg16_bus16, u16, u16, )
68c296d5f9SThomas Petazzoni
69c296d5f9SThomas Petazzoni void fbtft_write_reg8_bus9(struct fbtft_par *par, int len, ...)
70c296d5f9SThomas Petazzoni {
71c296d5f9SThomas Petazzoni va_list args;
72c296d5f9SThomas Petazzoni int i, ret;
73c296d5f9SThomas Petazzoni int pad = 0;
74c296d5f9SThomas Petazzoni u16 *buf = (u16 *)par->buf;
75c296d5f9SThomas Petazzoni
76c296d5f9SThomas Petazzoni if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) {
77c296d5f9SThomas Petazzoni va_start(args, len);
78c296d5f9SThomas Petazzoni for (i = 0; i < len; i++)
79c296d5f9SThomas Petazzoni *(((u8 *)buf) + i) = (u8)va_arg(args, unsigned int);
80c296d5f9SThomas Petazzoni va_end(args);
81c296d5f9SThomas Petazzoni fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par,
82333c7b94SLeonardo Brás par->info->device, u8, buf, len, "%s: ",
83333c7b94SLeonardo Brás __func__);
84c296d5f9SThomas Petazzoni }
85c296d5f9SThomas Petazzoni if (len <= 0)
86c296d5f9SThomas Petazzoni return;
87c296d5f9SThomas Petazzoni
88c296d5f9SThomas Petazzoni if (par->spi && (par->spi->bits_per_word == 8)) {
89c296d5f9SThomas Petazzoni /* we're emulating 9-bit, pad start of buffer with no-ops
90b2ac4a92SAnson Jacob * (assuming here that zero is a no-op)
91b2ac4a92SAnson Jacob */
92c296d5f9SThomas Petazzoni pad = (len % 4) ? 4 - (len % 4) : 0;
93c296d5f9SThomas Petazzoni for (i = 0; i < pad; i++)
94c296d5f9SThomas Petazzoni *buf++ = 0x000;
95c296d5f9SThomas Petazzoni }
96c296d5f9SThomas Petazzoni
97c296d5f9SThomas Petazzoni va_start(args, len);
98c296d5f9SThomas Petazzoni *buf++ = (u8)va_arg(args, unsigned int);
99c296d5f9SThomas Petazzoni i = len - 1;
100c296d5f9SThomas Petazzoni while (i--) {
101c296d5f9SThomas Petazzoni *buf = (u8)va_arg(args, unsigned int);
102c296d5f9SThomas Petazzoni *buf++ |= 0x100; /* dc=1 */
103c296d5f9SThomas Petazzoni }
104c296d5f9SThomas Petazzoni va_end(args);
105c296d5f9SThomas Petazzoni ret = par->fbtftops.write(par, par->buf, (len + pad) * sizeof(u16));
106c296d5f9SThomas Petazzoni if (ret < 0) {
107c296d5f9SThomas Petazzoni dev_err(par->info->device,
108aed1c72eSHaneen Mohammed "write() failed and returned %d\n", ret);
109c296d5f9SThomas Petazzoni return;
110c296d5f9SThomas Petazzoni }
111c296d5f9SThomas Petazzoni }
112c296d5f9SThomas Petazzoni EXPORT_SYMBOL(fbtft_write_reg8_bus9);
113c296d5f9SThomas Petazzoni
114c296d5f9SThomas Petazzoni /*****************************************************************************
115c296d5f9SThomas Petazzoni *
116c296d5f9SThomas Petazzoni * int (*write_vmem)(struct fbtft_par *par);
117c296d5f9SThomas Petazzoni *
118c296d5f9SThomas Petazzoni *****************************************************************************/
119c296d5f9SThomas Petazzoni
120c296d5f9SThomas Petazzoni /* 16 bit pixel over 8-bit databus */
fbtft_write_vmem16_bus8(struct fbtft_par * par,size_t offset,size_t len)121c296d5f9SThomas Petazzoni int fbtft_write_vmem16_bus8(struct fbtft_par *par, size_t offset, size_t len)
122c296d5f9SThomas Petazzoni {
123c296d5f9SThomas Petazzoni u16 *vmem16;
124919a9bb6SAlex Wilson __be16 *txbuf16 = par->txbuf.buf;
125c296d5f9SThomas Petazzoni size_t remain;
126c296d5f9SThomas Petazzoni size_t to_copy;
127c296d5f9SThomas Petazzoni size_t tx_array_size;
128c296d5f9SThomas Petazzoni int i;
129c296d5f9SThomas Petazzoni int ret = 0;
130c296d5f9SThomas Petazzoni size_t startbyte_size = 0;
131c296d5f9SThomas Petazzoni
132c296d5f9SThomas Petazzoni fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s(offset=%zu, len=%zu)\n",
133c296d5f9SThomas Petazzoni __func__, offset, len);
134c296d5f9SThomas Petazzoni
135c296d5f9SThomas Petazzoni remain = len / 2;
1364b6dc179SLars Svensson vmem16 = (u16 *)(par->info->screen_buffer + offset);
137c296d5f9SThomas Petazzoni
138c440eee1SNishad Kamdar gpiod_set_value(par->gpio.dc, 1);
139c296d5f9SThomas Petazzoni
140c296d5f9SThomas Petazzoni /* non buffered write */
141c296d5f9SThomas Petazzoni if (!par->txbuf.buf)
142c296d5f9SThomas Petazzoni return par->fbtftops.write(par, vmem16, len);
143c296d5f9SThomas Petazzoni
144c296d5f9SThomas Petazzoni /* buffered write */
145c296d5f9SThomas Petazzoni tx_array_size = par->txbuf.len / 2;
146c296d5f9SThomas Petazzoni
147c296d5f9SThomas Petazzoni if (par->startbyte) {
14864f93e17SJanani Ravichandran txbuf16 = par->txbuf.buf + 1;
149c296d5f9SThomas Petazzoni tx_array_size -= 2;
150c296d5f9SThomas Petazzoni *(u8 *)(par->txbuf.buf) = par->startbyte | 0x2;
151c296d5f9SThomas Petazzoni startbyte_size = 1;
152c296d5f9SThomas Petazzoni }
153c296d5f9SThomas Petazzoni
154c296d5f9SThomas Petazzoni while (remain) {
1553589d3c9SAlison Schofield to_copy = min(tx_array_size, remain);
156c296d5f9SThomas Petazzoni dev_dbg(par->info->device, "to_copy=%zu, remain=%zu\n",
157c296d5f9SThomas Petazzoni to_copy, remain - to_copy);
158c296d5f9SThomas Petazzoni
159c296d5f9SThomas Petazzoni for (i = 0; i < to_copy; i++)
160c296d5f9SThomas Petazzoni txbuf16[i] = cpu_to_be16(vmem16[i]);
161c296d5f9SThomas Petazzoni
162c296d5f9SThomas Petazzoni vmem16 = vmem16 + to_copy;
163c296d5f9SThomas Petazzoni ret = par->fbtftops.write(par, par->txbuf.buf,
164c296d5f9SThomas Petazzoni startbyte_size + to_copy * 2);
165c296d5f9SThomas Petazzoni if (ret < 0)
166c296d5f9SThomas Petazzoni return ret;
167c296d5f9SThomas Petazzoni remain -= to_copy;
168c296d5f9SThomas Petazzoni }
169c296d5f9SThomas Petazzoni
170c296d5f9SThomas Petazzoni return ret;
171c296d5f9SThomas Petazzoni }
172c296d5f9SThomas Petazzoni EXPORT_SYMBOL(fbtft_write_vmem16_bus8);
173c296d5f9SThomas Petazzoni
174c296d5f9SThomas Petazzoni /* 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)175c296d5f9SThomas Petazzoni int fbtft_write_vmem16_bus9(struct fbtft_par *par, size_t offset, size_t len)
176c296d5f9SThomas Petazzoni {
1776b626c77SLars Svensson u8 *vmem8;
178c296d5f9SThomas Petazzoni u16 *txbuf16 = par->txbuf.buf;
179c296d5f9SThomas Petazzoni size_t remain;
180c296d5f9SThomas Petazzoni size_t to_copy;
181c296d5f9SThomas Petazzoni size_t tx_array_size;
182c296d5f9SThomas Petazzoni int i;
183c296d5f9SThomas Petazzoni int ret = 0;
184c296d5f9SThomas Petazzoni
185c296d5f9SThomas Petazzoni fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s(offset=%zu, len=%zu)\n",
186c296d5f9SThomas Petazzoni __func__, offset, len);
187c296d5f9SThomas Petazzoni
188c296d5f9SThomas Petazzoni if (!par->txbuf.buf) {
189c296d5f9SThomas Petazzoni dev_err(par->info->device, "%s: txbuf.buf is NULL\n", __func__);
190c296d5f9SThomas Petazzoni return -1;
191c296d5f9SThomas Petazzoni }
192c296d5f9SThomas Petazzoni
193c296d5f9SThomas Petazzoni remain = len;
1944b6dc179SLars Svensson vmem8 = par->info->screen_buffer + offset;
195c296d5f9SThomas Petazzoni
196c296d5f9SThomas Petazzoni tx_array_size = par->txbuf.len / 2;
197c296d5f9SThomas Petazzoni
198c296d5f9SThomas Petazzoni while (remain) {
1993589d3c9SAlison Schofield to_copy = min(tx_array_size, remain);
200c296d5f9SThomas Petazzoni dev_dbg(par->info->device, "to_copy=%zu, remain=%zu\n",
201c296d5f9SThomas Petazzoni to_copy, remain - to_copy);
202c296d5f9SThomas Petazzoni
203c296d5f9SThomas Petazzoni #ifdef __LITTLE_ENDIAN
204c296d5f9SThomas Petazzoni for (i = 0; i < to_copy; i += 2) {
2056b626c77SLars Svensson txbuf16[i] = 0x0100 | vmem8[i + 1];
2066b626c77SLars Svensson txbuf16[i + 1] = 0x0100 | vmem8[i];
207c296d5f9SThomas Petazzoni }
208c296d5f9SThomas Petazzoni #else
209c296d5f9SThomas Petazzoni for (i = 0; i < to_copy; i++)
2106b626c77SLars Svensson txbuf16[i] = 0x0100 | vmem8[i];
211c296d5f9SThomas Petazzoni #endif
212c296d5f9SThomas Petazzoni vmem8 = vmem8 + to_copy;
213c296d5f9SThomas Petazzoni ret = par->fbtftops.write(par, par->txbuf.buf, to_copy * 2);
214c296d5f9SThomas Petazzoni if (ret < 0)
215c296d5f9SThomas Petazzoni return ret;
216c296d5f9SThomas Petazzoni remain -= to_copy;
217c296d5f9SThomas Petazzoni }
218c296d5f9SThomas Petazzoni
219c296d5f9SThomas Petazzoni return ret;
220c296d5f9SThomas Petazzoni }
221c296d5f9SThomas Petazzoni EXPORT_SYMBOL(fbtft_write_vmem16_bus9);
222c296d5f9SThomas Petazzoni
fbtft_write_vmem8_bus8(struct fbtft_par * par,size_t offset,size_t len)223c296d5f9SThomas Petazzoni int fbtft_write_vmem8_bus8(struct fbtft_par *par, size_t offset, size_t len)
224c296d5f9SThomas Petazzoni {
225c296d5f9SThomas Petazzoni dev_err(par->info->device, "%s: function not implemented\n", __func__);
226c296d5f9SThomas Petazzoni return -1;
227c296d5f9SThomas Petazzoni }
228c296d5f9SThomas Petazzoni EXPORT_SYMBOL(fbtft_write_vmem8_bus8);
229c296d5f9SThomas Petazzoni
230c296d5f9SThomas Petazzoni /* 16 bit pixel over 16-bit databus */
fbtft_write_vmem16_bus16(struct fbtft_par * par,size_t offset,size_t len)231c296d5f9SThomas Petazzoni int fbtft_write_vmem16_bus16(struct fbtft_par *par, size_t offset, size_t len)
232c296d5f9SThomas Petazzoni {
233c296d5f9SThomas Petazzoni u16 *vmem16;
234c296d5f9SThomas Petazzoni
235c296d5f9SThomas Petazzoni fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s(offset=%zu, len=%zu)\n",
236c296d5f9SThomas Petazzoni __func__, offset, len);
237c296d5f9SThomas Petazzoni
2384b6dc179SLars Svensson vmem16 = (u16 *)(par->info->screen_buffer + offset);
239c296d5f9SThomas Petazzoni
240c296d5f9SThomas Petazzoni /* no need for buffered write with 16-bit bus */
241e70065fdSHeiner Kallweit return fbtft_write_buf_dc(par, vmem16, len, 1);
242c296d5f9SThomas Petazzoni }
243c296d5f9SThomas Petazzoni EXPORT_SYMBOL(fbtft_write_vmem16_bus16);
244