blob: 128eb7793a602afe693beb654290e7ff63644d19 [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,
369 const struct fb_var_screeninfo *new_var)
370{
371 struct fb_var_screeninfo *old_var = &ch->display_var;
372 struct fb_videomode old_mode;
373 struct fb_videomode new_mode;
374
375 fb_var_to_videomode(&old_mode, old_var);
376 fb_var_to_videomode(&new_mode, new_var);
377
378 dev_dbg(ch->info->dev, "Old %ux%u, new %ux%u\n",
379 old_mode.xres, old_mode.yres, new_mode.xres, new_mode.yres);
380
381 if (fb_mode_is_equal(&old_mode, &new_mode)) {
382 /* It can be a different monitor with an equal video-mode */
383 old_var->width = new_var->width;
384 old_var->height = new_var->height;
385 return false;
386 }
387
388 dev_dbg(ch->info->dev, "Switching %u -> %u lines\n",
389 old_mode.yres, new_mode.yres);
390 *old_var = *new_var;
391
392 return true;
393}
394
395static int sh_mobile_check_var(struct fb_var_screeninfo *var,
396 struct fb_info *info);
397
398static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan *ch,
399 enum sh_mobile_lcdc_entity_event event,
400 struct fb_var_screeninfo *var)
401{
402 struct fb_info *info = ch->info;
403 int ret = 0;
404
405 switch (event) {
406 case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT:
407 /* HDMI plug in */
408 if (lock_fb_info(info)) {
409 console_lock();
410
411 if (!sh_mobile_lcdc_must_reconfigure(ch, var) &&
412 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 */
417 info->var.width = var->width;
418 info->var.height = var->height;
419 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 */
442 var->bits_per_pixel = info->var.bits_per_pixel;
443 ret = sh_mobile_check_var(var, info);
444 break;
445 }
446
447 return ret;
448}
449
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200450/* -----------------------------------------------------------------------------
451 * Format helpers
452 */
453
454static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
455{
456 if (var->grayscale > 1)
457 return var->grayscale;
458
459 switch (var->bits_per_pixel) {
460 case 16:
461 return V4L2_PIX_FMT_RGB565;
462 case 24:
463 return V4L2_PIX_FMT_BGR24;
464 case 32:
465 return V4L2_PIX_FMT_BGR32;
466 default:
467 return 0;
468 }
469}
470
471static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
472{
473 return var->grayscale > 1;
474}
475
476static bool sh_mobile_format_is_yuv(const struct fb_var_screeninfo *var)
477{
478 if (var->grayscale <= 1)
479 return false;
480
481 switch (var->grayscale) {
482 case V4L2_PIX_FMT_NV12:
483 case V4L2_PIX_FMT_NV21:
484 case V4L2_PIX_FMT_NV16:
485 case V4L2_PIX_FMT_NV61:
486 case V4L2_PIX_FMT_NV24:
487 case V4L2_PIX_FMT_NV42:
488 return true;
489
490 default:
491 return false;
492 }
493}
494
495/* -----------------------------------------------------------------------------
496 * Start, stop and IRQ
497 */
498
Magnus Damm85645572008-12-19 15:34:41 +0900499static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
500{
501 struct sh_mobile_lcdc_priv *priv = data;
Magnus Damm2feb0752009-03-13 15:36:55 +0000502 struct sh_mobile_lcdc_chan *ch;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000503 unsigned long ldintr;
Magnus Damm2feb0752009-03-13 15:36:55 +0000504 int is_sub;
505 int k;
Magnus Damm85645572008-12-19 15:34:41 +0900506
Laurent Pinchartdc486652011-07-13 12:13:47 +0200507 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
508 ldintr = lcdc_read(priv, _LDINTR);
509 lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
Magnus Damm85645572008-12-19 15:34:41 +0900510
Magnus Damm2feb0752009-03-13 15:36:55 +0000511 /* figure out if this interrupt is for main or sub lcd */
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200512 is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
Magnus Damm2feb0752009-03-13 15:36:55 +0000513
Phil Edworthy9dd38812009-09-15 12:00:18 +0000514 /* wake up channel and disable clocks */
Magnus Damm2feb0752009-03-13 15:36:55 +0000515 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
516 ch = &priv->ch[k];
517
518 if (!ch->enabled)
519 continue;
520
Laurent Pinchartdc486652011-07-13 12:13:47 +0200521 /* Frame End */
Phil Edworthy9dd38812009-09-15 12:00:18 +0000522 if (ldintr & LDINTR_FS) {
523 if (is_sub == lcdc_chan_is_sublcd(ch)) {
524 ch->frame_end = 1;
525 wake_up(&ch->frame_end_wait);
Magnus Damm2feb0752009-03-13 15:36:55 +0000526
Phil Edworthy9dd38812009-09-15 12:00:18 +0000527 sh_mobile_lcdc_clk_off(priv);
528 }
529 }
530
531 /* VSYNC End */
Phil Edworthy40331b22010-02-15 13:57:49 +0000532 if (ldintr & LDINTR_VES)
533 complete(&ch->vsync_completion);
Magnus Damm2feb0752009-03-13 15:36:55 +0000534 }
535
Magnus Damm85645572008-12-19 15:34:41 +0900536 return IRQ_HANDLED;
537}
538
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700539static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
540 int start)
541{
542 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
543 int k;
544
545 /* start or stop the lcdc */
546 if (start)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200547 lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700548 else
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200549 lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700550
551 /* wait until power is applied/stopped on all channels */
552 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
553 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
554 while (1) {
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200555 tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
556 & LDPMR_LPS;
557 if (start && tmp == LDPMR_LPS)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700558 break;
559 if (!start && tmp == 0)
560 break;
561 cpu_relax();
562 }
563
564 if (!start)
565 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
566}
567
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000568static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
569{
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000570 struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
571 unsigned long h_total, hsync_pos, display_h_total;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000572 u32 tmp;
573
574 tmp = ch->ldmt1r_value;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200575 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
576 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
577 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
578 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
579 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
580 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
581 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000582 lcdc_write_chan(ch, LDMT1R, tmp);
583
584 /* setup SYS bus */
585 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
586 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
587
588 /* horizontal configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000589 h_total = display_var->xres + display_var->hsync_len +
590 display_var->left_margin + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000591 tmp = h_total / 8; /* HTCN */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000592 tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000593 lcdc_write_chan(ch, LDHCNR, tmp);
594
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000595 hsync_pos = display_var->xres + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000596 tmp = hsync_pos / 8; /* HSYNP */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000597 tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000598 lcdc_write_chan(ch, LDHSYNR, tmp);
599
600 /* vertical configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000601 tmp = display_var->yres + display_var->vsync_len +
602 display_var->upper_margin + display_var->lower_margin; /* VTLN */
603 tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000604 lcdc_write_chan(ch, LDVLNR, tmp);
605
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000606 tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
607 tmp |= display_var->vsync_len << 16; /* VSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000608 lcdc_write_chan(ch, LDVSYNR, tmp);
609
610 /* Adjust horizontal synchronisation for HDMI */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000611 display_h_total = display_var->xres + display_var->hsync_len +
612 display_var->left_margin + display_var->right_margin;
613 tmp = ((display_var->xres & 7) << 24) |
614 ((display_h_total & 7) << 16) |
615 ((display_var->hsync_len & 7) << 8) |
Kuninori Morimoto41e583c2011-11-08 20:33:29 -0800616 (hsync_pos & 7);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000617 lcdc_write_chan(ch, LDHAJR, tmp);
618}
619
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200620/*
621 * __sh_mobile_lcdc_start - Configure and tart the LCDC
622 * @priv: LCDC device
623 *
624 * Configure all enabled channels and start the LCDC device. All external
625 * devices (clocks, MERAM, panels, ...) are not touched by this function.
626 */
627static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700628{
629 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700630 unsigned long tmp;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200631 int k, m;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700632
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200633 /* Enable LCDC channels. Read data from external memory, avoid using the
634 * BEU for now.
635 */
636 lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
Magnus Damm85645572008-12-19 15:34:41 +0900637
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200638 /* Stop the LCDC first and disable all interrupts. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700639 sh_mobile_lcdc_start_stop(priv, 0);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200640 lcdc_write(priv, _LDINTR, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700641
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200642 /* Configure power supply, dot clocks and start them. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700643 tmp = priv->lddckr;
644 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
645 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200646 if (!ch->enabled)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700647 continue;
648
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200649 /* Power supply */
650 lcdc_write_chan(ch, LDPMR, 0);
651
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700652 m = ch->cfg.clock_divider;
653 if (!m)
654 continue;
655
Laurent Pinchart505c7de52011-07-13 12:13:47 +0200656 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
657 * denominator.
658 */
659 lcdc_write_chan(ch, LDDCKPAT1R, 0);
660 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
661
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700662 if (m == 1)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200663 m = LDDCKR_MOSEL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700664 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700665 }
666
667 lcdc_write(priv, _LDDCKR, tmp);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700668 lcdc_write(priv, _LDDCKSTPR, 0);
669 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
670
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200671 /* Setup geometry, format, frame buffer memory and operation mode. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700672 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
673 ch = &priv->ch[k];
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700674 if (!ch->enabled)
675 continue;
676
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000677 sh_mobile_lcdc_geometry(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700678
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100679 switch (sh_mobile_format_fourcc(&ch->info->var)) {
680 case V4L2_PIX_FMT_RGB565:
681 tmp = LDDFR_PKF_RGB16;
682 break;
683 case V4L2_PIX_FMT_BGR24:
684 tmp = LDDFR_PKF_RGB24;
685 break;
686 case V4L2_PIX_FMT_BGR32:
687 tmp = LDDFR_PKF_ARGB32;
688 break;
689 case V4L2_PIX_FMT_NV12:
690 case V4L2_PIX_FMT_NV21:
691 tmp = LDDFR_CC | LDDFR_YF_420;
692 break;
693 case V4L2_PIX_FMT_NV16:
694 case V4L2_PIX_FMT_NV61:
695 tmp = LDDFR_CC | LDDFR_YF_422;
696 break;
697 case V4L2_PIX_FMT_NV24:
698 case V4L2_PIX_FMT_NV42:
699 tmp = LDDFR_CC | LDDFR_YF_444;
700 break;
701 }
702
703 if (sh_mobile_format_is_yuv(&ch->info->var)) {
704 switch (ch->info->var.colorspace) {
705 case V4L2_COLORSPACE_REC709:
706 tmp |= LDDFR_CF1;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000707 break;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100708 case V4L2_COLORSPACE_JPEG:
709 tmp |= LDDFR_CF0;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000710 break;
711 }
Magnus Damm417d4822011-01-05 10:21:00 +0000712 }
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200713
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700714 lcdc_write_chan(ch, LDDFR, tmp);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200715 lcdc_write_chan(ch, LDMLSR, ch->pitch);
716 lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100717 if (sh_mobile_format_is_yuv(&ch->info->var))
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200718 lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700719
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200720 /* When using deferred I/O mode, configure the LCDC for one-shot
721 * operation and enable the frame end interrupt. Otherwise use
722 * continuous read mode.
723 */
724 if (ch->ldmt1r_value & LDMT1R_IFM &&
725 ch->cfg.sys_bus_cfg.deferred_io_msec) {
726 lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
727 lcdc_write(priv, _LDINTR, LDINTR_FE);
728 } else {
729 lcdc_write_chan(ch, LDSM1R, 0);
730 }
731 }
Damian7caa4342011-05-18 11:10:07 +0000732
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200733 /* Word and long word swap. */
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100734 switch (sh_mobile_format_fourcc(&priv->ch[0].info->var)) {
735 case V4L2_PIX_FMT_RGB565:
736 case V4L2_PIX_FMT_NV21:
737 case V4L2_PIX_FMT_NV61:
738 case V4L2_PIX_FMT_NV42:
739 tmp = LDDDSR_LS | LDDDSR_WS;
740 break;
741 case V4L2_PIX_FMT_BGR24:
742 case V4L2_PIX_FMT_NV12:
743 case V4L2_PIX_FMT_NV16:
744 case V4L2_PIX_FMT_NV24:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200745 tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100746 break;
747 case V4L2_PIX_FMT_BGR32:
748 default:
749 tmp = LDDDSR_LS;
750 break;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200751 }
752 lcdc_write(priv, _LDDDSR, tmp);
Damian7caa4342011-05-18 11:10:07 +0000753
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200754 /* Enable the display output. */
755 lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
756 sh_mobile_lcdc_start_stop(priv, 1);
757 priv->started = 1;
758}
Damian7caa4342011-05-18 11:10:07 +0000759
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200760static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
761{
762 struct sh_mobile_meram_info *mdev = priv->meram_dev;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200763 struct sh_mobile_lcdc_chan *ch;
764 unsigned long tmp;
765 int ret;
766 int k;
767
768 /* enable clocks before accessing the hardware */
769 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
770 if (priv->ch[k].enabled)
771 sh_mobile_lcdc_clk_on(priv);
772 }
773
774 /* reset */
775 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
776 lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
777
778 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
Laurent Pinchartafaad832011-09-11 22:59:04 +0200779 struct sh_mobile_lcdc_panel_cfg *panel;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200780
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200781 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200782 if (!ch->enabled)
783 continue;
784
Laurent Pinchartafaad832011-09-11 22:59:04 +0200785 panel = &ch->cfg.panel_cfg;
786 if (panel->setup_sys) {
787 ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200788 if (ret)
789 return ret;
790 }
791 }
792
793 /* Compute frame buffer base address and pitch for each channel. */
794 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
795 struct sh_mobile_meram_cfg *cfg;
796 int pixelformat;
797
798 ch = &priv->ch[k];
799 if (!ch->enabled)
800 continue;
801
802 ch->base_addr_y = ch->info->fix.smem_start;
803 ch->base_addr_c = ch->base_addr_y
804 + ch->info->var.xres
805 * ch->info->var.yres_virtual;
806 ch->pitch = ch->info->fix.line_length;
807
808 /* Enable MERAM if possible. */
809 cfg = ch->cfg.meram_cfg;
810 if (mdev == NULL || mdev->ops == NULL || cfg == NULL)
811 continue;
812
813 /* we need to de-init configured ICBs before we can
814 * re-initialize them.
815 */
816 if (ch->meram_enabled) {
817 mdev->ops->meram_unregister(mdev, cfg);
Damian7caa4342011-05-18 11:10:07 +0000818 ch->meram_enabled = 0;
Damian7caa4342011-05-18 11:10:07 +0000819 }
820
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100821 switch (sh_mobile_format_fourcc(&ch->info->var)) {
822 case V4L2_PIX_FMT_NV12:
823 case V4L2_PIX_FMT_NV21:
824 case V4L2_PIX_FMT_NV16:
825 case V4L2_PIX_FMT_NV61:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200826 pixelformat = SH_MOBILE_MERAM_PF_NV;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100827 break;
828 case V4L2_PIX_FMT_NV24:
829 case V4L2_PIX_FMT_NV42:
830 pixelformat = SH_MOBILE_MERAM_PF_NV24;
831 break;
832 case V4L2_PIX_FMT_RGB565:
833 case V4L2_PIX_FMT_BGR24:
834 case V4L2_PIX_FMT_BGR32:
835 default:
836 pixelformat = SH_MOBILE_MERAM_PF_RGB;
837 break;
838 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700839
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200840 ret = mdev->ops->meram_register(mdev, cfg, ch->pitch,
841 ch->info->var.yres, pixelformat,
842 ch->base_addr_y, ch->base_addr_c,
843 &ch->base_addr_y, &ch->base_addr_c,
844 &ch->pitch);
845 if (!ret)
846 ch->meram_enabled = 1;
847 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700848
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200849 /* Start the LCDC. */
850 __sh_mobile_lcdc_start(priv);
851
852 /* Setup deferred I/O, tell the board code to enable the panels, and
853 * turn backlight on.
854 */
855 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
856 ch = &priv->ch[k];
857 if (!ch->enabled)
858 continue;
859
Magnus Damm85645572008-12-19 15:34:41 +0900860 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200861 if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
Magnus Damm85645572008-12-19 15:34:41 +0900862 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
863 ch->defio.delay = msecs_to_jiffies(tmp);
Paul Mundte33afdd2009-07-07 11:24:32 +0900864 ch->info->fbdefio = &ch->defio;
865 fb_deferred_io_init(ch->info);
Magnus Damm85645572008-12-19 15:34:41 +0900866 }
Magnus Damm21bc1f02009-08-15 02:53:16 +0000867
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200868 sh_mobile_lcdc_display_on(ch);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000869
870 if (ch->bl) {
871 ch->bl->props.power = FB_BLANK_UNBLANK;
872 backlight_update_status(ch->bl);
873 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700874 }
875
876 return 0;
877}
878
879static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
880{
881 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700882 int k;
883
Magnus Damm2feb0752009-03-13 15:36:55 +0000884 /* clean up deferred io and ask board code to disable panel */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700885 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
886 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +0000887 if (!ch->enabled)
888 continue;
Magnus Damm2feb0752009-03-13 15:36:55 +0000889
890 /* deferred io mode:
891 * flush frame, and wait for frame end interrupt
892 * clean up deferred io and enable clock
893 */
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +0000894 if (ch->info && ch->info->fbdefio) {
Magnus Damm2feb0752009-03-13 15:36:55 +0000895 ch->frame_end = 0;
Paul Mundte33afdd2009-07-07 11:24:32 +0900896 schedule_delayed_work(&ch->info->deferred_work, 0);
Magnus Damm2feb0752009-03-13 15:36:55 +0000897 wait_event(ch->frame_end_wait, ch->frame_end);
Paul Mundte33afdd2009-07-07 11:24:32 +0900898 fb_deferred_io_cleanup(ch->info);
899 ch->info->fbdefio = NULL;
Magnus Damm2feb0752009-03-13 15:36:55 +0000900 sh_mobile_lcdc_clk_on(priv);
901 }
902
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000903 if (ch->bl) {
904 ch->bl->props.power = FB_BLANK_POWERDOWN;
905 backlight_update_status(ch->bl);
906 }
907
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200908 sh_mobile_lcdc_display_off(ch);
Damian7caa4342011-05-18 11:10:07 +0000909
910 /* disable the meram */
911 if (ch->meram_enabled) {
912 struct sh_mobile_meram_cfg *cfg;
913 struct sh_mobile_meram_info *mdev;
914 cfg = ch->cfg.meram_cfg;
915 mdev = priv->meram_dev;
916 mdev->ops->meram_unregister(mdev, cfg);
917 ch->meram_enabled = 0;
918 }
919
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700920 }
921
922 /* stop the lcdc */
Magnus Damm8e9bb192009-05-20 14:34:43 +0000923 if (priv->started) {
924 sh_mobile_lcdc_start_stop(priv, 0);
925 priv->started = 0;
926 }
Magnus Dammb51339f2008-10-31 20:23:26 +0900927
Magnus Damm85645572008-12-19 15:34:41 +0900928 /* stop clocks */
929 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
930 if (priv->ch[k].enabled)
931 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700932}
933
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200934/* -----------------------------------------------------------------------------
935 * Frame buffer operations
936 */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700937
938static int sh_mobile_lcdc_setcolreg(u_int regno,
939 u_int red, u_int green, u_int blue,
940 u_int transp, struct fb_info *info)
941{
942 u32 *palette = info->pseudo_palette;
943
944 if (regno >= PALETTE_NR)
945 return -EINVAL;
946
947 /* only FB_VISUAL_TRUECOLOR supported */
948
949 red >>= 16 - info->var.red.length;
950 green >>= 16 - info->var.green.length;
951 blue >>= 16 - info->var.blue.length;
952 transp >>= 16 - info->var.transp.length;
953
954 palette[regno] = (red << info->var.red.offset) |
955 (green << info->var.green.offset) |
956 (blue << info->var.blue.offset) |
957 (transp << info->var.transp.offset);
958
959 return 0;
960}
961
962static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
963 .id = "SH Mobile LCDC",
964 .type = FB_TYPE_PACKED_PIXELS,
965 .visual = FB_VISUAL_TRUECOLOR,
966 .accel = FB_ACCEL_NONE,
Phil Edworthy9dd38812009-09-15 12:00:18 +0000967 .xpanstep = 0,
968 .ypanstep = 1,
969 .ywrapstep = 0,
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100970 .capabilities = FB_CAP_FOURCC,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700971};
972
Magnus Damm85645572008-12-19 15:34:41 +0900973static void sh_mobile_lcdc_fillrect(struct fb_info *info,
974 const struct fb_fillrect *rect)
975{
976 sys_fillrect(info, rect);
977 sh_mobile_lcdc_deferred_io_touch(info);
978}
979
980static void sh_mobile_lcdc_copyarea(struct fb_info *info,
981 const struct fb_copyarea *area)
982{
983 sys_copyarea(info, area);
984 sh_mobile_lcdc_deferred_io_touch(info);
985}
986
987static void sh_mobile_lcdc_imageblit(struct fb_info *info,
988 const struct fb_image *image)
989{
990 sys_imageblit(info, image);
991 sh_mobile_lcdc_deferred_io_touch(info);
992}
993
Phil Edworthy9dd38812009-09-15 12:00:18 +0000994static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
995 struct fb_info *info)
996{
997 struct sh_mobile_lcdc_chan *ch = info->par;
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000998 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
999 unsigned long ldrcntr;
1000 unsigned long new_pan_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001001 unsigned long base_addr_y, base_addr_c;
1002 unsigned long c_offset;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001003 bool yuv = sh_mobile_format_is_yuv(&info->var);
Phil Edworthy9dd38812009-09-15 12:00:18 +00001004
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001005 if (!yuv)
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +02001006 new_pan_offset = var->yoffset * info->fix.line_length
1007 + var->xoffset * (info->var.bits_per_pixel / 8);
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001008 else
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +02001009 new_pan_offset = var->yoffset * info->fix.line_length
1010 + var->xoffset;
Phil Edworthy9dd38812009-09-15 12:00:18 +00001011
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001012 if (new_pan_offset == ch->pan_offset)
1013 return 0; /* No change, do nothing */
1014
1015 ldrcntr = lcdc_read(priv, _LDRCNTR);
1016
1017 /* Set the source address for the next refresh */
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001018 base_addr_y = ch->dma_handle + new_pan_offset;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001019 if (yuv) {
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001020 /* Set y offset */
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +02001021 c_offset = var->yoffset * info->fix.line_length
1022 * (info->var.bits_per_pixel - 8) / 8;
1023 base_addr_c = ch->dma_handle
1024 + info->var.xres * info->var.yres_virtual
1025 + c_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001026 /* Set x offset */
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001027 if (sh_mobile_format_fourcc(&info->var) == V4L2_PIX_FMT_NV24)
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001028 base_addr_c += 2 * var->xoffset;
1029 else
1030 base_addr_c += var->xoffset;
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001031 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001032
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001033 if (ch->meram_enabled) {
Damian7caa4342011-05-18 11:10:07 +00001034 struct sh_mobile_meram_cfg *cfg;
1035 struct sh_mobile_meram_info *mdev;
Damian7caa4342011-05-18 11:10:07 +00001036 int ret;
1037
1038 cfg = ch->cfg.meram_cfg;
1039 mdev = priv->meram_dev;
1040 ret = mdev->ops->meram_update(mdev, cfg,
1041 base_addr_y, base_addr_c,
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001042 &base_addr_y, &base_addr_c);
Damian7caa4342011-05-18 11:10:07 +00001043 if (ret)
1044 return ret;
Damian7caa4342011-05-18 11:10:07 +00001045 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001046
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001047 ch->base_addr_y = base_addr_y;
1048 ch->base_addr_c = base_addr_c;
1049
1050 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001051 if (yuv)
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001052 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
1053
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001054 if (lcdc_chan_is_sublcd(ch))
1055 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
1056 else
1057 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
1058
1059 ch->pan_offset = new_pan_offset;
1060
1061 sh_mobile_lcdc_deferred_io_touch(info);
Phil Edworthy9dd38812009-09-15 12:00:18 +00001062
1063 return 0;
1064}
1065
Phil Edworthy40331b22010-02-15 13:57:49 +00001066static int sh_mobile_wait_for_vsync(struct fb_info *info)
1067{
1068 struct sh_mobile_lcdc_chan *ch = info->par;
1069 unsigned long ldintr;
1070 int ret;
1071
Laurent Pinchartdc486652011-07-13 12:13:47 +02001072 /* Enable VSync End interrupt and be careful not to acknowledge any
1073 * pending interrupt.
1074 */
Phil Edworthy40331b22010-02-15 13:57:49 +00001075 ldintr = lcdc_read(ch->lcdc, _LDINTR);
Laurent Pinchartdc486652011-07-13 12:13:47 +02001076 ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
Phil Edworthy40331b22010-02-15 13:57:49 +00001077 lcdc_write(ch->lcdc, _LDINTR, ldintr);
1078
1079 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
1080 msecs_to_jiffies(100));
1081 if (!ret)
1082 return -ETIMEDOUT;
1083
1084 return 0;
1085}
1086
1087static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
1088 unsigned long arg)
1089{
1090 int retval;
1091
1092 switch (cmd) {
1093 case FBIO_WAITFORVSYNC:
1094 retval = sh_mobile_wait_for_vsync(info);
1095 break;
1096
1097 default:
1098 retval = -ENOIOCTLCMD;
1099 break;
1100 }
1101 return retval;
1102}
1103
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001104static void sh_mobile_fb_reconfig(struct fb_info *info)
1105{
1106 struct sh_mobile_lcdc_chan *ch = info->par;
1107 struct fb_videomode mode1, mode2;
1108 struct fb_event event;
1109 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1110
1111 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1112 /* More framebuffer users are active */
1113 return;
1114
1115 fb_var_to_videomode(&mode1, &ch->display_var);
1116 fb_var_to_videomode(&mode2, &info->var);
1117
1118 if (fb_mode_is_equal(&mode1, &mode2))
1119 return;
1120
1121 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1122 if (fb_set_var(info, &ch->display_var) < 0)
1123 /* Couldn't reconfigure, hopefully, can continue as before */
1124 return;
1125
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001126 /*
1127 * fb_set_var() calls the notifier change internally, only if
1128 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1129 * user event, we have to call the chain ourselves.
1130 */
1131 event.info = info;
Arnd Hannemanncc267ec2010-11-15 21:43:22 +00001132 event.data = &mode1;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001133 fb_notifier_call_chain(evnt, &event);
1134}
1135
1136/*
1137 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1138 * user == 1, or with console sem held, if user == 0.
1139 */
1140static int sh_mobile_release(struct fb_info *info, int user)
1141{
1142 struct sh_mobile_lcdc_chan *ch = info->par;
1143
1144 mutex_lock(&ch->open_lock);
1145 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1146
1147 ch->use_count--;
1148
1149 /* Nothing to reconfigure, when called from fbcon */
1150 if (user) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001151 console_lock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001152 sh_mobile_fb_reconfig(info);
Torben Hohnac751ef2011-01-25 15:07:35 -08001153 console_unlock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001154 }
1155
1156 mutex_unlock(&ch->open_lock);
1157
1158 return 0;
1159}
1160
1161static int sh_mobile_open(struct fb_info *info, int user)
1162{
1163 struct sh_mobile_lcdc_chan *ch = info->par;
1164
1165 mutex_lock(&ch->open_lock);
1166 ch->use_count++;
1167
1168 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1169 mutex_unlock(&ch->open_lock);
1170
1171 return 0;
1172}
1173
1174static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1175{
1176 struct sh_mobile_lcdc_chan *ch = info->par;
Magnus Damm417d4822011-01-05 10:21:00 +00001177 struct sh_mobile_lcdc_priv *p = ch->lcdc;
Laurent Pinchart03862192011-08-31 13:00:53 +02001178 unsigned int best_dist = (unsigned int)-1;
1179 unsigned int best_xres = 0;
1180 unsigned int best_yres = 0;
1181 unsigned int i;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001182
Laurent Pinchart03862192011-08-31 13:00:53 +02001183 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001184 return -EINVAL;
Laurent Pinchart03862192011-08-31 13:00:53 +02001185
1186 /* If board code provides us with a list of available modes, make sure
1187 * we use one of them. Find the mode closest to the requested one. The
1188 * distance between two modes is defined as the size of the
1189 * non-overlapping parts of the two rectangles.
1190 */
1191 for (i = 0; i < ch->cfg.num_cfg; ++i) {
1192 const struct fb_videomode *mode = &ch->cfg.lcd_cfg[i];
1193 unsigned int dist;
1194
1195 /* We can only round up. */
1196 if (var->xres > mode->xres || var->yres > mode->yres)
1197 continue;
1198
1199 dist = var->xres * var->yres + mode->xres * mode->yres
1200 - 2 * min(var->xres, mode->xres)
1201 * min(var->yres, mode->yres);
1202
1203 if (dist < best_dist) {
1204 best_xres = mode->xres;
1205 best_yres = mode->yres;
1206 best_dist = dist;
1207 }
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001208 }
Magnus Damm417d4822011-01-05 10:21:00 +00001209
Laurent Pinchart03862192011-08-31 13:00:53 +02001210 /* If no available mode can be used, return an error. */
1211 if (ch->cfg.num_cfg != 0) {
1212 if (best_dist == (unsigned int)-1)
1213 return -EINVAL;
1214
1215 var->xres = best_xres;
1216 var->yres = best_yres;
1217 }
1218
1219 /* Make sure the virtual resolution is at least as big as the visible
1220 * resolution.
1221 */
1222 if (var->xres_virtual < var->xres)
1223 var->xres_virtual = var->xres;
1224 if (var->yres_virtual < var->yres)
1225 var->yres_virtual = var->yres;
1226
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001227 if (sh_mobile_format_is_fourcc(var)) {
1228 switch (var->grayscale) {
1229 case V4L2_PIX_FMT_NV12:
1230 case V4L2_PIX_FMT_NV21:
1231 var->bits_per_pixel = 12;
1232 break;
1233 case V4L2_PIX_FMT_RGB565:
1234 case V4L2_PIX_FMT_NV16:
1235 case V4L2_PIX_FMT_NV61:
1236 var->bits_per_pixel = 16;
1237 break;
1238 case V4L2_PIX_FMT_BGR24:
1239 case V4L2_PIX_FMT_NV24:
1240 case V4L2_PIX_FMT_NV42:
1241 var->bits_per_pixel = 24;
1242 break;
1243 case V4L2_PIX_FMT_BGR32:
1244 var->bits_per_pixel = 32;
1245 break;
1246 default:
1247 return -EINVAL;
1248 }
Laurent Pinchart03862192011-08-31 13:00:53 +02001249
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001250 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1251 * respectively.
1252 */
1253 if (!sh_mobile_format_is_yuv(var))
1254 var->colorspace = V4L2_COLORSPACE_SRGB;
1255 else if (var->colorspace != V4L2_COLORSPACE_REC709)
1256 var->colorspace = V4L2_COLORSPACE_JPEG;
1257 } else {
1258 if (var->bits_per_pixel <= 16) { /* RGB 565 */
1259 var->bits_per_pixel = 16;
1260 var->red.offset = 11;
1261 var->red.length = 5;
1262 var->green.offset = 5;
1263 var->green.length = 6;
1264 var->blue.offset = 0;
1265 var->blue.length = 5;
1266 var->transp.offset = 0;
1267 var->transp.length = 0;
1268 } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
1269 var->bits_per_pixel = 24;
1270 var->red.offset = 16;
1271 var->red.length = 8;
1272 var->green.offset = 8;
1273 var->green.length = 8;
1274 var->blue.offset = 0;
1275 var->blue.length = 8;
1276 var->transp.offset = 0;
1277 var->transp.length = 0;
1278 } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
1279 var->bits_per_pixel = 32;
1280 var->red.offset = 16;
1281 var->red.length = 8;
1282 var->green.offset = 8;
1283 var->green.length = 8;
1284 var->blue.offset = 0;
1285 var->blue.length = 8;
1286 var->transp.offset = 24;
1287 var->transp.length = 8;
1288 } else
1289 return -EINVAL;
1290
1291 var->red.msb_right = 0;
1292 var->green.msb_right = 0;
1293 var->blue.msb_right = 0;
1294 var->transp.msb_right = 0;
1295 }
Laurent Pinchart03862192011-08-31 13:00:53 +02001296
1297 /* Make sure we don't exceed our allocated memory. */
1298 if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
1299 info->fix.smem_len)
1300 return -EINVAL;
1301
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001302 /* only accept the forced_fourcc for dual channel configurations */
1303 if (p->forced_fourcc &&
1304 p->forced_fourcc != sh_mobile_format_fourcc(var))
Magnus Damm417d4822011-01-05 10:21:00 +00001305 return -EINVAL;
1306
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001307 return 0;
1308}
Phil Edworthy40331b22010-02-15 13:57:49 +00001309
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001310static int sh_mobile_set_par(struct fb_info *info)
1311{
1312 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchart91fba482011-08-31 13:00:56 +02001313 u32 line_length = info->fix.line_length;
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001314 int ret;
1315
1316 sh_mobile_lcdc_stop(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001317
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001318 if (sh_mobile_format_is_yuv(&info->var))
Laurent Pinchart91fba482011-08-31 13:00:56 +02001319 info->fix.line_length = info->var.xres;
1320 else
1321 info->fix.line_length = info->var.xres
1322 * info->var.bits_per_pixel / 8;
1323
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001324 ret = sh_mobile_lcdc_start(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001325 if (ret < 0) {
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001326 dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001327 info->fix.line_length = line_length;
1328 }
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001329
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001330 if (sh_mobile_format_is_fourcc(&info->var)) {
1331 info->fix.type = FB_TYPE_FOURCC;
1332 info->fix.visual = FB_VISUAL_FOURCC;
1333 } else {
1334 info->fix.type = FB_TYPE_PACKED_PIXELS;
1335 info->fix.visual = FB_VISUAL_TRUECOLOR;
1336 }
1337
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001338 return ret;
1339}
1340
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001341/*
1342 * Screen blanking. Behavior is as follows:
1343 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1344 * FB_BLANK_NORMAL: screen blanked, clocks enabled
1345 * FB_BLANK_VSYNC,
1346 * FB_BLANK_HSYNC,
1347 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1348 */
1349static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
1350{
1351 struct sh_mobile_lcdc_chan *ch = info->par;
1352 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1353
1354 /* blank the screen? */
1355 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
1356 struct fb_fillrect rect = {
1357 .width = info->var.xres,
1358 .height = info->var.yres,
1359 };
1360 sh_mobile_lcdc_fillrect(info, &rect);
1361 }
1362 /* turn clocks on? */
1363 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
1364 sh_mobile_lcdc_clk_on(p);
1365 }
1366 /* turn clocks off? */
1367 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
1368 /* make sure the screen is updated with the black fill before
1369 * switching the clocks off. one vsync is not enough since
1370 * blanking may occur in the middle of a refresh. deferred io
1371 * mode will reenable the clocks and update the screen in time,
1372 * so it does not need this. */
1373 if (!info->fbdefio) {
1374 sh_mobile_wait_for_vsync(info);
1375 sh_mobile_wait_for_vsync(info);
1376 }
1377 sh_mobile_lcdc_clk_off(p);
1378 }
1379
1380 ch->blank_status = blank;
1381 return 0;
1382}
1383
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001384static struct fb_ops sh_mobile_lcdc_ops = {
Phil Edworthy9dd38812009-09-15 12:00:18 +00001385 .owner = THIS_MODULE,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001386 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
Magnus Damm2540c112008-12-17 17:29:49 +09001387 .fb_read = fb_sys_read,
1388 .fb_write = fb_sys_write,
Magnus Damm85645572008-12-19 15:34:41 +09001389 .fb_fillrect = sh_mobile_lcdc_fillrect,
1390 .fb_copyarea = sh_mobile_lcdc_copyarea,
1391 .fb_imageblit = sh_mobile_lcdc_imageblit,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001392 .fb_blank = sh_mobile_lcdc_blank,
Phil Edworthy9dd38812009-09-15 12:00:18 +00001393 .fb_pan_display = sh_mobile_fb_pan_display,
Phil Edworthy40331b22010-02-15 13:57:49 +00001394 .fb_ioctl = sh_mobile_ioctl,
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001395 .fb_open = sh_mobile_open,
1396 .fb_release = sh_mobile_release,
1397 .fb_check_var = sh_mobile_check_var,
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001398 .fb_set_par = sh_mobile_set_par,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001399};
1400
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001401/* -----------------------------------------------------------------------------
1402 * Backlight
1403 */
1404
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001405static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
1406{
1407 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001408 int brightness = bdev->props.brightness;
1409
1410 if (bdev->props.power != FB_BLANK_UNBLANK ||
1411 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
1412 brightness = 0;
1413
Laurent Pinchart43059b02011-09-11 22:59:04 +02001414 return ch->cfg.bl_info.set_brightness(brightness);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001415}
1416
1417static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
1418{
1419 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001420
Laurent Pinchart43059b02011-09-11 22:59:04 +02001421 return ch->cfg.bl_info.get_brightness();
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001422}
1423
1424static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
1425 struct fb_info *info)
1426{
1427 return (info->bl_dev == bdev);
1428}
1429
1430static struct backlight_ops sh_mobile_lcdc_bl_ops = {
1431 .options = BL_CORE_SUSPENDRESUME,
1432 .update_status = sh_mobile_lcdc_update_bl,
1433 .get_brightness = sh_mobile_lcdc_get_brightness,
1434 .check_fb = sh_mobile_lcdc_check_fb,
1435};
1436
1437static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
1438 struct sh_mobile_lcdc_chan *ch)
1439{
1440 struct backlight_device *bl;
1441
1442 bl = backlight_device_register(ch->cfg.bl_info.name, parent, ch,
1443 &sh_mobile_lcdc_bl_ops, NULL);
Dan Carpenterbeee1f22011-03-21 15:03:13 +00001444 if (IS_ERR(bl)) {
1445 dev_err(parent, "unable to register backlight device: %ld\n",
1446 PTR_ERR(bl));
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001447 return NULL;
1448 }
1449
1450 bl->props.max_brightness = ch->cfg.bl_info.max_brightness;
1451 bl->props.brightness = bl->props.max_brightness;
1452 backlight_update_status(bl);
1453
1454 return bl;
1455}
1456
1457static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
1458{
1459 backlight_device_unregister(bdev);
1460}
1461
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001462/* -----------------------------------------------------------------------------
1463 * Power management
1464 */
1465
Magnus Damm2feb0752009-03-13 15:36:55 +00001466static int sh_mobile_lcdc_suspend(struct device *dev)
1467{
1468 struct platform_device *pdev = to_platform_device(dev);
1469
1470 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
1471 return 0;
1472}
1473
1474static int sh_mobile_lcdc_resume(struct device *dev)
1475{
1476 struct platform_device *pdev = to_platform_device(dev);
1477
1478 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
1479}
1480
Magnus Damm0246c472009-08-14 10:49:08 +00001481static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
1482{
1483 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001484 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001485
1486 /* turn off LCDC hardware */
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001487 lcdc_write(priv, _LDCNT1R, 0);
1488
Magnus Damm0246c472009-08-14 10:49:08 +00001489 return 0;
1490}
1491
1492static int sh_mobile_lcdc_runtime_resume(struct device *dev)
1493{
1494 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001495 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001496
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001497 __sh_mobile_lcdc_start(priv);
Magnus Damm0246c472009-08-14 10:49:08 +00001498
1499 return 0;
1500}
1501
Alexey Dobriyan47145212009-12-14 18:00:08 -08001502static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
Magnus Damm2feb0752009-03-13 15:36:55 +00001503 .suspend = sh_mobile_lcdc_suspend,
1504 .resume = sh_mobile_lcdc_resume,
Magnus Damm0246c472009-08-14 10:49:08 +00001505 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
1506 .runtime_resume = sh_mobile_lcdc_runtime_resume,
Magnus Damm2feb0752009-03-13 15:36:55 +00001507};
1508
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001509/* -----------------------------------------------------------------------------
1510 * Framebuffer notifier
1511 */
1512
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001513/* locking: called with info->lock held */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001514static int sh_mobile_lcdc_notify(struct notifier_block *nb,
1515 unsigned long action, void *data)
1516{
1517 struct fb_event *event = data;
1518 struct fb_info *info = event->info;
1519 struct sh_mobile_lcdc_chan *ch = info->par;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001520
1521 if (&ch->lcdc->notifier != nb)
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001522 return NOTIFY_DONE;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001523
1524 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
1525 __func__, action, event->data);
1526
1527 switch(action) {
1528 case FB_EVENT_SUSPEND:
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001529 sh_mobile_lcdc_display_off(ch);
Guennadi Liakhovetskiafe417c2010-09-03 07:20:39 +00001530 sh_mobile_lcdc_stop(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001531 break;
1532 case FB_EVENT_RESUME:
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001533 mutex_lock(&ch->open_lock);
1534 sh_mobile_fb_reconfig(info);
1535 mutex_unlock(&ch->open_lock);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001536
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001537 sh_mobile_lcdc_display_on(ch);
Guennadi Liakhovetskiebe5e122011-05-05 16:33:40 +00001538 sh_mobile_lcdc_start(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001539 }
1540
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001541 return NOTIFY_OK;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001542}
1543
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001544/* -----------------------------------------------------------------------------
1545 * Probe/remove and driver init/exit
1546 */
1547
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001548static const struct fb_videomode default_720p __devinitconst = {
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001549 .name = "HDMI 720p",
1550 .xres = 1280,
1551 .yres = 720,
1552
1553 .left_margin = 220,
1554 .right_margin = 110,
1555 .hsync_len = 40,
1556
1557 .upper_margin = 20,
1558 .lower_margin = 5,
1559 .vsync_len = 5,
1560
1561 .pixclock = 13468,
1562 .refresh = 60,
1563 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
1564};
1565
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001566static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1567{
1568 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1569 struct fb_info *info;
1570 int i;
1571
1572 fb_unregister_client(&priv->notifier);
1573
1574 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
1575 if (priv->ch[i].info && priv->ch[i].info->dev)
1576 unregister_framebuffer(priv->ch[i].info);
1577
1578 sh_mobile_lcdc_stop(priv);
1579
1580 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001581 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001582
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001583 info = ch->info;
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001584 if (!info || !info->device)
1585 continue;
1586
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02001587 if (ch->tx_dev) {
1588 ch->tx_dev->lcdc = NULL;
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001589 module_put(ch->cfg.tx_dev->dev.driver->owner);
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02001590 }
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001591
1592 if (ch->sglist)
1593 vfree(ch->sglist);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001594
1595 if (info->screen_base)
1596 dma_free_coherent(&pdev->dev, info->fix.smem_len,
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001597 info->screen_base, ch->dma_handle);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001598 fb_dealloc_cmap(&info->cmap);
1599 framebuffer_release(info);
1600 }
1601
1602 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1603 if (priv->ch[i].bl)
1604 sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
1605 }
1606
Laurent Pinchart4774c122011-09-07 15:47:07 +02001607 if (priv->dot_clk) {
1608 pm_runtime_disable(&pdev->dev);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001609 clk_put(priv->dot_clk);
Laurent Pinchart4774c122011-09-07 15:47:07 +02001610 }
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001611
1612 if (priv->base)
1613 iounmap(priv->base);
1614
1615 if (priv->irq)
1616 free_irq(priv->irq, priv);
1617 kfree(priv);
1618 return 0;
1619}
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001620
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001621static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001622{
1623 int interface_type = ch->cfg.interface_type;
1624
1625 switch (interface_type) {
1626 case RGB8:
1627 case RGB9:
1628 case RGB12A:
1629 case RGB12B:
1630 case RGB16:
1631 case RGB18:
1632 case RGB24:
1633 case SYS8A:
1634 case SYS8B:
1635 case SYS8C:
1636 case SYS8D:
1637 case SYS9:
1638 case SYS12:
1639 case SYS16A:
1640 case SYS16B:
1641 case SYS16C:
1642 case SYS18:
1643 case SYS24:
1644 break;
1645 default:
1646 return -EINVAL;
1647 }
1648
1649 /* SUBLCD only supports SYS interface */
1650 if (lcdc_chan_is_sublcd(ch)) {
1651 if (!(interface_type & LDMT1R_IFM))
1652 return -EINVAL;
1653
1654 interface_type &= ~LDMT1R_IFM;
1655 }
1656
1657 ch->ldmt1r_value = interface_type;
1658 return 0;
1659}
1660
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001661static int __devinit
1662sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv *priv,
1663 struct sh_mobile_lcdc_chan *ch)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001664{
1665 struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
1666 const struct fb_videomode *max_mode;
1667 const struct fb_videomode *mode;
1668 struct fb_var_screeninfo *var;
1669 struct fb_info *info;
1670 unsigned int max_size;
1671 int num_cfg;
1672 void *buf;
1673 int ret;
1674 int i;
1675
Laurent Pincharta67472a2011-08-31 13:00:59 +02001676 mutex_init(&ch->open_lock);
Laurent Pinchartecd29942011-09-18 14:14:46 +02001677 ch->notify = sh_mobile_lcdc_display_notify;
Laurent Pincharta67472a2011-08-31 13:00:59 +02001678
1679 /* Allocate the frame buffer device. */
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001680 ch->info = framebuffer_alloc(0, priv->dev);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001681 if (!ch->info) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001682 dev_err(priv->dev, "unable to allocate fb_info\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001683 return -ENOMEM;
1684 }
1685
1686 info = ch->info;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001687 info->fbops = &sh_mobile_lcdc_ops;
1688 info->par = ch;
Laurent Pincharta67472a2011-08-31 13:00:59 +02001689 info->pseudo_palette = &ch->pseudo_palette;
1690 info->flags = FBINFO_FLAG_DEFAULT;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001691
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001692 if (cfg->tx_dev) {
1693 if (!cfg->tx_dev->dev.driver ||
1694 !try_module_get(cfg->tx_dev->dev.driver->owner)) {
1695 dev_warn(priv->dev,
1696 "unable to get transmitter device\n");
1697 return -EINVAL;
1698 }
1699 ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02001700 ch->tx_dev->lcdc = ch;
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001701 }
1702
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001703 /* Iterate through the modes to validate them and find the highest
1704 * resolution.
1705 */
1706 max_mode = NULL;
1707 max_size = 0;
1708
1709 for (i = 0, mode = cfg->lcd_cfg; i < cfg->num_cfg; i++, mode++) {
1710 unsigned int size = mode->yres * mode->xres;
1711
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001712 /* NV12/NV21 buffers must have even number of lines */
1713 if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
1714 cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001715 dev_err(priv->dev, "yres must be multiple of 2 for "
1716 "YCbCr420 mode.\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001717 return -EINVAL;
1718 }
1719
1720 if (size > max_size) {
1721 max_mode = mode;
1722 max_size = size;
1723 }
1724 }
1725
1726 if (!max_size)
1727 max_size = MAX_XRES * MAX_YRES;
1728 else
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001729 dev_dbg(priv->dev, "Found largest videomode %ux%u\n",
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001730 max_mode->xres, max_mode->yres);
1731
Laurent Pincharta67472a2011-08-31 13:00:59 +02001732 /* Create the mode list. */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001733 if (cfg->lcd_cfg == NULL) {
1734 mode = &default_720p;
1735 num_cfg = 1;
1736 } else {
1737 mode = cfg->lcd_cfg;
1738 num_cfg = cfg->num_cfg;
1739 }
1740
1741 fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
1742
Laurent Pincharta67472a2011-08-31 13:00:59 +02001743 /* Initialize variable screen information using the first mode as
1744 * default. The default Y virtual resolution is twice the panel size to
1745 * allow for double-buffering.
1746 */
1747 var = &info->var;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001748 fb_videomode_to_var(var, mode);
Laurent Pinchartafaad832011-09-11 22:59:04 +02001749 var->width = cfg->panel_cfg.width;
1750 var->height = cfg->panel_cfg.height;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001751 var->yres_virtual = var->yres * 2;
1752 var->activate = FB_ACTIVATE_NOW;
1753
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001754 switch (cfg->fourcc) {
1755 case V4L2_PIX_FMT_RGB565:
1756 var->bits_per_pixel = 16;
1757 break;
1758 case V4L2_PIX_FMT_BGR24:
1759 var->bits_per_pixel = 24;
1760 break;
1761 case V4L2_PIX_FMT_BGR32:
1762 var->bits_per_pixel = 32;
1763 break;
1764 default:
1765 var->grayscale = cfg->fourcc;
1766 break;
1767 }
1768
1769 /* Make sure the memory size check won't fail. smem_len is initialized
1770 * later based on var.
1771 */
1772 info->fix.smem_len = UINT_MAX;
Laurent Pincharta67472a2011-08-31 13:00:59 +02001773 ret = sh_mobile_check_var(var, info);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001774 if (ret)
1775 return ret;
1776
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001777 max_size = max_size * var->bits_per_pixel / 8 * 2;
1778
Laurent Pincharta67472a2011-08-31 13:00:59 +02001779 /* Allocate frame buffer memory and color map. */
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001780 buf = dma_alloc_coherent(priv->dev, max_size, &ch->dma_handle,
1781 GFP_KERNEL);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001782 if (!buf) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001783 dev_err(priv->dev, "unable to allocate buffer\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001784 return -ENOMEM;
1785 }
1786
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001787 ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1788 if (ret < 0) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001789 dev_err(priv->dev, "unable to allocate cmap\n");
1790 dma_free_coherent(priv->dev, max_size, buf, ch->dma_handle);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001791 return ret;
1792 }
1793
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001794 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1795 * for NV12 and NV21.
1796 */
1797 info->fix = sh_mobile_lcdc_fix;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001798 info->fix.smem_start = ch->dma_handle;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001799 info->fix.smem_len = max_size;
1800 if (cfg->fourcc == V4L2_PIX_FMT_NV12 ||
1801 cfg->fourcc == V4L2_PIX_FMT_NV21)
1802 info->fix.ypanstep = 2;
1803
1804 if (sh_mobile_format_is_yuv(var)) {
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001805 info->fix.line_length = var->xres;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001806 info->fix.visual = FB_VISUAL_FOURCC;
1807 } else {
1808 info->fix.line_length = var->xres * var->bits_per_pixel / 8;
1809 info->fix.visual = FB_VISUAL_TRUECOLOR;
1810 }
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001811
1812 info->screen_base = buf;
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001813 info->device = priv->dev;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001814 ch->display_var = *var;
1815
1816 return 0;
1817}
1818
Uwe Kleine-Königc2e13032010-02-04 20:56:51 +01001819static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001820{
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001821 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001822 struct sh_mobile_lcdc_priv *priv;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001823 struct resource *res;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001824 int num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001825 int error;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001826 int i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001827
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001828 if (!pdata) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001829 dev_err(&pdev->dev, "no platform data defined\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001830 return -EINVAL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001831 }
1832
1833 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Damm85645572008-12-19 15:34:41 +09001834 i = platform_get_irq(pdev, 0);
1835 if (!res || i < 0) {
1836 dev_err(&pdev->dev, "cannot get platform resources\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001837 return -ENOENT;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001838 }
1839
1840 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1841 if (!priv) {
1842 dev_err(&pdev->dev, "cannot allocate device data\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001843 return -ENOMEM;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001844 }
1845
Laurent Pinchart4774c122011-09-07 15:47:07 +02001846 priv->dev = &pdev->dev;
1847 priv->meram_dev = pdata->meram_dev;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001848 platform_set_drvdata(pdev, priv);
1849
Yong Zhangf8798cc2011-09-22 16:59:16 +08001850 error = request_irq(i, sh_mobile_lcdc_irq, 0,
Kay Sievers7ad33e72009-03-24 16:38:21 -07001851 dev_name(&pdev->dev), priv);
Magnus Damm85645572008-12-19 15:34:41 +09001852 if (error) {
1853 dev_err(&pdev->dev, "unable to request irq\n");
1854 goto err1;
1855 }
1856
1857 priv->irq = i;
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +00001858 atomic_set(&priv->hw_usecnt, -1);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001859
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001860 for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
1861 struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001862
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001863 ch->lcdc = priv;
1864 memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
1865
1866 error = sh_mobile_lcdc_check_interface(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001867 if (error) {
1868 dev_err(&pdev->dev, "unsupported interface type\n");
1869 goto err1;
1870 }
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001871 init_waitqueue_head(&ch->frame_end_wait);
1872 init_completion(&ch->vsync_completion);
1873 ch->pan_offset = 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001874
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001875 /* probe the backlight is there is one defined */
1876 if (ch->cfg.bl_info.max_brightness)
1877 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
1878
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001879 switch (pdata->ch[i].chan) {
1880 case LCDC_CHAN_MAINLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001881 ch->enabled = LDCNT2R_ME;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001882 ch->reg_offs = lcdc_offs_mainlcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001883 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001884 break;
1885 case LCDC_CHAN_SUBLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001886 ch->enabled = LDCNT2R_SE;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001887 ch->reg_offs = lcdc_offs_sublcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001888 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001889 break;
1890 }
1891 }
1892
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001893 if (!num_channels) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001894 dev_err(&pdev->dev, "no channels defined\n");
1895 error = -EINVAL;
1896 goto err1;
1897 }
1898
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001899 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001900 if (num_channels == 2)
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001901 priv->forced_fourcc = pdata->ch[0].fourcc;
Magnus Damm417d4822011-01-05 10:21:00 +00001902
Guennadi Liakhovetskidba6f382010-06-30 09:26:35 +00001903 priv->base = ioremap_nocache(res->start, resource_size(res));
1904 if (!priv->base)
1905 goto err1;
1906
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001907 error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001908 if (error) {
1909 dev_err(&pdev->dev, "unable to setup clocks\n");
1910 goto err1;
1911 }
1912
Laurent Pinchart4774c122011-09-07 15:47:07 +02001913 /* Enable runtime PM. */
1914 pm_runtime_enable(&pdev->dev);
Damian7caa4342011-05-18 11:10:07 +00001915
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001916 for (i = 0; i < num_channels; i++) {
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001917 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001918
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001919 error = sh_mobile_lcdc_channel_init(priv, ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001920 if (error)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001921 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001922 }
1923
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001924 error = sh_mobile_lcdc_start(priv);
1925 if (error) {
1926 dev_err(&pdev->dev, "unable to start hardware\n");
1927 goto err1;
1928 }
1929
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001930 for (i = 0; i < num_channels; i++) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00001931 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001932 struct fb_info *info = ch->info;
Paul Mundt1c6a3072009-07-01 06:50:31 +00001933
1934 if (info->fbdefio) {
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001935 ch->sglist = vmalloc(sizeof(struct scatterlist) *
Paul Mundt1c6a3072009-07-01 06:50:31 +00001936 info->fix.smem_len >> PAGE_SHIFT);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001937 if (!ch->sglist) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00001938 dev_err(&pdev->dev, "cannot allocate sglist\n");
1939 goto err1;
1940 }
1941 }
1942
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001943 info->bl_dev = ch->bl;
1944
Paul Mundt1c6a3072009-07-01 06:50:31 +00001945 error = register_framebuffer(info);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001946 if (error < 0)
1947 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001948
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001949 dev_info(&pdev->dev, "registered %s/%s as %dx%d %dbpp.\n",
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001950 pdev->name, (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
1951 "mainlcd" : "sublcd", info->var.xres, info->var.yres,
1952 info->var.bits_per_pixel);
Magnus Damm85645572008-12-19 15:34:41 +09001953
1954 /* deferred io mode: disable clock to save power */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001955 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
Magnus Damm85645572008-12-19 15:34:41 +09001956 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001957 }
1958
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001959 /* Failure ignored */
1960 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
1961 fb_register_client(&priv->notifier);
1962
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001963 return 0;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001964err1:
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001965 sh_mobile_lcdc_remove(pdev);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001966
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001967 return error;
1968}
1969
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001970static struct platform_driver sh_mobile_lcdc_driver = {
1971 .driver = {
1972 .name = "sh_mobile_lcdc_fb",
1973 .owner = THIS_MODULE,
Magnus Damm2feb0752009-03-13 15:36:55 +00001974 .pm = &sh_mobile_lcdc_dev_pm_ops,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001975 },
1976 .probe = sh_mobile_lcdc_probe,
1977 .remove = sh_mobile_lcdc_remove,
1978};
1979
Axel Lin4277f2c2011-11-26 10:25:54 +08001980module_platform_driver(sh_mobile_lcdc_driver);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001981
1982MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1983MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1984MODULE_LICENSE("GPL v2");