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