1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2017 Linaro Ltd.
4 * Copyright 2017 ZTE Corporation.
8 #include <linux/component.h>
9 #include <linux/mfd/syscon.h>
10 #include <linux/regmap.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_probe_helper.h>
16 #include "zx_drm_drv.h"
17 #include "zx_tvenc_regs.h"
20 struct zx_tvenc_pwrctrl {
21 struct regmap *regmap;
27 struct drm_connector connector;
28 struct drm_encoder encoder;
31 const struct vou_inf *inf;
32 struct zx_tvenc_pwrctrl pwrctrl;
35 #define to_zx_tvenc(x) container_of(x, struct zx_tvenc, x)
37 struct zx_tvenc_mode {
38 struct drm_display_mode mode;
47 u32 line_timing_param;
49 u32 blank_black_level;
52 u32 sub_carrier_phase1;
53 u32 phase_line_incr_cvbs;
57 * The CRM cannot directly provide a suitable frequency, and we have to
58 * ask a multiplied rate from CRM and use the divider in VOU to get the
61 #define TVENC_CLOCK_MULTIPLIER 4
63 static const struct zx_tvenc_mode tvenc_mode_pal = {
65 .clock = 13500 * TVENC_CLOCK_MULTIPLIER,
67 .hsync_start = 720 + 12,
68 .hsync_end = 720 + 12 + 2,
69 .htotal = 720 + 12 + 2 + 130,
71 .vsync_start = 576 + 2,
72 .vsync_end = 576 + 2 + 2,
73 .vtotal = 576 + 2 + 2 + 20,
74 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
75 DRM_MODE_FLAG_INTERLACE,
77 .video_info = 0x00040040,
78 .video_res = 0x05a9c760,
79 .field1_param = 0x0004d416,
80 .field2_param = 0x0009b94f,
81 .burst_line_odd1 = 0x0004d406,
82 .burst_line_even1 = 0x0009b53e,
83 .burst_line_odd2 = 0x0004d805,
84 .burst_line_even2 = 0x0009b93f,
85 .line_timing_param = 0x06a96fdf,
86 .weight_value = 0x00c188a0,
87 .blank_black_level = 0x0000fcfc,
88 .burst_level = 0x00001595,
89 .control_param = 0x00000001,
90 .sub_carrier_phase1 = 0x1504c566,
91 .phase_line_incr_cvbs = 0xc068db8c,
94 static const struct zx_tvenc_mode tvenc_mode_ntsc = {
96 .clock = 13500 * TVENC_CLOCK_MULTIPLIER,
98 .hsync_start = 720 + 16,
99 .hsync_end = 720 + 16 + 2,
100 .htotal = 720 + 16 + 2 + 120,
102 .vsync_start = 480 + 3,
103 .vsync_end = 480 + 3 + 2,
104 .vtotal = 480 + 3 + 2 + 17,
105 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
106 DRM_MODE_FLAG_INTERLACE,
108 .video_info = 0x00040080,
109 .video_res = 0x05a8375a,
110 .field1_param = 0x00041817,
111 .field2_param = 0x0008351e,
112 .burst_line_odd1 = 0x00041006,
113 .burst_line_even1 = 0x0008290d,
114 .burst_line_odd2 = 0x00000000,
115 .burst_line_even2 = 0x00000000,
116 .line_timing_param = 0x06a8ef9e,
117 .weight_value = 0x00b68197,
118 .blank_black_level = 0x0000f0f0,
119 .burst_level = 0x0000009c,
120 .control_param = 0x00000001,
121 .sub_carrier_phase1 = 0x10f83e10,
122 .phase_line_incr_cvbs = 0x80000000,
125 static const struct zx_tvenc_mode *tvenc_modes[] = {
130 static const struct zx_tvenc_mode *
131 zx_tvenc_find_zmode(struct drm_display_mode *mode)
135 for (i = 0; i < ARRAY_SIZE(tvenc_modes); i++) {
136 const struct zx_tvenc_mode *zmode = tvenc_modes[i];
138 if (drm_mode_equal(mode, &zmode->mode))
145 static void zx_tvenc_encoder_mode_set(struct drm_encoder *encoder,
146 struct drm_display_mode *mode,
147 struct drm_display_mode *adj_mode)
149 struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
150 const struct zx_tvenc_mode *zmode;
151 struct vou_div_config configs[] = {
152 { VOU_DIV_INF, VOU_DIV_4 },
153 { VOU_DIV_TVENC, VOU_DIV_1 },
154 { VOU_DIV_LAYER, VOU_DIV_2 },
157 zx_vou_config_dividers(encoder->crtc, configs, ARRAY_SIZE(configs));
159 zmode = zx_tvenc_find_zmode(mode);
161 DRM_DEV_ERROR(tvenc->dev, "failed to find zmode\n");
165 zx_writel(tvenc->mmio + VENC_VIDEO_INFO, zmode->video_info);
166 zx_writel(tvenc->mmio + VENC_VIDEO_RES, zmode->video_res);
167 zx_writel(tvenc->mmio + VENC_FIELD1_PARAM, zmode->field1_param);
168 zx_writel(tvenc->mmio + VENC_FIELD2_PARAM, zmode->field2_param);
169 zx_writel(tvenc->mmio + VENC_LINE_O_1, zmode->burst_line_odd1);
170 zx_writel(tvenc->mmio + VENC_LINE_E_1, zmode->burst_line_even1);
171 zx_writel(tvenc->mmio + VENC_LINE_O_2, zmode->burst_line_odd2);
172 zx_writel(tvenc->mmio + VENC_LINE_E_2, zmode->burst_line_even2);
173 zx_writel(tvenc->mmio + VENC_LINE_TIMING_PARAM,
174 zmode->line_timing_param);
175 zx_writel(tvenc->mmio + VENC_WEIGHT_VALUE, zmode->weight_value);
176 zx_writel(tvenc->mmio + VENC_BLANK_BLACK_LEVEL,
177 zmode->blank_black_level);
178 zx_writel(tvenc->mmio + VENC_BURST_LEVEL, zmode->burst_level);
179 zx_writel(tvenc->mmio + VENC_CONTROL_PARAM, zmode->control_param);
180 zx_writel(tvenc->mmio + VENC_SUB_CARRIER_PHASE1,
181 zmode->sub_carrier_phase1);
182 zx_writel(tvenc->mmio + VENC_PHASE_LINE_INCR_CVBS,
183 zmode->phase_line_incr_cvbs);
186 static void zx_tvenc_encoder_enable(struct drm_encoder *encoder)
188 struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
189 struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
191 /* Set bit to power up TVENC DAC */
192 regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask,
195 vou_inf_enable(VOU_TV_ENC, encoder->crtc);
197 zx_writel(tvenc->mmio + VENC_ENABLE, 1);
200 static void zx_tvenc_encoder_disable(struct drm_encoder *encoder)
202 struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
203 struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
205 zx_writel(tvenc->mmio + VENC_ENABLE, 0);
207 vou_inf_disable(VOU_TV_ENC, encoder->crtc);
209 /* Clear bit to power down TVENC DAC */
210 regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask, 0);
213 static const struct drm_encoder_helper_funcs zx_tvenc_encoder_helper_funcs = {
214 .enable = zx_tvenc_encoder_enable,
215 .disable = zx_tvenc_encoder_disable,
216 .mode_set = zx_tvenc_encoder_mode_set,
219 static const struct drm_encoder_funcs zx_tvenc_encoder_funcs = {
220 .destroy = drm_encoder_cleanup,
223 static int zx_tvenc_connector_get_modes(struct drm_connector *connector)
225 struct zx_tvenc *tvenc = to_zx_tvenc(connector);
226 struct device *dev = tvenc->dev;
229 for (i = 0; i < ARRAY_SIZE(tvenc_modes); i++) {
230 const struct zx_tvenc_mode *zmode = tvenc_modes[i];
231 struct drm_display_mode *mode;
233 mode = drm_mode_duplicate(connector->dev, &zmode->mode);
235 DRM_DEV_ERROR(dev, "failed to duplicate drm mode\n");
239 drm_mode_set_name(mode);
240 drm_mode_probed_add(connector, mode);
246 static enum drm_mode_status
247 zx_tvenc_connector_mode_valid(struct drm_connector *connector,
248 struct drm_display_mode *mode)
250 struct zx_tvenc *tvenc = to_zx_tvenc(connector);
251 const struct zx_tvenc_mode *zmode;
253 zmode = zx_tvenc_find_zmode(mode);
255 DRM_DEV_ERROR(tvenc->dev, "unsupported mode: %s\n", mode->name);
262 static struct drm_connector_helper_funcs zx_tvenc_connector_helper_funcs = {
263 .get_modes = zx_tvenc_connector_get_modes,
264 .mode_valid = zx_tvenc_connector_mode_valid,
267 static const struct drm_connector_funcs zx_tvenc_connector_funcs = {
268 .fill_modes = drm_helper_probe_single_connector_modes,
269 .destroy = drm_connector_cleanup,
270 .reset = drm_atomic_helper_connector_reset,
271 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
272 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
275 static int zx_tvenc_register(struct drm_device *drm, struct zx_tvenc *tvenc)
277 struct drm_encoder *encoder = &tvenc->encoder;
278 struct drm_connector *connector = &tvenc->connector;
281 * The tvenc is designed to use aux channel, as there is a deflicker
282 * block for the channel.
284 encoder->possible_crtcs = BIT(1);
286 drm_encoder_init(drm, encoder, &zx_tvenc_encoder_funcs,
287 DRM_MODE_ENCODER_TVDAC, NULL);
288 drm_encoder_helper_add(encoder, &zx_tvenc_encoder_helper_funcs);
290 connector->interlace_allowed = true;
292 drm_connector_init(drm, connector, &zx_tvenc_connector_funcs,
293 DRM_MODE_CONNECTOR_Composite);
294 drm_connector_helper_add(connector, &zx_tvenc_connector_helper_funcs);
296 drm_connector_attach_encoder(connector, encoder);
301 static int zx_tvenc_pwrctrl_init(struct zx_tvenc *tvenc)
303 struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
304 struct device *dev = tvenc->dev;
305 struct of_phandle_args out_args;
306 struct regmap *regmap;
309 ret = of_parse_phandle_with_fixed_args(dev->of_node,
310 "zte,tvenc-power-control", 2, 0, &out_args);
314 regmap = syscon_node_to_regmap(out_args.np);
315 if (IS_ERR(regmap)) {
316 ret = PTR_ERR(regmap);
320 pwrctrl->regmap = regmap;
321 pwrctrl->reg = out_args.args[0];
322 pwrctrl->mask = out_args.args[1];
325 of_node_put(out_args.np);
329 static int zx_tvenc_bind(struct device *dev, struct device *master, void *data)
331 struct platform_device *pdev = to_platform_device(dev);
332 struct drm_device *drm = data;
333 struct resource *res;
334 struct zx_tvenc *tvenc;
337 tvenc = devm_kzalloc(dev, sizeof(*tvenc), GFP_KERNEL);
342 dev_set_drvdata(dev, tvenc);
344 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
345 tvenc->mmio = devm_ioremap_resource(dev, res);
346 if (IS_ERR(tvenc->mmio)) {
347 ret = PTR_ERR(tvenc->mmio);
348 DRM_DEV_ERROR(dev, "failed to remap tvenc region: %d\n", ret);
352 ret = zx_tvenc_pwrctrl_init(tvenc);
354 DRM_DEV_ERROR(dev, "failed to init power control: %d\n", ret);
358 ret = zx_tvenc_register(drm, tvenc);
360 DRM_DEV_ERROR(dev, "failed to register tvenc: %d\n", ret);
367 static void zx_tvenc_unbind(struct device *dev, struct device *master,
373 static const struct component_ops zx_tvenc_component_ops = {
374 .bind = zx_tvenc_bind,
375 .unbind = zx_tvenc_unbind,
378 static int zx_tvenc_probe(struct platform_device *pdev)
380 return component_add(&pdev->dev, &zx_tvenc_component_ops);
383 static int zx_tvenc_remove(struct platform_device *pdev)
385 component_del(&pdev->dev, &zx_tvenc_component_ops);
389 static const struct of_device_id zx_tvenc_of_match[] = {
390 { .compatible = "zte,zx296718-tvenc", },
393 MODULE_DEVICE_TABLE(of, zx_tvenc_of_match);
395 struct platform_driver zx_tvenc_driver = {
396 .probe = zx_tvenc_probe,
397 .remove = zx_tvenc_remove,
400 .of_match_table = zx_tvenc_of_match,