au1100fb.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * BRIEF MODULE DESCRIPTION
  4. * Au1100 LCD Driver.
  5. *
  6. * Rewritten for 2.6 by Embedded Alley Solutions
  7. * <source@embeddedalley.com>, based on submissions by
  8. * Karl Lessard <klessard@sunrisetelecom.com>
  9. * <c.pellegrin@exadron.com>
  10. *
  11. * PM support added by Rodolfo Giometti <giometti@linux.it>
  12. * Cursor enable/disable by Rodolfo Giometti <giometti@linux.it>
  13. *
  14. * Copyright 2002 MontaVista Software
  15. * Author: MontaVista Software, Inc.
  16. * ppopov@mvista.com or source@mvista.com
  17. *
  18. * Copyright 2002 Alchemy Semiconductor
  19. * Author: Alchemy Semiconductor
  20. *
  21. * Based on:
  22. * linux/drivers/video/skeletonfb.c -- Skeleton for a frame buffer device
  23. * Created 28 Dec 1997 by Geert Uytterhoeven
  24. */
  25. #define pr_fmt(fmt) "au1100fb:" fmt "\n"
  26. #include <linux/clk.h>
  27. #include <linux/delay.h>
  28. #include <linux/io.h>
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/errno.h>
  32. #include <linux/string.h>
  33. #include <linux/mm.h>
  34. #include <linux/fb.h>
  35. #include <linux/init.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/ctype.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/slab.h>
  41. #if defined(__BIG_ENDIAN)
  42. #define LCD_CONTROL_DEFAULT_PO LCD_CONTROL_PO_11
  43. #else
  44. #define LCD_CONTROL_DEFAULT_PO LCD_CONTROL_PO_00
  45. #endif
  46. #define LCD_CONTROL_DEFAULT_SBPPF LCD_CONTROL_SBPPF_565
  47. /********************************************************************/
  48. /* LCD controller restrictions */
  49. #define AU1100_LCD_MAX_XRES 800
  50. #define AU1100_LCD_MAX_YRES 600
  51. #define AU1100_LCD_MAX_BPP 16
  52. #define AU1100_LCD_MAX_CLK 48000000
  53. #define AU1100_LCD_NBR_PALETTE_ENTRIES 256
  54. /* Default number of visible screen buffer to allocate */
  55. #define AU1100FB_NBR_VIDEO_BUFFERS 4
  56. /********************************************************************/
  57. struct au1100fb_panel
  58. {
  59. const char name[25]; /* Full name <vendor>_<model> */
  60. u32 control_base; /* Mode-independent control values */
  61. u32 clkcontrol_base; /* Panel pixclock preferences */
  62. u32 horztiming;
  63. u32 verttiming;
  64. u32 xres; /* Maximum horizontal resolution */
  65. u32 yres; /* Maximum vertical resolution */
  66. u32 bpp; /* Maximum depth supported */
  67. };
  68. struct au1100fb_regs
  69. {
  70. u32 lcd_control;
  71. u32 lcd_intstatus;
  72. u32 lcd_intenable;
  73. u32 lcd_horztiming;
  74. u32 lcd_verttiming;
  75. u32 lcd_clkcontrol;
  76. u32 lcd_dmaaddr0;
  77. u32 lcd_dmaaddr1;
  78. u32 lcd_words;
  79. u32 lcd_pwmdiv;
  80. u32 lcd_pwmhi;
  81. u32 reserved[(0x0400-0x002C)/4];
  82. u32 lcd_palettebase[256];
  83. };
  84. struct au1100fb_device {
  85. struct fb_info info; /* FB driver info record */
  86. struct au1100fb_panel *panel; /* Panel connected to this device */
  87. struct au1100fb_regs* regs; /* Registers memory map */
  88. size_t regs_len;
  89. unsigned int regs_phys;
  90. #ifdef CONFIG_PM
  91. /* stores the register values during suspend */
  92. struct au1100fb_regs pm_regs;
  93. #endif
  94. unsigned char* fb_mem; /* FrameBuffer memory map */
  95. size_t fb_len;
  96. dma_addr_t fb_phys;
  97. int panel_idx;
  98. struct clk *lcdclk;
  99. struct device *dev;
  100. };
  101. /********************************************************************/
  102. #define LCD_CONTROL (AU1100_LCD_BASE + 0x0)
  103. #define LCD_CONTROL_SBB_BIT 21
  104. #define LCD_CONTROL_SBB_MASK (0x3 << LCD_CONTROL_SBB_BIT)
  105. #define LCD_CONTROL_SBB_1 (0 << LCD_CONTROL_SBB_BIT)
  106. #define LCD_CONTROL_SBB_2 (1 << LCD_CONTROL_SBB_BIT)
  107. #define LCD_CONTROL_SBB_3 (2 << LCD_CONTROL_SBB_BIT)
  108. #define LCD_CONTROL_SBB_4 (3 << LCD_CONTROL_SBB_BIT)
  109. #define LCD_CONTROL_SBPPF_BIT 18
  110. #define LCD_CONTROL_SBPPF_MASK (0x7 << LCD_CONTROL_SBPPF_BIT)
  111. #define LCD_CONTROL_SBPPF_655 (0 << LCD_CONTROL_SBPPF_BIT)
  112. #define LCD_CONTROL_SBPPF_565 (1 << LCD_CONTROL_SBPPF_BIT)
  113. #define LCD_CONTROL_SBPPF_556 (2 << LCD_CONTROL_SBPPF_BIT)
  114. #define LCD_CONTROL_SBPPF_1555 (3 << LCD_CONTROL_SBPPF_BIT)
  115. #define LCD_CONTROL_SBPPF_5551 (4 << LCD_CONTROL_SBPPF_BIT)
  116. #define LCD_CONTROL_WP (1<<17)
  117. #define LCD_CONTROL_WD (1<<16)
  118. #define LCD_CONTROL_C (1<<15)
  119. #define LCD_CONTROL_SM_BIT 13
  120. #define LCD_CONTROL_SM_MASK (0x3 << LCD_CONTROL_SM_BIT)
  121. #define LCD_CONTROL_SM_0 (0 << LCD_CONTROL_SM_BIT)
  122. #define LCD_CONTROL_SM_90 (1 << LCD_CONTROL_SM_BIT)
  123. #define LCD_CONTROL_SM_180 (2 << LCD_CONTROL_SM_BIT)
  124. #define LCD_CONTROL_SM_270 (3 << LCD_CONTROL_SM_BIT)
  125. #define LCD_CONTROL_DB (1<<12)
  126. #define LCD_CONTROL_CCO (1<<11)
  127. #define LCD_CONTROL_DP (1<<10)
  128. #define LCD_CONTROL_PO_BIT 8
  129. #define LCD_CONTROL_PO_MASK (0x3 << LCD_CONTROL_PO_BIT)
  130. #define LCD_CONTROL_PO_00 (0 << LCD_CONTROL_PO_BIT)
  131. #define LCD_CONTROL_PO_01 (1 << LCD_CONTROL_PO_BIT)
  132. #define LCD_CONTROL_PO_10 (2 << LCD_CONTROL_PO_BIT)
  133. #define LCD_CONTROL_PO_11 (3 << LCD_CONTROL_PO_BIT)
  134. #define LCD_CONTROL_MPI (1<<7)
  135. #define LCD_CONTROL_PT (1<<6)
  136. #define LCD_CONTROL_PC (1<<5)
  137. #define LCD_CONTROL_BPP_BIT 1
  138. #define LCD_CONTROL_BPP_MASK (0x7 << LCD_CONTROL_BPP_BIT)
  139. #define LCD_CONTROL_BPP_1 (0 << LCD_CONTROL_BPP_BIT)
  140. #define LCD_CONTROL_BPP_2 (1 << LCD_CONTROL_BPP_BIT)
  141. #define LCD_CONTROL_BPP_4 (2 << LCD_CONTROL_BPP_BIT)
  142. #define LCD_CONTROL_BPP_8 (3 << LCD_CONTROL_BPP_BIT)
  143. #define LCD_CONTROL_BPP_12 (4 << LCD_CONTROL_BPP_BIT)
  144. #define LCD_CONTROL_BPP_16 (5 << LCD_CONTROL_BPP_BIT)
  145. #define LCD_CONTROL_GO (1<<0)
  146. #define LCD_INTSTATUS (AU1100_LCD_BASE + 0x4)
  147. #define LCD_INTENABLE (AU1100_LCD_BASE + 0x8)
  148. #define LCD_INT_SD (1<<7)
  149. #define LCD_INT_OF (1<<6)
  150. #define LCD_INT_UF (1<<5)
  151. #define LCD_INT_SA (1<<3)
  152. #define LCD_INT_SS (1<<2)
  153. #define LCD_INT_S1 (1<<1)
  154. #define LCD_INT_S0 (1<<0)
  155. #define LCD_HORZTIMING (AU1100_LCD_BASE + 0xC)
  156. #define LCD_HORZTIMING_HN2_BIT 24
  157. #define LCD_HORZTIMING_HN2_MASK (0xFF << LCD_HORZTIMING_HN2_BIT)
  158. #define LCD_HORZTIMING_HN2_N(N) ((((N)-1) << LCD_HORZTIMING_HN2_BIT) & LCD_HORZTIMING_HN2_MASK)
  159. #define LCD_HORZTIMING_HN1_BIT 16
  160. #define LCD_HORZTIMING_HN1_MASK (0xFF << LCD_HORZTIMING_HN1_BIT)
  161. #define LCD_HORZTIMING_HN1_N(N) ((((N)-1) << LCD_HORZTIMING_HN1_BIT) & LCD_HORZTIMING_HN1_MASK)
  162. #define LCD_HORZTIMING_HPW_BIT 10
  163. #define LCD_HORZTIMING_HPW_MASK (0x3F << LCD_HORZTIMING_HPW_BIT)
  164. #define LCD_HORZTIMING_HPW_N(N) ((((N)-1) << LCD_HORZTIMING_HPW_BIT) & LCD_HORZTIMING_HPW_MASK)
  165. #define LCD_HORZTIMING_PPL_BIT 0
  166. #define LCD_HORZTIMING_PPL_MASK (0x3FF << LCD_HORZTIMING_PPL_BIT)
  167. #define LCD_HORZTIMING_PPL_N(N) ((((N)-1) << LCD_HORZTIMING_PPL_BIT) & LCD_HORZTIMING_PPL_MASK)
  168. #define LCD_VERTTIMING (AU1100_LCD_BASE + 0x10)
  169. #define LCD_VERTTIMING_VN2_BIT 24
  170. #define LCD_VERTTIMING_VN2_MASK (0xFF << LCD_VERTTIMING_VN2_BIT)
  171. #define LCD_VERTTIMING_VN2_N(N) ((((N)-1) << LCD_VERTTIMING_VN2_BIT) & LCD_VERTTIMING_VN2_MASK)
  172. #define LCD_VERTTIMING_VN1_BIT 16
  173. #define LCD_VERTTIMING_VN1_MASK (0xFF << LCD_VERTTIMING_VN1_BIT)
  174. #define LCD_VERTTIMING_VN1_N(N) ((((N)-1) << LCD_VERTTIMING_VN1_BIT) & LCD_VERTTIMING_VN1_MASK)
  175. #define LCD_VERTTIMING_VPW_BIT 10
  176. #define LCD_VERTTIMING_VPW_MASK (0x3F << LCD_VERTTIMING_VPW_BIT)
  177. #define LCD_VERTTIMING_VPW_N(N) ((((N)-1) << LCD_VERTTIMING_VPW_BIT) & LCD_VERTTIMING_VPW_MASK)
  178. #define LCD_VERTTIMING_LPP_BIT 0
  179. #define LCD_VERTTIMING_LPP_MASK (0x3FF << LCD_VERTTIMING_LPP_BIT)
  180. #define LCD_VERTTIMING_LPP_N(N) ((((N)-1) << LCD_VERTTIMING_LPP_BIT) & LCD_VERTTIMING_LPP_MASK)
  181. #define LCD_CLKCONTROL (AU1100_LCD_BASE + 0x14)
  182. #define LCD_CLKCONTROL_IB (1<<18)
  183. #define LCD_CLKCONTROL_IC (1<<17)
  184. #define LCD_CLKCONTROL_IH (1<<16)
  185. #define LCD_CLKCONTROL_IV (1<<15)
  186. #define LCD_CLKCONTROL_BF_BIT 10
  187. #define LCD_CLKCONTROL_BF_MASK (0x1F << LCD_CLKCONTROL_BF_BIT)
  188. #define LCD_CLKCONTROL_BF_N(N) ((((N)-1) << LCD_CLKCONTROL_BF_BIT) & LCD_CLKCONTROL_BF_MASK)
  189. #define LCD_CLKCONTROL_PCD_BIT 0
  190. #define LCD_CLKCONTROL_PCD_MASK (0x3FF << LCD_CLKCONTROL_PCD_BIT)
  191. #define LCD_CLKCONTROL_PCD_N(N) (((N) << LCD_CLKCONTROL_PCD_BIT) & LCD_CLKCONTROL_PCD_MASK)
  192. #define LCD_DMAADDR0 (AU1100_LCD_BASE + 0x18)
  193. #define LCD_DMAADDR1 (AU1100_LCD_BASE + 0x1C)
  194. #define LCD_DMA_SA_BIT 5
  195. #define LCD_DMA_SA_MASK (0x7FFFFFF << LCD_DMA_SA_BIT)
  196. #define LCD_DMA_SA_N(N) ((N) & LCD_DMA_SA_MASK)
  197. #define LCD_WORDS (AU1100_LCD_BASE + 0x20)
  198. #define LCD_WRD_WRDS_BIT 0
  199. #define LCD_WRD_WRDS_MASK (0xFFFFFFFF << LCD_WRD_WRDS_BIT)
  200. #define LCD_WRD_WRDS_N(N) ((((N)-1) << LCD_WRD_WRDS_BIT) & LCD_WRD_WRDS_MASK)
  201. #define LCD_PWMDIV (AU1100_LCD_BASE + 0x24)
  202. #define LCD_PWMDIV_EN (1<<12)
  203. #define LCD_PWMDIV_PWMDIV_BIT 0
  204. #define LCD_PWMDIV_PWMDIV_MASK (0xFFF << LCD_PWMDIV_PWMDIV_BIT)
  205. #define LCD_PWMDIV_PWMDIV_N(N) ((((N)-1) << LCD_PWMDIV_PWMDIV_BIT) & LCD_PWMDIV_PWMDIV_MASK)
  206. #define LCD_PWMHI (AU1100_LCD_BASE + 0x28)
  207. #define LCD_PWMHI_PWMHI1_BIT 12
  208. #define LCD_PWMHI_PWMHI1_MASK (0xFFF << LCD_PWMHI_PWMHI1_BIT)
  209. #define LCD_PWMHI_PWMHI1_N(N) (((N) << LCD_PWMHI_PWMHI1_BIT) & LCD_PWMHI_PWMHI1_MASK)
  210. #define LCD_PWMHI_PWMHI0_BIT 0
  211. #define LCD_PWMHI_PWMHI0_MASK (0xFFF << LCD_PWMHI_PWMHI0_BIT)
  212. #define LCD_PWMHI_PWMHI0_N(N) (((N) << LCD_PWMHI_PWMHI0_BIT) & LCD_PWMHI_PWMHI0_MASK)
  213. #define LCD_PALLETTEBASE (AU1100_LCD_BASE + 0x400)
  214. #define LCD_PALLETTE_MONO_MI_BIT 0
  215. #define LCD_PALLETTE_MONO_MI_MASK (0xF << LCD_PALLETTE_MONO_MI_BIT)
  216. #define LCD_PALLETTE_MONO_MI_N(N) (((N)<< LCD_PALLETTE_MONO_MI_BIT) & LCD_PALLETTE_MONO_MI_MASK)
  217. #define LCD_PALLETTE_COLOR_RI_BIT 8
  218. #define LCD_PALLETTE_COLOR_RI_MASK (0xF << LCD_PALLETTE_COLOR_RI_BIT)
  219. #define LCD_PALLETTE_COLOR_RI_N(N) (((N)<< LCD_PALLETTE_COLOR_RI_BIT) & LCD_PALLETTE_COLOR_RI_MASK)
  220. #define LCD_PALLETTE_COLOR_GI_BIT 4
  221. #define LCD_PALLETTE_COLOR_GI_MASK (0xF << LCD_PALLETTE_COLOR_GI_BIT)
  222. #define LCD_PALLETTE_COLOR_GI_N(N) (((N)<< LCD_PALLETTE_COLOR_GI_BIT) & LCD_PALLETTE_COLOR_GI_MASK)
  223. #define LCD_PALLETTE_COLOR_BI_BIT 0
  224. #define LCD_PALLETTE_COLOR_BI_MASK (0xF << LCD_PALLETTE_COLOR_BI_BIT)
  225. #define LCD_PALLETTE_COLOR_BI_N(N) (((N)<< LCD_PALLETTE_COLOR_BI_BIT) & LCD_PALLETTE_COLOR_BI_MASK)
  226. #define LCD_PALLETTE_TFT_DC_BIT 0
  227. #define LCD_PALLETTE_TFT_DC_MASK (0xFFFF << LCD_PALLETTE_TFT_DC_BIT)
  228. #define LCD_PALLETTE_TFT_DC_N(N) (((N)<< LCD_PALLETTE_TFT_DC_BIT) & LCD_PALLETTE_TFT_DC_MASK)
  229. /********************************************************************/
  230. /* List of panels known to work with the AU1100 LCD controller.
  231. * To add a new panel, enter the same specifications as the
  232. * Generic_TFT one, and MAKE SURE that it doesn't conflicts
  233. * with the controller restrictions. Restrictions are:
  234. *
  235. * STN color panels: max_bpp <= 12
  236. * STN mono panels: max_bpp <= 4
  237. * TFT panels: max_bpp <= 16
  238. * max_xres <= 800
  239. * max_yres <= 600
  240. */
  241. static struct au1100fb_panel known_lcd_panels[] =
  242. {
  243. /* 800x600x16bpp CRT */
  244. [0] = {
  245. .name = "CRT_800x600_16",
  246. .xres = 800,
  247. .yres = 600,
  248. .bpp = 16,
  249. .control_base = 0x0004886A |
  250. LCD_CONTROL_DEFAULT_PO | LCD_CONTROL_DEFAULT_SBPPF |
  251. LCD_CONTROL_BPP_16 | LCD_CONTROL_SBB_4,
  252. .clkcontrol_base = 0x00020000,
  253. .horztiming = 0x005aff1f,
  254. .verttiming = 0x16000e57,
  255. },
  256. /* just the standard LCD */
  257. [1] = {
  258. .name = "WWPC LCD",
  259. .xres = 240,
  260. .yres = 320,
  261. .bpp = 16,
  262. .control_base = 0x0006806A,
  263. .horztiming = 0x0A1010EF,
  264. .verttiming = 0x0301013F,
  265. .clkcontrol_base = 0x00018001,
  266. },
  267. /* Sharp 320x240 TFT panel */
  268. [2] = {
  269. .name = "Sharp_LQ038Q5DR01",
  270. .xres = 320,
  271. .yres = 240,
  272. .bpp = 16,
  273. .control_base =
  274. ( LCD_CONTROL_SBPPF_565
  275. | LCD_CONTROL_C
  276. | LCD_CONTROL_SM_0
  277. | LCD_CONTROL_DEFAULT_PO
  278. | LCD_CONTROL_PT
  279. | LCD_CONTROL_PC
  280. | LCD_CONTROL_BPP_16 ),
  281. .horztiming =
  282. ( LCD_HORZTIMING_HN2_N(8)
  283. | LCD_HORZTIMING_HN1_N(60)
  284. | LCD_HORZTIMING_HPW_N(12)
  285. | LCD_HORZTIMING_PPL_N(320) ),
  286. .verttiming =
  287. ( LCD_VERTTIMING_VN2_N(5)
  288. | LCD_VERTTIMING_VN1_N(17)
  289. | LCD_VERTTIMING_VPW_N(1)
  290. | LCD_VERTTIMING_LPP_N(240) ),
  291. .clkcontrol_base = LCD_CLKCONTROL_PCD_N(1),
  292. },
  293. /* Hitachi SP14Q005 and possibly others */
  294. [3] = {
  295. .name = "Hitachi_SP14Qxxx",
  296. .xres = 320,
  297. .yres = 240,
  298. .bpp = 4,
  299. .control_base =
  300. ( LCD_CONTROL_C
  301. | LCD_CONTROL_BPP_4 ),
  302. .horztiming =
  303. ( LCD_HORZTIMING_HN2_N(1)
  304. | LCD_HORZTIMING_HN1_N(1)
  305. | LCD_HORZTIMING_HPW_N(1)
  306. | LCD_HORZTIMING_PPL_N(320) ),
  307. .verttiming =
  308. ( LCD_VERTTIMING_VN2_N(1)
  309. | LCD_VERTTIMING_VN1_N(1)
  310. | LCD_VERTTIMING_VPW_N(1)
  311. | LCD_VERTTIMING_LPP_N(240) ),
  312. .clkcontrol_base = LCD_CLKCONTROL_PCD_N(4),
  313. },
  314. /* Generic 640x480 TFT panel */
  315. [4] = {
  316. .name = "TFT_640x480_16",
  317. .xres = 640,
  318. .yres = 480,
  319. .bpp = 16,
  320. .control_base = 0x004806a | LCD_CONTROL_DEFAULT_PO,
  321. .horztiming = 0x3434d67f,
  322. .verttiming = 0x0e0e39df,
  323. .clkcontrol_base = LCD_CLKCONTROL_PCD_N(1),
  324. },
  325. /* Pb1100 LCDB 640x480 PrimeView TFT panel */
  326. [5] = {
  327. .name = "PrimeView_640x480_16",
  328. .xres = 640,
  329. .yres = 480,
  330. .bpp = 16,
  331. .control_base = 0x0004886a | LCD_CONTROL_DEFAULT_PO,
  332. .horztiming = 0x0e4bfe7f,
  333. .verttiming = 0x210805df,
  334. .clkcontrol_base = 0x00038001,
  335. },
  336. };
  337. /********************************************************************/
  338. /* Inline helpers */
  339. #define panel_is_dual(panel) (panel->control_base & LCD_CONTROL_DP)
  340. #define panel_is_active(panel)(panel->control_base & LCD_CONTROL_PT)
  341. #define panel_is_color(panel) (panel->control_base & LCD_CONTROL_PC)
  342. #define panel_swap_rgb(panel) (panel->control_base & LCD_CONTROL_CCO)
  343. #if defined(CONFIG_COMPILE_TEST) && (!defined(CONFIG_MIPS) || defined(CONFIG_64BIT))
  344. /*
  345. * KSEG1ADDR() is defined in arch/mips/include/asm/addrspace.h
  346. * for 32 bit configurations. Provide a stub for compile testing
  347. * on other platforms.
  348. */
  349. #define KSEG1ADDR(x) (x)
  350. #endif
  351. #define DRIVER_NAME "au1100fb"
  352. #define DRIVER_DESC "LCD controller driver for AU1100 processors"
  353. #define to_au1100fb_device(_info) \
  354. (_info ? container_of(_info, struct au1100fb_device, info) : NULL);
  355. /* Bitfields format supported by the controller. Note that the order of formats
  356. * SHOULD be the same as in the LCD_CONTROL_SBPPF field, so we can retrieve the
  357. * right pixel format by doing rgb_bitfields[LCD_CONTROL_SBPPF_XXX >> LCD_CONTROL_SBPPF]
  358. */
  359. struct fb_bitfield rgb_bitfields[][4] =
  360. {
  361. /* Red, Green, Blue, Transp */
  362. { { 10, 6, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
  363. { { 11, 5, 0 }, { 5, 6, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
  364. { { 11, 5, 0 }, { 6, 5, 0 }, { 0, 6, 0 }, { 0, 0, 0 } },
  365. { { 10, 5, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 15, 1, 0 } },
  366. { { 11, 5, 0 }, { 6, 5, 0 }, { 1, 5, 0 }, { 0, 1, 0 } },
  367. /* The last is used to describe 12bpp format */
  368. { { 8, 4, 0 }, { 4, 4, 0 }, { 0, 4, 0 }, { 0, 0, 0 } },
  369. };
  370. /* fb_blank
  371. * Blank the screen. Depending on the mode, the screen will be
  372. * activated with the backlight color, or desactivated
  373. */
  374. static int au1100fb_fb_blank(int blank_mode, struct fb_info *fbi)
  375. {
  376. struct au1100fb_device *fbdev = to_au1100fb_device(fbi);
  377. pr_devel("fb_blank %d %p", blank_mode, fbi);
  378. switch (blank_mode) {
  379. case VESA_NO_BLANKING:
  380. /* Turn on panel */
  381. fbdev->regs->lcd_control |= LCD_CONTROL_GO;
  382. wmb(); /* drain writebuffer */
  383. break;
  384. case VESA_VSYNC_SUSPEND:
  385. case VESA_HSYNC_SUSPEND:
  386. case VESA_POWERDOWN:
  387. /* Turn off panel */
  388. fbdev->regs->lcd_control &= ~LCD_CONTROL_GO;
  389. wmb(); /* drain writebuffer */
  390. break;
  391. default:
  392. break;
  393. }
  394. return 0;
  395. }
  396. /*
  397. * Set hardware with var settings. This will enable the controller with a specific
  398. * mode, normally validated with the fb_check_var method
  399. */
  400. static int au1100fb_setmode(struct au1100fb_device *fbdev)
  401. {
  402. struct fb_info *info;
  403. u32 words;
  404. int index;
  405. if (!fbdev)
  406. return -EINVAL;
  407. info = &fbdev->info;
  408. /* Update var-dependent FB info */
  409. if (panel_is_active(fbdev->panel) || panel_is_color(fbdev->panel)) {
  410. if (info->var.bits_per_pixel <= 8) {
  411. /* palettized */
  412. info->var.red.offset = 0;
  413. info->var.red.length = info->var.bits_per_pixel;
  414. info->var.red.msb_right = 0;
  415. info->var.green.offset = 0;
  416. info->var.green.length = info->var.bits_per_pixel;
  417. info->var.green.msb_right = 0;
  418. info->var.blue.offset = 0;
  419. info->var.blue.length = info->var.bits_per_pixel;
  420. info->var.blue.msb_right = 0;
  421. info->var.transp.offset = 0;
  422. info->var.transp.length = 0;
  423. info->var.transp.msb_right = 0;
  424. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  425. info->fix.line_length = info->var.xres_virtual /
  426. (8/info->var.bits_per_pixel);
  427. } else {
  428. /* non-palettized */
  429. index = (fbdev->panel->control_base & LCD_CONTROL_SBPPF_MASK) >> LCD_CONTROL_SBPPF_BIT;
  430. info->var.red = rgb_bitfields[index][0];
  431. info->var.green = rgb_bitfields[index][1];
  432. info->var.blue = rgb_bitfields[index][2];
  433. info->var.transp = rgb_bitfields[index][3];
  434. info->fix.visual = FB_VISUAL_TRUECOLOR;
  435. info->fix.line_length = info->var.xres_virtual << 1; /* depth=16 */
  436. }
  437. } else {
  438. /* mono */
  439. info->fix.visual = FB_VISUAL_MONO10;
  440. info->fix.line_length = info->var.xres_virtual / 8;
  441. }
  442. info->screen_size = info->fix.line_length * info->var.yres_virtual;
  443. info->var.rotate = ((fbdev->panel->control_base&LCD_CONTROL_SM_MASK) \
  444. >> LCD_CONTROL_SM_BIT) * 90;
  445. /* Determine BPP mode and format */
  446. fbdev->regs->lcd_control = fbdev->panel->control_base;
  447. fbdev->regs->lcd_horztiming = fbdev->panel->horztiming;
  448. fbdev->regs->lcd_verttiming = fbdev->panel->verttiming;
  449. fbdev->regs->lcd_clkcontrol = fbdev->panel->clkcontrol_base;
  450. fbdev->regs->lcd_intenable = 0;
  451. fbdev->regs->lcd_intstatus = 0;
  452. fbdev->regs->lcd_dmaaddr0 = LCD_DMA_SA_N(fbdev->fb_phys);
  453. if (panel_is_dual(fbdev->panel)) {
  454. /* Second panel display seconf half of screen if possible,
  455. * otherwise display the same as the first panel */
  456. if (info->var.yres_virtual >= (info->var.yres << 1)) {
  457. fbdev->regs->lcd_dmaaddr1 = LCD_DMA_SA_N(fbdev->fb_phys +
  458. (info->fix.line_length *
  459. (info->var.yres_virtual >> 1)));
  460. } else {
  461. fbdev->regs->lcd_dmaaddr1 = LCD_DMA_SA_N(fbdev->fb_phys);
  462. }
  463. }
  464. words = info->fix.line_length / sizeof(u32);
  465. if (!info->var.rotate || (info->var.rotate == 180)) {
  466. words *= info->var.yres_virtual;
  467. if (info->var.rotate /* 180 */) {
  468. words -= (words % 8); /* should be divisable by 8 */
  469. }
  470. }
  471. fbdev->regs->lcd_words = LCD_WRD_WRDS_N(words);
  472. fbdev->regs->lcd_pwmdiv = 0;
  473. fbdev->regs->lcd_pwmhi = 0;
  474. /* Resume controller */
  475. fbdev->regs->lcd_control |= LCD_CONTROL_GO;
  476. mdelay(10);
  477. au1100fb_fb_blank(VESA_NO_BLANKING, info);
  478. return 0;
  479. }
  480. /* fb_setcolreg
  481. * Set color in LCD palette.
  482. */
  483. static int au1100fb_fb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue,
  484. unsigned transp, struct fb_info *fbi)
  485. {
  486. struct au1100fb_device *fbdev;
  487. u32 *palette;
  488. u32 value;
  489. fbdev = to_au1100fb_device(fbi);
  490. palette = fbdev->regs->lcd_palettebase;
  491. if (regno > (AU1100_LCD_NBR_PALETTE_ENTRIES - 1))
  492. return -EINVAL;
  493. if (fbi->var.grayscale) {
  494. /* Convert color to grayscale */
  495. red = green = blue =
  496. (19595 * red + 38470 * green + 7471 * blue) >> 16;
  497. }
  498. if (fbi->fix.visual == FB_VISUAL_TRUECOLOR) {
  499. /* Place color in the pseudopalette */
  500. if (regno > 16)
  501. return -EINVAL;
  502. palette = (u32*)fbi->pseudo_palette;
  503. red >>= (16 - fbi->var.red.length);
  504. green >>= (16 - fbi->var.green.length);
  505. blue >>= (16 - fbi->var.blue.length);
  506. value = (red << fbi->var.red.offset) |
  507. (green << fbi->var.green.offset)|
  508. (blue << fbi->var.blue.offset);
  509. value &= 0xFFFF;
  510. } else if (panel_is_active(fbdev->panel)) {
  511. /* COLOR TFT PALLETTIZED (use RGB 565) */
  512. value = (red & 0xF800)|((green >> 5) & 0x07E0)|((blue >> 11) & 0x001F);
  513. value &= 0xFFFF;
  514. } else if (panel_is_color(fbdev->panel)) {
  515. /* COLOR STN MODE */
  516. value = (((panel_swap_rgb(fbdev->panel) ? blue : red) >> 12) & 0x000F) |
  517. ((green >> 8) & 0x00F0) |
  518. (((panel_swap_rgb(fbdev->panel) ? red : blue) >> 4) & 0x0F00);
  519. value &= 0xFFF;
  520. } else {
  521. /* MONOCHROME MODE */
  522. value = (green >> 12) & 0x000F;
  523. value &= 0xF;
  524. }
  525. palette[regno] = value;
  526. return 0;
  527. }
  528. /* fb_pan_display
  529. * Pan display in x and/or y as specified
  530. */
  531. static int au1100fb_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *fbi)
  532. {
  533. struct au1100fb_device *fbdev;
  534. int dy;
  535. fbdev = to_au1100fb_device(fbi);
  536. pr_devel("fb_pan_display %p %p", var, fbi);
  537. if (!var || !fbdev) {
  538. return -EINVAL;
  539. }
  540. if (var->xoffset - fbi->var.xoffset) {
  541. /* No support for X panning for now! */
  542. return -EINVAL;
  543. }
  544. pr_devel("fb_pan_display 2 %p %p", var, fbi);
  545. dy = var->yoffset - fbi->var.yoffset;
  546. if (dy) {
  547. u32 dmaaddr;
  548. pr_devel("Panning screen of %d lines", dy);
  549. dmaaddr = fbdev->regs->lcd_dmaaddr0;
  550. dmaaddr += (fbi->fix.line_length * dy);
  551. /* TODO: Wait for current frame to finished */
  552. fbdev->regs->lcd_dmaaddr0 = LCD_DMA_SA_N(dmaaddr);
  553. if (panel_is_dual(fbdev->panel)) {
  554. dmaaddr = fbdev->regs->lcd_dmaaddr1;
  555. dmaaddr += (fbi->fix.line_length * dy);
  556. fbdev->regs->lcd_dmaaddr0 = LCD_DMA_SA_N(dmaaddr);
  557. }
  558. }
  559. pr_devel("fb_pan_display 3 %p %p", var, fbi);
  560. return 0;
  561. }
  562. /* fb_mmap
  563. * Map video memory in user space. We don't use the generic fb_mmap method mainly
  564. * to allow the use of the TLB streaming flag (CCA=6)
  565. */
  566. static int au1100fb_fb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
  567. {
  568. struct au1100fb_device *fbdev = to_au1100fb_device(fbi);
  569. vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
  570. #ifndef CONFIG_S390
  571. /* On s390 pgprot_val() is a function and thus not a lvalue */
  572. pgprot_val(vma->vm_page_prot) |= (6 << 9); //CCA=6
  573. #endif
  574. return dma_mmap_coherent(fbdev->dev, vma, fbdev->fb_mem, fbdev->fb_phys,
  575. fbdev->fb_len);
  576. }
  577. static const struct fb_ops au1100fb_ops = {
  578. .owner = THIS_MODULE,
  579. __FB_DEFAULT_IOMEM_OPS_RDWR,
  580. .fb_setcolreg = au1100fb_fb_setcolreg,
  581. .fb_blank = au1100fb_fb_blank,
  582. .fb_pan_display = au1100fb_fb_pan_display,
  583. __FB_DEFAULT_IOMEM_OPS_DRAW,
  584. .fb_mmap = au1100fb_fb_mmap,
  585. };
  586. /*-------------------------------------------------------------------------*/
  587. static int au1100fb_setup(struct au1100fb_device *fbdev)
  588. {
  589. char *this_opt, *options;
  590. int num_panels = ARRAY_SIZE(known_lcd_panels);
  591. if (num_panels <= 0) {
  592. pr_err("No LCD panels supported by driver!");
  593. return -ENODEV;
  594. }
  595. if (fb_get_options(DRIVER_NAME, &options))
  596. return -ENODEV;
  597. if (!options)
  598. return -ENODEV;
  599. while ((this_opt = strsep(&options, ",")) != NULL) {
  600. /* Panel option */
  601. if (!strncmp(this_opt, "panel:", 6)) {
  602. int i;
  603. this_opt += 6;
  604. for (i = 0; i < num_panels; i++) {
  605. if (!strncmp(this_opt, known_lcd_panels[i].name,
  606. strlen(this_opt))) {
  607. fbdev->panel = &known_lcd_panels[i];
  608. fbdev->panel_idx = i;
  609. break;
  610. }
  611. }
  612. if (i >= num_panels) {
  613. pr_warn("Panel '%s' not supported!", this_opt);
  614. return -ENODEV;
  615. }
  616. }
  617. /* Unsupported option */
  618. else
  619. pr_warn("Unsupported option \"%s\"", this_opt);
  620. }
  621. pr_info("Panel=%s", fbdev->panel->name);
  622. return 0;
  623. }
  624. static int au1100fb_drv_probe(struct platform_device *dev)
  625. {
  626. struct au1100fb_device *fbdev;
  627. struct resource *regs_res;
  628. struct clk *c;
  629. /* Allocate new device private */
  630. fbdev = devm_kzalloc(&dev->dev, sizeof(*fbdev), GFP_KERNEL);
  631. if (!fbdev)
  632. return -ENOMEM;
  633. if (au1100fb_setup(fbdev))
  634. goto failed;
  635. platform_set_drvdata(dev, (void *)fbdev);
  636. fbdev->dev = &dev->dev;
  637. /* Allocate region for our registers and map them */
  638. regs_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  639. if (!regs_res) {
  640. pr_err("fail to retrieve registers resource");
  641. return -EFAULT;
  642. }
  643. fbdev->info.fix = (struct fb_fix_screeninfo) {
  644. .mmio_start = regs_res->start,
  645. .mmio_len = resource_size(regs_res),
  646. .id = "AU1100 FB",
  647. .xpanstep = 1,
  648. .ypanstep = 1,
  649. .type = FB_TYPE_PACKED_PIXELS,
  650. .accel = FB_ACCEL_NONE,
  651. };
  652. if (!devm_request_mem_region(&dev->dev,
  653. fbdev->info.fix.mmio_start,
  654. fbdev->info.fix.mmio_len,
  655. DRIVER_NAME)) {
  656. pr_err("fail to lock memory region at 0x%08lx",
  657. fbdev->info.fix.mmio_start);
  658. return -EBUSY;
  659. }
  660. fbdev->regs = (struct au1100fb_regs*)KSEG1ADDR(fbdev->info.fix.mmio_start);
  661. pr_devel("Register memory map at %p", fbdev->regs);
  662. pr_devel("phys=0x%08x, size=%zu", fbdev->regs_phys, fbdev->regs_len);
  663. c = clk_get(NULL, "lcd_intclk");
  664. if (!IS_ERR(c)) {
  665. fbdev->lcdclk = c;
  666. clk_set_rate(c, 48000000);
  667. clk_prepare_enable(c);
  668. }
  669. /* Allocate the framebuffer to the maximum screen size * nbr of video buffers */
  670. fbdev->fb_len = fbdev->panel->xres * fbdev->panel->yres *
  671. (fbdev->panel->bpp >> 3) * AU1100FB_NBR_VIDEO_BUFFERS;
  672. fbdev->fb_mem = dmam_alloc_coherent(&dev->dev,
  673. PAGE_ALIGN(fbdev->fb_len),
  674. &fbdev->fb_phys, GFP_KERNEL);
  675. if (!fbdev->fb_mem) {
  676. pr_err("fail to allocate framebuffer (size: %zuK))",
  677. fbdev->fb_len / 1024);
  678. return -ENOMEM;
  679. }
  680. fbdev->info.fix.smem_start = fbdev->fb_phys;
  681. fbdev->info.fix.smem_len = fbdev->fb_len;
  682. pr_devel("Framebuffer memory map at %p", fbdev->fb_mem);
  683. pr_devel("phys=0x%pad, size=%zuK", &fbdev->fb_phys, fbdev->fb_len / 1024);
  684. /* load the panel info into the var struct */
  685. fbdev->info.var = (struct fb_var_screeninfo) {
  686. .activate = FB_ACTIVATE_NOW,
  687. .height = -1,
  688. .width = -1,
  689. .vmode = FB_VMODE_NONINTERLACED,
  690. .bits_per_pixel = fbdev->panel->bpp,
  691. .xres = fbdev->panel->xres,
  692. .xres_virtual = fbdev->panel->xres,
  693. .yres = fbdev->panel->yres,
  694. .yres_virtual = fbdev->panel->yres,
  695. };
  696. fbdev->info.screen_base = fbdev->fb_mem;
  697. fbdev->info.fbops = &au1100fb_ops;
  698. fbdev->info.pseudo_palette =
  699. devm_kcalloc(&dev->dev, 16, sizeof(u32), GFP_KERNEL);
  700. if (!fbdev->info.pseudo_palette)
  701. return -ENOMEM;
  702. if (fb_alloc_cmap(&fbdev->info.cmap, AU1100_LCD_NBR_PALETTE_ENTRIES, 0) < 0) {
  703. pr_err("Fail to allocate colormap (%d entries)",
  704. AU1100_LCD_NBR_PALETTE_ENTRIES);
  705. return -EFAULT;
  706. }
  707. /* Set h/w registers */
  708. au1100fb_setmode(fbdev);
  709. /* Register new framebuffer */
  710. if (register_framebuffer(&fbdev->info) < 0) {
  711. pr_err("cannot register new framebuffer");
  712. goto failed;
  713. }
  714. return 0;
  715. failed:
  716. if (fbdev->lcdclk) {
  717. clk_disable_unprepare(fbdev->lcdclk);
  718. clk_put(fbdev->lcdclk);
  719. }
  720. if (fbdev->info.cmap.len != 0) {
  721. fb_dealloc_cmap(&fbdev->info.cmap);
  722. }
  723. return -ENODEV;
  724. }
  725. static void au1100fb_drv_remove(struct platform_device *dev)
  726. {
  727. struct au1100fb_device *fbdev = NULL;
  728. fbdev = platform_get_drvdata(dev);
  729. #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
  730. au1100fb_fb_blank(VESA_POWERDOWN, &fbdev->info);
  731. #endif
  732. fbdev->regs->lcd_control &= ~LCD_CONTROL_GO;
  733. /* Clean up all probe data */
  734. unregister_framebuffer(&fbdev->info);
  735. fb_dealloc_cmap(&fbdev->info.cmap);
  736. if (fbdev->lcdclk) {
  737. clk_disable_unprepare(fbdev->lcdclk);
  738. clk_put(fbdev->lcdclk);
  739. }
  740. }
  741. #ifdef CONFIG_PM
  742. static int au1100fb_drv_suspend(struct platform_device *dev, pm_message_t state)
  743. {
  744. struct au1100fb_device *fbdev = platform_get_drvdata(dev);
  745. if (!fbdev)
  746. return 0;
  747. /* Blank the LCD */
  748. au1100fb_fb_blank(VESA_POWERDOWN, &fbdev->info);
  749. clk_disable(fbdev->lcdclk);
  750. memcpy(&fbdev->pm_regs, fbdev->regs, sizeof(struct au1100fb_regs));
  751. return 0;
  752. }
  753. static int au1100fb_drv_resume(struct platform_device *dev)
  754. {
  755. struct au1100fb_device *fbdev = platform_get_drvdata(dev);
  756. int ret;
  757. if (!fbdev)
  758. return 0;
  759. memcpy(fbdev->regs, &fbdev->pm_regs, sizeof(struct au1100fb_regs));
  760. ret = clk_enable(fbdev->lcdclk);
  761. if (ret)
  762. return ret;
  763. /* Unblank the LCD */
  764. au1100fb_fb_blank(VESA_NO_BLANKING, &fbdev->info);
  765. return 0;
  766. }
  767. #else
  768. #define au1100fb_drv_suspend NULL
  769. #define au1100fb_drv_resume NULL
  770. #endif
  771. static struct platform_driver au1100fb_driver = {
  772. .driver = {
  773. .name = "au1100-lcd",
  774. },
  775. .probe = au1100fb_drv_probe,
  776. .remove = au1100fb_drv_remove,
  777. .suspend = au1100fb_drv_suspend,
  778. .resume = au1100fb_drv_resume,
  779. };
  780. module_platform_driver(au1100fb_driver);
  781. MODULE_DESCRIPTION(DRIVER_DESC);
  782. MODULE_LICENSE("GPL");