flexfb.c 18 KB

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