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