bf537-lq035.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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 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 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 = 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 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_one(UD, GPIOF_OUT_INIT_LOW, KBUILD_MODNAME)) {
  310. pr_err("requesting GPIO %d failed\n", UD);
  311. return -EBUSY;
  312. }
  313. if (gpio_request_one(LBR, GPIOF_OUT_INIT_HIGH, KBUILD_MODNAME)) {
  314. pr_err("requesting GPIO %d failed\n", LBR);
  315. gpio_free(UD);
  316. return -EBUSY;
  317. }
  318. #endif
  319. if (gpio_request_one(MOD, GPIOF_OUT_INIT_HIGH, KBUILD_MODNAME)) {
  320. pr_err("requesting GPIO %d failed\n", MOD);
  321. #if (defined(UD) && defined(LBR))
  322. gpio_free(LBR);
  323. gpio_free(UD);
  324. #endif
  325. return -EBUSY;
  326. }
  327. SSYNC();
  328. return 0;
  329. }
  330. static void free_ports(void)
  331. {
  332. u16 tmr_req[] = TIMERS;
  333. peripheral_free_list(ppi_pins);
  334. peripheral_free_list(tmr_req);
  335. #if defined(UD) && defined(LBR)
  336. gpio_free(LBR);
  337. gpio_free(UD);
  338. #endif
  339. gpio_free(MOD);
  340. }
  341. static struct fb_info bfin_lq035_fb;
  342. static struct fb_var_screeninfo bfin_lq035_fb_defined = {
  343. .bits_per_pixel = LCD_BBP,
  344. .activate = FB_ACTIVATE_TEST,
  345. .xres = LCD_X_RES, /*default portrait mode RGB*/
  346. .yres = LCD_Y_RES,
  347. .xres_virtual = LCD_X_RES,
  348. .yres_virtual = LCD_Y_RES,
  349. .height = -1,
  350. .width = -1,
  351. .left_margin = 0,
  352. .right_margin = 0,
  353. .upper_margin = 0,
  354. .lower_margin = 0,
  355. .red = {11, 5, 0},
  356. .green = {5, 6, 0},
  357. .blue = {0, 5, 0},
  358. .transp = {0, 0, 0},
  359. };
  360. static struct fb_fix_screeninfo bfin_lq035_fb_fix = {
  361. .id = KBUILD_MODNAME,
  362. .smem_len = ACTIVE_VIDEO_MEM_SIZE,
  363. .type = FB_TYPE_PACKED_PIXELS,
  364. .visual = FB_VISUAL_TRUECOLOR,
  365. .xpanstep = 0,
  366. .ypanstep = 0,
  367. .line_length = LCD_X_RES*(LCD_BBP/8),
  368. .accel = FB_ACCEL_NONE,
  369. };
  370. static int bfin_lq035_fb_open(struct fb_info *info, int user)
  371. {
  372. unsigned long flags;
  373. spin_lock_irqsave(&bfin_lq035_lock, flags);
  374. lq035_open_cnt++;
  375. spin_unlock_irqrestore(&bfin_lq035_lock, flags);
  376. if (lq035_open_cnt <= 1) {
  377. bfin_write_PPI_CONTROL(0);
  378. SSYNC();
  379. set_vcomm();
  380. config_dma();
  381. config_ppi();
  382. /* start dma */
  383. enable_dma(CH_PPI);
  384. SSYNC();
  385. bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() | PORT_EN);
  386. SSYNC();
  387. if (!t_conf_done) {
  388. config_timers();
  389. start_timers();
  390. }
  391. /* gpio_set_value(MOD,1); */
  392. }
  393. return 0;
  394. }
  395. static int bfin_lq035_fb_release(struct fb_info *info, int user)
  396. {
  397. unsigned long flags;
  398. spin_lock_irqsave(&bfin_lq035_lock, flags);
  399. lq035_open_cnt--;
  400. spin_unlock_irqrestore(&bfin_lq035_lock, flags);
  401. if (lq035_open_cnt <= 0) {
  402. bfin_write_PPI_CONTROL(0);
  403. SSYNC();
  404. disable_dma(CH_PPI);
  405. }
  406. return 0;
  407. }
  408. static int bfin_lq035_fb_check_var(struct fb_var_screeninfo *var,
  409. struct fb_info *info)
  410. {
  411. switch (var->bits_per_pixel) {
  412. case 16:/* DIRECTCOLOUR, 64k */
  413. var->red.offset = info->var.red.offset;
  414. var->green.offset = info->var.green.offset;
  415. var->blue.offset = info->var.blue.offset;
  416. var->red.length = info->var.red.length;
  417. var->green.length = info->var.green.length;
  418. var->blue.length = info->var.blue.length;
  419. var->transp.offset = 0;
  420. var->transp.length = 0;
  421. var->transp.msb_right = 0;
  422. var->red.msb_right = 0;
  423. var->green.msb_right = 0;
  424. var->blue.msb_right = 0;
  425. break;
  426. default:
  427. pr_debug("%s: depth not supported: %u BPP\n", __func__,
  428. var->bits_per_pixel);
  429. return -EINVAL;
  430. }
  431. if (info->var.xres != var->xres ||
  432. info->var.yres != var->yres ||
  433. info->var.xres_virtual != var->xres_virtual ||
  434. info->var.yres_virtual != var->yres_virtual) {
  435. pr_debug("%s: Resolution not supported: X%u x Y%u\n",
  436. __func__, var->xres, var->yres);
  437. return -EINVAL;
  438. }
  439. /*
  440. * Memory limit
  441. */
  442. if ((info->fix.line_length * var->yres_virtual) > info->fix.smem_len) {
  443. pr_debug("%s: Memory Limit requested yres_virtual = %u\n",
  444. __func__, var->yres_virtual);
  445. return -ENOMEM;
  446. }
  447. return 0;
  448. }
  449. /* fb_rotate
  450. * Rotate the display of this angle. This doesn't seems to be used by the core,
  451. * but as our hardware supports it, so why not implementing it...
  452. */
  453. static void bfin_lq035_fb_rotate(struct fb_info *fbi, int angle)
  454. {
  455. pr_debug("%s: %p %d", __func__, fbi, angle);
  456. #if (defined(UD) && defined(LBR))
  457. switch (angle) {
  458. case 180:
  459. gpio_set_value(LBR, 0);
  460. gpio_set_value(UD, 1);
  461. break;
  462. default:
  463. gpio_set_value(LBR, 1);
  464. gpio_set_value(UD, 0);
  465. break;
  466. }
  467. #endif
  468. }
  469. static int bfin_lq035_fb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  470. {
  471. if (nocursor)
  472. return 0;
  473. else
  474. return -EINVAL; /* just to force soft_cursor() call */
  475. }
  476. static int bfin_lq035_fb_setcolreg(u_int regno, u_int red, u_int green,
  477. u_int blue, u_int transp,
  478. struct fb_info *info)
  479. {
  480. if (regno >= NBR_PALETTE)
  481. return -EINVAL;
  482. if (info->var.grayscale)
  483. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  484. red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
  485. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  486. u32 value;
  487. /* Place color in the pseudopalette */
  488. if (regno > 16)
  489. return -EINVAL;
  490. red >>= (16 - info->var.red.length);
  491. green >>= (16 - info->var.green.length);
  492. blue >>= (16 - info->var.blue.length);
  493. value = (red << info->var.red.offset) |
  494. (green << info->var.green.offset)|
  495. (blue << info->var.blue.offset);
  496. value &= 0xFFFF;
  497. ((u32 *) (info->pseudo_palette))[regno] = value;
  498. }
  499. return 0;
  500. }
  501. static struct fb_ops bfin_lq035_fb_ops = {
  502. .owner = THIS_MODULE,
  503. .fb_open = bfin_lq035_fb_open,
  504. .fb_release = bfin_lq035_fb_release,
  505. .fb_check_var = bfin_lq035_fb_check_var,
  506. .fb_rotate = bfin_lq035_fb_rotate,
  507. .fb_fillrect = cfb_fillrect,
  508. .fb_copyarea = cfb_copyarea,
  509. .fb_imageblit = cfb_imageblit,
  510. .fb_cursor = bfin_lq035_fb_cursor,
  511. .fb_setcolreg = bfin_lq035_fb_setcolreg,
  512. };
  513. static int bl_get_brightness(struct backlight_device *bd)
  514. {
  515. return current_brightness;
  516. }
  517. static const struct backlight_ops bfin_lq035fb_bl_ops = {
  518. .get_brightness = bl_get_brightness,
  519. };
  520. static struct backlight_device *bl_dev;
  521. static int bfin_lcd_get_power(struct lcd_device *dev)
  522. {
  523. return 0;
  524. }
  525. static int bfin_lcd_set_power(struct lcd_device *dev, int power)
  526. {
  527. return 0;
  528. }
  529. static int bfin_lcd_get_contrast(struct lcd_device *dev)
  530. {
  531. return (int)vcomm_value;
  532. }
  533. static int bfin_lcd_set_contrast(struct lcd_device *dev, int contrast)
  534. {
  535. if (contrast > 255)
  536. contrast = 255;
  537. if (contrast < 0)
  538. contrast = 0;
  539. vcomm_value = (unsigned char)contrast;
  540. set_vcomm();
  541. return 0;
  542. }
  543. static int bfin_lcd_check_fb(struct lcd_device *lcd, struct fb_info *fi)
  544. {
  545. if (!fi || (fi == &bfin_lq035_fb))
  546. return 1;
  547. return 0;
  548. }
  549. static struct lcd_ops bfin_lcd_ops = {
  550. .get_power = bfin_lcd_get_power,
  551. .set_power = bfin_lcd_set_power,
  552. .get_contrast = bfin_lcd_get_contrast,
  553. .set_contrast = bfin_lcd_set_contrast,
  554. .check_fb = bfin_lcd_check_fb,
  555. };
  556. static struct lcd_device *lcd_dev;
  557. static int bfin_lq035_probe(struct platform_device *pdev)
  558. {
  559. struct backlight_properties props;
  560. dma_addr_t dma_handle;
  561. int ret;
  562. if (request_dma(CH_PPI, KBUILD_MODNAME)) {
  563. pr_err("couldn't request PPI DMA\n");
  564. return -EFAULT;
  565. }
  566. if (request_ports()) {
  567. pr_err("couldn't request gpio port\n");
  568. ret = -EFAULT;
  569. goto out_ports;
  570. }
  571. fb_buffer = dma_alloc_coherent(NULL, TOTAL_VIDEO_MEM_SIZE,
  572. &dma_handle, GFP_KERNEL);
  573. if (fb_buffer == NULL) {
  574. pr_err("couldn't allocate dma buffer\n");
  575. ret = -ENOMEM;
  576. goto out_dma_coherent;
  577. }
  578. if (L1_DATA_A_LENGTH)
  579. dma_desc_table = l1_data_sram_zalloc(TOTAL_DMA_DESC_SIZE);
  580. else
  581. dma_desc_table = dma_alloc_coherent(NULL, TOTAL_DMA_DESC_SIZE,
  582. &dma_handle, 0);
  583. if (dma_desc_table == NULL) {
  584. pr_err("couldn't allocate dma descriptor\n");
  585. ret = -ENOMEM;
  586. goto out_table;
  587. }
  588. bfin_lq035_fb.screen_base = (void *)fb_buffer;
  589. bfin_lq035_fb_fix.smem_start = (int)fb_buffer;
  590. if (landscape) {
  591. bfin_lq035_fb_defined.xres = LCD_Y_RES;
  592. bfin_lq035_fb_defined.yres = LCD_X_RES;
  593. bfin_lq035_fb_defined.xres_virtual = LCD_Y_RES;
  594. bfin_lq035_fb_defined.yres_virtual = LCD_X_RES;
  595. bfin_lq035_fb_fix.line_length = LCD_Y_RES*(LCD_BBP/8);
  596. } else {
  597. bfin_lq035_fb.screen_base += ACTIVE_VIDEO_MEM_OFFSET;
  598. bfin_lq035_fb_fix.smem_start += ACTIVE_VIDEO_MEM_OFFSET;
  599. }
  600. bfin_lq035_fb_defined.green.msb_right = 0;
  601. bfin_lq035_fb_defined.red.msb_right = 0;
  602. bfin_lq035_fb_defined.blue.msb_right = 0;
  603. bfin_lq035_fb_defined.green.offset = 5;
  604. bfin_lq035_fb_defined.green.length = 6;
  605. bfin_lq035_fb_defined.red.length = 5;
  606. bfin_lq035_fb_defined.blue.length = 5;
  607. if (bgr) {
  608. bfin_lq035_fb_defined.red.offset = 0;
  609. bfin_lq035_fb_defined.blue.offset = 11;
  610. } else {
  611. bfin_lq035_fb_defined.red.offset = 11;
  612. bfin_lq035_fb_defined.blue.offset = 0;
  613. }
  614. bfin_lq035_fb.fbops = &bfin_lq035_fb_ops;
  615. bfin_lq035_fb.var = bfin_lq035_fb_defined;
  616. bfin_lq035_fb.fix = bfin_lq035_fb_fix;
  617. bfin_lq035_fb.flags = FBINFO_DEFAULT;
  618. bfin_lq035_fb.pseudo_palette = devm_kzalloc(&pdev->dev,
  619. sizeof(u32) * 16,
  620. GFP_KERNEL);
  621. if (bfin_lq035_fb.pseudo_palette == NULL) {
  622. pr_err("failed to allocate pseudo_palette\n");
  623. ret = -ENOMEM;
  624. goto out_table;
  625. }
  626. if (fb_alloc_cmap(&bfin_lq035_fb.cmap, NBR_PALETTE, 0) < 0) {
  627. pr_err("failed to allocate colormap (%d entries)\n",
  628. NBR_PALETTE);
  629. ret = -EFAULT;
  630. goto out_table;
  631. }
  632. if (register_framebuffer(&bfin_lq035_fb) < 0) {
  633. pr_err("unable to register framebuffer\n");
  634. ret = -EINVAL;
  635. goto out_reg;
  636. }
  637. i2c_add_driver(&ad5280_driver);
  638. memset(&props, 0, sizeof(props));
  639. props.type = BACKLIGHT_RAW;
  640. props.max_brightness = MAX_BRIGHENESS;
  641. bl_dev = backlight_device_register("bf537-bl", NULL, NULL,
  642. &bfin_lq035fb_bl_ops, &props);
  643. lcd_dev = lcd_device_register(KBUILD_MODNAME, &pdev->dev, NULL,
  644. &bfin_lcd_ops);
  645. if (IS_ERR(lcd_dev)) {
  646. pr_err("unable to register lcd\n");
  647. ret = PTR_ERR(lcd_dev);
  648. goto out_lcd;
  649. }
  650. lcd_dev->props.max_contrast = 255,
  651. pr_info("initialized");
  652. return 0;
  653. out_lcd:
  654. unregister_framebuffer(&bfin_lq035_fb);
  655. out_reg:
  656. fb_dealloc_cmap(&bfin_lq035_fb.cmap);
  657. out_table:
  658. dma_free_coherent(NULL, TOTAL_VIDEO_MEM_SIZE, fb_buffer, 0);
  659. fb_buffer = NULL;
  660. out_dma_coherent:
  661. free_ports();
  662. out_ports:
  663. free_dma(CH_PPI);
  664. return ret;
  665. }
  666. static int bfin_lq035_remove(struct platform_device *pdev)
  667. {
  668. if (fb_buffer != NULL)
  669. dma_free_coherent(NULL, TOTAL_VIDEO_MEM_SIZE, fb_buffer, 0);
  670. if (L1_DATA_A_LENGTH)
  671. l1_data_sram_free(dma_desc_table);
  672. else
  673. dma_free_coherent(NULL, TOTAL_DMA_DESC_SIZE, NULL, 0);
  674. bfin_write_TIMER_DISABLE(TIMEN_SP|TIMEN_SPS|TIMEN_PS_CLS|
  675. TIMEN_LP|TIMEN_REV);
  676. t_conf_done = 0;
  677. free_dma(CH_PPI);
  678. fb_dealloc_cmap(&bfin_lq035_fb.cmap);
  679. lcd_device_unregister(lcd_dev);
  680. backlight_device_unregister(bl_dev);
  681. unregister_framebuffer(&bfin_lq035_fb);
  682. i2c_del_driver(&ad5280_driver);
  683. free_ports();
  684. pr_info("unregistered LCD driver\n");
  685. return 0;
  686. }
  687. #ifdef CONFIG_PM
  688. static int bfin_lq035_suspend(struct platform_device *pdev, pm_message_t state)
  689. {
  690. if (lq035_open_cnt > 0) {
  691. bfin_write_PPI_CONTROL(0);
  692. SSYNC();
  693. disable_dma(CH_PPI);
  694. }
  695. return 0;
  696. }
  697. static int bfin_lq035_resume(struct platform_device *pdev)
  698. {
  699. if (lq035_open_cnt > 0) {
  700. bfin_write_PPI_CONTROL(0);
  701. SSYNC();
  702. config_dma();
  703. config_ppi();
  704. enable_dma(CH_PPI);
  705. bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() | PORT_EN);
  706. SSYNC();
  707. config_timers();
  708. start_timers();
  709. } else {
  710. t_conf_done = 0;
  711. }
  712. return 0;
  713. }
  714. #else
  715. # define bfin_lq035_suspend NULL
  716. # define bfin_lq035_resume NULL
  717. #endif
  718. static struct platform_driver bfin_lq035_driver = {
  719. .probe = bfin_lq035_probe,
  720. .remove = bfin_lq035_remove,
  721. .suspend = bfin_lq035_suspend,
  722. .resume = bfin_lq035_resume,
  723. .driver = {
  724. .name = KBUILD_MODNAME,
  725. },
  726. };
  727. static int __init bfin_lq035_driver_init(void)
  728. {
  729. request_module("i2c-bfin-twi");
  730. return platform_driver_register(&bfin_lq035_driver);
  731. }
  732. module_init(bfin_lq035_driver_init);
  733. static void __exit bfin_lq035_driver_cleanup(void)
  734. {
  735. platform_driver_unregister(&bfin_lq035_driver);
  736. }
  737. module_exit(bfin_lq035_driver_cleanup);
  738. MODULE_DESCRIPTION("SHARP LQ035Q7DB03 TFT LCD Driver");
  739. MODULE_LICENSE("GPL");