blob: dc46be38c9706f4b0ef8e562475de1dd08a02ef2 [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>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070032
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +000033#include "sh_mobile_lcdcfb.h"
34
Laurent Pinchartc5deac32011-12-12 18:43:16 +010035/* ----------------------------------------------------------------------------
36 * Overlay register definitions
37 */
38
39#define LDBCR 0xb00
40#define LDBCR_UPC(n) (1 << ((n) + 16))
41#define LDBCR_UPF(n) (1 << ((n) + 8))
42#define LDBCR_UPD(n) (1 << ((n) + 0))
43#define LDBnBSIFR(n) (0xb20 + (n) * 0x20 + 0x00)
44#define LDBBSIFR_EN (1 << 31)
45#define LDBBSIFR_VS (1 << 29)
46#define LDBBSIFR_BRSEL (1 << 28)
47#define LDBBSIFR_MX (1 << 27)
48#define LDBBSIFR_MY (1 << 26)
49#define LDBBSIFR_CV3 (3 << 24)
50#define LDBBSIFR_CV2 (2 << 24)
51#define LDBBSIFR_CV1 (1 << 24)
52#define LDBBSIFR_CV0 (0 << 24)
53#define LDBBSIFR_CV_MASK (3 << 24)
54#define LDBBSIFR_LAY_MASK (0xff << 16)
55#define LDBBSIFR_LAY_SHIFT 16
56#define LDBBSIFR_ROP3_MASK (0xff << 16)
57#define LDBBSIFR_ROP3_SHIFT 16
58#define LDBBSIFR_AL_PL8 (3 << 14)
59#define LDBBSIFR_AL_PL1 (2 << 14)
60#define LDBBSIFR_AL_PK (1 << 14)
61#define LDBBSIFR_AL_1 (0 << 14)
62#define LDBBSIFR_AL_MASK (3 << 14)
63#define LDBBSIFR_SWPL (1 << 10)
64#define LDBBSIFR_SWPW (1 << 9)
65#define LDBBSIFR_SWPB (1 << 8)
66#define LDBBSIFR_RY (1 << 7)
67#define LDBBSIFR_CHRR_420 (2 << 0)
68#define LDBBSIFR_CHRR_422 (1 << 0)
69#define LDBBSIFR_CHRR_444 (0 << 0)
70#define LDBBSIFR_RPKF_ARGB32 (0x00 << 0)
71#define LDBBSIFR_RPKF_RGB16 (0x03 << 0)
72#define LDBBSIFR_RPKF_RGB24 (0x0b << 0)
73#define LDBBSIFR_RPKF_MASK (0x1f << 0)
74#define LDBnBSSZR(n) (0xb20 + (n) * 0x20 + 0x04)
75#define LDBBSSZR_BVSS_MASK (0xfff << 16)
76#define LDBBSSZR_BVSS_SHIFT 16
77#define LDBBSSZR_BHSS_MASK (0xfff << 0)
78#define LDBBSSZR_BHSS_SHIFT 0
79#define LDBnBLOCR(n) (0xb20 + (n) * 0x20 + 0x08)
80#define LDBBLOCR_CVLC_MASK (0xfff << 16)
81#define LDBBLOCR_CVLC_SHIFT 16
82#define LDBBLOCR_CHLC_MASK (0xfff << 0)
83#define LDBBLOCR_CHLC_SHIFT 0
84#define LDBnBSMWR(n) (0xb20 + (n) * 0x20 + 0x0c)
85#define LDBBSMWR_BSMWA_MASK (0xffff << 16)
86#define LDBBSMWR_BSMWA_SHIFT 16
87#define LDBBSMWR_BSMW_MASK (0xffff << 0)
88#define LDBBSMWR_BSMW_SHIFT 0
89#define LDBnBSAYR(n) (0xb20 + (n) * 0x20 + 0x10)
90#define LDBBSAYR_FG1A_MASK (0xff << 24)
91#define LDBBSAYR_FG1A_SHIFT 24
92#define LDBBSAYR_FG1R_MASK (0xff << 16)
93#define LDBBSAYR_FG1R_SHIFT 16
94#define LDBBSAYR_FG1G_MASK (0xff << 8)
95#define LDBBSAYR_FG1G_SHIFT 8
96#define LDBBSAYR_FG1B_MASK (0xff << 0)
97#define LDBBSAYR_FG1B_SHIFT 0
98#define LDBnBSACR(n) (0xb20 + (n) * 0x20 + 0x14)
99#define LDBBSACR_FG2A_MASK (0xff << 24)
100#define LDBBSACR_FG2A_SHIFT 24
101#define LDBBSACR_FG2R_MASK (0xff << 16)
102#define LDBBSACR_FG2R_SHIFT 16
103#define LDBBSACR_FG2G_MASK (0xff << 8)
104#define LDBBSACR_FG2G_SHIFT 8
105#define LDBBSACR_FG2B_MASK (0xff << 0)
106#define LDBBSACR_FG2B_SHIFT 0
107#define LDBnBSAAR(n) (0xb20 + (n) * 0x20 + 0x18)
108#define LDBBSAAR_AP_MASK (0xff << 24)
109#define LDBBSAAR_AP_SHIFT 24
110#define LDBBSAAR_R_MASK (0xff << 16)
111#define LDBBSAAR_R_SHIFT 16
112#define LDBBSAAR_GY_MASK (0xff << 8)
113#define LDBBSAAR_GY_SHIFT 8
114#define LDBBSAAR_B_MASK (0xff << 0)
115#define LDBBSAAR_B_SHIFT 0
116#define LDBnBPPCR(n) (0xb20 + (n) * 0x20 + 0x1c)
117#define LDBBPPCR_AP_MASK (0xff << 24)
118#define LDBBPPCR_AP_SHIFT 24
119#define LDBBPPCR_R_MASK (0xff << 16)
120#define LDBBPPCR_R_SHIFT 16
121#define LDBBPPCR_GY_MASK (0xff << 8)
122#define LDBBPPCR_GY_SHIFT 8
123#define LDBBPPCR_B_MASK (0xff << 0)
124#define LDBBPPCR_B_SHIFT 0
125#define LDBnBBGCL(n) (0xb10 + (n) * 0x04)
126#define LDBBBGCL_BGA_MASK (0xff << 24)
127#define LDBBBGCL_BGA_SHIFT 24
128#define LDBBBGCL_BGR_MASK (0xff << 16)
129#define LDBBBGCL_BGR_SHIFT 16
130#define LDBBBGCL_BGG_MASK (0xff << 8)
131#define LDBBBGCL_BGG_SHIFT 8
132#define LDBBBGCL_BGB_MASK (0xff << 0)
133#define LDBBBGCL_BGB_SHIFT 0
134
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000135#define SIDE_B_OFFSET 0x1000
136#define MIRROR_OFFSET 0x2000
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700137
Guennadi Liakhovetskid2ecbab2010-11-04 11:06:06 +0000138#define MAX_XRES 1920
139#define MAX_YRES 1080
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700140
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100141enum sh_mobile_lcdc_overlay_mode {
142 LCDC_OVERLAY_BLEND,
143 LCDC_OVERLAY_ROP3,
144};
145
146/*
147 * struct sh_mobile_lcdc_overlay - LCDC display overlay
148 *
149 * @channel: LCDC channel this overlay belongs to
150 * @cfg: Overlay configuration
151 * @info: Frame buffer device
152 * @index: Overlay index (0-3)
153 * @base: Overlay registers base address
154 * @enabled: True if the overlay is enabled
155 * @mode: Overlay blending mode (alpha blend or ROP3)
156 * @alpha: Global alpha blending value (0-255, for alpha blending mode)
157 * @rop3: Raster operation (for ROP3 mode)
158 * @fb_mem: Frame buffer virtual memory address
159 * @fb_size: Frame buffer size in bytes
160 * @dma_handle: Frame buffer DMA address
161 * @base_addr_y: Overlay base address (RGB or luma component)
162 * @base_addr_c: Overlay base address (chroma component)
Laurent Pincharta4aa25f2012-03-15 18:15:37 +0100163 * @pan_y_offset: Panning linear offset in bytes (luma component)
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100164 * @format: Current pixelf format
165 * @xres: Horizontal visible resolution
166 * @xres_virtual: Horizontal total resolution
167 * @yres: Vertical visible resolution
168 * @yres_virtual: Vertical total resolution
169 * @pitch: Overlay line pitch
170 * @pos_x: Horizontal overlay position
171 * @pos_y: Vertical overlay position
172 */
173struct sh_mobile_lcdc_overlay {
174 struct sh_mobile_lcdc_chan *channel;
175
176 const struct sh_mobile_lcdc_overlay_cfg *cfg;
177 struct fb_info *info;
178
179 unsigned int index;
180 unsigned long base;
181
182 bool enabled;
183 enum sh_mobile_lcdc_overlay_mode mode;
184 unsigned int alpha;
185 unsigned int rop3;
186
187 void *fb_mem;
188 unsigned long fb_size;
189
190 dma_addr_t dma_handle;
191 unsigned long base_addr_y;
192 unsigned long base_addr_c;
Laurent Pincharta4aa25f2012-03-15 18:15:37 +0100193 unsigned long pan_y_offset;
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100194
195 const struct sh_mobile_lcdc_format_info *format;
196 unsigned int xres;
197 unsigned int xres_virtual;
198 unsigned int yres;
199 unsigned int yres_virtual;
200 unsigned int pitch;
201 int pos_x;
202 int pos_y;
203};
204
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200205struct sh_mobile_lcdc_priv {
206 void __iomem *base;
207 int irq;
208 atomic_t hw_usecnt;
209 struct device *dev;
210 struct clk *dot_clk;
211 unsigned long lddckr;
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100212
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200213 struct sh_mobile_lcdc_chan ch[2];
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100214 struct sh_mobile_lcdc_overlay overlays[4];
215
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200216 struct notifier_block notifier;
217 int started;
218 int forced_fourcc; /* 2 channel LCDC must share fourcc setting */
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200219};
220
221/* -----------------------------------------------------------------------------
222 * Registers access
223 */
224
Magnus Damm0246c472009-08-14 10:49:08 +0000225static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700226 [LDDCKPAT1R] = 0x400,
227 [LDDCKPAT2R] = 0x404,
228 [LDMT1R] = 0x418,
229 [LDMT2R] = 0x41c,
230 [LDMT3R] = 0x420,
231 [LDDFR] = 0x424,
232 [LDSM1R] = 0x428,
Magnus Damm85645572008-12-19 15:34:41 +0900233 [LDSM2R] = 0x42c,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700234 [LDSA1R] = 0x430,
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000235 [LDSA2R] = 0x434,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700236 [LDMLSR] = 0x438,
237 [LDHCNR] = 0x448,
238 [LDHSYNR] = 0x44c,
239 [LDVLNR] = 0x450,
240 [LDVSYNR] = 0x454,
241 [LDPMR] = 0x460,
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000242 [LDHAJR] = 0x4a0,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700243};
244
Magnus Damm0246c472009-08-14 10:49:08 +0000245static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700246 [LDDCKPAT1R] = 0x408,
247 [LDDCKPAT2R] = 0x40c,
248 [LDMT1R] = 0x600,
249 [LDMT2R] = 0x604,
250 [LDMT3R] = 0x608,
251 [LDDFR] = 0x60c,
252 [LDSM1R] = 0x610,
Magnus Damm85645572008-12-19 15:34:41 +0900253 [LDSM2R] = 0x614,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700254 [LDSA1R] = 0x618,
255 [LDMLSR] = 0x620,
256 [LDHCNR] = 0x624,
257 [LDHSYNR] = 0x628,
258 [LDVLNR] = 0x62c,
259 [LDVSYNR] = 0x630,
260 [LDPMR] = 0x63c,
261};
262
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000263static bool banked(int reg_nr)
264{
265 switch (reg_nr) {
266 case LDMT1R:
267 case LDMT2R:
268 case LDMT3R:
269 case LDDFR:
270 case LDSM1R:
271 case LDSA1R:
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000272 case LDSA2R:
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000273 case LDMLSR:
274 case LDHCNR:
275 case LDHSYNR:
276 case LDVLNR:
277 case LDVSYNR:
278 return true;
279 }
280 return false;
281}
282
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200283static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
284{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100285 return chan->cfg->chan == LCDC_CHAN_SUBLCD;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200286}
287
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700288static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
289 int reg_nr, unsigned long data)
290{
291 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000292 if (banked(reg_nr))
293 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
294 SIDE_B_OFFSET);
295}
296
297static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
298 int reg_nr, unsigned long data)
299{
300 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
301 MIRROR_OFFSET);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700302}
303
304static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
305 int reg_nr)
306{
307 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
308}
309
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100310static void lcdc_write_overlay(struct sh_mobile_lcdc_overlay *ovl,
311 int reg, unsigned long data)
312{
313 iowrite32(data, ovl->channel->lcdc->base + reg);
314 iowrite32(data, ovl->channel->lcdc->base + reg + SIDE_B_OFFSET);
315}
316
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700317static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
318 unsigned long reg_offs, unsigned long data)
319{
320 iowrite32(data, priv->base + reg_offs);
321}
322
323static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
324 unsigned long reg_offs)
325{
326 return ioread32(priv->base + reg_offs);
327}
328
329static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
330 unsigned long reg_offs,
331 unsigned long mask, unsigned long until)
332{
333 while ((lcdc_read(priv, reg_offs) & mask) != until)
334 cpu_relax();
335}
336
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200337/* -----------------------------------------------------------------------------
338 * Clock management
339 */
340
341static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700342{
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200343 if (atomic_inc_and_test(&priv->hw_usecnt)) {
344 if (priv->dot_clk)
Laurent Pinchartdeccd242013-10-28 23:49:29 +0100345 clk_prepare_enable(priv->dot_clk);
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200346 pm_runtime_get_sync(priv->dev);
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200347 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700348}
349
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200350static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
351{
352 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200353 pm_runtime_put(priv->dev);
354 if (priv->dot_clk)
Laurent Pinchartdeccd242013-10-28 23:49:29 +0100355 clk_disable_unprepare(priv->dot_clk);
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200356 }
357}
358
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200359static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv *priv,
360 int clock_source)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200361{
Laurent Pinchart4774c122011-09-07 15:47:07 +0200362 struct clk *clk;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200363 char *str;
364
365 switch (clock_source) {
366 case LCDC_CLK_BUS:
367 str = "bus_clk";
368 priv->lddckr = LDDCKR_ICKSEL_BUS;
369 break;
370 case LCDC_CLK_PERIPHERAL:
371 str = "peripheral_clk";
372 priv->lddckr = LDDCKR_ICKSEL_MIPI;
373 break;
374 case LCDC_CLK_EXTERNAL:
375 str = NULL;
376 priv->lddckr = LDDCKR_ICKSEL_HDMI;
377 break;
378 default:
379 return -EINVAL;
380 }
381
Laurent Pinchart4774c122011-09-07 15:47:07 +0200382 if (str == NULL)
383 return 0;
384
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200385 clk = clk_get(priv->dev, str);
Laurent Pinchart4774c122011-09-07 15:47:07 +0200386 if (IS_ERR(clk)) {
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +0200387 dev_err(priv->dev, "cannot get dot clock %s\n", str);
Laurent Pinchart4774c122011-09-07 15:47:07 +0200388 return PTR_ERR(clk);
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200389 }
390
Laurent Pinchart4774c122011-09-07 15:47:07 +0200391 priv->dot_clk = clk;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200392 return 0;
393}
394
395/* -----------------------------------------------------------------------------
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200396 * Display, panel and deferred I/O
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200397 */
398
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700399static void lcdc_sys_write_index(void *handle, unsigned long data)
400{
401 struct sh_mobile_lcdc_chan *ch = handle;
402
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200403 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
404 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
405 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
406 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
407 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700408}
409
410static void lcdc_sys_write_data(void *handle, unsigned long data)
411{
412 struct sh_mobile_lcdc_chan *ch = handle;
413
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200414 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
415 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
416 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
417 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
418 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700419}
420
421static unsigned long lcdc_sys_read_data(void *handle)
422{
423 struct sh_mobile_lcdc_chan *ch = handle;
424
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200425 lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
426 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
427 lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
428 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700429 udelay(1);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200430 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700431
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200432 return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700433}
434
Laurent Pinchartd38d8402012-08-13 13:56:46 +0200435static struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
Kees Cook6895aff2017-01-11 17:09:50 +0100436 .write_index = lcdc_sys_write_index,
437 .write_data = lcdc_sys_write_data,
438 .read_data = lcdc_sys_read_data,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700439};
440
Paul Mundt1c6a3072009-07-01 06:50:31 +0000441static int sh_mobile_lcdc_sginit(struct fb_info *info,
442 struct list_head *pagelist)
443{
444 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchart58f03d92011-11-30 23:07:30 +0100445 unsigned int nr_pages_max = ch->fb_size >> PAGE_SHIFT;
Paul Mundt1c6a3072009-07-01 06:50:31 +0000446 struct page *page;
447 int nr_pages = 0;
448
449 sg_init_table(ch->sglist, nr_pages_max);
450
451 list_for_each_entry(page, pagelist, lru)
452 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
453
454 return nr_pages;
455}
456
Magnus Damm85645572008-12-19 15:34:41 +0900457static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
458 struct list_head *pagelist)
459{
460 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100461 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
Magnus Damm85645572008-12-19 15:34:41 +0900462
463 /* enable clocks before accessing hardware */
464 sh_mobile_lcdc_clk_on(ch->lcdc);
465
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900466 /*
467 * It's possible to get here without anything on the pagelist via
468 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
469 * invocation. In the former case, the acceleration routines are
470 * stepped in to when using the framebuffer console causing the
471 * workqueue to be scheduled without any dirty pages on the list.
472 *
473 * Despite this, a panel update is still needed given that the
474 * acceleration routines have their own methods for writing in
475 * that still need to be updated.
476 *
477 * The fsync() and empty pagelist case could be optimized for,
478 * but we don't bother, as any application exhibiting such
479 * behaviour is fundamentally broken anyways.
480 */
481 if (!list_empty(pagelist)) {
482 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
Paul Mundt1c6a3072009-07-01 06:50:31 +0000483
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900484 /* trigger panel update */
Laurent Pincharte8363142011-11-29 14:37:35 +0100485 dma_map_sg(ch->lcdc->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
Laurent Pinchartafaad832011-09-11 22:59:04 +0200486 if (panel->start_transfer)
487 panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200488 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
Laurent Pincharte8363142011-11-29 14:37:35 +0100489 dma_unmap_sg(ch->lcdc->dev, ch->sglist, nr_pages,
490 DMA_TO_DEVICE);
Magnus Dammef61aae2009-12-07 14:20:06 +0000491 } else {
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);
Magnus Dammef61aae2009-12-07 14:20:06 +0000495 }
Magnus Damm85645572008-12-19 15:34:41 +0900496}
497
498static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
499{
500 struct fb_deferred_io *fbdefio = info->fbdefio;
501
502 if (fbdefio)
503 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
504}
505
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200506static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan *ch)
507{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100508 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200509
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200510 if (ch->tx_dev) {
Laurent Pinchart458981c2011-11-28 23:19:59 +0100511 int ret;
512
513 ret = ch->tx_dev->ops->display_on(ch->tx_dev);
514 if (ret < 0)
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200515 return;
Laurent Pinchart458981c2011-11-28 23:19:59 +0100516
517 if (ret == SH_MOBILE_LCDC_DISPLAY_DISCONNECTED)
518 ch->info->state = FBINFO_STATE_SUSPENDED;
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200519 }
520
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200521 /* HDMI must be enabled before LCDC configuration */
Laurent Pinchartafaad832011-09-11 22:59:04 +0200522 if (panel->display_on)
523 panel->display_on();
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200524}
525
526static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan *ch)
527{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100528 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200529
Laurent Pinchartafaad832011-09-11 22:59:04 +0200530 if (panel->display_off)
531 panel->display_off();
Laurent Pinchart9a2985e2011-09-11 22:59:04 +0200532
533 if (ch->tx_dev)
534 ch->tx_dev->ops->display_off(ch->tx_dev);
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +0200535}
536
Laurent Pinchartecd29942011-09-18 14:14:46 +0200537static bool
538sh_mobile_lcdc_must_reconfigure(struct sh_mobile_lcdc_chan *ch,
Laurent Pincharte0c86012011-11-29 01:05:47 +0100539 const struct fb_videomode *new_mode)
Laurent Pinchartecd29942011-09-18 14:14:46 +0200540{
Laurent Pinchartecd29942011-09-18 14:14:46 +0200541 dev_dbg(ch->info->dev, "Old %ux%u, new %ux%u\n",
Laurent Pinchart2d045592011-11-29 13:42:48 +0100542 ch->display.mode.xres, ch->display.mode.yres,
543 new_mode->xres, new_mode->yres);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200544
Laurent Pincharte0c86012011-11-29 01:05:47 +0100545 /* It can be a different monitor with an equal video-mode */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100546 if (fb_mode_is_equal(&ch->display.mode, new_mode))
Laurent Pinchartecd29942011-09-18 14:14:46 +0200547 return false;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200548
549 dev_dbg(ch->info->dev, "Switching %u -> %u lines\n",
Laurent Pinchart2d045592011-11-29 13:42:48 +0100550 ch->display.mode.yres, new_mode->yres);
551 ch->display.mode = *new_mode;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200552
553 return true;
554}
555
Laurent Pinchartd7ad3342011-11-22 00:56:58 +0100556static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
557 struct fb_info *info);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200558
559static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan *ch,
560 enum sh_mobile_lcdc_entity_event event,
Laurent Pincharte0c86012011-11-29 01:05:47 +0100561 const struct fb_videomode *mode,
562 const struct fb_monspecs *monspec)
Laurent Pinchartecd29942011-09-18 14:14:46 +0200563{
564 struct fb_info *info = ch->info;
Laurent Pincharte0c86012011-11-29 01:05:47 +0100565 struct fb_var_screeninfo var;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200566 int ret = 0;
567
568 switch (event) {
569 case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT:
570 /* HDMI plug in */
Gu Zheng3a41c5d2013-11-05 18:00:57 +0800571 console_lock();
Laurent Pinchartecd29942011-09-18 14:14:46 +0200572 if (lock_fb_info(info)) {
Gu Zheng3a41c5d2013-11-05 18:00:57 +0800573
Laurent Pinchartecd29942011-09-18 14:14:46 +0200574
Laurent Pinchart2d045592011-11-29 13:42:48 +0100575 ch->display.width = monspec->max_x * 10;
576 ch->display.height = monspec->max_y * 10;
Laurent Pincharte0c86012011-11-29 01:05:47 +0100577
578 if (!sh_mobile_lcdc_must_reconfigure(ch, mode) &&
Laurent Pinchartecd29942011-09-18 14:14:46 +0200579 info->state == FBINFO_STATE_RUNNING) {
580 /* First activation with the default monitor.
581 * Just turn on, if we run a resume here, the
582 * logo disappears.
583 */
Laurent Pinchart856e8df2012-08-13 13:56:46 +0200584 info->var.width = ch->display.width;
585 info->var.height = ch->display.height;
Laurent Pinchartecd29942011-09-18 14:14:46 +0200586 sh_mobile_lcdc_display_on(ch);
587 } else {
588 /* New monitor or have to wake up */
589 fb_set_suspend(info, 0);
590 }
591
Gu Zheng3a41c5d2013-11-05 18:00:57 +0800592
Laurent Pinchartecd29942011-09-18 14:14:46 +0200593 unlock_fb_info(info);
594 }
Gu Zheng3a41c5d2013-11-05 18:00:57 +0800595 console_unlock();
Laurent Pinchartecd29942011-09-18 14:14:46 +0200596 break;
597
598 case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT:
599 /* HDMI disconnect */
Gu Zheng3a41c5d2013-11-05 18:00:57 +0800600 console_lock();
Laurent Pinchartecd29942011-09-18 14:14:46 +0200601 if (lock_fb_info(info)) {
Laurent Pinchartecd29942011-09-18 14:14:46 +0200602 fb_set_suspend(info, 1);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200603 unlock_fb_info(info);
604 }
Gu Zheng3a41c5d2013-11-05 18:00:57 +0800605 console_unlock();
Laurent Pinchartecd29942011-09-18 14:14:46 +0200606 break;
607
608 case SH_MOBILE_LCDC_EVENT_DISPLAY_MODE:
609 /* Validate a proposed new mode */
Laurent Pincharte0c86012011-11-29 01:05:47 +0100610 fb_videomode_to_var(&var, mode);
611 var.bits_per_pixel = info->var.bits_per_pixel;
612 var.grayscale = info->var.grayscale;
Laurent Pinchartd7ad3342011-11-22 00:56:58 +0100613 ret = sh_mobile_lcdc_check_var(&var, info);
Laurent Pinchartecd29942011-09-18 14:14:46 +0200614 break;
615 }
616
617 return ret;
618}
619
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200620/* -----------------------------------------------------------------------------
621 * Format helpers
622 */
623
Laurent Pinchart105784b2011-11-29 15:58:10 +0100624struct sh_mobile_lcdc_format_info {
625 u32 fourcc;
626 unsigned int bpp;
627 bool yuv;
628 u32 lddfr;
629};
630
631static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos[] = {
632 {
633 .fourcc = V4L2_PIX_FMT_RGB565,
634 .bpp = 16,
635 .yuv = false,
636 .lddfr = LDDFR_PKF_RGB16,
637 }, {
638 .fourcc = V4L2_PIX_FMT_BGR24,
639 .bpp = 24,
640 .yuv = false,
641 .lddfr = LDDFR_PKF_RGB24,
642 }, {
643 .fourcc = V4L2_PIX_FMT_BGR32,
644 .bpp = 32,
645 .yuv = false,
646 .lddfr = LDDFR_PKF_ARGB32,
647 }, {
648 .fourcc = V4L2_PIX_FMT_NV12,
649 .bpp = 12,
650 .yuv = true,
651 .lddfr = LDDFR_CC | LDDFR_YF_420,
652 }, {
653 .fourcc = V4L2_PIX_FMT_NV21,
654 .bpp = 12,
655 .yuv = true,
656 .lddfr = LDDFR_CC | LDDFR_YF_420,
657 }, {
658 .fourcc = V4L2_PIX_FMT_NV16,
659 .bpp = 16,
660 .yuv = true,
661 .lddfr = LDDFR_CC | LDDFR_YF_422,
662 }, {
663 .fourcc = V4L2_PIX_FMT_NV61,
664 .bpp = 16,
665 .yuv = true,
666 .lddfr = LDDFR_CC | LDDFR_YF_422,
667 }, {
668 .fourcc = V4L2_PIX_FMT_NV24,
669 .bpp = 24,
670 .yuv = true,
671 .lddfr = LDDFR_CC | LDDFR_YF_444,
672 }, {
673 .fourcc = V4L2_PIX_FMT_NV42,
674 .bpp = 24,
675 .yuv = true,
676 .lddfr = LDDFR_CC | LDDFR_YF_444,
677 },
678};
679
680static const struct sh_mobile_lcdc_format_info *
681sh_mobile_format_info(u32 fourcc)
682{
683 unsigned int i;
684
685 for (i = 0; i < ARRAY_SIZE(sh_mobile_format_infos); ++i) {
686 if (sh_mobile_format_infos[i].fourcc == fourcc)
687 return &sh_mobile_format_infos[i];
688 }
689
690 return NULL;
691}
692
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200693static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
694{
695 if (var->grayscale > 1)
696 return var->grayscale;
697
698 switch (var->bits_per_pixel) {
699 case 16:
700 return V4L2_PIX_FMT_RGB565;
701 case 24:
702 return V4L2_PIX_FMT_BGR24;
703 case 32:
704 return V4L2_PIX_FMT_BGR32;
705 default:
706 return 0;
707 }
708}
709
710static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
711{
712 return var->grayscale > 1;
713}
714
Laurent Pinchartf1f60b52011-09-07 11:09:26 +0200715/* -----------------------------------------------------------------------------
716 * Start, stop and IRQ
717 */
718
Magnus Damm85645572008-12-19 15:34:41 +0900719static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
720{
721 struct sh_mobile_lcdc_priv *priv = data;
Magnus Damm2feb0752009-03-13 15:36:55 +0000722 struct sh_mobile_lcdc_chan *ch;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000723 unsigned long ldintr;
Magnus Damm2feb0752009-03-13 15:36:55 +0000724 int is_sub;
725 int k;
Magnus Damm85645572008-12-19 15:34:41 +0900726
Laurent Pinchartdc486652011-07-13 12:13:47 +0200727 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
728 ldintr = lcdc_read(priv, _LDINTR);
729 lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
Magnus Damm85645572008-12-19 15:34:41 +0900730
Magnus Damm2feb0752009-03-13 15:36:55 +0000731 /* figure out if this interrupt is for main or sub lcd */
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200732 is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
Magnus Damm2feb0752009-03-13 15:36:55 +0000733
Phil Edworthy9dd38812009-09-15 12:00:18 +0000734 /* wake up channel and disable clocks */
Magnus Damm2feb0752009-03-13 15:36:55 +0000735 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
736 ch = &priv->ch[k];
737
738 if (!ch->enabled)
739 continue;
740
Laurent Pinchartdc486652011-07-13 12:13:47 +0200741 /* Frame End */
Phil Edworthy9dd38812009-09-15 12:00:18 +0000742 if (ldintr & LDINTR_FS) {
743 if (is_sub == lcdc_chan_is_sublcd(ch)) {
744 ch->frame_end = 1;
745 wake_up(&ch->frame_end_wait);
Magnus Damm2feb0752009-03-13 15:36:55 +0000746
Phil Edworthy9dd38812009-09-15 12:00:18 +0000747 sh_mobile_lcdc_clk_off(priv);
748 }
749 }
750
751 /* VSYNC End */
Phil Edworthy40331b22010-02-15 13:57:49 +0000752 if (ldintr & LDINTR_VES)
753 complete(&ch->vsync_completion);
Magnus Damm2feb0752009-03-13 15:36:55 +0000754 }
755
Magnus Damm85645572008-12-19 15:34:41 +0900756 return IRQ_HANDLED;
757}
758
Laurent Pinchartd7ad3342011-11-22 00:56:58 +0100759static int sh_mobile_lcdc_wait_for_vsync(struct sh_mobile_lcdc_chan *ch)
Laurent Pinchart49766772011-11-30 23:07:30 +0100760{
761 unsigned long ldintr;
762 int ret;
763
764 /* Enable VSync End interrupt and be careful not to acknowledge any
765 * pending interrupt.
766 */
767 ldintr = lcdc_read(ch->lcdc, _LDINTR);
768 ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
769 lcdc_write(ch->lcdc, _LDINTR, ldintr);
770
771 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
772 msecs_to_jiffies(100));
773 if (!ret)
774 return -ETIMEDOUT;
775
776 return 0;
777}
778
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700779static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
780 int start)
781{
782 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
783 int k;
784
785 /* start or stop the lcdc */
786 if (start)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200787 lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700788 else
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200789 lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700790
791 /* wait until power is applied/stopped on all channels */
792 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
793 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
794 while (1) {
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200795 tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
796 & LDPMR_LPS;
797 if (start && tmp == LDPMR_LPS)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700798 break;
799 if (!start && tmp == 0)
800 break;
801 cpu_relax();
802 }
803
804 if (!start)
805 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
806}
807
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000808static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
809{
Laurent Pinchart2d045592011-11-29 13:42:48 +0100810 const struct fb_var_screeninfo *var = &ch->info->var;
811 const struct fb_videomode *mode = &ch->display.mode;
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000812 unsigned long h_total, hsync_pos, display_h_total;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000813 u32 tmp;
814
815 tmp = ch->ldmt1r_value;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200816 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
817 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100818 tmp |= (ch->cfg->flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
819 tmp |= (ch->cfg->flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
820 tmp |= (ch->cfg->flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
821 tmp |= (ch->cfg->flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
822 tmp |= (ch->cfg->flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000823 lcdc_write_chan(ch, LDMT1R, tmp);
824
825 /* setup SYS bus */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100826 lcdc_write_chan(ch, LDMT2R, ch->cfg->sys_bus_cfg.ldmt2r);
827 lcdc_write_chan(ch, LDMT3R, ch->cfg->sys_bus_cfg.ldmt3r);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000828
829 /* horizontal configuration */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100830 h_total = mode->xres + mode->hsync_len + mode->left_margin
831 + mode->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000832 tmp = h_total / 8; /* HTCN */
Laurent Pinchart58f03d92011-11-30 23:07:30 +0100833 tmp |= (min(mode->xres, ch->xres) / 8) << 16; /* HDCN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000834 lcdc_write_chan(ch, LDHCNR, tmp);
835
Laurent Pinchart2d045592011-11-29 13:42:48 +0100836 hsync_pos = mode->xres + mode->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000837 tmp = hsync_pos / 8; /* HSYNP */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100838 tmp |= (mode->hsync_len / 8) << 16; /* HSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000839 lcdc_write_chan(ch, LDHSYNR, tmp);
840
841 /* vertical configuration */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100842 tmp = mode->yres + mode->vsync_len + mode->upper_margin
843 + mode->lower_margin; /* VTLN */
Laurent Pinchart58f03d92011-11-30 23:07:30 +0100844 tmp |= min(mode->yres, ch->yres) << 16; /* VDLN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000845 lcdc_write_chan(ch, LDVLNR, tmp);
846
Laurent Pinchart2d045592011-11-29 13:42:48 +0100847 tmp = mode->yres + mode->lower_margin; /* VSYNP */
848 tmp |= mode->vsync_len << 16; /* VSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000849 lcdc_write_chan(ch, LDVSYNR, tmp);
850
851 /* Adjust horizontal synchronisation for HDMI */
Laurent Pinchart2d045592011-11-29 13:42:48 +0100852 display_h_total = mode->xres + mode->hsync_len + mode->left_margin
853 + mode->right_margin;
854 tmp = ((mode->xres & 7) << 24) | ((display_h_total & 7) << 16)
855 | ((mode->hsync_len & 7) << 8) | (hsync_pos & 7);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000856 lcdc_write_chan(ch, LDHAJR, tmp);
Kuninori Morimoto9beb09f2013-03-04 20:44:39 -0800857 lcdc_write_chan_mirror(ch, LDHAJR, tmp);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000858}
859
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100860static void sh_mobile_lcdc_overlay_setup(struct sh_mobile_lcdc_overlay *ovl)
861{
862 u32 format = 0;
863
864 if (!ovl->enabled) {
865 lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
866 lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), 0);
867 lcdc_write(ovl->channel->lcdc, LDBCR,
868 LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
869 return;
870 }
871
872 ovl->base_addr_y = ovl->dma_handle;
Laurent Pincharta4aa25f2012-03-15 18:15:37 +0100873 ovl->base_addr_c = ovl->dma_handle
874 + ovl->xres_virtual * ovl->yres_virtual;
Laurent Pinchartc5deac32011-12-12 18:43:16 +0100875
876 switch (ovl->mode) {
877 case LCDC_OVERLAY_BLEND:
878 format = LDBBSIFR_EN | (ovl->alpha << LDBBSIFR_LAY_SHIFT);
879 break;
880
881 case LCDC_OVERLAY_ROP3:
882 format = LDBBSIFR_EN | LDBBSIFR_BRSEL
883 | (ovl->rop3 << LDBBSIFR_ROP3_SHIFT);
884 break;
885 }
886
887 switch (ovl->format->fourcc) {
888 case V4L2_PIX_FMT_RGB565:
889 case V4L2_PIX_FMT_NV21:
890 case V4L2_PIX_FMT_NV61:
891 case V4L2_PIX_FMT_NV42:
892 format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW;
893 break;
894 case V4L2_PIX_FMT_BGR24:
895 case V4L2_PIX_FMT_NV12:
896 case V4L2_PIX_FMT_NV16:
897 case V4L2_PIX_FMT_NV24:
898 format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW | LDBBSIFR_SWPB;
899 break;
900 case V4L2_PIX_FMT_BGR32:
901 default:
902 format |= LDBBSIFR_SWPL;
903 break;
904 }
905
906 switch (ovl->format->fourcc) {
907 case V4L2_PIX_FMT_RGB565:
908 format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB16;
909 break;
910 case V4L2_PIX_FMT_BGR24:
911 format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB24;
912 break;
913 case V4L2_PIX_FMT_BGR32:
914 format |= LDBBSIFR_AL_PK | LDBBSIFR_RY | LDDFR_PKF_ARGB32;
915 break;
916 case V4L2_PIX_FMT_NV12:
917 case V4L2_PIX_FMT_NV21:
918 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_420;
919 break;
920 case V4L2_PIX_FMT_NV16:
921 case V4L2_PIX_FMT_NV61:
922 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_422;
923 break;
924 case V4L2_PIX_FMT_NV24:
925 case V4L2_PIX_FMT_NV42:
926 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_444;
927 break;
928 }
929
930 lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
931
932 lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), format);
933
934 lcdc_write_overlay(ovl, LDBnBSSZR(ovl->index),
935 (ovl->yres << LDBBSSZR_BVSS_SHIFT) |
936 (ovl->xres << LDBBSSZR_BHSS_SHIFT));
937 lcdc_write_overlay(ovl, LDBnBLOCR(ovl->index),
938 (ovl->pos_y << LDBBLOCR_CVLC_SHIFT) |
939 (ovl->pos_x << LDBBLOCR_CHLC_SHIFT));
940 lcdc_write_overlay(ovl, LDBnBSMWR(ovl->index),
941 ovl->pitch << LDBBSMWR_BSMW_SHIFT);
942
943 lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
944 lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
945
946 lcdc_write(ovl->channel->lcdc, LDBCR,
947 LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
948}
949
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200950/*
Laurent Pinchartd7ad3342011-11-22 00:56:58 +0100951 * __sh_mobile_lcdc_start - Configure and start the LCDC
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200952 * @priv: LCDC device
953 *
954 * Configure all enabled channels and start the LCDC device. All external
955 * devices (clocks, MERAM, panels, ...) are not touched by this function.
956 */
957static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700958{
959 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700960 unsigned long tmp;
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200961 int k, m;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700962
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200963 /* Enable LCDC channels. Read data from external memory, avoid using the
964 * BEU for now.
965 */
966 lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
Magnus Damm85645572008-12-19 15:34:41 +0900967
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200968 /* Stop the LCDC first and disable all interrupts. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700969 sh_mobile_lcdc_start_stop(priv, 0);
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200970 lcdc_write(priv, _LDINTR, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700971
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200972 /* Configure power supply, dot clocks and start them. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700973 tmp = priv->lddckr;
974 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
975 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200976 if (!ch->enabled)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700977 continue;
978
Laurent Pinchart9a217e32011-07-13 12:13:47 +0200979 /* Power supply */
980 lcdc_write_chan(ch, LDPMR, 0);
981
Laurent Pinchartb5ef9672011-11-22 00:56:58 +0100982 m = ch->cfg->clock_divider;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700983 if (!m)
984 continue;
985
Laurent Pinchart505c7de52011-07-13 12:13:47 +0200986 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
987 * denominator.
988 */
989 lcdc_write_chan(ch, LDDCKPAT1R, 0);
990 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
991
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700992 if (m == 1)
Laurent Pinchartce1c0b02011-07-13 12:13:47 +0200993 m = LDDCKR_MOSEL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700994 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700995 }
996
997 lcdc_write(priv, _LDDCKR, tmp);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700998 lcdc_write(priv, _LDDCKSTPR, 0);
999 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
1000
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001001 /* Setup geometry, format, frame buffer memory and operation mode. */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001002 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1003 ch = &priv->ch[k];
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001004 if (!ch->enabled)
1005 continue;
1006
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001007 sh_mobile_lcdc_geometry(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001008
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001009 tmp = ch->format->lddfr;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001010
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001011 if (ch->format->yuv) {
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001012 switch (ch->colorspace) {
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001013 case V4L2_COLORSPACE_REC709:
1014 tmp |= LDDFR_CF1;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001015 break;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001016 case V4L2_COLORSPACE_JPEG:
1017 tmp |= LDDFR_CF0;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001018 break;
1019 }
Magnus Damm417d4822011-01-05 10:21:00 +00001020 }
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001021
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001022 lcdc_write_chan(ch, LDDFR, tmp);
Laurent Pinchart72c04af2012-05-18 10:58:26 +02001023 lcdc_write_chan(ch, LDMLSR, ch->line_size);
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001024 lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001025 if (ch->format->yuv)
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001026 lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001027
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001028 /* When using deferred I/O mode, configure the LCDC for one-shot
1029 * operation and enable the frame end interrupt. Otherwise use
1030 * continuous read mode.
1031 */
1032 if (ch->ldmt1r_value & LDMT1R_IFM &&
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001033 ch->cfg->sys_bus_cfg.deferred_io_msec) {
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001034 lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
1035 lcdc_write(priv, _LDINTR, LDINTR_FE);
1036 } else {
1037 lcdc_write_chan(ch, LDSM1R, 0);
1038 }
1039 }
Damian7caa4342011-05-18 11:10:07 +00001040
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001041 /* Word and long word swap. */
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001042 switch (priv->ch[0].format->fourcc) {
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001043 case V4L2_PIX_FMT_RGB565:
1044 case V4L2_PIX_FMT_NV21:
1045 case V4L2_PIX_FMT_NV61:
1046 case V4L2_PIX_FMT_NV42:
1047 tmp = LDDDSR_LS | LDDDSR_WS;
1048 break;
1049 case V4L2_PIX_FMT_BGR24:
1050 case V4L2_PIX_FMT_NV12:
1051 case V4L2_PIX_FMT_NV16:
1052 case V4L2_PIX_FMT_NV24:
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001053 tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001054 break;
1055 case V4L2_PIX_FMT_BGR32:
1056 default:
1057 tmp = LDDDSR_LS;
1058 break;
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001059 }
1060 lcdc_write(priv, _LDDDSR, tmp);
Damian7caa4342011-05-18 11:10:07 +00001061
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001062 /* Enable the display output. */
1063 lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
1064 sh_mobile_lcdc_start_stop(priv, 1);
1065 priv->started = 1;
1066}
Damian7caa4342011-05-18 11:10:07 +00001067
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001068static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
1069{
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001070 struct sh_mobile_lcdc_chan *ch;
1071 unsigned long tmp;
1072 int ret;
1073 int k;
1074
1075 /* enable clocks before accessing the hardware */
1076 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1077 if (priv->ch[k].enabled)
1078 sh_mobile_lcdc_clk_on(priv);
1079 }
1080
1081 /* reset */
1082 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
1083 lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
1084
1085 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001086 const struct sh_mobile_lcdc_panel_cfg *panel;
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001087
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001088 ch = &priv->ch[k];
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001089 if (!ch->enabled)
1090 continue;
1091
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001092 panel = &ch->cfg->panel_cfg;
Laurent Pinchartafaad832011-09-11 22:59:04 +02001093 if (panel->setup_sys) {
1094 ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001095 if (ret)
1096 return ret;
1097 }
1098 }
1099
1100 /* Compute frame buffer base address and pitch for each channel. */
1101 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001102 ch = &priv->ch[k];
1103 if (!ch->enabled)
1104 continue;
1105
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001106 ch->base_addr_y = ch->dma_handle;
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001107 ch->base_addr_c = ch->dma_handle
1108 + ch->xres_virtual * ch->yres_virtual;
Laurent Pinchart72c04af2012-05-18 10:58:26 +02001109 ch->line_size = ch->pitch;
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001110 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001111
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001112 for (k = 0; k < ARRAY_SIZE(priv->overlays); ++k) {
1113 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[k];
1114 sh_mobile_lcdc_overlay_setup(ovl);
1115 }
1116
Laurent Pinchart9a217e32011-07-13 12:13:47 +02001117 /* Start the LCDC. */
1118 __sh_mobile_lcdc_start(priv);
1119
1120 /* Setup deferred I/O, tell the board code to enable the panels, and
1121 * turn backlight on.
1122 */
1123 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1124 ch = &priv->ch[k];
1125 if (!ch->enabled)
1126 continue;
1127
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001128 tmp = ch->cfg->sys_bus_cfg.deferred_io_msec;
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02001129 if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
Magnus Damm85645572008-12-19 15:34:41 +09001130 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
1131 ch->defio.delay = msecs_to_jiffies(tmp);
Paul Mundte33afdd2009-07-07 11:24:32 +09001132 ch->info->fbdefio = &ch->defio;
1133 fb_deferred_io_init(ch->info);
Magnus Damm85645572008-12-19 15:34:41 +09001134 }
Magnus Damm21bc1f02009-08-15 02:53:16 +00001135
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001136 sh_mobile_lcdc_display_on(ch);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001137
1138 if (ch->bl) {
1139 ch->bl->props.power = FB_BLANK_UNBLANK;
1140 backlight_update_status(ch->bl);
1141 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001142 }
1143
1144 return 0;
1145}
1146
1147static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
1148{
1149 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001150 int k;
1151
Magnus Damm2feb0752009-03-13 15:36:55 +00001152 /* clean up deferred io and ask board code to disable panel */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001153 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1154 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +00001155 if (!ch->enabled)
1156 continue;
Magnus Damm2feb0752009-03-13 15:36:55 +00001157
1158 /* deferred io mode:
1159 * flush frame, and wait for frame end interrupt
1160 * clean up deferred io and enable clock
1161 */
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +00001162 if (ch->info && ch->info->fbdefio) {
Magnus Damm2feb0752009-03-13 15:36:55 +00001163 ch->frame_end = 0;
Paul Mundte33afdd2009-07-07 11:24:32 +09001164 schedule_delayed_work(&ch->info->deferred_work, 0);
Magnus Damm2feb0752009-03-13 15:36:55 +00001165 wait_event(ch->frame_end_wait, ch->frame_end);
Paul Mundte33afdd2009-07-07 11:24:32 +09001166 fb_deferred_io_cleanup(ch->info);
1167 ch->info->fbdefio = NULL;
Magnus Damm2feb0752009-03-13 15:36:55 +00001168 sh_mobile_lcdc_clk_on(priv);
1169 }
1170
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001171 if (ch->bl) {
1172 ch->bl->props.power = FB_BLANK_POWERDOWN;
1173 backlight_update_status(ch->bl);
1174 }
1175
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02001176 sh_mobile_lcdc_display_off(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001177 }
1178
1179 /* stop the lcdc */
Magnus Damm8e9bb192009-05-20 14:34:43 +00001180 if (priv->started) {
1181 sh_mobile_lcdc_start_stop(priv, 0);
1182 priv->started = 0;
1183 }
Magnus Dammb51339f2008-10-31 20:23:26 +09001184
Magnus Damm85645572008-12-19 15:34:41 +09001185 /* stop clocks */
1186 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
1187 if (priv->ch[k].enabled)
1188 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001189}
1190
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001191static int __sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
1192 struct fb_info *info)
1193{
1194 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
1195 return -EINVAL;
1196
1197 /* Make sure the virtual resolution is at least as big as the visible
1198 * resolution.
1199 */
1200 if (var->xres_virtual < var->xres)
1201 var->xres_virtual = var->xres;
1202 if (var->yres_virtual < var->yres)
1203 var->yres_virtual = var->yres;
1204
1205 if (sh_mobile_format_is_fourcc(var)) {
1206 const struct sh_mobile_lcdc_format_info *format;
1207
1208 format = sh_mobile_format_info(var->grayscale);
1209 if (format == NULL)
1210 return -EINVAL;
1211 var->bits_per_pixel = format->bpp;
1212
1213 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1214 * respectively.
1215 */
1216 if (!format->yuv)
1217 var->colorspace = V4L2_COLORSPACE_SRGB;
1218 else if (var->colorspace != V4L2_COLORSPACE_REC709)
1219 var->colorspace = V4L2_COLORSPACE_JPEG;
1220 } else {
1221 if (var->bits_per_pixel <= 16) { /* RGB 565 */
1222 var->bits_per_pixel = 16;
1223 var->red.offset = 11;
1224 var->red.length = 5;
1225 var->green.offset = 5;
1226 var->green.length = 6;
1227 var->blue.offset = 0;
1228 var->blue.length = 5;
1229 var->transp.offset = 0;
1230 var->transp.length = 0;
1231 } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
1232 var->bits_per_pixel = 24;
1233 var->red.offset = 16;
1234 var->red.length = 8;
1235 var->green.offset = 8;
1236 var->green.length = 8;
1237 var->blue.offset = 0;
1238 var->blue.length = 8;
1239 var->transp.offset = 0;
1240 var->transp.length = 0;
1241 } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
1242 var->bits_per_pixel = 32;
1243 var->red.offset = 16;
1244 var->red.length = 8;
1245 var->green.offset = 8;
1246 var->green.length = 8;
1247 var->blue.offset = 0;
1248 var->blue.length = 8;
1249 var->transp.offset = 24;
1250 var->transp.length = 8;
1251 } else
1252 return -EINVAL;
1253
1254 var->red.msb_right = 0;
1255 var->green.msb_right = 0;
1256 var->blue.msb_right = 0;
1257 var->transp.msb_right = 0;
1258 }
1259
1260 /* Make sure we don't exceed our allocated memory. */
1261 if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
1262 info->fix.smem_len)
1263 return -EINVAL;
1264
1265 return 0;
1266}
1267
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001268/* -----------------------------------------------------------------------------
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001269 * Frame buffer operations - Overlays
1270 */
1271
1272static ssize_t
1273overlay_alpha_show(struct device *dev, struct device_attribute *attr, char *buf)
1274{
1275 struct fb_info *info = dev_get_drvdata(dev);
1276 struct sh_mobile_lcdc_overlay *ovl = info->par;
1277
1278 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->alpha);
1279}
1280
1281static ssize_t
1282overlay_alpha_store(struct device *dev, struct device_attribute *attr,
1283 const char *buf, size_t count)
1284{
1285 struct fb_info *info = dev_get_drvdata(dev);
1286 struct sh_mobile_lcdc_overlay *ovl = info->par;
1287 unsigned int alpha;
1288 char *endp;
1289
1290 alpha = simple_strtoul(buf, &endp, 10);
1291 if (isspace(*endp))
1292 endp++;
1293
1294 if (endp - buf != count)
1295 return -EINVAL;
1296
1297 if (alpha > 255)
1298 return -EINVAL;
1299
1300 if (ovl->alpha != alpha) {
1301 ovl->alpha = alpha;
1302
1303 if (ovl->mode == LCDC_OVERLAY_BLEND && ovl->enabled)
1304 sh_mobile_lcdc_overlay_setup(ovl);
1305 }
1306
1307 return count;
1308}
1309
1310static ssize_t
1311overlay_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1312{
1313 struct fb_info *info = dev_get_drvdata(dev);
1314 struct sh_mobile_lcdc_overlay *ovl = info->par;
1315
1316 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->mode);
1317}
1318
1319static ssize_t
1320overlay_mode_store(struct device *dev, struct device_attribute *attr,
1321 const char *buf, size_t count)
1322{
1323 struct fb_info *info = dev_get_drvdata(dev);
1324 struct sh_mobile_lcdc_overlay *ovl = info->par;
1325 unsigned int mode;
1326 char *endp;
1327
1328 mode = simple_strtoul(buf, &endp, 10);
1329 if (isspace(*endp))
1330 endp++;
1331
1332 if (endp - buf != count)
1333 return -EINVAL;
1334
1335 if (mode != LCDC_OVERLAY_BLEND && mode != LCDC_OVERLAY_ROP3)
1336 return -EINVAL;
1337
1338 if (ovl->mode != mode) {
1339 ovl->mode = mode;
1340
1341 if (ovl->enabled)
1342 sh_mobile_lcdc_overlay_setup(ovl);
1343 }
1344
1345 return count;
1346}
1347
1348static ssize_t
1349overlay_position_show(struct device *dev, struct device_attribute *attr,
1350 char *buf)
1351{
1352 struct fb_info *info = dev_get_drvdata(dev);
1353 struct sh_mobile_lcdc_overlay *ovl = info->par;
1354
1355 return scnprintf(buf, PAGE_SIZE, "%d,%d\n", ovl->pos_x, ovl->pos_y);
1356}
1357
1358static ssize_t
1359overlay_position_store(struct device *dev, struct device_attribute *attr,
1360 const char *buf, size_t count)
1361{
1362 struct fb_info *info = dev_get_drvdata(dev);
1363 struct sh_mobile_lcdc_overlay *ovl = info->par;
1364 char *endp;
1365 int pos_x;
1366 int pos_y;
1367
1368 pos_x = simple_strtol(buf, &endp, 10);
1369 if (*endp != ',')
1370 return -EINVAL;
1371
1372 pos_y = simple_strtol(endp + 1, &endp, 10);
1373 if (isspace(*endp))
1374 endp++;
1375
1376 if (endp - buf != count)
1377 return -EINVAL;
1378
1379 if (ovl->pos_x != pos_x || ovl->pos_y != pos_y) {
1380 ovl->pos_x = pos_x;
1381 ovl->pos_y = pos_y;
1382
1383 if (ovl->enabled)
1384 sh_mobile_lcdc_overlay_setup(ovl);
1385 }
1386
1387 return count;
1388}
1389
1390static ssize_t
1391overlay_rop3_show(struct device *dev, struct device_attribute *attr, char *buf)
1392{
1393 struct fb_info *info = dev_get_drvdata(dev);
1394 struct sh_mobile_lcdc_overlay *ovl = info->par;
1395
1396 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->rop3);
1397}
1398
1399static ssize_t
1400overlay_rop3_store(struct device *dev, struct device_attribute *attr,
1401 const char *buf, size_t count)
1402{
1403 struct fb_info *info = dev_get_drvdata(dev);
1404 struct sh_mobile_lcdc_overlay *ovl = info->par;
1405 unsigned int rop3;
1406 char *endp;
1407
Laurent Pinchart14048ff2015-03-14 17:38:39 +02001408 rop3 = simple_strtoul(buf, &endp, 10);
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001409 if (isspace(*endp))
1410 endp++;
1411
1412 if (endp - buf != count)
1413 return -EINVAL;
1414
1415 if (rop3 > 255)
1416 return -EINVAL;
1417
1418 if (ovl->rop3 != rop3) {
1419 ovl->rop3 = rop3;
1420
1421 if (ovl->mode == LCDC_OVERLAY_ROP3 && ovl->enabled)
1422 sh_mobile_lcdc_overlay_setup(ovl);
1423 }
1424
1425 return count;
1426}
1427
1428static const struct device_attribute overlay_sysfs_attrs[] = {
1429 __ATTR(ovl_alpha, S_IRUGO|S_IWUSR,
1430 overlay_alpha_show, overlay_alpha_store),
1431 __ATTR(ovl_mode, S_IRUGO|S_IWUSR,
1432 overlay_mode_show, overlay_mode_store),
1433 __ATTR(ovl_position, S_IRUGO|S_IWUSR,
1434 overlay_position_show, overlay_position_store),
1435 __ATTR(ovl_rop3, S_IRUGO|S_IWUSR,
1436 overlay_rop3_show, overlay_rop3_store),
1437};
1438
1439static const struct fb_fix_screeninfo sh_mobile_lcdc_overlay_fix = {
1440 .id = "SH Mobile LCDC",
1441 .type = FB_TYPE_PACKED_PIXELS,
1442 .visual = FB_VISUAL_TRUECOLOR,
1443 .accel = FB_ACCEL_NONE,
Laurent Pinchart15dede82012-07-18 17:09:04 +02001444 .xpanstep = 1,
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001445 .ypanstep = 1,
1446 .ywrapstep = 0,
1447 .capabilities = FB_CAP_FOURCC,
1448};
1449
1450static int sh_mobile_lcdc_overlay_pan(struct fb_var_screeninfo *var,
1451 struct fb_info *info)
1452{
1453 struct sh_mobile_lcdc_overlay *ovl = info->par;
1454 unsigned long base_addr_y;
1455 unsigned long base_addr_c;
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001456 unsigned long y_offset;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001457 unsigned long c_offset;
1458
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001459 if (!ovl->format->yuv) {
1460 y_offset = (var->yoffset * ovl->xres_virtual + var->xoffset)
1461 * ovl->format->bpp / 8;
1462 c_offset = 0;
1463 } else {
1464 unsigned int xsub = ovl->format->bpp < 24 ? 2 : 1;
1465 unsigned int ysub = ovl->format->bpp < 16 ? 2 : 1;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001466
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001467 y_offset = var->yoffset * ovl->xres_virtual + var->xoffset;
1468 c_offset = var->yoffset / ysub * ovl->xres_virtual * 2 / xsub
1469 + var->xoffset * 2 / xsub;
1470 }
1471
1472 /* If the Y offset hasn't changed, the C offset hasn't either. There's
1473 * nothing to do in that case.
1474 */
1475 if (y_offset == ovl->pan_y_offset)
1476 return 0;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001477
1478 /* Set the source address for the next refresh */
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001479 base_addr_y = ovl->dma_handle + y_offset;
1480 base_addr_c = ovl->dma_handle + ovl->xres_virtual * ovl->yres_virtual
1481 + c_offset;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001482
1483 ovl->base_addr_y = base_addr_y;
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001484 ovl->base_addr_c = base_addr_c;
1485 ovl->pan_y_offset = y_offset;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001486
Laurent Pinchart8be7c662012-07-19 02:29:52 +02001487 lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
1488
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001489 lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
1490 lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
1491
Laurent Pinchart8be7c662012-07-19 02:29:52 +02001492 lcdc_write(ovl->channel->lcdc, LDBCR,
1493 LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
1494
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001495 return 0;
1496}
1497
1498static int sh_mobile_lcdc_overlay_ioctl(struct fb_info *info, unsigned int cmd,
1499 unsigned long arg)
1500{
1501 struct sh_mobile_lcdc_overlay *ovl = info->par;
1502
1503 switch (cmd) {
1504 case FBIO_WAITFORVSYNC:
1505 return sh_mobile_lcdc_wait_for_vsync(ovl->channel);
1506
1507 default:
1508 return -ENOIOCTLCMD;
1509 }
1510}
1511
1512static int sh_mobile_lcdc_overlay_check_var(struct fb_var_screeninfo *var,
1513 struct fb_info *info)
1514{
1515 return __sh_mobile_lcdc_check_var(var, info);
1516}
1517
1518static int sh_mobile_lcdc_overlay_set_par(struct fb_info *info)
1519{
1520 struct sh_mobile_lcdc_overlay *ovl = info->par;
1521
1522 ovl->format =
1523 sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
1524
1525 ovl->xres = info->var.xres;
1526 ovl->xres_virtual = info->var.xres_virtual;
1527 ovl->yres = info->var.yres;
1528 ovl->yres_virtual = info->var.yres_virtual;
1529
1530 if (ovl->format->yuv)
Laurent Pinchart16ca21c2012-07-18 16:29:20 +02001531 ovl->pitch = info->var.xres_virtual;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001532 else
Laurent Pinchart16ca21c2012-07-18 16:29:20 +02001533 ovl->pitch = info->var.xres_virtual * ovl->format->bpp / 8;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001534
1535 sh_mobile_lcdc_overlay_setup(ovl);
1536
1537 info->fix.line_length = ovl->pitch;
1538
1539 if (sh_mobile_format_is_fourcc(&info->var)) {
1540 info->fix.type = FB_TYPE_FOURCC;
1541 info->fix.visual = FB_VISUAL_FOURCC;
1542 } else {
1543 info->fix.type = FB_TYPE_PACKED_PIXELS;
1544 info->fix.visual = FB_VISUAL_TRUECOLOR;
1545 }
1546
1547 return 0;
1548}
1549
1550/* Overlay blanking. Disable the overlay when blanked. */
1551static int sh_mobile_lcdc_overlay_blank(int blank, struct fb_info *info)
1552{
1553 struct sh_mobile_lcdc_overlay *ovl = info->par;
1554
1555 ovl->enabled = !blank;
1556 sh_mobile_lcdc_overlay_setup(ovl);
1557
1558 /* Prevent the backlight from receiving a blanking event by returning
1559 * a non-zero value.
1560 */
1561 return 1;
1562}
1563
Hideki EIRAKUbf10a532012-08-16 19:13:20 +09001564static int
1565sh_mobile_lcdc_overlay_mmap(struct fb_info *info, struct vm_area_struct *vma)
1566{
1567 struct sh_mobile_lcdc_overlay *ovl = info->par;
1568
1569 return dma_mmap_coherent(ovl->channel->lcdc->dev, vma, ovl->fb_mem,
1570 ovl->dma_handle, ovl->fb_size);
1571}
1572
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001573static struct fb_ops sh_mobile_lcdc_overlay_ops = {
1574 .owner = THIS_MODULE,
1575 .fb_read = fb_sys_read,
1576 .fb_write = fb_sys_write,
1577 .fb_fillrect = sys_fillrect,
1578 .fb_copyarea = sys_copyarea,
1579 .fb_imageblit = sys_imageblit,
1580 .fb_blank = sh_mobile_lcdc_overlay_blank,
1581 .fb_pan_display = sh_mobile_lcdc_overlay_pan,
1582 .fb_ioctl = sh_mobile_lcdc_overlay_ioctl,
1583 .fb_check_var = sh_mobile_lcdc_overlay_check_var,
1584 .fb_set_par = sh_mobile_lcdc_overlay_set_par,
Hideki EIRAKUbf10a532012-08-16 19:13:20 +09001585 .fb_mmap = sh_mobile_lcdc_overlay_mmap,
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001586};
1587
1588static void
1589sh_mobile_lcdc_overlay_fb_unregister(struct sh_mobile_lcdc_overlay *ovl)
1590{
1591 struct fb_info *info = ovl->info;
1592
1593 if (info == NULL || info->dev == NULL)
1594 return;
1595
1596 unregister_framebuffer(ovl->info);
1597}
1598
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001599static int
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001600sh_mobile_lcdc_overlay_fb_register(struct sh_mobile_lcdc_overlay *ovl)
1601{
1602 struct sh_mobile_lcdc_priv *lcdc = ovl->channel->lcdc;
1603 struct fb_info *info = ovl->info;
1604 unsigned int i;
1605 int ret;
1606
1607 if (info == NULL)
1608 return 0;
1609
1610 ret = register_framebuffer(info);
1611 if (ret < 0)
1612 return ret;
1613
1614 dev_info(lcdc->dev, "registered %s/overlay %u as %dx%d %dbpp.\n",
1615 dev_name(lcdc->dev), ovl->index, info->var.xres,
1616 info->var.yres, info->var.bits_per_pixel);
1617
1618 for (i = 0; i < ARRAY_SIZE(overlay_sysfs_attrs); ++i) {
1619 ret = device_create_file(info->dev, &overlay_sysfs_attrs[i]);
1620 if (ret < 0)
1621 return ret;
1622 }
1623
1624 return 0;
1625}
1626
1627static void
1628sh_mobile_lcdc_overlay_fb_cleanup(struct sh_mobile_lcdc_overlay *ovl)
1629{
1630 struct fb_info *info = ovl->info;
1631
1632 if (info == NULL || info->device == NULL)
1633 return;
1634
1635 framebuffer_release(info);
1636}
1637
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001638static int
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001639sh_mobile_lcdc_overlay_fb_init(struct sh_mobile_lcdc_overlay *ovl)
1640{
1641 struct sh_mobile_lcdc_priv *priv = ovl->channel->lcdc;
1642 struct fb_var_screeninfo *var;
1643 struct fb_info *info;
1644
1645 /* Allocate and initialize the frame buffer device. */
1646 info = framebuffer_alloc(0, priv->dev);
1647 if (info == NULL) {
1648 dev_err(priv->dev, "unable to allocate fb_info\n");
1649 return -ENOMEM;
1650 }
1651
1652 ovl->info = info;
1653
1654 info->flags = FBINFO_FLAG_DEFAULT;
1655 info->fbops = &sh_mobile_lcdc_overlay_ops;
1656 info->device = priv->dev;
1657 info->screen_base = ovl->fb_mem;
1658 info->par = ovl;
1659
1660 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1661 * for NV12 and NV21.
1662 */
1663 info->fix = sh_mobile_lcdc_overlay_fix;
1664 snprintf(info->fix.id, sizeof(info->fix.id),
1665 "SH Mobile LCDC Overlay %u", ovl->index);
1666 info->fix.smem_start = ovl->dma_handle;
1667 info->fix.smem_len = ovl->fb_size;
1668 info->fix.line_length = ovl->pitch;
1669
1670 if (ovl->format->yuv)
1671 info->fix.visual = FB_VISUAL_FOURCC;
1672 else
1673 info->fix.visual = FB_VISUAL_TRUECOLOR;
1674
Laurent Pinchart15dede82012-07-18 17:09:04 +02001675 switch (ovl->format->fourcc) {
Laurent Pinchart15dede82012-07-18 17:09:04 +02001676 case V4L2_PIX_FMT_NV12:
1677 case V4L2_PIX_FMT_NV21:
Laurent Pinchartac33a202012-07-26 14:36:55 +02001678 info->fix.ypanstep = 2;
1679 case V4L2_PIX_FMT_NV16:
1680 case V4L2_PIX_FMT_NV61:
Laurent Pinchart15dede82012-07-18 17:09:04 +02001681 info->fix.xpanstep = 2;
1682 }
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001683
1684 /* Initialize variable screen information. */
1685 var = &info->var;
1686 memset(var, 0, sizeof(*var));
1687 var->xres = ovl->xres;
1688 var->yres = ovl->yres;
1689 var->xres_virtual = ovl->xres_virtual;
1690 var->yres_virtual = ovl->yres_virtual;
1691 var->activate = FB_ACTIVATE_NOW;
1692
1693 /* Use the legacy API by default for RGB formats, and the FOURCC API
1694 * for YUV formats.
1695 */
1696 if (!ovl->format->yuv)
1697 var->bits_per_pixel = ovl->format->bpp;
1698 else
1699 var->grayscale = ovl->format->fourcc;
1700
1701 return sh_mobile_lcdc_overlay_check_var(var, info);
1702}
1703
1704/* -----------------------------------------------------------------------------
1705 * Frame buffer operations - main frame buffer
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02001706 */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001707
1708static int sh_mobile_lcdc_setcolreg(u_int regno,
1709 u_int red, u_int green, u_int blue,
1710 u_int transp, struct fb_info *info)
1711{
1712 u32 *palette = info->pseudo_palette;
1713
1714 if (regno >= PALETTE_NR)
1715 return -EINVAL;
1716
1717 /* only FB_VISUAL_TRUECOLOR supported */
1718
1719 red >>= 16 - info->var.red.length;
1720 green >>= 16 - info->var.green.length;
1721 blue >>= 16 - info->var.blue.length;
1722 transp >>= 16 - info->var.transp.length;
1723
1724 palette[regno] = (red << info->var.red.offset) |
1725 (green << info->var.green.offset) |
1726 (blue << info->var.blue.offset) |
1727 (transp << info->var.transp.offset);
1728
1729 return 0;
1730}
1731
Laurent Pinchart3281e542011-11-22 00:56:58 +01001732static const struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001733 .id = "SH Mobile LCDC",
1734 .type = FB_TYPE_PACKED_PIXELS,
1735 .visual = FB_VISUAL_TRUECOLOR,
1736 .accel = FB_ACCEL_NONE,
Laurent Pinchart15dede82012-07-18 17:09:04 +02001737 .xpanstep = 1,
Phil Edworthy9dd38812009-09-15 12:00:18 +00001738 .ypanstep = 1,
1739 .ywrapstep = 0,
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001740 .capabilities = FB_CAP_FOURCC,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001741};
1742
Magnus Damm85645572008-12-19 15:34:41 +09001743static void sh_mobile_lcdc_fillrect(struct fb_info *info,
1744 const struct fb_fillrect *rect)
1745{
1746 sys_fillrect(info, rect);
1747 sh_mobile_lcdc_deferred_io_touch(info);
1748}
1749
1750static void sh_mobile_lcdc_copyarea(struct fb_info *info,
1751 const struct fb_copyarea *area)
1752{
1753 sys_copyarea(info, area);
1754 sh_mobile_lcdc_deferred_io_touch(info);
1755}
1756
1757static void sh_mobile_lcdc_imageblit(struct fb_info *info,
1758 const struct fb_image *image)
1759{
1760 sys_imageblit(info, image);
1761 sh_mobile_lcdc_deferred_io_touch(info);
1762}
1763
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001764static int sh_mobile_lcdc_pan(struct fb_var_screeninfo *var,
1765 struct fb_info *info)
Phil Edworthy9dd38812009-09-15 12:00:18 +00001766{
1767 struct sh_mobile_lcdc_chan *ch = info->par;
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001768 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
1769 unsigned long ldrcntr;
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001770 unsigned long base_addr_y, base_addr_c;
1771 unsigned long y_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001772 unsigned long c_offset;
Phil Edworthy9dd38812009-09-15 12:00:18 +00001773
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001774 if (!ch->format->yuv) {
1775 y_offset = (var->yoffset * ch->xres_virtual + var->xoffset)
1776 * ch->format->bpp / 8;
1777 c_offset = 0;
1778 } else {
1779 unsigned int xsub = ch->format->bpp < 24 ? 2 : 1;
1780 unsigned int ysub = ch->format->bpp < 16 ? 2 : 1;
Phil Edworthy9dd38812009-09-15 12:00:18 +00001781
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001782 y_offset = var->yoffset * ch->xres_virtual + var->xoffset;
1783 c_offset = var->yoffset / ysub * ch->xres_virtual * 2 / xsub
1784 + var->xoffset * 2 / xsub;
1785 }
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001786
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001787 /* If the Y offset hasn't changed, the C offset hasn't either. There's
1788 * nothing to do in that case.
1789 */
1790 if (y_offset == ch->pan_y_offset)
1791 return 0;
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001792
1793 /* Set the source address for the next refresh */
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001794 base_addr_y = ch->dma_handle + y_offset;
1795 base_addr_c = ch->dma_handle + ch->xres_virtual * ch->yres_virtual
1796 + c_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001797
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001798 ch->base_addr_y = base_addr_y;
1799 ch->base_addr_c = base_addr_c;
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001800 ch->pan_y_offset = y_offset;
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001801
1802 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001803 if (ch->format->yuv)
Laurent Pinchart49d79ba2011-07-13 12:13:47 +02001804 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
1805
Laurent Pincharta4aa25f2012-03-15 18:15:37 +01001806 ldrcntr = lcdc_read(priv, _LDRCNTR);
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001807 if (lcdc_chan_is_sublcd(ch))
1808 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
1809 else
1810 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
1811
Phil Edworthy92e1f9a2010-02-11 10:24:25 +00001812
1813 sh_mobile_lcdc_deferred_io_touch(info);
Phil Edworthy9dd38812009-09-15 12:00:18 +00001814
1815 return 0;
1816}
1817
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001818static int sh_mobile_lcdc_ioctl(struct fb_info *info, unsigned int cmd,
1819 unsigned long arg)
Phil Edworthy40331b22010-02-15 13:57:49 +00001820{
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001821 struct sh_mobile_lcdc_chan *ch = info->par;
Phil Edworthy40331b22010-02-15 13:57:49 +00001822 int retval;
1823
1824 switch (cmd) {
1825 case FBIO_WAITFORVSYNC:
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001826 retval = sh_mobile_lcdc_wait_for_vsync(ch);
Phil Edworthy40331b22010-02-15 13:57:49 +00001827 break;
1828
1829 default:
1830 retval = -ENOIOCTLCMD;
1831 break;
1832 }
1833 return retval;
1834}
1835
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001836static void sh_mobile_fb_reconfig(struct fb_info *info)
1837{
1838 struct sh_mobile_lcdc_chan *ch = info->par;
Laurent Pinchart2d045592011-11-29 13:42:48 +01001839 struct fb_var_screeninfo var;
1840 struct fb_videomode mode;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001841 struct fb_event event;
1842 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1843
1844 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1845 /* More framebuffer users are active */
1846 return;
1847
Laurent Pinchart2d045592011-11-29 13:42:48 +01001848 fb_var_to_videomode(&mode, &info->var);
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001849
Laurent Pinchart2d045592011-11-29 13:42:48 +01001850 if (fb_mode_is_equal(&ch->display.mode, &mode))
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001851 return;
1852
1853 /* Display has been re-plugged, framebuffer is free now, reconfigure */
Laurent Pinchart2d045592011-11-29 13:42:48 +01001854 var = info->var;
1855 fb_videomode_to_var(&var, &ch->display.mode);
1856 var.width = ch->display.width;
1857 var.height = ch->display.height;
1858 var.activate = FB_ACTIVATE_NOW;
1859
1860 if (fb_set_var(info, &var) < 0)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001861 /* Couldn't reconfigure, hopefully, can continue as before */
1862 return;
1863
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001864 /*
1865 * fb_set_var() calls the notifier change internally, only if
1866 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1867 * user event, we have to call the chain ourselves.
1868 */
1869 event.info = info;
Laurent Pinchart2d045592011-11-29 13:42:48 +01001870 event.data = &ch->display.mode;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001871 fb_notifier_call_chain(evnt, &event);
1872}
1873
1874/*
1875 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1876 * user == 1, or with console sem held, if user == 0.
1877 */
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001878static int sh_mobile_lcdc_release(struct fb_info *info, int user)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001879{
1880 struct sh_mobile_lcdc_chan *ch = info->par;
1881
1882 mutex_lock(&ch->open_lock);
1883 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1884
1885 ch->use_count--;
1886
1887 /* Nothing to reconfigure, when called from fbcon */
1888 if (user) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001889 console_lock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001890 sh_mobile_fb_reconfig(info);
Torben Hohnac751ef2011-01-25 15:07:35 -08001891 console_unlock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001892 }
1893
1894 mutex_unlock(&ch->open_lock);
1895
1896 return 0;
1897}
1898
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001899static int sh_mobile_lcdc_open(struct fb_info *info, int user)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001900{
1901 struct sh_mobile_lcdc_chan *ch = info->par;
1902
1903 mutex_lock(&ch->open_lock);
1904 ch->use_count++;
1905
1906 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1907 mutex_unlock(&ch->open_lock);
1908
1909 return 0;
1910}
1911
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001912static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
1913 struct fb_info *info)
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001914{
1915 struct sh_mobile_lcdc_chan *ch = info->par;
Magnus Damm417d4822011-01-05 10:21:00 +00001916 struct sh_mobile_lcdc_priv *p = ch->lcdc;
Laurent Pinchart03862192011-08-31 13:00:53 +02001917 unsigned int best_dist = (unsigned int)-1;
1918 unsigned int best_xres = 0;
1919 unsigned int best_yres = 0;
1920 unsigned int i;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001921 int ret;
Laurent Pinchart03862192011-08-31 13:00:53 +02001922
1923 /* If board code provides us with a list of available modes, make sure
1924 * we use one of them. Find the mode closest to the requested one. The
1925 * distance between two modes is defined as the size of the
1926 * non-overlapping parts of the two rectangles.
1927 */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001928 for (i = 0; i < ch->cfg->num_modes; ++i) {
1929 const struct fb_videomode *mode = &ch->cfg->lcd_modes[i];
Laurent Pinchart03862192011-08-31 13:00:53 +02001930 unsigned int dist;
1931
1932 /* We can only round up. */
1933 if (var->xres > mode->xres || var->yres > mode->yres)
1934 continue;
1935
1936 dist = var->xres * var->yres + mode->xres * mode->yres
1937 - 2 * min(var->xres, mode->xres)
1938 * min(var->yres, mode->yres);
1939
1940 if (dist < best_dist) {
1941 best_xres = mode->xres;
1942 best_yres = mode->yres;
1943 best_dist = dist;
1944 }
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001945 }
Magnus Damm417d4822011-01-05 10:21:00 +00001946
Laurent Pinchart03862192011-08-31 13:00:53 +02001947 /* If no available mode can be used, return an error. */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01001948 if (ch->cfg->num_modes != 0) {
Laurent Pinchart03862192011-08-31 13:00:53 +02001949 if (best_dist == (unsigned int)-1)
1950 return -EINVAL;
1951
1952 var->xres = best_xres;
1953 var->yres = best_yres;
1954 }
1955
Laurent Pinchartc5deac32011-12-12 18:43:16 +01001956 ret = __sh_mobile_lcdc_check_var(var, info);
1957 if (ret < 0)
1958 return ret;
Laurent Pinchart03862192011-08-31 13:00:53 +02001959
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001960 /* only accept the forced_fourcc for dual channel configurations */
1961 if (p->forced_fourcc &&
1962 p->forced_fourcc != sh_mobile_format_fourcc(var))
Magnus Damm417d4822011-01-05 10:21:00 +00001963 return -EINVAL;
1964
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001965 return 0;
1966}
Phil Edworthy40331b22010-02-15 13:57:49 +00001967
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01001968static int sh_mobile_lcdc_set_par(struct fb_info *info)
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001969{
1970 struct sh_mobile_lcdc_chan *ch = info->par;
1971 int ret;
1972
1973 sh_mobile_lcdc_stop(ch->lcdc);
Laurent Pinchart91fba482011-08-31 13:00:56 +02001974
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001975 ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001976 ch->colorspace = info->var.colorspace;
1977
1978 ch->xres = info->var.xres;
1979 ch->xres_virtual = info->var.xres_virtual;
1980 ch->yres = info->var.yres;
1981 ch->yres_virtual = info->var.yres_virtual;
1982
1983 if (ch->format->yuv)
Laurent Pinchart16ca21c2012-07-18 16:29:20 +02001984 ch->pitch = info->var.xres_virtual;
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001985 else
Laurent Pinchart16ca21c2012-07-18 16:29:20 +02001986 ch->pitch = info->var.xres_virtual * ch->format->bpp / 8;
Laurent Pinchartfc9e78e2011-11-29 16:05:36 +01001987
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001988 ret = sh_mobile_lcdc_start(ch->lcdc);
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001989 if (ret < 0)
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001990 dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
Laurent Pinchart58f03d92011-11-30 23:07:30 +01001991
1992 info->fix.line_length = ch->pitch;
Laurent Pincharted5bebf2011-08-31 13:00:54 +02001993
Laurent Pinchartedd153a2011-12-13 14:02:28 +01001994 if (sh_mobile_format_is_fourcc(&info->var)) {
1995 info->fix.type = FB_TYPE_FOURCC;
1996 info->fix.visual = FB_VISUAL_FOURCC;
1997 } else {
1998 info->fix.type = FB_TYPE_PACKED_PIXELS;
1999 info->fix.visual = FB_VISUAL_TRUECOLOR;
2000 }
2001
Laurent Pincharted5bebf2011-08-31 13:00:54 +02002002 return ret;
2003}
2004
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00002005/*
2006 * Screen blanking. Behavior is as follows:
2007 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
2008 * FB_BLANK_NORMAL: screen blanked, clocks enabled
2009 * FB_BLANK_VSYNC,
2010 * FB_BLANK_HSYNC,
2011 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
2012 */
2013static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
2014{
2015 struct sh_mobile_lcdc_chan *ch = info->par;
2016 struct sh_mobile_lcdc_priv *p = ch->lcdc;
2017
2018 /* blank the screen? */
2019 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
2020 struct fb_fillrect rect = {
Laurent Pinchart58f03d92011-11-30 23:07:30 +01002021 .width = ch->xres,
2022 .height = ch->yres,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00002023 };
2024 sh_mobile_lcdc_fillrect(info, &rect);
2025 }
2026 /* turn clocks on? */
2027 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
2028 sh_mobile_lcdc_clk_on(p);
2029 }
2030 /* turn clocks off? */
2031 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
2032 /* make sure the screen is updated with the black fill before
2033 * switching the clocks off. one vsync is not enough since
2034 * blanking may occur in the middle of a refresh. deferred io
2035 * mode will reenable the clocks and update the screen in time,
2036 * so it does not need this. */
2037 if (!info->fbdefio) {
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01002038 sh_mobile_lcdc_wait_for_vsync(ch);
2039 sh_mobile_lcdc_wait_for_vsync(ch);
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00002040 }
2041 sh_mobile_lcdc_clk_off(p);
2042 }
2043
2044 ch->blank_status = blank;
2045 return 0;
2046}
2047
Hideki EIRAKUbf10a532012-08-16 19:13:20 +09002048static int
2049sh_mobile_lcdc_mmap(struct fb_info *info, struct vm_area_struct *vma)
2050{
2051 struct sh_mobile_lcdc_chan *ch = info->par;
2052
2053 return dma_mmap_coherent(ch->lcdc->dev, vma, ch->fb_mem,
2054 ch->dma_handle, ch->fb_size);
2055}
2056
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002057static struct fb_ops sh_mobile_lcdc_ops = {
Phil Edworthy9dd38812009-09-15 12:00:18 +00002058 .owner = THIS_MODULE,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002059 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
Magnus Damm2540c112008-12-17 17:29:49 +09002060 .fb_read = fb_sys_read,
2061 .fb_write = fb_sys_write,
Magnus Damm85645572008-12-19 15:34:41 +09002062 .fb_fillrect = sh_mobile_lcdc_fillrect,
2063 .fb_copyarea = sh_mobile_lcdc_copyarea,
2064 .fb_imageblit = sh_mobile_lcdc_imageblit,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00002065 .fb_blank = sh_mobile_lcdc_blank,
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01002066 .fb_pan_display = sh_mobile_lcdc_pan,
2067 .fb_ioctl = sh_mobile_lcdc_ioctl,
2068 .fb_open = sh_mobile_lcdc_open,
2069 .fb_release = sh_mobile_lcdc_release,
2070 .fb_check_var = sh_mobile_lcdc_check_var,
2071 .fb_set_par = sh_mobile_lcdc_set_par,
Hideki EIRAKUbf10a532012-08-16 19:13:20 +09002072 .fb_mmap = sh_mobile_lcdc_mmap,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002073};
2074
Laurent Pincharta67f3792011-11-29 14:37:35 +01002075static void
2076sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
2077{
2078 if (ch->info && ch->info->dev)
2079 unregister_framebuffer(ch->info);
2080}
2081
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002082static int
Laurent Pincharta67f3792011-11-29 14:37:35 +01002083sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
2084{
2085 struct fb_info *info = ch->info;
2086 int ret;
2087
2088 if (info->fbdefio) {
2089 ch->sglist = vmalloc(sizeof(struct scatterlist) *
2090 ch->fb_size >> PAGE_SHIFT);
Markus Elfringe2810182018-04-26 12:24:18 +02002091 if (!ch->sglist)
Laurent Pincharta67f3792011-11-29 14:37:35 +01002092 return -ENOMEM;
Laurent Pincharta67f3792011-11-29 14:37:35 +01002093 }
2094
2095 info->bl_dev = ch->bl;
2096
2097 ret = register_framebuffer(info);
2098 if (ret < 0)
2099 return ret;
2100
2101 dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002102 dev_name(ch->lcdc->dev), (ch->cfg->chan == LCDC_CHAN_MAINLCD) ?
Laurent Pincharta67f3792011-11-29 14:37:35 +01002103 "mainlcd" : "sublcd", info->var.xres, info->var.yres,
2104 info->var.bits_per_pixel);
2105
2106 /* deferred io mode: disable clock to save power */
2107 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
2108 sh_mobile_lcdc_clk_off(ch->lcdc);
2109
2110 return ret;
2111}
2112
2113static void
2114sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
2115{
2116 struct fb_info *info = ch->info;
2117
2118 if (!info || !info->device)
2119 return;
2120
Markus Elfringf8582752014-11-22 16:51:31 +01002121 vfree(ch->sglist);
Laurent Pincharta67f3792011-11-29 14:37:35 +01002122
2123 fb_dealloc_cmap(&info->cmap);
2124 framebuffer_release(info);
2125}
2126
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002127static int
Laurent Pincharta67f3792011-11-29 14:37:35 +01002128sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
Laurent Pinchart352d6132012-08-13 13:56:46 +02002129 const struct fb_videomode *modes,
Laurent Pincharta67f3792011-11-29 14:37:35 +01002130 unsigned int num_modes)
2131{
2132 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
2133 struct fb_var_screeninfo *var;
2134 struct fb_info *info;
2135 int ret;
2136
2137 /* Allocate and initialize the frame buffer device. Create the modes
2138 * list and allocate the color map.
2139 */
2140 info = framebuffer_alloc(0, priv->dev);
2141 if (info == NULL) {
2142 dev_err(priv->dev, "unable to allocate fb_info\n");
2143 return -ENOMEM;
2144 }
2145
2146 ch->info = info;
2147
2148 info->flags = FBINFO_FLAG_DEFAULT;
2149 info->fbops = &sh_mobile_lcdc_ops;
2150 info->device = priv->dev;
2151 info->screen_base = ch->fb_mem;
2152 info->pseudo_palette = &ch->pseudo_palette;
2153 info->par = ch;
2154
Laurent Pinchart352d6132012-08-13 13:56:46 +02002155 fb_videomode_to_modelist(modes, num_modes, &info->modelist);
Laurent Pincharta67f3792011-11-29 14:37:35 +01002156
2157 ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
2158 if (ret < 0) {
2159 dev_err(priv->dev, "unable to allocate cmap\n");
2160 return ret;
2161 }
2162
2163 /* Initialize fixed screen information. Restrict pan to 2 lines steps
2164 * for NV12 and NV21.
2165 */
2166 info->fix = sh_mobile_lcdc_fix;
2167 info->fix.smem_start = ch->dma_handle;
2168 info->fix.smem_len = ch->fb_size;
Laurent Pinchart58f03d92011-11-30 23:07:30 +01002169 info->fix.line_length = ch->pitch;
2170
2171 if (ch->format->yuv)
2172 info->fix.visual = FB_VISUAL_FOURCC;
2173 else
2174 info->fix.visual = FB_VISUAL_TRUECOLOR;
2175
Laurent Pinchart15dede82012-07-18 17:09:04 +02002176 switch (ch->format->fourcc) {
Laurent Pinchart15dede82012-07-18 17:09:04 +02002177 case V4L2_PIX_FMT_NV12:
2178 case V4L2_PIX_FMT_NV21:
Laurent Pinchartac33a202012-07-26 14:36:55 +02002179 info->fix.ypanstep = 2;
2180 case V4L2_PIX_FMT_NV16:
2181 case V4L2_PIX_FMT_NV61:
Laurent Pinchart15dede82012-07-18 17:09:04 +02002182 info->fix.xpanstep = 2;
2183 }
Laurent Pincharta67f3792011-11-29 14:37:35 +01002184
2185 /* Initialize variable screen information using the first mode as
Laurent Pinchartbd5f2c62012-07-18 16:59:16 +02002186 * default.
Laurent Pincharta67f3792011-11-29 14:37:35 +01002187 */
2188 var = &info->var;
Laurent Pinchart352d6132012-08-13 13:56:46 +02002189 fb_videomode_to_var(var, modes);
Laurent Pinchart856e8df2012-08-13 13:56:46 +02002190 var->width = ch->display.width;
2191 var->height = ch->display.height;
Laurent Pinchartbd5f2c62012-07-18 16:59:16 +02002192 var->xres_virtual = ch->xres_virtual;
2193 var->yres_virtual = ch->yres_virtual;
Laurent Pincharta67f3792011-11-29 14:37:35 +01002194 var->activate = FB_ACTIVATE_NOW;
2195
2196 /* Use the legacy API by default for RGB formats, and the FOURCC API
2197 * for YUV formats.
2198 */
2199 if (!ch->format->yuv)
2200 var->bits_per_pixel = ch->format->bpp;
2201 else
2202 var->grayscale = ch->format->fourcc;
2203
Laurent Pinchartd7ad3342011-11-22 00:56:58 +01002204 ret = sh_mobile_lcdc_check_var(var, info);
Laurent Pincharta67f3792011-11-29 14:37:35 +01002205 if (ret)
2206 return ret;
2207
Laurent Pincharta67f3792011-11-29 14:37:35 +01002208 return 0;
2209}
2210
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002211/* -----------------------------------------------------------------------------
2212 * Backlight
2213 */
2214
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002215static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
2216{
2217 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002218 int brightness = bdev->props.brightness;
2219
2220 if (bdev->props.power != FB_BLANK_UNBLANK ||
2221 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
2222 brightness = 0;
2223
Laurent Pinchart656d4f32012-08-15 18:10:03 +02002224 ch->bl_brightness = brightness;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002225 return ch->cfg->bl_info.set_brightness(brightness);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002226}
2227
2228static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
2229{
2230 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002231
Laurent Pinchart656d4f32012-08-15 18:10:03 +02002232 return ch->bl_brightness;
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002233}
2234
2235static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
2236 struct fb_info *info)
2237{
2238 return (info->bl_dev == bdev);
2239}
2240
Arvind Yadav0e258842017-06-14 17:40:57 +02002241static const struct backlight_ops sh_mobile_lcdc_bl_ops = {
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002242 .options = BL_CORE_SUSPENDRESUME,
2243 .update_status = sh_mobile_lcdc_update_bl,
2244 .get_brightness = sh_mobile_lcdc_get_brightness,
2245 .check_fb = sh_mobile_lcdc_check_fb,
2246};
2247
2248static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
2249 struct sh_mobile_lcdc_chan *ch)
2250{
2251 struct backlight_device *bl;
2252
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002253 bl = backlight_device_register(ch->cfg->bl_info.name, parent, ch,
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002254 &sh_mobile_lcdc_bl_ops, NULL);
Dan Carpenterbeee1f22011-03-21 15:03:13 +00002255 if (IS_ERR(bl)) {
2256 dev_err(parent, "unable to register backlight device: %ld\n",
2257 PTR_ERR(bl));
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002258 return NULL;
2259 }
2260
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002261 bl->props.max_brightness = ch->cfg->bl_info.max_brightness;
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002262 bl->props.brightness = bl->props.max_brightness;
2263 backlight_update_status(bl);
2264
2265 return bl;
2266}
2267
2268static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
2269{
2270 backlight_device_unregister(bdev);
2271}
2272
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002273/* -----------------------------------------------------------------------------
2274 * Power management
2275 */
2276
Magnus Damm2feb0752009-03-13 15:36:55 +00002277static int sh_mobile_lcdc_suspend(struct device *dev)
2278{
2279 struct platform_device *pdev = to_platform_device(dev);
2280
2281 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
2282 return 0;
2283}
2284
2285static int sh_mobile_lcdc_resume(struct device *dev)
2286{
2287 struct platform_device *pdev = to_platform_device(dev);
2288
2289 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
2290}
2291
Magnus Damm0246c472009-08-14 10:49:08 +00002292static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
2293{
Wolfram Sangb2faabc2018-04-24 18:11:21 +02002294 struct sh_mobile_lcdc_priv *priv = dev_get_drvdata(dev);
Magnus Damm0246c472009-08-14 10:49:08 +00002295
2296 /* turn off LCDC hardware */
Laurent Pinchart2427bb22011-07-13 12:13:47 +02002297 lcdc_write(priv, _LDCNT1R, 0);
2298
Magnus Damm0246c472009-08-14 10:49:08 +00002299 return 0;
2300}
2301
2302static int sh_mobile_lcdc_runtime_resume(struct device *dev)
2303{
Wolfram Sangb2faabc2018-04-24 18:11:21 +02002304 struct sh_mobile_lcdc_priv *priv = dev_get_drvdata(dev);
Magnus Damm0246c472009-08-14 10:49:08 +00002305
Laurent Pinchart2427bb22011-07-13 12:13:47 +02002306 __sh_mobile_lcdc_start(priv);
Magnus Damm0246c472009-08-14 10:49:08 +00002307
2308 return 0;
2309}
2310
Alexey Dobriyan47145212009-12-14 18:00:08 -08002311static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
Magnus Damm2feb0752009-03-13 15:36:55 +00002312 .suspend = sh_mobile_lcdc_suspend,
2313 .resume = sh_mobile_lcdc_resume,
Magnus Damm0246c472009-08-14 10:49:08 +00002314 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
2315 .runtime_resume = sh_mobile_lcdc_runtime_resume,
Magnus Damm2feb0752009-03-13 15:36:55 +00002316};
2317
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002318/* -----------------------------------------------------------------------------
2319 * Framebuffer notifier
2320 */
2321
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00002322/* locking: called with info->lock held */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002323static int sh_mobile_lcdc_notify(struct notifier_block *nb,
2324 unsigned long action, void *data)
2325{
2326 struct fb_event *event = data;
2327 struct fb_info *info = event->info;
2328 struct sh_mobile_lcdc_chan *ch = info->par;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002329
2330 if (&ch->lcdc->notifier != nb)
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00002331 return NOTIFY_DONE;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002332
2333 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
2334 __func__, action, event->data);
2335
2336 switch(action) {
2337 case FB_EVENT_SUSPEND:
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02002338 sh_mobile_lcdc_display_off(ch);
Guennadi Liakhovetskiafe417c2010-09-03 07:20:39 +00002339 sh_mobile_lcdc_stop(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002340 break;
2341 case FB_EVENT_RESUME:
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00002342 mutex_lock(&ch->open_lock);
2343 sh_mobile_fb_reconfig(info);
2344 mutex_unlock(&ch->open_lock);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002345
Laurent Pinchart37c5dcc2011-09-09 15:45:43 +02002346 sh_mobile_lcdc_display_on(ch);
Guennadi Liakhovetskiebe5e122011-05-05 16:33:40 +00002347 sh_mobile_lcdc_start(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002348 }
2349
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00002350 return NOTIFY_OK;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002351}
2352
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002353/* -----------------------------------------------------------------------------
2354 * Probe/remove and driver init/exit
2355 */
2356
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002357static const struct fb_videomode default_720p = {
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002358 .name = "HDMI 720p",
2359 .xres = 1280,
2360 .yres = 720,
2361
2362 .left_margin = 220,
2363 .right_margin = 110,
2364 .hsync_len = 40,
2365
2366 .upper_margin = 20,
2367 .lower_margin = 5,
2368 .vsync_len = 5,
2369
2370 .pixclock = 13468,
2371 .refresh = 60,
2372 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
2373};
2374
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002375static int sh_mobile_lcdc_remove(struct platform_device *pdev)
2376{
2377 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002378 unsigned int i;
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002379
2380 fb_unregister_client(&priv->notifier);
2381
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002382 for (i = 0; i < ARRAY_SIZE(priv->overlays); i++)
2383 sh_mobile_lcdc_overlay_fb_unregister(&priv->overlays[i]);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002384 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
Laurent Pincharta67f3792011-11-29 14:37:35 +01002385 sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002386
2387 sh_mobile_lcdc_stop(priv);
2388
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002389 for (i = 0; i < ARRAY_SIZE(priv->overlays); i++) {
2390 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2391
2392 sh_mobile_lcdc_overlay_fb_cleanup(ovl);
2393
2394 if (ovl->fb_mem)
2395 dma_free_coherent(&pdev->dev, ovl->fb_size,
2396 ovl->fb_mem, ovl->dma_handle);
2397 }
2398
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002399 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02002400 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002401
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02002402 if (ch->tx_dev) {
2403 ch->tx_dev->lcdc = NULL;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002404 module_put(ch->cfg->tx_dev->dev.driver->owner);
Laurent Pincharte34d0bb2011-09-18 12:21:17 +02002405 }
Laurent Pinchart9a2985e2011-09-11 22:59:04 +02002406
Laurent Pincharta67f3792011-11-29 14:37:35 +01002407 sh_mobile_lcdc_channel_fb_cleanup(ch);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002408
Laurent Pincharta67f3792011-11-29 14:37:35 +01002409 if (ch->fb_mem)
2410 dma_free_coherent(&pdev->dev, ch->fb_size,
2411 ch->fb_mem, ch->dma_handle);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002412 }
2413
2414 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
Laurent Pinchart0c75c4e2012-03-15 18:34:05 +01002415 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
2416
2417 if (ch->bl)
2418 sh_mobile_lcdc_bl_remove(ch->bl);
2419 mutex_destroy(&ch->open_lock);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002420 }
2421
Laurent Pinchart4774c122011-09-07 15:47:07 +02002422 if (priv->dot_clk) {
2423 pm_runtime_disable(&pdev->dev);
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002424 clk_put(priv->dot_clk);
Laurent Pinchart4774c122011-09-07 15:47:07 +02002425 }
Laurent Pinchartb4bee692011-08-31 13:00:57 +02002426
2427 if (priv->base)
2428 iounmap(priv->base);
2429
2430 if (priv->irq)
2431 free_irq(priv->irq, priv);
2432 kfree(priv);
2433 return 0;
2434}
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002435
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002436static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002437{
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002438 int interface_type = ch->cfg->interface_type;
Laurent Pinchartf1f60b52011-09-07 11:09:26 +02002439
2440 switch (interface_type) {
2441 case RGB8:
2442 case RGB9:
2443 case RGB12A:
2444 case RGB12B:
2445 case RGB16:
2446 case RGB18:
2447 case RGB24:
2448 case SYS8A:
2449 case SYS8B:
2450 case SYS8C:
2451 case SYS8D:
2452 case SYS9:
2453 case SYS12:
2454 case SYS16A:
2455 case SYS16B:
2456 case SYS16C:
2457 case SYS18:
2458 case SYS24:
2459 break;
2460 default:
2461 return -EINVAL;
2462 }
2463
2464 /* SUBLCD only supports SYS interface */
2465 if (lcdc_chan_is_sublcd(ch)) {
2466 if (!(interface_type & LDMT1R_IFM))
2467 return -EINVAL;
2468
2469 interface_type &= ~LDMT1R_IFM;
2470 }
2471
2472 ch->ldmt1r_value = interface_type;
2473 return 0;
2474}
2475
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002476static int
Laurent Pinchart07073302012-08-13 13:56:46 +02002477sh_mobile_lcdc_overlay_init(struct sh_mobile_lcdc_overlay *ovl)
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002478{
2479 const struct sh_mobile_lcdc_format_info *format;
Laurent Pinchart07073302012-08-13 13:56:46 +02002480 struct device *dev = ovl->channel->lcdc->dev;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002481 int ret;
2482
2483 if (ovl->cfg->fourcc == 0)
2484 return 0;
2485
2486 /* Validate the format. */
2487 format = sh_mobile_format_info(ovl->cfg->fourcc);
2488 if (format == NULL) {
Laurent Pinchart07073302012-08-13 13:56:46 +02002489 dev_err(dev, "Invalid FOURCC %08x\n", ovl->cfg->fourcc);
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002490 return -EINVAL;
2491 }
2492
2493 ovl->enabled = false;
2494 ovl->mode = LCDC_OVERLAY_BLEND;
2495 ovl->alpha = 255;
2496 ovl->rop3 = 0;
2497 ovl->pos_x = 0;
2498 ovl->pos_y = 0;
2499
2500 /* The default Y virtual resolution is twice the panel size to allow for
2501 * double-buffering.
2502 */
2503 ovl->format = format;
2504 ovl->xres = ovl->cfg->max_xres;
2505 ovl->xres_virtual = ovl->xres;
2506 ovl->yres = ovl->cfg->max_yres;
2507 ovl->yres_virtual = ovl->yres * 2;
2508
2509 if (!format->yuv)
Laurent Pinchart16ca21c2012-07-18 16:29:20 +02002510 ovl->pitch = ovl->xres_virtual * format->bpp / 8;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002511 else
Laurent Pinchart16ca21c2012-07-18 16:29:20 +02002512 ovl->pitch = ovl->xres_virtual;
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002513
2514 /* Allocate frame buffer memory. */
2515 ovl->fb_size = ovl->cfg->max_xres * ovl->cfg->max_yres
2516 * format->bpp / 8 * 2;
Laurent Pinchart07073302012-08-13 13:56:46 +02002517 ovl->fb_mem = dma_alloc_coherent(dev, ovl->fb_size, &ovl->dma_handle,
2518 GFP_KERNEL);
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002519 if (!ovl->fb_mem) {
Laurent Pinchart07073302012-08-13 13:56:46 +02002520 dev_err(dev, "unable to allocate buffer\n");
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002521 return -ENOMEM;
2522 }
2523
2524 ret = sh_mobile_lcdc_overlay_fb_init(ovl);
2525 if (ret < 0)
2526 return ret;
2527
2528 return 0;
2529}
2530
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002531static int
Laurent Pinchart07073302012-08-13 13:56:46 +02002532sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002533{
Laurent Pinchart105784b2011-11-29 15:58:10 +01002534 const struct sh_mobile_lcdc_format_info *format;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002535 const struct sh_mobile_lcdc_chan_cfg *cfg = ch->cfg;
Laurent Pinchart07073302012-08-13 13:56:46 +02002536 struct device *dev = ch->lcdc->dev;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002537 const struct fb_videomode *max_mode;
2538 const struct fb_videomode *mode;
Laurent Pincharta67f3792011-11-29 14:37:35 +01002539 unsigned int num_modes;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002540 unsigned int max_size;
Laurent Pincharta67f3792011-11-29 14:37:35 +01002541 unsigned int i;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002542
Laurent Pinchartecd29942011-09-18 14:14:46 +02002543 ch->notify = sh_mobile_lcdc_display_notify;
Laurent Pincharta67472a2011-08-31 13:00:59 +02002544
Laurent Pinchart105784b2011-11-29 15:58:10 +01002545 /* Validate the format. */
2546 format = sh_mobile_format_info(cfg->fourcc);
2547 if (format == NULL) {
Laurent Pinchart07073302012-08-13 13:56:46 +02002548 dev_err(dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
Laurent Pinchart105784b2011-11-29 15:58:10 +01002549 return -EINVAL;
2550 }
2551
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002552 /* Iterate through the modes to validate them and find the highest
2553 * resolution.
2554 */
2555 max_mode = NULL;
2556 max_size = 0;
2557
Laurent Pinchart93ff2592011-11-29 14:33:41 +01002558 for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002559 unsigned int size = mode->yres * mode->xres;
2560
Laurent Pinchartedd153a2011-12-13 14:02:28 +01002561 /* NV12/NV21 buffers must have even number of lines */
2562 if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
2563 cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
Laurent Pinchart07073302012-08-13 13:56:46 +02002564 dev_err(dev, "yres must be multiple of 2 for "
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02002565 "YCbCr420 mode.\n");
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002566 return -EINVAL;
2567 }
2568
2569 if (size > max_size) {
2570 max_mode = mode;
2571 max_size = size;
2572 }
2573 }
2574
2575 if (!max_size)
2576 max_size = MAX_XRES * MAX_YRES;
2577 else
Laurent Pinchart07073302012-08-13 13:56:46 +02002578 dev_dbg(dev, "Found largest videomode %ux%u\n",
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002579 max_mode->xres, max_mode->yres);
2580
Laurent Pinchart93ff2592011-11-29 14:33:41 +01002581 if (cfg->lcd_modes == NULL) {
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002582 mode = &default_720p;
Laurent Pinchart93ff2592011-11-29 14:33:41 +01002583 num_modes = 1;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002584 } else {
Laurent Pinchart93ff2592011-11-29 14:33:41 +01002585 mode = cfg->lcd_modes;
2586 num_modes = cfg->num_modes;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002587 }
2588
Laurent Pinchartbd5f2c62012-07-18 16:59:16 +02002589 /* Use the first mode as default. The default Y virtual resolution is
2590 * twice the panel size to allow for double-buffering.
2591 */
Laurent Pinchart58f03d92011-11-30 23:07:30 +01002592 ch->format = format;
2593 ch->xres = mode->xres;
2594 ch->xres_virtual = mode->xres;
2595 ch->yres = mode->yres;
2596 ch->yres_virtual = mode->yres * 2;
2597
2598 if (!format->yuv) {
2599 ch->colorspace = V4L2_COLORSPACE_SRGB;
Laurent Pinchart16ca21c2012-07-18 16:29:20 +02002600 ch->pitch = ch->xres_virtual * format->bpp / 8;
Laurent Pinchart58f03d92011-11-30 23:07:30 +01002601 } else {
2602 ch->colorspace = V4L2_COLORSPACE_REC709;
Laurent Pinchart16ca21c2012-07-18 16:29:20 +02002603 ch->pitch = ch->xres_virtual;
Laurent Pinchart58f03d92011-11-30 23:07:30 +01002604 }
2605
Laurent Pincharta67f3792011-11-29 14:37:35 +01002606 ch->display.width = cfg->panel_cfg.width;
2607 ch->display.height = cfg->panel_cfg.height;
2608 ch->display.mode = *mode;
2609
2610 /* Allocate frame buffer memory. */
2611 ch->fb_size = max_size * format->bpp / 8 * 2;
Laurent Pinchart07073302012-08-13 13:56:46 +02002612 ch->fb_mem = dma_alloc_coherent(dev, ch->fb_size, &ch->dma_handle,
Laurent Pincharta67f3792011-11-29 14:37:35 +01002613 GFP_KERNEL);
2614 if (ch->fb_mem == NULL) {
Laurent Pinchart07073302012-08-13 13:56:46 +02002615 dev_err(dev, "unable to allocate buffer\n");
Laurent Pincharta67f3792011-11-29 14:37:35 +01002616 return -ENOMEM;
2617 }
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002618
Laurent Pinchart13f80ee2011-11-29 01:46:12 +01002619 /* Initialize the transmitter device if present. */
2620 if (cfg->tx_dev) {
2621 if (!cfg->tx_dev->dev.driver ||
2622 !try_module_get(cfg->tx_dev->dev.driver->owner)) {
Laurent Pinchart07073302012-08-13 13:56:46 +02002623 dev_warn(dev, "unable to get transmitter device\n");
Laurent Pinchart13f80ee2011-11-29 01:46:12 +01002624 return -EINVAL;
2625 }
2626 ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
2627 ch->tx_dev->lcdc = ch;
2628 ch->tx_dev->def_mode = *mode;
2629 }
2630
Laurent Pincharta67f3792011-11-29 14:37:35 +01002631 return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002632}
2633
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002634static int sh_mobile_lcdc_probe(struct platform_device *pdev)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002635{
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00002636 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002637 struct sh_mobile_lcdc_priv *priv;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002638 struct resource *res;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002639 int num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002640 int error;
Geert Uytterhoeven7374ccc2015-04-03 14:50:28 +02002641 int irq, i;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002642
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00002643 if (!pdata) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002644 dev_err(&pdev->dev, "no platform data defined\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002645 return -EINVAL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002646 }
2647
2648 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Geert Uytterhoeven7374ccc2015-04-03 14:50:28 +02002649 irq = platform_get_irq(pdev, 0);
2650 if (!res || irq < 0) {
Magnus Damm85645572008-12-19 15:34:41 +09002651 dev_err(&pdev->dev, "cannot get platform resources\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002652 return -ENOENT;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002653 }
2654
2655 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Markus Elfringe2810182018-04-26 12:24:18 +02002656 if (!priv)
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002657 return -ENOMEM;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002658
Laurent Pinchart4774c122011-09-07 15:47:07 +02002659 priv->dev = &pdev->dev;
Bartlomiej Zolnierkiewicz9076aa92018-05-14 15:47:30 +02002660
Geert Uytterhoeven7374ccc2015-04-03 14:50:28 +02002661 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
2662 mutex_init(&priv->ch[i].open_lock);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002663 platform_set_drvdata(pdev, priv);
2664
Geert Uytterhoeven7374ccc2015-04-03 14:50:28 +02002665 error = request_irq(irq, sh_mobile_lcdc_irq, 0,
Kay Sievers7ad33e72009-03-24 16:38:21 -07002666 dev_name(&pdev->dev), priv);
Magnus Damm85645572008-12-19 15:34:41 +09002667 if (error) {
2668 dev_err(&pdev->dev, "unable to request irq\n");
2669 goto err1;
2670 }
2671
Geert Uytterhoeven7374ccc2015-04-03 14:50:28 +02002672 priv->irq = irq;
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +00002673 atomic_set(&priv->hw_usecnt, -1);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002674
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002675 for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
2676 struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002677
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00002678 ch->lcdc = priv;
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002679 ch->cfg = &pdata->ch[i];
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00002680
2681 error = sh_mobile_lcdc_check_interface(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002682 if (error) {
2683 dev_err(&pdev->dev, "unsupported interface type\n");
2684 goto err1;
2685 }
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00002686 init_waitqueue_head(&ch->frame_end_wait);
2687 init_completion(&ch->vsync_completion);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002688
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002689 /* probe the backlight is there is one defined */
Laurent Pinchartb5ef9672011-11-22 00:56:58 +01002690 if (ch->cfg->bl_info.max_brightness)
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00002691 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
2692
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002693 switch (pdata->ch[i].chan) {
2694 case LCDC_CHAN_MAINLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02002695 ch->enabled = LDCNT2R_ME;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00002696 ch->reg_offs = lcdc_offs_mainlcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002697 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002698 break;
2699 case LCDC_CHAN_SUBLCD:
Laurent Pinchartce1c0b02011-07-13 12:13:47 +02002700 ch->enabled = LDCNT2R_SE;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00002701 ch->reg_offs = lcdc_offs_sublcd;
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002702 num_channels++;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002703 break;
2704 }
2705 }
2706
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002707 if (!num_channels) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002708 dev_err(&pdev->dev, "no channels defined\n");
2709 error = -EINVAL;
2710 goto err1;
2711 }
2712
Laurent Pinchartedd153a2011-12-13 14:02:28 +01002713 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002714 if (num_channels == 2)
Laurent Pinchartedd153a2011-12-13 14:02:28 +01002715 priv->forced_fourcc = pdata->ch[0].fourcc;
Magnus Damm417d4822011-01-05 10:21:00 +00002716
Guennadi Liakhovetskidba6f382010-06-30 09:26:35 +00002717 priv->base = ioremap_nocache(res->start, resource_size(res));
Wei Yongjunb49898c2017-02-08 16:43:59 +01002718 if (!priv->base) {
2719 error = -ENOMEM;
Guennadi Liakhovetskidba6f382010-06-30 09:26:35 +00002720 goto err1;
Wei Yongjunb49898c2017-02-08 16:43:59 +01002721 }
Guennadi Liakhovetskidba6f382010-06-30 09:26:35 +00002722
Laurent Pinchart0a7f17a2011-09-07 16:02:31 +02002723 error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002724 if (error) {
2725 dev_err(&pdev->dev, "unable to setup clocks\n");
2726 goto err1;
2727 }
2728
Laurent Pinchart4774c122011-09-07 15:47:07 +02002729 /* Enable runtime PM. */
2730 pm_runtime_enable(&pdev->dev);
Damian7caa4342011-05-18 11:10:07 +00002731
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002732 for (i = 0; i < num_channels; i++) {
Laurent Pinchart07073302012-08-13 13:56:46 +02002733 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002734
Laurent Pinchart07073302012-08-13 13:56:46 +02002735 error = sh_mobile_lcdc_channel_init(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002736 if (error)
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002737 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002738 }
2739
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002740 for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
2741 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2742
2743 ovl->cfg = &pdata->overlays[i];
2744 ovl->channel = &priv->ch[0];
2745
Laurent Pinchart07073302012-08-13 13:56:46 +02002746 error = sh_mobile_lcdc_overlay_init(ovl);
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002747 if (error)
2748 goto err1;
2749 }
2750
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002751 error = sh_mobile_lcdc_start(priv);
2752 if (error) {
2753 dev_err(&pdev->dev, "unable to start hardware\n");
2754 goto err1;
2755 }
2756
Laurent Pinchart3ce05592011-08-31 13:00:58 +02002757 for (i = 0; i < num_channels; i++) {
Paul Mundt1c6a3072009-07-01 06:50:31 +00002758 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Paul Mundt1c6a3072009-07-01 06:50:31 +00002759
Laurent Pincharta67f3792011-11-29 14:37:35 +01002760 error = sh_mobile_lcdc_channel_fb_register(ch);
2761 if (error)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002762 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002763 }
2764
Laurent Pinchartc5deac32011-12-12 18:43:16 +01002765 for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
2766 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2767
2768 error = sh_mobile_lcdc_overlay_fb_register(ovl);
2769 if (error)
2770 goto err1;
2771 }
2772
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00002773 /* Failure ignored */
2774 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
2775 fb_register_client(&priv->notifier);
2776
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002777 return 0;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002778err1:
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002779 sh_mobile_lcdc_remove(pdev);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00002780
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002781 return error;
2782}
2783
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002784static struct platform_driver sh_mobile_lcdc_driver = {
2785 .driver = {
2786 .name = "sh_mobile_lcdc_fb",
Magnus Damm2feb0752009-03-13 15:36:55 +00002787 .pm = &sh_mobile_lcdc_dev_pm_ops,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002788 },
2789 .probe = sh_mobile_lcdc_probe,
2790 .remove = sh_mobile_lcdc_remove,
2791};
2792
Axel Lin4277f2c2011-11-26 10:25:54 +08002793module_platform_driver(sh_mobile_lcdc_driver);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07002794
2795MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
2796MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
2797MODULE_LICENSE("GPL v2");