]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/video/sh_mobile_lcdcfb.c
ARM: mach-shmobile: ap4evb: modify touchpanel judgment condition
[net-next-2.6.git] / drivers / video / sh_mobile_lcdcfb.c
CommitLineData
cfb4f5d1
MD
1/*
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>
cfb4f5d1 15#include <linux/clk.h>
0246c471 16#include <linux/pm_runtime.h>
cfb4f5d1
MD
17#include <linux/platform_device.h>
18#include <linux/dma-mapping.h>
8564557a 19#include <linux/interrupt.h>
1c6a307a 20#include <linux/vmalloc.h>
40331b21 21#include <linux/ioctl.h>
5a0e3ad6 22#include <linux/slab.h>
225c9a8d 23#include <video/sh_mobile_lcdc.h>
8564557a 24#include <asm/atomic.h>
cfb4f5d1 25
6de9edd5
GL
26#include "sh_mobile_lcdcfb.h"
27
a6f15ade
PE
28#define SIDE_B_OFFSET 0x1000
29#define MIRROR_OFFSET 0x2000
cfb4f5d1 30
cfb4f5d1
MD
31/* shared registers */
32#define _LDDCKR 0x410
33#define _LDDCKSTPR 0x414
34#define _LDINTR 0x468
35#define _LDSR 0x46c
36#define _LDCNT1R 0x470
37#define _LDCNT2R 0x474
9dd38819 38#define _LDRCNTR 0x478
cfb4f5d1
MD
39#define _LDDDSR 0x47c
40#define _LDDWD0R 0x800
41#define _LDDRDR 0x840
42#define _LDDWAR 0x900
43#define _LDDRAR 0x904
44
0246c471
MD
45/* shared registers and their order for context save/restore */
46static int lcdc_shared_regs[] = {
47 _LDDCKR,
48 _LDDCKSTPR,
49 _LDINTR,
50 _LDDDSR,
51 _LDCNT1R,
52 _LDCNT2R,
53};
54#define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
55
0246c471 56static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
cfb4f5d1
MD
57 [LDDCKPAT1R] = 0x400,
58 [LDDCKPAT2R] = 0x404,
59 [LDMT1R] = 0x418,
60 [LDMT2R] = 0x41c,
61 [LDMT3R] = 0x420,
62 [LDDFR] = 0x424,
63 [LDSM1R] = 0x428,
8564557a 64 [LDSM2R] = 0x42c,
cfb4f5d1
MD
65 [LDSA1R] = 0x430,
66 [LDMLSR] = 0x438,
67 [LDHCNR] = 0x448,
68 [LDHSYNR] = 0x44c,
69 [LDVLNR] = 0x450,
70 [LDVSYNR] = 0x454,
71 [LDPMR] = 0x460,
6011bdea 72 [LDHAJR] = 0x4a0,
cfb4f5d1
MD
73};
74
0246c471 75static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
cfb4f5d1
MD
76 [LDDCKPAT1R] = 0x408,
77 [LDDCKPAT2R] = 0x40c,
78 [LDMT1R] = 0x600,
79 [LDMT2R] = 0x604,
80 [LDMT3R] = 0x608,
81 [LDDFR] = 0x60c,
82 [LDSM1R] = 0x610,
8564557a 83 [LDSM2R] = 0x614,
cfb4f5d1
MD
84 [LDSA1R] = 0x618,
85 [LDMLSR] = 0x620,
86 [LDHCNR] = 0x624,
87 [LDHSYNR] = 0x628,
88 [LDVLNR] = 0x62c,
89 [LDVSYNR] = 0x630,
90 [LDPMR] = 0x63c,
91};
92
93#define START_LCDC 0x00000001
94#define LCDC_RESET 0x00000100
95#define DISPLAY_BEU 0x00000008
96#define LCDC_ENABLE 0x00000001
8564557a 97#define LDINTR_FE 0x00000400
9dd38819
PE
98#define LDINTR_VSE 0x00000200
99#define LDINTR_VEE 0x00000100
8564557a 100#define LDINTR_FS 0x00000004
9dd38819
PE
101#define LDINTR_VSS 0x00000002
102#define LDINTR_VES 0x00000001
a6f15ade
PE
103#define LDRCNTR_SRS 0x00020000
104#define LDRCNTR_SRC 0x00010000
105#define LDRCNTR_MRS 0x00000002
106#define LDRCNTR_MRC 0x00000001
40331b21 107#define LDSR_MRS 0x00000100
cfb4f5d1 108
0246c471
MD
109struct sh_mobile_lcdc_priv {
110 void __iomem *base;
111 int irq;
112 atomic_t hw_usecnt;
113 struct device *dev;
114 struct clk *dot_clk;
115 unsigned long lddckr;
116 struct sh_mobile_lcdc_chan ch[2];
6011bdea 117 struct notifier_block notifier;
0246c471
MD
118 unsigned long saved_shared_regs[NR_SHARED_REGS];
119 int started;
120};
121
a6f15ade
PE
122static bool banked(int reg_nr)
123{
124 switch (reg_nr) {
125 case LDMT1R:
126 case LDMT2R:
127 case LDMT3R:
128 case LDDFR:
129 case LDSM1R:
130 case LDSA1R:
131 case LDMLSR:
132 case LDHCNR:
133 case LDHSYNR:
134 case LDVLNR:
135 case LDVSYNR:
136 return true;
137 }
138 return false;
139}
140
cfb4f5d1
MD
141static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
142 int reg_nr, unsigned long data)
143{
144 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
a6f15ade
PE
145 if (banked(reg_nr))
146 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
147 SIDE_B_OFFSET);
148}
149
150static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
151 int reg_nr, unsigned long data)
152{
153 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
154 MIRROR_OFFSET);
cfb4f5d1
MD
155}
156
157static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
158 int reg_nr)
159{
160 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
161}
162
163static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
164 unsigned long reg_offs, unsigned long data)
165{
166 iowrite32(data, priv->base + reg_offs);
167}
168
169static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
170 unsigned long reg_offs)
171{
172 return ioread32(priv->base + reg_offs);
173}
174
175static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
176 unsigned long reg_offs,
177 unsigned long mask, unsigned long until)
178{
179 while ((lcdc_read(priv, reg_offs) & mask) != until)
180 cpu_relax();
181}
182
183static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
184{
185 return chan->cfg.chan == LCDC_CHAN_SUBLCD;
186}
187
188static void lcdc_sys_write_index(void *handle, unsigned long data)
189{
190 struct sh_mobile_lcdc_chan *ch = handle;
191
192 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
193 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
194 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
909f10de 195 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
cfb4f5d1
MD
196}
197
198static void lcdc_sys_write_data(void *handle, unsigned long data)
199{
200 struct sh_mobile_lcdc_chan *ch = handle;
201
202 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
203 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
204 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
909f10de 205 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
cfb4f5d1
MD
206}
207
208static unsigned long lcdc_sys_read_data(void *handle)
209{
210 struct sh_mobile_lcdc_chan *ch = handle;
211
212 lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
213 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
214 lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
215 udelay(1);
909f10de 216 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
cfb4f5d1 217
ec56b66f 218 return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
cfb4f5d1
MD
219}
220
221struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
222 lcdc_sys_write_index,
223 lcdc_sys_write_data,
224 lcdc_sys_read_data,
225};
226
8564557a
MD
227static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
228{
0246c471
MD
229 if (atomic_inc_and_test(&priv->hw_usecnt)) {
230 pm_runtime_get_sync(priv->dev);
8564557a
MD
231 if (priv->dot_clk)
232 clk_enable(priv->dot_clk);
233 }
234}
235
236static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
237{
0246c471 238 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
8564557a
MD
239 if (priv->dot_clk)
240 clk_disable(priv->dot_clk);
0246c471 241 pm_runtime_put(priv->dev);
8564557a
MD
242 }
243}
8564557a 244
1c6a307a
PM
245static int sh_mobile_lcdc_sginit(struct fb_info *info,
246 struct list_head *pagelist)
247{
248 struct sh_mobile_lcdc_chan *ch = info->par;
249 unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
250 struct page *page;
251 int nr_pages = 0;
252
253 sg_init_table(ch->sglist, nr_pages_max);
254
255 list_for_each_entry(page, pagelist, lru)
256 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
257
258 return nr_pages;
259}
260
8564557a
MD
261static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
262 struct list_head *pagelist)
263{
264 struct sh_mobile_lcdc_chan *ch = info->par;
ef61aae4 265 struct sh_mobile_lcdc_board_cfg *bcfg = &ch->cfg.board_cfg;
8564557a
MD
266
267 /* enable clocks before accessing hardware */
268 sh_mobile_lcdc_clk_on(ch->lcdc);
269
5c1a56b5
PM
270 /*
271 * It's possible to get here without anything on the pagelist via
272 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
273 * invocation. In the former case, the acceleration routines are
274 * stepped in to when using the framebuffer console causing the
275 * workqueue to be scheduled without any dirty pages on the list.
276 *
277 * Despite this, a panel update is still needed given that the
278 * acceleration routines have their own methods for writing in
279 * that still need to be updated.
280 *
281 * The fsync() and empty pagelist case could be optimized for,
282 * but we don't bother, as any application exhibiting such
283 * behaviour is fundamentally broken anyways.
284 */
285 if (!list_empty(pagelist)) {
286 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
287
288 /* trigger panel update */
289 dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
ef61aae4
MD
290 if (bcfg->start_transfer)
291 bcfg->start_transfer(bcfg->board_data, ch,
292 &sh_mobile_lcdc_sys_bus_ops);
5c1a56b5
PM
293 lcdc_write_chan(ch, LDSM2R, 1);
294 dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
ef61aae4
MD
295 } else {
296 if (bcfg->start_transfer)
297 bcfg->start_transfer(bcfg->board_data, ch,
298 &sh_mobile_lcdc_sys_bus_ops);
5c1a56b5 299 lcdc_write_chan(ch, LDSM2R, 1);
ef61aae4 300 }
8564557a
MD
301}
302
303static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
304{
305 struct fb_deferred_io *fbdefio = info->fbdefio;
306
307 if (fbdefio)
308 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
309}
310
311static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
312{
313 struct sh_mobile_lcdc_priv *priv = data;
2feb075a 314 struct sh_mobile_lcdc_chan *ch;
8564557a 315 unsigned long tmp;
9dd38819 316 unsigned long ldintr;
2feb075a
MD
317 int is_sub;
318 int k;
8564557a
MD
319
320 /* acknowledge interrupt */
9dd38819
PE
321 ldintr = tmp = lcdc_read(priv, _LDINTR);
322 /*
323 * disable further VSYNC End IRQs, preserve all other enabled IRQs,
324 * write 0 to bits 0-6 to ack all triggered IRQs.
325 */
326 tmp &= 0xffffff00 & ~LDINTR_VEE;
8564557a
MD
327 lcdc_write(priv, _LDINTR, tmp);
328
2feb075a
MD
329 /* figure out if this interrupt is for main or sub lcd */
330 is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
331
9dd38819 332 /* wake up channel and disable clocks */
2feb075a
MD
333 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
334 ch = &priv->ch[k];
335
336 if (!ch->enabled)
337 continue;
338
9dd38819
PE
339 /* Frame Start */
340 if (ldintr & LDINTR_FS) {
341 if (is_sub == lcdc_chan_is_sublcd(ch)) {
342 ch->frame_end = 1;
343 wake_up(&ch->frame_end_wait);
2feb075a 344
9dd38819
PE
345 sh_mobile_lcdc_clk_off(priv);
346 }
347 }
348
349 /* VSYNC End */
40331b21
PE
350 if (ldintr & LDINTR_VES)
351 complete(&ch->vsync_completion);
2feb075a
MD
352 }
353
8564557a
MD
354 return IRQ_HANDLED;
355}
356
cfb4f5d1
MD
357static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
358 int start)
359{
360 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
361 int k;
362
363 /* start or stop the lcdc */
364 if (start)
365 lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
366 else
367 lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
368
369 /* wait until power is applied/stopped on all channels */
370 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
371 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
372 while (1) {
373 tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
374 if (start && tmp == 3)
375 break;
376 if (!start && tmp == 0)
377 break;
378 cpu_relax();
379 }
380
381 if (!start)
382 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
383}
384
6011bdea
GL
385static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
386{
1c120deb
GL
387 struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
388 unsigned long h_total, hsync_pos, display_h_total;
6011bdea
GL
389 u32 tmp;
390
391 tmp = ch->ldmt1r_value;
392 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
393 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
394 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
395 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
396 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
397 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
398 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
399 lcdc_write_chan(ch, LDMT1R, tmp);
400
401 /* setup SYS bus */
402 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
403 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
404
405 /* horizontal configuration */
1c120deb
GL
406 h_total = display_var->xres + display_var->hsync_len +
407 display_var->left_margin + display_var->right_margin;
6011bdea 408 tmp = h_total / 8; /* HTCN */
1c120deb 409 tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
6011bdea
GL
410 lcdc_write_chan(ch, LDHCNR, tmp);
411
1c120deb 412 hsync_pos = display_var->xres + display_var->right_margin;
6011bdea 413 tmp = hsync_pos / 8; /* HSYNP */
1c120deb 414 tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
6011bdea
GL
415 lcdc_write_chan(ch, LDHSYNR, tmp);
416
417 /* vertical configuration */
1c120deb
GL
418 tmp = display_var->yres + display_var->vsync_len +
419 display_var->upper_margin + display_var->lower_margin; /* VTLN */
420 tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
6011bdea
GL
421 lcdc_write_chan(ch, LDVLNR, tmp);
422
1c120deb
GL
423 tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
424 tmp |= display_var->vsync_len << 16; /* VSYNW */
6011bdea
GL
425 lcdc_write_chan(ch, LDVSYNR, tmp);
426
427 /* Adjust horizontal synchronisation for HDMI */
1c120deb
GL
428 display_h_total = display_var->xres + display_var->hsync_len +
429 display_var->left_margin + display_var->right_margin;
430 tmp = ((display_var->xres & 7) << 24) |
431 ((display_h_total & 7) << 16) |
432 ((display_var->hsync_len & 7) << 8) |
6011bdea
GL
433 hsync_pos;
434 lcdc_write_chan(ch, LDHAJR, tmp);
435}
436
cfb4f5d1
MD
437static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
438{
439 struct sh_mobile_lcdc_chan *ch;
cfb4f5d1
MD
440 struct sh_mobile_lcdc_board_cfg *board_cfg;
441 unsigned long tmp;
442 int k, m;
443 int ret = 0;
444
8564557a
MD
445 /* enable clocks before accessing the hardware */
446 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
447 if (priv->ch[k].enabled)
448 sh_mobile_lcdc_clk_on(priv);
449
cfb4f5d1
MD
450 /* reset */
451 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
452 lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
453
454 /* enable LCDC channels */
455 tmp = lcdc_read(priv, _LDCNT2R);
456 tmp |= priv->ch[0].enabled;
457 tmp |= priv->ch[1].enabled;
458 lcdc_write(priv, _LDCNT2R, tmp);
459
460 /* read data from external memory, avoid using the BEU for now */
461 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
462
463 /* stop the lcdc first */
464 sh_mobile_lcdc_start_stop(priv, 0);
465
466 /* configure clocks */
467 tmp = priv->lddckr;
468 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
469 ch = &priv->ch[k];
470
471 if (!priv->ch[k].enabled)
472 continue;
473
474 m = ch->cfg.clock_divider;
475 if (!m)
476 continue;
477
478 if (m == 1)
479 m = 1 << 6;
480 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
481
1c120deb 482 lcdc_write_chan(ch, LDDCKPAT1R, 0);
cfb4f5d1
MD
483 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
484 }
485
486 lcdc_write(priv, _LDDCKR, tmp);
487
488 /* start dotclock again */
489 lcdc_write(priv, _LDDCKSTPR, 0);
490 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
491
8564557a 492 /* interrupts are disabled to begin with */
cfb4f5d1
MD
493 lcdc_write(priv, _LDINTR, 0);
494
495 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
496 ch = &priv->ch[k];
cfb4f5d1
MD
497
498 if (!ch->enabled)
499 continue;
500
6011bdea 501 sh_mobile_lcdc_geometry(ch);
cfb4f5d1
MD
502
503 /* power supply */
504 lcdc_write_chan(ch, LDPMR, 0);
505
cfb4f5d1
MD
506 board_cfg = &ch->cfg.board_cfg;
507 if (board_cfg->setup_sys)
508 ret = board_cfg->setup_sys(board_cfg->board_data, ch,
509 &sh_mobile_lcdc_sys_bus_ops);
510 if (ret)
511 return ret;
512 }
513
cfb4f5d1
MD
514 /* word and long word swap */
515 lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 6);
516
517 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
518 ch = &priv->ch[k];
519
520 if (!priv->ch[k].enabled)
521 continue;
522
523 /* set bpp format in PKF[4:0] */
524 tmp = lcdc_read_chan(ch, LDDFR);
1c120deb 525 tmp &= ~0x0001001f;
e33afddc 526 tmp |= (ch->info->var.bits_per_pixel == 16) ? 3 : 0;
cfb4f5d1
MD
527 lcdc_write_chan(ch, LDDFR, tmp);
528
529 /* point out our frame buffer */
e33afddc 530 lcdc_write_chan(ch, LDSA1R, ch->info->fix.smem_start);
cfb4f5d1
MD
531
532 /* set line size */
e33afddc 533 lcdc_write_chan(ch, LDMLSR, ch->info->fix.line_length);
cfb4f5d1 534
8564557a
MD
535 /* setup deferred io if SYS bus */
536 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
537 if (ch->ldmt1r_value & (1 << 12) && tmp) {
538 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
539 ch->defio.delay = msecs_to_jiffies(tmp);
e33afddc
PM
540 ch->info->fbdefio = &ch->defio;
541 fb_deferred_io_init(ch->info);
8564557a
MD
542
543 /* one-shot mode */
544 lcdc_write_chan(ch, LDSM1R, 1);
545
546 /* enable "Frame End Interrupt Enable" bit */
547 lcdc_write(priv, _LDINTR, LDINTR_FE);
548
549 } else {
550 /* continuous read mode */
551 lcdc_write_chan(ch, LDSM1R, 0);
552 }
cfb4f5d1
MD
553 }
554
555 /* display output */
556 lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
557
558 /* start the lcdc */
559 sh_mobile_lcdc_start_stop(priv, 1);
8e9bb19e 560 priv->started = 1;
cfb4f5d1
MD
561
562 /* tell the board code to enable the panel */
563 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
564 ch = &priv->ch[k];
21bc1f02
MD
565 if (!ch->enabled)
566 continue;
567
cfb4f5d1 568 board_cfg = &ch->cfg.board_cfg;
6de9edd5 569 if (try_module_get(board_cfg->owner) && board_cfg->display_on) {
c2439398 570 board_cfg->display_on(board_cfg->board_data, ch->info);
6de9edd5
GL
571 module_put(board_cfg->owner);
572 }
cfb4f5d1
MD
573 }
574
575 return 0;
576}
577
578static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
579{
580 struct sh_mobile_lcdc_chan *ch;
581 struct sh_mobile_lcdc_board_cfg *board_cfg;
582 int k;
583
2feb075a 584 /* clean up deferred io and ask board code to disable panel */
cfb4f5d1
MD
585 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
586 ch = &priv->ch[k];
21bc1f02
MD
587 if (!ch->enabled)
588 continue;
8564557a 589
2feb075a
MD
590 /* deferred io mode:
591 * flush frame, and wait for frame end interrupt
592 * clean up deferred io and enable clock
593 */
5ef6b505 594 if (ch->info && ch->info->fbdefio) {
2feb075a 595 ch->frame_end = 0;
e33afddc 596 schedule_delayed_work(&ch->info->deferred_work, 0);
2feb075a 597 wait_event(ch->frame_end_wait, ch->frame_end);
e33afddc
PM
598 fb_deferred_io_cleanup(ch->info);
599 ch->info->fbdefio = NULL;
2feb075a 600 sh_mobile_lcdc_clk_on(priv);
8564557a 601 }
2feb075a
MD
602
603 board_cfg = &ch->cfg.board_cfg;
6de9edd5 604 if (try_module_get(board_cfg->owner) && board_cfg->display_off) {
2feb075a 605 board_cfg->display_off(board_cfg->board_data);
6de9edd5
GL
606 module_put(board_cfg->owner);
607 }
cfb4f5d1
MD
608 }
609
610 /* stop the lcdc */
8e9bb19e
MD
611 if (priv->started) {
612 sh_mobile_lcdc_start_stop(priv, 0);
613 priv->started = 0;
614 }
b51339ff 615
8564557a
MD
616 /* stop clocks */
617 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
618 if (priv->ch[k].enabled)
619 sh_mobile_lcdc_clk_off(priv);
cfb4f5d1
MD
620}
621
622static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
623{
624 int ifm, miftyp;
625
626 switch (ch->cfg.interface_type) {
627 case RGB8: ifm = 0; miftyp = 0; break;
628 case RGB9: ifm = 0; miftyp = 4; break;
629 case RGB12A: ifm = 0; miftyp = 5; break;
630 case RGB12B: ifm = 0; miftyp = 6; break;
631 case RGB16: ifm = 0; miftyp = 7; break;
632 case RGB18: ifm = 0; miftyp = 10; break;
633 case RGB24: ifm = 0; miftyp = 11; break;
634 case SYS8A: ifm = 1; miftyp = 0; break;
635 case SYS8B: ifm = 1; miftyp = 1; break;
636 case SYS8C: ifm = 1; miftyp = 2; break;
637 case SYS8D: ifm = 1; miftyp = 3; break;
638 case SYS9: ifm = 1; miftyp = 4; break;
639 case SYS12: ifm = 1; miftyp = 5; break;
640 case SYS16A: ifm = 1; miftyp = 7; break;
641 case SYS16B: ifm = 1; miftyp = 8; break;
642 case SYS16C: ifm = 1; miftyp = 9; break;
643 case SYS18: ifm = 1; miftyp = 10; break;
644 case SYS24: ifm = 1; miftyp = 11; break;
645 default: goto bad;
646 }
647
648 /* SUBLCD only supports SYS interface */
649 if (lcdc_chan_is_sublcd(ch)) {
650 if (ifm == 0)
651 goto bad;
652 else
653 ifm = 0;
654 }
655
656 ch->ldmt1r_value = (ifm << 12) | miftyp;
657 return 0;
658 bad:
659 return -EINVAL;
660}
661
b51339ff
MD
662static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
663 int clock_source,
cfb4f5d1
MD
664 struct sh_mobile_lcdc_priv *priv)
665{
666 char *str;
667 int icksel;
668
669 switch (clock_source) {
670 case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
671 case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
672 case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
673 default:
674 return -EINVAL;
675 }
676
677 priv->lddckr = icksel << 16;
678
679 if (str) {
b51339ff
MD
680 priv->dot_clk = clk_get(&pdev->dev, str);
681 if (IS_ERR(priv->dot_clk)) {
682 dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
b51339ff 683 return PTR_ERR(priv->dot_clk);
cfb4f5d1 684 }
cfb4f5d1 685 }
0246c471
MD
686
687 /* Runtime PM support involves two step for this driver:
688 * 1) Enable Runtime PM
689 * 2) Force Runtime PM Resume since hardware is accessed from probe()
690 */
8bed9055 691 priv->dev = &pdev->dev;
0246c471
MD
692 pm_runtime_enable(priv->dev);
693 pm_runtime_resume(priv->dev);
cfb4f5d1
MD
694 return 0;
695}
696
697static int sh_mobile_lcdc_setcolreg(u_int regno,
698 u_int red, u_int green, u_int blue,
699 u_int transp, struct fb_info *info)
700{
701 u32 *palette = info->pseudo_palette;
702
703 if (regno >= PALETTE_NR)
704 return -EINVAL;
705
706 /* only FB_VISUAL_TRUECOLOR supported */
707
708 red >>= 16 - info->var.red.length;
709 green >>= 16 - info->var.green.length;
710 blue >>= 16 - info->var.blue.length;
711 transp >>= 16 - info->var.transp.length;
712
713 palette[regno] = (red << info->var.red.offset) |
714 (green << info->var.green.offset) |
715 (blue << info->var.blue.offset) |
716 (transp << info->var.transp.offset);
717
718 return 0;
719}
720
721static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
722 .id = "SH Mobile LCDC",
723 .type = FB_TYPE_PACKED_PIXELS,
724 .visual = FB_VISUAL_TRUECOLOR,
725 .accel = FB_ACCEL_NONE,
9dd38819
PE
726 .xpanstep = 0,
727 .ypanstep = 1,
728 .ywrapstep = 0,
cfb4f5d1
MD
729};
730
8564557a
MD
731static void sh_mobile_lcdc_fillrect(struct fb_info *info,
732 const struct fb_fillrect *rect)
733{
734 sys_fillrect(info, rect);
735 sh_mobile_lcdc_deferred_io_touch(info);
736}
737
738static void sh_mobile_lcdc_copyarea(struct fb_info *info,
739 const struct fb_copyarea *area)
740{
741 sys_copyarea(info, area);
742 sh_mobile_lcdc_deferred_io_touch(info);
743}
744
745static void sh_mobile_lcdc_imageblit(struct fb_info *info,
746 const struct fb_image *image)
747{
748 sys_imageblit(info, image);
749 sh_mobile_lcdc_deferred_io_touch(info);
750}
751
9dd38819
PE
752static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
753 struct fb_info *info)
754{
755 struct sh_mobile_lcdc_chan *ch = info->par;
92e1f9a7
PE
756 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
757 unsigned long ldrcntr;
758 unsigned long new_pan_offset;
759
760 new_pan_offset = (var->yoffset * info->fix.line_length) +
761 (var->xoffset * (info->var.bits_per_pixel / 8));
9dd38819 762
92e1f9a7 763 if (new_pan_offset == ch->pan_offset)
9dd38819
PE
764 return 0; /* No change, do nothing */
765
92e1f9a7 766 ldrcntr = lcdc_read(priv, _LDRCNTR);
9dd38819 767
92e1f9a7
PE
768 /* Set the source address for the next refresh */
769 lcdc_write_chan_mirror(ch, LDSA1R, ch->dma_handle + new_pan_offset);
770 if (lcdc_chan_is_sublcd(ch))
771 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
772 else
773 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
774
775 ch->pan_offset = new_pan_offset;
776
777 sh_mobile_lcdc_deferred_io_touch(info);
9dd38819
PE
778
779 return 0;
780}
781
40331b21
PE
782static int sh_mobile_wait_for_vsync(struct fb_info *info)
783{
784 struct sh_mobile_lcdc_chan *ch = info->par;
785 unsigned long ldintr;
786 int ret;
787
788 /* Enable VSync End interrupt */
789 ldintr = lcdc_read(ch->lcdc, _LDINTR);
790 ldintr |= LDINTR_VEE;
791 lcdc_write(ch->lcdc, _LDINTR, ldintr);
792
793 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
794 msecs_to_jiffies(100));
795 if (!ret)
796 return -ETIMEDOUT;
797
798 return 0;
799}
800
801static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
802 unsigned long arg)
803{
804 int retval;
805
806 switch (cmd) {
807 case FBIO_WAITFORVSYNC:
808 retval = sh_mobile_wait_for_vsync(info);
809 break;
810
811 default:
812 retval = -ENOIOCTLCMD;
813 break;
814 }
815 return retval;
816}
817
cfb4f5d1 818static struct fb_ops sh_mobile_lcdc_ops = {
9dd38819 819 .owner = THIS_MODULE,
cfb4f5d1 820 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
2540c111
MD
821 .fb_read = fb_sys_read,
822 .fb_write = fb_sys_write,
8564557a
MD
823 .fb_fillrect = sh_mobile_lcdc_fillrect,
824 .fb_copyarea = sh_mobile_lcdc_copyarea,
825 .fb_imageblit = sh_mobile_lcdc_imageblit,
9dd38819 826 .fb_pan_display = sh_mobile_fb_pan_display,
40331b21 827 .fb_ioctl = sh_mobile_ioctl,
cfb4f5d1
MD
828};
829
830static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
831{
832 switch (bpp) {
833 case 16: /* PKF[4:0] = 00011 - RGB 565 */
834 var->red.offset = 11;
835 var->red.length = 5;
836 var->green.offset = 5;
837 var->green.length = 6;
838 var->blue.offset = 0;
839 var->blue.length = 5;
840 var->transp.offset = 0;
841 var->transp.length = 0;
842 break;
843
844 case 32: /* PKF[4:0] = 00000 - RGB 888
845 * sh7722 pdf says 00RRGGBB but reality is GGBB00RR
846 * this may be because LDDDSR has word swap enabled..
847 */
848 var->red.offset = 0;
849 var->red.length = 8;
850 var->green.offset = 24;
851 var->green.length = 8;
852 var->blue.offset = 16;
853 var->blue.length = 8;
854 var->transp.offset = 0;
855 var->transp.length = 0;
856 break;
857 default:
858 return -EINVAL;
859 }
860 var->bits_per_pixel = bpp;
861 var->red.msb_right = 0;
862 var->green.msb_right = 0;
863 var->blue.msb_right = 0;
864 var->transp.msb_right = 0;
865 return 0;
866}
867
2feb075a
MD
868static int sh_mobile_lcdc_suspend(struct device *dev)
869{
870 struct platform_device *pdev = to_platform_device(dev);
871
872 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
873 return 0;
874}
875
876static int sh_mobile_lcdc_resume(struct device *dev)
877{
878 struct platform_device *pdev = to_platform_device(dev);
879
880 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
881}
882
0246c471
MD
883static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
884{
885 struct platform_device *pdev = to_platform_device(dev);
886 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
887 struct sh_mobile_lcdc_chan *ch;
888 int k, n;
889
890 /* save per-channel registers */
891 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
892 ch = &p->ch[k];
893 if (!ch->enabled)
894 continue;
895 for (n = 0; n < NR_CH_REGS; n++)
896 ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
897 }
898
899 /* save shared registers */
900 for (n = 0; n < NR_SHARED_REGS; n++)
901 p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
902
903 /* turn off LCDC hardware */
904 lcdc_write(p, _LDCNT1R, 0);
905 return 0;
906}
907
908static int sh_mobile_lcdc_runtime_resume(struct device *dev)
909{
910 struct platform_device *pdev = to_platform_device(dev);
911 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
912 struct sh_mobile_lcdc_chan *ch;
913 int k, n;
914
915 /* restore per-channel registers */
916 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
917 ch = &p->ch[k];
918 if (!ch->enabled)
919 continue;
920 for (n = 0; n < NR_CH_REGS; n++)
921 lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
922 }
923
924 /* restore shared registers */
925 for (n = 0; n < NR_SHARED_REGS; n++)
926 lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
927
928 return 0;
929}
930
47145210 931static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
2feb075a
MD
932 .suspend = sh_mobile_lcdc_suspend,
933 .resume = sh_mobile_lcdc_resume,
0246c471
MD
934 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
935 .runtime_resume = sh_mobile_lcdc_runtime_resume,
2feb075a
MD
936};
937
6de9edd5 938/* locking: called with info->lock held */
6011bdea
GL
939static int sh_mobile_lcdc_notify(struct notifier_block *nb,
940 unsigned long action, void *data)
941{
942 struct fb_event *event = data;
943 struct fb_info *info = event->info;
944 struct sh_mobile_lcdc_chan *ch = info->par;
945 struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
946 struct fb_var_screeninfo *var;
afe417c0 947 int ret;
6011bdea
GL
948
949 if (&ch->lcdc->notifier != nb)
baf16374 950 return NOTIFY_DONE;
6011bdea
GL
951
952 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
953 __func__, action, event->data);
954
955 switch(action) {
956 case FB_EVENT_SUSPEND:
6de9edd5 957 if (try_module_get(board_cfg->owner) && board_cfg->display_off) {
6011bdea 958 board_cfg->display_off(board_cfg->board_data);
6de9edd5
GL
959 module_put(board_cfg->owner);
960 }
6011bdea 961 pm_runtime_put(info->device);
afe417c0 962 sh_mobile_lcdc_stop(ch->lcdc);
6011bdea
GL
963 break;
964 case FB_EVENT_RESUME:
965 var = &info->var;
966
967 /* HDMI must be enabled before LCDC configuration */
6de9edd5 968 if (try_module_get(board_cfg->owner) && board_cfg->display_on) {
6011bdea 969 board_cfg->display_on(board_cfg->board_data, ch->info);
6de9edd5
GL
970 module_put(board_cfg->owner);
971 }
6011bdea 972
afe417c0
GL
973 ret = sh_mobile_lcdc_start(ch->lcdc);
974 if (!ret)
975 pm_runtime_get_sync(info->device);
6011bdea
GL
976 }
977
baf16374 978 return NOTIFY_OK;
6011bdea
GL
979}
980
cfb4f5d1
MD
981static int sh_mobile_lcdc_remove(struct platform_device *pdev);
982
c2e13037 983static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
cfb4f5d1
MD
984{
985 struct fb_info *info;
986 struct sh_mobile_lcdc_priv *priv;
01ac25b5 987 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
cfb4f5d1
MD
988 struct sh_mobile_lcdc_chan_cfg *cfg;
989 struct resource *res;
990 int error;
991 void *buf;
992 int i, j;
993
01ac25b5 994 if (!pdata) {
cfb4f5d1 995 dev_err(&pdev->dev, "no platform data defined\n");
8bed9055 996 return -EINVAL;
cfb4f5d1
MD
997 }
998
999 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
8564557a
MD
1000 i = platform_get_irq(pdev, 0);
1001 if (!res || i < 0) {
1002 dev_err(&pdev->dev, "cannot get platform resources\n");
8bed9055 1003 return -ENOENT;
cfb4f5d1
MD
1004 }
1005
1006 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1007 if (!priv) {
1008 dev_err(&pdev->dev, "cannot allocate device data\n");
8bed9055 1009 return -ENOMEM;
cfb4f5d1
MD
1010 }
1011
8bed9055
GL
1012 platform_set_drvdata(pdev, priv);
1013
8564557a 1014 error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
7ad33e74 1015 dev_name(&pdev->dev), priv);
8564557a
MD
1016 if (error) {
1017 dev_err(&pdev->dev, "unable to request irq\n");
1018 goto err1;
1019 }
1020
1021 priv->irq = i;
5ef6b505 1022 atomic_set(&priv->hw_usecnt, -1);
cfb4f5d1
MD
1023
1024 j = 0;
1025 for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
01ac25b5 1026 struct sh_mobile_lcdc_chan *ch = priv->ch + j;
cfb4f5d1 1027
01ac25b5
GL
1028 ch->lcdc = priv;
1029 memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
1030
1031 error = sh_mobile_lcdc_check_interface(ch);
cfb4f5d1
MD
1032 if (error) {
1033 dev_err(&pdev->dev, "unsupported interface type\n");
1034 goto err1;
1035 }
01ac25b5
GL
1036 init_waitqueue_head(&ch->frame_end_wait);
1037 init_completion(&ch->vsync_completion);
1038 ch->pan_offset = 0;
cfb4f5d1
MD
1039
1040 switch (pdata->ch[i].chan) {
1041 case LCDC_CHAN_MAINLCD:
01ac25b5
GL
1042 ch->enabled = 1 << 1;
1043 ch->reg_offs = lcdc_offs_mainlcd;
cfb4f5d1
MD
1044 j++;
1045 break;
1046 case LCDC_CHAN_SUBLCD:
01ac25b5
GL
1047 ch->enabled = 1 << 2;
1048 ch->reg_offs = lcdc_offs_sublcd;
cfb4f5d1
MD
1049 j++;
1050 break;
1051 }
1052 }
1053
1054 if (!j) {
1055 dev_err(&pdev->dev, "no channels defined\n");
1056 error = -EINVAL;
1057 goto err1;
1058 }
1059
dba6f385
GL
1060 priv->base = ioremap_nocache(res->start, resource_size(res));
1061 if (!priv->base)
1062 goto err1;
1063
b51339ff 1064 error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
cfb4f5d1
MD
1065 if (error) {
1066 dev_err(&pdev->dev, "unable to setup clocks\n");
1067 goto err1;
1068 }
1069
cfb4f5d1 1070 for (i = 0; i < j; i++) {
6011bdea 1071 struct fb_var_screeninfo *var;
71d3b0fc 1072 const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
01ac25b5 1073 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
71d3b0fc
GL
1074 unsigned long max_size = 0;
1075 int k;
cfb4f5d1 1076
01ac25b5
GL
1077 cfg = &ch->cfg;
1078
1079 ch->info = framebuffer_alloc(0, &pdev->dev);
1080 if (!ch->info) {
e33afddc
PM
1081 dev_err(&pdev->dev, "unable to allocate fb_info\n");
1082 error = -ENOMEM;
1083 break;
1084 }
1085
01ac25b5 1086 info = ch->info;
6011bdea 1087 var = &info->var;
cfb4f5d1 1088 info->fbops = &sh_mobile_lcdc_ops;
71d3b0fc 1089 fb_videomode_to_var(var, &cfg->lcd_cfg[0]);
9dd38819 1090 /* Default Y virtual resolution is 2x panel size */
6011bdea 1091 var->yres_virtual = var->yres * 2;
6011bdea
GL
1092
1093 error = sh_mobile_lcdc_set_bpp(var, cfg->bpp);
cfb4f5d1
MD
1094 if (error)
1095 break;
1096
71d3b0fc
GL
1097 for (k = 0, lcd_cfg = cfg->lcd_cfg;
1098 k < cfg->num_cfg;
1099 k++, lcd_cfg++) {
1100 unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
1101
1102 if (size > max_size) {
1103 max_cfg = lcd_cfg;
1104 max_size = size;
1105 }
1106 }
1107
1108 dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
1109 max_cfg->xres, max_cfg->yres);
1110
cfb4f5d1 1111 info->fix = sh_mobile_lcdc_fix;
71d3b0fc
GL
1112 info->fix.line_length = cfg->lcd_cfg[0].xres * (cfg->bpp / 8);
1113 info->fix.smem_len = max_size * (cfg->bpp / 8) * 2;
cfb4f5d1
MD
1114
1115 buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
01ac25b5 1116 &ch->dma_handle, GFP_KERNEL);
cfb4f5d1
MD
1117 if (!buf) {
1118 dev_err(&pdev->dev, "unable to allocate buffer\n");
1119 error = -ENOMEM;
1120 break;
1121 }
1122
01ac25b5 1123 info->pseudo_palette = &ch->pseudo_palette;
cfb4f5d1
MD
1124 info->flags = FBINFO_FLAG_DEFAULT;
1125
1126 error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1127 if (error < 0) {
1128 dev_err(&pdev->dev, "unable to allocate cmap\n");
1129 dma_free_coherent(&pdev->dev, info->fix.smem_len,
01ac25b5 1130 buf, ch->dma_handle);
cfb4f5d1
MD
1131 break;
1132 }
1133
01ac25b5 1134 info->fix.smem_start = ch->dma_handle;
cfb4f5d1
MD
1135 info->screen_base = buf;
1136 info->device = &pdev->dev;
01ac25b5 1137 info->par = ch;
1c120deb 1138 ch->display_var = *var;
cfb4f5d1
MD
1139 }
1140
1141 if (error)
1142 goto err1;
1143
1144 error = sh_mobile_lcdc_start(priv);
1145 if (error) {
1146 dev_err(&pdev->dev, "unable to start hardware\n");
1147 goto err1;
1148 }
1149
1150 for (i = 0; i < j; i++) {
1c6a307a
PM
1151 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
1152
e33afddc 1153 info = ch->info;
1c6a307a
PM
1154
1155 if (info->fbdefio) {
8bed9055 1156 ch->sglist = vmalloc(sizeof(struct scatterlist) *
1c6a307a 1157 info->fix.smem_len >> PAGE_SHIFT);
8bed9055 1158 if (!ch->sglist) {
1c6a307a
PM
1159 dev_err(&pdev->dev, "cannot allocate sglist\n");
1160 goto err1;
1161 }
1162 }
1163
afe417c0 1164 fb_videomode_to_modelist(ch->cfg.lcd_cfg, ch->cfg.num_cfg, &info->modelist);
1c6a307a 1165 error = register_framebuffer(info);
cfb4f5d1
MD
1166 if (error < 0)
1167 goto err1;
cfb4f5d1 1168
cfb4f5d1
MD
1169 dev_info(info->dev,
1170 "registered %s/%s as %dx%d %dbpp.\n",
1171 pdev->name,
1c6a307a 1172 (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
cfb4f5d1 1173 "mainlcd" : "sublcd",
44432407
GL
1174 (int) ch->cfg.lcd_cfg[0].xres,
1175 (int) ch->cfg.lcd_cfg[0].yres,
1c6a307a 1176 ch->cfg.bpp);
8564557a
MD
1177
1178 /* deferred io mode: disable clock to save power */
6011bdea 1179 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
8564557a 1180 sh_mobile_lcdc_clk_off(priv);
cfb4f5d1
MD
1181 }
1182
6011bdea
GL
1183 /* Failure ignored */
1184 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
1185 fb_register_client(&priv->notifier);
1186
cfb4f5d1 1187 return 0;
8bed9055 1188err1:
cfb4f5d1 1189 sh_mobile_lcdc_remove(pdev);
8bed9055 1190
cfb4f5d1
MD
1191 return error;
1192}
1193
1194static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1195{
1196 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1197 struct fb_info *info;
1198 int i;
1199
6011bdea
GL
1200 fb_unregister_client(&priv->notifier);
1201
cfb4f5d1 1202 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
8bed9055 1203 if (priv->ch[i].info && priv->ch[i].info->dev)
e33afddc 1204 unregister_framebuffer(priv->ch[i].info);
cfb4f5d1
MD
1205
1206 sh_mobile_lcdc_stop(priv);
1207
1208 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
e33afddc 1209 info = priv->ch[i].info;
cfb4f5d1 1210
e33afddc 1211 if (!info || !info->device)
cfb4f5d1
MD
1212 continue;
1213
1c6a307a
PM
1214 if (priv->ch[i].sglist)
1215 vfree(priv->ch[i].sglist);
1216
cfb4f5d1
MD
1217 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1218 info->screen_base, priv->ch[i].dma_handle);
1219 fb_dealloc_cmap(&info->cmap);
e33afddc 1220 framebuffer_release(info);
cfb4f5d1
MD
1221 }
1222
b51339ff
MD
1223 if (priv->dot_clk)
1224 clk_put(priv->dot_clk);
0246c471 1225
8bed9055
GL
1226 if (priv->dev)
1227 pm_runtime_disable(priv->dev);
cfb4f5d1
MD
1228
1229 if (priv->base)
1230 iounmap(priv->base);
1231
8564557a
MD
1232 if (priv->irq)
1233 free_irq(priv->irq, priv);
cfb4f5d1
MD
1234 kfree(priv);
1235 return 0;
1236}
1237
1238static struct platform_driver sh_mobile_lcdc_driver = {
1239 .driver = {
1240 .name = "sh_mobile_lcdc_fb",
1241 .owner = THIS_MODULE,
2feb075a 1242 .pm = &sh_mobile_lcdc_dev_pm_ops,
cfb4f5d1
MD
1243 },
1244 .probe = sh_mobile_lcdc_probe,
1245 .remove = sh_mobile_lcdc_remove,
1246};
1247
1248static int __init sh_mobile_lcdc_init(void)
1249{
1250 return platform_driver_register(&sh_mobile_lcdc_driver);
1251}
1252
1253static void __exit sh_mobile_lcdc_exit(void)
1254{
1255 platform_driver_unregister(&sh_mobile_lcdc_driver);
1256}
1257
1258module_init(sh_mobile_lcdc_init);
1259module_exit(sh_mobile_lcdc_exit);
1260
1261MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1262MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1263MODULE_LICENSE("GPL v2");