atmel_lcdfb.c 40 KB

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