b8415412dd1b5c1e2b0213634ba2edf21531bc36
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / tve / rk3036 / rk3036_tve.c
1 /*
2  * rk3036_tve.c
3  *
4  * Driver for rockchip rk3036 tv encoder control
5  * Copyright (C) 2014
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  *
12  *
13  */
14 #include <linux/clk.h>
15 #include <linux/display-sys.h>
16 #include <linux/device.h>
17 #include <linux/delay.h>
18 #include <linux/fb.h>
19 #include <linux/io.h>
20 #include <linux/module.h>
21 #include <linux/rk_fb.h>
22 #include <linux/rockchip/grf.h>
23 #include <linux/rockchip/iomap.h>
24 #include "../../hdmi/rockchip-hdmiv2/rockchip_hdmiv2.h"
25 #include "rk3036_tve.h"
26
27 static const struct fb_videomode rk3036_cvbs_mode[] = {
28         /*name          refresh xres    yres    pixclock        h_bp    h_fp    v_bp    v_fp    h_pw    v_pw                    polariry                                PorI            flag*/
29 /*      {"NTSC",        60,     720,    480,    27000000,       57,     19,     19,     0,      62,     3,      FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,   FB_VMODE_INTERLACED,    0},
30         {"PAL",         50,     720,    576,    27000000,       69,     12,     19,     2,      63,     3,      FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,   FB_VMODE_INTERLACED,    0},
31 */      {"NTSC",        60,     720,    480,    27000000,       43,     33,     19,     0,      62,     3,      FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,   FB_VMODE_INTERLACED,    0},
32         {"PAL",         50,     720,    576,    27000000,       48,     33,     19,     2,      63,     3,      FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,   FB_VMODE_INTERLACED,    0},
33 };
34
35 static struct rk3036_tve *rk3036_tve;
36
37 static int cvbsformat = -1;
38
39 #define tve_writel(offset, v)   writel_relaxed(v, rk3036_tve->regbase + offset)
40 #define tve_readl(offset)       readl_relaxed(rk3036_tve->regbase + offset)
41
42 #define tve_dac_writel(offset, v)   writel_relaxed(v, rk3036_tve->vdacbase + offset)
43 #define tve_dac_readl(offset)   readl_relaxed(rk3036_tve->vdacbase + offset)
44
45 #ifdef DEBUG
46 #define TVEDBG(format, ...) \
47                 dev_info(rk3036_tve->dev,\
48                  "RK3036 TVE: " format "\n", ## __VA_ARGS__)
49 #else
50 #define TVEDBG(format, ...)
51 #endif
52
53 static void dac_enable(bool enable)
54 {
55         u32 mask, val;
56         u32 grfreg = 0;
57
58         TVEDBG("%s enable %d\n", __func__, enable);
59
60         if (enable) {
61                 mask = m_VBG_EN | m_DAC_EN | m_DAC_GAIN;
62                 if (rk3036_tve->soctype == SOC_RK312X) {
63                         val = m_VBG_EN | m_DAC_EN |
64                               v_DAC_GAIN(rk3036_tve->daclevel);
65                         grfreg = RK312X_GRF_TVE_CON;
66                 } else if (rk3036_tve->soctype == SOC_RK3036) {
67                         val = m_VBG_EN | m_DAC_EN |
68                               v_DAC_GAIN(rk3036_tve->daclevel);
69                         grfreg = RK3036_GRF_SOC_CON3;
70                 } else if (rk3036_tve->soctype == SOC_RK322X) {
71                         val = 0x70;
72                 }
73         } else {
74                 mask = m_VBG_EN | m_DAC_EN;
75                 val = 0;
76                 if (rk3036_tve->soctype == SOC_RK312X)
77                         grfreg = RK312X_GRF_TVE_CON;
78                 else if (rk3036_tve->soctype == SOC_RK3036)
79                         grfreg = RK3036_GRF_SOC_CON3;
80                 else if (rk3036_tve->soctype == SOC_RK322X)
81                         val = v_CUR_REG(0x7) | m_DR_PWR_DOWN | m_BG_PWR_DOWN;
82         }
83         if (grfreg)
84                 grf_writel(grfreg, (mask << 16) | val);
85         else if (rk3036_tve->vdacbase)
86                 tve_dac_writel(VDAC_VDAC1, val);
87 }
88
89 static void rk322x_dac_init(void)
90 {
91         /*tve_dac_writel(VDAC_VDAC0, 0x0);*/
92         tve_dac_writel(VDAC_VDAC1, v_CUR_REG(0x7) |
93                                    m_DR_PWR_DOWN | m_BG_PWR_DOWN);
94         tve_dac_writel(VDAC_VDAC2, v_CUR_CTR(rk3036_tve->daclevel));
95         tve_dac_writel(VDAC_VDAC3, v_CAB_EN(0));
96 }
97
98 static void tve_set_mode(int mode)
99 {
100         TVEDBG("%s mode %d\n", __func__, mode);
101         if (cvbsformat >= 0)
102                 return;
103         if (rk3036_tve->soctype != SOC_RK322X) {
104                 tve_writel(TV_RESET, v_RESET(1));
105                 usleep_range(100, 110);
106                 tve_writel(TV_RESET, v_RESET(0));
107         }
108         if (rk3036_tve->inputformat == INPUT_FORMAT_RGB)
109                 tve_writel(TV_CTRL, v_CVBS_MODE(mode) | v_CLK_UPSTREAM_EN(2) |
110                            v_TIMING_EN(2) | v_LUMA_FILTER_GAIN(0) |
111                            v_LUMA_FILTER_UPSAMPLE(1) | v_CSC_PATH(0));
112         else
113                 tve_writel(TV_CTRL, v_CVBS_MODE(mode) | v_CLK_UPSTREAM_EN(2) |
114                            v_TIMING_EN(2) | v_LUMA_FILTER_GAIN(0) |
115                            v_LUMA_FILTER_UPSAMPLE(1) | v_CSC_PATH(3));
116
117         tve_writel(TV_LUMA_FILTER0, rk3036_tve->lumafilter0);
118         tve_writel(TV_LUMA_FILTER1, rk3036_tve->lumafilter1);
119         tve_writel(TV_LUMA_FILTER2, rk3036_tve->lumafilter2);
120
121         if (mode == TVOUT_CVBS_NTSC) {
122                 tve_writel(TV_ROUTING, v_DAC_SENSE_EN(0) | v_Y_IRE_7_5(1) |
123                         v_Y_AGC_PULSE_ON(0) | v_Y_VIDEO_ON(1) |
124                         v_YPP_MODE(1) | v_Y_SYNC_ON(1) | v_PIC_MODE(mode));
125                 tve_writel(TV_BW_CTRL, v_CHROMA_BW(BP_FILTER_NTSC) |
126                         v_COLOR_DIFF_BW(COLOR_DIFF_FILTER_BW_1_3));
127                 tve_writel(TV_SATURATION, 0x0042543C);
128                 if (rk3036_tve->test_mode)
129                         tve_writel(TV_BRIGHTNESS_CONTRAST, 0x00008300);
130                 else
131                         tve_writel(TV_BRIGHTNESS_CONTRAST, 0x00007900);
132
133                 tve_writel(TV_FREQ_SC,  0x21F07BD7);
134                 tve_writel(TV_SYNC_TIMING, 0x00C07a81);
135                 tve_writel(TV_ADJ_TIMING, 0x96B40000 | 0x70);
136                 tve_writel(TV_ACT_ST,   0x001500D6);
137                 tve_writel(TV_ACT_TIMING, 0x069800FC | (1 << 12) | (1 << 28));
138
139         } else if (mode == TVOUT_CVBS_PAL) {
140                 tve_writel(TV_ROUTING, v_DAC_SENSE_EN(0) | v_Y_IRE_7_5(0) |
141                         v_Y_AGC_PULSE_ON(0) | v_Y_VIDEO_ON(1) |
142                         v_YPP_MODE(1) | v_Y_SYNC_ON(1) | v_PIC_MODE(mode));
143                 tve_writel(TV_BW_CTRL, v_CHROMA_BW(BP_FILTER_PAL) |
144                         v_COLOR_DIFF_BW(COLOR_DIFF_FILTER_BW_1_3));
145
146                 tve_writel(TV_SATURATION, rk3036_tve->saturation);
147                 tve_writel(TV_BRIGHTNESS_CONTRAST, rk3036_tve->brightcontrast);
148
149                 tve_writel(TV_FREQ_SC,  0x2A098ACB);
150                 tve_writel(TV_SYNC_TIMING, 0x00C28381);
151                 tve_writel(TV_ADJ_TIMING, (0xc << 28) | 0x06c00800 | 0x80);
152                 tve_writel(TV_ACT_ST,   0x001500F6);
153                 tve_writel(TV_ACT_TIMING, 0x0694011D | (1 << 12) | (2 << 28));
154
155                 tve_writel(TV_ADJ_TIMING, rk3036_tve->adjtiming);
156                 tve_writel(TV_ACT_TIMING, 0x0694011D |
157                                 (1 << 12) | (2 << 28));
158         }
159 }
160
161 static int tve_switch_fb(const struct fb_videomode *modedb, int enable)
162 {
163         struct rk_screen *screen = &rk3036_tve->screen;
164
165         if (!modedb)
166                 return -1;
167
168         memset(screen, 0, sizeof(struct rk_screen));
169         /* screen type & face */
170         if (rk3036_tve->test_mode)
171                 screen->type = SCREEN_TVOUT_TEST;
172         else
173                 screen->type = SCREEN_TVOUT;
174         screen->face = OUT_P888;
175         screen->color_mode = COLOR_YCBCR;
176         screen->mode = *modedb;
177
178         /* Pin polarity */
179         if (FB_SYNC_HOR_HIGH_ACT & modedb->sync)
180                 screen->pin_hsync = 1;
181         else
182                 screen->pin_hsync = 0;
183         if (FB_SYNC_VERT_HIGH_ACT & modedb->sync)
184                 screen->pin_vsync = 1;
185         else
186                 screen->pin_vsync = 0;
187
188         screen->pin_den = 0;
189         screen->pin_dclk = 0;
190         screen->pixelrepeat = 1;
191
192         /* Swap rule */
193         screen->swap_rb = 0;
194         screen->swap_rg = 0;
195         screen->swap_gb = 0;
196         screen->swap_delta = 0;
197         screen->swap_dumy = 0;
198
199         /* Operation function*/
200         screen->init = NULL;
201         screen->standby = NULL;
202         rk_fb_switch_screen(screen, enable, 0);
203
204         if (enable) {
205                 if (rk3036_tve->soctype == SOC_RK322X)
206                         ext_pll_set_27m_out();
207                 if (screen->mode.yres == 480)
208                         tve_set_mode(TVOUT_CVBS_NTSC);
209                 else
210                         tve_set_mode(TVOUT_CVBS_PAL);
211         }
212         return 0;
213 }
214
215 static int cvbs_set_enable(struct rk_display_device *device, int enable)
216 {
217         TVEDBG("%s enable %d\n", __func__, enable);
218         mutex_lock(&rk3036_tve->tve_lock);
219         if (rk3036_tve->enable != enable) {
220                 rk3036_tve->enable = enable;
221                 if (rk3036_tve->suspend) {
222                         mutex_unlock(&rk3036_tve->tve_lock);
223                         return 0;
224                 }
225
226                 if (enable == 0) {
227                         dac_enable(false);
228                         cvbsformat = -1;
229                         tve_switch_fb(rk3036_tve->mode, 0);
230                 } else if (enable == 1) {
231                         tve_switch_fb(rk3036_tve->mode, 1);
232                         dac_enable(true);
233                 }
234         }
235         mutex_unlock(&rk3036_tve->tve_lock);
236         return 0;
237 }
238
239 static int cvbs_get_enable(struct rk_display_device *device)
240 {
241         TVEDBG("%s enable %d\n", __func__, rk3036_tve->enable);
242         return rk3036_tve->enable;
243 }
244
245 static int cvbs_get_status(struct rk_display_device *device)
246 {
247         return 1;
248 }
249
250 static int
251 cvbs_get_modelist(struct rk_display_device *device, struct list_head **modelist)
252 {
253         *modelist = &rk3036_tve->modelist;
254         return 0;
255 }
256
257 static int
258 cvbs_set_mode(struct rk_display_device *device, struct fb_videomode *mode)
259 {
260         int i;
261
262         for (i = 0; i < ARRAY_SIZE(rk3036_cvbs_mode); i++) {
263                 if (fb_mode_is_equal(&rk3036_cvbs_mode[i], mode)) {
264                         if (rk3036_tve->mode != &rk3036_cvbs_mode[i]) {
265                                 rk3036_tve->mode =
266                                 (struct fb_videomode *)&rk3036_cvbs_mode[i];
267                                 if (rk3036_tve->enable && !rk3036_tve->suspend) {
268                                         dac_enable(false);
269                                         msleep(200);
270                                         tve_switch_fb(rk3036_tve->mode, 1);
271                                         dac_enable(true);
272                                 }
273                         }
274                         return 0;
275                 }
276         }
277         TVEDBG("%s\n", __func__);
278         return -1;
279 }
280
281 static int
282 cvbs_get_mode(struct rk_display_device *device, struct fb_videomode *mode)
283 {
284         *mode = *rk3036_tve->mode;
285         return 0;
286 }
287
288 static int
289 tve_fb_event_notify(struct notifier_block *self,
290                     unsigned long action, void *data)
291 {
292         struct fb_event *event = data;
293         int blank_mode = *((int *)event->data);
294
295         if (action == FB_EARLY_EVENT_BLANK) {
296                 switch (blank_mode) {
297                 case FB_BLANK_UNBLANK:
298                         break;
299                 default:
300                         TVEDBG("suspend tve\n");
301                         if (!rk3036_tve->suspend) {
302                                 rk3036_tve->suspend = 1;
303                                 if (rk3036_tve->enable) {
304                                         tve_switch_fb(rk3036_tve->mode, 0);
305                                         dac_enable(false);
306                                         if (rk3036_tve->soctype == SOC_RK322X)
307                                                 clk_disable_unprepare(rk3036_tve->dac_clk);
308                                 }
309                         }
310                         break;
311                 }
312         } else if (action == FB_EVENT_BLANK) {
313                 switch (blank_mode) {
314                 case FB_BLANK_UNBLANK:
315                         TVEDBG("resume tve\n");
316                         mutex_lock(&rk3036_tve->tve_lock);
317                         if (rk3036_tve->suspend) {
318                                 if (rk3036_tve->soctype == SOC_RK322X) {
319                                         clk_prepare_enable(rk3036_tve->dac_clk);
320                                         rk322x_dac_init();
321                                 }
322                                 rk3036_tve->suspend = 0;
323                                 if (rk3036_tve->enable) {
324                                         tve_switch_fb(rk3036_tve->mode, 1);
325                                         dac_enable(true);
326                                 }
327                         }
328                         mutex_unlock(&rk3036_tve->tve_lock);
329                         break;
330                 default:
331                         break;
332                 }
333         }
334         return NOTIFY_OK;
335 }
336
337 static struct notifier_block tve_fb_notifier = {
338         .notifier_call = tve_fb_event_notify,
339 };
340
341 static struct rk_display_ops cvbs_display_ops = {
342         .setenable = cvbs_set_enable,
343         .getenable = cvbs_get_enable,
344         .getstatus = cvbs_get_status,
345         .getmodelist = cvbs_get_modelist,
346         .setmode = cvbs_set_mode,
347         .getmode = cvbs_get_mode,
348 };
349
350 static int
351 display_cvbs_probe(struct rk_display_device *device, void *devdata)
352 {
353         device->owner = THIS_MODULE;
354         strcpy(device->type, "TV");
355         device->name = "cvbs";
356         device->priority = DISPLAY_PRIORITY_TV;
357         device->priv_data = devdata;
358         device->ops = &cvbs_display_ops;
359         return 1;
360 }
361
362 static struct rk_display_driver display_cvbs = {
363         .probe = display_cvbs_probe,
364 };
365
366 #if defined(CONFIG_OF)
367 static const struct of_device_id rk3036_tve_dt_ids[] = {
368         {.compatible = "rockchip,rk3036-tve",},
369         {.compatible = "rockchip,rk312x-tve",},
370         {.compatible = "rockchip,rk322x-tve",},
371         {}
372 };
373 #endif
374
375 static int __init bootloader_tve_setup(char *str)
376 {
377         static int ret;
378
379         if (str) {
380                 pr_info("cvbs init tve.format is %s\n", str);
381                 ret = sscanf(str, "%d", &cvbsformat);
382         }
383
384         return 0;
385 }
386
387 early_param("tve.format", bootloader_tve_setup);
388
389 static int rk3036_tve_parse_dt(struct device_node *np,
390                                struct rk3036_tve *rk3036_tve)
391 {
392         int ret;
393         u32 val;
394
395         if (rk3036_tve->soctype == SOC_RK312X) {
396                 ret = of_property_read_u32(np, "test_mode", &val);
397                 if (ret < 0)
398                         goto errer;
399                 else
400                         rk3036_tve->test_mode = val;
401         }
402
403         ret = of_property_read_u32(np, "saturation", &val);
404         if ((val == 0) || (ret < 0))
405                 goto errer;
406         else
407                 rk3036_tve->saturation = val;
408
409         ret = of_property_read_u32(np, "brightcontrast", &val);
410         if ((val == 0) || (ret < 0))
411                 goto errer;
412         else
413                 rk3036_tve->brightcontrast = val;
414
415         ret = of_property_read_u32(np, "adjtiming", &val);
416         if ((val == 0) || (ret < 0))
417                 goto errer;
418         else
419                 rk3036_tve->adjtiming = val;
420
421         ret = of_property_read_u32(np, "lumafilter0", &val);
422         if ((val == 0) || (ret < 0))
423                 goto errer;
424         else
425                 rk3036_tve->lumafilter0 = val;
426
427         ret = of_property_read_u32(np, "lumafilter1", &val);
428         if ((val == 0) || (ret < 0))
429                 goto errer;
430         else
431                 rk3036_tve->lumafilter1 = val;
432
433         ret = of_property_read_u32(np, "lumafilter2", &val);
434         if ((val == 0) || (ret < 0))
435                 goto errer;
436         else
437                 rk3036_tve->lumafilter2 = val;
438
439         ret = of_property_read_u32(np, "daclevel", &val);
440         if ((val == 0) || (ret < 0))
441                 goto errer;
442         else
443                 rk3036_tve->daclevel = val;
444
445         return 0;
446 errer:
447         return -1;
448 }
449
450 static int rk3036_tve_probe(struct platform_device *pdev)
451 {
452         struct device_node *np = pdev->dev.of_node;
453         struct resource *res;
454         const struct of_device_id *match;
455         int i;
456         int ret;
457
458         match = of_match_node(rk3036_tve_dt_ids, np);
459         if (!match)
460                 return PTR_ERR(match);
461
462         rk3036_tve = devm_kzalloc(&pdev->dev,
463                                   sizeof(struct rk3036_tve), GFP_KERNEL);
464         if (!rk3036_tve) {
465                 dev_err(&pdev->dev, "rk3036 tv encoder device kmalloc fail!");
466                 return -ENOMEM;
467         }
468
469         if (!strcmp(match->compatible, "rockchip,rk3036-tve")) {
470                 rk3036_tve->soctype = SOC_RK3036;
471                 rk3036_tve->inputformat = INPUT_FORMAT_RGB;
472         } else if (!strcmp(match->compatible, "rockchip,rk312x-tve")) {
473                 rk3036_tve->soctype = SOC_RK312X;
474                 rk3036_tve->inputformat = INPUT_FORMAT_YUV;
475         } else if (!strcmp(match->compatible, "rockchip,rk322x-tve")) {
476                 rk3036_tve->soctype = SOC_RK322X;
477                 rk3036_tve->inputformat = INPUT_FORMAT_YUV;
478         } else {
479                 dev_err(&pdev->dev, "It is not a valid tv encoder!");
480                 return -ENOMEM;
481         }
482
483         ret = rk3036_tve_parse_dt(np, rk3036_tve);
484         if (ret) {
485                 dev_err(&pdev->dev, "TVE parse dts error!");
486                 return -EINVAL;
487         }
488
489         platform_set_drvdata(pdev, rk3036_tve);
490         rk3036_tve->dev = &pdev->dev;
491         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
492         rk3036_tve->reg_phy_base = res->start;
493         rk3036_tve->len = resource_size(res);
494         rk3036_tve->regbase = ioremap(res->start, rk3036_tve->len);
495         if (IS_ERR(rk3036_tve->regbase)) {
496                 dev_err(&pdev->dev,
497                         "rk3036 tv encoder device map registers failed!");
498                 return PTR_ERR(rk3036_tve->regbase);
499         }
500         if (rk3036_tve->soctype == SOC_RK322X) {
501                 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
502                 rk3036_tve->len = resource_size(res);
503                 rk3036_tve->vdacbase = devm_ioremap(rk3036_tve->dev,
504                                                     res->start,
505                                                     rk3036_tve->len);
506                 if (IS_ERR(rk3036_tve->vdacbase)) {
507                         dev_err(&pdev->dev,
508                                 "rk3036 tv encoder device dac map registers failed!");
509                         return PTR_ERR(rk3036_tve->vdacbase);
510                 }
511                 rk3036_tve->dac_clk =
512                                 devm_clk_get(rk3036_tve->dev, "pclk_vdac");
513                 if (IS_ERR(rk3036_tve->dac_clk)) {
514                         dev_err(&pdev->dev,
515                                 "Unable to get vdac_clk\n");
516                         return PTR_ERR(rk3036_tve->dac_clk);
517                 }
518                 clk_prepare_enable(rk3036_tve->dac_clk);
519                 if (cvbsformat < 0)
520                         rk322x_dac_init();
521         }
522         mutex_init(&rk3036_tve->tve_lock);
523         INIT_LIST_HEAD(&rk3036_tve->modelist);
524         for (i = 0; i < ARRAY_SIZE(rk3036_cvbs_mode); i++)
525                 fb_add_videomode(&rk3036_cvbs_mode[i], &rk3036_tve->modelist);
526          if (cvbsformat >= 0) {
527                 rk3036_tve->mode =
528                         (struct fb_videomode *)&rk3036_cvbs_mode[cvbsformat];
529                 rk3036_tve->enable = 1;
530                 tve_switch_fb(rk3036_tve->mode, 1);
531         } else {
532                 rk3036_tve->mode = (struct fb_videomode *)&rk3036_cvbs_mode[1];
533         }
534         rk3036_tve->ddev =
535                 rk_display_device_register(&display_cvbs, &pdev->dev, NULL);
536         rk_display_device_enable(rk3036_tve->ddev);
537
538         fb_register_client(&tve_fb_notifier);
539         cvbsformat = -1;
540         dev_info(&pdev->dev, "%s tv encoder probe ok\n", match->compatible);
541         return 0;
542 }
543
544 static void rk3036_tve_shutdown(struct platform_device *pdev)
545 {
546 }
547
548 static struct platform_driver rk3036_tve_driver = {
549         .probe = rk3036_tve_probe,
550         .remove = NULL,
551         .driver = {
552                 .name = "rk3036-tve",
553                 .owner = THIS_MODULE,
554                 .of_match_table = of_match_ptr(rk3036_tve_dt_ids),
555         },
556         .shutdown = rk3036_tve_shutdown,
557 };
558
559 static int __init rk3036_tve_init(void)
560 {
561         return platform_driver_register(&rk3036_tve_driver);
562 }
563
564 static void __exit rk3036_tve_exit(void)
565 {
566         platform_driver_unregister(&rk3036_tve_driver);
567 }
568
569 module_init(rk3036_tve_init);
570 module_exit(rk3036_tve_exit);
571
572 /* Module information */
573 MODULE_DESCRIPTION("ROCKCHIP RK3036 TV Encoder ");
574 MODULE_LICENSE("GPL");