blob: a899b6df46cc9c69b47f09e14bc296908e19c6f0 [file] [log] [blame]
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001/*
2 * SuperH Mobile LCDC Framebuffer
3 *
4 * Copyright (c) 2008 Magnus Damm
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
10
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020011#include <linux/atomic.h>
12#include <linux/backlight.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070013#include <linux/clk.h>
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020014#include <linux/console.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070015#include <linux/dma-mapping.h>
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020016#include <linux/delay.h>
17#include <linux/gpio.h>
18#include <linux/init.h>
Magnus Damm85645572008-12-19 15:34:41 +090019#include <linux/interrupt.h>
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020020#include <linux/ioctl.h>
21#include <linux/kernel.h>
22#include <linux/mm.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
26#include <linux/slab.h>
Laurent Pinchartedd153a2011-12-13 14:02:28 +010027#include <linux/videodev2.h>
Paul Mundt1c6a3072009-07-01 06:50:31 +000028#include <linux/vmalloc.h>
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020029
Paul Mundt225c9a82008-10-01 16:24:32 +090030#include <video/sh_mobile_lcdc.h>
Laurent Pinchart8a209742011-07-13 12:13:47 +020031#include <video/sh_mobile_meram.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070032
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +000033#include "sh_mobile_lcdcfb.h"
34
Phil Edworthya6f15ad2009-09-15 12:00:30 +000035#define SIDE_B_OFFSET 0x1000
36#define MIRROR_OFFSET 0x2000
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070037
Guennadi Liakhovetskid2ecbab2010-11-04 11:06:06 +000038#define MAX_XRES 1920
39#define MAX_YRES 1080
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070040
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020041struct sh_mobile_lcdc_priv {
42 void __iomem *base;
43 int irq;
44 atomic_t hw_usecnt;
45 struct device *dev;
46 struct clk *dot_clk;
47 unsigned long lddckr;
48 struct sh_mobile_lcdc_chan ch[2];
49 struct notifier_block notifier;
50 int started;
51 int forced_fourcc; /* 2 channel LCDC must share fourcc setting */
52 struct sh_mobile_meram_info *meram_dev;
53};
54
55/* -----------------------------------------------------------------------------
56 * Registers access
57 */
58
Magnus Damm0246c472009-08-14 10:49:08 +000059static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070060 [LDDCKPAT1R] = 0x400,
61 [LDDCKPAT2R] = 0x404,
62 [LDMT1R] = 0x418,
63 [LDMT2R] = 0x41c,
64 [LDMT3R] = 0x420,
65 [LDDFR] = 0x424,
66 [LDSM1R] = 0x428,
Magnus Damm85645572008-12-19 15:34:41 +090067 [LDSM2R] = 0x42c,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070068 [LDSA1R] = 0x430,
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +000069 [LDSA2R] = 0x434,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070070 [LDMLSR] = 0x438,
71 [LDHCNR] = 0x448,
72 [LDHSYNR] = 0x44c,
73 [LDVLNR] = 0x450,
74 [LDVSYNR] = 0x454,
75 [LDPMR] = 0x460,
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +000076 [LDHAJR] = 0x4a0,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070077};
78
Magnus Damm0246c472009-08-14 10:49:08 +000079static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070080 [LDDCKPAT1R] = 0x408,
81 [LDDCKPAT2R] = 0x40c,
82 [LDMT1R] = 0x600,
83 [LDMT2R] = 0x604,
84 [LDMT3R] = 0x608,
85 [LDDFR] = 0x60c,
86 [LDSM1R] = 0x610,
Magnus Damm85645572008-12-19 15:34:41 +090087 [LDSM2R] = 0x614,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070088 [LDSA1R] = 0x618,
89 [LDMLSR] = 0x620,
90 [LDHCNR] = 0x624,
91 [LDHSYNR] = 0x628,
92 [LDVLNR] = 0x62c,
93 [LDVSYNR] = 0x630,
94 [LDPMR] = 0x63c,
95};
96
Phil Edworthya6f15ad2009-09-15 12:00:30 +000097static bool banked(int reg_nr)
98{
99 switch (reg_nr) {
100 case LDMT1R:
101 case LDMT2R:
102 case LDMT3R:
103 case LDDFR:
104 case LDSM1R:
105 case LDSA1R:
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000106 case LDSA2R:
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000107 case LDMLSR:
108 case LDHCNR:
109 case LDHSYNR:
110 case LDVLNR:
111 case LDVSYNR:
112 return true;
113 }
114 return false;
115}
116
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200117static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
118{
119 return chan->cfg.chan == LCDC_CHAN_SUBLCD;
120}
121
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700122static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
123 int reg_nr, unsigned long data)
124{
125 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000126 if (banked(reg_nr))
127 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
128 SIDE_B_OFFSET);
129}
130
131static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
132 int reg_nr, unsigned long data)
133{
134 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
135 MIRROR_OFFSET);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700136}
137
138static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
139 int reg_nr)
140{
141 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
142}
143
144static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
145 unsigned long reg_offs, unsigned long data)
146{
147 iowrite32(data, priv->base + reg_offs);
148}
149
150static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
151 unsigned long reg_offs)
152{
153 return ioread32(priv->base + reg_offs);
154}
155
156static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
157 unsigned long reg_offs,
158 unsigned long mask, unsigned long until)
159{
160 while ((lcdc_read(priv, reg_offs) & mask) != until)
161 cpu_relax();
162}
163
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200164/* -----------------------------------------------------------------------------
165 * Clock management
166 */
167
168static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700169{
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200170 if (atomic_inc_and_test(&priv->hw_usecnt)) {
171 if (priv->dot_clk)
172 clk_enable(priv->dot_clk);
173 pm_runtime_get_sync(priv->dev);
174 if (priv->meram_dev && priv->meram_dev->pdev)
175 pm_runtime_get_sync(&priv->meram_dev->pdev->dev);
176 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700177}
178
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200179static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
180{
181 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
182 if (priv->meram_dev && priv->meram_dev->pdev)
183 pm_runtime_put_sync(&priv->meram_dev->pdev->dev);
184 pm_runtime_put(priv->dev);
185 if (priv->dot_clk)
186 clk_disable(priv->dot_clk);
187 }
188}
189
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200190static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv *priv,
191 int clock_source)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200192{
Laurent Pinchart4774c122011-09-07 15:47:07 +0200193 struct clk *clk;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200194 char *str;
195
196 switch (clock_source) {
197 case LCDC_CLK_BUS:
198 str = "bus_clk";
199 priv->lddckr = LDDCKR_ICKSEL_BUS;
200 break;
201 case LCDC_CLK_PERIPHERAL:
202 str = "peripheral_clk";
203 priv->lddckr = LDDCKR_ICKSEL_MIPI;
204 break;
205 case LCDC_CLK_EXTERNAL:
206 str = NULL;
207 priv->lddckr = LDDCKR_ICKSEL_HDMI;
208 break;
209 default:
210 return -EINVAL;
211 }
212
Laurent Pinchart4774c122011-09-07 15:47:07 +0200213 if (str == NULL)
214 return 0;
215
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200216 clk = clk_get(priv->dev, str);
Laurent Pinchart4774c122011-09-07 15:47:07 +0200217 if (IS_ERR(clk)) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200218 dev_err(priv->dev, "cannot get dot clock %s\n", str);
Laurent Pinchart4774c122011-09-07 15:47:07 +0200219 return PTR_ERR(clk);
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200220 }
221
Laurent Pinchart4774c122011-09-07 15:47:07 +0200222 priv->dot_clk = clk;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200223 return 0;
224}
225
226/* -----------------------------------------------------------------------------
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200227 * Display, panel and deferred I/O
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200228 */
229
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700230static void lcdc_sys_write_index(void *handle, unsigned long data)
231{
232 struct sh_mobile_lcdc_chan *ch = handle;
233
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200234 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
235 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
236 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
237 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
238 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700239}
240
241static void lcdc_sys_write_data(void *handle, unsigned long data)
242{
243 struct sh_mobile_lcdc_chan *ch = handle;
244
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200245 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
246 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
247 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
248 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
249 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700250}
251
252static unsigned long lcdc_sys_read_data(void *handle)
253{
254 struct sh_mobile_lcdc_chan *ch = handle;
255
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200256 lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
257 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
258 lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
259 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700260 udelay(1);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200261 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700262
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200263 return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700264}
265
266struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
267 lcdc_sys_write_index,
268 lcdc_sys_write_data,
269 lcdc_sys_read_data,
270};
271
Paul Mundt1c6a3072009-07-01 06:50:31 +0000272static int sh_mobile_lcdc_sginit(struct fb_info *info,
273 struct list_head *pagelist)
274{
275 struct sh_mobile_lcdc_chan *ch = info->par;
276 unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
277 struct page *page;
278 int nr_pages = 0;
279
280 sg_init_table(ch->sglist, nr_pages_max);
281
282 list_for_each_entry(page, pagelist, lru)
283 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
284
285 return nr_pages;
286}
287
Magnus Damm85645572008-12-19 15:34:41 +0900288static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
289 struct list_head *pagelist)
290{
291 struct sh_mobile_lcdc_chan *ch = info->par;
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 Pinchart37c5dcc2011-09-09 15:45:43 +0200338static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan *ch)
339{
340 struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
341
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200342 if (ch->tx_dev) {
343 if (ch->tx_dev->ops->display_on(ch->tx_dev, ch->info) < 0)
344 return;
345 }
346
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200347 /* HDMI must be enabled before LCDC configuration */
Laurent Pinchartaa7b5b02011-09-11 22:59:04 +0200348 if (board_cfg->display_on)
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200349 board_cfg->display_on(board_cfg->board_data, ch->info);
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200350}
351
352static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan *ch)
353{
354 struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
355
Laurent Pinchartaa7b5b02011-09-11 22:59:04 +0200356 if (board_cfg->display_off)
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200357 board_cfg->display_off(board_cfg->board_data);
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200358
359 if (ch->tx_dev)
360 ch->tx_dev->ops->display_off(ch->tx_dev);
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200361}
362
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200363/* -----------------------------------------------------------------------------
364 * Format helpers
365 */
366
367static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
368{
369 if (var->grayscale > 1)
370 return var->grayscale;
371
372 switch (var->bits_per_pixel) {
373 case 16:
374 return V4L2_PIX_FMT_RGB565;
375 case 24:
376 return V4L2_PIX_FMT_BGR24;
377 case 32:
378 return V4L2_PIX_FMT_BGR32;
379 default:
380 return 0;
381 }
382}
383
384static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
385{
386 return var->grayscale > 1;
387}
388
389static bool sh_mobile_format_is_yuv(const struct fb_var_screeninfo *var)
390{
391 if (var->grayscale <= 1)
392 return false;
393
394 switch (var->grayscale) {
395 case V4L2_PIX_FMT_NV12:
396 case V4L2_PIX_FMT_NV21:
397 case V4L2_PIX_FMT_NV16:
398 case V4L2_PIX_FMT_NV61:
399 case V4L2_PIX_FMT_NV24:
400 case V4L2_PIX_FMT_NV42:
401 return true;
402
403 default:
404 return false;
405 }
406}
407
408/* -----------------------------------------------------------------------------
409 * Start, stop and IRQ
410 */
411
Magnus Damm85645572008-12-19 15:34:41 +0900412static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
413{
414 struct sh_mobile_lcdc_priv *priv = data;
Magnus Damm2feb0752009-03-13 15:36:55 +0000415 struct sh_mobile_lcdc_chan *ch;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000416 unsigned long ldintr;
Magnus Damm2feb0752009-03-13 15:36:55 +0000417 int is_sub;
418 int k;
Magnus Damm85645572008-12-19 15:34:41 +0900419
Laurent Pinchartdc486652011-07-13 12:13:47 +0200420 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
421 ldintr = lcdc_read(priv, _LDINTR);
422 lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
Magnus Damm85645572008-12-19 15:34:41 +0900423
Magnus Damm2feb0752009-03-13 15:36:55 +0000424 /* figure out if this interrupt is for main or sub lcd */
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200425 is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
Magnus Damm2feb0752009-03-13 15:36:55 +0000426
Phil Edworthy9dd38812009-09-15 12:00:18 +0000427 /* wake up channel and disable clocks */
Magnus Damm2feb0752009-03-13 15:36:55 +0000428 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
429 ch = &priv->ch[k];
430
431 if (!ch->enabled)
432 continue;
433
Laurent Pinchartdc486652011-07-13 12:13:47 +0200434 /* Frame End */
Phil Edworthy9dd38812009-09-15 12:00:18 +0000435 if (ldintr & LDINTR_FS) {
436 if (is_sub == lcdc_chan_is_sublcd(ch)) {
437 ch->frame_end = 1;
438 wake_up(&ch->frame_end_wait);
Magnus Damm2feb0752009-03-13 15:36:55 +0000439
Phil Edworthy9dd38812009-09-15 12:00:18 +0000440 sh_mobile_lcdc_clk_off(priv);
441 }
442 }
443
444 /* VSYNC End */
Phil Edworthy40331b22010-02-15 13:57:49 +0000445 if (ldintr & LDINTR_VES)
446 complete(&ch->vsync_completion);
Magnus Damm2feb0752009-03-13 15:36:55 +0000447 }
448
Magnus Damm85645572008-12-19 15:34:41 +0900449 return IRQ_HANDLED;
450}
451
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700452static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
453 int start)
454{
455 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
456 int k;
457
458 /* start or stop the lcdc */
459 if (start)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200460 lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700461 else
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200462 lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700463
464 /* wait until power is applied/stopped on all channels */
465 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
466 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
467 while (1) {
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200468 tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
469 & LDPMR_LPS;
470 if (start && tmp == LDPMR_LPS)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700471 break;
472 if (!start && tmp == 0)
473 break;
474 cpu_relax();
475 }
476
477 if (!start)
478 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
479}
480
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000481static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
482{
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000483 struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
484 unsigned long h_total, hsync_pos, display_h_total;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000485 u32 tmp;
486
487 tmp = ch->ldmt1r_value;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200488 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
489 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
490 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
491 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
492 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
493 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
494 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000495 lcdc_write_chan(ch, LDMT1R, tmp);
496
497 /* setup SYS bus */
498 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
499 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
500
501 /* horizontal configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000502 h_total = display_var->xres + display_var->hsync_len +
503 display_var->left_margin + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000504 tmp = h_total / 8; /* HTCN */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000505 tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000506 lcdc_write_chan(ch, LDHCNR, tmp);
507
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000508 hsync_pos = display_var->xres + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000509 tmp = hsync_pos / 8; /* HSYNP */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000510 tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000511 lcdc_write_chan(ch, LDHSYNR, tmp);
512
513 /* vertical configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000514 tmp = display_var->yres + display_var->vsync_len +
515 display_var->upper_margin + display_var->lower_margin; /* VTLN */
516 tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000517 lcdc_write_chan(ch, LDVLNR, tmp);
518
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000519 tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
520 tmp |= display_var->vsync_len << 16; /* VSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000521 lcdc_write_chan(ch, LDVSYNR, tmp);
522
523 /* Adjust horizontal synchronisation for HDMI */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000524 display_h_total = display_var->xres + display_var->hsync_len +
525 display_var->left_margin + display_var->right_margin;
526 tmp = ((display_var->xres & 7) << 24) |
527 ((display_h_total & 7) << 16) |
528 ((display_var->hsync_len & 7) << 8) |
Kuninori Morimoto41e583c2011-11-08 20:33:29 -0800529 (hsync_pos & 7);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000530 lcdc_write_chan(ch, LDHAJR, tmp);
531}
532
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200533/*
534 * __sh_mobile_lcdc_start - Configure and tart the LCDC
535 * @priv: LCDC device
536 *
537 * Configure all enabled channels and start the LCDC device. All external
538 * devices (clocks, MERAM, panels, ...) are not touched by this function.
539 */
540static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700541{
542 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700543 unsigned long tmp;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200544 int k, m;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700545
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200546 /* Enable LCDC channels. Read data from external memory, avoid using the
547 * BEU for now.
548 */
549 lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
Magnus Damm85645572008-12-19 15:34:41 +0900550
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200551 /* Stop the LCDC first and disable all interrupts. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700552 sh_mobile_lcdc_start_stop(priv, 0);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200553 lcdc_write(priv, _LDINTR, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700554
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200555 /* Configure power supply, dot clocks and start them. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700556 tmp = priv->lddckr;
557 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
558 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200559 if (!ch->enabled)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700560 continue;
561
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200562 /* Power supply */
563 lcdc_write_chan(ch, LDPMR, 0);
564
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700565 m = ch->cfg.clock_divider;
566 if (!m)
567 continue;
568
Laurent Pinchart505c7de52011-07-13 12:13:47 +0200569 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
570 * denominator.
571 */
572 lcdc_write_chan(ch, LDDCKPAT1R, 0);
573 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
574
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700575 if (m == 1)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200576 m = LDDCKR_MOSEL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700577 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700578 }
579
580 lcdc_write(priv, _LDDCKR, tmp);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700581 lcdc_write(priv, _LDDCKSTPR, 0);
582 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
583
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200584 /* Setup geometry, format, frame buffer memory and operation mode. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700585 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
586 ch = &priv->ch[k];
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700587 if (!ch->enabled)
588 continue;
589
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000590 sh_mobile_lcdc_geometry(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700591
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100592 switch (sh_mobile_format_fourcc(&ch->info->var)) {
593 case V4L2_PIX_FMT_RGB565:
594 tmp = LDDFR_PKF_RGB16;
595 break;
596 case V4L2_PIX_FMT_BGR24:
597 tmp = LDDFR_PKF_RGB24;
598 break;
599 case V4L2_PIX_FMT_BGR32:
600 tmp = LDDFR_PKF_ARGB32;
601 break;
602 case V4L2_PIX_FMT_NV12:
603 case V4L2_PIX_FMT_NV21:
604 tmp = LDDFR_CC | LDDFR_YF_420;
605 break;
606 case V4L2_PIX_FMT_NV16:
607 case V4L2_PIX_FMT_NV61:
608 tmp = LDDFR_CC | LDDFR_YF_422;
609 break;
610 case V4L2_PIX_FMT_NV24:
611 case V4L2_PIX_FMT_NV42:
612 tmp = LDDFR_CC | LDDFR_YF_444;
613 break;
614 }
615
616 if (sh_mobile_format_is_yuv(&ch->info->var)) {
617 switch (ch->info->var.colorspace) {
618 case V4L2_COLORSPACE_REC709:
619 tmp |= LDDFR_CF1;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000620 break;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100621 case V4L2_COLORSPACE_JPEG:
622 tmp |= LDDFR_CF0;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000623 break;
624 }
Magnus Damm417d4822011-01-05 10:21:00 +0000625 }
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200626
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700627 lcdc_write_chan(ch, LDDFR, tmp);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200628 lcdc_write_chan(ch, LDMLSR, ch->pitch);
629 lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100630 if (sh_mobile_format_is_yuv(&ch->info->var))
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200631 lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700632
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200633 /* When using deferred I/O mode, configure the LCDC for one-shot
634 * operation and enable the frame end interrupt. Otherwise use
635 * continuous read mode.
636 */
637 if (ch->ldmt1r_value & LDMT1R_IFM &&
638 ch->cfg.sys_bus_cfg.deferred_io_msec) {
639 lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
640 lcdc_write(priv, _LDINTR, LDINTR_FE);
641 } else {
642 lcdc_write_chan(ch, LDSM1R, 0);
643 }
644 }
Damian7caa4342011-05-18 11:10:07 +0000645
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200646 /* Word and long word swap. */
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100647 switch (sh_mobile_format_fourcc(&priv->ch[0].info->var)) {
648 case V4L2_PIX_FMT_RGB565:
649 case V4L2_PIX_FMT_NV21:
650 case V4L2_PIX_FMT_NV61:
651 case V4L2_PIX_FMT_NV42:
652 tmp = LDDDSR_LS | LDDDSR_WS;
653 break;
654 case V4L2_PIX_FMT_BGR24:
655 case V4L2_PIX_FMT_NV12:
656 case V4L2_PIX_FMT_NV16:
657 case V4L2_PIX_FMT_NV24:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200658 tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100659 break;
660 case V4L2_PIX_FMT_BGR32:
661 default:
662 tmp = LDDDSR_LS;
663 break;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200664 }
665 lcdc_write(priv, _LDDDSR, tmp);
Damian7caa4342011-05-18 11:10:07 +0000666
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200667 /* Enable the display output. */
668 lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
669 sh_mobile_lcdc_start_stop(priv, 1);
670 priv->started = 1;
671}
Damian7caa4342011-05-18 11:10:07 +0000672
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200673static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
674{
675 struct sh_mobile_meram_info *mdev = priv->meram_dev;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200676 struct sh_mobile_lcdc_chan *ch;
677 unsigned long tmp;
678 int ret;
679 int k;
680
681 /* enable clocks before accessing the hardware */
682 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
683 if (priv->ch[k].enabled)
684 sh_mobile_lcdc_clk_on(priv);
685 }
686
687 /* reset */
688 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
689 lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
690
691 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200692 struct sh_mobile_lcdc_board_cfg *board_cfg;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200693
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200694 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200695 if (!ch->enabled)
696 continue;
697
698 board_cfg = &ch->cfg.board_cfg;
699 if (board_cfg->setup_sys) {
700 ret = board_cfg->setup_sys(board_cfg->board_data, ch,
701 &sh_mobile_lcdc_sys_bus_ops);
702 if (ret)
703 return ret;
704 }
705 }
706
707 /* Compute frame buffer base address and pitch for each channel. */
708 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
709 struct sh_mobile_meram_cfg *cfg;
710 int pixelformat;
711
712 ch = &priv->ch[k];
713 if (!ch->enabled)
714 continue;
715
716 ch->base_addr_y = ch->info->fix.smem_start;
717 ch->base_addr_c = ch->base_addr_y
718 + ch->info->var.xres
719 * ch->info->var.yres_virtual;
720 ch->pitch = ch->info->fix.line_length;
721
722 /* Enable MERAM if possible. */
723 cfg = ch->cfg.meram_cfg;
724 if (mdev == NULL || mdev->ops == NULL || cfg == NULL)
725 continue;
726
727 /* we need to de-init configured ICBs before we can
728 * re-initialize them.
729 */
730 if (ch->meram_enabled) {
731 mdev->ops->meram_unregister(mdev, cfg);
Damian7caa4342011-05-18 11:10:07 +0000732 ch->meram_enabled = 0;
Damian7caa4342011-05-18 11:10:07 +0000733 }
734
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100735 switch (sh_mobile_format_fourcc(&ch->info->var)) {
736 case V4L2_PIX_FMT_NV12:
737 case V4L2_PIX_FMT_NV21:
738 case V4L2_PIX_FMT_NV16:
739 case V4L2_PIX_FMT_NV61:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200740 pixelformat = SH_MOBILE_MERAM_PF_NV;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100741 break;
742 case V4L2_PIX_FMT_NV24:
743 case V4L2_PIX_FMT_NV42:
744 pixelformat = SH_MOBILE_MERAM_PF_NV24;
745 break;
746 case V4L2_PIX_FMT_RGB565:
747 case V4L2_PIX_FMT_BGR24:
748 case V4L2_PIX_FMT_BGR32:
749 default:
750 pixelformat = SH_MOBILE_MERAM_PF_RGB;
751 break;
752 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700753
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200754 ret = mdev->ops->meram_register(mdev, cfg, ch->pitch,
755 ch->info->var.yres, pixelformat,
756 ch->base_addr_y, ch->base_addr_c,
757 &ch->base_addr_y, &ch->base_addr_c,
758 &ch->pitch);
759 if (!ret)
760 ch->meram_enabled = 1;
761 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700762
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200763 /* Start the LCDC. */
764 __sh_mobile_lcdc_start(priv);
765
766 /* Setup deferred I/O, tell the board code to enable the panels, and
767 * turn backlight on.
768 */
769 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
770 ch = &priv->ch[k];
771 if (!ch->enabled)
772 continue;
773
Magnus Damm85645572008-12-19 15:34:41 +0900774 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200775 if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
Magnus Damm85645572008-12-19 15:34:41 +0900776 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
777 ch->defio.delay = msecs_to_jiffies(tmp);
Paul Mundte33afdd2009-07-07 11:24:32 +0900778 ch->info->fbdefio = &ch->defio;
779 fb_deferred_io_init(ch->info);
Magnus Damm85645572008-12-19 15:34:41 +0900780 }
Magnus Damm21bc1f02009-08-15 02:53:16 +0000781
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200782 sh_mobile_lcdc_display_on(ch);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000783
784 if (ch->bl) {
785 ch->bl->props.power = FB_BLANK_UNBLANK;
786 backlight_update_status(ch->bl);
787 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700788 }
789
790 return 0;
791}
792
793static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
794{
795 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700796 int k;
797
Magnus Damm2feb0752009-03-13 15:36:55 +0000798 /* clean up deferred io and ask board code to disable panel */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700799 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
800 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +0000801 if (!ch->enabled)
802 continue;
Magnus Damm2feb0752009-03-13 15:36:55 +0000803
804 /* deferred io mode:
805 * flush frame, and wait for frame end interrupt
806 * clean up deferred io and enable clock
807 */
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +0000808 if (ch->info && ch->info->fbdefio) {
Magnus Damm2feb0752009-03-13 15:36:55 +0000809 ch->frame_end = 0;
Paul Mundte33afdd2009-07-07 11:24:32 +0900810 schedule_delayed_work(&ch->info->deferred_work, 0);
Magnus Damm2feb0752009-03-13 15:36:55 +0000811 wait_event(ch->frame_end_wait, ch->frame_end);
Paul Mundte33afdd2009-07-07 11:24:32 +0900812 fb_deferred_io_cleanup(ch->info);
813 ch->info->fbdefio = NULL;
Magnus Damm2feb0752009-03-13 15:36:55 +0000814 sh_mobile_lcdc_clk_on(priv);
815 }
816
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000817 if (ch->bl) {
818 ch->bl->props.power = FB_BLANK_POWERDOWN;
819 backlight_update_status(ch->bl);
820 }
821
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200822 sh_mobile_lcdc_display_off(ch);
Damian7caa4342011-05-18 11:10:07 +0000823
824 /* disable the meram */
825 if (ch->meram_enabled) {
826 struct sh_mobile_meram_cfg *cfg;
827 struct sh_mobile_meram_info *mdev;
828 cfg = ch->cfg.meram_cfg;
829 mdev = priv->meram_dev;
830 mdev->ops->meram_unregister(mdev, cfg);
831 ch->meram_enabled = 0;
832 }
833
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700834 }
835
836 /* stop the lcdc */
Magnus Damm8e9bb192009-05-20 14:34:43 +0000837 if (priv->started) {
838 sh_mobile_lcdc_start_stop(priv, 0);
839 priv->started = 0;
840 }
Magnus Dammb51339f2008-10-31 20:23:26 +0900841
Magnus Damm85645572008-12-19 15:34:41 +0900842 /* stop clocks */
843 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
844 if (priv->ch[k].enabled)
845 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700846}
847
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200848/* -----------------------------------------------------------------------------
849 * Frame buffer operations
850 */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700851
852static int sh_mobile_lcdc_setcolreg(u_int regno,
853 u_int red, u_int green, u_int blue,
854 u_int transp, struct fb_info *info)
855{
856 u32 *palette = info->pseudo_palette;
857
858 if (regno >= PALETTE_NR)
859 return -EINVAL;
860
861 /* only FB_VISUAL_TRUECOLOR supported */
862
863 red >>= 16 - info->var.red.length;
864 green >>= 16 - info->var.green.length;
865 blue >>= 16 - info->var.blue.length;
866 transp >>= 16 - info->var.transp.length;
867
868 palette[regno] = (red << info->var.red.offset) |
869 (green << info->var.green.offset) |
870 (blue << info->var.blue.offset) |
871 (transp << info->var.transp.offset);
872
873 return 0;
874}
875
876static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
877 .id = "SH Mobile LCDC",
878 .type = FB_TYPE_PACKED_PIXELS,
879 .visual = FB_VISUAL_TRUECOLOR,
880 .accel = FB_ACCEL_NONE,
Phil Edworthy9dd38812009-09-15 12:00:18 +0000881 .xpanstep = 0,
882 .ypanstep = 1,
883 .ywrapstep = 0,
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100884 .capabilities = FB_CAP_FOURCC,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700885};
886
Magnus Damm85645572008-12-19 15:34:41 +0900887static void sh_mobile_lcdc_fillrect(struct fb_info *info,
888 const struct fb_fillrect *rect)
889{
890 sys_fillrect(info, rect);
891 sh_mobile_lcdc_deferred_io_touch(info);
892}
893
894static void sh_mobile_lcdc_copyarea(struct fb_info *info,
895 const struct fb_copyarea *area)
896{
897 sys_copyarea(info, area);
898 sh_mobile_lcdc_deferred_io_touch(info);
899}
900
901static void sh_mobile_lcdc_imageblit(struct fb_info *info,
902 const struct fb_image *image)
903{
904 sys_imageblit(info, image);
905 sh_mobile_lcdc_deferred_io_touch(info);
906}
907
Phil Edworthy9dd38812009-09-15 12:00:18 +0000908static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
909 struct fb_info *info)
910{
911 struct sh_mobile_lcdc_chan *ch = info->par;
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000912 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
913 unsigned long ldrcntr;
914 unsigned long new_pan_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000915 unsigned long base_addr_y, base_addr_c;
916 unsigned long c_offset;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100917 bool yuv = sh_mobile_format_is_yuv(&info->var);
Phil Edworthy9dd38812009-09-15 12:00:18 +0000918
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100919 if (!yuv)
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +0200920 new_pan_offset = var->yoffset * info->fix.line_length
921 + var->xoffset * (info->var.bits_per_pixel / 8);
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000922 else
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +0200923 new_pan_offset = var->yoffset * info->fix.line_length
924 + var->xoffset;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000925
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000926 if (new_pan_offset == ch->pan_offset)
927 return 0; /* No change, do nothing */
928
929 ldrcntr = lcdc_read(priv, _LDRCNTR);
930
931 /* Set the source address for the next refresh */
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000932 base_addr_y = ch->dma_handle + new_pan_offset;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100933 if (yuv) {
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000934 /* Set y offset */
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +0200935 c_offset = var->yoffset * info->fix.line_length
936 * (info->var.bits_per_pixel - 8) / 8;
937 base_addr_c = ch->dma_handle
938 + info->var.xres * info->var.yres_virtual
939 + c_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000940 /* Set x offset */
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100941 if (sh_mobile_format_fourcc(&info->var) == V4L2_PIX_FMT_NV24)
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000942 base_addr_c += 2 * var->xoffset;
943 else
944 base_addr_c += var->xoffset;
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200945 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000946
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200947 if (ch->meram_enabled) {
Damian7caa4342011-05-18 11:10:07 +0000948 struct sh_mobile_meram_cfg *cfg;
949 struct sh_mobile_meram_info *mdev;
Damian7caa4342011-05-18 11:10:07 +0000950 int ret;
951
952 cfg = ch->cfg.meram_cfg;
953 mdev = priv->meram_dev;
954 ret = mdev->ops->meram_update(mdev, cfg,
955 base_addr_y, base_addr_c,
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200956 &base_addr_y, &base_addr_c);
Damian7caa4342011-05-18 11:10:07 +0000957 if (ret)
958 return ret;
Damian7caa4342011-05-18 11:10:07 +0000959 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000960
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200961 ch->base_addr_y = base_addr_y;
962 ch->base_addr_c = base_addr_c;
963
964 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100965 if (yuv)
Laurent Pinchart49d79ba2011-07-13 12:13:47 +0200966 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
967
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000968 if (lcdc_chan_is_sublcd(ch))
969 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
970 else
971 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
972
973 ch->pan_offset = new_pan_offset;
974
975 sh_mobile_lcdc_deferred_io_touch(info);
Phil Edworthy9dd38812009-09-15 12:00:18 +0000976
977 return 0;
978}
979
Phil Edworthy40331b22010-02-15 13:57:49 +0000980static int sh_mobile_wait_for_vsync(struct fb_info *info)
981{
982 struct sh_mobile_lcdc_chan *ch = info->par;
983 unsigned long ldintr;
984 int ret;
985
Laurent Pinchartdc486652011-07-13 12:13:47 +0200986 /* Enable VSync End interrupt and be careful not to acknowledge any
987 * pending interrupt.
988 */
Phil Edworthy40331b22010-02-15 13:57:49 +0000989 ldintr = lcdc_read(ch->lcdc, _LDINTR);
Laurent Pinchartdc486652011-07-13 12:13:47 +0200990 ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
Phil Edworthy40331b22010-02-15 13:57:49 +0000991 lcdc_write(ch->lcdc, _LDINTR, ldintr);
992
993 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
994 msecs_to_jiffies(100));
995 if (!ret)
996 return -ETIMEDOUT;
997
998 return 0;
999}
1000
1001static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
1002 unsigned long arg)
1003{
1004 int retval;
1005
1006 switch (cmd) {
1007 case FBIO_WAITFORVSYNC:
1008 retval = sh_mobile_wait_for_vsync(info);
1009 break;
1010
1011 default:
1012 retval = -ENOIOCTLCMD;
1013 break;
1014 }
1015 return retval;
1016}
1017
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001018static void sh_mobile_fb_reconfig(struct fb_info *info)
1019{
1020 struct sh_mobile_lcdc_chan *ch = info->par;
1021 struct fb_videomode mode1, mode2;
1022 struct fb_event event;
1023 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1024
1025 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1026 /* More framebuffer users are active */
1027 return;
1028
1029 fb_var_to_videomode(&mode1, &ch->display_var);
1030 fb_var_to_videomode(&mode2, &info->var);
1031
1032 if (fb_mode_is_equal(&mode1, &mode2))
1033 return;
1034
1035 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1036 if (fb_set_var(info, &ch->display_var) < 0)
1037 /* Couldn't reconfigure, hopefully, can continue as before */
1038 return;
1039
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001040 /*
1041 * fb_set_var() calls the notifier change internally, only if
1042 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1043 * user event, we have to call the chain ourselves.
1044 */
1045 event.info = info;
Arnd Hannemanncc267ec2010-11-15 21:43:22 +00001046 event.data = &mode1;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001047 fb_notifier_call_chain(evnt, &event);
1048}
1049
1050/*
1051 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1052 * user == 1, or with console sem held, if user == 0.
1053 */
1054static int sh_mobile_release(struct fb_info *info, int user)
1055{
1056 struct sh_mobile_lcdc_chan *ch = info->par;
1057
1058 mutex_lock(&ch->open_lock);
1059 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1060
1061 ch->use_count--;
1062
1063 /* Nothing to reconfigure, when called from fbcon */
1064 if (user) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001065 console_lock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001066 sh_mobile_fb_reconfig(info);
Torben Hohnac751ef2011-01-25 15:07:35 -08001067 console_unlock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001068 }
1069
1070 mutex_unlock(&ch->open_lock);
1071
1072 return 0;
1073}
1074
1075static int sh_mobile_open(struct fb_info *info, int user)
1076{
1077 struct sh_mobile_lcdc_chan *ch = info->par;
1078
1079 mutex_lock(&ch->open_lock);
1080 ch->use_count++;
1081
1082 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1083 mutex_unlock(&ch->open_lock);
1084
1085 return 0;
1086}
1087
1088static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1089{
1090 struct sh_mobile_lcdc_chan *ch = info->par;
Magnus Damm417d4822011-01-05 10:21:00 +00001091 struct sh_mobile_lcdc_priv *p = ch->lcdc;
Laurent Pinchart03862192011-08-31 13:00:53 +02001092 unsigned int best_dist = (unsigned int)-1;
1093 unsigned int best_xres = 0;
1094 unsigned int best_yres = 0;
1095 unsigned int i;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001096
Laurent Pinchart03862192011-08-31 13:00:53 +02001097 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001098 return -EINVAL;
Laurent Pinchart03862192011-08-31 13:00:53 +02001099
1100 /* If board code provides us with a list of available modes, make sure
1101 * we use one of them. Find the mode closest to the requested one. The
1102 * distance between two modes is defined as the size of the
1103 * non-overlapping parts of the two rectangles.
1104 */
1105 for (i = 0; i < ch->cfg.num_cfg; ++i) {
1106 const struct fb_videomode *mode = &ch->cfg.lcd_cfg[i];
1107 unsigned int dist;
1108
1109 /* We can only round up. */
1110 if (var->xres > mode->xres || var->yres > mode->yres)
1111 continue;
1112
1113 dist = var->xres * var->yres + mode->xres * mode->yres
1114 - 2 * min(var->xres, mode->xres)
1115 * min(var->yres, mode->yres);
1116
1117 if (dist < best_dist) {
1118 best_xres = mode->xres;
1119 best_yres = mode->yres;
1120 best_dist = dist;
1121 }
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001122 }
Magnus Damm417d4822011-01-05 10:21:00 +00001123
Laurent Pinchart03862192011-08-31 13:00:53 +02001124 /* If no available mode can be used, return an error. */
1125 if (ch->cfg.num_cfg != 0) {
1126 if (best_dist == (unsigned int)-1)
1127 return -EINVAL;
1128
1129 var->xres = best_xres;
1130 var->yres = best_yres;
1131 }
1132
1133 /* Make sure the virtual resolution is at least as big as the visible
1134 * resolution.
1135 */
1136 if (var->xres_virtual < var->xres)
1137 var->xres_virtual = var->xres;
1138 if (var->yres_virtual < var->yres)
1139 var->yres_virtual = var->yres;
1140
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001141 if (sh_mobile_format_is_fourcc(var)) {
1142 switch (var->grayscale) {
1143 case V4L2_PIX_FMT_NV12:
1144 case V4L2_PIX_FMT_NV21:
1145 var->bits_per_pixel = 12;
1146 break;
1147 case V4L2_PIX_FMT_RGB565:
1148 case V4L2_PIX_FMT_NV16:
1149 case V4L2_PIX_FMT_NV61:
1150 var->bits_per_pixel = 16;
1151 break;
1152 case V4L2_PIX_FMT_BGR24:
1153 case V4L2_PIX_FMT_NV24:
1154 case V4L2_PIX_FMT_NV42:
1155 var->bits_per_pixel = 24;
1156 break;
1157 case V4L2_PIX_FMT_BGR32:
1158 var->bits_per_pixel = 32;
1159 break;
1160 default:
1161 return -EINVAL;
1162 }
Laurent Pinchart03862192011-08-31 13:00:53 +02001163
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001164 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1165 * respectively.
1166 */
1167 if (!sh_mobile_format_is_yuv(var))
1168 var->colorspace = V4L2_COLORSPACE_SRGB;
1169 else if (var->colorspace != V4L2_COLORSPACE_REC709)
1170 var->colorspace = V4L2_COLORSPACE_JPEG;
1171 } else {
1172 if (var->bits_per_pixel <= 16) { /* RGB 565 */
1173 var->bits_per_pixel = 16;
1174 var->red.offset = 11;
1175 var->red.length = 5;
1176 var->green.offset = 5;
1177 var->green.length = 6;
1178 var->blue.offset = 0;
1179 var->blue.length = 5;
1180 var->transp.offset = 0;
1181 var->transp.length = 0;
1182 } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
1183 var->bits_per_pixel = 24;
1184 var->red.offset = 16;
1185 var->red.length = 8;
1186 var->green.offset = 8;
1187 var->green.length = 8;
1188 var->blue.offset = 0;
1189 var->blue.length = 8;
1190 var->transp.offset = 0;
1191 var->transp.length = 0;
1192 } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
1193 var->bits_per_pixel = 32;
1194 var->red.offset = 16;
1195 var->red.length = 8;
1196 var->green.offset = 8;
1197 var->green.length = 8;
1198 var->blue.offset = 0;
1199 var->blue.length = 8;
1200 var->transp.offset = 24;
1201 var->transp.length = 8;
1202 } else
1203 return -EINVAL;
1204
1205 var->red.msb_right = 0;
1206 var->green.msb_right = 0;
1207 var->blue.msb_right = 0;
1208 var->transp.msb_right = 0;
1209 }
Laurent Pinchart03862192011-08-31 13:00:53 +02001210
1211 /* Make sure we don't exceed our allocated memory. */
1212 if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
1213 info->fix.smem_len)
1214 return -EINVAL;
1215
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001216 /* only accept the forced_fourcc for dual channel configurations */
1217 if (p->forced_fourcc &&
1218 p->forced_fourcc != sh_mobile_format_fourcc(var))
Magnus Damm417d4822011-01-05 10:21:00 +00001219 return -EINVAL;
1220
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001221 return 0;
1222}
Phil Edworthy40331b22010-02-15 13:57:49 +00001223
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001224static int sh_mobile_set_par(struct fb_info *info)
1225{
1226 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchart91fba482011-08-31 13:00:56 +02001227 u32 line_length = info->fix.line_length;
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001228 int ret;
1229
1230 sh_mobile_lcdc_stop(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001231
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001232 if (sh_mobile_format_is_yuv(&info->var))
Laurent Pinchart91fba482011-08-31 13:00:56 +02001233 info->fix.line_length = info->var.xres;
1234 else
1235 info->fix.line_length = info->var.xres
1236 * info->var.bits_per_pixel / 8;
1237
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001238 ret = sh_mobile_lcdc_start(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001239 if (ret < 0) {
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001240 dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001241 info->fix.line_length = line_length;
1242 }
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001243
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001244 if (sh_mobile_format_is_fourcc(&info->var)) {
1245 info->fix.type = FB_TYPE_FOURCC;
1246 info->fix.visual = FB_VISUAL_FOURCC;
1247 } else {
1248 info->fix.type = FB_TYPE_PACKED_PIXELS;
1249 info->fix.visual = FB_VISUAL_TRUECOLOR;
1250 }
1251
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001252 return ret;
1253}
1254
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001255/*
1256 * Screen blanking. Behavior is as follows:
1257 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1258 * FB_BLANK_NORMAL: screen blanked, clocks enabled
1259 * FB_BLANK_VSYNC,
1260 * FB_BLANK_HSYNC,
1261 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1262 */
1263static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
1264{
1265 struct sh_mobile_lcdc_chan *ch = info->par;
1266 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1267
1268 /* blank the screen? */
1269 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
1270 struct fb_fillrect rect = {
1271 .width = info->var.xres,
1272 .height = info->var.yres,
1273 };
1274 sh_mobile_lcdc_fillrect(info, &rect);
1275 }
1276 /* turn clocks on? */
1277 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
1278 sh_mobile_lcdc_clk_on(p);
1279 }
1280 /* turn clocks off? */
1281 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
1282 /* make sure the screen is updated with the black fill before
1283 * switching the clocks off. one vsync is not enough since
1284 * blanking may occur in the middle of a refresh. deferred io
1285 * mode will reenable the clocks and update the screen in time,
1286 * so it does not need this. */
1287 if (!info->fbdefio) {
1288 sh_mobile_wait_for_vsync(info);
1289 sh_mobile_wait_for_vsync(info);
1290 }
1291 sh_mobile_lcdc_clk_off(p);
1292 }
1293
1294 ch->blank_status = blank;
1295 return 0;
1296}
1297
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001298static struct fb_ops sh_mobile_lcdc_ops = {
Phil Edworthy9dd38812009-09-15 12:00:18 +00001299 .owner = THIS_MODULE,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001300 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
Magnus Damm2540c112008-12-17 17:29:49 +09001301 .fb_read = fb_sys_read,
1302 .fb_write = fb_sys_write,
Magnus Damm85645572008-12-19 15:34:41 +09001303 .fb_fillrect = sh_mobile_lcdc_fillrect,
1304 .fb_copyarea = sh_mobile_lcdc_copyarea,
1305 .fb_imageblit = sh_mobile_lcdc_imageblit,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001306 .fb_blank = sh_mobile_lcdc_blank,
Phil Edworthy9dd38812009-09-15 12:00:18 +00001307 .fb_pan_display = sh_mobile_fb_pan_display,
Phil Edworthy40331b22010-02-15 13:57:49 +00001308 .fb_ioctl = sh_mobile_ioctl,
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001309 .fb_open = sh_mobile_open,
1310 .fb_release = sh_mobile_release,
1311 .fb_check_var = sh_mobile_check_var,
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001312 .fb_set_par = sh_mobile_set_par,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001313};
1314
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001315/* -----------------------------------------------------------------------------
1316 * Backlight
1317 */
1318
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001319static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
1320{
1321 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1322 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1323 int brightness = bdev->props.brightness;
1324
1325 if (bdev->props.power != FB_BLANK_UNBLANK ||
1326 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
1327 brightness = 0;
1328
1329 return cfg->set_brightness(cfg->board_data, brightness);
1330}
1331
1332static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
1333{
1334 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1335 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1336
1337 return cfg->get_brightness(cfg->board_data);
1338}
1339
1340static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
1341 struct fb_info *info)
1342{
1343 return (info->bl_dev == bdev);
1344}
1345
1346static struct backlight_ops sh_mobile_lcdc_bl_ops = {
1347 .options = BL_CORE_SUSPENDRESUME,
1348 .update_status = sh_mobile_lcdc_update_bl,
1349 .get_brightness = sh_mobile_lcdc_get_brightness,
1350 .check_fb = sh_mobile_lcdc_check_fb,
1351};
1352
1353static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
1354 struct sh_mobile_lcdc_chan *ch)
1355{
1356 struct backlight_device *bl;
1357
1358 bl = backlight_device_register(ch->cfg.bl_info.name, parent, ch,
1359 &sh_mobile_lcdc_bl_ops, NULL);
Dan Carpenterbeee1f22011-03-21 15:03:13 +00001360 if (IS_ERR(bl)) {
1361 dev_err(parent, "unable to register backlight device: %ld\n",
1362 PTR_ERR(bl));
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001363 return NULL;
1364 }
1365
1366 bl->props.max_brightness = ch->cfg.bl_info.max_brightness;
1367 bl->props.brightness = bl->props.max_brightness;
1368 backlight_update_status(bl);
1369
1370 return bl;
1371}
1372
1373static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
1374{
1375 backlight_device_unregister(bdev);
1376}
1377
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001378/* -----------------------------------------------------------------------------
1379 * Power management
1380 */
1381
Magnus Damm2feb0752009-03-13 15:36:55 +00001382static int sh_mobile_lcdc_suspend(struct device *dev)
1383{
1384 struct platform_device *pdev = to_platform_device(dev);
1385
1386 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
1387 return 0;
1388}
1389
1390static int sh_mobile_lcdc_resume(struct device *dev)
1391{
1392 struct platform_device *pdev = to_platform_device(dev);
1393
1394 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
1395}
1396
Magnus Damm0246c472009-08-14 10:49:08 +00001397static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
1398{
1399 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001400 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001401
1402 /* turn off LCDC hardware */
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001403 lcdc_write(priv, _LDCNT1R, 0);
1404
Magnus Damm0246c472009-08-14 10:49:08 +00001405 return 0;
1406}
1407
1408static int sh_mobile_lcdc_runtime_resume(struct device *dev)
1409{
1410 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001411 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001412
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001413 __sh_mobile_lcdc_start(priv);
Magnus Damm0246c472009-08-14 10:49:08 +00001414
1415 return 0;
1416}
1417
Alexey Dobriyan47145212009-12-14 18:00:08 -08001418static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
Magnus Damm2feb0752009-03-13 15:36:55 +00001419 .suspend = sh_mobile_lcdc_suspend,
1420 .resume = sh_mobile_lcdc_resume,
Magnus Damm0246c472009-08-14 10:49:08 +00001421 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
1422 .runtime_resume = sh_mobile_lcdc_runtime_resume,
Magnus Damm2feb0752009-03-13 15:36:55 +00001423};
1424
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001425/* -----------------------------------------------------------------------------
1426 * Framebuffer notifier
1427 */
1428
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001429/* locking: called with info->lock held */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001430static int sh_mobile_lcdc_notify(struct notifier_block *nb,
1431 unsigned long action, void *data)
1432{
1433 struct fb_event *event = data;
1434 struct fb_info *info = event->info;
1435 struct sh_mobile_lcdc_chan *ch = info->par;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001436
1437 if (&ch->lcdc->notifier != nb)
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001438 return NOTIFY_DONE;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001439
1440 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
1441 __func__, action, event->data);
1442
1443 switch(action) {
1444 case FB_EVENT_SUSPEND:
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001445 sh_mobile_lcdc_display_off(ch);
Guennadi Liakhovetskiafe417c2010-09-03 07:20:39 +00001446 sh_mobile_lcdc_stop(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001447 break;
1448 case FB_EVENT_RESUME:
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001449 mutex_lock(&ch->open_lock);
1450 sh_mobile_fb_reconfig(info);
1451 mutex_unlock(&ch->open_lock);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001452
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001453 sh_mobile_lcdc_display_on(ch);
Guennadi Liakhovetskiebe5e122011-05-05 16:33:40 +00001454 sh_mobile_lcdc_start(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001455 }
1456
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001457 return NOTIFY_OK;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001458}
1459
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001460/* -----------------------------------------------------------------------------
1461 * Probe/remove and driver init/exit
1462 */
1463
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001464static const struct fb_videomode default_720p __devinitconst = {
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001465 .name = "HDMI 720p",
1466 .xres = 1280,
1467 .yres = 720,
1468
1469 .left_margin = 220,
1470 .right_margin = 110,
1471 .hsync_len = 40,
1472
1473 .upper_margin = 20,
1474 .lower_margin = 5,
1475 .vsync_len = 5,
1476
1477 .pixclock = 13468,
1478 .refresh = 60,
1479 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
1480};
1481
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001482static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1483{
1484 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1485 struct fb_info *info;
1486 int i;
1487
1488 fb_unregister_client(&priv->notifier);
1489
1490 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
1491 if (priv->ch[i].info && priv->ch[i].info->dev)
1492 unregister_framebuffer(priv->ch[i].info);
1493
1494 sh_mobile_lcdc_stop(priv);
1495
1496 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001497 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001498
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001499 info = ch->info;
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001500 if (!info || !info->device)
1501 continue;
1502
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001503 if (ch->tx_dev)
1504 module_put(ch->cfg.tx_dev->dev.driver->owner);
1505
1506 if (ch->sglist)
1507 vfree(ch->sglist);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001508
1509 if (info->screen_base)
1510 dma_free_coherent(&pdev->dev, info->fix.smem_len,
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001511 info->screen_base, ch->dma_handle);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001512 fb_dealloc_cmap(&info->cmap);
1513 framebuffer_release(info);
1514 }
1515
1516 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1517 if (priv->ch[i].bl)
1518 sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
1519 }
1520
Laurent Pinchart4774c122011-09-07 15:47:07 +02001521 if (priv->dot_clk) {
1522 pm_runtime_disable(&pdev->dev);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001523 clk_put(priv->dot_clk);
Laurent Pinchart4774c122011-09-07 15:47:07 +02001524 }
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001525
1526 if (priv->base)
1527 iounmap(priv->base);
1528
1529 if (priv->irq)
1530 free_irq(priv->irq, priv);
1531 kfree(priv);
1532 return 0;
1533}
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001534
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001535static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001536{
1537 int interface_type = ch->cfg.interface_type;
1538
1539 switch (interface_type) {
1540 case RGB8:
1541 case RGB9:
1542 case RGB12A:
1543 case RGB12B:
1544 case RGB16:
1545 case RGB18:
1546 case RGB24:
1547 case SYS8A:
1548 case SYS8B:
1549 case SYS8C:
1550 case SYS8D:
1551 case SYS9:
1552 case SYS12:
1553 case SYS16A:
1554 case SYS16B:
1555 case SYS16C:
1556 case SYS18:
1557 case SYS24:
1558 break;
1559 default:
1560 return -EINVAL;
1561 }
1562
1563 /* SUBLCD only supports SYS interface */
1564 if (lcdc_chan_is_sublcd(ch)) {
1565 if (!(interface_type & LDMT1R_IFM))
1566 return -EINVAL;
1567
1568 interface_type &= ~LDMT1R_IFM;
1569 }
1570
1571 ch->ldmt1r_value = interface_type;
1572 return 0;
1573}
1574
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001575static int __devinit
1576sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv *priv,
1577 struct sh_mobile_lcdc_chan *ch)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001578{
1579 struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
1580 const struct fb_videomode *max_mode;
1581 const struct fb_videomode *mode;
1582 struct fb_var_screeninfo *var;
1583 struct fb_info *info;
1584 unsigned int max_size;
1585 int num_cfg;
1586 void *buf;
1587 int ret;
1588 int i;
1589
Laurent Pincharta67472a2011-08-31 13:00:59 +02001590 mutex_init(&ch->open_lock);
1591
1592 /* Allocate the frame buffer device. */
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001593 ch->info = framebuffer_alloc(0, priv->dev);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001594 if (!ch->info) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001595 dev_err(priv->dev, "unable to allocate fb_info\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001596 return -ENOMEM;
1597 }
1598
1599 info = ch->info;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001600 info->fbops = &sh_mobile_lcdc_ops;
1601 info->par = ch;
Laurent Pincharta67472a2011-08-31 13:00:59 +02001602 info->pseudo_palette = &ch->pseudo_palette;
1603 info->flags = FBINFO_FLAG_DEFAULT;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001604
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001605 if (cfg->tx_dev) {
1606 if (!cfg->tx_dev->dev.driver ||
1607 !try_module_get(cfg->tx_dev->dev.driver->owner)) {
1608 dev_warn(priv->dev,
1609 "unable to get transmitter device\n");
1610 return -EINVAL;
1611 }
1612 ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
1613 }
1614
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001615 /* Iterate through the modes to validate them and find the highest
1616 * resolution.
1617 */
1618 max_mode = NULL;
1619 max_size = 0;
1620
1621 for (i = 0, mode = cfg->lcd_cfg; i < cfg->num_cfg; i++, mode++) {
1622 unsigned int size = mode->yres * mode->xres;
1623
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001624 /* NV12/NV21 buffers must have even number of lines */
1625 if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
1626 cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001627 dev_err(priv->dev, "yres must be multiple of 2 for "
1628 "YCbCr420 mode.\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001629 return -EINVAL;
1630 }
1631
1632 if (size > max_size) {
1633 max_mode = mode;
1634 max_size = size;
1635 }
1636 }
1637
1638 if (!max_size)
1639 max_size = MAX_XRES * MAX_YRES;
1640 else
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001641 dev_dbg(priv->dev, "Found largest videomode %ux%u\n",
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001642 max_mode->xres, max_mode->yres);
1643
Laurent Pincharta67472a2011-08-31 13:00:59 +02001644 /* Create the mode list. */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001645 if (cfg->lcd_cfg == NULL) {
1646 mode = &default_720p;
1647 num_cfg = 1;
1648 } else {
1649 mode = cfg->lcd_cfg;
1650 num_cfg = cfg->num_cfg;
1651 }
1652
1653 fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
1654
Laurent Pincharta67472a2011-08-31 13:00:59 +02001655 /* Initialize variable screen information using the first mode as
1656 * default. The default Y virtual resolution is twice the panel size to
1657 * allow for double-buffering.
1658 */
1659 var = &info->var;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001660 fb_videomode_to_var(var, mode);
1661 var->width = cfg->lcd_size_cfg.width;
1662 var->height = cfg->lcd_size_cfg.height;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001663 var->yres_virtual = var->yres * 2;
1664 var->activate = FB_ACTIVATE_NOW;
1665
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001666 switch (cfg->fourcc) {
1667 case V4L2_PIX_FMT_RGB565:
1668 var->bits_per_pixel = 16;
1669 break;
1670 case V4L2_PIX_FMT_BGR24:
1671 var->bits_per_pixel = 24;
1672 break;
1673 case V4L2_PIX_FMT_BGR32:
1674 var->bits_per_pixel = 32;
1675 break;
1676 default:
1677 var->grayscale = cfg->fourcc;
1678 break;
1679 }
1680
1681 /* Make sure the memory size check won't fail. smem_len is initialized
1682 * later based on var.
1683 */
1684 info->fix.smem_len = UINT_MAX;
Laurent Pincharta67472a2011-08-31 13:00:59 +02001685 ret = sh_mobile_check_var(var, info);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001686 if (ret)
1687 return ret;
1688
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001689 max_size = max_size * var->bits_per_pixel / 8 * 2;
1690
Laurent Pincharta67472a2011-08-31 13:00:59 +02001691 /* Allocate frame buffer memory and color map. */
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001692 buf = dma_alloc_coherent(priv->dev, max_size, &ch->dma_handle,
1693 GFP_KERNEL);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001694 if (!buf) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001695 dev_err(priv->dev, "unable to allocate buffer\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001696 return -ENOMEM;
1697 }
1698
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001699 ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1700 if (ret < 0) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001701 dev_err(priv->dev, "unable to allocate cmap\n");
1702 dma_free_coherent(priv->dev, max_size, buf, ch->dma_handle);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001703 return ret;
1704 }
1705
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001706 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1707 * for NV12 and NV21.
1708 */
1709 info->fix = sh_mobile_lcdc_fix;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001710 info->fix.smem_start = ch->dma_handle;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001711 info->fix.smem_len = max_size;
1712 if (cfg->fourcc == V4L2_PIX_FMT_NV12 ||
1713 cfg->fourcc == V4L2_PIX_FMT_NV21)
1714 info->fix.ypanstep = 2;
1715
1716 if (sh_mobile_format_is_yuv(var)) {
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001717 info->fix.line_length = var->xres;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001718 info->fix.visual = FB_VISUAL_FOURCC;
1719 } else {
1720 info->fix.line_length = var->xres * var->bits_per_pixel / 8;
1721 info->fix.visual = FB_VISUAL_TRUECOLOR;
1722 }
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001723
1724 info->screen_base = buf;
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001725 info->device = priv->dev;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001726 ch->display_var = *var;
1727
1728 return 0;
1729}
1730
Uwe Kleine-Königc2e13032010-02-04 20:56:51 +01001731static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001732{
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001733 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001734 struct sh_mobile_lcdc_priv *priv;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001735 struct resource *res;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001736 int num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001737 int error;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001738 int i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001739
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001740 if (!pdata) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001741 dev_err(&pdev->dev, "no platform data defined\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001742 return -EINVAL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001743 }
1744
1745 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Damm85645572008-12-19 15:34:41 +09001746 i = platform_get_irq(pdev, 0);
1747 if (!res || i < 0) {
1748 dev_err(&pdev->dev, "cannot get platform resources\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001749 return -ENOENT;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001750 }
1751
1752 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1753 if (!priv) {
1754 dev_err(&pdev->dev, "cannot allocate device data\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001755 return -ENOMEM;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001756 }
1757
Laurent Pinchart4774c122011-09-07 15:47:07 +02001758 priv->dev = &pdev->dev;
1759 priv->meram_dev = pdata->meram_dev;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001760 platform_set_drvdata(pdev, priv);
1761
Yong Zhangf8798cc2011-09-22 16:59:16 +08001762 error = request_irq(i, sh_mobile_lcdc_irq, 0,
Kay Sievers7ad33e72009-03-24 16:38:21 -07001763 dev_name(&pdev->dev), priv);
Magnus Damm85645572008-12-19 15:34:41 +09001764 if (error) {
1765 dev_err(&pdev->dev, "unable to request irq\n");
1766 goto err1;
1767 }
1768
1769 priv->irq = i;
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +00001770 atomic_set(&priv->hw_usecnt, -1);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001771
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001772 for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
1773 struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001774
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001775 ch->lcdc = priv;
1776 memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
1777
1778 error = sh_mobile_lcdc_check_interface(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001779 if (error) {
1780 dev_err(&pdev->dev, "unsupported interface type\n");
1781 goto err1;
1782 }
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001783 init_waitqueue_head(&ch->frame_end_wait);
1784 init_completion(&ch->vsync_completion);
1785 ch->pan_offset = 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001786
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001787 /* probe the backlight is there is one defined */
1788 if (ch->cfg.bl_info.max_brightness)
1789 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
1790
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001791 switch (pdata->ch[i].chan) {
1792 case LCDC_CHAN_MAINLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001793 ch->enabled = LDCNT2R_ME;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001794 ch->reg_offs = lcdc_offs_mainlcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001795 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001796 break;
1797 case LCDC_CHAN_SUBLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001798 ch->enabled = LDCNT2R_SE;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001799 ch->reg_offs = lcdc_offs_sublcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001800 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001801 break;
1802 }
1803 }
1804
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001805 if (!num_channels) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001806 dev_err(&pdev->dev, "no channels defined\n");
1807 error = -EINVAL;
1808 goto err1;
1809 }
1810
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001811 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001812 if (num_channels == 2)
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001813 priv->forced_fourcc = pdata->ch[0].fourcc;
Magnus Damm417d4822011-01-05 10:21:00 +00001814
Guennadi Liakhovetskidba6f382010-06-30 09:26:35 +00001815 priv->base = ioremap_nocache(res->start, resource_size(res));
1816 if (!priv->base)
1817 goto err1;
1818
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001819 error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001820 if (error) {
1821 dev_err(&pdev->dev, "unable to setup clocks\n");
1822 goto err1;
1823 }
1824
Laurent Pinchart4774c122011-09-07 15:47:07 +02001825 /* Enable runtime PM. */
1826 pm_runtime_enable(&pdev->dev);
Damian7caa4342011-05-18 11:10:07 +00001827
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001828 for (i = 0; i < num_channels; i++) {
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001829 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001830
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001831 error = sh_mobile_lcdc_channel_init(priv, ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001832 if (error)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001833 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001834 }
1835
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001836 error = sh_mobile_lcdc_start(priv);
1837 if (error) {
1838 dev_err(&pdev->dev, "unable to start hardware\n");
1839 goto err1;
1840 }
1841
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001842 for (i = 0; i < num_channels; i++) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00001843 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001844 struct fb_info *info = ch->info;
Paul Mundt1c6a3072009-07-01 06:50:31 +00001845
1846 if (info->fbdefio) {
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001847 ch->sglist = vmalloc(sizeof(struct scatterlist) *
Paul Mundt1c6a3072009-07-01 06:50:31 +00001848 info->fix.smem_len >> PAGE_SHIFT);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001849 if (!ch->sglist) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00001850 dev_err(&pdev->dev, "cannot allocate sglist\n");
1851 goto err1;
1852 }
1853 }
1854
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001855 info->bl_dev = ch->bl;
1856
Paul Mundt1c6a3072009-07-01 06:50:31 +00001857 error = register_framebuffer(info);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001858 if (error < 0)
1859 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001860
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001861 dev_info(&pdev->dev, "registered %s/%s as %dx%d %dbpp.\n",
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001862 pdev->name, (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
1863 "mainlcd" : "sublcd", info->var.xres, info->var.yres,
1864 info->var.bits_per_pixel);
Magnus Damm85645572008-12-19 15:34:41 +09001865
1866 /* deferred io mode: disable clock to save power */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001867 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
Magnus Damm85645572008-12-19 15:34:41 +09001868 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001869 }
1870
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001871 /* Failure ignored */
1872 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
1873 fb_register_client(&priv->notifier);
1874
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001875 return 0;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001876err1:
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001877 sh_mobile_lcdc_remove(pdev);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001878
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001879 return error;
1880}
1881
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001882static struct platform_driver sh_mobile_lcdc_driver = {
1883 .driver = {
1884 .name = "sh_mobile_lcdc_fb",
1885 .owner = THIS_MODULE,
Magnus Damm2feb0752009-03-13 15:36:55 +00001886 .pm = &sh_mobile_lcdc_dev_pm_ops,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001887 },
1888 .probe = sh_mobile_lcdc_probe,
1889 .remove = sh_mobile_lcdc_remove,
1890};
1891
Axel Lin4277f2c2011-11-26 10:25:54 +08001892module_platform_driver(sh_mobile_lcdc_driver);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001893
1894MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1895MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1896MODULE_LICENSE("GPL v2");