1 /* 2 * (C) Copyright 2008-2017 Fuzhou Rockchip Electronics Co., Ltd 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <config.h> 8 #include <common.h> 9 #include <errno.h> 10 #include <dm/device.h> 11 #include <dm/read.h> 12 #include <dm/ofnode.h> 13 #include <syscon.h> 14 #include <regmap.h> 15 #include <dm/device.h> 16 #include <dm/read.h> 17 #include <linux/media-bus-format.h> 18 19 #include "rockchip_display.h" 20 #include "rockchip_connector.h" 21 #include "rockchip_phy.h" 22 #include "rockchip_panel.h" 23 24 #define HIWORD_UPDATE(v, h, l) (((v) << (l)) | (GENMASK(h, l) << 16)) 25 26 #define PX30_GRF_PD_VO_CON1 0x0438 27 #define PX30_LVDS_SELECT(x) HIWORD_UPDATE(x, 14, 13) 28 #define PX30_LVDS_MODE_EN(x) HIWORD_UPDATE(x, 12, 12) 29 #define PX30_LVDS_MSBSEL(x) HIWORD_UPDATE(x, 11, 11) 30 #define PX30_LVDS_P2S_EN(x) HIWORD_UPDATE(x, 6, 6) 31 #define PX30_LVDS_VOP_SEL(x) HIWORD_UPDATE(x, 1, 1) 32 33 #define RK3126_GRF_LVDS_CON0 0x0150 34 #define RK3126_LVDS_P2S_EN(x) HIWORD_UPDATE(x, 9, 9) 35 #define RK3126_LVDS_MODE_EN(x) HIWORD_UPDATE(x, 6, 6) 36 #define RK3126_LVDS_MSBSEL(x) HIWORD_UPDATE(x, 3, 3) 37 #define RK3126_LVDS_SELECT(x) HIWORD_UPDATE(x, 2, 1) 38 39 #define RK3288_GRF_SOC_CON6 0x025c 40 #define RK3288_LVDS_LCDC_SEL(x) HIWORD_UPDATE(x, 3, 3) 41 #define RK3288_GRF_SOC_CON7 0x0260 42 #define RK3288_LVDS_PWRDWN(x) HIWORD_UPDATE(x, 15, 15) 43 #define RK3288_LVDS_CON_ENABLE_2(x) HIWORD_UPDATE(x, 12, 12) 44 #define RK3288_LVDS_CON_ENABLE_1(x) HIWORD_UPDATE(x, 11, 11) 45 #define RK3288_LVDS_CON_DEN_POL(x) HIWORD_UPDATE(x, 10, 10) 46 #define RK3288_LVDS_CON_HS_POL(x) HIWORD_UPDATE(x, 9, 9) 47 #define RK3288_LVDS_CON_CLKINV(x) HIWORD_UPDATE(x, 8, 8) 48 #define RK3288_LVDS_CON_STARTPHASE(x) HIWORD_UPDATE(x, 7, 7) 49 #define RK3288_LVDS_CON_TTL_EN(x) HIWORD_UPDATE(x, 6, 6) 50 #define RK3288_LVDS_CON_STARTSEL(x) HIWORD_UPDATE(x, 5, 5) 51 #define RK3288_LVDS_CON_CHASEL(x) HIWORD_UPDATE(x, 4, 4) 52 #define RK3288_LVDS_CON_MSBSEL(x) HIWORD_UPDATE(x, 3, 3) 53 #define RK3288_LVDS_CON_SELECT(x) HIWORD_UPDATE(x, 2, 0) 54 55 #define RK3368_GRF_SOC_CON7 0x041c 56 #define RK3368_LVDS_SELECT(x) HIWORD_UPDATE(x, 14, 13) 57 #define RK3368_LVDS_MODE_EN(x) HIWORD_UPDATE(x, 12, 12) 58 #define RK3368_LVDS_MSBSEL(x) HIWORD_UPDATE(x, 11, 11) 59 #define RK3368_LVDS_P2S_EN(x) HIWORD_UPDATE(x, 6, 6) 60 61 enum lvds_format { 62 LVDS_8BIT_MODE_FORMAT_1, 63 LVDS_8BIT_MODE_FORMAT_2, 64 LVDS_8BIT_MODE_FORMAT_3, 65 LVDS_6BIT_MODE, 66 }; 67 68 struct rockchip_lvds; 69 70 struct rockchip_lvds_funcs { 71 void (*enable)(struct rockchip_lvds *lvds, int pipe); 72 void (*disable)(struct rockchip_lvds *lvds); 73 }; 74 75 struct rockchip_lvds { 76 struct udevice *dev; 77 struct regmap *grf; 78 struct rockchip_phy *phy; 79 const struct drm_display_mode *mode; 80 const struct rockchip_lvds_funcs *funcs; 81 enum lvds_format format; 82 bool data_swap; 83 bool dual_channel; 84 }; 85 86 static inline struct rockchip_lvds *state_to_lvds(struct display_state *state) 87 { 88 struct connector_state *conn_state = &state->conn_state; 89 90 return dev_get_priv(conn_state->dev); 91 } 92 93 static int rockchip_lvds_connector_init(struct display_state *state) 94 { 95 struct rockchip_lvds *lvds = state_to_lvds(state); 96 struct connector_state *conn_state = &state->conn_state; 97 struct rockchip_panel *panel = state_get_panel(state); 98 99 lvds->mode = &conn_state->mode; 100 lvds->phy = conn_state->phy; 101 102 switch (panel->bus_format) { 103 case MEDIA_BUS_FMT_RGB666_1X7X3_JEIDA: /* jeida-18 */ 104 lvds->format = LVDS_6BIT_MODE; 105 break; 106 case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA: /* jeida-24 */ 107 lvds->format = LVDS_8BIT_MODE_FORMAT_2; 108 break; 109 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG: /* vesa-18 */ 110 lvds->format = LVDS_8BIT_MODE_FORMAT_3; 111 break; 112 case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG: /* vesa-24 */ 113 default: 114 lvds->format = LVDS_8BIT_MODE_FORMAT_1; 115 break; 116 } 117 118 conn_state->type = DRM_MODE_CONNECTOR_LVDS; 119 conn_state->output_mode = ROCKCHIP_OUT_MODE_P888; 120 conn_state->color_space = V4L2_COLORSPACE_DEFAULT; 121 122 return 0; 123 } 124 125 static int rockchip_lvds_connector_enable(struct display_state *state) 126 { 127 struct rockchip_lvds *lvds = state_to_lvds(state); 128 struct crtc_state *crtc_state = &state->crtc_state; 129 int pipe = crtc_state->crtc_id; 130 int ret; 131 132 if (lvds->funcs->enable) 133 lvds->funcs->enable(lvds, pipe); 134 135 ret = rockchip_phy_set_mode(lvds->phy, PHY_MODE_VIDEO_LVDS); 136 if (ret) { 137 dev_err(lvds->dev, "failed to set phy mode: %d\n", ret); 138 return ret; 139 } 140 141 rockchip_phy_power_on(lvds->phy); 142 143 return 0; 144 } 145 146 static int rockchip_lvds_connector_disable(struct display_state *state) 147 { 148 struct rockchip_lvds *lvds = state_to_lvds(state); 149 150 rockchip_phy_power_off(lvds->phy); 151 152 if (lvds->funcs->disable) 153 lvds->funcs->disable(lvds); 154 155 return 0; 156 } 157 158 static const struct rockchip_connector_funcs rockchip_lvds_connector_funcs = { 159 .init = rockchip_lvds_connector_init, 160 .enable = rockchip_lvds_connector_enable, 161 .disable = rockchip_lvds_connector_disable, 162 }; 163 164 static int rockchip_lvds_probe(struct udevice *dev) 165 { 166 struct rockchip_lvds *lvds = dev_get_priv(dev); 167 const struct rockchip_connector *connector = 168 (const struct rockchip_connector *)dev_get_driver_data(dev); 169 170 lvds->dev = dev; 171 lvds->funcs = connector->data; 172 lvds->grf = syscon_get_regmap(dev_get_parent(dev)); 173 lvds->dual_channel = dev_read_bool(dev, "dual-channel"); 174 lvds->data_swap = dev_read_bool(dev, "rockchip,data-swap"); 175 176 return 0; 177 } 178 179 static void px30_lvds_enable(struct rockchip_lvds *lvds, int pipe) 180 { 181 regmap_write(lvds->grf, PX30_GRF_PD_VO_CON1, 182 PX30_LVDS_SELECT(lvds->format) | 183 PX30_LVDS_MODE_EN(1) | PX30_LVDS_MSBSEL(1) | 184 PX30_LVDS_P2S_EN(1) | PX30_LVDS_VOP_SEL(pipe)); 185 } 186 187 static void px30_lvds_disable(struct rockchip_lvds *lvds) 188 { 189 regmap_write(lvds->grf, PX30_GRF_PD_VO_CON1, 190 PX30_LVDS_MODE_EN(0) | PX30_LVDS_P2S_EN(0)); 191 } 192 193 static const struct rockchip_lvds_funcs px30_lvds_funcs = { 194 .enable = px30_lvds_enable, 195 .disable = px30_lvds_disable, 196 }; 197 198 static const struct rockchip_connector px30_lvds_driver_data = { 199 .funcs = &rockchip_lvds_connector_funcs, 200 .data = &px30_lvds_funcs, 201 }; 202 203 static void rk3126_lvds_enable(struct rockchip_lvds *lvds, int pipe) 204 { 205 regmap_write(lvds->grf, RK3126_GRF_LVDS_CON0, 206 RK3126_LVDS_P2S_EN(1) | RK3126_LVDS_MODE_EN(1) | 207 RK3126_LVDS_MSBSEL(1) | RK3126_LVDS_SELECT(lvds->format)); 208 } 209 210 static void rk3126_lvds_disable(struct rockchip_lvds *lvds) 211 { 212 regmap_write(lvds->grf, RK3126_GRF_LVDS_CON0, 213 RK3126_LVDS_P2S_EN(0) | RK3126_LVDS_MODE_EN(0)); 214 } 215 216 static const struct rockchip_lvds_funcs rk3126_lvds_funcs = { 217 .enable = rk3126_lvds_enable, 218 .disable = rk3126_lvds_disable, 219 }; 220 221 static const struct rockchip_connector rk3126_lvds_driver_data = { 222 .funcs = &rockchip_lvds_connector_funcs, 223 .data = &rk3126_lvds_funcs, 224 }; 225 226 static void rk3288_lvds_enable(struct rockchip_lvds *lvds, int pipe) 227 { 228 const struct drm_display_mode *mode = lvds->mode; 229 u32 val; 230 231 regmap_write(lvds->grf, RK3288_GRF_SOC_CON6, 232 RK3288_LVDS_LCDC_SEL(pipe)); 233 234 val = RK3288_LVDS_PWRDWN(0) | RK3288_LVDS_CON_CLKINV(0) | 235 RK3288_LVDS_CON_CHASEL(lvds->dual_channel) | 236 RK3288_LVDS_CON_SELECT(lvds->format); 237 238 if (lvds->dual_channel) { 239 u32 h_bp = mode->htotal - mode->hsync_start; 240 241 val |= RK3288_LVDS_CON_ENABLE_2(1) | 242 RK3288_LVDS_CON_ENABLE_1(1) | 243 RK3288_LVDS_CON_STARTSEL(lvds->data_swap); 244 245 if (h_bp % 2) 246 val |= RK3288_LVDS_CON_STARTPHASE(1); 247 else 248 val |= RK3288_LVDS_CON_STARTPHASE(0); 249 } else { 250 val |= RK3288_LVDS_CON_ENABLE_2(0) | 251 RK3288_LVDS_CON_ENABLE_1(1); 252 } 253 254 regmap_write(lvds->grf, RK3288_GRF_SOC_CON7, val); 255 256 rockchip_phy_set_bus_width(lvds->phy, lvds->dual_channel ? 2 : 1); 257 } 258 259 static void rk3288_lvds_disable(struct rockchip_lvds *lvds) 260 { 261 regmap_write(lvds->grf, RK3288_GRF_SOC_CON7, RK3288_LVDS_PWRDWN(1)); 262 } 263 264 static const struct rockchip_lvds_funcs rk3288_lvds_funcs = { 265 .enable = rk3288_lvds_enable, 266 .disable = rk3288_lvds_disable, 267 }; 268 269 static const struct rockchip_connector rk3288_lvds_driver_data = { 270 .funcs = &rockchip_lvds_connector_funcs, 271 .data = &rk3288_lvds_funcs, 272 }; 273 274 static void rk3368_lvds_enable(struct rockchip_lvds *lvds, int pipe) 275 { 276 regmap_write(lvds->grf, RK3368_GRF_SOC_CON7, 277 RK3368_LVDS_SELECT(lvds->format) | 278 RK3368_LVDS_MODE_EN(1) | RK3368_LVDS_MSBSEL(1) | 279 RK3368_LVDS_P2S_EN(1)); 280 } 281 282 static void rk3368_lvds_disable(struct rockchip_lvds *lvds) 283 { 284 regmap_write(lvds->grf, RK3368_GRF_SOC_CON7, 285 RK3368_LVDS_MODE_EN(0) | RK3368_LVDS_P2S_EN(0)); 286 } 287 288 static const struct rockchip_lvds_funcs rk3368_lvds_funcs = { 289 .enable = rk3368_lvds_enable, 290 .disable = rk3368_lvds_disable, 291 }; 292 293 static const struct rockchip_connector rk3368_lvds_driver_data = { 294 .funcs = &rockchip_lvds_connector_funcs, 295 .data = &rk3368_lvds_funcs, 296 }; 297 298 static const struct udevice_id rockchip_lvds_ids[] = { 299 { 300 .compatible = "rockchip,px30-lvds", 301 .data = (ulong)&px30_lvds_driver_data, 302 }, 303 { 304 .compatible = "rockchip,rk3126-lvds", 305 .data = (ulong)&rk3126_lvds_driver_data, 306 }, 307 { 308 .compatible = "rockchip,rk3288-lvds", 309 .data = (ulong)&rk3288_lvds_driver_data, 310 }, 311 { 312 .compatible = "rockchip,rk3368-lvds", 313 .data = (ulong)&rk3368_lvds_driver_data, 314 }, 315 {} 316 }; 317 318 U_BOOT_DRIVER(rockchip_lvds) = { 319 .name = "rockchip_lvds", 320 .id = UCLASS_DISPLAY, 321 .of_match = rockchip_lvds_ids, 322 .probe = rockchip_lvds_probe, 323 .priv_auto_alloc_size = sizeof(struct rockchip_lvds), 324 }; 325