fbtft-bus.c 8.4 KB

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