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 */ 52 struct fbtft_gpio { 53 char name[FBTFT_GPIO_NAME_SIZE]; 54 unsigned gpio; 55 }; 56 57 struct 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 */ 84 struct 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 */ 130 struct 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 */ 159 struct 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 */ 216 struct 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 */ 265 void fbtft_dbg_hex(const struct device *dev, int groupsize, 266 void *buf, size_t len, const char *fmt, ...); 267 struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display, 268 struct device *dev, 269 struct fbtft_platform_data *pdata); 270 void fbtft_framebuffer_release(struct fb_info *info); 271 int fbtft_register_framebuffer(struct fb_info *fb_info); 272 int fbtft_unregister_framebuffer(struct fb_info *fb_info); 273 void fbtft_register_backlight(struct fbtft_par *par); 274 void fbtft_unregister_backlight(struct fbtft_par *par); 275 int fbtft_init_display(struct fbtft_par *par); 276 int fbtft_probe_common(struct fbtft_display *display, struct spi_device *sdev, 277 struct platform_device *pdev); 278 int fbtft_remove_common(struct device *dev, struct fb_info *info); 279 280 /* fbtft-io.c */ 281 int fbtft_write_spi(struct fbtft_par *par, void *buf, size_t len); 282 int fbtft_write_spi_emulate_9(struct fbtft_par *par, void *buf, size_t len); 283 int fbtft_read_spi(struct fbtft_par *par, void *buf, size_t len); 284 int fbtft_write_gpio8_wr(struct fbtft_par *par, void *buf, size_t len); 285 int fbtft_write_gpio16_wr(struct fbtft_par *par, void *buf, size_t len); 286 int fbtft_write_gpio16_wr_latched(struct fbtft_par *par, void *buf, size_t len); 287 288 /* fbtft-bus.c */ 289 int fbtft_write_vmem8_bus8(struct fbtft_par *par, size_t offset, size_t len); 290 int fbtft_write_vmem16_bus16(struct fbtft_par *par, size_t offset, size_t len); 291 int fbtft_write_vmem16_bus8(struct fbtft_par *par, size_t offset, size_t len); 292 int fbtft_write_vmem16_bus9(struct fbtft_par *par, size_t offset, size_t len); 293 void fbtft_write_reg8_bus8(struct fbtft_par *par, int len, ...); 294 void fbtft_write_reg8_bus9(struct fbtft_par *par, int len, ...); 295 void fbtft_write_reg16_bus8(struct fbtft_par *par, int len, ...); 296 void fbtft_write_reg16_bus16(struct fbtft_par *par, int len, ...); 297 298 299 #define FBTFT_REGISTER_DRIVER(_name, _compatible, _display) \ 300 \ 301 static int fbtft_driver_probe_spi(struct spi_device *spi) \ 302 { \ 303 return fbtft_probe_common(_display, spi, NULL); \ 304 } \ 305 \ 306 static int fbtft_driver_remove_spi(struct spi_device *spi) \ 307 { \ 308 struct fb_info *info = spi_get_drvdata(spi); \ 309 \ 310 return fbtft_remove_common(&spi->dev, info); \ 311 } \ 312 \ 313 static int fbtft_driver_probe_pdev(struct platform_device *pdev) \ 314 { \ 315 return fbtft_probe_common(_display, NULL, pdev); \ 316 } \ 317 \ 318 static int fbtft_driver_remove_pdev(struct platform_device *pdev) \ 319 { \ 320 struct fb_info *info = platform_get_drvdata(pdev); \ 321 \ 322 return fbtft_remove_common(&pdev->dev, info); \ 323 } \ 324 \ 325 static const struct of_device_id dt_ids[] = { \ 326 { .compatible = _compatible }, \ 327 {}, \ 328 }; \ 329 \ 330 MODULE_DEVICE_TABLE(of, dt_ids); \ 331 \ 332 \ 333 static struct spi_driver fbtft_driver_spi_driver = { \ 334 .driver = { \ 335 .name = _name, \ 336 .owner = THIS_MODULE, \ 337 .of_match_table = of_match_ptr(dt_ids), \ 338 }, \ 339 .probe = fbtft_driver_probe_spi, \ 340 .remove = fbtft_driver_remove_spi, \ 341 }; \ 342 \ 343 static struct platform_driver fbtft_driver_platform_driver = { \ 344 .driver = { \ 345 .name = _name, \ 346 .owner = THIS_MODULE, \ 347 .of_match_table = of_match_ptr(dt_ids), \ 348 }, \ 349 .probe = fbtft_driver_probe_pdev, \ 350 .remove = fbtft_driver_remove_pdev, \ 351 }; \ 352 \ 353 static int __init fbtft_driver_module_init(void) \ 354 { \ 355 int ret; \ 356 \ 357 ret = spi_register_driver(&fbtft_driver_spi_driver); \ 358 if (ret < 0) \ 359 return ret; \ 360 return platform_driver_register(&fbtft_driver_platform_driver); \ 361 } \ 362 \ 363 static void __exit fbtft_driver_module_exit(void) \ 364 { \ 365 spi_unregister_driver(&fbtft_driver_spi_driver); \ 366 platform_driver_unregister(&fbtft_driver_platform_driver); \ 367 } \ 368 \ 369 module_init(fbtft_driver_module_init); \ 370 module_exit(fbtft_driver_module_exit); 371 372 373 /* Debug macros */ 374 375 /* shorthand debug levels */ 376 #define DEBUG_LEVEL_1 DEBUG_REQUEST_GPIOS 377 #define DEBUG_LEVEL_2 (DEBUG_LEVEL_1 | DEBUG_DRIVER_INIT_FUNCTIONS | DEBUG_TIME_FIRST_UPDATE) 378 #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) 379 #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) 380 #define DEBUG_LEVEL_5 (DEBUG_LEVEL_3 | DEBUG_UPDATE_DISPLAY) 381 #define DEBUG_LEVEL_6 (DEBUG_LEVEL_4 | DEBUG_LEVEL_5) 382 #define DEBUG_LEVEL_7 0xFFFFFFFF 383 384 #define DEBUG_DRIVER_INIT_FUNCTIONS (1<<3) 385 #define DEBUG_TIME_FIRST_UPDATE (1<<4) 386 #define DEBUG_TIME_EACH_UPDATE (1<<5) 387 #define DEBUG_DEFERRED_IO (1<<6) 388 #define DEBUG_FBTFT_INIT_FUNCTIONS (1<<7) 389 390 /* fbops */ 391 #define DEBUG_FB_READ (1<<8) 392 #define DEBUG_FB_WRITE (1<<9) 393 #define DEBUG_FB_FILLRECT (1<<10) 394 #define DEBUG_FB_COPYAREA (1<<11) 395 #define DEBUG_FB_IMAGEBLIT (1<<12) 396 #define DEBUG_FB_SETCOLREG (1<<13) 397 #define DEBUG_FB_BLANK (1<<14) 398 399 #define DEBUG_SYSFS (1<<16) 400 401 /* fbtftops */ 402 #define DEBUG_BACKLIGHT (1<<17) 403 #define DEBUG_READ (1<<18) 404 #define DEBUG_WRITE (1<<19) 405 #define DEBUG_WRITE_VMEM (1<<20) 406 #define DEBUG_WRITE_REGISTER (1<<21) 407 #define DEBUG_SET_ADDR_WIN (1<<22) 408 #define DEBUG_RESET (1<<23) 409 #define DEBUG_MKDIRTY (1<<24) 410 #define DEBUG_UPDATE_DISPLAY (1<<25) 411 #define DEBUG_INIT_DISPLAY (1<<26) 412 #define DEBUG_BLANK (1<<27) 413 #define DEBUG_REQUEST_GPIOS (1<<28) 414 #define DEBUG_FREE_GPIOS (1<<29) 415 #define DEBUG_REQUEST_GPIOS_MATCH (1<<30) 416 #define DEBUG_VERIFY_GPIOS (1<<31) 417 418 419 #define fbtft_init_dbg(dev, format, arg...) \ 420 do { \ 421 if (unlikely((dev)->platform_data && \ 422 (((struct fbtft_platform_data *)(dev)->platform_data)->display.debug & DEBUG_DRIVER_INIT_FUNCTIONS))) \ 423 dev_info(dev, format, ##arg); \ 424 } while (0) 425 426 #define fbtft_par_dbg(level, par, format, arg...) \ 427 do { \ 428 if (unlikely(par->debug & level)) \ 429 dev_info(par->info->device, format, ##arg); \ 430 } while (0) 431 432 433 #define fbtft_par_dbg_hex(level, par, dev, type, buf, num, format, arg...) \ 434 do { \ 435 if (unlikely(par->debug & level)) \ 436 fbtft_dbg_hex(dev, sizeof(type), buf, num * sizeof(type), format, ##arg); \ 437 } while (0) 438 439 #endif /* __LINUX_FBTFT_H */ 440