Merge tag 'gpio-v5.3-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / zte / zx_tvenc.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2017 Linaro Ltd.
4  * Copyright 2017 ZTE Corporation.
5  */
6
7 #include <linux/clk.h>
8 #include <linux/component.h>
9 #include <linux/mfd/syscon.h>
10 #include <linux/regmap.h>
11
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_probe_helper.h>
14 #include <drm/drmP.h>
15
16 #include "zx_drm_drv.h"
17 #include "zx_tvenc_regs.h"
18 #include "zx_vou.h"
19
20 struct zx_tvenc_pwrctrl {
21         struct regmap *regmap;
22         u32 reg;
23         u32 mask;
24 };
25
26 struct zx_tvenc {
27         struct drm_connector connector;
28         struct drm_encoder encoder;
29         struct device *dev;
30         void __iomem *mmio;
31         const struct vou_inf *inf;
32         struct zx_tvenc_pwrctrl pwrctrl;
33 };
34
35 #define to_zx_tvenc(x) container_of(x, struct zx_tvenc, x)
36
37 struct zx_tvenc_mode {
38         struct drm_display_mode mode;
39         u32 video_info;
40         u32 video_res;
41         u32 field1_param;
42         u32 field2_param;
43         u32 burst_line_odd1;
44         u32 burst_line_even1;
45         u32 burst_line_odd2;
46         u32 burst_line_even2;
47         u32 line_timing_param;
48         u32 weight_value;
49         u32 blank_black_level;
50         u32 burst_level;
51         u32 control_param;
52         u32 sub_carrier_phase1;
53         u32 phase_line_incr_cvbs;
54 };
55
56 /*
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
59  * desired one.
60  */
61 #define TVENC_CLOCK_MULTIPLIER  4
62
63 static const struct zx_tvenc_mode tvenc_mode_pal = {
64         .mode = {
65                 .clock = 13500 * TVENC_CLOCK_MULTIPLIER,
66                 .hdisplay = 720,
67                 .hsync_start = 720 + 12,
68                 .hsync_end = 720 + 12 + 2,
69                 .htotal = 720 + 12 + 2 + 130,
70                 .vdisplay = 576,
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,
76         },
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,
92 };
93
94 static const struct zx_tvenc_mode tvenc_mode_ntsc = {
95         .mode = {
96                 .clock = 13500 * TVENC_CLOCK_MULTIPLIER,
97                 .hdisplay = 720,
98                 .hsync_start = 720 + 16,
99                 .hsync_end = 720 + 16 + 2,
100                 .htotal = 720 + 16 + 2 + 120,
101                 .vdisplay = 480,
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,
107         },
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,
123 };
124
125 static const struct zx_tvenc_mode *tvenc_modes[] = {
126         &tvenc_mode_pal,
127         &tvenc_mode_ntsc,
128 };
129
130 static const struct zx_tvenc_mode *
131 zx_tvenc_find_zmode(struct drm_display_mode *mode)
132 {
133         int i;
134
135         for (i = 0; i < ARRAY_SIZE(tvenc_modes); i++) {
136                 const struct zx_tvenc_mode *zmode = tvenc_modes[i];
137
138                 if (drm_mode_equal(mode, &zmode->mode))
139                         return zmode;
140         }
141
142         return NULL;
143 }
144
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)
148 {
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 },
155         };
156
157         zx_vou_config_dividers(encoder->crtc, configs, ARRAY_SIZE(configs));
158
159         zmode = zx_tvenc_find_zmode(mode);
160         if (!zmode) {
161                 DRM_DEV_ERROR(tvenc->dev, "failed to find zmode\n");
162                 return;
163         }
164
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);
184 }
185
186 static void zx_tvenc_encoder_enable(struct drm_encoder *encoder)
187 {
188         struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
189         struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
190
191         /* Set bit to power up TVENC DAC */
192         regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask,
193                            pwrctrl->mask);
194
195         vou_inf_enable(VOU_TV_ENC, encoder->crtc);
196
197         zx_writel(tvenc->mmio + VENC_ENABLE, 1);
198 }
199
200 static void zx_tvenc_encoder_disable(struct drm_encoder *encoder)
201 {
202         struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
203         struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
204
205         zx_writel(tvenc->mmio + VENC_ENABLE, 0);
206
207         vou_inf_disable(VOU_TV_ENC, encoder->crtc);
208
209         /* Clear bit to power down TVENC DAC */
210         regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask, 0);
211 }
212
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,
217 };
218
219 static const struct drm_encoder_funcs zx_tvenc_encoder_funcs = {
220         .destroy = drm_encoder_cleanup,
221 };
222
223 static int zx_tvenc_connector_get_modes(struct drm_connector *connector)
224 {
225         struct zx_tvenc *tvenc = to_zx_tvenc(connector);
226         struct device *dev = tvenc->dev;
227         int i;
228
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;
232
233                 mode = drm_mode_duplicate(connector->dev, &zmode->mode);
234                 if (!mode) {
235                         DRM_DEV_ERROR(dev, "failed to duplicate drm mode\n");
236                         continue;
237                 }
238
239                 drm_mode_set_name(mode);
240                 drm_mode_probed_add(connector, mode);
241         }
242
243         return i;
244 }
245
246 static enum drm_mode_status
247 zx_tvenc_connector_mode_valid(struct drm_connector *connector,
248                               struct drm_display_mode *mode)
249 {
250         struct zx_tvenc *tvenc = to_zx_tvenc(connector);
251         const struct zx_tvenc_mode *zmode;
252
253         zmode = zx_tvenc_find_zmode(mode);
254         if (!zmode) {
255                 DRM_DEV_ERROR(tvenc->dev, "unsupported mode: %s\n", mode->name);
256                 return MODE_NOMODE;
257         }
258
259         return MODE_OK;
260 }
261
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,
265 };
266
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,
273 };
274
275 static int zx_tvenc_register(struct drm_device *drm, struct zx_tvenc *tvenc)
276 {
277         struct drm_encoder *encoder = &tvenc->encoder;
278         struct drm_connector *connector = &tvenc->connector;
279
280         /*
281          * The tvenc is designed to use aux channel, as there is a deflicker
282          * block for the channel.
283          */
284         encoder->possible_crtcs = BIT(1);
285
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);
289
290         connector->interlace_allowed = true;
291
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);
295
296         drm_connector_attach_encoder(connector, encoder);
297
298         return 0;
299 }
300
301 static int zx_tvenc_pwrctrl_init(struct zx_tvenc *tvenc)
302 {
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;
307         int ret;
308
309         ret = of_parse_phandle_with_fixed_args(dev->of_node,
310                                 "zte,tvenc-power-control", 2, 0, &out_args);
311         if (ret)
312                 return ret;
313
314         regmap = syscon_node_to_regmap(out_args.np);
315         if (IS_ERR(regmap)) {
316                 ret = PTR_ERR(regmap);
317                 goto out;
318         }
319
320         pwrctrl->regmap = regmap;
321         pwrctrl->reg = out_args.args[0];
322         pwrctrl->mask = out_args.args[1];
323
324 out:
325         of_node_put(out_args.np);
326         return ret;
327 }
328
329 static int zx_tvenc_bind(struct device *dev, struct device *master, void *data)
330 {
331         struct platform_device *pdev = to_platform_device(dev);
332         struct drm_device *drm = data;
333         struct resource *res;
334         struct zx_tvenc *tvenc;
335         int ret;
336
337         tvenc = devm_kzalloc(dev, sizeof(*tvenc), GFP_KERNEL);
338         if (!tvenc)
339                 return -ENOMEM;
340
341         tvenc->dev = dev;
342         dev_set_drvdata(dev, tvenc);
343
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);
349                 return ret;
350         }
351
352         ret = zx_tvenc_pwrctrl_init(tvenc);
353         if (ret) {
354                 DRM_DEV_ERROR(dev, "failed to init power control: %d\n", ret);
355                 return ret;
356         }
357
358         ret = zx_tvenc_register(drm, tvenc);
359         if (ret) {
360                 DRM_DEV_ERROR(dev, "failed to register tvenc: %d\n", ret);
361                 return ret;
362         }
363
364         return 0;
365 }
366
367 static void zx_tvenc_unbind(struct device *dev, struct device *master,
368                             void *data)
369 {
370         /* Nothing to do */
371 }
372
373 static const struct component_ops zx_tvenc_component_ops = {
374         .bind = zx_tvenc_bind,
375         .unbind = zx_tvenc_unbind,
376 };
377
378 static int zx_tvenc_probe(struct platform_device *pdev)
379 {
380         return component_add(&pdev->dev, &zx_tvenc_component_ops);
381 }
382
383 static int zx_tvenc_remove(struct platform_device *pdev)
384 {
385         component_del(&pdev->dev, &zx_tvenc_component_ops);
386         return 0;
387 }
388
389 static const struct of_device_id zx_tvenc_of_match[] = {
390         { .compatible = "zte,zx296718-tvenc", },
391         { /* end */ },
392 };
393 MODULE_DEVICE_TABLE(of, zx_tvenc_of_match);
394
395 struct platform_driver zx_tvenc_driver = {
396         .probe = zx_tvenc_probe,
397         .remove = zx_tvenc_remove,
398         .driver = {
399                 .name = "zx-tvenc",
400                 .of_match_table = zx_tvenc_of_match,
401         },
402 };