4 * Driver for rockchip rk3036 tv encoder control
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.
14 #include <linux/clk.h>
15 #include <linux/display-sys.h>
16 #include <linux/device.h>
17 #include <linux/delay.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"
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},
35 static struct rk3036_tve *rk3036_tve;
37 static int cvbsformat = -1;
39 #define tve_writel(offset, v) writel_relaxed(v, rk3036_tve->regbase + offset)
40 #define tve_readl(offset) readl_relaxed(rk3036_tve->regbase + offset)
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)
46 #define TVEDBG(format, ...) \
47 dev_info(rk3036_tve->dev,\
48 "RK3036 TVE: " format "\n", ## __VA_ARGS__)
50 #define TVEDBG(format, ...)
53 static void dac_enable(bool enable)
58 TVEDBG("%s enable %d\n", __func__, 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) {
74 mask = m_VBG_EN | m_DAC_EN;
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;
84 grf_writel(grfreg, (mask << 16) | val);
85 else if (rk3036_tve->vdacbase)
86 tve_dac_writel(VDAC_VDAC1, val);
89 static void rk322x_dac_init(void)
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));
98 static void tve_set_mode(int mode)
100 TVEDBG("%s mode %d\n", __func__, mode);
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));
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));
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));
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);
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);
131 tve_writel(TV_BRIGHTNESS_CONTRAST, 0x00007900);
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));
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));
146 tve_writel(TV_SATURATION, rk3036_tve->saturation);
147 tve_writel(TV_BRIGHTNESS_CONTRAST, rk3036_tve->brightcontrast);
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));
155 tve_writel(TV_ADJ_TIMING, rk3036_tve->adjtiming);
156 tve_writel(TV_ACT_TIMING, 0x0694011D |
157 (1 << 12) | (2 << 28));
161 static int tve_switch_fb(const struct fb_videomode *modedb, int enable)
163 struct rk_screen *screen = &rk3036_tve->screen;
168 memset(screen, 0, sizeof(struct rk_screen));
169 /* screen type & face */
170 if (rk3036_tve->test_mode)
171 screen->type = SCREEN_TVOUT_TEST;
173 screen->type = SCREEN_TVOUT;
174 screen->face = OUT_P888;
175 screen->color_mode = COLOR_YCBCR;
176 screen->mode = *modedb;
179 if (FB_SYNC_HOR_HIGH_ACT & modedb->sync)
180 screen->pin_hsync = 1;
182 screen->pin_hsync = 0;
183 if (FB_SYNC_VERT_HIGH_ACT & modedb->sync)
184 screen->pin_vsync = 1;
186 screen->pin_vsync = 0;
189 screen->pin_dclk = 0;
190 screen->pixelrepeat = 1;
196 screen->swap_delta = 0;
197 screen->swap_dumy = 0;
199 /* Operation function*/
201 screen->standby = NULL;
202 rk_fb_switch_screen(screen, enable, 0);
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);
210 tve_set_mode(TVOUT_CVBS_PAL);
215 static int cvbs_set_enable(struct rk_display_device *device, int enable)
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);
229 tve_switch_fb(rk3036_tve->mode, 0);
230 } else if (enable == 1) {
231 tve_switch_fb(rk3036_tve->mode, 1);
235 mutex_unlock(&rk3036_tve->tve_lock);
239 static int cvbs_get_enable(struct rk_display_device *device)
241 TVEDBG("%s enable %d\n", __func__, rk3036_tve->enable);
242 return rk3036_tve->enable;
245 static int cvbs_get_status(struct rk_display_device *device)
251 cvbs_get_modelist(struct rk_display_device *device, struct list_head **modelist)
253 *modelist = &rk3036_tve->modelist;
258 cvbs_set_mode(struct rk_display_device *device, struct fb_videomode *mode)
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]) {
266 (struct fb_videomode *)&rk3036_cvbs_mode[i];
267 if (rk3036_tve->enable && !rk3036_tve->suspend) {
270 tve_switch_fb(rk3036_tve->mode, 1);
277 TVEDBG("%s\n", __func__);
282 cvbs_get_mode(struct rk_display_device *device, struct fb_videomode *mode)
284 *mode = *rk3036_tve->mode;
289 tve_fb_event_notify(struct notifier_block *self,
290 unsigned long action, void *data)
292 struct fb_event *event = data;
293 int blank_mode = *((int *)event->data);
295 if (action == FB_EARLY_EVENT_BLANK) {
296 switch (blank_mode) {
297 case FB_BLANK_UNBLANK:
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);
306 if (rk3036_tve->soctype == SOC_RK322X)
307 clk_disable_unprepare(rk3036_tve->dac_clk);
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);
322 rk3036_tve->suspend = 0;
323 if (rk3036_tve->enable) {
324 tve_switch_fb(rk3036_tve->mode, 1);
328 mutex_unlock(&rk3036_tve->tve_lock);
337 static struct notifier_block tve_fb_notifier = {
338 .notifier_call = tve_fb_event_notify,
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,
351 display_cvbs_probe(struct rk_display_device *device, void *devdata)
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;
362 static struct rk_display_driver display_cvbs = {
363 .probe = display_cvbs_probe,
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",},
375 static int __init bootloader_tve_setup(char *str)
380 pr_info("cvbs init tve.format is %s\n", str);
381 ret = sscanf(str, "%d", &cvbsformat);
387 early_param("tve.format", bootloader_tve_setup);
389 static int rk3036_tve_parse_dt(struct device_node *np,
390 struct rk3036_tve *rk3036_tve)
395 if (rk3036_tve->soctype == SOC_RK312X) {
396 ret = of_property_read_u32(np, "test_mode", &val);
400 rk3036_tve->test_mode = val;
403 ret = of_property_read_u32(np, "saturation", &val);
404 if ((val == 0) || (ret < 0))
407 rk3036_tve->saturation = val;
409 ret = of_property_read_u32(np, "brightcontrast", &val);
410 if ((val == 0) || (ret < 0))
413 rk3036_tve->brightcontrast = val;
415 ret = of_property_read_u32(np, "adjtiming", &val);
416 if ((val == 0) || (ret < 0))
419 rk3036_tve->adjtiming = val;
421 ret = of_property_read_u32(np, "lumafilter0", &val);
422 if ((val == 0) || (ret < 0))
425 rk3036_tve->lumafilter0 = val;
427 ret = of_property_read_u32(np, "lumafilter1", &val);
428 if ((val == 0) || (ret < 0))
431 rk3036_tve->lumafilter1 = val;
433 ret = of_property_read_u32(np, "lumafilter2", &val);
434 if ((val == 0) || (ret < 0))
437 rk3036_tve->lumafilter2 = val;
439 ret = of_property_read_u32(np, "daclevel", &val);
440 if ((val == 0) || (ret < 0))
443 rk3036_tve->daclevel = val;
450 static int rk3036_tve_probe(struct platform_device *pdev)
452 struct device_node *np = pdev->dev.of_node;
453 struct resource *res;
454 const struct of_device_id *match;
458 match = of_match_node(rk3036_tve_dt_ids, np);
460 return PTR_ERR(match);
462 rk3036_tve = devm_kzalloc(&pdev->dev,
463 sizeof(struct rk3036_tve), GFP_KERNEL);
465 dev_err(&pdev->dev, "rk3036 tv encoder device kmalloc fail!");
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;
479 dev_err(&pdev->dev, "It is not a valid tv encoder!");
483 ret = rk3036_tve_parse_dt(np, rk3036_tve);
485 dev_err(&pdev->dev, "TVE parse dts error!");
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)) {
497 "rk3036 tv encoder device map registers failed!");
498 return PTR_ERR(rk3036_tve->regbase);
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,
506 if (IS_ERR(rk3036_tve->vdacbase)) {
508 "rk3036 tv encoder device dac map registers failed!");
509 return PTR_ERR(rk3036_tve->vdacbase);
511 rk3036_tve->dac_clk =
512 devm_clk_get(rk3036_tve->dev, "pclk_vdac");
513 if (IS_ERR(rk3036_tve->dac_clk)) {
515 "Unable to get vdac_clk\n");
516 return PTR_ERR(rk3036_tve->dac_clk);
518 clk_prepare_enable(rk3036_tve->dac_clk);
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) {
528 (struct fb_videomode *)&rk3036_cvbs_mode[cvbsformat];
529 rk3036_tve->enable = 1;
530 tve_switch_fb(rk3036_tve->mode, 1);
532 rk3036_tve->mode = (struct fb_videomode *)&rk3036_cvbs_mode[1];
535 rk_display_device_register(&display_cvbs, &pdev->dev, NULL);
536 rk_display_device_enable(rk3036_tve->ddev);
538 fb_register_client(&tve_fb_notifier);
540 dev_info(&pdev->dev, "%s tv encoder probe ok\n", match->compatible);
544 static void rk3036_tve_shutdown(struct platform_device *pdev)
548 static struct platform_driver rk3036_tve_driver = {
549 .probe = rk3036_tve_probe,
552 .name = "rk3036-tve",
553 .owner = THIS_MODULE,
554 .of_match_table = of_match_ptr(rk3036_tve_dt_ids),
556 .shutdown = rk3036_tve_shutdown,
559 static int __init rk3036_tve_init(void)
561 return platform_driver_register(&rk3036_tve_driver);
564 static void __exit rk3036_tve_exit(void)
566 platform_driver_unregister(&rk3036_tve_driver);
569 module_init(rk3036_tve_init);
570 module_exit(rk3036_tve_exit);
572 /* Module information */
573 MODULE_DESCRIPTION("ROCKCHIP RK3036 TV Encoder ");
574 MODULE_LICENSE("GPL");