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
8 #include <linux/component.h>
9 #include <linux/module.h>
10 #include <linux/of_gpio.h>
11 #include <linux/platform_device.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_crtc_helper.h>
16 #include <drm/drm_panel.h>
18 #include "sti_awg_utils.h"
19 #include "sti_mixer.h"
22 #define DVO_AWG_DIGSYNC_CTRL 0x0000
23 #define DVO_DOF_CFG 0x0004
24 #define DVO_LUT_PROG_LOW 0x0008
25 #define DVO_LUT_PROG_MID 0x000C
26 #define DVO_LUT_PROG_HIGH 0x0010
27 #define DVO_DIGSYNC_INSTR_I 0x0100
29 #define DVO_AWG_CTRL_EN BIT(0)
30 #define DVO_AWG_FRAME_BASED_SYNC BIT(2)
32 #define DVO_DOF_EN_LOWBYTE BIT(0)
33 #define DVO_DOF_EN_MIDBYTE BIT(1)
34 #define DVO_DOF_EN_HIGHBYTE BIT(2)
35 #define DVO_DOF_EN BIT(6)
36 #define DVO_DOF_MOD_COUNT_SHIFT 8
38 #define DVO_LUT_ZERO 0
40 #define DVO_LUT_Y_G_DEL 2
41 #define DVO_LUT_CB_B 3
42 #define DVO_LUT_CB_B_DEL 4
43 #define DVO_LUT_CR_R 5
44 #define DVO_LUT_CR_R_DEL 6
45 #define DVO_LUT_HOLD 7
53 struct awg_code_generation_params
*fw_gen_params
,
54 struct awg_timing
*timing
);
57 static struct dvo_config rgb_24bit_de_cfg
= {
58 .flags
= (0L << DVO_DOF_MOD_COUNT_SHIFT
),
59 .lowbyte
= DVO_LUT_CR_R
,
60 .midbyte
= DVO_LUT_Y_G
,
61 .highbyte
= DVO_LUT_CB_B
,
62 .awg_fwgen_fct
= sti_awg_generate_code_data_enable_mode
,
66 * STI digital video output structure
69 * @drm_dev: pointer to drm device
70 * @mode: current display mode selected
71 * @regs: dvo registers
72 * @clk_pix: pixel clock for dvo
74 * @clk_main_parent: dvo parent clock if main path used
75 * @clk_aux_parent: dvo parent clock if aux path used
76 * @panel_node: panel node reference from device tree
77 * @panel: reference to the panel connected to the dvo
78 * @enabled: true if dvo is enabled else false
79 * @encoder: drm_encoder it is bound
83 struct drm_device
*drm_dev
;
84 struct drm_display_mode mode
;
88 struct clk
*clk_main_parent
;
89 struct clk
*clk_aux_parent
;
90 struct device_node
*panel_node
;
91 struct drm_panel
*panel
;
92 struct dvo_config
*config
;
94 struct drm_encoder
*encoder
;
95 struct drm_bridge
*bridge
;
98 struct sti_dvo_connector
{
99 struct drm_connector drm_connector
;
100 struct drm_encoder
*encoder
;
104 #define to_sti_dvo_connector(x) \
105 container_of(x, struct sti_dvo_connector, drm_connector)
107 #define BLANKING_LEVEL 16
108 int dvo_awg_generate_code(struct sti_dvo
*dvo
, u8
*ram_size
, u32
*ram_code
)
110 struct drm_display_mode
*mode
= &dvo
->mode
;
111 struct dvo_config
*config
= dvo
->config
;
112 struct awg_code_generation_params fw_gen_params
;
113 struct awg_timing timing
;
115 fw_gen_params
.ram_code
= ram_code
;
116 fw_gen_params
.instruction_offset
= 0;
118 timing
.total_lines
= mode
->vtotal
;
119 timing
.active_lines
= mode
->vdisplay
;
120 timing
.blanking_lines
= mode
->vsync_start
- mode
->vdisplay
;
121 timing
.trailing_lines
= mode
->vtotal
- mode
->vsync_start
;
122 timing
.total_pixels
= mode
->htotal
;
123 timing
.active_pixels
= mode
->hdisplay
;
124 timing
.blanking_pixels
= mode
->hsync_start
- mode
->hdisplay
;
125 timing
.trailing_pixels
= mode
->htotal
- mode
->hsync_start
;
126 timing
.blanking_level
= BLANKING_LEVEL
;
128 if (config
->awg_fwgen_fct(&fw_gen_params
, &timing
)) {
129 DRM_ERROR("AWG firmware not properly generated\n");
133 *ram_size
= fw_gen_params
.instruction_offset
;
138 /* Configure AWG, writing instructions
140 * @dvo: pointer to DVO structure
141 * @awg_ram_code: pointer to AWG instructions table
142 * @nb: nb of AWG instructions
144 static void dvo_awg_configure(struct sti_dvo
*dvo
, u32
*awg_ram_code
, int nb
)
148 DRM_DEBUG_DRIVER("\n");
150 for (i
= 0; i
< nb
; i
++)
151 writel(awg_ram_code
[i
],
152 dvo
->regs
+ DVO_DIGSYNC_INSTR_I
+ i
* 4);
153 for (i
= nb
; i
< AWG_MAX_INST
; i
++)
154 writel(0, dvo
->regs
+ DVO_DIGSYNC_INSTR_I
+ i
* 4);
156 writel(DVO_AWG_CTRL_EN
, dvo
->regs
+ DVO_AWG_DIGSYNC_CTRL
);
159 static void sti_dvo_disable(struct drm_bridge
*bridge
)
161 struct sti_dvo
*dvo
= bridge
->driver_private
;
166 DRM_DEBUG_DRIVER("\n");
168 if (dvo
->config
->awg_fwgen_fct
)
169 writel(0x00000000, dvo
->regs
+ DVO_AWG_DIGSYNC_CTRL
);
171 writel(0x00000000, dvo
->regs
+ DVO_DOF_CFG
);
174 dvo
->panel
->funcs
->disable(dvo
->panel
);
176 /* Disable/unprepare dvo clock */
177 clk_disable_unprepare(dvo
->clk_pix
);
178 clk_disable_unprepare(dvo
->clk
);
180 dvo
->enabled
= false;
183 static void sti_dvo_pre_enable(struct drm_bridge
*bridge
)
185 struct sti_dvo
*dvo
= bridge
->driver_private
;
186 struct dvo_config
*config
= dvo
->config
;
189 DRM_DEBUG_DRIVER("\n");
194 /* Make sure DVO is disabled */
195 writel(0x00000000, dvo
->regs
+ DVO_DOF_CFG
);
196 writel(0x00000000, dvo
->regs
+ DVO_AWG_DIGSYNC_CTRL
);
198 if (config
->awg_fwgen_fct
) {
200 u32 awg_ram_code
[AWG_MAX_INST
];
202 if (!dvo_awg_generate_code(dvo
, &nb_instr
, awg_ram_code
))
203 dvo_awg_configure(dvo
, awg_ram_code
, nb_instr
);
208 /* Prepare/enable clocks */
209 if (clk_prepare_enable(dvo
->clk_pix
))
210 DRM_ERROR("Failed to prepare/enable dvo_pix clk\n");
211 if (clk_prepare_enable(dvo
->clk
))
212 DRM_ERROR("Failed to prepare/enable dvo clk\n");
215 dvo
->panel
->funcs
->enable(dvo
->panel
);
218 writel(config
->lowbyte
, dvo
->regs
+ DVO_LUT_PROG_LOW
);
219 writel(config
->midbyte
, dvo
->regs
+ DVO_LUT_PROG_MID
);
220 writel(config
->highbyte
, dvo
->regs
+ DVO_LUT_PROG_HIGH
);
222 /* Digital output formatter config */
223 val
= (config
->flags
| DVO_DOF_EN
);
224 writel(val
, dvo
->regs
+ DVO_DOF_CFG
);
229 static void sti_dvo_set_mode(struct drm_bridge
*bridge
,
230 struct drm_display_mode
*mode
,
231 struct drm_display_mode
*adjusted_mode
)
233 struct sti_dvo
*dvo
= bridge
->driver_private
;
234 struct sti_mixer
*mixer
= to_sti_mixer(dvo
->encoder
->crtc
);
235 int rate
= mode
->clock
* 1000;
239 DRM_DEBUG_DRIVER("\n");
241 memcpy(&dvo
->mode
, mode
, sizeof(struct drm_display_mode
));
243 /* According to the path used (main or aux), the dvo clocks should
244 * have a different parent clock. */
245 if (mixer
->id
== STI_MIXER_MAIN
)
246 clkp
= dvo
->clk_main_parent
;
248 clkp
= dvo
->clk_aux_parent
;
251 clk_set_parent(dvo
->clk_pix
, clkp
);
252 clk_set_parent(dvo
->clk
, clkp
);
255 /* DVO clocks = compositor clock */
256 ret
= clk_set_rate(dvo
->clk_pix
, rate
);
258 DRM_ERROR("Cannot set rate (%dHz) for dvo_pix clk\n", rate
);
262 ret
= clk_set_rate(dvo
->clk
, rate
);
264 DRM_ERROR("Cannot set rate (%dHz) for dvo clk\n", rate
);
268 /* For now, we only support 24bit data enable (DE) synchro format */
269 dvo
->config
= &rgb_24bit_de_cfg
;
272 static void sti_dvo_bridge_nope(struct drm_bridge
*bridge
)
277 static const struct drm_bridge_funcs sti_dvo_bridge_funcs
= {
278 .pre_enable
= sti_dvo_pre_enable
,
279 .enable
= sti_dvo_bridge_nope
,
280 .disable
= sti_dvo_disable
,
281 .post_disable
= sti_dvo_bridge_nope
,
282 .mode_set
= sti_dvo_set_mode
,
285 static int sti_dvo_connector_get_modes(struct drm_connector
*connector
)
287 struct sti_dvo_connector
*dvo_connector
288 = to_sti_dvo_connector(connector
);
289 struct sti_dvo
*dvo
= dvo_connector
->dvo
;
292 return dvo
->panel
->funcs
->get_modes(dvo
->panel
);
297 #define CLK_TOLERANCE_HZ 50
299 static int sti_dvo_connector_mode_valid(struct drm_connector
*connector
,
300 struct drm_display_mode
*mode
)
302 int target
= mode
->clock
* 1000;
303 int target_min
= target
- CLK_TOLERANCE_HZ
;
304 int target_max
= target
+ CLK_TOLERANCE_HZ
;
306 struct sti_dvo_connector
*dvo_connector
307 = to_sti_dvo_connector(connector
);
308 struct sti_dvo
*dvo
= dvo_connector
->dvo
;
310 result
= clk_round_rate(dvo
->clk_pix
, target
);
312 DRM_DEBUG_DRIVER("target rate = %d => available rate = %d\n",
315 if ((result
< target_min
) || (result
> target_max
)) {
316 DRM_DEBUG_DRIVER("dvo pixclk=%d not supported\n", target
);
323 struct drm_encoder
*sti_dvo_best_encoder(struct drm_connector
*connector
)
325 struct sti_dvo_connector
*dvo_connector
326 = to_sti_dvo_connector(connector
);
328 /* Best encoder is the one associated during connector creation */
329 return dvo_connector
->encoder
;
332 static struct drm_connector_helper_funcs sti_dvo_connector_helper_funcs
= {
333 .get_modes
= sti_dvo_connector_get_modes
,
334 .mode_valid
= sti_dvo_connector_mode_valid
,
335 .best_encoder
= sti_dvo_best_encoder
,
338 static enum drm_connector_status
339 sti_dvo_connector_detect(struct drm_connector
*connector
, bool force
)
341 struct sti_dvo_connector
*dvo_connector
342 = to_sti_dvo_connector(connector
);
343 struct sti_dvo
*dvo
= dvo_connector
->dvo
;
345 DRM_DEBUG_DRIVER("\n");
348 dvo
->panel
= of_drm_find_panel(dvo
->panel_node
);
351 if (!drm_panel_attach(dvo
->panel
, connector
))
352 return connector_status_connected
;
354 return connector_status_disconnected
;
357 static void sti_dvo_connector_destroy(struct drm_connector
*connector
)
359 struct sti_dvo_connector
*dvo_connector
360 = to_sti_dvo_connector(connector
);
362 drm_connector_unregister(connector
);
363 drm_connector_cleanup(connector
);
364 kfree(dvo_connector
);
367 static struct drm_connector_funcs sti_dvo_connector_funcs
= {
368 .dpms
= drm_atomic_helper_connector_dpms
,
369 .fill_modes
= drm_helper_probe_single_connector_modes
,
370 .detect
= sti_dvo_connector_detect
,
371 .destroy
= sti_dvo_connector_destroy
,
372 .reset
= drm_atomic_helper_connector_reset
,
373 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
374 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
377 static struct drm_encoder
*sti_dvo_find_encoder(struct drm_device
*dev
)
379 struct drm_encoder
*encoder
;
381 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
382 if (encoder
->encoder_type
== DRM_MODE_ENCODER_LVDS
)
389 static int sti_dvo_bind(struct device
*dev
, struct device
*master
, void *data
)
391 struct sti_dvo
*dvo
= dev_get_drvdata(dev
);
392 struct drm_device
*drm_dev
= data
;
393 struct drm_encoder
*encoder
;
394 struct sti_dvo_connector
*connector
;
395 struct drm_connector
*drm_connector
;
396 struct drm_bridge
*bridge
;
399 /* Set the drm device handle */
400 dvo
->drm_dev
= drm_dev
;
402 encoder
= sti_dvo_find_encoder(drm_dev
);
406 connector
= devm_kzalloc(dev
, sizeof(*connector
), GFP_KERNEL
);
410 connector
->dvo
= dvo
;
412 bridge
= devm_kzalloc(dev
, sizeof(*bridge
), GFP_KERNEL
);
416 bridge
->driver_private
= dvo
;
417 bridge
->funcs
= &sti_dvo_bridge_funcs
;
418 bridge
->of_node
= dvo
->dev
.of_node
;
419 err
= drm_bridge_add(bridge
);
421 DRM_ERROR("Failed to add bridge\n");
425 err
= drm_bridge_attach(drm_dev
, bridge
);
427 DRM_ERROR("Failed to attach bridge\n");
431 dvo
->bridge
= bridge
;
432 encoder
->bridge
= bridge
;
433 connector
->encoder
= encoder
;
434 dvo
->encoder
= encoder
;
436 drm_connector
= (struct drm_connector
*)connector
;
438 drm_connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
440 drm_connector_init(drm_dev
, drm_connector
,
441 &sti_dvo_connector_funcs
, DRM_MODE_CONNECTOR_LVDS
);
442 drm_connector_helper_add(drm_connector
,
443 &sti_dvo_connector_helper_funcs
);
445 err
= drm_connector_register(drm_connector
);
449 err
= drm_mode_connector_attach_encoder(drm_connector
, encoder
);
451 DRM_ERROR("Failed to attach a connector to a encoder\n");
458 drm_connector_unregister(drm_connector
);
460 drm_bridge_remove(bridge
);
461 drm_connector_cleanup(drm_connector
);
465 static void sti_dvo_unbind(struct device
*dev
,
466 struct device
*master
, void *data
)
468 struct sti_dvo
*dvo
= dev_get_drvdata(dev
);
470 drm_bridge_remove(dvo
->bridge
);
473 static const struct component_ops sti_dvo_ops
= {
474 .bind
= sti_dvo_bind
,
475 .unbind
= sti_dvo_unbind
,
478 static int sti_dvo_probe(struct platform_device
*pdev
)
480 struct device
*dev
= &pdev
->dev
;
482 struct resource
*res
;
483 struct device_node
*np
= dev
->of_node
;
485 DRM_INFO("%s\n", __func__
);
487 dvo
= devm_kzalloc(dev
, sizeof(*dvo
), GFP_KERNEL
);
489 DRM_ERROR("Failed to allocate memory for DVO\n");
493 dvo
->dev
= pdev
->dev
;
495 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dvo-reg");
497 DRM_ERROR("Invalid dvo resource\n");
500 dvo
->regs
= devm_ioremap_nocache(dev
, res
->start
,
505 dvo
->clk_pix
= devm_clk_get(dev
, "dvo_pix");
506 if (IS_ERR(dvo
->clk_pix
)) {
507 DRM_ERROR("Cannot get dvo_pix clock\n");
508 return PTR_ERR(dvo
->clk_pix
);
511 dvo
->clk
= devm_clk_get(dev
, "dvo");
512 if (IS_ERR(dvo
->clk
)) {
513 DRM_ERROR("Cannot get dvo clock\n");
514 return PTR_ERR(dvo
->clk
);
517 dvo
->clk_main_parent
= devm_clk_get(dev
, "main_parent");
518 if (IS_ERR(dvo
->clk_main_parent
)) {
519 DRM_DEBUG_DRIVER("Cannot get main_parent clock\n");
520 dvo
->clk_main_parent
= NULL
;
523 dvo
->clk_aux_parent
= devm_clk_get(dev
, "aux_parent");
524 if (IS_ERR(dvo
->clk_aux_parent
)) {
525 DRM_DEBUG_DRIVER("Cannot get aux_parent clock\n");
526 dvo
->clk_aux_parent
= NULL
;
529 dvo
->panel_node
= of_parse_phandle(np
, "sti,panel", 0);
530 if (!dvo
->panel_node
)
531 DRM_ERROR("No panel associated to the dvo output\n");
533 platform_set_drvdata(pdev
, dvo
);
535 return component_add(&pdev
->dev
, &sti_dvo_ops
);
538 static int sti_dvo_remove(struct platform_device
*pdev
)
540 component_del(&pdev
->dev
, &sti_dvo_ops
);
544 static struct of_device_id dvo_of_match
[] = {
545 { .compatible
= "st,stih407-dvo", },
548 MODULE_DEVICE_TABLE(of
, dvo_of_match
);
550 struct platform_driver sti_dvo_driver
= {
553 .owner
= THIS_MODULE
,
554 .of_match_table
= dvo_of_match
,
556 .probe
= sti_dvo_probe
,
557 .remove
= sti_dvo_remove
,
560 module_platform_driver(sti_dvo_driver
);
562 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
563 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
564 MODULE_LICENSE("GPL");