atmel_lcdfb.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. /*
  2. * Driver for AT91/AT32 LCD Controller
  3. *
  4. * Copyright (C) 2007 Atmel Corporation
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive for
  8. * more details.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/clk.h>
  15. #include <linux/fb.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/backlight.h>
  19. #include <linux/gfp.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_data/atmel.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_gpio.h>
  25. #include <video/of_display_timing.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <video/videomode.h>
  28. #include <mach/cpu.h>
  29. #include <asm/gpio.h>
  30. #include <video/atmel_lcdc.h>
  31. struct atmel_lcdfb_config {
  32. bool have_alt_pixclock;
  33. bool have_hozval;
  34. bool have_intensity_bit;
  35. };
  36. /* LCD Controller info data structure, stored in device platform_data */
  37. struct atmel_lcdfb_info {
  38. spinlock_t lock;
  39. struct fb_info *info;
  40. void __iomem *mmio;
  41. int irq_base;
  42. struct work_struct task;
  43. unsigned int smem_len;
  44. struct platform_device *pdev;
  45. struct clk *bus_clk;
  46. struct clk *lcdc_clk;
  47. struct backlight_device *backlight;
  48. u8 bl_power;
  49. u8 saved_lcdcon;
  50. u32 pseudo_palette[16];
  51. bool have_intensity_bit;
  52. struct atmel_lcdfb_pdata pdata;
  53. struct atmel_lcdfb_config *config;
  54. struct regulator *reg_lcd;
  55. };
  56. struct atmel_lcdfb_power_ctrl_gpio {
  57. int gpio;
  58. int active_low;
  59. struct list_head list;
  60. };
  61. #define lcdc_readl(sinfo, reg) __raw_readl((sinfo)->mmio+(reg))
  62. #define lcdc_writel(sinfo, reg, val) __raw_writel((val), (sinfo)->mmio+(reg))
  63. /* configurable parameters */
  64. #define ATMEL_LCDC_CVAL_DEFAULT 0xc8
  65. #define ATMEL_LCDC_DMA_BURST_LEN 8 /* words */
  66. #define ATMEL_LCDC_FIFO_SIZE 512 /* words */
  67. static struct atmel_lcdfb_config at91sam9261_config = {
  68. .have_hozval = true,
  69. .have_intensity_bit = true,
  70. };
  71. static struct atmel_lcdfb_config at91sam9263_config = {
  72. .have_intensity_bit = true,
  73. };
  74. static struct atmel_lcdfb_config at91sam9g10_config = {
  75. .have_hozval = true,
  76. };
  77. static struct atmel_lcdfb_config at91sam9g45_config = {
  78. .have_alt_pixclock = true,
  79. };
  80. static struct atmel_lcdfb_config at91sam9g45es_config = {
  81. };
  82. static struct atmel_lcdfb_config at91sam9rl_config = {
  83. .have_intensity_bit = true,
  84. };
  85. static struct atmel_lcdfb_config at32ap_config = {
  86. .have_hozval = true,
  87. };
  88. static const struct platform_device_id atmel_lcdfb_devtypes[] = {
  89. {
  90. .name = "at91sam9261-lcdfb",
  91. .driver_data = (unsigned long)&at91sam9261_config,
  92. }, {
  93. .name = "at91sam9263-lcdfb",
  94. .driver_data = (unsigned long)&at91sam9263_config,
  95. }, {
  96. .name = "at91sam9g10-lcdfb",
  97. .driver_data = (unsigned long)&at91sam9g10_config,
  98. }, {
  99. .name = "at91sam9g45-lcdfb",
  100. .driver_data = (unsigned long)&at91sam9g45_config,
  101. }, {
  102. .name = "at91sam9g45es-lcdfb",
  103. .driver_data = (unsigned long)&at91sam9g45es_config,
  104. }, {
  105. .name = "at91sam9rl-lcdfb",
  106. .driver_data = (unsigned long)&at91sam9rl_config,
  107. }, {
  108. .name = "at32ap-lcdfb",
  109. .driver_data = (unsigned long)&at32ap_config,
  110. }, {
  111. /* terminator */
  112. }
  113. };
  114. MODULE_DEVICE_TABLE(platform, atmel_lcdfb_devtypes);
  115. static struct atmel_lcdfb_config *
  116. atmel_lcdfb_get_config(struct platform_device *pdev)
  117. {
  118. unsigned long data;
  119. data = platform_get_device_id(pdev)->driver_data;
  120. return (struct atmel_lcdfb_config *)data;
  121. }
  122. #if defined(CONFIG_ARCH_AT91)
  123. #define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
  124. | FBINFO_PARTIAL_PAN_OK \
  125. | FBINFO_HWACCEL_YPAN)
  126. static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
  127. struct fb_var_screeninfo *var,
  128. struct fb_info *info)
  129. {
  130. }
  131. #elif defined(CONFIG_AVR32)
  132. #define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
  133. | FBINFO_PARTIAL_PAN_OK \
  134. | FBINFO_HWACCEL_XPAN \
  135. | FBINFO_HWACCEL_YPAN)
  136. static void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
  137. struct fb_var_screeninfo *var,
  138. struct fb_info *info)
  139. {
  140. u32 dma2dcfg;
  141. u32 pixeloff;
  142. pixeloff = (var->xoffset * info->var.bits_per_pixel) & 0x1f;
  143. dma2dcfg = (info->var.xres_virtual - info->var.xres)
  144. * info->var.bits_per_pixel / 8;
  145. dma2dcfg |= pixeloff << ATMEL_LCDC_PIXELOFF_OFFSET;
  146. lcdc_writel(sinfo, ATMEL_LCDC_DMA2DCFG, dma2dcfg);
  147. /* Update configuration */
  148. lcdc_writel(sinfo, ATMEL_LCDC_DMACON,
  149. lcdc_readl(sinfo, ATMEL_LCDC_DMACON)
  150. | ATMEL_LCDC_DMAUPDT);
  151. }
  152. #endif
  153. static u32 contrast_ctr = ATMEL_LCDC_PS_DIV8
  154. | ATMEL_LCDC_POL_POSITIVE
  155. | ATMEL_LCDC_ENA_PWMENABLE;
  156. #ifdef CONFIG_BACKLIGHT_ATMEL_LCDC
  157. /* some bl->props field just changed */
  158. static int atmel_bl_update_status(struct backlight_device *bl)
  159. {
  160. struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
  161. int power = sinfo->bl_power;
  162. int brightness = bl->props.brightness;
  163. /* REVISIT there may be a meaningful difference between
  164. * fb_blank and power ... there seem to be some cases
  165. * this doesn't handle correctly.
  166. */
  167. if (bl->props.fb_blank != sinfo->bl_power)
  168. power = bl->props.fb_blank;
  169. else if (bl->props.power != sinfo->bl_power)
  170. power = bl->props.power;
  171. if (brightness < 0 && power == FB_BLANK_UNBLANK)
  172. brightness = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
  173. else if (power != FB_BLANK_UNBLANK)
  174. brightness = 0;
  175. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, brightness);
  176. if (contrast_ctr & ATMEL_LCDC_POL_POSITIVE)
  177. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR,
  178. brightness ? contrast_ctr : 0);
  179. else
  180. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
  181. bl->props.fb_blank = bl->props.power = sinfo->bl_power = power;
  182. return 0;
  183. }
  184. static int atmel_bl_get_brightness(struct backlight_device *bl)
  185. {
  186. struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
  187. return lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
  188. }
  189. static const struct backlight_ops atmel_lcdc_bl_ops = {
  190. .update_status = atmel_bl_update_status,
  191. .get_brightness = atmel_bl_get_brightness,
  192. };
  193. static void init_backlight(struct atmel_lcdfb_info *sinfo)
  194. {
  195. struct backlight_properties props;
  196. struct backlight_device *bl;
  197. sinfo->bl_power = FB_BLANK_UNBLANK;
  198. if (sinfo->backlight)
  199. return;
  200. memset(&props, 0, sizeof(struct backlight_properties));
  201. props.type = BACKLIGHT_RAW;
  202. props.max_brightness = 0xff;
  203. bl = backlight_device_register("backlight", &sinfo->pdev->dev, sinfo,
  204. &atmel_lcdc_bl_ops, &props);
  205. if (IS_ERR(bl)) {
  206. dev_err(&sinfo->pdev->dev, "error %ld on backlight register\n",
  207. PTR_ERR(bl));
  208. return;
  209. }
  210. sinfo->backlight = bl;
  211. bl->props.power = FB_BLANK_UNBLANK;
  212. bl->props.fb_blank = FB_BLANK_UNBLANK;
  213. bl->props.brightness = atmel_bl_get_brightness(bl);
  214. }
  215. static void exit_backlight(struct atmel_lcdfb_info *sinfo)
  216. {
  217. if (!sinfo->backlight)
  218. return;
  219. if (sinfo->backlight->ops) {
  220. sinfo->backlight->props.power = FB_BLANK_POWERDOWN;
  221. sinfo->backlight->ops->update_status(sinfo->backlight);
  222. }
  223. backlight_device_unregister(sinfo->backlight);
  224. }
  225. #else
  226. static void init_backlight(struct atmel_lcdfb_info *sinfo)
  227. {
  228. dev_warn(&sinfo->pdev->dev, "backlight control is not available\n");
  229. }
  230. static void exit_backlight(struct atmel_lcdfb_info *sinfo)
  231. {
  232. }
  233. #endif
  234. static void init_contrast(struct atmel_lcdfb_info *sinfo)
  235. {
  236. struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
  237. /* contrast pwm can be 'inverted' */
  238. if (pdata->lcdcon_pol_negative)
  239. contrast_ctr &= ~(ATMEL_LCDC_POL_POSITIVE);
  240. /* have some default contrast/backlight settings */
  241. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
  242. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT);
  243. if (pdata->lcdcon_is_backlight)
  244. init_backlight(sinfo);
  245. }
  246. static inline void atmel_lcdfb_power_control(struct atmel_lcdfb_info *sinfo, int on)
  247. {
  248. int ret;
  249. struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
  250. if (pdata->atmel_lcdfb_power_control)
  251. pdata->atmel_lcdfb_power_control(pdata, on);
  252. else if (sinfo->reg_lcd) {
  253. if (on) {
  254. ret = regulator_enable(sinfo->reg_lcd);
  255. if (ret)
  256. dev_err(&sinfo->pdev->dev,
  257. "lcd regulator enable failed: %d\n", ret);
  258. } else {
  259. ret = regulator_disable(sinfo->reg_lcd);
  260. if (ret)
  261. dev_err(&sinfo->pdev->dev,
  262. "lcd regulator disable failed: %d\n", ret);
  263. }
  264. }
  265. }
  266. static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata = {
  267. .type = FB_TYPE_PACKED_PIXELS,
  268. .visual = FB_VISUAL_TRUECOLOR,
  269. .xpanstep = 0,
  270. .ypanstep = 1,
  271. .ywrapstep = 0,
  272. .accel = FB_ACCEL_NONE,
  273. };
  274. static unsigned long compute_hozval(struct atmel_lcdfb_info *sinfo,
  275. unsigned long xres)
  276. {
  277. unsigned long lcdcon2;
  278. unsigned long value;
  279. if (!sinfo->config->have_hozval)
  280. return xres;
  281. lcdcon2 = lcdc_readl(sinfo, ATMEL_LCDC_LCDCON2);
  282. value = xres;
  283. if ((lcdcon2 & ATMEL_LCDC_DISTYPE) != ATMEL_LCDC_DISTYPE_TFT) {
  284. /* STN display */
  285. if ((lcdcon2 & ATMEL_LCDC_DISTYPE) == ATMEL_LCDC_DISTYPE_STNCOLOR) {
  286. value *= 3;
  287. }
  288. if ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_4
  289. || ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_8
  290. && (lcdcon2 & ATMEL_LCDC_SCANMOD) == ATMEL_LCDC_SCANMOD_DUAL ))
  291. value = DIV_ROUND_UP(value, 4);
  292. else
  293. value = DIV_ROUND_UP(value, 8);
  294. }
  295. return value;
  296. }
  297. static void atmel_lcdfb_stop_nowait(struct atmel_lcdfb_info *sinfo)
  298. {
  299. struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
  300. /* Turn off the LCD controller and the DMA controller */
  301. lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
  302. pdata->guard_time << ATMEL_LCDC_GUARDT_OFFSET);
  303. /* Wait for the LCDC core to become idle */
  304. while (lcdc_readl(sinfo, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
  305. msleep(10);
  306. lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0);
  307. }
  308. static void atmel_lcdfb_stop(struct atmel_lcdfb_info *sinfo)
  309. {
  310. atmel_lcdfb_stop_nowait(sinfo);
  311. /* Wait for DMA engine to become idle... */
  312. while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
  313. msleep(10);
  314. }
  315. static void atmel_lcdfb_start(struct atmel_lcdfb_info *sinfo)
  316. {
  317. struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
  318. lcdc_writel(sinfo, ATMEL_LCDC_DMACON, pdata->default_dmacon);
  319. lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
  320. (pdata->guard_time << ATMEL_LCDC_GUARDT_OFFSET)
  321. | ATMEL_LCDC_PWR);
  322. }
  323. static void atmel_lcdfb_update_dma(struct fb_info *info,
  324. struct fb_var_screeninfo *var)
  325. {
  326. struct atmel_lcdfb_info *sinfo = info->par;
  327. struct fb_fix_screeninfo *fix = &info->fix;
  328. unsigned long dma_addr;
  329. dma_addr = (fix->smem_start + var->yoffset * fix->line_length
  330. + var->xoffset * info->var.bits_per_pixel / 8);
  331. dma_addr &= ~3UL;
  332. /* Set framebuffer DMA base address and pixel offset */
  333. lcdc_writel(sinfo, ATMEL_LCDC_DMABADDR1, dma_addr);
  334. atmel_lcdfb_update_dma2d(sinfo, var, info);
  335. }
  336. static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info *sinfo)
  337. {
  338. struct fb_info *info = sinfo->info;
  339. dma_free_writecombine(info->device, info->fix.smem_len,
  340. info->screen_base, info->fix.smem_start);
  341. }
  342. /**
  343. * atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory
  344. * @sinfo: the frame buffer to allocate memory for
  345. *
  346. * This function is called only from the atmel_lcdfb_probe()
  347. * so no locking by fb_info->mm_lock around smem_len setting is needed.
  348. */
  349. static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
  350. {
  351. struct fb_info *info = sinfo->info;
  352. struct fb_var_screeninfo *var = &info->var;
  353. unsigned int smem_len;
  354. smem_len = (var->xres_virtual * var->yres_virtual
  355. * ((var->bits_per_pixel + 7) / 8));
  356. info->fix.smem_len = max(smem_len, sinfo->smem_len);
  357. info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len,
  358. (dma_addr_t *)&info->fix.smem_start, GFP_KERNEL);
  359. if (!info->screen_base) {
  360. return -ENOMEM;
  361. }
  362. memset(info->screen_base, 0, info->fix.smem_len);
  363. return 0;
  364. }
  365. static const struct fb_videomode *atmel_lcdfb_choose_mode(struct fb_var_screeninfo *var,
  366. struct fb_info *info)
  367. {
  368. struct fb_videomode varfbmode;
  369. const struct fb_videomode *fbmode = NULL;
  370. fb_var_to_videomode(&varfbmode, var);
  371. fbmode = fb_find_nearest_mode(&varfbmode, &info->modelist);
  372. if (fbmode)
  373. fb_videomode_to_var(var, fbmode);
  374. return fbmode;
  375. }
  376. /**
  377. * atmel_lcdfb_check_var - Validates a var passed in.
  378. * @var: frame buffer variable screen structure
  379. * @info: frame buffer structure that represents a single frame buffer
  380. *
  381. * Checks to see if the hardware supports the state requested by
  382. * var passed in. This function does not alter the hardware
  383. * state!!! This means the data stored in struct fb_info and
  384. * struct atmel_lcdfb_info do not change. This includes the var
  385. * inside of struct fb_info. Do NOT change these. This function
  386. * can be called on its own if we intent to only test a mode and
  387. * not actually set it. The stuff in modedb.c is a example of
  388. * this. If the var passed in is slightly off by what the
  389. * hardware can support then we alter the var PASSED in to what
  390. * we can do. If the hardware doesn't support mode change a
  391. * -EINVAL will be returned by the upper layers. You don't need
  392. * to implement this function then. If you hardware doesn't
  393. * support changing the resolution then this function is not
  394. * needed. In this case the driver would just provide a var that
  395. * represents the static state the screen is in.
  396. *
  397. * Returns negative errno on error, or zero on success.
  398. */
  399. static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
  400. struct fb_info *info)
  401. {
  402. struct device *dev = info->device;
  403. struct atmel_lcdfb_info *sinfo = info->par;
  404. struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
  405. unsigned long clk_value_khz;
  406. clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
  407. dev_dbg(dev, "%s:\n", __func__);
  408. if (!(var->pixclock && var->bits_per_pixel)) {
  409. /* choose a suitable mode if possible */
  410. if (!atmel_lcdfb_choose_mode(var, info)) {
  411. dev_err(dev, "needed value not specified\n");
  412. return -EINVAL;
  413. }
  414. }
  415. dev_dbg(dev, " resolution: %ux%u\n", var->xres, var->yres);
  416. dev_dbg(dev, " pixclk: %lu KHz\n", PICOS2KHZ(var->pixclock));
  417. dev_dbg(dev, " bpp: %u\n", var->bits_per_pixel);
  418. dev_dbg(dev, " clk: %lu KHz\n", clk_value_khz);
  419. if (PICOS2KHZ(var->pixclock) > clk_value_khz) {
  420. dev_err(dev, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var->pixclock));
  421. return -EINVAL;
  422. }
  423. /* Do not allow to have real resoulution larger than virtual */
  424. if (var->xres > var->xres_virtual)
  425. var->xres_virtual = var->xres;
  426. if (var->yres > var->yres_virtual)
  427. var->yres_virtual = var->yres;
  428. /* Force same alignment for each line */
  429. var->xres = (var->xres + 3) & ~3UL;
  430. var->xres_virtual = (var->xres_virtual + 3) & ~3UL;
  431. var->red.msb_right = var->green.msb_right = var->blue.msb_right = 0;
  432. var->transp.msb_right = 0;
  433. var->transp.offset = var->transp.length = 0;
  434. var->xoffset = var->yoffset = 0;
  435. if (info->fix.smem_len) {
  436. unsigned int smem_len = (var->xres_virtual * var->yres_virtual
  437. * ((var->bits_per_pixel + 7) / 8));
  438. if (smem_len > info->fix.smem_len) {
  439. dev_err(dev, "Frame buffer is too small (%u) for screen size (need at least %u)\n",
  440. info->fix.smem_len, smem_len);
  441. return -EINVAL;
  442. }
  443. }
  444. /* Saturate vertical and horizontal timings at maximum values */
  445. var->vsync_len = min_t(u32, var->vsync_len,
  446. (ATMEL_LCDC_VPW >> ATMEL_LCDC_VPW_OFFSET) + 1);
  447. var->upper_margin = min_t(u32, var->upper_margin,
  448. ATMEL_LCDC_VBP >> ATMEL_LCDC_VBP_OFFSET);
  449. var->lower_margin = min_t(u32, var->lower_margin,
  450. ATMEL_LCDC_VFP);
  451. var->right_margin = min_t(u32, var->right_margin,
  452. (ATMEL_LCDC_HFP >> ATMEL_LCDC_HFP_OFFSET) + 1);
  453. var->hsync_len = min_t(u32, var->hsync_len,
  454. (ATMEL_LCDC_HPW >> ATMEL_LCDC_HPW_OFFSET) + 1);
  455. var->left_margin = min_t(u32, var->left_margin,
  456. ATMEL_LCDC_HBP + 1);
  457. /* Some parameters can't be zero */
  458. var->vsync_len = max_t(u32, var->vsync_len, 1);
  459. var->right_margin = max_t(u32, var->right_margin, 1);
  460. var->hsync_len = max_t(u32, var->hsync_len, 1);
  461. var->left_margin = max_t(u32, var->left_margin, 1);
  462. switch (var->bits_per_pixel) {
  463. case 1:
  464. case 2:
  465. case 4:
  466. case 8:
  467. var->red.offset = var->green.offset = var->blue.offset = 0;
  468. var->red.length = var->green.length = var->blue.length
  469. = var->bits_per_pixel;
  470. break;
  471. case 16:
  472. /* Older SOCs use IBGR:555 rather than BGR:565. */
  473. if (sinfo->config->have_intensity_bit)
  474. var->green.length = 5;
  475. else
  476. var->green.length = 6;
  477. if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
  478. /* RGB:5X5 mode */
  479. var->red.offset = var->green.length + 5;
  480. var->blue.offset = 0;
  481. } else {
  482. /* BGR:5X5 mode */
  483. var->red.offset = 0;
  484. var->blue.offset = var->green.length + 5;
  485. }
  486. var->green.offset = 5;
  487. var->red.length = var->blue.length = 5;
  488. break;
  489. case 32:
  490. var->transp.offset = 24;
  491. var->transp.length = 8;
  492. /* fall through */
  493. case 24:
  494. if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
  495. /* RGB:888 mode */
  496. var->red.offset = 16;
  497. var->blue.offset = 0;
  498. } else {
  499. /* BGR:888 mode */
  500. var->red.offset = 0;
  501. var->blue.offset = 16;
  502. }
  503. var->green.offset = 8;
  504. var->red.length = var->green.length = var->blue.length = 8;
  505. break;
  506. default:
  507. dev_err(dev, "color depth %d not supported\n",
  508. var->bits_per_pixel);
  509. return -EINVAL;
  510. }
  511. return 0;
  512. }
  513. /*
  514. * LCD reset sequence
  515. */
  516. static void atmel_lcdfb_reset(struct atmel_lcdfb_info *sinfo)
  517. {
  518. might_sleep();
  519. atmel_lcdfb_stop(sinfo);
  520. atmel_lcdfb_start(sinfo);
  521. }
  522. /**
  523. * atmel_lcdfb_set_par - Alters the hardware state.
  524. * @info: frame buffer structure that represents a single frame buffer
  525. *
  526. * Using the fb_var_screeninfo in fb_info we set the resolution
  527. * of the this particular framebuffer. This function alters the
  528. * par AND the fb_fix_screeninfo stored in fb_info. It doesn't
  529. * not alter var in fb_info since we are using that data. This
  530. * means we depend on the data in var inside fb_info to be
  531. * supported by the hardware. atmel_lcdfb_check_var is always called
  532. * before atmel_lcdfb_set_par to ensure this. Again if you can't
  533. * change the resolution you don't need this function.
  534. *
  535. */
  536. static int atmel_lcdfb_set_par(struct fb_info *info)
  537. {
  538. struct atmel_lcdfb_info *sinfo = info->par;
  539. struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
  540. unsigned long hozval_linesz;
  541. unsigned long value;
  542. unsigned long clk_value_khz;
  543. unsigned long bits_per_line;
  544. unsigned long pix_factor = 2;
  545. might_sleep();
  546. dev_dbg(info->device, "%s:\n", __func__);
  547. dev_dbg(info->device, " * resolution: %ux%u (%ux%u virtual)\n",
  548. info->var.xres, info->var.yres,
  549. info->var.xres_virtual, info->var.yres_virtual);
  550. atmel_lcdfb_stop_nowait(sinfo);
  551. if (info->var.bits_per_pixel == 1)
  552. info->fix.visual = FB_VISUAL_MONO01;
  553. else if (info->var.bits_per_pixel <= 8)
  554. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  555. else
  556. info->fix.visual = FB_VISUAL_TRUECOLOR;
  557. bits_per_line = info->var.xres_virtual * info->var.bits_per_pixel;
  558. info->fix.line_length = DIV_ROUND_UP(bits_per_line, 8);
  559. /* Re-initialize the DMA engine... */
  560. dev_dbg(info->device, " * update DMA engine\n");
  561. atmel_lcdfb_update_dma(info, &info->var);
  562. /* ...set frame size and burst length = 8 words (?) */
  563. value = (info->var.yres * info->var.xres * info->var.bits_per_pixel) / 32;
  564. value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET);
  565. lcdc_writel(sinfo, ATMEL_LCDC_DMAFRMCFG, value);
  566. /* Now, the LCDC core... */
  567. /* Set pixel clock */
  568. if (sinfo->config->have_alt_pixclock)
  569. pix_factor = 1;
  570. clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
  571. value = DIV_ROUND_UP(clk_value_khz, PICOS2KHZ(info->var.pixclock));
  572. if (value < pix_factor) {
  573. dev_notice(info->device, "Bypassing pixel clock divider\n");
  574. lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
  575. } else {
  576. value = (value / pix_factor) - 1;
  577. dev_dbg(info->device, " * programming CLKVAL = 0x%08lx\n",
  578. value);
  579. lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1,
  580. value << ATMEL_LCDC_CLKVAL_OFFSET);
  581. info->var.pixclock =
  582. KHZ2PICOS(clk_value_khz / (pix_factor * (value + 1)));
  583. dev_dbg(info->device, " updated pixclk: %lu KHz\n",
  584. PICOS2KHZ(info->var.pixclock));
  585. }
  586. /* Initialize control register 2 */
  587. value = pdata->default_lcdcon2;
  588. if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
  589. value |= ATMEL_LCDC_INVLINE_INVERTED;
  590. if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
  591. value |= ATMEL_LCDC_INVFRAME_INVERTED;
  592. switch (info->var.bits_per_pixel) {
  593. case 1: value |= ATMEL_LCDC_PIXELSIZE_1; break;
  594. case 2: value |= ATMEL_LCDC_PIXELSIZE_2; break;
  595. case 4: value |= ATMEL_LCDC_PIXELSIZE_4; break;
  596. case 8: value |= ATMEL_LCDC_PIXELSIZE_8; break;
  597. case 15: /* fall through */
  598. case 16: value |= ATMEL_LCDC_PIXELSIZE_16; break;
  599. case 24: value |= ATMEL_LCDC_PIXELSIZE_24; break;
  600. case 32: value |= ATMEL_LCDC_PIXELSIZE_32; break;
  601. default: BUG(); break;
  602. }
  603. dev_dbg(info->device, " * LCDCON2 = %08lx\n", value);
  604. lcdc_writel(sinfo, ATMEL_LCDC_LCDCON2, value);
  605. /* Vertical timing */
  606. value = (info->var.vsync_len - 1) << ATMEL_LCDC_VPW_OFFSET;
  607. value |= info->var.upper_margin << ATMEL_LCDC_VBP_OFFSET;
  608. value |= info->var.lower_margin;
  609. dev_dbg(info->device, " * LCDTIM1 = %08lx\n", value);
  610. lcdc_writel(sinfo, ATMEL_LCDC_TIM1, value);
  611. /* Horizontal timing */
  612. value = (info->var.right_margin - 1) << ATMEL_LCDC_HFP_OFFSET;
  613. value |= (info->var.hsync_len - 1) << ATMEL_LCDC_HPW_OFFSET;
  614. value |= (info->var.left_margin - 1);
  615. dev_dbg(info->device, " * LCDTIM2 = %08lx\n", value);
  616. lcdc_writel(sinfo, ATMEL_LCDC_TIM2, value);
  617. /* Horizontal value (aka line size) */
  618. hozval_linesz = compute_hozval(sinfo, info->var.xres);
  619. /* Display size */
  620. value = (hozval_linesz - 1) << ATMEL_LCDC_HOZVAL_OFFSET;
  621. value |= info->var.yres - 1;
  622. dev_dbg(info->device, " * LCDFRMCFG = %08lx\n", value);
  623. lcdc_writel(sinfo, ATMEL_LCDC_LCDFRMCFG, value);
  624. /* FIFO Threshold: Use formula from data sheet */
  625. value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3);
  626. lcdc_writel(sinfo, ATMEL_LCDC_FIFO, value);
  627. /* Toggle LCD_MODE every frame */
  628. lcdc_writel(sinfo, ATMEL_LCDC_MVAL, 0);
  629. /* Disable all interrupts */
  630. lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
  631. /* Enable FIFO & DMA errors */
  632. lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
  633. /* ...wait for DMA engine to become idle... */
  634. while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
  635. msleep(10);
  636. atmel_lcdfb_start(sinfo);
  637. dev_dbg(info->device, " * DONE\n");
  638. return 0;
  639. }
  640. static inline unsigned int chan_to_field(unsigned int chan, const struct fb_bitfield *bf)
  641. {
  642. chan &= 0xffff;
  643. chan >>= 16 - bf->length;
  644. return chan << bf->offset;
  645. }
  646. /**
  647. * atmel_lcdfb_setcolreg - Optional function. Sets a color register.
  648. * @regno: Which register in the CLUT we are programming
  649. * @red: The red value which can be up to 16 bits wide
  650. * @green: The green value which can be up to 16 bits wide
  651. * @blue: The blue value which can be up to 16 bits wide.
  652. * @transp: If supported the alpha value which can be up to 16 bits wide.
  653. * @info: frame buffer info structure
  654. *
  655. * Set a single color register. The values supplied have a 16 bit
  656. * magnitude which needs to be scaled in this function for the hardware.
  657. * Things to take into consideration are how many color registers, if
  658. * any, are supported with the current color visual. With truecolor mode
  659. * no color palettes are supported. Here a pseudo palette is created
  660. * which we store the value in pseudo_palette in struct fb_info. For
  661. * pseudocolor mode we have a limited color palette. To deal with this
  662. * we can program what color is displayed for a particular pixel value.
  663. * DirectColor is similar in that we can program each color field. If
  664. * we have a static colormap we don't need to implement this function.
  665. *
  666. * Returns negative errno on error, or zero on success. In an
  667. * ideal world, this would have been the case, but as it turns
  668. * out, the other drivers return 1 on failure, so that's what
  669. * we're going to do.
  670. */
  671. static int atmel_lcdfb_setcolreg(unsigned int regno, unsigned int red,
  672. unsigned int green, unsigned int blue,
  673. unsigned int transp, struct fb_info *info)
  674. {
  675. struct atmel_lcdfb_info *sinfo = info->par;
  676. struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
  677. unsigned int val;
  678. u32 *pal;
  679. int ret = 1;
  680. if (info->var.grayscale)
  681. red = green = blue = (19595 * red + 38470 * green
  682. + 7471 * blue) >> 16;
  683. switch (info->fix.visual) {
  684. case FB_VISUAL_TRUECOLOR:
  685. if (regno < 16) {
  686. pal = info->pseudo_palette;
  687. val = chan_to_field(red, &info->var.red);
  688. val |= chan_to_field(green, &info->var.green);
  689. val |= chan_to_field(blue, &info->var.blue);
  690. pal[regno] = val;
  691. ret = 0;
  692. }
  693. break;
  694. case FB_VISUAL_PSEUDOCOLOR:
  695. if (regno < 256) {
  696. if (sinfo->config->have_intensity_bit) {
  697. /* old style I+BGR:555 */
  698. val = ((red >> 11) & 0x001f);
  699. val |= ((green >> 6) & 0x03e0);
  700. val |= ((blue >> 1) & 0x7c00);
  701. /*
  702. * TODO: intensity bit. Maybe something like
  703. * ~(red[10] ^ green[10] ^ blue[10]) & 1
  704. */
  705. } else {
  706. /* new style BGR:565 / RGB:565 */
  707. if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
  708. val = ((blue >> 11) & 0x001f);
  709. val |= ((red >> 0) & 0xf800);
  710. } else {
  711. val = ((red >> 11) & 0x001f);
  712. val |= ((blue >> 0) & 0xf800);
  713. }
  714. val |= ((green >> 5) & 0x07e0);
  715. }
  716. lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
  717. ret = 0;
  718. }
  719. break;
  720. case FB_VISUAL_MONO01:
  721. if (regno < 2) {
  722. val = (regno == 0) ? 0x00 : 0x1F;
  723. lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
  724. ret = 0;
  725. }
  726. break;
  727. }
  728. return ret;
  729. }
  730. static int atmel_lcdfb_pan_display(struct fb_var_screeninfo *var,
  731. struct fb_info *info)
  732. {
  733. dev_dbg(info->device, "%s\n", __func__);
  734. atmel_lcdfb_update_dma(info, var);
  735. return 0;
  736. }
  737. static int atmel_lcdfb_blank(int blank_mode, struct fb_info *info)
  738. {
  739. struct atmel_lcdfb_info *sinfo = info->par;
  740. switch (blank_mode) {
  741. case FB_BLANK_UNBLANK:
  742. case FB_BLANK_NORMAL:
  743. atmel_lcdfb_start(sinfo);
  744. break;
  745. case FB_BLANK_VSYNC_SUSPEND:
  746. case FB_BLANK_HSYNC_SUSPEND:
  747. break;
  748. case FB_BLANK_POWERDOWN:
  749. atmel_lcdfb_stop(sinfo);
  750. break;
  751. default:
  752. return -EINVAL;
  753. }
  754. /* let fbcon do a soft blank for us */
  755. return ((blank_mode == FB_BLANK_NORMAL) ? 1 : 0);
  756. }
  757. static struct fb_ops atmel_lcdfb_ops = {
  758. .owner = THIS_MODULE,
  759. .fb_check_var = atmel_lcdfb_check_var,
  760. .fb_set_par = atmel_lcdfb_set_par,
  761. .fb_setcolreg = atmel_lcdfb_setcolreg,
  762. .fb_blank = atmel_lcdfb_blank,
  763. .fb_pan_display = atmel_lcdfb_pan_display,
  764. .fb_fillrect = cfb_fillrect,
  765. .fb_copyarea = cfb_copyarea,
  766. .fb_imageblit = cfb_imageblit,
  767. };
  768. static irqreturn_t atmel_lcdfb_interrupt(int irq, void *dev_id)
  769. {
  770. struct fb_info *info = dev_id;
  771. struct atmel_lcdfb_info *sinfo = info->par;
  772. u32 status;
  773. status = lcdc_readl(sinfo, ATMEL_LCDC_ISR);
  774. if (status & ATMEL_LCDC_UFLWI) {
  775. dev_warn(info->device, "FIFO underflow %#x\n", status);
  776. /* reset DMA and FIFO to avoid screen shifting */
  777. schedule_work(&sinfo->task);
  778. }
  779. lcdc_writel(sinfo, ATMEL_LCDC_ICR, status);
  780. return IRQ_HANDLED;
  781. }
  782. /*
  783. * LCD controller task (to reset the LCD)
  784. */
  785. static void atmel_lcdfb_task(struct work_struct *work)
  786. {
  787. struct atmel_lcdfb_info *sinfo =
  788. container_of(work, struct atmel_lcdfb_info, task);
  789. atmel_lcdfb_reset(sinfo);
  790. }
  791. static int __init atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info *sinfo)
  792. {
  793. struct fb_info *info = sinfo->info;
  794. int ret = 0;
  795. info->var.activate |= FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
  796. dev_info(info->device,
  797. "%luKiB frame buffer at %08lx (mapped at %p)\n",
  798. (unsigned long)info->fix.smem_len / 1024,
  799. (unsigned long)info->fix.smem_start,
  800. info->screen_base);
  801. /* Allocate colormap */
  802. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  803. if (ret < 0)
  804. dev_err(info->device, "Alloc color map failed\n");
  805. return ret;
  806. }
  807. static void atmel_lcdfb_start_clock(struct atmel_lcdfb_info *sinfo)
  808. {
  809. clk_prepare_enable(sinfo->bus_clk);
  810. clk_prepare_enable(sinfo->lcdc_clk);
  811. }
  812. static void atmel_lcdfb_stop_clock(struct atmel_lcdfb_info *sinfo)
  813. {
  814. clk_disable_unprepare(sinfo->bus_clk);
  815. clk_disable_unprepare(sinfo->lcdc_clk);
  816. }
  817. #ifdef CONFIG_OF
  818. static const struct of_device_id atmel_lcdfb_dt_ids[] = {
  819. { .compatible = "atmel,at91sam9261-lcdc" , .data = &at91sam9261_config, },
  820. { .compatible = "atmel,at91sam9263-lcdc" , .data = &at91sam9263_config, },
  821. { .compatible = "atmel,at91sam9g10-lcdc" , .data = &at91sam9g10_config, },
  822. { .compatible = "atmel,at91sam9g45-lcdc" , .data = &at91sam9g45_config, },
  823. { .compatible = "atmel,at91sam9g45es-lcdc" , .data = &at91sam9g45es_config, },
  824. { .compatible = "atmel,at91sam9rl-lcdc" , .data = &at91sam9rl_config, },
  825. { .compatible = "atmel,at32ap-lcdc" , .data = &at32ap_config, },
  826. { /* sentinel */ }
  827. };
  828. MODULE_DEVICE_TABLE(of, atmel_lcdfb_dt_ids);
  829. static const char *atmel_lcdfb_wiring_modes[] = {
  830. [ATMEL_LCDC_WIRING_BGR] = "BRG",
  831. [ATMEL_LCDC_WIRING_RGB] = "RGB",
  832. };
  833. const int atmel_lcdfb_get_of_wiring_modes(struct device_node *np)
  834. {
  835. const char *mode;
  836. int err, i;
  837. err = of_property_read_string(np, "atmel,lcd-wiring-mode", &mode);
  838. if (err < 0)
  839. return ATMEL_LCDC_WIRING_BGR;
  840. for (i = 0; i < ARRAY_SIZE(atmel_lcdfb_wiring_modes); i++)
  841. if (!strcasecmp(mode, atmel_lcdfb_wiring_modes[i]))
  842. return i;
  843. return -ENODEV;
  844. }
  845. static void atmel_lcdfb_power_control_gpio(struct atmel_lcdfb_pdata *pdata, int on)
  846. {
  847. struct atmel_lcdfb_power_ctrl_gpio *og;
  848. list_for_each_entry(og, &pdata->pwr_gpios, list)
  849. gpio_set_value(og->gpio, on);
  850. }
  851. static int atmel_lcdfb_of_init(struct atmel_lcdfb_info *sinfo)
  852. {
  853. struct fb_info *info = sinfo->info;
  854. struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
  855. struct fb_var_screeninfo *var = &info->var;
  856. struct device *dev = &sinfo->pdev->dev;
  857. struct device_node *np =dev->of_node;
  858. struct device_node *display_np;
  859. struct device_node *timings_np;
  860. struct display_timings *timings;
  861. enum of_gpio_flags flags;
  862. struct atmel_lcdfb_power_ctrl_gpio *og;
  863. bool is_gpio_power = false;
  864. int ret = -ENOENT;
  865. int i, gpio;
  866. sinfo->config = (struct atmel_lcdfb_config*)
  867. of_match_device(atmel_lcdfb_dt_ids, dev)->data;
  868. display_np = of_parse_phandle(np, "display", 0);
  869. if (!display_np) {
  870. dev_err(dev, "failed to find display phandle\n");
  871. return -ENOENT;
  872. }
  873. ret = of_property_read_u32(display_np, "bits-per-pixel", &var->bits_per_pixel);
  874. if (ret < 0) {
  875. dev_err(dev, "failed to get property bits-per-pixel\n");
  876. goto put_display_node;
  877. }
  878. ret = of_property_read_u32(display_np, "atmel,guard-time", &pdata->guard_time);
  879. if (ret < 0) {
  880. dev_err(dev, "failed to get property atmel,guard-time\n");
  881. goto put_display_node;
  882. }
  883. ret = of_property_read_u32(display_np, "atmel,lcdcon2", &pdata->default_lcdcon2);
  884. if (ret < 0) {
  885. dev_err(dev, "failed to get property atmel,lcdcon2\n");
  886. goto put_display_node;
  887. }
  888. ret = of_property_read_u32(display_np, "atmel,dmacon", &pdata->default_dmacon);
  889. if (ret < 0) {
  890. dev_err(dev, "failed to get property bits-per-pixel\n");
  891. goto put_display_node;
  892. }
  893. INIT_LIST_HEAD(&pdata->pwr_gpios);
  894. ret = -ENOMEM;
  895. for (i = 0; i < of_gpio_named_count(display_np, "atmel,power-control-gpio"); i++) {
  896. gpio = of_get_named_gpio_flags(display_np, "atmel,power-control-gpio",
  897. i, &flags);
  898. if (gpio < 0)
  899. continue;
  900. og = devm_kzalloc(dev, sizeof(*og), GFP_KERNEL);
  901. if (!og)
  902. goto put_display_node;
  903. og->gpio = gpio;
  904. og->active_low = flags & OF_GPIO_ACTIVE_LOW;
  905. is_gpio_power = true;
  906. ret = devm_gpio_request(dev, gpio, "lcd-power-control-gpio");
  907. if (ret) {
  908. dev_err(dev, "request gpio %d failed\n", gpio);
  909. goto put_display_node;
  910. }
  911. ret = gpio_direction_output(gpio, og->active_low);
  912. if (ret) {
  913. dev_err(dev, "set direction output gpio %d failed\n", gpio);
  914. goto put_display_node;
  915. }
  916. list_add(&og->list, &pdata->pwr_gpios);
  917. }
  918. if (is_gpio_power)
  919. pdata->atmel_lcdfb_power_control = atmel_lcdfb_power_control_gpio;
  920. ret = atmel_lcdfb_get_of_wiring_modes(display_np);
  921. if (ret < 0) {
  922. dev_err(dev, "invalid atmel,lcd-wiring-mode\n");
  923. goto put_display_node;
  924. }
  925. pdata->lcd_wiring_mode = ret;
  926. pdata->lcdcon_is_backlight = of_property_read_bool(display_np, "atmel,lcdcon-backlight");
  927. pdata->lcdcon_pol_negative = of_property_read_bool(display_np, "atmel,lcdcon-backlight-inverted");
  928. timings = of_get_display_timings(display_np);
  929. if (!timings) {
  930. dev_err(dev, "failed to get display timings\n");
  931. ret = -EINVAL;
  932. goto put_display_node;
  933. }
  934. timings_np = of_find_node_by_name(display_np, "display-timings");
  935. if (!timings_np) {
  936. dev_err(dev, "failed to find display-timings node\n");
  937. ret = -ENODEV;
  938. goto put_display_node;
  939. }
  940. for (i = 0; i < of_get_child_count(timings_np); i++) {
  941. struct videomode vm;
  942. struct fb_videomode fb_vm;
  943. ret = videomode_from_timings(timings, &vm, i);
  944. if (ret < 0)
  945. goto put_timings_node;
  946. ret = fb_videomode_from_videomode(&vm, &fb_vm);
  947. if (ret < 0)
  948. goto put_timings_node;
  949. fb_add_videomode(&fb_vm, &info->modelist);
  950. }
  951. return 0;
  952. put_timings_node:
  953. of_node_put(timings_np);
  954. put_display_node:
  955. of_node_put(display_np);
  956. return ret;
  957. }
  958. #else
  959. static int atmel_lcdfb_of_init(struct atmel_lcdfb_info *sinfo)
  960. {
  961. return 0;
  962. }
  963. #endif
  964. static int __init atmel_lcdfb_probe(struct platform_device *pdev)
  965. {
  966. struct device *dev = &pdev->dev;
  967. struct fb_info *info;
  968. struct atmel_lcdfb_info *sinfo;
  969. struct atmel_lcdfb_pdata *pdata = NULL;
  970. struct resource *regs = NULL;
  971. struct resource *map = NULL;
  972. struct fb_modelist *modelist;
  973. int ret;
  974. dev_dbg(dev, "%s BEGIN\n", __func__);
  975. ret = -ENOMEM;
  976. info = framebuffer_alloc(sizeof(struct atmel_lcdfb_info), dev);
  977. if (!info) {
  978. dev_err(dev, "cannot allocate memory\n");
  979. goto out;
  980. }
  981. sinfo = info->par;
  982. sinfo->pdev = pdev;
  983. sinfo->info = info;
  984. INIT_LIST_HEAD(&info->modelist);
  985. if (pdev->dev.of_node) {
  986. ret = atmel_lcdfb_of_init(sinfo);
  987. if (ret)
  988. goto free_info;
  989. } else if (dev_get_platdata(dev)) {
  990. struct fb_monspecs *monspecs;
  991. int i;
  992. pdata = dev_get_platdata(dev);
  993. monspecs = pdata->default_monspecs;
  994. sinfo->pdata = *pdata;
  995. for (i = 0; i < monspecs->modedb_len; i++)
  996. fb_add_videomode(&monspecs->modedb[i], &info->modelist);
  997. sinfo->config = atmel_lcdfb_get_config(pdev);
  998. info->var.bits_per_pixel = pdata->default_bpp ? pdata->default_bpp : 16;
  999. memcpy(&info->monspecs, pdata->default_monspecs, sizeof(info->monspecs));
  1000. } else {
  1001. dev_err(dev, "cannot get default configuration\n");
  1002. goto free_info;
  1003. }
  1004. if (!sinfo->config)
  1005. goto free_info;
  1006. sinfo->reg_lcd = devm_regulator_get(&pdev->dev, "lcd");
  1007. if (IS_ERR(sinfo->reg_lcd))
  1008. sinfo->reg_lcd = NULL;
  1009. info->flags = ATMEL_LCDFB_FBINFO_DEFAULT;
  1010. info->pseudo_palette = sinfo->pseudo_palette;
  1011. info->fbops = &atmel_lcdfb_ops;
  1012. info->fix = atmel_lcdfb_fix;
  1013. strcpy(info->fix.id, sinfo->pdev->name);
  1014. /* Enable LCDC Clocks */
  1015. sinfo->bus_clk = clk_get(dev, "hclk");
  1016. if (IS_ERR(sinfo->bus_clk)) {
  1017. ret = PTR_ERR(sinfo->bus_clk);
  1018. goto free_info;
  1019. }
  1020. sinfo->lcdc_clk = clk_get(dev, "lcdc_clk");
  1021. if (IS_ERR(sinfo->lcdc_clk)) {
  1022. ret = PTR_ERR(sinfo->lcdc_clk);
  1023. goto put_bus_clk;
  1024. }
  1025. atmel_lcdfb_start_clock(sinfo);
  1026. modelist = list_first_entry(&info->modelist,
  1027. struct fb_modelist, list);
  1028. fb_videomode_to_var(&info->var, &modelist->mode);
  1029. atmel_lcdfb_check_var(&info->var, info);
  1030. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1031. if (!regs) {
  1032. dev_err(dev, "resources unusable\n");
  1033. ret = -ENXIO;
  1034. goto stop_clk;
  1035. }
  1036. sinfo->irq_base = platform_get_irq(pdev, 0);
  1037. if (sinfo->irq_base < 0) {
  1038. dev_err(dev, "unable to get irq\n");
  1039. ret = sinfo->irq_base;
  1040. goto stop_clk;
  1041. }
  1042. /* Initialize video memory */
  1043. map = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1044. if (map) {
  1045. /* use a pre-allocated memory buffer */
  1046. info->fix.smem_start = map->start;
  1047. info->fix.smem_len = resource_size(map);
  1048. if (!request_mem_region(info->fix.smem_start,
  1049. info->fix.smem_len, pdev->name)) {
  1050. ret = -EBUSY;
  1051. goto stop_clk;
  1052. }
  1053. info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
  1054. if (!info->screen_base) {
  1055. ret = -ENOMEM;
  1056. goto release_intmem;
  1057. }
  1058. /*
  1059. * Don't clear the framebuffer -- someone may have set
  1060. * up a splash image.
  1061. */
  1062. } else {
  1063. /* allocate memory buffer */
  1064. ret = atmel_lcdfb_alloc_video_memory(sinfo);
  1065. if (ret < 0) {
  1066. dev_err(dev, "cannot allocate framebuffer: %d\n", ret);
  1067. goto stop_clk;
  1068. }
  1069. }
  1070. /* LCDC registers */
  1071. info->fix.mmio_start = regs->start;
  1072. info->fix.mmio_len = resource_size(regs);
  1073. if (!request_mem_region(info->fix.mmio_start,
  1074. info->fix.mmio_len, pdev->name)) {
  1075. ret = -EBUSY;
  1076. goto free_fb;
  1077. }
  1078. sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);
  1079. if (!sinfo->mmio) {
  1080. dev_err(dev, "cannot map LCDC registers\n");
  1081. ret = -ENOMEM;
  1082. goto release_mem;
  1083. }
  1084. /* Initialize PWM for contrast or backlight ("off") */
  1085. init_contrast(sinfo);
  1086. /* interrupt */
  1087. ret = request_irq(sinfo->irq_base, atmel_lcdfb_interrupt, 0, pdev->name, info);
  1088. if (ret) {
  1089. dev_err(dev, "request_irq failed: %d\n", ret);
  1090. goto unmap_mmio;
  1091. }
  1092. /* Some operations on the LCDC might sleep and
  1093. * require a preemptible task context */
  1094. INIT_WORK(&sinfo->task, atmel_lcdfb_task);
  1095. ret = atmel_lcdfb_init_fbinfo(sinfo);
  1096. if (ret < 0) {
  1097. dev_err(dev, "init fbinfo failed: %d\n", ret);
  1098. goto unregister_irqs;
  1099. }
  1100. ret = atmel_lcdfb_set_par(info);
  1101. if (ret < 0) {
  1102. dev_err(dev, "set par failed: %d\n", ret);
  1103. goto unregister_irqs;
  1104. }
  1105. dev_set_drvdata(dev, info);
  1106. /*
  1107. * Tell the world that we're ready to go
  1108. */
  1109. ret = register_framebuffer(info);
  1110. if (ret < 0) {
  1111. dev_err(dev, "failed to register framebuffer device: %d\n", ret);
  1112. goto reset_drvdata;
  1113. }
  1114. /* Power up the LCDC screen */
  1115. atmel_lcdfb_power_control(sinfo, 1);
  1116. dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %d\n",
  1117. info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base);
  1118. return 0;
  1119. reset_drvdata:
  1120. dev_set_drvdata(dev, NULL);
  1121. fb_dealloc_cmap(&info->cmap);
  1122. unregister_irqs:
  1123. cancel_work_sync(&sinfo->task);
  1124. free_irq(sinfo->irq_base, info);
  1125. unmap_mmio:
  1126. exit_backlight(sinfo);
  1127. iounmap(sinfo->mmio);
  1128. release_mem:
  1129. release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
  1130. free_fb:
  1131. if (map)
  1132. iounmap(info->screen_base);
  1133. else
  1134. atmel_lcdfb_free_video_memory(sinfo);
  1135. release_intmem:
  1136. if (map)
  1137. release_mem_region(info->fix.smem_start, info->fix.smem_len);
  1138. stop_clk:
  1139. atmel_lcdfb_stop_clock(sinfo);
  1140. clk_put(sinfo->lcdc_clk);
  1141. put_bus_clk:
  1142. clk_put(sinfo->bus_clk);
  1143. free_info:
  1144. framebuffer_release(info);
  1145. out:
  1146. dev_dbg(dev, "%s FAILED\n", __func__);
  1147. return ret;
  1148. }
  1149. static int __exit atmel_lcdfb_remove(struct platform_device *pdev)
  1150. {
  1151. struct device *dev = &pdev->dev;
  1152. struct fb_info *info = dev_get_drvdata(dev);
  1153. struct atmel_lcdfb_info *sinfo;
  1154. struct atmel_lcdfb_pdata *pdata;
  1155. if (!info || !info->par)
  1156. return 0;
  1157. sinfo = info->par;
  1158. pdata = &sinfo->pdata;
  1159. cancel_work_sync(&sinfo->task);
  1160. exit_backlight(sinfo);
  1161. atmel_lcdfb_power_control(sinfo, 0);
  1162. unregister_framebuffer(info);
  1163. atmel_lcdfb_stop_clock(sinfo);
  1164. clk_put(sinfo->lcdc_clk);
  1165. clk_put(sinfo->bus_clk);
  1166. fb_dealloc_cmap(&info->cmap);
  1167. free_irq(sinfo->irq_base, info);
  1168. iounmap(sinfo->mmio);
  1169. release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
  1170. if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) {
  1171. iounmap(info->screen_base);
  1172. release_mem_region(info->fix.smem_start, info->fix.smem_len);
  1173. } else {
  1174. atmel_lcdfb_free_video_memory(sinfo);
  1175. }
  1176. framebuffer_release(info);
  1177. return 0;
  1178. }
  1179. #ifdef CONFIG_PM
  1180. static int atmel_lcdfb_suspend(struct platform_device *pdev, pm_message_t mesg)
  1181. {
  1182. struct fb_info *info = platform_get_drvdata(pdev);
  1183. struct atmel_lcdfb_info *sinfo = info->par;
  1184. /*
  1185. * We don't want to handle interrupts while the clock is
  1186. * stopped. It may take forever.
  1187. */
  1188. lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
  1189. sinfo->saved_lcdcon = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_CTR);
  1190. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, 0);
  1191. atmel_lcdfb_power_control(sinfo, 0);
  1192. atmel_lcdfb_stop(sinfo);
  1193. atmel_lcdfb_stop_clock(sinfo);
  1194. return 0;
  1195. }
  1196. static int atmel_lcdfb_resume(struct platform_device *pdev)
  1197. {
  1198. struct fb_info *info = platform_get_drvdata(pdev);
  1199. struct atmel_lcdfb_info *sinfo = info->par;
  1200. atmel_lcdfb_start_clock(sinfo);
  1201. atmel_lcdfb_start(sinfo);
  1202. atmel_lcdfb_power_control(sinfo, 1);
  1203. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, sinfo->saved_lcdcon);
  1204. /* Enable FIFO & DMA errors */
  1205. lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI
  1206. | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
  1207. return 0;
  1208. }
  1209. #else
  1210. #define atmel_lcdfb_suspend NULL
  1211. #define atmel_lcdfb_resume NULL
  1212. #endif
  1213. static struct platform_driver atmel_lcdfb_driver = {
  1214. .remove = __exit_p(atmel_lcdfb_remove),
  1215. .suspend = atmel_lcdfb_suspend,
  1216. .resume = atmel_lcdfb_resume,
  1217. .id_table = atmel_lcdfb_devtypes,
  1218. .driver = {
  1219. .name = "atmel_lcdfb",
  1220. .owner = THIS_MODULE,
  1221. .of_match_table = of_match_ptr(atmel_lcdfb_dt_ids),
  1222. },
  1223. };
  1224. module_platform_driver_probe(atmel_lcdfb_driver, atmel_lcdfb_probe);
  1225. MODULE_DESCRIPTION("AT91/AT32 LCD Controller framebuffer driver");
  1226. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  1227. MODULE_LICENSE("GPL");