video: fsl_dcu_fb: refactor init functions
[oweals/u-boot.git] / board / freescale / ls1021aqds / dcu.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2014 Freescale Semiconductor, Inc.
4  *
5  * FSL DCU Framebuffer driver
6  */
7
8 #include <asm/io.h>
9 #include <common.h>
10 #include <fsl_dcu_fb.h>
11 #include <i2c.h>
12 #include "div64.h"
13 #include "../common/diu_ch7301.h"
14 #include "ls1021aqds_qixis.h"
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 static int select_i2c_ch_pca9547(u8 ch)
19 {
20         int ret;
21
22         ret = i2c_write(I2C_MUX_PCA_ADDR_PRI, 0, 1, &ch, 1);
23         if (ret) {
24                 puts("PCA: failed to select proper channel\n");
25                 return ret;
26         }
27
28         return 0;
29 }
30
31 unsigned int dcu_set_pixel_clock(unsigned int pixclock)
32 {
33         unsigned long long div;
34
35         div = (unsigned long long)(gd->bus_clk / 1000);
36         div *= (unsigned long long)pixclock;
37         do_div(div, 1000000000);
38
39         return div;
40 }
41
42 int platform_dcu_init(struct fb_info *fbinfo,
43                       unsigned int xres,
44                       unsigned int yres,
45                       const char *port,
46                       struct fb_videomode *dcu_fb_videomode)
47 {
48         const char *name;
49         unsigned int pixel_format;
50         int ret;
51         u8 ch;
52
53         /* Mux I2C3+I2C4 as HSYNC+VSYNC */
54         ret = i2c_read(CONFIG_SYS_I2C_QIXIS_ADDR, QIXIS_DCU_BRDCFG5,
55                        1, &ch, 1);
56         if (ret) {
57                 printf("Error: failed to read I2C @%02x\n",
58                        CONFIG_SYS_I2C_QIXIS_ADDR);
59                 return ret;
60         }
61         ch &= 0x1F;
62         ch |= 0xA0;
63         ret = i2c_write(CONFIG_SYS_I2C_QIXIS_ADDR, QIXIS_DCU_BRDCFG5,
64                         1, &ch, 1);
65         if (ret) {
66                 printf("Error: failed to write I2C @%02x\n",
67                        CONFIG_SYS_I2C_QIXIS_ADDR);
68                 return ret;
69         }
70
71         if (strncmp(port, "hdmi", 4) == 0) {
72                 unsigned long pixval;
73
74                 name = "HDMI";
75
76                 pixval = 1000000000 / dcu_fb_videomode->pixclock;
77                 pixval *= 1000;
78
79                 i2c_set_bus_num(CONFIG_SYS_I2C_DVI_BUS_NUM);
80                 select_i2c_ch_pca9547(I2C_MUX_CH_CH7301);
81                 diu_set_dvi_encoder(pixval);
82                 select_i2c_ch_pca9547(I2C_MUX_CH_DEFAULT);
83         } else {
84                 return 0;
85         }
86
87         printf("DCU: Switching to %s monitor @ %ux%u\n", name, xres, yres);
88
89         pixel_format = 32;
90         fsl_dcu_init(fbinfo, xres, yres, pixel_format);
91
92         return 0;
93 }