1
2
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
24
25
26
27
28struct fbtft_gpio {
29 char name[FBTFT_GPIO_NAME_SIZE];
30 struct gpio_desc *gpio;
31};
32
33struct fbtft_par;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60struct 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106struct 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
125
126
127
128
129
130
131
132
133
134
135struct 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191struct 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_t dirty_lock;
206 unsigned int dirty_lines_start;
207 unsigned int dirty_lines_end;
208 struct {
209 struct gpio_desc *reset;
210 struct gpio_desc *dc;
211 struct gpio_desc *rd;
212 struct gpio_desc *wr;
213 struct gpio_desc *latch;
214 struct gpio_desc *cs;
215 struct gpio_desc *db[16];
216 struct gpio_desc *led[16];
217 struct gpio_desc *aux[16];
218 } gpio;
219 const s16 *init_sequence;
220 struct {
221 struct mutex lock;
222 u32 *curves;
223 int num_values;
224 int num_curves;
225 } gamma;
226 unsigned long debug;
227 bool first_update_done;
228 ktime_t update_time;
229 bool bgr;
230 void *extra;
231 bool polarity;
232};
233
234#define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
235
236#define write_reg(par, ...) \
237 ((par)->fbtftops.write_register(par, NUMARGS(__VA_ARGS__), __VA_ARGS__))
238
239
240int fbtft_write_buf_dc(struct fbtft_par *par, void *buf, size_t len, int dc);
241__printf(5, 6)
242void fbtft_dbg_hex(const struct device *dev, int groupsize,
243 void *buf, size_t len, const char *fmt, ...);
244struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
245 struct device *dev,
246 struct fbtft_platform_data *pdata);
247void fbtft_framebuffer_release(struct fb_info *info);
248int fbtft_register_framebuffer(struct fb_info *fb_info);
249int fbtft_unregister_framebuffer(struct fb_info *fb_info);
250void fbtft_register_backlight(struct fbtft_par *par);
251void fbtft_unregister_backlight(struct fbtft_par *par);
252int fbtft_init_display(struct fbtft_par *par);
253int fbtft_probe_common(struct fbtft_display *display, struct spi_device *sdev,
254 struct platform_device *pdev);
255int fbtft_remove_common(struct device *dev, struct fb_info *info);
256
257
258int fbtft_write_spi(struct fbtft_par *par, void *buf, size_t len);
259int fbtft_write_spi_emulate_9(struct fbtft_par *par, void *buf, size_t len);
260int fbtft_read_spi(struct fbtft_par *par, void *buf, size_t len);
261int fbtft_write_gpio8_wr(struct fbtft_par *par, void *buf, size_t len);
262int fbtft_write_gpio16_wr(struct fbtft_par *par, void *buf, size_t len);
263int fbtft_write_gpio16_wr_latched(struct fbtft_par *par, void *buf, size_t len);
264
265
266int fbtft_write_vmem8_bus8(struct fbtft_par *par, size_t offset, size_t len);
267int fbtft_write_vmem16_bus16(struct fbtft_par *par, size_t offset, size_t len);
268int fbtft_write_vmem16_bus8(struct fbtft_par *par, size_t offset, size_t len);
269int fbtft_write_vmem16_bus9(struct fbtft_par *par, size_t offset, size_t len);
270void fbtft_write_reg8_bus8(struct fbtft_par *par, int len, ...);
271void fbtft_write_reg8_bus9(struct fbtft_par *par, int len, ...);
272void fbtft_write_reg16_bus8(struct fbtft_par *par, int len, ...);
273void fbtft_write_reg16_bus16(struct fbtft_par *par, int len, ...);
274
275#define FBTFT_REGISTER_DRIVER(_name, _compatible, _display) \
276 \
277static int fbtft_driver_probe_spi(struct spi_device *spi) \
278{ \
279 return fbtft_probe_common(_display, spi, NULL); \
280} \
281 \
282static int fbtft_driver_remove_spi(struct spi_device *spi) \
283{ \
284 struct fb_info *info = spi_get_drvdata(spi); \
285 \
286 return fbtft_remove_common(&spi->dev, info); \
287} \
288 \
289static int fbtft_driver_probe_pdev(struct platform_device *pdev) \
290{ \
291 return fbtft_probe_common(_display, NULL, pdev); \
292} \
293 \
294static int fbtft_driver_remove_pdev(struct platform_device *pdev) \
295{ \
296 struct fb_info *info = platform_get_drvdata(pdev); \
297 \
298 return fbtft_remove_common(&pdev->dev, info); \
299} \
300 \
301static const struct of_device_id dt_ids[] = { \
302 { .compatible = _compatible }, \
303 {}, \
304}; \
305 \
306MODULE_DEVICE_TABLE(of, dt_ids); \
307 \
308 \
309static struct spi_driver fbtft_driver_spi_driver = { \
310 .driver = { \
311 .name = _name, \
312 .of_match_table = dt_ids, \
313 }, \
314 .probe = fbtft_driver_probe_spi, \
315 .remove = fbtft_driver_remove_spi, \
316}; \
317 \
318static struct platform_driver fbtft_driver_platform_driver = { \
319 .driver = { \
320 .name = _name, \
321 .owner = THIS_MODULE, \
322 .of_match_table = dt_ids, \
323 }, \
324 .probe = fbtft_driver_probe_pdev, \
325 .remove = fbtft_driver_remove_pdev, \
326}; \
327 \
328static int __init fbtft_driver_module_init(void) \
329{ \
330 int ret; \
331 \
332 ret = spi_register_driver(&fbtft_driver_spi_driver); \
333 if (ret < 0) \
334 return ret; \
335 return platform_driver_register(&fbtft_driver_platform_driver); \
336} \
337 \
338static void __exit fbtft_driver_module_exit(void) \
339{ \
340 spi_unregister_driver(&fbtft_driver_spi_driver); \
341 platform_driver_unregister(&fbtft_driver_platform_driver); \
342} \
343 \
344module_init(fbtft_driver_module_init); \
345module_exit(fbtft_driver_module_exit);
346
347
348
349
350#define DEBUG_LEVEL_1 DEBUG_REQUEST_GPIOS
351#define DEBUG_LEVEL_2 (DEBUG_LEVEL_1 | DEBUG_DRIVER_INIT_FUNCTIONS \
352 | DEBUG_TIME_FIRST_UPDATE)
353#define DEBUG_LEVEL_3 (DEBUG_LEVEL_2 | DEBUG_RESET | DEBUG_INIT_DISPLAY \
354 | DEBUG_BLANK | DEBUG_REQUEST_GPIOS \
355 | DEBUG_FREE_GPIOS \
356 | DEBUG_VERIFY_GPIOS \
357 | DEBUG_BACKLIGHT | DEBUG_SYSFS)
358#define DEBUG_LEVEL_4 (DEBUG_LEVEL_2 | DEBUG_FB_READ | DEBUG_FB_WRITE \
359 | DEBUG_FB_FILLRECT \
360 | DEBUG_FB_COPYAREA \
361 | DEBUG_FB_IMAGEBLIT | DEBUG_FB_BLANK)
362#define DEBUG_LEVEL_5 (DEBUG_LEVEL_3 | DEBUG_UPDATE_DISPLAY)
363#define DEBUG_LEVEL_6 (DEBUG_LEVEL_4 | DEBUG_LEVEL_5)
364#define DEBUG_LEVEL_7 0xFFFFFFFF
365
366#define DEBUG_DRIVER_INIT_FUNCTIONS BIT(3)
367#define DEBUG_TIME_FIRST_UPDATE BIT(4)
368#define DEBUG_TIME_EACH_UPDATE BIT(5)
369#define DEBUG_DEFERRED_IO BIT(6)
370#define DEBUG_FBTFT_INIT_FUNCTIONS BIT(7)
371
372
373#define DEBUG_FB_READ BIT(8)
374#define DEBUG_FB_WRITE BIT(9)
375#define DEBUG_FB_FILLRECT BIT(10)
376#define DEBUG_FB_COPYAREA BIT(11)
377#define DEBUG_FB_IMAGEBLIT BIT(12)
378#define DEBUG_FB_SETCOLREG BIT(13)
379#define DEBUG_FB_BLANK BIT(14)
380
381#define DEBUG_SYSFS BIT(16)
382
383
384#define DEBUG_BACKLIGHT BIT(17)
385#define DEBUG_READ BIT(18)
386#define DEBUG_WRITE BIT(19)
387#define DEBUG_WRITE_VMEM BIT(20)
388#define DEBUG_WRITE_REGISTER BIT(21)
389#define DEBUG_SET_ADDR_WIN BIT(22)
390#define DEBUG_RESET BIT(23)
391#define DEBUG_MKDIRTY BIT(24)
392#define DEBUG_UPDATE_DISPLAY BIT(25)
393#define DEBUG_INIT_DISPLAY BIT(26)
394#define DEBUG_BLANK BIT(27)
395#define DEBUG_REQUEST_GPIOS BIT(28)
396#define DEBUG_FREE_GPIOS BIT(29)
397#define DEBUG_REQUEST_GPIOS_MATCH BIT(30)
398#define DEBUG_VERIFY_GPIOS BIT(31)
399
400#define fbtft_init_dbg(dev, format, arg...) \
401do { \
402 if (unlikely((dev)->platform_data && \
403 (((struct fbtft_platform_data *)(dev)->platform_data)->display.debug & DEBUG_DRIVER_INIT_FUNCTIONS))) \
404 dev_info(dev, format, ##arg); \
405} while (0)
406
407#define fbtft_par_dbg(level, par, format, arg...) \
408do { \
409 if (unlikely((par)->debug & (level))) \
410 dev_info((par)->info->device, format, ##arg); \
411} while (0)
412
413#define fbtft_par_dbg_hex(level, par, dev, type, buf, num, format, arg...) \
414do { \
415 if (unlikely((par)->debug & (level))) \
416 fbtft_dbg_hex(dev, sizeof(type), buf,\
417 (num) * sizeof(type), format, ##arg); \
418} while (0)
419
420#endif
421