blob: d3299ff543b14f5ea9f6d47803dd21594bae5a68 [file] [log] [blame]
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001/*
2 * SuperH Mobile LCDC Framebuffer
3 *
4 * Copyright (c) 2008 Magnus Damm
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
10
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020011#include <linux/atomic.h>
12#include <linux/backlight.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070013#include <linux/clk.h>
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020014#include <linux/console.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070015#include <linux/dma-mapping.h>
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020016#include <linux/delay.h>
17#include <linux/gpio.h>
18#include <linux/init.h>
Magnus Damm85645572008-12-19 15:34:41 +090019#include <linux/interrupt.h>
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020020#include <linux/ioctl.h>
21#include <linux/kernel.h>
22#include <linux/mm.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
26#include <linux/slab.h>
Laurent Pinchartedd153a2011-12-13 14:02:28 +010027#include <linux/videodev2.h>
Paul Mundt1c6a3072009-07-01 06:50:31 +000028#include <linux/vmalloc.h>
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020029
Paul Mundt225c9a82008-10-01 16:24:32 +090030#include <video/sh_mobile_lcdc.h>
Laurent Pinchart8a209742011-07-13 12:13:47 +020031#include <video/sh_mobile_meram.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070032
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +000033#include "sh_mobile_lcdcfb.h"
34
Phil Edworthya6f15ad2009-09-15 12:00:30 +000035#define SIDE_B_OFFSET 0x1000
36#define MIRROR_OFFSET 0x2000
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070037
Guennadi Liakhovetskid2ecbab2010-11-04 11:06:06 +000038#define MAX_XRES 1920
39#define MAX_YRES 1080
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070040
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020041struct sh_mobile_lcdc_priv {
42 void __iomem *base;
43 int irq;
44 atomic_t hw_usecnt;
45 struct device *dev;
46 struct clk *dot_clk;
47 unsigned long lddckr;
48 struct sh_mobile_lcdc_chan ch[2];
49 struct notifier_block notifier;
50 int started;
51 int forced_fourcc; /* 2 channel LCDC must share fourcc setting */
52 struct sh_mobile_meram_info *meram_dev;
53};
54
55/* -----------------------------------------------------------------------------
56 * Registers access
57 */
58
Magnus Damm0246c472009-08-14 10:49:08 +000059static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070060 [LDDCKPAT1R] = 0x400,
61 [LDDCKPAT2R] = 0x404,
62 [LDMT1R] = 0x418,
63 [LDMT2R] = 0x41c,
64 [LDMT3R] = 0x420,
65 [LDDFR] = 0x424,
66 [LDSM1R] = 0x428,
Magnus Damm85645572008-12-19 15:34:41 +090067 [LDSM2R] = 0x42c,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070068 [LDSA1R] = 0x430,
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +000069 [LDSA2R] = 0x434,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070070 [LDMLSR] = 0x438,
71 [LDHCNR] = 0x448,
72 [LDHSYNR] = 0x44c,
73 [LDVLNR] = 0x450,
74 [LDVSYNR] = 0x454,
75 [LDPMR] = 0x460,
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +000076 [LDHAJR] = 0x4a0,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070077};
78
Magnus Damm0246c472009-08-14 10:49:08 +000079static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070080 [LDDCKPAT1R] = 0x408,
81 [LDDCKPAT2R] = 0x40c,
82 [LDMT1R] = 0x600,
83 [LDMT2R] = 0x604,
84 [LDMT3R] = 0x608,
85 [LDDFR] = 0x60c,
86 [LDSM1R] = 0x610,
Magnus Damm85645572008-12-19 15:34:41 +090087 [LDSM2R] = 0x614,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070088 [LDSA1R] = 0x618,
89 [LDMLSR] = 0x620,
90 [LDHCNR] = 0x624,
91 [LDHSYNR] = 0x628,
92 [LDVLNR] = 0x62c,
93 [LDVSYNR] = 0x630,
94 [LDPMR] = 0x63c,
95};
96
Phil Edworthya6f15ad2009-09-15 12:00:30 +000097static bool banked(int reg_nr)
98{
99 switch (reg_nr) {
100 case LDMT1R:
101 case LDMT2R:
102 case LDMT3R:
103 case LDDFR:
104 case LDSM1R:
105 case LDSA1R:
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000106 case LDSA2R:
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000107 case LDMLSR:
108 case LDHCNR:
109 case LDHSYNR:
110 case LDVLNR:
111 case LDVSYNR:
112 return true;
113 }
114 return false;
115}
116
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200117static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
118{
119 return chan->cfg.chan == LCDC_CHAN_SUBLCD;
120}
121
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700122static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
123 int reg_nr, unsigned long data)
124{
125 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000126 if (banked(reg_nr))
127 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
128 SIDE_B_OFFSET);
129}
130
131static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
132 int reg_nr, unsigned long data)
133{
134 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
135 MIRROR_OFFSET);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700136}
137
138static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
139 int reg_nr)
140{
141 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
142}
143
144static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
145 unsigned long reg_offs, unsigned long data)
146{
147 iowrite32(data, priv->base + reg_offs);
148}
149
150static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
151 unsigned long reg_offs)
152{
153 return ioread32(priv->base + reg_offs);
154}
155
156static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
157 unsigned long reg_offs,
158 unsigned long mask, unsigned long until)
159{
160 while ((lcdc_read(priv, reg_offs) & mask) != until)
161 cpu_relax();
162}
163
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200164/* -----------------------------------------------------------------------------
165 * Clock management
166 */
167
168static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700169{
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200170 if (atomic_inc_and_test(&priv->hw_usecnt)) {
171 if (priv->dot_clk)
172 clk_enable(priv->dot_clk);
173 pm_runtime_get_sync(priv->dev);
174 if (priv->meram_dev && priv->meram_dev->pdev)
175 pm_runtime_get_sync(&priv->meram_dev->pdev->dev);
176 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700177}
178
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200179static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
180{
181 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
182 if (priv->meram_dev && priv->meram_dev->pdev)
183 pm_runtime_put_sync(&priv->meram_dev->pdev->dev);
184 pm_runtime_put(priv->dev);
185 if (priv->dot_clk)
186 clk_disable(priv->dot_clk);
187 }
188}
189
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200190static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv *priv,
191 int clock_source)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200192{
Laurent Pinchart4774c122011-09-07 15:47:07 +0200193 struct clk *clk;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200194 char *str;
195
196 switch (clock_source) {
197 case LCDC_CLK_BUS:
198 str = "bus_clk";
199 priv->lddckr = LDDCKR_ICKSEL_BUS;
200 break;
201 case LCDC_CLK_PERIPHERAL:
202 str = "peripheral_clk";
203 priv->lddckr = LDDCKR_ICKSEL_MIPI;
204 break;
205 case LCDC_CLK_EXTERNAL:
206 str = NULL;
207 priv->lddckr = LDDCKR_ICKSEL_HDMI;
208 break;
209 default:
210 return -EINVAL;
211 }
212
Laurent Pinchart4774c122011-09-07 15:47:07 +0200213 if (str == NULL)
214 return 0;
215
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200216 clk = clk_get(priv->dev, str);
Laurent Pinchart4774c122011-09-07 15:47:07 +0200217 if (IS_ERR(clk)) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200218 dev_err(priv->dev, "cannot get dot clock %s\n", str);
Laurent Pinchart4774c122011-09-07 15:47:07 +0200219 return PTR_ERR(clk);
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200220 }
221
Laurent Pinchart4774c122011-09-07 15:47:07 +0200222 priv->dot_clk = clk;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200223 return 0;
224}
225
226/* -----------------------------------------------------------------------------
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200227 * Display, panel and deferred I/O
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200228 */
229
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700230static void lcdc_sys_write_index(void *handle, unsigned long data)
231{
232 struct sh_mobile_lcdc_chan *ch = handle;
233
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200234 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
235 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
236 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
237 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
238 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700239}
240
241static void lcdc_sys_write_data(void *handle, unsigned long data)
242{
243 struct sh_mobile_lcdc_chan *ch = handle;
244
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200245 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
246 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
247 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
248 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
249 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700250}
251
252static unsigned long lcdc_sys_read_data(void *handle)
253{
254 struct sh_mobile_lcdc_chan *ch = handle;
255
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200256 lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
257 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
258 lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
259 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700260 udelay(1);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200261 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700262
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200263 return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700264}
265
266struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
267 lcdc_sys_write_index,
268 lcdc_sys_write_data,
269 lcdc_sys_read_data,
270};
271
Paul Mundt1c6a3072009-07-01 06:50:31 +0000272static int sh_mobile_lcdc_sginit(struct fb_info *info,
273 struct list_head *pagelist)
274{
275 struct sh_mobile_lcdc_chan *ch = info->par;
276 unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
277 struct page *page;
278 int nr_pages = 0;
279
280 sg_init_table(ch->sglist, nr_pages_max);
281
282 list_for_each_entry(page, pagelist, lru)
283 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
284
285 return nr_pages;
286}
287
Magnus Damm85645572008-12-19 15:34:41 +0900288static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
289 struct list_head *pagelist)
290{
291 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchartafaad832011-09-11 22:59:04 +0200292 struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg.panel_cfg;
Magnus Damm85645572008-12-19 15:34:41 +0900293
294 /* enable clocks before accessing hardware */
295 sh_mobile_lcdc_clk_on(ch->lcdc);
296
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900297 /*
298 * It's possible to get here without anything on the pagelist via
299 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
300 * invocation. In the former case, the acceleration routines are
301 * stepped in to when using the framebuffer console causing the
302 * workqueue to be scheduled without any dirty pages on the list.
303 *
304 * Despite this, a panel update is still needed given that the
305 * acceleration routines have their own methods for writing in
306 * that still need to be updated.
307 *
308 * The fsync() and empty pagelist case could be optimized for,
309 * but we don't bother, as any application exhibiting such
310 * behaviour is fundamentally broken anyways.
311 */
312 if (!list_empty(pagelist)) {
313 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
Paul Mundt1c6a3072009-07-01 06:50:31 +0000314
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900315 /* trigger panel update */
316 dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
Laurent Pinchartafaad832011-09-11 22:59:04 +0200317 if (panel->start_transfer)
318 panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200319 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900320 dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
Magnus Dammef61aae2009-12-07 14:20:06 +0000321 } else {
Laurent Pinchartafaad832011-09-11 22:59:04 +0200322 if (panel->start_transfer)
323 panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200324 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
Magnus Dammef61aae2009-12-07 14:20:06 +0000325 }
Magnus Damm85645572008-12-19 15:34:41 +0900326}
327
328static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
329{
330 struct fb_deferred_io *fbdefio = info->fbdefio;
331
332 if (fbdefio)
333 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
334}
335
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200336static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan *ch)
337{
Laurent Pinchartafaad832011-09-11 22:59:04 +0200338 struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg.panel_cfg;
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200339
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200340 if (ch->tx_dev) {
Laurent Pinchart458981c2011-11-28 23:19:59 +0100341 int ret;
342
343 ret = ch->tx_dev->ops->display_on(ch->tx_dev);
344 if (ret < 0)
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200345 return;
Laurent Pinchart458981c2011-11-28 23:19:59 +0100346
347 if (ret == SH_MOBILE_LCDC_DISPLAY_DISCONNECTED)
348 ch->info->state = FBINFO_STATE_SUSPENDED;
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200349 }
350
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200351 /* HDMI must be enabled before LCDC configuration */
Laurent Pinchartafaad832011-09-11 22:59:04 +0200352 if (panel->display_on)
353 panel->display_on();
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200354}
355
356static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan *ch)
357{
Laurent Pinchartafaad832011-09-11 22:59:04 +0200358 struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg.panel_cfg;
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200359
Laurent Pinchartafaad832011-09-11 22:59:04 +0200360 if (panel->display_off)
361 panel->display_off();
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200362
363 if (ch->tx_dev)
364 ch->tx_dev->ops->display_off(ch->tx_dev);
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200365}
366
Laurent Pinchartecd29942011-09-18 14:14:46 +0200367static bool
368sh_mobile_lcdc_must_reconfigure(struct sh_mobile_lcdc_chan *ch,
Laurent Pincharte0c86012011-11-29 01:05:47 +0100369 const struct fb_videomode *new_mode)
Laurent Pinchartecd29942011-09-18 14:14:46 +0200370{
371 struct fb_var_screeninfo *old_var = &ch->display_var;
372 struct fb_videomode old_mode;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200373
374 fb_var_to_videomode(&old_mode, old_var);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200375
376 dev_dbg(ch->info->dev, "Old %ux%u, new %ux%u\n",
Laurent Pincharte0c86012011-11-29 01:05:47 +0100377 old_mode.xres, old_mode.yres, new_mode->xres, new_mode->yres);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200378
Laurent Pincharte0c86012011-11-29 01:05:47 +0100379 /* It can be a different monitor with an equal video-mode */
380 if (fb_mode_is_equal(&old_mode, new_mode))
Laurent Pinchartecd29942011-09-18 14:14:46 +0200381 return false;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200382
383 dev_dbg(ch->info->dev, "Switching %u -> %u lines\n",
Laurent Pincharte0c86012011-11-29 01:05:47 +0100384 old_mode.yres, new_mode->yres);
385 fb_videomode_to_var(old_var, new_mode);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200386
387 return true;
388}
389
390static int sh_mobile_check_var(struct fb_var_screeninfo *var,
391 struct fb_info *info);
392
393static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan *ch,
394 enum sh_mobile_lcdc_entity_event event,
Laurent Pincharte0c86012011-11-29 01:05:47 +0100395 const struct fb_videomode *mode,
396 const struct fb_monspecs *monspec)
Laurent Pinchartecd29942011-09-18 14:14:46 +0200397{
398 struct fb_info *info = ch->info;
Laurent Pincharte0c86012011-11-29 01:05:47 +0100399 struct fb_var_screeninfo var;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200400 int ret = 0;
401
402 switch (event) {
403 case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT:
404 /* HDMI plug in */
405 if (lock_fb_info(info)) {
406 console_lock();
407
Laurent Pincharte0c86012011-11-29 01:05:47 +0100408 ch->display_var.width = monspec->max_x * 10;
409 ch->display_var.height = monspec->max_y * 10;
410
411 if (!sh_mobile_lcdc_must_reconfigure(ch, mode) &&
Laurent Pinchartecd29942011-09-18 14:14:46 +0200412 info->state == FBINFO_STATE_RUNNING) {
413 /* First activation with the default monitor.
414 * Just turn on, if we run a resume here, the
415 * logo disappears.
416 */
Laurent Pincharte0c86012011-11-29 01:05:47 +0100417 info->var.width = monspec->max_x * 10;
418 info->var.height = monspec->max_y * 10;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200419 sh_mobile_lcdc_display_on(ch);
420 } else {
421 /* New monitor or have to wake up */
422 fb_set_suspend(info, 0);
423 }
424
425 console_unlock();
426 unlock_fb_info(info);
427 }
428 break;
429
430 case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT:
431 /* HDMI disconnect */
432 if (lock_fb_info(info)) {
433 console_lock();
434 fb_set_suspend(info, 1);
435 console_unlock();
436 unlock_fb_info(info);
437 }
438 break;
439
440 case SH_MOBILE_LCDC_EVENT_DISPLAY_MODE:
441 /* Validate a proposed new mode */
Laurent Pincharte0c86012011-11-29 01:05:47 +0100442 fb_videomode_to_var(&var, mode);
443 var.bits_per_pixel = info->var.bits_per_pixel;
444 var.grayscale = info->var.grayscale;
445 ret = sh_mobile_check_var(&var, info);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200446 break;
447 }
448
449 return ret;
450}
451
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200452/* -----------------------------------------------------------------------------
453 * Format helpers
454 */
455
456static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
457{
458 if (var->grayscale > 1)
459 return var->grayscale;
460
461 switch (var->bits_per_pixel) {
462 case 16:
463 return V4L2_PIX_FMT_RGB565;
464 case 24:
465 return V4L2_PIX_FMT_BGR24;
466 case 32:
467 return V4L2_PIX_FMT_BGR32;
468 default:
469 return 0;
470 }
471}
472
473static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
474{
475 return var->grayscale > 1;
476}
477
478static bool sh_mobile_format_is_yuv(const struct fb_var_screeninfo *var)
479{
480 if (var->grayscale <= 1)
481 return false;
482
483 switch (var->grayscale) {
484 case V4L2_PIX_FMT_NV12:
485 case V4L2_PIX_FMT_NV21:
486 case V4L2_PIX_FMT_NV16:
487 case V4L2_PIX_FMT_NV61:
488 case V4L2_PIX_FMT_NV24:
489 case V4L2_PIX_FMT_NV42:
490 return true;
491
492 default:
493 return false;
494 }
495}
496
497/* -----------------------------------------------------------------------------
498 * Start, stop and IRQ
499 */
500
Magnus Damm85645572008-12-19 15:34:41 +0900501static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
502{
503 struct sh_mobile_lcdc_priv *priv = data;
Magnus Damm2feb0752009-03-13 15:36:55 +0000504 struct sh_mobile_lcdc_chan *ch;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000505 unsigned long ldintr;
Magnus Damm2feb0752009-03-13 15:36:55 +0000506 int is_sub;
507 int k;
Magnus Damm85645572008-12-19 15:34:41 +0900508
Laurent Pinchartdc486652011-07-13 12:13:47 +0200509 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
510 ldintr = lcdc_read(priv, _LDINTR);
511 lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
Magnus Damm85645572008-12-19 15:34:41 +0900512
Magnus Damm2feb0752009-03-13 15:36:55 +0000513 /* figure out if this interrupt is for main or sub lcd */
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200514 is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
Magnus Damm2feb0752009-03-13 15:36:55 +0000515
Phil Edworthy9dd38812009-09-15 12:00:18 +0000516 /* wake up channel and disable clocks */
Magnus Damm2feb0752009-03-13 15:36:55 +0000517 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
518 ch = &priv->ch[k];
519
520 if (!ch->enabled)
521 continue;
522
Laurent Pinchartdc486652011-07-13 12:13:47 +0200523 /* Frame End */
Phil Edworthy9dd38812009-09-15 12:00:18 +0000524 if (ldintr & LDINTR_FS) {
525 if (is_sub == lcdc_chan_is_sublcd(ch)) {
526 ch->frame_end = 1;
527 wake_up(&ch->frame_end_wait);
Magnus Damm2feb0752009-03-13 15:36:55 +0000528
Phil Edworthy9dd38812009-09-15 12:00:18 +0000529 sh_mobile_lcdc_clk_off(priv);
530 }
531 }
532
533 /* VSYNC End */
Phil Edworthy40331b22010-02-15 13:57:49 +0000534 if (ldintr & LDINTR_VES)
535 complete(&ch->vsync_completion);
Magnus Damm2feb0752009-03-13 15:36:55 +0000536 }
537
Magnus Damm85645572008-12-19 15:34:41 +0900538 return IRQ_HANDLED;
539}
540
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700541static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
542 int start)
543{
544 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
545 int k;
546
547 /* start or stop the lcdc */
548 if (start)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200549 lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700550 else
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200551 lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700552
553 /* wait until power is applied/stopped on all channels */
554 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
555 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
556 while (1) {
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200557 tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
558 & LDPMR_LPS;
559 if (start && tmp == LDPMR_LPS)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700560 break;
561 if (!start && tmp == 0)
562 break;
563 cpu_relax();
564 }
565
566 if (!start)
567 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
568}
569
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000570static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
571{
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000572 struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
573 unsigned long h_total, hsync_pos, display_h_total;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000574 u32 tmp;
575
576 tmp = ch->ldmt1r_value;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200577 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
578 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
579 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
580 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
581 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
582 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
583 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000584 lcdc_write_chan(ch, LDMT1R, tmp);
585
586 /* setup SYS bus */
587 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
588 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
589
590 /* horizontal configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000591 h_total = display_var->xres + display_var->hsync_len +
592 display_var->left_margin + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000593 tmp = h_total / 8; /* HTCN */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000594 tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000595 lcdc_write_chan(ch, LDHCNR, tmp);
596
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000597 hsync_pos = display_var->xres + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000598 tmp = hsync_pos / 8; /* HSYNP */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000599 tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000600 lcdc_write_chan(ch, LDHSYNR, tmp);
601
602 /* vertical configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000603 tmp = display_var->yres + display_var->vsync_len +
604 display_var->upper_margin + display_var->lower_margin; /* VTLN */
605 tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000606 lcdc_write_chan(ch, LDVLNR, tmp);
607
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000608 tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
609 tmp |= display_var->vsync_len << 16; /* VSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000610 lcdc_write_chan(ch, LDVSYNR, tmp);
611
612 /* Adjust horizontal synchronisation for HDMI */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000613 display_h_total = display_var->xres + display_var->hsync_len +
614 display_var->left_margin + display_var->right_margin;
615 tmp = ((display_var->xres & 7) << 24) |
616 ((display_h_total & 7) << 16) |
617 ((display_var->hsync_len & 7) << 8) |
Kuninori Morimoto41e583c2011-11-08 20:33:29 -0800618 (hsync_pos & 7);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000619 lcdc_write_chan(ch, LDHAJR, tmp);
620}
621
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200622/*
623 * __sh_mobile_lcdc_start - Configure and tart the LCDC
624 * @priv: LCDC device
625 *
626 * Configure all enabled channels and start the LCDC device. All external
627 * devices (clocks, MERAM, panels, ...) are not touched by this function.
628 */
629static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700630{
631 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700632 unsigned long tmp;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200633 int k, m;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700634
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200635 /* Enable LCDC channels. Read data from external memory, avoid using the
636 * BEU for now.
637 */
638 lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
Magnus Damm85645572008-12-19 15:34:41 +0900639
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200640 /* Stop the LCDC first and disable all interrupts. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700641 sh_mobile_lcdc_start_stop(priv, 0);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200642 lcdc_write(priv, _LDINTR, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700643
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200644 /* Configure power supply, dot clocks and start them. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700645 tmp = priv->lddckr;
646 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
647 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200648 if (!ch->enabled)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700649 continue;
650
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200651 /* Power supply */
652 lcdc_write_chan(ch, LDPMR, 0);
653
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700654 m = ch->cfg.clock_divider;
655 if (!m)
656 continue;
657
Laurent Pinchart505c7de52011-07-13 12:13:47 +0200658 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
659 * denominator.
660 */
661 lcdc_write_chan(ch, LDDCKPAT1R, 0);
662 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
663
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700664 if (m == 1)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200665 m = LDDCKR_MOSEL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700666 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700667 }
668
669 lcdc_write(priv, _LDDCKR, tmp);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700670 lcdc_write(priv, _LDDCKSTPR, 0);
671 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
672
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200673 /* Setup geometry, format, frame buffer memory and operation mode. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700674 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
675 ch = &priv->ch[k];
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700676 if (!ch->enabled)
677 continue;
678
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000679 sh_mobile_lcdc_geometry(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700680
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100681 switch (sh_mobile_format_fourcc(&ch->info->var)) {
682 case V4L2_PIX_FMT_RGB565:
683 tmp = LDDFR_PKF_RGB16;
684 break;
685 case V4L2_PIX_FMT_BGR24:
686 tmp = LDDFR_PKF_RGB24;
687 break;
688 case V4L2_PIX_FMT_BGR32:
689 tmp = LDDFR_PKF_ARGB32;
690 break;
691 case V4L2_PIX_FMT_NV12:
692 case V4L2_PIX_FMT_NV21:
693 tmp = LDDFR_CC | LDDFR_YF_420;
694 break;
695 case V4L2_PIX_FMT_NV16:
696 case V4L2_PIX_FMT_NV61:
697 tmp = LDDFR_CC | LDDFR_YF_422;
698 break;
699 case V4L2_PIX_FMT_NV24:
700 case V4L2_PIX_FMT_NV42:
701 tmp = LDDFR_CC | LDDFR_YF_444;
702 break;
703 }
704
705 if (sh_mobile_format_is_yuv(&ch->info->var)) {
706 switch (ch->info->var.colorspace) {
707 case V4L2_COLORSPACE_REC709:
708 tmp |= LDDFR_CF1;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000709 break;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100710 case V4L2_COLORSPACE_JPEG:
711 tmp |= LDDFR_CF0;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000712 break;
713 }
Magnus Damm417d4822011-01-05 10:21:00 +0000714 }
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200715
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700716 lcdc_write_chan(ch, LDDFR, tmp);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200717 lcdc_write_chan(ch, LDMLSR, ch->pitch);
718 lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100719 if (sh_mobile_format_is_yuv(&ch->info->var))
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200720 lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700721
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200722 /* When using deferred I/O mode, configure the LCDC for one-shot
723 * operation and enable the frame end interrupt. Otherwise use
724 * continuous read mode.
725 */
726 if (ch->ldmt1r_value & LDMT1R_IFM &&
727 ch->cfg.sys_bus_cfg.deferred_io_msec) {
728 lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
729 lcdc_write(priv, _LDINTR, LDINTR_FE);
730 } else {
731 lcdc_write_chan(ch, LDSM1R, 0);
732 }
733 }
Damian7caa4342011-05-18 11:10:07 +0000734
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200735 /* Word and long word swap. */
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100736 switch (sh_mobile_format_fourcc(&priv->ch[0].info->var)) {
737 case V4L2_PIX_FMT_RGB565:
738 case V4L2_PIX_FMT_NV21:
739 case V4L2_PIX_FMT_NV61:
740 case V4L2_PIX_FMT_NV42:
741 tmp = LDDDSR_LS | LDDDSR_WS;
742 break;
743 case V4L2_PIX_FMT_BGR24:
744 case V4L2_PIX_FMT_NV12:
745 case V4L2_PIX_FMT_NV16:
746 case V4L2_PIX_FMT_NV24:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200747 tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100748 break;
749 case V4L2_PIX_FMT_BGR32:
750 default:
751 tmp = LDDDSR_LS;
752 break;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200753 }
754 lcdc_write(priv, _LDDDSR, tmp);
Damian7caa4342011-05-18 11:10:07 +0000755
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200756 /* Enable the display output. */
757 lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
758 sh_mobile_lcdc_start_stop(priv, 1);
759 priv->started = 1;
760}
Damian7caa4342011-05-18 11:10:07 +0000761
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200762static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
763{
764 struct sh_mobile_meram_info *mdev = priv->meram_dev;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200765 struct sh_mobile_lcdc_chan *ch;
766 unsigned long tmp;
767 int ret;
768 int k;
769
770 /* enable clocks before accessing the hardware */
771 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
772 if (priv->ch[k].enabled)
773 sh_mobile_lcdc_clk_on(priv);
774 }
775
776 /* reset */
777 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
778 lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
779
780 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
Laurent Pinchartafaad832011-09-11 22:59:04 +0200781 struct sh_mobile_lcdc_panel_cfg *panel;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200782
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200783 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200784 if (!ch->enabled)
785 continue;
786
Laurent Pinchartafaad832011-09-11 22:59:04 +0200787 panel = &ch->cfg.panel_cfg;
788 if (panel->setup_sys) {
789 ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200790 if (ret)
791 return ret;
792 }
793 }
794
795 /* Compute frame buffer base address and pitch for each channel. */
796 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
797 struct sh_mobile_meram_cfg *cfg;
798 int pixelformat;
799
800 ch = &priv->ch[k];
801 if (!ch->enabled)
802 continue;
803
804 ch->base_addr_y = ch->info->fix.smem_start;
805 ch->base_addr_c = ch->base_addr_y
806 + ch->info->var.xres
807 * ch->info->var.yres_virtual;
808 ch->pitch = ch->info->fix.line_length;
809
810 /* Enable MERAM if possible. */
811 cfg = ch->cfg.meram_cfg;
812 if (mdev == NULL || mdev->ops == NULL || cfg == NULL)
813 continue;
814
815 /* we need to de-init configured ICBs before we can
816 * re-initialize them.
817 */
818 if (ch->meram_enabled) {
819 mdev->ops->meram_unregister(mdev, cfg);
Damian7caa4342011-05-18 11:10:07 +0000820 ch->meram_enabled = 0;
Damian7caa4342011-05-18 11:10:07 +0000821 }
822
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100823 switch (sh_mobile_format_fourcc(&ch->info->var)) {
824 case V4L2_PIX_FMT_NV12:
825 case V4L2_PIX_FMT_NV21:
826 case V4L2_PIX_FMT_NV16:
827 case V4L2_PIX_FMT_NV61:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200828 pixelformat = SH_MOBILE_MERAM_PF_NV;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100829 break;
830 case V4L2_PIX_FMT_NV24:
831 case V4L2_PIX_FMT_NV42:
832 pixelformat = SH_MOBILE_MERAM_PF_NV24;
833 break;
834 case V4L2_PIX_FMT_RGB565:
835 case V4L2_PIX_FMT_BGR24:
836 case V4L2_PIX_FMT_BGR32:
837 default:
838 pixelformat = SH_MOBILE_MERAM_PF_RGB;
839 break;
840 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700841
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200842 ret = mdev->ops->meram_register(mdev, cfg, ch->pitch,
843 ch->info->var.yres, pixelformat,
844 ch->base_addr_y, ch->base_addr_c,
845 &ch->base_addr_y, &ch->base_addr_c,
846 &ch->pitch);
847 if (!ret)
848 ch->meram_enabled = 1;
849 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700850
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200851 /* Start the LCDC. */
852 __sh_mobile_lcdc_start(priv);
853
854 /* Setup deferred I/O, tell the board code to enable the panels, and
855 * turn backlight on.
856 */
857 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
858 ch = &priv->ch[k];
859 if (!ch->enabled)
860 continue;
861
Magnus Damm85645572008-12-19 15:34:41 +0900862 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200863 if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
Magnus Damm85645572008-12-19 15:34:41 +0900864 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
865 ch->defio.delay = msecs_to_jiffies(tmp);
Paul Mundte33afdd2009-07-07 11:24:32 +0900866 ch->info->fbdefio = &ch->defio;
867 fb_deferred_io_init(ch->info);
Magnus Damm85645572008-12-19 15:34:41 +0900868 }
Magnus Damm21bc1f02009-08-15 02:53:16 +0000869
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200870 sh_mobile_lcdc_display_on(ch);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000871
872 if (ch->bl) {
873 ch->bl->props.power = FB_BLANK_UNBLANK;
874 backlight_update_status(ch->bl);
875 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700876 }
877
878 return 0;
879}
880
881static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
882{
883 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700884 int k;
885
Magnus Damm2feb0752009-03-13 15:36:55 +0000886 /* clean up deferred io and ask board code to disable panel */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700887 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
888 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +0000889 if (!ch->enabled)
890 continue;
Magnus Damm2feb0752009-03-13 15:36:55 +0000891
892 /* deferred io mode:
893 * flush frame, and wait for frame end interrupt
894 * clean up deferred io and enable clock
895 */
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +0000896 if (ch->info && ch->info->fbdefio) {
Magnus Damm2feb0752009-03-13 15:36:55 +0000897 ch->frame_end = 0;
Paul Mundte33afdd2009-07-07 11:24:32 +0900898 schedule_delayed_work(&ch->info->deferred_work, 0);
Magnus Damm2feb0752009-03-13 15:36:55 +0000899 wait_event(ch->frame_end_wait, ch->frame_end);
Paul Mundte33afdd2009-07-07 11:24:32 +0900900 fb_deferred_io_cleanup(ch->info);
901 ch->info->fbdefio = NULL;
Magnus Damm2feb0752009-03-13 15:36:55 +0000902 sh_mobile_lcdc_clk_on(priv);
903 }
904
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000905 if (ch->bl) {
906 ch->bl->props.power = FB_BLANK_POWERDOWN;
907 backlight_update_status(ch->bl);
908 }
909
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200910 sh_mobile_lcdc_display_off(ch);
Damian7caa4342011-05-18 11:10:07 +0000911
912 /* disable the meram */
913 if (ch->meram_enabled) {
914 struct sh_mobile_meram_cfg *cfg;
915 struct sh_mobile_meram_info *mdev;
916 cfg = ch->cfg.meram_cfg;
917 mdev = priv->meram_dev;
918 mdev->ops->meram_unregister(mdev, cfg);
919 ch->meram_enabled = 0;
920 }
921
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700922 }
923
924 /* stop the lcdc */
Magnus Damm8e9bb192009-05-20 14:34:43 +0000925 if (priv->started) {
926 sh_mobile_lcdc_start_stop(priv, 0);
927 priv->started = 0;
928 }
Magnus Dammb51339f2008-10-31 20:23:26 +0900929
Magnus Damm85645572008-12-19 15:34:41 +0900930 /* stop clocks */
931 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
932 if (priv->ch[k].enabled)
933 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700934}
935
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200936/* -----------------------------------------------------------------------------
937 * Frame buffer operations
938 */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700939
940static int sh_mobile_lcdc_setcolreg(u_int regno,
941 u_int red, u_int green, u_int blue,
942 u_int transp, struct fb_info *info)
943{
944 u32 *palette = info->pseudo_palette;
945
946 if (regno >= PALETTE_NR)
947 return -EINVAL;
948
949 /* only FB_VISUAL_TRUECOLOR supported */
950
951 red >>= 16 - info->var.red.length;
952 green >>= 16 - info->var.green.length;
953 blue >>= 16 - info->var.blue.length;
954 transp >>= 16 - info->var.transp.length;
955
956 palette[regno] = (red << info->var.red.offset) |
957 (green << info->var.green.offset) |
958 (blue << info->var.blue.offset) |
959 (transp << info->var.transp.offset);
960
961 return 0;
962}
963
964static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
965 .id = "SH Mobile LCDC",
966 .type = FB_TYPE_PACKED_PIXELS,
967 .visual = FB_VISUAL_TRUECOLOR,
968 .accel = FB_ACCEL_NONE,
Phil Edworthy9dd38812009-09-15 12:00:18 +0000969 .xpanstep = 0,
970 .ypanstep = 1,
971 .ywrapstep = 0,
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100972 .capabilities = FB_CAP_FOURCC,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700973};
974
Magnus Damm85645572008-12-19 15:34:41 +0900975static void sh_mobile_lcdc_fillrect(struct fb_info *info,
976 const struct fb_fillrect *rect)
977{
978 sys_fillrect(info, rect);
979 sh_mobile_lcdc_deferred_io_touch(info);
980}
981
982static void sh_mobile_lcdc_copyarea(struct fb_info *info,
983 const struct fb_copyarea *area)
984{
985 sys_copyarea(info, area);
986 sh_mobile_lcdc_deferred_io_touch(info);
987}
988
989static void sh_mobile_lcdc_imageblit(struct fb_info *info,
990 const struct fb_image *image)
991{
992 sys_imageblit(info, image);
993 sh_mobile_lcdc_deferred_io_touch(info);
994}
995
Phil Edworthy9dd38812009-09-15 12:00:18 +0000996static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
997 struct fb_info *info)
998{
999 struct sh_mobile_lcdc_chan *ch = info->par;
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001000 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
1001 unsigned long ldrcntr;
1002 unsigned long new_pan_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001003 unsigned long base_addr_y, base_addr_c;
1004 unsigned long c_offset;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001005 bool yuv = sh_mobile_format_is_yuv(&info->var);
Phil Edworthy9dd38812009-09-15 12:00:18 +00001006
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001007 if (!yuv)
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +02001008 new_pan_offset = var->yoffset * info->fix.line_length
1009 + var->xoffset * (info->var.bits_per_pixel / 8);
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001010 else
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +02001011 new_pan_offset = var->yoffset * info->fix.line_length
1012 + var->xoffset;
Phil Edworthy9dd38812009-09-15 12:00:18 +00001013
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001014 if (new_pan_offset == ch->pan_offset)
1015 return 0; /* No change, do nothing */
1016
1017 ldrcntr = lcdc_read(priv, _LDRCNTR);
1018
1019 /* Set the source address for the next refresh */
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001020 base_addr_y = ch->dma_handle + new_pan_offset;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001021 if (yuv) {
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001022 /* Set y offset */
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +02001023 c_offset = var->yoffset * info->fix.line_length
1024 * (info->var.bits_per_pixel - 8) / 8;
1025 base_addr_c = ch->dma_handle
1026 + info->var.xres * info->var.yres_virtual
1027 + c_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001028 /* Set x offset */
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001029 if (sh_mobile_format_fourcc(&info->var) == V4L2_PIX_FMT_NV24)
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001030 base_addr_c += 2 * var->xoffset;
1031 else
1032 base_addr_c += var->xoffset;
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001033 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001034
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001035 if (ch->meram_enabled) {
Damian7caa4342011-05-18 11:10:07 +00001036 struct sh_mobile_meram_cfg *cfg;
1037 struct sh_mobile_meram_info *mdev;
Damian7caa4342011-05-18 11:10:07 +00001038 int ret;
1039
1040 cfg = ch->cfg.meram_cfg;
1041 mdev = priv->meram_dev;
1042 ret = mdev->ops->meram_update(mdev, cfg,
1043 base_addr_y, base_addr_c,
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001044 &base_addr_y, &base_addr_c);
Damian7caa4342011-05-18 11:10:07 +00001045 if (ret)
1046 return ret;
Damian7caa4342011-05-18 11:10:07 +00001047 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001048
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001049 ch->base_addr_y = base_addr_y;
1050 ch->base_addr_c = base_addr_c;
1051
1052 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001053 if (yuv)
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001054 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
1055
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001056 if (lcdc_chan_is_sublcd(ch))
1057 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
1058 else
1059 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
1060
1061 ch->pan_offset = new_pan_offset;
1062
1063 sh_mobile_lcdc_deferred_io_touch(info);
Phil Edworthy9dd38812009-09-15 12:00:18 +00001064
1065 return 0;
1066}
1067
Phil Edworthy40331b22010-02-15 13:57:49 +00001068static int sh_mobile_wait_for_vsync(struct fb_info *info)
1069{
1070 struct sh_mobile_lcdc_chan *ch = info->par;
1071 unsigned long ldintr;
1072 int ret;
1073
Laurent Pinchartdc486652011-07-13 12:13:47 +02001074 /* Enable VSync End interrupt and be careful not to acknowledge any
1075 * pending interrupt.
1076 */
Phil Edworthy40331b22010-02-15 13:57:49 +00001077 ldintr = lcdc_read(ch->lcdc, _LDINTR);
Laurent Pinchartdc486652011-07-13 12:13:47 +02001078 ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
Phil Edworthy40331b22010-02-15 13:57:49 +00001079 lcdc_write(ch->lcdc, _LDINTR, ldintr);
1080
1081 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
1082 msecs_to_jiffies(100));
1083 if (!ret)
1084 return -ETIMEDOUT;
1085
1086 return 0;
1087}
1088
1089static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
1090 unsigned long arg)
1091{
1092 int retval;
1093
1094 switch (cmd) {
1095 case FBIO_WAITFORVSYNC:
1096 retval = sh_mobile_wait_for_vsync(info);
1097 break;
1098
1099 default:
1100 retval = -ENOIOCTLCMD;
1101 break;
1102 }
1103 return retval;
1104}
1105
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001106static void sh_mobile_fb_reconfig(struct fb_info *info)
1107{
1108 struct sh_mobile_lcdc_chan *ch = info->par;
1109 struct fb_videomode mode1, mode2;
1110 struct fb_event event;
1111 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1112
1113 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1114 /* More framebuffer users are active */
1115 return;
1116
1117 fb_var_to_videomode(&mode1, &ch->display_var);
1118 fb_var_to_videomode(&mode2, &info->var);
1119
1120 if (fb_mode_is_equal(&mode1, &mode2))
1121 return;
1122
1123 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1124 if (fb_set_var(info, &ch->display_var) < 0)
1125 /* Couldn't reconfigure, hopefully, can continue as before */
1126 return;
1127
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001128 /*
1129 * fb_set_var() calls the notifier change internally, only if
1130 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1131 * user event, we have to call the chain ourselves.
1132 */
1133 event.info = info;
Arnd Hannemanncc267ec2010-11-15 21:43:22 +00001134 event.data = &mode1;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001135 fb_notifier_call_chain(evnt, &event);
1136}
1137
1138/*
1139 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1140 * user == 1, or with console sem held, if user == 0.
1141 */
1142static int sh_mobile_release(struct fb_info *info, int user)
1143{
1144 struct sh_mobile_lcdc_chan *ch = info->par;
1145
1146 mutex_lock(&ch->open_lock);
1147 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1148
1149 ch->use_count--;
1150
1151 /* Nothing to reconfigure, when called from fbcon */
1152 if (user) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001153 console_lock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001154 sh_mobile_fb_reconfig(info);
Torben Hohnac751ef2011-01-25 15:07:35 -08001155 console_unlock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001156 }
1157
1158 mutex_unlock(&ch->open_lock);
1159
1160 return 0;
1161}
1162
1163static int sh_mobile_open(struct fb_info *info, int user)
1164{
1165 struct sh_mobile_lcdc_chan *ch = info->par;
1166
1167 mutex_lock(&ch->open_lock);
1168 ch->use_count++;
1169
1170 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1171 mutex_unlock(&ch->open_lock);
1172
1173 return 0;
1174}
1175
1176static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1177{
1178 struct sh_mobile_lcdc_chan *ch = info->par;
Magnus Damm417d4822011-01-05 10:21:00 +00001179 struct sh_mobile_lcdc_priv *p = ch->lcdc;
Laurent Pinchart03862192011-08-31 13:00:53 +02001180 unsigned int best_dist = (unsigned int)-1;
1181 unsigned int best_xres = 0;
1182 unsigned int best_yres = 0;
1183 unsigned int i;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001184
Laurent Pinchart03862192011-08-31 13:00:53 +02001185 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001186 return -EINVAL;
Laurent Pinchart03862192011-08-31 13:00:53 +02001187
1188 /* If board code provides us with a list of available modes, make sure
1189 * we use one of them. Find the mode closest to the requested one. The
1190 * distance between two modes is defined as the size of the
1191 * non-overlapping parts of the two rectangles.
1192 */
1193 for (i = 0; i < ch->cfg.num_cfg; ++i) {
1194 const struct fb_videomode *mode = &ch->cfg.lcd_cfg[i];
1195 unsigned int dist;
1196
1197 /* We can only round up. */
1198 if (var->xres > mode->xres || var->yres > mode->yres)
1199 continue;
1200
1201 dist = var->xres * var->yres + mode->xres * mode->yres
1202 - 2 * min(var->xres, mode->xres)
1203 * min(var->yres, mode->yres);
1204
1205 if (dist < best_dist) {
1206 best_xres = mode->xres;
1207 best_yres = mode->yres;
1208 best_dist = dist;
1209 }
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001210 }
Magnus Damm417d4822011-01-05 10:21:00 +00001211
Laurent Pinchart03862192011-08-31 13:00:53 +02001212 /* If no available mode can be used, return an error. */
1213 if (ch->cfg.num_cfg != 0) {
1214 if (best_dist == (unsigned int)-1)
1215 return -EINVAL;
1216
1217 var->xres = best_xres;
1218 var->yres = best_yres;
1219 }
1220
1221 /* Make sure the virtual resolution is at least as big as the visible
1222 * resolution.
1223 */
1224 if (var->xres_virtual < var->xres)
1225 var->xres_virtual = var->xres;
1226 if (var->yres_virtual < var->yres)
1227 var->yres_virtual = var->yres;
1228
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001229 if (sh_mobile_format_is_fourcc(var)) {
1230 switch (var->grayscale) {
1231 case V4L2_PIX_FMT_NV12:
1232 case V4L2_PIX_FMT_NV21:
1233 var->bits_per_pixel = 12;
1234 break;
1235 case V4L2_PIX_FMT_RGB565:
1236 case V4L2_PIX_FMT_NV16:
1237 case V4L2_PIX_FMT_NV61:
1238 var->bits_per_pixel = 16;
1239 break;
1240 case V4L2_PIX_FMT_BGR24:
1241 case V4L2_PIX_FMT_NV24:
1242 case V4L2_PIX_FMT_NV42:
1243 var->bits_per_pixel = 24;
1244 break;
1245 case V4L2_PIX_FMT_BGR32:
1246 var->bits_per_pixel = 32;
1247 break;
1248 default:
1249 return -EINVAL;
1250 }
Laurent Pinchart03862192011-08-31 13:00:53 +02001251
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001252 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1253 * respectively.
1254 */
1255 if (!sh_mobile_format_is_yuv(var))
1256 var->colorspace = V4L2_COLORSPACE_SRGB;
1257 else if (var->colorspace != V4L2_COLORSPACE_REC709)
1258 var->colorspace = V4L2_COLORSPACE_JPEG;
1259 } else {
1260 if (var->bits_per_pixel <= 16) { /* RGB 565 */
1261 var->bits_per_pixel = 16;
1262 var->red.offset = 11;
1263 var->red.length = 5;
1264 var->green.offset = 5;
1265 var->green.length = 6;
1266 var->blue.offset = 0;
1267 var->blue.length = 5;
1268 var->transp.offset = 0;
1269 var->transp.length = 0;
1270 } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
1271 var->bits_per_pixel = 24;
1272 var->red.offset = 16;
1273 var->red.length = 8;
1274 var->green.offset = 8;
1275 var->green.length = 8;
1276 var->blue.offset = 0;
1277 var->blue.length = 8;
1278 var->transp.offset = 0;
1279 var->transp.length = 0;
1280 } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
1281 var->bits_per_pixel = 32;
1282 var->red.offset = 16;
1283 var->red.length = 8;
1284 var->green.offset = 8;
1285 var->green.length = 8;
1286 var->blue.offset = 0;
1287 var->blue.length = 8;
1288 var->transp.offset = 24;
1289 var->transp.length = 8;
1290 } else
1291 return -EINVAL;
1292
1293 var->red.msb_right = 0;
1294 var->green.msb_right = 0;
1295 var->blue.msb_right = 0;
1296 var->transp.msb_right = 0;
1297 }
Laurent Pinchart03862192011-08-31 13:00:53 +02001298
1299 /* Make sure we don't exceed our allocated memory. */
1300 if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
1301 info->fix.smem_len)
1302 return -EINVAL;
1303
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001304 /* only accept the forced_fourcc for dual channel configurations */
1305 if (p->forced_fourcc &&
1306 p->forced_fourcc != sh_mobile_format_fourcc(var))
Magnus Damm417d4822011-01-05 10:21:00 +00001307 return -EINVAL;
1308
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001309 return 0;
1310}
Phil Edworthy40331b22010-02-15 13:57:49 +00001311
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001312static int sh_mobile_set_par(struct fb_info *info)
1313{
1314 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchart91fba482011-08-31 13:00:56 +02001315 u32 line_length = info->fix.line_length;
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001316 int ret;
1317
1318 sh_mobile_lcdc_stop(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001319
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001320 if (sh_mobile_format_is_yuv(&info->var))
Laurent Pinchart91fba482011-08-31 13:00:56 +02001321 info->fix.line_length = info->var.xres;
1322 else
1323 info->fix.line_length = info->var.xres
1324 * info->var.bits_per_pixel / 8;
1325
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001326 ret = sh_mobile_lcdc_start(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001327 if (ret < 0) {
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001328 dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001329 info->fix.line_length = line_length;
1330 }
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001331
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001332 if (sh_mobile_format_is_fourcc(&info->var)) {
1333 info->fix.type = FB_TYPE_FOURCC;
1334 info->fix.visual = FB_VISUAL_FOURCC;
1335 } else {
1336 info->fix.type = FB_TYPE_PACKED_PIXELS;
1337 info->fix.visual = FB_VISUAL_TRUECOLOR;
1338 }
1339
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001340 return ret;
1341}
1342
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001343/*
1344 * Screen blanking. Behavior is as follows:
1345 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1346 * FB_BLANK_NORMAL: screen blanked, clocks enabled
1347 * FB_BLANK_VSYNC,
1348 * FB_BLANK_HSYNC,
1349 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1350 */
1351static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
1352{
1353 struct sh_mobile_lcdc_chan *ch = info->par;
1354 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1355
1356 /* blank the screen? */
1357 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
1358 struct fb_fillrect rect = {
1359 .width = info->var.xres,
1360 .height = info->var.yres,
1361 };
1362 sh_mobile_lcdc_fillrect(info, &rect);
1363 }
1364 /* turn clocks on? */
1365 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
1366 sh_mobile_lcdc_clk_on(p);
1367 }
1368 /* turn clocks off? */
1369 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
1370 /* make sure the screen is updated with the black fill before
1371 * switching the clocks off. one vsync is not enough since
1372 * blanking may occur in the middle of a refresh. deferred io
1373 * mode will reenable the clocks and update the screen in time,
1374 * so it does not need this. */
1375 if (!info->fbdefio) {
1376 sh_mobile_wait_for_vsync(info);
1377 sh_mobile_wait_for_vsync(info);
1378 }
1379 sh_mobile_lcdc_clk_off(p);
1380 }
1381
1382 ch->blank_status = blank;
1383 return 0;
1384}
1385
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001386static struct fb_ops sh_mobile_lcdc_ops = {
Phil Edworthy9dd38812009-09-15 12:00:18 +00001387 .owner = THIS_MODULE,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001388 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
Magnus Damm2540c112008-12-17 17:29:49 +09001389 .fb_read = fb_sys_read,
1390 .fb_write = fb_sys_write,
Magnus Damm85645572008-12-19 15:34:41 +09001391 .fb_fillrect = sh_mobile_lcdc_fillrect,
1392 .fb_copyarea = sh_mobile_lcdc_copyarea,
1393 .fb_imageblit = sh_mobile_lcdc_imageblit,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001394 .fb_blank = sh_mobile_lcdc_blank,
Phil Edworthy9dd38812009-09-15 12:00:18 +00001395 .fb_pan_display = sh_mobile_fb_pan_display,
Phil Edworthy40331b22010-02-15 13:57:49 +00001396 .fb_ioctl = sh_mobile_ioctl,
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001397 .fb_open = sh_mobile_open,
1398 .fb_release = sh_mobile_release,
1399 .fb_check_var = sh_mobile_check_var,
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001400 .fb_set_par = sh_mobile_set_par,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001401};
1402
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001403/* -----------------------------------------------------------------------------
1404 * Backlight
1405 */
1406
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001407static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
1408{
1409 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001410 int brightness = bdev->props.brightness;
1411
1412 if (bdev->props.power != FB_BLANK_UNBLANK ||
1413 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
1414 brightness = 0;
1415
Laurent Pinchart43059b02011-09-11 22:59:04 +02001416 return ch->cfg.bl_info.set_brightness(brightness);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001417}
1418
1419static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
1420{
1421 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001422
Laurent Pinchart43059b02011-09-11 22:59:04 +02001423 return ch->cfg.bl_info.get_brightness();
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001424}
1425
1426static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
1427 struct fb_info *info)
1428{
1429 return (info->bl_dev == bdev);
1430}
1431
1432static struct backlight_ops sh_mobile_lcdc_bl_ops = {
1433 .options = BL_CORE_SUSPENDRESUME,
1434 .update_status = sh_mobile_lcdc_update_bl,
1435 .get_brightness = sh_mobile_lcdc_get_brightness,
1436 .check_fb = sh_mobile_lcdc_check_fb,
1437};
1438
1439static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
1440 struct sh_mobile_lcdc_chan *ch)
1441{
1442 struct backlight_device *bl;
1443
1444 bl = backlight_device_register(ch->cfg.bl_info.name, parent, ch,
1445 &sh_mobile_lcdc_bl_ops, NULL);
Dan Carpenterbeee1f22011-03-21 15:03:13 +00001446 if (IS_ERR(bl)) {
1447 dev_err(parent, "unable to register backlight device: %ld\n",
1448 PTR_ERR(bl));
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001449 return NULL;
1450 }
1451
1452 bl->props.max_brightness = ch->cfg.bl_info.max_brightness;
1453 bl->props.brightness = bl->props.max_brightness;
1454 backlight_update_status(bl);
1455
1456 return bl;
1457}
1458
1459static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
1460{
1461 backlight_device_unregister(bdev);
1462}
1463
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001464/* -----------------------------------------------------------------------------
1465 * Power management
1466 */
1467
Magnus Damm2feb0752009-03-13 15:36:55 +00001468static int sh_mobile_lcdc_suspend(struct device *dev)
1469{
1470 struct platform_device *pdev = to_platform_device(dev);
1471
1472 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
1473 return 0;
1474}
1475
1476static int sh_mobile_lcdc_resume(struct device *dev)
1477{
1478 struct platform_device *pdev = to_platform_device(dev);
1479
1480 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
1481}
1482
Magnus Damm0246c472009-08-14 10:49:08 +00001483static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
1484{
1485 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001486 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001487
1488 /* turn off LCDC hardware */
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001489 lcdc_write(priv, _LDCNT1R, 0);
1490
Magnus Damm0246c472009-08-14 10:49:08 +00001491 return 0;
1492}
1493
1494static int sh_mobile_lcdc_runtime_resume(struct device *dev)
1495{
1496 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001497 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001498
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001499 __sh_mobile_lcdc_start(priv);
Magnus Damm0246c472009-08-14 10:49:08 +00001500
1501 return 0;
1502}
1503
Alexey Dobriyan47145212009-12-14 18:00:08 -08001504static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
Magnus Damm2feb0752009-03-13 15:36:55 +00001505 .suspend = sh_mobile_lcdc_suspend,
1506 .resume = sh_mobile_lcdc_resume,
Magnus Damm0246c472009-08-14 10:49:08 +00001507 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
1508 .runtime_resume = sh_mobile_lcdc_runtime_resume,
Magnus Damm2feb0752009-03-13 15:36:55 +00001509};
1510
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001511/* -----------------------------------------------------------------------------
1512 * Framebuffer notifier
1513 */
1514
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001515/* locking: called with info->lock held */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001516static int sh_mobile_lcdc_notify(struct notifier_block *nb,
1517 unsigned long action, void *data)
1518{
1519 struct fb_event *event = data;
1520 struct fb_info *info = event->info;
1521 struct sh_mobile_lcdc_chan *ch = info->par;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001522
1523 if (&ch->lcdc->notifier != nb)
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001524 return NOTIFY_DONE;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001525
1526 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
1527 __func__, action, event->data);
1528
1529 switch(action) {
1530 case FB_EVENT_SUSPEND:
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001531 sh_mobile_lcdc_display_off(ch);
Guennadi Liakhovetskiafe417c2010-09-03 07:20:39 +00001532 sh_mobile_lcdc_stop(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001533 break;
1534 case FB_EVENT_RESUME:
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001535 mutex_lock(&ch->open_lock);
1536 sh_mobile_fb_reconfig(info);
1537 mutex_unlock(&ch->open_lock);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001538
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001539 sh_mobile_lcdc_display_on(ch);
Guennadi Liakhovetskiebe5e122011-05-05 16:33:40 +00001540 sh_mobile_lcdc_start(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001541 }
1542
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001543 return NOTIFY_OK;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001544}
1545
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001546/* -----------------------------------------------------------------------------
1547 * Probe/remove and driver init/exit
1548 */
1549
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001550static const struct fb_videomode default_720p __devinitconst = {
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001551 .name = "HDMI 720p",
1552 .xres = 1280,
1553 .yres = 720,
1554
1555 .left_margin = 220,
1556 .right_margin = 110,
1557 .hsync_len = 40,
1558
1559 .upper_margin = 20,
1560 .lower_margin = 5,
1561 .vsync_len = 5,
1562
1563 .pixclock = 13468,
1564 .refresh = 60,
1565 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
1566};
1567
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001568static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1569{
1570 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1571 struct fb_info *info;
1572 int i;
1573
1574 fb_unregister_client(&priv->notifier);
1575
1576 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
1577 if (priv->ch[i].info && priv->ch[i].info->dev)
1578 unregister_framebuffer(priv->ch[i].info);
1579
1580 sh_mobile_lcdc_stop(priv);
1581
1582 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001583 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001584
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001585 info = ch->info;
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001586 if (!info || !info->device)
1587 continue;
1588
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02001589 if (ch->tx_dev) {
1590 ch->tx_dev->lcdc = NULL;
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001591 module_put(ch->cfg.tx_dev->dev.driver->owner);
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02001592 }
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001593
1594 if (ch->sglist)
1595 vfree(ch->sglist);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001596
1597 if (info->screen_base)
1598 dma_free_coherent(&pdev->dev, info->fix.smem_len,
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001599 info->screen_base, ch->dma_handle);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001600 fb_dealloc_cmap(&info->cmap);
1601 framebuffer_release(info);
1602 }
1603
1604 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1605 if (priv->ch[i].bl)
1606 sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
1607 }
1608
Laurent Pinchart4774c122011-09-07 15:47:07 +02001609 if (priv->dot_clk) {
1610 pm_runtime_disable(&pdev->dev);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001611 clk_put(priv->dot_clk);
Laurent Pinchart4774c122011-09-07 15:47:07 +02001612 }
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001613
1614 if (priv->base)
1615 iounmap(priv->base);
1616
1617 if (priv->irq)
1618 free_irq(priv->irq, priv);
1619 kfree(priv);
1620 return 0;
1621}
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001622
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001623static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001624{
1625 int interface_type = ch->cfg.interface_type;
1626
1627 switch (interface_type) {
1628 case RGB8:
1629 case RGB9:
1630 case RGB12A:
1631 case RGB12B:
1632 case RGB16:
1633 case RGB18:
1634 case RGB24:
1635 case SYS8A:
1636 case SYS8B:
1637 case SYS8C:
1638 case SYS8D:
1639 case SYS9:
1640 case SYS12:
1641 case SYS16A:
1642 case SYS16B:
1643 case SYS16C:
1644 case SYS18:
1645 case SYS24:
1646 break;
1647 default:
1648 return -EINVAL;
1649 }
1650
1651 /* SUBLCD only supports SYS interface */
1652 if (lcdc_chan_is_sublcd(ch)) {
1653 if (!(interface_type & LDMT1R_IFM))
1654 return -EINVAL;
1655
1656 interface_type &= ~LDMT1R_IFM;
1657 }
1658
1659 ch->ldmt1r_value = interface_type;
1660 return 0;
1661}
1662
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001663static int __devinit
1664sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv *priv,
1665 struct sh_mobile_lcdc_chan *ch)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001666{
1667 struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
1668 const struct fb_videomode *max_mode;
1669 const struct fb_videomode *mode;
1670 struct fb_var_screeninfo *var;
1671 struct fb_info *info;
1672 unsigned int max_size;
1673 int num_cfg;
1674 void *buf;
1675 int ret;
1676 int i;
1677
Laurent Pincharta67472a2011-08-31 13:00:59 +02001678 mutex_init(&ch->open_lock);
Laurent Pinchartecd29942011-09-18 14:14:46 +02001679 ch->notify = sh_mobile_lcdc_display_notify;
Laurent Pincharta67472a2011-08-31 13:00:59 +02001680
1681 /* Allocate the frame buffer device. */
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001682 ch->info = framebuffer_alloc(0, priv->dev);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001683 if (!ch->info) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001684 dev_err(priv->dev, "unable to allocate fb_info\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001685 return -ENOMEM;
1686 }
1687
1688 info = ch->info;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001689 info->fbops = &sh_mobile_lcdc_ops;
1690 info->par = ch;
Laurent Pincharta67472a2011-08-31 13:00:59 +02001691 info->pseudo_palette = &ch->pseudo_palette;
1692 info->flags = FBINFO_FLAG_DEFAULT;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001693
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001694 if (cfg->tx_dev) {
1695 if (!cfg->tx_dev->dev.driver ||
1696 !try_module_get(cfg->tx_dev->dev.driver->owner)) {
1697 dev_warn(priv->dev,
1698 "unable to get transmitter device\n");
1699 return -EINVAL;
1700 }
1701 ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02001702 ch->tx_dev->lcdc = ch;
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001703 }
1704
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001705 /* Iterate through the modes to validate them and find the highest
1706 * resolution.
1707 */
1708 max_mode = NULL;
1709 max_size = 0;
1710
1711 for (i = 0, mode = cfg->lcd_cfg; i < cfg->num_cfg; i++, mode++) {
1712 unsigned int size = mode->yres * mode->xres;
1713
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001714 /* NV12/NV21 buffers must have even number of lines */
1715 if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
1716 cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001717 dev_err(priv->dev, "yres must be multiple of 2 for "
1718 "YCbCr420 mode.\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001719 return -EINVAL;
1720 }
1721
1722 if (size > max_size) {
1723 max_mode = mode;
1724 max_size = size;
1725 }
1726 }
1727
1728 if (!max_size)
1729 max_size = MAX_XRES * MAX_YRES;
1730 else
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001731 dev_dbg(priv->dev, "Found largest videomode %ux%u\n",
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001732 max_mode->xres, max_mode->yres);
1733
Laurent Pincharta67472a2011-08-31 13:00:59 +02001734 /* Create the mode list. */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001735 if (cfg->lcd_cfg == NULL) {
1736 mode = &default_720p;
1737 num_cfg = 1;
1738 } else {
1739 mode = cfg->lcd_cfg;
1740 num_cfg = cfg->num_cfg;
1741 }
1742
1743 fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
1744
Laurent Pincharta67472a2011-08-31 13:00:59 +02001745 /* Initialize variable screen information using the first mode as
1746 * default. The default Y virtual resolution is twice the panel size to
1747 * allow for double-buffering.
1748 */
1749 var = &info->var;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001750 fb_videomode_to_var(var, mode);
Laurent Pinchartafaad832011-09-11 22:59:04 +02001751 var->width = cfg->panel_cfg.width;
1752 var->height = cfg->panel_cfg.height;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001753 var->yres_virtual = var->yres * 2;
1754 var->activate = FB_ACTIVATE_NOW;
1755
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001756 switch (cfg->fourcc) {
1757 case V4L2_PIX_FMT_RGB565:
1758 var->bits_per_pixel = 16;
1759 break;
1760 case V4L2_PIX_FMT_BGR24:
1761 var->bits_per_pixel = 24;
1762 break;
1763 case V4L2_PIX_FMT_BGR32:
1764 var->bits_per_pixel = 32;
1765 break;
1766 default:
1767 var->grayscale = cfg->fourcc;
1768 break;
1769 }
1770
1771 /* Make sure the memory size check won't fail. smem_len is initialized
1772 * later based on var.
1773 */
1774 info->fix.smem_len = UINT_MAX;
Laurent Pincharta67472a2011-08-31 13:00:59 +02001775 ret = sh_mobile_check_var(var, info);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001776 if (ret)
1777 return ret;
1778
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001779 max_size = max_size * var->bits_per_pixel / 8 * 2;
1780
Laurent Pincharta67472a2011-08-31 13:00:59 +02001781 /* Allocate frame buffer memory and color map. */
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001782 buf = dma_alloc_coherent(priv->dev, max_size, &ch->dma_handle,
1783 GFP_KERNEL);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001784 if (!buf) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001785 dev_err(priv->dev, "unable to allocate buffer\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001786 return -ENOMEM;
1787 }
1788
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001789 ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1790 if (ret < 0) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001791 dev_err(priv->dev, "unable to allocate cmap\n");
1792 dma_free_coherent(priv->dev, max_size, buf, ch->dma_handle);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001793 return ret;
1794 }
1795
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001796 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1797 * for NV12 and NV21.
1798 */
1799 info->fix = sh_mobile_lcdc_fix;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001800 info->fix.smem_start = ch->dma_handle;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001801 info->fix.smem_len = max_size;
1802 if (cfg->fourcc == V4L2_PIX_FMT_NV12 ||
1803 cfg->fourcc == V4L2_PIX_FMT_NV21)
1804 info->fix.ypanstep = 2;
1805
1806 if (sh_mobile_format_is_yuv(var)) {
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001807 info->fix.line_length = var->xres;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001808 info->fix.visual = FB_VISUAL_FOURCC;
1809 } else {
1810 info->fix.line_length = var->xres * var->bits_per_pixel / 8;
1811 info->fix.visual = FB_VISUAL_TRUECOLOR;
1812 }
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001813
1814 info->screen_base = buf;
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001815 info->device = priv->dev;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001816 ch->display_var = *var;
1817
1818 return 0;
1819}
1820
Uwe Kleine-Königc2e13032010-02-04 20:56:51 +01001821static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001822{
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001823 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001824 struct sh_mobile_lcdc_priv *priv;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001825 struct resource *res;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001826 int num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001827 int error;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001828 int i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001829
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001830 if (!pdata) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001831 dev_err(&pdev->dev, "no platform data defined\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001832 return -EINVAL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001833 }
1834
1835 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Damm85645572008-12-19 15:34:41 +09001836 i = platform_get_irq(pdev, 0);
1837 if (!res || i < 0) {
1838 dev_err(&pdev->dev, "cannot get platform resources\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001839 return -ENOENT;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001840 }
1841
1842 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1843 if (!priv) {
1844 dev_err(&pdev->dev, "cannot allocate device data\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001845 return -ENOMEM;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001846 }
1847
Laurent Pinchart4774c122011-09-07 15:47:07 +02001848 priv->dev = &pdev->dev;
1849 priv->meram_dev = pdata->meram_dev;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001850 platform_set_drvdata(pdev, priv);
1851
Yong Zhangf8798cc2011-09-22 16:59:16 +08001852 error = request_irq(i, sh_mobile_lcdc_irq, 0,
Kay Sievers7ad33e72009-03-24 16:38:21 -07001853 dev_name(&pdev->dev), priv);
Magnus Damm85645572008-12-19 15:34:41 +09001854 if (error) {
1855 dev_err(&pdev->dev, "unable to request irq\n");
1856 goto err1;
1857 }
1858
1859 priv->irq = i;
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +00001860 atomic_set(&priv->hw_usecnt, -1);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001861
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001862 for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
1863 struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001864
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001865 ch->lcdc = priv;
1866 memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
1867
1868 error = sh_mobile_lcdc_check_interface(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001869 if (error) {
1870 dev_err(&pdev->dev, "unsupported interface type\n");
1871 goto err1;
1872 }
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001873 init_waitqueue_head(&ch->frame_end_wait);
1874 init_completion(&ch->vsync_completion);
1875 ch->pan_offset = 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001876
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001877 /* probe the backlight is there is one defined */
1878 if (ch->cfg.bl_info.max_brightness)
1879 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
1880
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001881 switch (pdata->ch[i].chan) {
1882 case LCDC_CHAN_MAINLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001883 ch->enabled = LDCNT2R_ME;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001884 ch->reg_offs = lcdc_offs_mainlcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001885 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001886 break;
1887 case LCDC_CHAN_SUBLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001888 ch->enabled = LDCNT2R_SE;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001889 ch->reg_offs = lcdc_offs_sublcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001890 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001891 break;
1892 }
1893 }
1894
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001895 if (!num_channels) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001896 dev_err(&pdev->dev, "no channels defined\n");
1897 error = -EINVAL;
1898 goto err1;
1899 }
1900
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001901 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001902 if (num_channels == 2)
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001903 priv->forced_fourcc = pdata->ch[0].fourcc;
Magnus Damm417d4822011-01-05 10:21:00 +00001904
Guennadi Liakhovetskidba6f382010-06-30 09:26:35 +00001905 priv->base = ioremap_nocache(res->start, resource_size(res));
1906 if (!priv->base)
1907 goto err1;
1908
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001909 error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001910 if (error) {
1911 dev_err(&pdev->dev, "unable to setup clocks\n");
1912 goto err1;
1913 }
1914
Laurent Pinchart4774c122011-09-07 15:47:07 +02001915 /* Enable runtime PM. */
1916 pm_runtime_enable(&pdev->dev);
Damian7caa4342011-05-18 11:10:07 +00001917
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001918 for (i = 0; i < num_channels; i++) {
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001919 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001920
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001921 error = sh_mobile_lcdc_channel_init(priv, ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001922 if (error)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001923 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001924 }
1925
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001926 error = sh_mobile_lcdc_start(priv);
1927 if (error) {
1928 dev_err(&pdev->dev, "unable to start hardware\n");
1929 goto err1;
1930 }
1931
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001932 for (i = 0; i < num_channels; i++) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00001933 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001934 struct fb_info *info = ch->info;
Paul Mundt1c6a3072009-07-01 06:50:31 +00001935
1936 if (info->fbdefio) {
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001937 ch->sglist = vmalloc(sizeof(struct scatterlist) *
Paul Mundt1c6a3072009-07-01 06:50:31 +00001938 info->fix.smem_len >> PAGE_SHIFT);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001939 if (!ch->sglist) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00001940 dev_err(&pdev->dev, "cannot allocate sglist\n");
1941 goto err1;
1942 }
1943 }
1944
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001945 info->bl_dev = ch->bl;
1946
Paul Mundt1c6a3072009-07-01 06:50:31 +00001947 error = register_framebuffer(info);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001948 if (error < 0)
1949 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001950
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001951 dev_info(&pdev->dev, "registered %s/%s as %dx%d %dbpp.\n",
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001952 pdev->name, (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
1953 "mainlcd" : "sublcd", info->var.xres, info->var.yres,
1954 info->var.bits_per_pixel);
Magnus Damm85645572008-12-19 15:34:41 +09001955
1956 /* deferred io mode: disable clock to save power */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001957 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
Magnus Damm85645572008-12-19 15:34:41 +09001958 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001959 }
1960
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001961 /* Failure ignored */
1962 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
1963 fb_register_client(&priv->notifier);
1964
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001965 return 0;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001966err1:
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001967 sh_mobile_lcdc_remove(pdev);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001968
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001969 return error;
1970}
1971
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001972static struct platform_driver sh_mobile_lcdc_driver = {
1973 .driver = {
1974 .name = "sh_mobile_lcdc_fb",
1975 .owner = THIS_MODULE,
Magnus Damm2feb0752009-03-13 15:36:55 +00001976 .pm = &sh_mobile_lcdc_dev_pm_ops,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001977 },
1978 .probe = sh_mobile_lcdc_probe,
1979 .remove = sh_mobile_lcdc_remove,
1980};
1981
Axel Lin4277f2c2011-11-26 10:25:54 +08001982module_platform_driver(sh_mobile_lcdc_driver);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001983
1984MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1985MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1986MODULE_LICENSE("GPL v2");