blob: 51fde1b2a7938889d1a6dcfc1a5cd02d0f5863da [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 Beisert72330b0e2008-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
Uwe Kleine-Königded77a72022-07-23 19:57:19 +020044#define PCR_TFT (1 << 31)
45#define PCR_BPIX_8 (3 << 25)
46#define PCR_BPIX_12 (4 << 25)
47#define PCR_BPIX_16 (5 << 25)
48#define PCR_BPIX_18 (6 << 25)
49
50struct imx_fb_videomode {
51 struct fb_videomode mode;
52 u32 pcr;
53 bool aus_mode;
54 unsigned char bpp;
55};
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070056
57/*
58 * Complain if VAR is out of range.
59 */
60#define DEBUG_VAR 1
61
Juergen Beisert72330b0e2008-12-16 11:44:07 +010062#define DRIVER_NAME "imx-fb"
63
64#define LCDC_SSA 0x00
65
66#define LCDC_SIZE 0x04
67#define SIZE_XMAX(x) ((((x) >> 4) & 0x3f) << 20)
Sascha Hauer1d0f9872009-01-26 17:29:10 +010068
Shawn Guoe69dc9a2012-09-16 19:59:53 +080069#define YMAX_MASK_IMX1 0x1ff
70#define YMAX_MASK_IMX21 0x3ff
Juergen Beisert72330b0e2008-12-16 11:44:07 +010071
72#define LCDC_VPW 0x08
73#define VPW_VPW(x) ((x) & 0x3ff)
74
75#define LCDC_CPOS 0x0C
76#define CPOS_CC1 (1<<31)
77#define CPOS_CC0 (1<<30)
78#define CPOS_OP (1<<28)
79#define CPOS_CXP(x) (((x) & 3ff) << 16)
Sascha Hauer1d0f9872009-01-26 17:29:10 +010080
Juergen Beisert72330b0e2008-12-16 11:44:07 +010081#define LCDC_LCWHB 0x10
82#define LCWHB_BK_EN (1<<31)
83#define LCWHB_CW(w) (((w) & 0x1f) << 24)
84#define LCWHB_CH(h) (((h) & 0x1f) << 16)
85#define LCWHB_BD(x) ((x) & 0xff)
86
87#define LCDC_LCHCC 0x14
Sascha Hauer1d0f9872009-01-26 17:29:10 +010088
Juergen Beisert72330b0e2008-12-16 11:44:07 +010089#define LCDC_PCR 0x18
90
91#define LCDC_HCR 0x1C
92#define HCR_H_WIDTH(x) (((x) & 0x3f) << 26)
93#define HCR_H_WAIT_1(x) (((x) & 0xff) << 8)
94#define HCR_H_WAIT_2(x) ((x) & 0xff)
95
96#define LCDC_VCR 0x20
97#define VCR_V_WIDTH(x) (((x) & 0x3f) << 26)
98#define VCR_V_WAIT_1(x) (((x) & 0xff) << 8)
99#define VCR_V_WAIT_2(x) ((x) & 0xff)
100
101#define LCDC_POS 0x24
102#define POS_POS(x) ((x) & 1f)
103
104#define LCDC_LSCR1 0x28
105/* bit fields in imxfb.h */
106
107#define LCDC_PWMR 0x2C
108/* bit fields in imxfb.h */
109
110#define LCDC_DMACR 0x30
111/* bit fields in imxfb.h */
112
113#define LCDC_RMCR 0x34
Sascha Hauer1d0f9872009-01-26 17:29:10 +0100114
Sascha Hauerf142b612011-03-03 15:12:44 +0100115#define RMCR_LCDC_EN_MX1 (1<<1)
Sascha Hauer1d0f9872009-01-26 17:29:10 +0100116
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100117#define RMCR_SELF_REF (1<<0)
118
119#define LCDC_LCDICR 0x38
120#define LCDICR_INT_SYN (1<<2)
121#define LCDICR_INT_CON (1)
122
123#define LCDC_LCDISR 0x40
124#define LCDISR_UDR_ERR (1<<3)
125#define LCDISR_ERR_RES (1<<2)
126#define LCDISR_EOF (1<<1)
127#define LCDISR_BOF (1<<0)
128
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200129#define IMXFB_LSCR1_DEFAULT 0x00120300
130
Martin Kaiserb62ea412017-04-21 16:47:11 +0200131#define LCDC_LAUSCR 0x80
132#define LAUSCR_AUS_MODE (1<<31)
133
Sascha Hauer343684f2009-03-19 08:25:41 +0100134/* Used fb-mode. Can be set on kernel command line, therefore file-static. */
135static const char *fb_mode;
136
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100137/*
138 * These are the bitfields for each
139 * display depth that we support.
140 */
141struct imxfb_rgb {
142 struct fb_bitfield red;
143 struct fb_bitfield green;
144 struct fb_bitfield blue;
145 struct fb_bitfield transp;
146};
147
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800148enum imxfb_type {
149 IMX1_FB,
150 IMX21_FB,
151};
152
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100153struct imxfb_info {
154 struct platform_device *pdev;
155 void __iomem *regs;
Sascha Hauer13aaea02012-03-07 09:30:36 +0100156 struct clk *clk_ipg;
157 struct clk *clk_ahb;
158 struct clk *clk_per;
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800159 enum imxfb_type devtype;
Fabio Estevame6b267c2013-01-16 10:58:41 -0200160 bool enabled;
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100161
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100162 /*
163 * These are the addresses we mapped
164 * the framebuffer memory region to.
165 */
166 dma_addr_t map_dma;
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100167 u_int map_size;
168
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100169 u_int palette_size;
170
171 dma_addr_t dbar1;
172 dma_addr_t dbar2;
173
174 u_int pcr;
Martin Kaiserb62ea412017-04-21 16:47:11 +0200175 u_int lauscr;
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100176 u_int pwmr;
177 u_int lscr1;
178 u_int dmacr;
Alexander Shiyanb7d2d372014-02-15 09:56:23 +0400179 bool cmap_inverse;
180 bool cmap_static;
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100181
Sascha Hauer343684f2009-03-19 08:25:41 +0100182 struct imx_fb_videomode *mode;
183 int num_modes;
184
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400185 struct regulator *lcd_pwr;
Uwe Kleine-König5c694f82020-03-23 22:16:26 +0100186 int lcd_pwr_enabled;
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100187};
188
Krzysztof Kozlowskibd388a22015-05-02 00:38:39 +0900189static const struct platform_device_id imxfb_devtype[] = {
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800190 {
191 .name = "imx1-fb",
192 .driver_data = IMX1_FB,
193 }, {
194 .name = "imx21-fb",
195 .driver_data = IMX21_FB,
196 }, {
197 /* sentinel */
198 }
199};
200MODULE_DEVICE_TABLE(platform, imxfb_devtype);
201
Sanjeev Sharma5d802442015-02-09 16:32:27 +0530202static const struct of_device_id imxfb_of_dev_id[] = {
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200203 {
204 .compatible = "fsl,imx1-fb",
205 .data = &imxfb_devtype[IMX1_FB],
206 }, {
207 .compatible = "fsl,imx21-fb",
208 .data = &imxfb_devtype[IMX21_FB],
209 }, {
210 /* sentinel */
211 }
212};
213MODULE_DEVICE_TABLE(of, imxfb_of_dev_id);
214
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800215static inline int is_imx1_fb(struct imxfb_info *fbi)
216{
217 return fbi->devtype == IMX1_FB;
218}
219
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100220#define IMX_NAME "IMX"
221
222/*
223 * Minimum X and Y resolutions
224 */
225#define MIN_XRES 64
226#define MIN_YRES 64
227
Sascha Hauer15122222009-01-26 17:31:02 +0100228/* Actually this really is 18bit support, the lowest 2 bits of each colour
229 * are unused in hardware. We claim to have 24bit support to make software
230 * like X work, which does not support 18bit.
231 */
232static struct imxfb_rgb def_rgb_18 = {
233 .red = {.offset = 16, .length = 8,},
234 .green = {.offset = 8, .length = 8,},
235 .blue = {.offset = 0, .length = 8,},
236 .transp = {.offset = 0, .length = 0,},
237};
238
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100239static struct imxfb_rgb def_rgb_16_tft = {
240 .red = {.offset = 11, .length = 5,},
241 .green = {.offset = 5, .length = 6,},
242 .blue = {.offset = 0, .length = 5,},
243 .transp = {.offset = 0, .length = 0,},
244};
245
246static struct imxfb_rgb def_rgb_16_stn = {
Sascha Hauer66c87192008-12-16 11:44:08 +0100247 .red = {.offset = 8, .length = 4,},
248 .green = {.offset = 4, .length = 4,},
249 .blue = {.offset = 0, .length = 4,},
250 .transp = {.offset = 0, .length = 0,},
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700251};
252
253static struct imxfb_rgb def_rgb_8 = {
Sascha Hauer66c87192008-12-16 11:44:08 +0100254 .red = {.offset = 0, .length = 8,},
255 .green = {.offset = 0, .length = 8,},
256 .blue = {.offset = 0, .length = 8,},
257 .transp = {.offset = 0, .length = 0,},
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700258};
259
Sascha Hauer66c87192008-12-16 11:44:08 +0100260static int imxfb_activate_var(struct fb_var_screeninfo *var,
261 struct fb_info *info);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700262
263static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
264{
265 chan &= 0xffff;
266 chan >>= 16 - bf->length;
267 return chan << bf->offset;
268}
269
Sascha Hauer66c87192008-12-16 11:44:08 +0100270static int imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
271 u_int trans, struct fb_info *info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700272{
273 struct imxfb_info *fbi = info->par;
274 u_int val, ret = 1;
275
276#define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
277 if (regno < fbi->palette_size) {
278 val = (CNVT_TOHW(red, 4) << 8) |
279 (CNVT_TOHW(green,4) << 4) |
280 CNVT_TOHW(blue, 4);
281
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100282 writel(val, fbi->regs + 0x800 + (regno << 2));
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700283 ret = 0;
284 }
285 return ret;
286}
287
Sascha Hauer66c87192008-12-16 11:44:08 +0100288static int imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700289 u_int trans, struct fb_info *info)
290{
291 struct imxfb_info *fbi = info->par;
292 unsigned int val;
293 int ret = 1;
294
295 /*
296 * If inverse mode was selected, invert all the colours
297 * rather than the register number. The register number
298 * is what you poke into the framebuffer to produce the
299 * colour you requested.
300 */
301 if (fbi->cmap_inverse) {
302 red = 0xffff - red;
303 green = 0xffff - green;
304 blue = 0xffff - blue;
305 }
306
307 /*
308 * If greyscale is true, then we convert the RGB value
309 * to greyscale no mater what visual we are using.
310 */
311 if (info->var.grayscale)
312 red = green = blue = (19595 * red + 38470 * green +
313 7471 * blue) >> 16;
314
315 switch (info->fix.visual) {
316 case FB_VISUAL_TRUECOLOR:
317 /*
318 * 12 or 16-bit True Colour. We encode the RGB value
319 * according to the RGB bitfield information.
320 */
321 if (regno < 16) {
322 u32 *pal = info->pseudo_palette;
323
324 val = chan_to_field(red, &info->var.red);
325 val |= chan_to_field(green, &info->var.green);
326 val |= chan_to_field(blue, &info->var.blue);
327
328 pal[regno] = val;
329 ret = 0;
330 }
331 break;
332
333 case FB_VISUAL_STATIC_PSEUDOCOLOR:
334 case FB_VISUAL_PSEUDOCOLOR:
335 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
336 break;
337 }
338
339 return ret;
340}
341
Sascha Hauer343684f2009-03-19 08:25:41 +0100342static const struct imx_fb_videomode *imxfb_find_mode(struct imxfb_info *fbi)
343{
344 struct imx_fb_videomode *m;
345 int i;
346
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200347 if (!fb_mode)
348 return &fbi->mode[0];
349
Sascha Hauer343684f2009-03-19 08:25:41 +0100350 for (i = 0, m = &fbi->mode[0]; i < fbi->num_modes; i++, m++) {
351 if (!strcmp(m->mode.name, fb_mode))
352 return m;
353 }
354 return NULL;
355}
356
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700357/*
358 * imxfb_check_var():
359 * Round up in the following order: bits_per_pixel, xres,
360 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
361 * bitfields, horizontal timing, vertical timing.
362 */
Sascha Hauer66c87192008-12-16 11:44:08 +0100363static int imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700364{
365 struct imxfb_info *fbi = info->par;
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100366 struct imxfb_rgb *rgb;
Sascha Hauer343684f2009-03-19 08:25:41 +0100367 const struct imx_fb_videomode *imxfb_mode;
368 unsigned long lcd_clk;
369 unsigned long long tmp;
370 u32 pcr = 0;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700371
372 if (var->xres < MIN_XRES)
373 var->xres = MIN_XRES;
374 if (var->yres < MIN_YRES)
375 var->yres = MIN_YRES;
Sascha Hauer343684f2009-03-19 08:25:41 +0100376
377 imxfb_mode = imxfb_find_mode(fbi);
378 if (!imxfb_mode)
379 return -EINVAL;
380
381 var->xres = imxfb_mode->mode.xres;
382 var->yres = imxfb_mode->mode.yres;
383 var->bits_per_pixel = imxfb_mode->bpp;
384 var->pixclock = imxfb_mode->mode.pixclock;
385 var->hsync_len = imxfb_mode->mode.hsync_len;
386 var->left_margin = imxfb_mode->mode.left_margin;
387 var->right_margin = imxfb_mode->mode.right_margin;
388 var->vsync_len = imxfb_mode->mode.vsync_len;
389 var->upper_margin = imxfb_mode->mode.upper_margin;
390 var->lower_margin = imxfb_mode->mode.lower_margin;
391 var->sync = imxfb_mode->mode.sync;
392 var->xres_virtual = max(var->xres_virtual, var->xres);
393 var->yres_virtual = max(var->yres_virtual, var->yres);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700394
395 pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
Sascha Hauer343684f2009-03-19 08:25:41 +0100396
Sascha Hauer13aaea02012-03-07 09:30:36 +0100397 lcd_clk = clk_get_rate(fbi->clk_per);
Sascha Hauer343684f2009-03-19 08:25:41 +0100398
399 tmp = var->pixclock * (unsigned long long)lcd_clk;
400
401 do_div(tmp, 1000000);
402
403 if (do_div(tmp, 1000000) > 500000)
404 tmp++;
405
406 pcr = (unsigned int)tmp;
407
408 if (--pcr > 0x3F) {
409 pcr = 0x3F;
410 printk(KERN_WARNING "Must limit pixel clock to %luHz\n",
411 lcd_clk / pcr);
412 }
413
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700414 switch (var->bits_per_pixel) {
Sascha Hauer15122222009-01-26 17:31:02 +0100415 case 32:
Sascha Hauer343684f2009-03-19 08:25:41 +0100416 pcr |= PCR_BPIX_18;
Sascha Hauer15122222009-01-26 17:31:02 +0100417 rgb = &def_rgb_18;
418 break;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700419 case 16:
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100420 default:
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800421 if (is_imx1_fb(fbi))
Sascha Hauer343684f2009-03-19 08:25:41 +0100422 pcr |= PCR_BPIX_12;
423 else
424 pcr |= PCR_BPIX_16;
425
426 if (imxfb_mode->pcr & PCR_TFT)
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100427 rgb = &def_rgb_16_tft;
428 else
429 rgb = &def_rgb_16_stn;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700430 break;
431 case 8:
Sascha Hauer343684f2009-03-19 08:25:41 +0100432 pcr |= PCR_BPIX_8;
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100433 rgb = &def_rgb_8;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700434 break;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700435 }
436
Sascha Hauer343684f2009-03-19 08:25:41 +0100437 /* add sync polarities */
438 pcr |= imxfb_mode->pcr & ~(0x3f | (7 << 25));
439
440 fbi->pcr = pcr;
Martin Kaiserb62ea412017-04-21 16:47:11 +0200441 /*
442 * The LCDC AUS Mode Control Register does not exist on imx1.
443 */
444 if (!is_imx1_fb(fbi) && imxfb_mode->aus_mode)
445 fbi->lauscr = LAUSCR_AUS_MODE;
Sascha Hauer343684f2009-03-19 08:25:41 +0100446
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700447 /*
448 * Copy the RGB parameters for this display
449 * from the machine specific parameters.
450 */
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100451 var->red = rgb->red;
452 var->green = rgb->green;
453 var->blue = rgb->blue;
454 var->transp = rgb->transp;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700455
456 pr_debug("RGBT length = %d:%d:%d:%d\n",
457 var->red.length, var->green.length, var->blue.length,
458 var->transp.length);
459
460 pr_debug("RGBT offset = %d:%d:%d:%d\n",
461 var->red.offset, var->green.offset, var->blue.offset,
462 var->transp.offset);
463
464 return 0;
465}
466
467/*
468 * imxfb_set_par():
469 * Set the user defined part of the display for the specified console
470 */
471static int imxfb_set_par(struct fb_info *info)
472{
473 struct imxfb_info *fbi = info->par;
474 struct fb_var_screeninfo *var = &info->var;
475
Sascha Hauer15122222009-01-26 17:31:02 +0100476 if (var->bits_per_pixel == 16 || var->bits_per_pixel == 32)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700477 info->fix.visual = FB_VISUAL_TRUECOLOR;
478 else if (!fbi->cmap_static)
479 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
480 else {
481 /*
482 * Some people have weird ideas about wanting static
483 * pseudocolor maps. I suspect their user space
484 * applications are broken.
485 */
486 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
487 }
488
Sascha Hauer66c87192008-12-16 11:44:08 +0100489 info->fix.line_length = var->xres_virtual * var->bits_per_pixel / 8;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700490 fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
491
492 imxfb_activate_var(var, info);
493
494 return 0;
495}
496
Uwe Kleine-Königcc6df3a2016-05-04 11:43:18 +0200497static int imxfb_enable_controller(struct imxfb_info *fbi)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700498{
Uwe Kleine-Königcc6df3a2016-05-04 11:43:18 +0200499 int ret;
Fabio Estevame6b267c2013-01-16 10:58:41 -0200500
501 if (fbi->enabled)
Uwe Kleine-Königcc6df3a2016-05-04 11:43:18 +0200502 return 0;
Fabio Estevame6b267c2013-01-16 10:58:41 -0200503
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700504 pr_debug("Enabling LCD controller\n");
505
Alexander Shiyanb7d2d372014-02-15 09:56:23 +0400506 writel(fbi->map_dma, fbi->regs + LCDC_SSA);
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100507
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100508 /* panning offset 0 (0 pixel offset) */
509 writel(0x00000000, fbi->regs + LCDC_POS);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700510
511 /* disable hardware cursor */
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100512 writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
513 fbi->regs + LCDC_CPOS);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700514
Sascha Hauerf142b612011-03-03 15:12:44 +0100515 /*
516 * RMCR_LCDC_EN_MX1 is present on i.MX1 only, but doesn't hurt
517 * on other SoCs
518 */
519 writel(RMCR_LCDC_EN_MX1, fbi->regs + LCDC_RMCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700520
Uwe Kleine-Königcc6df3a2016-05-04 11:43:18 +0200521 ret = clk_prepare_enable(fbi->clk_ipg);
522 if (ret)
523 goto err_enable_ipg;
524
525 ret = clk_prepare_enable(fbi->clk_ahb);
526 if (ret)
527 goto err_enable_ahb;
528
529 ret = clk_prepare_enable(fbi->clk_per);
530 if (ret)
531 goto err_enable_per;
532
Fabio Estevame6b267c2013-01-16 10:58:41 -0200533 fbi->enabled = true;
Uwe Kleine-Königcc6df3a2016-05-04 11:43:18 +0200534 return 0;
535
536err_enable_per:
537 clk_disable_unprepare(fbi->clk_ahb);
538err_enable_ahb:
539 clk_disable_unprepare(fbi->clk_ipg);
540err_enable_ipg:
541 writel(0, fbi->regs + LCDC_RMCR);
542
543 return ret;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700544}
545
546static void imxfb_disable_controller(struct imxfb_info *fbi)
547{
Fabio Estevame6b267c2013-01-16 10:58:41 -0200548 if (!fbi->enabled)
549 return;
550
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700551 pr_debug("Disabling LCD controller\n");
552
Sascha Hauer13aaea02012-03-07 09:30:36 +0100553 clk_disable_unprepare(fbi->clk_per);
Sascha Hauer13aaea02012-03-07 09:30:36 +0100554 clk_disable_unprepare(fbi->clk_ahb);
Uwe Kleine-Königcc6df3a2016-05-04 11:43:18 +0200555 clk_disable_unprepare(fbi->clk_ipg);
Fabio Estevame6b267c2013-01-16 10:58:41 -0200556 fbi->enabled = false;
Sascha Hauerf909ef62009-01-15 15:21:00 +0100557
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100558 writel(0, fbi->regs + LCDC_RMCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700559}
560
561static int imxfb_blank(int blank, struct fb_info *info)
562{
563 struct imxfb_info *fbi = info->par;
564
565 pr_debug("imxfb_blank: blank=%d\n", blank);
566
567 switch (blank) {
568 case FB_BLANK_POWERDOWN:
569 case FB_BLANK_VSYNC_SUSPEND:
570 case FB_BLANK_HSYNC_SUSPEND:
571 case FB_BLANK_NORMAL:
572 imxfb_disable_controller(fbi);
573 break;
574
575 case FB_BLANK_UNBLANK:
Uwe Kleine-Königcc6df3a2016-05-04 11:43:18 +0200576 return imxfb_enable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700577 }
578 return 0;
579}
580
Jani Nikula8a48ac332019-12-03 18:38:50 +0200581static const struct fb_ops imxfb_ops = {
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700582 .owner = THIS_MODULE,
583 .fb_check_var = imxfb_check_var,
584 .fb_set_par = imxfb_set_par,
585 .fb_setcolreg = imxfb_setcolreg,
586 .fb_fillrect = cfb_fillrect,
587 .fb_copyarea = cfb_copyarea,
588 .fb_imageblit = cfb_imageblit,
589 .fb_blank = imxfb_blank,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700590};
591
592/*
593 * imxfb_activate_var():
594 * Configures LCD Controller based on entries in var parameter. Settings are
595 * only written to the controller if changes were made.
596 */
597static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
598{
599 struct imxfb_info *fbi = info->par;
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800600 u32 ymax_mask = is_imx1_fb(fbi) ? YMAX_MASK_IMX1 : YMAX_MASK_IMX21;
Sascha Hauerf909ef62009-01-15 15:21:00 +0100601
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700602 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
603 var->xres, var->hsync_len,
604 var->left_margin, var->right_margin);
605 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
606 var->yres, var->vsync_len,
607 var->upper_margin, var->lower_margin);
608
609#if DEBUG_VAR
610 if (var->xres < 16 || var->xres > 1024)
611 printk(KERN_ERR "%s: invalid xres %d\n",
612 info->fix.id, var->xres);
613 if (var->hsync_len < 1 || var->hsync_len > 64)
614 printk(KERN_ERR "%s: invalid hsync_len %d\n",
615 info->fix.id, var->hsync_len);
616 if (var->left_margin > 255)
617 printk(KERN_ERR "%s: invalid left_margin %d\n",
618 info->fix.id, var->left_margin);
619 if (var->right_margin > 255)
620 printk(KERN_ERR "%s: invalid right_margin %d\n",
621 info->fix.id, var->right_margin);
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800622 if (var->yres < 1 || var->yres > ymax_mask)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700623 printk(KERN_ERR "%s: invalid yres %d\n",
624 info->fix.id, var->yres);
625 if (var->vsync_len > 100)
626 printk(KERN_ERR "%s: invalid vsync_len %d\n",
627 info->fix.id, var->vsync_len);
628 if (var->upper_margin > 63)
629 printk(KERN_ERR "%s: invalid upper_margin %d\n",
630 info->fix.id, var->upper_margin);
631 if (var->lower_margin > 255)
632 printk(KERN_ERR "%s: invalid lower_margin %d\n",
633 info->fix.id, var->lower_margin);
634#endif
635
Sascha Hauer343684f2009-03-19 08:25:41 +0100636 /* physical screen start address */
637 writel(VPW_VPW(var->xres * var->bits_per_pixel / 8 / 4),
638 fbi->regs + LCDC_VPW);
639
Sascha Hauer7e8549b2009-02-14 16:29:38 +0100640 writel(HCR_H_WIDTH(var->hsync_len - 1) |
641 HCR_H_WAIT_1(var->right_margin - 1) |
642 HCR_H_WAIT_2(var->left_margin - 3),
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100643 fbi->regs + LCDC_HCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700644
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100645 writel(VCR_V_WIDTH(var->vsync_len) |
Sascha Hauerd6ed5752008-12-16 11:44:08 +0100646 VCR_V_WAIT_1(var->lower_margin) |
647 VCR_V_WAIT_2(var->upper_margin),
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100648 fbi->regs + LCDC_VCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700649
Shawn Guoe69dc9a2012-09-16 19:59:53 +0800650 writel(SIZE_XMAX(var->xres) | (var->yres & ymax_mask),
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100651 fbi->regs + LCDC_SIZE);
Sascha Hauerf909ef62009-01-15 15:21:00 +0100652
Sascha Hauer343684f2009-03-19 08:25:41 +0100653 writel(fbi->pcr, fbi->regs + LCDC_PCR);
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200654 if (fbi->pwmr)
655 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100656 writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200657
658 /* dmacr = 0 is no valid value, as we need DMA control marks. */
659 if (fbi->dmacr)
660 writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700661
Martin Kaiserb62ea412017-04-21 16:47:11 +0200662 if (fbi->lauscr)
663 writel(fbi->lauscr, fbi->regs + LCDC_LAUSCR);
664
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700665 return 0;
666}
667
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200668static int imxfb_init_fbinfo(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700669{
Julia Lawall0b5e0f42021-02-09 22:13:25 +0100670 struct fb_info *info = platform_get_drvdata(pdev);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700671 struct imxfb_info *fbi = info->par;
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200672 struct device_node *np;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700673
Harvey Harrison5ae12172008-04-28 02:15:47 -0700674 pr_debug("%s\n",__func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700675
Kees Cook6da2ec52018-06-12 13:55:00 -0700676 info->pseudo_palette = kmalloc_array(16, sizeof(u32), GFP_KERNEL);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700677 if (!info->pseudo_palette)
678 return -ENOMEM;
679
680 memset(fbi, 0, sizeof(struct imxfb_info));
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700681
Gwenhael Goavec-Merouafc10302013-01-13 16:56:43 +0100682 fbi->devtype = pdev->id_entry->driver_data;
683
Wolfram Sang8d026852022-08-18 23:01:17 +0200684 strscpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700685
Sascha Hauer66c87192008-12-16 11:44:08 +0100686 info->fix.type = FB_TYPE_PACKED_PIXELS;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700687 info->fix.type_aux = 0;
688 info->fix.xpanstep = 0;
689 info->fix.ypanstep = 0;
690 info->fix.ywrapstep = 0;
Sascha Hauer66c87192008-12-16 11:44:08 +0100691 info->fix.accel = FB_ACCEL_NONE;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700692
693 info->var.nonstd = 0;
694 info->var.activate = FB_ACTIVATE_NOW;
695 info->var.height = -1;
696 info->var.width = -1;
697 info->var.accel_flags = 0;
Sascha Hauer66c87192008-12-16 11:44:08 +0100698 info->var.vmode = FB_VMODE_NONINTERLACED;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700699
700 info->fbops = &imxfb_ops;
Sascha Hauer66c87192008-12-16 11:44:08 +0100701 info->flags = FBINFO_FLAG_DEFAULT |
702 FBINFO_READS_FAST;
Sascha Hauer343684f2009-03-19 08:25:41 +0100703
Uwe Kleine-Könige948d322022-07-23 19:57:17 +0200704 np = pdev->dev.of_node;
705 info->var.grayscale = of_property_read_bool(np,
706 "cmap-greyscale");
707 fbi->cmap_inverse = of_property_read_bool(np, "cmap-inverse");
708 fbi->cmap_static = of_property_read_bool(np, "cmap-static");
Denis Cariklicf071d22014-03-14 11:55:52 +0100709
Uwe Kleine-Könige948d322022-07-23 19:57:17 +0200710 fbi->lscr1 = IMXFB_LSCR1_DEFAULT;
Denis Cariklicf071d22014-03-14 11:55:52 +0100711
Uwe Kleine-Könige948d322022-07-23 19:57:17 +0200712 of_property_read_u32(np, "fsl,lpccr", &fbi->pwmr);
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200713
Uwe Kleine-Könige948d322022-07-23 19:57:17 +0200714 of_property_read_u32(np, "fsl,lscr1", &fbi->lscr1);
715
716 of_property_read_u32(np, "fsl,dmacr", &fbi->dmacr);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700717
718 return 0;
719}
720
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200721static int imxfb_of_read_mode(struct device *dev, struct device_node *np,
722 struct imx_fb_videomode *imxfb_mode)
723{
724 int ret;
725 struct fb_videomode *of_mode = &imxfb_mode->mode;
726 u32 bpp;
727 u32 pcr;
728
729 ret = of_property_read_string(np, "model", &of_mode->name);
730 if (ret)
731 of_mode->name = NULL;
732
733 ret = of_get_fb_videomode(np, of_mode, OF_USE_NATIVE_MODE);
734 if (ret) {
735 dev_err(dev, "Failed to get videomode from DT\n");
736 return ret;
737 }
738
739 ret = of_property_read_u32(np, "bits-per-pixel", &bpp);
740 ret |= of_property_read_u32(np, "fsl,pcr", &pcr);
741
742 if (ret) {
743 dev_err(dev, "Failed to read bpp and pcr from DT\n");
744 return -EINVAL;
745 }
746
747 if (bpp < 1 || bpp > 255) {
748 dev_err(dev, "Bits per pixel have to be between 1 and 255\n");
749 return -EINVAL;
750 }
751
752 imxfb_mode->bpp = bpp;
753 imxfb_mode->pcr = pcr;
754
Martin Kaiserb62ea412017-04-21 16:47:11 +0200755 /*
756 * fsl,aus-mode is optional
757 */
758 imxfb_mode->aus_mode = of_property_read_bool(np, "fsl,aus-mode");
759
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200760 return 0;
761}
762
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400763static int imxfb_lcd_check_fb(struct lcd_device *lcddev, struct fb_info *fi)
764{
765 struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
766
767 if (!fi || fi->par == fbi)
768 return 1;
769
770 return 0;
771}
772
Alexander Shiyanf590f992014-02-15 09:56:25 +0400773static int imxfb_lcd_get_contrast(struct lcd_device *lcddev)
774{
775 struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
776
777 return fbi->pwmr & 0xff;
778}
779
780static int imxfb_lcd_set_contrast(struct lcd_device *lcddev, int contrast)
781{
782 struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
783
784 if (fbi->pwmr && fbi->enabled) {
785 if (contrast > 255)
786 contrast = 255;
787 else if (contrast < 0)
788 contrast = 0;
789
790 fbi->pwmr &= ~0xff;
791 fbi->pwmr |= contrast;
792
793 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
794 }
795
796 return 0;
797}
798
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400799static int imxfb_lcd_get_power(struct lcd_device *lcddev)
800{
801 struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
802
Uwe Kleine-König46ffe102016-05-04 11:43:16 +0200803 if (!IS_ERR(fbi->lcd_pwr) &&
804 !regulator_is_enabled(fbi->lcd_pwr))
805 return FB_BLANK_POWERDOWN;
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400806
Uwe Kleine-König46ffe102016-05-04 11:43:16 +0200807 return FB_BLANK_UNBLANK;
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400808}
809
Uwe Kleine-König5c694f82020-03-23 22:16:26 +0100810static int imxfb_regulator_set(struct imxfb_info *fbi, int enable)
811{
812 int ret;
813
814 if (enable == fbi->lcd_pwr_enabled)
815 return 0;
816
817 if (enable)
818 ret = regulator_enable(fbi->lcd_pwr);
819 else
820 ret = regulator_disable(fbi->lcd_pwr);
821
822 if (ret == 0)
823 fbi->lcd_pwr_enabled = enable;
824
825 return ret;
826}
827
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400828static int imxfb_lcd_set_power(struct lcd_device *lcddev, int power)
829{
830 struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
831
Uwe Kleine-König5c694f82020-03-23 22:16:26 +0100832 if (!IS_ERR(fbi->lcd_pwr))
833 return imxfb_regulator_set(fbi, power == FB_BLANK_UNBLANK);
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400834
835 return 0;
836}
837
838static struct lcd_ops imxfb_lcd_ops = {
839 .check_fb = imxfb_lcd_check_fb,
Alexander Shiyanf590f992014-02-15 09:56:25 +0400840 .get_contrast = imxfb_lcd_get_contrast,
841 .set_contrast = imxfb_lcd_set_contrast,
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400842 .get_power = imxfb_lcd_get_power,
843 .set_power = imxfb_lcd_set_power,
844};
845
Alexander Shiyande501362014-02-15 09:56:24 +0400846static int imxfb_setup(void)
847{
848 char *opt, *options = NULL;
849
850 if (fb_get_options("imxfb", &options))
851 return -ENODEV;
852
853 if (!options || !*options)
854 return 0;
855
856 while ((opt = strsep(&options, ",")) != NULL) {
857 if (!*opt)
858 continue;
859 else
860 fb_mode = opt;
861 }
862
863 return 0;
864}
865
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200866static int imxfb_probe(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700867{
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700868 struct imxfb_info *fbi;
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +0400869 struct lcd_device *lcd;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700870 struct fb_info *info;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700871 struct resource *res;
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200872 struct imx_fb_videomode *m;
873 const struct of_device_id *of_id;
Uwe Kleine-Könige948d322022-07-23 19:57:17 +0200874 struct device_node *display_np;
Sascha Hauer343684f2009-03-19 08:25:41 +0100875 int ret, i;
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200876 int bytes_per_pixel;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700877
Sascha Hauerd6b51502009-06-29 11:41:09 +0200878 dev_info(&pdev->dev, "i.MX Framebuffer driver\n");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700879
Alexander Shiyande501362014-02-15 09:56:24 +0400880 ret = imxfb_setup();
881 if (ret < 0)
882 return ret;
883
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200884 of_id = of_match_device(imxfb_of_dev_id, &pdev->dev);
885 if (of_id)
886 pdev->id_entry = of_id->data;
887
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700888 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sascha Hauer66c87192008-12-16 11:44:08 +0100889 if (!res)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700890 return -ENODEV;
891
Russell King3ae5eae2005-11-09 22:32:44 +0000892 info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100893 if (!info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700894 return -ENOMEM;
895
896 fbi = info->par;
897
Russell King3ae5eae2005-11-09 22:32:44 +0000898 platform_set_drvdata(pdev, info);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700899
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100900 ret = imxfb_init_fbinfo(pdev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100901 if (ret < 0)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700902 goto failed_init;
903
Uwe Kleine-Könige948d322022-07-23 19:57:17 +0200904 fb_mode = NULL;
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200905
Uwe Kleine-Könige948d322022-07-23 19:57:17 +0200906 display_np = of_parse_phandle(pdev->dev.of_node, "display", 0);
907 if (!display_np) {
908 dev_err(&pdev->dev, "No display defined in devicetree\n");
909 ret = -EINVAL;
910 goto failed_of_parse;
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200911 }
912
Uwe Kleine-Könige948d322022-07-23 19:57:17 +0200913 /*
914 * imxfb does not support more modes, we choose only the native
915 * mode.
916 */
917 fbi->num_modes = 1;
918
919 fbi->mode = devm_kzalloc(&pdev->dev,
920 sizeof(struct imx_fb_videomode), GFP_KERNEL);
921 if (!fbi->mode) {
922 ret = -ENOMEM;
923 of_node_put(display_np);
924 goto failed_of_parse;
925 }
926
927 ret = imxfb_of_read_mode(&pdev->dev, display_np, fbi->mode);
928 of_node_put(display_np);
929 if (ret)
930 goto failed_of_parse;
931
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200932 /* Calculate maximum bytes used per pixel. In most cases this should
933 * be the same as m->bpp/8 */
934 m = &fbi->mode[0];
935 bytes_per_pixel = (m->bpp + 7) / 8;
936 for (i = 0; i < fbi->num_modes; i++, m++)
937 info->fix.smem_len = max_t(size_t, info->fix.smem_len,
938 m->mode.xres * m->mode.yres * bytes_per_pixel);
939
Sascha Hauer13aaea02012-03-07 09:30:36 +0100940 fbi->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
941 if (IS_ERR(fbi->clk_ipg)) {
942 ret = PTR_ERR(fbi->clk_ipg);
943 goto failed_getclock;
944 }
945
Fabio Estevamb82fe6d2016-01-19 11:10:57 -0200946 /*
947 * The LCDC controller does not have an enable bit. The
948 * controller starts directly when the clocks are enabled.
949 * If the clocks are enabled when the controller is not yet
950 * programmed with proper register values (enabled at the
951 * bootloader, for example) then it just goes into some undefined
952 * state.
953 * To avoid this issue, let's enable and disable LCDC IPG clock
954 * so that we force some kind of 'reset' to the LCDC block.
955 */
956 ret = clk_prepare_enable(fbi->clk_ipg);
957 if (ret)
958 goto failed_getclock;
959 clk_disable_unprepare(fbi->clk_ipg);
960
Sascha Hauer13aaea02012-03-07 09:30:36 +0100961 fbi->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
962 if (IS_ERR(fbi->clk_ahb)) {
963 ret = PTR_ERR(fbi->clk_ahb);
964 goto failed_getclock;
965 }
966
967 fbi->clk_per = devm_clk_get(&pdev->dev, "per");
968 if (IS_ERR(fbi->clk_per)) {
969 ret = PTR_ERR(fbi->clk_per);
Sascha Hauerf909ef62009-01-15 15:21:00 +0100970 goto failed_getclock;
971 }
972
Uwe Kleine-Königb083c222022-07-23 19:57:20 +0200973 fbi->regs = devm_ioremap_resource(&pdev->dev, res);
Yang Yingliangc5c75022022-07-29 10:41:34 +0800974 if (IS_ERR(fbi->regs)) {
Yang Yingliangc5c75022022-07-29 10:41:34 +0800975 ret = PTR_ERR(fbi->regs);
Juergen Beisert72330b0e2008-12-16 11:44:07 +0100976 goto failed_ioremap;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700977 }
978
Alexander Shiyanb7d2d372014-02-15 09:56:23 +0400979 fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
Bartlomiej Zolnierkiewicz739a6432019-06-21 13:04:39 +0200980 info->screen_buffer = dma_alloc_wc(&pdev->dev, fbi->map_size,
981 &fbi->map_dma, GFP_KERNEL);
Wei Yongjun732146a2019-07-05 17:42:13 +0200982 if (!info->screen_buffer) {
Christophe JAILLET767d7242021-05-06 20:57:05 +0200983 dev_err(&pdev->dev, "Failed to allocate video RAM\n");
Alexander Shiyanb7d2d372014-02-15 09:56:23 +0400984 ret = -ENOMEM;
985 goto failed_map;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700986 }
987
Alexander Shiyanb7d2d372014-02-15 09:56:23 +0400988 info->fix.smem_start = fbi->map_dma;
989
Sascha Hauer343684f2009-03-19 08:25:41 +0100990 INIT_LIST_HEAD(&info->modelist);
Markus Pargmann1b6c7932013-05-26 12:35:38 +0200991 for (i = 0; i < fbi->num_modes; i++)
992 fb_add_videomode(&fbi->mode[i].mode, &info->modelist);
Sascha Hauer343684f2009-03-19 08:25:41 +0100993
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700994 /*
995 * This makes sure that our colour bitfield
996 * descriptors are correctly initialised.
997 */
998 imxfb_check_var(&info->var, info);
999
Martin Kaiserdc312122017-01-11 17:09:50 +01001000 /*
1001 * For modes > 8bpp, the color map is bypassed.
1002 * Therefore, 256 entries are enough.
1003 */
1004 ret = fb_alloc_cmap(&info->cmap, 256, 0);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001005 if (ret < 0)
1006 goto failed_cmap;
1007
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001008 imxfb_set_par(info);
1009 ret = register_framebuffer(info);
1010 if (ret < 0) {
Pavel Pisaf99c8922006-01-07 10:44:32 +00001011 dev_err(&pdev->dev, "failed to register framebuffer\n");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001012 goto failed_register;
1013 }
1014
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +04001015 fbi->lcd_pwr = devm_regulator_get(&pdev->dev, "lcd");
Masahiro Yamada45586c72020-02-03 17:37:45 -08001016 if (PTR_ERR(fbi->lcd_pwr) == -EPROBE_DEFER) {
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +04001017 ret = -EPROBE_DEFER;
1018 goto failed_lcd;
1019 }
1020
1021 lcd = devm_lcd_device_register(&pdev->dev, "imxfb-lcd", &pdev->dev, fbi,
1022 &imxfb_lcd_ops);
1023 if (IS_ERR(lcd)) {
1024 ret = PTR_ERR(lcd);
1025 goto failed_lcd;
1026 }
1027
Alexander Shiyanf590f992014-02-15 09:56:25 +04001028 lcd->props.max_contrast = 0xff;
1029
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001030 imxfb_enable_controller(fbi);
Eric Bénard7a2bb232010-07-16 15:09:07 +02001031 fbi->pdev = pdev;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001032
1033 return 0;
1034
Alexander Shiyan9fe21fd2013-12-21 15:08:00 +04001035failed_lcd:
1036 unregister_framebuffer(info);
1037
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001038failed_register:
1039 fb_dealloc_cmap(&info->cmap);
1040failed_cmap:
Bartlomiej Zolnierkiewicz739a6432019-06-21 13:04:39 +02001041 dma_free_wc(&pdev->dev, fbi->map_size, info->screen_buffer,
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -08001042 fbi->map_dma);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001043failed_map:
Juergen Beisert72330b0e2008-12-16 11:44:07 +01001044failed_ioremap:
Julia Lawall609d3bb2011-06-01 17:10:11 +00001045failed_getclock:
Sascha Hauerd6b51502009-06-29 11:41:09 +02001046 release_mem_region(res->start, resource_size(res));
Markus Pargmann1b6c7932013-05-26 12:35:38 +02001047failed_of_parse:
Juergen Beisert72330b0e2008-12-16 11:44:07 +01001048 kfree(info->pseudo_palette);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001049failed_init:
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001050 framebuffer_release(info);
1051 return ret;
1052}
1053
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001054static int imxfb_remove(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001055{
Russell King3ae5eae2005-11-09 22:32:44 +00001056 struct fb_info *info = platform_get_drvdata(pdev);
Sascha Hauer772a9e632005-07-17 20:15:36 +01001057 struct imxfb_info *fbi = info->par;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001058
Sascha Hauer772a9e632005-07-17 20:15:36 +01001059 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001060
1061 unregister_framebuffer(info);
Dan Carpenter5ae29642017-07-31 18:45:41 +02001062 fb_dealloc_cmap(&info->cmap);
Bartlomiej Zolnierkiewicz739a6432019-06-21 13:04:39 +02001063 dma_free_wc(&pdev->dev, fbi->map_size, info->screen_buffer,
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -08001064 fbi->map_dma);
Dan Carpenter5ae29642017-07-31 18:45:41 +02001065 kfree(info->pseudo_palette);
1066 framebuffer_release(info);
Sascha Hauerf909ef62009-01-15 15:21:00 +01001067
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001068 return 0;
1069}
1070
Alexander Shiyanf1d2ff82014-02-15 09:56:26 +04001071static int __maybe_unused imxfb_suspend(struct device *dev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001072{
Alexander Shiyanf1d2ff82014-02-15 09:56:26 +04001073 struct fb_info *info = dev_get_drvdata(dev);
Sascha Hauer772a9e632005-07-17 20:15:36 +01001074 struct imxfb_info *fbi = info->par;
Alexander Shiyanf1d2ff82014-02-15 09:56:26 +04001075
Sascha Hauer772a9e632005-07-17 20:15:36 +01001076 imxfb_disable_controller(fbi);
Alexander Shiyanf1d2ff82014-02-15 09:56:26 +04001077
1078 return 0;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001079}
1080
Alexander Shiyanf1d2ff82014-02-15 09:56:26 +04001081static int __maybe_unused imxfb_resume(struct device *dev)
1082{
1083 struct fb_info *info = dev_get_drvdata(dev);
1084 struct imxfb_info *fbi = info->par;
1085
1086 imxfb_enable_controller(fbi);
1087
1088 return 0;
1089}
1090
1091static SIMPLE_DEV_PM_OPS(imxfb_pm_ops, imxfb_suspend, imxfb_resume);
1092
Russell King3ae5eae2005-11-09 22:32:44 +00001093static struct platform_driver imxfb_driver = {
Russell King3ae5eae2005-11-09 22:32:44 +00001094 .driver = {
Juergen Beisert72330b0e2008-12-16 11:44:07 +01001095 .name = DRIVER_NAME,
Markus Pargmann1b6c7932013-05-26 12:35:38 +02001096 .of_match_table = imxfb_of_dev_id,
Alexander Shiyanf1d2ff82014-02-15 09:56:26 +04001097 .pm = &imxfb_pm_ops,
Russell King3ae5eae2005-11-09 22:32:44 +00001098 },
Alexander Shiyanf1d2ff82014-02-15 09:56:26 +04001099 .probe = imxfb_probe,
1100 .remove = imxfb_remove,
Shawn Guoe69dc9a2012-09-16 19:59:53 +08001101 .id_table = imxfb_devtype,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001102};
Alexander Shiyande501362014-02-15 09:56:24 +04001103module_platform_driver(imxfb_driver);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001104
Fabio Estevame3d5fb72011-01-10 09:47:41 -02001105MODULE_DESCRIPTION("Freescale i.MX framebuffer driver");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001106MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1107MODULE_LICENSE("GPL");