2 * Copyright (C) 2013 Noralf Tronnes
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.
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.
15 #ifndef __LINUX_FBTFT_H
16 #define __LINUX_FBTFT_H
19 #include <linux/spinlock.h>
20 #include <linux/spi/spi.h>
21 #include <linux/platform_device.h>
23 #define FBTFT_NOP 0x00
24 #define FBTFT_SWRESET 0x01
25 #define FBTFT_RDDID 0x04
26 #define FBTFT_RDDST 0x09
27 #define FBTFT_CASET 0x2A
28 #define FBTFT_RASET 0x2B
29 #define FBTFT_RAMWR 0x2C
31 #define FBTFT_ONBOARD_BACKLIGHT 2
33 #define FBTFT_GPIO_NO_MATCH 0xFFFF
34 #define FBTFT_GPIO_NAME_SIZE 32
35 #define FBTFT_MAX_INIT_SEQUENCE 512
36 #define FBTFT_GAMMA_MAX_VALUES_TOTAL 128
38 #define FBTFT_OF_INIT_CMD BIT(24)
39 #define FBTFT_OF_INIT_DELAY BIT(25)
42 * struct fbtft_gpio - Structure that holds one pinname to gpio mapping
43 * @name: pinname (reset, dc, etc.)
48 char name
[FBTFT_GPIO_NAME_SIZE
];
55 * struct fbtft_ops - FBTFT operations structure
56 * @write: Writes to interface bus
57 * @read: Reads from interface bus
58 * @write_vmem: Writes video memory to display
59 * @write_reg: Writes to controller register
60 * @set_addr_win: Set the GRAM update window
61 * @reset: Reset the LCD controller
62 * @mkdirty: Marks display lines for update
63 * @update_display: Updates the display
64 * @init_display: Initializes the display
65 * @blank: Blank the display (optional)
66 * @request_gpios_match: Do pinname to gpio matching
67 * @request_gpios: Request gpios from the kernel
68 * @free_gpios: Free previously requested gpios
69 * @verify_gpios: Verify that necessary gpios is present (optional)
70 * @register_backlight: Used to register backlight device (optional)
71 * @unregister_backlight: Unregister backlight device (optional)
72 * @set_var: Configure LCD with values from variables like @rotate and @bgr
74 * @set_gamma: Set Gamma curve (optional)
76 * Most of these operations have default functions assigned to them in
77 * fbtft_framebuffer_alloc()
80 int (*write
)(struct fbtft_par
*par
, void *buf
, size_t len
);
81 int (*read
)(struct fbtft_par
*par
, void *buf
, size_t len
);
82 int (*write_vmem
)(struct fbtft_par
*par
, size_t offset
, size_t len
);
83 void (*write_register
)(struct fbtft_par
*par
, int len
, ...);
85 void (*set_addr_win
)(struct fbtft_par
*par
,
86 int xs
, int ys
, int xe
, int ye
);
87 void (*reset
)(struct fbtft_par
*par
);
88 void (*mkdirty
)(struct fb_info
*info
, int from
, int to
);
89 void (*update_display
)(struct fbtft_par
*par
,
90 unsigned start_line
, unsigned end_line
);
91 int (*init_display
)(struct fbtft_par
*par
);
92 int (*blank
)(struct fbtft_par
*par
, bool on
);
94 unsigned long (*request_gpios_match
)(struct fbtft_par
*par
,
95 const struct fbtft_gpio
*gpio
);
96 int (*request_gpios
)(struct fbtft_par
*par
);
97 int (*verify_gpios
)(struct fbtft_par
*par
);
99 void (*register_backlight
)(struct fbtft_par
*par
);
100 void (*unregister_backlight
)(struct fbtft_par
*par
);
102 int (*set_var
)(struct fbtft_par
*par
);
103 int (*set_gamma
)(struct fbtft_par
*par
, unsigned long *curves
);
107 * struct fbtft_display - Describes the display properties
108 * @width: Width of display in pixels
109 * @height: Height of display in pixels
110 * @regwidth: LCD Controller Register width in bits
111 * @buswidth: Display interface bus width in bits
112 * @backlight: Backlight type.
113 * @fbtftops: FBTFT operations provided by driver or device (platform_data)
114 * @bpp: Bits per pixel
115 * @fps: Frames per second
116 * @txbuflen: Size of transmit buffer
117 * @init_sequence: Pointer to LCD initialization array
118 * @gamma: String representation of Gamma curve(s)
119 * @gamma_num: Number of Gamma curves
120 * @gamma_len: Number of values per Gamma curve
121 * @debug: Initial debug value
123 * This structure is not stored by FBTFT except for init_sequence.
125 struct fbtft_display
{
131 struct fbtft_ops fbtftops
;
143 * struct fbtft_platform_data - Passes display specific data to the driver
144 * @display: Display properties
145 * @gpios: Pointer to an array of pinname to gpio mappings
146 * @rotate: Display rotation angle
147 * @bgr: LCD Controller BGR bit
148 * @fps: Frames per second (this will go away, use @fps in @fbtft_display)
149 * @txbuflen: Size of transmit buffer
150 * @startbyte: When set, enables use of Startbyte in transfers
151 * @gamma: String representation of Gamma curve(s)
152 * @extra: A way to pass extra info
154 struct fbtft_platform_data
{
155 struct fbtft_display display
;
156 const struct fbtft_gpio
*gpios
;
167 * struct fbtft_par - Main FBTFT data structure
169 * This structure holds all relevant data to operate the display
171 * See sourcefile for documentation since nested structs is not
172 * supported by kernel-doc.
175 /* @spi: Set if it is a SPI device
176 * @pdev: Set if it is a platform device
177 * @info: Pointer to framebuffer fb_info structure
178 * @pdata: Pointer to platform data
180 * @pseudo_palette: Used by fb_set_colreg()
181 * @txbuf.buf: Transmit buffer
182 * @txbuf.len: Transmit buffer length
183 * @buf: Small buffer used when writing init data over SPI
184 * @startbyte: Used by some controllers when in SPI mode.
185 * Format: 6 bit Device id + RS bit + RW bit
186 * @fbtftops: FBTFT operations provided by driver or device (platform_data)
187 * @dirty_lock: Protects dirty_lines_start and dirty_lines_end
188 * @dirty_lines_start: Where to begin updating display
189 * @dirty_lines_end: Where to end updating display
190 * @gpio.reset: GPIO used to reset display
191 * @gpio.dc: Data/Command signal, also known as RS
192 * @gpio.rd: Read latching signal
193 * @gpio.wr: Write latching signal
194 * @gpio.latch: Bus latch signal, eg. 16->8 bit bus latch
195 * @gpio.cs: LCD Chip Select with parallel interface bus
196 * @gpio.db[16]: Parallel databus
197 * @gpio.led[16]: Led control signals
198 * @gpio.aux[16]: Auxiliary signals, not used by core
199 * @init_sequence: Pointer to LCD initialization array
200 * @gamma.lock: Mutex for Gamma curve locking
201 * @gamma.curves: Pointer to Gamma curve array
202 * @gamma.num_values: Number of values per Gamma curve
203 * @gamma.num_curves: Number of Gamma curves
204 * @debug: Pointer to debug value
206 * @first_update_done: Used to only time the first display update
207 * @update_time: Used to calculate 'fps' in debug output
209 * @extra: Extra info needed by driver
212 struct spi_device
*spi
;
213 struct platform_device
*pdev
;
214 struct fb_info
*info
;
215 struct fbtft_platform_data
*pdata
;
217 u32 pseudo_palette
[16];
225 struct fbtft_ops fbtftops
;
226 spinlock_t dirty_lock
;
227 unsigned dirty_lines_start
;
228 unsigned dirty_lines_end
;
243 unsigned long *curves
;
248 bool first_update_done
;
254 #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int))
256 #define write_reg(par, ...) \
257 par->fbtftops.write_register(par, NUMARGS(__VA_ARGS__), __VA_ARGS__)
260 void fbtft_dbg_hex(const struct device
*dev
, int groupsize
,
261 void *buf
, size_t len
, const char *fmt
, ...);
262 struct fb_info
*fbtft_framebuffer_alloc(struct fbtft_display
*display
,
264 struct fbtft_platform_data
*pdata
);
265 void fbtft_framebuffer_release(struct fb_info
*info
);
266 int fbtft_register_framebuffer(struct fb_info
*fb_info
);
267 int fbtft_unregister_framebuffer(struct fb_info
*fb_info
);
268 void fbtft_register_backlight(struct fbtft_par
*par
);
269 void fbtft_unregister_backlight(struct fbtft_par
*par
);
270 int fbtft_init_display(struct fbtft_par
*par
);
271 int fbtft_probe_common(struct fbtft_display
*display
, struct spi_device
*sdev
,
272 struct platform_device
*pdev
);
273 int fbtft_remove_common(struct device
*dev
, struct fb_info
*info
);
276 int fbtft_write_spi(struct fbtft_par
*par
, void *buf
, size_t len
);
277 int fbtft_write_spi_emulate_9(struct fbtft_par
*par
, void *buf
, size_t len
);
278 int fbtft_read_spi(struct fbtft_par
*par
, void *buf
, size_t len
);
279 int fbtft_write_gpio8_wr(struct fbtft_par
*par
, void *buf
, size_t len
);
280 int fbtft_write_gpio16_wr(struct fbtft_par
*par
, void *buf
, size_t len
);
281 int fbtft_write_gpio16_wr_latched(struct fbtft_par
*par
, void *buf
, size_t len
);
284 int fbtft_write_vmem8_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
);
285 int fbtft_write_vmem16_bus16(struct fbtft_par
*par
, size_t offset
, size_t len
);
286 int fbtft_write_vmem16_bus8(struct fbtft_par
*par
, size_t offset
, size_t len
);
287 int fbtft_write_vmem16_bus9(struct fbtft_par
*par
, size_t offset
, size_t len
);
288 void fbtft_write_reg8_bus8(struct fbtft_par
*par
, int len
, ...);
289 void fbtft_write_reg8_bus9(struct fbtft_par
*par
, int len
, ...);
290 void fbtft_write_reg16_bus8(struct fbtft_par
*par
, int len
, ...);
291 void fbtft_write_reg16_bus16(struct fbtft_par
*par
, int len
, ...);
293 #define FBTFT_REGISTER_DRIVER(_name, _compatible, _display) \
295 static int fbtft_driver_probe_spi(struct spi_device *spi) \
297 return fbtft_probe_common(_display, spi, NULL); \
300 static int fbtft_driver_remove_spi(struct spi_device *spi) \
302 struct fb_info *info = spi_get_drvdata(spi); \
304 return fbtft_remove_common(&spi->dev, info); \
307 static int fbtft_driver_probe_pdev(struct platform_device *pdev) \
309 return fbtft_probe_common(_display, NULL, pdev); \
312 static int fbtft_driver_remove_pdev(struct platform_device *pdev) \
314 struct fb_info *info = platform_get_drvdata(pdev); \
316 return fbtft_remove_common(&pdev->dev, info); \
319 static const struct of_device_id dt_ids[] = { \
320 { .compatible = _compatible }, \
324 MODULE_DEVICE_TABLE(of, dt_ids); \
327 static struct spi_driver fbtft_driver_spi_driver = { \
330 .of_match_table = of_match_ptr(dt_ids), \
332 .probe = fbtft_driver_probe_spi, \
333 .remove = fbtft_driver_remove_spi, \
336 static struct platform_driver fbtft_driver_platform_driver = { \
339 .owner = THIS_MODULE, \
340 .of_match_table = of_match_ptr(dt_ids), \
342 .probe = fbtft_driver_probe_pdev, \
343 .remove = fbtft_driver_remove_pdev, \
346 static int __init fbtft_driver_module_init(void) \
350 ret = spi_register_driver(&fbtft_driver_spi_driver); \
353 return platform_driver_register(&fbtft_driver_platform_driver); \
356 static void __exit fbtft_driver_module_exit(void) \
358 spi_unregister_driver(&fbtft_driver_spi_driver); \
359 platform_driver_unregister(&fbtft_driver_platform_driver); \
362 module_init(fbtft_driver_module_init); \
363 module_exit(fbtft_driver_module_exit);
367 /* shorthand debug levels */
368 #define DEBUG_LEVEL_1 DEBUG_REQUEST_GPIOS
369 #define DEBUG_LEVEL_2 (DEBUG_LEVEL_1 | DEBUG_DRIVER_INIT_FUNCTIONS | DEBUG_TIME_FIRST_UPDATE)
370 #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)
371 #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)
372 #define DEBUG_LEVEL_5 (DEBUG_LEVEL_3 | DEBUG_UPDATE_DISPLAY)
373 #define DEBUG_LEVEL_6 (DEBUG_LEVEL_4 | DEBUG_LEVEL_5)
374 #define DEBUG_LEVEL_7 0xFFFFFFFF
376 #define DEBUG_DRIVER_INIT_FUNCTIONS (1<<3)
377 #define DEBUG_TIME_FIRST_UPDATE (1<<4)
378 #define DEBUG_TIME_EACH_UPDATE (1<<5)
379 #define DEBUG_DEFERRED_IO (1<<6)
380 #define DEBUG_FBTFT_INIT_FUNCTIONS (1<<7)
383 #define DEBUG_FB_READ (1<<8)
384 #define DEBUG_FB_WRITE (1<<9)
385 #define DEBUG_FB_FILLRECT (1<<10)
386 #define DEBUG_FB_COPYAREA (1<<11)
387 #define DEBUG_FB_IMAGEBLIT (1<<12)
388 #define DEBUG_FB_SETCOLREG (1<<13)
389 #define DEBUG_FB_BLANK (1<<14)
391 #define DEBUG_SYSFS (1<<16)
394 #define DEBUG_BACKLIGHT (1<<17)
395 #define DEBUG_READ (1<<18)
396 #define DEBUG_WRITE (1<<19)
397 #define DEBUG_WRITE_VMEM (1<<20)
398 #define DEBUG_WRITE_REGISTER (1<<21)
399 #define DEBUG_SET_ADDR_WIN (1<<22)
400 #define DEBUG_RESET (1<<23)
401 #define DEBUG_MKDIRTY (1<<24)
402 #define DEBUG_UPDATE_DISPLAY (1<<25)
403 #define DEBUG_INIT_DISPLAY (1<<26)
404 #define DEBUG_BLANK (1<<27)
405 #define DEBUG_REQUEST_GPIOS (1<<28)
406 #define DEBUG_FREE_GPIOS (1<<29)
407 #define DEBUG_REQUEST_GPIOS_MATCH (1<<30)
408 #define DEBUG_VERIFY_GPIOS (1<<31)
410 #define fbtft_init_dbg(dev, format, arg...) \
412 if (unlikely((dev)->platform_data && \
413 (((struct fbtft_platform_data *)(dev)->platform_data)->display.debug & DEBUG_DRIVER_INIT_FUNCTIONS))) \
414 dev_info(dev, format, ##arg); \
417 #define fbtft_par_dbg(level, par, format, arg...) \
419 if (unlikely(par->debug & level)) \
420 dev_info(par->info->device, format, ##arg); \
423 #define fbtft_par_dbg_hex(level, par, dev, type, buf, num, format, arg...) \
425 if (unlikely(par->debug & level)) \
426 fbtft_dbg_hex(dev, sizeof(type), buf, num * sizeof(type), format, ##arg); \
429 #endif /* __LINUX_FBTFT_H */