fbtft.h 16 KB

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