blob: 4b54cd5c956443bb7af97d936310f2cb519e1d32 [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{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100119 return chan->cfg->chan == LCDC_CHAN_SUBLCD;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200120}
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;
Laurent Pinchart58f03d92011-11-30 23:07:30 +0100276 unsigned int nr_pages_max = ch->fb_size >> PAGE_SHIFT;
Paul Mundt1c6a3072009-07-01 06:50:31 +0000277 struct page *page;
278 int nr_pages = 0;
279
280 sg_init_table(ch->sglist, nr_pages_max);
281
282 list_for_each_entry(page, pagelist, lru)
283 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
284
285 return nr_pages;
286}
287
Magnus Damm85645572008-12-19 15:34:41 +0900288static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
289 struct list_head *pagelist)
290{
291 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100292 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
Magnus Damm85645572008-12-19 15:34:41 +0900293
294 /* enable clocks before accessing hardware */
295 sh_mobile_lcdc_clk_on(ch->lcdc);
296
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900297 /*
298 * It's possible to get here without anything on the pagelist via
299 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
300 * invocation. In the former case, the acceleration routines are
301 * stepped in to when using the framebuffer console causing the
302 * workqueue to be scheduled without any dirty pages on the list.
303 *
304 * Despite this, a panel update is still needed given that the
305 * acceleration routines have their own methods for writing in
306 * that still need to be updated.
307 *
308 * The fsync() and empty pagelist case could be optimized for,
309 * but we don't bother, as any application exhibiting such
310 * behaviour is fundamentally broken anyways.
311 */
312 if (!list_empty(pagelist)) {
313 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
Paul Mundt1c6a3072009-07-01 06:50:31 +0000314
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900315 /* trigger panel update */
Laurent Pincharte8363142011-11-29 14:37:35 +0100316 dma_map_sg(ch->lcdc->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
Laurent Pinchartafaad832011-09-11 22:59:04 +0200317 if (panel->start_transfer)
318 panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200319 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
Laurent Pincharte8363142011-11-29 14:37:35 +0100320 dma_unmap_sg(ch->lcdc->dev, ch->sglist, nr_pages,
321 DMA_TO_DEVICE);
Magnus Dammef61aae2009-12-07 14:20:06 +0000322 } else {
Laurent Pinchartafaad832011-09-11 22:59:04 +0200323 if (panel->start_transfer)
324 panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200325 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
Magnus Dammef61aae2009-12-07 14:20:06 +0000326 }
Magnus Damm85645572008-12-19 15:34:41 +0900327}
328
329static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
330{
331 struct fb_deferred_io *fbdefio = info->fbdefio;
332
333 if (fbdefio)
334 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
335}
336
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200337static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan *ch)
338{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100339 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200340
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200341 if (ch->tx_dev) {
Laurent Pinchart458981c2011-11-28 23:19:59 +0100342 int ret;
343
344 ret = ch->tx_dev->ops->display_on(ch->tx_dev);
345 if (ret < 0)
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200346 return;
Laurent Pinchart458981c2011-11-28 23:19:59 +0100347
348 if (ret == SH_MOBILE_LCDC_DISPLAY_DISCONNECTED)
349 ch->info->state = FBINFO_STATE_SUSPENDED;
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200350 }
351
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200352 /* HDMI must be enabled before LCDC configuration */
Laurent Pinchartafaad832011-09-11 22:59:04 +0200353 if (panel->display_on)
354 panel->display_on();
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200355}
356
357static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan *ch)
358{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100359 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200360
Laurent Pinchartafaad832011-09-11 22:59:04 +0200361 if (panel->display_off)
362 panel->display_off();
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200363
364 if (ch->tx_dev)
365 ch->tx_dev->ops->display_off(ch->tx_dev);
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200366}
367
Laurent Pinchartecd29942011-09-18 14:14:46 +0200368static bool
369sh_mobile_lcdc_must_reconfigure(struct sh_mobile_lcdc_chan *ch,
Laurent Pincharte0c86012011-11-29 01:05:47 +0100370 const struct fb_videomode *new_mode)
Laurent Pinchartecd29942011-09-18 14:14:46 +0200371{
Laurent Pinchartecd29942011-09-18 14:14:46 +0200372 dev_dbg(ch->info->dev, "Old %ux%u, new %ux%u\n",
Laurent Pinchart2d045592011-11-29 13:42:48 +0100373 ch->display.mode.xres, ch->display.mode.yres,
374 new_mode->xres, new_mode->yres);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200375
Laurent Pincharte0c86012011-11-29 01:05:47 +0100376 /* It can be a different monitor with an equal video-mode */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100377 if (fb_mode_is_equal(&ch->display.mode, new_mode))
Laurent Pinchartecd29942011-09-18 14:14:46 +0200378 return false;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200379
380 dev_dbg(ch->info->dev, "Switching %u -> %u lines\n",
Laurent Pinchart2d045592011-11-29 13:42:48 +0100381 ch->display.mode.yres, new_mode->yres);
382 ch->display.mode = *new_mode;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200383
384 return true;
385}
386
387static int sh_mobile_check_var(struct fb_var_screeninfo *var,
388 struct fb_info *info);
389
390static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan *ch,
391 enum sh_mobile_lcdc_entity_event event,
Laurent Pincharte0c86012011-11-29 01:05:47 +0100392 const struct fb_videomode *mode,
393 const struct fb_monspecs *monspec)
Laurent Pinchartecd29942011-09-18 14:14:46 +0200394{
395 struct fb_info *info = ch->info;
Laurent Pincharte0c86012011-11-29 01:05:47 +0100396 struct fb_var_screeninfo var;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200397 int ret = 0;
398
399 switch (event) {
400 case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT:
401 /* HDMI plug in */
402 if (lock_fb_info(info)) {
403 console_lock();
404
Laurent Pinchart2d045592011-11-29 13:42:48 +0100405 ch->display.width = monspec->max_x * 10;
406 ch->display.height = monspec->max_y * 10;
Laurent Pincharte0c86012011-11-29 01:05:47 +0100407
408 if (!sh_mobile_lcdc_must_reconfigure(ch, mode) &&
Laurent Pinchartecd29942011-09-18 14:14:46 +0200409 info->state == FBINFO_STATE_RUNNING) {
410 /* First activation with the default monitor.
411 * Just turn on, if we run a resume here, the
412 * logo disappears.
413 */
Laurent Pincharte0c86012011-11-29 01:05:47 +0100414 info->var.width = monspec->max_x * 10;
415 info->var.height = monspec->max_y * 10;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200416 sh_mobile_lcdc_display_on(ch);
417 } else {
418 /* New monitor or have to wake up */
419 fb_set_suspend(info, 0);
420 }
421
422 console_unlock();
423 unlock_fb_info(info);
424 }
425 break;
426
427 case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT:
428 /* HDMI disconnect */
429 if (lock_fb_info(info)) {
430 console_lock();
431 fb_set_suspend(info, 1);
432 console_unlock();
433 unlock_fb_info(info);
434 }
435 break;
436
437 case SH_MOBILE_LCDC_EVENT_DISPLAY_MODE:
438 /* Validate a proposed new mode */
Laurent Pincharte0c86012011-11-29 01:05:47 +0100439 fb_videomode_to_var(&var, mode);
440 var.bits_per_pixel = info->var.bits_per_pixel;
441 var.grayscale = info->var.grayscale;
442 ret = sh_mobile_check_var(&var, info);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200443 break;
444 }
445
446 return ret;
447}
448
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200449/* -----------------------------------------------------------------------------
450 * Format helpers
451 */
452
Laurent Pinchart105784b2011-11-29 15:58:10 +0100453struct sh_mobile_lcdc_format_info {
454 u32 fourcc;
455 unsigned int bpp;
456 bool yuv;
457 u32 lddfr;
458};
459
460static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos[] = {
461 {
462 .fourcc = V4L2_PIX_FMT_RGB565,
463 .bpp = 16,
464 .yuv = false,
465 .lddfr = LDDFR_PKF_RGB16,
466 }, {
467 .fourcc = V4L2_PIX_FMT_BGR24,
468 .bpp = 24,
469 .yuv = false,
470 .lddfr = LDDFR_PKF_RGB24,
471 }, {
472 .fourcc = V4L2_PIX_FMT_BGR32,
473 .bpp = 32,
474 .yuv = false,
475 .lddfr = LDDFR_PKF_ARGB32,
476 }, {
477 .fourcc = V4L2_PIX_FMT_NV12,
478 .bpp = 12,
479 .yuv = true,
480 .lddfr = LDDFR_CC | LDDFR_YF_420,
481 }, {
482 .fourcc = V4L2_PIX_FMT_NV21,
483 .bpp = 12,
484 .yuv = true,
485 .lddfr = LDDFR_CC | LDDFR_YF_420,
486 }, {
487 .fourcc = V4L2_PIX_FMT_NV16,
488 .bpp = 16,
489 .yuv = true,
490 .lddfr = LDDFR_CC | LDDFR_YF_422,
491 }, {
492 .fourcc = V4L2_PIX_FMT_NV61,
493 .bpp = 16,
494 .yuv = true,
495 .lddfr = LDDFR_CC | LDDFR_YF_422,
496 }, {
497 .fourcc = V4L2_PIX_FMT_NV24,
498 .bpp = 24,
499 .yuv = true,
500 .lddfr = LDDFR_CC | LDDFR_YF_444,
501 }, {
502 .fourcc = V4L2_PIX_FMT_NV42,
503 .bpp = 24,
504 .yuv = true,
505 .lddfr = LDDFR_CC | LDDFR_YF_444,
506 },
507};
508
509static const struct sh_mobile_lcdc_format_info *
510sh_mobile_format_info(u32 fourcc)
511{
512 unsigned int i;
513
514 for (i = 0; i < ARRAY_SIZE(sh_mobile_format_infos); ++i) {
515 if (sh_mobile_format_infos[i].fourcc == fourcc)
516 return &sh_mobile_format_infos[i];
517 }
518
519 return NULL;
520}
521
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200522static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
523{
524 if (var->grayscale > 1)
525 return var->grayscale;
526
527 switch (var->bits_per_pixel) {
528 case 16:
529 return V4L2_PIX_FMT_RGB565;
530 case 24:
531 return V4L2_PIX_FMT_BGR24;
532 case 32:
533 return V4L2_PIX_FMT_BGR32;
534 default:
535 return 0;
536 }
537}
538
539static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
540{
541 return var->grayscale > 1;
542}
543
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200544/* -----------------------------------------------------------------------------
545 * Start, stop and IRQ
546 */
547
Magnus Damm85645572008-12-19 15:34:41 +0900548static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
549{
550 struct sh_mobile_lcdc_priv *priv = data;
Magnus Damm2feb0752009-03-13 15:36:55 +0000551 struct sh_mobile_lcdc_chan *ch;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000552 unsigned long ldintr;
Magnus Damm2feb0752009-03-13 15:36:55 +0000553 int is_sub;
554 int k;
Magnus Damm85645572008-12-19 15:34:41 +0900555
Laurent Pinchartdc486652011-07-13 12:13:47 +0200556 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
557 ldintr = lcdc_read(priv, _LDINTR);
558 lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
Magnus Damm85645572008-12-19 15:34:41 +0900559
Magnus Damm2feb0752009-03-13 15:36:55 +0000560 /* figure out if this interrupt is for main or sub lcd */
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200561 is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
Magnus Damm2feb0752009-03-13 15:36:55 +0000562
Phil Edworthy9dd38812009-09-15 12:00:18 +0000563 /* wake up channel and disable clocks */
Magnus Damm2feb0752009-03-13 15:36:55 +0000564 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
565 ch = &priv->ch[k];
566
567 if (!ch->enabled)
568 continue;
569
Laurent Pinchartdc486652011-07-13 12:13:47 +0200570 /* Frame End */
Phil Edworthy9dd38812009-09-15 12:00:18 +0000571 if (ldintr & LDINTR_FS) {
572 if (is_sub == lcdc_chan_is_sublcd(ch)) {
573 ch->frame_end = 1;
574 wake_up(&ch->frame_end_wait);
Magnus Damm2feb0752009-03-13 15:36:55 +0000575
Phil Edworthy9dd38812009-09-15 12:00:18 +0000576 sh_mobile_lcdc_clk_off(priv);
577 }
578 }
579
580 /* VSYNC End */
Phil Edworthy40331b22010-02-15 13:57:49 +0000581 if (ldintr & LDINTR_VES)
582 complete(&ch->vsync_completion);
Magnus Damm2feb0752009-03-13 15:36:55 +0000583 }
584
Magnus Damm85645572008-12-19 15:34:41 +0900585 return IRQ_HANDLED;
586}
587
Laurent Pinchart49766772011-11-30 23:07:30 +0100588static int sh_mobile_wait_for_vsync(struct sh_mobile_lcdc_chan *ch)
589{
590 unsigned long ldintr;
591 int ret;
592
593 /* Enable VSync End interrupt and be careful not to acknowledge any
594 * pending interrupt.
595 */
596 ldintr = lcdc_read(ch->lcdc, _LDINTR);
597 ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
598 lcdc_write(ch->lcdc, _LDINTR, ldintr);
599
600 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
601 msecs_to_jiffies(100));
602 if (!ret)
603 return -ETIMEDOUT;
604
605 return 0;
606}
607
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700608static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
609 int start)
610{
611 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
612 int k;
613
614 /* start or stop the lcdc */
615 if (start)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200616 lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700617 else
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200618 lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700619
620 /* wait until power is applied/stopped on all channels */
621 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
622 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
623 while (1) {
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200624 tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
625 & LDPMR_LPS;
626 if (start && tmp == LDPMR_LPS)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700627 break;
628 if (!start && tmp == 0)
629 break;
630 cpu_relax();
631 }
632
633 if (!start)
634 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
635}
636
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000637static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
638{
Laurent Pinchart2d045592011-11-29 13:42:48 +0100639 const struct fb_var_screeninfo *var = &ch->info->var;
640 const struct fb_videomode *mode = &ch->display.mode;
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000641 unsigned long h_total, hsync_pos, display_h_total;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000642 u32 tmp;
643
644 tmp = ch->ldmt1r_value;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200645 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
646 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100647 tmp |= (ch->cfg->flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
648 tmp |= (ch->cfg->flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
649 tmp |= (ch->cfg->flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
650 tmp |= (ch->cfg->flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
651 tmp |= (ch->cfg->flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000652 lcdc_write_chan(ch, LDMT1R, tmp);
653
654 /* setup SYS bus */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100655 lcdc_write_chan(ch, LDMT2R, ch->cfg->sys_bus_cfg.ldmt2r);
656 lcdc_write_chan(ch, LDMT3R, ch->cfg->sys_bus_cfg.ldmt3r);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000657
658 /* horizontal configuration */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100659 h_total = mode->xres + mode->hsync_len + mode->left_margin
660 + mode->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000661 tmp = h_total / 8; /* HTCN */
Laurent Pinchart58f03d92011-11-30 23:07:30 +0100662 tmp |= (min(mode->xres, ch->xres) / 8) << 16; /* HDCN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000663 lcdc_write_chan(ch, LDHCNR, tmp);
664
Laurent Pinchart2d045592011-11-29 13:42:48 +0100665 hsync_pos = mode->xres + mode->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000666 tmp = hsync_pos / 8; /* HSYNP */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100667 tmp |= (mode->hsync_len / 8) << 16; /* HSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000668 lcdc_write_chan(ch, LDHSYNR, tmp);
669
670 /* vertical configuration */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100671 tmp = mode->yres + mode->vsync_len + mode->upper_margin
672 + mode->lower_margin; /* VTLN */
Laurent Pinchart58f03d92011-11-30 23:07:30 +0100673 tmp |= min(mode->yres, ch->yres) << 16; /* VDLN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000674 lcdc_write_chan(ch, LDVLNR, tmp);
675
Laurent Pinchart2d045592011-11-29 13:42:48 +0100676 tmp = mode->yres + mode->lower_margin; /* VSYNP */
677 tmp |= mode->vsync_len << 16; /* VSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000678 lcdc_write_chan(ch, LDVSYNR, tmp);
679
680 /* Adjust horizontal synchronisation for HDMI */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100681 display_h_total = mode->xres + mode->hsync_len + mode->left_margin
682 + mode->right_margin;
683 tmp = ((mode->xres & 7) << 24) | ((display_h_total & 7) << 16)
684 | ((mode->hsync_len & 7) << 8) | (hsync_pos & 7);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000685 lcdc_write_chan(ch, LDHAJR, tmp);
686}
687
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200688/*
689 * __sh_mobile_lcdc_start - Configure and tart the LCDC
690 * @priv: LCDC device
691 *
692 * Configure all enabled channels and start the LCDC device. All external
693 * devices (clocks, MERAM, panels, ...) are not touched by this function.
694 */
695static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700696{
697 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700698 unsigned long tmp;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200699 int k, m;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700700
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200701 /* Enable LCDC channels. Read data from external memory, avoid using the
702 * BEU for now.
703 */
704 lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
Magnus Damm85645572008-12-19 15:34:41 +0900705
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200706 /* Stop the LCDC first and disable all interrupts. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700707 sh_mobile_lcdc_start_stop(priv, 0);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200708 lcdc_write(priv, _LDINTR, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700709
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200710 /* Configure power supply, dot clocks and start them. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700711 tmp = priv->lddckr;
712 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
713 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200714 if (!ch->enabled)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700715 continue;
716
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200717 /* Power supply */
718 lcdc_write_chan(ch, LDPMR, 0);
719
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100720 m = ch->cfg->clock_divider;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700721 if (!m)
722 continue;
723
Laurent Pinchart505c7de52011-07-13 12:13:47 +0200724 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
725 * denominator.
726 */
727 lcdc_write_chan(ch, LDDCKPAT1R, 0);
728 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
729
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700730 if (m == 1)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200731 m = LDDCKR_MOSEL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700732 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700733 }
734
735 lcdc_write(priv, _LDDCKR, tmp);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700736 lcdc_write(priv, _LDDCKSTPR, 0);
737 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
738
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200739 /* Setup geometry, format, frame buffer memory and operation mode. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700740 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
741 ch = &priv->ch[k];
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700742 if (!ch->enabled)
743 continue;
744
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000745 sh_mobile_lcdc_geometry(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700746
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +0100747 tmp = ch->format->lddfr;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100748
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +0100749 if (ch->format->yuv) {
Laurent Pinchart58f03d92011-11-30 23:07:30 +0100750 switch (ch->colorspace) {
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100751 case V4L2_COLORSPACE_REC709:
752 tmp |= LDDFR_CF1;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000753 break;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100754 case V4L2_COLORSPACE_JPEG:
755 tmp |= LDDFR_CF0;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000756 break;
757 }
Magnus Damm417d4822011-01-05 10:21:00 +0000758 }
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200759
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700760 lcdc_write_chan(ch, LDDFR, tmp);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200761 lcdc_write_chan(ch, LDMLSR, ch->pitch);
762 lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +0100763 if (ch->format->yuv)
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200764 lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700765
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200766 /* When using deferred I/O mode, configure the LCDC for one-shot
767 * operation and enable the frame end interrupt. Otherwise use
768 * continuous read mode.
769 */
770 if (ch->ldmt1r_value & LDMT1R_IFM &&
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100771 ch->cfg->sys_bus_cfg.deferred_io_msec) {
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200772 lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
773 lcdc_write(priv, _LDINTR, LDINTR_FE);
774 } else {
775 lcdc_write_chan(ch, LDSM1R, 0);
776 }
777 }
Damian7caa4342011-05-18 11:10:07 +0000778
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200779 /* Word and long word swap. */
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +0100780 switch (priv->ch[0].format->fourcc) {
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100781 case V4L2_PIX_FMT_RGB565:
782 case V4L2_PIX_FMT_NV21:
783 case V4L2_PIX_FMT_NV61:
784 case V4L2_PIX_FMT_NV42:
785 tmp = LDDDSR_LS | LDDDSR_WS;
786 break;
787 case V4L2_PIX_FMT_BGR24:
788 case V4L2_PIX_FMT_NV12:
789 case V4L2_PIX_FMT_NV16:
790 case V4L2_PIX_FMT_NV24:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200791 tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100792 break;
793 case V4L2_PIX_FMT_BGR32:
794 default:
795 tmp = LDDDSR_LS;
796 break;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200797 }
798 lcdc_write(priv, _LDDDSR, tmp);
Damian7caa4342011-05-18 11:10:07 +0000799
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200800 /* Enable the display output. */
801 lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
802 sh_mobile_lcdc_start_stop(priv, 1);
803 priv->started = 1;
804}
Damian7caa4342011-05-18 11:10:07 +0000805
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200806static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
807{
808 struct sh_mobile_meram_info *mdev = priv->meram_dev;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200809 struct sh_mobile_lcdc_chan *ch;
810 unsigned long tmp;
811 int ret;
812 int k;
813
814 /* enable clocks before accessing the hardware */
815 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
816 if (priv->ch[k].enabled)
817 sh_mobile_lcdc_clk_on(priv);
818 }
819
820 /* reset */
821 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
822 lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
823
824 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100825 const struct sh_mobile_lcdc_panel_cfg *panel;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200826
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200827 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200828 if (!ch->enabled)
829 continue;
830
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100831 panel = &ch->cfg->panel_cfg;
Laurent Pinchartafaad832011-09-11 22:59:04 +0200832 if (panel->setup_sys) {
833 ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200834 if (ret)
835 return ret;
836 }
837 }
838
839 /* Compute frame buffer base address and pitch for each channel. */
840 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200841 int pixelformat;
Laurent Pinchart48110052011-12-12 16:36:13 +0100842 void *meram;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200843
844 ch = &priv->ch[k];
845 if (!ch->enabled)
846 continue;
847
Laurent Pinchart58f03d92011-11-30 23:07:30 +0100848 ch->base_addr_y = ch->dma_handle;
849 ch->base_addr_c = ch->base_addr_y + ch->xres * ch->yres_virtual;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200850
851 /* Enable MERAM if possible. */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100852 if (mdev == NULL || mdev->ops == NULL ||
853 ch->cfg->meram_cfg == NULL)
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200854 continue;
855
856 /* we need to de-init configured ICBs before we can
857 * re-initialize them.
858 */
Laurent Pinchart48110052011-12-12 16:36:13 +0100859 if (ch->meram) {
860 mdev->ops->meram_unregister(mdev, ch->meram);
861 ch->meram = NULL;
Damian7caa4342011-05-18 11:10:07 +0000862 }
863
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +0100864 switch (ch->format->fourcc) {
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100865 case V4L2_PIX_FMT_NV12:
866 case V4L2_PIX_FMT_NV21:
867 case V4L2_PIX_FMT_NV16:
868 case V4L2_PIX_FMT_NV61:
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200869 pixelformat = SH_MOBILE_MERAM_PF_NV;
Laurent Pinchartedd153a2011-12-13 14:02:28 +0100870 break;
871 case V4L2_PIX_FMT_NV24:
872 case V4L2_PIX_FMT_NV42:
873 pixelformat = SH_MOBILE_MERAM_PF_NV24;
874 break;
875 case V4L2_PIX_FMT_RGB565:
876 case V4L2_PIX_FMT_BGR24:
877 case V4L2_PIX_FMT_BGR32:
878 default:
879 pixelformat = SH_MOBILE_MERAM_PF_RGB;
880 break;
881 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700882
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100883 meram = mdev->ops->meram_register(mdev, ch->cfg->meram_cfg,
884 ch->pitch, ch->yres, pixelformat,
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200885 &ch->pitch);
Laurent Pinchart97d16fe2011-11-22 00:56:58 +0100886 if (!IS_ERR(meram)) {
887 mdev->ops->meram_update(mdev, meram,
888 ch->base_addr_y, ch->base_addr_c,
889 &ch->base_addr_y, &ch->base_addr_c);
Laurent Pinchart48110052011-12-12 16:36:13 +0100890 ch->meram = meram;
Laurent Pinchart97d16fe2011-11-22 00:56:58 +0100891 }
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200892 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700893
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200894 /* Start the LCDC. */
895 __sh_mobile_lcdc_start(priv);
896
897 /* Setup deferred I/O, tell the board code to enable the panels, and
898 * turn backlight on.
899 */
900 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
901 ch = &priv->ch[k];
902 if (!ch->enabled)
903 continue;
904
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100905 tmp = ch->cfg->sys_bus_cfg.deferred_io_msec;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200906 if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
Magnus Damm85645572008-12-19 15:34:41 +0900907 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
908 ch->defio.delay = msecs_to_jiffies(tmp);
Paul Mundte33afdd2009-07-07 11:24:32 +0900909 ch->info->fbdefio = &ch->defio;
910 fb_deferred_io_init(ch->info);
Magnus Damm85645572008-12-19 15:34:41 +0900911 }
Magnus Damm21bc1f02009-08-15 02:53:16 +0000912
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200913 sh_mobile_lcdc_display_on(ch);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000914
915 if (ch->bl) {
916 ch->bl->props.power = FB_BLANK_UNBLANK;
917 backlight_update_status(ch->bl);
918 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700919 }
920
921 return 0;
922}
923
924static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
925{
926 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700927 int k;
928
Magnus Damm2feb0752009-03-13 15:36:55 +0000929 /* clean up deferred io and ask board code to disable panel */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700930 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
931 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +0000932 if (!ch->enabled)
933 continue;
Magnus Damm2feb0752009-03-13 15:36:55 +0000934
935 /* deferred io mode:
936 * flush frame, and wait for frame end interrupt
937 * clean up deferred io and enable clock
938 */
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +0000939 if (ch->info && ch->info->fbdefio) {
Magnus Damm2feb0752009-03-13 15:36:55 +0000940 ch->frame_end = 0;
Paul Mundte33afdd2009-07-07 11:24:32 +0900941 schedule_delayed_work(&ch->info->deferred_work, 0);
Magnus Damm2feb0752009-03-13 15:36:55 +0000942 wait_event(ch->frame_end_wait, ch->frame_end);
Paul Mundte33afdd2009-07-07 11:24:32 +0900943 fb_deferred_io_cleanup(ch->info);
944 ch->info->fbdefio = NULL;
Magnus Damm2feb0752009-03-13 15:36:55 +0000945 sh_mobile_lcdc_clk_on(priv);
946 }
947
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000948 if (ch->bl) {
949 ch->bl->props.power = FB_BLANK_POWERDOWN;
950 backlight_update_status(ch->bl);
951 }
952
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200953 sh_mobile_lcdc_display_off(ch);
Damian7caa4342011-05-18 11:10:07 +0000954
955 /* disable the meram */
Laurent Pinchart48110052011-12-12 16:36:13 +0100956 if (ch->meram) {
Damian7caa4342011-05-18 11:10:07 +0000957 struct sh_mobile_meram_info *mdev;
Damian7caa4342011-05-18 11:10:07 +0000958 mdev = priv->meram_dev;
Laurent Pinchart48110052011-12-12 16:36:13 +0100959 mdev->ops->meram_unregister(mdev, ch->meram);
960 ch->meram = 0;
Damian7caa4342011-05-18 11:10:07 +0000961 }
962
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700963 }
964
965 /* stop the lcdc */
Magnus Damm8e9bb192009-05-20 14:34:43 +0000966 if (priv->started) {
967 sh_mobile_lcdc_start_stop(priv, 0);
968 priv->started = 0;
969 }
Magnus Dammb51339f2008-10-31 20:23:26 +0900970
Magnus Damm85645572008-12-19 15:34:41 +0900971 /* stop clocks */
972 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
973 if (priv->ch[k].enabled)
974 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700975}
976
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200977/* -----------------------------------------------------------------------------
978 * Frame buffer operations
979 */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700980
981static int sh_mobile_lcdc_setcolreg(u_int regno,
982 u_int red, u_int green, u_int blue,
983 u_int transp, struct fb_info *info)
984{
985 u32 *palette = info->pseudo_palette;
986
987 if (regno >= PALETTE_NR)
988 return -EINVAL;
989
990 /* only FB_VISUAL_TRUECOLOR supported */
991
992 red >>= 16 - info->var.red.length;
993 green >>= 16 - info->var.green.length;
994 blue >>= 16 - info->var.blue.length;
995 transp >>= 16 - info->var.transp.length;
996
997 palette[regno] = (red << info->var.red.offset) |
998 (green << info->var.green.offset) |
999 (blue << info->var.blue.offset) |
1000 (transp << info->var.transp.offset);
1001
1002 return 0;
1003}
1004
1005static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
1006 .id = "SH Mobile LCDC",
1007 .type = FB_TYPE_PACKED_PIXELS,
1008 .visual = FB_VISUAL_TRUECOLOR,
1009 .accel = FB_ACCEL_NONE,
Phil Edworthy9dd38812009-09-15 12:00:18 +00001010 .xpanstep = 0,
1011 .ypanstep = 1,
1012 .ywrapstep = 0,
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001013 .capabilities = FB_CAP_FOURCC,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001014};
1015
Magnus Damm85645572008-12-19 15:34:41 +09001016static void sh_mobile_lcdc_fillrect(struct fb_info *info,
1017 const struct fb_fillrect *rect)
1018{
1019 sys_fillrect(info, rect);
1020 sh_mobile_lcdc_deferred_io_touch(info);
1021}
1022
1023static void sh_mobile_lcdc_copyarea(struct fb_info *info,
1024 const struct fb_copyarea *area)
1025{
1026 sys_copyarea(info, area);
1027 sh_mobile_lcdc_deferred_io_touch(info);
1028}
1029
1030static void sh_mobile_lcdc_imageblit(struct fb_info *info,
1031 const struct fb_image *image)
1032{
1033 sys_imageblit(info, image);
1034 sh_mobile_lcdc_deferred_io_touch(info);
1035}
1036
Phil Edworthy9dd38812009-09-15 12:00:18 +00001037static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
1038 struct fb_info *info)
1039{
1040 struct sh_mobile_lcdc_chan *ch = info->par;
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001041 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
1042 unsigned long ldrcntr;
1043 unsigned long new_pan_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001044 unsigned long base_addr_y, base_addr_c;
1045 unsigned long c_offset;
Phil Edworthy9dd38812009-09-15 12:00:18 +00001046
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001047 if (!ch->format->yuv)
1048 new_pan_offset = var->yoffset * ch->pitch
1049 + var->xoffset * (ch->format->bpp / 8);
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001050 else
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001051 new_pan_offset = var->yoffset * ch->pitch + var->xoffset;
Phil Edworthy9dd38812009-09-15 12:00:18 +00001052
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001053 if (new_pan_offset == ch->pan_offset)
1054 return 0; /* No change, do nothing */
1055
1056 ldrcntr = lcdc_read(priv, _LDRCNTR);
1057
1058 /* Set the source address for the next refresh */
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001059 base_addr_y = ch->dma_handle + new_pan_offset;
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001060 if (ch->format->yuv) {
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001061 /* Set y offset */
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001062 c_offset = var->yoffset * ch->pitch
1063 * (ch->format->bpp - 8) / 8;
1064 base_addr_c = ch->dma_handle + ch->xres * ch->yres_virtual
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +02001065 + c_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001066 /* Set x offset */
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001067 if (ch->format->fourcc == V4L2_PIX_FMT_NV24)
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001068 base_addr_c += 2 * var->xoffset;
1069 else
1070 base_addr_c += var->xoffset;
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001071 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001072
Laurent Pinchart48110052011-12-12 16:36:13 +01001073 if (ch->meram) {
Damian7caa4342011-05-18 11:10:07 +00001074 struct sh_mobile_meram_info *mdev;
Damian7caa4342011-05-18 11:10:07 +00001075 int ret;
1076
Damian7caa4342011-05-18 11:10:07 +00001077 mdev = priv->meram_dev;
Laurent Pinchart48110052011-12-12 16:36:13 +01001078 ret = mdev->ops->meram_update(mdev, ch->meram,
Damian7caa4342011-05-18 11:10:07 +00001079 base_addr_y, base_addr_c,
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001080 &base_addr_y, &base_addr_c);
Damian7caa4342011-05-18 11:10:07 +00001081 if (ret)
1082 return ret;
Damian7caa4342011-05-18 11:10:07 +00001083 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001084
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001085 ch->base_addr_y = base_addr_y;
1086 ch->base_addr_c = base_addr_c;
1087
1088 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001089 if (ch->format->yuv)
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001090 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
1091
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001092 if (lcdc_chan_is_sublcd(ch))
1093 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
1094 else
1095 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
1096
1097 ch->pan_offset = new_pan_offset;
1098
1099 sh_mobile_lcdc_deferred_io_touch(info);
Phil Edworthy9dd38812009-09-15 12:00:18 +00001100
1101 return 0;
1102}
1103
Phil Edworthy40331b22010-02-15 13:57:49 +00001104static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
1105 unsigned long arg)
1106{
1107 int retval;
1108
1109 switch (cmd) {
1110 case FBIO_WAITFORVSYNC:
Laurent Pinchart49766772011-11-30 23:07:30 +01001111 retval = sh_mobile_wait_for_vsync(info->par);
Phil Edworthy40331b22010-02-15 13:57:49 +00001112 break;
1113
1114 default:
1115 retval = -ENOIOCTLCMD;
1116 break;
1117 }
1118 return retval;
1119}
1120
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001121static void sh_mobile_fb_reconfig(struct fb_info *info)
1122{
1123 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchart2d045592011-11-29 13:42:48 +01001124 struct fb_var_screeninfo var;
1125 struct fb_videomode mode;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001126 struct fb_event event;
1127 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1128
1129 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1130 /* More framebuffer users are active */
1131 return;
1132
Laurent Pinchart2d045592011-11-29 13:42:48 +01001133 fb_var_to_videomode(&mode, &info->var);
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001134
Laurent Pinchart2d045592011-11-29 13:42:48 +01001135 if (fb_mode_is_equal(&ch->display.mode, &mode))
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001136 return;
1137
1138 /* Display has been re-plugged, framebuffer is free now, reconfigure */
Laurent Pinchart2d045592011-11-29 13:42:48 +01001139 var = info->var;
1140 fb_videomode_to_var(&var, &ch->display.mode);
1141 var.width = ch->display.width;
1142 var.height = ch->display.height;
1143 var.activate = FB_ACTIVATE_NOW;
1144
1145 if (fb_set_var(info, &var) < 0)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001146 /* Couldn't reconfigure, hopefully, can continue as before */
1147 return;
1148
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001149 /*
1150 * fb_set_var() calls the notifier change internally, only if
1151 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1152 * user event, we have to call the chain ourselves.
1153 */
1154 event.info = info;
Laurent Pinchart2d045592011-11-29 13:42:48 +01001155 event.data = &ch->display.mode;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001156 fb_notifier_call_chain(evnt, &event);
1157}
1158
1159/*
1160 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1161 * user == 1, or with console sem held, if user == 0.
1162 */
1163static int sh_mobile_release(struct fb_info *info, int user)
1164{
1165 struct sh_mobile_lcdc_chan *ch = info->par;
1166
1167 mutex_lock(&ch->open_lock);
1168 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1169
1170 ch->use_count--;
1171
1172 /* Nothing to reconfigure, when called from fbcon */
1173 if (user) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001174 console_lock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001175 sh_mobile_fb_reconfig(info);
Torben Hohnac751ef2011-01-25 15:07:35 -08001176 console_unlock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001177 }
1178
1179 mutex_unlock(&ch->open_lock);
1180
1181 return 0;
1182}
1183
1184static int sh_mobile_open(struct fb_info *info, int user)
1185{
1186 struct sh_mobile_lcdc_chan *ch = info->par;
1187
1188 mutex_lock(&ch->open_lock);
1189 ch->use_count++;
1190
1191 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1192 mutex_unlock(&ch->open_lock);
1193
1194 return 0;
1195}
1196
1197static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1198{
1199 struct sh_mobile_lcdc_chan *ch = info->par;
Magnus Damm417d4822011-01-05 10:21:00 +00001200 struct sh_mobile_lcdc_priv *p = ch->lcdc;
Laurent Pinchart03862192011-08-31 13:00:53 +02001201 unsigned int best_dist = (unsigned int)-1;
1202 unsigned int best_xres = 0;
1203 unsigned int best_yres = 0;
1204 unsigned int i;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001205
Laurent Pinchart03862192011-08-31 13:00:53 +02001206 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001207 return -EINVAL;
Laurent Pinchart03862192011-08-31 13:00:53 +02001208
1209 /* If board code provides us with a list of available modes, make sure
1210 * we use one of them. Find the mode closest to the requested one. The
1211 * distance between two modes is defined as the size of the
1212 * non-overlapping parts of the two rectangles.
1213 */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001214 for (i = 0; i < ch->cfg->num_modes; ++i) {
1215 const struct fb_videomode *mode = &ch->cfg->lcd_modes[i];
Laurent Pinchart03862192011-08-31 13:00:53 +02001216 unsigned int dist;
1217
1218 /* We can only round up. */
1219 if (var->xres > mode->xres || var->yres > mode->yres)
1220 continue;
1221
1222 dist = var->xres * var->yres + mode->xres * mode->yres
1223 - 2 * min(var->xres, mode->xres)
1224 * min(var->yres, mode->yres);
1225
1226 if (dist < best_dist) {
1227 best_xres = mode->xres;
1228 best_yres = mode->yres;
1229 best_dist = dist;
1230 }
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001231 }
Magnus Damm417d4822011-01-05 10:21:00 +00001232
Laurent Pinchart03862192011-08-31 13:00:53 +02001233 /* If no available mode can be used, return an error. */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001234 if (ch->cfg->num_modes != 0) {
Laurent Pinchart03862192011-08-31 13:00:53 +02001235 if (best_dist == (unsigned int)-1)
1236 return -EINVAL;
1237
1238 var->xres = best_xres;
1239 var->yres = best_yres;
1240 }
1241
1242 /* Make sure the virtual resolution is at least as big as the visible
1243 * resolution.
1244 */
1245 if (var->xres_virtual < var->xres)
1246 var->xres_virtual = var->xres;
1247 if (var->yres_virtual < var->yres)
1248 var->yres_virtual = var->yres;
1249
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001250 if (sh_mobile_format_is_fourcc(var)) {
Laurent Pinchart105784b2011-11-29 15:58:10 +01001251 const struct sh_mobile_lcdc_format_info *format;
1252
1253 format = sh_mobile_format_info(var->grayscale);
1254 if (format == NULL)
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001255 return -EINVAL;
Laurent Pinchart105784b2011-11-29 15:58:10 +01001256 var->bits_per_pixel = format->bpp;
Laurent Pinchart03862192011-08-31 13:00:53 +02001257
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001258 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1259 * respectively.
1260 */
Laurent Pinchart105784b2011-11-29 15:58:10 +01001261 if (!format->yuv)
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001262 var->colorspace = V4L2_COLORSPACE_SRGB;
1263 else if (var->colorspace != V4L2_COLORSPACE_REC709)
1264 var->colorspace = V4L2_COLORSPACE_JPEG;
1265 } else {
1266 if (var->bits_per_pixel <= 16) { /* RGB 565 */
1267 var->bits_per_pixel = 16;
1268 var->red.offset = 11;
1269 var->red.length = 5;
1270 var->green.offset = 5;
1271 var->green.length = 6;
1272 var->blue.offset = 0;
1273 var->blue.length = 5;
1274 var->transp.offset = 0;
1275 var->transp.length = 0;
1276 } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
1277 var->bits_per_pixel = 24;
1278 var->red.offset = 16;
1279 var->red.length = 8;
1280 var->green.offset = 8;
1281 var->green.length = 8;
1282 var->blue.offset = 0;
1283 var->blue.length = 8;
1284 var->transp.offset = 0;
1285 var->transp.length = 0;
1286 } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
1287 var->bits_per_pixel = 32;
1288 var->red.offset = 16;
1289 var->red.length = 8;
1290 var->green.offset = 8;
1291 var->green.length = 8;
1292 var->blue.offset = 0;
1293 var->blue.length = 8;
1294 var->transp.offset = 24;
1295 var->transp.length = 8;
1296 } else
1297 return -EINVAL;
1298
1299 var->red.msb_right = 0;
1300 var->green.msb_right = 0;
1301 var->blue.msb_right = 0;
1302 var->transp.msb_right = 0;
1303 }
Laurent Pinchart03862192011-08-31 13:00:53 +02001304
1305 /* Make sure we don't exceed our allocated memory. */
1306 if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
1307 info->fix.smem_len)
1308 return -EINVAL;
1309
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001310 /* only accept the forced_fourcc for dual channel configurations */
1311 if (p->forced_fourcc &&
1312 p->forced_fourcc != sh_mobile_format_fourcc(var))
Magnus Damm417d4822011-01-05 10:21:00 +00001313 return -EINVAL;
1314
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001315 return 0;
1316}
Phil Edworthy40331b22010-02-15 13:57:49 +00001317
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001318static int sh_mobile_set_par(struct fb_info *info)
1319{
1320 struct sh_mobile_lcdc_chan *ch = info->par;
1321 int ret;
1322
1323 sh_mobile_lcdc_stop(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001324
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001325 ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001326 ch->colorspace = info->var.colorspace;
1327
1328 ch->xres = info->var.xres;
1329 ch->xres_virtual = info->var.xres_virtual;
1330 ch->yres = info->var.yres;
1331 ch->yres_virtual = info->var.yres_virtual;
1332
1333 if (ch->format->yuv)
1334 ch->pitch = info->var.xres;
1335 else
1336 ch->pitch = info->var.xres * ch->format->bpp / 8;
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001337
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001338 ret = sh_mobile_lcdc_start(ch->lcdc);
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001339 if (ret < 0)
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001340 dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001341
1342 info->fix.line_length = ch->pitch;
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001343
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001344 if (sh_mobile_format_is_fourcc(&info->var)) {
1345 info->fix.type = FB_TYPE_FOURCC;
1346 info->fix.visual = FB_VISUAL_FOURCC;
1347 } else {
1348 info->fix.type = FB_TYPE_PACKED_PIXELS;
1349 info->fix.visual = FB_VISUAL_TRUECOLOR;
1350 }
1351
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001352 return ret;
1353}
1354
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001355/*
1356 * Screen blanking. Behavior is as follows:
1357 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1358 * FB_BLANK_NORMAL: screen blanked, clocks enabled
1359 * FB_BLANK_VSYNC,
1360 * FB_BLANK_HSYNC,
1361 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1362 */
1363static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
1364{
1365 struct sh_mobile_lcdc_chan *ch = info->par;
1366 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1367
1368 /* blank the screen? */
1369 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
1370 struct fb_fillrect rect = {
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001371 .width = ch->xres,
1372 .height = ch->yres,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001373 };
1374 sh_mobile_lcdc_fillrect(info, &rect);
1375 }
1376 /* turn clocks on? */
1377 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
1378 sh_mobile_lcdc_clk_on(p);
1379 }
1380 /* turn clocks off? */
1381 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
1382 /* make sure the screen is updated with the black fill before
1383 * switching the clocks off. one vsync is not enough since
1384 * blanking may occur in the middle of a refresh. deferred io
1385 * mode will reenable the clocks and update the screen in time,
1386 * so it does not need this. */
1387 if (!info->fbdefio) {
Laurent Pinchart49766772011-11-30 23:07:30 +01001388 sh_mobile_wait_for_vsync(ch);
1389 sh_mobile_wait_for_vsync(ch);
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001390 }
1391 sh_mobile_lcdc_clk_off(p);
1392 }
1393
1394 ch->blank_status = blank;
1395 return 0;
1396}
1397
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001398static struct fb_ops sh_mobile_lcdc_ops = {
Phil Edworthy9dd38812009-09-15 12:00:18 +00001399 .owner = THIS_MODULE,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001400 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
Magnus Damm2540c112008-12-17 17:29:49 +09001401 .fb_read = fb_sys_read,
1402 .fb_write = fb_sys_write,
Magnus Damm85645572008-12-19 15:34:41 +09001403 .fb_fillrect = sh_mobile_lcdc_fillrect,
1404 .fb_copyarea = sh_mobile_lcdc_copyarea,
1405 .fb_imageblit = sh_mobile_lcdc_imageblit,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001406 .fb_blank = sh_mobile_lcdc_blank,
Phil Edworthy9dd38812009-09-15 12:00:18 +00001407 .fb_pan_display = sh_mobile_fb_pan_display,
Phil Edworthy40331b22010-02-15 13:57:49 +00001408 .fb_ioctl = sh_mobile_ioctl,
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001409 .fb_open = sh_mobile_open,
1410 .fb_release = sh_mobile_release,
1411 .fb_check_var = sh_mobile_check_var,
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001412 .fb_set_par = sh_mobile_set_par,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001413};
1414
Laurent Pincharta67f3792011-11-29 14:37:35 +01001415static void
1416sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
1417{
1418 if (ch->info && ch->info->dev)
1419 unregister_framebuffer(ch->info);
1420}
1421
1422static int __devinit
1423sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
1424{
1425 struct fb_info *info = ch->info;
1426 int ret;
1427
1428 if (info->fbdefio) {
1429 ch->sglist = vmalloc(sizeof(struct scatterlist) *
1430 ch->fb_size >> PAGE_SHIFT);
1431 if (!ch->sglist) {
1432 dev_err(ch->lcdc->dev, "cannot allocate sglist\n");
1433 return -ENOMEM;
1434 }
1435 }
1436
1437 info->bl_dev = ch->bl;
1438
1439 ret = register_framebuffer(info);
1440 if (ret < 0)
1441 return ret;
1442
1443 dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001444 dev_name(ch->lcdc->dev), (ch->cfg->chan == LCDC_CHAN_MAINLCD) ?
Laurent Pincharta67f3792011-11-29 14:37:35 +01001445 "mainlcd" : "sublcd", info->var.xres, info->var.yres,
1446 info->var.bits_per_pixel);
1447
1448 /* deferred io mode: disable clock to save power */
1449 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
1450 sh_mobile_lcdc_clk_off(ch->lcdc);
1451
1452 return ret;
1453}
1454
1455static void
1456sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
1457{
1458 struct fb_info *info = ch->info;
1459
1460 if (!info || !info->device)
1461 return;
1462
1463 if (ch->sglist)
1464 vfree(ch->sglist);
1465
1466 fb_dealloc_cmap(&info->cmap);
1467 framebuffer_release(info);
1468}
1469
1470static int __devinit
1471sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
1472 const struct fb_videomode *mode,
1473 unsigned int num_modes)
1474{
1475 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
1476 struct fb_var_screeninfo *var;
1477 struct fb_info *info;
1478 int ret;
1479
1480 /* Allocate and initialize the frame buffer device. Create the modes
1481 * list and allocate the color map.
1482 */
1483 info = framebuffer_alloc(0, priv->dev);
1484 if (info == NULL) {
1485 dev_err(priv->dev, "unable to allocate fb_info\n");
1486 return -ENOMEM;
1487 }
1488
1489 ch->info = info;
1490
1491 info->flags = FBINFO_FLAG_DEFAULT;
1492 info->fbops = &sh_mobile_lcdc_ops;
1493 info->device = priv->dev;
1494 info->screen_base = ch->fb_mem;
1495 info->pseudo_palette = &ch->pseudo_palette;
1496 info->par = ch;
1497
1498 fb_videomode_to_modelist(mode, num_modes, &info->modelist);
1499
1500 ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1501 if (ret < 0) {
1502 dev_err(priv->dev, "unable to allocate cmap\n");
1503 return ret;
1504 }
1505
1506 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1507 * for NV12 and NV21.
1508 */
1509 info->fix = sh_mobile_lcdc_fix;
1510 info->fix.smem_start = ch->dma_handle;
1511 info->fix.smem_len = ch->fb_size;
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001512 info->fix.line_length = ch->pitch;
1513
1514 if (ch->format->yuv)
1515 info->fix.visual = FB_VISUAL_FOURCC;
1516 else
1517 info->fix.visual = FB_VISUAL_TRUECOLOR;
1518
Laurent Pincharta67f3792011-11-29 14:37:35 +01001519 if (ch->format->fourcc == V4L2_PIX_FMT_NV12 ||
1520 ch->format->fourcc == V4L2_PIX_FMT_NV21)
1521 info->fix.ypanstep = 2;
1522
1523 /* Initialize variable screen information using the first mode as
1524 * default. The default Y virtual resolution is twice the panel size to
1525 * allow for double-buffering.
1526 */
1527 var = &info->var;
1528 fb_videomode_to_var(var, mode);
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001529 var->width = ch->cfg->panel_cfg.width;
1530 var->height = ch->cfg->panel_cfg.height;
Laurent Pincharta67f3792011-11-29 14:37:35 +01001531 var->yres_virtual = var->yres * 2;
1532 var->activate = FB_ACTIVATE_NOW;
1533
1534 /* Use the legacy API by default for RGB formats, and the FOURCC API
1535 * for YUV formats.
1536 */
1537 if (!ch->format->yuv)
1538 var->bits_per_pixel = ch->format->bpp;
1539 else
1540 var->grayscale = ch->format->fourcc;
1541
1542 ret = sh_mobile_check_var(var, info);
1543 if (ret)
1544 return ret;
1545
Laurent Pincharta67f3792011-11-29 14:37:35 +01001546 return 0;
1547}
1548
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001549/* -----------------------------------------------------------------------------
1550 * Backlight
1551 */
1552
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001553static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
1554{
1555 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001556 int brightness = bdev->props.brightness;
1557
1558 if (bdev->props.power != FB_BLANK_UNBLANK ||
1559 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
1560 brightness = 0;
1561
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001562 return ch->cfg->bl_info.set_brightness(brightness);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001563}
1564
1565static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
1566{
1567 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001568
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001569 return ch->cfg->bl_info.get_brightness();
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001570}
1571
1572static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
1573 struct fb_info *info)
1574{
1575 return (info->bl_dev == bdev);
1576}
1577
1578static struct backlight_ops sh_mobile_lcdc_bl_ops = {
1579 .options = BL_CORE_SUSPENDRESUME,
1580 .update_status = sh_mobile_lcdc_update_bl,
1581 .get_brightness = sh_mobile_lcdc_get_brightness,
1582 .check_fb = sh_mobile_lcdc_check_fb,
1583};
1584
1585static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
1586 struct sh_mobile_lcdc_chan *ch)
1587{
1588 struct backlight_device *bl;
1589
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001590 bl = backlight_device_register(ch->cfg->bl_info.name, parent, ch,
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001591 &sh_mobile_lcdc_bl_ops, NULL);
Dan Carpenterbeee1f22011-03-21 15:03:13 +00001592 if (IS_ERR(bl)) {
1593 dev_err(parent, "unable to register backlight device: %ld\n",
1594 PTR_ERR(bl));
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001595 return NULL;
1596 }
1597
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001598 bl->props.max_brightness = ch->cfg->bl_info.max_brightness;
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001599 bl->props.brightness = bl->props.max_brightness;
1600 backlight_update_status(bl);
1601
1602 return bl;
1603}
1604
1605static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
1606{
1607 backlight_device_unregister(bdev);
1608}
1609
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001610/* -----------------------------------------------------------------------------
1611 * Power management
1612 */
1613
Magnus Damm2feb0752009-03-13 15:36:55 +00001614static int sh_mobile_lcdc_suspend(struct device *dev)
1615{
1616 struct platform_device *pdev = to_platform_device(dev);
1617
1618 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
1619 return 0;
1620}
1621
1622static int sh_mobile_lcdc_resume(struct device *dev)
1623{
1624 struct platform_device *pdev = to_platform_device(dev);
1625
1626 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
1627}
1628
Magnus Damm0246c472009-08-14 10:49:08 +00001629static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
1630{
1631 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001632 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001633
1634 /* turn off LCDC hardware */
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001635 lcdc_write(priv, _LDCNT1R, 0);
1636
Magnus Damm0246c472009-08-14 10:49:08 +00001637 return 0;
1638}
1639
1640static int sh_mobile_lcdc_runtime_resume(struct device *dev)
1641{
1642 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001643 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00001644
Laurent Pinchart2427bb22011-07-13 12:13:47 +02001645 __sh_mobile_lcdc_start(priv);
Magnus Damm0246c472009-08-14 10:49:08 +00001646
1647 return 0;
1648}
1649
Alexey Dobriyan47145212009-12-14 18:00:08 -08001650static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
Magnus Damm2feb0752009-03-13 15:36:55 +00001651 .suspend = sh_mobile_lcdc_suspend,
1652 .resume = sh_mobile_lcdc_resume,
Magnus Damm0246c472009-08-14 10:49:08 +00001653 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
1654 .runtime_resume = sh_mobile_lcdc_runtime_resume,
Magnus Damm2feb0752009-03-13 15:36:55 +00001655};
1656
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001657/* -----------------------------------------------------------------------------
1658 * Framebuffer notifier
1659 */
1660
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001661/* locking: called with info->lock held */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001662static int sh_mobile_lcdc_notify(struct notifier_block *nb,
1663 unsigned long action, void *data)
1664{
1665 struct fb_event *event = data;
1666 struct fb_info *info = event->info;
1667 struct sh_mobile_lcdc_chan *ch = info->par;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001668
1669 if (&ch->lcdc->notifier != nb)
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001670 return NOTIFY_DONE;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001671
1672 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
1673 __func__, action, event->data);
1674
1675 switch(action) {
1676 case FB_EVENT_SUSPEND:
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001677 sh_mobile_lcdc_display_off(ch);
Guennadi Liakhovetskiafe417c2010-09-03 07:20:39 +00001678 sh_mobile_lcdc_stop(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001679 break;
1680 case FB_EVENT_RESUME:
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001681 mutex_lock(&ch->open_lock);
1682 sh_mobile_fb_reconfig(info);
1683 mutex_unlock(&ch->open_lock);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001684
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001685 sh_mobile_lcdc_display_on(ch);
Guennadi Liakhovetskiebe5e122011-05-05 16:33:40 +00001686 sh_mobile_lcdc_start(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001687 }
1688
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001689 return NOTIFY_OK;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001690}
1691
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001692/* -----------------------------------------------------------------------------
1693 * Probe/remove and driver init/exit
1694 */
1695
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001696static const struct fb_videomode default_720p __devinitconst = {
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001697 .name = "HDMI 720p",
1698 .xres = 1280,
1699 .yres = 720,
1700
1701 .left_margin = 220,
1702 .right_margin = 110,
1703 .hsync_len = 40,
1704
1705 .upper_margin = 20,
1706 .lower_margin = 5,
1707 .vsync_len = 5,
1708
1709 .pixclock = 13468,
1710 .refresh = 60,
1711 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
1712};
1713
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001714static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1715{
1716 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001717 int i;
1718
1719 fb_unregister_client(&priv->notifier);
1720
1721 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
Laurent Pincharta67f3792011-11-29 14:37:35 +01001722 sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001723
1724 sh_mobile_lcdc_stop(priv);
1725
1726 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001727 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001728
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02001729 if (ch->tx_dev) {
1730 ch->tx_dev->lcdc = NULL;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001731 module_put(ch->cfg->tx_dev->dev.driver->owner);
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02001732 }
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02001733
Laurent Pincharta67f3792011-11-29 14:37:35 +01001734 sh_mobile_lcdc_channel_fb_cleanup(ch);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001735
Laurent Pincharta67f3792011-11-29 14:37:35 +01001736 if (ch->fb_mem)
1737 dma_free_coherent(&pdev->dev, ch->fb_size,
1738 ch->fb_mem, ch->dma_handle);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001739 }
1740
1741 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1742 if (priv->ch[i].bl)
1743 sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
1744 }
1745
Laurent Pinchart4774c122011-09-07 15:47:07 +02001746 if (priv->dot_clk) {
1747 pm_runtime_disable(&pdev->dev);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001748 clk_put(priv->dot_clk);
Laurent Pinchart4774c122011-09-07 15:47:07 +02001749 }
Laurent Pinchartb4bee692011-08-31 13:00:57 +02001750
1751 if (priv->base)
1752 iounmap(priv->base);
1753
1754 if (priv->irq)
1755 free_irq(priv->irq, priv);
1756 kfree(priv);
1757 return 0;
1758}
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001759
Laurent Pinchart217e9c42011-09-07 11:59:00 +02001760static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001761{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001762 int interface_type = ch->cfg->interface_type;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001763
1764 switch (interface_type) {
1765 case RGB8:
1766 case RGB9:
1767 case RGB12A:
1768 case RGB12B:
1769 case RGB16:
1770 case RGB18:
1771 case RGB24:
1772 case SYS8A:
1773 case SYS8B:
1774 case SYS8C:
1775 case SYS8D:
1776 case SYS9:
1777 case SYS12:
1778 case SYS16A:
1779 case SYS16B:
1780 case SYS16C:
1781 case SYS18:
1782 case SYS24:
1783 break;
1784 default:
1785 return -EINVAL;
1786 }
1787
1788 /* SUBLCD only supports SYS interface */
1789 if (lcdc_chan_is_sublcd(ch)) {
1790 if (!(interface_type & LDMT1R_IFM))
1791 return -EINVAL;
1792
1793 interface_type &= ~LDMT1R_IFM;
1794 }
1795
1796 ch->ldmt1r_value = interface_type;
1797 return 0;
1798}
1799
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001800static int __devinit
1801sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv *priv,
1802 struct sh_mobile_lcdc_chan *ch)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001803{
Laurent Pinchart105784b2011-11-29 15:58:10 +01001804 const struct sh_mobile_lcdc_format_info *format;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001805 const struct sh_mobile_lcdc_chan_cfg *cfg = ch->cfg;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001806 const struct fb_videomode *max_mode;
1807 const struct fb_videomode *mode;
Laurent Pincharta67f3792011-11-29 14:37:35 +01001808 unsigned int num_modes;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001809 unsigned int max_size;
Laurent Pincharta67f3792011-11-29 14:37:35 +01001810 unsigned int i;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001811
Laurent Pincharta67472a2011-08-31 13:00:59 +02001812 mutex_init(&ch->open_lock);
Laurent Pinchartecd29942011-09-18 14:14:46 +02001813 ch->notify = sh_mobile_lcdc_display_notify;
Laurent Pincharta67472a2011-08-31 13:00:59 +02001814
Laurent Pinchart105784b2011-11-29 15:58:10 +01001815 /* Validate the format. */
1816 format = sh_mobile_format_info(cfg->fourcc);
1817 if (format == NULL) {
1818 dev_err(priv->dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
1819 return -EINVAL;
1820 }
1821
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001822 /* Iterate through the modes to validate them and find the highest
1823 * resolution.
1824 */
1825 max_mode = NULL;
1826 max_size = 0;
1827
Laurent Pinchart93ff2592011-11-29 14:33:41 +01001828 for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001829 unsigned int size = mode->yres * mode->xres;
1830
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001831 /* NV12/NV21 buffers must have even number of lines */
1832 if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
1833 cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001834 dev_err(priv->dev, "yres must be multiple of 2 for "
1835 "YCbCr420 mode.\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001836 return -EINVAL;
1837 }
1838
1839 if (size > max_size) {
1840 max_mode = mode;
1841 max_size = size;
1842 }
1843 }
1844
1845 if (!max_size)
1846 max_size = MAX_XRES * MAX_YRES;
1847 else
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001848 dev_dbg(priv->dev, "Found largest videomode %ux%u\n",
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001849 max_mode->xres, max_mode->yres);
1850
Laurent Pinchart93ff2592011-11-29 14:33:41 +01001851 if (cfg->lcd_modes == NULL) {
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001852 mode = &default_720p;
Laurent Pinchart93ff2592011-11-29 14:33:41 +01001853 num_modes = 1;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001854 } else {
Laurent Pinchart93ff2592011-11-29 14:33:41 +01001855 mode = cfg->lcd_modes;
1856 num_modes = cfg->num_modes;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001857 }
1858
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001859 /* Use the first mode as default. */
1860 ch->format = format;
1861 ch->xres = mode->xres;
1862 ch->xres_virtual = mode->xres;
1863 ch->yres = mode->yres;
1864 ch->yres_virtual = mode->yres * 2;
1865
1866 if (!format->yuv) {
1867 ch->colorspace = V4L2_COLORSPACE_SRGB;
1868 ch->pitch = ch->xres * format->bpp / 8;
1869 } else {
1870 ch->colorspace = V4L2_COLORSPACE_REC709;
1871 ch->pitch = ch->xres;
1872 }
1873
Laurent Pincharta67f3792011-11-29 14:37:35 +01001874 ch->display.width = cfg->panel_cfg.width;
1875 ch->display.height = cfg->panel_cfg.height;
1876 ch->display.mode = *mode;
1877
1878 /* Allocate frame buffer memory. */
1879 ch->fb_size = max_size * format->bpp / 8 * 2;
1880 ch->fb_mem = dma_alloc_coherent(priv->dev, ch->fb_size, &ch->dma_handle,
1881 GFP_KERNEL);
1882 if (ch->fb_mem == NULL) {
1883 dev_err(priv->dev, "unable to allocate buffer\n");
1884 return -ENOMEM;
1885 }
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001886
Laurent Pinchart13f80ee2011-11-29 01:46:12 +01001887 /* Initialize the transmitter device if present. */
1888 if (cfg->tx_dev) {
1889 if (!cfg->tx_dev->dev.driver ||
1890 !try_module_get(cfg->tx_dev->dev.driver->owner)) {
1891 dev_warn(priv->dev,
1892 "unable to get transmitter device\n");
1893 return -EINVAL;
1894 }
1895 ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
1896 ch->tx_dev->lcdc = ch;
1897 ch->tx_dev->def_mode = *mode;
1898 }
1899
Laurent Pincharta67f3792011-11-29 14:37:35 +01001900 return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001901}
1902
Uwe Kleine-Königc2e13032010-02-04 20:56:51 +01001903static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001904{
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001905 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001906 struct sh_mobile_lcdc_priv *priv;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001907 struct resource *res;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001908 int num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001909 int error;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001910 int i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001911
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001912 if (!pdata) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001913 dev_err(&pdev->dev, "no platform data defined\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001914 return -EINVAL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001915 }
1916
1917 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Damm85645572008-12-19 15:34:41 +09001918 i = platform_get_irq(pdev, 0);
1919 if (!res || i < 0) {
1920 dev_err(&pdev->dev, "cannot get platform resources\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001921 return -ENOENT;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001922 }
1923
1924 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1925 if (!priv) {
1926 dev_err(&pdev->dev, "cannot allocate device data\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001927 return -ENOMEM;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001928 }
1929
Laurent Pinchart4774c122011-09-07 15:47:07 +02001930 priv->dev = &pdev->dev;
1931 priv->meram_dev = pdata->meram_dev;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001932 platform_set_drvdata(pdev, priv);
1933
Yong Zhangf8798cc2011-09-22 16:59:16 +08001934 error = request_irq(i, sh_mobile_lcdc_irq, 0,
Kay Sievers7ad33e72009-03-24 16:38:21 -07001935 dev_name(&pdev->dev), priv);
Magnus Damm85645572008-12-19 15:34:41 +09001936 if (error) {
1937 dev_err(&pdev->dev, "unable to request irq\n");
1938 goto err1;
1939 }
1940
1941 priv->irq = i;
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +00001942 atomic_set(&priv->hw_usecnt, -1);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001943
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001944 for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
1945 struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001946
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001947 ch->lcdc = priv;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001948 ch->cfg = &pdata->ch[i];
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001949
1950 error = sh_mobile_lcdc_check_interface(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001951 if (error) {
1952 dev_err(&pdev->dev, "unsupported interface type\n");
1953 goto err1;
1954 }
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001955 init_waitqueue_head(&ch->frame_end_wait);
1956 init_completion(&ch->vsync_completion);
1957 ch->pan_offset = 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001958
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001959 /* probe the backlight is there is one defined */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001960 if (ch->cfg->bl_info.max_brightness)
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001961 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
1962
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001963 switch (pdata->ch[i].chan) {
1964 case LCDC_CHAN_MAINLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001965 ch->enabled = LDCNT2R_ME;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001966 ch->reg_offs = lcdc_offs_mainlcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001967 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001968 break;
1969 case LCDC_CHAN_SUBLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001970 ch->enabled = LDCNT2R_SE;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001971 ch->reg_offs = lcdc_offs_sublcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001972 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001973 break;
1974 }
1975 }
1976
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001977 if (!num_channels) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001978 dev_err(&pdev->dev, "no channels defined\n");
1979 error = -EINVAL;
1980 goto err1;
1981 }
1982
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001983 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02001984 if (num_channels == 2)
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001985 priv->forced_fourcc = pdata->ch[0].fourcc;
Magnus Damm417d4822011-01-05 10:21:00 +00001986
Guennadi Liakhovetskidba6f382010-06-30 09:26:35 +00001987 priv->base = ioremap_nocache(res->start, resource_size(res));
1988 if (!priv->base)
1989 goto err1;
1990
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02001991 error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001992 if (error) {
1993 dev_err(&pdev->dev, "unable to setup clocks\n");
1994 goto err1;
1995 }
1996
Laurent Pinchart4774c122011-09-07 15:47:07 +02001997 /* Enable runtime PM. */
1998 pm_runtime_enable(&pdev->dev);
Damian7caa4342011-05-18 11:10:07 +00001999
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002000 for (i = 0; i < num_channels; i++) {
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00002001 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002002
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02002003 error = sh_mobile_lcdc_channel_init(priv, ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002004 if (error)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002005 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002006 }
2007
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002008 error = sh_mobile_lcdc_start(priv);
2009 if (error) {
2010 dev_err(&pdev->dev, "unable to start hardware\n");
2011 goto err1;
2012 }
2013
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002014 for (i = 0; i < num_channels; i++) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00002015 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Paul Mundt1c6a3072009-07-01 06:50:31 +00002016
Laurent Pincharta67f3792011-11-29 14:37:35 +01002017 error = sh_mobile_lcdc_channel_fb_register(ch);
2018 if (error)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002019 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002020 }
2021
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002022 /* Failure ignored */
2023 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
2024 fb_register_client(&priv->notifier);
2025
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002026 return 0;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002027err1:
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002028 sh_mobile_lcdc_remove(pdev);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002029
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002030 return error;
2031}
2032
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002033static struct platform_driver sh_mobile_lcdc_driver = {
2034 .driver = {
2035 .name = "sh_mobile_lcdc_fb",
2036 .owner = THIS_MODULE,
Magnus Damm2feb0752009-03-13 15:36:55 +00002037 .pm = &sh_mobile_lcdc_dev_pm_ops,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002038 },
2039 .probe = sh_mobile_lcdc_probe,
2040 .remove = sh_mobile_lcdc_remove,
2041};
2042
Axel Lin4277f2c2011-11-26 10:25:54 +08002043module_platform_driver(sh_mobile_lcdc_driver);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002044
2045MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
2046MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
2047MODULE_LICENSE("GPL v2");