blob: a805f96eb1639989c801d2fe1d60dc6b3cc0dcc7 [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/* -----------------------------------------------------------------------------
227 * Sys panel and deferred I/O
228 */
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;
Magnus Dammef61aae2009-12-07 14:20:06 +0000292 struct sh_mobile_lcdc_board_cfg *bcfg = &ch->cfg.board_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);
Magnus Dammef61aae2009-12-07 14:20:06 +0000317 if (bcfg->start_transfer)
318 bcfg->start_transfer(bcfg->board_data, ch,
319 &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200320 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900321 dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
Magnus Dammef61aae2009-12-07 14:20:06 +0000322 } else {
323 if (bcfg->start_transfer)
324 bcfg->start_transfer(bcfg->board_data, ch,
325 &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200326 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
Magnus Dammef61aae2009-12-07 14:20:06 +0000327 }
Magnus Damm85645572008-12-19 15:34:41 +0900328}
329
330static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
331{
332 struct fb_deferred_io *fbdefio = info->fbdefio;
333
334 if (fbdefio)
335 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
336}
337
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200338/* -----------------------------------------------------------------------------
339 * Format helpers
340 */
341
342static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
343{
344 if (var->grayscale > 1)
345 return var->grayscale;
346
347 switch (var->bits_per_pixel) {
348 case 16:
349 return V4L2_PIX_FMT_RGB565;
350 case 24:
351 return V4L2_PIX_FMT_BGR24;
352 case 32:
353 return V4L2_PIX_FMT_BGR32;
354 default:
355 return 0;
356 }
357}
358
359static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
360{
361 return var->grayscale > 1;
362}
363
364static bool sh_mobile_format_is_yuv(const struct fb_var_screeninfo *var)
365{
366 if (var->grayscale <= 1)
367 return false;
368
369 switch (var->grayscale) {
370 case V4L2_PIX_FMT_NV12:
371 case V4L2_PIX_FMT_NV21:
372 case V4L2_PIX_FMT_NV16:
373 case V4L2_PIX_FMT_NV61:
374 case V4L2_PIX_FMT_NV24:
375 case V4L2_PIX_FMT_NV42:
376 return true;
377
378 default:
379 return false;
380 }
381}
382
383/* -----------------------------------------------------------------------------
384 * Start, stop and IRQ
385 */
386
Magnus Damm85645572008-12-19 15:34:41 +0900387static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
388{
389 struct sh_mobile_lcdc_priv *priv = data;
Magnus Damm2feb0752009-03-13 15:36:55 +0000390 struct sh_mobile_lcdc_chan *ch;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000391 unsigned long ldintr;
Magnus Damm2feb0752009-03-13 15:36:55 +0000392 int is_sub;
393 int k;
Magnus Damm85645572008-12-19 15:34:41 +0900394
Laurent Pinchartdc486652011-07-13 12:13:47 +0200395 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
396 ldintr = lcdc_read(priv, _LDINTR);
397 lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
Magnus Damm85645572008-12-19 15:34:41 +0900398
Magnus Damm2feb0752009-03-13 15:36:55 +0000399 /* figure out if this interrupt is for main or sub lcd */
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200400 is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
Magnus Damm2feb0752009-03-13 15:36:55 +0000401
Phil Edworthy9dd38812009-09-15 12:00:18 +0000402 /* wake up channel and disable clocks */
Magnus Damm2feb0752009-03-13 15:36:55 +0000403 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
404 ch = &priv->ch[k];
405
406 if (!ch->enabled)
407 continue;
408
Laurent Pinchartdc486652011-07-13 12:13:47 +0200409 /* Frame End */
Phil Edworthy9dd38812009-09-15 12:00:18 +0000410 if (ldintr & LDINTR_FS) {
411 if (is_sub == lcdc_chan_is_sublcd(ch)) {
412 ch->frame_end = 1;
413 wake_up(&ch->frame_end_wait);
Magnus Damm2feb0752009-03-13 15:36:55 +0000414
Phil Edworthy9dd38812009-09-15 12:00:18 +0000415 sh_mobile_lcdc_clk_off(priv);
416 }
417 }
418
419 /* VSYNC End */
Phil Edworthy40331b22010-02-15 13:57:49 +0000420 if (ldintr & LDINTR_VES)
421 complete(&ch->vsync_completion);
Magnus Damm2feb0752009-03-13 15:36:55 +0000422 }
423
Magnus Damm85645572008-12-19 15:34:41 +0900424 return IRQ_HANDLED;
425}
426
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700427static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
428 int start)
429{
430 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
431 int k;
432
433 /* start or stop the lcdc */
434 if (start)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200435 lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700436 else
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200437 lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700438
439 /* wait until power is applied/stopped on all channels */
440 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
441 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
442 while (1) {
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200443 tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
444 & LDPMR_LPS;
445 if (start && tmp == LDPMR_LPS)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700446 break;
447 if (!start && tmp == 0)
448 break;
449 cpu_relax();
450 }
451
452 if (!start)
453 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
454}
455
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000456static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
457{
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000458 struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
459 unsigned long h_total, hsync_pos, display_h_total;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000460 u32 tmp;
461
462 tmp = ch->ldmt1r_value;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200463 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
464 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
465 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
466 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
467 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
468 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
469 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000470 lcdc_write_chan(ch, LDMT1R, tmp);
471
472 /* setup SYS bus */
473 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
474 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
475
476 /* horizontal configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000477 h_total = display_var->xres + display_var->hsync_len +
478 display_var->left_margin + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000479 tmp = h_total / 8; /* HTCN */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000480 tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000481 lcdc_write_chan(ch, LDHCNR, tmp);
482
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000483 hsync_pos = display_var->xres + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000484 tmp = hsync_pos / 8; /* HSYNP */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000485 tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000486 lcdc_write_chan(ch, LDHSYNR, tmp);
487
488 /* vertical configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000489 tmp = display_var->yres + display_var->vsync_len +
490 display_var->upper_margin + display_var->lower_margin; /* VTLN */
491 tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000492 lcdc_write_chan(ch, LDVLNR, tmp);
493
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000494 tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
495 tmp |= display_var->vsync_len << 16; /* VSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000496 lcdc_write_chan(ch, LDVSYNR, tmp);
497
498 /* Adjust horizontal synchronisation for HDMI */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000499 display_h_total = display_var->xres + display_var->hsync_len +
500 display_var->left_margin + display_var->right_margin;
501 tmp = ((display_var->xres & 7) << 24) |
502 ((display_h_total & 7) << 16) |
503 ((display_var->hsync_len & 7) << 8) |
Kuninori Morimoto41e583c2011-11-08 20:33:29 -0800504 (hsync_pos & 7);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000505 lcdc_write_chan(ch, LDHAJR, tmp);
506}
507
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200508/*
509 * __sh_mobile_lcdc_start - Configure and tart the LCDC
510 * @priv: LCDC device
511 *
512 * Configure all enabled channels and start the LCDC device. All external
513 * devices (clocks, MERAM, panels, ...) are not touched by this function.
514 */
515static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700516{
517 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700518 unsigned long tmp;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200519 int k, m;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700520
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200521 /* Enable LCDC channels. Read data from external memory, avoid using the
522 * BEU for now.
523 */
524 lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
Magnus Damm85645572008-12-19 15:34:41 +0900525
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200526 /* Stop the LCDC first and disable all interrupts. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700527 sh_mobile_lcdc_start_stop(priv, 0);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200528 lcdc_write(priv, _LDINTR, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700529
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200530 /* Configure power supply, dot clocks and start them. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700531 tmp = priv->lddckr;
532 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
533 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200534 if (!ch->enabled)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700535 continue;
536
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200537 /* Power supply */
538 lcdc_write_chan(ch, LDPMR, 0);
539
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700540 m = ch->cfg.clock_divider;
541 if (!m)
542 continue;
543
Laurent Pinchart505c7de52011-07-13 12:13:47 +0200544 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
545 * denominator.
546 */
547 lcdc_write_chan(ch, LDDCKPAT1R, 0);
548 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
549
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700550 if (m == 1)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200551 m = LDDCKR_MOSEL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700552 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700553 }
554
555 lcdc_write(priv, _LDDCKR, tmp);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700556 lcdc_write(priv, _LDDCKSTPR, 0);
557 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
558
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200559 /* Setup geometry, format, frame buffer memory and operation mode. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700560 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
561 ch = &priv->ch[k];
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700562 if (!ch->enabled)
563 continue;
564
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000565 sh_mobile_lcdc_geometry(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700566
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100567 switch (sh_mobile_format_fourcc(&ch->info->var)) {
568 case V4L2_PIX_FMT_RGB565:
569 tmp = LDDFR_PKF_RGB16;
570 break;
571 case V4L2_PIX_FMT_BGR24:
572 tmp = LDDFR_PKF_RGB24;
573 break;
574 case V4L2_PIX_FMT_BGR32:
575 tmp = LDDFR_PKF_ARGB32;
576 break;
577 case V4L2_PIX_FMT_NV12:
578 case V4L2_PIX_FMT_NV21:
579 tmp = LDDFR_CC | LDDFR_YF_420;
580 break;
581 case V4L2_PIX_FMT_NV16:
582 case V4L2_PIX_FMT_NV61:
583 tmp = LDDFR_CC | LDDFR_YF_422;
584 break;
585 case V4L2_PIX_FMT_NV24:
586 case V4L2_PIX_FMT_NV42:
587 tmp = LDDFR_CC | LDDFR_YF_444;
588 break;
589 }
590
591 if (sh_mobile_format_is_yuv(&ch->info->var)) {
592 switch (ch->info->var.colorspace) {
593 case V4L2_COLORSPACE_REC709:
594 tmp |= LDDFR_CF1;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000595 break;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100596 case V4L2_COLORSPACE_JPEG:
597 tmp |= LDDFR_CF0;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000598 break;
599 }
Magnus Damm417d4822011-01-05 10:21:00 +0000600 }
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200601
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700602 lcdc_write_chan(ch, LDDFR, tmp);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200603 lcdc_write_chan(ch, LDMLSR, ch->pitch);
604 lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100605 if (sh_mobile_format_is_yuv(&ch->info->var))
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200606 lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700607
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200608 /* When using deferred I/O mode, configure the LCDC for one-shot
609 * operation and enable the frame end interrupt. Otherwise use
610 * continuous read mode.
611 */
612 if (ch->ldmt1r_value & LDMT1R_IFM &&
613 ch->cfg.sys_bus_cfg.deferred_io_msec) {
614 lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
615 lcdc_write(priv, _LDINTR, LDINTR_FE);
616 } else {
617 lcdc_write_chan(ch, LDSM1R, 0);
618 }
619 }
Damian7caa4342011-05-18 11:10:07 +0000620
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200621 /* Word and long word swap. */
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100622 switch (sh_mobile_format_fourcc(&priv->ch[0].info->var)) {
623 case V4L2_PIX_FMT_RGB565:
624 case V4L2_PIX_FMT_NV21:
625 case V4L2_PIX_FMT_NV61:
626 case V4L2_PIX_FMT_NV42:
627 tmp = LDDDSR_LS | LDDDSR_WS;
628 break;
629 case V4L2_PIX_FMT_BGR24:
630 case V4L2_PIX_FMT_NV12:
631 case V4L2_PIX_FMT_NV16:
632 case V4L2_PIX_FMT_NV24:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200633 tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100634 break;
635 case V4L2_PIX_FMT_BGR32:
636 default:
637 tmp = LDDDSR_LS;
638 break;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200639 }
640 lcdc_write(priv, _LDDDSR, tmp);
Damian7caa4342011-05-18 11:10:07 +0000641
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200642 /* Enable the display output. */
643 lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
644 sh_mobile_lcdc_start_stop(priv, 1);
645 priv->started = 1;
646}
Damian7caa4342011-05-18 11:10:07 +0000647
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200648static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
649{
650 struct sh_mobile_meram_info *mdev = priv->meram_dev;
651 struct sh_mobile_lcdc_board_cfg *board_cfg;
652 struct sh_mobile_lcdc_chan *ch;
653 unsigned long tmp;
654 int ret;
655 int k;
656
657 /* enable clocks before accessing the hardware */
658 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
659 if (priv->ch[k].enabled)
660 sh_mobile_lcdc_clk_on(priv);
661 }
662
663 /* reset */
664 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
665 lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
666
667 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
668 ch = &priv->ch[k];
669
670 if (!ch->enabled)
671 continue;
672
673 board_cfg = &ch->cfg.board_cfg;
674 if (board_cfg->setup_sys) {
675 ret = board_cfg->setup_sys(board_cfg->board_data, ch,
676 &sh_mobile_lcdc_sys_bus_ops);
677 if (ret)
678 return ret;
679 }
680 }
681
682 /* Compute frame buffer base address and pitch for each channel. */
683 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
684 struct sh_mobile_meram_cfg *cfg;
685 int pixelformat;
686
687 ch = &priv->ch[k];
688 if (!ch->enabled)
689 continue;
690
691 ch->base_addr_y = ch->info->fix.smem_start;
692 ch->base_addr_c = ch->base_addr_y
693 + ch->info->var.xres
694 * ch->info->var.yres_virtual;
695 ch->pitch = ch->info->fix.line_length;
696
697 /* Enable MERAM if possible. */
698 cfg = ch->cfg.meram_cfg;
699 if (mdev == NULL || mdev->ops == NULL || cfg == NULL)
700 continue;
701
702 /* we need to de-init configured ICBs before we can
703 * re-initialize them.
704 */
705 if (ch->meram_enabled) {
706 mdev->ops->meram_unregister(mdev, cfg);
Damian7caa4342011-05-18 11:10:07 +0000707 ch->meram_enabled = 0;
Damian7caa4342011-05-18 11:10:07 +0000708 }
709
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100710 switch (sh_mobile_format_fourcc(&ch->info->var)) {
711 case V4L2_PIX_FMT_NV12:
712 case V4L2_PIX_FMT_NV21:
713 case V4L2_PIX_FMT_NV16:
714 case V4L2_PIX_FMT_NV61:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200715 pixelformat = SH_MOBILE_MERAM_PF_NV;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100716 break;
717 case V4L2_PIX_FMT_NV24:
718 case V4L2_PIX_FMT_NV42:
719 pixelformat = SH_MOBILE_MERAM_PF_NV24;
720 break;
721 case V4L2_PIX_FMT_RGB565:
722 case V4L2_PIX_FMT_BGR24:
723 case V4L2_PIX_FMT_BGR32:
724 default:
725 pixelformat = SH_MOBILE_MERAM_PF_RGB;
726 break;
727 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700728
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200729 ret = mdev->ops->meram_register(mdev, cfg, ch->pitch,
730 ch->info->var.yres, pixelformat,
731 ch->base_addr_y, ch->base_addr_c,
732 &ch->base_addr_y, &ch->base_addr_c,
733 &ch->pitch);
734 if (!ret)
735 ch->meram_enabled = 1;
736 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700737
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200738 /* Start the LCDC. */
739 __sh_mobile_lcdc_start(priv);
740
741 /* Setup deferred I/O, tell the board code to enable the panels, and
742 * turn backlight on.
743 */
744 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
745 ch = &priv->ch[k];
746 if (!ch->enabled)
747 continue;
748
Magnus Damm85645572008-12-19 15:34:41 +0900749 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200750 if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
Magnus Damm85645572008-12-19 15:34:41 +0900751 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
752 ch->defio.delay = msecs_to_jiffies(tmp);
Paul Mundte33afdd2009-07-07 11:24:32 +0900753 ch->info->fbdefio = &ch->defio;
754 fb_deferred_io_init(ch->info);
Magnus Damm85645572008-12-19 15:34:41 +0900755 }
Magnus Damm21bc1f02009-08-15 02:53:16 +0000756
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700757 board_cfg = &ch->cfg.board_cfg;
Alexandre Courbot247f9932011-02-23 08:41:50 +0000758 if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
Guennadi Liakhovetskic2439392010-07-21 10:13:17 +0000759 board_cfg->display_on(board_cfg->board_data, ch->info);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +0000760 module_put(board_cfg->owner);
761 }
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000762
763 if (ch->bl) {
764 ch->bl->props.power = FB_BLANK_UNBLANK;
765 backlight_update_status(ch->bl);
766 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700767 }
768
769 return 0;
770}
771
772static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
773{
774 struct sh_mobile_lcdc_chan *ch;
775 struct sh_mobile_lcdc_board_cfg *board_cfg;
776 int k;
777
Magnus Damm2feb0752009-03-13 15:36:55 +0000778 /* clean up deferred io and ask board code to disable panel */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700779 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
780 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +0000781 if (!ch->enabled)
782 continue;
Magnus Damm2feb0752009-03-13 15:36:55 +0000783
784 /* deferred io mode:
785 * flush frame, and wait for frame end interrupt
786 * clean up deferred io and enable clock
787 */
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +0000788 if (ch->info && ch->info->fbdefio) {
Magnus Damm2feb0752009-03-13 15:36:55 +0000789 ch->frame_end = 0;
Paul Mundte33afdd2009-07-07 11:24:32 +0900790 schedule_delayed_work(&ch->info->deferred_work, 0);
Magnus Damm2feb0752009-03-13 15:36:55 +0000791 wait_event(ch->frame_end_wait, ch->frame_end);
Paul Mundte33afdd2009-07-07 11:24:32 +0900792 fb_deferred_io_cleanup(ch->info);
793 ch->info->fbdefio = NULL;
Magnus Damm2feb0752009-03-13 15:36:55 +0000794 sh_mobile_lcdc_clk_on(priv);
795 }
796
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000797 if (ch->bl) {
798 ch->bl->props.power = FB_BLANK_POWERDOWN;
799 backlight_update_status(ch->bl);
800 }
801
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700802 board_cfg = &ch->cfg.board_cfg;
Alexandre Courbot247f9932011-02-23 08:41:50 +0000803 if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700804 board_cfg->display_off(board_cfg->board_data);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +0000805 module_put(board_cfg->owner);
806 }
Damian7caa4342011-05-18 11:10:07 +0000807
808 /* disable the meram */
809 if (ch->meram_enabled) {
810 struct sh_mobile_meram_cfg *cfg;
811 struct sh_mobile_meram_info *mdev;
812 cfg = ch->cfg.meram_cfg;
813 mdev = priv->meram_dev;
814 mdev->ops->meram_unregister(mdev, cfg);
815 ch->meram_enabled = 0;
816 }
817
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700818 }
819
820 /* stop the lcdc */
Magnus Damm8e9bb192009-05-20 14:34:43 +0000821 if (priv->started) {
822 sh_mobile_lcdc_start_stop(priv, 0);
823 priv->started = 0;
824 }
Magnus Dammb51339f2008-10-31 20:23:26 +0900825
Magnus Damm85645572008-12-19 15:34:41 +0900826 /* stop clocks */
827 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
828 if (priv->ch[k].enabled)
829 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700830}
831
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200832/* -----------------------------------------------------------------------------
833 * Frame buffer operations
834 */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700835
836static int sh_mobile_lcdc_setcolreg(u_int regno,
837 u_int red, u_int green, u_int blue,
838 u_int transp, struct fb_info *info)
839{
840 u32 *palette = info->pseudo_palette;
841
842 if (regno >= PALETTE_NR)
843 return -EINVAL;
844
845 /* only FB_VISUAL_TRUECOLOR supported */
846
847 red >>= 16 - info->var.red.length;
848 green >>= 16 - info->var.green.length;
849 blue >>= 16 - info->var.blue.length;
850 transp >>= 16 - info->var.transp.length;
851
852 palette[regno] = (red << info->var.red.offset) |
853 (green << info->var.green.offset) |
854 (blue << info->var.blue.offset) |
855 (transp << info->var.transp.offset);
856
857 return 0;
858}
859
860static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
861 .id = "SH Mobile LCDC",
862 .type = FB_TYPE_PACKED_PIXELS,
863 .visual = FB_VISUAL_TRUECOLOR,
864 .accel = FB_ACCEL_NONE,
Phil Edworthy9dd38812009-09-15 12:00:18 +0000865 .xpanstep = 0,
866 .ypanstep = 1,
867 .ywrapstep = 0,
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100868 .capabilities = FB_CAP_FOURCC,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700869};
870
Magnus Damm85645572008-12-19 15:34:41 +0900871static void sh_mobile_lcdc_fillrect(struct fb_info *info,
872 const struct fb_fillrect *rect)
873{
874 sys_fillrect(info, rect);
875 sh_mobile_lcdc_deferred_io_touch(info);
876}
877
878static void sh_mobile_lcdc_copyarea(struct fb_info *info,
879 const struct fb_copyarea *area)
880{
881 sys_copyarea(info, area);
882 sh_mobile_lcdc_deferred_io_touch(info);
883}
884
885static void sh_mobile_lcdc_imageblit(struct fb_info *info,
886 const struct fb_image *image)
887{
888 sys_imageblit(info, image);
889 sh_mobile_lcdc_deferred_io_touch(info);
890}
891
Phil Edworthy9dd38812009-09-15 12:00:18 +0000892static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
893 struct fb_info *info)
894{
895 struct sh_mobile_lcdc_chan *ch = info->par;
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000896 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
897 unsigned long ldrcntr;
898 unsigned long new_pan_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000899 unsigned long base_addr_y, base_addr_c;
900 unsigned long c_offset;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100901 bool yuv = sh_mobile_format_is_yuv(&info->var);
Phil Edworthy9dd38812009-09-15 12:00:18 +0000902
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100903 if (!yuv)
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +0200904 new_pan_offset = var->yoffset * info->fix.line_length
905 + var->xoffset * (info->var.bits_per_pixel / 8);
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000906 else
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +0200907 new_pan_offset = var->yoffset * info->fix.line_length
908 + var->xoffset;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000909
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000910 if (new_pan_offset == ch->pan_offset)
911 return 0; /* No change, do nothing */
912
913 ldrcntr = lcdc_read(priv, _LDRCNTR);
914
915 /* Set the source address for the next refresh */
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000916 base_addr_y = ch->dma_handle + new_pan_offset;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100917 if (yuv) {
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000918 /* Set y offset */
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +0200919 c_offset = var->yoffset * info->fix.line_length
920 * (info->var.bits_per_pixel - 8) / 8;
921 base_addr_c = ch->dma_handle
922 + info->var.xres * info->var.yres_virtual
923 + c_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000924 /* Set x offset */
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100925 if (sh_mobile_format_fourcc(&info->var) == V4L2_PIX_FMT_NV24)
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000926 base_addr_c += 2 * var->xoffset;
927 else
928 base_addr_c += var->xoffset;
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200929 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000930
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200931 if (ch->meram_enabled) {
Damian7caa4342011-05-18 11:10:07 +0000932 struct sh_mobile_meram_cfg *cfg;
933 struct sh_mobile_meram_info *mdev;
Damian7caa4342011-05-18 11:10:07 +0000934 int ret;
935
936 cfg = ch->cfg.meram_cfg;
937 mdev = priv->meram_dev;
938 ret = mdev->ops->meram_update(mdev, cfg,
939 base_addr_y, base_addr_c,
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200940 &base_addr_y, &base_addr_c);
Damian7caa4342011-05-18 11:10:07 +0000941 if (ret)
942 return ret;
Damian7caa4342011-05-18 11:10:07 +0000943 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000944
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200945 ch->base_addr_y = base_addr_y;
946 ch->base_addr_c = base_addr_c;
947
948 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100949 if (yuv)
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200950 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
951
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000952 if (lcdc_chan_is_sublcd(ch))
953 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
954 else
955 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
956
957 ch->pan_offset = new_pan_offset;
958
959 sh_mobile_lcdc_deferred_io_touch(info);
Phil Edworthy9dd38812009-09-15 12:00:18 +0000960
961 return 0;
962}
963
Phil Edworthy40331b22010-02-15 13:57:49 +0000964static int sh_mobile_wait_for_vsync(struct fb_info *info)
965{
966 struct sh_mobile_lcdc_chan *ch = info->par;
967 unsigned long ldintr;
968 int ret;
969
Laurent Pinchartdc486652011-07-13 12:13:47 +0200970 /* Enable VSync End interrupt and be careful not to acknowledge any
971 * pending interrupt.
972 */
Phil Edworthy40331b22010-02-15 13:57:49 +0000973 ldintr = lcdc_read(ch->lcdc, _LDINTR);
Laurent Pinchartdc486652011-07-13 12:13:47 +0200974 ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
Phil Edworthy40331b22010-02-15 13:57:49 +0000975 lcdc_write(ch->lcdc, _LDINTR, ldintr);
976
977 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
978 msecs_to_jiffies(100));
979 if (!ret)
980 return -ETIMEDOUT;
981
982 return 0;
983}
984
985static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
986 unsigned long arg)
987{
988 int retval;
989
990 switch (cmd) {
991 case FBIO_WAITFORVSYNC:
992 retval = sh_mobile_wait_for_vsync(info);
993 break;
994
995 default:
996 retval = -ENOIOCTLCMD;
997 break;
998 }
999 return retval;
1000}
1001
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001002static void sh_mobile_fb_reconfig(struct fb_info *info)
1003{
1004 struct sh_mobile_lcdc_chan *ch = info->par;
1005 struct fb_videomode mode1, mode2;
1006 struct fb_event event;
1007 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1008
1009 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1010 /* More framebuffer users are active */
1011 return;
1012
1013 fb_var_to_videomode(&mode1, &ch->display_var);
1014 fb_var_to_videomode(&mode2, &info->var);
1015
1016 if (fb_mode_is_equal(&mode1, &mode2))
1017 return;
1018
1019 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1020 if (fb_set_var(info, &ch->display_var) < 0)
1021 /* Couldn't reconfigure, hopefully, can continue as before */
1022 return;
1023
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001024 /*
1025 * fb_set_var() calls the notifier change internally, only if
1026 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1027 * user event, we have to call the chain ourselves.
1028 */
1029 event.info = info;
Arnd Hannemanncc267ec2010-11-15 21:43:22 +00001030 event.data = &mode1;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001031 fb_notifier_call_chain(evnt, &event);
1032}
1033
1034/*
1035 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1036 * user == 1, or with console sem held, if user == 0.
1037 */
1038static int sh_mobile_release(struct fb_info *info, int user)
1039{
1040 struct sh_mobile_lcdc_chan *ch = info->par;
1041
1042 mutex_lock(&ch->open_lock);
1043 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1044
1045 ch->use_count--;
1046
1047 /* Nothing to reconfigure, when called from fbcon */
1048 if (user) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001049 console_lock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001050 sh_mobile_fb_reconfig(info);
Torben Hohnac751ef2011-01-25 15:07:35 -08001051 console_unlock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001052 }
1053
1054 mutex_unlock(&ch->open_lock);
1055
1056 return 0;
1057}
1058
1059static int sh_mobile_open(struct fb_info *info, int user)
1060{
1061 struct sh_mobile_lcdc_chan *ch = info->par;
1062
1063 mutex_lock(&ch->open_lock);
1064 ch->use_count++;
1065
1066 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1067 mutex_unlock(&ch->open_lock);
1068
1069 return 0;
1070}
1071
1072static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1073{
1074 struct sh_mobile_lcdc_chan *ch = info->par;
Magnus Damm417d4822011-01-05 10:21:00 +00001075 struct sh_mobile_lcdc_priv *p = ch->lcdc;
Laurent Pinchart03862192011-08-31 13:00:53 +02001076 unsigned int best_dist = (unsigned int)-1;
1077 unsigned int best_xres = 0;
1078 unsigned int best_yres = 0;
1079 unsigned int i;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001080
Laurent Pinchart03862192011-08-31 13:00:53 +02001081 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001082 return -EINVAL;
Laurent Pinchart03862192011-08-31 13:00:53 +02001083
1084 /* If board code provides us with a list of available modes, make sure
1085 * we use one of them. Find the mode closest to the requested one. The
1086 * distance between two modes is defined as the size of the
1087 * non-overlapping parts of the two rectangles.
1088 */
1089 for (i = 0; i < ch->cfg.num_cfg; ++i) {
1090 const struct fb_videomode *mode = &ch->cfg.lcd_cfg[i];
1091 unsigned int dist;
1092
1093 /* We can only round up. */
1094 if (var->xres > mode->xres || var->yres > mode->yres)
1095 continue;
1096
1097 dist = var->xres * var->yres + mode->xres * mode->yres
1098 - 2 * min(var->xres, mode->xres)
1099 * min(var->yres, mode->yres);
1100
1101 if (dist < best_dist) {
1102 best_xres = mode->xres;
1103 best_yres = mode->yres;
1104 best_dist = dist;
1105 }
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001106 }
Magnus Damm417d4822011-01-05 10:21:00 +00001107
Laurent Pinchart03862192011-08-31 13:00:53 +02001108 /* If no available mode can be used, return an error. */
1109 if (ch->cfg.num_cfg != 0) {
1110 if (best_dist == (unsigned int)-1)
1111 return -EINVAL;
1112
1113 var->xres = best_xres;
1114 var->yres = best_yres;
1115 }
1116
1117 /* Make sure the virtual resolution is at least as big as the visible
1118 * resolution.
1119 */
1120 if (var->xres_virtual < var->xres)
1121 var->xres_virtual = var->xres;
1122 if (var->yres_virtual < var->yres)
1123 var->yres_virtual = var->yres;
1124
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001125 if (sh_mobile_format_is_fourcc(var)) {
1126 switch (var->grayscale) {
1127 case V4L2_PIX_FMT_NV12:
1128 case V4L2_PIX_FMT_NV21:
1129 var->bits_per_pixel = 12;
1130 break;
1131 case V4L2_PIX_FMT_RGB565:
1132 case V4L2_PIX_FMT_NV16:
1133 case V4L2_PIX_FMT_NV61:
1134 var->bits_per_pixel = 16;
1135 break;
1136 case V4L2_PIX_FMT_BGR24:
1137 case V4L2_PIX_FMT_NV24:
1138 case V4L2_PIX_FMT_NV42:
1139 var->bits_per_pixel = 24;
1140 break;
1141 case V4L2_PIX_FMT_BGR32:
1142 var->bits_per_pixel = 32;
1143 break;
1144 default:
1145 return -EINVAL;
1146 }
Laurent Pinchart03862192011-08-31 13:00:53 +02001147
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001148 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1149 * respectively.
1150 */
1151 if (!sh_mobile_format_is_yuv(var))
1152 var->colorspace = V4L2_COLORSPACE_SRGB;
1153 else if (var->colorspace != V4L2_COLORSPACE_REC709)
1154 var->colorspace = V4L2_COLORSPACE_JPEG;
1155 } else {
1156 if (var->bits_per_pixel <= 16) { /* RGB 565 */
1157 var->bits_per_pixel = 16;
1158 var->red.offset = 11;
1159 var->red.length = 5;
1160 var->green.offset = 5;
1161 var->green.length = 6;
1162 var->blue.offset = 0;
1163 var->blue.length = 5;
1164 var->transp.offset = 0;
1165 var->transp.length = 0;
1166 } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
1167 var->bits_per_pixel = 24;
1168 var->red.offset = 16;
1169 var->red.length = 8;
1170 var->green.offset = 8;
1171 var->green.length = 8;
1172 var->blue.offset = 0;
1173 var->blue.length = 8;
1174 var->transp.offset = 0;
1175 var->transp.length = 0;
1176 } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
1177 var->bits_per_pixel = 32;
1178 var->red.offset = 16;
1179 var->red.length = 8;
1180 var->green.offset = 8;
1181 var->green.length = 8;
1182 var->blue.offset = 0;
1183 var->blue.length = 8;
1184 var->transp.offset = 24;
1185 var->transp.length = 8;
1186 } else
1187 return -EINVAL;
1188
1189 var->red.msb_right = 0;
1190 var->green.msb_right = 0;
1191 var->blue.msb_right = 0;
1192 var->transp.msb_right = 0;
1193 }
Laurent Pinchart03862192011-08-31 13:00:53 +02001194
1195 /* Make sure we don't exceed our allocated memory. */
1196 if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
1197 info->fix.smem_len)
1198 return -EINVAL;
1199
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001200 /* only accept the forced_fourcc for dual channel configurations */
1201 if (p->forced_fourcc &&
1202 p->forced_fourcc != sh_mobile_format_fourcc(var))
Magnus Damm417d4822011-01-05 10:21:00 +00001203 return -EINVAL;
1204
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001205 return 0;
1206}
Phil Edworthy40331b22010-02-15 13:57:49 +00001207
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001208static int sh_mobile_set_par(struct fb_info *info)
1209{
1210 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchart91fba482011-08-31 13:00:56 +02001211 u32 line_length = info->fix.line_length;
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001212 int ret;
1213
1214 sh_mobile_lcdc_stop(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001215
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001216 if (sh_mobile_format_is_yuv(&info->var))
Laurent Pinchart91fba482011-08-31 13:00:56 +02001217 info->fix.line_length = info->var.xres;
1218 else
1219 info->fix.line_length = info->var.xres
1220 * info->var.bits_per_pixel / 8;
1221
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001222 ret = sh_mobile_lcdc_start(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001223 if (ret < 0) {
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001224 dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001225 info->fix.line_length = line_length;
1226 }
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001227
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001228 if (sh_mobile_format_is_fourcc(&info->var)) {
1229 info->fix.type = FB_TYPE_FOURCC;
1230 info->fix.visual = FB_VISUAL_FOURCC;
1231 } else {
1232 info->fix.type = FB_TYPE_PACKED_PIXELS;
1233 info->fix.visual = FB_VISUAL_TRUECOLOR;
1234 }
1235
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001236 return ret;
1237}
1238
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001239/*
1240 * Screen blanking. Behavior is as follows:
1241 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1242 * FB_BLANK_NORMAL: screen blanked, clocks enabled
1243 * FB_BLANK_VSYNC,
1244 * FB_BLANK_HSYNC,
1245 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1246 */
1247static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
1248{
1249 struct sh_mobile_lcdc_chan *ch = info->par;
1250 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1251
1252 /* blank the screen? */
1253 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
1254 struct fb_fillrect rect = {
1255 .width = info->var.xres,
1256 .height = info->var.yres,
1257 };
1258 sh_mobile_lcdc_fillrect(info, &rect);
1259 }
1260 /* turn clocks on? */
1261 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
1262 sh_mobile_lcdc_clk_on(p);
1263 }
1264 /* turn clocks off? */
1265 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
1266 /* make sure the screen is updated with the black fill before
1267 * switching the clocks off. one vsync is not enough since
1268 * blanking may occur in the middle of a refresh. deferred io
1269 * mode will reenable the clocks and update the screen in time,
1270 * so it does not need this. */
1271 if (!info->fbdefio) {
1272 sh_mobile_wait_for_vsync(info);
1273 sh_mobile_wait_for_vsync(info);
1274 }
1275 sh_mobile_lcdc_clk_off(p);
1276 }
1277
1278 ch->blank_status = blank;
1279 return 0;
1280}
1281
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001282static struct fb_ops sh_mobile_lcdc_ops = {
Phil Edworthy9dd38812009-09-15 12:00:18 +00001283 .owner = THIS_MODULE,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001284 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
Magnus Damm2540c112008-12-17 17:29:49 +09001285 .fb_read = fb_sys_read,
1286 .fb_write = fb_sys_write,
Magnus Damm85645572008-12-19 15:34:41 +09001287 .fb_fillrect = sh_mobile_lcdc_fillrect,
1288 .fb_copyarea = sh_mobile_lcdc_copyarea,
1289 .fb_imageblit = sh_mobile_lcdc_imageblit,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001290 .fb_blank = sh_mobile_lcdc_blank,
Phil Edworthy9dd38812009-09-15 12:00:18 +00001291 .fb_pan_display = sh_mobile_fb_pan_display,
Phil Edworthy40331b22010-02-15 13:57:49 +00001292 .fb_ioctl = sh_mobile_ioctl,
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001293 .fb_open = sh_mobile_open,
1294 .fb_release = sh_mobile_release,
1295 .fb_check_var = sh_mobile_check_var,
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001296 .fb_set_par = sh_mobile_set_par,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001297};
1298
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001299/* -----------------------------------------------------------------------------
1300 * Backlight
1301 */
1302
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001303static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
1304{
1305 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1306 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1307 int brightness = bdev->props.brightness;
1308
1309 if (bdev->props.power != FB_BLANK_UNBLANK ||
1310 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
1311 brightness = 0;
1312
1313 return cfg->set_brightness(cfg->board_data, brightness);
1314}
1315
1316static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
1317{
1318 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1319 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1320
1321 return cfg->get_brightness(cfg->board_data);
1322}
1323
1324static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
1325 struct fb_info *info)
1326{
1327 return (info->bl_dev == bdev);
1328}
1329
1330static struct backlight_ops sh_mobile_lcdc_bl_ops = {
1331 .options = BL_CORE_SUSPENDRESUME,
1332 .update_status = sh_mobile_lcdc_update_bl,
1333 .get_brightness = sh_mobile_lcdc_get_brightness,
1334 .check_fb = sh_mobile_lcdc_check_fb,
1335};
1336
1337static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
1338 struct sh_mobile_lcdc_chan *ch)
1339{
1340 struct backlight_device *bl;
1341
1342 bl = backlight_device_register(ch->cfg.bl_info.name, parent, ch,
1343 &sh_mobile_lcdc_bl_ops, NULL);
Dan Carpenterbeee1f22011-03-21 15:03:13 +00001344 if (IS_ERR(bl)) {
1345 dev_err(parent, "unable to register backlight device: %ld\n",
1346 PTR_ERR(bl));
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001347 return NULL;
1348 }
1349
1350 bl->props.max_brightness = ch->cfg.bl_info.max_brightness;
1351 bl->props.brightness = bl->props.max_brightness;
1352 backlight_update_status(bl);
1353
1354 return bl;
1355}
1356
1357static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
1358{
1359 backlight_device_unregister(bdev);
1360}
1361
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001362/* -----------------------------------------------------------------------------
1363 * Power management
1364 */
1365
Magnus Damm2feb0752009-03-13 15:36:55 +00001366static int sh_mobile_lcdc_suspend(struct device *dev)
1367{
1368 struct platform_device *pdev = to_platform_device(dev);
1369
1370 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
1371 return 0;
1372}
1373
1374static int sh_mobile_lcdc_resume(struct device *dev)
1375{
1376 struct platform_device *pdev = to_platform_device(dev);
1377
1378 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
1379}
1380
Magnus Damm0246c472009-08-14 10:49:08 +00001381static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
1382{
1383 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001384 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001385
1386 /* turn off LCDC hardware */
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001387 lcdc_write(priv, _LDCNT1R, 0);
1388
Magnus Damm0246c472009-08-14 10:49:08 +00001389 return 0;
1390}
1391
1392static int sh_mobile_lcdc_runtime_resume(struct device *dev)
1393{
1394 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001395 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001396
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001397 __sh_mobile_lcdc_start(priv);
Magnus Damm0246c472009-08-14 10:49:08 +00001398
1399 return 0;
1400}
1401
Alexey Dobriyan47145212009-12-14 18:00:08 -08001402static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
Magnus Damm2feb0752009-03-13 15:36:55 +00001403 .suspend = sh_mobile_lcdc_suspend,
1404 .resume = sh_mobile_lcdc_resume,
Magnus Damm0246c472009-08-14 10:49:08 +00001405 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
1406 .runtime_resume = sh_mobile_lcdc_runtime_resume,
Magnus Damm2feb0752009-03-13 15:36:55 +00001407};
1408
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001409/* -----------------------------------------------------------------------------
1410 * Framebuffer notifier
1411 */
1412
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001413/* locking: called with info->lock held */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001414static int sh_mobile_lcdc_notify(struct notifier_block *nb,
1415 unsigned long action, void *data)
1416{
1417 struct fb_event *event = data;
1418 struct fb_info *info = event->info;
1419 struct sh_mobile_lcdc_chan *ch = info->par;
1420 struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001421
1422 if (&ch->lcdc->notifier != nb)
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001423 return NOTIFY_DONE;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001424
1425 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
1426 __func__, action, event->data);
1427
1428 switch(action) {
1429 case FB_EVENT_SUSPEND:
Alexandre Courbot247f9932011-02-23 08:41:50 +00001430 if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001431 board_cfg->display_off(board_cfg->board_data);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001432 module_put(board_cfg->owner);
1433 }
Guennadi Liakhovetskiafe417c2010-09-03 07:20:39 +00001434 sh_mobile_lcdc_stop(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001435 break;
1436 case FB_EVENT_RESUME:
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001437 mutex_lock(&ch->open_lock);
1438 sh_mobile_fb_reconfig(info);
1439 mutex_unlock(&ch->open_lock);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001440
1441 /* HDMI must be enabled before LCDC configuration */
Alexandre Courbot247f9932011-02-23 08:41:50 +00001442 if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001443 board_cfg->display_on(board_cfg->board_data, info);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001444 module_put(board_cfg->owner);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001445 }
1446
Guennadi Liakhovetskiebe5e122011-05-05 16:33:40 +00001447 sh_mobile_lcdc_start(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001448 }
1449
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001450 return NOTIFY_OK;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001451}
1452
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001453/* -----------------------------------------------------------------------------
1454 * Probe/remove and driver init/exit
1455 */
1456
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001457static const struct fb_videomode default_720p __devinitconst = {
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001458 .name = "HDMI 720p",
1459 .xres = 1280,
1460 .yres = 720,
1461
1462 .left_margin = 220,
1463 .right_margin = 110,
1464 .hsync_len = 40,
1465
1466 .upper_margin = 20,
1467 .lower_margin = 5,
1468 .vsync_len = 5,
1469
1470 .pixclock = 13468,
1471 .refresh = 60,
1472 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
1473};
1474
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001475static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1476{
1477 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1478 struct fb_info *info;
1479 int i;
1480
1481 fb_unregister_client(&priv->notifier);
1482
1483 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
1484 if (priv->ch[i].info && priv->ch[i].info->dev)
1485 unregister_framebuffer(priv->ch[i].info);
1486
1487 sh_mobile_lcdc_stop(priv);
1488
1489 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1490 info = priv->ch[i].info;
1491
1492 if (!info || !info->device)
1493 continue;
1494
1495 if (priv->ch[i].sglist)
1496 vfree(priv->ch[i].sglist);
1497
1498 if (info->screen_base)
1499 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1500 info->screen_base,
1501 priv->ch[i].dma_handle);
1502 fb_dealloc_cmap(&info->cmap);
1503 framebuffer_release(info);
1504 }
1505
1506 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1507 if (priv->ch[i].bl)
1508 sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
1509 }
1510
Laurent Pinchart4774c122011-09-07 15:47:07 +02001511 if (priv->dot_clk) {
1512 pm_runtime_disable(&pdev->dev);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001513 clk_put(priv->dot_clk);
Laurent Pinchart4774c122011-09-07 15:47:07 +02001514 }
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001515
1516 if (priv->base)
1517 iounmap(priv->base);
1518
1519 if (priv->irq)
1520 free_irq(priv->irq, priv);
1521 kfree(priv);
1522 return 0;
1523}
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001524
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001525static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001526{
1527 int interface_type = ch->cfg.interface_type;
1528
1529 switch (interface_type) {
1530 case RGB8:
1531 case RGB9:
1532 case RGB12A:
1533 case RGB12B:
1534 case RGB16:
1535 case RGB18:
1536 case RGB24:
1537 case SYS8A:
1538 case SYS8B:
1539 case SYS8C:
1540 case SYS8D:
1541 case SYS9:
1542 case SYS12:
1543 case SYS16A:
1544 case SYS16B:
1545 case SYS16C:
1546 case SYS18:
1547 case SYS24:
1548 break;
1549 default:
1550 return -EINVAL;
1551 }
1552
1553 /* SUBLCD only supports SYS interface */
1554 if (lcdc_chan_is_sublcd(ch)) {
1555 if (!(interface_type & LDMT1R_IFM))
1556 return -EINVAL;
1557
1558 interface_type &= ~LDMT1R_IFM;
1559 }
1560
1561 ch->ldmt1r_value = interface_type;
1562 return 0;
1563}
1564
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001565static int __devinit
1566sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv *priv,
1567 struct sh_mobile_lcdc_chan *ch)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001568{
1569 struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
1570 const struct fb_videomode *max_mode;
1571 const struct fb_videomode *mode;
1572 struct fb_var_screeninfo *var;
1573 struct fb_info *info;
1574 unsigned int max_size;
1575 int num_cfg;
1576 void *buf;
1577 int ret;
1578 int i;
1579
Laurent Pincharta67472a2011-08-31 13:00:59 +02001580 mutex_init(&ch->open_lock);
1581
1582 /* Allocate the frame buffer device. */
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001583 ch->info = framebuffer_alloc(0, priv->dev);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001584 if (!ch->info) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001585 dev_err(priv->dev, "unable to allocate fb_info\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001586 return -ENOMEM;
1587 }
1588
1589 info = ch->info;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001590 info->fbops = &sh_mobile_lcdc_ops;
1591 info->par = ch;
Laurent Pincharta67472a2011-08-31 13:00:59 +02001592 info->pseudo_palette = &ch->pseudo_palette;
1593 info->flags = FBINFO_FLAG_DEFAULT;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001594
1595 /* Iterate through the modes to validate them and find the highest
1596 * resolution.
1597 */
1598 max_mode = NULL;
1599 max_size = 0;
1600
1601 for (i = 0, mode = cfg->lcd_cfg; i < cfg->num_cfg; i++, mode++) {
1602 unsigned int size = mode->yres * mode->xres;
1603
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001604 /* NV12/NV21 buffers must have even number of lines */
1605 if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
1606 cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001607 dev_err(priv->dev, "yres must be multiple of 2 for "
1608 "YCbCr420 mode.\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001609 return -EINVAL;
1610 }
1611
1612 if (size > max_size) {
1613 max_mode = mode;
1614 max_size = size;
1615 }
1616 }
1617
1618 if (!max_size)
1619 max_size = MAX_XRES * MAX_YRES;
1620 else
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001621 dev_dbg(priv->dev, "Found largest videomode %ux%u\n",
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001622 max_mode->xres, max_mode->yres);
1623
Laurent Pincharta67472a2011-08-31 13:00:59 +02001624 /* Create the mode list. */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001625 if (cfg->lcd_cfg == NULL) {
1626 mode = &default_720p;
1627 num_cfg = 1;
1628 } else {
1629 mode = cfg->lcd_cfg;
1630 num_cfg = cfg->num_cfg;
1631 }
1632
1633 fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
1634
Laurent Pincharta67472a2011-08-31 13:00:59 +02001635 /* Initialize variable screen information using the first mode as
1636 * default. The default Y virtual resolution is twice the panel size to
1637 * allow for double-buffering.
1638 */
1639 var = &info->var;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001640 fb_videomode_to_var(var, mode);
1641 var->width = cfg->lcd_size_cfg.width;
1642 var->height = cfg->lcd_size_cfg.height;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001643 var->yres_virtual = var->yres * 2;
1644 var->activate = FB_ACTIVATE_NOW;
1645
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001646 switch (cfg->fourcc) {
1647 case V4L2_PIX_FMT_RGB565:
1648 var->bits_per_pixel = 16;
1649 break;
1650 case V4L2_PIX_FMT_BGR24:
1651 var->bits_per_pixel = 24;
1652 break;
1653 case V4L2_PIX_FMT_BGR32:
1654 var->bits_per_pixel = 32;
1655 break;
1656 default:
1657 var->grayscale = cfg->fourcc;
1658 break;
1659 }
1660
1661 /* Make sure the memory size check won't fail. smem_len is initialized
1662 * later based on var.
1663 */
1664 info->fix.smem_len = UINT_MAX;
Laurent Pincharta67472a2011-08-31 13:00:59 +02001665 ret = sh_mobile_check_var(var, info);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001666 if (ret)
1667 return ret;
1668
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001669 max_size = max_size * var->bits_per_pixel / 8 * 2;
1670
Laurent Pincharta67472a2011-08-31 13:00:59 +02001671 /* Allocate frame buffer memory and color map. */
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001672 buf = dma_alloc_coherent(priv->dev, max_size, &ch->dma_handle,
1673 GFP_KERNEL);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001674 if (!buf) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001675 dev_err(priv->dev, "unable to allocate buffer\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001676 return -ENOMEM;
1677 }
1678
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001679 ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1680 if (ret < 0) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001681 dev_err(priv->dev, "unable to allocate cmap\n");
1682 dma_free_coherent(priv->dev, max_size, buf, ch->dma_handle);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001683 return ret;
1684 }
1685
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001686 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1687 * for NV12 and NV21.
1688 */
1689 info->fix = sh_mobile_lcdc_fix;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001690 info->fix.smem_start = ch->dma_handle;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001691 info->fix.smem_len = max_size;
1692 if (cfg->fourcc == V4L2_PIX_FMT_NV12 ||
1693 cfg->fourcc == V4L2_PIX_FMT_NV21)
1694 info->fix.ypanstep = 2;
1695
1696 if (sh_mobile_format_is_yuv(var)) {
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001697 info->fix.line_length = var->xres;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001698 info->fix.visual = FB_VISUAL_FOURCC;
1699 } else {
1700 info->fix.line_length = var->xres * var->bits_per_pixel / 8;
1701 info->fix.visual = FB_VISUAL_TRUECOLOR;
1702 }
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001703
1704 info->screen_base = buf;
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001705 info->device = priv->dev;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001706 ch->display_var = *var;
1707
1708 return 0;
1709}
1710
Uwe Kleine-Königc2e13032010-02-04 20:56:51 +01001711static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001712{
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001713 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001714 struct sh_mobile_lcdc_priv *priv;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001715 struct resource *res;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001716 int num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001717 int error;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001718 int i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001719
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001720 if (!pdata) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001721 dev_err(&pdev->dev, "no platform data defined\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001722 return -EINVAL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001723 }
1724
1725 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Damm85645572008-12-19 15:34:41 +09001726 i = platform_get_irq(pdev, 0);
1727 if (!res || i < 0) {
1728 dev_err(&pdev->dev, "cannot get platform resources\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001729 return -ENOENT;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001730 }
1731
1732 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1733 if (!priv) {
1734 dev_err(&pdev->dev, "cannot allocate device data\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001735 return -ENOMEM;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001736 }
1737
Laurent Pinchart4774c122011-09-07 15:47:07 +02001738 priv->dev = &pdev->dev;
1739 priv->meram_dev = pdata->meram_dev;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001740 platform_set_drvdata(pdev, priv);
1741
Yong Zhangf8798cc2011-09-22 16:59:16 +08001742 error = request_irq(i, sh_mobile_lcdc_irq, 0,
Kay Sievers7ad33e72009-03-24 16:38:21 -07001743 dev_name(&pdev->dev), priv);
Magnus Damm85645572008-12-19 15:34:41 +09001744 if (error) {
1745 dev_err(&pdev->dev, "unable to request irq\n");
1746 goto err1;
1747 }
1748
1749 priv->irq = i;
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +00001750 atomic_set(&priv->hw_usecnt, -1);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001751
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001752 for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
1753 struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001754
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001755 ch->lcdc = priv;
1756 memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
1757
1758 error = sh_mobile_lcdc_check_interface(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001759 if (error) {
1760 dev_err(&pdev->dev, "unsupported interface type\n");
1761 goto err1;
1762 }
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001763 init_waitqueue_head(&ch->frame_end_wait);
1764 init_completion(&ch->vsync_completion);
1765 ch->pan_offset = 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001766
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001767 /* probe the backlight is there is one defined */
1768 if (ch->cfg.bl_info.max_brightness)
1769 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
1770
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001771 switch (pdata->ch[i].chan) {
1772 case LCDC_CHAN_MAINLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001773 ch->enabled = LDCNT2R_ME;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001774 ch->reg_offs = lcdc_offs_mainlcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001775 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001776 break;
1777 case LCDC_CHAN_SUBLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001778 ch->enabled = LDCNT2R_SE;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001779 ch->reg_offs = lcdc_offs_sublcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001780 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001781 break;
1782 }
1783 }
1784
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001785 if (!num_channels) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001786 dev_err(&pdev->dev, "no channels defined\n");
1787 error = -EINVAL;
1788 goto err1;
1789 }
1790
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001791 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001792 if (num_channels == 2)
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001793 priv->forced_fourcc = pdata->ch[0].fourcc;
Magnus Damm417d4822011-01-05 10:21:00 +00001794
Guennadi Liakhovetskidba6f382010-06-30 09:26:35 +00001795 priv->base = ioremap_nocache(res->start, resource_size(res));
1796 if (!priv->base)
1797 goto err1;
1798
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001799 error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001800 if (error) {
1801 dev_err(&pdev->dev, "unable to setup clocks\n");
1802 goto err1;
1803 }
1804
Laurent Pinchart4774c122011-09-07 15:47:07 +02001805 /* Enable runtime PM. */
1806 pm_runtime_enable(&pdev->dev);
Damian7caa4342011-05-18 11:10:07 +00001807
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001808 for (i = 0; i < num_channels; i++) {
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001809 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001810
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001811 error = sh_mobile_lcdc_channel_init(priv, ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001812 if (error)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001813 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001814 }
1815
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001816 error = sh_mobile_lcdc_start(priv);
1817 if (error) {
1818 dev_err(&pdev->dev, "unable to start hardware\n");
1819 goto err1;
1820 }
1821
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001822 for (i = 0; i < num_channels; i++) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00001823 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001824 struct fb_info *info = ch->info;
Paul Mundt1c6a3072009-07-01 06:50:31 +00001825
1826 if (info->fbdefio) {
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001827 ch->sglist = vmalloc(sizeof(struct scatterlist) *
Paul Mundt1c6a3072009-07-01 06:50:31 +00001828 info->fix.smem_len >> PAGE_SHIFT);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001829 if (!ch->sglist) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00001830 dev_err(&pdev->dev, "cannot allocate sglist\n");
1831 goto err1;
1832 }
1833 }
1834
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001835 info->bl_dev = ch->bl;
1836
Paul Mundt1c6a3072009-07-01 06:50:31 +00001837 error = register_framebuffer(info);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001838 if (error < 0)
1839 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001840
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001841 dev_info(&pdev->dev, "registered %s/%s as %dx%d %dbpp.\n",
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001842 pdev->name, (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
1843 "mainlcd" : "sublcd", info->var.xres, info->var.yres,
1844 info->var.bits_per_pixel);
Magnus Damm85645572008-12-19 15:34:41 +09001845
1846 /* deferred io mode: disable clock to save power */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001847 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
Magnus Damm85645572008-12-19 15:34:41 +09001848 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001849 }
1850
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001851 /* Failure ignored */
1852 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
1853 fb_register_client(&priv->notifier);
1854
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001855 return 0;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001856err1:
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001857 sh_mobile_lcdc_remove(pdev);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001858
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001859 return error;
1860}
1861
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001862static struct platform_driver sh_mobile_lcdc_driver = {
1863 .driver = {
1864 .name = "sh_mobile_lcdc_fb",
1865 .owner = THIS_MODULE,
Magnus Damm2feb0752009-03-13 15:36:55 +00001866 .pm = &sh_mobile_lcdc_dev_pm_ops,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001867 },
1868 .probe = sh_mobile_lcdc_probe,
1869 .remove = sh_mobile_lcdc_remove,
1870};
1871
Axel Lin4277f2c2011-11-26 10:25:54 +08001872module_platform_driver(sh_mobile_lcdc_driver);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001873
1874MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1875MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1876MODULE_LICENSE("GPL v2");