bf537-lq035.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*
  2. * Analog Devices Blackfin(BF537 STAMP) + SHARP TFT LCD.
  3. * http://docs.blackfin.uclinux.org/doku.php?id=hw:cards:tft-lcd
  4. *
  5. * Copyright 2006-2010 Analog Devices Inc.
  6. * Licensed under the GPL-2.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/string.h>
  13. #include <linux/mm.h>
  14. #include <linux/delay.h>
  15. #include <linux/fb.h>
  16. #include <linux/ioport.h>
  17. #include <linux/init.h>
  18. #include <linux/types.h>
  19. #include <linux/gpio.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/sched.h>
  22. #include <linux/timer.h>
  23. #include <linux/device.h>
  24. #include <linux/backlight.h>
  25. #include <linux/lcd.h>
  26. #include <linux/i2c.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/slab.h>
  30. #include <linux/platform_device.h>
  31. #include <asm/blackfin.h>
  32. #include <asm/irq.h>
  33. #include <asm/dpmc.h>
  34. #include <asm/dma.h>
  35. #include <asm/portmux.h>
  36. #define NO_BL 1
  37. #define MAX_BRIGHENESS 95
  38. #define MIN_BRIGHENESS 5
  39. #define NBR_PALETTE 256
  40. static const unsigned short ppi_pins[] = {
  41. P_PPI0_CLK, P_PPI0_D0, P_PPI0_D1, P_PPI0_D2, P_PPI0_D3,
  42. P_PPI0_D4, P_PPI0_D5, P_PPI0_D6, P_PPI0_D7,
  43. P_PPI0_D8, P_PPI0_D9, P_PPI0_D10, P_PPI0_D11,
  44. P_PPI0_D12, P_PPI0_D13, P_PPI0_D14, P_PPI0_D15, 0
  45. };
  46. static unsigned char *fb_buffer; /* RGB Buffer */
  47. static unsigned long *dma_desc_table;
  48. static int t_conf_done, lq035_open_cnt;
  49. static DEFINE_SPINLOCK(bfin_lq035_lock);
  50. static int landscape;
  51. module_param(landscape, int, 0);
  52. MODULE_PARM_DESC(landscape,
  53. "LANDSCAPE use 320x240 instead of Native 240x320 Resolution");
  54. static int bgr;
  55. module_param(bgr, int, 0);
  56. MODULE_PARM_DESC(bgr,
  57. "BGR use 16-bit BGR-565 instead of RGB-565");
  58. static int nocursor = 1;
  59. module_param(nocursor, int, 0644);
  60. MODULE_PARM_DESC(nocursor, "cursor enable/disable");
  61. static unsigned long current_brightness; /* backlight */
  62. /* AD5280 vcomm */
  63. static unsigned char vcomm_value = 150;
  64. static struct i2c_client *ad5280_client;
  65. static void set_vcomm(void)
  66. {
  67. int nr;
  68. if (!ad5280_client)
  69. return;
  70. nr = i2c_smbus_write_byte_data(ad5280_client, 0x00, vcomm_value);
  71. if (nr)
  72. pr_err("i2c_smbus_write_byte_data fail: %d\n", nr);
  73. }
  74. static int __devinit ad5280_probe(struct i2c_client *client,
  75. const struct i2c_device_id *id)
  76. {
  77. int ret;
  78. if (!i2c_check_functionality(client->adapter,
  79. I2C_FUNC_SMBUS_BYTE_DATA)) {
  80. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  81. return -EIO;
  82. }
  83. ret = i2c_smbus_write_byte_data(client, 0x00, vcomm_value);
  84. if (ret) {
  85. dev_err(&client->dev, "write fail: %d\n", ret);
  86. return ret;
  87. }
  88. ad5280_client = client;
  89. return 0;
  90. }
  91. static int __devexit ad5280_remove(struct i2c_client *client)
  92. {
  93. ad5280_client = NULL;
  94. return 0;
  95. }
  96. static const struct i2c_device_id ad5280_id[] = {
  97. {"bf537-lq035-ad5280", 0},
  98. {}
  99. };
  100. MODULE_DEVICE_TABLE(i2c, ad5280_id);
  101. static struct i2c_driver ad5280_driver = {
  102. .driver = {
  103. .name = "bf537-lq035-ad5280",
  104. },
  105. .probe = ad5280_probe,
  106. .remove = __devexit_p(ad5280_remove),
  107. .id_table = ad5280_id,
  108. };
  109. #ifdef CONFIG_PNAV10
  110. #define MOD GPIO_PH13
  111. #define bfin_write_TIMER_LP_CONFIG bfin_write_TIMER0_CONFIG
  112. #define bfin_write_TIMER_LP_WIDTH bfin_write_TIMER0_WIDTH
  113. #define bfin_write_TIMER_LP_PERIOD bfin_write_TIMER0_PERIOD
  114. #define bfin_read_TIMER_LP_COUNTER bfin_read_TIMER0_COUNTER
  115. #define TIMDIS_LP TIMDIS0
  116. #define TIMEN_LP TIMEN0
  117. #define bfin_write_TIMER_SPS_CONFIG bfin_write_TIMER1_CONFIG
  118. #define bfin_write_TIMER_SPS_WIDTH bfin_write_TIMER1_WIDTH
  119. #define bfin_write_TIMER_SPS_PERIOD bfin_write_TIMER1_PERIOD
  120. #define TIMDIS_SPS TIMDIS1
  121. #define TIMEN_SPS TIMEN1
  122. #define bfin_write_TIMER_SP_CONFIG bfin_write_TIMER5_CONFIG
  123. #define bfin_write_TIMER_SP_WIDTH bfin_write_TIMER5_WIDTH
  124. #define bfin_write_TIMER_SP_PERIOD bfin_write_TIMER5_PERIOD
  125. #define TIMDIS_SP TIMDIS5
  126. #define TIMEN_SP TIMEN5
  127. #define bfin_write_TIMER_PS_CLS_CONFIG bfin_write_TIMER2_CONFIG
  128. #define bfin_write_TIMER_PS_CLS_WIDTH bfin_write_TIMER2_WIDTH
  129. #define bfin_write_TIMER_PS_CLS_PERIOD bfin_write_TIMER2_PERIOD
  130. #define TIMDIS_PS_CLS TIMDIS2
  131. #define TIMEN_PS_CLS TIMEN2
  132. #define bfin_write_TIMER_REV_CONFIG bfin_write_TIMER3_CONFIG
  133. #define bfin_write_TIMER_REV_WIDTH bfin_write_TIMER3_WIDTH
  134. #define bfin_write_TIMER_REV_PERIOD bfin_write_TIMER3_PERIOD
  135. #define TIMDIS_REV TIMDIS3
  136. #define TIMEN_REV TIMEN3
  137. #define bfin_read_TIMER_REV_COUNTER bfin_read_TIMER3_COUNTER
  138. #define FREQ_PPI_CLK (5*1024*1024) /* PPI_CLK 5MHz */
  139. #define TIMERS {P_TMR0, P_TMR1, P_TMR2, P_TMR3, P_TMR5, 0}
  140. #else
  141. #define UD GPIO_PF13 /* Up / Down */
  142. #define MOD GPIO_PF10
  143. #define LBR GPIO_PF14 /* Left Right */
  144. #define bfin_write_TIMER_LP_CONFIG bfin_write_TIMER6_CONFIG
  145. #define bfin_write_TIMER_LP_WIDTH bfin_write_TIMER6_WIDTH
  146. #define bfin_write_TIMER_LP_PERIOD bfin_write_TIMER6_PERIOD
  147. #define bfin_read_TIMER_LP_COUNTER bfin_read_TIMER6_COUNTER
  148. #define TIMDIS_LP TIMDIS6
  149. #define TIMEN_LP TIMEN6
  150. #define bfin_write_TIMER_SPS_CONFIG bfin_write_TIMER1_CONFIG
  151. #define bfin_write_TIMER_SPS_WIDTH bfin_write_TIMER1_WIDTH
  152. #define bfin_write_TIMER_SPS_PERIOD bfin_write_TIMER1_PERIOD
  153. #define TIMDIS_SPS TIMDIS1
  154. #define TIMEN_SPS TIMEN1
  155. #define bfin_write_TIMER_SP_CONFIG bfin_write_TIMER0_CONFIG
  156. #define bfin_write_TIMER_SP_WIDTH bfin_write_TIMER0_WIDTH
  157. #define bfin_write_TIMER_SP_PERIOD bfin_write_TIMER0_PERIOD
  158. #define TIMDIS_SP TIMDIS0
  159. #define TIMEN_SP TIMEN0
  160. #define bfin_write_TIMER_PS_CLS_CONFIG bfin_write_TIMER7_CONFIG
  161. #define bfin_write_TIMER_PS_CLS_WIDTH bfin_write_TIMER7_WIDTH
  162. #define bfin_write_TIMER_PS_CLS_PERIOD bfin_write_TIMER7_PERIOD
  163. #define TIMDIS_PS_CLS TIMDIS7
  164. #define TIMEN_PS_CLS TIMEN7
  165. #define bfin_write_TIMER_REV_CONFIG bfin_write_TIMER5_CONFIG
  166. #define bfin_write_TIMER_REV_WIDTH bfin_write_TIMER5_WIDTH
  167. #define bfin_write_TIMER_REV_PERIOD bfin_write_TIMER5_PERIOD
  168. #define TIMDIS_REV TIMDIS5
  169. #define TIMEN_REV TIMEN5
  170. #define bfin_read_TIMER_REV_COUNTER bfin_read_TIMER5_COUNTER
  171. #define FREQ_PPI_CLK (6*1000*1000) /* PPI_CLK 6MHz */
  172. #define TIMERS {P_TMR0, P_TMR1, P_TMR5, P_TMR6, P_TMR7, 0}
  173. #endif
  174. #define LCD_X_RES 240 /* Horizontal Resolution */
  175. #define LCD_Y_RES 320 /* Vertical Resolution */
  176. #define LCD_BBP 16 /* Bit Per Pixel */
  177. /* the LCD and the DMA start counting differently;
  178. * since one starts at 0 and the other starts at 1,
  179. * we have a difference of 1 between START_LINES
  180. * and U_LINES.
  181. */
  182. #define START_LINES 8 /* lines for field flyback or field blanking signal */
  183. #define U_LINES 9 /* number of undisplayed blanking lines */
  184. #define FRAMES_PER_SEC (60)
  185. #define DCLKS_PER_FRAME (FREQ_PPI_CLK/FRAMES_PER_SEC)
  186. #define DCLKS_PER_LINE (DCLKS_PER_FRAME/(LCD_Y_RES+U_LINES))
  187. #define PPI_CONFIG_VALUE (PORT_DIR|XFR_TYPE|DLEN_16|POLS)
  188. #define PPI_DELAY_VALUE (0)
  189. #define TIMER_CONFIG (PWM_OUT|PERIOD_CNT|TIN_SEL|CLK_SEL)
  190. #define ACTIVE_VIDEO_MEM_OFFSET (LCD_X_RES*START_LINES*(LCD_BBP/8))
  191. #define ACTIVE_VIDEO_MEM_SIZE (LCD_Y_RES*LCD_X_RES*(LCD_BBP/8))
  192. #define TOTAL_VIDEO_MEM_SIZE ((LCD_Y_RES+U_LINES)*LCD_X_RES*(LCD_BBP/8))
  193. #define TOTAL_DMA_DESC_SIZE (2 * sizeof(u32) * (LCD_Y_RES + U_LINES))
  194. static void start_timers(void) /* CHECK with HW */
  195. {
  196. unsigned long flags;
  197. local_irq_save(flags);
  198. bfin_write_TIMER_ENABLE(TIMEN_REV);
  199. SSYNC();
  200. while (bfin_read_TIMER_REV_COUNTER() <= 11)
  201. continue;
  202. bfin_write_TIMER_ENABLE(TIMEN_LP);
  203. SSYNC();
  204. while (bfin_read_TIMER_LP_COUNTER() < 3)
  205. continue;
  206. bfin_write_TIMER_ENABLE(TIMEN_SP|TIMEN_SPS|TIMEN_PS_CLS);
  207. SSYNC();
  208. t_conf_done = 1;
  209. local_irq_restore(flags);
  210. }
  211. static void config_timers(void)
  212. {
  213. /* Stop timers */
  214. bfin_write_TIMER_DISABLE(TIMDIS_SP|TIMDIS_SPS|TIMDIS_REV|
  215. TIMDIS_LP|TIMDIS_PS_CLS);
  216. SSYNC();
  217. /* LP, timer 6 */
  218. bfin_write_TIMER_LP_CONFIG(TIMER_CONFIG|PULSE_HI);
  219. bfin_write_TIMER_LP_WIDTH(1);
  220. bfin_write_TIMER_LP_PERIOD(DCLKS_PER_LINE);
  221. SSYNC();
  222. /* SPS, timer 1 */
  223. bfin_write_TIMER_SPS_CONFIG(TIMER_CONFIG|PULSE_HI);
  224. bfin_write_TIMER_SPS_WIDTH(DCLKS_PER_LINE*2);
  225. bfin_write_TIMER_SPS_PERIOD((DCLKS_PER_LINE * (LCD_Y_RES+U_LINES)));
  226. SSYNC();
  227. /* SP, timer 0 */
  228. bfin_write_TIMER_SP_CONFIG(TIMER_CONFIG|PULSE_HI);
  229. bfin_write_TIMER_SP_WIDTH(1);
  230. bfin_write_TIMER_SP_PERIOD(DCLKS_PER_LINE);
  231. SSYNC();
  232. /* PS & CLS, timer 7 */
  233. bfin_write_TIMER_PS_CLS_CONFIG(TIMER_CONFIG);
  234. bfin_write_TIMER_PS_CLS_WIDTH(LCD_X_RES + START_LINES);
  235. bfin_write_TIMER_PS_CLS_PERIOD(DCLKS_PER_LINE);
  236. SSYNC();
  237. #ifdef NO_BL
  238. /* REV, timer 5 */
  239. bfin_write_TIMER_REV_CONFIG(TIMER_CONFIG|PULSE_HI);
  240. bfin_write_TIMER_REV_WIDTH(DCLKS_PER_LINE);
  241. bfin_write_TIMER_REV_PERIOD(DCLKS_PER_LINE*2);
  242. SSYNC();
  243. #endif
  244. }
  245. static void config_ppi(void)
  246. {
  247. bfin_write_PPI_DELAY(PPI_DELAY_VALUE);
  248. bfin_write_PPI_COUNT(LCD_X_RES-1);
  249. /* 0x10 -> PORT_CFG -> 2 or 3 frame syncs */
  250. bfin_write_PPI_CONTROL((PPI_CONFIG_VALUE|0x10) & (~POLS));
  251. }
  252. static int config_dma(void)
  253. {
  254. u32 i;
  255. if (landscape) {
  256. for (i = 0; i < U_LINES; ++i) {
  257. /* blanking lines point to first line of fb_buffer */
  258. dma_desc_table[2*i] = (unsigned long)&dma_desc_table[2*i+2];
  259. dma_desc_table[2*i+1] = (unsigned long)fb_buffer;
  260. }
  261. for (i = U_LINES; i < U_LINES + LCD_Y_RES; ++i) {
  262. /* visible lines */
  263. dma_desc_table[2*i] = (unsigned long)&dma_desc_table[2*i+2];
  264. dma_desc_table[2*i+1] = (unsigned long)fb_buffer +
  265. (LCD_Y_RES+U_LINES-1-i)*2;
  266. }
  267. /* last descriptor points to first */
  268. dma_desc_table[2*(LCD_Y_RES+U_LINES-1)] = (unsigned long)&dma_desc_table[0];
  269. set_dma_x_count(CH_PPI, LCD_X_RES);
  270. set_dma_x_modify(CH_PPI, LCD_Y_RES * (LCD_BBP / 8));
  271. set_dma_y_count(CH_PPI, 0);
  272. set_dma_y_modify(CH_PPI, 0);
  273. set_dma_next_desc_addr(CH_PPI, (void *)dma_desc_table[0]);
  274. set_dma_config(CH_PPI, DMAFLOW_LARGE | NDSIZE_4 | WDSIZE_16);
  275. } else {
  276. set_dma_config(CH_PPI, set_bfin_dma_config(DIR_READ,
  277. DMA_FLOW_AUTO,
  278. INTR_DISABLE,
  279. DIMENSION_2D,
  280. DATA_SIZE_16,
  281. DMA_NOSYNC_KEEP_DMA_BUF));
  282. set_dma_x_count(CH_PPI, LCD_X_RES);
  283. set_dma_x_modify(CH_PPI, LCD_BBP / 8);
  284. set_dma_y_count(CH_PPI, LCD_Y_RES+U_LINES);
  285. set_dma_y_modify(CH_PPI, LCD_BBP / 8);
  286. set_dma_start_addr(CH_PPI, (unsigned long) fb_buffer);
  287. }
  288. return 0;
  289. }
  290. static int __devinit request_ports(void)
  291. {
  292. u16 tmr_req[] = TIMERS;
  293. /*
  294. UD: PF13
  295. MOD: PF10
  296. LBR: PF14
  297. PPI_CLK: PF15
  298. */
  299. if (peripheral_request_list(ppi_pins, KBUILD_MODNAME)) {
  300. pr_err("requesting PPI peripheral failed\n");
  301. return -EBUSY;
  302. }
  303. if (peripheral_request_list(tmr_req, KBUILD_MODNAME)) {
  304. peripheral_free_list(ppi_pins);
  305. pr_err("requesting timer peripheral failed\n");
  306. return -EBUSY;
  307. }
  308. #if (defined(UD) && defined(LBR))
  309. if (gpio_request(UD, KBUILD_MODNAME)) {
  310. pr_err("requesting GPIO %d failed\n", UD);
  311. return -EBUSY;
  312. }
  313. if (gpio_request(LBR, KBUILD_MODNAME)) {
  314. pr_err("requesting GPIO %d failed\n", LBR);
  315. gpio_free(UD);
  316. return -EBUSY;
  317. }
  318. gpio_direction_output(UD, 0);
  319. gpio_direction_output(LBR, 1);
  320. #endif
  321. if (gpio_request(MOD, KBUILD_MODNAME)) {
  322. pr_err("requesting GPIO %d failed\n", MOD);
  323. #if (defined(UD) && defined(LBR))
  324. gpio_free(LBR);
  325. gpio_free(UD);
  326. #endif
  327. return -EBUSY;
  328. }
  329. gpio_direction_output(MOD, 1);
  330. SSYNC();
  331. return 0;
  332. }
  333. static void free_ports(void)
  334. {
  335. u16 tmr_req[] = TIMERS;
  336. peripheral_free_list(ppi_pins);
  337. peripheral_free_list(tmr_req);
  338. #if defined(UD) && defined(LBR)
  339. gpio_free(LBR);
  340. gpio_free(UD);
  341. #endif
  342. gpio_free(MOD);
  343. }
  344. static struct fb_info bfin_lq035_fb;
  345. static struct fb_var_screeninfo bfin_lq035_fb_defined = {
  346. .bits_per_pixel = LCD_BBP,
  347. .activate = FB_ACTIVATE_TEST,
  348. .xres = LCD_X_RES, /*default portrait mode RGB*/
  349. .yres = LCD_Y_RES,
  350. .xres_virtual = LCD_X_RES,
  351. .yres_virtual = LCD_Y_RES,
  352. .height = -1,
  353. .width = -1,
  354. .left_margin = 0,
  355. .right_margin = 0,
  356. .upper_margin = 0,
  357. .lower_margin = 0,
  358. .red = {11, 5, 0},
  359. .green = {5, 6, 0},
  360. .blue = {0, 5, 0},
  361. .transp = {0, 0, 0},
  362. };
  363. static struct fb_fix_screeninfo bfin_lq035_fb_fix __devinitdata = {
  364. .id = KBUILD_MODNAME,
  365. .smem_len = ACTIVE_VIDEO_MEM_SIZE,
  366. .type = FB_TYPE_PACKED_PIXELS,
  367. .visual = FB_VISUAL_TRUECOLOR,
  368. .xpanstep = 0,
  369. .ypanstep = 0,
  370. .line_length = LCD_X_RES*(LCD_BBP/8),
  371. .accel = FB_ACCEL_NONE,
  372. };
  373. static int bfin_lq035_fb_open(struct fb_info *info, int user)
  374. {
  375. unsigned long flags;
  376. spin_lock_irqsave(&bfin_lq035_lock, flags);
  377. lq035_open_cnt++;
  378. spin_unlock_irqrestore(&bfin_lq035_lock, flags);
  379. if (lq035_open_cnt <= 1) {
  380. bfin_write_PPI_CONTROL(0);
  381. SSYNC();
  382. set_vcomm();
  383. config_dma();
  384. config_ppi();
  385. /* start dma */
  386. enable_dma(CH_PPI);
  387. SSYNC();
  388. bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() | PORT_EN);
  389. SSYNC();
  390. if (!t_conf_done) {
  391. config_timers();
  392. start_timers();
  393. }
  394. /* gpio_set_value(MOD,1); */
  395. }
  396. return 0;
  397. }
  398. static int bfin_lq035_fb_release(struct fb_info *info, int user)
  399. {
  400. unsigned long flags;
  401. spin_lock_irqsave(&bfin_lq035_lock, flags);
  402. lq035_open_cnt--;
  403. spin_unlock_irqrestore(&bfin_lq035_lock, flags);
  404. if (lq035_open_cnt <= 0) {
  405. bfin_write_PPI_CONTROL(0);
  406. SSYNC();
  407. disable_dma(CH_PPI);
  408. }
  409. return 0;
  410. }
  411. static int bfin_lq035_fb_check_var(struct fb_var_screeninfo *var,
  412. struct fb_info *info)
  413. {
  414. switch (var->bits_per_pixel) {
  415. case 16:/* DIRECTCOLOUR, 64k */
  416. var->red.offset = info->var.red.offset;
  417. var->green.offset = info->var.green.offset;
  418. var->blue.offset = info->var.blue.offset;
  419. var->red.length = info->var.red.length;
  420. var->green.length = info->var.green.length;
  421. var->blue.length = info->var.blue.length;
  422. var->transp.offset = 0;
  423. var->transp.length = 0;
  424. var->transp.msb_right = 0;
  425. var->red.msb_right = 0;
  426. var->green.msb_right = 0;
  427. var->blue.msb_right = 0;
  428. break;
  429. default:
  430. pr_debug("%s: depth not supported: %u BPP\n", __func__,
  431. var->bits_per_pixel);
  432. return -EINVAL;
  433. }
  434. if (info->var.xres != var->xres ||
  435. info->var.yres != var->yres ||
  436. info->var.xres_virtual != var->xres_virtual ||
  437. info->var.yres_virtual != var->yres_virtual) {
  438. pr_debug("%s: Resolution not supported: X%u x Y%u\n",
  439. __func__, var->xres, var->yres);
  440. return -EINVAL;
  441. }
  442. /*
  443. * Memory limit
  444. */
  445. if ((info->fix.line_length * var->yres_virtual) > info->fix.smem_len) {
  446. pr_debug("%s: Memory Limit requested yres_virtual = %u\n",
  447. __func__, var->yres_virtual);
  448. return -ENOMEM;
  449. }
  450. return 0;
  451. }
  452. /* fb_rotate
  453. * Rotate the display of this angle. This doesn't seems to be used by the core,
  454. * but as our hardware supports it, so why not implementing it...
  455. */
  456. static void bfin_lq035_fb_rotate(struct fb_info *fbi, int angle)
  457. {
  458. pr_debug("%s: %p %d", __func__, fbi, angle);
  459. #if (defined(UD) && defined(LBR))
  460. switch (angle) {
  461. case 180:
  462. gpio_set_value(LBR, 0);
  463. gpio_set_value(UD, 1);
  464. break;
  465. default:
  466. gpio_set_value(LBR, 1);
  467. gpio_set_value(UD, 0);
  468. break;
  469. }
  470. #endif
  471. }
  472. static int bfin_lq035_fb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  473. {
  474. if (nocursor)
  475. return 0;
  476. else
  477. return -EINVAL; /* just to force soft_cursor() call */
  478. }
  479. static int bfin_lq035_fb_setcolreg(u_int regno, u_int red, u_int green,
  480. u_int blue, u_int transp,
  481. struct fb_info *info)
  482. {
  483. if (regno >= NBR_PALETTE)
  484. return -EINVAL;
  485. if (info->var.grayscale)
  486. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  487. red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
  488. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  489. u32 value;
  490. /* Place color in the pseudopalette */
  491. if (regno > 16)
  492. return -EINVAL;
  493. red >>= (16 - info->var.red.length);
  494. green >>= (16 - info->var.green.length);
  495. blue >>= (16 - info->var.blue.length);
  496. value = (red << info->var.red.offset) |
  497. (green << info->var.green.offset)|
  498. (blue << info->var.blue.offset);
  499. value &= 0xFFFF;
  500. ((u32 *) (info->pseudo_palette))[regno] = value;
  501. }
  502. return 0;
  503. }
  504. static struct fb_ops bfin_lq035_fb_ops = {
  505. .owner = THIS_MODULE,
  506. .fb_open = bfin_lq035_fb_open,
  507. .fb_release = bfin_lq035_fb_release,
  508. .fb_check_var = bfin_lq035_fb_check_var,
  509. .fb_rotate = bfin_lq035_fb_rotate,
  510. .fb_fillrect = cfb_fillrect,
  511. .fb_copyarea = cfb_copyarea,
  512. .fb_imageblit = cfb_imageblit,
  513. .fb_cursor = bfin_lq035_fb_cursor,
  514. .fb_setcolreg = bfin_lq035_fb_setcolreg,
  515. };
  516. static int bl_get_brightness(struct backlight_device *bd)
  517. {
  518. return current_brightness;
  519. }
  520. static const struct backlight_ops bfin_lq035fb_bl_ops = {
  521. .get_brightness = bl_get_brightness,
  522. };
  523. static struct backlight_device *bl_dev;
  524. static int bfin_lcd_get_power(struct lcd_device *dev)
  525. {
  526. return 0;
  527. }
  528. static int bfin_lcd_set_power(struct lcd_device *dev, int power)
  529. {
  530. return 0;
  531. }
  532. static int bfin_lcd_get_contrast(struct lcd_device *dev)
  533. {
  534. return (int)vcomm_value;
  535. }
  536. static int bfin_lcd_set_contrast(struct lcd_device *dev, int contrast)
  537. {
  538. if (contrast > 255)
  539. contrast = 255;
  540. if (contrast < 0)
  541. contrast = 0;
  542. vcomm_value = (unsigned char)contrast;
  543. set_vcomm();
  544. return 0;
  545. }
  546. static int bfin_lcd_check_fb(struct lcd_device *lcd, struct fb_info *fi)
  547. {
  548. if (!fi || (fi == &bfin_lq035_fb))
  549. return 1;
  550. return 0;
  551. }
  552. static struct lcd_ops bfin_lcd_ops = {
  553. .get_power = bfin_lcd_get_power,
  554. .set_power = bfin_lcd_set_power,
  555. .get_contrast = bfin_lcd_get_contrast,
  556. .set_contrast = bfin_lcd_set_contrast,
  557. .check_fb = bfin_lcd_check_fb,
  558. };
  559. static struct lcd_device *lcd_dev;
  560. static int __devinit bfin_lq035_probe(struct platform_device *pdev)
  561. {
  562. struct backlight_properties props;
  563. dma_addr_t dma_handle;
  564. int ret;
  565. if (request_dma(CH_PPI, KBUILD_MODNAME)) {
  566. pr_err("couldn't request PPI DMA\n");
  567. return -EFAULT;
  568. }
  569. if (request_ports()) {
  570. pr_err("couldn't request gpio port\n");
  571. ret = -EFAULT;
  572. goto out_ports;
  573. }
  574. fb_buffer = dma_alloc_coherent(NULL, TOTAL_VIDEO_MEM_SIZE,
  575. &dma_handle, GFP_KERNEL);
  576. if (fb_buffer == NULL) {
  577. pr_err("couldn't allocate dma buffer\n");
  578. ret = -ENOMEM;
  579. goto out_dma_coherent;
  580. }
  581. if (L1_DATA_A_LENGTH)
  582. dma_desc_table = l1_data_sram_zalloc(TOTAL_DMA_DESC_SIZE);
  583. else
  584. dma_desc_table = dma_alloc_coherent(NULL, TOTAL_DMA_DESC_SIZE,
  585. &dma_handle, 0);
  586. if (dma_desc_table == NULL) {
  587. pr_err("couldn't allocate dma descriptor\n");
  588. ret = -ENOMEM;
  589. goto out_table;
  590. }
  591. bfin_lq035_fb.screen_base = (void *)fb_buffer;
  592. bfin_lq035_fb_fix.smem_start = (int)fb_buffer;
  593. if (landscape) {
  594. bfin_lq035_fb_defined.xres = LCD_Y_RES;
  595. bfin_lq035_fb_defined.yres = LCD_X_RES;
  596. bfin_lq035_fb_defined.xres_virtual = LCD_Y_RES;
  597. bfin_lq035_fb_defined.yres_virtual = LCD_X_RES;
  598. bfin_lq035_fb_fix.line_length = LCD_Y_RES*(LCD_BBP/8);
  599. } else {
  600. bfin_lq035_fb.screen_base += ACTIVE_VIDEO_MEM_OFFSET;
  601. bfin_lq035_fb_fix.smem_start += ACTIVE_VIDEO_MEM_OFFSET;
  602. }
  603. bfin_lq035_fb_defined.green.msb_right = 0;
  604. bfin_lq035_fb_defined.red.msb_right = 0;
  605. bfin_lq035_fb_defined.blue.msb_right = 0;
  606. bfin_lq035_fb_defined.green.offset = 5;
  607. bfin_lq035_fb_defined.green.length = 6;
  608. bfin_lq035_fb_defined.red.length = 5;
  609. bfin_lq035_fb_defined.blue.length = 5;
  610. if (bgr) {
  611. bfin_lq035_fb_defined.red.offset = 0;
  612. bfin_lq035_fb_defined.blue.offset = 11;
  613. } else {
  614. bfin_lq035_fb_defined.red.offset = 11;
  615. bfin_lq035_fb_defined.blue.offset = 0;
  616. }
  617. bfin_lq035_fb.fbops = &bfin_lq035_fb_ops;
  618. bfin_lq035_fb.var = bfin_lq035_fb_defined;
  619. bfin_lq035_fb.fix = bfin_lq035_fb_fix;
  620. bfin_lq035_fb.flags = FBINFO_DEFAULT;
  621. bfin_lq035_fb.pseudo_palette = kzalloc(sizeof(u32) * 16, GFP_KERNEL);
  622. if (bfin_lq035_fb.pseudo_palette == NULL) {
  623. pr_err("failed to allocate pseudo_palette\n");
  624. ret = -ENOMEM;
  625. goto out_palette;
  626. }
  627. if (fb_alloc_cmap(&bfin_lq035_fb.cmap, NBR_PALETTE, 0) < 0) {
  628. pr_err("failed to allocate colormap (%d entries)\n",
  629. NBR_PALETTE);
  630. ret = -EFAULT;
  631. goto out_cmap;
  632. }
  633. if (register_framebuffer(&bfin_lq035_fb) < 0) {
  634. pr_err("unable to register framebuffer\n");
  635. ret = -EINVAL;
  636. goto out_reg;
  637. }
  638. i2c_add_driver(&ad5280_driver);
  639. memset(&props, 0, sizeof(props));
  640. props.type = BACKLIGHT_RAW;
  641. props.max_brightness = MAX_BRIGHENESS;
  642. bl_dev = backlight_device_register("bf537-bl", NULL, NULL,
  643. &bfin_lq035fb_bl_ops, &props);
  644. lcd_dev = lcd_device_register(KBUILD_MODNAME, &pdev->dev, NULL,
  645. &bfin_lcd_ops);
  646. if (IS_ERR(lcd_dev)) {
  647. pr_err("unable to register lcd\n");
  648. ret = PTR_ERR(lcd_dev);
  649. goto out_lcd;
  650. }
  651. lcd_dev->props.max_contrast = 255,
  652. pr_info("initialized");
  653. return 0;
  654. out_lcd:
  655. unregister_framebuffer(&bfin_lq035_fb);
  656. out_reg:
  657. fb_dealloc_cmap(&bfin_lq035_fb.cmap);
  658. out_cmap:
  659. kfree(bfin_lq035_fb.pseudo_palette);
  660. out_palette:
  661. out_table:
  662. dma_free_coherent(NULL, TOTAL_VIDEO_MEM_SIZE, fb_buffer, 0);
  663. fb_buffer = NULL;
  664. out_dma_coherent:
  665. free_ports();
  666. out_ports:
  667. free_dma(CH_PPI);
  668. return ret;
  669. }
  670. static int __devexit bfin_lq035_remove(struct platform_device *pdev)
  671. {
  672. if (fb_buffer != NULL)
  673. dma_free_coherent(NULL, TOTAL_VIDEO_MEM_SIZE, fb_buffer, 0);
  674. if (L1_DATA_A_LENGTH)
  675. l1_data_sram_free(dma_desc_table);
  676. else
  677. dma_free_coherent(NULL, TOTAL_DMA_DESC_SIZE, NULL, 0);
  678. bfin_write_TIMER_DISABLE(TIMEN_SP|TIMEN_SPS|TIMEN_PS_CLS|
  679. TIMEN_LP|TIMEN_REV);
  680. t_conf_done = 0;
  681. free_dma(CH_PPI);
  682. kfree(bfin_lq035_fb.pseudo_palette);
  683. fb_dealloc_cmap(&bfin_lq035_fb.cmap);
  684. lcd_device_unregister(lcd_dev);
  685. backlight_device_unregister(bl_dev);
  686. unregister_framebuffer(&bfin_lq035_fb);
  687. i2c_del_driver(&ad5280_driver);
  688. free_ports();
  689. pr_info("unregistered LCD driver\n");
  690. return 0;
  691. }
  692. #ifdef CONFIG_PM
  693. static int bfin_lq035_suspend(struct platform_device *pdev, pm_message_t state)
  694. {
  695. if (lq035_open_cnt > 0) {
  696. bfin_write_PPI_CONTROL(0);
  697. SSYNC();
  698. disable_dma(CH_PPI);
  699. }
  700. return 0;
  701. }
  702. static int bfin_lq035_resume(struct platform_device *pdev)
  703. {
  704. if (lq035_open_cnt > 0) {
  705. bfin_write_PPI_CONTROL(0);
  706. SSYNC();
  707. config_dma();
  708. config_ppi();
  709. enable_dma(CH_PPI);
  710. bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() | PORT_EN);
  711. SSYNC();
  712. config_timers();
  713. start_timers();
  714. } else {
  715. t_conf_done = 0;
  716. }
  717. return 0;
  718. }
  719. #else
  720. # define bfin_lq035_suspend NULL
  721. # define bfin_lq035_resume NULL
  722. #endif
  723. static struct platform_driver bfin_lq035_driver = {
  724. .probe = bfin_lq035_probe,
  725. .remove = __devexit_p(bfin_lq035_remove),
  726. .suspend = bfin_lq035_suspend,
  727. .resume = bfin_lq035_resume,
  728. .driver = {
  729. .name = KBUILD_MODNAME,
  730. .owner = THIS_MODULE,
  731. },
  732. };
  733. static int __init bfin_lq035_driver_init(void)
  734. {
  735. request_module("i2c-bfin-twi");
  736. return platform_driver_register(&bfin_lq035_driver);
  737. }
  738. module_init(bfin_lq035_driver_init);
  739. static void __exit bfin_lq035_driver_cleanup(void)
  740. {
  741. platform_driver_unregister(&bfin_lq035_driver);
  742. }
  743. module_exit(bfin_lq035_driver_cleanup);
  744. MODULE_DESCRIPTION("SHARP LQ035Q7DB03 TFT LCD Driver");
  745. MODULE_LICENSE("GPL");