Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) STMicroelectronics SA 2014
4 * Author: Vincent Abriou <vincent.abriou@st.com> for STMicroelectronics.
5 */
6
7#include <linux/clk.h>
8#include <linux/component.h>
9#include <linux/debugfs.h>
10#include <linux/module.h>
11#include <linux/of_gpio.h>
12#include <linux/platform_device.h>
13
14#include <drm/drm_atomic_helper.h>
15#include <drm/drm_device.h>
16#include <drm/drm_panel.h>
17#include <drm/drm_print.h>
18#include <drm/drm_probe_helper.h>
19
20#include "sti_awg_utils.h"
21#include "sti_drv.h"
22#include "sti_mixer.h"
23
24/* DVO registers */
25#define DVO_AWG_DIGSYNC_CTRL 0x0000
26#define DVO_DOF_CFG 0x0004
27#define DVO_LUT_PROG_LOW 0x0008
28#define DVO_LUT_PROG_MID 0x000C
29#define DVO_LUT_PROG_HIGH 0x0010
30#define DVO_DIGSYNC_INSTR_I 0x0100
31
32#define DVO_AWG_CTRL_EN BIT(0)
33#define DVO_AWG_FRAME_BASED_SYNC BIT(2)
34
35#define DVO_DOF_EN_LOWBYTE BIT(0)
36#define DVO_DOF_EN_MIDBYTE BIT(1)
37#define DVO_DOF_EN_HIGHBYTE BIT(2)
38#define DVO_DOF_EN BIT(6)
39#define DVO_DOF_MOD_COUNT_SHIFT 8
40
41#define DVO_LUT_ZERO 0
42#define DVO_LUT_Y_G 1
43#define DVO_LUT_Y_G_DEL 2
44#define DVO_LUT_CB_B 3
45#define DVO_LUT_CB_B_DEL 4
46#define DVO_LUT_CR_R 5
47#define DVO_LUT_CR_R_DEL 6
48#define DVO_LUT_HOLD 7
49
50struct dvo_config {
51 u32 flags;
52 u32 lowbyte;
53 u32 midbyte;
54 u32 highbyte;
55 int (*awg_fwgen_fct)(
56 struct awg_code_generation_params *fw_gen_params,
57 struct awg_timing *timing);
58};
59
60static struct dvo_config rgb_24bit_de_cfg = {
61 .flags = (0L << DVO_DOF_MOD_COUNT_SHIFT),
62 .lowbyte = DVO_LUT_CR_R,
63 .midbyte = DVO_LUT_Y_G,
64 .highbyte = DVO_LUT_CB_B,
65 .awg_fwgen_fct = sti_awg_generate_code_data_enable_mode,
66};
67
68/**
69 * STI digital video output structure
70 *
71 * @dev: driver device
72 * @drm_dev: pointer to drm device
73 * @mode: current display mode selected
74 * @regs: dvo registers
75 * @clk_pix: pixel clock for dvo
76 * @clk: clock for dvo
77 * @clk_main_parent: dvo parent clock if main path used
78 * @clk_aux_parent: dvo parent clock if aux path used
79 * @panel_node: panel node reference from device tree
80 * @panel: reference to the panel connected to the dvo
81 * @enabled: true if dvo is enabled else false
82 * @encoder: drm_encoder it is bound
83 */
84struct sti_dvo {
85 struct device dev;
86 struct drm_device *drm_dev;
87 struct drm_display_mode mode;
88 void __iomem *regs;
89 struct clk *clk_pix;
90 struct clk *clk;
91 struct clk *clk_main_parent;
92 struct clk *clk_aux_parent;
93 struct device_node *panel_node;
94 struct drm_panel *panel;
95 struct dvo_config *config;
96 bool enabled;
97 struct drm_encoder *encoder;
98 struct drm_bridge *bridge;
99};
100
101struct sti_dvo_connector {
102 struct drm_connector drm_connector;
103 struct drm_encoder *encoder;
104 struct sti_dvo *dvo;
105};
106
107#define to_sti_dvo_connector(x) \
108 container_of(x, struct sti_dvo_connector, drm_connector)
109
110#define BLANKING_LEVEL 16
111static int dvo_awg_generate_code(struct sti_dvo *dvo, u8 *ram_size, u32 *ram_code)
112{
113 struct drm_display_mode *mode = &dvo->mode;
114 struct dvo_config *config = dvo->config;
115 struct awg_code_generation_params fw_gen_params;
116 struct awg_timing timing;
117
118 fw_gen_params.ram_code = ram_code;
119 fw_gen_params.instruction_offset = 0;
120
121 timing.total_lines = mode->vtotal;
122 timing.active_lines = mode->vdisplay;
123 timing.blanking_lines = mode->vsync_start - mode->vdisplay;
124 timing.trailing_lines = mode->vtotal - mode->vsync_start;
125 timing.total_pixels = mode->htotal;
126 timing.active_pixels = mode->hdisplay;
127 timing.blanking_pixels = mode->hsync_start - mode->hdisplay;
128 timing.trailing_pixels = mode->htotal - mode->hsync_start;
129 timing.blanking_level = BLANKING_LEVEL;
130
131 if (config->awg_fwgen_fct(&fw_gen_params, &timing)) {
132 DRM_ERROR("AWG firmware not properly generated\n");
133 return -EINVAL;
134 }
135
136 *ram_size = fw_gen_params.instruction_offset;
137
138 return 0;
139}
140
141/* Configure AWG, writing instructions
142 *
143 * @dvo: pointer to DVO structure
144 * @awg_ram_code: pointer to AWG instructions table
145 * @nb: nb of AWG instructions
146 */
147static void dvo_awg_configure(struct sti_dvo *dvo, u32 *awg_ram_code, int nb)
148{
149 int i;
150
151 DRM_DEBUG_DRIVER("\n");
152
153 for (i = 0; i < nb; i++)
154 writel(awg_ram_code[i],
155 dvo->regs + DVO_DIGSYNC_INSTR_I + i * 4);
156 for (i = nb; i < AWG_MAX_INST; i++)
157 writel(0, dvo->regs + DVO_DIGSYNC_INSTR_I + i * 4);
158
159 writel(DVO_AWG_CTRL_EN, dvo->regs + DVO_AWG_DIGSYNC_CTRL);
160}
161
162#define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
163 readl(dvo->regs + reg))
164
165static void dvo_dbg_awg_microcode(struct seq_file *s, void __iomem *reg)
166{
167 unsigned int i;
168
169 seq_puts(s, "\n\n");
170 seq_puts(s, " DVO AWG microcode:");
171 for (i = 0; i < AWG_MAX_INST; i++) {
172 if (i % 8 == 0)
173 seq_printf(s, "\n %04X:", i);
174 seq_printf(s, " %04X", readl(reg + i * 4));
175 }
176}
177
178static int dvo_dbg_show(struct seq_file *s, void *data)
179{
180 struct drm_info_node *node = s->private;
181 struct sti_dvo *dvo = (struct sti_dvo *)node->info_ent->data;
182
183 seq_printf(s, "DVO: (vaddr = 0x%p)", dvo->regs);
184 DBGFS_DUMP(DVO_AWG_DIGSYNC_CTRL);
185 DBGFS_DUMP(DVO_DOF_CFG);
186 DBGFS_DUMP(DVO_LUT_PROG_LOW);
187 DBGFS_DUMP(DVO_LUT_PROG_MID);
188 DBGFS_DUMP(DVO_LUT_PROG_HIGH);
189 dvo_dbg_awg_microcode(s, dvo->regs + DVO_DIGSYNC_INSTR_I);
190 seq_putc(s, '\n');
191 return 0;
192}
193
194static struct drm_info_list dvo_debugfs_files[] = {
195 { "dvo", dvo_dbg_show, 0, NULL },
196};
197
198static int dvo_debugfs_init(struct sti_dvo *dvo, struct drm_minor *minor)
199{
200 unsigned int i;
201
202 for (i = 0; i < ARRAY_SIZE(dvo_debugfs_files); i++)
203 dvo_debugfs_files[i].data = dvo;
204
205 return drm_debugfs_create_files(dvo_debugfs_files,
206 ARRAY_SIZE(dvo_debugfs_files),
207 minor->debugfs_root, minor);
208}
209
210static void sti_dvo_disable(struct drm_bridge *bridge)
211{
212 struct sti_dvo *dvo = bridge->driver_private;
213
214 if (!dvo->enabled)
215 return;
216
217 DRM_DEBUG_DRIVER("\n");
218
219 if (dvo->config->awg_fwgen_fct)
220 writel(0x00000000, dvo->regs + DVO_AWG_DIGSYNC_CTRL);
221
222 writel(0x00000000, dvo->regs + DVO_DOF_CFG);
223
224 drm_panel_disable(dvo->panel);
225
226 /* Disable/unprepare dvo clock */
227 clk_disable_unprepare(dvo->clk_pix);
228 clk_disable_unprepare(dvo->clk);
229
230 dvo->enabled = false;
231}
232
233static void sti_dvo_pre_enable(struct drm_bridge *bridge)
234{
235 struct sti_dvo *dvo = bridge->driver_private;
236 struct dvo_config *config = dvo->config;
237 u32 val;
238
239 DRM_DEBUG_DRIVER("\n");
240
241 if (dvo->enabled)
242 return;
243
244 /* Make sure DVO is disabled */
245 writel(0x00000000, dvo->regs + DVO_DOF_CFG);
246 writel(0x00000000, dvo->regs + DVO_AWG_DIGSYNC_CTRL);
247
248 if (config->awg_fwgen_fct) {
249 u8 nb_instr;
250 u32 awg_ram_code[AWG_MAX_INST];
251 /* Configure AWG */
252 if (!dvo_awg_generate_code(dvo, &nb_instr, awg_ram_code))
253 dvo_awg_configure(dvo, awg_ram_code, nb_instr);
254 else
255 return;
256 }
257
258 /* Prepare/enable clocks */
259 if (clk_prepare_enable(dvo->clk_pix))
260 DRM_ERROR("Failed to prepare/enable dvo_pix clk\n");
261 if (clk_prepare_enable(dvo->clk))
262 DRM_ERROR("Failed to prepare/enable dvo clk\n");
263
264 drm_panel_enable(dvo->panel);
265
266 /* Set LUT */
267 writel(config->lowbyte, dvo->regs + DVO_LUT_PROG_LOW);
268 writel(config->midbyte, dvo->regs + DVO_LUT_PROG_MID);
269 writel(config->highbyte, dvo->regs + DVO_LUT_PROG_HIGH);
270
271 /* Digital output formatter config */
272 val = (config->flags | DVO_DOF_EN);
273 writel(val, dvo->regs + DVO_DOF_CFG);
274
275 dvo->enabled = true;
276}
277
278static void sti_dvo_set_mode(struct drm_bridge *bridge,
279 const struct drm_display_mode *mode,
280 const struct drm_display_mode *adjusted_mode)
281{
282 struct sti_dvo *dvo = bridge->driver_private;
283 struct sti_mixer *mixer = to_sti_mixer(dvo->encoder->crtc);
284 int rate = mode->clock * 1000;
285 struct clk *clkp;
286 int ret;
287
288 DRM_DEBUG_DRIVER("\n");
289
290 memcpy(&dvo->mode, mode, sizeof(struct drm_display_mode));
291
292 /* According to the path used (main or aux), the dvo clocks should
293 * have a different parent clock. */
294 if (mixer->id == STI_MIXER_MAIN)
295 clkp = dvo->clk_main_parent;
296 else
297 clkp = dvo->clk_aux_parent;
298
299 if (clkp) {
300 clk_set_parent(dvo->clk_pix, clkp);
301 clk_set_parent(dvo->clk, clkp);
302 }
303
304 /* DVO clocks = compositor clock */
305 ret = clk_set_rate(dvo->clk_pix, rate);
306 if (ret < 0) {
307 DRM_ERROR("Cannot set rate (%dHz) for dvo_pix clk\n", rate);
308 return;
309 }
310
311 ret = clk_set_rate(dvo->clk, rate);
312 if (ret < 0) {
313 DRM_ERROR("Cannot set rate (%dHz) for dvo clk\n", rate);
314 return;
315 }
316
317 /* For now, we only support 24bit data enable (DE) synchro format */
318 dvo->config = &rgb_24bit_de_cfg;
319}
320
321static void sti_dvo_bridge_nope(struct drm_bridge *bridge)
322{
323 /* do nothing */
324}
325
326static const struct drm_bridge_funcs sti_dvo_bridge_funcs = {
327 .pre_enable = sti_dvo_pre_enable,
328 .enable = sti_dvo_bridge_nope,
329 .disable = sti_dvo_disable,
330 .post_disable = sti_dvo_bridge_nope,
331 .mode_set = sti_dvo_set_mode,
332};
333
334static int sti_dvo_connector_get_modes(struct drm_connector *connector)
335{
336 struct sti_dvo_connector *dvo_connector
337 = to_sti_dvo_connector(connector);
338 struct sti_dvo *dvo = dvo_connector->dvo;
339
340 if (dvo->panel)
341 return drm_panel_get_modes(dvo->panel);
342
343 return 0;
344}
345
346#define CLK_TOLERANCE_HZ 50
347
348static int sti_dvo_connector_mode_valid(struct drm_connector *connector,
349 struct drm_display_mode *mode)
350{
351 int target = mode->clock * 1000;
352 int target_min = target - CLK_TOLERANCE_HZ;
353 int target_max = target + CLK_TOLERANCE_HZ;
354 int result;
355 struct sti_dvo_connector *dvo_connector
356 = to_sti_dvo_connector(connector);
357 struct sti_dvo *dvo = dvo_connector->dvo;
358
359 result = clk_round_rate(dvo->clk_pix, target);
360
361 DRM_DEBUG_DRIVER("target rate = %d => available rate = %d\n",
362 target, result);
363
364 if ((result < target_min) || (result > target_max)) {
365 DRM_DEBUG_DRIVER("dvo pixclk=%d not supported\n", target);
366 return MODE_BAD;
367 }
368
369 return MODE_OK;
370}
371
372static const
373struct drm_connector_helper_funcs sti_dvo_connector_helper_funcs = {
374 .get_modes = sti_dvo_connector_get_modes,
375 .mode_valid = sti_dvo_connector_mode_valid,
376};
377
378static enum drm_connector_status
379sti_dvo_connector_detect(struct drm_connector *connector, bool force)
380{
381 struct sti_dvo_connector *dvo_connector
382 = to_sti_dvo_connector(connector);
383 struct sti_dvo *dvo = dvo_connector->dvo;
384
385 DRM_DEBUG_DRIVER("\n");
386
387 if (!dvo->panel) {
388 dvo->panel = of_drm_find_panel(dvo->panel_node);
389 if (IS_ERR(dvo->panel))
390 dvo->panel = NULL;
391 else
392 drm_panel_attach(dvo->panel, connector);
393 }
394
395 if (dvo->panel)
396 return connector_status_connected;
397
398 return connector_status_disconnected;
399}
400
401static int sti_dvo_late_register(struct drm_connector *connector)
402{
403 struct sti_dvo_connector *dvo_connector
404 = to_sti_dvo_connector(connector);
405 struct sti_dvo *dvo = dvo_connector->dvo;
406
407 if (dvo_debugfs_init(dvo, dvo->drm_dev->primary)) {
408 DRM_ERROR("DVO debugfs setup failed\n");
409 return -EINVAL;
410 }
411
412 return 0;
413}
414
415static const struct drm_connector_funcs sti_dvo_connector_funcs = {
416 .fill_modes = drm_helper_probe_single_connector_modes,
417 .detect = sti_dvo_connector_detect,
418 .destroy = drm_connector_cleanup,
419 .reset = drm_atomic_helper_connector_reset,
420 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
421 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
422 .late_register = sti_dvo_late_register,
423};
424
425static struct drm_encoder *sti_dvo_find_encoder(struct drm_device *dev)
426{
427 struct drm_encoder *encoder;
428
429 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
430 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
431 return encoder;
432 }
433
434 return NULL;
435}
436
437static int sti_dvo_bind(struct device *dev, struct device *master, void *data)
438{
439 struct sti_dvo *dvo = dev_get_drvdata(dev);
440 struct drm_device *drm_dev = data;
441 struct drm_encoder *encoder;
442 struct sti_dvo_connector *connector;
443 struct drm_connector *drm_connector;
444 struct drm_bridge *bridge;
445 int err;
446
447 /* Set the drm device handle */
448 dvo->drm_dev = drm_dev;
449
450 encoder = sti_dvo_find_encoder(drm_dev);
451 if (!encoder)
452 return -ENOMEM;
453
454 connector = devm_kzalloc(dev, sizeof(*connector), GFP_KERNEL);
455 if (!connector)
456 return -ENOMEM;
457
458 connector->dvo = dvo;
459
460 bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
461 if (!bridge)
462 return -ENOMEM;
463
464 bridge->driver_private = dvo;
465 bridge->funcs = &sti_dvo_bridge_funcs;
466 bridge->of_node = dvo->dev.of_node;
467 drm_bridge_add(bridge);
468
469 err = drm_bridge_attach(encoder, bridge, NULL);
470 if (err) {
471 DRM_ERROR("Failed to attach bridge\n");
472 return err;
473 }
474
475 dvo->bridge = bridge;
476 connector->encoder = encoder;
477 dvo->encoder = encoder;
478
479 drm_connector = (struct drm_connector *)connector;
480
481 drm_connector->polled = DRM_CONNECTOR_POLL_HPD;
482
483 drm_connector_init(drm_dev, drm_connector,
484 &sti_dvo_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
485 drm_connector_helper_add(drm_connector,
486 &sti_dvo_connector_helper_funcs);
487
488 err = drm_connector_attach_encoder(drm_connector, encoder);
489 if (err) {
490 DRM_ERROR("Failed to attach a connector to a encoder\n");
491 goto err_sysfs;
492 }
493
494 return 0;
495
496err_sysfs:
497 drm_bridge_remove(bridge);
498 return -EINVAL;
499}
500
501static void sti_dvo_unbind(struct device *dev,
502 struct device *master, void *data)
503{
504 struct sti_dvo *dvo = dev_get_drvdata(dev);
505
506 drm_bridge_remove(dvo->bridge);
507}
508
509static const struct component_ops sti_dvo_ops = {
510 .bind = sti_dvo_bind,
511 .unbind = sti_dvo_unbind,
512};
513
514static int sti_dvo_probe(struct platform_device *pdev)
515{
516 struct device *dev = &pdev->dev;
517 struct sti_dvo *dvo;
518 struct resource *res;
519 struct device_node *np = dev->of_node;
520
521 DRM_INFO("%s\n", __func__);
522
523 dvo = devm_kzalloc(dev, sizeof(*dvo), GFP_KERNEL);
524 if (!dvo) {
525 DRM_ERROR("Failed to allocate memory for DVO\n");
526 return -ENOMEM;
527 }
528
529 dvo->dev = pdev->dev;
530
531 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvo-reg");
532 if (!res) {
533 DRM_ERROR("Invalid dvo resource\n");
534 return -ENOMEM;
535 }
536 dvo->regs = devm_ioremap_nocache(dev, res->start,
537 resource_size(res));
538 if (!dvo->regs)
539 return -ENOMEM;
540
541 dvo->clk_pix = devm_clk_get(dev, "dvo_pix");
542 if (IS_ERR(dvo->clk_pix)) {
543 DRM_ERROR("Cannot get dvo_pix clock\n");
544 return PTR_ERR(dvo->clk_pix);
545 }
546
547 dvo->clk = devm_clk_get(dev, "dvo");
548 if (IS_ERR(dvo->clk)) {
549 DRM_ERROR("Cannot get dvo clock\n");
550 return PTR_ERR(dvo->clk);
551 }
552
553 dvo->clk_main_parent = devm_clk_get(dev, "main_parent");
554 if (IS_ERR(dvo->clk_main_parent)) {
555 DRM_DEBUG_DRIVER("Cannot get main_parent clock\n");
556 dvo->clk_main_parent = NULL;
557 }
558
559 dvo->clk_aux_parent = devm_clk_get(dev, "aux_parent");
560 if (IS_ERR(dvo->clk_aux_parent)) {
561 DRM_DEBUG_DRIVER("Cannot get aux_parent clock\n");
562 dvo->clk_aux_parent = NULL;
563 }
564
565 dvo->panel_node = of_parse_phandle(np, "sti,panel", 0);
566 if (!dvo->panel_node)
567 DRM_ERROR("No panel associated to the dvo output\n");
568 of_node_put(dvo->panel_node);
569
570 platform_set_drvdata(pdev, dvo);
571
572 return component_add(&pdev->dev, &sti_dvo_ops);
573}
574
575static int sti_dvo_remove(struct platform_device *pdev)
576{
577 component_del(&pdev->dev, &sti_dvo_ops);
578 return 0;
579}
580
581static const struct of_device_id dvo_of_match[] = {
582 { .compatible = "st,stih407-dvo", },
583 { /* end node */ }
584};
585MODULE_DEVICE_TABLE(of, dvo_of_match);
586
587struct platform_driver sti_dvo_driver = {
588 .driver = {
589 .name = "sti-dvo",
590 .owner = THIS_MODULE,
591 .of_match_table = dvo_of_match,
592 },
593 .probe = sti_dvo_probe,
594 .remove = sti_dvo_remove,
595};
596
597MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
598MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
599MODULE_LICENSE("GPL");
1/*
2 * Copyright (C) STMicroelectronics SA 2014
3 * Author: Vincent Abriou <vincent.abriou@st.com> for STMicroelectronics.
4 * License terms: GNU General Public License (GPL), version 2
5 */
6
7#include <linux/clk.h>
8#include <linux/component.h>
9#include <linux/debugfs.h>
10#include <linux/module.h>
11#include <linux/of_gpio.h>
12#include <linux/platform_device.h>
13
14#include <drm/drmP.h>
15#include <drm/drm_atomic_helper.h>
16#include <drm/drm_crtc_helper.h>
17#include <drm/drm_panel.h>
18
19#include "sti_awg_utils.h"
20#include "sti_drv.h"
21#include "sti_mixer.h"
22
23/* DVO registers */
24#define DVO_AWG_DIGSYNC_CTRL 0x0000
25#define DVO_DOF_CFG 0x0004
26#define DVO_LUT_PROG_LOW 0x0008
27#define DVO_LUT_PROG_MID 0x000C
28#define DVO_LUT_PROG_HIGH 0x0010
29#define DVO_DIGSYNC_INSTR_I 0x0100
30
31#define DVO_AWG_CTRL_EN BIT(0)
32#define DVO_AWG_FRAME_BASED_SYNC BIT(2)
33
34#define DVO_DOF_EN_LOWBYTE BIT(0)
35#define DVO_DOF_EN_MIDBYTE BIT(1)
36#define DVO_DOF_EN_HIGHBYTE BIT(2)
37#define DVO_DOF_EN BIT(6)
38#define DVO_DOF_MOD_COUNT_SHIFT 8
39
40#define DVO_LUT_ZERO 0
41#define DVO_LUT_Y_G 1
42#define DVO_LUT_Y_G_DEL 2
43#define DVO_LUT_CB_B 3
44#define DVO_LUT_CB_B_DEL 4
45#define DVO_LUT_CR_R 5
46#define DVO_LUT_CR_R_DEL 6
47#define DVO_LUT_HOLD 7
48
49struct dvo_config {
50 u32 flags;
51 u32 lowbyte;
52 u32 midbyte;
53 u32 highbyte;
54 int (*awg_fwgen_fct)(
55 struct awg_code_generation_params *fw_gen_params,
56 struct awg_timing *timing);
57};
58
59static struct dvo_config rgb_24bit_de_cfg = {
60 .flags = (0L << DVO_DOF_MOD_COUNT_SHIFT),
61 .lowbyte = DVO_LUT_CR_R,
62 .midbyte = DVO_LUT_Y_G,
63 .highbyte = DVO_LUT_CB_B,
64 .awg_fwgen_fct = sti_awg_generate_code_data_enable_mode,
65};
66
67/**
68 * STI digital video output structure
69 *
70 * @dev: driver device
71 * @drm_dev: pointer to drm device
72 * @mode: current display mode selected
73 * @regs: dvo registers
74 * @clk_pix: pixel clock for dvo
75 * @clk: clock for dvo
76 * @clk_main_parent: dvo parent clock if main path used
77 * @clk_aux_parent: dvo parent clock if aux path used
78 * @panel_node: panel node reference from device tree
79 * @panel: reference to the panel connected to the dvo
80 * @enabled: true if dvo is enabled else false
81 * @encoder: drm_encoder it is bound
82 */
83struct sti_dvo {
84 struct device dev;
85 struct drm_device *drm_dev;
86 struct drm_display_mode mode;
87 void __iomem *regs;
88 struct clk *clk_pix;
89 struct clk *clk;
90 struct clk *clk_main_parent;
91 struct clk *clk_aux_parent;
92 struct device_node *panel_node;
93 struct drm_panel *panel;
94 struct dvo_config *config;
95 bool enabled;
96 struct drm_encoder *encoder;
97 struct drm_bridge *bridge;
98};
99
100struct sti_dvo_connector {
101 struct drm_connector drm_connector;
102 struct drm_encoder *encoder;
103 struct sti_dvo *dvo;
104};
105
106#define to_sti_dvo_connector(x) \
107 container_of(x, struct sti_dvo_connector, drm_connector)
108
109#define BLANKING_LEVEL 16
110static int dvo_awg_generate_code(struct sti_dvo *dvo, u8 *ram_size, u32 *ram_code)
111{
112 struct drm_display_mode *mode = &dvo->mode;
113 struct dvo_config *config = dvo->config;
114 struct awg_code_generation_params fw_gen_params;
115 struct awg_timing timing;
116
117 fw_gen_params.ram_code = ram_code;
118 fw_gen_params.instruction_offset = 0;
119
120 timing.total_lines = mode->vtotal;
121 timing.active_lines = mode->vdisplay;
122 timing.blanking_lines = mode->vsync_start - mode->vdisplay;
123 timing.trailing_lines = mode->vtotal - mode->vsync_start;
124 timing.total_pixels = mode->htotal;
125 timing.active_pixels = mode->hdisplay;
126 timing.blanking_pixels = mode->hsync_start - mode->hdisplay;
127 timing.trailing_pixels = mode->htotal - mode->hsync_start;
128 timing.blanking_level = BLANKING_LEVEL;
129
130 if (config->awg_fwgen_fct(&fw_gen_params, &timing)) {
131 DRM_ERROR("AWG firmware not properly generated\n");
132 return -EINVAL;
133 }
134
135 *ram_size = fw_gen_params.instruction_offset;
136
137 return 0;
138}
139
140/* Configure AWG, writing instructions
141 *
142 * @dvo: pointer to DVO structure
143 * @awg_ram_code: pointer to AWG instructions table
144 * @nb: nb of AWG instructions
145 */
146static void dvo_awg_configure(struct sti_dvo *dvo, u32 *awg_ram_code, int nb)
147{
148 int i;
149
150 DRM_DEBUG_DRIVER("\n");
151
152 for (i = 0; i < nb; i++)
153 writel(awg_ram_code[i],
154 dvo->regs + DVO_DIGSYNC_INSTR_I + i * 4);
155 for (i = nb; i < AWG_MAX_INST; i++)
156 writel(0, dvo->regs + DVO_DIGSYNC_INSTR_I + i * 4);
157
158 writel(DVO_AWG_CTRL_EN, dvo->regs + DVO_AWG_DIGSYNC_CTRL);
159}
160
161#define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
162 readl(dvo->regs + reg))
163
164static void dvo_dbg_awg_microcode(struct seq_file *s, void __iomem *reg)
165{
166 unsigned int i;
167
168 seq_puts(s, "\n\n");
169 seq_puts(s, " DVO AWG microcode:");
170 for (i = 0; i < AWG_MAX_INST; i++) {
171 if (i % 8 == 0)
172 seq_printf(s, "\n %04X:", i);
173 seq_printf(s, " %04X", readl(reg + i * 4));
174 }
175}
176
177static int dvo_dbg_show(struct seq_file *s, void *data)
178{
179 struct drm_info_node *node = s->private;
180 struct sti_dvo *dvo = (struct sti_dvo *)node->info_ent->data;
181
182 seq_printf(s, "DVO: (vaddr = 0x%p)", dvo->regs);
183 DBGFS_DUMP(DVO_AWG_DIGSYNC_CTRL);
184 DBGFS_DUMP(DVO_DOF_CFG);
185 DBGFS_DUMP(DVO_LUT_PROG_LOW);
186 DBGFS_DUMP(DVO_LUT_PROG_MID);
187 DBGFS_DUMP(DVO_LUT_PROG_HIGH);
188 dvo_dbg_awg_microcode(s, dvo->regs + DVO_DIGSYNC_INSTR_I);
189 seq_puts(s, "\n");
190
191 return 0;
192}
193
194static struct drm_info_list dvo_debugfs_files[] = {
195 { "dvo", dvo_dbg_show, 0, NULL },
196};
197
198static void dvo_debugfs_exit(struct sti_dvo *dvo, struct drm_minor *minor)
199{
200 drm_debugfs_remove_files(dvo_debugfs_files,
201 ARRAY_SIZE(dvo_debugfs_files),
202 minor);
203}
204
205static int dvo_debugfs_init(struct sti_dvo *dvo, struct drm_minor *minor)
206{
207 unsigned int i;
208
209 for (i = 0; i < ARRAY_SIZE(dvo_debugfs_files); i++)
210 dvo_debugfs_files[i].data = dvo;
211
212 return drm_debugfs_create_files(dvo_debugfs_files,
213 ARRAY_SIZE(dvo_debugfs_files),
214 minor->debugfs_root, minor);
215}
216
217static void sti_dvo_disable(struct drm_bridge *bridge)
218{
219 struct sti_dvo *dvo = bridge->driver_private;
220
221 if (!dvo->enabled)
222 return;
223
224 DRM_DEBUG_DRIVER("\n");
225
226 if (dvo->config->awg_fwgen_fct)
227 writel(0x00000000, dvo->regs + DVO_AWG_DIGSYNC_CTRL);
228
229 writel(0x00000000, dvo->regs + DVO_DOF_CFG);
230
231 if (dvo->panel)
232 dvo->panel->funcs->disable(dvo->panel);
233
234 /* Disable/unprepare dvo clock */
235 clk_disable_unprepare(dvo->clk_pix);
236 clk_disable_unprepare(dvo->clk);
237
238 dvo->enabled = false;
239}
240
241static void sti_dvo_pre_enable(struct drm_bridge *bridge)
242{
243 struct sti_dvo *dvo = bridge->driver_private;
244 struct dvo_config *config = dvo->config;
245 u32 val;
246
247 DRM_DEBUG_DRIVER("\n");
248
249 if (dvo->enabled)
250 return;
251
252 /* Make sure DVO is disabled */
253 writel(0x00000000, dvo->regs + DVO_DOF_CFG);
254 writel(0x00000000, dvo->regs + DVO_AWG_DIGSYNC_CTRL);
255
256 if (config->awg_fwgen_fct) {
257 u8 nb_instr;
258 u32 awg_ram_code[AWG_MAX_INST];
259 /* Configure AWG */
260 if (!dvo_awg_generate_code(dvo, &nb_instr, awg_ram_code))
261 dvo_awg_configure(dvo, awg_ram_code, nb_instr);
262 else
263 return;
264 }
265
266 /* Prepare/enable clocks */
267 if (clk_prepare_enable(dvo->clk_pix))
268 DRM_ERROR("Failed to prepare/enable dvo_pix clk\n");
269 if (clk_prepare_enable(dvo->clk))
270 DRM_ERROR("Failed to prepare/enable dvo clk\n");
271
272 if (dvo->panel)
273 dvo->panel->funcs->enable(dvo->panel);
274
275 /* Set LUT */
276 writel(config->lowbyte, dvo->regs + DVO_LUT_PROG_LOW);
277 writel(config->midbyte, dvo->regs + DVO_LUT_PROG_MID);
278 writel(config->highbyte, dvo->regs + DVO_LUT_PROG_HIGH);
279
280 /* Digital output formatter config */
281 val = (config->flags | DVO_DOF_EN);
282 writel(val, dvo->regs + DVO_DOF_CFG);
283
284 dvo->enabled = true;
285}
286
287static void sti_dvo_set_mode(struct drm_bridge *bridge,
288 struct drm_display_mode *mode,
289 struct drm_display_mode *adjusted_mode)
290{
291 struct sti_dvo *dvo = bridge->driver_private;
292 struct sti_mixer *mixer = to_sti_mixer(dvo->encoder->crtc);
293 int rate = mode->clock * 1000;
294 struct clk *clkp;
295 int ret;
296
297 DRM_DEBUG_DRIVER("\n");
298
299 memcpy(&dvo->mode, mode, sizeof(struct drm_display_mode));
300
301 /* According to the path used (main or aux), the dvo clocks should
302 * have a different parent clock. */
303 if (mixer->id == STI_MIXER_MAIN)
304 clkp = dvo->clk_main_parent;
305 else
306 clkp = dvo->clk_aux_parent;
307
308 if (clkp) {
309 clk_set_parent(dvo->clk_pix, clkp);
310 clk_set_parent(dvo->clk, clkp);
311 }
312
313 /* DVO clocks = compositor clock */
314 ret = clk_set_rate(dvo->clk_pix, rate);
315 if (ret < 0) {
316 DRM_ERROR("Cannot set rate (%dHz) for dvo_pix clk\n", rate);
317 return;
318 }
319
320 ret = clk_set_rate(dvo->clk, rate);
321 if (ret < 0) {
322 DRM_ERROR("Cannot set rate (%dHz) for dvo clk\n", rate);
323 return;
324 }
325
326 /* For now, we only support 24bit data enable (DE) synchro format */
327 dvo->config = &rgb_24bit_de_cfg;
328}
329
330static void sti_dvo_bridge_nope(struct drm_bridge *bridge)
331{
332 /* do nothing */
333}
334
335static const struct drm_bridge_funcs sti_dvo_bridge_funcs = {
336 .pre_enable = sti_dvo_pre_enable,
337 .enable = sti_dvo_bridge_nope,
338 .disable = sti_dvo_disable,
339 .post_disable = sti_dvo_bridge_nope,
340 .mode_set = sti_dvo_set_mode,
341};
342
343static int sti_dvo_connector_get_modes(struct drm_connector *connector)
344{
345 struct sti_dvo_connector *dvo_connector
346 = to_sti_dvo_connector(connector);
347 struct sti_dvo *dvo = dvo_connector->dvo;
348
349 if (dvo->panel)
350 return dvo->panel->funcs->get_modes(dvo->panel);
351
352 return 0;
353}
354
355#define CLK_TOLERANCE_HZ 50
356
357static int sti_dvo_connector_mode_valid(struct drm_connector *connector,
358 struct drm_display_mode *mode)
359{
360 int target = mode->clock * 1000;
361 int target_min = target - CLK_TOLERANCE_HZ;
362 int target_max = target + CLK_TOLERANCE_HZ;
363 int result;
364 struct sti_dvo_connector *dvo_connector
365 = to_sti_dvo_connector(connector);
366 struct sti_dvo *dvo = dvo_connector->dvo;
367
368 result = clk_round_rate(dvo->clk_pix, target);
369
370 DRM_DEBUG_DRIVER("target rate = %d => available rate = %d\n",
371 target, result);
372
373 if ((result < target_min) || (result > target_max)) {
374 DRM_DEBUG_DRIVER("dvo pixclk=%d not supported\n", target);
375 return MODE_BAD;
376 }
377
378 return MODE_OK;
379}
380
381static const
382struct drm_connector_helper_funcs sti_dvo_connector_helper_funcs = {
383 .get_modes = sti_dvo_connector_get_modes,
384 .mode_valid = sti_dvo_connector_mode_valid,
385};
386
387static enum drm_connector_status
388sti_dvo_connector_detect(struct drm_connector *connector, bool force)
389{
390 struct sti_dvo_connector *dvo_connector
391 = to_sti_dvo_connector(connector);
392 struct sti_dvo *dvo = dvo_connector->dvo;
393
394 DRM_DEBUG_DRIVER("\n");
395
396 if (!dvo->panel) {
397 dvo->panel = of_drm_find_panel(dvo->panel_node);
398 if (dvo->panel)
399 drm_panel_attach(dvo->panel, connector);
400 }
401
402 if (dvo->panel)
403 return connector_status_connected;
404
405 return connector_status_disconnected;
406}
407
408static int sti_dvo_late_register(struct drm_connector *connector)
409{
410 struct sti_dvo_connector *dvo_connector
411 = to_sti_dvo_connector(connector);
412 struct sti_dvo *dvo = dvo_connector->dvo;
413
414 if (dvo_debugfs_init(dvo, dvo->drm_dev->primary)) {
415 DRM_ERROR("DVO debugfs setup failed\n");
416 return -EINVAL;
417 }
418
419 return 0;
420}
421
422static const struct drm_connector_funcs sti_dvo_connector_funcs = {
423 .dpms = drm_atomic_helper_connector_dpms,
424 .fill_modes = drm_helper_probe_single_connector_modes,
425 .detect = sti_dvo_connector_detect,
426 .destroy = drm_connector_cleanup,
427 .reset = drm_atomic_helper_connector_reset,
428 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
429 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
430 .late_register = sti_dvo_late_register,
431};
432
433static struct drm_encoder *sti_dvo_find_encoder(struct drm_device *dev)
434{
435 struct drm_encoder *encoder;
436
437 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
438 if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
439 return encoder;
440 }
441
442 return NULL;
443}
444
445static int sti_dvo_bind(struct device *dev, struct device *master, void *data)
446{
447 struct sti_dvo *dvo = dev_get_drvdata(dev);
448 struct drm_device *drm_dev = data;
449 struct drm_encoder *encoder;
450 struct sti_dvo_connector *connector;
451 struct drm_connector *drm_connector;
452 struct drm_bridge *bridge;
453 int err;
454
455 /* Set the drm device handle */
456 dvo->drm_dev = drm_dev;
457
458 encoder = sti_dvo_find_encoder(drm_dev);
459 if (!encoder)
460 return -ENOMEM;
461
462 connector = devm_kzalloc(dev, sizeof(*connector), GFP_KERNEL);
463 if (!connector)
464 return -ENOMEM;
465
466 connector->dvo = dvo;
467
468 bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
469 if (!bridge)
470 return -ENOMEM;
471
472 bridge->driver_private = dvo;
473 bridge->funcs = &sti_dvo_bridge_funcs;
474 bridge->of_node = dvo->dev.of_node;
475 err = drm_bridge_add(bridge);
476 if (err) {
477 DRM_ERROR("Failed to add bridge\n");
478 return err;
479 }
480
481 err = drm_bridge_attach(drm_dev, bridge);
482 if (err) {
483 DRM_ERROR("Failed to attach bridge\n");
484 return err;
485 }
486
487 dvo->bridge = bridge;
488 encoder->bridge = bridge;
489 connector->encoder = encoder;
490 dvo->encoder = encoder;
491
492 drm_connector = (struct drm_connector *)connector;
493
494 drm_connector->polled = DRM_CONNECTOR_POLL_HPD;
495
496 drm_connector_init(drm_dev, drm_connector,
497 &sti_dvo_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
498 drm_connector_helper_add(drm_connector,
499 &sti_dvo_connector_helper_funcs);
500
501 err = drm_mode_connector_attach_encoder(drm_connector, encoder);
502 if (err) {
503 DRM_ERROR("Failed to attach a connector to a encoder\n");
504 goto err_sysfs;
505 }
506
507 return 0;
508
509err_sysfs:
510 drm_bridge_remove(bridge);
511 return -EINVAL;
512}
513
514static void sti_dvo_unbind(struct device *dev,
515 struct device *master, void *data)
516{
517 struct sti_dvo *dvo = dev_get_drvdata(dev);
518 struct drm_device *drm_dev = data;
519
520 dvo_debugfs_exit(dvo, drm_dev->primary);
521
522 drm_bridge_remove(dvo->bridge);
523}
524
525static const struct component_ops sti_dvo_ops = {
526 .bind = sti_dvo_bind,
527 .unbind = sti_dvo_unbind,
528};
529
530static int sti_dvo_probe(struct platform_device *pdev)
531{
532 struct device *dev = &pdev->dev;
533 struct sti_dvo *dvo;
534 struct resource *res;
535 struct device_node *np = dev->of_node;
536
537 DRM_INFO("%s\n", __func__);
538
539 dvo = devm_kzalloc(dev, sizeof(*dvo), GFP_KERNEL);
540 if (!dvo) {
541 DRM_ERROR("Failed to allocate memory for DVO\n");
542 return -ENOMEM;
543 }
544
545 dvo->dev = pdev->dev;
546
547 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dvo-reg");
548 if (!res) {
549 DRM_ERROR("Invalid dvo resource\n");
550 return -ENOMEM;
551 }
552 dvo->regs = devm_ioremap_nocache(dev, res->start,
553 resource_size(res));
554 if (!dvo->regs)
555 return -ENOMEM;
556
557 dvo->clk_pix = devm_clk_get(dev, "dvo_pix");
558 if (IS_ERR(dvo->clk_pix)) {
559 DRM_ERROR("Cannot get dvo_pix clock\n");
560 return PTR_ERR(dvo->clk_pix);
561 }
562
563 dvo->clk = devm_clk_get(dev, "dvo");
564 if (IS_ERR(dvo->clk)) {
565 DRM_ERROR("Cannot get dvo clock\n");
566 return PTR_ERR(dvo->clk);
567 }
568
569 dvo->clk_main_parent = devm_clk_get(dev, "main_parent");
570 if (IS_ERR(dvo->clk_main_parent)) {
571 DRM_DEBUG_DRIVER("Cannot get main_parent clock\n");
572 dvo->clk_main_parent = NULL;
573 }
574
575 dvo->clk_aux_parent = devm_clk_get(dev, "aux_parent");
576 if (IS_ERR(dvo->clk_aux_parent)) {
577 DRM_DEBUG_DRIVER("Cannot get aux_parent clock\n");
578 dvo->clk_aux_parent = NULL;
579 }
580
581 dvo->panel_node = of_parse_phandle(np, "sti,panel", 0);
582 if (!dvo->panel_node)
583 DRM_ERROR("No panel associated to the dvo output\n");
584 of_node_put(dvo->panel_node);
585
586 platform_set_drvdata(pdev, dvo);
587
588 return component_add(&pdev->dev, &sti_dvo_ops);
589}
590
591static int sti_dvo_remove(struct platform_device *pdev)
592{
593 component_del(&pdev->dev, &sti_dvo_ops);
594 return 0;
595}
596
597static struct of_device_id dvo_of_match[] = {
598 { .compatible = "st,stih407-dvo", },
599 { /* end node */ }
600};
601MODULE_DEVICE_TABLE(of, dvo_of_match);
602
603struct platform_driver sti_dvo_driver = {
604 .driver = {
605 .name = "sti-dvo",
606 .owner = THIS_MODULE,
607 .of_match_table = dvo_of_match,
608 },
609 .probe = sti_dvo_probe,
610 .remove = sti_dvo_remove,
611};
612
613MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
614MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
615MODULE_LICENSE("GPL");