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 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_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 30 31 #define FBTFT_ONBOARD_BACKLIGHT 2 32 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 37 38 #define FBTFT_OF_INIT_CMD BIT(24) 39 #define FBTFT_OF_INIT_DELAY BIT(25) 40 41 /** 42 * struct fbtft_gpio - Structure that holds one pinname to gpio mapping 43 * @name: pinname (reset, dc, etc.) 44 * @gpio: GPIO number 45 * 46 */ 47 struct fbtft_gpio { 48 char name[FBTFT_GPIO_NAME_SIZE]; 49 unsigned gpio; 50 }; 51 52 struct fbtft_par; 53 54 /** 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 73 * (optional) 74 * @set_gamma: Set Gamma curve (optional) 75 * 76 * Most of these operations have default functions assigned to them in 77 * fbtft_framebuffer_alloc() 78 */ 79 struct fbtft_ops { 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, ...); 84 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); 93 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); 98 99 void (*register_backlight)(struct fbtft_par *par); 100 void (*unregister_backlight)(struct fbtft_par *par); 101 102 int (*set_var)(struct fbtft_par *par); 103 int (*set_gamma)(struct fbtft_par *par, unsigned long *curves); 104 }; 105 106 /** 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 122 * 123 * This structure is not stored by FBTFT except for init_sequence. 124 */ 125 struct fbtft_display { 126 unsigned width; 127 unsigned height; 128 unsigned regwidth; 129 unsigned buswidth; 130 unsigned backlight; 131 struct fbtft_ops fbtftops; 132 unsigned bpp; 133 unsigned fps; 134 int txbuflen; 135 int *init_sequence; 136 char *gamma; 137 int gamma_num; 138 int gamma_len; 139 unsigned long debug; 140 }; 141 142 /** 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 153 */ 154 struct fbtft_platform_data { 155 struct fbtft_display display; 156 const struct fbtft_gpio *gpios; 157 unsigned rotate; 158 bool bgr; 159 unsigned fps; 160 int txbuflen; 161 u8 startbyte; 162 char *gamma; 163 void *extra; 164 }; 165 166 /** 167 * struct fbtft_par - Main FBTFT data structure 168 * 169 * This structure holds all relevant data to operate the display 170 * 171 * See sourcefile for documentation since nested structs is not 172 * supported by kernel-doc. 173 * 174 */ 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 179 * @ssbuf: Not used 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 205 * @current_debug: 206 * @first_update_done: Used to only time the first display update 207 * @update_time: Used to calculate 'fps' in debug output 208 * @bgr: BGR mode/\n 209 * @extra: Extra info needed by driver 210 */ 211 struct fbtft_par { 212 struct spi_device *spi; 213 struct platform_device *pdev; 214 struct fb_info *info; 215 struct fbtft_platform_data *pdata; 216 u16 *ssbuf; 217 u32 pseudo_palette[16]; 218 struct { 219 void *buf; 220 dma_addr_t dma; 221 size_t len; 222 } txbuf; 223 u8 *buf; 224 u8 startbyte; 225 struct fbtft_ops fbtftops; 226 spinlock_t dirty_lock; 227 unsigned dirty_lines_start; 228 unsigned dirty_lines_end; 229 struct { 230 int reset; 231 int dc; 232 int rd; 233 int wr; 234 int latch; 235 int cs; 236 int db[16]; 237 int led[16]; 238 int aux[16]; 239 } gpio; 240 int *init_sequence; 241 struct { 242 struct mutex lock; 243 unsigned long *curves; 244 int num_values; 245 int num_curves; 246 } gamma; 247 unsigned long debug; 248 bool first_update_done; 249 ktime_t update_time; 250 bool bgr; 251 void *extra; 252 }; 253 254 #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int)) 255 256 #define write_reg(par, ...) \ 257 par->fbtftops.write_register(par, NUMARGS(__VA_ARGS__), __VA_ARGS__) 258 259 /* fbtft-core.c */ 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, 263 struct device *dev, 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); 274 275 /* fbtft-io.c */ 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); 282 283 /* fbtft-bus.c */ 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, ...); 292 293 #define FBTFT_REGISTER_DRIVER(_name, _compatible, _display) \ 294 \ 295 static int fbtft_driver_probe_spi(struct spi_device *spi) \ 296 { \ 297 return fbtft_probe_common(_display, spi, NULL); \ 298 } \ 299 \ 300 static int fbtft_driver_remove_spi(struct spi_device *spi) \ 301 { \ 302 struct fb_info *info = spi_get_drvdata(spi); \ 303 \ 304 return fbtft_remove_common(&spi->dev, info); \ 305 } \ 306 \ 307 static int fbtft_driver_probe_pdev(struct platform_device *pdev) \ 308 { \ 309 return fbtft_probe_common(_display, NULL, pdev); \ 310 } \ 311 \ 312 static int fbtft_driver_remove_pdev(struct platform_device *pdev) \ 313 { \ 314 struct fb_info *info = platform_get_drvdata(pdev); \ 315 \ 316 return fbtft_remove_common(&pdev->dev, info); \ 317 } \ 318 \ 319 static const struct of_device_id dt_ids[] = { \ 320 { .compatible = _compatible }, \ 321 {}, \ 322 }; \ 323 \ 324 MODULE_DEVICE_TABLE(of, dt_ids); \ 325 \ 326 \ 327 static struct spi_driver fbtft_driver_spi_driver = { \ 328 .driver = { \ 329 .name = _name, \ 330 .of_match_table = of_match_ptr(dt_ids), \ 331 }, \ 332 .probe = fbtft_driver_probe_spi, \ 333 .remove = fbtft_driver_remove_spi, \ 334 }; \ 335 \ 336 static struct platform_driver fbtft_driver_platform_driver = { \ 337 .driver = { \ 338 .name = _name, \ 339 .owner = THIS_MODULE, \ 340 .of_match_table = of_match_ptr(dt_ids), \ 341 }, \ 342 .probe = fbtft_driver_probe_pdev, \ 343 .remove = fbtft_driver_remove_pdev, \ 344 }; \ 345 \ 346 static int __init fbtft_driver_module_init(void) \ 347 { \ 348 int ret; \ 349 \ 350 ret = spi_register_driver(&fbtft_driver_spi_driver); \ 351 if (ret < 0) \ 352 return ret; \ 353 return platform_driver_register(&fbtft_driver_platform_driver); \ 354 } \ 355 \ 356 static void __exit fbtft_driver_module_exit(void) \ 357 { \ 358 spi_unregister_driver(&fbtft_driver_spi_driver); \ 359 platform_driver_unregister(&fbtft_driver_platform_driver); \ 360 } \ 361 \ 362 module_init(fbtft_driver_module_init); \ 363 module_exit(fbtft_driver_module_exit); 364 365 /* Debug macros */ 366 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 375 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) 381 382 /* fbops */ 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) 390 391 #define DEBUG_SYSFS (1<<16) 392 393 /* fbtftops */ 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) 409 410 #define fbtft_init_dbg(dev, format, arg...) \ 411 do { \ 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); \ 415 } while (0) 416 417 #define fbtft_par_dbg(level, par, format, arg...) \ 418 do { \ 419 if (unlikely(par->debug & level)) \ 420 dev_info(par->info->device, format, ##arg); \ 421 } while (0) 422 423 #define fbtft_par_dbg_hex(level, par, dev, type, buf, num, format, arg...) \ 424 do { \ 425 if (unlikely(par->debug & level)) \ 426 fbtft_dbg_hex(dev, sizeof(type), buf, num * sizeof(type), format, ##arg); \ 427 } while (0) 428 429 #endif /* __LINUX_FBTFT_H */ 430