fbtft.h 17 KB

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