Merge branch 'soc' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/renesas...
[firefly-linux-kernel-4.4.55.git] / drivers / video / omap2 / dss / rfbi.c
1 /*
2  * linux/drivers/video/omap2/dss/rfbi.c
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6  *
7  * Some code and ideas taken from drivers/video/omap/ driver
8  * by Imre Deak.
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License version 2 as published by
12  * the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #define DSS_SUBSYS_NAME "RFBI"
24
25 #include <linux/kernel.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/export.h>
28 #include <linux/vmalloc.h>
29 #include <linux/clk.h>
30 #include <linux/io.h>
31 #include <linux/delay.h>
32 #include <linux/kfifo.h>
33 #include <linux/ktime.h>
34 #include <linux/hrtimer.h>
35 #include <linux/seq_file.h>
36 #include <linux/semaphore.h>
37 #include <linux/platform_device.h>
38 #include <linux/pm_runtime.h>
39
40 #include <video/omapdss.h>
41 #include "dss.h"
42
43 struct rfbi_reg { u16 idx; };
44
45 #define RFBI_REG(idx)           ((const struct rfbi_reg) { idx })
46
47 #define RFBI_REVISION           RFBI_REG(0x0000)
48 #define RFBI_SYSCONFIG          RFBI_REG(0x0010)
49 #define RFBI_SYSSTATUS          RFBI_REG(0x0014)
50 #define RFBI_CONTROL            RFBI_REG(0x0040)
51 #define RFBI_PIXEL_CNT          RFBI_REG(0x0044)
52 #define RFBI_LINE_NUMBER        RFBI_REG(0x0048)
53 #define RFBI_CMD                RFBI_REG(0x004c)
54 #define RFBI_PARAM              RFBI_REG(0x0050)
55 #define RFBI_DATA               RFBI_REG(0x0054)
56 #define RFBI_READ               RFBI_REG(0x0058)
57 #define RFBI_STATUS             RFBI_REG(0x005c)
58
59 #define RFBI_CONFIG(n)          RFBI_REG(0x0060 + (n)*0x18)
60 #define RFBI_ONOFF_TIME(n)      RFBI_REG(0x0064 + (n)*0x18)
61 #define RFBI_CYCLE_TIME(n)      RFBI_REG(0x0068 + (n)*0x18)
62 #define RFBI_DATA_CYCLE1(n)     RFBI_REG(0x006c + (n)*0x18)
63 #define RFBI_DATA_CYCLE2(n)     RFBI_REG(0x0070 + (n)*0x18)
64 #define RFBI_DATA_CYCLE3(n)     RFBI_REG(0x0074 + (n)*0x18)
65
66 #define RFBI_VSYNC_WIDTH        RFBI_REG(0x0090)
67 #define RFBI_HSYNC_WIDTH        RFBI_REG(0x0094)
68
69 #define REG_FLD_MOD(idx, val, start, end) \
70         rfbi_write_reg(idx, FLD_MOD(rfbi_read_reg(idx), val, start, end))
71
72 enum omap_rfbi_cycleformat {
73         OMAP_DSS_RFBI_CYCLEFORMAT_1_1 = 0,
74         OMAP_DSS_RFBI_CYCLEFORMAT_2_1 = 1,
75         OMAP_DSS_RFBI_CYCLEFORMAT_3_1 = 2,
76         OMAP_DSS_RFBI_CYCLEFORMAT_3_2 = 3,
77 };
78
79 enum omap_rfbi_datatype {
80         OMAP_DSS_RFBI_DATATYPE_12 = 0,
81         OMAP_DSS_RFBI_DATATYPE_16 = 1,
82         OMAP_DSS_RFBI_DATATYPE_18 = 2,
83         OMAP_DSS_RFBI_DATATYPE_24 = 3,
84 };
85
86 enum omap_rfbi_parallelmode {
87         OMAP_DSS_RFBI_PARALLELMODE_8 = 0,
88         OMAP_DSS_RFBI_PARALLELMODE_9 = 1,
89         OMAP_DSS_RFBI_PARALLELMODE_12 = 2,
90         OMAP_DSS_RFBI_PARALLELMODE_16 = 3,
91 };
92
93 static int rfbi_convert_timings(struct rfbi_timings *t);
94 static void rfbi_get_clk_info(u32 *clk_period, u32 *max_clk_div);
95
96 static struct {
97         struct platform_device *pdev;
98         void __iomem    *base;
99
100         unsigned long   l4_khz;
101
102         enum omap_rfbi_datatype datatype;
103         enum omap_rfbi_parallelmode parallelmode;
104
105         enum omap_rfbi_te_mode te_mode;
106         int te_enabled;
107
108         void (*framedone_callback)(void *data);
109         void *framedone_callback_data;
110
111         struct omap_dss_device *dssdev[2];
112
113         struct semaphore bus_lock;
114 } rfbi;
115
116 static inline void rfbi_write_reg(const struct rfbi_reg idx, u32 val)
117 {
118         __raw_writel(val, rfbi.base + idx.idx);
119 }
120
121 static inline u32 rfbi_read_reg(const struct rfbi_reg idx)
122 {
123         return __raw_readl(rfbi.base + idx.idx);
124 }
125
126 static int rfbi_runtime_get(void)
127 {
128         int r;
129
130         DSSDBG("rfbi_runtime_get\n");
131
132         r = pm_runtime_get_sync(&rfbi.pdev->dev);
133         WARN_ON(r < 0);
134         return r < 0 ? r : 0;
135 }
136
137 static void rfbi_runtime_put(void)
138 {
139         int r;
140
141         DSSDBG("rfbi_runtime_put\n");
142
143         r = pm_runtime_put_sync(&rfbi.pdev->dev);
144         WARN_ON(r < 0);
145 }
146
147 void rfbi_bus_lock(void)
148 {
149         down(&rfbi.bus_lock);
150 }
151 EXPORT_SYMBOL(rfbi_bus_lock);
152
153 void rfbi_bus_unlock(void)
154 {
155         up(&rfbi.bus_lock);
156 }
157 EXPORT_SYMBOL(rfbi_bus_unlock);
158
159 void omap_rfbi_write_command(const void *buf, u32 len)
160 {
161         switch (rfbi.parallelmode) {
162         case OMAP_DSS_RFBI_PARALLELMODE_8:
163         {
164                 const u8 *b = buf;
165                 for (; len; len--)
166                         rfbi_write_reg(RFBI_CMD, *b++);
167                 break;
168         }
169
170         case OMAP_DSS_RFBI_PARALLELMODE_16:
171         {
172                 const u16 *w = buf;
173                 BUG_ON(len & 1);
174                 for (; len; len -= 2)
175                         rfbi_write_reg(RFBI_CMD, *w++);
176                 break;
177         }
178
179         case OMAP_DSS_RFBI_PARALLELMODE_9:
180         case OMAP_DSS_RFBI_PARALLELMODE_12:
181         default:
182                 BUG();
183         }
184 }
185 EXPORT_SYMBOL(omap_rfbi_write_command);
186
187 void omap_rfbi_read_data(void *buf, u32 len)
188 {
189         switch (rfbi.parallelmode) {
190         case OMAP_DSS_RFBI_PARALLELMODE_8:
191         {
192                 u8 *b = buf;
193                 for (; len; len--) {
194                         rfbi_write_reg(RFBI_READ, 0);
195                         *b++ = rfbi_read_reg(RFBI_READ);
196                 }
197                 break;
198         }
199
200         case OMAP_DSS_RFBI_PARALLELMODE_16:
201         {
202                 u16 *w = buf;
203                 BUG_ON(len & ~1);
204                 for (; len; len -= 2) {
205                         rfbi_write_reg(RFBI_READ, 0);
206                         *w++ = rfbi_read_reg(RFBI_READ);
207                 }
208                 break;
209         }
210
211         case OMAP_DSS_RFBI_PARALLELMODE_9:
212         case OMAP_DSS_RFBI_PARALLELMODE_12:
213         default:
214                 BUG();
215         }
216 }
217 EXPORT_SYMBOL(omap_rfbi_read_data);
218
219 void omap_rfbi_write_data(const void *buf, u32 len)
220 {
221         switch (rfbi.parallelmode) {
222         case OMAP_DSS_RFBI_PARALLELMODE_8:
223         {
224                 const u8 *b = buf;
225                 for (; len; len--)
226                         rfbi_write_reg(RFBI_PARAM, *b++);
227                 break;
228         }
229
230         case OMAP_DSS_RFBI_PARALLELMODE_16:
231         {
232                 const u16 *w = buf;
233                 BUG_ON(len & 1);
234                 for (; len; len -= 2)
235                         rfbi_write_reg(RFBI_PARAM, *w++);
236                 break;
237         }
238
239         case OMAP_DSS_RFBI_PARALLELMODE_9:
240         case OMAP_DSS_RFBI_PARALLELMODE_12:
241         default:
242                 BUG();
243
244         }
245 }
246 EXPORT_SYMBOL(omap_rfbi_write_data);
247
248 void omap_rfbi_write_pixels(const void __iomem *buf, int scr_width,
249                 u16 x, u16 y,
250                 u16 w, u16 h)
251 {
252         int start_offset = scr_width * y + x;
253         int horiz_offset = scr_width - w;
254         int i;
255
256         if (rfbi.datatype == OMAP_DSS_RFBI_DATATYPE_16 &&
257            rfbi.parallelmode == OMAP_DSS_RFBI_PARALLELMODE_8) {
258                 const u16 __iomem *pd = buf;
259                 pd += start_offset;
260
261                 for (; h; --h) {
262                         for (i = 0; i < w; ++i) {
263                                 const u8 __iomem *b = (const u8 __iomem *)pd;
264                                 rfbi_write_reg(RFBI_PARAM, __raw_readb(b+1));
265                                 rfbi_write_reg(RFBI_PARAM, __raw_readb(b+0));
266                                 ++pd;
267                         }
268                         pd += horiz_offset;
269                 }
270         } else if (rfbi.datatype == OMAP_DSS_RFBI_DATATYPE_24 &&
271            rfbi.parallelmode == OMAP_DSS_RFBI_PARALLELMODE_8) {
272                 const u32 __iomem *pd = buf;
273                 pd += start_offset;
274
275                 for (; h; --h) {
276                         for (i = 0; i < w; ++i) {
277                                 const u8 __iomem *b = (const u8 __iomem *)pd;
278                                 rfbi_write_reg(RFBI_PARAM, __raw_readb(b+2));
279                                 rfbi_write_reg(RFBI_PARAM, __raw_readb(b+1));
280                                 rfbi_write_reg(RFBI_PARAM, __raw_readb(b+0));
281                                 ++pd;
282                         }
283                         pd += horiz_offset;
284                 }
285         } else if (rfbi.datatype == OMAP_DSS_RFBI_DATATYPE_16 &&
286            rfbi.parallelmode == OMAP_DSS_RFBI_PARALLELMODE_16) {
287                 const u16 __iomem *pd = buf;
288                 pd += start_offset;
289
290                 for (; h; --h) {
291                         for (i = 0; i < w; ++i) {
292                                 rfbi_write_reg(RFBI_PARAM, __raw_readw(pd));
293                                 ++pd;
294                         }
295                         pd += horiz_offset;
296                 }
297         } else {
298                 BUG();
299         }
300 }
301 EXPORT_SYMBOL(omap_rfbi_write_pixels);
302
303 static void rfbi_transfer_area(struct omap_dss_device *dssdev, u16 width,
304                 u16 height, void (*callback)(void *data), void *data)
305 {
306         u32 l;
307         struct omap_video_timings timings = {
308                 .hsw            = 1,
309                 .hfp            = 1,
310                 .hbp            = 1,
311                 .vsw            = 1,
312                 .vfp            = 0,
313                 .vbp            = 0,
314                 .x_res          = width,
315                 .y_res          = height,
316         };
317
318         /*BUG_ON(callback == 0);*/
319         BUG_ON(rfbi.framedone_callback != NULL);
320
321         DSSDBG("rfbi_transfer_area %dx%d\n", width, height);
322
323         dss_mgr_set_timings(dssdev->manager, &timings);
324
325         dispc_mgr_enable(dssdev->manager->id, true);
326
327         rfbi.framedone_callback = callback;
328         rfbi.framedone_callback_data = data;
329
330         rfbi_write_reg(RFBI_PIXEL_CNT, width * height);
331
332         l = rfbi_read_reg(RFBI_CONTROL);
333         l = FLD_MOD(l, 1, 0, 0); /* enable */
334         if (!rfbi.te_enabled)
335                 l = FLD_MOD(l, 1, 4, 4); /* ITE */
336
337         rfbi_write_reg(RFBI_CONTROL, l);
338 }
339
340 static void framedone_callback(void *data, u32 mask)
341 {
342         void (*callback)(void *data);
343
344         DSSDBG("FRAMEDONE\n");
345
346         REG_FLD_MOD(RFBI_CONTROL, 0, 0, 0);
347
348         callback = rfbi.framedone_callback;
349         rfbi.framedone_callback = NULL;
350
351         if (callback != NULL)
352                 callback(rfbi.framedone_callback_data);
353 }
354
355 #if 1 /* VERBOSE */
356 static void rfbi_print_timings(void)
357 {
358         u32 l;
359         u32 time;
360
361         l = rfbi_read_reg(RFBI_CONFIG(0));
362         time = 1000000000 / rfbi.l4_khz;
363         if (l & (1 << 4))
364                 time *= 2;
365
366         DSSDBG("Tick time %u ps\n", time);
367         l = rfbi_read_reg(RFBI_ONOFF_TIME(0));
368         DSSDBG("CSONTIME %d, CSOFFTIME %d, WEONTIME %d, WEOFFTIME %d, "
369                 "REONTIME %d, REOFFTIME %d\n",
370                 l & 0x0f, (l >> 4) & 0x3f, (l >> 10) & 0x0f, (l >> 14) & 0x3f,
371                 (l >> 20) & 0x0f, (l >> 24) & 0x3f);
372
373         l = rfbi_read_reg(RFBI_CYCLE_TIME(0));
374         DSSDBG("WECYCLETIME %d, RECYCLETIME %d, CSPULSEWIDTH %d, "
375                 "ACCESSTIME %d\n",
376                 (l & 0x3f), (l >> 6) & 0x3f, (l >> 12) & 0x3f,
377                 (l >> 22) & 0x3f);
378 }
379 #else
380 static void rfbi_print_timings(void) {}
381 #endif
382
383
384
385
386 static u32 extif_clk_period;
387
388 static inline unsigned long round_to_extif_ticks(unsigned long ps, int div)
389 {
390         int bus_tick = extif_clk_period * div;
391         return (ps + bus_tick - 1) / bus_tick * bus_tick;
392 }
393
394 static int calc_reg_timing(struct rfbi_timings *t, int div)
395 {
396         t->clk_div = div;
397
398         t->cs_on_time = round_to_extif_ticks(t->cs_on_time, div);
399
400         t->we_on_time = round_to_extif_ticks(t->we_on_time, div);
401         t->we_off_time = round_to_extif_ticks(t->we_off_time, div);
402         t->we_cycle_time = round_to_extif_ticks(t->we_cycle_time, div);
403
404         t->re_on_time = round_to_extif_ticks(t->re_on_time, div);
405         t->re_off_time = round_to_extif_ticks(t->re_off_time, div);
406         t->re_cycle_time = round_to_extif_ticks(t->re_cycle_time, div);
407
408         t->access_time = round_to_extif_ticks(t->access_time, div);
409         t->cs_off_time = round_to_extif_ticks(t->cs_off_time, div);
410         t->cs_pulse_width = round_to_extif_ticks(t->cs_pulse_width, div);
411
412         DSSDBG("[reg]cson %d csoff %d reon %d reoff %d\n",
413                t->cs_on_time, t->cs_off_time, t->re_on_time, t->re_off_time);
414         DSSDBG("[reg]weon %d weoff %d recyc %d wecyc %d\n",
415                t->we_on_time, t->we_off_time, t->re_cycle_time,
416                t->we_cycle_time);
417         DSSDBG("[reg]rdaccess %d cspulse %d\n",
418                t->access_time, t->cs_pulse_width);
419
420         return rfbi_convert_timings(t);
421 }
422
423 static int calc_extif_timings(struct rfbi_timings *t)
424 {
425         u32 max_clk_div;
426         int div;
427
428         rfbi_get_clk_info(&extif_clk_period, &max_clk_div);
429         for (div = 1; div <= max_clk_div; div++) {
430                 if (calc_reg_timing(t, div) == 0)
431                         break;
432         }
433
434         if (div <= max_clk_div)
435                 return 0;
436
437         DSSERR("can't setup timings\n");
438         return -1;
439 }
440
441
442 static void rfbi_set_timings(int rfbi_module, struct rfbi_timings *t)
443 {
444         int r;
445
446         if (!t->converted) {
447                 r = calc_extif_timings(t);
448                 if (r < 0)
449                         DSSERR("Failed to calc timings\n");
450         }
451
452         BUG_ON(!t->converted);
453
454         rfbi_write_reg(RFBI_ONOFF_TIME(rfbi_module), t->tim[0]);
455         rfbi_write_reg(RFBI_CYCLE_TIME(rfbi_module), t->tim[1]);
456
457         /* TIMEGRANULARITY */
458         REG_FLD_MOD(RFBI_CONFIG(rfbi_module),
459                     (t->tim[2] ? 1 : 0), 4, 4);
460
461         rfbi_print_timings();
462 }
463
464 static int ps_to_rfbi_ticks(int time, int div)
465 {
466         unsigned long tick_ps;
467         int ret;
468
469         /* Calculate in picosecs to yield more exact results */
470         tick_ps = 1000000000 / (rfbi.l4_khz) * div;
471
472         ret = (time + tick_ps - 1) / tick_ps;
473
474         return ret;
475 }
476
477 static void rfbi_get_clk_info(u32 *clk_period, u32 *max_clk_div)
478 {
479         *clk_period = 1000000000 / rfbi.l4_khz;
480         *max_clk_div = 2;
481 }
482
483 static int rfbi_convert_timings(struct rfbi_timings *t)
484 {
485         u32 l;
486         int reon, reoff, weon, weoff, cson, csoff, cs_pulse;
487         int actim, recyc, wecyc;
488         int div = t->clk_div;
489
490         if (div <= 0 || div > 2)
491                 return -1;
492
493         /* Make sure that after conversion it still holds that:
494          * weoff > weon, reoff > reon, recyc >= reoff, wecyc >= weoff,
495          * csoff > cson, csoff >= max(weoff, reoff), actim > reon
496          */
497         weon = ps_to_rfbi_ticks(t->we_on_time, div);
498         weoff = ps_to_rfbi_ticks(t->we_off_time, div);
499         if (weoff <= weon)
500                 weoff = weon + 1;
501         if (weon > 0x0f)
502                 return -1;
503         if (weoff > 0x3f)
504                 return -1;
505
506         reon = ps_to_rfbi_ticks(t->re_on_time, div);
507         reoff = ps_to_rfbi_ticks(t->re_off_time, div);
508         if (reoff <= reon)
509                 reoff = reon + 1;
510         if (reon > 0x0f)
511                 return -1;
512         if (reoff > 0x3f)
513                 return -1;
514
515         cson = ps_to_rfbi_ticks(t->cs_on_time, div);
516         csoff = ps_to_rfbi_ticks(t->cs_off_time, div);
517         if (csoff <= cson)
518                 csoff = cson + 1;
519         if (csoff < max(weoff, reoff))
520                 csoff = max(weoff, reoff);
521         if (cson > 0x0f)
522                 return -1;
523         if (csoff > 0x3f)
524                 return -1;
525
526         l =  cson;
527         l |= csoff << 4;
528         l |= weon  << 10;
529         l |= weoff << 14;
530         l |= reon  << 20;
531         l |= reoff << 24;
532
533         t->tim[0] = l;
534
535         actim = ps_to_rfbi_ticks(t->access_time, div);
536         if (actim <= reon)
537                 actim = reon + 1;
538         if (actim > 0x3f)
539                 return -1;
540
541         wecyc = ps_to_rfbi_ticks(t->we_cycle_time, div);
542         if (wecyc < weoff)
543                 wecyc = weoff;
544         if (wecyc > 0x3f)
545                 return -1;
546
547         recyc = ps_to_rfbi_ticks(t->re_cycle_time, div);
548         if (recyc < reoff)
549                 recyc = reoff;
550         if (recyc > 0x3f)
551                 return -1;
552
553         cs_pulse = ps_to_rfbi_ticks(t->cs_pulse_width, div);
554         if (cs_pulse > 0x3f)
555                 return -1;
556
557         l =  wecyc;
558         l |= recyc    << 6;
559         l |= cs_pulse << 12;
560         l |= actim    << 22;
561
562         t->tim[1] = l;
563
564         t->tim[2] = div - 1;
565
566         t->converted = 1;
567
568         return 0;
569 }
570
571 /* xxx FIX module selection missing */
572 int omap_rfbi_setup_te(enum omap_rfbi_te_mode mode,
573                              unsigned hs_pulse_time, unsigned vs_pulse_time,
574                              int hs_pol_inv, int vs_pol_inv, int extif_div)
575 {
576         int hs, vs;
577         int min;
578         u32 l;
579
580         hs = ps_to_rfbi_ticks(hs_pulse_time, 1);
581         vs = ps_to_rfbi_ticks(vs_pulse_time, 1);
582         if (hs < 2)
583                 return -EDOM;
584         if (mode == OMAP_DSS_RFBI_TE_MODE_2)
585                 min = 2;
586         else /* OMAP_DSS_RFBI_TE_MODE_1 */
587                 min = 4;
588         if (vs < min)
589                 return -EDOM;
590         if (vs == hs)
591                 return -EINVAL;
592         rfbi.te_mode = mode;
593         DSSDBG("setup_te: mode %d hs %d vs %d hs_inv %d vs_inv %d\n",
594                 mode, hs, vs, hs_pol_inv, vs_pol_inv);
595
596         rfbi_write_reg(RFBI_HSYNC_WIDTH, hs);
597         rfbi_write_reg(RFBI_VSYNC_WIDTH, vs);
598
599         l = rfbi_read_reg(RFBI_CONFIG(0));
600         if (hs_pol_inv)
601                 l &= ~(1 << 21);
602         else
603                 l |= 1 << 21;
604         if (vs_pol_inv)
605                 l &= ~(1 << 20);
606         else
607                 l |= 1 << 20;
608
609         return 0;
610 }
611 EXPORT_SYMBOL(omap_rfbi_setup_te);
612
613 /* xxx FIX module selection missing */
614 int omap_rfbi_enable_te(bool enable, unsigned line)
615 {
616         u32 l;
617
618         DSSDBG("te %d line %d mode %d\n", enable, line, rfbi.te_mode);
619         if (line > (1 << 11) - 1)
620                 return -EINVAL;
621
622         l = rfbi_read_reg(RFBI_CONFIG(0));
623         l &= ~(0x3 << 2);
624         if (enable) {
625                 rfbi.te_enabled = 1;
626                 l |= rfbi.te_mode << 2;
627         } else
628                 rfbi.te_enabled = 0;
629         rfbi_write_reg(RFBI_CONFIG(0), l);
630         rfbi_write_reg(RFBI_LINE_NUMBER, line);
631
632         return 0;
633 }
634 EXPORT_SYMBOL(omap_rfbi_enable_te);
635
636 static int rfbi_configure(int rfbi_module, int bpp, int lines)
637 {
638         u32 l;
639         int cycle1 = 0, cycle2 = 0, cycle3 = 0;
640         enum omap_rfbi_cycleformat cycleformat;
641         enum omap_rfbi_datatype datatype;
642         enum omap_rfbi_parallelmode parallelmode;
643
644         switch (bpp) {
645         case 12:
646                 datatype = OMAP_DSS_RFBI_DATATYPE_12;
647                 break;
648         case 16:
649                 datatype = OMAP_DSS_RFBI_DATATYPE_16;
650                 break;
651         case 18:
652                 datatype = OMAP_DSS_RFBI_DATATYPE_18;
653                 break;
654         case 24:
655                 datatype = OMAP_DSS_RFBI_DATATYPE_24;
656                 break;
657         default:
658                 BUG();
659                 return 1;
660         }
661         rfbi.datatype = datatype;
662
663         switch (lines) {
664         case 8:
665                 parallelmode = OMAP_DSS_RFBI_PARALLELMODE_8;
666                 break;
667         case 9:
668                 parallelmode = OMAP_DSS_RFBI_PARALLELMODE_9;
669                 break;
670         case 12:
671                 parallelmode = OMAP_DSS_RFBI_PARALLELMODE_12;
672                 break;
673         case 16:
674                 parallelmode = OMAP_DSS_RFBI_PARALLELMODE_16;
675                 break;
676         default:
677                 BUG();
678                 return 1;
679         }
680         rfbi.parallelmode = parallelmode;
681
682         if ((bpp % lines) == 0) {
683                 switch (bpp / lines) {
684                 case 1:
685                         cycleformat = OMAP_DSS_RFBI_CYCLEFORMAT_1_1;
686                         break;
687                 case 2:
688                         cycleformat = OMAP_DSS_RFBI_CYCLEFORMAT_2_1;
689                         break;
690                 case 3:
691                         cycleformat = OMAP_DSS_RFBI_CYCLEFORMAT_3_1;
692                         break;
693                 default:
694                         BUG();
695                         return 1;
696                 }
697         } else if ((2 * bpp % lines) == 0) {
698                 if ((2 * bpp / lines) == 3)
699                         cycleformat = OMAP_DSS_RFBI_CYCLEFORMAT_3_2;
700                 else {
701                         BUG();
702                         return 1;
703                 }
704         } else {
705                 BUG();
706                 return 1;
707         }
708
709         switch (cycleformat) {
710         case OMAP_DSS_RFBI_CYCLEFORMAT_1_1:
711                 cycle1 = lines;
712                 break;
713
714         case OMAP_DSS_RFBI_CYCLEFORMAT_2_1:
715                 cycle1 = lines;
716                 cycle2 = lines;
717                 break;
718
719         case OMAP_DSS_RFBI_CYCLEFORMAT_3_1:
720                 cycle1 = lines;
721                 cycle2 = lines;
722                 cycle3 = lines;
723                 break;
724
725         case OMAP_DSS_RFBI_CYCLEFORMAT_3_2:
726                 cycle1 = lines;
727                 cycle2 = (lines / 2) | ((lines / 2) << 16);
728                 cycle3 = (lines << 16);
729                 break;
730         }
731
732         REG_FLD_MOD(RFBI_CONTROL, 0, 3, 2); /* clear CS */
733
734         l = 0;
735         l |= FLD_VAL(parallelmode, 1, 0);
736         l |= FLD_VAL(0, 3, 2);          /* TRIGGERMODE: ITE */
737         l |= FLD_VAL(0, 4, 4);          /* TIMEGRANULARITY */
738         l |= FLD_VAL(datatype, 6, 5);
739         /* l |= FLD_VAL(2, 8, 7); */    /* L4FORMAT, 2pix/L4 */
740         l |= FLD_VAL(0, 8, 7);  /* L4FORMAT, 1pix/L4 */
741         l |= FLD_VAL(cycleformat, 10, 9);
742         l |= FLD_VAL(0, 12, 11);        /* UNUSEDBITS */
743         l |= FLD_VAL(0, 16, 16);        /* A0POLARITY */
744         l |= FLD_VAL(0, 17, 17);        /* REPOLARITY */
745         l |= FLD_VAL(0, 18, 18);        /* WEPOLARITY */
746         l |= FLD_VAL(0, 19, 19);        /* CSPOLARITY */
747         l |= FLD_VAL(1, 20, 20);        /* TE_VSYNC_POLARITY */
748         l |= FLD_VAL(1, 21, 21);        /* HSYNCPOLARITY */
749         rfbi_write_reg(RFBI_CONFIG(rfbi_module), l);
750
751         rfbi_write_reg(RFBI_DATA_CYCLE1(rfbi_module), cycle1);
752         rfbi_write_reg(RFBI_DATA_CYCLE2(rfbi_module), cycle2);
753         rfbi_write_reg(RFBI_DATA_CYCLE3(rfbi_module), cycle3);
754
755
756         l = rfbi_read_reg(RFBI_CONTROL);
757         l = FLD_MOD(l, rfbi_module+1, 3, 2); /* Select CSx */
758         l = FLD_MOD(l, 0, 1, 1); /* clear bypass */
759         rfbi_write_reg(RFBI_CONTROL, l);
760
761
762         DSSDBG("RFBI config: bpp %d, lines %d, cycles: 0x%x 0x%x 0x%x\n",
763                bpp, lines, cycle1, cycle2, cycle3);
764
765         return 0;
766 }
767
768 int omap_rfbi_configure(struct omap_dss_device *dssdev, int pixel_size,
769                 int data_lines)
770 {
771         return rfbi_configure(dssdev->phy.rfbi.channel, pixel_size, data_lines);
772 }
773 EXPORT_SYMBOL(omap_rfbi_configure);
774
775 int omap_rfbi_prepare_update(struct omap_dss_device *dssdev,
776                 u16 *x, u16 *y, u16 *w, u16 *h)
777 {
778         u16 dw, dh;
779         struct omap_video_timings timings = {
780                 .hsw            = 1,
781                 .hfp            = 1,
782                 .hbp            = 1,
783                 .vsw            = 1,
784                 .vfp            = 0,
785                 .vbp            = 0,
786                 .x_res          = *w,
787                 .y_res          = *h,
788         };
789
790         dssdev->driver->get_resolution(dssdev, &dw, &dh);
791
792         if  (*x > dw || *y > dh)
793                 return -EINVAL;
794
795         if (*x + *w > dw)
796                 return -EINVAL;
797
798         if (*y + *h > dh)
799                 return -EINVAL;
800
801         if (*w == 1)
802                 return -EINVAL;
803
804         if (*w == 0 || *h == 0)
805                 return -EINVAL;
806
807         dss_mgr_set_timings(dssdev->manager, &timings);
808
809         return 0;
810 }
811 EXPORT_SYMBOL(omap_rfbi_prepare_update);
812
813 int omap_rfbi_update(struct omap_dss_device *dssdev,
814                 u16 x, u16 y, u16 w, u16 h,
815                 void (*callback)(void *), void *data)
816 {
817         rfbi_transfer_area(dssdev, w, h, callback, data);
818         return 0;
819 }
820 EXPORT_SYMBOL(omap_rfbi_update);
821
822 static void rfbi_dump_regs(struct seq_file *s)
823 {
824 #define DUMPREG(r) seq_printf(s, "%-35s %08x\n", #r, rfbi_read_reg(r))
825
826         if (rfbi_runtime_get())
827                 return;
828
829         DUMPREG(RFBI_REVISION);
830         DUMPREG(RFBI_SYSCONFIG);
831         DUMPREG(RFBI_SYSSTATUS);
832         DUMPREG(RFBI_CONTROL);
833         DUMPREG(RFBI_PIXEL_CNT);
834         DUMPREG(RFBI_LINE_NUMBER);
835         DUMPREG(RFBI_CMD);
836         DUMPREG(RFBI_PARAM);
837         DUMPREG(RFBI_DATA);
838         DUMPREG(RFBI_READ);
839         DUMPREG(RFBI_STATUS);
840
841         DUMPREG(RFBI_CONFIG(0));
842         DUMPREG(RFBI_ONOFF_TIME(0));
843         DUMPREG(RFBI_CYCLE_TIME(0));
844         DUMPREG(RFBI_DATA_CYCLE1(0));
845         DUMPREG(RFBI_DATA_CYCLE2(0));
846         DUMPREG(RFBI_DATA_CYCLE3(0));
847
848         DUMPREG(RFBI_CONFIG(1));
849         DUMPREG(RFBI_ONOFF_TIME(1));
850         DUMPREG(RFBI_CYCLE_TIME(1));
851         DUMPREG(RFBI_DATA_CYCLE1(1));
852         DUMPREG(RFBI_DATA_CYCLE2(1));
853         DUMPREG(RFBI_DATA_CYCLE3(1));
854
855         DUMPREG(RFBI_VSYNC_WIDTH);
856         DUMPREG(RFBI_HSYNC_WIDTH);
857
858         rfbi_runtime_put();
859 #undef DUMPREG
860 }
861
862 int omapdss_rfbi_display_enable(struct omap_dss_device *dssdev)
863 {
864         int r;
865
866         if (dssdev->manager == NULL) {
867                 DSSERR("failed to enable display: no manager\n");
868                 return -ENODEV;
869         }
870
871         r = rfbi_runtime_get();
872         if (r)
873                 return r;
874
875         r = omap_dss_start_device(dssdev);
876         if (r) {
877                 DSSERR("failed to start device\n");
878                 goto err0;
879         }
880
881         r = omap_dispc_register_isr(framedone_callback, NULL,
882                         DISPC_IRQ_FRAMEDONE);
883         if (r) {
884                 DSSERR("can't get FRAMEDONE irq\n");
885                 goto err1;
886         }
887
888         dispc_mgr_set_lcd_display_type(dssdev->manager->id,
889                         OMAP_DSS_LCD_DISPLAY_TFT);
890
891         dispc_mgr_set_io_pad_mode(DSS_IO_PAD_MODE_RFBI);
892         dispc_mgr_enable_stallmode(dssdev->manager->id, true);
893
894         dispc_mgr_set_tft_data_lines(dssdev->manager->id, dssdev->ctrl.pixel_size);
895
896         rfbi_configure(dssdev->phy.rfbi.channel,
897                                dssdev->ctrl.pixel_size,
898                                dssdev->phy.rfbi.data_lines);
899
900         rfbi_set_timings(dssdev->phy.rfbi.channel,
901                          &dssdev->ctrl.rfbi_timings);
902
903
904         return 0;
905 err1:
906         omap_dss_stop_device(dssdev);
907 err0:
908         rfbi_runtime_put();
909         return r;
910 }
911 EXPORT_SYMBOL(omapdss_rfbi_display_enable);
912
913 void omapdss_rfbi_display_disable(struct omap_dss_device *dssdev)
914 {
915         omap_dispc_unregister_isr(framedone_callback, NULL,
916                         DISPC_IRQ_FRAMEDONE);
917         omap_dss_stop_device(dssdev);
918
919         rfbi_runtime_put();
920 }
921 EXPORT_SYMBOL(omapdss_rfbi_display_disable);
922
923 static int __init rfbi_init_display(struct omap_dss_device *dssdev)
924 {
925         rfbi.dssdev[dssdev->phy.rfbi.channel] = dssdev;
926         dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE;
927         return 0;
928 }
929
930 static void __init rfbi_probe_pdata(struct platform_device *pdev)
931 {
932         struct omap_dss_board_info *pdata = pdev->dev.platform_data;
933         int i, r;
934
935         for (i = 0; i < pdata->num_devices; ++i) {
936                 struct omap_dss_device *dssdev = pdata->devices[i];
937
938                 if (dssdev->type != OMAP_DISPLAY_TYPE_DBI)
939                         continue;
940
941                 r = rfbi_init_display(dssdev);
942                 if (r) {
943                         DSSERR("device %s init failed: %d\n", dssdev->name, r);
944                         continue;
945                 }
946
947                 r = omap_dss_register_device(dssdev, &pdev->dev, i);
948                 if (r)
949                         DSSERR("device %s register failed: %d\n",
950                                 dssdev->name, r);
951         }
952 }
953
954 /* RFBI HW IP initialisation */
955 static int __init omap_rfbihw_probe(struct platform_device *pdev)
956 {
957         u32 rev;
958         struct resource *rfbi_mem;
959         struct clk *clk;
960         int r;
961
962         rfbi.pdev = pdev;
963
964         sema_init(&rfbi.bus_lock, 1);
965
966         rfbi_mem = platform_get_resource(rfbi.pdev, IORESOURCE_MEM, 0);
967         if (!rfbi_mem) {
968                 DSSERR("can't get IORESOURCE_MEM RFBI\n");
969                 return -EINVAL;
970         }
971
972         rfbi.base = devm_ioremap(&pdev->dev, rfbi_mem->start,
973                                  resource_size(rfbi_mem));
974         if (!rfbi.base) {
975                 DSSERR("can't ioremap RFBI\n");
976                 return -ENOMEM;
977         }
978
979         clk = clk_get(&pdev->dev, "ick");
980         if (IS_ERR(clk)) {
981                 DSSERR("can't get ick\n");
982                 return PTR_ERR(clk);
983         }
984
985         rfbi.l4_khz = clk_get_rate(clk) / 1000;
986
987         clk_put(clk);
988
989         pm_runtime_enable(&pdev->dev);
990
991         r = rfbi_runtime_get();
992         if (r)
993                 goto err_runtime_get;
994
995         msleep(10);
996
997         rev = rfbi_read_reg(RFBI_REVISION);
998         dev_dbg(&pdev->dev, "OMAP RFBI rev %d.%d\n",
999                FLD_GET(rev, 7, 4), FLD_GET(rev, 3, 0));
1000
1001         rfbi_runtime_put();
1002
1003         dss_debugfs_create_file("rfbi", rfbi_dump_regs);
1004
1005         rfbi_probe_pdata(pdev);
1006
1007         return 0;
1008
1009 err_runtime_get:
1010         pm_runtime_disable(&pdev->dev);
1011         return r;
1012 }
1013
1014 static int __exit omap_rfbihw_remove(struct platform_device *pdev)
1015 {
1016         omap_dss_unregister_child_devices(&pdev->dev);
1017         pm_runtime_disable(&pdev->dev);
1018         return 0;
1019 }
1020
1021 static int rfbi_runtime_suspend(struct device *dev)
1022 {
1023         dispc_runtime_put();
1024
1025         return 0;
1026 }
1027
1028 static int rfbi_runtime_resume(struct device *dev)
1029 {
1030         int r;
1031
1032         r = dispc_runtime_get();
1033         if (r < 0)
1034                 return r;
1035
1036         return 0;
1037 }
1038
1039 static const struct dev_pm_ops rfbi_pm_ops = {
1040         .runtime_suspend = rfbi_runtime_suspend,
1041         .runtime_resume = rfbi_runtime_resume,
1042 };
1043
1044 static struct platform_driver omap_rfbihw_driver = {
1045         .remove         = __exit_p(omap_rfbihw_remove),
1046         .driver         = {
1047                 .name   = "omapdss_rfbi",
1048                 .owner  = THIS_MODULE,
1049                 .pm     = &rfbi_pm_ops,
1050         },
1051 };
1052
1053 int __init rfbi_init_platform_driver(void)
1054 {
1055         return platform_driver_probe(&omap_rfbihw_driver, omap_rfbihw_probe);
1056 }
1057
1058 void __exit rfbi_uninit_platform_driver(void)
1059 {
1060         platform_driver_unregister(&omap_rfbihw_driver);
1061 }