fbtft-io.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. int fbtft_write_spi(struct fbtft_par *par, void *buf, size_t len)
  8. {
  9. struct spi_transfer t = {
  10. .tx_buf = buf,
  11. .len = len,
  12. };
  13. struct spi_message m;
  14. fbtft_par_dbg_hex(DEBUG_WRITE, par, par->info->device, u8, buf, len,
  15. "%s(len=%d): ", __func__, len);
  16. if (!par->spi) {
  17. dev_err(par->info->device,
  18. "%s: par->spi is unexpectedly NULL\n", __func__);
  19. return -1;
  20. }
  21. spi_message_init(&m);
  22. spi_message_add_tail(&t, &m);
  23. return spi_sync(par->spi, &m);
  24. }
  25. EXPORT_SYMBOL(fbtft_write_spi);
  26. /**
  27. * fbtft_write_spi_emulate_9() - write SPI emulating 9-bit
  28. * @par: Driver data
  29. * @buf: Buffer to write
  30. * @len: Length of buffer (must be divisible by 8)
  31. *
  32. * When 9-bit SPI is not available, this function can be used to emulate that.
  33. * par->extra must hold a transformation buffer used for transfer.
  34. */
  35. int fbtft_write_spi_emulate_9(struct fbtft_par *par, void *buf, size_t len)
  36. {
  37. u16 *src = buf;
  38. u8 *dst = par->extra;
  39. size_t size = len / 2;
  40. size_t added = 0;
  41. int bits, i, j;
  42. u64 val, dc, tmp;
  43. fbtft_par_dbg_hex(DEBUG_WRITE, par, par->info->device, u8, buf, len,
  44. "%s(len=%d): ", __func__, len);
  45. if (!par->extra) {
  46. dev_err(par->info->device, "%s: error: par->extra is NULL\n",
  47. __func__);
  48. return -EINVAL;
  49. }
  50. if ((len % 8) != 0) {
  51. dev_err(par->info->device,
  52. "error: len=%zu must be divisible by 8\n", len);
  53. return -EINVAL;
  54. }
  55. for (i = 0; i < size; i += 8) {
  56. tmp = 0;
  57. bits = 63;
  58. for (j = 0; j < 7; j++) {
  59. dc = (*src & 0x0100) ? 1 : 0;
  60. val = *src & 0x00FF;
  61. tmp |= dc << bits;
  62. bits -= 8;
  63. tmp |= val << bits--;
  64. src++;
  65. }
  66. tmp |= ((*src & 0x0100) ? 1 : 0);
  67. *(__be64 *)dst = cpu_to_be64(tmp);
  68. dst += 8;
  69. *dst++ = (u8)(*src++ & 0x00FF);
  70. added++;
  71. }
  72. return spi_write(par->spi, par->extra, size + added);
  73. }
  74. EXPORT_SYMBOL(fbtft_write_spi_emulate_9);
  75. int fbtft_read_spi(struct fbtft_par *par, void *buf, size_t len)
  76. {
  77. int ret;
  78. u8 txbuf[32] = { 0, };
  79. struct spi_transfer t = {
  80. .speed_hz = 2000000,
  81. .rx_buf = buf,
  82. .len = len,
  83. };
  84. struct spi_message m;
  85. if (!par->spi) {
  86. dev_err(par->info->device,
  87. "%s: par->spi is unexpectedly NULL\n", __func__);
  88. return -ENODEV;
  89. }
  90. if (par->startbyte) {
  91. if (len > 32) {
  92. dev_err(par->info->device,
  93. "len=%zu can't be larger than 32 when using 'startbyte'\n",
  94. len);
  95. return -EINVAL;
  96. }
  97. txbuf[0] = par->startbyte | 0x3;
  98. t.tx_buf = txbuf;
  99. fbtft_par_dbg_hex(DEBUG_READ, par, par->info->device, u8,
  100. txbuf, len, "%s(len=%d) txbuf => ",
  101. __func__, len);
  102. }
  103. spi_message_init(&m);
  104. spi_message_add_tail(&t, &m);
  105. ret = spi_sync(par->spi, &m);
  106. fbtft_par_dbg_hex(DEBUG_READ, par, par->info->device, u8, buf, len,
  107. "%s(len=%d) buf <= ", __func__, len);
  108. return ret;
  109. }
  110. EXPORT_SYMBOL(fbtft_read_spi);
  111. /*
  112. * Optimized use of gpiolib is twice as fast as no optimization
  113. * only one driver can use the optimized version at a time
  114. */
  115. int fbtft_write_gpio8_wr(struct fbtft_par *par, void *buf, size_t len)
  116. {
  117. u8 data;
  118. int i;
  119. #ifndef DO_NOT_OPTIMIZE_FBTFT_WRITE_GPIO
  120. static u8 prev_data;
  121. #endif
  122. fbtft_par_dbg_hex(DEBUG_WRITE, par, par->info->device, u8, buf, len,
  123. "%s(len=%d): ", __func__, len);
  124. while (len--) {
  125. data = *(u8 *)buf;
  126. /* Start writing by pulling down /WR */
  127. gpio_set_value(par->gpio.wr, 0);
  128. /* Set data */
  129. #ifndef DO_NOT_OPTIMIZE_FBTFT_WRITE_GPIO
  130. if (data == prev_data) {
  131. gpio_set_value(par->gpio.wr, 0); /* used as delay */
  132. } else {
  133. for (i = 0; i < 8; i++) {
  134. if ((data & 1) != (prev_data & 1))
  135. gpio_set_value(par->gpio.db[i],
  136. data & 1);
  137. data >>= 1;
  138. prev_data >>= 1;
  139. }
  140. }
  141. #else
  142. for (i = 0; i < 8; i++) {
  143. gpio_set_value(par->gpio.db[i], data & 1);
  144. data >>= 1;
  145. }
  146. #endif
  147. /* Pullup /WR */
  148. gpio_set_value(par->gpio.wr, 1);
  149. #ifndef DO_NOT_OPTIMIZE_FBTFT_WRITE_GPIO
  150. prev_data = *(u8 *)buf;
  151. #endif
  152. buf++;
  153. }
  154. return 0;
  155. }
  156. EXPORT_SYMBOL(fbtft_write_gpio8_wr);
  157. int fbtft_write_gpio16_wr(struct fbtft_par *par, void *buf, size_t len)
  158. {
  159. u16 data;
  160. int i;
  161. #ifndef DO_NOT_OPTIMIZE_FBTFT_WRITE_GPIO
  162. static u16 prev_data;
  163. #endif
  164. fbtft_par_dbg_hex(DEBUG_WRITE, par, par->info->device, u8, buf, len,
  165. "%s(len=%d): ", __func__, len);
  166. while (len) {
  167. data = *(u16 *)buf;
  168. /* Start writing by pulling down /WR */
  169. gpio_set_value(par->gpio.wr, 0);
  170. /* Set data */
  171. #ifndef DO_NOT_OPTIMIZE_FBTFT_WRITE_GPIO
  172. if (data == prev_data) {
  173. gpio_set_value(par->gpio.wr, 0); /* used as delay */
  174. } else {
  175. for (i = 0; i < 16; i++) {
  176. if ((data & 1) != (prev_data & 1))
  177. gpio_set_value(par->gpio.db[i],
  178. data & 1);
  179. data >>= 1;
  180. prev_data >>= 1;
  181. }
  182. }
  183. #else
  184. for (i = 0; i < 16; i++) {
  185. gpio_set_value(par->gpio.db[i], data & 1);
  186. data >>= 1;
  187. }
  188. #endif
  189. /* Pullup /WR */
  190. gpio_set_value(par->gpio.wr, 1);
  191. #ifndef DO_NOT_OPTIMIZE_FBTFT_WRITE_GPIO
  192. prev_data = *(u16 *)buf;
  193. #endif
  194. buf += 2;
  195. len -= 2;
  196. }
  197. return 0;
  198. }
  199. EXPORT_SYMBOL(fbtft_write_gpio16_wr);
  200. int fbtft_write_gpio16_wr_latched(struct fbtft_par *par, void *buf, size_t len)
  201. {
  202. dev_err(par->info->device, "%s: function not implemented\n", __func__);
  203. return -1;
  204. }
  205. EXPORT_SYMBOL(fbtft_write_gpio16_wr_latched);