fbtft.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Copyright (C) 2013 Noralf Tronnes
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef __LINUX_FBTFT_H
  15. #define __LINUX_FBTFT_H
  16. #include <linux/fb.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/platform_device.h>
  20. #define FBTFT_ONBOARD_BACKLIGHT 2
  21. #define FBTFT_GPIO_NO_MATCH 0xFFFF
  22. #define FBTFT_GPIO_NAME_SIZE 32
  23. #define FBTFT_MAX_INIT_SEQUENCE 512
  24. #define FBTFT_GAMMA_MAX_VALUES_TOTAL 128
  25. #define FBTFT_OF_INIT_CMD BIT(24)
  26. #define FBTFT_OF_INIT_DELAY BIT(25)
  27. /**
  28. * struct fbtft_gpio - Structure that holds one pinname to gpio mapping
  29. * @name: pinname (reset, dc, etc.)
  30. * @gpio: GPIO number
  31. *
  32. */
  33. struct fbtft_gpio {
  34. char name[FBTFT_GPIO_NAME_SIZE];
  35. unsigned int gpio;
  36. };
  37. struct fbtft_par;
  38. /**
  39. * struct fbtft_ops - FBTFT operations structure
  40. * @write: Writes to interface bus
  41. * @read: Reads from interface bus
  42. * @write_vmem: Writes video memory to display
  43. * @write_reg: Writes to controller register
  44. * @set_addr_win: Set the GRAM update window
  45. * @reset: Reset the LCD controller
  46. * @mkdirty: Marks display lines for update
  47. * @update_display: Updates the display
  48. * @init_display: Initializes the display
  49. * @blank: Blank the display (optional)
  50. * @request_gpios_match: Do pinname to gpio matching
  51. * @request_gpios: Request gpios from the kernel
  52. * @free_gpios: Free previously requested gpios
  53. * @verify_gpios: Verify that necessary gpios is present (optional)
  54. * @register_backlight: Used to register backlight device (optional)
  55. * @unregister_backlight: Unregister backlight device (optional)
  56. * @set_var: Configure LCD with values from variables like @rotate and @bgr
  57. * (optional)
  58. * @set_gamma: Set Gamma curve (optional)
  59. *
  60. * Most of these operations have default functions assigned to them in
  61. * fbtft_framebuffer_alloc()
  62. */
  63. struct fbtft_ops {
  64. int (*write)(struct fbtft_par *par, void *buf, size_t len);
  65. int (*read)(struct fbtft_par *par, void *buf, size_t len);
  66. int (*write_vmem)(struct fbtft_par *par, size_t offset, size_t len);
  67. void (*write_register)(struct fbtft_par *par, int len, ...);
  68. void (*set_addr_win)(struct fbtft_par *par,
  69. int xs, int ys, int xe, int ye);
  70. void (*reset)(struct fbtft_par *par);
  71. void (*mkdirty)(struct fb_info *info, int from, int to);
  72. void (*update_display)(struct fbtft_par *par,
  73. unsigned int start_line, unsigned int end_line);
  74. int (*init_display)(struct fbtft_par *par);
  75. int (*blank)(struct fbtft_par *par, bool on);
  76. unsigned long (*request_gpios_match)(struct fbtft_par *par,
  77. const struct fbtft_gpio *gpio);
  78. int (*request_gpios)(struct fbtft_par *par);
  79. int (*verify_gpios)(struct fbtft_par *par);
  80. void (*register_backlight)(struct fbtft_par *par);
  81. void (*unregister_backlight)(struct fbtft_par *par);
  82. int (*set_var)(struct fbtft_par *par);
  83. int (*set_gamma)(struct fbtft_par *par, unsigned long *curves);
  84. };
  85. /**
  86. * struct fbtft_display - Describes the display properties
  87. * @width: Width of display in pixels
  88. * @height: Height of display in pixels
  89. * @regwidth: LCD Controller Register width in bits
  90. * @buswidth: Display interface bus width in bits
  91. * @backlight: Backlight type.
  92. * @fbtftops: FBTFT operations provided by driver or device (platform_data)
  93. * @bpp: Bits per pixel
  94. * @fps: Frames per second
  95. * @txbuflen: Size of transmit buffer
  96. * @init_sequence: Pointer to LCD initialization array
  97. * @gamma: String representation of Gamma curve(s)
  98. * @gamma_num: Number of Gamma curves
  99. * @gamma_len: Number of values per Gamma curve
  100. * @debug: Initial debug value
  101. *
  102. * This structure is not stored by FBTFT except for init_sequence.
  103. */
  104. struct fbtft_display {
  105. unsigned int width;
  106. unsigned int height;
  107. unsigned int regwidth;
  108. unsigned int buswidth;
  109. unsigned int backlight;
  110. struct fbtft_ops fbtftops;
  111. unsigned int bpp;
  112. unsigned int fps;
  113. int txbuflen;
  114. int *init_sequence;
  115. char *gamma;
  116. int gamma_num;
  117. int gamma_len;
  118. unsigned long debug;
  119. };
  120. /**
  121. * struct fbtft_platform_data - Passes display specific data to the driver
  122. * @display: Display properties
  123. * @gpios: Pointer to an array of pinname to gpio mappings
  124. * @rotate: Display rotation angle
  125. * @bgr: LCD Controller BGR bit
  126. * @fps: Frames per second (this will go away, use @fps in @fbtft_display)
  127. * @txbuflen: Size of transmit buffer
  128. * @startbyte: When set, enables use of Startbyte in transfers
  129. * @gamma: String representation of Gamma curve(s)
  130. * @extra: A way to pass extra info
  131. */
  132. struct fbtft_platform_data {
  133. struct fbtft_display display;
  134. const struct fbtft_gpio *gpios;
  135. unsigned int rotate;
  136. bool bgr;
  137. unsigned int fps;
  138. int txbuflen;
  139. u8 startbyte;
  140. char *gamma;
  141. void *extra;
  142. };
  143. /**
  144. * struct fbtft_par - Main FBTFT data structure
  145. *
  146. * This structure holds all relevant data to operate the display
  147. *
  148. * See sourcefile for documentation since nested structs is not
  149. * supported by kernel-doc.
  150. *
  151. */
  152. /* @spi: Set if it is a SPI device
  153. * @pdev: Set if it is a platform device
  154. * @info: Pointer to framebuffer fb_info structure
  155. * @pdata: Pointer to platform data
  156. * @ssbuf: Not used
  157. * @pseudo_palette: Used by fb_set_colreg()
  158. * @txbuf.buf: Transmit buffer
  159. * @txbuf.len: Transmit buffer length
  160. * @buf: Small buffer used when writing init data over SPI
  161. * @startbyte: Used by some controllers when in SPI mode.
  162. * Format: 6 bit Device id + RS bit + RW bit
  163. * @fbtftops: FBTFT operations provided by driver or device (platform_data)
  164. * @dirty_lock: Protects dirty_lines_start and dirty_lines_end
  165. * @dirty_lines_start: Where to begin updating display
  166. * @dirty_lines_end: Where to end updating display
  167. * @gpio.reset: GPIO used to reset display
  168. * @gpio.dc: Data/Command signal, also known as RS
  169. * @gpio.rd: Read latching signal
  170. * @gpio.wr: Write latching signal
  171. * @gpio.latch: Bus latch signal, eg. 16->8 bit bus latch
  172. * @gpio.cs: LCD Chip Select with parallel interface bus
  173. * @gpio.db[16]: Parallel databus
  174. * @gpio.led[16]: Led control signals
  175. * @gpio.aux[16]: Auxiliary signals, not used by core
  176. * @init_sequence: Pointer to LCD initialization array
  177. * @gamma.lock: Mutex for Gamma curve locking
  178. * @gamma.curves: Pointer to Gamma curve array
  179. * @gamma.num_values: Number of values per Gamma curve
  180. * @gamma.num_curves: Number of Gamma curves
  181. * @debug: Pointer to debug value
  182. * @current_debug:
  183. * @first_update_done: Used to only time the first display update
  184. * @update_time: Used to calculate 'fps' in debug output
  185. * @bgr: BGR mode/\n
  186. * @extra: Extra info needed by driver
  187. */
  188. struct fbtft_par {
  189. struct spi_device *spi;
  190. struct platform_device *pdev;
  191. struct fb_info *info;
  192. struct fbtft_platform_data *pdata;
  193. u16 *ssbuf;
  194. u32 pseudo_palette[16];
  195. struct {
  196. void *buf;
  197. dma_addr_t dma;
  198. size_t len;
  199. } txbuf;
  200. u8 *buf;
  201. u8 startbyte;
  202. struct fbtft_ops fbtftops;
  203. spinlock_t dirty_lock;
  204. unsigned int dirty_lines_start;
  205. unsigned int dirty_lines_end;
  206. struct {
  207. int reset;
  208. int dc;
  209. int rd;
  210. int wr;
  211. int latch;
  212. int cs;
  213. int db[16];
  214. int led[16];
  215. int aux[16];
  216. } gpio;
  217. int *init_sequence;
  218. struct {
  219. struct mutex lock;
  220. unsigned long *curves;
  221. int num_values;
  222. int num_curves;
  223. } gamma;
  224. unsigned long debug;
  225. bool first_update_done;
  226. ktime_t update_time;
  227. bool bgr;
  228. void *extra;
  229. };
  230. #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int))
  231. #define write_reg(par, ...) \
  232. par->fbtftops.write_register(par, NUMARGS(__VA_ARGS__), __VA_ARGS__)
  233. /* fbtft-core.c */
  234. void fbtft_dbg_hex(const struct device *dev, int groupsize,
  235. void *buf, size_t len, const char *fmt, ...);
  236. struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
  237. struct device *dev,
  238. struct fbtft_platform_data *pdata);
  239. void fbtft_framebuffer_release(struct fb_info *info);
  240. int fbtft_register_framebuffer(struct fb_info *fb_info);
  241. int fbtft_unregister_framebuffer(struct fb_info *fb_info);
  242. void fbtft_register_backlight(struct fbtft_par *par);
  243. void fbtft_unregister_backlight(struct fbtft_par *par);
  244. int fbtft_init_display(struct fbtft_par *par);
  245. int fbtft_probe_common(struct fbtft_display *display, struct spi_device *sdev,
  246. struct platform_device *pdev);
  247. int fbtft_remove_common(struct device *dev, struct fb_info *info);
  248. /* fbtft-io.c */
  249. int fbtft_write_spi(struct fbtft_par *par, void *buf, size_t len);
  250. int fbtft_write_spi_emulate_9(struct fbtft_par *par, void *buf, size_t len);
  251. int fbtft_read_spi(struct fbtft_par *par, void *buf, size_t len);
  252. int fbtft_write_gpio8_wr(struct fbtft_par *par, void *buf, size_t len);
  253. int fbtft_write_gpio16_wr(struct fbtft_par *par, void *buf, size_t len);
  254. int fbtft_write_gpio16_wr_latched(struct fbtft_par *par, void *buf, size_t len);
  255. /* fbtft-bus.c */
  256. int fbtft_write_vmem8_bus8(struct fbtft_par *par, size_t offset, size_t len);
  257. int fbtft_write_vmem16_bus16(struct fbtft_par *par, size_t offset, size_t len);
  258. int fbtft_write_vmem16_bus8(struct fbtft_par *par, size_t offset, size_t len);
  259. int fbtft_write_vmem16_bus9(struct fbtft_par *par, size_t offset, size_t len);
  260. void fbtft_write_reg8_bus8(struct fbtft_par *par, int len, ...);
  261. void fbtft_write_reg8_bus9(struct fbtft_par *par, int len, ...);
  262. void fbtft_write_reg16_bus8(struct fbtft_par *par, int len, ...);
  263. void fbtft_write_reg16_bus16(struct fbtft_par *par, int len, ...);
  264. #define FBTFT_REGISTER_DRIVER(_name, _compatible, _display) \
  265. \
  266. static int fbtft_driver_probe_spi(struct spi_device *spi) \
  267. { \
  268. return fbtft_probe_common(_display, spi, NULL); \
  269. } \
  270. \
  271. static int fbtft_driver_remove_spi(struct spi_device *spi) \
  272. { \
  273. struct fb_info *info = spi_get_drvdata(spi); \
  274. \
  275. return fbtft_remove_common(&spi->dev, info); \
  276. } \
  277. \
  278. static int fbtft_driver_probe_pdev(struct platform_device *pdev) \
  279. { \
  280. return fbtft_probe_common(_display, NULL, pdev); \
  281. } \
  282. \
  283. static int fbtft_driver_remove_pdev(struct platform_device *pdev) \
  284. { \
  285. struct fb_info *info = platform_get_drvdata(pdev); \
  286. \
  287. return fbtft_remove_common(&pdev->dev, info); \
  288. } \
  289. \
  290. static const struct of_device_id dt_ids[] = { \
  291. { .compatible = _compatible }, \
  292. {}, \
  293. }; \
  294. \
  295. MODULE_DEVICE_TABLE(of, dt_ids); \
  296. \
  297. \
  298. static struct spi_driver fbtft_driver_spi_driver = { \
  299. .driver = { \
  300. .name = _name, \
  301. .of_match_table = of_match_ptr(dt_ids), \
  302. }, \
  303. .probe = fbtft_driver_probe_spi, \
  304. .remove = fbtft_driver_remove_spi, \
  305. }; \
  306. \
  307. static struct platform_driver fbtft_driver_platform_driver = { \
  308. .driver = { \
  309. .name = _name, \
  310. .owner = THIS_MODULE, \
  311. .of_match_table = of_match_ptr(dt_ids), \
  312. }, \
  313. .probe = fbtft_driver_probe_pdev, \
  314. .remove = fbtft_driver_remove_pdev, \
  315. }; \
  316. \
  317. static int __init fbtft_driver_module_init(void) \
  318. { \
  319. int ret; \
  320. \
  321. ret = spi_register_driver(&fbtft_driver_spi_driver); \
  322. if (ret < 0) \
  323. return ret; \
  324. return platform_driver_register(&fbtft_driver_platform_driver); \
  325. } \
  326. \
  327. static void __exit fbtft_driver_module_exit(void) \
  328. { \
  329. spi_unregister_driver(&fbtft_driver_spi_driver); \
  330. platform_driver_unregister(&fbtft_driver_platform_driver); \
  331. } \
  332. \
  333. module_init(fbtft_driver_module_init); \
  334. module_exit(fbtft_driver_module_exit);
  335. /* Debug macros */
  336. /* shorthand debug levels */
  337. #define DEBUG_LEVEL_1 DEBUG_REQUEST_GPIOS
  338. #define DEBUG_LEVEL_2 (DEBUG_LEVEL_1 | DEBUG_DRIVER_INIT_FUNCTIONS | DEBUG_TIME_FIRST_UPDATE)
  339. #define DEBUG_LEVEL_3 (DEBUG_LEVEL_2 | DEBUG_RESET | DEBUG_INIT_DISPLAY | DEBUG_BLANK | DEBUG_REQUEST_GPIOS | DEBUG_FREE_GPIOS | DEBUG_VERIFY_GPIOS | DEBUG_BACKLIGHT | DEBUG_SYSFS)
  340. #define DEBUG_LEVEL_4 (DEBUG_LEVEL_2 | DEBUG_FB_READ | DEBUG_FB_WRITE | DEBUG_FB_FILLRECT | DEBUG_FB_COPYAREA | DEBUG_FB_IMAGEBLIT | DEBUG_FB_BLANK)
  341. #define DEBUG_LEVEL_5 (DEBUG_LEVEL_3 | DEBUG_UPDATE_DISPLAY)
  342. #define DEBUG_LEVEL_6 (DEBUG_LEVEL_4 | DEBUG_LEVEL_5)
  343. #define DEBUG_LEVEL_7 0xFFFFFFFF
  344. #define DEBUG_DRIVER_INIT_FUNCTIONS (1<<3)
  345. #define DEBUG_TIME_FIRST_UPDATE (1<<4)
  346. #define DEBUG_TIME_EACH_UPDATE (1<<5)
  347. #define DEBUG_DEFERRED_IO (1<<6)
  348. #define DEBUG_FBTFT_INIT_FUNCTIONS (1<<7)
  349. /* fbops */
  350. #define DEBUG_FB_READ (1<<8)
  351. #define DEBUG_FB_WRITE (1<<9)
  352. #define DEBUG_FB_FILLRECT (1<<10)
  353. #define DEBUG_FB_COPYAREA (1<<11)
  354. #define DEBUG_FB_IMAGEBLIT (1<<12)
  355. #define DEBUG_FB_SETCOLREG (1<<13)
  356. #define DEBUG_FB_BLANK (1<<14)
  357. #define DEBUG_SYSFS (1<<16)
  358. /* fbtftops */
  359. #define DEBUG_BACKLIGHT (1<<17)
  360. #define DEBUG_READ (1<<18)
  361. #define DEBUG_WRITE (1<<19)
  362. #define DEBUG_WRITE_VMEM (1<<20)
  363. #define DEBUG_WRITE_REGISTER (1<<21)
  364. #define DEBUG_SET_ADDR_WIN (1<<22)
  365. #define DEBUG_RESET (1<<23)
  366. #define DEBUG_MKDIRTY (1<<24)
  367. #define DEBUG_UPDATE_DISPLAY (1<<25)
  368. #define DEBUG_INIT_DISPLAY (1<<26)
  369. #define DEBUG_BLANK (1<<27)
  370. #define DEBUG_REQUEST_GPIOS (1<<28)
  371. #define DEBUG_FREE_GPIOS (1<<29)
  372. #define DEBUG_REQUEST_GPIOS_MATCH (1<<30)
  373. #define DEBUG_VERIFY_GPIOS (1<<31)
  374. #define fbtft_init_dbg(dev, format, arg...) \
  375. do { \
  376. if (unlikely((dev)->platform_data && \
  377. (((struct fbtft_platform_data *)(dev)->platform_data)->display.debug & DEBUG_DRIVER_INIT_FUNCTIONS))) \
  378. dev_info(dev, format, ##arg); \
  379. } while (0)
  380. #define fbtft_par_dbg(level, par, format, arg...) \
  381. do { \
  382. if (unlikely(par->debug & level)) \
  383. dev_info(par->info->device, format, ##arg); \
  384. } while (0)
  385. #define fbtft_par_dbg_hex(level, par, dev, type, buf, num, format, arg...) \
  386. do { \
  387. if (unlikely(par->debug & level)) \
  388. fbtft_dbg_hex(dev, sizeof(type), buf, num * sizeof(type), format, ##arg); \
  389. } while (0)
  390. #endif /* __LINUX_FBTFT_H */