2 * Copyright 2017 Linaro Ltd.
3 * Copyright 2017 ZTE Corporation.
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.
11 #include <linux/clk.h>
12 #include <linux/component.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_print.h>
20 #include <drm/drm_probe_helper.h>
22 #include "zx_drm_drv.h"
23 #include "zx_tvenc_regs.h"
26 struct zx_tvenc_pwrctrl {
27 struct regmap *regmap;
33 struct drm_connector connector;
34 struct drm_encoder encoder;
37 const struct vou_inf *inf;
38 struct zx_tvenc_pwrctrl pwrctrl;
41 #define to_zx_tvenc(x) container_of(x, struct zx_tvenc, x)
43 struct zx_tvenc_mode {
44 struct drm_display_mode mode;
53 u32 line_timing_param;
55 u32 blank_black_level;
58 u32 sub_carrier_phase1;
59 u32 phase_line_incr_cvbs;
63 * The CRM cannot directly provide a suitable frequency, and we have to
64 * ask a multiplied rate from CRM and use the divider in VOU to get the
67 #define TVENC_CLOCK_MULTIPLIER 4
69 static const struct zx_tvenc_mode tvenc_mode_pal = {
71 .clock = 13500 * TVENC_CLOCK_MULTIPLIER,
73 .hsync_start = 720 + 12,
74 .hsync_end = 720 + 12 + 2,
75 .htotal = 720 + 12 + 2 + 130,
77 .vsync_start = 576 + 2,
78 .vsync_end = 576 + 2 + 2,
79 .vtotal = 576 + 2 + 2 + 20,
80 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
81 DRM_MODE_FLAG_INTERLACE,
83 .video_info = 0x00040040,
84 .video_res = 0x05a9c760,
85 .field1_param = 0x0004d416,
86 .field2_param = 0x0009b94f,
87 .burst_line_odd1 = 0x0004d406,
88 .burst_line_even1 = 0x0009b53e,
89 .burst_line_odd2 = 0x0004d805,
90 .burst_line_even2 = 0x0009b93f,
91 .line_timing_param = 0x06a96fdf,
92 .weight_value = 0x00c188a0,
93 .blank_black_level = 0x0000fcfc,
94 .burst_level = 0x00001595,
95 .control_param = 0x00000001,
96 .sub_carrier_phase1 = 0x1504c566,
97 .phase_line_incr_cvbs = 0xc068db8c,
100 static const struct zx_tvenc_mode tvenc_mode_ntsc = {
102 .clock = 13500 * TVENC_CLOCK_MULTIPLIER,
104 .hsync_start = 720 + 16,
105 .hsync_end = 720 + 16 + 2,
106 .htotal = 720 + 16 + 2 + 120,
108 .vsync_start = 480 + 3,
109 .vsync_end = 480 + 3 + 2,
110 .vtotal = 480 + 3 + 2 + 17,
111 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
112 DRM_MODE_FLAG_INTERLACE,
114 .video_info = 0x00040080,
115 .video_res = 0x05a8375a,
116 .field1_param = 0x00041817,
117 .field2_param = 0x0008351e,
118 .burst_line_odd1 = 0x00041006,
119 .burst_line_even1 = 0x0008290d,
120 .burst_line_odd2 = 0x00000000,
121 .burst_line_even2 = 0x00000000,
122 .line_timing_param = 0x06a8ef9e,
123 .weight_value = 0x00b68197,
124 .blank_black_level = 0x0000f0f0,
125 .burst_level = 0x0000009c,
126 .control_param = 0x00000001,
127 .sub_carrier_phase1 = 0x10f83e10,
128 .phase_line_incr_cvbs = 0x80000000,
131 static const struct zx_tvenc_mode *tvenc_modes[] = {
136 static const struct zx_tvenc_mode *
137 zx_tvenc_find_zmode(struct drm_display_mode *mode)
141 for (i = 0; i < ARRAY_SIZE(tvenc_modes); i++) {
142 const struct zx_tvenc_mode *zmode = tvenc_modes[i];
144 if (drm_mode_equal(mode, &zmode->mode))
151 static void zx_tvenc_encoder_mode_set(struct drm_encoder *encoder,
152 struct drm_display_mode *mode,
153 struct drm_display_mode *adj_mode)
155 struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
156 const struct zx_tvenc_mode *zmode;
157 struct vou_div_config configs[] = {
158 { VOU_DIV_INF, VOU_DIV_4 },
159 { VOU_DIV_TVENC, VOU_DIV_1 },
160 { VOU_DIV_LAYER, VOU_DIV_2 },
163 zx_vou_config_dividers(encoder->crtc, configs, ARRAY_SIZE(configs));
165 zmode = zx_tvenc_find_zmode(mode);
167 DRM_DEV_ERROR(tvenc->dev, "failed to find zmode\n");
171 zx_writel(tvenc->mmio + VENC_VIDEO_INFO, zmode->video_info);
172 zx_writel(tvenc->mmio + VENC_VIDEO_RES, zmode->video_res);
173 zx_writel(tvenc->mmio + VENC_FIELD1_PARAM, zmode->field1_param);
174 zx_writel(tvenc->mmio + VENC_FIELD2_PARAM, zmode->field2_param);
175 zx_writel(tvenc->mmio + VENC_LINE_O_1, zmode->burst_line_odd1);
176 zx_writel(tvenc->mmio + VENC_LINE_E_1, zmode->burst_line_even1);
177 zx_writel(tvenc->mmio + VENC_LINE_O_2, zmode->burst_line_odd2);
178 zx_writel(tvenc->mmio + VENC_LINE_E_2, zmode->burst_line_even2);
179 zx_writel(tvenc->mmio + VENC_LINE_TIMING_PARAM,
180 zmode->line_timing_param);
181 zx_writel(tvenc->mmio + VENC_WEIGHT_VALUE, zmode->weight_value);
182 zx_writel(tvenc->mmio + VENC_BLANK_BLACK_LEVEL,
183 zmode->blank_black_level);
184 zx_writel(tvenc->mmio + VENC_BURST_LEVEL, zmode->burst_level);
185 zx_writel(tvenc->mmio + VENC_CONTROL_PARAM, zmode->control_param);
186 zx_writel(tvenc->mmio + VENC_SUB_CARRIER_PHASE1,
187 zmode->sub_carrier_phase1);
188 zx_writel(tvenc->mmio + VENC_PHASE_LINE_INCR_CVBS,
189 zmode->phase_line_incr_cvbs);
192 static void zx_tvenc_encoder_enable(struct drm_encoder *encoder)
194 struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
195 struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
197 /* Set bit to power up TVENC DAC */
198 regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask,
201 vou_inf_enable(VOU_TV_ENC, encoder->crtc);
203 zx_writel(tvenc->mmio + VENC_ENABLE, 1);
206 static void zx_tvenc_encoder_disable(struct drm_encoder *encoder)
208 struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
209 struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
211 zx_writel(tvenc->mmio + VENC_ENABLE, 0);
213 vou_inf_disable(VOU_TV_ENC, encoder->crtc);
215 /* Clear bit to power down TVENC DAC */
216 regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask, 0);
219 static const struct drm_encoder_helper_funcs zx_tvenc_encoder_helper_funcs = {
220 .enable = zx_tvenc_encoder_enable,
221 .disable = zx_tvenc_encoder_disable,
222 .mode_set = zx_tvenc_encoder_mode_set,
225 static const struct drm_encoder_funcs zx_tvenc_encoder_funcs = {
226 .destroy = drm_encoder_cleanup,
229 static int zx_tvenc_connector_get_modes(struct drm_connector *connector)
231 struct zx_tvenc *tvenc = to_zx_tvenc(connector);
232 struct device *dev = tvenc->dev;
235 for (i = 0; i < ARRAY_SIZE(tvenc_modes); i++) {
236 const struct zx_tvenc_mode *zmode = tvenc_modes[i];
237 struct drm_display_mode *mode;
239 mode = drm_mode_duplicate(connector->dev, &zmode->mode);
241 DRM_DEV_ERROR(dev, "failed to duplicate drm mode\n");
245 drm_mode_set_name(mode);
246 drm_mode_probed_add(connector, mode);
252 static enum drm_mode_status
253 zx_tvenc_connector_mode_valid(struct drm_connector *connector,
254 struct drm_display_mode *mode)
256 struct zx_tvenc *tvenc = to_zx_tvenc(connector);
257 const struct zx_tvenc_mode *zmode;
259 zmode = zx_tvenc_find_zmode(mode);
261 DRM_DEV_ERROR(tvenc->dev, "unsupported mode: %s\n", mode->name);
268 static struct drm_connector_helper_funcs zx_tvenc_connector_helper_funcs = {
269 .get_modes = zx_tvenc_connector_get_modes,
270 .mode_valid = zx_tvenc_connector_mode_valid,
273 static const struct drm_connector_funcs zx_tvenc_connector_funcs = {
274 .fill_modes = drm_helper_probe_single_connector_modes,
275 .destroy = drm_connector_cleanup,
276 .reset = drm_atomic_helper_connector_reset,
277 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
278 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
281 static int zx_tvenc_register(struct drm_device *drm, struct zx_tvenc *tvenc)
283 struct drm_encoder *encoder = &tvenc->encoder;
284 struct drm_connector *connector = &tvenc->connector;
287 * The tvenc is designed to use aux channel, as there is a deflicker
288 * block for the channel.
290 encoder->possible_crtcs = BIT(1);
292 drm_encoder_init(drm, encoder, &zx_tvenc_encoder_funcs,
293 DRM_MODE_ENCODER_TVDAC, NULL);
294 drm_encoder_helper_add(encoder, &zx_tvenc_encoder_helper_funcs);
296 connector->interlace_allowed = true;
298 drm_connector_init(drm, connector, &zx_tvenc_connector_funcs,
299 DRM_MODE_CONNECTOR_Composite);
300 drm_connector_helper_add(connector, &zx_tvenc_connector_helper_funcs);
302 drm_connector_attach_encoder(connector, encoder);
307 static int zx_tvenc_pwrctrl_init(struct zx_tvenc *tvenc)
309 struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
310 struct device *dev = tvenc->dev;
311 struct of_phandle_args out_args;
312 struct regmap *regmap;
315 ret = of_parse_phandle_with_fixed_args(dev->of_node,
316 "zte,tvenc-power-control", 2, 0, &out_args);
320 regmap = syscon_node_to_regmap(out_args.np);
321 if (IS_ERR(regmap)) {
322 ret = PTR_ERR(regmap);
326 pwrctrl->regmap = regmap;
327 pwrctrl->reg = out_args.args[0];
328 pwrctrl->mask = out_args.args[1];
331 of_node_put(out_args.np);
335 static int zx_tvenc_bind(struct device *dev, struct device *master, void *data)
337 struct platform_device *pdev = to_platform_device(dev);
338 struct drm_device *drm = data;
339 struct resource *res;
340 struct zx_tvenc *tvenc;
343 tvenc = devm_kzalloc(dev, sizeof(*tvenc), GFP_KERNEL);
348 dev_set_drvdata(dev, tvenc);
350 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
351 tvenc->mmio = devm_ioremap_resource(dev, res);
352 if (IS_ERR(tvenc->mmio)) {
353 ret = PTR_ERR(tvenc->mmio);
354 DRM_DEV_ERROR(dev, "failed to remap tvenc region: %d\n", ret);
358 ret = zx_tvenc_pwrctrl_init(tvenc);
360 DRM_DEV_ERROR(dev, "failed to init power control: %d\n", ret);
364 ret = zx_tvenc_register(drm, tvenc);
366 DRM_DEV_ERROR(dev, "failed to register tvenc: %d\n", ret);
373 static void zx_tvenc_unbind(struct device *dev, struct device *master,
379 static const struct component_ops zx_tvenc_component_ops = {
380 .bind = zx_tvenc_bind,
381 .unbind = zx_tvenc_unbind,
384 static int zx_tvenc_probe(struct platform_device *pdev)
386 return component_add(&pdev->dev, &zx_tvenc_component_ops);
389 static int zx_tvenc_remove(struct platform_device *pdev)
391 component_del(&pdev->dev, &zx_tvenc_component_ops);
395 static const struct of_device_id zx_tvenc_of_match[] = {
396 { .compatible = "zte,zx296718-tvenc", },
399 MODULE_DEVICE_TABLE(of, zx_tvenc_of_match);
401 struct platform_driver zx_tvenc_driver = {
402 .probe = zx_tvenc_probe,
403 .remove = zx_tvenc_remove,
406 .of_match_table = zx_tvenc_of_match,