blob: 12c8bd1d24d5321af7106c3738eeffbee056b25d [file] [log] [blame]
Sascha Hauerf0a523b2011-01-14 15:22:31 +01001/*
2 * Copyright (C) 2010 Juergen Beisert, Pengutronix
3 *
4 * This code is based on:
5 * Author: Vitaly Wool <vital@embeddedalley.com>
6 *
7 * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
8 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#define DRIVER_NAME "mxsfb"
21
22/**
23 * @file
24 * @brief LCDIF driver for i.MX23 and i.MX28
25 *
26 * The LCDIF support four modes of operation
27 * - MPU interface (to drive smart displays) -> not supported yet
28 * - VSYNC interface (like MPU interface plus Vsync) -> not supported yet
29 * - Dotclock interface (to drive LC displays with RGB data and sync signals)
30 * - DVI (to drive ITU-R BT656) -> not supported yet
31 *
32 * This driver depends on a correct setup of the pins used for this purpose
33 * (platform specific).
34 *
35 * For the developer: Don't forget to set the data bus width to the display
36 * in the imx_fb_videomode structure. You will else end up with ugly colours.
37 * If you fight against jitter you can vary the clock delay. This is a feature
38 * of the i.MX28 and you can vary it between 2 ns ... 8 ns in 2 ns steps. Give
39 * the required value in the imx_fb_videomode structure.
40 */
41
Axel Lin36893672011-09-01 08:11:59 +080042#include <linux/module.h>
Sascha Hauerf0a523b2011-01-14 15:22:31 +010043#include <linux/kernel.h>
Shawn Guo73fc6102012-06-25 19:54:35 +080044#include <linux/of_device.h>
Sascha Hauerf0a523b2011-01-14 15:22:31 +010045#include <linux/platform_device.h>
46#include <linux/clk.h>
47#include <linux/dma-mapping.h>
48#include <linux/io.h>
Shawn Guoc8b5cfc2013-03-14 13:21:56 +080049#include <linux/fb.h>
Fabio Estevam43444292013-04-07 15:44:59 -030050#include <linux/regulator/consumer.h>
Fabio Estevamd7321df2013-05-08 21:05:55 +080051#include <video/of_display_timing.h>
Lothar Waßmann838bdf72014-01-06 14:17:58 +010052#include <video/of_videomode.h>
Shawn Guo66940652013-03-14 10:57:34 +080053#include <video/videomode.h>
Sascha Hauerf0a523b2011-01-14 15:22:31 +010054
55#define REG_SET 4
56#define REG_CLR 8
57
58#define LCDC_CTRL 0x00
59#define LCDC_CTRL1 0x10
60#define LCDC_V4_CTRL2 0x20
61#define LCDC_V3_TRANSFER_COUNT 0x20
62#define LCDC_V4_TRANSFER_COUNT 0x30
63#define LCDC_V4_CUR_BUF 0x40
64#define LCDC_V4_NEXT_BUF 0x50
65#define LCDC_V3_CUR_BUF 0x30
66#define LCDC_V3_NEXT_BUF 0x40
67#define LCDC_TIMING 0x60
68#define LCDC_VDCTRL0 0x70
69#define LCDC_VDCTRL1 0x80
70#define LCDC_VDCTRL2 0x90
71#define LCDC_VDCTRL3 0xa0
72#define LCDC_VDCTRL4 0xb0
73#define LCDC_DVICTRL0 0xc0
74#define LCDC_DVICTRL1 0xd0
75#define LCDC_DVICTRL2 0xe0
76#define LCDC_DVICTRL3 0xf0
77#define LCDC_DVICTRL4 0x100
78#define LCDC_V4_DATA 0x180
79#define LCDC_V3_DATA 0x1b0
80#define LCDC_V4_DEBUG0 0x1d0
81#define LCDC_V3_DEBUG0 0x1f0
82
83#define CTRL_SFTRST (1 << 31)
84#define CTRL_CLKGATE (1 << 30)
85#define CTRL_BYPASS_COUNT (1 << 19)
86#define CTRL_VSYNC_MODE (1 << 18)
87#define CTRL_DOTCLK_MODE (1 << 17)
88#define CTRL_DATA_SELECT (1 << 16)
89#define CTRL_SET_BUS_WIDTH(x) (((x) & 0x3) << 10)
90#define CTRL_GET_BUS_WIDTH(x) (((x) >> 10) & 0x3)
91#define CTRL_SET_WORD_LENGTH(x) (((x) & 0x3) << 8)
92#define CTRL_GET_WORD_LENGTH(x) (((x) >> 8) & 0x3)
93#define CTRL_MASTER (1 << 5)
94#define CTRL_DF16 (1 << 3)
95#define CTRL_DF18 (1 << 2)
96#define CTRL_DF24 (1 << 1)
97#define CTRL_RUN (1 << 0)
98
99#define CTRL1_FIFO_CLEAR (1 << 21)
100#define CTRL1_SET_BYTE_PACKAGING(x) (((x) & 0xf) << 16)
101#define CTRL1_GET_BYTE_PACKAGING(x) (((x) >> 16) & 0xf)
102
103#define TRANSFER_COUNT_SET_VCOUNT(x) (((x) & 0xffff) << 16)
104#define TRANSFER_COUNT_GET_VCOUNT(x) (((x) >> 16) & 0xffff)
105#define TRANSFER_COUNT_SET_HCOUNT(x) ((x) & 0xffff)
106#define TRANSFER_COUNT_GET_HCOUNT(x) ((x) & 0xffff)
107
108
109#define VDCTRL0_ENABLE_PRESENT (1 << 28)
110#define VDCTRL0_VSYNC_ACT_HIGH (1 << 27)
111#define VDCTRL0_HSYNC_ACT_HIGH (1 << 26)
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800112#define VDCTRL0_DOTCLK_ACT_FALLING (1 << 25)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100113#define VDCTRL0_ENABLE_ACT_HIGH (1 << 24)
114#define VDCTRL0_VSYNC_PERIOD_UNIT (1 << 21)
115#define VDCTRL0_VSYNC_PULSE_WIDTH_UNIT (1 << 20)
116#define VDCTRL0_HALF_LINE (1 << 19)
117#define VDCTRL0_HALF_LINE_MODE (1 << 18)
118#define VDCTRL0_SET_VSYNC_PULSE_WIDTH(x) ((x) & 0x3ffff)
119#define VDCTRL0_GET_VSYNC_PULSE_WIDTH(x) ((x) & 0x3ffff)
120
121#define VDCTRL2_SET_HSYNC_PERIOD(x) ((x) & 0x3ffff)
122#define VDCTRL2_GET_HSYNC_PERIOD(x) ((x) & 0x3ffff)
123
124#define VDCTRL3_MUX_SYNC_SIGNALS (1 << 29)
125#define VDCTRL3_VSYNC_ONLY (1 << 28)
126#define SET_HOR_WAIT_CNT(x) (((x) & 0xfff) << 16)
127#define GET_HOR_WAIT_CNT(x) (((x) >> 16) & 0xfff)
128#define SET_VERT_WAIT_CNT(x) ((x) & 0xffff)
129#define GET_VERT_WAIT_CNT(x) ((x) & 0xffff)
130
131#define VDCTRL4_SET_DOTCLK_DLY(x) (((x) & 0x7) << 29) /* v4 only */
132#define VDCTRL4_GET_DOTCLK_DLY(x) (((x) >> 29) & 0x7) /* v4 only */
133#define VDCTRL4_SYNC_SIGNALS_ON (1 << 18)
134#define SET_DOTCLK_H_VALID_DATA_CNT(x) ((x) & 0x3ffff)
135
136#define DEBUG0_HSYNC (1 < 26)
137#define DEBUG0_VSYNC (1 < 25)
138
139#define MIN_XRES 120
140#define MIN_YRES 120
141
142#define RED 0
143#define GREEN 1
144#define BLUE 2
145#define TRANSP 3
146
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800147#define STMLCDIF_8BIT 1 /** pixel data bus to the display is of 8 bit width */
148#define STMLCDIF_16BIT 0 /** pixel data bus to the display is of 16 bit width */
149#define STMLCDIF_18BIT 2 /** pixel data bus to the display is of 18 bit width */
150#define STMLCDIF_24BIT 3 /** pixel data bus to the display is of 24 bit width */
151
152#define MXSFB_SYNC_DATA_ENABLE_HIGH_ACT (1 << 6)
Stefan Agner2d814822017-11-09 18:09:30 +0100153#define MXSFB_SYNC_DOTCLK_FALLING_ACT (1 << 7) /* negative edge sampling */
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800154
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100155enum mxsfb_devtype {
156 MXSFB_V3,
157 MXSFB_V4,
158};
159
160/* CPU dependent register offsets */
161struct mxsfb_devdata {
162 unsigned transfer_count;
163 unsigned cur_buf;
164 unsigned next_buf;
165 unsigned debug0;
166 unsigned hs_wdth_mask;
167 unsigned hs_wdth_shift;
168 unsigned ipversion;
169};
170
171struct mxsfb_info {
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100172 struct platform_device *pdev;
173 struct clk *clk;
Fabio Estevamd301a5a2014-10-25 11:28:47 -0200174 struct clk *clk_axi;
175 struct clk *clk_disp_axi;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100176 void __iomem *base; /* registers */
177 unsigned allocated_size;
178 int enabled;
179 unsigned ld_intf_width;
180 unsigned dotclk_delay;
181 const struct mxsfb_devdata *devdata;
Marek Vasut6a150752013-03-18 19:24:02 +0100182 u32 sync;
Fabio Estevam43444292013-04-07 15:44:59 -0300183 struct regulator *reg_lcd;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100184};
185
186#define mxsfb_is_v3(host) (host->devdata->ipversion == 3)
187#define mxsfb_is_v4(host) (host->devdata->ipversion == 4)
188
189static const struct mxsfb_devdata mxsfb_devdata[] = {
190 [MXSFB_V3] = {
191 .transfer_count = LCDC_V3_TRANSFER_COUNT,
192 .cur_buf = LCDC_V3_CUR_BUF,
193 .next_buf = LCDC_V3_NEXT_BUF,
194 .debug0 = LCDC_V3_DEBUG0,
195 .hs_wdth_mask = 0xff,
196 .hs_wdth_shift = 24,
197 .ipversion = 3,
198 },
199 [MXSFB_V4] = {
200 .transfer_count = LCDC_V4_TRANSFER_COUNT,
201 .cur_buf = LCDC_V4_CUR_BUF,
202 .next_buf = LCDC_V4_NEXT_BUF,
203 .debug0 = LCDC_V4_DEBUG0,
204 .hs_wdth_mask = 0x3fff,
205 .hs_wdth_shift = 18,
206 .ipversion = 4,
207 },
208};
209
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100210/* mask and shift depends on architecture */
211static inline u32 set_hsync_pulse_width(struct mxsfb_info *host, unsigned val)
212{
213 return (val & host->devdata->hs_wdth_mask) <<
214 host->devdata->hs_wdth_shift;
215}
216
217static inline u32 get_hsync_pulse_width(struct mxsfb_info *host, unsigned val)
218{
219 return (val >> host->devdata->hs_wdth_shift) &
220 host->devdata->hs_wdth_mask;
221}
222
223static const struct fb_bitfield def_rgb565[] = {
224 [RED] = {
225 .offset = 11,
226 .length = 5,
227 },
228 [GREEN] = {
229 .offset = 5,
230 .length = 6,
231 },
232 [BLUE] = {
233 .offset = 0,
234 .length = 5,
235 },
236 [TRANSP] = { /* no support for transparency */
237 .length = 0,
238 }
239};
240
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100241static const struct fb_bitfield def_rgb888[] = {
242 [RED] = {
243 .offset = 16,
244 .length = 8,
245 },
246 [GREEN] = {
247 .offset = 8,
248 .length = 8,
249 },
250 [BLUE] = {
251 .offset = 0,
252 .length = 8,
253 },
254 [TRANSP] = { /* no support for transparency */
255 .length = 0,
256 }
257};
258
259static inline unsigned chan_to_field(unsigned chan, struct fb_bitfield *bf)
260{
261 chan &= 0xffff;
262 chan >>= 16 - bf->length;
263 return chan << bf->offset;
264}
265
266static int mxsfb_check_var(struct fb_var_screeninfo *var,
267 struct fb_info *fb_info)
268{
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100269 struct mxsfb_info *host = fb_info->par;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100270 const struct fb_bitfield *rgb = NULL;
271
272 if (var->xres < MIN_XRES)
273 var->xres = MIN_XRES;
274 if (var->yres < MIN_YRES)
275 var->yres = MIN_YRES;
276
277 var->xres_virtual = var->xres;
278
279 var->yres_virtual = var->yres;
280
281 switch (var->bits_per_pixel) {
282 case 16:
283 /* always expect RGB 565 */
284 rgb = def_rgb565;
285 break;
286 case 32:
287 switch (host->ld_intf_width) {
288 case STMLCDIF_8BIT:
289 pr_debug("Unsupported LCD bus width mapping\n");
290 break;
291 case STMLCDIF_16BIT:
292 case STMLCDIF_18BIT:
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100293 case STMLCDIF_24BIT:
294 /* real 24 bit */
295 rgb = def_rgb888;
296 break;
297 }
298 break;
299 default:
Lothar Waßmann18dd44d2014-01-06 14:17:57 +0100300 pr_err("Unsupported colour depth: %u\n", var->bits_per_pixel);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100301 return -EINVAL;
302 }
303
304 /*
305 * Copy the RGB parameters for this display
306 * from the machine specific parameters.
307 */
308 var->red = rgb[RED];
309 var->green = rgb[GREEN];
310 var->blue = rgb[BLUE];
311 var->transp = rgb[TRANSP];
312
313 return 0;
314}
315
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800316static inline void mxsfb_enable_axi_clk(struct mxsfb_info *host)
317{
318 if (host->clk_axi)
319 clk_prepare_enable(host->clk_axi);
320}
321
322static inline void mxsfb_disable_axi_clk(struct mxsfb_info *host)
323{
324 if (host->clk_axi)
325 clk_disable_unprepare(host->clk_axi);
326}
327
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100328static void mxsfb_enable_controller(struct fb_info *fb_info)
329{
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100330 struct mxsfb_info *host = fb_info->par;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100331 u32 reg;
Fabio Estevam43444292013-04-07 15:44:59 -0300332 int ret;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100333
334 dev_dbg(&host->pdev->dev, "%s\n", __func__);
335
Fabio Estevam43444292013-04-07 15:44:59 -0300336 if (host->reg_lcd) {
337 ret = regulator_enable(host->reg_lcd);
338 if (ret) {
339 dev_err(&host->pdev->dev,
340 "lcd regulator enable failed: %d\n", ret);
341 return;
342 }
343 }
344
Fabio Estevamd301a5a2014-10-25 11:28:47 -0200345 if (host->clk_disp_axi)
346 clk_prepare_enable(host->clk_disp_axi);
Shawn Guoca4c22d32011-12-20 14:12:54 +0800347 clk_prepare_enable(host->clk);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100348 clk_set_rate(host->clk, PICOS2KHZ(fb_info->var.pixclock) * 1000U);
349
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800350 mxsfb_enable_axi_clk(host);
351
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100352 /* if it was disabled, re-enable the mode again */
353 writel(CTRL_DOTCLK_MODE, host->base + LCDC_CTRL + REG_SET);
354
355 /* enable the SYNC signals first, then the DMA engine */
356 reg = readl(host->base + LCDC_VDCTRL4);
357 reg |= VDCTRL4_SYNC_SIGNALS_ON;
358 writel(reg, host->base + LCDC_VDCTRL4);
359
360 writel(CTRL_RUN, host->base + LCDC_CTRL + REG_SET);
361
362 host->enabled = 1;
363}
364
365static void mxsfb_disable_controller(struct fb_info *fb_info)
366{
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100367 struct mxsfb_info *host = fb_info->par;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100368 unsigned loop;
369 u32 reg;
Fabio Estevam43444292013-04-07 15:44:59 -0300370 int ret;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100371
372 dev_dbg(&host->pdev->dev, "%s\n", __func__);
373
374 /*
375 * Even if we disable the controller here, it will still continue
376 * until its FIFOs are running out of data
377 */
378 writel(CTRL_DOTCLK_MODE, host->base + LCDC_CTRL + REG_CLR);
379
380 loop = 1000;
381 while (loop) {
382 reg = readl(host->base + LCDC_CTRL);
383 if (!(reg & CTRL_RUN))
384 break;
385 loop--;
386 }
387
Lothar Waßmann6c1ecba2012-11-22 13:49:14 +0100388 reg = readl(host->base + LCDC_VDCTRL4);
389 writel(reg & ~VDCTRL4_SYNC_SIGNALS_ON, host->base + LCDC_VDCTRL4);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100390
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800391 mxsfb_disable_axi_clk(host);
392
Shawn Guoca4c22d32011-12-20 14:12:54 +0800393 clk_disable_unprepare(host->clk);
Fabio Estevamd301a5a2014-10-25 11:28:47 -0200394 if (host->clk_disp_axi)
395 clk_disable_unprepare(host->clk_disp_axi);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100396
397 host->enabled = 0;
Fabio Estevam43444292013-04-07 15:44:59 -0300398
399 if (host->reg_lcd) {
400 ret = regulator_disable(host->reg_lcd);
401 if (ret)
402 dev_err(&host->pdev->dev,
403 "lcd regulator disable failed: %d\n", ret);
404 }
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100405}
406
407static int mxsfb_set_par(struct fb_info *fb_info)
408{
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100409 struct mxsfb_info *host = fb_info->par;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100410 u32 ctrl, vdctrl0, vdctrl4;
411 int line_size, fb_size;
412 int reenable = 0;
413
414 line_size = fb_info->var.xres * (fb_info->var.bits_per_pixel >> 3);
415 fb_size = fb_info->var.yres_virtual * line_size;
416
417 if (fb_size > fb_info->fix.smem_len)
418 return -ENOMEM;
419
420 fb_info->fix.line_length = line_size;
421
422 /*
423 * It seems, you can't re-program the controller if it is still running.
424 * This may lead into shifted pictures (FIFO issue?).
425 * So, first stop the controller and drain its FIFOs
426 */
427 if (host->enabled) {
428 reenable = 1;
429 mxsfb_disable_controller(fb_info);
430 }
431
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800432 mxsfb_enable_axi_clk(host);
433
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100434 /* clear the FIFOs */
435 writel(CTRL1_FIFO_CLEAR, host->base + LCDC_CTRL1 + REG_SET);
436
437 ctrl = CTRL_BYPASS_COUNT | CTRL_MASTER |
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700438 CTRL_SET_BUS_WIDTH(host->ld_intf_width);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100439
440 switch (fb_info->var.bits_per_pixel) {
441 case 16:
442 dev_dbg(&host->pdev->dev, "Setting up RGB565 mode\n");
443 ctrl |= CTRL_SET_WORD_LENGTH(0);
444 writel(CTRL1_SET_BYTE_PACKAGING(0xf), host->base + LCDC_CTRL1);
445 break;
446 case 32:
447 dev_dbg(&host->pdev->dev, "Setting up RGB888/666 mode\n");
448 ctrl |= CTRL_SET_WORD_LENGTH(3);
449 switch (host->ld_intf_width) {
450 case STMLCDIF_8BIT:
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800451 mxsfb_disable_axi_clk(host);
Lothar Waßmann18dd44d2014-01-06 14:17:57 +0100452 dev_err(&host->pdev->dev,
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100453 "Unsupported LCD bus width mapping\n");
454 return -EINVAL;
455 case STMLCDIF_16BIT:
456 case STMLCDIF_18BIT:
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100457 case STMLCDIF_24BIT:
458 /* real 24 bit */
459 break;
460 }
461 /* do not use packed pixels = one pixel per word instead */
462 writel(CTRL1_SET_BYTE_PACKAGING(0x7), host->base + LCDC_CTRL1);
463 break;
464 default:
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800465 mxsfb_disable_axi_clk(host);
Lothar Waßmann18dd44d2014-01-06 14:17:57 +0100466 dev_err(&host->pdev->dev, "Unhandled color depth of %u\n",
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100467 fb_info->var.bits_per_pixel);
468 return -EINVAL;
469 }
470
471 writel(ctrl, host->base + LCDC_CTRL);
472
473 writel(TRANSFER_COUNT_SET_VCOUNT(fb_info->var.yres) |
474 TRANSFER_COUNT_SET_HCOUNT(fb_info->var.xres),
475 host->base + host->devdata->transfer_count);
476
477 vdctrl0 = VDCTRL0_ENABLE_PRESENT | /* always in DOTCLOCK mode */
478 VDCTRL0_VSYNC_PERIOD_UNIT |
479 VDCTRL0_VSYNC_PULSE_WIDTH_UNIT |
480 VDCTRL0_SET_VSYNC_PULSE_WIDTH(fb_info->var.vsync_len);
481 if (fb_info->var.sync & FB_SYNC_HOR_HIGH_ACT)
482 vdctrl0 |= VDCTRL0_HSYNC_ACT_HIGH;
483 if (fb_info->var.sync & FB_SYNC_VERT_HIGH_ACT)
484 vdctrl0 |= VDCTRL0_VSYNC_ACT_HIGH;
Marek Vasut6a150752013-03-18 19:24:02 +0100485 if (host->sync & MXSFB_SYNC_DATA_ENABLE_HIGH_ACT)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100486 vdctrl0 |= VDCTRL0_ENABLE_ACT_HIGH;
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800487 if (host->sync & MXSFB_SYNC_DOTCLK_FALLING_ACT)
488 vdctrl0 |= VDCTRL0_DOTCLK_ACT_FALLING;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100489
490 writel(vdctrl0, host->base + LCDC_VDCTRL0);
491
492 /* frame length in lines */
493 writel(fb_info->var.upper_margin + fb_info->var.vsync_len +
494 fb_info->var.lower_margin + fb_info->var.yres,
495 host->base + LCDC_VDCTRL1);
496
497 /* line length in units of clocks or pixels */
498 writel(set_hsync_pulse_width(host, fb_info->var.hsync_len) |
499 VDCTRL2_SET_HSYNC_PERIOD(fb_info->var.left_margin +
500 fb_info->var.hsync_len + fb_info->var.right_margin +
501 fb_info->var.xres),
502 host->base + LCDC_VDCTRL2);
503
504 writel(SET_HOR_WAIT_CNT(fb_info->var.left_margin +
505 fb_info->var.hsync_len) |
506 SET_VERT_WAIT_CNT(fb_info->var.upper_margin +
507 fb_info->var.vsync_len),
508 host->base + LCDC_VDCTRL3);
509
510 vdctrl4 = SET_DOTCLK_H_VALID_DATA_CNT(fb_info->var.xres);
511 if (mxsfb_is_v4(host))
512 vdctrl4 |= VDCTRL4_SET_DOTCLK_DLY(host->dotclk_delay);
513 writel(vdctrl4, host->base + LCDC_VDCTRL4);
514
515 writel(fb_info->fix.smem_start +
516 fb_info->fix.line_length * fb_info->var.yoffset,
517 host->base + host->devdata->next_buf);
518
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800519 mxsfb_disable_axi_clk(host);
520
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100521 if (reenable)
522 mxsfb_enable_controller(fb_info);
523
524 return 0;
525}
526
527static int mxsfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
528 u_int transp, struct fb_info *fb_info)
529{
530 unsigned int val;
531 int ret = -EINVAL;
532
533 /*
534 * If greyscale is true, then we convert the RGB value
535 * to greyscale no matter what visual we are using.
536 */
537 if (fb_info->var.grayscale)
538 red = green = blue = (19595 * red + 38470 * green +
539 7471 * blue) >> 16;
540
541 switch (fb_info->fix.visual) {
542 case FB_VISUAL_TRUECOLOR:
543 /*
544 * 12 or 16-bit True Colour. We encode the RGB value
545 * according to the RGB bitfield information.
546 */
547 if (regno < 16) {
548 u32 *pal = fb_info->pseudo_palette;
549
550 val = chan_to_field(red, &fb_info->var.red);
551 val |= chan_to_field(green, &fb_info->var.green);
552 val |= chan_to_field(blue, &fb_info->var.blue);
553
554 pal[regno] = val;
555 ret = 0;
556 }
557 break;
558
559 case FB_VISUAL_STATIC_PSEUDOCOLOR:
560 case FB_VISUAL_PSEUDOCOLOR:
561 break;
562 }
563
564 return ret;
565}
566
567static int mxsfb_blank(int blank, struct fb_info *fb_info)
568{
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100569 struct mxsfb_info *host = fb_info->par;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100570
571 switch (blank) {
572 case FB_BLANK_POWERDOWN:
573 case FB_BLANK_VSYNC_SUSPEND:
574 case FB_BLANK_HSYNC_SUSPEND:
575 case FB_BLANK_NORMAL:
576 if (host->enabled)
577 mxsfb_disable_controller(fb_info);
578 break;
579
580 case FB_BLANK_UNBLANK:
581 if (!host->enabled)
582 mxsfb_enable_controller(fb_info);
583 break;
584 }
585 return 0;
586}
587
588static int mxsfb_pan_display(struct fb_var_screeninfo *var,
589 struct fb_info *fb_info)
590{
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100591 struct mxsfb_info *host = fb_info->par;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100592 unsigned offset;
593
594 if (var->xoffset != 0)
595 return -EINVAL;
596
597 offset = fb_info->fix.line_length * var->yoffset;
598
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800599 mxsfb_enable_axi_clk(host);
600
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100601 /* update on next VSYNC */
602 writel(fb_info->fix.smem_start + offset,
603 host->base + host->devdata->next_buf);
604
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800605 mxsfb_disable_axi_clk(host);
606
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100607 return 0;
608}
609
610static struct fb_ops mxsfb_ops = {
611 .owner = THIS_MODULE,
612 .fb_check_var = mxsfb_check_var,
613 .fb_set_par = mxsfb_set_par,
614 .fb_setcolreg = mxsfb_setcolreg,
615 .fb_blank = mxsfb_blank,
616 .fb_pan_display = mxsfb_pan_display,
617 .fb_fillrect = cfb_fillrect,
618 .fb_copyarea = cfb_copyarea,
619 .fb_imageblit = cfb_imageblit,
620};
621
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100622static int mxsfb_restore_mode(struct fb_info *fb_info,
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100623 struct fb_videomode *vmode)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100624{
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100625 struct mxsfb_info *host = fb_info->par;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100626 unsigned line_count;
627 unsigned period;
628 unsigned long pa, fbsize;
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800629 int bits_per_pixel, ofs, ret = 0;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100630 u32 transfer_count, vdctrl0, vdctrl2, vdctrl3, vdctrl4, ctrl;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100631
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800632 mxsfb_enable_axi_clk(host);
633
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100634 /* Only restore the mode when the controller is running */
635 ctrl = readl(host->base + LCDC_CTRL);
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800636 if (!(ctrl & CTRL_RUN)) {
637 ret = -EINVAL;
638 goto err;
639 }
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100640
641 vdctrl0 = readl(host->base + LCDC_VDCTRL0);
642 vdctrl2 = readl(host->base + LCDC_VDCTRL2);
643 vdctrl3 = readl(host->base + LCDC_VDCTRL3);
644 vdctrl4 = readl(host->base + LCDC_VDCTRL4);
645
646 transfer_count = readl(host->base + host->devdata->transfer_count);
647
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100648 vmode->xres = TRANSFER_COUNT_GET_HCOUNT(transfer_count);
649 vmode->yres = TRANSFER_COUNT_GET_VCOUNT(transfer_count);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100650
651 switch (CTRL_GET_WORD_LENGTH(ctrl)) {
652 case 0:
653 bits_per_pixel = 16;
654 break;
655 case 3:
656 bits_per_pixel = 32;
Marek Vasut6d0bb812013-09-26 13:13:58 +0200657 break;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100658 case 1:
659 default:
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800660 ret = -EINVAL;
661 goto err;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100662 }
663
664 fb_info->var.bits_per_pixel = bits_per_pixel;
665
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100666 vmode->pixclock = KHZ2PICOS(clk_get_rate(host->clk) / 1000U);
667 vmode->hsync_len = get_hsync_pulse_width(host, vdctrl2);
668 vmode->left_margin = GET_HOR_WAIT_CNT(vdctrl3) - vmode->hsync_len;
669 vmode->right_margin = VDCTRL2_GET_HSYNC_PERIOD(vdctrl2) -
670 vmode->hsync_len - vmode->left_margin - vmode->xres;
671 vmode->vsync_len = VDCTRL0_GET_VSYNC_PULSE_WIDTH(vdctrl0);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100672 period = readl(host->base + LCDC_VDCTRL1);
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100673 vmode->upper_margin = GET_VERT_WAIT_CNT(vdctrl3) - vmode->vsync_len;
674 vmode->lower_margin = period - vmode->vsync_len -
675 vmode->upper_margin - vmode->yres;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100676
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100677 vmode->vmode = FB_VMODE_NONINTERLACED;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100678
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100679 vmode->sync = 0;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100680 if (vdctrl0 & VDCTRL0_HSYNC_ACT_HIGH)
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100681 vmode->sync |= FB_SYNC_HOR_HIGH_ACT;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100682 if (vdctrl0 & VDCTRL0_VSYNC_ACT_HIGH)
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100683 vmode->sync |= FB_SYNC_VERT_HIGH_ACT;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100684
685 pr_debug("Reconstructed video mode:\n");
686 pr_debug("%dx%d, hsync: %u left: %u, right: %u, vsync: %u, upper: %u, lower: %u\n",
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100687 vmode->xres, vmode->yres, vmode->hsync_len, vmode->left_margin,
688 vmode->right_margin, vmode->vsync_len, vmode->upper_margin,
689 vmode->lower_margin);
690 pr_debug("pixclk: %ldkHz\n", PICOS2KHZ(vmode->pixclock));
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100691
692 host->ld_intf_width = CTRL_GET_BUS_WIDTH(ctrl);
693 host->dotclk_delay = VDCTRL4_GET_DOTCLK_DLY(vdctrl4);
694
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100695 fb_info->fix.line_length = vmode->xres * (bits_per_pixel >> 3);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100696
697 pa = readl(host->base + host->devdata->cur_buf);
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100698 fbsize = fb_info->fix.line_length * vmode->yres;
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800699 if (pa < fb_info->fix.smem_start) {
700 ret = -EINVAL;
701 goto err;
702 }
703 if (pa + fbsize > fb_info->fix.smem_start + fb_info->fix.smem_len) {
704 ret = -EINVAL;
705 goto err;
706 }
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100707 ofs = pa - fb_info->fix.smem_start;
708 if (ofs) {
709 memmove(fb_info->screen_base, fb_info->screen_base + ofs, fbsize);
710 writel(fb_info->fix.smem_start, host->base + host->devdata->next_buf);
711 }
712
713 line_count = fb_info->fix.smem_len / fb_info->fix.line_length;
714 fb_info->fix.ypanstep = 1;
715
Shawn Guoca4c22d32011-12-20 14:12:54 +0800716 clk_prepare_enable(host->clk);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100717 host->enabled = 1;
718
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800719err:
720 if (ret)
721 mxsfb_disable_axi_clk(host);
722
723 return ret;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100724}
725
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100726static int mxsfb_init_fbinfo_dt(struct fb_info *fb_info,
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100727 struct fb_videomode *vmode)
Shawn Guo66940652013-03-14 10:57:34 +0800728{
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100729 struct mxsfb_info *host = fb_info->par;
Shawn Guo66940652013-03-14 10:57:34 +0800730 struct fb_var_screeninfo *var = &fb_info->var;
731 struct device *dev = &host->pdev->dev;
732 struct device_node *np = host->pdev->dev.of_node;
733 struct device_node *display_np;
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100734 struct videomode vm;
Shawn Guo66940652013-03-14 10:57:34 +0800735 u32 width;
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100736 int ret;
Shawn Guo66940652013-03-14 10:57:34 +0800737
738 display_np = of_parse_phandle(np, "display", 0);
739 if (!display_np) {
740 dev_err(dev, "failed to find display phandle\n");
741 return -ENOENT;
742 }
743
744 ret = of_property_read_u32(display_np, "bus-width", &width);
745 if (ret < 0) {
746 dev_err(dev, "failed to get property bus-width\n");
747 goto put_display_node;
748 }
749
750 switch (width) {
751 case 8:
752 host->ld_intf_width = STMLCDIF_8BIT;
753 break;
754 case 16:
755 host->ld_intf_width = STMLCDIF_16BIT;
756 break;
757 case 18:
758 host->ld_intf_width = STMLCDIF_18BIT;
759 break;
760 case 24:
761 host->ld_intf_width = STMLCDIF_24BIT;
762 break;
763 default:
764 dev_err(dev, "invalid bus-width value\n");
765 ret = -EINVAL;
766 goto put_display_node;
767 }
768
769 ret = of_property_read_u32(display_np, "bits-per-pixel",
770 &var->bits_per_pixel);
771 if (ret < 0) {
772 dev_err(dev, "failed to get property bits-per-pixel\n");
773 goto put_display_node;
774 }
775
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100776 ret = of_get_videomode(display_np, &vm, OF_USE_NATIVE_MODE);
777 if (ret) {
778 dev_err(dev, "failed to get videomode from DT\n");
Shawn Guo66940652013-03-14 10:57:34 +0800779 goto put_display_node;
780 }
781
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100782 ret = fb_videomode_from_videomode(&vm, vmode);
783 if (ret < 0)
Shawn Guo66940652013-03-14 10:57:34 +0800784 goto put_display_node;
Shawn Guo66940652013-03-14 10:57:34 +0800785
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100786 if (vm.flags & DISPLAY_FLAGS_DE_HIGH)
787 host->sync |= MXSFB_SYNC_DATA_ENABLE_HIGH_ACT;
Stefan Agner2d814822017-11-09 18:09:30 +0100788
789 /*
790 * The PIXDATA flags of the display_flags enum are controller
791 * centric, e.g. NEGEDGE means drive data on negative edge.
792 * However, the drivers flag is display centric: Sample the
793 * data on negative (falling) edge. Therefore, check for the
794 * POSEDGE flag:
795 * drive on positive edge => sample on negative edge
796 */
797 if (vm.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100798 host->sync |= MXSFB_SYNC_DOTCLK_FALLING_ACT;
Shawn Guo66940652013-03-14 10:57:34 +0800799
Shawn Guo66940652013-03-14 10:57:34 +0800800put_display_node:
801 of_node_put(display_np);
802 return ret;
803}
804
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100805static int mxsfb_init_fbinfo(struct fb_info *fb_info,
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100806 struct fb_videomode *vmode)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100807{
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100808 int ret;
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100809 struct mxsfb_info *host = fb_info->par;
Marek Vasut03577d62016-08-26 17:53:35 +0200810 struct device *dev = &host->pdev->dev;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100811 struct fb_var_screeninfo *var = &fb_info->var;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100812 dma_addr_t fb_phys;
813 void *fb_virt;
Shawn Guo4aa02c72013-03-13 14:03:12 +0800814 unsigned fb_size;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100815
816 fb_info->fbops = &mxsfb_ops;
817 fb_info->flags = FBINFO_FLAG_DEFAULT | FBINFO_READS_FAST;
818 strlcpy(fb_info->fix.id, "mxs", sizeof(fb_info->fix.id));
819 fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
820 fb_info->fix.ypanstep = 1;
821 fb_info->fix.visual = FB_VISUAL_TRUECOLOR,
822 fb_info->fix.accel = FB_ACCEL_NONE;
823
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100824 ret = mxsfb_init_fbinfo_dt(fb_info, vmode);
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800825 if (ret)
826 return ret;
Shawn Guo66940652013-03-14 10:57:34 +0800827
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100828 var->nonstd = 0;
829 var->activate = FB_ACTIVATE_NOW;
830 var->accel_flags = 0;
831 var->vmode = FB_VMODE_NONINTERLACED;
832
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100833 /* Memory allocation for framebuffer */
Shawn Guo4aa02c72013-03-13 14:03:12 +0800834 fb_size = SZ_2M;
Marek Vasut03577d62016-08-26 17:53:35 +0200835 fb_virt = dma_alloc_wc(dev, PAGE_ALIGN(fb_size), &fb_phys, GFP_KERNEL);
Shawn Guo4aa02c72013-03-13 14:03:12 +0800836 if (!fb_virt)
837 return -ENOMEM;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100838
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100839 fb_info->fix.smem_start = fb_phys;
840 fb_info->screen_base = fb_virt;
841 fb_info->screen_size = fb_info->fix.smem_len = fb_size;
842
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100843 if (mxsfb_restore_mode(fb_info, vmode))
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100844 memset(fb_virt, 0, fb_size);
845
846 return 0;
847}
848
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100849static void mxsfb_free_videomem(struct fb_info *fb_info)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100850{
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100851 struct mxsfb_info *host = fb_info->par;
Marek Vasut03577d62016-08-26 17:53:35 +0200852 struct device *dev = &host->pdev->dev;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100853
Marek Vasut03577d62016-08-26 17:53:35 +0200854 dma_free_wc(dev, fb_info->screen_size, fb_info->screen_base,
855 fb_info->fix.smem_start);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100856}
857
Krzysztof Kozlowski99e62ef2015-05-02 00:38:40 +0900858static const struct platform_device_id mxsfb_devtype[] = {
Shawn Guo73fc6102012-06-25 19:54:35 +0800859 {
860 .name = "imx23-fb",
861 .driver_data = MXSFB_V3,
862 }, {
863 .name = "imx28-fb",
864 .driver_data = MXSFB_V4,
865 }, {
866 /* sentinel */
867 }
868};
869MODULE_DEVICE_TABLE(platform, mxsfb_devtype);
870
871static const struct of_device_id mxsfb_dt_ids[] = {
872 { .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devtype[0], },
873 { .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devtype[1], },
874 { /* sentinel */ }
875};
876MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
877
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800878static int mxsfb_probe(struct platform_device *pdev)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100879{
Shawn Guo73fc6102012-06-25 19:54:35 +0800880 const struct of_device_id *of_id =
881 of_match_device(mxsfb_dt_ids, &pdev->dev);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100882 struct resource *res;
883 struct mxsfb_info *host;
884 struct fb_info *fb_info;
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100885 struct fb_videomode *mode;
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800886 int ret;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100887
Shawn Guo73fc6102012-06-25 19:54:35 +0800888 if (of_id)
889 pdev->id_entry = of_id->data;
890
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100891 fb_info = framebuffer_alloc(sizeof(struct mxsfb_info), &pdev->dev);
892 if (!fb_info) {
893 dev_err(&pdev->dev, "Failed to allocate fbdev\n");
Shawn Guo9e548572013-03-13 13:37:11 +0800894 return -ENOMEM;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100895 }
896
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100897 mode = devm_kzalloc(&pdev->dev, sizeof(struct fb_videomode),
898 GFP_KERNEL);
899 if (mode == NULL)
900 return -ENOMEM;
901
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100902 host = fb_info->par;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100903
Julia Lawall51617d12013-08-14 11:11:06 +0200904 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Shawn Guo9e548572013-03-13 13:37:11 +0800905 host->base = devm_ioremap_resource(&pdev->dev, res);
906 if (IS_ERR(host->base)) {
Shawn Guo9e548572013-03-13 13:37:11 +0800907 ret = PTR_ERR(host->base);
908 goto fb_release;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100909 }
910
911 host->pdev = pdev;
912 platform_set_drvdata(pdev, host);
913
914 host->devdata = &mxsfb_devdata[pdev->id_entry->driver_data];
915
Shawn Guo9e548572013-03-13 13:37:11 +0800916 host->clk = devm_clk_get(&host->pdev->dev, NULL);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100917 if (IS_ERR(host->clk)) {
918 ret = PTR_ERR(host->clk);
Shawn Guo9e548572013-03-13 13:37:11 +0800919 goto fb_release;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100920 }
921
Fabio Estevamd301a5a2014-10-25 11:28:47 -0200922 host->clk_axi = devm_clk_get(&host->pdev->dev, "axi");
923 if (IS_ERR(host->clk_axi))
924 host->clk_axi = NULL;
925
926 host->clk_disp_axi = devm_clk_get(&host->pdev->dev, "disp_axi");
927 if (IS_ERR(host->clk_disp_axi))
928 host->clk_disp_axi = NULL;
929
Fabio Estevam43444292013-04-07 15:44:59 -0300930 host->reg_lcd = devm_regulator_get(&pdev->dev, "lcd");
931 if (IS_ERR(host->reg_lcd))
932 host->reg_lcd = NULL;
Shawn Guo73fc6102012-06-25 19:54:35 +0800933
Kees Cooka86854d2018-06-12 14:07:58 -0700934 fb_info->pseudo_palette = devm_kcalloc(&pdev->dev, 16, sizeof(u32),
Shawn Guo9e548572013-03-13 13:37:11 +0800935 GFP_KERNEL);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100936 if (!fb_info->pseudo_palette) {
937 ret = -ENOMEM;
Shawn Guo9e548572013-03-13 13:37:11 +0800938 goto fb_release;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100939 }
940
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100941 ret = mxsfb_init_fbinfo(fb_info, mode);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100942 if (ret != 0)
Shawn Guo9e548572013-03-13 13:37:11 +0800943 goto fb_release;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100944
Lothar Waßmann838bdf72014-01-06 14:17:58 +0100945 fb_videomode_to_var(&fb_info->var, mode);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100946
947 /* init the color fields */
948 mxsfb_check_var(&fb_info->var, fb_info);
949
950 platform_set_drvdata(pdev, fb_info);
951
952 ret = register_framebuffer(fb_info);
953 if (ret != 0) {
954 dev_err(&pdev->dev,"Failed to register framebuffer\n");
Shawn Guo9e548572013-03-13 13:37:11 +0800955 goto fb_destroy;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100956 }
957
958 if (!host->enabled) {
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800959 mxsfb_enable_axi_clk(host);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100960 writel(0, host->base + LCDC_CTRL);
Liu Ying2fa3b4c2015-04-03 12:51:05 +0800961 mxsfb_disable_axi_clk(host);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100962 mxsfb_set_par(fb_info);
963 mxsfb_enable_controller(fb_info);
964 }
965
966 dev_info(&pdev->dev, "initialized\n");
967
968 return 0;
969
Shawn Guo9e548572013-03-13 13:37:11 +0800970fb_destroy:
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100971 if (host->enabled)
Shawn Guoca4c22d32011-12-20 14:12:54 +0800972 clk_disable_unprepare(host->clk);
Shawn Guo9e548572013-03-13 13:37:11 +0800973fb_release:
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100974 framebuffer_release(fb_info);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100975
976 return ret;
977}
978
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800979static int mxsfb_remove(struct platform_device *pdev)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100980{
981 struct fb_info *fb_info = platform_get_drvdata(pdev);
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100982 struct mxsfb_info *host = fb_info->par;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100983
984 if (host->enabled)
985 mxsfb_disable_controller(fb_info);
986
987 unregister_framebuffer(fb_info);
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100988 mxsfb_free_videomem(fb_info);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100989
990 framebuffer_release(fb_info);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100991
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100992 return 0;
993}
994
Marek Vasutd313a862012-04-19 20:31:02 +0200995static void mxsfb_shutdown(struct platform_device *pdev)
996{
997 struct fb_info *fb_info = platform_get_drvdata(pdev);
Michael Trimarchi91ae3392018-01-15 17:04:22 +0100998 struct mxsfb_info *host = fb_info->par;
Marek Vasutd313a862012-04-19 20:31:02 +0200999
Liu Ying2fa3b4c2015-04-03 12:51:05 +08001000 mxsfb_enable_axi_clk(host);
1001
Marek Vasutd313a862012-04-19 20:31:02 +02001002 /*
1003 * Force stop the LCD controller as keeping it running during reboot
1004 * might interfere with the BootROM's boot mode pads sampling.
1005 */
1006 writel(CTRL_RUN, host->base + LCDC_CTRL + REG_CLR);
Liu Ying2fa3b4c2015-04-03 12:51:05 +08001007
1008 mxsfb_disable_axi_clk(host);
Marek Vasutd313a862012-04-19 20:31:02 +02001009}
1010
Sascha Hauerf0a523b2011-01-14 15:22:31 +01001011static struct platform_driver mxsfb_driver = {
1012 .probe = mxsfb_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001013 .remove = mxsfb_remove,
Marek Vasutd313a862012-04-19 20:31:02 +02001014 .shutdown = mxsfb_shutdown,
Sascha Hauerf0a523b2011-01-14 15:22:31 +01001015 .id_table = mxsfb_devtype,
1016 .driver = {
1017 .name = DRIVER_NAME,
Shawn Guo73fc6102012-06-25 19:54:35 +08001018 .of_match_table = mxsfb_dt_ids,
Sascha Hauerf0a523b2011-01-14 15:22:31 +01001019 },
1020};
1021
Marek Vasut396fa992011-12-19 16:37:59 +01001022module_platform_driver(mxsfb_driver);
Sascha Hauerf0a523b2011-01-14 15:22:31 +01001023
1024MODULE_DESCRIPTION("Freescale mxs framebuffer driver");
1025MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1026MODULE_LICENSE("GPL");