flexfb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * Generic FB driver for TFT LCD displays
  3. *
  4. * Copyright (C) 2013 Noralf Tronnes
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/gpio.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/delay.h>
  27. #include "fbtft.h"
  28. #define DRVNAME "flexfb"
  29. static char *chip;
  30. module_param(chip, charp, 0);
  31. MODULE_PARM_DESC(chip, "LCD controller");
  32. static unsigned int width;
  33. module_param(width, uint, 0);
  34. MODULE_PARM_DESC(width, "Display width");
  35. static unsigned int height;
  36. module_param(height, uint, 0);
  37. MODULE_PARM_DESC(height, "Display height");
  38. static int init[512];
  39. static int init_num;
  40. module_param_array(init, int, &init_num, 0);
  41. MODULE_PARM_DESC(init, "Init sequence");
  42. static unsigned int setaddrwin;
  43. module_param(setaddrwin, uint, 0);
  44. MODULE_PARM_DESC(setaddrwin, "Which set_addr_win() implementation to use");
  45. static unsigned int buswidth = 8;
  46. module_param(buswidth, uint, 0);
  47. MODULE_PARM_DESC(buswidth, "Width of databus (default: 8)");
  48. static unsigned int regwidth = 8;
  49. module_param(regwidth, uint, 0);
  50. MODULE_PARM_DESC(regwidth, "Width of controller register (default: 8)");
  51. static bool nobacklight;
  52. module_param(nobacklight, bool, 0);
  53. MODULE_PARM_DESC(nobacklight, "Turn off backlight functionality.");
  54. static bool latched;
  55. module_param(latched, bool, 0);
  56. MODULE_PARM_DESC(latched, "Use with latched 16-bit databus");
  57. static int *initp;
  58. static int initp_num;
  59. /* default init sequences */
  60. static int st7735r_init[] = {
  61. -1, 0x01, -2, 150, -1, 0x11, -2, 500, -1, 0xB1, 0x01, 0x2C, 0x2D, -1, 0xB2, 0x01, 0x2C, 0x2D, -1, 0xB3, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D,
  62. -1, 0xB4, 0x07, -1, 0xC0, 0xA2, 0x02, 0x84, -1, 0xC1, 0xC5, -1, 0xC2, 0x0A, 0x00, -1, 0xC3, 0x8A, 0x2A, -1, 0xC4, 0x8A, 0xEE, -1, 0xC5, 0x0E,
  63. -1, 0x20, -1, 0x36, 0xC0, -1, 0x3A, 0x05, -1, 0xE0, 0x0f, 0x1a, 0x0f, 0x18, 0x2f, 0x28, 0x20, 0x22, 0x1f, 0x1b, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10,
  64. -1, 0xE1, 0x0f, 0x1b, 0x0f, 0x17, 0x33, 0x2c, 0x29, 0x2e, 0x30, 0x30, 0x39, 0x3f, 0x00, 0x07, 0x03, 0x10, -1, 0x29, -2, 100, -1, 0x13, -2, 10, -3 };
  65. static int ssd1289_init[] = {
  66. -1, 0x00, 0x0001, -1, 0x03, 0xA8A4, -1, 0x0C, 0x0000, -1, 0x0D, 0x080C, -1, 0x0E, 0x2B00, -1, 0x1E, 0x00B7, -1, 0x01, 0x2B3F, -1, 0x02, 0x0600,
  67. -1, 0x10, 0x0000, -1, 0x11, 0x6070, -1, 0x05, 0x0000, -1, 0x06, 0x0000, -1, 0x16, 0xEF1C, -1, 0x17, 0x0003, -1, 0x07, 0x0233, -1, 0x0B, 0x0000,
  68. -1, 0x0F, 0x0000, -1, 0x41, 0x0000, -1, 0x42, 0x0000, -1, 0x48, 0x0000, -1, 0x49, 0x013F, -1, 0x4A, 0x0000, -1, 0x4B, 0x0000, -1, 0x44, 0xEF00,
  69. -1, 0x45, 0x0000, -1, 0x46, 0x013F, -1, 0x30, 0x0707, -1, 0x31, 0x0204, -1, 0x32, 0x0204, -1, 0x33, 0x0502, -1, 0x34, 0x0507, -1, 0x35, 0x0204,
  70. -1, 0x36, 0x0204, -1, 0x37, 0x0502, -1, 0x3A, 0x0302, -1, 0x3B, 0x0302, -1, 0x23, 0x0000, -1, 0x24, 0x0000, -1, 0x25, 0x8000, -1, 0x4f, 0x0000,
  71. -1, 0x4e, 0x0000, -1, 0x22, -3 };
  72. static int hx8340bn_init[] = {
  73. -1, 0xC1, 0xFF, 0x83, 0x40, -1, 0x11, -2, 150, -1, 0xCA, 0x70, 0x00, 0xD9, -1, 0xB0, 0x01, 0x11,
  74. -1, 0xC9, 0x90, 0x49, 0x10, 0x28, 0x28, 0x10, 0x00, 0x06, -2, 20, -1, 0xC2, 0x60, 0x71, 0x01, 0x0E, 0x05, 0x02, 0x09, 0x31, 0x0A,
  75. -1, 0xC3, 0x67, 0x30, 0x61, 0x17, 0x48, 0x07, 0x05, 0x33, -2, 10, -1, 0xB5, 0x35, 0x20, 0x45, -1, 0xB4, 0x33, 0x25, 0x4C, -2, 10,
  76. -1, 0x3A, 0x05, -1, 0x29, -2, 10, -3 };
  77. static int ili9225_init[] = {
  78. -1, 0x0001, 0x011C, -1, 0x0002, 0x0100, -1, 0x0003, 0x1030, -1, 0x0008, 0x0808, -1, 0x000C, 0x0000, -1, 0x000F, 0x0A01, -1, 0x0020, 0x0000,
  79. -1, 0x0021, 0x0000, -2, 50, -1, 0x0010, 0x0A00, -1, 0x0011, 0x1038, -2, 50, -1, 0x0012, 0x1121, -1, 0x0013, 0x004E, -1, 0x0014, 0x676F,
  80. -1, 0x0030, 0x0000, -1, 0x0031, 0x00DB, -1, 0x0032, 0x0000, -1, 0x0033, 0x0000, -1, 0x0034, 0x00DB, -1, 0x0035, 0x0000, -1, 0x0036, 0x00AF,
  81. -1, 0x0037, 0x0000, -1, 0x0038, 0x00DB, -1, 0x0039, 0x0000, -1, 0x0050, 0x0000, -1, 0x0051, 0x060A, -1, 0x0052, 0x0D0A, -1, 0x0053, 0x0303,
  82. -1, 0x0054, 0x0A0D, -1, 0x0055, 0x0A06, -1, 0x0056, 0x0000, -1, 0x0057, 0x0303, -1, 0x0058, 0x0000, -1, 0x0059, 0x0000, -2, 50,
  83. -1, 0x0007, 0x1017, -2, 50, -3 };
  84. static int ili9320_init[] = {
  85. -1, 0x00E5, 0x8000, -1, 0x0000, 0x0001, -1, 0x0001, 0x0100, -1, 0x0002, 0x0700, -1, 0x0003, 0x1030, -1, 0x0004, 0x0000, -1, 0x0008, 0x0202,
  86. -1, 0x0009, 0x0000, -1, 0x000A, 0x0000, -1, 0x000C, 0x0000, -1, 0x000D, 0x0000, -1, 0x000F, 0x0000, -1, 0x0010, 0x0000, -1, 0x0011, 0x0007,
  87. -1, 0x0012, 0x0000, -1, 0x0013, 0x0000, -2, 200, -1, 0x0010, 0x17B0, -1, 0x0011, 0x0031, -2, 50, -1, 0x0012, 0x0138, -2, 50, -1, 0x0013, 0x1800,
  88. -1, 0x0029, 0x0008, -2, 50, -1, 0x0020, 0x0000, -1, 0x0021, 0x0000, -1, 0x0030, 0x0000, -1, 0x0031, 0x0505, -1, 0x0032, 0x0004,
  89. -1, 0x0035, 0x0006, -1, 0x0036, 0x0707, -1, 0x0037, 0x0105, -1, 0x0038, 0x0002, -1, 0x0039, 0x0707, -1, 0x003C, 0x0704, -1, 0x003D, 0x0807,
  90. -1, 0x0050, 0x0000, -1, 0x0051, 0x00EF, -1, 0x0052, 0x0000, -1, 0x0053, 0x013F, -1, 0x0060, 0x2700, -1, 0x0061, 0x0001, -1, 0x006A, 0x0000,
  91. -1, 0x0080, 0x0000, -1, 0x0081, 0x0000, -1, 0x0082, 0x0000, -1, 0x0083, 0x0000, -1, 0x0084, 0x0000, -1, 0x0085, 0x0000, -1, 0x0090, 0x0010,
  92. -1, 0x0092, 0x0000, -1, 0x0093, 0x0003, -1, 0x0095, 0x0110, -1, 0x0097, 0x0000, -1, 0x0098, 0x0000, -1, 0x0007, 0x0173, -3 };
  93. static int ili9325_init[] = {
  94. -1, 0x00E3, 0x3008, -1, 0x00E7, 0x0012, -1, 0x00EF, 0x1231, -1, 0x0001, 0x0100, -1, 0x0002, 0x0700, -1, 0x0003, 0x1030, -1, 0x0004, 0x0000,
  95. -1, 0x0008, 0x0207, -1, 0x0009, 0x0000, -1, 0x000A, 0x0000, -1, 0x000C, 0x0000, -1, 0x000D, 0x0000, -1, 0x000F, 0x0000, -1, 0x0010, 0x0000,
  96. -1, 0x0011, 0x0007, -1, 0x0012, 0x0000, -1, 0x0013, 0x0000, -2, 200, -1, 0x0010, 0x1690, -1, 0x0011, 0x0223, -2, 50, -1, 0x0012, 0x000D, -2, 50,
  97. -1, 0x0013, 0x1200, -1, 0x0029, 0x000A, -1, 0x002B, 0x000C, -2, 50, -1, 0x0020, 0x0000, -1, 0x0021, 0x0000, -1, 0x0030, 0x0000,
  98. -1, 0x0031, 0x0506, -1, 0x0032, 0x0104, -1, 0x0035, 0x0207, -1, 0x0036, 0x000F, -1, 0x0037, 0x0306, -1, 0x0038, 0x0102, -1, 0x0039, 0x0707,
  99. -1, 0x003C, 0x0702, -1, 0x003D, 0x1604, -1, 0x0050, 0x0000, -1, 0x0051, 0x00EF, -1, 0x0052, 0x0000, -1, 0x0053, 0x013F, -1, 0x0060, 0xA700,
  100. -1, 0x0061, 0x0001, -1, 0x006A, 0x0000, -1, 0x0080, 0x0000, -1, 0x0081, 0x0000, -1, 0x0082, 0x0000, -1, 0x0083, 0x0000, -1, 0x0084, 0x0000,
  101. -1, 0x0085, 0x0000, -1, 0x0090, 0x0010, -1, 0x0092, 0x0600, -1, 0x0007, 0x0133, -3 };
  102. static int ili9341_init[] = {
  103. -1, 0x28, -2, 20, -1, 0xCF, 0x00, 0x83, 0x30, -1, 0xED, 0x64, 0x03, 0x12, 0x81, -1, 0xE8, 0x85, 0x01, 0x79,
  104. -1, 0xCB, 0x39, 0x2c, 0x00, 0x34, 0x02, -1, 0xF7, 0x20, -1, 0xEA, 0x00, 0x00, -1, 0xC0, 0x26, -1, 0xC1, 0x11,
  105. -1, 0xC5, 0x35, 0x3E, -1, 0xC7, 0xBE, -1, 0xB1, 0x00, 0x1B, -1, 0xB6, 0x0a, 0x82, 0x27, 0x00, -1, 0xB7, 0x07,
  106. -1, 0x3A, 0x55, -1, 0x36, 0x48, -1, 0x11, -2, 120, -1, 0x29, -2, 20, -3 };
  107. static int ssd1351_init[] = { -1, 0xfd, 0x12, -1, 0xfd, 0xb1, -1, 0xae, -1, 0xb3, 0xf1, -1, 0xca, 0x7f, -1, 0xa0, 0x74,
  108. -1, 0x15, 0x00, 0x7f, -1, 0x75, 0x00, 0x7f, -1, 0xa1, 0x00, -1, 0xa2, 0x00, -1, 0xb5, 0x00,
  109. -1, 0xab, 0x01, -1, 0xb1, 0x32, -1, 0xb4, 0xa0, 0xb5, 0x55, -1, 0xbb, 0x17, -1, 0xbe, 0x05,
  110. -1, 0xc1, 0xc8, 0x80, 0xc8, -1, 0xc7, 0x0f, -1, 0xb6, 0x01, -1, 0xa6, -1, 0xaf, -3 };
  111. /* ili9320, ili9325 */
  112. static void flexfb_set_addr_win_1(struct fbtft_par *par,
  113. int xs, int ys, int xe, int ye)
  114. {
  115. fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
  116. "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n",
  117. __func__, xs, ys, xe, ye);
  118. switch (par->info->var.rotate) {
  119. /* R20h = Horizontal GRAM Start Address */
  120. /* R21h = Vertical GRAM Start Address */
  121. case 0:
  122. write_reg(par, 0x0020, xs);
  123. write_reg(par, 0x0021, ys);
  124. break;
  125. case 180:
  126. write_reg(par, 0x0020, width - 1 - xs);
  127. write_reg(par, 0x0021, height - 1 - ys);
  128. break;
  129. case 270:
  130. write_reg(par, 0x0020, width - 1 - ys);
  131. write_reg(par, 0x0021, xs);
  132. break;
  133. case 90:
  134. write_reg(par, 0x0020, ys);
  135. write_reg(par, 0x0021, height - 1 - xs);
  136. break;
  137. }
  138. write_reg(par, 0x0022); /* Write Data to GRAM */
  139. }
  140. /* ssd1289 */
  141. static void flexfb_set_addr_win_2(struct fbtft_par *par,
  142. int xs, int ys, int xe, int ye)
  143. {
  144. fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
  145. "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n",
  146. __func__, xs, ys, xe, ye);
  147. switch (par->info->var.rotate) {
  148. /* R4Eh - Set GDDRAM X address counter */
  149. /* R4Fh - Set GDDRAM Y address counter */
  150. case 0:
  151. write_reg(par, 0x4e, xs);
  152. write_reg(par, 0x4f, ys);
  153. break;
  154. case 180:
  155. write_reg(par, 0x4e, par->info->var.xres - 1 - xs);
  156. write_reg(par, 0x4f, par->info->var.yres - 1 - ys);
  157. break;
  158. case 270:
  159. write_reg(par, 0x4e, par->info->var.yres - 1 - ys);
  160. write_reg(par, 0x4f, xs);
  161. break;
  162. case 90:
  163. write_reg(par, 0x4e, ys);
  164. write_reg(par, 0x4f, par->info->var.xres - 1 - xs);
  165. break;
  166. }
  167. /* R22h - RAM data write */
  168. write_reg(par, 0x22, 0);
  169. }
  170. /* ssd1351 */
  171. static void set_addr_win_3(struct fbtft_par *par,
  172. int xs, int ys, int xe, int ye)
  173. {
  174. fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
  175. "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__,
  176. xs, ys, xe, ye);
  177. write_reg(par, 0x15, xs, xe);
  178. write_reg(par, 0x75, ys, ye);
  179. write_reg(par, 0x5C);
  180. }
  181. static int flexfb_verify_gpios_dc(struct fbtft_par *par)
  182. {
  183. fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
  184. if (par->gpio.dc < 0) {
  185. dev_err(par->info->device,
  186. "Missing info about 'dc' gpio. Aborting.\n");
  187. return -EINVAL;
  188. }
  189. return 0;
  190. }
  191. static int flexfb_verify_gpios_db(struct fbtft_par *par)
  192. {
  193. int i;
  194. int num_db = buswidth;
  195. fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
  196. if (par->gpio.dc < 0) {
  197. dev_err(par->info->device, "Missing info about 'dc' gpio. Aborting.\n");
  198. return -EINVAL;
  199. }
  200. if (par->gpio.wr < 0) {
  201. dev_err(par->info->device, "Missing info about 'wr' gpio. Aborting.\n");
  202. return -EINVAL;
  203. }
  204. if (latched && (par->gpio.latch < 0)) {
  205. dev_err(par->info->device, "Missing info about 'latch' gpio. Aborting.\n");
  206. return -EINVAL;
  207. }
  208. if (latched)
  209. num_db = buswidth/2;
  210. for (i = 0; i < num_db; i++) {
  211. if (par->gpio.db[i] < 0) {
  212. dev_err(par->info->device,
  213. "Missing info about 'db%02d' gpio. Aborting.\n",
  214. i);
  215. return -EINVAL;
  216. }
  217. }
  218. return 0;
  219. }
  220. static struct fbtft_display flex_display = { };
  221. static int flexfb_probe_common(struct spi_device *sdev,
  222. struct platform_device *pdev)
  223. {
  224. struct device *dev;
  225. struct fb_info *info;
  226. struct fbtft_par *par;
  227. int ret;
  228. initp = init;
  229. initp_num = init_num;
  230. if (sdev)
  231. dev = &sdev->dev;
  232. else
  233. dev = &pdev->dev;
  234. fbtft_init_dbg(dev, "%s(%s)\n", __func__,
  235. sdev ? "'SPI device'" : "'Platform device'");
  236. if (chip) {
  237. if (!strcmp(chip, "st7735r")) {
  238. if (!width)
  239. width = 128;
  240. if (!height)
  241. height = 160;
  242. if (init_num == 0) {
  243. initp = st7735r_init;
  244. initp_num = ARRAY_SIZE(st7735r_init);
  245. }
  246. } else if (!strcmp(chip, "hx8340bn")) {
  247. if (!width)
  248. width = 176;
  249. if (!height)
  250. height = 220;
  251. setaddrwin = 0;
  252. if (init_num == 0) {
  253. initp = hx8340bn_init;
  254. initp_num = ARRAY_SIZE(hx8340bn_init);
  255. }
  256. } else if (!strcmp(chip, "ili9225")) {
  257. if (!width)
  258. width = 176;
  259. if (!height)
  260. height = 220;
  261. setaddrwin = 0;
  262. regwidth = 16;
  263. if (init_num == 0) {
  264. initp = ili9225_init;
  265. initp_num = ARRAY_SIZE(ili9225_init);
  266. }
  267. } else if (!strcmp(chip, "ili9320")) {
  268. if (!width)
  269. width = 240;
  270. if (!height)
  271. height = 320;
  272. setaddrwin = 1;
  273. regwidth = 16;
  274. if (init_num == 0) {
  275. initp = ili9320_init;
  276. initp_num = ARRAY_SIZE(ili9320_init);
  277. }
  278. } else if (!strcmp(chip, "ili9325")) {
  279. if (!width)
  280. width = 240;
  281. if (!height)
  282. height = 320;
  283. setaddrwin = 1;
  284. regwidth = 16;
  285. if (init_num == 0) {
  286. initp = ili9325_init;
  287. initp_num = ARRAY_SIZE(ili9325_init);
  288. }
  289. } else if (!strcmp(chip, "ili9341")) {
  290. if (!width)
  291. width = 240;
  292. if (!height)
  293. height = 320;
  294. setaddrwin = 0;
  295. regwidth = 8;
  296. if (init_num == 0) {
  297. initp = ili9341_init;
  298. initp_num = ARRAY_SIZE(ili9341_init);
  299. }
  300. } else if (!strcmp(chip, "ssd1289")) {
  301. if (!width)
  302. width = 240;
  303. if (!height)
  304. height = 320;
  305. setaddrwin = 2;
  306. regwidth = 16;
  307. if (init_num == 0) {
  308. initp = ssd1289_init;
  309. initp_num = ARRAY_SIZE(ssd1289_init);
  310. }
  311. } else if (!strcmp(chip, "ssd1351")) {
  312. if (!width)
  313. width = 128;
  314. if (!height)
  315. height = 128;
  316. setaddrwin = 3;
  317. if (init_num == 0) {
  318. initp = ssd1351_init;
  319. initp_num = ARRAY_SIZE(ssd1351_init);
  320. }
  321. } else {
  322. dev_err(dev, "chip=%s is not supported\n", chip);
  323. return -EINVAL;
  324. }
  325. }
  326. if (width == 0 || height == 0) {
  327. dev_err(dev, "argument(s) missing: width and height has to be set.\n");
  328. return -EINVAL;
  329. }
  330. flex_display.width = width;
  331. flex_display.height = height;
  332. fbtft_init_dbg(dev, "Display resolution: %dx%d\n", width, height);
  333. fbtft_init_dbg(dev, "chip = %s\n", chip ? chip : "not set");
  334. fbtft_init_dbg(dev, "setaddrwin = %d\n", setaddrwin);
  335. fbtft_init_dbg(dev, "regwidth = %d\n", regwidth);
  336. fbtft_init_dbg(dev, "buswidth = %d\n", buswidth);
  337. info = fbtft_framebuffer_alloc(&flex_display, dev);
  338. if (!info)
  339. return -ENOMEM;
  340. par = info->par;
  341. if (sdev)
  342. par->spi = sdev;
  343. else
  344. par->pdev = pdev;
  345. if (!par->init_sequence)
  346. par->init_sequence = initp;
  347. par->fbtftops.init_display = fbtft_init_display;
  348. /* registerwrite functions */
  349. switch (regwidth) {
  350. case 8:
  351. par->fbtftops.write_register = fbtft_write_reg8_bus8;
  352. break;
  353. case 16:
  354. par->fbtftops.write_register = fbtft_write_reg16_bus8;
  355. break;
  356. default:
  357. dev_err(dev,
  358. "argument 'regwidth': %d is not supported.\n",
  359. regwidth);
  360. return -EINVAL;
  361. }
  362. /* bus functions */
  363. if (sdev) {
  364. par->fbtftops.write = fbtft_write_spi;
  365. switch (buswidth) {
  366. case 8:
  367. par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
  368. if (!par->startbyte)
  369. par->fbtftops.verify_gpios = flexfb_verify_gpios_dc;
  370. break;
  371. case 9:
  372. if (regwidth == 16) {
  373. dev_err(dev, "argument 'regwidth': %d is not supported with buswidth=%d and SPI.\n", regwidth, buswidth);
  374. return -EINVAL;
  375. }
  376. par->fbtftops.write_register = fbtft_write_reg8_bus9;
  377. par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
  378. sdev->bits_per_word = 9;
  379. ret = sdev->master->setup(sdev);
  380. if (ret) {
  381. dev_warn(dev,
  382. "9-bit SPI not available, emulating using 8-bit.\n");
  383. sdev->bits_per_word = 8;
  384. ret = sdev->master->setup(sdev);
  385. if (ret)
  386. goto out_release;
  387. /* allocate buffer with room for dc bits */
  388. par->extra = devm_kzalloc(par->info->device,
  389. par->txbuf.len + (par->txbuf.len / 8) + 8,
  390. GFP_KERNEL);
  391. if (!par->extra) {
  392. ret = -ENOMEM;
  393. goto out_release;
  394. }
  395. par->fbtftops.write = fbtft_write_spi_emulate_9;
  396. }
  397. break;
  398. default:
  399. dev_err(dev, "argument 'buswidth': %d is not supported with SPI.\n", buswidth);
  400. return -EINVAL;
  401. }
  402. } else {
  403. par->fbtftops.verify_gpios = flexfb_verify_gpios_db;
  404. switch (buswidth) {
  405. case 8:
  406. par->fbtftops.write = fbtft_write_gpio8_wr;
  407. par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
  408. break;
  409. case 16:
  410. par->fbtftops.write_register = fbtft_write_reg16_bus16;
  411. if (latched)
  412. par->fbtftops.write = fbtft_write_gpio16_wr_latched;
  413. else
  414. par->fbtftops.write = fbtft_write_gpio16_wr;
  415. par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
  416. break;
  417. default:
  418. dev_err(dev, "argument 'buswidth': %d is not supported with parallel.\n", buswidth);
  419. return -EINVAL;
  420. }
  421. }
  422. /* set_addr_win function */
  423. switch (setaddrwin) {
  424. case 0:
  425. /* use default */
  426. break;
  427. case 1:
  428. par->fbtftops.set_addr_win = flexfb_set_addr_win_1;
  429. break;
  430. case 2:
  431. par->fbtftops.set_addr_win = flexfb_set_addr_win_2;
  432. break;
  433. case 3:
  434. par->fbtftops.set_addr_win = set_addr_win_3;
  435. break;
  436. default:
  437. dev_err(dev, "argument 'setaddrwin': unknown value %d.\n",
  438. setaddrwin);
  439. return -EINVAL;
  440. }
  441. if (!nobacklight)
  442. par->fbtftops.register_backlight = fbtft_register_backlight;
  443. ret = fbtft_register_framebuffer(info);
  444. if (ret < 0)
  445. goto out_release;
  446. return 0;
  447. out_release:
  448. fbtft_framebuffer_release(info);
  449. return ret;
  450. }
  451. static int flexfb_remove_common(struct device *dev, struct fb_info *info)
  452. {
  453. struct fbtft_par *par;
  454. if (!info)
  455. return -EINVAL;
  456. par = info->par;
  457. if (par)
  458. fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
  459. "%s()\n", __func__);
  460. fbtft_unregister_framebuffer(info);
  461. fbtft_framebuffer_release(info);
  462. return 0;
  463. }
  464. static int flexfb_probe_spi(struct spi_device *spi)
  465. {
  466. return flexfb_probe_common(spi, NULL);
  467. }
  468. static int flexfb_remove_spi(struct spi_device *spi)
  469. {
  470. struct fb_info *info = spi_get_drvdata(spi);
  471. return flexfb_remove_common(&spi->dev, info);
  472. }
  473. static int flexfb_probe_pdev(struct platform_device *pdev)
  474. {
  475. return flexfb_probe_common(NULL, pdev);
  476. }
  477. static int flexfb_remove_pdev(struct platform_device *pdev)
  478. {
  479. struct fb_info *info = platform_get_drvdata(pdev);
  480. return flexfb_remove_common(&pdev->dev, info);
  481. }
  482. static struct spi_driver flexfb_spi_driver = {
  483. .driver = {
  484. .name = DRVNAME,
  485. .owner = THIS_MODULE,
  486. },
  487. .probe = flexfb_probe_spi,
  488. .remove = flexfb_remove_spi,
  489. };
  490. static const struct platform_device_id flexfb_platform_ids[] = {
  491. { "flexpfb", 0 },
  492. { },
  493. };
  494. static struct platform_driver flexfb_platform_driver = {
  495. .driver = {
  496. .name = DRVNAME,
  497. },
  498. .id_table = flexfb_platform_ids,
  499. .probe = flexfb_probe_pdev,
  500. .remove = flexfb_remove_pdev,
  501. };
  502. static int __init flexfb_init(void)
  503. {
  504. int ret, ret2;
  505. ret = spi_register_driver(&flexfb_spi_driver);
  506. ret2 = platform_driver_register(&flexfb_platform_driver);
  507. if (ret < 0)
  508. return ret;
  509. return ret2;
  510. }
  511. static void __exit flexfb_exit(void)
  512. {
  513. spi_unregister_driver(&flexfb_spi_driver);
  514. platform_driver_unregister(&flexfb_platform_driver);
  515. }
  516. /* ------------------------------------------------------------------------- */
  517. module_init(flexfb_init);
  518. module_exit(flexfb_exit);
  519. MODULE_DESCRIPTION("Generic FB driver for TFT LCD displays");
  520. MODULE_AUTHOR("Noralf Tronnes");
  521. MODULE_LICENSE("GPL");