blob: 5b07053bbd013caf6991ec40d83192e3fc79bd0a [file] [log] [blame]
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001/*
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07002 * Freescale i.MX Frame Buffer device driver
3 *
4 * Copyright (C) 2004 Sascha Hauer, Pengutronix
5 * Based on acornfb.c Copyright (C) Russell King.
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file COPYING in the main directory of this archive for
9 * more details.
10 *
11 * Please direct your questions and comments on this driver to the following
12 * email address:
13 *
14 * linux-arm-kernel@lists.arm.linux.org.uk
15 */
16
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070017#include <linux/module.h>
18#include <linux/kernel.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070019#include <linux/errno.h>
20#include <linux/string.h>
21#include <linux/interrupt.h>
22#include <linux/slab.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070023#include <linux/mm.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070024#include <linux/fb.h>
25#include <linux/delay.h>
26#include <linux/init.h>
27#include <linux/ioport.h>
28#include <linux/cpufreq.h>
Sascha Hauerf909ef62009-01-15 15:21:00 +010029#include <linux/clk.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010030#include <linux/platform_device.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070031#include <linux/dma-mapping.h>
Juergen Beisert72330b02008-12-16 11:44:07 +010032#include <linux/io.h>
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +040033#include <linux/lcd.h>
Sascha Hauerf909ef62009-01-15 15:21:00 +010034#include <linux/math64.h>
Markus Pargmann1b6c7932013-05-26 12:35:38 +020035#include <linux/of.h>
36#include <linux/of_device.h>
37
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +040038#include <linux/regulator/consumer.h>
39
Markus Pargmann1b6c7932013-05-26 12:35:38 +020040#include <video/of_display_timing.h>
41#include <video/of_videomode.h>
42#include <video/videomode.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070043
Arnd Bergmann82906b12012-08-24 15:14:29 +020044#include <linux/platform_data/video-imxfb.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070045
46/*
47 * Complain if VAR is out of range.
48 */
49#define DEBUG_VAR 1
50
Eric Bénard81ef8062010-08-02 08:39:55 +010051#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || \
52 (defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) && \
53 defined(CONFIG_FB_IMX_MODULE))
54#define PWMR_BACKLIGHT_AVAILABLE
55#endif
56
Juergen Beisert72330b02008-12-16 11:44:07 +010057#define DRIVER_NAME "imx-fb"
58
59#define LCDC_SSA 0x00
60
61#define LCDC_SIZE 0x04
62#define SIZE_XMAX(x) ((((x) >> 4) & 0x3f) << 20)
Sascha Hauer1d0f9872009-01-26 17:29:10 +010063
Shawn Guoe69dc9a2012-09-16 19:59:53 +080064#define YMAX_MASK_IMX1 0x1ff
65#define YMAX_MASK_IMX21 0x3ff
Juergen Beisert72330b02008-12-16 11:44:07 +010066
67#define LCDC_VPW 0x08
68#define VPW_VPW(x) ((x) & 0x3ff)
69
70#define LCDC_CPOS 0x0C
71#define CPOS_CC1 (1<<31)
72#define CPOS_CC0 (1<<30)
73#define CPOS_OP (1<<28)
74#define CPOS_CXP(x) (((x) & 3ff) << 16)
Sascha Hauer1d0f9872009-01-26 17:29:10 +010075
Juergen Beisert72330b02008-12-16 11:44:07 +010076#define LCDC_LCWHB 0x10
77#define LCWHB_BK_EN (1<<31)
78#define LCWHB_CW(w) (((w) & 0x1f) << 24)
79#define LCWHB_CH(h) (((h) & 0x1f) << 16)
80#define LCWHB_BD(x) ((x) & 0xff)
81
82#define LCDC_LCHCC 0x14
Sascha Hauer1d0f9872009-01-26 17:29:10 +010083
Juergen Beisert72330b02008-12-16 11:44:07 +010084#define LCDC_PCR 0x18
85
86#define LCDC_HCR 0x1C
87#define HCR_H_WIDTH(x) (((x) & 0x3f) << 26)
88#define HCR_H_WAIT_1(x) (((x) & 0xff) << 8)
89#define HCR_H_WAIT_2(x) ((x) & 0xff)
90
91#define LCDC_VCR 0x20
92#define VCR_V_WIDTH(x) (((x) & 0x3f) << 26)
93#define VCR_V_WAIT_1(x) (((x) & 0xff) << 8)
94#define VCR_V_WAIT_2(x) ((x) & 0xff)
95
96#define LCDC_POS 0x24
97#define POS_POS(x) ((x) & 1f)
98
99#define LCDC_LSCR1 0x28
100/* bit fields in imxfb.h */
101
102#define LCDC_PWMR 0x2C
103/* bit fields in imxfb.h */
104
105#define LCDC_DMACR 0x30
106/* bit fields in imxfb.h */
107
108#define LCDC_RMCR 0x34
Sascha Hauer1d0f9872009-01-26 17:29:10 +0100109
Sascha Hauerf142b612011-03-03 15:12:44 +0100110#define RMCR_LCDC_EN_MX1 (1<<1)
Sascha Hauer1d0f9872009-01-26 17:29:10 +0100111
Juergen Beisert72330b02008-12-16 11:44:07 +0100112#define RMCR_SELF_REF (1<<0)
113
114#define LCDC_LCDICR 0x38
115#define LCDICR_INT_SYN (1<<2)
116#define LCDICR_INT_CON (1)
117
118#define LCDC_LCDISR 0x40
119#define LCDISR_UDR_ERR (1<<3)
120#define LCDISR_ERR_RES (1<<2)
121#define LCDISR_EOF (1<<1)
122#define LCDISR_BOF (1<<0)
123
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200124#define IMXFB_LSCR1_DEFAULT 0x00120300
125
Sascha Hauer343684f2009-03-19 08:25:41 +0100126/* Used fb-mode. Can be set on kernel command line, therefore file-static. */
127static const char *fb_mode;
128
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100129/*
130 * These are the bitfields for each
131 * display depth that we support.
132 */
133struct imxfb_rgb {
134 struct fb_bitfield red;
135 struct fb_bitfield green;
136 struct fb_bitfield blue;
137 struct fb_bitfield transp;
138};
139
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800140enum imxfb_type {
141 IMX1_FB,
142 IMX21_FB,
143};
144
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100145struct imxfb_info {
146 struct platform_device *pdev;
147 void __iomem *regs;
Sascha Hauer13aaea02012-03-07 09:30:36 +0100148 struct clk *clk_ipg;
149 struct clk *clk_ahb;
150 struct clk *clk_per;
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800151 enum imxfb_type devtype;
Fabio Estevame6b267c2013-01-16 10:58:41 -0200152 bool enabled;
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100153
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100154 /*
155 * These are the addresses we mapped
156 * the framebuffer memory region to.
157 */
158 dma_addr_t map_dma;
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100159 u_int map_size;
160
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100161 u_int palette_size;
162
163 dma_addr_t dbar1;
164 dma_addr_t dbar2;
165
166 u_int pcr;
167 u_int pwmr;
168 u_int lscr1;
169 u_int dmacr;
Alexander Shiyanb7d2d372014-02-15 09:56:23 +0400170 bool cmap_inverse;
171 bool cmap_static;
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100172
Sascha Hauer343684f2009-03-19 08:25:41 +0100173 struct imx_fb_videomode *mode;
174 int num_modes;
Eric Bénard81ef8062010-08-02 08:39:55 +0100175#ifdef PWMR_BACKLIGHT_AVAILABLE
Eric Bénard7a2bb232010-07-16 15:09:07 +0200176 struct backlight_device *bl;
Eric Bénard81ef8062010-08-02 08:39:55 +0100177#endif
Sascha Hauer343684f2009-03-19 08:25:41 +0100178
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400179 struct regulator *lcd_pwr;
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100180};
181
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800182static struct platform_device_id imxfb_devtype[] = {
183 {
184 .name = "imx1-fb",
185 .driver_data = IMX1_FB,
186 }, {
187 .name = "imx21-fb",
188 .driver_data = IMX21_FB,
189 }, {
190 /* sentinel */
191 }
192};
193MODULE_DEVICE_TABLE(platform, imxfb_devtype);
194
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200195static struct of_device_id imxfb_of_dev_id[] = {
196 {
197 .compatible = "fsl,imx1-fb",
198 .data = &imxfb_devtype[IMX1_FB],
199 }, {
200 .compatible = "fsl,imx21-fb",
201 .data = &imxfb_devtype[IMX21_FB],
202 }, {
203 /* sentinel */
204 }
205};
206MODULE_DEVICE_TABLE(of, imxfb_of_dev_id);
207
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800208static inline int is_imx1_fb(struct imxfb_info *fbi)
209{
210 return fbi->devtype == IMX1_FB;
211}
212
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100213#define IMX_NAME "IMX"
214
215/*
216 * Minimum X and Y resolutions
217 */
218#define MIN_XRES 64
219#define MIN_YRES 64
220
Sascha Hauer15122222009-01-26 17:31:02 +0100221/* Actually this really is 18bit support, the lowest 2 bits of each colour
222 * are unused in hardware. We claim to have 24bit support to make software
223 * like X work, which does not support 18bit.
224 */
225static struct imxfb_rgb def_rgb_18 = {
226 .red = {.offset = 16, .length = 8,},
227 .green = {.offset = 8, .length = 8,},
228 .blue = {.offset = 0, .length = 8,},
229 .transp = {.offset = 0, .length = 0,},
230};
231
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100232static struct imxfb_rgb def_rgb_16_tft = {
233 .red = {.offset = 11, .length = 5,},
234 .green = {.offset = 5, .length = 6,},
235 .blue = {.offset = 0, .length = 5,},
236 .transp = {.offset = 0, .length = 0,},
237};
238
239static struct imxfb_rgb def_rgb_16_stn = {
Sascha Hauer66c87192008-12-16 11:44:08 +0100240 .red = {.offset = 8, .length = 4,},
241 .green = {.offset = 4, .length = 4,},
242 .blue = {.offset = 0, .length = 4,},
243 .transp = {.offset = 0, .length = 0,},
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700244};
245
246static struct imxfb_rgb def_rgb_8 = {
Sascha Hauer66c87192008-12-16 11:44:08 +0100247 .red = {.offset = 0, .length = 8,},
248 .green = {.offset = 0, .length = 8,},
249 .blue = {.offset = 0, .length = 8,},
250 .transp = {.offset = 0, .length = 0,},
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700251};
252
Sascha Hauer66c87192008-12-16 11:44:08 +0100253static int imxfb_activate_var(struct fb_var_screeninfo *var,
254 struct fb_info *info);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700255
256static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
257{
258 chan &= 0xffff;
259 chan >>= 16 - bf->length;
260 return chan << bf->offset;
261}
262
Sascha Hauer66c87192008-12-16 11:44:08 +0100263static int imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
264 u_int trans, struct fb_info *info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700265{
266 struct imxfb_info *fbi = info->par;
267 u_int val, ret = 1;
268
269#define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
270 if (regno < fbi->palette_size) {
271 val = (CNVT_TOHW(red, 4) << 8) |
272 (CNVT_TOHW(green,4) << 4) |
273 CNVT_TOHW(blue, 4);
274
Juergen Beisert72330b02008-12-16 11:44:07 +0100275 writel(val, fbi->regs + 0x800 + (regno << 2));
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700276 ret = 0;
277 }
278 return ret;
279}
280
Sascha Hauer66c87192008-12-16 11:44:08 +0100281static int imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700282 u_int trans, struct fb_info *info)
283{
284 struct imxfb_info *fbi = info->par;
285 unsigned int val;
286 int ret = 1;
287
288 /*
289 * If inverse mode was selected, invert all the colours
290 * rather than the register number. The register number
291 * is what you poke into the framebuffer to produce the
292 * colour you requested.
293 */
294 if (fbi->cmap_inverse) {
295 red = 0xffff - red;
296 green = 0xffff - green;
297 blue = 0xffff - blue;
298 }
299
300 /*
301 * If greyscale is true, then we convert the RGB value
302 * to greyscale no mater what visual we are using.
303 */
304 if (info->var.grayscale)
305 red = green = blue = (19595 * red + 38470 * green +
306 7471 * blue) >> 16;
307
308 switch (info->fix.visual) {
309 case FB_VISUAL_TRUECOLOR:
310 /*
311 * 12 or 16-bit True Colour. We encode the RGB value
312 * according to the RGB bitfield information.
313 */
314 if (regno < 16) {
315 u32 *pal = info->pseudo_palette;
316
317 val = chan_to_field(red, &info->var.red);
318 val |= chan_to_field(green, &info->var.green);
319 val |= chan_to_field(blue, &info->var.blue);
320
321 pal[regno] = val;
322 ret = 0;
323 }
324 break;
325
326 case FB_VISUAL_STATIC_PSEUDOCOLOR:
327 case FB_VISUAL_PSEUDOCOLOR:
328 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
329 break;
330 }
331
332 return ret;
333}
334
Sascha Hauer343684f2009-03-19 08:25:41 +0100335static const struct imx_fb_videomode *imxfb_find_mode(struct imxfb_info *fbi)
336{
337 struct imx_fb_videomode *m;
338 int i;
339
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200340 if (!fb_mode)
341 return &fbi->mode[0];
342
Sascha Hauer343684f2009-03-19 08:25:41 +0100343 for (i = 0, m = &fbi->mode[0]; i < fbi->num_modes; i++, m++) {
344 if (!strcmp(m->mode.name, fb_mode))
345 return m;
346 }
347 return NULL;
348}
349
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700350/*
351 * imxfb_check_var():
352 * Round up in the following order: bits_per_pixel, xres,
353 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
354 * bitfields, horizontal timing, vertical timing.
355 */
Sascha Hauer66c87192008-12-16 11:44:08 +0100356static int imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700357{
358 struct imxfb_info *fbi = info->par;
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100359 struct imxfb_rgb *rgb;
Sascha Hauer343684f2009-03-19 08:25:41 +0100360 const struct imx_fb_videomode *imxfb_mode;
361 unsigned long lcd_clk;
362 unsigned long long tmp;
363 u32 pcr = 0;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700364
365 if (var->xres < MIN_XRES)
366 var->xres = MIN_XRES;
367 if (var->yres < MIN_YRES)
368 var->yres = MIN_YRES;
Sascha Hauer343684f2009-03-19 08:25:41 +0100369
370 imxfb_mode = imxfb_find_mode(fbi);
371 if (!imxfb_mode)
372 return -EINVAL;
373
374 var->xres = imxfb_mode->mode.xres;
375 var->yres = imxfb_mode->mode.yres;
376 var->bits_per_pixel = imxfb_mode->bpp;
377 var->pixclock = imxfb_mode->mode.pixclock;
378 var->hsync_len = imxfb_mode->mode.hsync_len;
379 var->left_margin = imxfb_mode->mode.left_margin;
380 var->right_margin = imxfb_mode->mode.right_margin;
381 var->vsync_len = imxfb_mode->mode.vsync_len;
382 var->upper_margin = imxfb_mode->mode.upper_margin;
383 var->lower_margin = imxfb_mode->mode.lower_margin;
384 var->sync = imxfb_mode->mode.sync;
385 var->xres_virtual = max(var->xres_virtual, var->xres);
386 var->yres_virtual = max(var->yres_virtual, var->yres);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700387
388 pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
Sascha Hauer343684f2009-03-19 08:25:41 +0100389
Sascha Hauer13aaea02012-03-07 09:30:36 +0100390 lcd_clk = clk_get_rate(fbi->clk_per);
Sascha Hauer343684f2009-03-19 08:25:41 +0100391
392 tmp = var->pixclock * (unsigned long long)lcd_clk;
393
394 do_div(tmp, 1000000);
395
396 if (do_div(tmp, 1000000) > 500000)
397 tmp++;
398
399 pcr = (unsigned int)tmp;
400
401 if (--pcr > 0x3F) {
402 pcr = 0x3F;
403 printk(KERN_WARNING "Must limit pixel clock to %luHz\n",
404 lcd_clk / pcr);
405 }
406
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700407 switch (var->bits_per_pixel) {
Sascha Hauer15122222009-01-26 17:31:02 +0100408 case 32:
Sascha Hauer343684f2009-03-19 08:25:41 +0100409 pcr |= PCR_BPIX_18;
Sascha Hauer15122222009-01-26 17:31:02 +0100410 rgb = &def_rgb_18;
411 break;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700412 case 16:
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100413 default:
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800414 if (is_imx1_fb(fbi))
Sascha Hauer343684f2009-03-19 08:25:41 +0100415 pcr |= PCR_BPIX_12;
416 else
417 pcr |= PCR_BPIX_16;
418
419 if (imxfb_mode->pcr & PCR_TFT)
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100420 rgb = &def_rgb_16_tft;
421 else
422 rgb = &def_rgb_16_stn;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700423 break;
424 case 8:
Sascha Hauer343684f2009-03-19 08:25:41 +0100425 pcr |= PCR_BPIX_8;
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100426 rgb = &def_rgb_8;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700427 break;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700428 }
429
Sascha Hauer343684f2009-03-19 08:25:41 +0100430 /* add sync polarities */
431 pcr |= imxfb_mode->pcr & ~(0x3f | (7 << 25));
432
433 fbi->pcr = pcr;
434
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700435 /*
436 * Copy the RGB parameters for this display
437 * from the machine specific parameters.
438 */
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100439 var->red = rgb->red;
440 var->green = rgb->green;
441 var->blue = rgb->blue;
442 var->transp = rgb->transp;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700443
444 pr_debug("RGBT length = %d:%d:%d:%d\n",
445 var->red.length, var->green.length, var->blue.length,
446 var->transp.length);
447
448 pr_debug("RGBT offset = %d:%d:%d:%d\n",
449 var->red.offset, var->green.offset, var->blue.offset,
450 var->transp.offset);
451
452 return 0;
453}
454
455/*
456 * imxfb_set_par():
457 * Set the user defined part of the display for the specified console
458 */
459static int imxfb_set_par(struct fb_info *info)
460{
461 struct imxfb_info *fbi = info->par;
462 struct fb_var_screeninfo *var = &info->var;
463
Sascha Hauer15122222009-01-26 17:31:02 +0100464 if (var->bits_per_pixel == 16 || var->bits_per_pixel == 32)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700465 info->fix.visual = FB_VISUAL_TRUECOLOR;
466 else if (!fbi->cmap_static)
467 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
468 else {
469 /*
470 * Some people have weird ideas about wanting static
471 * pseudocolor maps. I suspect their user space
472 * applications are broken.
473 */
474 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
475 }
476
Sascha Hauer66c87192008-12-16 11:44:08 +0100477 info->fix.line_length = var->xres_virtual * var->bits_per_pixel / 8;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700478 fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
479
480 imxfb_activate_var(var, info);
481
482 return 0;
483}
484
Eric Bénard81ef8062010-08-02 08:39:55 +0100485#ifdef PWMR_BACKLIGHT_AVAILABLE
Eric Bénard7a2bb232010-07-16 15:09:07 +0200486static int imxfb_bl_get_brightness(struct backlight_device *bl)
487{
488 struct imxfb_info *fbi = bl_get_data(bl);
489
490 return readl(fbi->regs + LCDC_PWMR) & 0xFF;
491}
492
493static int imxfb_bl_update_status(struct backlight_device *bl)
494{
495 struct imxfb_info *fbi = bl_get_data(bl);
496 int brightness = bl->props.brightness;
497
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200498 if (!fbi->pwmr)
499 return 0;
500
Eric Bénard7a2bb232010-07-16 15:09:07 +0200501 if (bl->props.power != FB_BLANK_UNBLANK)
502 brightness = 0;
503 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
504 brightness = 0;
505
506 fbi->pwmr = (fbi->pwmr & ~0xFF) | brightness;
507
Sascha Hauer13aaea02012-03-07 09:30:36 +0100508 if (bl->props.fb_blank != FB_BLANK_UNBLANK) {
509 clk_prepare_enable(fbi->clk_ipg);
510 clk_prepare_enable(fbi->clk_ahb);
511 clk_prepare_enable(fbi->clk_per);
512 }
Eric Bénard7a2bb232010-07-16 15:09:07 +0200513 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
Sascha Hauer13aaea02012-03-07 09:30:36 +0100514 if (bl->props.fb_blank != FB_BLANK_UNBLANK) {
515 clk_disable_unprepare(fbi->clk_per);
516 clk_disable_unprepare(fbi->clk_ahb);
517 clk_disable_unprepare(fbi->clk_ipg);
518 }
Eric Bénard7a2bb232010-07-16 15:09:07 +0200519
520 return 0;
521}
522
523static const struct backlight_ops imxfb_lcdc_bl_ops = {
524 .update_status = imxfb_bl_update_status,
525 .get_brightness = imxfb_bl_get_brightness,
526};
527
528static void imxfb_init_backlight(struct imxfb_info *fbi)
529{
530 struct backlight_properties props;
531 struct backlight_device *bl;
532
533 if (fbi->bl)
534 return;
535
536 memset(&props, 0, sizeof(struct backlight_properties));
537 props.max_brightness = 0xff;
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700538 props.type = BACKLIGHT_RAW;
Eric Bénard7a2bb232010-07-16 15:09:07 +0200539 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
540
541 bl = backlight_device_register("imxfb-bl", &fbi->pdev->dev, fbi,
542 &imxfb_lcdc_bl_ops, &props);
543 if (IS_ERR(bl)) {
544 dev_err(&fbi->pdev->dev, "error %ld on backlight register\n",
545 PTR_ERR(bl));
546 return;
547 }
548
549 fbi->bl = bl;
550 bl->props.power = FB_BLANK_UNBLANK;
551 bl->props.fb_blank = FB_BLANK_UNBLANK;
552 bl->props.brightness = imxfb_bl_get_brightness(bl);
553}
554
555static void imxfb_exit_backlight(struct imxfb_info *fbi)
556{
557 if (fbi->bl)
558 backlight_device_unregister(fbi->bl);
559}
Eric Bénard81ef8062010-08-02 08:39:55 +0100560#endif
Eric Bénard7a2bb232010-07-16 15:09:07 +0200561
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700562static void imxfb_enable_controller(struct imxfb_info *fbi)
563{
Fabio Estevame6b267c2013-01-16 10:58:41 -0200564
565 if (fbi->enabled)
566 return;
567
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700568 pr_debug("Enabling LCD controller\n");
569
Alexander Shiyanb7d2d372014-02-15 09:56:23 +0400570 writel(fbi->map_dma, fbi->regs + LCDC_SSA);
Juergen Beisert72330b02008-12-16 11:44:07 +0100571
Juergen Beisert72330b02008-12-16 11:44:07 +0100572 /* panning offset 0 (0 pixel offset) */
573 writel(0x00000000, fbi->regs + LCDC_POS);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700574
575 /* disable hardware cursor */
Juergen Beisert72330b02008-12-16 11:44:07 +0100576 writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
577 fbi->regs + LCDC_CPOS);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700578
Sascha Hauerf142b612011-03-03 15:12:44 +0100579 /*
580 * RMCR_LCDC_EN_MX1 is present on i.MX1 only, but doesn't hurt
581 * on other SoCs
582 */
583 writel(RMCR_LCDC_EN_MX1, fbi->regs + LCDC_RMCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700584
Sascha Hauer13aaea02012-03-07 09:30:36 +0100585 clk_prepare_enable(fbi->clk_ipg);
586 clk_prepare_enable(fbi->clk_ahb);
587 clk_prepare_enable(fbi->clk_per);
Fabio Estevame6b267c2013-01-16 10:58:41 -0200588 fbi->enabled = true;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700589}
590
591static void imxfb_disable_controller(struct imxfb_info *fbi)
592{
Fabio Estevame6b267c2013-01-16 10:58:41 -0200593 if (!fbi->enabled)
594 return;
595
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700596 pr_debug("Disabling LCD controller\n");
597
Sascha Hauer13aaea02012-03-07 09:30:36 +0100598 clk_disable_unprepare(fbi->clk_per);
599 clk_disable_unprepare(fbi->clk_ipg);
600 clk_disable_unprepare(fbi->clk_ahb);
Fabio Estevame6b267c2013-01-16 10:58:41 -0200601 fbi->enabled = false;
Sascha Hauerf909ef62009-01-15 15:21:00 +0100602
Juergen Beisert72330b02008-12-16 11:44:07 +0100603 writel(0, fbi->regs + LCDC_RMCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700604}
605
606static int imxfb_blank(int blank, struct fb_info *info)
607{
608 struct imxfb_info *fbi = info->par;
609
610 pr_debug("imxfb_blank: blank=%d\n", blank);
611
612 switch (blank) {
613 case FB_BLANK_POWERDOWN:
614 case FB_BLANK_VSYNC_SUSPEND:
615 case FB_BLANK_HSYNC_SUSPEND:
616 case FB_BLANK_NORMAL:
617 imxfb_disable_controller(fbi);
618 break;
619
620 case FB_BLANK_UNBLANK:
621 imxfb_enable_controller(fbi);
622 break;
623 }
624 return 0;
625}
626
627static struct fb_ops imxfb_ops = {
628 .owner = THIS_MODULE,
629 .fb_check_var = imxfb_check_var,
630 .fb_set_par = imxfb_set_par,
631 .fb_setcolreg = imxfb_setcolreg,
632 .fb_fillrect = cfb_fillrect,
633 .fb_copyarea = cfb_copyarea,
634 .fb_imageblit = cfb_imageblit,
635 .fb_blank = imxfb_blank,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700636};
637
638/*
639 * imxfb_activate_var():
640 * Configures LCD Controller based on entries in var parameter. Settings are
641 * only written to the controller if changes were made.
642 */
643static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
644{
645 struct imxfb_info *fbi = info->par;
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800646 u32 ymax_mask = is_imx1_fb(fbi) ? YMAX_MASK_IMX1 : YMAX_MASK_IMX21;
Sascha Hauerf909ef62009-01-15 15:21:00 +0100647
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700648 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
649 var->xres, var->hsync_len,
650 var->left_margin, var->right_margin);
651 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
652 var->yres, var->vsync_len,
653 var->upper_margin, var->lower_margin);
654
655#if DEBUG_VAR
656 if (var->xres < 16 || var->xres > 1024)
657 printk(KERN_ERR "%s: invalid xres %d\n",
658 info->fix.id, var->xres);
659 if (var->hsync_len < 1 || var->hsync_len > 64)
660 printk(KERN_ERR "%s: invalid hsync_len %d\n",
661 info->fix.id, var->hsync_len);
662 if (var->left_margin > 255)
663 printk(KERN_ERR "%s: invalid left_margin %d\n",
664 info->fix.id, var->left_margin);
665 if (var->right_margin > 255)
666 printk(KERN_ERR "%s: invalid right_margin %d\n",
667 info->fix.id, var->right_margin);
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800668 if (var->yres < 1 || var->yres > ymax_mask)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700669 printk(KERN_ERR "%s: invalid yres %d\n",
670 info->fix.id, var->yres);
671 if (var->vsync_len > 100)
672 printk(KERN_ERR "%s: invalid vsync_len %d\n",
673 info->fix.id, var->vsync_len);
674 if (var->upper_margin > 63)
675 printk(KERN_ERR "%s: invalid upper_margin %d\n",
676 info->fix.id, var->upper_margin);
677 if (var->lower_margin > 255)
678 printk(KERN_ERR "%s: invalid lower_margin %d\n",
679 info->fix.id, var->lower_margin);
680#endif
681
Sascha Hauer343684f2009-03-19 08:25:41 +0100682 /* physical screen start address */
683 writel(VPW_VPW(var->xres * var->bits_per_pixel / 8 / 4),
684 fbi->regs + LCDC_VPW);
685
Sascha Hauer7e8549b2009-02-14 16:29:38 +0100686 writel(HCR_H_WIDTH(var->hsync_len - 1) |
687 HCR_H_WAIT_1(var->right_margin - 1) |
688 HCR_H_WAIT_2(var->left_margin - 3),
Juergen Beisert72330b02008-12-16 11:44:07 +0100689 fbi->regs + LCDC_HCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700690
Juergen Beisert72330b02008-12-16 11:44:07 +0100691 writel(VCR_V_WIDTH(var->vsync_len) |
Sascha Hauerd6ed5752008-12-16 11:44:08 +0100692 VCR_V_WAIT_1(var->lower_margin) |
693 VCR_V_WAIT_2(var->upper_margin),
Juergen Beisert72330b02008-12-16 11:44:07 +0100694 fbi->regs + LCDC_VCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700695
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800696 writel(SIZE_XMAX(var->xres) | (var->yres & ymax_mask),
Juergen Beisert72330b02008-12-16 11:44:07 +0100697 fbi->regs + LCDC_SIZE);
Sascha Hauerf909ef62009-01-15 15:21:00 +0100698
Sascha Hauer343684f2009-03-19 08:25:41 +0100699 writel(fbi->pcr, fbi->regs + LCDC_PCR);
Eric Bénard81ef8062010-08-02 08:39:55 +0100700#ifndef PWMR_BACKLIGHT_AVAILABLE
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200701 if (fbi->pwmr)
702 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
Eric Bénard81ef8062010-08-02 08:39:55 +0100703#endif
Juergen Beisert72330b02008-12-16 11:44:07 +0100704 writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200705
706 /* dmacr = 0 is no valid value, as we need DMA control marks. */
707 if (fbi->dmacr)
708 writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700709
710 return 0;
711}
712
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700713#ifdef CONFIG_PM
714/*
715 * Power management hooks. Note that we won't be called from IRQ context,
716 * unlike the blank functions above, so we may sleep.
717 */
Russell King3ae5eae2005-11-09 22:32:44 +0000718static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700719{
Uwe Kleine-König1ec56202010-02-02 13:44:10 -0800720 struct fb_info *info = platform_get_drvdata(dev);
721 struct imxfb_info *fbi = info->par;
Sascha Hauer66c87192008-12-16 11:44:08 +0100722
723 pr_debug("%s\n", __func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700724
Russell King9480e302005-10-28 09:52:56 -0700725 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700726 return 0;
727}
728
Russell King3ae5eae2005-11-09 22:32:44 +0000729static int imxfb_resume(struct platform_device *dev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700730{
Uwe Kleine-König1ec56202010-02-02 13:44:10 -0800731 struct fb_info *info = platform_get_drvdata(dev);
732 struct imxfb_info *fbi = info->par;
Sascha Hauer66c87192008-12-16 11:44:08 +0100733
734 pr_debug("%s\n", __func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700735
Russell King9480e302005-10-28 09:52:56 -0700736 imxfb_enable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700737 return 0;
738}
739#else
740#define imxfb_suspend NULL
741#define imxfb_resume NULL
742#endif
743
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200744static int imxfb_init_fbinfo(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700745{
Jingoo Han1c8034c2013-09-17 14:08:58 +0900746 struct imx_fb_platform_data *pdata = dev_get_platdata(&pdev->dev);
Juergen Beisert72330b02008-12-16 11:44:07 +0100747 struct fb_info *info = dev_get_drvdata(&pdev->dev);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700748 struct imxfb_info *fbi = info->par;
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200749 struct device_node *np;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700750
Harvey Harrison5ae12172008-04-28 02:15:47 -0700751 pr_debug("%s\n",__func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700752
Sascha Hauer66c87192008-12-16 11:44:08 +0100753 info->pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700754 if (!info->pseudo_palette)
755 return -ENOMEM;
756
757 memset(fbi, 0, sizeof(struct imxfb_info));
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700758
Gwenhael Goavec-Merouafc10302013-01-13 16:56:43 +0100759 fbi->devtype = pdev->id_entry->driver_data;
760
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700761 strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
762
Sascha Hauer66c87192008-12-16 11:44:08 +0100763 info->fix.type = FB_TYPE_PACKED_PIXELS;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700764 info->fix.type_aux = 0;
765 info->fix.xpanstep = 0;
766 info->fix.ypanstep = 0;
767 info->fix.ywrapstep = 0;
Sascha Hauer66c87192008-12-16 11:44:08 +0100768 info->fix.accel = FB_ACCEL_NONE;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700769
770 info->var.nonstd = 0;
771 info->var.activate = FB_ACTIVATE_NOW;
772 info->var.height = -1;
773 info->var.width = -1;
774 info->var.accel_flags = 0;
Sascha Hauer66c87192008-12-16 11:44:08 +0100775 info->var.vmode = FB_VMODE_NONINTERLACED;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700776
777 info->fbops = &imxfb_ops;
Sascha Hauer66c87192008-12-16 11:44:08 +0100778 info->flags = FBINFO_FLAG_DEFAULT |
779 FBINFO_READS_FAST;
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200780 if (pdata) {
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200781 fbi->lscr1 = pdata->lscr1;
782 fbi->dmacr = pdata->dmacr;
783 fbi->pwmr = pdata->pwmr;
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200784 } else {
785 np = pdev->dev.of_node;
786 info->var.grayscale = of_property_read_bool(np,
787 "cmap-greyscale");
788 fbi->cmap_inverse = of_property_read_bool(np, "cmap-inverse");
789 fbi->cmap_static = of_property_read_bool(np, "cmap-static");
Sascha Hauer343684f2009-03-19 08:25:41 +0100790
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200791 fbi->lscr1 = IMXFB_LSCR1_DEFAULT;
792 of_property_read_u32(np, "fsl,lscr1", &fbi->lscr1);
793
794 of_property_read_u32(np, "fsl,dmacr", &fbi->dmacr);
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200795 }
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700796
797 return 0;
798}
799
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200800static int imxfb_of_read_mode(struct device *dev, struct device_node *np,
801 struct imx_fb_videomode *imxfb_mode)
802{
803 int ret;
804 struct fb_videomode *of_mode = &imxfb_mode->mode;
805 u32 bpp;
806 u32 pcr;
807
808 ret = of_property_read_string(np, "model", &of_mode->name);
809 if (ret)
810 of_mode->name = NULL;
811
812 ret = of_get_fb_videomode(np, of_mode, OF_USE_NATIVE_MODE);
813 if (ret) {
814 dev_err(dev, "Failed to get videomode from DT\n");
815 return ret;
816 }
817
818 ret = of_property_read_u32(np, "bits-per-pixel", &bpp);
819 ret |= of_property_read_u32(np, "fsl,pcr", &pcr);
820
821 if (ret) {
822 dev_err(dev, "Failed to read bpp and pcr from DT\n");
823 return -EINVAL;
824 }
825
826 if (bpp < 1 || bpp > 255) {
827 dev_err(dev, "Bits per pixel have to be between 1 and 255\n");
828 return -EINVAL;
829 }
830
831 imxfb_mode->bpp = bpp;
832 imxfb_mode->pcr = pcr;
833
834 return 0;
835}
836
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400837static int imxfb_lcd_check_fb(struct lcd_device *lcddev, struct fb_info *fi)
838{
839 struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
840
841 if (!fi || fi->par == fbi)
842 return 1;
843
844 return 0;
845}
846
847static int imxfb_lcd_get_power(struct lcd_device *lcddev)
848{
849 struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
850
851 if (!IS_ERR(fbi->lcd_pwr))
852 return regulator_is_enabled(fbi->lcd_pwr);
853
854 return 1;
855}
856
857static int imxfb_lcd_set_power(struct lcd_device *lcddev, int power)
858{
859 struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
860
861 if (!IS_ERR(fbi->lcd_pwr)) {
862 if (power)
863 return regulator_enable(fbi->lcd_pwr);
864 else
865 return regulator_disable(fbi->lcd_pwr);
866 }
867
868 return 0;
869}
870
871static struct lcd_ops imxfb_lcd_ops = {
872 .check_fb = imxfb_lcd_check_fb,
873 .get_power = imxfb_lcd_get_power,
874 .set_power = imxfb_lcd_set_power,
875};
876
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200877static int imxfb_probe(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700878{
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700879 struct imxfb_info *fbi;
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400880 struct lcd_device *lcd;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700881 struct fb_info *info;
Sascha Hauer27889272008-12-16 11:44:09 +0100882 struct imx_fb_platform_data *pdata;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700883 struct resource *res;
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200884 struct imx_fb_videomode *m;
885 const struct of_device_id *of_id;
Sascha Hauer343684f2009-03-19 08:25:41 +0100886 int ret, i;
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200887 int bytes_per_pixel;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700888
Sascha Hauerd6b51502009-06-29 11:41:09 +0200889 dev_info(&pdev->dev, "i.MX Framebuffer driver\n");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700890
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200891 of_id = of_match_device(imxfb_of_dev_id, &pdev->dev);
892 if (of_id)
893 pdev->id_entry = of_id->data;
894
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700895 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sascha Hauer66c87192008-12-16 11:44:08 +0100896 if (!res)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700897 return -ENODEV;
898
Jingoo Han1c8034c2013-09-17 14:08:58 +0900899 pdata = dev_get_platdata(&pdev->dev);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700900
Russell King3ae5eae2005-11-09 22:32:44 +0000901 info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100902 if (!info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700903 return -ENOMEM;
904
905 fbi = info->par;
906
Russell King3ae5eae2005-11-09 22:32:44 +0000907 platform_set_drvdata(pdev, info);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700908
Juergen Beisert72330b02008-12-16 11:44:07 +0100909 ret = imxfb_init_fbinfo(pdev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100910 if (ret < 0)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700911 goto failed_init;
912
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200913 if (pdata) {
914 if (!fb_mode)
915 fb_mode = pdata->mode[0].mode.name;
916
917 fbi->mode = pdata->mode;
918 fbi->num_modes = pdata->num_modes;
919 } else {
920 struct device_node *display_np;
921 fb_mode = NULL;
922
923 display_np = of_parse_phandle(pdev->dev.of_node, "display", 0);
924 if (!display_np) {
925 dev_err(&pdev->dev, "No display defined in devicetree\n");
926 ret = -EINVAL;
927 goto failed_of_parse;
928 }
929
930 /*
931 * imxfb does not support more modes, we choose only the native
932 * mode.
933 */
934 fbi->num_modes = 1;
935
936 fbi->mode = devm_kzalloc(&pdev->dev,
937 sizeof(struct imx_fb_videomode), GFP_KERNEL);
938 if (!fbi->mode) {
939 ret = -ENOMEM;
940 goto failed_of_parse;
941 }
942
943 ret = imxfb_of_read_mode(&pdev->dev, display_np, fbi->mode);
944 if (ret)
945 goto failed_of_parse;
946 }
947
948 /* Calculate maximum bytes used per pixel. In most cases this should
949 * be the same as m->bpp/8 */
950 m = &fbi->mode[0];
951 bytes_per_pixel = (m->bpp + 7) / 8;
952 for (i = 0; i < fbi->num_modes; i++, m++)
953 info->fix.smem_len = max_t(size_t, info->fix.smem_len,
954 m->mode.xres * m->mode.yres * bytes_per_pixel);
955
Juergen Beisert72330b02008-12-16 11:44:07 +0100956 res = request_mem_region(res->start, resource_size(res),
957 DRIVER_NAME);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700958 if (!res) {
959 ret = -EBUSY;
Juergen Beisert72330b02008-12-16 11:44:07 +0100960 goto failed_req;
961 }
962
Sascha Hauer13aaea02012-03-07 09:30:36 +0100963 fbi->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
964 if (IS_ERR(fbi->clk_ipg)) {
965 ret = PTR_ERR(fbi->clk_ipg);
966 goto failed_getclock;
967 }
968
969 fbi->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
970 if (IS_ERR(fbi->clk_ahb)) {
971 ret = PTR_ERR(fbi->clk_ahb);
972 goto failed_getclock;
973 }
974
975 fbi->clk_per = devm_clk_get(&pdev->dev, "per");
976 if (IS_ERR(fbi->clk_per)) {
977 ret = PTR_ERR(fbi->clk_per);
Sascha Hauerf909ef62009-01-15 15:21:00 +0100978 goto failed_getclock;
979 }
980
Juergen Beisert72330b02008-12-16 11:44:07 +0100981 fbi->regs = ioremap(res->start, resource_size(res));
982 if (fbi->regs == NULL) {
Sascha Hauerd6b51502009-06-29 11:41:09 +0200983 dev_err(&pdev->dev, "Cannot map frame buffer registers\n");
Peter Senna Tschudincaf0a5c2012-09-18 14:07:56 +0200984 ret = -ENOMEM;
Juergen Beisert72330b02008-12-16 11:44:07 +0100985 goto failed_ioremap;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700986 }
987
Alexander Shiyanb7d2d372014-02-15 09:56:23 +0400988 fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
989 info->screen_base = dma_alloc_writecombine(&pdev->dev, fbi->map_size,
990 &fbi->map_dma, GFP_KERNEL);
Juergen Beisert72330b02008-12-16 11:44:07 +0100991
Alexander Shiyanb7d2d372014-02-15 09:56:23 +0400992 if (!info->screen_base) {
993 dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
994 ret = -ENOMEM;
995 goto failed_map;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700996 }
997
Alexander Shiyanb7d2d372014-02-15 09:56:23 +0400998 info->fix.smem_start = fbi->map_dma;
999
Markus Pargmann1b6c7932013-05-26 12:35:38 +02001000 if (pdata && pdata->init) {
Sascha Hauerc0b90a32009-01-15 15:37:22 +01001001 ret = pdata->init(fbi->pdev);
1002 if (ret)
1003 goto failed_platform_init;
1004 }
1005
Sascha Hauer343684f2009-03-19 08:25:41 +01001006
1007 INIT_LIST_HEAD(&info->modelist);
Markus Pargmann1b6c7932013-05-26 12:35:38 +02001008 for (i = 0; i < fbi->num_modes; i++)
1009 fb_add_videomode(&fbi->mode[i].mode, &info->modelist);
Sascha Hauer343684f2009-03-19 08:25:41 +01001010
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001011 /*
1012 * This makes sure that our colour bitfield
1013 * descriptors are correctly initialised.
1014 */
1015 imxfb_check_var(&info->var, info);
1016
Sascha Hauer66c87192008-12-16 11:44:08 +01001017 ret = fb_alloc_cmap(&info->cmap, 1 << info->var.bits_per_pixel, 0);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001018 if (ret < 0)
1019 goto failed_cmap;
1020
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001021 imxfb_set_par(info);
1022 ret = register_framebuffer(info);
1023 if (ret < 0) {
Pavel Pisaf99c8922006-01-07 10:44:32 +00001024 dev_err(&pdev->dev, "failed to register framebuffer\n");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001025 goto failed_register;
1026 }
1027
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +04001028 fbi->lcd_pwr = devm_regulator_get(&pdev->dev, "lcd");
1029 if (IS_ERR(fbi->lcd_pwr) && (PTR_ERR(fbi->lcd_pwr) == -EPROBE_DEFER)) {
1030 ret = -EPROBE_DEFER;
1031 goto failed_lcd;
1032 }
1033
1034 lcd = devm_lcd_device_register(&pdev->dev, "imxfb-lcd", &pdev->dev, fbi,
1035 &imxfb_lcd_ops);
1036 if (IS_ERR(lcd)) {
1037 ret = PTR_ERR(lcd);
1038 goto failed_lcd;
1039 }
1040
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001041 imxfb_enable_controller(fbi);
Eric Bénard7a2bb232010-07-16 15:09:07 +02001042 fbi->pdev = pdev;
Eric Bénard81ef8062010-08-02 08:39:55 +01001043#ifdef PWMR_BACKLIGHT_AVAILABLE
Eric Bénard7a2bb232010-07-16 15:09:07 +02001044 imxfb_init_backlight(fbi);
Eric Bénard81ef8062010-08-02 08:39:55 +01001045#endif
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001046
1047 return 0;
1048
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +04001049failed_lcd:
1050 unregister_framebuffer(info);
1051
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001052failed_register:
1053 fb_dealloc_cmap(&info->cmap);
1054failed_cmap:
Markus Pargmann1b6c7932013-05-26 12:35:38 +02001055 if (pdata && pdata->exit)
Sascha Hauerc0b90a32009-01-15 15:37:22 +01001056 pdata->exit(fbi->pdev);
1057failed_platform_init:
Alexander Shiyanb7d2d372014-02-15 09:56:23 +04001058 dma_free_writecombine(&pdev->dev, fbi->map_size, info->screen_base,
1059 fbi->map_dma);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001060failed_map:
Juergen Beisert72330b02008-12-16 11:44:07 +01001061 iounmap(fbi->regs);
1062failed_ioremap:
Julia Lawall609d3bb2011-06-01 17:10:11 +00001063failed_getclock:
Sascha Hauerd6b51502009-06-29 11:41:09 +02001064 release_mem_region(res->start, resource_size(res));
Juergen Beisert72330b02008-12-16 11:44:07 +01001065failed_req:
Markus Pargmann1b6c7932013-05-26 12:35:38 +02001066failed_of_parse:
Juergen Beisert72330b02008-12-16 11:44:07 +01001067 kfree(info->pseudo_palette);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001068failed_init:
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001069 framebuffer_release(info);
1070 return ret;
1071}
1072
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001073static int imxfb_remove(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001074{
Sascha Hauerc0b90a32009-01-15 15:37:22 +01001075 struct imx_fb_platform_data *pdata;
Russell King3ae5eae2005-11-09 22:32:44 +00001076 struct fb_info *info = platform_get_drvdata(pdev);
Sascha Hauer772a9e62005-07-17 20:15:36 +01001077 struct imxfb_info *fbi = info->par;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001078 struct resource *res;
1079
1080 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1081
Sascha Hauer772a9e62005-07-17 20:15:36 +01001082 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001083
Eric Bénard81ef8062010-08-02 08:39:55 +01001084#ifdef PWMR_BACKLIGHT_AVAILABLE
Eric Bénard7a2bb232010-07-16 15:09:07 +02001085 imxfb_exit_backlight(fbi);
Eric Bénard81ef8062010-08-02 08:39:55 +01001086#endif
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001087 unregister_framebuffer(info);
1088
Jingoo Han1c8034c2013-09-17 14:08:58 +09001089 pdata = dev_get_platdata(&pdev->dev);
Markus Pargmann1b6c7932013-05-26 12:35:38 +02001090 if (pdata && pdata->exit)
Sascha Hauerc0b90a32009-01-15 15:37:22 +01001091 pdata->exit(fbi->pdev);
1092
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001093 fb_dealloc_cmap(&info->cmap);
1094 kfree(info->pseudo_palette);
1095 framebuffer_release(info);
1096
Alexander Shiyanb7d2d372014-02-15 09:56:23 +04001097 dma_free_writecombine(&pdev->dev, fbi->map_size, info->screen_base,
1098 fbi->map_dma);
1099
Juergen Beisert72330b02008-12-16 11:44:07 +01001100 iounmap(fbi->regs);
Sascha Hauerd6b51502009-06-29 11:41:09 +02001101 release_mem_region(res->start, resource_size(res));
Sascha Hauerf909ef62009-01-15 15:21:00 +01001102
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001103 return 0;
1104}
1105
Sachin Kamat486dd6d2013-05-10 17:25:08 +05301106static void imxfb_shutdown(struct platform_device *dev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001107{
Russell King3ae5eae2005-11-09 22:32:44 +00001108 struct fb_info *info = platform_get_drvdata(dev);
Sascha Hauer772a9e62005-07-17 20:15:36 +01001109 struct imxfb_info *fbi = info->par;
1110 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001111}
1112
Russell King3ae5eae2005-11-09 22:32:44 +00001113static struct platform_driver imxfb_driver = {
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001114 .suspend = imxfb_suspend,
1115 .resume = imxfb_resume,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001116 .remove = imxfb_remove,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001117 .shutdown = imxfb_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00001118 .driver = {
Juergen Beisert72330b02008-12-16 11:44:07 +01001119 .name = DRIVER_NAME,
Markus Pargmann1b6c7932013-05-26 12:35:38 +02001120 .of_match_table = imxfb_of_dev_id,
Russell King3ae5eae2005-11-09 22:32:44 +00001121 },
Shawn Guoe69dc9a2012-09-16 19:59:53 +08001122 .id_table = imxfb_devtype,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001123};
1124
Sascha Hauer343684f2009-03-19 08:25:41 +01001125static int imxfb_setup(void)
1126{
1127#ifndef MODULE
1128 char *opt, *options = NULL;
1129
1130 if (fb_get_options("imxfb", &options))
1131 return -ENODEV;
1132
1133 if (!options || !*options)
1134 return 0;
1135
1136 while ((opt = strsep(&options, ",")) != NULL) {
1137 if (!*opt)
1138 continue;
1139 else
1140 fb_mode = opt;
1141 }
1142#endif
1143 return 0;
1144}
1145
Sachin Kamat486dd6d2013-05-10 17:25:08 +05301146static int __init imxfb_init(void)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001147{
Sascha Hauer343684f2009-03-19 08:25:41 +01001148 int ret = imxfb_setup();
1149
1150 if (ret < 0)
1151 return ret;
1152
Juergen Beisert72330b02008-12-16 11:44:07 +01001153 return platform_driver_probe(&imxfb_driver, imxfb_probe);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001154}
1155
1156static void __exit imxfb_cleanup(void)
1157{
Russell King3ae5eae2005-11-09 22:32:44 +00001158 platform_driver_unregister(&imxfb_driver);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001159}
1160
1161module_init(imxfb_init);
1162module_exit(imxfb_cleanup);
1163
Fabio Estevame3d5fb72011-01-10 09:47:41 -02001164MODULE_DESCRIPTION("Freescale i.MX framebuffer driver");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001165MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1166MODULE_LICENSE("GPL");