]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/zte/zx_tvenc.c
drm/zte: drop use of drmP.h
[linux.git] / drivers / gpu / drm / zte / zx_tvenc.c
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/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_print.h>
20 #include <drm/drm_probe_helper.h>
21
22 #include "zx_drm_drv.h"
23 #include "zx_tvenc_regs.h"
24 #include "zx_vou.h"
25
26 struct zx_tvenc_pwrctrl {
27         struct regmap *regmap;
28         u32 reg;
29         u32 mask;
30 };
31
32 struct zx_tvenc {
33         struct drm_connector connector;
34         struct drm_encoder encoder;
35         struct device *dev;
36         void __iomem *mmio;
37         const struct vou_inf *inf;
38         struct zx_tvenc_pwrctrl pwrctrl;
39 };
40
41 #define to_zx_tvenc(x) container_of(x, struct zx_tvenc, x)
42
43 struct zx_tvenc_mode {
44         struct drm_display_mode mode;
45         u32 video_info;
46         u32 video_res;
47         u32 field1_param;
48         u32 field2_param;
49         u32 burst_line_odd1;
50         u32 burst_line_even1;
51         u32 burst_line_odd2;
52         u32 burst_line_even2;
53         u32 line_timing_param;
54         u32 weight_value;
55         u32 blank_black_level;
56         u32 burst_level;
57         u32 control_param;
58         u32 sub_carrier_phase1;
59         u32 phase_line_incr_cvbs;
60 };
61
62 /*
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
65  * desired one.
66  */
67 #define TVENC_CLOCK_MULTIPLIER  4
68
69 static const struct zx_tvenc_mode tvenc_mode_pal = {
70         .mode = {
71                 .clock = 13500 * TVENC_CLOCK_MULTIPLIER,
72                 .hdisplay = 720,
73                 .hsync_start = 720 + 12,
74                 .hsync_end = 720 + 12 + 2,
75                 .htotal = 720 + 12 + 2 + 130,
76                 .vdisplay = 576,
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,
82         },
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,
98 };
99
100 static const struct zx_tvenc_mode tvenc_mode_ntsc = {
101         .mode = {
102                 .clock = 13500 * TVENC_CLOCK_MULTIPLIER,
103                 .hdisplay = 720,
104                 .hsync_start = 720 + 16,
105                 .hsync_end = 720 + 16 + 2,
106                 .htotal = 720 + 16 + 2 + 120,
107                 .vdisplay = 480,
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,
113         },
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,
129 };
130
131 static const struct zx_tvenc_mode *tvenc_modes[] = {
132         &tvenc_mode_pal,
133         &tvenc_mode_ntsc,
134 };
135
136 static const struct zx_tvenc_mode *
137 zx_tvenc_find_zmode(struct drm_display_mode *mode)
138 {
139         int i;
140
141         for (i = 0; i < ARRAY_SIZE(tvenc_modes); i++) {
142                 const struct zx_tvenc_mode *zmode = tvenc_modes[i];
143
144                 if (drm_mode_equal(mode, &zmode->mode))
145                         return zmode;
146         }
147
148         return NULL;
149 }
150
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)
154 {
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 },
161         };
162
163         zx_vou_config_dividers(encoder->crtc, configs, ARRAY_SIZE(configs));
164
165         zmode = zx_tvenc_find_zmode(mode);
166         if (!zmode) {
167                 DRM_DEV_ERROR(tvenc->dev, "failed to find zmode\n");
168                 return;
169         }
170
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);
190 }
191
192 static void zx_tvenc_encoder_enable(struct drm_encoder *encoder)
193 {
194         struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
195         struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
196
197         /* Set bit to power up TVENC DAC */
198         regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask,
199                            pwrctrl->mask);
200
201         vou_inf_enable(VOU_TV_ENC, encoder->crtc);
202
203         zx_writel(tvenc->mmio + VENC_ENABLE, 1);
204 }
205
206 static void zx_tvenc_encoder_disable(struct drm_encoder *encoder)
207 {
208         struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
209         struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
210
211         zx_writel(tvenc->mmio + VENC_ENABLE, 0);
212
213         vou_inf_disable(VOU_TV_ENC, encoder->crtc);
214
215         /* Clear bit to power down TVENC DAC */
216         regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask, 0);
217 }
218
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,
223 };
224
225 static const struct drm_encoder_funcs zx_tvenc_encoder_funcs = {
226         .destroy = drm_encoder_cleanup,
227 };
228
229 static int zx_tvenc_connector_get_modes(struct drm_connector *connector)
230 {
231         struct zx_tvenc *tvenc = to_zx_tvenc(connector);
232         struct device *dev = tvenc->dev;
233         int i;
234
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;
238
239                 mode = drm_mode_duplicate(connector->dev, &zmode->mode);
240                 if (!mode) {
241                         DRM_DEV_ERROR(dev, "failed to duplicate drm mode\n");
242                         continue;
243                 }
244
245                 drm_mode_set_name(mode);
246                 drm_mode_probed_add(connector, mode);
247         }
248
249         return i;
250 }
251
252 static enum drm_mode_status
253 zx_tvenc_connector_mode_valid(struct drm_connector *connector,
254                               struct drm_display_mode *mode)
255 {
256         struct zx_tvenc *tvenc = to_zx_tvenc(connector);
257         const struct zx_tvenc_mode *zmode;
258
259         zmode = zx_tvenc_find_zmode(mode);
260         if (!zmode) {
261                 DRM_DEV_ERROR(tvenc->dev, "unsupported mode: %s\n", mode->name);
262                 return MODE_NOMODE;
263         }
264
265         return MODE_OK;
266 }
267
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,
271 };
272
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,
279 };
280
281 static int zx_tvenc_register(struct drm_device *drm, struct zx_tvenc *tvenc)
282 {
283         struct drm_encoder *encoder = &tvenc->encoder;
284         struct drm_connector *connector = &tvenc->connector;
285
286         /*
287          * The tvenc is designed to use aux channel, as there is a deflicker
288          * block for the channel.
289          */
290         encoder->possible_crtcs = BIT(1);
291
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);
295
296         connector->interlace_allowed = true;
297
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);
301
302         drm_connector_attach_encoder(connector, encoder);
303
304         return 0;
305 }
306
307 static int zx_tvenc_pwrctrl_init(struct zx_tvenc *tvenc)
308 {
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;
313         int ret;
314
315         ret = of_parse_phandle_with_fixed_args(dev->of_node,
316                                 "zte,tvenc-power-control", 2, 0, &out_args);
317         if (ret)
318                 return ret;
319
320         regmap = syscon_node_to_regmap(out_args.np);
321         if (IS_ERR(regmap)) {
322                 ret = PTR_ERR(regmap);
323                 goto out;
324         }
325
326         pwrctrl->regmap = regmap;
327         pwrctrl->reg = out_args.args[0];
328         pwrctrl->mask = out_args.args[1];
329
330 out:
331         of_node_put(out_args.np);
332         return ret;
333 }
334
335 static int zx_tvenc_bind(struct device *dev, struct device *master, void *data)
336 {
337         struct platform_device *pdev = to_platform_device(dev);
338         struct drm_device *drm = data;
339         struct resource *res;
340         struct zx_tvenc *tvenc;
341         int ret;
342
343         tvenc = devm_kzalloc(dev, sizeof(*tvenc), GFP_KERNEL);
344         if (!tvenc)
345                 return -ENOMEM;
346
347         tvenc->dev = dev;
348         dev_set_drvdata(dev, tvenc);
349
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);
355                 return ret;
356         }
357
358         ret = zx_tvenc_pwrctrl_init(tvenc);
359         if (ret) {
360                 DRM_DEV_ERROR(dev, "failed to init power control: %d\n", ret);
361                 return ret;
362         }
363
364         ret = zx_tvenc_register(drm, tvenc);
365         if (ret) {
366                 DRM_DEV_ERROR(dev, "failed to register tvenc: %d\n", ret);
367                 return ret;
368         }
369
370         return 0;
371 }
372
373 static void zx_tvenc_unbind(struct device *dev, struct device *master,
374                             void *data)
375 {
376         /* Nothing to do */
377 }
378
379 static const struct component_ops zx_tvenc_component_ops = {
380         .bind = zx_tvenc_bind,
381         .unbind = zx_tvenc_unbind,
382 };
383
384 static int zx_tvenc_probe(struct platform_device *pdev)
385 {
386         return component_add(&pdev->dev, &zx_tvenc_component_ops);
387 }
388
389 static int zx_tvenc_remove(struct platform_device *pdev)
390 {
391         component_del(&pdev->dev, &zx_tvenc_component_ops);
392         return 0;
393 }
394
395 static const struct of_device_id zx_tvenc_of_match[] = {
396         { .compatible = "zte,zx296718-tvenc", },
397         { /* end */ },
398 };
399 MODULE_DEVICE_TABLE(of, zx_tvenc_of_match);
400
401 struct platform_driver zx_tvenc_driver = {
402         .probe = zx_tvenc_probe,
403         .remove = zx_tvenc_remove,
404         .driver = {
405                 .name = "zx-tvenc",
406                 .of_match_table = zx_tvenc_of_match,
407         },
408 };