Shawn Guo | 098988c | 2017-01-19 22:28:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Linaro Ltd. |
| 3 | * Copyright 2017 ZTE Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/clk.h> |
| 12 | #include <linux/component.h> |
| 13 | #include <linux/mfd/syscon.h> |
| 14 | #include <linux/regmap.h> |
| 15 | |
| 16 | #include <drm/drm_atomic_helper.h> |
| 17 | #include <drm/drm_crtc_helper.h> |
| 18 | #include <drm/drmP.h> |
| 19 | |
| 20 | #include "zx_drm_drv.h" |
| 21 | #include "zx_tvenc_regs.h" |
| 22 | #include "zx_vou.h" |
| 23 | |
| 24 | struct zx_tvenc_pwrctrl { |
| 25 | struct regmap *regmap; |
| 26 | u32 reg; |
| 27 | u32 mask; |
| 28 | }; |
| 29 | |
| 30 | struct zx_tvenc { |
| 31 | struct drm_connector connector; |
| 32 | struct drm_encoder encoder; |
| 33 | struct device *dev; |
| 34 | void __iomem *mmio; |
| 35 | const struct vou_inf *inf; |
| 36 | struct zx_tvenc_pwrctrl pwrctrl; |
| 37 | }; |
| 38 | |
| 39 | #define to_zx_tvenc(x) container_of(x, struct zx_tvenc, x) |
| 40 | |
| 41 | struct zx_tvenc_mode { |
| 42 | struct drm_display_mode mode; |
| 43 | u32 video_info; |
| 44 | u32 video_res; |
| 45 | u32 field1_param; |
| 46 | u32 field2_param; |
| 47 | u32 burst_line_odd1; |
| 48 | u32 burst_line_even1; |
| 49 | u32 burst_line_odd2; |
| 50 | u32 burst_line_even2; |
| 51 | u32 line_timing_param; |
| 52 | u32 weight_value; |
| 53 | u32 blank_black_level; |
| 54 | u32 burst_level; |
| 55 | u32 control_param; |
| 56 | u32 sub_carrier_phase1; |
| 57 | u32 phase_line_incr_cvbs; |
| 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * The CRM cannot directly provide a suitable frequency, and we have to |
| 62 | * ask a multiplied rate from CRM and use the divider in VOU to get the |
| 63 | * desired one. |
| 64 | */ |
| 65 | #define TVENC_CLOCK_MULTIPLIER 4 |
| 66 | |
| 67 | static const struct zx_tvenc_mode tvenc_mode_pal = { |
| 68 | .mode = { |
| 69 | .clock = 13500 * TVENC_CLOCK_MULTIPLIER, |
| 70 | .hdisplay = 720, |
| 71 | .hsync_start = 720 + 12, |
| 72 | .hsync_end = 720 + 12 + 2, |
| 73 | .htotal = 720 + 12 + 2 + 130, |
| 74 | .vdisplay = 576, |
| 75 | .vsync_start = 576 + 2, |
| 76 | .vsync_end = 576 + 2 + 2, |
| 77 | .vtotal = 576 + 2 + 2 + 20, |
| 78 | .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC | |
| 79 | DRM_MODE_FLAG_INTERLACE, |
| 80 | }, |
| 81 | .video_info = 0x00040040, |
| 82 | .video_res = 0x05a9c760, |
| 83 | .field1_param = 0x0004d416, |
| 84 | .field2_param = 0x0009b94f, |
| 85 | .burst_line_odd1 = 0x0004d406, |
| 86 | .burst_line_even1 = 0x0009b53e, |
| 87 | .burst_line_odd2 = 0x0004d805, |
| 88 | .burst_line_even2 = 0x0009b93f, |
| 89 | .line_timing_param = 0x06a96fdf, |
| 90 | .weight_value = 0x00c188a0, |
| 91 | .blank_black_level = 0x0000fcfc, |
| 92 | .burst_level = 0x00001595, |
| 93 | .control_param = 0x00000001, |
| 94 | .sub_carrier_phase1 = 0x1504c566, |
| 95 | .phase_line_incr_cvbs = 0xc068db8c, |
| 96 | }; |
| 97 | |
| 98 | static const struct zx_tvenc_mode tvenc_mode_ntsc = { |
| 99 | .mode = { |
| 100 | .clock = 13500 * TVENC_CLOCK_MULTIPLIER, |
| 101 | .hdisplay = 720, |
| 102 | .hsync_start = 720 + 16, |
| 103 | .hsync_end = 720 + 16 + 2, |
| 104 | .htotal = 720 + 16 + 2 + 120, |
| 105 | .vdisplay = 480, |
| 106 | .vsync_start = 480 + 3, |
| 107 | .vsync_end = 480 + 3 + 2, |
| 108 | .vtotal = 480 + 3 + 2 + 17, |
| 109 | .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC | |
| 110 | DRM_MODE_FLAG_INTERLACE, |
| 111 | }, |
| 112 | .video_info = 0x00040080, |
| 113 | .video_res = 0x05a8375a, |
| 114 | .field1_param = 0x00041817, |
| 115 | .field2_param = 0x0008351e, |
| 116 | .burst_line_odd1 = 0x00041006, |
| 117 | .burst_line_even1 = 0x0008290d, |
| 118 | .burst_line_odd2 = 0x00000000, |
| 119 | .burst_line_even2 = 0x00000000, |
| 120 | .line_timing_param = 0x06a8ef9e, |
| 121 | .weight_value = 0x00b68197, |
| 122 | .blank_black_level = 0x0000f0f0, |
| 123 | .burst_level = 0x0000009c, |
| 124 | .control_param = 0x00000001, |
| 125 | .sub_carrier_phase1 = 0x10f83e10, |
| 126 | .phase_line_incr_cvbs = 0x80000000, |
| 127 | }; |
| 128 | |
| 129 | static const struct zx_tvenc_mode *tvenc_modes[] = { |
| 130 | &tvenc_mode_pal, |
| 131 | &tvenc_mode_ntsc, |
| 132 | }; |
| 133 | |
| 134 | static const struct zx_tvenc_mode * |
| 135 | zx_tvenc_find_zmode(struct drm_display_mode *mode) |
| 136 | { |
| 137 | int i; |
| 138 | |
| 139 | for (i = 0; i < ARRAY_SIZE(tvenc_modes); i++) { |
| 140 | const struct zx_tvenc_mode *zmode = tvenc_modes[i]; |
| 141 | |
| 142 | if (drm_mode_equal(mode, &zmode->mode)) |
| 143 | return zmode; |
| 144 | } |
| 145 | |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | static void zx_tvenc_encoder_mode_set(struct drm_encoder *encoder, |
| 150 | struct drm_display_mode *mode, |
| 151 | struct drm_display_mode *adj_mode) |
| 152 | { |
| 153 | struct zx_tvenc *tvenc = to_zx_tvenc(encoder); |
| 154 | const struct zx_tvenc_mode *zmode; |
| 155 | struct vou_div_config configs[] = { |
| 156 | { VOU_DIV_INF, VOU_DIV_4 }, |
| 157 | { VOU_DIV_TVENC, VOU_DIV_1 }, |
| 158 | { VOU_DIV_LAYER, VOU_DIV_2 }, |
| 159 | }; |
| 160 | |
| 161 | zx_vou_config_dividers(encoder->crtc, configs, ARRAY_SIZE(configs)); |
| 162 | |
| 163 | zmode = zx_tvenc_find_zmode(mode); |
| 164 | if (!zmode) { |
| 165 | DRM_DEV_ERROR(tvenc->dev, "failed to find zmode\n"); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | zx_writel(tvenc->mmio + VENC_VIDEO_INFO, zmode->video_info); |
| 170 | zx_writel(tvenc->mmio + VENC_VIDEO_RES, zmode->video_res); |
| 171 | zx_writel(tvenc->mmio + VENC_FIELD1_PARAM, zmode->field1_param); |
| 172 | zx_writel(tvenc->mmio + VENC_FIELD2_PARAM, zmode->field2_param); |
| 173 | zx_writel(tvenc->mmio + VENC_LINE_O_1, zmode->burst_line_odd1); |
| 174 | zx_writel(tvenc->mmio + VENC_LINE_E_1, zmode->burst_line_even1); |
| 175 | zx_writel(tvenc->mmio + VENC_LINE_O_2, zmode->burst_line_odd2); |
| 176 | zx_writel(tvenc->mmio + VENC_LINE_E_2, zmode->burst_line_even2); |
| 177 | zx_writel(tvenc->mmio + VENC_LINE_TIMING_PARAM, |
| 178 | zmode->line_timing_param); |
| 179 | zx_writel(tvenc->mmio + VENC_WEIGHT_VALUE, zmode->weight_value); |
| 180 | zx_writel(tvenc->mmio + VENC_BLANK_BLACK_LEVEL, |
| 181 | zmode->blank_black_level); |
| 182 | zx_writel(tvenc->mmio + VENC_BURST_LEVEL, zmode->burst_level); |
| 183 | zx_writel(tvenc->mmio + VENC_CONTROL_PARAM, zmode->control_param); |
| 184 | zx_writel(tvenc->mmio + VENC_SUB_CARRIER_PHASE1, |
| 185 | zmode->sub_carrier_phase1); |
| 186 | zx_writel(tvenc->mmio + VENC_PHASE_LINE_INCR_CVBS, |
| 187 | zmode->phase_line_incr_cvbs); |
| 188 | } |
| 189 | |
| 190 | static void zx_tvenc_encoder_enable(struct drm_encoder *encoder) |
| 191 | { |
| 192 | struct zx_tvenc *tvenc = to_zx_tvenc(encoder); |
| 193 | struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl; |
| 194 | |
| 195 | /* Set bit to power up TVENC DAC */ |
| 196 | regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask, |
| 197 | pwrctrl->mask); |
| 198 | |
| 199 | vou_inf_enable(VOU_TV_ENC, encoder->crtc); |
| 200 | |
| 201 | zx_writel(tvenc->mmio + VENC_ENABLE, 1); |
| 202 | } |
| 203 | |
| 204 | static void zx_tvenc_encoder_disable(struct drm_encoder *encoder) |
| 205 | { |
| 206 | struct zx_tvenc *tvenc = to_zx_tvenc(encoder); |
| 207 | struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl; |
| 208 | |
| 209 | zx_writel(tvenc->mmio + VENC_ENABLE, 0); |
| 210 | |
| 211 | vou_inf_disable(VOU_TV_ENC, encoder->crtc); |
| 212 | |
| 213 | /* Clear bit to power down TVENC DAC */ |
| 214 | regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask, 0); |
| 215 | } |
| 216 | |
| 217 | static const struct drm_encoder_helper_funcs zx_tvenc_encoder_helper_funcs = { |
| 218 | .enable = zx_tvenc_encoder_enable, |
| 219 | .disable = zx_tvenc_encoder_disable, |
| 220 | .mode_set = zx_tvenc_encoder_mode_set, |
| 221 | }; |
| 222 | |
| 223 | static const struct drm_encoder_funcs zx_tvenc_encoder_funcs = { |
| 224 | .destroy = drm_encoder_cleanup, |
| 225 | }; |
| 226 | |
| 227 | static int zx_tvenc_connector_get_modes(struct drm_connector *connector) |
| 228 | { |
| 229 | struct zx_tvenc *tvenc = to_zx_tvenc(connector); |
| 230 | struct device *dev = tvenc->dev; |
| 231 | int i; |
| 232 | |
| 233 | for (i = 0; i < ARRAY_SIZE(tvenc_modes); i++) { |
| 234 | const struct zx_tvenc_mode *zmode = tvenc_modes[i]; |
| 235 | struct drm_display_mode *mode; |
| 236 | |
| 237 | mode = drm_mode_duplicate(connector->dev, &zmode->mode); |
| 238 | if (!mode) { |
| 239 | DRM_DEV_ERROR(dev, "failed to duplicate drm mode\n"); |
| 240 | continue; |
| 241 | } |
| 242 | |
| 243 | drm_mode_set_name(mode); |
| 244 | drm_mode_probed_add(connector, mode); |
| 245 | } |
| 246 | |
| 247 | return i; |
| 248 | } |
| 249 | |
| 250 | static enum drm_mode_status |
| 251 | zx_tvenc_connector_mode_valid(struct drm_connector *connector, |
| 252 | struct drm_display_mode *mode) |
| 253 | { |
| 254 | struct zx_tvenc *tvenc = to_zx_tvenc(connector); |
| 255 | const struct zx_tvenc_mode *zmode; |
| 256 | |
| 257 | zmode = zx_tvenc_find_zmode(mode); |
| 258 | if (!zmode) { |
| 259 | DRM_DEV_ERROR(tvenc->dev, "unsupported mode: %s\n", mode->name); |
| 260 | return MODE_NOMODE; |
| 261 | } |
| 262 | |
| 263 | return MODE_OK; |
| 264 | } |
| 265 | |
| 266 | static struct drm_connector_helper_funcs zx_tvenc_connector_helper_funcs = { |
| 267 | .get_modes = zx_tvenc_connector_get_modes, |
| 268 | .mode_valid = zx_tvenc_connector_mode_valid, |
| 269 | }; |
| 270 | |
| 271 | static const struct drm_connector_funcs zx_tvenc_connector_funcs = { |
| 272 | .dpms = drm_atomic_helper_connector_dpms, |
| 273 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 274 | .destroy = drm_connector_cleanup, |
| 275 | .reset = drm_atomic_helper_connector_reset, |
| 276 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, |
| 277 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, |
| 278 | }; |
| 279 | |
| 280 | static int zx_tvenc_register(struct drm_device *drm, struct zx_tvenc *tvenc) |
| 281 | { |
| 282 | struct drm_encoder *encoder = &tvenc->encoder; |
| 283 | struct drm_connector *connector = &tvenc->connector; |
| 284 | |
| 285 | /* |
| 286 | * The tvenc is designed to use aux channel, as there is a deflicker |
| 287 | * block for the channel. |
| 288 | */ |
| 289 | encoder->possible_crtcs = BIT(1); |
| 290 | |
| 291 | drm_encoder_init(drm, encoder, &zx_tvenc_encoder_funcs, |
| 292 | DRM_MODE_ENCODER_TVDAC, NULL); |
| 293 | drm_encoder_helper_add(encoder, &zx_tvenc_encoder_helper_funcs); |
| 294 | |
| 295 | connector->interlace_allowed = true; |
| 296 | |
| 297 | drm_connector_init(drm, connector, &zx_tvenc_connector_funcs, |
| 298 | DRM_MODE_CONNECTOR_Composite); |
| 299 | drm_connector_helper_add(connector, &zx_tvenc_connector_helper_funcs); |
| 300 | |
| 301 | drm_mode_connector_attach_encoder(connector, encoder); |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static int zx_tvenc_pwrctrl_init(struct zx_tvenc *tvenc) |
| 307 | { |
| 308 | struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl; |
| 309 | struct device *dev = tvenc->dev; |
| 310 | struct of_phandle_args out_args; |
| 311 | struct regmap *regmap; |
| 312 | int ret; |
| 313 | |
| 314 | ret = of_parse_phandle_with_fixed_args(dev->of_node, |
| 315 | "zte,tvenc-power-control", 2, 0, &out_args); |
| 316 | if (ret) |
| 317 | return ret; |
| 318 | |
| 319 | regmap = syscon_node_to_regmap(out_args.np); |
| 320 | if (IS_ERR(regmap)) { |
| 321 | ret = PTR_ERR(regmap); |
| 322 | goto out; |
| 323 | } |
| 324 | |
| 325 | pwrctrl->regmap = regmap; |
| 326 | pwrctrl->reg = out_args.args[0]; |
| 327 | pwrctrl->mask = out_args.args[1]; |
| 328 | |
| 329 | out: |
| 330 | of_node_put(out_args.np); |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | static int zx_tvenc_bind(struct device *dev, struct device *master, void *data) |
| 335 | { |
| 336 | struct platform_device *pdev = to_platform_device(dev); |
| 337 | struct drm_device *drm = data; |
| 338 | struct resource *res; |
| 339 | struct zx_tvenc *tvenc; |
| 340 | int ret; |
| 341 | |
| 342 | tvenc = devm_kzalloc(dev, sizeof(*tvenc), GFP_KERNEL); |
| 343 | if (!tvenc) |
| 344 | return -ENOMEM; |
| 345 | |
| 346 | tvenc->dev = dev; |
| 347 | dev_set_drvdata(dev, tvenc); |
| 348 | |
| 349 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 350 | tvenc->mmio = devm_ioremap_resource(dev, res); |
| 351 | if (IS_ERR(tvenc->mmio)) { |
| 352 | ret = PTR_ERR(tvenc->mmio); |
| 353 | DRM_DEV_ERROR(dev, "failed to remap tvenc region: %d\n", ret); |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | ret = zx_tvenc_pwrctrl_init(tvenc); |
| 358 | if (ret) { |
| 359 | DRM_DEV_ERROR(dev, "failed to init power control: %d\n", ret); |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | ret = zx_tvenc_register(drm, tvenc); |
| 364 | if (ret) { |
| 365 | DRM_DEV_ERROR(dev, "failed to register tvenc: %d\n", ret); |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static void zx_tvenc_unbind(struct device *dev, struct device *master, |
| 373 | void *data) |
| 374 | { |
| 375 | /* Nothing to do */ |
| 376 | } |
| 377 | |
| 378 | static const struct component_ops zx_tvenc_component_ops = { |
| 379 | .bind = zx_tvenc_bind, |
| 380 | .unbind = zx_tvenc_unbind, |
| 381 | }; |
| 382 | |
| 383 | static int zx_tvenc_probe(struct platform_device *pdev) |
| 384 | { |
| 385 | return component_add(&pdev->dev, &zx_tvenc_component_ops); |
| 386 | } |
| 387 | |
| 388 | static int zx_tvenc_remove(struct platform_device *pdev) |
| 389 | { |
| 390 | component_del(&pdev->dev, &zx_tvenc_component_ops); |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static const struct of_device_id zx_tvenc_of_match[] = { |
| 395 | { .compatible = "zte,zx296718-tvenc", }, |
| 396 | { /* end */ }, |
| 397 | }; |
| 398 | MODULE_DEVICE_TABLE(of, zx_tvenc_of_match); |
| 399 | |
| 400 | struct platform_driver zx_tvenc_driver = { |
| 401 | .probe = zx_tvenc_probe, |
| 402 | .remove = zx_tvenc_remove, |
| 403 | .driver = { |
| 404 | .name = "zx-tvenc", |
| 405 | .of_match_table = zx_tvenc_of_match, |
| 406 | }, |
| 407 | }; |