You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
16 KiB

  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. struct gpio_desc *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. unsigned int rotate;
  124. bool bgr;
  125. unsigned int fps;
  126. int txbuflen;
  127. u8 startbyte;
  128. char *gamma;
  129. void *extra;
  130. };
  131. /**
  132. * struct fbtft_par - Main FBTFT data structure
  133. *
  134. * This structure holds all relevant data to operate the display
  135. *
  136. * See sourcefile for documentation since nested structs is not
  137. * supported by kernel-doc.
  138. *
  139. */
  140. /* @spi: Set if it is a SPI device
  141. * @pdev: Set if it is a platform device
  142. * @info: Pointer to framebuffer fb_info structure
  143. * @pdata: Pointer to platform data
  144. * @ssbuf: Not used
  145. * @pseudo_palette: Used by fb_set_colreg()
  146. * @txbuf.buf: Transmit buffer
  147. * @txbuf.len: Transmit buffer length
  148. * @buf: Small buffer used when writing init data over SPI
  149. * @startbyte: Used by some controllers when in SPI mode.
  150. * Format: 6 bit Device id + RS bit + RW bit
  151. * @fbtftops: FBTFT operations provided by driver or device (platform_data)
  152. * @dirty_lock: Protects dirty_lines_start and dirty_lines_end
  153. * @dirty_lines_start: Where to begin updating display
  154. * @dirty_lines_end: Where to end updating display
  155. * @gpio.reset: GPIO used to reset display
  156. * @gpio.dc: Data/Command signal, also known as RS
  157. * @gpio.rd: Read latching signal
  158. * @gpio.wr: Write latching signal
  159. * @gpio.latch: Bus latch signal, eg. 16->8 bit bus latch
  160. * @gpio.cs: LCD Chip Select with parallel interface bus
  161. * @gpio.db[16]: Parallel databus
  162. * @gpio.led[16]: Led control signals
  163. * @gpio.aux[16]: Auxiliary signals, not used by core
  164. * @init_sequence: Pointer to LCD initialization array
  165. * @gamma.lock: Mutex for Gamma curve locking
  166. * @gamma.curves: Pointer to Gamma curve array
  167. * @gamma.num_values: Number of values per Gamma curve
  168. * @gamma.num_curves: Number of Gamma curves
  169. * @debug: Pointer to debug value
  170. * @current_debug:
  171. * @first_update_done: Used to only time the first display update
  172. * @update_time: Used to calculate 'fps' in debug output
  173. * @bgr: BGR mode/\n
  174. * @extra: Extra info needed by driver
  175. */
  176. struct fbtft_par {
  177. struct spi_device *spi;
  178. struct platform_device *pdev;
  179. struct fb_info *info;
  180. struct fbtft_platform_data *pdata;
  181. u16 *ssbuf;
  182. u32 pseudo_palette[16];
  183. struct {
  184. void *buf;
  185. size_t len;
  186. } txbuf;
  187. u8 *buf;
  188. u8 startbyte;
  189. struct fbtft_ops fbtftops;
  190. spinlock_t dirty_lock;
  191. unsigned int dirty_lines_start;
  192. unsigned int dirty_lines_end;
  193. struct {
  194. struct gpio_desc *reset;
  195. struct gpio_desc *dc;
  196. struct gpio_desc *rd;
  197. struct gpio_desc *wr;
  198. struct gpio_desc *latch;
  199. struct gpio_desc *cs;
  200. struct gpio_desc *db[16];
  201. struct gpio_desc *led[16];
  202. struct gpio_desc *aux[16];
  203. } gpio;
  204. const s16 *init_sequence;
  205. struct {
  206. struct mutex lock;
  207. u32 *curves;
  208. int num_values;
  209. int num_curves;
  210. } gamma;
  211. unsigned long debug;
  212. bool first_update_done;
  213. ktime_t update_time;
  214. bool bgr;
  215. void *extra;
  216. bool polarity;
  217. };
  218. #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
  219. #define write_reg(par, ...) \
  220. ((par)->fbtftops.write_register(par, NUMARGS(__VA_ARGS__), __VA_ARGS__))
  221. /* fbtft-core.c */
  222. int fbtft_write_buf_dc(struct fbtft_par *par, void *buf, size_t len, int dc);
  223. __printf(5, 6)
  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 BIT(3)
  335. #define DEBUG_TIME_FIRST_UPDATE BIT(4)
  336. #define DEBUG_TIME_EACH_UPDATE BIT(5)
  337. #define DEBUG_DEFERRED_IO BIT(6)
  338. #define DEBUG_FBTFT_INIT_FUNCTIONS BIT(7)
  339. /* fbops */
  340. #define DEBUG_FB_READ BIT(8)
  341. #define DEBUG_FB_WRITE BIT(9)
  342. #define DEBUG_FB_FILLRECT BIT(10)
  343. #define DEBUG_FB_COPYAREA BIT(11)
  344. #define DEBUG_FB_IMAGEBLIT BIT(12)
  345. #define DEBUG_FB_SETCOLREG BIT(13)
  346. #define DEBUG_FB_BLANK BIT(14)
  347. #define DEBUG_SYSFS BIT(16)
  348. /* fbtftops */
  349. #define DEBUG_BACKLIGHT BIT(17)
  350. #define DEBUG_READ BIT(18)
  351. #define DEBUG_WRITE BIT(19)
  352. #define DEBUG_WRITE_VMEM BIT(20)
  353. #define DEBUG_WRITE_REGISTER BIT(21)
  354. #define DEBUG_SET_ADDR_WIN BIT(22)
  355. #define DEBUG_RESET BIT(23)
  356. #define DEBUG_MKDIRTY BIT(24)
  357. #define DEBUG_UPDATE_DISPLAY BIT(25)
  358. #define DEBUG_INIT_DISPLAY BIT(26)
  359. #define DEBUG_BLANK BIT(27)
  360. #define DEBUG_REQUEST_GPIOS BIT(28)
  361. #define DEBUG_FREE_GPIOS BIT(29)
  362. #define DEBUG_REQUEST_GPIOS_MATCH BIT(30)
  363. #define DEBUG_VERIFY_GPIOS BIT(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 */