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