PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/eHacks/ILI932X/ili9325.c

https://github.com/shawnchain/sandbox
C | 1256 lines | 876 code | 157 blank | 223 comment | 45 complexity | 75d5712569f65f01128660907a8ae52b MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. /*
  2. * ILI9320 Framebuffer
  3. *
  4. * ToDo: Fix this text vv
  5. *
  6. * Original: Copyright (c) 2009 Jean-Christian de Rivaz
  7. *
  8. * Console support, 320x240 instead of 240x320:
  9. * Copyright (c) 2012 Jeroen Domburg <jeroen@spritesmods.com>
  10. *
  11. * Bits and pieces borrowed from the fsl-ili9325.c:
  12. * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
  13. * Author: Alison Wang <b18965@freescale.com>
  14. * Jason Jin <Jason.jin@freescale.com>
  15. *
  16. * Supports ILI9320 chip by Shawn Chain <shawn.chain@gmail.com>
  17. *
  18. * This file is subject to the terms and conditions of the GNU General Public
  19. * License. See the file "COPYING" in the main directory of this archive
  20. * for more details.
  21. *
  22. * The Solomon Systech ILI9325 chip drive TFT screen up to 240x320.
  23. *
  24. * For direct I/O-mode:
  25. *
  26. * This driver expect the SSD1286 to be connected to a 16 bits local bus
  27. * and to be set in the 16 bits parallel interface mode. To use it you must
  28. * define in your board file a struct platform_device with a name set to
  29. * "ili9325" and a struct resource array with two IORESOURCE_MEM: the first
  30. * for the control register; the second for the data register.
  31. *
  32. *
  33. * LCDs in their own, native SPI mode aren't supported yet, mostly because I
  34. * can't get my hands on a cheap one.
  35. */
  36. //#define DEBUG
  37. #include <linux/kernel.h>
  38. #include <linux/device.h>
  39. #include <linux/module.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/mm.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/fb.h>
  44. #include <asm/io.h>
  45. #include <linux/delay.h>
  46. #define LCD_NCS 4 // BRCM_GPIO_04 <--> P1_07 <--> RPI_GPIO_7
  47. // for PCB rev2
  48. #define LCD_NRD 27 // BRCM_GPIO_27 <--> p1_13 <--> RPI_GPIO_? (rev2)
  49. #define LCD_NWR 17 // BRCM_GPIO_17 <--> p1_11 <--> RPI_GPIO_0
  50. #define LCD_RS 18 // BRCM_GPIO_18 <--> p1_12 <--> RPI_GPIO_1
  51. #define LCD_NRST 7 // BRCM_GPIO_07 <--> p1_26 <--> RPI_SPI_CE1_N
  52. #define LCD_D10 22 // BRCM_GPIO_22 <--> p1_15 <--> RPI_GPIO_3
  53. #define LCD_D11 23 // BRCM_GPIO_23 <--> p1_16 <--> RPI_GPIO_4
  54. #define LCD_D12 24 // BRCM_GPIO_24 <--> p1_18 <--> RPI_GPIO_5
  55. #define LCD_D13 10 // BRCM_GPIO_10 <--> p1_19 <--> RPI_SPI_MOSI
  56. #define LCD_D14 25 // BRCM_GPIO_25 <--> p1_22 <--> RPI_GPIO_6
  57. #define LCD_D15 9 // BRCM_GPIO_09 <--> p1_21 <--> RPI_SPI_MISO
  58. #define LCD_D16 11 // BRCM_GPIO_11 <--> p1_23 <--> RPI_SPI_SCLK
  59. #define LCD_D17 8 // BRCM_GPIO_08 <--> p1_24 <--> RPI_CE0_N
  60. #define UPSIDEDOWN
  61. struct ili9325_page {
  62. unsigned short x;
  63. unsigned short y;
  64. unsigned short *buffer;
  65. unsigned short *oldbuffer;
  66. unsigned short len;
  67. int must_update;
  68. };
  69. struct ili9325 {
  70. struct device *dev;
  71. struct fb_info *info;
  72. unsigned int pages_count;
  73. struct ili9325_page *pages;
  74. unsigned long pseudo_palette[17];
  75. int backlight;
  76. };
  77. // ==== GPIO HI/LO operations ====
  78. // GPIO_BASE+0x28 = GPCLR0(GPIO Pin Output Clear 0)
  79. // GPIO_BASE+0x1C = GPSET0(GPIO Pin Output Set 0)
  80. // Always use GPSET0 because pin number larger than 32 is not used here.
  81. // - Shawn
  82. #define BRCM_GPSET0 (GPIO_BASE + 0x1C)
  83. #define BRCM_GPCLR0 (GPIO_BASE + 0x28)
  84. #define BRCM_GPLEV0 (GPIO_BASE + 0x34)
  85. #define BRCM_GPPUD (GPIO_BASE + 0x94)
  86. #define BRCM_GPPUDCLK0 (GPIO_BASE + 0x98)
  87. #define GPIO_HI(port) writel((1<<port), __io_address(BRCM_GPSET0));
  88. #define GPIO_LO(port) writel((1<<port), __io_address(BRCM_GPCLR0));
  89. #define GPIO_NOOP writel(0, __io_address(BRCM_GPCLR0));
  90. // ==== GPIO Pull up/down setup ====
  91. static inline void gpio_setpud(unsigned int port, unsigned int val){
  92. writel(val,__io_address(BRCM_GPPUD));
  93. GPIO_NOOP;
  94. writel((1<<port),__io_address(BRCM_GPPUDCLK0)); // the pin 01 pull down
  95. GPIO_NOOP;
  96. }
  97. // ==== GPIO Pin Mode Setups ====
  98. // See BCM2835 GPIO Function Select Registers datasheet.
  99. // The selector register uses 3bits for each GPIO port function definition
  100. // For a 32bit register, it contains up to 10 GPIO port function definitions.
  101. // Eg: set GPFEL0->bit[12,13,14] = 001 means Use GPIO4 as output
  102. // - Shawn
  103. #define PIN_MODE_INPUT 0
  104. #define PIN_MODE_OUTPUT 1
  105. #define GPIO_DATA_READ ili932x_gpio_set_databus_mode(PIN_MODE_INPUT);
  106. #define GPIO_DATA_WRITE ili932x_gpio_set_databus_mode(PIN_MODE_OUTPUT);
  107. #define GPIO_ALT_OFFSET(g) ((((g)/10))*4)
  108. #define GPIO_ALT_VAL(a, g) ((a)<<(((g)%10)*3))
  109. static inline void ili932x_gpio_set_pin_mode(int gpio,int mode){
  110. unsigned int v;
  111. v=readl(__io_address(GPIO_BASE+GPIO_ALT_OFFSET(gpio)));
  112. v&=~GPIO_ALT_VAL(0x7, gpio); //clear existing bits 0111
  113. v|=GPIO_ALT_VAL(mode, gpio); //pin mode, currently output or input only
  114. writel(v, __io_address(GPIO_BASE+GPIO_ALT_OFFSET(gpio)));
  115. }
  116. static inline void ili932x_gpio_set_databus_mode(int mode){
  117. //TODO use one-shot changes
  118. ili932x_gpio_set_pin_mode(LCD_D10,mode);
  119. ili932x_gpio_set_pin_mode(LCD_D11,mode);
  120. ili932x_gpio_set_pin_mode(LCD_D12,mode);
  121. ili932x_gpio_set_pin_mode(LCD_D13,mode);
  122. ili932x_gpio_set_pin_mode(LCD_D14,mode);
  123. ili932x_gpio_set_pin_mode(LCD_D15,mode);
  124. ili932x_gpio_set_pin_mode(LCD_D16,mode);
  125. ili932x_gpio_set_pin_mode(LCD_D17,mode);
  126. }
  127. // ==== Data Read/Write ====
  128. /*
  129. Use direct GPIO reg access instead of the gpiolib framework: because we want to write
  130. multiple bits at once to the (presumably connected over a slower bus) GPIO block,
  131. this involves less writes and so will be faster.
  132. */
  133. /* macros to get at IO space when running virtually */
  134. #define GPIOSET(no, ishigh){ if (ishigh) set|=(1<<no); else reset|=(1<<no); } while(0)
  135. static inline void ili932x_gpio_set_databus_level(unsigned char aByte){
  136. unsigned int set=0,reset=0;
  137. GPIOSET(LCD_D10, (aByte&0x01)); // 8bit data
  138. GPIOSET(LCD_D11, (aByte&0x02));
  139. GPIOSET(LCD_D12, (aByte&0x04));
  140. GPIOSET(LCD_D13, (aByte&0x08));
  141. GPIOSET(LCD_D14, (aByte&0x10));
  142. GPIOSET(LCD_D15, (aByte&0x20));
  143. GPIOSET(LCD_D16, (aByte&0x40));
  144. GPIOSET(LCD_D17, (aByte&0x80));
  145. writel(set, __io_address(BRCM_GPSET0));
  146. writel(reset, __io_address(BRCM_GPCLR0));
  147. }
  148. static inline void ili932x_write_begin(void){
  149. GPIO_LO(LCD_NCS);
  150. // GPIO_DATA_WRITE; - Databus set for write
  151. ili932x_gpio_set_databus_mode(PIN_MODE_OUTPUT);
  152. }
  153. static inline void ili932x_read_begin(void){
  154. GPIO_LO(LCD_NCS);
  155. // GPIO_DATA_READ; - Databus set for write
  156. ili932x_gpio_set_databus_mode(PIN_MODE_INPUT);
  157. }
  158. #define LCD_CMD 0
  159. #define LCD_DATA 1
  160. /**
  161. * Write a word, assume nCS is LOW
  162. */
  163. static inline void ili932x_write_unsafe(unsigned int data, int cmdOrData){
  164. unsigned int set=0;
  165. unsigned int reset=0;
  166. unsigned char aByte = 0;
  167. if(cmdOrData == LCD_CMD){
  168. GPIO_LO(LCD_RS);
  169. }
  170. // write high 8bits
  171. aByte = (data >> 8);
  172. // We don't use GPIOSET macro here for performance reason
  173. set |= (((aByte >> 0)&0x01) << LCD_D10);
  174. set |= (((aByte >> 1)&0x01) << LCD_D11);
  175. set |= (((aByte >> 2)&0x01) << LCD_D12);
  176. set |= (((aByte >> 3)&0x01) << LCD_D13);
  177. set |= (((aByte >> 4)&0x01) << LCD_D14);
  178. set |= (((aByte >> 5)&0x01) << LCD_D15);
  179. set |= (((aByte >> 6)&0x01) << LCD_D16);
  180. set |= (((aByte >> 7)&0x01) << LCD_D17);
  181. reset |= ((1 & ~((aByte >> 0)&0x01)) << LCD_D10);
  182. reset |= ((1 & ~((aByte >> 1)&0x01)) << LCD_D11);
  183. reset |= ((1 & ~((aByte >> 2)&0x01)) << LCD_D12);
  184. reset |= ((1 & ~((aByte >> 3)&0x01)) << LCD_D13);
  185. reset |= ((1 & ~((aByte >> 4)&0x01)) << LCD_D14);
  186. reset |= ((1 & ~((aByte >> 5)&0x01)) << LCD_D15);
  187. reset |= ((1 & ~((aByte >> 6)&0x01)) << LCD_D16);
  188. reset |= ((1 & ~((aByte >> 7)&0x01)) << LCD_D17);
  189. writel(set, __io_address(BRCM_GPSET0));
  190. writel(reset, __io_address(BRCM_GPCLR0));
  191. GPIO_LO(LCD_NWR);
  192. GPIO_NOOP;
  193. GPIO_HI(LCD_NWR);
  194. // write low 8bits
  195. set = 0;
  196. reset = 0;
  197. aByte = (data & 0xff);
  198. set |= (((aByte >> 0)&0x01) << LCD_D10);
  199. set |= (((aByte >> 1)&0x01) << LCD_D11);
  200. set |= (((aByte >> 2)&0x01) << LCD_D12);
  201. set |= (((aByte >> 3)&0x01) << LCD_D13);
  202. set |= (((aByte >> 4)&0x01) << LCD_D14);
  203. set |= (((aByte >> 5)&0x01) << LCD_D15);
  204. set |= (((aByte >> 6)&0x01) << LCD_D16);
  205. set |= (((aByte >> 7)&0x01) << LCD_D17);
  206. reset |= ((1 & ~((aByte >> 0)&0x01)) << LCD_D10);
  207. reset |= ((1 & ~((aByte >> 1)&0x01)) << LCD_D11);
  208. reset |= ((1 & ~((aByte >> 2)&0x01)) << LCD_D12);
  209. reset |= ((1 & ~((aByte >> 3)&0x01)) << LCD_D13);
  210. reset |= ((1 & ~((aByte >> 4)&0x01)) << LCD_D14);
  211. reset |= ((1 & ~((aByte >> 5)&0x01)) << LCD_D15);
  212. reset |= ((1 & ~((aByte >> 6)&0x01)) << LCD_D16);
  213. reset |= ((1 & ~((aByte >> 7)&0x01)) << LCD_D17);
  214. writel(set, __io_address(BRCM_GPSET0));
  215. writel(reset, __io_address(BRCM_GPCLR0));
  216. GPIO_LO(LCD_NWR);
  217. GPIO_NOOP;//msleep(10);
  218. GPIO_HI(LCD_NWR);
  219. if(cmdOrData == LCD_CMD){
  220. // restore the RS state
  221. GPIO_HI(LCD_RS);
  222. }
  223. #if 0
  224. printk(KERN_INFO "ili932x_fb ili932x_write_unsafe: %#06x\n",data);
  225. #endif
  226. }
  227. static inline void ili932x_write_end(void){
  228. GPIO_HI(LCD_NCS);
  229. }
  230. /**
  231. * Write a word to LCD controller
  232. */
  233. static inline void ili932x_write(unsigned int data, int cmdOrData){
  234. ili932x_write_begin();
  235. ili932x_write_unsafe(data,cmdOrData);
  236. ili932x_write_end();
  237. }
  238. #define GPIOGET(n,v) ((v & (1<<n)) >> n)
  239. static inline unsigned int ili932x_read_unsafe(void){
  240. unsigned int val = 0;
  241. unsigned int pinLevels = 0;
  242. unsigned char aByte = 0;
  243. // GPIO_DATA_READ; - Databus set for read
  244. ili932x_gpio_set_databus_mode(PIN_MODE_INPUT);
  245. ili932x_gpio_set_databus_level(0xFF);
  246. // Read the high part
  247. GPIO_LO(LCD_NRD);//writel((1<<LCD_NRD), __io_address(GPIO_BASE+0x28));
  248. msleep(10);
  249. pinLevels = readl(__io_address(BRCM_GPLEV0)); // a 32bit mask value
  250. aByte |= (GPIOGET(LCD_D10,pinLevels) << 0); // bit 0 - 7
  251. aByte |= (GPIOGET(LCD_D11,pinLevels) << 1);
  252. aByte |= (GPIOGET(LCD_D12,pinLevels) << 2);
  253. aByte |= (GPIOGET(LCD_D13,pinLevels) << 3);
  254. aByte |= (GPIOGET(LCD_D14,pinLevels) << 4);
  255. aByte |= (GPIOGET(LCD_D15,pinLevels) << 5);
  256. aByte |= (GPIOGET(LCD_D16,pinLevels) << 6);
  257. aByte |= (GPIOGET(LCD_D17,pinLevels) << 7);
  258. val = ((aByte<<8) & 0xFF00);
  259. GPIO_HI(LCD_NRD);//writel((1<<LCD_NRD), __io_address(GPIO_BASE+0x1C));
  260. // Read the low part
  261. aByte = 0;
  262. GPIO_LO(LCD_NRD);//writel((1<<LCD_NRD), __io_address(GPIO_BASE+0x28));
  263. msleep(10);
  264. pinLevels = readl(__io_address(BRCM_GPLEV0)); // a 32bit mask value
  265. aByte |= (GPIOGET(LCD_D10,pinLevels) << 0); // bit 0 - 7
  266. aByte |= (GPIOGET(LCD_D11,pinLevels) << 1);
  267. aByte |= (GPIOGET(LCD_D12,pinLevels) << 2);
  268. aByte |= (GPIOGET(LCD_D13,pinLevels) << 3);
  269. aByte |= (GPIOGET(LCD_D14,pinLevels) << 4);
  270. aByte |= (GPIOGET(LCD_D15,pinLevels) << 5);
  271. aByte |= (GPIOGET(LCD_D16,pinLevels) << 6);
  272. aByte |= (GPIOGET(LCD_D17,pinLevels) << 7);
  273. val |= aByte;
  274. GPIO_HI(LCD_NRD);//writel((1<<LCD_NRD), __io_address(GPIO_BASE+0x1C));
  275. printk(KERN_INFO "ili932x_fb ili932x_read_unsafe: %#06x\n",val);
  276. return val;
  277. }
  278. static inline unsigned int ili932x_read(void){
  279. unsigned int value = 0;
  280. ili932x_read_begin();
  281. value = ili932x_read_unsafe();
  282. ili932x_write_end();
  283. return value;
  284. }
  285. // ---- LCD Register Operator ----
  286. #define LCD_SET_REG(x,y) lcd_write_register(x,y)
  287. static inline unsigned int lcd_read_register(unsigned int reg){
  288. unsigned int value = 0;
  289. // write command
  290. ili932x_write(reg,LCD_CMD);
  291. // read data
  292. value = ili932x_read();
  293. return value;
  294. }
  295. static inline void lcd_write_register(unsigned int reg, unsigned int val){
  296. // write command
  297. ili932x_write(reg,LCD_CMD);
  298. // write data
  299. ili932x_write(val,LCD_DATA);
  300. }
  301. // ---- Initialize the BCM board GPIO pins ----//
  302. static inline void ili9325_gpio_init(void)
  303. {
  304. ili932x_gpio_set_pin_mode(LCD_NCS,PIN_MODE_OUTPUT);
  305. ili932x_gpio_set_pin_mode(LCD_NWR,PIN_MODE_OUTPUT); //write
  306. ili932x_gpio_set_pin_mode(LCD_NRD,PIN_MODE_OUTPUT); //read
  307. ili932x_gpio_set_pin_mode(LCD_RS,PIN_MODE_OUTPUT); //cmd or data
  308. ili932x_gpio_set_pin_mode(LCD_NRST,PIN_MODE_OUTPUT);//reset
  309. // Use BCM_GPUPD/GPUPDCLK0 to setup the pin pull up.
  310. gpio_setpud(LCD_NCS,1);
  311. gpio_setpud(LCD_RS,1);
  312. gpio_setpud(LCD_NRD,1);
  313. gpio_setpud(LCD_NWR,1);
  314. gpio_setpud(LCD_NRST,1);
  315. // setup data bus for write
  316. ili932x_gpio_set_databus_mode(PIN_MODE_OUTPUT);
  317. ili932x_gpio_set_databus_level(0xff);
  318. }
  319. // ---- Hardware reset the LCD controller ----
  320. static inline int ili932x_init_controller(void) {
  321. int driverCode = 0;
  322. // reset LCD controller
  323. printk(KERN_INFO "ili932x_fb: Resetting TFT controller\n");
  324. GPIO_LO(LCD_NRST);
  325. msleep(20);
  326. GPIO_HI(LCD_NRST);
  327. msleep(20);
  328. printk(KERN_INFO "ili932x_fb: Starting TFT controller Oscillation\n");
  329. // Start oscillation
  330. LCD_SET_REG(0x0000,0x0001); // Start Oscillation
  331. msleep(50);
  332. // read the driver version code
  333. driverCode = lcd_read_register(0x0000);
  334. printk(KERN_INFO "ili932x_fb: Read TFT controller type code: %#08x\n",driverCode);
  335. if(driverCode == 0){
  336. printk(KERN_INFO "ili932x_fb: No TFT controller found.\n");
  337. return 0;
  338. }
  339. return driverCode;
  340. }
  341. /*
  342. * Set LCD position pointer for display
  343. */
  344. static void ili9325_setptr(struct ili9325 *item, int x, int y) {
  345. #ifdef UPSIDEDOWN
  346. LCD_SET_REG(0x0020,y); // Horizontal GRAM Start Address
  347. LCD_SET_REG(0x0021,(item->info->var.xres - 1)-x); // Vertical GRAM Start Address
  348. #else
  349. LCD_SET_REG(0x0020,(item->info->var.yres - 1)-y); // Horizontal GRAM Start Address
  350. LCD_SET_REG(0x0021,x); // Vertical GRAM Start Address
  351. #endif
  352. ili932x_write(0x0022,LCD_CMD); // Prepare for data
  353. }
  354. static void ili9325_setptr_unsafe(struct ili9325 *item, int x, int y) {
  355. unsigned int r20, r21;
  356. #ifdef UPSIDEDOWN
  357. r20 = y; // Horizontal
  358. r21 = (item->info->var.xres - 1)-x;// Vertical
  359. #else
  360. r20 = (item->info->var.yres - 1)-y; // Horizontal
  361. r20 = x; // Vertical
  362. #endif
  363. ili932x_write_unsafe(0x0020,LCD_CMD);
  364. ili932x_write_unsafe(r20,LCD_DATA);// Horizontal GRAM Start Address
  365. ili932x_write_unsafe(0x0021,LCD_CMD);
  366. ili932x_write_unsafe(r21,LCD_DATA);// Vertical GRAM Start Address
  367. ili932x_write_unsafe(0x0022,LCD_CMD); // Prepare for data
  368. }
  369. /*
  370. * write fb buffer data to TFT
  371. */
  372. static void ili9325_copy(struct ili9325 *item, unsigned int index)
  373. {
  374. unsigned short x;
  375. unsigned short y;
  376. unsigned short *buffer;
  377. unsigned short *oldbuffer;
  378. unsigned int len;
  379. unsigned int count;
  380. int sendNewPos=1;
  381. x = item->pages[index].x;
  382. y = item->pages[index].y;
  383. buffer = item->pages[index].buffer;
  384. oldbuffer = item->pages[index].oldbuffer;
  385. len = item->pages[index].len;
  386. dev_dbg(item->dev,
  387. "%s: page[%u]: x=%3hu y=%3hu buffer=0x%p len=%3hu\n",
  388. __func__, index, x, y, buffer, len);
  389. //Only update changed pixels in the page.
  390. ili932x_write_begin();
  391. for (count = 0; count < len; count++) {
  392. if (buffer[count]==oldbuffer[count]) {
  393. sendNewPos=1;
  394. } else {
  395. if (sendNewPos) {
  396. ili9325_setptr_unsafe(item, x, y);
  397. sendNewPos=0;
  398. }
  399. ili932x_write_unsafe(buffer[count],LCD_DATA);//ili932x_write_word(buffer[count], 1);
  400. oldbuffer[count]=buffer[count];
  401. }
  402. x++;
  403. if (x>=item->info->var.xres) {
  404. y++;
  405. x=0;
  406. }
  407. }
  408. ili932x_write_end();
  409. }
  410. static void ili9325_update_all(struct ili9325 *item)
  411. {
  412. unsigned short i;
  413. struct fb_deferred_io *fbdefio = item->info->fbdefio;
  414. for (i = 0; i < item->pages_count; i++) {
  415. item->pages[i].must_update=1;
  416. }
  417. schedule_delayed_work(&item->info->deferred_work, fbdefio->delay);
  418. }
  419. static void ili9325_update(struct fb_info *info, struct list_head *pagelist)
  420. {
  421. struct ili9325 *item = (struct ili9325 *)info->par;
  422. struct page *page;
  423. int i;
  424. //We can be called because of pagefaults (mmap'ed framebuffer, pages
  425. //returned in *pagelist) or because of kernel activity
  426. //(pages[i]/must_update!=0). Add the former to the list of the latter.
  427. list_for_each_entry(page, pagelist, lru) {
  428. item->pages[page->index].must_update=1;
  429. }
  430. //Copy changed pages.
  431. for (i=0; i<item->pages_count; i++) {
  432. //ToDo: Small race here between checking and setting must_update,
  433. //maybe lock?
  434. if (item->pages[i].must_update) {
  435. item->pages[i].must_update=0;
  436. ili9325_copy(item, i);
  437. }
  438. }
  439. }
  440. #define ILI9320_REG_OSCILLATION 0x00
  441. #define ILI9320_REG_DRIVER_OUT_CTRL 0x01
  442. #define ILI9320_REG_LCD_DRIVE_AC 0x02
  443. #define ILI9320_REG_POWER_CTRL_1 0x03
  444. #define ILI9320_REG_DISPLAY_CTRL 0x07
  445. #define ILI9320_REG_FRAME_CYCLE 0x0b
  446. #define ILI9320_REG_POWER_CTRL_2 0x0c
  447. #define ILI9320_REG_POWER_CTRL_3 0x0d
  448. #define ILI9320_REG_POWER_CTRL_4 0x0e
  449. #define ILI9320_REG_GATE_SCAN_START 0x0f
  450. #define ILI9320_REG_SLEEP_MODE 0x10
  451. #define ILI9320_REG_ENTRY_MODE 0x11
  452. #define ILI9320_REG_POWER_CTRL_5 0x1e
  453. #define ILI9320_REG_GDDRAM_DATA 0x22
  454. #define ILI9320_REG_WR_DATA_MASK_1 0x23
  455. #define ILI9320_REG_WR_DATA_MASK_2 0x24
  456. #define ILI9320_REG_FRAME_FREQUENCY 0x25
  457. #define ILI9320_REG_GAMMA_CTRL_1 0x30
  458. #define ILI9320_REG_GAMME_CTRL_2 0x31
  459. #define ILI9320_REG_GAMMA_CTRL_3 0x32
  460. #define ILI9320_REG_GAMMA_CTRL_4 0x33
  461. #define ILI9320_REG_GAMMA_CTRL_5 0x34
  462. #define ILI9320_REG_GAMMA_CTRL_6 0x35
  463. #define ILI9320_REG_GAMMA_CTRL_7 0x36
  464. #define ILI9320_REG_GAMMA_CTRL_8 0x37
  465. #define ILI9320_REG_GAMMA_CTRL_9 0x3a
  466. #define ILI9320_REG_GAMMA_CTRL_10 0x3b
  467. #define ILI9320_REG_V_SCROLL_CTRL_1 0x41
  468. #define ILI9320_REG_V_SCROLL_CTRL_2 0x42
  469. #define ILI9320_REG_H_RAM_ADR_POS 0x44
  470. #define ILI9320_REG_V_RAM_ADR_START 0x45
  471. #define ILI9320_REG_V_RAM_ADR_END 0x46
  472. #define ILI9320_REG_FIRST_WIN_START 0x48
  473. #define ILI9320_REG_FIRST_WIN_END 0x49
  474. #define ILI9320_REG_SECND_WIN_START 0x4a
  475. #define ILI9320_REG_SECND_WIN_END 0x4b
  476. #define ILI9320_REG_GDDRAM_X_ADDR 0x4e
  477. #define ILI9320_REG_GDDRAM_Y_ADDR 0x4f
  478. #define LCD_ALIGNMENT_VERTICAL 1
  479. static void inline ili9320_setup_1(struct ili9325 *item){
  480. /*** Display Setting ***/
  481. lcd_write_register(0x01, 0x0100); //Driver output control (1)
  482. lcd_write_register(0x02, 0x0700); //LCD driving control
  483. // Check page 55 in the datasheet for more information about alignment
  484. #if LCD_ALIGNMENT_VERTICAL
  485. lcd_write_register(0x03, 0x1018); //Entry mode
  486. #else // align horizontal
  487. lcd_write_register(0x03, 0x1028); //Entry mode
  488. #endif
  489. lcd_write_register(0x04, 0x0000); //Resize control
  490. lcd_write_register(0x08, 0x0202); //Display control (2)
  491. lcd_write_register(0x09, 0x0000); //Display control (3)
  492. /*** Power Control ***/
  493. lcd_write_register(0x07, 0x0101); //power control 1 BT, AP
  494. lcd_write_register(0x17, 0x0001);
  495. lcd_write_register(0x10, 0x0000);
  496. lcd_write_register(0x11, 0x0007); //power control 2 DC, VC
  497. lcd_write_register(0x12, 0x0000); //power control 3 VRH
  498. lcd_write_register(0x12, 0x0000); //power control 3 VRH
  499. lcd_write_register(0x13, 0x0000); //power control 4 VCOM amplitude
  500. msleep(20);
  501. lcd_write_register(0x10, 0x16B0); //power control 1 BT,AP
  502. lcd_write_register(0x11, 0x0037); //power control 2 DC,VC
  503. msleep(50);
  504. lcd_write_register(0x12, 0x013E); //power control 3 VRH
  505. msleep(50);
  506. lcd_write_register(0x13, 0x1A00); //power control 4 vcom amplitude
  507. lcd_write_register(0x29, 0x000F); //power control 7 VCOMH
  508. msleep(50);
  509. lcd_write_register(0x20, 0x0000); //Horizontal GRAM Address Set
  510. lcd_write_register(0x21, 0x0000); //Vertical GRAM Address Set
  511. lcd_write_register(0x50, 0x0000); //Horizontal Address Start Position
  512. lcd_write_register(0x51, 0x00EF); //Horizontal Address end Position (239)
  513. lcd_write_register(0x52, 0x0000); //Vertical Address Start Position
  514. lcd_write_register(0x53, 0x013F); //Vertical Address end Position (319)
  515. lcd_write_register(0x60, 0x2700); //Driver Output Control 2
  516. // lcd_write_register(0x61, 0x0001); //Base Image Display Control
  517. lcd_write_register(0x61, 0x0003); //Base Image Display Control
  518. lcd_write_register(0x6a, 0x0000); //Base Image Display Control
  519. lcd_write_register(0x90, 0x0010); //Panel Interface Control 1
  520. lcd_write_register(0x92, 0x0000); //Panel Interface Control 2
  521. lcd_write_register(0x93, 0x0000); //Panel Interface Control 3
  522. /*** GAMMA Control ***/
  523. lcd_write_register(0x30, 0x0007);
  524. lcd_write_register(0x31, 0x0403);
  525. lcd_write_register(0x32, 0x0404);
  526. lcd_write_register(0x35, 0x0002);
  527. lcd_write_register(0x36, 0x0707);
  528. lcd_write_register(0x37, 0x0606);
  529. lcd_write_register(0x38, 0x0106);
  530. lcd_write_register(0x39, 0x0007);
  531. lcd_write_register(0x3c, 0x0700);
  532. lcd_write_register(0x3d, 0x0707);
  533. lcd_write_register(0x07, 0x0173);
  534. GPIO_HI(LCD_NCS);
  535. msleep(30);
  536. GPIO_LO(LCD_NCS);
  537. }
  538. /*
  539. ili9320_setup_2(struct ili9325 *item){
  540. //DCT=b1010=fosc/4 BT=b001=VGH:+6,VGL:-4
  541. //DC=b1010=fosc/4 AP=b010=small to medium
  542. LCD_SET_REG(ILI9320_REG_POWER_CTRL_1, 0xa2a4);
  543. //VRC=b100:5.5V
  544. LCD_SET_REG(ILI9320_REG_POWER_CTRL_2, 0x0004);
  545. //VRH=b1000:Vref*2.165
  546. LCD_SET_REG(ILI9320_REG_POWER_CTRL_3, 0x0308);
  547. //VCOMG=1 VDV=b1000:VLCD63*1.05
  548. LCD_SET_REG(ILI9320_REG_POWER_CTRL_4, 0x3000);
  549. //nOTP=1 VCM=0x2a:VLCD63*0.77
  550. LCD_SET_REG(ILI9320_REG_POWER_CTRL_5, 0x00ea);
  551. //RL=0 REV=1 CAD=0 BGR=1 SM=0 TB=1 MUX=0x13f=319
  552. LCD_SET_REG(ILI9320_REG_DRIVER_OUT_CTRL, 0x2b3f);
  553. //FLD=0 ENWS=0 D/C=1 EOR=1 WSMD=0 NW=0x00=0
  554. LCD_SET_REG(ILI9320_REG_LCD_DRIVE_AC, 0x0600);
  555. //SLP=0
  556. LCD_SET_REG(ILI9320_REG_SLEEP_MODE, 0x0000);
  557. //VSMode=0 DFM=3:65k TRAMS=0 OEDef=0 WMode=0 Dmode=0
  558. //TY=0 ID=2 AM=1 LG=0 (orig: ID=3, AM=0)
  559. // ssd1289_reg_set(item, SSD1289_REG_ENTRY_MODE, 0x6030);
  560. LCD_SET_REG(ILI9320_REG_ENTRY_MODE, 0x6028);
  561. //PT=0 VLE=1 SPT=0 GON=1 DTE=1 CM=0 D=3
  562. LCD_SET_REG(ILI9320_REG_DISPLAY_CTRL, 0x0233);
  563. //NO=0 SDT=0 EQ=0 DIV=0 SDIV=1 SRTN=1 RTN=9:25 clock
  564. LCD_SET_REG(ILI9320_REG_FRAME_CYCLE, 0x0039);
  565. //SCN=0
  566. LCD_SET_REG(ILI9320_REG_GATE_SCAN_START, 0x0000);
  567. //PKP1=7 PKP0=7
  568. LCD_SET_REG(ILI9320_REG_GAMMA_CTRL_1, 0x0707);
  569. //PKP3=2 PKP2=4
  570. LCD_SET_REG(ILI9320_REG_GAMME_CTRL_2, 0x0204);
  571. //PKP5=2 PKP4=2
  572. LCD_SET_REG(ILI9320_REG_GAMMA_CTRL_3, 0x0204);
  573. //PRP1=5 PRP0=2
  574. LCD_SET_REG(ILI9320_REG_GAMMA_CTRL_4, 0x0502);
  575. //PKN1=5 PKN0=7
  576. LCD_SET_REG(ILI9320_REG_GAMMA_CTRL_5, 0x0507);
  577. //PKN3=2 PNN2=4
  578. LCD_SET_REG(ILI9320_REG_GAMMA_CTRL_6, 0x0204);
  579. //PKN5=2 PKN4=4
  580. LCD_SET_REG(ILI9320_REG_GAMMA_CTRL_7, 0x0204);
  581. //PRN1=5 PRN0=2
  582. LCD_SET_REG(ILI9320_REG_GAMMA_CTRL_8, 0x0502);
  583. //VRP1=3 VRP0=2
  584. LCD_SET_REG(ILI9320_REG_GAMMA_CTRL_9, 0x0302);
  585. //VRN1=3 VRN0=2
  586. LCD_SET_REG(ILI9320_REG_GAMMA_CTRL_10, 0x0302);
  587. //WMR=0 WMG=0
  588. LCD_SET_REG(ILI9320_REG_WR_DATA_MASK_1, 0x0000);
  589. //WMB=0
  590. LCD_SET_REG(ILI9320_REG_WR_DATA_MASK_2, 0x0000);
  591. //OSC=b1010:548k
  592. LCD_SET_REG(ILI9320_REG_FRAME_FREQUENCY, 0xa000);
  593. //SS1=0
  594. LCD_SET_REG(ILI9320_REG_FIRST_WIN_START, 0x0000);
  595. //SE1=319
  596. LCD_SET_REG(ILI9320_REG_FIRST_WIN_END, (item->info->var.xres - 1));
  597. //SS2=0
  598. LCD_SET_REG(ILI9320_REG_SECND_WIN_START, 0x0000);
  599. //SE2=0
  600. LCD_SET_REG(ILI9320_REG_SECND_WIN_END, 0x0000);
  601. //VL1=0
  602. LCD_SET_REG(ILI9320_REG_V_SCROLL_CTRL_1, 0x0000);
  603. //VL2=0
  604. LCD_SET_REG(ILI9320_REG_V_SCROLL_CTRL_2, 0x0000);
  605. //HEA=0xef=239 HSA=0
  606. LCD_SET_REG(ILI9320_REG_H_RAM_ADR_POS, (item->info->var.yres - 1) << 8);
  607. //VSA=0
  608. LCD_SET_REG(ILI9320_REG_V_RAM_ADR_START, 0x0000);
  609. //VEA=0x13f=319
  610. LCD_SET_REG(ILI9320_REG_V_RAM_ADR_END, (item->info->var.xres - 1));
  611. }
  612. */
  613. static void inline ili9325_setup(struct ili9325 *item){
  614. /*** Display Setting ***/
  615. lcd_write_register(0x01, 0x0100); //Driver output control (1)
  616. lcd_write_register(0x02, 0x0700); //LCD driving control
  617. // Check page 55 in the datasheet for more information about alignment
  618. #ifdef UPSIDEDOWN
  619. lcd_write_register(0x03, 0x1018); //Entry mode
  620. #else
  621. lcd_write_register(0x03, 0x1028); //Entry mode
  622. #endif
  623. lcd_write_register(0x04, 0x0000); //Resize control
  624. lcd_write_register(0x08, 0x0207); //Display control (2)
  625. lcd_write_register(0x09, 0x0000); //Display control (3)
  626. lcd_write_register(0x0A, 0x0000);
  627. lcd_write_register(0x0C, 0x0000);
  628. lcd_write_register(0x0D, 0x0000);
  629. lcd_write_register(0x0F, 0x0000);
  630. msleep(50);
  631. /*** Power Control ***/
  632. lcd_write_register(0x10, 0x0000);
  633. lcd_write_register(0x11, 0x0007); //power control 2 DC, VC
  634. lcd_write_register(0x12, 0x0000); //power control 3 VRH
  635. lcd_write_register(0x13, 0x0000); //power control 4 VCOM amplitude
  636. msleep(100);
  637. // lcd_write_register(0x07, 0x0101); //power control 1 BT, AP
  638. // lcd_write_register(0x17, 0x0001);
  639. // msleep(20);
  640. lcd_write_register(0x10, 0x1490); //power control 1 BT,AP
  641. lcd_write_register(0x11, 0x0227); //power control 2 DC,VC
  642. msleep(50);
  643. lcd_write_register(0x12, 0x001c); //power control 3 VRH
  644. msleep(50);
  645. lcd_write_register(0x13, 0x0A00); //power control 4 vcom amplitude
  646. lcd_write_register(0x29, 0x000F); //power control 7 VCOMH
  647. msleep(50);
  648. lcd_write_register(0x20, 0x0000); //Horizontal GRAM Address Set
  649. lcd_write_register(0x21, 0x0000); //Vertical GRAM Address Set
  650. /*** GAMMA Control ***/
  651. lcd_write_register(0x30, 0x0000);
  652. lcd_write_register(0x31, 0x0203);
  653. lcd_write_register(0x32, 0x0001);
  654. lcd_write_register(0x35, 0x0205);
  655. lcd_write_register(0x36, 0x030c);
  656. lcd_write_register(0x37, 0x0607);
  657. lcd_write_register(0x38, 0x0405);
  658. lcd_write_register(0x39, 0x0707);
  659. lcd_write_register(0x3c, 0x0502);
  660. lcd_write_register(0x3d, 0x1008);
  661. msleep(50);
  662. lcd_write_register(0x50, 0x0000); //Horizontal Address Start Position
  663. lcd_write_register(0x51, 0x00EF); //Horizontal Address end Position (239)
  664. lcd_write_register(0x52, 0x0000); //Vertical Address Start Position
  665. lcd_write_register(0x53, 0x013F); //Vertical Address end Position (319)
  666. lcd_write_register(0x60, 0xA700); //Driver Output Control 2
  667. lcd_write_register(0x61, 0x0001); //Base Image Display Control
  668. lcd_write_register(0x6a, 0x0000); //Base Image Display Control
  669. msleep(50);
  670. //-------------- Partial Display Control ---------//
  671. lcd_write_register(0x0080,0);
  672. lcd_write_register(0x0081,0);
  673. lcd_write_register(0x0082,0);
  674. lcd_write_register(0x0083,0);
  675. lcd_write_register(0x0084,0);
  676. lcd_write_register(0x0085,0);
  677. //-------------- Panel Control -------------------//
  678. lcd_write_register(0x90, 0x0010); //Panel Interface Control 1
  679. lcd_write_register(0x92, 0x0600); //Panel Interface Control 2
  680. lcd_write_register(0x93, 0x0003); //Panel Interface Control 3
  681. lcd_write_register(0x95, 0x0110);
  682. lcd_write_register(0x97, 0x0000);
  683. lcd_write_register(0x98, 0x0000);
  684. msleep(50);
  685. // 262K color and display ON
  686. lcd_write_register(0x07, 0x0133);
  687. msleep(100);
  688. }
  689. /*
  690. * Setup 9320/9325 TFT controller
  691. */
  692. static int __init ili932x_setup(struct ili9325 *item){
  693. int i = 0;
  694. int code = 0;
  695. printk(KERN_INFO "ili932x_fb: Setup GPIO ports\n");
  696. ili9325_gpio_init();
  697. while((code = ili932x_init_controller()) == 0){
  698. if(i++ < 100){
  699. printk(KERN_INFO "ili932x_fb: Initializing TFT controller failed, will retry in 3s later\n");
  700. msleep(3000);
  701. }else{
  702. printk(KERN_INFO "ili932x_fb: Initialization aborted\n");
  703. return -1;
  704. }
  705. }
  706. switch(code){
  707. case 0x9320:
  708. ili9320_setup_1(item);
  709. // ili9320_setup_2(item);
  710. // ili9320_setup_3(item);
  711. break;
  712. case 0x9325:
  713. ili9325_setup(item);
  714. break;
  715. default:
  716. // unknow controller code
  717. printk(KERN_INFO "ili932x_fb: Unsupported TFT controller, type code: %#08x\n",code);
  718. return -1;
  719. }
  720. return 0;
  721. }
  722. static inline void ili932x_clearscreen(struct ili9325 *item){
  723. int x;
  724. dev_dbg(item->dev, "%s: item=0x%p\n", __func__, (void *)item);
  725. //Clear screen
  726. ili9325_setptr(item, 0, 0); // LCD_CMD(0x0022) is called already
  727. ili932x_write_begin();
  728. for (x=0; x<320*240; x++){
  729. ili932x_write_unsafe(0,LCD_DATA);
  730. }
  731. ili932x_write_end();
  732. ili9325_setptr(item, 0, 0);
  733. ili932x_write_begin();
  734. for (x=0; x<320*240; x++){
  735. ili932x_write_unsafe(item->pages[0].buffer[x],LCD_DATA);
  736. }
  737. ili932x_write_end();
  738. }
  739. //This routine will allocate the buffer for the complete framebuffer. This
  740. //is one continuous chunk of 16-bit pixel values; userspace programs
  741. //will write here.
  742. static int __init ili9325_video_alloc(struct ili9325 *item)
  743. {
  744. unsigned int frame_size;
  745. dev_dbg(item->dev, "%s: item=0x%p\n", __func__, (void *)item);
  746. frame_size = item->info->fix.line_length * item->info->var.yres;
  747. dev_dbg(item->dev, "%s: item=0x%p frame_size=%u\n",
  748. __func__, (void *)item, frame_size);
  749. item->pages_count = frame_size / PAGE_SIZE;
  750. if ((item->pages_count * PAGE_SIZE) < frame_size) {
  751. item->pages_count++;
  752. }
  753. dev_dbg(item->dev, "%s: item=0x%p pages_count=%u\n",
  754. __func__, (void *)item, item->pages_count);
  755. item->info->fix.smem_len = item->pages_count * PAGE_SIZE;
  756. item->info->fix.smem_start =
  757. (unsigned long)vmalloc(item->info->fix.smem_len);
  758. if (!item->info->fix.smem_start) {
  759. dev_err(item->dev, "%s: unable to vmalloc\n", __func__);
  760. return -ENOMEM;
  761. }
  762. memset((void *)item->info->fix.smem_start, 0, item->info->fix.smem_len);
  763. return 0;
  764. }
  765. static void ili9325_video_free(struct ili9325 *item)
  766. {
  767. dev_dbg(item->dev, "%s: item=0x%p\n", __func__, (void *)item);
  768. kfree((void *)item->info->fix.smem_start);
  769. }
  770. //This routine will allocate a ili9325_page struct for each vm page in the
  771. //main framebuffer memory. Each struct will contain a pointer to the page
  772. //start, an x- and y-offset, and the length of the pagebuffer which is in the framebuffer.
  773. static int __init ili9325_pages_alloc(struct ili9325 *item)
  774. {
  775. unsigned short pixels_per_page;
  776. unsigned short yoffset_per_page;
  777. unsigned short xoffset_per_page;
  778. unsigned int index;
  779. unsigned short x = 0;
  780. unsigned short y = 0;
  781. unsigned short *buffer;
  782. unsigned short *oldbuffer;
  783. unsigned int len;
  784. dev_dbg(item->dev, "%s: item=0x%p\n", __func__, (void *)item);
  785. item->pages = kmalloc(item->pages_count * sizeof(struct ili9325_page),
  786. GFP_KERNEL);
  787. if (!item->pages) {
  788. dev_err(item->dev, "%s: unable to kmalloc for ili9325_page\n",
  789. __func__);
  790. return -ENOMEM;
  791. }
  792. pixels_per_page = PAGE_SIZE / (item->info->var.bits_per_pixel / 8);
  793. yoffset_per_page = pixels_per_page / item->info->var.xres;
  794. xoffset_per_page = pixels_per_page -
  795. (yoffset_per_page * item->info->var.xres);
  796. dev_dbg(item->dev, "%s: item=0x%p pixels_per_page=%hu "
  797. "yoffset_per_page=%hu xoffset_per_page=%hu\n",
  798. __func__, (void *)item, pixels_per_page,
  799. yoffset_per_page, xoffset_per_page);
  800. oldbuffer = kmalloc(item->pages_count * pixels_per_page * 2,
  801. GFP_KERNEL);
  802. if (!oldbuffer) {
  803. dev_err(item->dev, "%s: unable to kmalloc for ili9325_page oldbuffer\n",
  804. __func__);
  805. return -ENOMEM;
  806. }
  807. buffer = (unsigned short *)item->info->fix.smem_start;
  808. for (index = 0; index < item->pages_count; index++) {
  809. len = (item->info->var.xres * item->info->var.yres) -
  810. (index * pixels_per_page);
  811. if (len > pixels_per_page) {
  812. len = pixels_per_page;
  813. }
  814. dev_dbg(item->dev,
  815. "%s: page[%d]: x=%3hu y=%3hu buffer=0x%p len=%3hu\n",
  816. __func__, index, x, y, buffer, len);
  817. item->pages[index].x = x;
  818. item->pages[index].y = y;
  819. item->pages[index].buffer = buffer;
  820. item->pages[index].oldbuffer = oldbuffer;
  821. item->pages[index].len = len;
  822. x += xoffset_per_page;
  823. if (x >= item->info->var.xres) {
  824. y++;
  825. x -= item->info->var.xres;
  826. }
  827. y += yoffset_per_page;
  828. buffer += pixels_per_page;
  829. oldbuffer += pixels_per_page;
  830. }
  831. return 0;
  832. }
  833. static void ili9325_pages_free(struct ili9325 *item)
  834. {
  835. dev_dbg(item->dev, "%s: item=0x%p\n", __func__, (void *)item);
  836. kfree(item->pages);
  837. }
  838. static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
  839. {
  840. return ((val<<width) + 0x7FFF - val)>>16;
  841. }
  842. //This routine is needed because the console driver won't work without it.
  843. static int ili9325_setcolreg(unsigned regno,
  844. unsigned red, unsigned green, unsigned blue,
  845. unsigned transp, struct fb_info *info)
  846. {
  847. int ret = 1;
  848. /*
  849. * If greyscale is true, then we convert the RGB value
  850. * to greyscale no matter what visual we are using.
  851. */
  852. if (info->var.grayscale)
  853. red = green = blue = (19595 * red + 38470 * green +
  854. 7471 * blue) >> 16;
  855. switch (info->fix.visual) {
  856. case FB_VISUAL_TRUECOLOR:
  857. if (regno < 16) {
  858. u32 *pal = info->pseudo_palette;
  859. u32 value;
  860. red = CNVT_TOHW(red, info->var.red.length);
  861. green = CNVT_TOHW(green, info->var.green.length);
  862. blue = CNVT_TOHW(blue, info->var.blue.length);
  863. transp = CNVT_TOHW(transp, info->var.transp.length);
  864. value = (red << info->var.red.offset) |
  865. (green << info->var.green.offset) |
  866. (blue << info->var.blue.offset) |
  867. (transp << info->var.transp.offset);
  868. pal[regno] = value;
  869. ret = 0;
  870. }
  871. break;
  872. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  873. case FB_VISUAL_PSEUDOCOLOR:
  874. break;
  875. }
  876. return ret;
  877. }
  878. static int ili9325_blank(int blank_mode, struct fb_info *info)
  879. {
  880. struct ili9325 *item = (struct ili9325 *)info->par;
  881. if (blank_mode == FB_BLANK_UNBLANK)
  882. item->backlight=1;
  883. else
  884. item->backlight=0;
  885. //Item->backlight won't take effect until the LCD is written to. Force that
  886. //by dirty'ing a page.
  887. item->pages[0].must_update=1;
  888. schedule_delayed_work(&info->deferred_work, 0);
  889. return 0;
  890. }
  891. static void ili9325_touch(struct fb_info *info, int x, int y, int w, int h)
  892. {
  893. struct fb_deferred_io *fbdefio = info->fbdefio;
  894. struct ili9325 *item = (struct ili9325 *)info->par;
  895. int i, ystart, yend;
  896. if (fbdefio) {
  897. //Touch the pages the y-range hits, so the deferred io will update them.
  898. for (i=0; i<item->pages_count; i++) {
  899. ystart=item->pages[i].y;
  900. yend=item->pages[i].y+(item->pages[i].len/info->fix.line_length)+1;
  901. if (!((y+h)<ystart || y>yend)) {
  902. item->pages[i].must_update=1;
  903. }
  904. }
  905. //Schedule the deferred IO to kick in after a delay.
  906. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  907. }
  908. }
  909. static void ili9325_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
  910. {
  911. sys_fillrect(p, rect);
  912. ili9325_touch(p, rect->dx, rect->dy, rect->width, rect->height);
  913. }
  914. static void ili9325_imageblit(struct fb_info *p, const struct fb_image *image)
  915. {
  916. sys_imageblit(p, image);
  917. ili9325_touch(p, image->dx, image->dy, image->width, image->height);
  918. }
  919. static void ili9325_copyarea(struct fb_info *p, const struct fb_copyarea *area)
  920. {
  921. sys_copyarea(p, area);
  922. ili9325_touch(p, area->dx, area->dy, area->width, area->height);
  923. }
  924. static ssize_t ili9325_write(struct fb_info *p, const char __user *buf,
  925. size_t count, loff_t *ppos)
  926. {
  927. ssize_t res;
  928. res = fb_sys_write(p, buf, count, ppos);
  929. ili9325_touch(p, 0, 0, p->var.xres, p->var.yres);
  930. return res;
  931. }
  932. static struct fb_ops ili9325_fbops = {
  933. .owner = THIS_MODULE,
  934. .fb_read = fb_sys_read,
  935. .fb_write = ili9325_write,
  936. .fb_fillrect = ili9325_fillrect,
  937. .fb_copyarea = ili9325_copyarea,
  938. .fb_imageblit = ili9325_imageblit,
  939. .fb_setcolreg = ili9325_setcolreg,
  940. .fb_blank = ili9325_blank,
  941. };
  942. static struct fb_fix_screeninfo ili9325_fix __initdata = {
  943. .id = "ILI9325",
  944. .type = FB_TYPE_PACKED_PIXELS,
  945. .visual = FB_VISUAL_TRUECOLOR,
  946. .accel = FB_ACCEL_NONE,
  947. .line_length = 320 * 2,
  948. };
  949. static struct fb_var_screeninfo ili9325_var __initdata = {
  950. .xres = 320,
  951. .yres = 240,
  952. .xres_virtual = 320,
  953. .yres_virtual = 240,
  954. .width = 320,
  955. .height = 240,
  956. .bits_per_pixel = 16,
  957. .red = {11, 5, 0},
  958. .green = {5, 6, 0},
  959. .blue = {0, 5, 0},
  960. .activate = FB_ACTIVATE_NOW,
  961. .vmode = FB_VMODE_NONINTERLACED,
  962. };
  963. static struct fb_deferred_io ili9325_defio = {
  964. .delay = HZ / 25,
  965. .deferred_io = &ili9325_update,
  966. };
  967. static int __init ili9325_probe(struct platform_device *dev)
  968. {
  969. int ret = 0;
  970. struct ili9325 *item;
  971. struct fb_info *info;
  972. dev_dbg(&dev->dev, "%s\n", __func__);
  973. printk(KERN_INFO "ili932x_fb: Driver start up\n");
  974. item = kzalloc(sizeof(struct ili9325), GFP_KERNEL);
  975. if (!item) {
  976. dev_err(&dev->dev,
  977. "%s: unable to kzalloc for ili9325\n", __func__);
  978. ret = -ENOMEM;
  979. goto out;
  980. }
  981. item->dev = &dev->dev;
  982. dev_set_drvdata(&dev->dev, item);
  983. item->backlight=1;
  984. // Prepare framebuffer
  985. info = framebuffer_alloc(sizeof(struct ili9325), &dev->dev);
  986. if (!info) {
  987. ret = -ENOMEM;
  988. dev_err(&dev->dev,
  989. "%s: unable to framebuffer_alloc\n", __func__);
  990. goto out_item;
  991. }
  992. info->pseudo_palette = &item->pseudo_palette;
  993. item->info = info;
  994. info->par = item;
  995. info->dev = &dev->dev;
  996. info->fbops = &ili9325_fbops;
  997. info->flags = FBINFO_FLAG_DEFAULT|FBINFO_VIRTFB;
  998. info->fix = ili9325_fix;
  999. info->var = ili9325_var;
  1000. ret = ili9325_video_alloc(item);
  1001. if (ret) {
  1002. dev_err(&dev->dev,
  1003. "%s: unable to ili9325_video_alloc\n", __func__);
  1004. goto out_info;
  1005. }
  1006. info->screen_base = (char __iomem *)item->info->fix.smem_start;
  1007. ret = ili9325_pages_alloc(item);
  1008. if (ret < 0) {
  1009. dev_err(&dev->dev,
  1010. "%s: unable to ili9325_pages_init\n", __func__);
  1011. goto out_video;
  1012. }
  1013. info->fbdefio = &ili9325_defio;
  1014. fb_deferred_io_init(info);
  1015. // setup the controller
  1016. if(0 != ili932x_setup(item)){
  1017. printk(KERN_WARNING "ili932x_fb: No ili932x TFT/LCD controller found\n");
  1018. goto out_pages;
  1019. }
  1020. // driver initialized
  1021. printk(KERN_INFO "ili932x_fb: Clear screen\n");
  1022. ili932x_clearscreen(item);
  1023. ili9325_update_all(item);
  1024. // now we can register framebuffer
  1025. ret = register_framebuffer(info);
  1026. if (ret < 0) {
  1027. dev_err(&dev->dev,
  1028. "%s: unable to register_frambuffer\n", __func__);
  1029. goto out_pages;
  1030. }
  1031. return ret;
  1032. out_pages:
  1033. ili9325_pages_free(item);
  1034. out_video:
  1035. ili9325_video_free(item);
  1036. out_info:
  1037. framebuffer_release(info);
  1038. out_item:
  1039. kfree(item);
  1040. out:
  1041. return ret;
  1042. }
  1043. static int ili9325_remove(struct platform_device *dev)
  1044. {
  1045. struct fb_info *info = dev_get_drvdata(&dev->dev);
  1046. struct ili9325 *item = (struct ili9325 *)info->par;
  1047. if(item){
  1048. unregister_framebuffer(info);
  1049. ili9325_pages_free(item);
  1050. ili9325_video_free(item);
  1051. framebuffer_release(info);
  1052. kfree(item);
  1053. }
  1054. return 0;
  1055. }
  1056. #ifdef CONFIG_PM
  1057. static int ili9325_suspend(struct platform_device *dev, pm_message_t state)
  1058. {
  1059. // struct fb_info *info = dev_get_drvdata(&spi->dev);
  1060. // struct ili9325 *item = (struct ili9325 *)info->par;
  1061. /* enter into sleep mode */
  1062. // ili9325_reg_set(item, ILI9325_REG_SLEEP_MODE, 0x0001);
  1063. return 0;
  1064. }
  1065. static int ili9325_resume(struct platform_device *dev)
  1066. {
  1067. // struct fb_info *info = dev_get_drvdata(&spi->dev);
  1068. // struct ili9325 *item = (struct ili9325 *)info->par;
  1069. /* leave sleep mode */
  1070. // ili9325_reg_set(item, ILI9325_REG_SLEEP_MODE, 0x0000);
  1071. return 0;
  1072. }
  1073. #else
  1074. #define ili9325_suspend NULL
  1075. #define ili9325_resume NULL
  1076. #endif
  1077. static struct platform_driver ili9325_driver = {
  1078. .probe = ili9325_probe,
  1079. .driver = {
  1080. .name = "ili9325",
  1081. },
  1082. };
  1083. static int __init ili9325_init(void)
  1084. {
  1085. int ret = 0;
  1086. pr_debug("%s\n", __func__);
  1087. ret = platform_driver_register(&ili9325_driver);
  1088. if (ret) {
  1089. pr_err("%s: unable to platform_driver_register\n", __func__);
  1090. }
  1091. return ret;
  1092. }
  1093. module_init(ili9325_init);
  1094. module_remove(ili9325_remove);
  1095. MODULE_DESCRIPTION("ILI932X LCD Driver");
  1096. MODULE_AUTHOR("Jeroen Domburg <jeroen@spritesmods.com>, Shawn Chain <shawn.chain@gmail.com>");
  1097. MODULE_LICENSE("GPL");