blob: 7f30cb33a2037a1cc69251be35a07de496a7307a [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>
15#include <linux/fb.h>
16#include <linux/clk.h>
Magnus Damm0246c472009-08-14 10:49:08 +000017#include <linux/pm_runtime.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070018#include <linux/platform_device.h>
19#include <linux/dma-mapping.h>
Magnus Damm85645572008-12-19 15:34:41 +090020#include <linux/interrupt.h>
Paul Mundt1c6a3072009-07-01 06:50:31 +000021#include <linux/vmalloc.h>
Paul Mundt225c9a82008-10-01 16:24:32 +090022#include <video/sh_mobile_lcdc.h>
Magnus Damm85645572008-12-19 15:34:41 +090023#include <asm/atomic.h>
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070024
25#define PALETTE_NR 16
26
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070027/* shared registers */
28#define _LDDCKR 0x410
29#define _LDDCKSTPR 0x414
30#define _LDINTR 0x468
31#define _LDSR 0x46c
32#define _LDCNT1R 0x470
33#define _LDCNT2R 0x474
Phil Edworthy9dd38812009-09-15 12:00:18 +000034#define _LDRCNTR 0x478
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070035#define _LDDDSR 0x47c
36#define _LDDWD0R 0x800
37#define _LDDRDR 0x840
38#define _LDDWAR 0x900
39#define _LDDRAR 0x904
40
Magnus Damm0246c472009-08-14 10:49:08 +000041/* shared registers and their order for context save/restore */
42static int lcdc_shared_regs[] = {
43 _LDDCKR,
44 _LDDCKSTPR,
45 _LDINTR,
46 _LDDDSR,
47 _LDCNT1R,
48 _LDCNT2R,
49};
50#define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
51
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070052/* per-channel registers */
53enum { LDDCKPAT1R, LDDCKPAT2R, LDMT1R, LDMT2R, LDMT3R, LDDFR, LDSM1R,
Magnus Damm0246c472009-08-14 10:49:08 +000054 LDSM2R, LDSA1R, LDMLSR, LDHCNR, LDHSYNR, LDVLNR, LDVSYNR, LDPMR,
55 NR_CH_REGS };
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070056
Magnus Damm0246c472009-08-14 10:49:08 +000057static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070058 [LDDCKPAT1R] = 0x400,
59 [LDDCKPAT2R] = 0x404,
60 [LDMT1R] = 0x418,
61 [LDMT2R] = 0x41c,
62 [LDMT3R] = 0x420,
63 [LDDFR] = 0x424,
64 [LDSM1R] = 0x428,
Magnus Damm85645572008-12-19 15:34:41 +090065 [LDSM2R] = 0x42c,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070066 [LDSA1R] = 0x430,
67 [LDMLSR] = 0x438,
68 [LDHCNR] = 0x448,
69 [LDHSYNR] = 0x44c,
70 [LDVLNR] = 0x450,
71 [LDVSYNR] = 0x454,
72 [LDPMR] = 0x460,
73};
74
Magnus Damm0246c472009-08-14 10:49:08 +000075static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070076 [LDDCKPAT1R] = 0x408,
77 [LDDCKPAT2R] = 0x40c,
78 [LDMT1R] = 0x600,
79 [LDMT2R] = 0x604,
80 [LDMT3R] = 0x608,
81 [LDDFR] = 0x60c,
82 [LDSM1R] = 0x610,
Magnus Damm85645572008-12-19 15:34:41 +090083 [LDSM2R] = 0x614,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -070084 [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
Magnus Damm85645572008-12-19 15:34:41 +090097#define LDINTR_FE 0x00000400
Phil Edworthy9dd38812009-09-15 12:00:18 +000098#define LDINTR_VSE 0x00000200
99#define LDINTR_VEE 0x00000100
Magnus Damm85645572008-12-19 15:34:41 +0900100#define LDINTR_FS 0x00000004
Phil Edworthy9dd38812009-09-15 12:00:18 +0000101#define LDINTR_VSS 0x00000002
102#define LDINTR_VES 0x00000001
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700103
Magnus Damm0246c472009-08-14 10:49:08 +0000104struct sh_mobile_lcdc_priv;
105struct sh_mobile_lcdc_chan {
106 struct sh_mobile_lcdc_priv *lcdc;
107 unsigned long *reg_offs;
108 unsigned long ldmt1r_value;
109 unsigned long enabled; /* ME and SE in LDCNT2R */
110 struct sh_mobile_lcdc_chan_cfg cfg;
111 u32 pseudo_palette[PALETTE_NR];
112 unsigned long saved_ch_regs[NR_CH_REGS];
113 struct fb_info *info;
114 dma_addr_t dma_handle;
115 struct fb_deferred_io defio;
116 struct scatterlist *sglist;
117 unsigned long frame_end;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000118 unsigned long pan_offset;
119 unsigned long new_pan_offset;
Magnus Damm0246c472009-08-14 10:49:08 +0000120 wait_queue_head_t frame_end_wait;
121};
122
123struct sh_mobile_lcdc_priv {
124 void __iomem *base;
125 int irq;
126 atomic_t hw_usecnt;
127 struct device *dev;
128 struct clk *dot_clk;
129 unsigned long lddckr;
130 struct sh_mobile_lcdc_chan ch[2];
131 unsigned long saved_shared_regs[NR_SHARED_REGS];
132 int started;
133};
134
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700135static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
136 int reg_nr, unsigned long data)
137{
138 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
139}
140
141static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
142 int reg_nr)
143{
144 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
145}
146
147static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
148 unsigned long reg_offs, unsigned long data)
149{
150 iowrite32(data, priv->base + reg_offs);
151}
152
153static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
154 unsigned long reg_offs)
155{
156 return ioread32(priv->base + reg_offs);
157}
158
159static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
160 unsigned long reg_offs,
161 unsigned long mask, unsigned long until)
162{
163 while ((lcdc_read(priv, reg_offs) & mask) != until)
164 cpu_relax();
165}
166
167static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
168{
169 return chan->cfg.chan == LCDC_CHAN_SUBLCD;
170}
171
172static void lcdc_sys_write_index(void *handle, unsigned long data)
173{
174 struct sh_mobile_lcdc_chan *ch = handle;
175
176 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
177 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
178 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
Magnus Damm909f10d2009-08-06 14:28:12 +0000179 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700180}
181
182static void lcdc_sys_write_data(void *handle, unsigned long data)
183{
184 struct sh_mobile_lcdc_chan *ch = handle;
185
186 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
187 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
188 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
Magnus Damm909f10d2009-08-06 14:28:12 +0000189 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700190}
191
192static unsigned long lcdc_sys_read_data(void *handle)
193{
194 struct sh_mobile_lcdc_chan *ch = handle;
195
196 lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
197 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
198 lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
199 udelay(1);
Magnus Damm909f10d2009-08-06 14:28:12 +0000200 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700201
Magnus Dammec56b662009-08-06 14:34:38 +0000202 return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700203}
204
205struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
206 lcdc_sys_write_index,
207 lcdc_sys_write_data,
208 lcdc_sys_read_data,
209};
210
Magnus Damm85645572008-12-19 15:34:41 +0900211static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
212{
Magnus Damm0246c472009-08-14 10:49:08 +0000213 if (atomic_inc_and_test(&priv->hw_usecnt)) {
214 pm_runtime_get_sync(priv->dev);
Magnus Damm85645572008-12-19 15:34:41 +0900215 if (priv->dot_clk)
216 clk_enable(priv->dot_clk);
217 }
218}
219
220static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
221{
Magnus Damm0246c472009-08-14 10:49:08 +0000222 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
Magnus Damm85645572008-12-19 15:34:41 +0900223 if (priv->dot_clk)
224 clk_disable(priv->dot_clk);
Magnus Damm0246c472009-08-14 10:49:08 +0000225 pm_runtime_put(priv->dev);
Magnus Damm85645572008-12-19 15:34:41 +0900226 }
227}
Magnus Damm85645572008-12-19 15:34:41 +0900228
Paul Mundt1c6a3072009-07-01 06:50:31 +0000229static int sh_mobile_lcdc_sginit(struct fb_info *info,
230 struct list_head *pagelist)
231{
232 struct sh_mobile_lcdc_chan *ch = info->par;
233 unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
234 struct page *page;
235 int nr_pages = 0;
236
237 sg_init_table(ch->sglist, nr_pages_max);
238
239 list_for_each_entry(page, pagelist, lru)
240 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
241
242 return nr_pages;
243}
244
Magnus Damm85645572008-12-19 15:34:41 +0900245static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
246 struct list_head *pagelist)
247{
248 struct sh_mobile_lcdc_chan *ch = info->par;
Paul Mundt1c6a3072009-07-01 06:50:31 +0000249 unsigned int nr_pages;
Magnus Damm85645572008-12-19 15:34:41 +0900250
251 /* enable clocks before accessing hardware */
252 sh_mobile_lcdc_clk_on(ch->lcdc);
253
Paul Mundt1c6a3072009-07-01 06:50:31 +0000254 nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
255 dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
256
Magnus Damm85645572008-12-19 15:34:41 +0900257 /* trigger panel update */
258 lcdc_write_chan(ch, LDSM2R, 1);
Paul Mundt1c6a3072009-07-01 06:50:31 +0000259
260 dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
Magnus Damm85645572008-12-19 15:34:41 +0900261}
262
263static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
264{
265 struct fb_deferred_io *fbdefio = info->fbdefio;
266
267 if (fbdefio)
268 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
269}
270
271static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
272{
273 struct sh_mobile_lcdc_priv *priv = data;
Magnus Damm2feb0752009-03-13 15:36:55 +0000274 struct sh_mobile_lcdc_chan *ch;
Magnus Damm85645572008-12-19 15:34:41 +0900275 unsigned long tmp;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000276 unsigned long ldintr;
Magnus Damm2feb0752009-03-13 15:36:55 +0000277 int is_sub;
278 int k;
Magnus Damm85645572008-12-19 15:34:41 +0900279
280 /* acknowledge interrupt */
Phil Edworthy9dd38812009-09-15 12:00:18 +0000281 ldintr = tmp = lcdc_read(priv, _LDINTR);
282 /*
283 * disable further VSYNC End IRQs, preserve all other enabled IRQs,
284 * write 0 to bits 0-6 to ack all triggered IRQs.
285 */
286 tmp &= 0xffffff00 & ~LDINTR_VEE;
Magnus Damm85645572008-12-19 15:34:41 +0900287 lcdc_write(priv, _LDINTR, tmp);
288
Magnus Damm2feb0752009-03-13 15:36:55 +0000289 /* figure out if this interrupt is for main or sub lcd */
290 is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
291
Phil Edworthy9dd38812009-09-15 12:00:18 +0000292 /* wake up channel and disable clocks */
Magnus Damm2feb0752009-03-13 15:36:55 +0000293 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
294 ch = &priv->ch[k];
295
296 if (!ch->enabled)
297 continue;
298
Phil Edworthy9dd38812009-09-15 12:00:18 +0000299 /* Frame Start */
300 if (ldintr & LDINTR_FS) {
301 if (is_sub == lcdc_chan_is_sublcd(ch)) {
302 ch->frame_end = 1;
303 wake_up(&ch->frame_end_wait);
Magnus Damm2feb0752009-03-13 15:36:55 +0000304
Phil Edworthy9dd38812009-09-15 12:00:18 +0000305 sh_mobile_lcdc_clk_off(priv);
306 }
307 }
308
309 /* VSYNC End */
310 if (ldintr & LDINTR_VES) {
311 /* Set the source address for the next refresh */
312 lcdc_write_chan(ch, LDSA1R, ch->dma_handle +
313 ch->new_pan_offset);
314 lcdc_write(ch->lcdc, _LDRCNTR, 0);
315 ch->pan_offset = ch->new_pan_offset;
Magnus Damm2feb0752009-03-13 15:36:55 +0000316 }
317 }
318
Magnus Damm85645572008-12-19 15:34:41 +0900319 return IRQ_HANDLED;
320}
321
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700322static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
323 int start)
324{
325 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
326 int k;
327
328 /* start or stop the lcdc */
329 if (start)
330 lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
331 else
332 lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
333
334 /* wait until power is applied/stopped on all channels */
335 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
336 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
337 while (1) {
338 tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
339 if (start && tmp == 3)
340 break;
341 if (!start && tmp == 0)
342 break;
343 cpu_relax();
344 }
345
346 if (!start)
347 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
348}
349
350static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
351{
352 struct sh_mobile_lcdc_chan *ch;
353 struct fb_videomode *lcd_cfg;
354 struct sh_mobile_lcdc_board_cfg *board_cfg;
355 unsigned long tmp;
356 int k, m;
357 int ret = 0;
358
Magnus Damm85645572008-12-19 15:34:41 +0900359 /* enable clocks before accessing the hardware */
360 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
361 if (priv->ch[k].enabled)
362 sh_mobile_lcdc_clk_on(priv);
363
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700364 /* reset */
365 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
366 lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
367
368 /* enable LCDC channels */
369 tmp = lcdc_read(priv, _LDCNT2R);
370 tmp |= priv->ch[0].enabled;
371 tmp |= priv->ch[1].enabled;
372 lcdc_write(priv, _LDCNT2R, tmp);
373
374 /* read data from external memory, avoid using the BEU for now */
375 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
376
377 /* stop the lcdc first */
378 sh_mobile_lcdc_start_stop(priv, 0);
379
380 /* configure clocks */
381 tmp = priv->lddckr;
382 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
383 ch = &priv->ch[k];
384
385 if (!priv->ch[k].enabled)
386 continue;
387
388 m = ch->cfg.clock_divider;
389 if (!m)
390 continue;
391
392 if (m == 1)
393 m = 1 << 6;
394 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
395
396 lcdc_write_chan(ch, LDDCKPAT1R, 0x00000000);
397 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
398 }
399
400 lcdc_write(priv, _LDDCKR, tmp);
401
402 /* start dotclock again */
403 lcdc_write(priv, _LDDCKSTPR, 0);
404 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
405
Magnus Damm85645572008-12-19 15:34:41 +0900406 /* interrupts are disabled to begin with */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700407 lcdc_write(priv, _LDINTR, 0);
408
409 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
410 ch = &priv->ch[k];
411 lcd_cfg = &ch->cfg.lcd_cfg;
412
413 if (!ch->enabled)
414 continue;
415
416 tmp = ch->ldmt1r_value;
417 tmp |= (lcd_cfg->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
418 tmp |= (lcd_cfg->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
Magnus Dammf400f512008-10-09 18:48:16 +0900419 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
420 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
421 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
422 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
423 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700424 lcdc_write_chan(ch, LDMT1R, tmp);
425
426 /* setup SYS bus */
427 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
428 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
429
430 /* horizontal configuration */
431 tmp = lcd_cfg->xres + lcd_cfg->hsync_len;
432 tmp += lcd_cfg->left_margin;
433 tmp += lcd_cfg->right_margin;
434 tmp /= 8; /* HTCN */
435 tmp |= (lcd_cfg->xres / 8) << 16; /* HDCN */
436 lcdc_write_chan(ch, LDHCNR, tmp);
437
438 tmp = lcd_cfg->xres;
439 tmp += lcd_cfg->right_margin;
440 tmp /= 8; /* HSYNP */
441 tmp |= (lcd_cfg->hsync_len / 8) << 16; /* HSYNW */
442 lcdc_write_chan(ch, LDHSYNR, tmp);
443
444 /* power supply */
445 lcdc_write_chan(ch, LDPMR, 0);
446
447 /* vertical configuration */
448 tmp = lcd_cfg->yres + lcd_cfg->vsync_len;
449 tmp += lcd_cfg->upper_margin;
450 tmp += lcd_cfg->lower_margin; /* VTLN */
451 tmp |= lcd_cfg->yres << 16; /* VDLN */
452 lcdc_write_chan(ch, LDVLNR, tmp);
453
454 tmp = lcd_cfg->yres;
455 tmp += lcd_cfg->lower_margin; /* VSYNP */
456 tmp |= lcd_cfg->vsync_len << 16; /* VSYNW */
457 lcdc_write_chan(ch, LDVSYNR, tmp);
458
459 board_cfg = &ch->cfg.board_cfg;
460 if (board_cfg->setup_sys)
461 ret = board_cfg->setup_sys(board_cfg->board_data, ch,
462 &sh_mobile_lcdc_sys_bus_ops);
463 if (ret)
464 return ret;
465 }
466
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700467 /* word and long word swap */
468 lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 6);
469
470 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
471 ch = &priv->ch[k];
472
473 if (!priv->ch[k].enabled)
474 continue;
475
476 /* set bpp format in PKF[4:0] */
477 tmp = lcdc_read_chan(ch, LDDFR);
478 tmp &= ~(0x0001001f);
Paul Mundte33afdd2009-07-07 11:24:32 +0900479 tmp |= (ch->info->var.bits_per_pixel == 16) ? 3 : 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700480 lcdc_write_chan(ch, LDDFR, tmp);
481
482 /* point out our frame buffer */
Paul Mundte33afdd2009-07-07 11:24:32 +0900483 lcdc_write_chan(ch, LDSA1R, ch->info->fix.smem_start);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700484
485 /* set line size */
Paul Mundte33afdd2009-07-07 11:24:32 +0900486 lcdc_write_chan(ch, LDMLSR, ch->info->fix.line_length);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700487
Magnus Damm85645572008-12-19 15:34:41 +0900488 /* setup deferred io if SYS bus */
489 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
490 if (ch->ldmt1r_value & (1 << 12) && tmp) {
491 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
492 ch->defio.delay = msecs_to_jiffies(tmp);
Paul Mundte33afdd2009-07-07 11:24:32 +0900493 ch->info->fbdefio = &ch->defio;
494 fb_deferred_io_init(ch->info);
Magnus Damm85645572008-12-19 15:34:41 +0900495
496 /* one-shot mode */
497 lcdc_write_chan(ch, LDSM1R, 1);
498
499 /* enable "Frame End Interrupt Enable" bit */
500 lcdc_write(priv, _LDINTR, LDINTR_FE);
501
502 } else {
503 /* continuous read mode */
504 lcdc_write_chan(ch, LDSM1R, 0);
505 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700506 }
507
508 /* display output */
509 lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
510
511 /* start the lcdc */
512 sh_mobile_lcdc_start_stop(priv, 1);
Magnus Damm8e9bb192009-05-20 14:34:43 +0000513 priv->started = 1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700514
515 /* tell the board code to enable the panel */
516 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
517 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +0000518 if (!ch->enabled)
519 continue;
520
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700521 board_cfg = &ch->cfg.board_cfg;
522 if (board_cfg->display_on)
523 board_cfg->display_on(board_cfg->board_data);
524 }
525
526 return 0;
527}
528
529static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
530{
531 struct sh_mobile_lcdc_chan *ch;
532 struct sh_mobile_lcdc_board_cfg *board_cfg;
533 int k;
534
Magnus Damm2feb0752009-03-13 15:36:55 +0000535 /* clean up deferred io and ask board code to disable panel */
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700536 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
537 ch = &priv->ch[k];
Magnus Damm21bc1f02009-08-15 02:53:16 +0000538 if (!ch->enabled)
539 continue;
Magnus Damm2feb0752009-03-13 15:36:55 +0000540
541 /* deferred io mode:
542 * flush frame, and wait for frame end interrupt
543 * clean up deferred io and enable clock
544 */
Paul Mundte33afdd2009-07-07 11:24:32 +0900545 if (ch->info->fbdefio) {
Magnus Damm2feb0752009-03-13 15:36:55 +0000546 ch->frame_end = 0;
Paul Mundte33afdd2009-07-07 11:24:32 +0900547 schedule_delayed_work(&ch->info->deferred_work, 0);
Magnus Damm2feb0752009-03-13 15:36:55 +0000548 wait_event(ch->frame_end_wait, ch->frame_end);
Paul Mundte33afdd2009-07-07 11:24:32 +0900549 fb_deferred_io_cleanup(ch->info);
550 ch->info->fbdefio = NULL;
Magnus Damm2feb0752009-03-13 15:36:55 +0000551 sh_mobile_lcdc_clk_on(priv);
552 }
553
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700554 board_cfg = &ch->cfg.board_cfg;
555 if (board_cfg->display_off)
556 board_cfg->display_off(board_cfg->board_data);
557 }
558
559 /* stop the lcdc */
Magnus Damm8e9bb192009-05-20 14:34:43 +0000560 if (priv->started) {
561 sh_mobile_lcdc_start_stop(priv, 0);
562 priv->started = 0;
563 }
Magnus Dammb51339f2008-10-31 20:23:26 +0900564
Magnus Damm85645572008-12-19 15:34:41 +0900565 /* stop clocks */
566 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
567 if (priv->ch[k].enabled)
568 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700569}
570
571static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
572{
573 int ifm, miftyp;
574
575 switch (ch->cfg.interface_type) {
576 case RGB8: ifm = 0; miftyp = 0; break;
577 case RGB9: ifm = 0; miftyp = 4; break;
578 case RGB12A: ifm = 0; miftyp = 5; break;
579 case RGB12B: ifm = 0; miftyp = 6; break;
580 case RGB16: ifm = 0; miftyp = 7; break;
581 case RGB18: ifm = 0; miftyp = 10; break;
582 case RGB24: ifm = 0; miftyp = 11; break;
583 case SYS8A: ifm = 1; miftyp = 0; break;
584 case SYS8B: ifm = 1; miftyp = 1; break;
585 case SYS8C: ifm = 1; miftyp = 2; break;
586 case SYS8D: ifm = 1; miftyp = 3; break;
587 case SYS9: ifm = 1; miftyp = 4; break;
588 case SYS12: ifm = 1; miftyp = 5; break;
589 case SYS16A: ifm = 1; miftyp = 7; break;
590 case SYS16B: ifm = 1; miftyp = 8; break;
591 case SYS16C: ifm = 1; miftyp = 9; break;
592 case SYS18: ifm = 1; miftyp = 10; break;
593 case SYS24: ifm = 1; miftyp = 11; break;
594 default: goto bad;
595 }
596
597 /* SUBLCD only supports SYS interface */
598 if (lcdc_chan_is_sublcd(ch)) {
599 if (ifm == 0)
600 goto bad;
601 else
602 ifm = 0;
603 }
604
605 ch->ldmt1r_value = (ifm << 12) | miftyp;
606 return 0;
607 bad:
608 return -EINVAL;
609}
610
Magnus Dammb51339f2008-10-31 20:23:26 +0900611static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
612 int clock_source,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700613 struct sh_mobile_lcdc_priv *priv)
614{
615 char *str;
616 int icksel;
617
618 switch (clock_source) {
619 case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
620 case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
621 case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
622 default:
623 return -EINVAL;
624 }
625
626 priv->lddckr = icksel << 16;
627
628 if (str) {
Magnus Dammb51339f2008-10-31 20:23:26 +0900629 priv->dot_clk = clk_get(&pdev->dev, str);
630 if (IS_ERR(priv->dot_clk)) {
631 dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
Magnus Dammb51339f2008-10-31 20:23:26 +0900632 return PTR_ERR(priv->dot_clk);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700633 }
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700634 }
Magnus Damm0246c472009-08-14 10:49:08 +0000635 atomic_set(&priv->hw_usecnt, -1);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700636
Magnus Damm0246c472009-08-14 10:49:08 +0000637 /* Runtime PM support involves two step for this driver:
638 * 1) Enable Runtime PM
639 * 2) Force Runtime PM Resume since hardware is accessed from probe()
640 */
641 pm_runtime_enable(priv->dev);
642 pm_runtime_resume(priv->dev);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700643 return 0;
644}
645
646static int sh_mobile_lcdc_setcolreg(u_int regno,
647 u_int red, u_int green, u_int blue,
648 u_int transp, struct fb_info *info)
649{
650 u32 *palette = info->pseudo_palette;
651
652 if (regno >= PALETTE_NR)
653 return -EINVAL;
654
655 /* only FB_VISUAL_TRUECOLOR supported */
656
657 red >>= 16 - info->var.red.length;
658 green >>= 16 - info->var.green.length;
659 blue >>= 16 - info->var.blue.length;
660 transp >>= 16 - info->var.transp.length;
661
662 palette[regno] = (red << info->var.red.offset) |
663 (green << info->var.green.offset) |
664 (blue << info->var.blue.offset) |
665 (transp << info->var.transp.offset);
666
667 return 0;
668}
669
670static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
671 .id = "SH Mobile LCDC",
672 .type = FB_TYPE_PACKED_PIXELS,
673 .visual = FB_VISUAL_TRUECOLOR,
674 .accel = FB_ACCEL_NONE,
Phil Edworthy9dd38812009-09-15 12:00:18 +0000675 .xpanstep = 0,
676 .ypanstep = 1,
677 .ywrapstep = 0,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700678};
679
Magnus Damm85645572008-12-19 15:34:41 +0900680static void sh_mobile_lcdc_fillrect(struct fb_info *info,
681 const struct fb_fillrect *rect)
682{
683 sys_fillrect(info, rect);
684 sh_mobile_lcdc_deferred_io_touch(info);
685}
686
687static void sh_mobile_lcdc_copyarea(struct fb_info *info,
688 const struct fb_copyarea *area)
689{
690 sys_copyarea(info, area);
691 sh_mobile_lcdc_deferred_io_touch(info);
692}
693
694static void sh_mobile_lcdc_imageblit(struct fb_info *info,
695 const struct fb_image *image)
696{
697 sys_imageblit(info, image);
698 sh_mobile_lcdc_deferred_io_touch(info);
699}
700
Phil Edworthy9dd38812009-09-15 12:00:18 +0000701static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
702 struct fb_info *info)
703{
704 struct sh_mobile_lcdc_chan *ch = info->par;
705
706 if (info->var.xoffset == var->xoffset &&
707 info->var.yoffset == var->yoffset)
708 return 0; /* No change, do nothing */
709
710 ch->new_pan_offset = (var->yoffset * info->fix.line_length) +
711 (var->xoffset * (info->var.bits_per_pixel / 8));
712
713 if (ch->new_pan_offset != ch->pan_offset) {
714 unsigned long ldintr;
715 ldintr = lcdc_read(ch->lcdc, _LDINTR);
716 ldintr |= LDINTR_VEE;
717 lcdc_write(ch->lcdc, _LDINTR, ldintr);
718 sh_mobile_lcdc_deferred_io_touch(info);
719 }
720
721 return 0;
722}
723
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700724static struct fb_ops sh_mobile_lcdc_ops = {
Phil Edworthy9dd38812009-09-15 12:00:18 +0000725 .owner = THIS_MODULE,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700726 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
Magnus Damm2540c112008-12-17 17:29:49 +0900727 .fb_read = fb_sys_read,
728 .fb_write = fb_sys_write,
Magnus Damm85645572008-12-19 15:34:41 +0900729 .fb_fillrect = sh_mobile_lcdc_fillrect,
730 .fb_copyarea = sh_mobile_lcdc_copyarea,
731 .fb_imageblit = sh_mobile_lcdc_imageblit,
Phil Edworthy9dd38812009-09-15 12:00:18 +0000732 .fb_pan_display = sh_mobile_fb_pan_display,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700733};
734
735static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
736{
737 switch (bpp) {
738 case 16: /* PKF[4:0] = 00011 - RGB 565 */
739 var->red.offset = 11;
740 var->red.length = 5;
741 var->green.offset = 5;
742 var->green.length = 6;
743 var->blue.offset = 0;
744 var->blue.length = 5;
745 var->transp.offset = 0;
746 var->transp.length = 0;
747 break;
748
749 case 32: /* PKF[4:0] = 00000 - RGB 888
750 * sh7722 pdf says 00RRGGBB but reality is GGBB00RR
751 * this may be because LDDDSR has word swap enabled..
752 */
753 var->red.offset = 0;
754 var->red.length = 8;
755 var->green.offset = 24;
756 var->green.length = 8;
757 var->blue.offset = 16;
758 var->blue.length = 8;
759 var->transp.offset = 0;
760 var->transp.length = 0;
761 break;
762 default:
763 return -EINVAL;
764 }
765 var->bits_per_pixel = bpp;
766 var->red.msb_right = 0;
767 var->green.msb_right = 0;
768 var->blue.msb_right = 0;
769 var->transp.msb_right = 0;
770 return 0;
771}
772
Magnus Damm2feb0752009-03-13 15:36:55 +0000773static int sh_mobile_lcdc_suspend(struct device *dev)
774{
775 struct platform_device *pdev = to_platform_device(dev);
776
777 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
778 return 0;
779}
780
781static int sh_mobile_lcdc_resume(struct device *dev)
782{
783 struct platform_device *pdev = to_platform_device(dev);
784
785 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
786}
787
Magnus Damm0246c472009-08-14 10:49:08 +0000788static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
789{
790 struct platform_device *pdev = to_platform_device(dev);
791 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
792 struct sh_mobile_lcdc_chan *ch;
793 int k, n;
794
795 /* save per-channel registers */
796 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
797 ch = &p->ch[k];
798 if (!ch->enabled)
799 continue;
800 for (n = 0; n < NR_CH_REGS; n++)
801 ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
802 }
803
804 /* save shared registers */
805 for (n = 0; n < NR_SHARED_REGS; n++)
806 p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
807
808 /* turn off LCDC hardware */
809 lcdc_write(p, _LDCNT1R, 0);
810 return 0;
811}
812
813static int sh_mobile_lcdc_runtime_resume(struct device *dev)
814{
815 struct platform_device *pdev = to_platform_device(dev);
816 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
817 struct sh_mobile_lcdc_chan *ch;
818 int k, n;
819
820 /* restore per-channel registers */
821 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
822 ch = &p->ch[k];
823 if (!ch->enabled)
824 continue;
825 for (n = 0; n < NR_CH_REGS; n++)
826 lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
827 }
828
829 /* restore shared registers */
830 for (n = 0; n < NR_SHARED_REGS; n++)
831 lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
832
833 return 0;
834}
835
Magnus Damm2feb0752009-03-13 15:36:55 +0000836static struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
837 .suspend = sh_mobile_lcdc_suspend,
838 .resume = sh_mobile_lcdc_resume,
Magnus Damm0246c472009-08-14 10:49:08 +0000839 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
840 .runtime_resume = sh_mobile_lcdc_runtime_resume,
Magnus Damm2feb0752009-03-13 15:36:55 +0000841};
842
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700843static int sh_mobile_lcdc_remove(struct platform_device *pdev);
844
845static int __init sh_mobile_lcdc_probe(struct platform_device *pdev)
846{
847 struct fb_info *info;
848 struct sh_mobile_lcdc_priv *priv;
849 struct sh_mobile_lcdc_info *pdata;
850 struct sh_mobile_lcdc_chan_cfg *cfg;
851 struct resource *res;
852 int error;
853 void *buf;
854 int i, j;
855
856 if (!pdev->dev.platform_data) {
857 dev_err(&pdev->dev, "no platform data defined\n");
858 error = -EINVAL;
859 goto err0;
860 }
861
862 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Magnus Damm85645572008-12-19 15:34:41 +0900863 i = platform_get_irq(pdev, 0);
864 if (!res || i < 0) {
865 dev_err(&pdev->dev, "cannot get platform resources\n");
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700866 error = -ENOENT;
867 goto err0;
868 }
869
870 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
871 if (!priv) {
872 dev_err(&pdev->dev, "cannot allocate device data\n");
873 error = -ENOMEM;
874 goto err0;
875 }
876
Magnus Damm85645572008-12-19 15:34:41 +0900877 error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
Kay Sievers7ad33e72009-03-24 16:38:21 -0700878 dev_name(&pdev->dev), priv);
Magnus Damm85645572008-12-19 15:34:41 +0900879 if (error) {
880 dev_err(&pdev->dev, "unable to request irq\n");
881 goto err1;
882 }
883
884 priv->irq = i;
Magnus Damm0246c472009-08-14 10:49:08 +0000885 priv->dev = &pdev->dev;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700886 platform_set_drvdata(pdev, priv);
887 pdata = pdev->dev.platform_data;
888
889 j = 0;
890 for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
891 priv->ch[j].lcdc = priv;
892 memcpy(&priv->ch[j].cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
893
894 error = sh_mobile_lcdc_check_interface(&priv->ch[i]);
895 if (error) {
896 dev_err(&pdev->dev, "unsupported interface type\n");
897 goto err1;
898 }
Magnus Damm2feb0752009-03-13 15:36:55 +0000899 init_waitqueue_head(&priv->ch[i].frame_end_wait);
Phil Edworthy9dd38812009-09-15 12:00:18 +0000900 priv->ch[j].pan_offset = 0;
901 priv->ch[j].new_pan_offset = 0;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700902
903 switch (pdata->ch[i].chan) {
904 case LCDC_CHAN_MAINLCD:
905 priv->ch[j].enabled = 1 << 1;
906 priv->ch[j].reg_offs = lcdc_offs_mainlcd;
907 j++;
908 break;
909 case LCDC_CHAN_SUBLCD:
910 priv->ch[j].enabled = 1 << 2;
911 priv->ch[j].reg_offs = lcdc_offs_sublcd;
912 j++;
913 break;
914 }
915 }
916
917 if (!j) {
918 dev_err(&pdev->dev, "no channels defined\n");
919 error = -EINVAL;
920 goto err1;
921 }
922
Magnus Dammb51339f2008-10-31 20:23:26 +0900923 error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700924 if (error) {
925 dev_err(&pdev->dev, "unable to setup clocks\n");
926 goto err1;
927 }
928
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700929 priv->base = ioremap_nocache(res->start, (res->end - res->start) + 1);
930
931 for (i = 0; i < j; i++) {
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700932 cfg = &priv->ch[i].cfg;
933
Paul Mundte33afdd2009-07-07 11:24:32 +0900934 priv->ch[i].info = framebuffer_alloc(0, &pdev->dev);
935 if (!priv->ch[i].info) {
936 dev_err(&pdev->dev, "unable to allocate fb_info\n");
937 error = -ENOMEM;
938 break;
939 }
940
941 info = priv->ch[i].info;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700942 info->fbops = &sh_mobile_lcdc_ops;
943 info->var.xres = info->var.xres_virtual = cfg->lcd_cfg.xres;
Phil Edworthy9dd38812009-09-15 12:00:18 +0000944 info->var.yres = cfg->lcd_cfg.yres;
945 /* Default Y virtual resolution is 2x panel size */
946 info->var.yres_virtual = info->var.yres * 2;
Magnus Dammce9c0082008-08-11 15:26:00 +0900947 info->var.width = cfg->lcd_size_cfg.width;
948 info->var.height = cfg->lcd_size_cfg.height;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700949 info->var.activate = FB_ACTIVATE_NOW;
950 error = sh_mobile_lcdc_set_bpp(&info->var, cfg->bpp);
951 if (error)
952 break;
953
954 info->fix = sh_mobile_lcdc_fix;
955 info->fix.line_length = cfg->lcd_cfg.xres * (cfg->bpp / 8);
Phil Edworthy9dd38812009-09-15 12:00:18 +0000956 info->fix.smem_len = info->fix.line_length *
957 info->var.yres_virtual;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700958
959 buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
960 &priv->ch[i].dma_handle, GFP_KERNEL);
961 if (!buf) {
962 dev_err(&pdev->dev, "unable to allocate buffer\n");
963 error = -ENOMEM;
964 break;
965 }
966
967 info->pseudo_palette = &priv->ch[i].pseudo_palette;
968 info->flags = FBINFO_FLAG_DEFAULT;
969
970 error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
971 if (error < 0) {
972 dev_err(&pdev->dev, "unable to allocate cmap\n");
973 dma_free_coherent(&pdev->dev, info->fix.smem_len,
974 buf, priv->ch[i].dma_handle);
975 break;
976 }
977
978 memset(buf, 0, info->fix.smem_len);
979 info->fix.smem_start = priv->ch[i].dma_handle;
980 info->screen_base = buf;
981 info->device = &pdev->dev;
Magnus Damm85645572008-12-19 15:34:41 +0900982 info->par = &priv->ch[i];
Magnus Dammcfb4f5d2008-07-23 21:31:24 -0700983 }
984
985 if (error)
986 goto err1;
987
988 error = sh_mobile_lcdc_start(priv);
989 if (error) {
990 dev_err(&pdev->dev, "unable to start hardware\n");
991 goto err1;
992 }
993
994 for (i = 0; i < j; i++) {
Paul Mundt1c6a3072009-07-01 06:50:31 +0000995 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
996
Paul Mundte33afdd2009-07-07 11:24:32 +0900997 info = ch->info;
Paul Mundt1c6a3072009-07-01 06:50:31 +0000998
999 if (info->fbdefio) {
1000 priv->ch->sglist = vmalloc(sizeof(struct scatterlist) *
1001 info->fix.smem_len >> PAGE_SHIFT);
1002 if (!priv->ch->sglist) {
1003 dev_err(&pdev->dev, "cannot allocate sglist\n");
1004 goto err1;
1005 }
1006 }
1007
1008 error = register_framebuffer(info);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001009 if (error < 0)
1010 goto err1;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001011
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001012 dev_info(info->dev,
1013 "registered %s/%s as %dx%d %dbpp.\n",
1014 pdev->name,
Paul Mundt1c6a3072009-07-01 06:50:31 +00001015 (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001016 "mainlcd" : "sublcd",
Paul Mundt1c6a3072009-07-01 06:50:31 +00001017 (int) ch->cfg.lcd_cfg.xres,
1018 (int) ch->cfg.lcd_cfg.yres,
1019 ch->cfg.bpp);
Magnus Damm85645572008-12-19 15:34:41 +09001020
1021 /* deferred io mode: disable clock to save power */
1022 if (info->fbdefio)
1023 sh_mobile_lcdc_clk_off(priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001024 }
1025
1026 return 0;
1027 err1:
1028 sh_mobile_lcdc_remove(pdev);
1029 err0:
1030 return error;
1031}
1032
1033static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1034{
1035 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1036 struct fb_info *info;
1037 int i;
1038
1039 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
Paul Mundte33afdd2009-07-07 11:24:32 +09001040 if (priv->ch[i].info->dev)
1041 unregister_framebuffer(priv->ch[i].info);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001042
1043 sh_mobile_lcdc_stop(priv);
1044
1045 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
Paul Mundte33afdd2009-07-07 11:24:32 +09001046 info = priv->ch[i].info;
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001047
Paul Mundte33afdd2009-07-07 11:24:32 +09001048 if (!info || !info->device)
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001049 continue;
1050
Paul Mundt1c6a3072009-07-01 06:50:31 +00001051 if (priv->ch[i].sglist)
1052 vfree(priv->ch[i].sglist);
1053
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001054 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1055 info->screen_base, priv->ch[i].dma_handle);
1056 fb_dealloc_cmap(&info->cmap);
Paul Mundte33afdd2009-07-07 11:24:32 +09001057 framebuffer_release(info);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001058 }
1059
Magnus Dammb51339f2008-10-31 20:23:26 +09001060 if (priv->dot_clk)
1061 clk_put(priv->dot_clk);
Magnus Damm0246c472009-08-14 10:49:08 +00001062
1063 pm_runtime_disable(priv->dev);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001064
1065 if (priv->base)
1066 iounmap(priv->base);
1067
Magnus Damm85645572008-12-19 15:34:41 +09001068 if (priv->irq)
1069 free_irq(priv->irq, priv);
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001070 kfree(priv);
1071 return 0;
1072}
1073
1074static struct platform_driver sh_mobile_lcdc_driver = {
1075 .driver = {
1076 .name = "sh_mobile_lcdc_fb",
1077 .owner = THIS_MODULE,
Magnus Damm2feb0752009-03-13 15:36:55 +00001078 .pm = &sh_mobile_lcdc_dev_pm_ops,
Magnus Dammcfb4f5d2008-07-23 21:31:24 -07001079 },
1080 .probe = sh_mobile_lcdc_probe,
1081 .remove = sh_mobile_lcdc_remove,
1082};
1083
1084static int __init sh_mobile_lcdc_init(void)
1085{
1086 return platform_driver_register(&sh_mobile_lcdc_driver);
1087}
1088
1089static void __exit sh_mobile_lcdc_exit(void)
1090{
1091 platform_driver_unregister(&sh_mobile_lcdc_driver);
1092}
1093
1094module_init(sh_mobile_lcdc_init);
1095module_exit(sh_mobile_lcdc_exit);
1096
1097MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1098MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1099MODULE_LICENSE("GPL v2");