atmel_lcdfb.c 36 KB

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