atmel_lcdfb.c 39 KB

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