blob: 75f613b024bf3c103c9458551803d6b0bdb52d60 [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
190static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
191 int clock_source,
192 struct sh_mobile_lcdc_priv *priv)
193{
Laurent Pinchart4774c122011-09-07 15:47:07 +0200194 struct clk *clk;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200195 char *str;
196
197 switch (clock_source) {
198 case LCDC_CLK_BUS:
199 str = "bus_clk";
200 priv->lddckr = LDDCKR_ICKSEL_BUS;
201 break;
202 case LCDC_CLK_PERIPHERAL:
203 str = "peripheral_clk";
204 priv->lddckr = LDDCKR_ICKSEL_MIPI;
205 break;
206 case LCDC_CLK_EXTERNAL:
207 str = NULL;
208 priv->lddckr = LDDCKR_ICKSEL_HDMI;
209 break;
210 default:
211 return -EINVAL;
212 }
213
Laurent Pinchart4774c122011-09-07 15:47:07 +0200214 if (str == NULL)
215 return 0;
216
217 clk = clk_get(&pdev->dev, str);
218 if (IS_ERR(clk)) {
219 dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
220 return PTR_ERR(clk);
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200221 }
222
Laurent Pinchart4774c122011-09-07 15:47:07 +0200223 priv->dot_clk = clk;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200224 return 0;
225}
226
227/* -----------------------------------------------------------------------------
228 * Sys panel and deferred I/O
229 */
230
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700231static void lcdc_sys_write_index(void *handle, unsigned long data)
232{
233 struct sh_mobile_lcdc_chan *ch = handle;
234
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200235 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
236 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
237 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
238 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
239 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700240}
241
242static void lcdc_sys_write_data(void *handle, unsigned long data)
243{
244 struct sh_mobile_lcdc_chan *ch = handle;
245
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200246 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
247 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
248 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
249 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
250 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700251}
252
253static unsigned long lcdc_sys_read_data(void *handle)
254{
255 struct sh_mobile_lcdc_chan *ch = handle;
256
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200257 lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
258 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
259 lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
260 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700261 udelay(1);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200262 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700263
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200264 return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700265}
266
267struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
268 lcdc_sys_write_index,
269 lcdc_sys_write_data,
270 lcdc_sys_read_data,
271};
272
Paul Mundt1c6a3072009-07-01 06:50:31 +0000273static int sh_mobile_lcdc_sginit(struct fb_info *info,
274 struct list_head *pagelist)
275{
276 struct sh_mobile_lcdc_chan *ch = info->par;
277 unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
278 struct page *page;
279 int nr_pages = 0;
280
281 sg_init_table(ch->sglist, nr_pages_max);
282
283 list_for_each_entry(page, pagelist, lru)
284 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
285
286 return nr_pages;
287}
288
Magnus Damm85645572008-12-19 15:34:41 +0900289static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
290 struct list_head *pagelist)
291{
292 struct sh_mobile_lcdc_chan *ch = info->par;
Magnus Dammef61aae2009-12-07 14:20:06 +0000293 struct sh_mobile_lcdc_board_cfg *bcfg = &ch->cfg.board_cfg;
Magnus Damm85645572008-12-19 15:34:41 +0900294
295 /* enable clocks before accessing hardware */
296 sh_mobile_lcdc_clk_on(ch->lcdc);
297
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900298 /*
299 * It's possible to get here without anything on the pagelist via
300 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
301 * invocation. In the former case, the acceleration routines are
302 * stepped in to when using the framebuffer console causing the
303 * workqueue to be scheduled without any dirty pages on the list.
304 *
305 * Despite this, a panel update is still needed given that the
306 * acceleration routines have their own methods for writing in
307 * that still need to be updated.
308 *
309 * The fsync() and empty pagelist case could be optimized for,
310 * but we don't bother, as any application exhibiting such
311 * behaviour is fundamentally broken anyways.
312 */
313 if (!list_empty(pagelist)) {
314 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
Paul Mundt1c6a3072009-07-01 06:50:31 +0000315
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900316 /* trigger panel update */
317 dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
Magnus Dammef61aae2009-12-07 14:20:06 +0000318 if (bcfg->start_transfer)
319 bcfg->start_transfer(bcfg->board_data, ch,
320 &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200321 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900322 dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
Magnus Dammef61aae2009-12-07 14:20:06 +0000323 } else {
324 if (bcfg->start_transfer)
325 bcfg->start_transfer(bcfg->board_data, ch,
326 &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200327 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
Magnus Dammef61aae2009-12-07 14:20:06 +0000328 }
Magnus Damm85645572008-12-19 15:34:41 +0900329}
330
331static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
332{
333 struct fb_deferred_io *fbdefio = info->fbdefio;
334
335 if (fbdefio)
336 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
337}
338
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200339/* -----------------------------------------------------------------------------
340 * Format helpers
341 */
342
343static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
344{
345 if (var->grayscale > 1)
346 return var->grayscale;
347
348 switch (var->bits_per_pixel) {
349 case 16:
350 return V4L2_PIX_FMT_RGB565;
351 case 24:
352 return V4L2_PIX_FMT_BGR24;
353 case 32:
354 return V4L2_PIX_FMT_BGR32;
355 default:
356 return 0;
357 }
358}
359
360static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
361{
362 return var->grayscale > 1;
363}
364
365static bool sh_mobile_format_is_yuv(const struct fb_var_screeninfo *var)
366{
367 if (var->grayscale <= 1)
368 return false;
369
370 switch (var->grayscale) {
371 case V4L2_PIX_FMT_NV12:
372 case V4L2_PIX_FMT_NV21:
373 case V4L2_PIX_FMT_NV16:
374 case V4L2_PIX_FMT_NV61:
375 case V4L2_PIX_FMT_NV24:
376 case V4L2_PIX_FMT_NV42:
377 return true;
378
379 default:
380 return false;
381 }
382}
383
384/* -----------------------------------------------------------------------------
385 * Start, stop and IRQ
386 */
387
Magnus Damm85645572008-12-19 15:34:41 +0900388static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
389{
390 struct sh_mobile_lcdc_priv *priv = data;
Magnus Damm2feb0752009-03-13 15:36:55 +0000391 struct sh_mobile_lcdc_chan *ch;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000392 unsigned long ldintr;
Magnus Damm2feb0752009-03-13 15:36:55 +0000393 int is_sub;
394 int k;
Magnus Damm85645572008-12-19 15:34:41 +0900395
Laurent Pinchartdc486652011-07-13 12:13:47 +0200396 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
397 ldintr = lcdc_read(priv, _LDINTR);
398 lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
Magnus Damm85645572008-12-19 15:34:41 +0900399
Magnus Damm2feb0752009-03-13 15:36:55 +0000400 /* figure out if this interrupt is for main or sub lcd */
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200401 is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
Magnus Damm2feb0752009-03-13 15:36:55 +0000402
Phil Edworthy9dd38812009-09-15 12:00:18 +0000403 /* wake up channel and disable clocks */
Magnus Damm2feb0752009-03-13 15:36:55 +0000404 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
405 ch = &priv->ch[k];
406
407 if (!ch->enabled)
408 continue;
409
Laurent Pinchartdc486652011-07-13 12:13:47 +0200410 /* Frame End */
Phil Edworthy9dd38812009-09-15 12:00:18 +0000411 if (ldintr & LDINTR_FS) {
412 if (is_sub == lcdc_chan_is_sublcd(ch)) {
413 ch->frame_end = 1;
414 wake_up(&ch->frame_end_wait);
Magnus Damm2feb0752009-03-13 15:36:55 +0000415
Phil Edworthy9dd38812009-09-15 12:00:18 +0000416 sh_mobile_lcdc_clk_off(priv);
417 }
418 }
419
420 /* VSYNC End */
Phil Edworthy40331b22010-02-15 13:57:49 +0000421 if (ldintr & LDINTR_VES)
422 complete(&ch->vsync_completion);
Magnus Damm2feb0752009-03-13 15:36:55 +0000423 }
424
Magnus Damm85645572008-12-19 15:34:41 +0900425 return IRQ_HANDLED;
426}
427
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700428static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
429 int start)
430{
431 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
432 int k;
433
434 /* start or stop the lcdc */
435 if (start)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200436 lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700437 else
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200438 lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700439
440 /* wait until power is applied/stopped on all channels */
441 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
442 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
443 while (1) {
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200444 tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
445 & LDPMR_LPS;
446 if (start && tmp == LDPMR_LPS)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700447 break;
448 if (!start && tmp == 0)
449 break;
450 cpu_relax();
451 }
452
453 if (!start)
454 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
455}
456
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000457static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
458{
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000459 struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
460 unsigned long h_total, hsync_pos, display_h_total;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000461 u32 tmp;
462
463 tmp = ch->ldmt1r_value;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200464 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
465 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
466 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
467 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
468 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
469 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
470 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000471 lcdc_write_chan(ch, LDMT1R, tmp);
472
473 /* setup SYS bus */
474 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
475 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
476
477 /* horizontal configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000478 h_total = display_var->xres + display_var->hsync_len +
479 display_var->left_margin + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000480 tmp = h_total / 8; /* HTCN */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000481 tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000482 lcdc_write_chan(ch, LDHCNR, tmp);
483
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000484 hsync_pos = display_var->xres + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000485 tmp = hsync_pos / 8; /* HSYNP */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000486 tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000487 lcdc_write_chan(ch, LDHSYNR, tmp);
488
489 /* vertical configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000490 tmp = display_var->yres + display_var->vsync_len +
491 display_var->upper_margin + display_var->lower_margin; /* VTLN */
492 tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000493 lcdc_write_chan(ch, LDVLNR, tmp);
494
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000495 tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
496 tmp |= display_var->vsync_len << 16; /* VSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000497 lcdc_write_chan(ch, LDVSYNR, tmp);
498
499 /* Adjust horizontal synchronisation for HDMI */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000500 display_h_total = display_var->xres + display_var->hsync_len +
501 display_var->left_margin + display_var->right_margin;
502 tmp = ((display_var->xres & 7) << 24) |
503 ((display_h_total & 7) << 16) |
504 ((display_var->hsync_len & 7) << 8) |
Kuninori Morimoto41e583c2011-11-08 20:33:29 -0800505 (hsync_pos & 7);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000506 lcdc_write_chan(ch, LDHAJR, tmp);
507}
508
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200509/*
510 * __sh_mobile_lcdc_start - Configure and tart the LCDC
511 * @priv: LCDC device
512 *
513 * Configure all enabled channels and start the LCDC device. All external
514 * devices (clocks, MERAM, panels, ...) are not touched by this function.
515 */
516static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700517{
518 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700519 unsigned long tmp;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200520 int k, m;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700521
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200522 /* Enable LCDC channels. Read data from external memory, avoid using the
523 * BEU for now.
524 */
525 lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
Magnus Damm85645572008-12-19 15:34:41 +0900526
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200527 /* Stop the LCDC first and disable all interrupts. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700528 sh_mobile_lcdc_start_stop(priv, 0);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200529 lcdc_write(priv, _LDINTR, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700530
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200531 /* Configure power supply, dot clocks and start them. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700532 tmp = priv->lddckr;
533 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
534 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200535 if (!ch->enabled)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700536 continue;
537
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200538 /* Power supply */
539 lcdc_write_chan(ch, LDPMR, 0);
540
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700541 m = ch->cfg.clock_divider;
542 if (!m)
543 continue;
544
Laurent Pinchart505c7de52011-07-13 12:13:47 +0200545 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
546 * denominator.
547 */
548 lcdc_write_chan(ch, LDDCKPAT1R, 0);
549 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
550
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700551 if (m == 1)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200552 m = LDDCKR_MOSEL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700553 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700554 }
555
556 lcdc_write(priv, _LDDCKR, tmp);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700557 lcdc_write(priv, _LDDCKSTPR, 0);
558 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
559
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200560 /* Setup geometry, format, frame buffer memory and operation mode. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700561 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
562 ch = &priv->ch[k];
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700563 if (!ch->enabled)
564 continue;
565
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000566 sh_mobile_lcdc_geometry(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700567
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100568 switch (sh_mobile_format_fourcc(&ch->info->var)) {
569 case V4L2_PIX_FMT_RGB565:
570 tmp = LDDFR_PKF_RGB16;
571 break;
572 case V4L2_PIX_FMT_BGR24:
573 tmp = LDDFR_PKF_RGB24;
574 break;
575 case V4L2_PIX_FMT_BGR32:
576 tmp = LDDFR_PKF_ARGB32;
577 break;
578 case V4L2_PIX_FMT_NV12:
579 case V4L2_PIX_FMT_NV21:
580 tmp = LDDFR_CC | LDDFR_YF_420;
581 break;
582 case V4L2_PIX_FMT_NV16:
583 case V4L2_PIX_FMT_NV61:
584 tmp = LDDFR_CC | LDDFR_YF_422;
585 break;
586 case V4L2_PIX_FMT_NV24:
587 case V4L2_PIX_FMT_NV42:
588 tmp = LDDFR_CC | LDDFR_YF_444;
589 break;
590 }
591
592 if (sh_mobile_format_is_yuv(&ch->info->var)) {
593 switch (ch->info->var.colorspace) {
594 case V4L2_COLORSPACE_REC709:
595 tmp |= LDDFR_CF1;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000596 break;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100597 case V4L2_COLORSPACE_JPEG:
598 tmp |= LDDFR_CF0;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000599 break;
600 }
Magnus Damm417d4822011-01-05 10:21:00 +0000601 }
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200602
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700603 lcdc_write_chan(ch, LDDFR, tmp);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200604 lcdc_write_chan(ch, LDMLSR, ch->pitch);
605 lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100606 if (sh_mobile_format_is_yuv(&ch->info->var))
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200607 lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700608
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200609 /* When using deferred I/O mode, configure the LCDC for one-shot
610 * operation and enable the frame end interrupt. Otherwise use
611 * continuous read mode.
612 */
613 if (ch->ldmt1r_value & LDMT1R_IFM &&
614 ch->cfg.sys_bus_cfg.deferred_io_msec) {
615 lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
616 lcdc_write(priv, _LDINTR, LDINTR_FE);
617 } else {
618 lcdc_write_chan(ch, LDSM1R, 0);
619 }
620 }
Damian7caa4342011-05-18 11:10:07 +0000621
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200622 /* Word and long word swap. */
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100623 switch (sh_mobile_format_fourcc(&priv->ch[0].info->var)) {
624 case V4L2_PIX_FMT_RGB565:
625 case V4L2_PIX_FMT_NV21:
626 case V4L2_PIX_FMT_NV61:
627 case V4L2_PIX_FMT_NV42:
628 tmp = LDDDSR_LS | LDDDSR_WS;
629 break;
630 case V4L2_PIX_FMT_BGR24:
631 case V4L2_PIX_FMT_NV12:
632 case V4L2_PIX_FMT_NV16:
633 case V4L2_PIX_FMT_NV24:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200634 tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100635 break;
636 case V4L2_PIX_FMT_BGR32:
637 default:
638 tmp = LDDDSR_LS;
639 break;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200640 }
641 lcdc_write(priv, _LDDDSR, tmp);
Damian7caa4342011-05-18 11:10:07 +0000642
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200643 /* Enable the display output. */
644 lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
645 sh_mobile_lcdc_start_stop(priv, 1);
646 priv->started = 1;
647}
Damian7caa4342011-05-18 11:10:07 +0000648
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200649static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
650{
651 struct sh_mobile_meram_info *mdev = priv->meram_dev;
652 struct sh_mobile_lcdc_board_cfg *board_cfg;
653 struct sh_mobile_lcdc_chan *ch;
654 unsigned long tmp;
655 int ret;
656 int k;
657
658 /* enable clocks before accessing the hardware */
659 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
660 if (priv->ch[k].enabled)
661 sh_mobile_lcdc_clk_on(priv);
662 }
663
664 /* reset */
665 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
666 lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
667
668 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
669 ch = &priv->ch[k];
670
671 if (!ch->enabled)
672 continue;
673
674 board_cfg = &ch->cfg.board_cfg;
675 if (board_cfg->setup_sys) {
676 ret = board_cfg->setup_sys(board_cfg->board_data, ch,
677 &sh_mobile_lcdc_sys_bus_ops);
678 if (ret)
679 return ret;
680 }
681 }
682
683 /* Compute frame buffer base address and pitch for each channel. */
684 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
685 struct sh_mobile_meram_cfg *cfg;
686 int pixelformat;
687
688 ch = &priv->ch[k];
689 if (!ch->enabled)
690 continue;
691
692 ch->base_addr_y = ch->info->fix.smem_start;
693 ch->base_addr_c = ch->base_addr_y
694 + ch->info->var.xres
695 * ch->info->var.yres_virtual;
696 ch->pitch = ch->info->fix.line_length;
697
698 /* Enable MERAM if possible. */
699 cfg = ch->cfg.meram_cfg;
700 if (mdev == NULL || mdev->ops == NULL || cfg == NULL)
701 continue;
702
703 /* we need to de-init configured ICBs before we can
704 * re-initialize them.
705 */
706 if (ch->meram_enabled) {
707 mdev->ops->meram_unregister(mdev, cfg);
Damian7caa4342011-05-18 11:10:07 +0000708 ch->meram_enabled = 0;
Damian7caa4342011-05-18 11:10:07 +0000709 }
710
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100711 switch (sh_mobile_format_fourcc(&ch->info->var)) {
712 case V4L2_PIX_FMT_NV12:
713 case V4L2_PIX_FMT_NV21:
714 case V4L2_PIX_FMT_NV16:
715 case V4L2_PIX_FMT_NV61:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200716 pixelformat = SH_MOBILE_MERAM_PF_NV;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100717 break;
718 case V4L2_PIX_FMT_NV24:
719 case V4L2_PIX_FMT_NV42:
720 pixelformat = SH_MOBILE_MERAM_PF_NV24;
721 break;
722 case V4L2_PIX_FMT_RGB565:
723 case V4L2_PIX_FMT_BGR24:
724 case V4L2_PIX_FMT_BGR32:
725 default:
726 pixelformat = SH_MOBILE_MERAM_PF_RGB;
727 break;
728 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700729
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200730 ret = mdev->ops->meram_register(mdev, cfg, ch->pitch,
731 ch->info->var.yres, pixelformat,
732 ch->base_addr_y, ch->base_addr_c,
733 &ch->base_addr_y, &ch->base_addr_c,
734 &ch->pitch);
735 if (!ret)
736 ch->meram_enabled = 1;
737 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700738
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200739 /* Start the LCDC. */
740 __sh_mobile_lcdc_start(priv);
741
742 /* Setup deferred I/O, tell the board code to enable the panels, and
743 * turn backlight on.
744 */
745 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
746 ch = &priv->ch[k];
747 if (!ch->enabled)
748 continue;
749
Magnus Damm85645572008-12-19 15:34:41 +0900750 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200751 if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
Magnus Damm85645572008-12-19 15:34:41 +0900752 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
753 ch->defio.delay = msecs_to_jiffies(tmp);
Paul Mundte33afdd2009-07-07 11:24:32 +0900754 ch->info->fbdefio = &ch->defio;
755 fb_deferred_io_init(ch->info);
Magnus Damm85645572008-12-19 15:34:41 +0900756 }
Magnus Damm21bc1f02009-08-15 02:53:16 +0000757
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700758 board_cfg = &ch->cfg.board_cfg;
Alexandre Courbot247f9932011-02-23 08:41:50 +0000759 if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
Guennadi Liakhovetskic2439392010-07-21 10:13:17 +0000760 board_cfg->display_on(board_cfg->board_data, ch->info);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +0000761 module_put(board_cfg->owner);
762 }
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000763
764 if (ch->bl) {
765 ch->bl->props.power = FB_BLANK_UNBLANK;
766 backlight_update_status(ch->bl);
767 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700768 }
769
770 return 0;
771}
772
773static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
774{
775 struct sh_mobile_lcdc_chan *ch;
776 struct sh_mobile_lcdc_board_cfg *board_cfg;
777 int k;
778
Magnus Damm2feb0752009-03-13 15:36:55 +0000779 /* clean up deferred io and ask board code to disable panel */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700780 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
781 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +0000782 if (!ch->enabled)
783 continue;
Magnus Damm2feb0752009-03-13 15:36:55 +0000784
785 /* deferred io mode:
786 * flush frame, and wait for frame end interrupt
787 * clean up deferred io and enable clock
788 */
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +0000789 if (ch->info && ch->info->fbdefio) {
Magnus Damm2feb0752009-03-13 15:36:55 +0000790 ch->frame_end = 0;
Paul Mundte33afdd2009-07-07 11:24:32 +0900791 schedule_delayed_work(&ch->info->deferred_work, 0);
Magnus Damm2feb0752009-03-13 15:36:55 +0000792 wait_event(ch->frame_end_wait, ch->frame_end);
Paul Mundte33afdd2009-07-07 11:24:32 +0900793 fb_deferred_io_cleanup(ch->info);
794 ch->info->fbdefio = NULL;
Magnus Damm2feb0752009-03-13 15:36:55 +0000795 sh_mobile_lcdc_clk_on(priv);
796 }
797
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000798 if (ch->bl) {
799 ch->bl->props.power = FB_BLANK_POWERDOWN;
800 backlight_update_status(ch->bl);
801 }
802
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700803 board_cfg = &ch->cfg.board_cfg;
Alexandre Courbot247f9932011-02-23 08:41:50 +0000804 if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700805 board_cfg->display_off(board_cfg->board_data);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +0000806 module_put(board_cfg->owner);
807 }
Damian7caa4342011-05-18 11:10:07 +0000808
809 /* disable the meram */
810 if (ch->meram_enabled) {
811 struct sh_mobile_meram_cfg *cfg;
812 struct sh_mobile_meram_info *mdev;
813 cfg = ch->cfg.meram_cfg;
814 mdev = priv->meram_dev;
815 mdev->ops->meram_unregister(mdev, cfg);
816 ch->meram_enabled = 0;
817 }
818
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700819 }
820
821 /* stop the lcdc */
Magnus Damm8e9bb192009-05-20 14:34:43 +0000822 if (priv->started) {
823 sh_mobile_lcdc_start_stop(priv, 0);
824 priv->started = 0;
825 }
Magnus Dammb51339f2008-10-31 20:23:26 +0900826
Magnus Damm85645572008-12-19 15:34:41 +0900827 /* stop clocks */
828 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
829 if (priv->ch[k].enabled)
830 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700831}
832
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200833/* -----------------------------------------------------------------------------
834 * Frame buffer operations
835 */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700836
837static int sh_mobile_lcdc_setcolreg(u_int regno,
838 u_int red, u_int green, u_int blue,
839 u_int transp, struct fb_info *info)
840{
841 u32 *palette = info->pseudo_palette;
842
843 if (regno >= PALETTE_NR)
844 return -EINVAL;
845
846 /* only FB_VISUAL_TRUECOLOR supported */
847
848 red >>= 16 - info->var.red.length;
849 green >>= 16 - info->var.green.length;
850 blue >>= 16 - info->var.blue.length;
851 transp >>= 16 - info->var.transp.length;
852
853 palette[regno] = (red << info->var.red.offset) |
854 (green << info->var.green.offset) |
855 (blue << info->var.blue.offset) |
856 (transp << info->var.transp.offset);
857
858 return 0;
859}
860
861static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
862 .id = "SH Mobile LCDC",
863 .type = FB_TYPE_PACKED_PIXELS,
864 .visual = FB_VISUAL_TRUECOLOR,
865 .accel = FB_ACCEL_NONE,
Phil Edworthy9dd38812009-09-15 12:00:18 +0000866 .xpanstep = 0,
867 .ypanstep = 1,
868 .ywrapstep = 0,
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100869 .capabilities = FB_CAP_FOURCC,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700870};
871
Magnus Damm85645572008-12-19 15:34:41 +0900872static void sh_mobile_lcdc_fillrect(struct fb_info *info,
873 const struct fb_fillrect *rect)
874{
875 sys_fillrect(info, rect);
876 sh_mobile_lcdc_deferred_io_touch(info);
877}
878
879static void sh_mobile_lcdc_copyarea(struct fb_info *info,
880 const struct fb_copyarea *area)
881{
882 sys_copyarea(info, area);
883 sh_mobile_lcdc_deferred_io_touch(info);
884}
885
886static void sh_mobile_lcdc_imageblit(struct fb_info *info,
887 const struct fb_image *image)
888{
889 sys_imageblit(info, image);
890 sh_mobile_lcdc_deferred_io_touch(info);
891}
892
Phil Edworthy9dd38812009-09-15 12:00:18 +0000893static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
894 struct fb_info *info)
895{
896 struct sh_mobile_lcdc_chan *ch = info->par;
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000897 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
898 unsigned long ldrcntr;
899 unsigned long new_pan_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000900 unsigned long base_addr_y, base_addr_c;
901 unsigned long c_offset;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100902 bool yuv = sh_mobile_format_is_yuv(&info->var);
Phil Edworthy9dd38812009-09-15 12:00:18 +0000903
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100904 if (!yuv)
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +0200905 new_pan_offset = var->yoffset * info->fix.line_length
906 + var->xoffset * (info->var.bits_per_pixel / 8);
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000907 else
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +0200908 new_pan_offset = var->yoffset * info->fix.line_length
909 + var->xoffset;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000910
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000911 if (new_pan_offset == ch->pan_offset)
912 return 0; /* No change, do nothing */
913
914 ldrcntr = lcdc_read(priv, _LDRCNTR);
915
916 /* Set the source address for the next refresh */
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000917 base_addr_y = ch->dma_handle + new_pan_offset;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100918 if (yuv) {
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000919 /* Set y offset */
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +0200920 c_offset = var->yoffset * info->fix.line_length
921 * (info->var.bits_per_pixel - 8) / 8;
922 base_addr_c = ch->dma_handle
923 + info->var.xres * info->var.yres_virtual
924 + c_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000925 /* Set x offset */
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100926 if (sh_mobile_format_fourcc(&info->var) == V4L2_PIX_FMT_NV24)
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000927 base_addr_c += 2 * var->xoffset;
928 else
929 base_addr_c += var->xoffset;
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200930 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000931
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200932 if (ch->meram_enabled) {
Damian7caa4342011-05-18 11:10:07 +0000933 struct sh_mobile_meram_cfg *cfg;
934 struct sh_mobile_meram_info *mdev;
Damian7caa4342011-05-18 11:10:07 +0000935 int ret;
936
937 cfg = ch->cfg.meram_cfg;
938 mdev = priv->meram_dev;
939 ret = mdev->ops->meram_update(mdev, cfg,
940 base_addr_y, base_addr_c,
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200941 &base_addr_y, &base_addr_c);
Damian7caa4342011-05-18 11:10:07 +0000942 if (ret)
943 return ret;
Damian7caa4342011-05-18 11:10:07 +0000944 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000945
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200946 ch->base_addr_y = base_addr_y;
947 ch->base_addr_c = base_addr_c;
948
949 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100950 if (yuv)
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200951 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
952
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000953 if (lcdc_chan_is_sublcd(ch))
954 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
955 else
956 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
957
958 ch->pan_offset = new_pan_offset;
959
960 sh_mobile_lcdc_deferred_io_touch(info);
Phil Edworthy9dd38812009-09-15 12:00:18 +0000961
962 return 0;
963}
964
Phil Edworthy40331b22010-02-15 13:57:49 +0000965static int sh_mobile_wait_for_vsync(struct fb_info *info)
966{
967 struct sh_mobile_lcdc_chan *ch = info->par;
968 unsigned long ldintr;
969 int ret;
970
Laurent Pinchartdc486652011-07-13 12:13:47 +0200971 /* Enable VSync End interrupt and be careful not to acknowledge any
972 * pending interrupt.
973 */
Phil Edworthy40331b22010-02-15 13:57:49 +0000974 ldintr = lcdc_read(ch->lcdc, _LDINTR);
Laurent Pinchartdc486652011-07-13 12:13:47 +0200975 ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
Phil Edworthy40331b22010-02-15 13:57:49 +0000976 lcdc_write(ch->lcdc, _LDINTR, ldintr);
977
978 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
979 msecs_to_jiffies(100));
980 if (!ret)
981 return -ETIMEDOUT;
982
983 return 0;
984}
985
986static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
987 unsigned long arg)
988{
989 int retval;
990
991 switch (cmd) {
992 case FBIO_WAITFORVSYNC:
993 retval = sh_mobile_wait_for_vsync(info);
994 break;
995
996 default:
997 retval = -ENOIOCTLCMD;
998 break;
999 }
1000 return retval;
1001}
1002
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001003static void sh_mobile_fb_reconfig(struct fb_info *info)
1004{
1005 struct sh_mobile_lcdc_chan *ch = info->par;
1006 struct fb_videomode mode1, mode2;
1007 struct fb_event event;
1008 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1009
1010 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1011 /* More framebuffer users are active */
1012 return;
1013
1014 fb_var_to_videomode(&mode1, &ch->display_var);
1015 fb_var_to_videomode(&mode2, &info->var);
1016
1017 if (fb_mode_is_equal(&mode1, &mode2))
1018 return;
1019
1020 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1021 if (fb_set_var(info, &ch->display_var) < 0)
1022 /* Couldn't reconfigure, hopefully, can continue as before */
1023 return;
1024
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001025 /*
1026 * fb_set_var() calls the notifier change internally, only if
1027 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1028 * user event, we have to call the chain ourselves.
1029 */
1030 event.info = info;
Arnd Hannemanncc267ec2010-11-15 21:43:22 +00001031 event.data = &mode1;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001032 fb_notifier_call_chain(evnt, &event);
1033}
1034
1035/*
1036 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1037 * user == 1, or with console sem held, if user == 0.
1038 */
1039static int sh_mobile_release(struct fb_info *info, int user)
1040{
1041 struct sh_mobile_lcdc_chan *ch = info->par;
1042
1043 mutex_lock(&ch->open_lock);
1044 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1045
1046 ch->use_count--;
1047
1048 /* Nothing to reconfigure, when called from fbcon */
1049 if (user) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001050 console_lock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001051 sh_mobile_fb_reconfig(info);
Torben Hohnac751ef2011-01-25 15:07:35 -08001052 console_unlock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001053 }
1054
1055 mutex_unlock(&ch->open_lock);
1056
1057 return 0;
1058}
1059
1060static int sh_mobile_open(struct fb_info *info, int user)
1061{
1062 struct sh_mobile_lcdc_chan *ch = info->par;
1063
1064 mutex_lock(&ch->open_lock);
1065 ch->use_count++;
1066
1067 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1068 mutex_unlock(&ch->open_lock);
1069
1070 return 0;
1071}
1072
1073static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1074{
1075 struct sh_mobile_lcdc_chan *ch = info->par;
Magnus Damm417d4822011-01-05 10:21:00 +00001076 struct sh_mobile_lcdc_priv *p = ch->lcdc;
Laurent Pinchart03862192011-08-31 13:00:53 +02001077 unsigned int best_dist = (unsigned int)-1;
1078 unsigned int best_xres = 0;
1079 unsigned int best_yres = 0;
1080 unsigned int i;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001081
Laurent Pinchart03862192011-08-31 13:00:53 +02001082 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001083 return -EINVAL;
Laurent Pinchart03862192011-08-31 13:00:53 +02001084
1085 /* If board code provides us with a list of available modes, make sure
1086 * we use one of them. Find the mode closest to the requested one. The
1087 * distance between two modes is defined as the size of the
1088 * non-overlapping parts of the two rectangles.
1089 */
1090 for (i = 0; i < ch->cfg.num_cfg; ++i) {
1091 const struct fb_videomode *mode = &ch->cfg.lcd_cfg[i];
1092 unsigned int dist;
1093
1094 /* We can only round up. */
1095 if (var->xres > mode->xres || var->yres > mode->yres)
1096 continue;
1097
1098 dist = var->xres * var->yres + mode->xres * mode->yres
1099 - 2 * min(var->xres, mode->xres)
1100 * min(var->yres, mode->yres);
1101
1102 if (dist < best_dist) {
1103 best_xres = mode->xres;
1104 best_yres = mode->yres;
1105 best_dist = dist;
1106 }
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001107 }
Magnus Damm417d4822011-01-05 10:21:00 +00001108
Laurent Pinchart03862192011-08-31 13:00:53 +02001109 /* If no available mode can be used, return an error. */
1110 if (ch->cfg.num_cfg != 0) {
1111 if (best_dist == (unsigned int)-1)
1112 return -EINVAL;
1113
1114 var->xres = best_xres;
1115 var->yres = best_yres;
1116 }
1117
1118 /* Make sure the virtual resolution is at least as big as the visible
1119 * resolution.
1120 */
1121 if (var->xres_virtual < var->xres)
1122 var->xres_virtual = var->xres;
1123 if (var->yres_virtual < var->yres)
1124 var->yres_virtual = var->yres;
1125
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001126 if (sh_mobile_format_is_fourcc(var)) {
1127 switch (var->grayscale) {
1128 case V4L2_PIX_FMT_NV12:
1129 case V4L2_PIX_FMT_NV21:
1130 var->bits_per_pixel = 12;
1131 break;
1132 case V4L2_PIX_FMT_RGB565:
1133 case V4L2_PIX_FMT_NV16:
1134 case V4L2_PIX_FMT_NV61:
1135 var->bits_per_pixel = 16;
1136 break;
1137 case V4L2_PIX_FMT_BGR24:
1138 case V4L2_PIX_FMT_NV24:
1139 case V4L2_PIX_FMT_NV42:
1140 var->bits_per_pixel = 24;
1141 break;
1142 case V4L2_PIX_FMT_BGR32:
1143 var->bits_per_pixel = 32;
1144 break;
1145 default:
1146 return -EINVAL;
1147 }
Laurent Pinchart03862192011-08-31 13:00:53 +02001148
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001149 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1150 * respectively.
1151 */
1152 if (!sh_mobile_format_is_yuv(var))
1153 var->colorspace = V4L2_COLORSPACE_SRGB;
1154 else if (var->colorspace != V4L2_COLORSPACE_REC709)
1155 var->colorspace = V4L2_COLORSPACE_JPEG;
1156 } else {
1157 if (var->bits_per_pixel <= 16) { /* RGB 565 */
1158 var->bits_per_pixel = 16;
1159 var->red.offset = 11;
1160 var->red.length = 5;
1161 var->green.offset = 5;
1162 var->green.length = 6;
1163 var->blue.offset = 0;
1164 var->blue.length = 5;
1165 var->transp.offset = 0;
1166 var->transp.length = 0;
1167 } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
1168 var->bits_per_pixel = 24;
1169 var->red.offset = 16;
1170 var->red.length = 8;
1171 var->green.offset = 8;
1172 var->green.length = 8;
1173 var->blue.offset = 0;
1174 var->blue.length = 8;
1175 var->transp.offset = 0;
1176 var->transp.length = 0;
1177 } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
1178 var->bits_per_pixel = 32;
1179 var->red.offset = 16;
1180 var->red.length = 8;
1181 var->green.offset = 8;
1182 var->green.length = 8;
1183 var->blue.offset = 0;
1184 var->blue.length = 8;
1185 var->transp.offset = 24;
1186 var->transp.length = 8;
1187 } else
1188 return -EINVAL;
1189
1190 var->red.msb_right = 0;
1191 var->green.msb_right = 0;
1192 var->blue.msb_right = 0;
1193 var->transp.msb_right = 0;
1194 }
Laurent Pinchart03862192011-08-31 13:00:53 +02001195
1196 /* Make sure we don't exceed our allocated memory. */
1197 if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
1198 info->fix.smem_len)
1199 return -EINVAL;
1200
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001201 /* only accept the forced_fourcc for dual channel configurations */
1202 if (p->forced_fourcc &&
1203 p->forced_fourcc != sh_mobile_format_fourcc(var))
Magnus Damm417d4822011-01-05 10:21:00 +00001204 return -EINVAL;
1205
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001206 return 0;
1207}
Phil Edworthy40331b22010-02-15 13:57:49 +00001208
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001209static int sh_mobile_set_par(struct fb_info *info)
1210{
1211 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchart91fba482011-08-31 13:00:56 +02001212 u32 line_length = info->fix.line_length;
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001213 int ret;
1214
1215 sh_mobile_lcdc_stop(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001216
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001217 if (sh_mobile_format_is_yuv(&info->var))
Laurent Pinchart91fba482011-08-31 13:00:56 +02001218 info->fix.line_length = info->var.xres;
1219 else
1220 info->fix.line_length = info->var.xres
1221 * info->var.bits_per_pixel / 8;
1222
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001223 ret = sh_mobile_lcdc_start(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001224 if (ret < 0) {
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001225 dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001226 info->fix.line_length = line_length;
1227 }
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001228
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001229 if (sh_mobile_format_is_fourcc(&info->var)) {
1230 info->fix.type = FB_TYPE_FOURCC;
1231 info->fix.visual = FB_VISUAL_FOURCC;
1232 } else {
1233 info->fix.type = FB_TYPE_PACKED_PIXELS;
1234 info->fix.visual = FB_VISUAL_TRUECOLOR;
1235 }
1236
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001237 return ret;
1238}
1239
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001240/*
1241 * Screen blanking. Behavior is as follows:
1242 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1243 * FB_BLANK_NORMAL: screen blanked, clocks enabled
1244 * FB_BLANK_VSYNC,
1245 * FB_BLANK_HSYNC,
1246 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1247 */
1248static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
1249{
1250 struct sh_mobile_lcdc_chan *ch = info->par;
1251 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1252
1253 /* blank the screen? */
1254 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
1255 struct fb_fillrect rect = {
1256 .width = info->var.xres,
1257 .height = info->var.yres,
1258 };
1259 sh_mobile_lcdc_fillrect(info, &rect);
1260 }
1261 /* turn clocks on? */
1262 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
1263 sh_mobile_lcdc_clk_on(p);
1264 }
1265 /* turn clocks off? */
1266 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
1267 /* make sure the screen is updated with the black fill before
1268 * switching the clocks off. one vsync is not enough since
1269 * blanking may occur in the middle of a refresh. deferred io
1270 * mode will reenable the clocks and update the screen in time,
1271 * so it does not need this. */
1272 if (!info->fbdefio) {
1273 sh_mobile_wait_for_vsync(info);
1274 sh_mobile_wait_for_vsync(info);
1275 }
1276 sh_mobile_lcdc_clk_off(p);
1277 }
1278
1279 ch->blank_status = blank;
1280 return 0;
1281}
1282
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001283static struct fb_ops sh_mobile_lcdc_ops = {
Phil Edworthy9dd38812009-09-15 12:00:18 +00001284 .owner = THIS_MODULE,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001285 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
Magnus Damm2540c112008-12-17 17:29:49 +09001286 .fb_read = fb_sys_read,
1287 .fb_write = fb_sys_write,
Magnus Damm85645572008-12-19 15:34:41 +09001288 .fb_fillrect = sh_mobile_lcdc_fillrect,
1289 .fb_copyarea = sh_mobile_lcdc_copyarea,
1290 .fb_imageblit = sh_mobile_lcdc_imageblit,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001291 .fb_blank = sh_mobile_lcdc_blank,
Phil Edworthy9dd38812009-09-15 12:00:18 +00001292 .fb_pan_display = sh_mobile_fb_pan_display,
Phil Edworthy40331b22010-02-15 13:57:49 +00001293 .fb_ioctl = sh_mobile_ioctl,
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001294 .fb_open = sh_mobile_open,
1295 .fb_release = sh_mobile_release,
1296 .fb_check_var = sh_mobile_check_var,
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001297 .fb_set_par = sh_mobile_set_par,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001298};
1299
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001300/* -----------------------------------------------------------------------------
1301 * Backlight
1302 */
1303
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001304static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
1305{
1306 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1307 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1308 int brightness = bdev->props.brightness;
1309
1310 if (bdev->props.power != FB_BLANK_UNBLANK ||
1311 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
1312 brightness = 0;
1313
1314 return cfg->set_brightness(cfg->board_data, brightness);
1315}
1316
1317static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
1318{
1319 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1320 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1321
1322 return cfg->get_brightness(cfg->board_data);
1323}
1324
1325static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
1326 struct fb_info *info)
1327{
1328 return (info->bl_dev == bdev);
1329}
1330
1331static struct backlight_ops sh_mobile_lcdc_bl_ops = {
1332 .options = BL_CORE_SUSPENDRESUME,
1333 .update_status = sh_mobile_lcdc_update_bl,
1334 .get_brightness = sh_mobile_lcdc_get_brightness,
1335 .check_fb = sh_mobile_lcdc_check_fb,
1336};
1337
1338static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
1339 struct sh_mobile_lcdc_chan *ch)
1340{
1341 struct backlight_device *bl;
1342
1343 bl = backlight_device_register(ch->cfg.bl_info.name, parent, ch,
1344 &sh_mobile_lcdc_bl_ops, NULL);
Dan Carpenterbeee1f22011-03-21 15:03:13 +00001345 if (IS_ERR(bl)) {
1346 dev_err(parent, "unable to register backlight device: %ld\n",
1347 PTR_ERR(bl));
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001348 return NULL;
1349 }
1350
1351 bl->props.max_brightness = ch->cfg.bl_info.max_brightness;
1352 bl->props.brightness = bl->props.max_brightness;
1353 backlight_update_status(bl);
1354
1355 return bl;
1356}
1357
1358static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
1359{
1360 backlight_device_unregister(bdev);
1361}
1362
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001363/* -----------------------------------------------------------------------------
1364 * Power management
1365 */
1366
Magnus Damm2feb0752009-03-13 15:36:55 +00001367static int sh_mobile_lcdc_suspend(struct device *dev)
1368{
1369 struct platform_device *pdev = to_platform_device(dev);
1370
1371 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
1372 return 0;
1373}
1374
1375static int sh_mobile_lcdc_resume(struct device *dev)
1376{
1377 struct platform_device *pdev = to_platform_device(dev);
1378
1379 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
1380}
1381
Magnus Damm0246c472009-08-14 10:49:08 +00001382static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
1383{
1384 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001385 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001386
1387 /* turn off LCDC hardware */
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001388 lcdc_write(priv, _LDCNT1R, 0);
1389
Magnus Damm0246c472009-08-14 10:49:08 +00001390 return 0;
1391}
1392
1393static int sh_mobile_lcdc_runtime_resume(struct device *dev)
1394{
1395 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001396 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001397
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001398 __sh_mobile_lcdc_start(priv);
Magnus Damm0246c472009-08-14 10:49:08 +00001399
1400 return 0;
1401}
1402
Alexey Dobriyan47145212009-12-14 18:00:08 -08001403static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
Magnus Damm2feb0752009-03-13 15:36:55 +00001404 .suspend = sh_mobile_lcdc_suspend,
1405 .resume = sh_mobile_lcdc_resume,
Magnus Damm0246c472009-08-14 10:49:08 +00001406 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
1407 .runtime_resume = sh_mobile_lcdc_runtime_resume,
Magnus Damm2feb0752009-03-13 15:36:55 +00001408};
1409
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001410/* -----------------------------------------------------------------------------
1411 * Framebuffer notifier
1412 */
1413
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001414/* locking: called with info->lock held */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001415static int sh_mobile_lcdc_notify(struct notifier_block *nb,
1416 unsigned long action, void *data)
1417{
1418 struct fb_event *event = data;
1419 struct fb_info *info = event->info;
1420 struct sh_mobile_lcdc_chan *ch = info->par;
1421 struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001422
1423 if (&ch->lcdc->notifier != nb)
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001424 return NOTIFY_DONE;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001425
1426 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
1427 __func__, action, event->data);
1428
1429 switch(action) {
1430 case FB_EVENT_SUSPEND:
Alexandre Courbot247f9932011-02-23 08:41:50 +00001431 if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001432 board_cfg->display_off(board_cfg->board_data);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001433 module_put(board_cfg->owner);
1434 }
Guennadi Liakhovetskiafe417c2010-09-03 07:20:39 +00001435 sh_mobile_lcdc_stop(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001436 break;
1437 case FB_EVENT_RESUME:
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001438 mutex_lock(&ch->open_lock);
1439 sh_mobile_fb_reconfig(info);
1440 mutex_unlock(&ch->open_lock);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001441
1442 /* HDMI must be enabled before LCDC configuration */
Alexandre Courbot247f9932011-02-23 08:41:50 +00001443 if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001444 board_cfg->display_on(board_cfg->board_data, info);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001445 module_put(board_cfg->owner);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001446 }
1447
Guennadi Liakhovetskiebe5e122011-05-05 16:33:40 +00001448 sh_mobile_lcdc_start(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001449 }
1450
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001451 return NOTIFY_OK;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001452}
1453
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001454/* -----------------------------------------------------------------------------
1455 * Probe/remove and driver init/exit
1456 */
1457
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001458static const struct fb_videomode default_720p __devinitconst = {
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001459 .name = "HDMI 720p",
1460 .xres = 1280,
1461 .yres = 720,
1462
1463 .left_margin = 220,
1464 .right_margin = 110,
1465 .hsync_len = 40,
1466
1467 .upper_margin = 20,
1468 .lower_margin = 5,
1469 .vsync_len = 5,
1470
1471 .pixclock = 13468,
1472 .refresh = 60,
1473 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
1474};
1475
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001476static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1477{
1478 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1479 struct fb_info *info;
1480 int i;
1481
1482 fb_unregister_client(&priv->notifier);
1483
1484 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
1485 if (priv->ch[i].info && priv->ch[i].info->dev)
1486 unregister_framebuffer(priv->ch[i].info);
1487
1488 sh_mobile_lcdc_stop(priv);
1489
1490 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1491 info = priv->ch[i].info;
1492
1493 if (!info || !info->device)
1494 continue;
1495
1496 if (priv->ch[i].sglist)
1497 vfree(priv->ch[i].sglist);
1498
1499 if (info->screen_base)
1500 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1501 info->screen_base,
1502 priv->ch[i].dma_handle);
1503 fb_dealloc_cmap(&info->cmap);
1504 framebuffer_release(info);
1505 }
1506
1507 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1508 if (priv->ch[i].bl)
1509 sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
1510 }
1511
Laurent Pinchart4774c122011-09-07 15:47:07 +02001512 if (priv->dot_clk) {
1513 pm_runtime_disable(&pdev->dev);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001514 clk_put(priv->dot_clk);
Laurent Pinchart4774c122011-09-07 15:47:07 +02001515 }
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001516
1517 if (priv->base)
1518 iounmap(priv->base);
1519
1520 if (priv->irq)
1521 free_irq(priv->irq, priv);
1522 kfree(priv);
1523 return 0;
1524}
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001525
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001526static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001527{
1528 int interface_type = ch->cfg.interface_type;
1529
1530 switch (interface_type) {
1531 case RGB8:
1532 case RGB9:
1533 case RGB12A:
1534 case RGB12B:
1535 case RGB16:
1536 case RGB18:
1537 case RGB24:
1538 case SYS8A:
1539 case SYS8B:
1540 case SYS8C:
1541 case SYS8D:
1542 case SYS9:
1543 case SYS12:
1544 case SYS16A:
1545 case SYS16B:
1546 case SYS16C:
1547 case SYS18:
1548 case SYS24:
1549 break;
1550 default:
1551 return -EINVAL;
1552 }
1553
1554 /* SUBLCD only supports SYS interface */
1555 if (lcdc_chan_is_sublcd(ch)) {
1556 if (!(interface_type & LDMT1R_IFM))
1557 return -EINVAL;
1558
1559 interface_type &= ~LDMT1R_IFM;
1560 }
1561
1562 ch->ldmt1r_value = interface_type;
1563 return 0;
1564}
1565
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001566static int __devinit sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch,
1567 struct device *dev)
1568{
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 Pinchart3ce05592011-08-31 13:00:58 +02001583 ch->info = framebuffer_alloc(0, dev);
1584 if (!ch->info) {
1585 dev_err(dev, "unable to allocate fb_info\n");
1586 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 Pinchart3ce05592011-08-31 13:00:58 +02001607 dev_err(dev, "yres must be multiple of 2 for YCbCr420 "
1608 "mode.\n");
1609 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
1621 dev_dbg(dev, "Found largest videomode %ux%u\n",
1622 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 Pinchartedd153a2011-12-13 14:02:28 +01001672 buf = dma_alloc_coherent(dev, max_size, &ch->dma_handle, GFP_KERNEL);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001673 if (!buf) {
1674 dev_err(dev, "unable to allocate buffer\n");
1675 return -ENOMEM;
1676 }
1677
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001678 ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1679 if (ret < 0) {
1680 dev_err(dev, "unable to allocate cmap\n");
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001681 dma_free_coherent(dev, max_size, buf, ch->dma_handle);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001682 return ret;
1683 }
1684
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001685 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1686 * for NV12 and NV21.
1687 */
1688 info->fix = sh_mobile_lcdc_fix;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001689 info->fix.smem_start = ch->dma_handle;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001690 info->fix.smem_len = max_size;
1691 if (cfg->fourcc == V4L2_PIX_FMT_NV12 ||
1692 cfg->fourcc == V4L2_PIX_FMT_NV21)
1693 info->fix.ypanstep = 2;
1694
1695 if (sh_mobile_format_is_yuv(var)) {
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001696 info->fix.line_length = var->xres;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001697 info->fix.visual = FB_VISUAL_FOURCC;
1698 } else {
1699 info->fix.line_length = var->xres * var->bits_per_pixel / 8;
1700 info->fix.visual = FB_VISUAL_TRUECOLOR;
1701 }
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001702
1703 info->screen_base = buf;
1704 info->device = dev;
1705 ch->display_var = *var;
1706
1707 return 0;
1708}
1709
Uwe Kleine-Königc2e13032010-02-04 20:56:51 +01001710static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001711{
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001712 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001713 struct sh_mobile_lcdc_priv *priv;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001714 struct resource *res;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001715 int num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001716 int error;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001717 int i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001718
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001719 if (!pdata) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001720 dev_err(&pdev->dev, "no platform data defined\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001721 return -EINVAL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001722 }
1723
1724 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Damm85645572008-12-19 15:34:41 +09001725 i = platform_get_irq(pdev, 0);
1726 if (!res || i < 0) {
1727 dev_err(&pdev->dev, "cannot get platform resources\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001728 return -ENOENT;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001729 }
1730
1731 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1732 if (!priv) {
1733 dev_err(&pdev->dev, "cannot allocate device data\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001734 return -ENOMEM;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001735 }
1736
Laurent Pinchart4774c122011-09-07 15:47:07 +02001737 priv->dev = &pdev->dev;
1738 priv->meram_dev = pdata->meram_dev;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001739 platform_set_drvdata(pdev, priv);
1740
Yong Zhangf8798cc2011-09-22 16:59:16 +08001741 error = request_irq(i, sh_mobile_lcdc_irq, 0,
Kay Sievers7ad33e72009-03-24 16:38:21 -07001742 dev_name(&pdev->dev), priv);
Magnus Damm85645572008-12-19 15:34:41 +09001743 if (error) {
1744 dev_err(&pdev->dev, "unable to request irq\n");
1745 goto err1;
1746 }
1747
1748 priv->irq = i;
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +00001749 atomic_set(&priv->hw_usecnt, -1);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001750
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001751 for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
1752 struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001753
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001754 ch->lcdc = priv;
1755 memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
1756
1757 error = sh_mobile_lcdc_check_interface(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001758 if (error) {
1759 dev_err(&pdev->dev, "unsupported interface type\n");
1760 goto err1;
1761 }
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001762 init_waitqueue_head(&ch->frame_end_wait);
1763 init_completion(&ch->vsync_completion);
1764 ch->pan_offset = 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001765
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001766 /* probe the backlight is there is one defined */
1767 if (ch->cfg.bl_info.max_brightness)
1768 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
1769
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001770 switch (pdata->ch[i].chan) {
1771 case LCDC_CHAN_MAINLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001772 ch->enabled = LDCNT2R_ME;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001773 ch->reg_offs = lcdc_offs_mainlcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001774 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001775 break;
1776 case LCDC_CHAN_SUBLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001777 ch->enabled = LDCNT2R_SE;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001778 ch->reg_offs = lcdc_offs_sublcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001779 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001780 break;
1781 }
1782 }
1783
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001784 if (!num_channels) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001785 dev_err(&pdev->dev, "no channels defined\n");
1786 error = -EINVAL;
1787 goto err1;
1788 }
1789
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001790 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001791 if (num_channels == 2)
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001792 priv->forced_fourcc = pdata->ch[0].fourcc;
Magnus Damm417d4822011-01-05 10:21:00 +00001793
Guennadi Liakhovetskidba6f382010-06-30 09:26:35 +00001794 priv->base = ioremap_nocache(res->start, resource_size(res));
1795 if (!priv->base)
1796 goto err1;
1797
Magnus Dammb51339f2008-10-31 20:23:26 +09001798 error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001799 if (error) {
1800 dev_err(&pdev->dev, "unable to setup clocks\n");
1801 goto err1;
1802 }
1803
Laurent Pinchart4774c122011-09-07 15:47:07 +02001804 /* Enable runtime PM. */
1805 pm_runtime_enable(&pdev->dev);
Damian7caa4342011-05-18 11:10:07 +00001806
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001807 for (i = 0; i < num_channels; i++) {
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001808 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001809
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001810 error = sh_mobile_lcdc_channel_init(ch, &pdev->dev);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001811 if (error)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001812 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001813 }
1814
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001815 error = sh_mobile_lcdc_start(priv);
1816 if (error) {
1817 dev_err(&pdev->dev, "unable to start hardware\n");
1818 goto err1;
1819 }
1820
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001821 for (i = 0; i < num_channels; i++) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00001822 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001823 struct fb_info *info = ch->info;
Paul Mundt1c6a3072009-07-01 06:50:31 +00001824
1825 if (info->fbdefio) {
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001826 ch->sglist = vmalloc(sizeof(struct scatterlist) *
Paul Mundt1c6a3072009-07-01 06:50:31 +00001827 info->fix.smem_len >> PAGE_SHIFT);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001828 if (!ch->sglist) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00001829 dev_err(&pdev->dev, "cannot allocate sglist\n");
1830 goto err1;
1831 }
1832 }
1833
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001834 info->bl_dev = ch->bl;
1835
Paul Mundt1c6a3072009-07-01 06:50:31 +00001836 error = register_framebuffer(info);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001837 if (error < 0)
1838 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001839
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001840 dev_info(info->dev, "registered %s/%s as %dx%d %dbpp.\n",
1841 pdev->name, (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
1842 "mainlcd" : "sublcd", info->var.xres, info->var.yres,
1843 info->var.bits_per_pixel);
Magnus Damm85645572008-12-19 15:34:41 +09001844
1845 /* deferred io mode: disable clock to save power */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001846 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
Magnus Damm85645572008-12-19 15:34:41 +09001847 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001848 }
1849
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001850 /* Failure ignored */
1851 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
1852 fb_register_client(&priv->notifier);
1853
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001854 return 0;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001855err1:
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001856 sh_mobile_lcdc_remove(pdev);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001857
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001858 return error;
1859}
1860
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001861static struct platform_driver sh_mobile_lcdc_driver = {
1862 .driver = {
1863 .name = "sh_mobile_lcdc_fb",
1864 .owner = THIS_MODULE,
Magnus Damm2feb0752009-03-13 15:36:55 +00001865 .pm = &sh_mobile_lcdc_dev_pm_ops,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001866 },
1867 .probe = sh_mobile_lcdc_probe,
1868 .remove = sh_mobile_lcdc_remove,
1869};
1870
Axel Lin4277f2c2011-11-26 10:25:54 +08001871module_platform_driver(sh_mobile_lcdc_driver);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001872
1873MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1874MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1875MODULE_LICENSE("GPL v2");