blob: 98e81b31fbc47227d996606e2476a597cdef9e7c [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>
Laurent Pinchartc5deac32011-12-12 18:43:16 +010015#include <linux/ctype.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070016#include <linux/dma-mapping.h>
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020017#include <linux/delay.h>
18#include <linux/gpio.h>
19#include <linux/init.h>
Magnus Damm85645572008-12-19 15:34:41 +090020#include <linux/interrupt.h>
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020021#include <linux/ioctl.h>
22#include <linux/kernel.h>
23#include <linux/mm.h>
24#include <linux/module.h>
25#include <linux/platform_device.h>
26#include <linux/pm_runtime.h>
27#include <linux/slab.h>
Laurent Pinchartedd153a2011-12-13 14:02:28 +010028#include <linux/videodev2.h>
Paul Mundt1c6a3072009-07-01 06:50:31 +000029#include <linux/vmalloc.h>
Laurent Pinchartf1f60b52011-09-07 11:09:26 +020030
Paul Mundt225c9a82008-10-01 16:24:32 +090031#include <video/sh_mobile_lcdc.h>
Laurent Pinchart8a209742011-07-13 12:13:47 +020032#include <video/sh_mobile_meram.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070033
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +000034#include "sh_mobile_lcdcfb.h"
35
Laurent Pinchartc5deac32011-12-12 18:43:16 +010036/* ----------------------------------------------------------------------------
37 * Overlay register definitions
38 */
39
40#define LDBCR 0xb00
41#define LDBCR_UPC(n) (1 << ((n) + 16))
42#define LDBCR_UPF(n) (1 << ((n) + 8))
43#define LDBCR_UPD(n) (1 << ((n) + 0))
44#define LDBnBSIFR(n) (0xb20 + (n) * 0x20 + 0x00)
45#define LDBBSIFR_EN (1 << 31)
46#define LDBBSIFR_VS (1 << 29)
47#define LDBBSIFR_BRSEL (1 << 28)
48#define LDBBSIFR_MX (1 << 27)
49#define LDBBSIFR_MY (1 << 26)
50#define LDBBSIFR_CV3 (3 << 24)
51#define LDBBSIFR_CV2 (2 << 24)
52#define LDBBSIFR_CV1 (1 << 24)
53#define LDBBSIFR_CV0 (0 << 24)
54#define LDBBSIFR_CV_MASK (3 << 24)
55#define LDBBSIFR_LAY_MASK (0xff << 16)
56#define LDBBSIFR_LAY_SHIFT 16
57#define LDBBSIFR_ROP3_MASK (0xff << 16)
58#define LDBBSIFR_ROP3_SHIFT 16
59#define LDBBSIFR_AL_PL8 (3 << 14)
60#define LDBBSIFR_AL_PL1 (2 << 14)
61#define LDBBSIFR_AL_PK (1 << 14)
62#define LDBBSIFR_AL_1 (0 << 14)
63#define LDBBSIFR_AL_MASK (3 << 14)
64#define LDBBSIFR_SWPL (1 << 10)
65#define LDBBSIFR_SWPW (1 << 9)
66#define LDBBSIFR_SWPB (1 << 8)
67#define LDBBSIFR_RY (1 << 7)
68#define LDBBSIFR_CHRR_420 (2 << 0)
69#define LDBBSIFR_CHRR_422 (1 << 0)
70#define LDBBSIFR_CHRR_444 (0 << 0)
71#define LDBBSIFR_RPKF_ARGB32 (0x00 << 0)
72#define LDBBSIFR_RPKF_RGB16 (0x03 << 0)
73#define LDBBSIFR_RPKF_RGB24 (0x0b << 0)
74#define LDBBSIFR_RPKF_MASK (0x1f << 0)
75#define LDBnBSSZR(n) (0xb20 + (n) * 0x20 + 0x04)
76#define LDBBSSZR_BVSS_MASK (0xfff << 16)
77#define LDBBSSZR_BVSS_SHIFT 16
78#define LDBBSSZR_BHSS_MASK (0xfff << 0)
79#define LDBBSSZR_BHSS_SHIFT 0
80#define LDBnBLOCR(n) (0xb20 + (n) * 0x20 + 0x08)
81#define LDBBLOCR_CVLC_MASK (0xfff << 16)
82#define LDBBLOCR_CVLC_SHIFT 16
83#define LDBBLOCR_CHLC_MASK (0xfff << 0)
84#define LDBBLOCR_CHLC_SHIFT 0
85#define LDBnBSMWR(n) (0xb20 + (n) * 0x20 + 0x0c)
86#define LDBBSMWR_BSMWA_MASK (0xffff << 16)
87#define LDBBSMWR_BSMWA_SHIFT 16
88#define LDBBSMWR_BSMW_MASK (0xffff << 0)
89#define LDBBSMWR_BSMW_SHIFT 0
90#define LDBnBSAYR(n) (0xb20 + (n) * 0x20 + 0x10)
91#define LDBBSAYR_FG1A_MASK (0xff << 24)
92#define LDBBSAYR_FG1A_SHIFT 24
93#define LDBBSAYR_FG1R_MASK (0xff << 16)
94#define LDBBSAYR_FG1R_SHIFT 16
95#define LDBBSAYR_FG1G_MASK (0xff << 8)
96#define LDBBSAYR_FG1G_SHIFT 8
97#define LDBBSAYR_FG1B_MASK (0xff << 0)
98#define LDBBSAYR_FG1B_SHIFT 0
99#define LDBnBSACR(n) (0xb20 + (n) * 0x20 + 0x14)
100#define LDBBSACR_FG2A_MASK (0xff << 24)
101#define LDBBSACR_FG2A_SHIFT 24
102#define LDBBSACR_FG2R_MASK (0xff << 16)
103#define LDBBSACR_FG2R_SHIFT 16
104#define LDBBSACR_FG2G_MASK (0xff << 8)
105#define LDBBSACR_FG2G_SHIFT 8
106#define LDBBSACR_FG2B_MASK (0xff << 0)
107#define LDBBSACR_FG2B_SHIFT 0
108#define LDBnBSAAR(n) (0xb20 + (n) * 0x20 + 0x18)
109#define LDBBSAAR_AP_MASK (0xff << 24)
110#define LDBBSAAR_AP_SHIFT 24
111#define LDBBSAAR_R_MASK (0xff << 16)
112#define LDBBSAAR_R_SHIFT 16
113#define LDBBSAAR_GY_MASK (0xff << 8)
114#define LDBBSAAR_GY_SHIFT 8
115#define LDBBSAAR_B_MASK (0xff << 0)
116#define LDBBSAAR_B_SHIFT 0
117#define LDBnBPPCR(n) (0xb20 + (n) * 0x20 + 0x1c)
118#define LDBBPPCR_AP_MASK (0xff << 24)
119#define LDBBPPCR_AP_SHIFT 24
120#define LDBBPPCR_R_MASK (0xff << 16)
121#define LDBBPPCR_R_SHIFT 16
122#define LDBBPPCR_GY_MASK (0xff << 8)
123#define LDBBPPCR_GY_SHIFT 8
124#define LDBBPPCR_B_MASK (0xff << 0)
125#define LDBBPPCR_B_SHIFT 0
126#define LDBnBBGCL(n) (0xb10 + (n) * 0x04)
127#define LDBBBGCL_BGA_MASK (0xff << 24)
128#define LDBBBGCL_BGA_SHIFT 24
129#define LDBBBGCL_BGR_MASK (0xff << 16)
130#define LDBBBGCL_BGR_SHIFT 16
131#define LDBBBGCL_BGG_MASK (0xff << 8)
132#define LDBBBGCL_BGG_SHIFT 8
133#define LDBBBGCL_BGB_MASK (0xff << 0)
134#define LDBBBGCL_BGB_SHIFT 0
135
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000136#define SIDE_B_OFFSET 0x1000
137#define MIRROR_OFFSET 0x2000
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700138
Guennadi Liakhovetskid2ecbab2010-11-04 11:06:06 +0000139#define MAX_XRES 1920
140#define MAX_YRES 1080
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700141
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100142enum sh_mobile_lcdc_overlay_mode {
143 LCDC_OVERLAY_BLEND,
144 LCDC_OVERLAY_ROP3,
145};
146
147/*
148 * struct sh_mobile_lcdc_overlay - LCDC display overlay
149 *
150 * @channel: LCDC channel this overlay belongs to
151 * @cfg: Overlay configuration
152 * @info: Frame buffer device
153 * @index: Overlay index (0-3)
154 * @base: Overlay registers base address
155 * @enabled: True if the overlay is enabled
156 * @mode: Overlay blending mode (alpha blend or ROP3)
157 * @alpha: Global alpha blending value (0-255, for alpha blending mode)
158 * @rop3: Raster operation (for ROP3 mode)
159 * @fb_mem: Frame buffer virtual memory address
160 * @fb_size: Frame buffer size in bytes
161 * @dma_handle: Frame buffer DMA address
162 * @base_addr_y: Overlay base address (RGB or luma component)
163 * @base_addr_c: Overlay base address (chroma component)
164 * @pan_offset: Current pan offset in bytes
165 * @format: Current pixelf format
166 * @xres: Horizontal visible resolution
167 * @xres_virtual: Horizontal total resolution
168 * @yres: Vertical visible resolution
169 * @yres_virtual: Vertical total resolution
170 * @pitch: Overlay line pitch
171 * @pos_x: Horizontal overlay position
172 * @pos_y: Vertical overlay position
173 */
174struct sh_mobile_lcdc_overlay {
175 struct sh_mobile_lcdc_chan *channel;
176
177 const struct sh_mobile_lcdc_overlay_cfg *cfg;
178 struct fb_info *info;
179
180 unsigned int index;
181 unsigned long base;
182
183 bool enabled;
184 enum sh_mobile_lcdc_overlay_mode mode;
185 unsigned int alpha;
186 unsigned int rop3;
187
188 void *fb_mem;
189 unsigned long fb_size;
190
191 dma_addr_t dma_handle;
192 unsigned long base_addr_y;
193 unsigned long base_addr_c;
194 unsigned long pan_offset;
195
196 const struct sh_mobile_lcdc_format_info *format;
197 unsigned int xres;
198 unsigned int xres_virtual;
199 unsigned int yres;
200 unsigned int yres_virtual;
201 unsigned int pitch;
202 int pos_x;
203 int pos_y;
204};
205
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200206struct sh_mobile_lcdc_priv {
207 void __iomem *base;
208 int irq;
209 atomic_t hw_usecnt;
210 struct device *dev;
211 struct clk *dot_clk;
212 unsigned long lddckr;
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100213
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200214 struct sh_mobile_lcdc_chan ch[2];
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100215 struct sh_mobile_lcdc_overlay overlays[4];
216
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200217 struct notifier_block notifier;
218 int started;
219 int forced_fourcc; /* 2 channel LCDC must share fourcc setting */
220 struct sh_mobile_meram_info *meram_dev;
221};
222
223/* -----------------------------------------------------------------------------
224 * Registers access
225 */
226
Magnus Damm0246c472009-08-14 10:49:08 +0000227static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700228 [LDDCKPAT1R] = 0x400,
229 [LDDCKPAT2R] = 0x404,
230 [LDMT1R] = 0x418,
231 [LDMT2R] = 0x41c,
232 [LDMT3R] = 0x420,
233 [LDDFR] = 0x424,
234 [LDSM1R] = 0x428,
Magnus Damm85645572008-12-19 15:34:41 +0900235 [LDSM2R] = 0x42c,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700236 [LDSA1R] = 0x430,
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000237 [LDSA2R] = 0x434,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700238 [LDMLSR] = 0x438,
239 [LDHCNR] = 0x448,
240 [LDHSYNR] = 0x44c,
241 [LDVLNR] = 0x450,
242 [LDVSYNR] = 0x454,
243 [LDPMR] = 0x460,
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000244 [LDHAJR] = 0x4a0,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700245};
246
Magnus Damm0246c472009-08-14 10:49:08 +0000247static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700248 [LDDCKPAT1R] = 0x408,
249 [LDDCKPAT2R] = 0x40c,
250 [LDMT1R] = 0x600,
251 [LDMT2R] = 0x604,
252 [LDMT3R] = 0x608,
253 [LDDFR] = 0x60c,
254 [LDSM1R] = 0x610,
Magnus Damm85645572008-12-19 15:34:41 +0900255 [LDSM2R] = 0x614,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700256 [LDSA1R] = 0x618,
257 [LDMLSR] = 0x620,
258 [LDHCNR] = 0x624,
259 [LDHSYNR] = 0x628,
260 [LDVLNR] = 0x62c,
261 [LDVSYNR] = 0x630,
262 [LDPMR] = 0x63c,
263};
264
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000265static bool banked(int reg_nr)
266{
267 switch (reg_nr) {
268 case LDMT1R:
269 case LDMT2R:
270 case LDMT3R:
271 case LDDFR:
272 case LDSM1R:
273 case LDSA1R:
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000274 case LDSA2R:
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000275 case LDMLSR:
276 case LDHCNR:
277 case LDHSYNR:
278 case LDVLNR:
279 case LDVSYNR:
280 return true;
281 }
282 return false;
283}
284
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200285static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
286{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100287 return chan->cfg->chan == LCDC_CHAN_SUBLCD;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200288}
289
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700290static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
291 int reg_nr, unsigned long data)
292{
293 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000294 if (banked(reg_nr))
295 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
296 SIDE_B_OFFSET);
297}
298
299static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
300 int reg_nr, unsigned long data)
301{
302 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
303 MIRROR_OFFSET);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700304}
305
306static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
307 int reg_nr)
308{
309 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
310}
311
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100312static void lcdc_write_overlay(struct sh_mobile_lcdc_overlay *ovl,
313 int reg, unsigned long data)
314{
315 iowrite32(data, ovl->channel->lcdc->base + reg);
316 iowrite32(data, ovl->channel->lcdc->base + reg + SIDE_B_OFFSET);
317}
318
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700319static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
320 unsigned long reg_offs, unsigned long data)
321{
322 iowrite32(data, priv->base + reg_offs);
323}
324
325static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
326 unsigned long reg_offs)
327{
328 return ioread32(priv->base + reg_offs);
329}
330
331static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
332 unsigned long reg_offs,
333 unsigned long mask, unsigned long until)
334{
335 while ((lcdc_read(priv, reg_offs) & mask) != until)
336 cpu_relax();
337}
338
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200339/* -----------------------------------------------------------------------------
340 * Clock management
341 */
342
343static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700344{
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200345 if (atomic_inc_and_test(&priv->hw_usecnt)) {
346 if (priv->dot_clk)
347 clk_enable(priv->dot_clk);
348 pm_runtime_get_sync(priv->dev);
349 if (priv->meram_dev && priv->meram_dev->pdev)
350 pm_runtime_get_sync(&priv->meram_dev->pdev->dev);
351 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700352}
353
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200354static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
355{
356 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
357 if (priv->meram_dev && priv->meram_dev->pdev)
358 pm_runtime_put_sync(&priv->meram_dev->pdev->dev);
359 pm_runtime_put(priv->dev);
360 if (priv->dot_clk)
361 clk_disable(priv->dot_clk);
362 }
363}
364
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200365static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv *priv,
366 int clock_source)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200367{
Laurent Pinchart4774c122011-09-07 15:47:07 +0200368 struct clk *clk;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200369 char *str;
370
371 switch (clock_source) {
372 case LCDC_CLK_BUS:
373 str = "bus_clk";
374 priv->lddckr = LDDCKR_ICKSEL_BUS;
375 break;
376 case LCDC_CLK_PERIPHERAL:
377 str = "peripheral_clk";
378 priv->lddckr = LDDCKR_ICKSEL_MIPI;
379 break;
380 case LCDC_CLK_EXTERNAL:
381 str = NULL;
382 priv->lddckr = LDDCKR_ICKSEL_HDMI;
383 break;
384 default:
385 return -EINVAL;
386 }
387
Laurent Pinchart4774c122011-09-07 15:47:07 +0200388 if (str == NULL)
389 return 0;
390
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200391 clk = clk_get(priv->dev, str);
Laurent Pinchart4774c122011-09-07 15:47:07 +0200392 if (IS_ERR(clk)) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200393 dev_err(priv->dev, "cannot get dot clock %s\n", str);
Laurent Pinchart4774c122011-09-07 15:47:07 +0200394 return PTR_ERR(clk);
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200395 }
396
Laurent Pinchart4774c122011-09-07 15:47:07 +0200397 priv->dot_clk = clk;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200398 return 0;
399}
400
401/* -----------------------------------------------------------------------------
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200402 * Display, panel and deferred I/O
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200403 */
404
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700405static void lcdc_sys_write_index(void *handle, unsigned long data)
406{
407 struct sh_mobile_lcdc_chan *ch = handle;
408
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200409 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
410 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
411 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
412 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
413 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700414}
415
416static void lcdc_sys_write_data(void *handle, unsigned long data)
417{
418 struct sh_mobile_lcdc_chan *ch = handle;
419
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200420 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
421 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
422 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
423 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
424 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700425}
426
427static unsigned long lcdc_sys_read_data(void *handle)
428{
429 struct sh_mobile_lcdc_chan *ch = handle;
430
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200431 lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
432 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
433 lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
434 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700435 udelay(1);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200436 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700437
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200438 return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700439}
440
441struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
442 lcdc_sys_write_index,
443 lcdc_sys_write_data,
444 lcdc_sys_read_data,
445};
446
Paul Mundt1c6a3072009-07-01 06:50:31 +0000447static int sh_mobile_lcdc_sginit(struct fb_info *info,
448 struct list_head *pagelist)
449{
450 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchart58f03d92011-11-30 23:07:30 +0100451 unsigned int nr_pages_max = ch->fb_size >> PAGE_SHIFT;
Paul Mundt1c6a3072009-07-01 06:50:31 +0000452 struct page *page;
453 int nr_pages = 0;
454
455 sg_init_table(ch->sglist, nr_pages_max);
456
457 list_for_each_entry(page, pagelist, lru)
458 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
459
460 return nr_pages;
461}
462
Magnus Damm85645572008-12-19 15:34:41 +0900463static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
464 struct list_head *pagelist)
465{
466 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100467 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
Magnus Damm85645572008-12-19 15:34:41 +0900468
469 /* enable clocks before accessing hardware */
470 sh_mobile_lcdc_clk_on(ch->lcdc);
471
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900472 /*
473 * It's possible to get here without anything on the pagelist via
474 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
475 * invocation. In the former case, the acceleration routines are
476 * stepped in to when using the framebuffer console causing the
477 * workqueue to be scheduled without any dirty pages on the list.
478 *
479 * Despite this, a panel update is still needed given that the
480 * acceleration routines have their own methods for writing in
481 * that still need to be updated.
482 *
483 * The fsync() and empty pagelist case could be optimized for,
484 * but we don't bother, as any application exhibiting such
485 * behaviour is fundamentally broken anyways.
486 */
487 if (!list_empty(pagelist)) {
488 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
Paul Mundt1c6a3072009-07-01 06:50:31 +0000489
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900490 /* trigger panel update */
Laurent Pincharte8363142011-11-29 14:37:35 +0100491 dma_map_sg(ch->lcdc->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
Laurent Pinchartafaad832011-09-11 22:59:04 +0200492 if (panel->start_transfer)
493 panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200494 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
Laurent Pincharte8363142011-11-29 14:37:35 +0100495 dma_unmap_sg(ch->lcdc->dev, ch->sglist, nr_pages,
496 DMA_TO_DEVICE);
Magnus Dammef61aae2009-12-07 14:20:06 +0000497 } else {
Laurent Pinchartafaad832011-09-11 22:59:04 +0200498 if (panel->start_transfer)
499 panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200500 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
Magnus Dammef61aae2009-12-07 14:20:06 +0000501 }
Magnus Damm85645572008-12-19 15:34:41 +0900502}
503
504static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
505{
506 struct fb_deferred_io *fbdefio = info->fbdefio;
507
508 if (fbdefio)
509 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
510}
511
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200512static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan *ch)
513{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100514 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200515
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200516 if (ch->tx_dev) {
Laurent Pinchart458981c2011-11-28 23:19:59 +0100517 int ret;
518
519 ret = ch->tx_dev->ops->display_on(ch->tx_dev);
520 if (ret < 0)
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200521 return;
Laurent Pinchart458981c2011-11-28 23:19:59 +0100522
523 if (ret == SH_MOBILE_LCDC_DISPLAY_DISCONNECTED)
524 ch->info->state = FBINFO_STATE_SUSPENDED;
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200525 }
526
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200527 /* HDMI must be enabled before LCDC configuration */
Laurent Pinchartafaad832011-09-11 22:59:04 +0200528 if (panel->display_on)
529 panel->display_on();
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200530}
531
532static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan *ch)
533{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100534 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200535
Laurent Pinchartafaad832011-09-11 22:59:04 +0200536 if (panel->display_off)
537 panel->display_off();
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200538
539 if (ch->tx_dev)
540 ch->tx_dev->ops->display_off(ch->tx_dev);
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200541}
542
Laurent Pinchartecd29942011-09-18 14:14:46 +0200543static bool
544sh_mobile_lcdc_must_reconfigure(struct sh_mobile_lcdc_chan *ch,
Laurent Pincharte0c86012011-11-29 01:05:47 +0100545 const struct fb_videomode *new_mode)
Laurent Pinchartecd29942011-09-18 14:14:46 +0200546{
Laurent Pinchartecd29942011-09-18 14:14:46 +0200547 dev_dbg(ch->info->dev, "Old %ux%u, new %ux%u\n",
Laurent Pinchart2d045592011-11-29 13:42:48 +0100548 ch->display.mode.xres, ch->display.mode.yres,
549 new_mode->xres, new_mode->yres);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200550
Laurent Pincharte0c86012011-11-29 01:05:47 +0100551 /* It can be a different monitor with an equal video-mode */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100552 if (fb_mode_is_equal(&ch->display.mode, new_mode))
Laurent Pinchartecd29942011-09-18 14:14:46 +0200553 return false;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200554
555 dev_dbg(ch->info->dev, "Switching %u -> %u lines\n",
Laurent Pinchart2d045592011-11-29 13:42:48 +0100556 ch->display.mode.yres, new_mode->yres);
557 ch->display.mode = *new_mode;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200558
559 return true;
560}
561
Laurent Pinchartd7ad3342011-11-22 00:56:58 +0100562static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
563 struct fb_info *info);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200564
565static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan *ch,
566 enum sh_mobile_lcdc_entity_event event,
Laurent Pincharte0c86012011-11-29 01:05:47 +0100567 const struct fb_videomode *mode,
568 const struct fb_monspecs *monspec)
Laurent Pinchartecd29942011-09-18 14:14:46 +0200569{
570 struct fb_info *info = ch->info;
Laurent Pincharte0c86012011-11-29 01:05:47 +0100571 struct fb_var_screeninfo var;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200572 int ret = 0;
573
574 switch (event) {
575 case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT:
576 /* HDMI plug in */
577 if (lock_fb_info(info)) {
578 console_lock();
579
Laurent Pinchart2d045592011-11-29 13:42:48 +0100580 ch->display.width = monspec->max_x * 10;
581 ch->display.height = monspec->max_y * 10;
Laurent Pincharte0c86012011-11-29 01:05:47 +0100582
583 if (!sh_mobile_lcdc_must_reconfigure(ch, mode) &&
Laurent Pinchartecd29942011-09-18 14:14:46 +0200584 info->state == FBINFO_STATE_RUNNING) {
585 /* First activation with the default monitor.
586 * Just turn on, if we run a resume here, the
587 * logo disappears.
588 */
Laurent Pincharte0c86012011-11-29 01:05:47 +0100589 info->var.width = monspec->max_x * 10;
590 info->var.height = monspec->max_y * 10;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200591 sh_mobile_lcdc_display_on(ch);
592 } else {
593 /* New monitor or have to wake up */
594 fb_set_suspend(info, 0);
595 }
596
597 console_unlock();
598 unlock_fb_info(info);
599 }
600 break;
601
602 case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT:
603 /* HDMI disconnect */
604 if (lock_fb_info(info)) {
605 console_lock();
606 fb_set_suspend(info, 1);
607 console_unlock();
608 unlock_fb_info(info);
609 }
610 break;
611
612 case SH_MOBILE_LCDC_EVENT_DISPLAY_MODE:
613 /* Validate a proposed new mode */
Laurent Pincharte0c86012011-11-29 01:05:47 +0100614 fb_videomode_to_var(&var, mode);
615 var.bits_per_pixel = info->var.bits_per_pixel;
616 var.grayscale = info->var.grayscale;
Laurent Pinchartd7ad3342011-11-22 00:56:58 +0100617 ret = sh_mobile_lcdc_check_var(&var, info);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200618 break;
619 }
620
621 return ret;
622}
623
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200624/* -----------------------------------------------------------------------------
625 * Format helpers
626 */
627
Laurent Pinchart105784b2011-11-29 15:58:10 +0100628struct sh_mobile_lcdc_format_info {
629 u32 fourcc;
630 unsigned int bpp;
631 bool yuv;
632 u32 lddfr;
633};
634
635static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos[] = {
636 {
637 .fourcc = V4L2_PIX_FMT_RGB565,
638 .bpp = 16,
639 .yuv = false,
640 .lddfr = LDDFR_PKF_RGB16,
641 }, {
642 .fourcc = V4L2_PIX_FMT_BGR24,
643 .bpp = 24,
644 .yuv = false,
645 .lddfr = LDDFR_PKF_RGB24,
646 }, {
647 .fourcc = V4L2_PIX_FMT_BGR32,
648 .bpp = 32,
649 .yuv = false,
650 .lddfr = LDDFR_PKF_ARGB32,
651 }, {
652 .fourcc = V4L2_PIX_FMT_NV12,
653 .bpp = 12,
654 .yuv = true,
655 .lddfr = LDDFR_CC | LDDFR_YF_420,
656 }, {
657 .fourcc = V4L2_PIX_FMT_NV21,
658 .bpp = 12,
659 .yuv = true,
660 .lddfr = LDDFR_CC | LDDFR_YF_420,
661 }, {
662 .fourcc = V4L2_PIX_FMT_NV16,
663 .bpp = 16,
664 .yuv = true,
665 .lddfr = LDDFR_CC | LDDFR_YF_422,
666 }, {
667 .fourcc = V4L2_PIX_FMT_NV61,
668 .bpp = 16,
669 .yuv = true,
670 .lddfr = LDDFR_CC | LDDFR_YF_422,
671 }, {
672 .fourcc = V4L2_PIX_FMT_NV24,
673 .bpp = 24,
674 .yuv = true,
675 .lddfr = LDDFR_CC | LDDFR_YF_444,
676 }, {
677 .fourcc = V4L2_PIX_FMT_NV42,
678 .bpp = 24,
679 .yuv = true,
680 .lddfr = LDDFR_CC | LDDFR_YF_444,
681 },
682};
683
684static const struct sh_mobile_lcdc_format_info *
685sh_mobile_format_info(u32 fourcc)
686{
687 unsigned int i;
688
689 for (i = 0; i < ARRAY_SIZE(sh_mobile_format_infos); ++i) {
690 if (sh_mobile_format_infos[i].fourcc == fourcc)
691 return &sh_mobile_format_infos[i];
692 }
693
694 return NULL;
695}
696
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200697static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
698{
699 if (var->grayscale > 1)
700 return var->grayscale;
701
702 switch (var->bits_per_pixel) {
703 case 16:
704 return V4L2_PIX_FMT_RGB565;
705 case 24:
706 return V4L2_PIX_FMT_BGR24;
707 case 32:
708 return V4L2_PIX_FMT_BGR32;
709 default:
710 return 0;
711 }
712}
713
714static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
715{
716 return var->grayscale > 1;
717}
718
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200719/* -----------------------------------------------------------------------------
720 * Start, stop and IRQ
721 */
722
Magnus Damm85645572008-12-19 15:34:41 +0900723static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
724{
725 struct sh_mobile_lcdc_priv *priv = data;
Magnus Damm2feb0752009-03-13 15:36:55 +0000726 struct sh_mobile_lcdc_chan *ch;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000727 unsigned long ldintr;
Magnus Damm2feb0752009-03-13 15:36:55 +0000728 int is_sub;
729 int k;
Magnus Damm85645572008-12-19 15:34:41 +0900730
Laurent Pinchartdc486652011-07-13 12:13:47 +0200731 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
732 ldintr = lcdc_read(priv, _LDINTR);
733 lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
Magnus Damm85645572008-12-19 15:34:41 +0900734
Magnus Damm2feb0752009-03-13 15:36:55 +0000735 /* figure out if this interrupt is for main or sub lcd */
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200736 is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
Magnus Damm2feb0752009-03-13 15:36:55 +0000737
Phil Edworthy9dd38812009-09-15 12:00:18 +0000738 /* wake up channel and disable clocks */
Magnus Damm2feb0752009-03-13 15:36:55 +0000739 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
740 ch = &priv->ch[k];
741
742 if (!ch->enabled)
743 continue;
744
Laurent Pinchartdc486652011-07-13 12:13:47 +0200745 /* Frame End */
Phil Edworthy9dd38812009-09-15 12:00:18 +0000746 if (ldintr & LDINTR_FS) {
747 if (is_sub == lcdc_chan_is_sublcd(ch)) {
748 ch->frame_end = 1;
749 wake_up(&ch->frame_end_wait);
Magnus Damm2feb0752009-03-13 15:36:55 +0000750
Phil Edworthy9dd38812009-09-15 12:00:18 +0000751 sh_mobile_lcdc_clk_off(priv);
752 }
753 }
754
755 /* VSYNC End */
Phil Edworthy40331b22010-02-15 13:57:49 +0000756 if (ldintr & LDINTR_VES)
757 complete(&ch->vsync_completion);
Magnus Damm2feb0752009-03-13 15:36:55 +0000758 }
759
Magnus Damm85645572008-12-19 15:34:41 +0900760 return IRQ_HANDLED;
761}
762
Laurent Pinchartd7ad3342011-11-22 00:56:58 +0100763static int sh_mobile_lcdc_wait_for_vsync(struct sh_mobile_lcdc_chan *ch)
Laurent Pinchart49766772011-11-30 23:07:30 +0100764{
765 unsigned long ldintr;
766 int ret;
767
768 /* Enable VSync End interrupt and be careful not to acknowledge any
769 * pending interrupt.
770 */
771 ldintr = lcdc_read(ch->lcdc, _LDINTR);
772 ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
773 lcdc_write(ch->lcdc, _LDINTR, ldintr);
774
775 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
776 msecs_to_jiffies(100));
777 if (!ret)
778 return -ETIMEDOUT;
779
780 return 0;
781}
782
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700783static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
784 int start)
785{
786 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
787 int k;
788
789 /* start or stop the lcdc */
790 if (start)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200791 lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700792 else
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200793 lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700794
795 /* wait until power is applied/stopped on all channels */
796 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
797 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
798 while (1) {
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200799 tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
800 & LDPMR_LPS;
801 if (start && tmp == LDPMR_LPS)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700802 break;
803 if (!start && tmp == 0)
804 break;
805 cpu_relax();
806 }
807
808 if (!start)
809 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
810}
811
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000812static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
813{
Laurent Pinchart2d045592011-11-29 13:42:48 +0100814 const struct fb_var_screeninfo *var = &ch->info->var;
815 const struct fb_videomode *mode = &ch->display.mode;
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000816 unsigned long h_total, hsync_pos, display_h_total;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000817 u32 tmp;
818
819 tmp = ch->ldmt1r_value;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200820 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
821 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100822 tmp |= (ch->cfg->flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
823 tmp |= (ch->cfg->flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
824 tmp |= (ch->cfg->flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
825 tmp |= (ch->cfg->flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
826 tmp |= (ch->cfg->flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000827 lcdc_write_chan(ch, LDMT1R, tmp);
828
829 /* setup SYS bus */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100830 lcdc_write_chan(ch, LDMT2R, ch->cfg->sys_bus_cfg.ldmt2r);
831 lcdc_write_chan(ch, LDMT3R, ch->cfg->sys_bus_cfg.ldmt3r);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000832
833 /* horizontal configuration */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100834 h_total = mode->xres + mode->hsync_len + mode->left_margin
835 + mode->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000836 tmp = h_total / 8; /* HTCN */
Laurent Pinchart58f03d92011-11-30 23:07:30 +0100837 tmp |= (min(mode->xres, ch->xres) / 8) << 16; /* HDCN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000838 lcdc_write_chan(ch, LDHCNR, tmp);
839
Laurent Pinchart2d045592011-11-29 13:42:48 +0100840 hsync_pos = mode->xres + mode->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000841 tmp = hsync_pos / 8; /* HSYNP */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100842 tmp |= (mode->hsync_len / 8) << 16; /* HSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000843 lcdc_write_chan(ch, LDHSYNR, tmp);
844
845 /* vertical configuration */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100846 tmp = mode->yres + mode->vsync_len + mode->upper_margin
847 + mode->lower_margin; /* VTLN */
Laurent Pinchart58f03d92011-11-30 23:07:30 +0100848 tmp |= min(mode->yres, ch->yres) << 16; /* VDLN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000849 lcdc_write_chan(ch, LDVLNR, tmp);
850
Laurent Pinchart2d045592011-11-29 13:42:48 +0100851 tmp = mode->yres + mode->lower_margin; /* VSYNP */
852 tmp |= mode->vsync_len << 16; /* VSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000853 lcdc_write_chan(ch, LDVSYNR, tmp);
854
855 /* Adjust horizontal synchronisation for HDMI */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100856 display_h_total = mode->xres + mode->hsync_len + mode->left_margin
857 + mode->right_margin;
858 tmp = ((mode->xres & 7) << 24) | ((display_h_total & 7) << 16)
859 | ((mode->hsync_len & 7) << 8) | (hsync_pos & 7);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000860 lcdc_write_chan(ch, LDHAJR, tmp);
861}
862
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100863static void sh_mobile_lcdc_overlay_setup(struct sh_mobile_lcdc_overlay *ovl)
864{
865 u32 format = 0;
866
867 if (!ovl->enabled) {
868 lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
869 lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), 0);
870 lcdc_write(ovl->channel->lcdc, LDBCR,
871 LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
872 return;
873 }
874
875 ovl->base_addr_y = ovl->dma_handle;
876 ovl->base_addr_c = ovl->base_addr_y + ovl->xres
877 * ovl->yres_virtual;
878
879 switch (ovl->mode) {
880 case LCDC_OVERLAY_BLEND:
881 format = LDBBSIFR_EN | (ovl->alpha << LDBBSIFR_LAY_SHIFT);
882 break;
883
884 case LCDC_OVERLAY_ROP3:
885 format = LDBBSIFR_EN | LDBBSIFR_BRSEL
886 | (ovl->rop3 << LDBBSIFR_ROP3_SHIFT);
887 break;
888 }
889
890 switch (ovl->format->fourcc) {
891 case V4L2_PIX_FMT_RGB565:
892 case V4L2_PIX_FMT_NV21:
893 case V4L2_PIX_FMT_NV61:
894 case V4L2_PIX_FMT_NV42:
895 format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW;
896 break;
897 case V4L2_PIX_FMT_BGR24:
898 case V4L2_PIX_FMT_NV12:
899 case V4L2_PIX_FMT_NV16:
900 case V4L2_PIX_FMT_NV24:
901 format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW | LDBBSIFR_SWPB;
902 break;
903 case V4L2_PIX_FMT_BGR32:
904 default:
905 format |= LDBBSIFR_SWPL;
906 break;
907 }
908
909 switch (ovl->format->fourcc) {
910 case V4L2_PIX_FMT_RGB565:
911 format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB16;
912 break;
913 case V4L2_PIX_FMT_BGR24:
914 format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB24;
915 break;
916 case V4L2_PIX_FMT_BGR32:
917 format |= LDBBSIFR_AL_PK | LDBBSIFR_RY | LDDFR_PKF_ARGB32;
918 break;
919 case V4L2_PIX_FMT_NV12:
920 case V4L2_PIX_FMT_NV21:
921 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_420;
922 break;
923 case V4L2_PIX_FMT_NV16:
924 case V4L2_PIX_FMT_NV61:
925 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_422;
926 break;
927 case V4L2_PIX_FMT_NV24:
928 case V4L2_PIX_FMT_NV42:
929 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_444;
930 break;
931 }
932
933 lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
934
935 lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), format);
936
937 lcdc_write_overlay(ovl, LDBnBSSZR(ovl->index),
938 (ovl->yres << LDBBSSZR_BVSS_SHIFT) |
939 (ovl->xres << LDBBSSZR_BHSS_SHIFT));
940 lcdc_write_overlay(ovl, LDBnBLOCR(ovl->index),
941 (ovl->pos_y << LDBBLOCR_CVLC_SHIFT) |
942 (ovl->pos_x << LDBBLOCR_CHLC_SHIFT));
943 lcdc_write_overlay(ovl, LDBnBSMWR(ovl->index),
944 ovl->pitch << LDBBSMWR_BSMW_SHIFT);
945
946 lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
947 lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
948
949 lcdc_write(ovl->channel->lcdc, LDBCR,
950 LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
951}
952
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200953/*
Laurent Pinchartd7ad3342011-11-22 00:56:58 +0100954 * __sh_mobile_lcdc_start - Configure and start the LCDC
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200955 * @priv: LCDC device
956 *
957 * Configure all enabled channels and start the LCDC device. All external
958 * devices (clocks, MERAM, panels, ...) are not touched by this function.
959 */
960static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700961{
962 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700963 unsigned long tmp;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200964 int k, m;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700965
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200966 /* Enable LCDC channels. Read data from external memory, avoid using the
967 * BEU for now.
968 */
969 lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
Magnus Damm85645572008-12-19 15:34:41 +0900970
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200971 /* Stop the LCDC first and disable all interrupts. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700972 sh_mobile_lcdc_start_stop(priv, 0);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200973 lcdc_write(priv, _LDINTR, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700974
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200975 /* Configure power supply, dot clocks and start them. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700976 tmp = priv->lddckr;
977 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
978 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200979 if (!ch->enabled)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700980 continue;
981
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200982 /* Power supply */
983 lcdc_write_chan(ch, LDPMR, 0);
984
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100985 m = ch->cfg->clock_divider;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700986 if (!m)
987 continue;
988
Laurent Pinchart505c7de52011-07-13 12:13:47 +0200989 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
990 * denominator.
991 */
992 lcdc_write_chan(ch, LDDCKPAT1R, 0);
993 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
994
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700995 if (m == 1)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200996 m = LDDCKR_MOSEL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700997 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700998 }
999
1000 lcdc_write(priv, _LDDCKR, tmp);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001001 lcdc_write(priv, _LDDCKSTPR, 0);
1002 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
1003
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001004 /* Setup geometry, format, frame buffer memory and operation mode. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001005 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1006 ch = &priv->ch[k];
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001007 if (!ch->enabled)
1008 continue;
1009
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001010 sh_mobile_lcdc_geometry(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001011
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001012 tmp = ch->format->lddfr;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001013
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001014 if (ch->format->yuv) {
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001015 switch (ch->colorspace) {
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001016 case V4L2_COLORSPACE_REC709:
1017 tmp |= LDDFR_CF1;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001018 break;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001019 case V4L2_COLORSPACE_JPEG:
1020 tmp |= LDDFR_CF0;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001021 break;
1022 }
Magnus Damm417d4822011-01-05 10:21:00 +00001023 }
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001024
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001025 lcdc_write_chan(ch, LDDFR, tmp);
Laurent Pinchart72c04af2012-05-18 10:58:26 +02001026 lcdc_write_chan(ch, LDMLSR, ch->line_size);
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001027 lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001028 if (ch->format->yuv)
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001029 lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001030
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001031 /* When using deferred I/O mode, configure the LCDC for one-shot
1032 * operation and enable the frame end interrupt. Otherwise use
1033 * continuous read mode.
1034 */
1035 if (ch->ldmt1r_value & LDMT1R_IFM &&
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001036 ch->cfg->sys_bus_cfg.deferred_io_msec) {
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001037 lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
1038 lcdc_write(priv, _LDINTR, LDINTR_FE);
1039 } else {
1040 lcdc_write_chan(ch, LDSM1R, 0);
1041 }
1042 }
Damian7caa4342011-05-18 11:10:07 +00001043
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001044 /* Word and long word swap. */
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001045 switch (priv->ch[0].format->fourcc) {
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001046 case V4L2_PIX_FMT_RGB565:
1047 case V4L2_PIX_FMT_NV21:
1048 case V4L2_PIX_FMT_NV61:
1049 case V4L2_PIX_FMT_NV42:
1050 tmp = LDDDSR_LS | LDDDSR_WS;
1051 break;
1052 case V4L2_PIX_FMT_BGR24:
1053 case V4L2_PIX_FMT_NV12:
1054 case V4L2_PIX_FMT_NV16:
1055 case V4L2_PIX_FMT_NV24:
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001056 tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001057 break;
1058 case V4L2_PIX_FMT_BGR32:
1059 default:
1060 tmp = LDDDSR_LS;
1061 break;
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001062 }
1063 lcdc_write(priv, _LDDDSR, tmp);
Damian7caa4342011-05-18 11:10:07 +00001064
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001065 /* Enable the display output. */
1066 lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
1067 sh_mobile_lcdc_start_stop(priv, 1);
1068 priv->started = 1;
1069}
Damian7caa4342011-05-18 11:10:07 +00001070
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001071static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
1072{
1073 struct sh_mobile_meram_info *mdev = priv->meram_dev;
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001074 struct sh_mobile_lcdc_chan *ch;
1075 unsigned long tmp;
1076 int ret;
1077 int k;
1078
1079 /* enable clocks before accessing the hardware */
1080 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1081 if (priv->ch[k].enabled)
1082 sh_mobile_lcdc_clk_on(priv);
1083 }
1084
1085 /* reset */
1086 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
1087 lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
1088
1089 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001090 const struct sh_mobile_lcdc_panel_cfg *panel;
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001091
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001092 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001093 if (!ch->enabled)
1094 continue;
1095
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001096 panel = &ch->cfg->panel_cfg;
Laurent Pinchartafaad832011-09-11 22:59:04 +02001097 if (panel->setup_sys) {
1098 ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001099 if (ret)
1100 return ret;
1101 }
1102 }
1103
1104 /* Compute frame buffer base address and pitch for each channel. */
1105 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001106 int pixelformat;
Laurent Pinchart48110052011-12-12 16:36:13 +01001107 void *meram;
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001108
1109 ch = &priv->ch[k];
1110 if (!ch->enabled)
1111 continue;
1112
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001113 ch->base_addr_y = ch->dma_handle;
1114 ch->base_addr_c = ch->base_addr_y + ch->xres * ch->yres_virtual;
Laurent Pinchart72c04af2012-05-18 10:58:26 +02001115 ch->line_size = ch->pitch;
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001116
1117 /* Enable MERAM if possible. */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001118 if (mdev == NULL || mdev->ops == NULL ||
1119 ch->cfg->meram_cfg == NULL)
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001120 continue;
1121
1122 /* we need to de-init configured ICBs before we can
1123 * re-initialize them.
1124 */
Laurent Pinchart48110052011-12-12 16:36:13 +01001125 if (ch->meram) {
1126 mdev->ops->meram_unregister(mdev, ch->meram);
1127 ch->meram = NULL;
Damian7caa4342011-05-18 11:10:07 +00001128 }
1129
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001130 switch (ch->format->fourcc) {
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001131 case V4L2_PIX_FMT_NV12:
1132 case V4L2_PIX_FMT_NV21:
1133 case V4L2_PIX_FMT_NV16:
1134 case V4L2_PIX_FMT_NV61:
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001135 pixelformat = SH_MOBILE_MERAM_PF_NV;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001136 break;
1137 case V4L2_PIX_FMT_NV24:
1138 case V4L2_PIX_FMT_NV42:
1139 pixelformat = SH_MOBILE_MERAM_PF_NV24;
1140 break;
1141 case V4L2_PIX_FMT_RGB565:
1142 case V4L2_PIX_FMT_BGR24:
1143 case V4L2_PIX_FMT_BGR32:
1144 default:
1145 pixelformat = SH_MOBILE_MERAM_PF_RGB;
1146 break;
1147 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001148
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001149 meram = mdev->ops->meram_register(mdev, ch->cfg->meram_cfg,
1150 ch->pitch, ch->yres, pixelformat,
Laurent Pinchart72c04af2012-05-18 10:58:26 +02001151 &ch->line_size);
Laurent Pinchart97d16fe2011-11-22 00:56:58 +01001152 if (!IS_ERR(meram)) {
1153 mdev->ops->meram_update(mdev, meram,
1154 ch->base_addr_y, ch->base_addr_c,
1155 &ch->base_addr_y, &ch->base_addr_c);
Laurent Pinchart48110052011-12-12 16:36:13 +01001156 ch->meram = meram;
Laurent Pinchart97d16fe2011-11-22 00:56:58 +01001157 }
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001158 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001159
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001160 for (k = 0; k < ARRAY_SIZE(priv->overlays); ++k) {
1161 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[k];
1162 sh_mobile_lcdc_overlay_setup(ovl);
1163 }
1164
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001165 /* Start the LCDC. */
1166 __sh_mobile_lcdc_start(priv);
1167
1168 /* Setup deferred I/O, tell the board code to enable the panels, and
1169 * turn backlight on.
1170 */
1171 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1172 ch = &priv->ch[k];
1173 if (!ch->enabled)
1174 continue;
1175
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001176 tmp = ch->cfg->sys_bus_cfg.deferred_io_msec;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001177 if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
Magnus Damm85645572008-12-19 15:34:41 +09001178 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
1179 ch->defio.delay = msecs_to_jiffies(tmp);
Paul Mundte33afdd2009-07-07 11:24:32 +09001180 ch->info->fbdefio = &ch->defio;
1181 fb_deferred_io_init(ch->info);
Magnus Damm85645572008-12-19 15:34:41 +09001182 }
Magnus Damm21bc1f02009-08-15 02:53:16 +00001183
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001184 sh_mobile_lcdc_display_on(ch);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001185
1186 if (ch->bl) {
1187 ch->bl->props.power = FB_BLANK_UNBLANK;
1188 backlight_update_status(ch->bl);
1189 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001190 }
1191
1192 return 0;
1193}
1194
1195static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
1196{
1197 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001198 int k;
1199
Magnus Damm2feb0752009-03-13 15:36:55 +00001200 /* clean up deferred io and ask board code to disable panel */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001201 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1202 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +00001203 if (!ch->enabled)
1204 continue;
Magnus Damm2feb0752009-03-13 15:36:55 +00001205
1206 /* deferred io mode:
1207 * flush frame, and wait for frame end interrupt
1208 * clean up deferred io and enable clock
1209 */
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +00001210 if (ch->info && ch->info->fbdefio) {
Magnus Damm2feb0752009-03-13 15:36:55 +00001211 ch->frame_end = 0;
Paul Mundte33afdd2009-07-07 11:24:32 +09001212 schedule_delayed_work(&ch->info->deferred_work, 0);
Magnus Damm2feb0752009-03-13 15:36:55 +00001213 wait_event(ch->frame_end_wait, ch->frame_end);
Paul Mundte33afdd2009-07-07 11:24:32 +09001214 fb_deferred_io_cleanup(ch->info);
1215 ch->info->fbdefio = NULL;
Magnus Damm2feb0752009-03-13 15:36:55 +00001216 sh_mobile_lcdc_clk_on(priv);
1217 }
1218
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001219 if (ch->bl) {
1220 ch->bl->props.power = FB_BLANK_POWERDOWN;
1221 backlight_update_status(ch->bl);
1222 }
1223
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001224 sh_mobile_lcdc_display_off(ch);
Damian7caa4342011-05-18 11:10:07 +00001225
1226 /* disable the meram */
Laurent Pinchart48110052011-12-12 16:36:13 +01001227 if (ch->meram) {
Damian7caa4342011-05-18 11:10:07 +00001228 struct sh_mobile_meram_info *mdev;
Damian7caa4342011-05-18 11:10:07 +00001229 mdev = priv->meram_dev;
Laurent Pinchart48110052011-12-12 16:36:13 +01001230 mdev->ops->meram_unregister(mdev, ch->meram);
1231 ch->meram = 0;
Damian7caa4342011-05-18 11:10:07 +00001232 }
1233
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001234 }
1235
1236 /* stop the lcdc */
Magnus Damm8e9bb192009-05-20 14:34:43 +00001237 if (priv->started) {
1238 sh_mobile_lcdc_start_stop(priv, 0);
1239 priv->started = 0;
1240 }
Magnus Dammb51339f2008-10-31 20:23:26 +09001241
Magnus Damm85645572008-12-19 15:34:41 +09001242 /* stop clocks */
1243 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
1244 if (priv->ch[k].enabled)
1245 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001246}
1247
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001248static int __sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
1249 struct fb_info *info)
1250{
1251 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
1252 return -EINVAL;
1253
1254 /* Make sure the virtual resolution is at least as big as the visible
1255 * resolution.
1256 */
1257 if (var->xres_virtual < var->xres)
1258 var->xres_virtual = var->xres;
1259 if (var->yres_virtual < var->yres)
1260 var->yres_virtual = var->yres;
1261
1262 if (sh_mobile_format_is_fourcc(var)) {
1263 const struct sh_mobile_lcdc_format_info *format;
1264
1265 format = sh_mobile_format_info(var->grayscale);
1266 if (format == NULL)
1267 return -EINVAL;
1268 var->bits_per_pixel = format->bpp;
1269
1270 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1271 * respectively.
1272 */
1273 if (!format->yuv)
1274 var->colorspace = V4L2_COLORSPACE_SRGB;
1275 else if (var->colorspace != V4L2_COLORSPACE_REC709)
1276 var->colorspace = V4L2_COLORSPACE_JPEG;
1277 } else {
1278 if (var->bits_per_pixel <= 16) { /* RGB 565 */
1279 var->bits_per_pixel = 16;
1280 var->red.offset = 11;
1281 var->red.length = 5;
1282 var->green.offset = 5;
1283 var->green.length = 6;
1284 var->blue.offset = 0;
1285 var->blue.length = 5;
1286 var->transp.offset = 0;
1287 var->transp.length = 0;
1288 } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
1289 var->bits_per_pixel = 24;
1290 var->red.offset = 16;
1291 var->red.length = 8;
1292 var->green.offset = 8;
1293 var->green.length = 8;
1294 var->blue.offset = 0;
1295 var->blue.length = 8;
1296 var->transp.offset = 0;
1297 var->transp.length = 0;
1298 } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
1299 var->bits_per_pixel = 32;
1300 var->red.offset = 16;
1301 var->red.length = 8;
1302 var->green.offset = 8;
1303 var->green.length = 8;
1304 var->blue.offset = 0;
1305 var->blue.length = 8;
1306 var->transp.offset = 24;
1307 var->transp.length = 8;
1308 } else
1309 return -EINVAL;
1310
1311 var->red.msb_right = 0;
1312 var->green.msb_right = 0;
1313 var->blue.msb_right = 0;
1314 var->transp.msb_right = 0;
1315 }
1316
1317 /* Make sure we don't exceed our allocated memory. */
1318 if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
1319 info->fix.smem_len)
1320 return -EINVAL;
1321
1322 return 0;
1323}
1324
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001325/* -----------------------------------------------------------------------------
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001326 * Frame buffer operations - Overlays
1327 */
1328
1329static ssize_t
1330overlay_alpha_show(struct device *dev, struct device_attribute *attr, char *buf)
1331{
1332 struct fb_info *info = dev_get_drvdata(dev);
1333 struct sh_mobile_lcdc_overlay *ovl = info->par;
1334
1335 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->alpha);
1336}
1337
1338static ssize_t
1339overlay_alpha_store(struct device *dev, struct device_attribute *attr,
1340 const char *buf, size_t count)
1341{
1342 struct fb_info *info = dev_get_drvdata(dev);
1343 struct sh_mobile_lcdc_overlay *ovl = info->par;
1344 unsigned int alpha;
1345 char *endp;
1346
1347 alpha = simple_strtoul(buf, &endp, 10);
1348 if (isspace(*endp))
1349 endp++;
1350
1351 if (endp - buf != count)
1352 return -EINVAL;
1353
1354 if (alpha > 255)
1355 return -EINVAL;
1356
1357 if (ovl->alpha != alpha) {
1358 ovl->alpha = alpha;
1359
1360 if (ovl->mode == LCDC_OVERLAY_BLEND && ovl->enabled)
1361 sh_mobile_lcdc_overlay_setup(ovl);
1362 }
1363
1364 return count;
1365}
1366
1367static ssize_t
1368overlay_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1369{
1370 struct fb_info *info = dev_get_drvdata(dev);
1371 struct sh_mobile_lcdc_overlay *ovl = info->par;
1372
1373 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->mode);
1374}
1375
1376static ssize_t
1377overlay_mode_store(struct device *dev, struct device_attribute *attr,
1378 const char *buf, size_t count)
1379{
1380 struct fb_info *info = dev_get_drvdata(dev);
1381 struct sh_mobile_lcdc_overlay *ovl = info->par;
1382 unsigned int mode;
1383 char *endp;
1384
1385 mode = simple_strtoul(buf, &endp, 10);
1386 if (isspace(*endp))
1387 endp++;
1388
1389 if (endp - buf != count)
1390 return -EINVAL;
1391
1392 if (mode != LCDC_OVERLAY_BLEND && mode != LCDC_OVERLAY_ROP3)
1393 return -EINVAL;
1394
1395 if (ovl->mode != mode) {
1396 ovl->mode = mode;
1397
1398 if (ovl->enabled)
1399 sh_mobile_lcdc_overlay_setup(ovl);
1400 }
1401
1402 return count;
1403}
1404
1405static ssize_t
1406overlay_position_show(struct device *dev, struct device_attribute *attr,
1407 char *buf)
1408{
1409 struct fb_info *info = dev_get_drvdata(dev);
1410 struct sh_mobile_lcdc_overlay *ovl = info->par;
1411
1412 return scnprintf(buf, PAGE_SIZE, "%d,%d\n", ovl->pos_x, ovl->pos_y);
1413}
1414
1415static ssize_t
1416overlay_position_store(struct device *dev, struct device_attribute *attr,
1417 const char *buf, size_t count)
1418{
1419 struct fb_info *info = dev_get_drvdata(dev);
1420 struct sh_mobile_lcdc_overlay *ovl = info->par;
1421 char *endp;
1422 int pos_x;
1423 int pos_y;
1424
1425 pos_x = simple_strtol(buf, &endp, 10);
1426 if (*endp != ',')
1427 return -EINVAL;
1428
1429 pos_y = simple_strtol(endp + 1, &endp, 10);
1430 if (isspace(*endp))
1431 endp++;
1432
1433 if (endp - buf != count)
1434 return -EINVAL;
1435
1436 if (ovl->pos_x != pos_x || ovl->pos_y != pos_y) {
1437 ovl->pos_x = pos_x;
1438 ovl->pos_y = pos_y;
1439
1440 if (ovl->enabled)
1441 sh_mobile_lcdc_overlay_setup(ovl);
1442 }
1443
1444 return count;
1445}
1446
1447static ssize_t
1448overlay_rop3_show(struct device *dev, struct device_attribute *attr, char *buf)
1449{
1450 struct fb_info *info = dev_get_drvdata(dev);
1451 struct sh_mobile_lcdc_overlay *ovl = info->par;
1452
1453 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->rop3);
1454}
1455
1456static ssize_t
1457overlay_rop3_store(struct device *dev, struct device_attribute *attr,
1458 const char *buf, size_t count)
1459{
1460 struct fb_info *info = dev_get_drvdata(dev);
1461 struct sh_mobile_lcdc_overlay *ovl = info->par;
1462 unsigned int rop3;
1463 char *endp;
1464
1465 rop3 = !!simple_strtoul(buf, &endp, 10);
1466 if (isspace(*endp))
1467 endp++;
1468
1469 if (endp - buf != count)
1470 return -EINVAL;
1471
1472 if (rop3 > 255)
1473 return -EINVAL;
1474
1475 if (ovl->rop3 != rop3) {
1476 ovl->rop3 = rop3;
1477
1478 if (ovl->mode == LCDC_OVERLAY_ROP3 && ovl->enabled)
1479 sh_mobile_lcdc_overlay_setup(ovl);
1480 }
1481
1482 return count;
1483}
1484
1485static const struct device_attribute overlay_sysfs_attrs[] = {
1486 __ATTR(ovl_alpha, S_IRUGO|S_IWUSR,
1487 overlay_alpha_show, overlay_alpha_store),
1488 __ATTR(ovl_mode, S_IRUGO|S_IWUSR,
1489 overlay_mode_show, overlay_mode_store),
1490 __ATTR(ovl_position, S_IRUGO|S_IWUSR,
1491 overlay_position_show, overlay_position_store),
1492 __ATTR(ovl_rop3, S_IRUGO|S_IWUSR,
1493 overlay_rop3_show, overlay_rop3_store),
1494};
1495
1496static const struct fb_fix_screeninfo sh_mobile_lcdc_overlay_fix = {
1497 .id = "SH Mobile LCDC",
1498 .type = FB_TYPE_PACKED_PIXELS,
1499 .visual = FB_VISUAL_TRUECOLOR,
1500 .accel = FB_ACCEL_NONE,
1501 .xpanstep = 0,
1502 .ypanstep = 1,
1503 .ywrapstep = 0,
1504 .capabilities = FB_CAP_FOURCC,
1505};
1506
1507static int sh_mobile_lcdc_overlay_pan(struct fb_var_screeninfo *var,
1508 struct fb_info *info)
1509{
1510 struct sh_mobile_lcdc_overlay *ovl = info->par;
1511 unsigned long base_addr_y;
1512 unsigned long base_addr_c;
1513 unsigned long pan_offset;
1514 unsigned long c_offset;
1515
1516 if (!ovl->format->yuv)
1517 pan_offset = var->yoffset * ovl->pitch
1518 + var->xoffset * (ovl->format->bpp / 8);
1519 else
1520 pan_offset = var->yoffset * ovl->pitch + var->xoffset;
1521
1522 if (pan_offset == ovl->pan_offset)
1523 return 0; /* No change, do nothing */
1524
1525 /* Set the source address for the next refresh */
1526 base_addr_y = ovl->dma_handle + pan_offset;
1527
1528 ovl->base_addr_y = base_addr_y;
1529 ovl->base_addr_c = base_addr_y;
1530
1531 if (ovl->format->yuv) {
1532 /* Set Y offset */
1533 c_offset = var->yoffset * ovl->pitch
1534 * (ovl->format->bpp - 8) / 8;
1535 base_addr_c = ovl->dma_handle
1536 + ovl->xres * ovl->yres_virtual
1537 + c_offset;
1538 /* Set X offset */
1539 if (ovl->format->fourcc == V4L2_PIX_FMT_NV24)
1540 base_addr_c += 2 * var->xoffset;
1541 else
1542 base_addr_c += var->xoffset;
1543
1544 ovl->base_addr_c = base_addr_c;
1545 }
1546
1547 lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
1548 lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
1549
1550 ovl->pan_offset = pan_offset;
1551
1552 return 0;
1553}
1554
1555static int sh_mobile_lcdc_overlay_ioctl(struct fb_info *info, unsigned int cmd,
1556 unsigned long arg)
1557{
1558 struct sh_mobile_lcdc_overlay *ovl = info->par;
1559
1560 switch (cmd) {
1561 case FBIO_WAITFORVSYNC:
1562 return sh_mobile_lcdc_wait_for_vsync(ovl->channel);
1563
1564 default:
1565 return -ENOIOCTLCMD;
1566 }
1567}
1568
1569static int sh_mobile_lcdc_overlay_check_var(struct fb_var_screeninfo *var,
1570 struct fb_info *info)
1571{
1572 return __sh_mobile_lcdc_check_var(var, info);
1573}
1574
1575static int sh_mobile_lcdc_overlay_set_par(struct fb_info *info)
1576{
1577 struct sh_mobile_lcdc_overlay *ovl = info->par;
1578
1579 ovl->format =
1580 sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
1581
1582 ovl->xres = info->var.xres;
1583 ovl->xres_virtual = info->var.xres_virtual;
1584 ovl->yres = info->var.yres;
1585 ovl->yres_virtual = info->var.yres_virtual;
1586
1587 if (ovl->format->yuv)
1588 ovl->pitch = info->var.xres;
1589 else
1590 ovl->pitch = info->var.xres * ovl->format->bpp / 8;
1591
1592 sh_mobile_lcdc_overlay_setup(ovl);
1593
1594 info->fix.line_length = ovl->pitch;
1595
1596 if (sh_mobile_format_is_fourcc(&info->var)) {
1597 info->fix.type = FB_TYPE_FOURCC;
1598 info->fix.visual = FB_VISUAL_FOURCC;
1599 } else {
1600 info->fix.type = FB_TYPE_PACKED_PIXELS;
1601 info->fix.visual = FB_VISUAL_TRUECOLOR;
1602 }
1603
1604 return 0;
1605}
1606
1607/* Overlay blanking. Disable the overlay when blanked. */
1608static int sh_mobile_lcdc_overlay_blank(int blank, struct fb_info *info)
1609{
1610 struct sh_mobile_lcdc_overlay *ovl = info->par;
1611
1612 ovl->enabled = !blank;
1613 sh_mobile_lcdc_overlay_setup(ovl);
1614
1615 /* Prevent the backlight from receiving a blanking event by returning
1616 * a non-zero value.
1617 */
1618 return 1;
1619}
1620
1621static struct fb_ops sh_mobile_lcdc_overlay_ops = {
1622 .owner = THIS_MODULE,
1623 .fb_read = fb_sys_read,
1624 .fb_write = fb_sys_write,
1625 .fb_fillrect = sys_fillrect,
1626 .fb_copyarea = sys_copyarea,
1627 .fb_imageblit = sys_imageblit,
1628 .fb_blank = sh_mobile_lcdc_overlay_blank,
1629 .fb_pan_display = sh_mobile_lcdc_overlay_pan,
1630 .fb_ioctl = sh_mobile_lcdc_overlay_ioctl,
1631 .fb_check_var = sh_mobile_lcdc_overlay_check_var,
1632 .fb_set_par = sh_mobile_lcdc_overlay_set_par,
1633};
1634
1635static void
1636sh_mobile_lcdc_overlay_fb_unregister(struct sh_mobile_lcdc_overlay *ovl)
1637{
1638 struct fb_info *info = ovl->info;
1639
1640 if (info == NULL || info->dev == NULL)
1641 return;
1642
1643 unregister_framebuffer(ovl->info);
1644}
1645
1646static int __devinit
1647sh_mobile_lcdc_overlay_fb_register(struct sh_mobile_lcdc_overlay *ovl)
1648{
1649 struct sh_mobile_lcdc_priv *lcdc = ovl->channel->lcdc;
1650 struct fb_info *info = ovl->info;
1651 unsigned int i;
1652 int ret;
1653
1654 if (info == NULL)
1655 return 0;
1656
1657 ret = register_framebuffer(info);
1658 if (ret < 0)
1659 return ret;
1660
1661 dev_info(lcdc->dev, "registered %s/overlay %u as %dx%d %dbpp.\n",
1662 dev_name(lcdc->dev), ovl->index, info->var.xres,
1663 info->var.yres, info->var.bits_per_pixel);
1664
1665 for (i = 0; i < ARRAY_SIZE(overlay_sysfs_attrs); ++i) {
1666 ret = device_create_file(info->dev, &overlay_sysfs_attrs[i]);
1667 if (ret < 0)
1668 return ret;
1669 }
1670
1671 return 0;
1672}
1673
1674static void
1675sh_mobile_lcdc_overlay_fb_cleanup(struct sh_mobile_lcdc_overlay *ovl)
1676{
1677 struct fb_info *info = ovl->info;
1678
1679 if (info == NULL || info->device == NULL)
1680 return;
1681
1682 framebuffer_release(info);
1683}
1684
1685static int __devinit
1686sh_mobile_lcdc_overlay_fb_init(struct sh_mobile_lcdc_overlay *ovl)
1687{
1688 struct sh_mobile_lcdc_priv *priv = ovl->channel->lcdc;
1689 struct fb_var_screeninfo *var;
1690 struct fb_info *info;
1691
1692 /* Allocate and initialize the frame buffer device. */
1693 info = framebuffer_alloc(0, priv->dev);
1694 if (info == NULL) {
1695 dev_err(priv->dev, "unable to allocate fb_info\n");
1696 return -ENOMEM;
1697 }
1698
1699 ovl->info = info;
1700
1701 info->flags = FBINFO_FLAG_DEFAULT;
1702 info->fbops = &sh_mobile_lcdc_overlay_ops;
1703 info->device = priv->dev;
1704 info->screen_base = ovl->fb_mem;
1705 info->par = ovl;
1706
1707 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1708 * for NV12 and NV21.
1709 */
1710 info->fix = sh_mobile_lcdc_overlay_fix;
1711 snprintf(info->fix.id, sizeof(info->fix.id),
1712 "SH Mobile LCDC Overlay %u", ovl->index);
1713 info->fix.smem_start = ovl->dma_handle;
1714 info->fix.smem_len = ovl->fb_size;
1715 info->fix.line_length = ovl->pitch;
1716
1717 if (ovl->format->yuv)
1718 info->fix.visual = FB_VISUAL_FOURCC;
1719 else
1720 info->fix.visual = FB_VISUAL_TRUECOLOR;
1721
1722 if (ovl->format->fourcc == V4L2_PIX_FMT_NV12 ||
1723 ovl->format->fourcc == V4L2_PIX_FMT_NV21)
1724 info->fix.ypanstep = 2;
1725
1726 /* Initialize variable screen information. */
1727 var = &info->var;
1728 memset(var, 0, sizeof(*var));
1729 var->xres = ovl->xres;
1730 var->yres = ovl->yres;
1731 var->xres_virtual = ovl->xres_virtual;
1732 var->yres_virtual = ovl->yres_virtual;
1733 var->activate = FB_ACTIVATE_NOW;
1734
1735 /* Use the legacy API by default for RGB formats, and the FOURCC API
1736 * for YUV formats.
1737 */
1738 if (!ovl->format->yuv)
1739 var->bits_per_pixel = ovl->format->bpp;
1740 else
1741 var->grayscale = ovl->format->fourcc;
1742
1743 return sh_mobile_lcdc_overlay_check_var(var, info);
1744}
1745
1746/* -----------------------------------------------------------------------------
1747 * Frame buffer operations - main frame buffer
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001748 */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001749
1750static int sh_mobile_lcdc_setcolreg(u_int regno,
1751 u_int red, u_int green, u_int blue,
1752 u_int transp, struct fb_info *info)
1753{
1754 u32 *palette = info->pseudo_palette;
1755
1756 if (regno >= PALETTE_NR)
1757 return -EINVAL;
1758
1759 /* only FB_VISUAL_TRUECOLOR supported */
1760
1761 red >>= 16 - info->var.red.length;
1762 green >>= 16 - info->var.green.length;
1763 blue >>= 16 - info->var.blue.length;
1764 transp >>= 16 - info->var.transp.length;
1765
1766 palette[regno] = (red << info->var.red.offset) |
1767 (green << info->var.green.offset) |
1768 (blue << info->var.blue.offset) |
1769 (transp << info->var.transp.offset);
1770
1771 return 0;
1772}
1773
Laurent Pinchart3281e542011-11-22 00:56:58 +01001774static const struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001775 .id = "SH Mobile LCDC",
1776 .type = FB_TYPE_PACKED_PIXELS,
1777 .visual = FB_VISUAL_TRUECOLOR,
1778 .accel = FB_ACCEL_NONE,
Phil Edworthy9dd38812009-09-15 12:00:18 +00001779 .xpanstep = 0,
1780 .ypanstep = 1,
1781 .ywrapstep = 0,
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001782 .capabilities = FB_CAP_FOURCC,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001783};
1784
Magnus Damm85645572008-12-19 15:34:41 +09001785static void sh_mobile_lcdc_fillrect(struct fb_info *info,
1786 const struct fb_fillrect *rect)
1787{
1788 sys_fillrect(info, rect);
1789 sh_mobile_lcdc_deferred_io_touch(info);
1790}
1791
1792static void sh_mobile_lcdc_copyarea(struct fb_info *info,
1793 const struct fb_copyarea *area)
1794{
1795 sys_copyarea(info, area);
1796 sh_mobile_lcdc_deferred_io_touch(info);
1797}
1798
1799static void sh_mobile_lcdc_imageblit(struct fb_info *info,
1800 const struct fb_image *image)
1801{
1802 sys_imageblit(info, image);
1803 sh_mobile_lcdc_deferred_io_touch(info);
1804}
1805
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001806static int sh_mobile_lcdc_pan(struct fb_var_screeninfo *var,
1807 struct fb_info *info)
Phil Edworthy9dd38812009-09-15 12:00:18 +00001808{
1809 struct sh_mobile_lcdc_chan *ch = info->par;
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001810 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
1811 unsigned long ldrcntr;
1812 unsigned long new_pan_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001813 unsigned long base_addr_y, base_addr_c;
1814 unsigned long c_offset;
Phil Edworthy9dd38812009-09-15 12:00:18 +00001815
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001816 if (!ch->format->yuv)
1817 new_pan_offset = var->yoffset * ch->pitch
1818 + var->xoffset * (ch->format->bpp / 8);
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001819 else
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001820 new_pan_offset = var->yoffset * ch->pitch + var->xoffset;
Phil Edworthy9dd38812009-09-15 12:00:18 +00001821
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001822 if (new_pan_offset == ch->pan_offset)
1823 return 0; /* No change, do nothing */
1824
1825 ldrcntr = lcdc_read(priv, _LDRCNTR);
1826
1827 /* Set the source address for the next refresh */
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001828 base_addr_y = ch->dma_handle + new_pan_offset;
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001829 if (ch->format->yuv) {
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001830 /* Set y offset */
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001831 c_offset = var->yoffset * ch->pitch
1832 * (ch->format->bpp - 8) / 8;
1833 base_addr_c = ch->dma_handle + ch->xres * ch->yres_virtual
Laurent Pinchartdc1d5ad2011-08-31 13:00:55 +02001834 + c_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001835 /* Set x offset */
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001836 if (ch->format->fourcc == V4L2_PIX_FMT_NV24)
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001837 base_addr_c += 2 * var->xoffset;
1838 else
1839 base_addr_c += var->xoffset;
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001840 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001841
Laurent Pinchart48110052011-12-12 16:36:13 +01001842 if (ch->meram) {
Damian7caa4342011-05-18 11:10:07 +00001843 struct sh_mobile_meram_info *mdev;
Damian7caa4342011-05-18 11:10:07 +00001844
Damian7caa4342011-05-18 11:10:07 +00001845 mdev = priv->meram_dev;
Laurent Pinchartcdf88b92011-11-22 00:56:58 +01001846 mdev->ops->meram_update(mdev, ch->meram,
Damian7caa4342011-05-18 11:10:07 +00001847 base_addr_y, base_addr_c,
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001848 &base_addr_y, &base_addr_c);
Damian7caa4342011-05-18 11:10:07 +00001849 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001850
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001851 ch->base_addr_y = base_addr_y;
1852 ch->base_addr_c = base_addr_c;
1853
1854 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001855 if (ch->format->yuv)
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001856 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
1857
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001858 if (lcdc_chan_is_sublcd(ch))
1859 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
1860 else
1861 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
1862
1863 ch->pan_offset = new_pan_offset;
1864
1865 sh_mobile_lcdc_deferred_io_touch(info);
Phil Edworthy9dd38812009-09-15 12:00:18 +00001866
1867 return 0;
1868}
1869
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001870static int sh_mobile_lcdc_ioctl(struct fb_info *info, unsigned int cmd,
1871 unsigned long arg)
Phil Edworthy40331b22010-02-15 13:57:49 +00001872{
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001873 struct sh_mobile_lcdc_chan *ch = info->par;
Phil Edworthy40331b22010-02-15 13:57:49 +00001874 int retval;
1875
1876 switch (cmd) {
1877 case FBIO_WAITFORVSYNC:
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001878 retval = sh_mobile_lcdc_wait_for_vsync(ch);
Phil Edworthy40331b22010-02-15 13:57:49 +00001879 break;
1880
1881 default:
1882 retval = -ENOIOCTLCMD;
1883 break;
1884 }
1885 return retval;
1886}
1887
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001888static void sh_mobile_fb_reconfig(struct fb_info *info)
1889{
1890 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchart2d045592011-11-29 13:42:48 +01001891 struct fb_var_screeninfo var;
1892 struct fb_videomode mode;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001893 struct fb_event event;
1894 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1895
1896 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1897 /* More framebuffer users are active */
1898 return;
1899
Laurent Pinchart2d045592011-11-29 13:42:48 +01001900 fb_var_to_videomode(&mode, &info->var);
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001901
Laurent Pinchart2d045592011-11-29 13:42:48 +01001902 if (fb_mode_is_equal(&ch->display.mode, &mode))
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001903 return;
1904
1905 /* Display has been re-plugged, framebuffer is free now, reconfigure */
Laurent Pinchart2d045592011-11-29 13:42:48 +01001906 var = info->var;
1907 fb_videomode_to_var(&var, &ch->display.mode);
1908 var.width = ch->display.width;
1909 var.height = ch->display.height;
1910 var.activate = FB_ACTIVATE_NOW;
1911
1912 if (fb_set_var(info, &var) < 0)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001913 /* Couldn't reconfigure, hopefully, can continue as before */
1914 return;
1915
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001916 /*
1917 * fb_set_var() calls the notifier change internally, only if
1918 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1919 * user event, we have to call the chain ourselves.
1920 */
1921 event.info = info;
Laurent Pinchart2d045592011-11-29 13:42:48 +01001922 event.data = &ch->display.mode;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001923 fb_notifier_call_chain(evnt, &event);
1924}
1925
1926/*
1927 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1928 * user == 1, or with console sem held, if user == 0.
1929 */
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001930static int sh_mobile_lcdc_release(struct fb_info *info, int user)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001931{
1932 struct sh_mobile_lcdc_chan *ch = info->par;
1933
1934 mutex_lock(&ch->open_lock);
1935 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1936
1937 ch->use_count--;
1938
1939 /* Nothing to reconfigure, when called from fbcon */
1940 if (user) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001941 console_lock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001942 sh_mobile_fb_reconfig(info);
Torben Hohnac751ef2011-01-25 15:07:35 -08001943 console_unlock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001944 }
1945
1946 mutex_unlock(&ch->open_lock);
1947
1948 return 0;
1949}
1950
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001951static int sh_mobile_lcdc_open(struct fb_info *info, int user)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001952{
1953 struct sh_mobile_lcdc_chan *ch = info->par;
1954
1955 mutex_lock(&ch->open_lock);
1956 ch->use_count++;
1957
1958 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1959 mutex_unlock(&ch->open_lock);
1960
1961 return 0;
1962}
1963
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001964static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
1965 struct fb_info *info)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001966{
1967 struct sh_mobile_lcdc_chan *ch = info->par;
Magnus Damm417d4822011-01-05 10:21:00 +00001968 struct sh_mobile_lcdc_priv *p = ch->lcdc;
Laurent Pinchart03862192011-08-31 13:00:53 +02001969 unsigned int best_dist = (unsigned int)-1;
1970 unsigned int best_xres = 0;
1971 unsigned int best_yres = 0;
1972 unsigned int i;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001973 int ret;
Laurent Pinchart03862192011-08-31 13:00:53 +02001974
1975 /* If board code provides us with a list of available modes, make sure
1976 * we use one of them. Find the mode closest to the requested one. The
1977 * distance between two modes is defined as the size of the
1978 * non-overlapping parts of the two rectangles.
1979 */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001980 for (i = 0; i < ch->cfg->num_modes; ++i) {
1981 const struct fb_videomode *mode = &ch->cfg->lcd_modes[i];
Laurent Pinchart03862192011-08-31 13:00:53 +02001982 unsigned int dist;
1983
1984 /* We can only round up. */
1985 if (var->xres > mode->xres || var->yres > mode->yres)
1986 continue;
1987
1988 dist = var->xres * var->yres + mode->xres * mode->yres
1989 - 2 * min(var->xres, mode->xres)
1990 * min(var->yres, mode->yres);
1991
1992 if (dist < best_dist) {
1993 best_xres = mode->xres;
1994 best_yres = mode->yres;
1995 best_dist = dist;
1996 }
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001997 }
Magnus Damm417d4822011-01-05 10:21:00 +00001998
Laurent Pinchart03862192011-08-31 13:00:53 +02001999 /* If no available mode can be used, return an error. */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002000 if (ch->cfg->num_modes != 0) {
Laurent Pinchart03862192011-08-31 13:00:53 +02002001 if (best_dist == (unsigned int)-1)
2002 return -EINVAL;
2003
2004 var->xres = best_xres;
2005 var->yres = best_yres;
2006 }
2007
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002008 ret = __sh_mobile_lcdc_check_var(var, info);
2009 if (ret < 0)
2010 return ret;
Laurent Pinchart03862192011-08-31 13:00:53 +02002011
Laurent Pinchartedd153a2011-12-13 14:02:28 +01002012 /* only accept the forced_fourcc for dual channel configurations */
2013 if (p->forced_fourcc &&
2014 p->forced_fourcc != sh_mobile_format_fourcc(var))
Magnus Damm417d4822011-01-05 10:21:00 +00002015 return -EINVAL;
2016
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00002017 return 0;
2018}
Phil Edworthy40331b22010-02-15 13:57:49 +00002019
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01002020static int sh_mobile_lcdc_set_par(struct fb_info *info)
Laurent Pincharted5bebf2011-08-31 13:00:54 +02002021{
2022 struct sh_mobile_lcdc_chan *ch = info->par;
2023 int ret;
2024
2025 sh_mobile_lcdc_stop(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02002026
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01002027 ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
Laurent Pinchart58f03d92011-11-30 23:07:30 +01002028 ch->colorspace = info->var.colorspace;
2029
2030 ch->xres = info->var.xres;
2031 ch->xres_virtual = info->var.xres_virtual;
2032 ch->yres = info->var.yres;
2033 ch->yres_virtual = info->var.yres_virtual;
2034
2035 if (ch->format->yuv)
2036 ch->pitch = info->var.xres;
2037 else
2038 ch->pitch = info->var.xres * ch->format->bpp / 8;
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01002039
Laurent Pincharted5bebf2011-08-31 13:00:54 +02002040 ret = sh_mobile_lcdc_start(ch->lcdc);
Laurent Pinchart58f03d92011-11-30 23:07:30 +01002041 if (ret < 0)
Laurent Pincharted5bebf2011-08-31 13:00:54 +02002042 dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
Laurent Pinchart58f03d92011-11-30 23:07:30 +01002043
2044 info->fix.line_length = ch->pitch;
Laurent Pincharted5bebf2011-08-31 13:00:54 +02002045
Laurent Pinchartedd153a2011-12-13 14:02:28 +01002046 if (sh_mobile_format_is_fourcc(&info->var)) {
2047 info->fix.type = FB_TYPE_FOURCC;
2048 info->fix.visual = FB_VISUAL_FOURCC;
2049 } else {
2050 info->fix.type = FB_TYPE_PACKED_PIXELS;
2051 info->fix.visual = FB_VISUAL_TRUECOLOR;
2052 }
2053
Laurent Pincharted5bebf2011-08-31 13:00:54 +02002054 return ret;
2055}
2056
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00002057/*
2058 * Screen blanking. Behavior is as follows:
2059 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
2060 * FB_BLANK_NORMAL: screen blanked, clocks enabled
2061 * FB_BLANK_VSYNC,
2062 * FB_BLANK_HSYNC,
2063 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
2064 */
2065static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
2066{
2067 struct sh_mobile_lcdc_chan *ch = info->par;
2068 struct sh_mobile_lcdc_priv *p = ch->lcdc;
2069
2070 /* blank the screen? */
2071 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
2072 struct fb_fillrect rect = {
Laurent Pinchart58f03d92011-11-30 23:07:30 +01002073 .width = ch->xres,
2074 .height = ch->yres,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00002075 };
2076 sh_mobile_lcdc_fillrect(info, &rect);
2077 }
2078 /* turn clocks on? */
2079 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
2080 sh_mobile_lcdc_clk_on(p);
2081 }
2082 /* turn clocks off? */
2083 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
2084 /* make sure the screen is updated with the black fill before
2085 * switching the clocks off. one vsync is not enough since
2086 * blanking may occur in the middle of a refresh. deferred io
2087 * mode will reenable the clocks and update the screen in time,
2088 * so it does not need this. */
2089 if (!info->fbdefio) {
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01002090 sh_mobile_lcdc_wait_for_vsync(ch);
2091 sh_mobile_lcdc_wait_for_vsync(ch);
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00002092 }
2093 sh_mobile_lcdc_clk_off(p);
2094 }
2095
2096 ch->blank_status = blank;
2097 return 0;
2098}
2099
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002100static struct fb_ops sh_mobile_lcdc_ops = {
Phil Edworthy9dd38812009-09-15 12:00:18 +00002101 .owner = THIS_MODULE,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002102 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
Magnus Damm2540c112008-12-17 17:29:49 +09002103 .fb_read = fb_sys_read,
2104 .fb_write = fb_sys_write,
Magnus Damm85645572008-12-19 15:34:41 +09002105 .fb_fillrect = sh_mobile_lcdc_fillrect,
2106 .fb_copyarea = sh_mobile_lcdc_copyarea,
2107 .fb_imageblit = sh_mobile_lcdc_imageblit,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00002108 .fb_blank = sh_mobile_lcdc_blank,
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01002109 .fb_pan_display = sh_mobile_lcdc_pan,
2110 .fb_ioctl = sh_mobile_lcdc_ioctl,
2111 .fb_open = sh_mobile_lcdc_open,
2112 .fb_release = sh_mobile_lcdc_release,
2113 .fb_check_var = sh_mobile_lcdc_check_var,
2114 .fb_set_par = sh_mobile_lcdc_set_par,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002115};
2116
Laurent Pincharta67f3792011-11-29 14:37:35 +01002117static void
2118sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
2119{
2120 if (ch->info && ch->info->dev)
2121 unregister_framebuffer(ch->info);
2122}
2123
2124static int __devinit
2125sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
2126{
2127 struct fb_info *info = ch->info;
2128 int ret;
2129
2130 if (info->fbdefio) {
2131 ch->sglist = vmalloc(sizeof(struct scatterlist) *
2132 ch->fb_size >> PAGE_SHIFT);
2133 if (!ch->sglist) {
2134 dev_err(ch->lcdc->dev, "cannot allocate sglist\n");
2135 return -ENOMEM;
2136 }
2137 }
2138
2139 info->bl_dev = ch->bl;
2140
2141 ret = register_framebuffer(info);
2142 if (ret < 0)
2143 return ret;
2144
2145 dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002146 dev_name(ch->lcdc->dev), (ch->cfg->chan == LCDC_CHAN_MAINLCD) ?
Laurent Pincharta67f3792011-11-29 14:37:35 +01002147 "mainlcd" : "sublcd", info->var.xres, info->var.yres,
2148 info->var.bits_per_pixel);
2149
2150 /* deferred io mode: disable clock to save power */
2151 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
2152 sh_mobile_lcdc_clk_off(ch->lcdc);
2153
2154 return ret;
2155}
2156
2157static void
2158sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
2159{
2160 struct fb_info *info = ch->info;
2161
2162 if (!info || !info->device)
2163 return;
2164
2165 if (ch->sglist)
2166 vfree(ch->sglist);
2167
2168 fb_dealloc_cmap(&info->cmap);
2169 framebuffer_release(info);
2170}
2171
2172static int __devinit
2173sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
2174 const struct fb_videomode *mode,
2175 unsigned int num_modes)
2176{
2177 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
2178 struct fb_var_screeninfo *var;
2179 struct fb_info *info;
2180 int ret;
2181
2182 /* Allocate and initialize the frame buffer device. Create the modes
2183 * list and allocate the color map.
2184 */
2185 info = framebuffer_alloc(0, priv->dev);
2186 if (info == NULL) {
2187 dev_err(priv->dev, "unable to allocate fb_info\n");
2188 return -ENOMEM;
2189 }
2190
2191 ch->info = info;
2192
2193 info->flags = FBINFO_FLAG_DEFAULT;
2194 info->fbops = &sh_mobile_lcdc_ops;
2195 info->device = priv->dev;
2196 info->screen_base = ch->fb_mem;
2197 info->pseudo_palette = &ch->pseudo_palette;
2198 info->par = ch;
2199
2200 fb_videomode_to_modelist(mode, num_modes, &info->modelist);
2201
2202 ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
2203 if (ret < 0) {
2204 dev_err(priv->dev, "unable to allocate cmap\n");
2205 return ret;
2206 }
2207
2208 /* Initialize fixed screen information. Restrict pan to 2 lines steps
2209 * for NV12 and NV21.
2210 */
2211 info->fix = sh_mobile_lcdc_fix;
2212 info->fix.smem_start = ch->dma_handle;
2213 info->fix.smem_len = ch->fb_size;
Laurent Pinchart58f03d92011-11-30 23:07:30 +01002214 info->fix.line_length = ch->pitch;
2215
2216 if (ch->format->yuv)
2217 info->fix.visual = FB_VISUAL_FOURCC;
2218 else
2219 info->fix.visual = FB_VISUAL_TRUECOLOR;
2220
Laurent Pincharta67f3792011-11-29 14:37:35 +01002221 if (ch->format->fourcc == V4L2_PIX_FMT_NV12 ||
2222 ch->format->fourcc == V4L2_PIX_FMT_NV21)
2223 info->fix.ypanstep = 2;
2224
2225 /* Initialize variable screen information using the first mode as
2226 * default. The default Y virtual resolution is twice the panel size to
2227 * allow for double-buffering.
2228 */
2229 var = &info->var;
2230 fb_videomode_to_var(var, mode);
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002231 var->width = ch->cfg->panel_cfg.width;
2232 var->height = ch->cfg->panel_cfg.height;
Laurent Pincharta67f3792011-11-29 14:37:35 +01002233 var->yres_virtual = var->yres * 2;
2234 var->activate = FB_ACTIVATE_NOW;
2235
2236 /* Use the legacy API by default for RGB formats, and the FOURCC API
2237 * for YUV formats.
2238 */
2239 if (!ch->format->yuv)
2240 var->bits_per_pixel = ch->format->bpp;
2241 else
2242 var->grayscale = ch->format->fourcc;
2243
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01002244 ret = sh_mobile_lcdc_check_var(var, info);
Laurent Pincharta67f3792011-11-29 14:37:35 +01002245 if (ret)
2246 return ret;
2247
Laurent Pincharta67f3792011-11-29 14:37:35 +01002248 return 0;
2249}
2250
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002251/* -----------------------------------------------------------------------------
2252 * Backlight
2253 */
2254
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002255static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
2256{
2257 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002258 int brightness = bdev->props.brightness;
2259
2260 if (bdev->props.power != FB_BLANK_UNBLANK ||
2261 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
2262 brightness = 0;
2263
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002264 return ch->cfg->bl_info.set_brightness(brightness);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002265}
2266
2267static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
2268{
2269 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002270
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002271 return ch->cfg->bl_info.get_brightness();
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002272}
2273
2274static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
2275 struct fb_info *info)
2276{
2277 return (info->bl_dev == bdev);
2278}
2279
2280static struct backlight_ops sh_mobile_lcdc_bl_ops = {
2281 .options = BL_CORE_SUSPENDRESUME,
2282 .update_status = sh_mobile_lcdc_update_bl,
2283 .get_brightness = sh_mobile_lcdc_get_brightness,
2284 .check_fb = sh_mobile_lcdc_check_fb,
2285};
2286
2287static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
2288 struct sh_mobile_lcdc_chan *ch)
2289{
2290 struct backlight_device *bl;
2291
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002292 bl = backlight_device_register(ch->cfg->bl_info.name, parent, ch,
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002293 &sh_mobile_lcdc_bl_ops, NULL);
Dan Carpenterbeee1f22011-03-21 15:03:13 +00002294 if (IS_ERR(bl)) {
2295 dev_err(parent, "unable to register backlight device: %ld\n",
2296 PTR_ERR(bl));
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002297 return NULL;
2298 }
2299
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002300 bl->props.max_brightness = ch->cfg->bl_info.max_brightness;
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002301 bl->props.brightness = bl->props.max_brightness;
2302 backlight_update_status(bl);
2303
2304 return bl;
2305}
2306
2307static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
2308{
2309 backlight_device_unregister(bdev);
2310}
2311
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002312/* -----------------------------------------------------------------------------
2313 * Power management
2314 */
2315
Magnus Damm2feb0752009-03-13 15:36:55 +00002316static int sh_mobile_lcdc_suspend(struct device *dev)
2317{
2318 struct platform_device *pdev = to_platform_device(dev);
2319
2320 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
2321 return 0;
2322}
2323
2324static int sh_mobile_lcdc_resume(struct device *dev)
2325{
2326 struct platform_device *pdev = to_platform_device(dev);
2327
2328 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
2329}
2330
Magnus Damm0246c472009-08-14 10:49:08 +00002331static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
2332{
2333 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02002334 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00002335
2336 /* turn off LCDC hardware */
Laurent Pinchart2427bb22011-07-13 12:13:47 +02002337 lcdc_write(priv, _LDCNT1R, 0);
2338
Magnus Damm0246c472009-08-14 10:49:08 +00002339 return 0;
2340}
2341
2342static int sh_mobile_lcdc_runtime_resume(struct device *dev)
2343{
2344 struct platform_device *pdev = to_platform_device(dev);
Laurent Pinchart2427bb22011-07-13 12:13:47 +02002345 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm0246c472009-08-14 10:49:08 +00002346
Laurent Pinchart2427bb22011-07-13 12:13:47 +02002347 __sh_mobile_lcdc_start(priv);
Magnus Damm0246c472009-08-14 10:49:08 +00002348
2349 return 0;
2350}
2351
Alexey Dobriyan47145212009-12-14 18:00:08 -08002352static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
Magnus Damm2feb0752009-03-13 15:36:55 +00002353 .suspend = sh_mobile_lcdc_suspend,
2354 .resume = sh_mobile_lcdc_resume,
Magnus Damm0246c472009-08-14 10:49:08 +00002355 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
2356 .runtime_resume = sh_mobile_lcdc_runtime_resume,
Magnus Damm2feb0752009-03-13 15:36:55 +00002357};
2358
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002359/* -----------------------------------------------------------------------------
2360 * Framebuffer notifier
2361 */
2362
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00002363/* locking: called with info->lock held */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002364static int sh_mobile_lcdc_notify(struct notifier_block *nb,
2365 unsigned long action, void *data)
2366{
2367 struct fb_event *event = data;
2368 struct fb_info *info = event->info;
2369 struct sh_mobile_lcdc_chan *ch = info->par;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002370
2371 if (&ch->lcdc->notifier != nb)
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00002372 return NOTIFY_DONE;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002373
2374 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
2375 __func__, action, event->data);
2376
2377 switch(action) {
2378 case FB_EVENT_SUSPEND:
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02002379 sh_mobile_lcdc_display_off(ch);
Guennadi Liakhovetskiafe417c2010-09-03 07:20:39 +00002380 sh_mobile_lcdc_stop(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002381 break;
2382 case FB_EVENT_RESUME:
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00002383 mutex_lock(&ch->open_lock);
2384 sh_mobile_fb_reconfig(info);
2385 mutex_unlock(&ch->open_lock);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002386
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02002387 sh_mobile_lcdc_display_on(ch);
Guennadi Liakhovetskiebe5e122011-05-05 16:33:40 +00002388 sh_mobile_lcdc_start(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002389 }
2390
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00002391 return NOTIFY_OK;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002392}
2393
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002394/* -----------------------------------------------------------------------------
2395 * Probe/remove and driver init/exit
2396 */
2397
Laurent Pinchart217e9c42011-09-07 11:59:00 +02002398static const struct fb_videomode default_720p __devinitconst = {
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002399 .name = "HDMI 720p",
2400 .xres = 1280,
2401 .yres = 720,
2402
2403 .left_margin = 220,
2404 .right_margin = 110,
2405 .hsync_len = 40,
2406
2407 .upper_margin = 20,
2408 .lower_margin = 5,
2409 .vsync_len = 5,
2410
2411 .pixclock = 13468,
2412 .refresh = 60,
2413 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
2414};
2415
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002416static int sh_mobile_lcdc_remove(struct platform_device *pdev)
2417{
2418 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002419 unsigned int i;
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002420
2421 fb_unregister_client(&priv->notifier);
2422
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002423 for (i = 0; i < ARRAY_SIZE(priv->overlays); i++)
2424 sh_mobile_lcdc_overlay_fb_unregister(&priv->overlays[i]);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002425 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
Laurent Pincharta67f3792011-11-29 14:37:35 +01002426 sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002427
2428 sh_mobile_lcdc_stop(priv);
2429
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002430 for (i = 0; i < ARRAY_SIZE(priv->overlays); i++) {
2431 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2432
2433 sh_mobile_lcdc_overlay_fb_cleanup(ovl);
2434
2435 if (ovl->fb_mem)
2436 dma_free_coherent(&pdev->dev, ovl->fb_size,
2437 ovl->fb_mem, ovl->dma_handle);
2438 }
2439
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002440 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02002441 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002442
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02002443 if (ch->tx_dev) {
2444 ch->tx_dev->lcdc = NULL;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002445 module_put(ch->cfg->tx_dev->dev.driver->owner);
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02002446 }
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02002447
Laurent Pincharta67f3792011-11-29 14:37:35 +01002448 sh_mobile_lcdc_channel_fb_cleanup(ch);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002449
Laurent Pincharta67f3792011-11-29 14:37:35 +01002450 if (ch->fb_mem)
2451 dma_free_coherent(&pdev->dev, ch->fb_size,
2452 ch->fb_mem, ch->dma_handle);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002453 }
2454
2455 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
2456 if (priv->ch[i].bl)
2457 sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
2458 }
2459
Laurent Pinchart4774c122011-09-07 15:47:07 +02002460 if (priv->dot_clk) {
2461 pm_runtime_disable(&pdev->dev);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002462 clk_put(priv->dot_clk);
Laurent Pinchart4774c122011-09-07 15:47:07 +02002463 }
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002464
2465 if (priv->base)
2466 iounmap(priv->base);
2467
2468 if (priv->irq)
2469 free_irq(priv->irq, priv);
2470 kfree(priv);
2471 return 0;
2472}
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002473
Laurent Pinchart217e9c42011-09-07 11:59:00 +02002474static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002475{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002476 int interface_type = ch->cfg->interface_type;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002477
2478 switch (interface_type) {
2479 case RGB8:
2480 case RGB9:
2481 case RGB12A:
2482 case RGB12B:
2483 case RGB16:
2484 case RGB18:
2485 case RGB24:
2486 case SYS8A:
2487 case SYS8B:
2488 case SYS8C:
2489 case SYS8D:
2490 case SYS9:
2491 case SYS12:
2492 case SYS16A:
2493 case SYS16B:
2494 case SYS16C:
2495 case SYS18:
2496 case SYS24:
2497 break;
2498 default:
2499 return -EINVAL;
2500 }
2501
2502 /* SUBLCD only supports SYS interface */
2503 if (lcdc_chan_is_sublcd(ch)) {
2504 if (!(interface_type & LDMT1R_IFM))
2505 return -EINVAL;
2506
2507 interface_type &= ~LDMT1R_IFM;
2508 }
2509
2510 ch->ldmt1r_value = interface_type;
2511 return 0;
2512}
2513
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02002514static int __devinit
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002515sh_mobile_lcdc_overlay_init(struct sh_mobile_lcdc_priv *priv,
2516 struct sh_mobile_lcdc_overlay *ovl)
2517{
2518 const struct sh_mobile_lcdc_format_info *format;
2519 int ret;
2520
2521 if (ovl->cfg->fourcc == 0)
2522 return 0;
2523
2524 /* Validate the format. */
2525 format = sh_mobile_format_info(ovl->cfg->fourcc);
2526 if (format == NULL) {
2527 dev_err(priv->dev, "Invalid FOURCC %08x\n", ovl->cfg->fourcc);
2528 return -EINVAL;
2529 }
2530
2531 ovl->enabled = false;
2532 ovl->mode = LCDC_OVERLAY_BLEND;
2533 ovl->alpha = 255;
2534 ovl->rop3 = 0;
2535 ovl->pos_x = 0;
2536 ovl->pos_y = 0;
2537
2538 /* The default Y virtual resolution is twice the panel size to allow for
2539 * double-buffering.
2540 */
2541 ovl->format = format;
2542 ovl->xres = ovl->cfg->max_xres;
2543 ovl->xres_virtual = ovl->xres;
2544 ovl->yres = ovl->cfg->max_yres;
2545 ovl->yres_virtual = ovl->yres * 2;
2546
2547 if (!format->yuv)
2548 ovl->pitch = ovl->xres * format->bpp / 8;
2549 else
2550 ovl->pitch = ovl->xres;
2551
2552 /* Allocate frame buffer memory. */
2553 ovl->fb_size = ovl->cfg->max_xres * ovl->cfg->max_yres
2554 * format->bpp / 8 * 2;
2555 ovl->fb_mem = dma_alloc_coherent(priv->dev, ovl->fb_size,
2556 &ovl->dma_handle, GFP_KERNEL);
2557 if (!ovl->fb_mem) {
2558 dev_err(priv->dev, "unable to allocate buffer\n");
2559 return -ENOMEM;
2560 }
2561
2562 ret = sh_mobile_lcdc_overlay_fb_init(ovl);
2563 if (ret < 0)
2564 return ret;
2565
2566 return 0;
2567}
2568
2569static int __devinit
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02002570sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv *priv,
2571 struct sh_mobile_lcdc_chan *ch)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002572{
Laurent Pinchart105784b2011-11-29 15:58:10 +01002573 const struct sh_mobile_lcdc_format_info *format;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002574 const struct sh_mobile_lcdc_chan_cfg *cfg = ch->cfg;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002575 const struct fb_videomode *max_mode;
2576 const struct fb_videomode *mode;
Laurent Pincharta67f3792011-11-29 14:37:35 +01002577 unsigned int num_modes;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002578 unsigned int max_size;
Laurent Pincharta67f3792011-11-29 14:37:35 +01002579 unsigned int i;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002580
Laurent Pincharta67472a2011-08-31 13:00:59 +02002581 mutex_init(&ch->open_lock);
Laurent Pinchartecd29942011-09-18 14:14:46 +02002582 ch->notify = sh_mobile_lcdc_display_notify;
Laurent Pincharta67472a2011-08-31 13:00:59 +02002583
Laurent Pinchart105784b2011-11-29 15:58:10 +01002584 /* Validate the format. */
2585 format = sh_mobile_format_info(cfg->fourcc);
2586 if (format == NULL) {
2587 dev_err(priv->dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
2588 return -EINVAL;
2589 }
2590
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002591 /* Iterate through the modes to validate them and find the highest
2592 * resolution.
2593 */
2594 max_mode = NULL;
2595 max_size = 0;
2596
Laurent Pinchart93ff2592011-11-29 14:33:41 +01002597 for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002598 unsigned int size = mode->yres * mode->xres;
2599
Laurent Pinchartedd153a2011-12-13 14:02:28 +01002600 /* NV12/NV21 buffers must have even number of lines */
2601 if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
2602 cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02002603 dev_err(priv->dev, "yres must be multiple of 2 for "
2604 "YCbCr420 mode.\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002605 return -EINVAL;
2606 }
2607
2608 if (size > max_size) {
2609 max_mode = mode;
2610 max_size = size;
2611 }
2612 }
2613
2614 if (!max_size)
2615 max_size = MAX_XRES * MAX_YRES;
2616 else
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02002617 dev_dbg(priv->dev, "Found largest videomode %ux%u\n",
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002618 max_mode->xres, max_mode->yres);
2619
Laurent Pinchart93ff2592011-11-29 14:33:41 +01002620 if (cfg->lcd_modes == NULL) {
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002621 mode = &default_720p;
Laurent Pinchart93ff2592011-11-29 14:33:41 +01002622 num_modes = 1;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002623 } else {
Laurent Pinchart93ff2592011-11-29 14:33:41 +01002624 mode = cfg->lcd_modes;
2625 num_modes = cfg->num_modes;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002626 }
2627
Laurent Pinchart58f03d92011-11-30 23:07:30 +01002628 /* Use the first mode as default. */
2629 ch->format = format;
2630 ch->xres = mode->xres;
2631 ch->xres_virtual = mode->xres;
2632 ch->yres = mode->yres;
2633 ch->yres_virtual = mode->yres * 2;
2634
2635 if (!format->yuv) {
2636 ch->colorspace = V4L2_COLORSPACE_SRGB;
2637 ch->pitch = ch->xres * format->bpp / 8;
2638 } else {
2639 ch->colorspace = V4L2_COLORSPACE_REC709;
2640 ch->pitch = ch->xres;
2641 }
2642
Laurent Pincharta67f3792011-11-29 14:37:35 +01002643 ch->display.width = cfg->panel_cfg.width;
2644 ch->display.height = cfg->panel_cfg.height;
2645 ch->display.mode = *mode;
2646
2647 /* Allocate frame buffer memory. */
2648 ch->fb_size = max_size * format->bpp / 8 * 2;
2649 ch->fb_mem = dma_alloc_coherent(priv->dev, ch->fb_size, &ch->dma_handle,
2650 GFP_KERNEL);
2651 if (ch->fb_mem == NULL) {
2652 dev_err(priv->dev, "unable to allocate buffer\n");
2653 return -ENOMEM;
2654 }
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002655
Laurent Pinchart13f80ee2011-11-29 01:46:12 +01002656 /* Initialize the transmitter device if present. */
2657 if (cfg->tx_dev) {
2658 if (!cfg->tx_dev->dev.driver ||
2659 !try_module_get(cfg->tx_dev->dev.driver->owner)) {
2660 dev_warn(priv->dev,
2661 "unable to get transmitter device\n");
2662 return -EINVAL;
2663 }
2664 ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
2665 ch->tx_dev->lcdc = ch;
2666 ch->tx_dev->def_mode = *mode;
2667 }
2668
Laurent Pincharta67f3792011-11-29 14:37:35 +01002669 return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002670}
2671
Uwe Kleine-Königc2e13032010-02-04 20:56:51 +01002672static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002673{
Guennadi Liakhovetski01ac25b52010-09-03 07:20:00 +00002674 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002675 struct sh_mobile_lcdc_priv *priv;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002676 struct resource *res;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002677 int num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002678 int error;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002679 int i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002680
Guennadi Liakhovetski01ac25b52010-09-03 07:20:00 +00002681 if (!pdata) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002682 dev_err(&pdev->dev, "no platform data defined\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002683 return -EINVAL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002684 }
2685
2686 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Damm85645572008-12-19 15:34:41 +09002687 i = platform_get_irq(pdev, 0);
2688 if (!res || i < 0) {
2689 dev_err(&pdev->dev, "cannot get platform resources\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002690 return -ENOENT;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002691 }
2692
2693 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2694 if (!priv) {
2695 dev_err(&pdev->dev, "cannot allocate device data\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002696 return -ENOMEM;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002697 }
2698
Laurent Pinchart4774c122011-09-07 15:47:07 +02002699 priv->dev = &pdev->dev;
2700 priv->meram_dev = pdata->meram_dev;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002701 platform_set_drvdata(pdev, priv);
2702
Yong Zhangf8798cc2011-09-22 16:59:16 +08002703 error = request_irq(i, sh_mobile_lcdc_irq, 0,
Kay Sievers7ad33e72009-03-24 16:38:21 -07002704 dev_name(&pdev->dev), priv);
Magnus Damm85645572008-12-19 15:34:41 +09002705 if (error) {
2706 dev_err(&pdev->dev, "unable to request irq\n");
2707 goto err1;
2708 }
2709
2710 priv->irq = i;
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +00002711 atomic_set(&priv->hw_usecnt, -1);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002712
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002713 for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
2714 struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002715
Guennadi Liakhovetski01ac25b52010-09-03 07:20:00 +00002716 ch->lcdc = priv;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002717 ch->cfg = &pdata->ch[i];
Guennadi Liakhovetski01ac25b52010-09-03 07:20:00 +00002718
2719 error = sh_mobile_lcdc_check_interface(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002720 if (error) {
2721 dev_err(&pdev->dev, "unsupported interface type\n");
2722 goto err1;
2723 }
Guennadi Liakhovetski01ac25b52010-09-03 07:20:00 +00002724 init_waitqueue_head(&ch->frame_end_wait);
2725 init_completion(&ch->vsync_completion);
2726 ch->pan_offset = 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002727
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002728 /* probe the backlight is there is one defined */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002729 if (ch->cfg->bl_info.max_brightness)
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002730 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
2731
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002732 switch (pdata->ch[i].chan) {
2733 case LCDC_CHAN_MAINLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02002734 ch->enabled = LDCNT2R_ME;
Guennadi Liakhovetski01ac25b52010-09-03 07:20:00 +00002735 ch->reg_offs = lcdc_offs_mainlcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002736 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002737 break;
2738 case LCDC_CHAN_SUBLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02002739 ch->enabled = LDCNT2R_SE;
Guennadi Liakhovetski01ac25b52010-09-03 07:20:00 +00002740 ch->reg_offs = lcdc_offs_sublcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002741 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002742 break;
2743 }
2744 }
2745
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002746 if (!num_channels) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002747 dev_err(&pdev->dev, "no channels defined\n");
2748 error = -EINVAL;
2749 goto err1;
2750 }
2751
Laurent Pinchartedd153a2011-12-13 14:02:28 +01002752 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002753 if (num_channels == 2)
Laurent Pinchartedd153a2011-12-13 14:02:28 +01002754 priv->forced_fourcc = pdata->ch[0].fourcc;
Magnus Damm417d4822011-01-05 10:21:00 +00002755
Guennadi Liakhovetskidba6f382010-06-30 09:26:35 +00002756 priv->base = ioremap_nocache(res->start, resource_size(res));
2757 if (!priv->base)
2758 goto err1;
2759
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02002760 error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002761 if (error) {
2762 dev_err(&pdev->dev, "unable to setup clocks\n");
2763 goto err1;
2764 }
2765
Laurent Pinchart4774c122011-09-07 15:47:07 +02002766 /* Enable runtime PM. */
2767 pm_runtime_enable(&pdev->dev);
Damian7caa4342011-05-18 11:10:07 +00002768
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002769 for (i = 0; i < num_channels; i++) {
Guennadi Liakhovetski01ac25b52010-09-03 07:20:00 +00002770 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002771
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02002772 error = sh_mobile_lcdc_channel_init(priv, ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002773 if (error)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002774 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002775 }
2776
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002777 for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
2778 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2779
2780 ovl->cfg = &pdata->overlays[i];
2781 ovl->channel = &priv->ch[0];
2782
2783 error = sh_mobile_lcdc_overlay_init(priv, ovl);
2784 if (error)
2785 goto err1;
2786 }
2787
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002788 error = sh_mobile_lcdc_start(priv);
2789 if (error) {
2790 dev_err(&pdev->dev, "unable to start hardware\n");
2791 goto err1;
2792 }
2793
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002794 for (i = 0; i < num_channels; i++) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00002795 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Paul Mundt1c6a3072009-07-01 06:50:31 +00002796
Laurent Pincharta67f3792011-11-29 14:37:35 +01002797 error = sh_mobile_lcdc_channel_fb_register(ch);
2798 if (error)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002799 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002800 }
2801
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002802 for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
2803 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2804
2805 error = sh_mobile_lcdc_overlay_fb_register(ovl);
2806 if (error)
2807 goto err1;
2808 }
2809
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002810 /* Failure ignored */
2811 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
2812 fb_register_client(&priv->notifier);
2813
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002814 return 0;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002815err1:
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002816 sh_mobile_lcdc_remove(pdev);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002817
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002818 return error;
2819}
2820
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002821static struct platform_driver sh_mobile_lcdc_driver = {
2822 .driver = {
2823 .name = "sh_mobile_lcdc_fb",
2824 .owner = THIS_MODULE,
Magnus Damm2feb0752009-03-13 15:36:55 +00002825 .pm = &sh_mobile_lcdc_dev_pm_ops,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002826 },
2827 .probe = sh_mobile_lcdc_probe,
2828 .remove = sh_mobile_lcdc_remove,
2829};
2830
Axel Lin4277f2c2011-11-26 10:25:54 +08002831module_platform_driver(sh_mobile_lcdc_driver);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002832
2833MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
2834MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
2835MODULE_LICENSE("GPL v2");