blob: 1c652da348eac950ee0c09f81c71c62673dcef72 [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
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/delay.h>
14#include <linux/mm.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070015#include <linux/clk.h>
Magnus Damm0246c472009-08-14 10:49:08 +000016#include <linux/pm_runtime.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070017#include <linux/platform_device.h>
18#include <linux/dma-mapping.h>
Magnus Damm85645572008-12-19 15:34:41 +090019#include <linux/interrupt.h>
Paul Mundt1c6a3072009-07-01 06:50:31 +000020#include <linux/vmalloc.h>
Phil Edworthy40331b22010-02-15 13:57:49 +000021#include <linux/ioctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +000023#include <linux/console.h>
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +000024#include <linux/backlight.h>
25#include <linux/gpio.h>
Paul Mundt225c9a82008-10-01 16:24:32 +090026#include <video/sh_mobile_lcdc.h>
Magnus Damm85645572008-12-19 15:34:41 +090027#include <asm/atomic.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070028
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +000029#include "sh_mobile_lcdcfb.h"
Damian7caa4342011-05-18 11:10:07 +000030#include "sh_mobile_meram.h"
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +000031
Phil Edworthya6f15ad2009-09-15 12:00:30 +000032#define SIDE_B_OFFSET 0x1000
33#define MIRROR_OFFSET 0x2000
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070034
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070035/* shared registers */
36#define _LDDCKR 0x410
37#define _LDDCKSTPR 0x414
38#define _LDINTR 0x468
39#define _LDSR 0x46c
40#define _LDCNT1R 0x470
41#define _LDCNT2R 0x474
Phil Edworthy9dd38812009-09-15 12:00:18 +000042#define _LDRCNTR 0x478
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070043#define _LDDDSR 0x47c
44#define _LDDWD0R 0x800
45#define _LDDRDR 0x840
46#define _LDDWAR 0x900
47#define _LDDRAR 0x904
48
Magnus Damm0246c472009-08-14 10:49:08 +000049/* shared registers and their order for context save/restore */
50static int lcdc_shared_regs[] = {
51 _LDDCKR,
52 _LDDCKSTPR,
53 _LDINTR,
54 _LDDDSR,
55 _LDCNT1R,
56 _LDCNT2R,
57};
58#define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
59
Guennadi Liakhovetskid2ecbab2010-11-04 11:06:06 +000060#define MAX_XRES 1920
61#define MAX_YRES 1080
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070062
Magnus Damm0246c472009-08-14 10:49:08 +000063static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070064 [LDDCKPAT1R] = 0x400,
65 [LDDCKPAT2R] = 0x404,
66 [LDMT1R] = 0x418,
67 [LDMT2R] = 0x41c,
68 [LDMT3R] = 0x420,
69 [LDDFR] = 0x424,
70 [LDSM1R] = 0x428,
Magnus Damm85645572008-12-19 15:34:41 +090071 [LDSM2R] = 0x42c,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070072 [LDSA1R] = 0x430,
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +000073 [LDSA2R] = 0x434,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070074 [LDMLSR] = 0x438,
75 [LDHCNR] = 0x448,
76 [LDHSYNR] = 0x44c,
77 [LDVLNR] = 0x450,
78 [LDVSYNR] = 0x454,
79 [LDPMR] = 0x460,
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +000080 [LDHAJR] = 0x4a0,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070081};
82
Magnus Damm0246c472009-08-14 10:49:08 +000083static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070084 [LDDCKPAT1R] = 0x408,
85 [LDDCKPAT2R] = 0x40c,
86 [LDMT1R] = 0x600,
87 [LDMT2R] = 0x604,
88 [LDMT3R] = 0x608,
89 [LDDFR] = 0x60c,
90 [LDSM1R] = 0x610,
Magnus Damm85645572008-12-19 15:34:41 +090091 [LDSM2R] = 0x614,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070092 [LDSA1R] = 0x618,
93 [LDMLSR] = 0x620,
94 [LDHCNR] = 0x624,
95 [LDHSYNR] = 0x628,
96 [LDVLNR] = 0x62c,
97 [LDVSYNR] = 0x630,
98 [LDPMR] = 0x63c,
99};
100
101#define START_LCDC 0x00000001
102#define LCDC_RESET 0x00000100
103#define DISPLAY_BEU 0x00000008
104#define LCDC_ENABLE 0x00000001
Magnus Damm85645572008-12-19 15:34:41 +0900105#define LDINTR_FE 0x00000400
Phil Edworthy9dd38812009-09-15 12:00:18 +0000106#define LDINTR_VSE 0x00000200
107#define LDINTR_VEE 0x00000100
Magnus Damm85645572008-12-19 15:34:41 +0900108#define LDINTR_FS 0x00000004
Phil Edworthy9dd38812009-09-15 12:00:18 +0000109#define LDINTR_VSS 0x00000002
110#define LDINTR_VES 0x00000001
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000111#define LDRCNTR_SRS 0x00020000
112#define LDRCNTR_SRC 0x00010000
113#define LDRCNTR_MRS 0x00000002
114#define LDRCNTR_MRC 0x00000001
Phil Edworthy40331b22010-02-15 13:57:49 +0000115#define LDSR_MRS 0x00000100
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700116
Guennadi Liakhovetskic44f9f72010-10-15 07:53:52 +0000117static const struct fb_videomode default_720p = {
118 .name = "HDMI 720p",
119 .xres = 1280,
120 .yres = 720,
121
Guennadi Liakhovetski5ae0cf82010-11-04 11:06:01 +0000122 .left_margin = 220,
123 .right_margin = 110,
124 .hsync_len = 40,
Guennadi Liakhovetskic44f9f72010-10-15 07:53:52 +0000125
126 .upper_margin = 20,
127 .lower_margin = 5,
128 .vsync_len = 5,
129
130 .pixclock = 13468,
Guennadi Liakhovetski5ae0cf82010-11-04 11:06:01 +0000131 .refresh = 60,
Guennadi Liakhovetskic44f9f72010-10-15 07:53:52 +0000132 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
Magnus Damm0246c472009-08-14 10:49:08 +0000133};
134
135struct sh_mobile_lcdc_priv {
136 void __iomem *base;
137 int irq;
138 atomic_t hw_usecnt;
139 struct device *dev;
140 struct clk *dot_clk;
141 unsigned long lddckr;
142 struct sh_mobile_lcdc_chan ch[2];
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000143 struct notifier_block notifier;
Magnus Damm0246c472009-08-14 10:49:08 +0000144 unsigned long saved_shared_regs[NR_SHARED_REGS];
145 int started;
Magnus Damm417d4822011-01-05 10:21:00 +0000146 int forced_bpp; /* 2 channel LCDC must share bpp setting */
Damian7caa4342011-05-18 11:10:07 +0000147 struct sh_mobile_meram_info *meram_dev;
Magnus Damm0246c472009-08-14 10:49:08 +0000148};
149
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000150static bool banked(int reg_nr)
151{
152 switch (reg_nr) {
153 case LDMT1R:
154 case LDMT2R:
155 case LDMT3R:
156 case LDDFR:
157 case LDSM1R:
158 case LDSA1R:
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000159 case LDSA2R:
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000160 case LDMLSR:
161 case LDHCNR:
162 case LDHSYNR:
163 case LDVLNR:
164 case LDVSYNR:
165 return true;
166 }
167 return false;
168}
169
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700170static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
171 int reg_nr, unsigned long data)
172{
173 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
Phil Edworthya6f15ad2009-09-15 12:00:30 +0000174 if (banked(reg_nr))
175 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
176 SIDE_B_OFFSET);
177}
178
179static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
180 int reg_nr, unsigned long data)
181{
182 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
183 MIRROR_OFFSET);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700184}
185
186static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
187 int reg_nr)
188{
189 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
190}
191
192static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
193 unsigned long reg_offs, unsigned long data)
194{
195 iowrite32(data, priv->base + reg_offs);
196}
197
198static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
199 unsigned long reg_offs)
200{
201 return ioread32(priv->base + reg_offs);
202}
203
204static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
205 unsigned long reg_offs,
206 unsigned long mask, unsigned long until)
207{
208 while ((lcdc_read(priv, reg_offs) & mask) != until)
209 cpu_relax();
210}
211
212static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
213{
214 return chan->cfg.chan == LCDC_CHAN_SUBLCD;
215}
216
217static void lcdc_sys_write_index(void *handle, unsigned long data)
218{
219 struct sh_mobile_lcdc_chan *ch = handle;
220
221 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
222 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
223 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
Magnus Damm909f10d2009-08-06 14:28:12 +0000224 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700225}
226
227static void lcdc_sys_write_data(void *handle, unsigned long data)
228{
229 struct sh_mobile_lcdc_chan *ch = handle;
230
231 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
232 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
233 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
Magnus Damm909f10d2009-08-06 14:28:12 +0000234 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700235}
236
237static unsigned long lcdc_sys_read_data(void *handle)
238{
239 struct sh_mobile_lcdc_chan *ch = handle;
240
241 lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
242 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
243 lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
244 udelay(1);
Magnus Damm909f10d2009-08-06 14:28:12 +0000245 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700246
Magnus Dammec56b662009-08-06 14:34:38 +0000247 return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700248}
249
250struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
251 lcdc_sys_write_index,
252 lcdc_sys_write_data,
253 lcdc_sys_read_data,
254};
255
Magnus Damm85645572008-12-19 15:34:41 +0900256static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
257{
Magnus Damm0246c472009-08-14 10:49:08 +0000258 if (atomic_inc_and_test(&priv->hw_usecnt)) {
259 pm_runtime_get_sync(priv->dev);
Magnus Damm85645572008-12-19 15:34:41 +0900260 if (priv->dot_clk)
261 clk_enable(priv->dot_clk);
262 }
263}
264
265static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
266{
Magnus Damm0246c472009-08-14 10:49:08 +0000267 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
Magnus Damm85645572008-12-19 15:34:41 +0900268 if (priv->dot_clk)
269 clk_disable(priv->dot_clk);
Magnus Damm0246c472009-08-14 10:49:08 +0000270 pm_runtime_put(priv->dev);
Magnus Damm85645572008-12-19 15:34:41 +0900271 }
272}
Magnus Damm85645572008-12-19 15:34:41 +0900273
Paul Mundt1c6a3072009-07-01 06:50:31 +0000274static int sh_mobile_lcdc_sginit(struct fb_info *info,
275 struct list_head *pagelist)
276{
277 struct sh_mobile_lcdc_chan *ch = info->par;
278 unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
279 struct page *page;
280 int nr_pages = 0;
281
282 sg_init_table(ch->sglist, nr_pages_max);
283
284 list_for_each_entry(page, pagelist, lru)
285 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
286
287 return nr_pages;
288}
289
Magnus Damm85645572008-12-19 15:34:41 +0900290static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
291 struct list_head *pagelist)
292{
293 struct sh_mobile_lcdc_chan *ch = info->par;
Magnus Dammef61aae2009-12-07 14:20:06 +0000294 struct sh_mobile_lcdc_board_cfg *bcfg = &ch->cfg.board_cfg;
Magnus Damm85645572008-12-19 15:34:41 +0900295
296 /* enable clocks before accessing hardware */
297 sh_mobile_lcdc_clk_on(ch->lcdc);
298
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900299 /*
300 * It's possible to get here without anything on the pagelist via
301 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
302 * invocation. In the former case, the acceleration routines are
303 * stepped in to when using the framebuffer console causing the
304 * workqueue to be scheduled without any dirty pages on the list.
305 *
306 * Despite this, a panel update is still needed given that the
307 * acceleration routines have their own methods for writing in
308 * that still need to be updated.
309 *
310 * The fsync() and empty pagelist case could be optimized for,
311 * but we don't bother, as any application exhibiting such
312 * behaviour is fundamentally broken anyways.
313 */
314 if (!list_empty(pagelist)) {
315 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
Paul Mundt1c6a3072009-07-01 06:50:31 +0000316
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900317 /* trigger panel update */
318 dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
Magnus Dammef61aae2009-12-07 14:20:06 +0000319 if (bcfg->start_transfer)
320 bcfg->start_transfer(bcfg->board_data, ch,
321 &sh_mobile_lcdc_sys_bus_ops);
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900322 lcdc_write_chan(ch, LDSM2R, 1);
323 dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
Magnus Dammef61aae2009-12-07 14:20:06 +0000324 } else {
325 if (bcfg->start_transfer)
326 bcfg->start_transfer(bcfg->board_data, ch,
327 &sh_mobile_lcdc_sys_bus_ops);
Paul Mundt5c1a56b2009-11-04 15:59:04 +0900328 lcdc_write_chan(ch, LDSM2R, 1);
Magnus Dammef61aae2009-12-07 14:20:06 +0000329 }
Magnus Damm85645572008-12-19 15:34:41 +0900330}
331
332static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
333{
334 struct fb_deferred_io *fbdefio = info->fbdefio;
335
336 if (fbdefio)
337 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
338}
339
340static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
341{
342 struct sh_mobile_lcdc_priv *priv = data;
Magnus Damm2feb0752009-03-13 15:36:55 +0000343 struct sh_mobile_lcdc_chan *ch;
Magnus Damm85645572008-12-19 15:34:41 +0900344 unsigned long tmp;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000345 unsigned long ldintr;
Magnus Damm2feb0752009-03-13 15:36:55 +0000346 int is_sub;
347 int k;
Magnus Damm85645572008-12-19 15:34:41 +0900348
349 /* acknowledge interrupt */
Phil Edworthy9dd38812009-09-15 12:00:18 +0000350 ldintr = tmp = lcdc_read(priv, _LDINTR);
351 /*
352 * disable further VSYNC End IRQs, preserve all other enabled IRQs,
353 * write 0 to bits 0-6 to ack all triggered IRQs.
354 */
355 tmp &= 0xffffff00 & ~LDINTR_VEE;
Magnus Damm85645572008-12-19 15:34:41 +0900356 lcdc_write(priv, _LDINTR, tmp);
357
Magnus Damm2feb0752009-03-13 15:36:55 +0000358 /* figure out if this interrupt is for main or sub lcd */
359 is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
360
Phil Edworthy9dd38812009-09-15 12:00:18 +0000361 /* wake up channel and disable clocks */
Magnus Damm2feb0752009-03-13 15:36:55 +0000362 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
363 ch = &priv->ch[k];
364
365 if (!ch->enabled)
366 continue;
367
Phil Edworthy9dd38812009-09-15 12:00:18 +0000368 /* Frame Start */
369 if (ldintr & LDINTR_FS) {
370 if (is_sub == lcdc_chan_is_sublcd(ch)) {
371 ch->frame_end = 1;
372 wake_up(&ch->frame_end_wait);
Magnus Damm2feb0752009-03-13 15:36:55 +0000373
Phil Edworthy9dd38812009-09-15 12:00:18 +0000374 sh_mobile_lcdc_clk_off(priv);
375 }
376 }
377
378 /* VSYNC End */
Phil Edworthy40331b22010-02-15 13:57:49 +0000379 if (ldintr & LDINTR_VES)
380 complete(&ch->vsync_completion);
Magnus Damm2feb0752009-03-13 15:36:55 +0000381 }
382
Magnus Damm85645572008-12-19 15:34:41 +0900383 return IRQ_HANDLED;
384}
385
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700386static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
387 int start)
388{
389 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
390 int k;
391
392 /* start or stop the lcdc */
393 if (start)
394 lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
395 else
396 lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
397
398 /* wait until power is applied/stopped on all channels */
399 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
400 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
401 while (1) {
402 tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
403 if (start && tmp == 3)
404 break;
405 if (!start && tmp == 0)
406 break;
407 cpu_relax();
408 }
409
410 if (!start)
411 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
412}
413
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000414static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
415{
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000416 struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
417 unsigned long h_total, hsync_pos, display_h_total;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000418 u32 tmp;
419
420 tmp = ch->ldmt1r_value;
421 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
422 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
423 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
424 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
425 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
426 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
427 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
428 lcdc_write_chan(ch, LDMT1R, tmp);
429
430 /* setup SYS bus */
431 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
432 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
433
434 /* horizontal configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000435 h_total = display_var->xres + display_var->hsync_len +
436 display_var->left_margin + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000437 tmp = h_total / 8; /* HTCN */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000438 tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000439 lcdc_write_chan(ch, LDHCNR, tmp);
440
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000441 hsync_pos = display_var->xres + display_var->right_margin;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000442 tmp = hsync_pos / 8; /* HSYNP */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000443 tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000444 lcdc_write_chan(ch, LDHSYNR, tmp);
445
446 /* vertical configuration */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000447 tmp = display_var->yres + display_var->vsync_len +
448 display_var->upper_margin + display_var->lower_margin; /* VTLN */
449 tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000450 lcdc_write_chan(ch, LDVLNR, tmp);
451
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000452 tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
453 tmp |= display_var->vsync_len << 16; /* VSYNW */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000454 lcdc_write_chan(ch, LDVSYNR, tmp);
455
456 /* Adjust horizontal synchronisation for HDMI */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000457 display_h_total = display_var->xres + display_var->hsync_len +
458 display_var->left_margin + display_var->right_margin;
459 tmp = ((display_var->xres & 7) << 24) |
460 ((display_h_total & 7) << 16) |
461 ((display_var->hsync_len & 7) << 8) |
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000462 hsync_pos;
463 lcdc_write_chan(ch, LDHAJR, tmp);
464}
465
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700466static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
467{
468 struct sh_mobile_lcdc_chan *ch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700469 struct sh_mobile_lcdc_board_cfg *board_cfg;
470 unsigned long tmp;
Magnus Damm417d4822011-01-05 10:21:00 +0000471 int bpp = 0;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000472 unsigned long ldddsr;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700473 int k, m;
474 int ret = 0;
475
Magnus Damm85645572008-12-19 15:34:41 +0900476 /* enable clocks before accessing the hardware */
Magnus Damm417d4822011-01-05 10:21:00 +0000477 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
478 if (priv->ch[k].enabled) {
Magnus Damm85645572008-12-19 15:34:41 +0900479 sh_mobile_lcdc_clk_on(priv);
Magnus Damm417d4822011-01-05 10:21:00 +0000480 if (!bpp)
481 bpp = priv->ch[k].info->var.bits_per_pixel;
482 }
483 }
Magnus Damm85645572008-12-19 15:34:41 +0900484
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700485 /* reset */
486 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
487 lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
488
489 /* enable LCDC channels */
490 tmp = lcdc_read(priv, _LDCNT2R);
491 tmp |= priv->ch[0].enabled;
492 tmp |= priv->ch[1].enabled;
493 lcdc_write(priv, _LDCNT2R, tmp);
494
495 /* read data from external memory, avoid using the BEU for now */
496 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
497
498 /* stop the lcdc first */
499 sh_mobile_lcdc_start_stop(priv, 0);
500
501 /* configure clocks */
502 tmp = priv->lddckr;
503 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
504 ch = &priv->ch[k];
505
506 if (!priv->ch[k].enabled)
507 continue;
508
509 m = ch->cfg.clock_divider;
510 if (!m)
511 continue;
512
513 if (m == 1)
514 m = 1 << 6;
515 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
516
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +0000517 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider denominator */
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +0000518 lcdc_write_chan(ch, LDDCKPAT1R, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700519 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
520 }
521
522 lcdc_write(priv, _LDDCKR, tmp);
523
524 /* start dotclock again */
525 lcdc_write(priv, _LDDCKSTPR, 0);
526 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
527
Magnus Damm85645572008-12-19 15:34:41 +0900528 /* interrupts are disabled to begin with */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700529 lcdc_write(priv, _LDINTR, 0);
530
531 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
532 ch = &priv->ch[k];
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700533
534 if (!ch->enabled)
535 continue;
536
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +0000537 sh_mobile_lcdc_geometry(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700538
539 /* power supply */
540 lcdc_write_chan(ch, LDPMR, 0);
541
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700542 board_cfg = &ch->cfg.board_cfg;
543 if (board_cfg->setup_sys)
544 ret = board_cfg->setup_sys(board_cfg->board_data, ch,
545 &sh_mobile_lcdc_sys_bus_ops);
546 if (ret)
547 return ret;
548 }
549
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700550 /* word and long word swap */
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000551 ldddsr = lcdc_read(priv, _LDDDSR);
552 if (priv->ch[0].info->var.nonstd)
553 lcdc_write(priv, _LDDDSR, ldddsr | 7);
554 else {
555 switch (bpp) {
556 case 16:
557 lcdc_write(priv, _LDDDSR, ldddsr | 6);
558 break;
559 case 24:
560 lcdc_write(priv, _LDDDSR, ldddsr | 7);
561 break;
562 case 32:
563 lcdc_write(priv, _LDDDSR, ldddsr | 4);
564 break;
565 }
Magnus Damm417d4822011-01-05 10:21:00 +0000566 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700567
568 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
Damian7caa4342011-05-18 11:10:07 +0000569 unsigned long base_addr_y;
570 unsigned long base_addr_c = 0;
571 int pitch;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700572 ch = &priv->ch[k];
573
574 if (!priv->ch[k].enabled)
575 continue;
576
577 /* set bpp format in PKF[4:0] */
578 tmp = lcdc_read_chan(ch, LDDFR);
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000579 tmp &= ~0x0003031f;
580 if (ch->info->var.nonstd) {
581 tmp |= (ch->info->var.nonstd << 16);
582 switch (ch->info->var.bits_per_pixel) {
583 case 12:
584 break;
585 case 16:
586 tmp |= (0x1 << 8);
587 break;
588 case 24:
589 tmp |= (0x2 << 8);
590 break;
591 }
592 } else {
593 switch (ch->info->var.bits_per_pixel) {
594 case 16:
595 tmp |= 0x03;
596 break;
597 case 24:
598 tmp |= 0x0b;
599 break;
600 case 32:
601 break;
602 }
Magnus Damm417d4822011-01-05 10:21:00 +0000603 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700604 lcdc_write_chan(ch, LDDFR, tmp);
605
Damian7caa4342011-05-18 11:10:07 +0000606 base_addr_y = ch->info->fix.smem_start;
607 base_addr_c = base_addr_y +
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000608 ch->info->var.xres *
Damian7caa4342011-05-18 11:10:07 +0000609 ch->info->var.yres_virtual;
610 pitch = ch->info->fix.line_length;
611
612 /* test if we can enable meram */
613 if (ch->cfg.meram_cfg && priv->meram_dev) {
614 struct sh_mobile_meram_cfg *cfg;
615 struct sh_mobile_meram_info *mdev;
616 unsigned long icb_addr_y, icb_addr_c;
617 int icb_pitch;
618 int pf;
619
620 cfg = ch->cfg.meram_cfg;
621 mdev = priv->meram_dev;
622 /* we need to de-init configured ICBs before we
623 * we can re-initialize them.
624 */
625 if (ch->meram_enabled)
626 mdev->ops->meram_unregister(mdev, cfg);
627
628 ch->meram_enabled = 0;
629
Damian3fedd2a2011-05-18 11:10:08 +0000630 if (ch->info->var.nonstd) {
631 if (ch->info->var.bits_per_pixel == 24)
632 pf = SH_MOBILE_MERAM_PF_NV24;
633 else
634 pf = SH_MOBILE_MERAM_PF_NV;
635 } else {
Damian7caa4342011-05-18 11:10:07 +0000636 pf = SH_MOBILE_MERAM_PF_RGB;
Damian3fedd2a2011-05-18 11:10:08 +0000637 }
Damian7caa4342011-05-18 11:10:07 +0000638
639 ret = mdev->ops->meram_register(mdev, cfg, pitch,
640 ch->info->var.yres,
641 pf,
642 base_addr_y,
643 base_addr_c,
644 &icb_addr_y,
645 &icb_addr_c,
646 &icb_pitch);
647 if (!ret) {
648 /* set LDSA1R value */
649 base_addr_y = icb_addr_y;
650 pitch = icb_pitch;
651
652 /* set LDSA2R value if required */
653 if (base_addr_c)
654 base_addr_c = icb_addr_c;
655
656 ch->meram_enabled = 1;
657 }
658 }
659
660 /* point out our frame buffer */
661 lcdc_write_chan(ch, LDSA1R, base_addr_y);
662 if (ch->info->var.nonstd)
663 lcdc_write_chan(ch, LDSA2R, base_addr_c);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700664
665 /* set line size */
Damian7caa4342011-05-18 11:10:07 +0000666 lcdc_write_chan(ch, LDMLSR, pitch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700667
Magnus Damm85645572008-12-19 15:34:41 +0900668 /* setup deferred io if SYS bus */
669 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
670 if (ch->ldmt1r_value & (1 << 12) && tmp) {
671 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
672 ch->defio.delay = msecs_to_jiffies(tmp);
Paul Mundte33afdd2009-07-07 11:24:32 +0900673 ch->info->fbdefio = &ch->defio;
674 fb_deferred_io_init(ch->info);
Magnus Damm85645572008-12-19 15:34:41 +0900675
676 /* one-shot mode */
677 lcdc_write_chan(ch, LDSM1R, 1);
678
679 /* enable "Frame End Interrupt Enable" bit */
680 lcdc_write(priv, _LDINTR, LDINTR_FE);
681
682 } else {
683 /* continuous read mode */
684 lcdc_write_chan(ch, LDSM1R, 0);
685 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700686 }
687
688 /* display output */
689 lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
690
691 /* start the lcdc */
692 sh_mobile_lcdc_start_stop(priv, 1);
Magnus Damm8e9bb192009-05-20 14:34:43 +0000693 priv->started = 1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700694
695 /* tell the board code to enable the panel */
696 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
697 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +0000698 if (!ch->enabled)
699 continue;
700
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700701 board_cfg = &ch->cfg.board_cfg;
Alexandre Courbot247f9932011-02-23 08:41:50 +0000702 if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
Guennadi Liakhovetskic2439392010-07-21 10:13:17 +0000703 board_cfg->display_on(board_cfg->board_data, ch->info);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +0000704 module_put(board_cfg->owner);
705 }
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000706
707 if (ch->bl) {
708 ch->bl->props.power = FB_BLANK_UNBLANK;
709 backlight_update_status(ch->bl);
710 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700711 }
712
713 return 0;
714}
715
716static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
717{
718 struct sh_mobile_lcdc_chan *ch;
719 struct sh_mobile_lcdc_board_cfg *board_cfg;
720 int k;
721
Magnus Damm2feb0752009-03-13 15:36:55 +0000722 /* clean up deferred io and ask board code to disable panel */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700723 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
724 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +0000725 if (!ch->enabled)
726 continue;
Magnus Damm2feb0752009-03-13 15:36:55 +0000727
728 /* deferred io mode:
729 * flush frame, and wait for frame end interrupt
730 * clean up deferred io and enable clock
731 */
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +0000732 if (ch->info && ch->info->fbdefio) {
Magnus Damm2feb0752009-03-13 15:36:55 +0000733 ch->frame_end = 0;
Paul Mundte33afdd2009-07-07 11:24:32 +0900734 schedule_delayed_work(&ch->info->deferred_work, 0);
Magnus Damm2feb0752009-03-13 15:36:55 +0000735 wait_event(ch->frame_end_wait, ch->frame_end);
Paul Mundte33afdd2009-07-07 11:24:32 +0900736 fb_deferred_io_cleanup(ch->info);
737 ch->info->fbdefio = NULL;
Magnus Damm2feb0752009-03-13 15:36:55 +0000738 sh_mobile_lcdc_clk_on(priv);
739 }
740
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +0000741 if (ch->bl) {
742 ch->bl->props.power = FB_BLANK_POWERDOWN;
743 backlight_update_status(ch->bl);
744 }
745
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700746 board_cfg = &ch->cfg.board_cfg;
Alexandre Courbot247f9932011-02-23 08:41:50 +0000747 if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700748 board_cfg->display_off(board_cfg->board_data);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +0000749 module_put(board_cfg->owner);
750 }
Damian7caa4342011-05-18 11:10:07 +0000751
752 /* disable the meram */
753 if (ch->meram_enabled) {
754 struct sh_mobile_meram_cfg *cfg;
755 struct sh_mobile_meram_info *mdev;
756 cfg = ch->cfg.meram_cfg;
757 mdev = priv->meram_dev;
758 mdev->ops->meram_unregister(mdev, cfg);
759 ch->meram_enabled = 0;
760 }
761
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700762 }
763
764 /* stop the lcdc */
Magnus Damm8e9bb192009-05-20 14:34:43 +0000765 if (priv->started) {
766 sh_mobile_lcdc_start_stop(priv, 0);
767 priv->started = 0;
768 }
Magnus Dammb51339f2008-10-31 20:23:26 +0900769
Magnus Damm85645572008-12-19 15:34:41 +0900770 /* stop clocks */
771 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
772 if (priv->ch[k].enabled)
773 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700774}
775
776static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
777{
778 int ifm, miftyp;
779
780 switch (ch->cfg.interface_type) {
781 case RGB8: ifm = 0; miftyp = 0; break;
782 case RGB9: ifm = 0; miftyp = 4; break;
783 case RGB12A: ifm = 0; miftyp = 5; break;
784 case RGB12B: ifm = 0; miftyp = 6; break;
785 case RGB16: ifm = 0; miftyp = 7; break;
786 case RGB18: ifm = 0; miftyp = 10; break;
787 case RGB24: ifm = 0; miftyp = 11; break;
788 case SYS8A: ifm = 1; miftyp = 0; break;
789 case SYS8B: ifm = 1; miftyp = 1; break;
790 case SYS8C: ifm = 1; miftyp = 2; break;
791 case SYS8D: ifm = 1; miftyp = 3; break;
792 case SYS9: ifm = 1; miftyp = 4; break;
793 case SYS12: ifm = 1; miftyp = 5; break;
794 case SYS16A: ifm = 1; miftyp = 7; break;
795 case SYS16B: ifm = 1; miftyp = 8; break;
796 case SYS16C: ifm = 1; miftyp = 9; break;
797 case SYS18: ifm = 1; miftyp = 10; break;
798 case SYS24: ifm = 1; miftyp = 11; break;
799 default: goto bad;
800 }
801
802 /* SUBLCD only supports SYS interface */
803 if (lcdc_chan_is_sublcd(ch)) {
804 if (ifm == 0)
805 goto bad;
806 else
807 ifm = 0;
808 }
809
810 ch->ldmt1r_value = (ifm << 12) | miftyp;
811 return 0;
812 bad:
813 return -EINVAL;
814}
815
Magnus Dammb51339f2008-10-31 20:23:26 +0900816static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
817 int clock_source,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700818 struct sh_mobile_lcdc_priv *priv)
819{
820 char *str;
821 int icksel;
822
823 switch (clock_source) {
824 case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
825 case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
826 case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
827 default:
828 return -EINVAL;
829 }
830
831 priv->lddckr = icksel << 16;
832
833 if (str) {
Magnus Dammb51339f2008-10-31 20:23:26 +0900834 priv->dot_clk = clk_get(&pdev->dev, str);
835 if (IS_ERR(priv->dot_clk)) {
836 dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
Magnus Dammb51339f2008-10-31 20:23:26 +0900837 return PTR_ERR(priv->dot_clk);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700838 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700839 }
840
Magnus Damm0246c472009-08-14 10:49:08 +0000841 /* Runtime PM support involves two step for this driver:
842 * 1) Enable Runtime PM
843 * 2) Force Runtime PM Resume since hardware is accessed from probe()
844 */
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +0000845 priv->dev = &pdev->dev;
Magnus Damm0246c472009-08-14 10:49:08 +0000846 pm_runtime_enable(priv->dev);
847 pm_runtime_resume(priv->dev);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700848 return 0;
849}
850
851static int sh_mobile_lcdc_setcolreg(u_int regno,
852 u_int red, u_int green, u_int blue,
853 u_int transp, struct fb_info *info)
854{
855 u32 *palette = info->pseudo_palette;
856
857 if (regno >= PALETTE_NR)
858 return -EINVAL;
859
860 /* only FB_VISUAL_TRUECOLOR supported */
861
862 red >>= 16 - info->var.red.length;
863 green >>= 16 - info->var.green.length;
864 blue >>= 16 - info->var.blue.length;
865 transp >>= 16 - info->var.transp.length;
866
867 palette[regno] = (red << info->var.red.offset) |
868 (green << info->var.green.offset) |
869 (blue << info->var.blue.offset) |
870 (transp << info->var.transp.offset);
871
872 return 0;
873}
874
875static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
876 .id = "SH Mobile LCDC",
877 .type = FB_TYPE_PACKED_PIXELS,
878 .visual = FB_VISUAL_TRUECOLOR,
879 .accel = FB_ACCEL_NONE,
Phil Edworthy9dd38812009-09-15 12:00:18 +0000880 .xpanstep = 0,
881 .ypanstep = 1,
882 .ywrapstep = 0,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700883};
884
Magnus Damm85645572008-12-19 15:34:41 +0900885static void sh_mobile_lcdc_fillrect(struct fb_info *info,
886 const struct fb_fillrect *rect)
887{
888 sys_fillrect(info, rect);
889 sh_mobile_lcdc_deferred_io_touch(info);
890}
891
892static void sh_mobile_lcdc_copyarea(struct fb_info *info,
893 const struct fb_copyarea *area)
894{
895 sys_copyarea(info, area);
896 sh_mobile_lcdc_deferred_io_touch(info);
897}
898
899static void sh_mobile_lcdc_imageblit(struct fb_info *info,
900 const struct fb_image *image)
901{
902 sys_imageblit(info, image);
903 sh_mobile_lcdc_deferred_io_touch(info);
904}
905
Phil Edworthy9dd38812009-09-15 12:00:18 +0000906static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
907 struct fb_info *info)
908{
909 struct sh_mobile_lcdc_chan *ch = info->par;
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000910 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
911 unsigned long ldrcntr;
912 unsigned long new_pan_offset;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000913 unsigned long base_addr_y, base_addr_c;
914 unsigned long c_offset;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000915
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000916 if (!var->nonstd)
917 new_pan_offset = (var->yoffset * info->fix.line_length) +
918 (var->xoffset * (info->var.bits_per_pixel / 8));
919 else
920 new_pan_offset = (var->yoffset * info->fix.line_length) +
921 (var->xoffset);
Phil Edworthy9dd38812009-09-15 12:00:18 +0000922
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000923 if (new_pan_offset == ch->pan_offset)
924 return 0; /* No change, do nothing */
925
926 ldrcntr = lcdc_read(priv, _LDRCNTR);
927
928 /* Set the source address for the next refresh */
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000929 base_addr_y = ch->dma_handle + new_pan_offset;
930 if (var->nonstd) {
931 /* Set y offset */
932 c_offset = (var->yoffset *
933 info->fix.line_length *
934 (info->var.bits_per_pixel - 8)) / 8;
935 base_addr_c = ch->dma_handle + var->xres * var->yres_virtual +
936 c_offset;
937 /* Set x offset */
938 if (info->var.bits_per_pixel == 24)
939 base_addr_c += 2 * var->xoffset;
940 else
941 base_addr_c += var->xoffset;
942 } else
943 base_addr_c = 0;
944
Damian7caa4342011-05-18 11:10:07 +0000945 if (!ch->meram_enabled) {
946 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
947 if (base_addr_c)
948 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
949 } else {
950 struct sh_mobile_meram_cfg *cfg;
951 struct sh_mobile_meram_info *mdev;
952 unsigned long icb_addr_y, icb_addr_c;
953 int ret;
954
955 cfg = ch->cfg.meram_cfg;
956 mdev = priv->meram_dev;
957 ret = mdev->ops->meram_update(mdev, cfg,
958 base_addr_y, base_addr_c,
959 &icb_addr_y, &icb_addr_c);
960 if (ret)
961 return ret;
962
963 lcdc_write_chan_mirror(ch, LDSA1R, icb_addr_y);
964 if (icb_addr_c)
965 lcdc_write_chan_mirror(ch, LDSA2R, icb_addr_c);
966
967 }
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +0000968
Phil Edworthy92e1f9a2010-02-11 10:24:25 +0000969 if (lcdc_chan_is_sublcd(ch))
970 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
971 else
972 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
973
974 ch->pan_offset = new_pan_offset;
975
976 sh_mobile_lcdc_deferred_io_touch(info);
Phil Edworthy9dd38812009-09-15 12:00:18 +0000977
978 return 0;
979}
980
Phil Edworthy40331b22010-02-15 13:57:49 +0000981static int sh_mobile_wait_for_vsync(struct fb_info *info)
982{
983 struct sh_mobile_lcdc_chan *ch = info->par;
984 unsigned long ldintr;
985 int ret;
986
987 /* Enable VSync End interrupt */
988 ldintr = lcdc_read(ch->lcdc, _LDINTR);
989 ldintr |= LDINTR_VEE;
990 lcdc_write(ch->lcdc, _LDINTR, ldintr);
991
992 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
993 msecs_to_jiffies(100));
994 if (!ret)
995 return -ETIMEDOUT;
996
997 return 0;
998}
999
1000static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
1001 unsigned long arg)
1002{
1003 int retval;
1004
1005 switch (cmd) {
1006 case FBIO_WAITFORVSYNC:
1007 retval = sh_mobile_wait_for_vsync(info);
1008 break;
1009
1010 default:
1011 retval = -ENOIOCTLCMD;
1012 break;
1013 }
1014 return retval;
1015}
1016
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001017static void sh_mobile_fb_reconfig(struct fb_info *info)
1018{
1019 struct sh_mobile_lcdc_chan *ch = info->par;
1020 struct fb_videomode mode1, mode2;
1021 struct fb_event event;
1022 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1023
1024 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1025 /* More framebuffer users are active */
1026 return;
1027
1028 fb_var_to_videomode(&mode1, &ch->display_var);
1029 fb_var_to_videomode(&mode2, &info->var);
1030
1031 if (fb_mode_is_equal(&mode1, &mode2))
1032 return;
1033
1034 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1035 if (fb_set_var(info, &ch->display_var) < 0)
1036 /* Couldn't reconfigure, hopefully, can continue as before */
1037 return;
1038
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001039 if (info->var.nonstd)
1040 info->fix.line_length = mode1.xres;
1041 else
1042 info->fix.line_length = mode1.xres * (ch->cfg.bpp / 8);
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001043
1044 /*
1045 * fb_set_var() calls the notifier change internally, only if
1046 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1047 * user event, we have to call the chain ourselves.
1048 */
1049 event.info = info;
Arnd Hannemanncc267ec2010-11-15 21:43:22 +00001050 event.data = &mode1;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001051 fb_notifier_call_chain(evnt, &event);
1052}
1053
1054/*
1055 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1056 * user == 1, or with console sem held, if user == 0.
1057 */
1058static int sh_mobile_release(struct fb_info *info, int user)
1059{
1060 struct sh_mobile_lcdc_chan *ch = info->par;
1061
1062 mutex_lock(&ch->open_lock);
1063 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1064
1065 ch->use_count--;
1066
1067 /* Nothing to reconfigure, when called from fbcon */
1068 if (user) {
Torben Hohnac751ef2011-01-25 15:07:35 -08001069 console_lock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001070 sh_mobile_fb_reconfig(info);
Torben Hohnac751ef2011-01-25 15:07:35 -08001071 console_unlock();
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001072 }
1073
1074 mutex_unlock(&ch->open_lock);
1075
1076 return 0;
1077}
1078
1079static int sh_mobile_open(struct fb_info *info, int user)
1080{
1081 struct sh_mobile_lcdc_chan *ch = info->par;
1082
1083 mutex_lock(&ch->open_lock);
1084 ch->use_count++;
1085
1086 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1087 mutex_unlock(&ch->open_lock);
1088
1089 return 0;
1090}
1091
1092static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1093{
1094 struct sh_mobile_lcdc_chan *ch = info->par;
Magnus Damm417d4822011-01-05 10:21:00 +00001095 struct sh_mobile_lcdc_priv *p = ch->lcdc;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001096
Guennadi Liakhovetskid2ecbab2010-11-04 11:06:06 +00001097 if (var->xres > MAX_XRES || var->yres > MAX_YRES ||
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001098 var->xres * var->yres * (ch->cfg.bpp / 8) * 2 > info->fix.smem_len) {
Paul Mundt830539d2010-11-10 17:51:44 +09001099 dev_warn(info->dev, "Invalid info: %u-%u-%u-%u x %u-%u-%u-%u @ %lukHz!\n",
Guennadi Liakhovetskid2ecbab2010-11-04 11:06:06 +00001100 var->left_margin, var->xres, var->right_margin, var->hsync_len,
1101 var->upper_margin, var->yres, var->lower_margin, var->vsync_len,
1102 PICOS2KHZ(var->pixclock));
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001103 return -EINVAL;
1104 }
Magnus Damm417d4822011-01-05 10:21:00 +00001105
1106 /* only accept the forced_bpp for dual channel configurations */
1107 if (p->forced_bpp && p->forced_bpp != var->bits_per_pixel)
1108 return -EINVAL;
1109
1110 switch (var->bits_per_pixel) {
1111 case 16: /* PKF[4:0] = 00011 - RGB 565 */
1112 case 24: /* PKF[4:0] = 01011 - RGB 888 */
1113 case 32: /* PKF[4:0] = 00000 - RGBA 888 */
1114 break;
1115 default:
1116 return -EINVAL;
1117 }
1118
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001119 return 0;
1120}
Phil Edworthy40331b22010-02-15 13:57:49 +00001121
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001122/*
1123 * Screen blanking. Behavior is as follows:
1124 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1125 * FB_BLANK_NORMAL: screen blanked, clocks enabled
1126 * FB_BLANK_VSYNC,
1127 * FB_BLANK_HSYNC,
1128 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1129 */
1130static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
1131{
1132 struct sh_mobile_lcdc_chan *ch = info->par;
1133 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1134
1135 /* blank the screen? */
1136 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
1137 struct fb_fillrect rect = {
1138 .width = info->var.xres,
1139 .height = info->var.yres,
1140 };
1141 sh_mobile_lcdc_fillrect(info, &rect);
1142 }
1143 /* turn clocks on? */
1144 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
1145 sh_mobile_lcdc_clk_on(p);
1146 }
1147 /* turn clocks off? */
1148 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
1149 /* make sure the screen is updated with the black fill before
1150 * switching the clocks off. one vsync is not enough since
1151 * blanking may occur in the middle of a refresh. deferred io
1152 * mode will reenable the clocks and update the screen in time,
1153 * so it does not need this. */
1154 if (!info->fbdefio) {
1155 sh_mobile_wait_for_vsync(info);
1156 sh_mobile_wait_for_vsync(info);
1157 }
1158 sh_mobile_lcdc_clk_off(p);
1159 }
1160
1161 ch->blank_status = blank;
1162 return 0;
1163}
1164
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001165static struct fb_ops sh_mobile_lcdc_ops = {
Phil Edworthy9dd38812009-09-15 12:00:18 +00001166 .owner = THIS_MODULE,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001167 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
Magnus Damm2540c112008-12-17 17:29:49 +09001168 .fb_read = fb_sys_read,
1169 .fb_write = fb_sys_write,
Magnus Damm85645572008-12-19 15:34:41 +09001170 .fb_fillrect = sh_mobile_lcdc_fillrect,
1171 .fb_copyarea = sh_mobile_lcdc_copyarea,
1172 .fb_imageblit = sh_mobile_lcdc_imageblit,
Alexandre Courbot8857b9a2011-02-23 08:36:30 +00001173 .fb_blank = sh_mobile_lcdc_blank,
Phil Edworthy9dd38812009-09-15 12:00:18 +00001174 .fb_pan_display = sh_mobile_fb_pan_display,
Phil Edworthy40331b22010-02-15 13:57:49 +00001175 .fb_ioctl = sh_mobile_ioctl,
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001176 .fb_open = sh_mobile_open,
1177 .fb_release = sh_mobile_release,
1178 .fb_check_var = sh_mobile_check_var,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001179};
1180
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001181static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
1182{
1183 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1184 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1185 int brightness = bdev->props.brightness;
1186
1187 if (bdev->props.power != FB_BLANK_UNBLANK ||
1188 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
1189 brightness = 0;
1190
1191 return cfg->set_brightness(cfg->board_data, brightness);
1192}
1193
1194static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
1195{
1196 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1197 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1198
1199 return cfg->get_brightness(cfg->board_data);
1200}
1201
1202static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
1203 struct fb_info *info)
1204{
1205 return (info->bl_dev == bdev);
1206}
1207
1208static struct backlight_ops sh_mobile_lcdc_bl_ops = {
1209 .options = BL_CORE_SUSPENDRESUME,
1210 .update_status = sh_mobile_lcdc_update_bl,
1211 .get_brightness = sh_mobile_lcdc_get_brightness,
1212 .check_fb = sh_mobile_lcdc_check_fb,
1213};
1214
1215static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
1216 struct sh_mobile_lcdc_chan *ch)
1217{
1218 struct backlight_device *bl;
1219
1220 bl = backlight_device_register(ch->cfg.bl_info.name, parent, ch,
1221 &sh_mobile_lcdc_bl_ops, NULL);
Dan Carpenterbeee1f22011-03-21 15:03:13 +00001222 if (IS_ERR(bl)) {
1223 dev_err(parent, "unable to register backlight device: %ld\n",
1224 PTR_ERR(bl));
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001225 return NULL;
1226 }
1227
1228 bl->props.max_brightness = ch->cfg.bl_info.max_brightness;
1229 bl->props.brightness = bl->props.max_brightness;
1230 backlight_update_status(bl);
1231
1232 return bl;
1233}
1234
1235static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
1236{
1237 backlight_device_unregister(bdev);
1238}
1239
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001240static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp,
1241 int nonstd)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001242{
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001243 if (nonstd) {
1244 switch (bpp) {
1245 case 12:
1246 case 16:
1247 case 24:
1248 var->bits_per_pixel = bpp;
1249 var->nonstd = nonstd;
1250 return 0;
1251 default:
1252 return -EINVAL;
1253 }
1254 }
1255
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001256 switch (bpp) {
1257 case 16: /* PKF[4:0] = 00011 - RGB 565 */
1258 var->red.offset = 11;
1259 var->red.length = 5;
1260 var->green.offset = 5;
1261 var->green.length = 6;
1262 var->blue.offset = 0;
1263 var->blue.length = 5;
1264 var->transp.offset = 0;
1265 var->transp.length = 0;
1266 break;
1267
Magnus Damm417d4822011-01-05 10:21:00 +00001268 case 24: /* PKF[4:0] = 01011 - RGB 888 */
1269 var->red.offset = 16;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001270 var->red.length = 8;
Magnus Damm417d4822011-01-05 10:21:00 +00001271 var->green.offset = 8;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001272 var->green.length = 8;
Magnus Damm417d4822011-01-05 10:21:00 +00001273 var->blue.offset = 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001274 var->blue.length = 8;
1275 var->transp.offset = 0;
1276 var->transp.length = 0;
1277 break;
Magnus Damm417d4822011-01-05 10:21:00 +00001278
1279 case 32: /* PKF[4:0] = 00000 - RGBA 888 */
1280 var->red.offset = 16;
1281 var->red.length = 8;
1282 var->green.offset = 8;
1283 var->green.length = 8;
1284 var->blue.offset = 0;
1285 var->blue.length = 8;
1286 var->transp.offset = 24;
1287 var->transp.length = 8;
1288 break;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001289 default:
1290 return -EINVAL;
1291 }
1292 var->bits_per_pixel = bpp;
1293 var->red.msb_right = 0;
1294 var->green.msb_right = 0;
1295 var->blue.msb_right = 0;
1296 var->transp.msb_right = 0;
1297 return 0;
1298}
1299
Magnus Damm2feb0752009-03-13 15:36:55 +00001300static int sh_mobile_lcdc_suspend(struct device *dev)
1301{
1302 struct platform_device *pdev = to_platform_device(dev);
1303
1304 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
1305 return 0;
1306}
1307
1308static int sh_mobile_lcdc_resume(struct device *dev)
1309{
1310 struct platform_device *pdev = to_platform_device(dev);
1311
1312 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
1313}
1314
Magnus Damm0246c472009-08-14 10:49:08 +00001315static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
1316{
1317 struct platform_device *pdev = to_platform_device(dev);
1318 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
1319 struct sh_mobile_lcdc_chan *ch;
1320 int k, n;
1321
1322 /* save per-channel registers */
1323 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
1324 ch = &p->ch[k];
1325 if (!ch->enabled)
1326 continue;
1327 for (n = 0; n < NR_CH_REGS; n++)
1328 ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
1329 }
1330
1331 /* save shared registers */
1332 for (n = 0; n < NR_SHARED_REGS; n++)
1333 p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
1334
1335 /* turn off LCDC hardware */
1336 lcdc_write(p, _LDCNT1R, 0);
1337 return 0;
1338}
1339
1340static int sh_mobile_lcdc_runtime_resume(struct device *dev)
1341{
1342 struct platform_device *pdev = to_platform_device(dev);
1343 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
1344 struct sh_mobile_lcdc_chan *ch;
1345 int k, n;
1346
1347 /* restore per-channel registers */
1348 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
1349 ch = &p->ch[k];
1350 if (!ch->enabled)
1351 continue;
1352 for (n = 0; n < NR_CH_REGS; n++)
1353 lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
1354 }
1355
1356 /* restore shared registers */
1357 for (n = 0; n < NR_SHARED_REGS; n++)
1358 lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
1359
1360 return 0;
1361}
1362
Alexey Dobriyan47145212009-12-14 18:00:08 -08001363static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
Magnus Damm2feb0752009-03-13 15:36:55 +00001364 .suspend = sh_mobile_lcdc_suspend,
1365 .resume = sh_mobile_lcdc_resume,
Magnus Damm0246c472009-08-14 10:49:08 +00001366 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
1367 .runtime_resume = sh_mobile_lcdc_runtime_resume,
Magnus Damm2feb0752009-03-13 15:36:55 +00001368};
1369
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001370/* locking: called with info->lock held */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001371static int sh_mobile_lcdc_notify(struct notifier_block *nb,
1372 unsigned long action, void *data)
1373{
1374 struct fb_event *event = data;
1375 struct fb_info *info = event->info;
1376 struct sh_mobile_lcdc_chan *ch = info->par;
1377 struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
Guennadi Liakhovetskiafe417c2010-09-03 07:20:39 +00001378 int ret;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001379
1380 if (&ch->lcdc->notifier != nb)
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001381 return NOTIFY_DONE;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001382
1383 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
1384 __func__, action, event->data);
1385
1386 switch(action) {
1387 case FB_EVENT_SUSPEND:
Alexandre Courbot247f9932011-02-23 08:41:50 +00001388 if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001389 board_cfg->display_off(board_cfg->board_data);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001390 module_put(board_cfg->owner);
1391 }
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001392 pm_runtime_put(info->device);
Guennadi Liakhovetskiafe417c2010-09-03 07:20:39 +00001393 sh_mobile_lcdc_stop(ch->lcdc);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001394 break;
1395 case FB_EVENT_RESUME:
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001396 mutex_lock(&ch->open_lock);
1397 sh_mobile_fb_reconfig(info);
1398 mutex_unlock(&ch->open_lock);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001399
1400 /* HDMI must be enabled before LCDC configuration */
Alexandre Courbot247f9932011-02-23 08:41:50 +00001401 if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001402 board_cfg->display_on(board_cfg->board_data, info);
Guennadi Liakhovetski6de9edd2010-09-03 07:20:23 +00001403 module_put(board_cfg->owner);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001404 }
1405
Guennadi Liakhovetskiafe417c2010-09-03 07:20:39 +00001406 ret = sh_mobile_lcdc_start(ch->lcdc);
1407 if (!ret)
1408 pm_runtime_get_sync(info->device);
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001409 }
1410
Guennadi Liakhovetskibaf16372010-09-03 07:20:08 +00001411 return NOTIFY_OK;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001412}
1413
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001414static int sh_mobile_lcdc_remove(struct platform_device *pdev);
1415
Uwe Kleine-Königc2e13032010-02-04 20:56:51 +01001416static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001417{
1418 struct fb_info *info;
1419 struct sh_mobile_lcdc_priv *priv;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001420 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001421 struct resource *res;
1422 int error;
1423 void *buf;
1424 int i, j;
1425
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001426 if (!pdata) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001427 dev_err(&pdev->dev, "no platform data defined\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001428 return -EINVAL;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001429 }
1430
1431 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Damm85645572008-12-19 15:34:41 +09001432 i = platform_get_irq(pdev, 0);
1433 if (!res || i < 0) {
1434 dev_err(&pdev->dev, "cannot get platform resources\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001435 return -ENOENT;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001436 }
1437
1438 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1439 if (!priv) {
1440 dev_err(&pdev->dev, "cannot allocate device data\n");
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001441 return -ENOMEM;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001442 }
1443
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001444 platform_set_drvdata(pdev, priv);
1445
Magnus Damm85645572008-12-19 15:34:41 +09001446 error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
Kay Sievers7ad33e72009-03-24 16:38:21 -07001447 dev_name(&pdev->dev), priv);
Magnus Damm85645572008-12-19 15:34:41 +09001448 if (error) {
1449 dev_err(&pdev->dev, "unable to request irq\n");
1450 goto err1;
1451 }
1452
1453 priv->irq = i;
Guennadi Liakhovetski5ef6b502010-09-03 07:19:57 +00001454 atomic_set(&priv->hw_usecnt, -1);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001455
1456 j = 0;
1457 for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001458 struct sh_mobile_lcdc_chan *ch = priv->ch + j;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001459
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001460 ch->lcdc = priv;
1461 memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
1462
1463 error = sh_mobile_lcdc_check_interface(ch);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001464 if (error) {
1465 dev_err(&pdev->dev, "unsupported interface type\n");
1466 goto err1;
1467 }
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001468 init_waitqueue_head(&ch->frame_end_wait);
1469 init_completion(&ch->vsync_completion);
1470 ch->pan_offset = 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001471
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001472 /* probe the backlight is there is one defined */
1473 if (ch->cfg.bl_info.max_brightness)
1474 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
1475
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001476 switch (pdata->ch[i].chan) {
1477 case LCDC_CHAN_MAINLCD:
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001478 ch->enabled = 1 << 1;
1479 ch->reg_offs = lcdc_offs_mainlcd;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001480 j++;
1481 break;
1482 case LCDC_CHAN_SUBLCD:
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001483 ch->enabled = 1 << 2;
1484 ch->reg_offs = lcdc_offs_sublcd;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001485 j++;
1486 break;
1487 }
1488 }
1489
1490 if (!j) {
1491 dev_err(&pdev->dev, "no channels defined\n");
1492 error = -EINVAL;
1493 goto err1;
1494 }
1495
Magnus Damm417d4822011-01-05 10:21:00 +00001496 /* for dual channel LCDC (MAIN + SUB) force shared bpp setting */
1497 if (j == 2)
1498 priv->forced_bpp = pdata->ch[0].bpp;
1499
Guennadi Liakhovetskidba6f382010-06-30 09:26:35 +00001500 priv->base = ioremap_nocache(res->start, resource_size(res));
1501 if (!priv->base)
1502 goto err1;
1503
Magnus Dammb51339f2008-10-31 20:23:26 +09001504 error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001505 if (error) {
1506 dev_err(&pdev->dev, "unable to setup clocks\n");
1507 goto err1;
1508 }
1509
Damian7caa4342011-05-18 11:10:07 +00001510 priv->meram_dev = pdata->meram_dev;
1511
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001512 for (i = 0; i < j; i++) {
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001513 struct fb_var_screeninfo *var;
Guennadi Liakhovetski71d3b0f2010-09-03 07:20:12 +00001514 const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001515 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
Guennadi Liakhovetskic44f9f72010-10-15 07:53:52 +00001516 struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
1517 const struct fb_videomode *mode = cfg->lcd_cfg;
Guennadi Liakhovetski71d3b0f2010-09-03 07:20:12 +00001518 unsigned long max_size = 0;
1519 int k;
Guennadi Liakhovetski5fd284e2010-11-04 11:06:11 +00001520 int num_cfg;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001521
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001522 ch->info = framebuffer_alloc(0, &pdev->dev);
1523 if (!ch->info) {
Paul Mundte33afdd2009-07-07 11:24:32 +09001524 dev_err(&pdev->dev, "unable to allocate fb_info\n");
1525 error = -ENOMEM;
1526 break;
1527 }
1528
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001529 info = ch->info;
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001530 var = &info->var;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001531 info->fbops = &sh_mobile_lcdc_ops;
Guennadi Liakhovetskic44f9f72010-10-15 07:53:52 +00001532 info->par = ch;
Guennadi Liakhovetskidd210502010-09-14 14:48:54 +00001533
1534 mutex_init(&ch->open_lock);
1535
Guennadi Liakhovetskic44f9f72010-10-15 07:53:52 +00001536 for (k = 0, lcd_cfg = mode;
1537 k < cfg->num_cfg && lcd_cfg;
Guennadi Liakhovetski71d3b0f2010-09-03 07:20:12 +00001538 k++, lcd_cfg++) {
1539 unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001540 /* NV12 buffers must have even number of lines */
1541 if ((cfg->nonstd) && cfg->bpp == 12 &&
1542 (lcd_cfg->yres & 0x1)) {
1543 dev_err(&pdev->dev, "yres must be multiple of 2"
1544 " for YCbCr420 mode.\n");
1545 error = -EINVAL;
1546 goto err1;
1547 }
Guennadi Liakhovetski71d3b0f2010-09-03 07:20:12 +00001548
1549 if (size > max_size) {
1550 max_cfg = lcd_cfg;
1551 max_size = size;
1552 }
1553 }
1554
Guennadi Liakhovetskic44f9f72010-10-15 07:53:52 +00001555 if (!mode)
Guennadi Liakhovetskid2ecbab2010-11-04 11:06:06 +00001556 max_size = MAX_XRES * MAX_YRES;
Guennadi Liakhovetskic44f9f72010-10-15 07:53:52 +00001557 else if (max_cfg)
1558 dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
1559 max_cfg->xres, max_cfg->yres);
Guennadi Liakhovetski71d3b0f2010-09-03 07:20:12 +00001560
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001561 info->fix = sh_mobile_lcdc_fix;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001562 info->fix.smem_len = max_size * 2 * cfg->bpp / 8;
1563
1564 /* Only pan in 2 line steps for NV12 */
1565 if (cfg->nonstd && cfg->bpp == 12)
1566 info->fix.ypanstep = 2;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001567
Guennadi Liakhovetski5fd284e2010-11-04 11:06:11 +00001568 if (!mode) {
Guennadi Liakhovetskic44f9f72010-10-15 07:53:52 +00001569 mode = &default_720p;
Guennadi Liakhovetski5fd284e2010-11-04 11:06:11 +00001570 num_cfg = 1;
1571 } else {
Guennadi Liakhovetskie0b9fb22010-12-21 11:46:26 +01001572 num_cfg = cfg->num_cfg;
Guennadi Liakhovetski5fd284e2010-11-04 11:06:11 +00001573 }
1574
1575 fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
Guennadi Liakhovetskic44f9f72010-10-15 07:53:52 +00001576
1577 fb_videomode_to_var(var, mode);
Guennadi Liakhovetskie0b9fb22010-12-21 11:46:26 +01001578 var->width = cfg->lcd_size_cfg.width;
1579 var->height = cfg->lcd_size_cfg.height;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001580 /* Default Y virtual resolution is 2x panel size */
1581 var->yres_virtual = var->yres * 2;
Paul Mundt1c6a3072009-07-01 06:50:31 +00001582 var->activate = FB_ACTIVATE_NOW;
Paul Mundt1c6a3072009-07-01 06:50:31 +00001583
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001584 error = sh_mobile_lcdc_set_bpp(var, cfg->bpp, cfg->nonstd);
Paul Mundt1c6a3072009-07-01 06:50:31 +00001585 if (error)
1586 break;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001587
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001588 buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001589 &ch->dma_handle, GFP_KERNEL);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001590 if (!buf) {
1591 dev_err(&pdev->dev, "unable to allocate buffer\n");
1592 error = -ENOMEM;
1593 break;
1594 }
1595
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001596 info->pseudo_palette = &ch->pseudo_palette;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001597 info->flags = FBINFO_FLAG_DEFAULT;
1598
1599 error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1600 if (error < 0) {
1601 dev_err(&pdev->dev, "unable to allocate cmap\n");
1602 dma_free_coherent(&pdev->dev, info->fix.smem_len,
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001603 buf, ch->dma_handle);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001604 break;
1605 }
1606
Guennadi Liakhovetski01ac25b2010-09-03 07:20:00 +00001607 info->fix.smem_start = ch->dma_handle;
Damian Hobson-Garcia53b50312011-02-24 05:47:13 +00001608 if (var->nonstd)
1609 info->fix.line_length = var->xres;
1610 else
1611 info->fix.line_length = var->xres * (cfg->bpp / 8);
1612
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001613 info->screen_base = buf;
1614 info->device = &pdev->dev;
Guennadi Liakhovetski1c120de2010-09-03 07:20:27 +00001615 ch->display_var = *var;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001616 }
1617
1618 if (error)
1619 goto err1;
1620
1621 error = sh_mobile_lcdc_start(priv);
1622 if (error) {
1623 dev_err(&pdev->dev, "unable to start hardware\n");
1624 goto err1;
1625 }
1626
1627 for (i = 0; i < j; i++) {
1628 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
1629
1630 info = ch->info;
1631
1632 if (info->fbdefio) {
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001633 ch->sglist = vmalloc(sizeof(struct scatterlist) *
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001634 info->fix.smem_len >> PAGE_SHIFT);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001635 if (!ch->sglist) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001636 dev_err(&pdev->dev, "cannot allocate sglist\n");
1637 goto err1;
1638 }
1639 }
1640
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001641 info->bl_dev = ch->bl;
1642
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001643 error = register_framebuffer(info);
1644 if (error < 0)
1645 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001646
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001647 dev_info(info->dev,
1648 "registered %s/%s as %dx%d %dbpp.\n",
1649 pdev->name,
Paul Mundt1c6a3072009-07-01 06:50:31 +00001650 (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001651 "mainlcd" : "sublcd",
Guennadi Liakhovetskic44f9f72010-10-15 07:53:52 +00001652 info->var.xres, info->var.yres,
Paul Mundt1c6a3072009-07-01 06:50:31 +00001653 ch->cfg.bpp);
Magnus Damm85645572008-12-19 15:34:41 +09001654
1655 /* deferred io mode: disable clock to save power */
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001656 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
Magnus Damm85645572008-12-19 15:34:41 +09001657 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001658 }
1659
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001660 /* Failure ignored */
1661 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
1662 fb_register_client(&priv->notifier);
1663
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001664 return 0;
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001665err1:
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001666 sh_mobile_lcdc_remove(pdev);
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001667
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001668 return error;
1669}
1670
1671static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1672{
1673 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1674 struct fb_info *info;
1675 int i;
1676
Guennadi Liakhovetski6011bde2010-07-21 10:13:21 +00001677 fb_unregister_client(&priv->notifier);
1678
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001679 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001680 if (priv->ch[i].info && priv->ch[i].info->dev)
Paul Mundte33afdd2009-07-07 11:24:32 +09001681 unregister_framebuffer(priv->ch[i].info);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001682
1683 sh_mobile_lcdc_stop(priv);
1684
1685 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
Paul Mundte33afdd2009-07-07 11:24:32 +09001686 info = priv->ch[i].info;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001687
Paul Mundte33afdd2009-07-07 11:24:32 +09001688 if (!info || !info->device)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001689 continue;
1690
Paul Mundt1c6a3072009-07-01 06:50:31 +00001691 if (priv->ch[i].sglist)
1692 vfree(priv->ch[i].sglist);
1693
Magnus Damm1ffbb032010-10-13 07:17:45 +00001694 if (info->screen_base)
1695 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1696 info->screen_base,
1697 priv->ch[i].dma_handle);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001698 fb_dealloc_cmap(&info->cmap);
Paul Mundte33afdd2009-07-07 11:24:32 +09001699 framebuffer_release(info);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001700 }
1701
Alexandre Courbot3b0fd9d2011-02-16 03:49:01 +00001702 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1703 if (priv->ch[i].bl)
1704 sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
1705 }
1706
Magnus Dammb51339f2008-10-31 20:23:26 +09001707 if (priv->dot_clk)
1708 clk_put(priv->dot_clk);
Magnus Damm0246c472009-08-14 10:49:08 +00001709
Guennadi Liakhovetski8bed9052010-04-30 16:07:00 +00001710 if (priv->dev)
1711 pm_runtime_disable(priv->dev);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001712
1713 if (priv->base)
1714 iounmap(priv->base);
1715
Magnus Damm85645572008-12-19 15:34:41 +09001716 if (priv->irq)
1717 free_irq(priv->irq, priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001718 kfree(priv);
1719 return 0;
1720}
1721
1722static struct platform_driver sh_mobile_lcdc_driver = {
1723 .driver = {
1724 .name = "sh_mobile_lcdc_fb",
1725 .owner = THIS_MODULE,
Magnus Damm2feb0752009-03-13 15:36:55 +00001726 .pm = &sh_mobile_lcdc_dev_pm_ops,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001727 },
1728 .probe = sh_mobile_lcdc_probe,
1729 .remove = sh_mobile_lcdc_remove,
1730};
1731
1732static int __init sh_mobile_lcdc_init(void)
1733{
1734 return platform_driver_register(&sh_mobile_lcdc_driver);
1735}
1736
1737static void __exit sh_mobile_lcdc_exit(void)
1738{
1739 platform_driver_unregister(&sh_mobile_lcdc_driver);
1740}
1741
1742module_init(sh_mobile_lcdc_init);
1743module_exit(sh_mobile_lcdc_exit);
1744
1745MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1746MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1747MODULE_LICENSE("GPL v2");