1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef __LINUX_FBTFT_H
16#define __LINUX_FBTFT_H
17
18#include <linux/fb.h>
19#include <linux/spinlock.h>
20#include <linux/spi/spi.h>
21#include <linux/platform_device.h>
22
23#define FBTFT_ONBOARD_BACKLIGHT 2
24
25#define FBTFT_GPIO_NO_MATCH 0xFFFF
26#define FBTFT_GPIO_NAME_SIZE 32
27#define FBTFT_MAX_INIT_SEQUENCE 512
28#define FBTFT_GAMMA_MAX_VALUES_TOTAL 128
29
30#define FBTFT_OF_INIT_CMD BIT(24)
31#define FBTFT_OF_INIT_DELAY BIT(25)
32
33
34
35
36
37
38
39struct fbtft_gpio {
40 char name[FBTFT_GPIO_NAME_SIZE];
41 unsigned int gpio;
42};
43
44struct fbtft_par;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71struct fbtft_ops {
72 int (*write)(struct fbtft_par *par, void *buf, size_t len);
73 int (*read)(struct fbtft_par *par, void *buf, size_t len);
74 int (*write_vmem)(struct fbtft_par *par, size_t offset, size_t len);
75 void (*write_register)(struct fbtft_par *par, int len, ...);
76
77 void (*set_addr_win)(struct fbtft_par *par,
78 int xs, int ys, int xe, int ye);
79 void (*reset)(struct fbtft_par *par);
80 void (*mkdirty)(struct fb_info *info, int from, int to);
81 void (*update_display)(struct fbtft_par *par,
82 unsigned int start_line, unsigned int end_line);
83 int (*init_display)(struct fbtft_par *par);
84 int (*blank)(struct fbtft_par *par, bool on);
85
86 unsigned long (*request_gpios_match)(struct fbtft_par *par,
87 const struct fbtft_gpio *gpio);
88 int (*request_gpios)(struct fbtft_par *par);
89 int (*verify_gpios)(struct fbtft_par *par);
90
91 void (*register_backlight)(struct fbtft_par *par);
92 void (*unregister_backlight)(struct fbtft_par *par);
93
94 int (*set_var)(struct fbtft_par *par);
95 int (*set_gamma)(struct fbtft_par *par, u32 *curves);
96};
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117struct fbtft_display {
118 unsigned int width;
119 unsigned int height;
120 unsigned int regwidth;
121 unsigned int buswidth;
122 unsigned int backlight;
123 struct fbtft_ops fbtftops;
124 unsigned int bpp;
125 unsigned int fps;
126 int txbuflen;
127 const s16 *init_sequence;
128 char *gamma;
129 int gamma_num;
130 int gamma_len;
131 unsigned long debug;
132};
133
134
135
136
137
138
139
140
141
142
143
144
145
146struct fbtft_platform_data {
147 struct fbtft_display display;
148 const struct fbtft_gpio *gpios;
149 unsigned int rotate;
150 bool bgr;
151 unsigned int fps;
152 int txbuflen;
153 u8 startbyte;
154 char *gamma;
155 void *extra;
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
191
192
193
194
195
196
197
198
199
200
201
202
203struct fbtft_par {
204 struct spi_device *spi;
205 struct platform_device *pdev;
206 struct fb_info *info;
207 struct fbtft_platform_data *pdata;
208 u16 *ssbuf;
209 u32 pseudo_palette[16];
210 struct {
211 void *buf;
212 size_t len;
213 } txbuf;
214 u8 *buf;
215 u8 startbyte;
216 struct fbtft_ops fbtftops;
217 spinlock_t dirty_lock;
218 unsigned int dirty_lines_start;
219 unsigned int dirty_lines_end;
220 struct {
221 int reset;
222 int dc;
223 int rd;
224 int wr;
225 int latch;
226 int cs;
227 int db[16];
228 int led[16];
229 int aux[16];
230 } gpio;
231 const s16 *init_sequence;
232 struct {
233 struct mutex lock;
234 u32 *curves;
235 int num_values;
236 int num_curves;
237 } gamma;
238 unsigned long debug;
239 bool first_update_done;
240 ktime_t update_time;
241 bool bgr;
242 void *extra;
243};
244
245#define NUMARGS(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int))
246
247#define write_reg(par, ...) \
248 par->fbtftops.write_register(par, NUMARGS(__VA_ARGS__), __VA_ARGS__)
249
250
251int fbtft_write_buf_dc(struct fbtft_par *par, void *buf, size_t len, int dc);
252void fbtft_dbg_hex(const struct device *dev, int groupsize,
253 void *buf, size_t len, const char *fmt, ...);
254struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
255 struct device *dev,
256 struct fbtft_platform_data *pdata);
257void fbtft_framebuffer_release(struct fb_info *info);
258int fbtft_register_framebuffer(struct fb_info *fb_info);
259int fbtft_unregister_framebuffer(struct fb_info *fb_info);
260void fbtft_register_backlight(struct fbtft_par *par);
261void fbtft_unregister_backlight(struct fbtft_par *par);
262int fbtft_init_display(struct fbtft_par *par);
263int fbtft_probe_common(struct fbtft_display *display, struct spi_device *sdev,
264 struct platform_device *pdev);
265int fbtft_remove_common(struct device *dev, struct fb_info *info);
266
267
268int fbtft_write_spi(struct fbtft_par *par, void *buf, size_t len);
269int fbtft_write_spi_emulate_9(struct fbtft_par *par, void *buf, size_t len);
270int fbtft_read_spi(struct fbtft_par *par, void *buf, size_t len);
271int fbtft_write_gpio8_wr(struct fbtft_par *par, void *buf, size_t len);
272int fbtft_write_gpio16_wr(struct fbtft_par *par, void *buf, size_t len);
273int fbtft_write_gpio16_wr_latched(struct fbtft_par *par, void *buf, size_t len);
274
275
276int fbtft_write_vmem8_bus8(struct fbtft_par *par, size_t offset, size_t len);
277int fbtft_write_vmem16_bus16(struct fbtft_par *par, size_t offset, size_t len);
278int fbtft_write_vmem16_bus8(struct fbtft_par *par, size_t offset, size_t len);
279int fbtft_write_vmem16_bus9(struct fbtft_par *par, size_t offset, size_t len);
280void fbtft_write_reg8_bus8(struct fbtft_par *par, int len, ...);
281void fbtft_write_reg8_bus9(struct fbtft_par *par, int len, ...);
282void fbtft_write_reg16_bus8(struct fbtft_par *par, int len, ...);
283void fbtft_write_reg16_bus16(struct fbtft_par *par, int len, ...);
284
285#define FBTFT_REGISTER_DRIVER(_name, _compatible, _display) \
286 \
287static int fbtft_driver_probe_spi(struct spi_device *spi) \
288{ \
289 return fbtft_probe_common(_display, spi, NULL); \
290} \
291 \
292static int fbtft_driver_remove_spi(struct spi_device *spi) \
293{ \
294 struct fb_info *info = spi_get_drvdata(spi); \
295 \
296 return fbtft_remove_common(&spi->dev, info); \
297} \
298 \
299static int fbtft_driver_probe_pdev(struct platform_device *pdev) \
300{ \
301 return fbtft_probe_common(_display, NULL, pdev); \
302} \
303 \
304static int fbtft_driver_remove_pdev(struct platform_device *pdev) \
305{ \
306 struct fb_info *info = platform_get_drvdata(pdev); \
307 \
308 return fbtft_remove_common(&pdev->dev, info); \
309} \
310 \
311static const struct of_device_id dt_ids[] = { \
312 { .compatible = _compatible }, \
313 {}, \
314}; \
315 \
316MODULE_DEVICE_TABLE(of, dt_ids); \
317 \
318 \
319static struct spi_driver fbtft_driver_spi_driver = { \
320 .driver = { \
321 .name = _name, \
322 .of_match_table = of_match_ptr(dt_ids), \
323 }, \
324 .probe = fbtft_driver_probe_spi, \
325 .remove = fbtft_driver_remove_spi, \
326}; \
327 \
328static struct platform_driver fbtft_driver_platform_driver = { \
329 .driver = { \
330 .name = _name, \
331 .owner = THIS_MODULE, \
332 .of_match_table = of_match_ptr(dt_ids), \
333 }, \
334 .probe = fbtft_driver_probe_pdev, \
335 .remove = fbtft_driver_remove_pdev, \
336}; \
337 \
338static int __init fbtft_driver_module_init(void) \
339{ \
340 int ret; \
341 \
342 ret = spi_register_driver(&fbtft_driver_spi_driver); \
343 if (ret < 0) \
344 return ret; \
345 return platform_driver_register(&fbtft_driver_platform_driver); \
346} \
347 \
348static void __exit fbtft_driver_module_exit(void) \
349{ \
350 spi_unregister_driver(&fbtft_driver_spi_driver); \
351 platform_driver_unregister(&fbtft_driver_platform_driver); \
352} \
353 \
354module_init(fbtft_driver_module_init); \
355module_exit(fbtft_driver_module_exit);
356
357
358
359
360#define DEBUG_LEVEL_1 DEBUG_REQUEST_GPIOS
361#define DEBUG_LEVEL_2 (DEBUG_LEVEL_1 | DEBUG_DRIVER_INIT_FUNCTIONS | DEBUG_TIME_FIRST_UPDATE)
362#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)
363#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)
364#define DEBUG_LEVEL_5 (DEBUG_LEVEL_3 | DEBUG_UPDATE_DISPLAY)
365#define DEBUG_LEVEL_6 (DEBUG_LEVEL_4 | DEBUG_LEVEL_5)
366#define DEBUG_LEVEL_7 0xFFFFFFFF
367
368#define DEBUG_DRIVER_INIT_FUNCTIONS (1<<3)
369#define DEBUG_TIME_FIRST_UPDATE (1<<4)
370#define DEBUG_TIME_EACH_UPDATE (1<<5)
371#define DEBUG_DEFERRED_IO (1<<6)
372#define DEBUG_FBTFT_INIT_FUNCTIONS (1<<7)
373
374
375#define DEBUG_FB_READ (1<<8)
376#define DEBUG_FB_WRITE (1<<9)
377#define DEBUG_FB_FILLRECT (1<<10)
378#define DEBUG_FB_COPYAREA (1<<11)
379#define DEBUG_FB_IMAGEBLIT (1<<12)
380#define DEBUG_FB_SETCOLREG (1<<13)
381#define DEBUG_FB_BLANK (1<<14)
382
383#define DEBUG_SYSFS (1<<16)
384
385
386#define DEBUG_BACKLIGHT (1<<17)
387#define DEBUG_READ (1<<18)
388#define DEBUG_WRITE (1<<19)
389#define DEBUG_WRITE_VMEM (1<<20)
390#define DEBUG_WRITE_REGISTER (1<<21)
391#define DEBUG_SET_ADDR_WIN (1<<22)
392#define DEBUG_RESET (1<<23)
393#define DEBUG_MKDIRTY (1<<24)
394#define DEBUG_UPDATE_DISPLAY (1<<25)
395#define DEBUG_INIT_DISPLAY (1<<26)
396#define DEBUG_BLANK (1<<27)
397#define DEBUG_REQUEST_GPIOS (1<<28)
398#define DEBUG_FREE_GPIOS (1<<29)
399#define DEBUG_REQUEST_GPIOS_MATCH (1<<30)
400#define DEBUG_VERIFY_GPIOS (1<<31)
401
402#define fbtft_init_dbg(dev, format, arg...) \
403do { \
404 if (unlikely((dev)->platform_data && \
405 (((struct fbtft_platform_data *)(dev)->platform_data)->display.debug & DEBUG_DRIVER_INIT_FUNCTIONS))) \
406 dev_info(dev, format, ##arg); \
407} while (0)
408
409#define fbtft_par_dbg(level, par, format, arg...) \
410do { \
411 if (unlikely(par->debug & level)) \
412 dev_info(par->info->device, format, ##arg); \
413} while (0)
414
415#define fbtft_par_dbg_hex(level, par, dev, type, buf, num, format, arg...) \
416do { \
417 if (unlikely(par->debug & level)) \
418 fbtft_dbg_hex(dev, sizeof(type), buf, num * sizeof(type), format, ##arg); \
419} while (0)
420
421#endif
422