1 // SPDX-License-Identifier: GPL-2.0+
3 * i.MX drm driver - Television Encoder (TVEv2)
5 * Copyright (C) 2013 Philipp Zabel, Pengutronix
8 #include <linux/clk-provider.h>
10 #include <linux/component.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/videodev2.h>
18 #include <video/imx-ipu-v3.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_fb_helper.h>
22 #include <drm/drm_managed.h>
23 #include <drm/drm_probe_helper.h>
24 #include <drm/drm_simple_kms_helper.h>
28 #define TVE_COM_CONF_REG 0x00
29 #define TVE_TVDAC0_CONT_REG 0x28
30 #define TVE_TVDAC1_CONT_REG 0x2c
31 #define TVE_TVDAC2_CONT_REG 0x30
32 #define TVE_CD_CONT_REG 0x34
33 #define TVE_INT_CONT_REG 0x64
34 #define TVE_STAT_REG 0x68
35 #define TVE_TST_MODE_REG 0x6c
36 #define TVE_MV_CONT_REG 0xdc
38 /* TVE_COM_CONF_REG */
39 #define TVE_SYNC_CH_2_EN BIT(22)
40 #define TVE_SYNC_CH_1_EN BIT(21)
41 #define TVE_SYNC_CH_0_EN BIT(20)
42 #define TVE_TV_OUT_MODE_MASK (0x7 << 12)
43 #define TVE_TV_OUT_DISABLE (0x0 << 12)
44 #define TVE_TV_OUT_CVBS_0 (0x1 << 12)
45 #define TVE_TV_OUT_CVBS_2 (0x2 << 12)
46 #define TVE_TV_OUT_CVBS_0_2 (0x3 << 12)
47 #define TVE_TV_OUT_SVIDEO_0_1 (0x4 << 12)
48 #define TVE_TV_OUT_SVIDEO_0_1_CVBS2_2 (0x5 << 12)
49 #define TVE_TV_OUT_YPBPR (0x6 << 12)
50 #define TVE_TV_OUT_RGB (0x7 << 12)
51 #define TVE_TV_STAND_MASK (0xf << 8)
52 #define TVE_TV_STAND_HD_1080P30 (0xc << 8)
53 #define TVE_P2I_CONV_EN BIT(7)
54 #define TVE_INP_VIDEO_FORM BIT(6)
55 #define TVE_INP_YCBCR_422 (0x0 << 6)
56 #define TVE_INP_YCBCR_444 (0x1 << 6)
57 #define TVE_DATA_SOURCE_MASK (0x3 << 4)
58 #define TVE_DATA_SOURCE_BUS1 (0x0 << 4)
59 #define TVE_DATA_SOURCE_BUS2 (0x1 << 4)
60 #define TVE_DATA_SOURCE_EXT (0x2 << 4)
61 #define TVE_DATA_SOURCE_TESTGEN (0x3 << 4)
62 #define TVE_IPU_CLK_EN_OFS 3
63 #define TVE_IPU_CLK_EN BIT(3)
64 #define TVE_DAC_SAMP_RATE_OFS 1
65 #define TVE_DAC_SAMP_RATE_WIDTH 2
66 #define TVE_DAC_SAMP_RATE_MASK (0x3 << 1)
67 #define TVE_DAC_FULL_RATE (0x0 << 1)
68 #define TVE_DAC_DIV2_RATE (0x1 << 1)
69 #define TVE_DAC_DIV4_RATE (0x2 << 1)
72 /* TVE_TVDACx_CONT_REG */
73 #define TVE_TVDAC_GAIN_MASK (0x3f << 0)
76 #define TVE_CD_CH_2_SM_EN BIT(22)
77 #define TVE_CD_CH_1_SM_EN BIT(21)
78 #define TVE_CD_CH_0_SM_EN BIT(20)
79 #define TVE_CD_CH_2_LM_EN BIT(18)
80 #define TVE_CD_CH_1_LM_EN BIT(17)
81 #define TVE_CD_CH_0_LM_EN BIT(16)
82 #define TVE_CD_CH_2_REF_LVL BIT(10)
83 #define TVE_CD_CH_1_REF_LVL BIT(9)
84 #define TVE_CD_CH_0_REF_LVL BIT(8)
85 #define TVE_CD_EN BIT(0)
87 /* TVE_INT_CONT_REG */
88 #define TVE_FRAME_END_IEN BIT(13)
89 #define TVE_CD_MON_END_IEN BIT(2)
90 #define TVE_CD_SM_IEN BIT(1)
91 #define TVE_CD_LM_IEN BIT(0)
93 /* TVE_TST_MODE_REG */
94 #define TVE_TVDAC_TEST_MODE_MASK (0x7 << 0)
96 #define IMX_TVE_DAC_VOLTAGE 2750000
103 struct imx_tve_encoder {
104 struct drm_connector connector;
105 struct drm_encoder encoder;
115 struct regmap *regmap;
116 struct regulator *dac_reg;
117 struct i2c_adapter *ddc;
119 struct clk *di_sel_clk;
120 struct clk_hw clk_hw_di;
124 static inline struct imx_tve *con_to_tve(struct drm_connector *c)
126 return container_of(c, struct imx_tve_encoder, connector)->tve;
129 static inline struct imx_tve *enc_to_tve(struct drm_encoder *e)
131 return container_of(e, struct imx_tve_encoder, encoder)->tve;
134 static void tve_enable(struct imx_tve *tve)
136 clk_prepare_enable(tve->clk);
137 regmap_update_bits(tve->regmap, TVE_COM_CONF_REG, TVE_EN, TVE_EN);
139 /* clear interrupt status register */
140 regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
142 /* cable detection irq disabled in VGA mode, enabled in TVOUT mode */
143 if (tve->mode == TVE_MODE_VGA)
144 regmap_write(tve->regmap, TVE_INT_CONT_REG, 0);
146 regmap_write(tve->regmap, TVE_INT_CONT_REG,
152 static void tve_disable(struct imx_tve *tve)
154 regmap_update_bits(tve->regmap, TVE_COM_CONF_REG, TVE_EN, 0);
155 clk_disable_unprepare(tve->clk);
158 static int tve_setup_tvout(struct imx_tve *tve)
163 static int tve_setup_vga(struct imx_tve *tve)
169 /* set gain to (1 + 10/128) to provide 0.7V peak-to-peak amplitude */
170 ret = regmap_update_bits(tve->regmap, TVE_TVDAC0_CONT_REG,
171 TVE_TVDAC_GAIN_MASK, 0x0a);
175 ret = regmap_update_bits(tve->regmap, TVE_TVDAC1_CONT_REG,
176 TVE_TVDAC_GAIN_MASK, 0x0a);
180 ret = regmap_update_bits(tve->regmap, TVE_TVDAC2_CONT_REG,
181 TVE_TVDAC_GAIN_MASK, 0x0a);
185 /* set configuration register */
186 mask = TVE_DATA_SOURCE_MASK | TVE_INP_VIDEO_FORM;
187 val = TVE_DATA_SOURCE_BUS2 | TVE_INP_YCBCR_444;
188 mask |= TVE_TV_STAND_MASK | TVE_P2I_CONV_EN;
189 val |= TVE_TV_STAND_HD_1080P30 | 0;
190 mask |= TVE_TV_OUT_MODE_MASK | TVE_SYNC_CH_0_EN;
191 val |= TVE_TV_OUT_RGB | TVE_SYNC_CH_0_EN;
192 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG, mask, val);
196 /* set test mode (as documented) */
197 return regmap_update_bits(tve->regmap, TVE_TST_MODE_REG,
198 TVE_TVDAC_TEST_MODE_MASK, 1);
201 static int imx_tve_connector_get_modes(struct drm_connector *connector)
203 struct imx_tve *tve = con_to_tve(connector);
210 edid = drm_get_edid(connector, tve->ddc);
212 drm_connector_update_edid_property(connector, edid);
213 ret = drm_add_edid_modes(connector, edid);
220 static int imx_tve_connector_mode_valid(struct drm_connector *connector,
221 struct drm_display_mode *mode)
223 struct imx_tve *tve = con_to_tve(connector);
226 /* pixel clock with 2x oversampling */
227 rate = clk_round_rate(tve->clk, 2000UL * mode->clock) / 2000;
228 if (rate == mode->clock)
231 /* pixel clock without oversampling */
232 rate = clk_round_rate(tve->clk, 1000UL * mode->clock) / 1000;
233 if (rate == mode->clock)
236 dev_warn(tve->dev, "ignoring mode %dx%d\n",
237 mode->hdisplay, mode->vdisplay);
242 static void imx_tve_encoder_mode_set(struct drm_encoder *encoder,
243 struct drm_display_mode *orig_mode,
244 struct drm_display_mode *mode)
246 struct imx_tve *tve = enc_to_tve(encoder);
247 unsigned long rounded_rate;
254 * we should try 4k * mode->clock first,
255 * and enable 4x oversampling for lower resolutions
257 rate = 2000UL * mode->clock;
258 clk_set_rate(tve->clk, rate);
259 rounded_rate = clk_get_rate(tve->clk);
260 if (rounded_rate >= rate)
262 clk_set_rate(tve->di_clk, rounded_rate / div);
264 ret = clk_set_parent(tve->di_sel_clk, tve->di_clk);
266 dev_err(tve->dev, "failed to set di_sel parent to tve_di: %d\n",
270 regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
271 TVE_IPU_CLK_EN, TVE_IPU_CLK_EN);
273 if (tve->mode == TVE_MODE_VGA)
274 ret = tve_setup_vga(tve);
276 ret = tve_setup_tvout(tve);
278 dev_err(tve->dev, "failed to set configuration: %d\n", ret);
281 static void imx_tve_encoder_enable(struct drm_encoder *encoder)
283 struct imx_tve *tve = enc_to_tve(encoder);
288 static void imx_tve_encoder_disable(struct drm_encoder *encoder)
290 struct imx_tve *tve = enc_to_tve(encoder);
295 static int imx_tve_atomic_check(struct drm_encoder *encoder,
296 struct drm_crtc_state *crtc_state,
297 struct drm_connector_state *conn_state)
299 struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
300 struct imx_tve *tve = enc_to_tve(encoder);
302 imx_crtc_state->bus_format = MEDIA_BUS_FMT_GBR888_1X24;
303 imx_crtc_state->di_hsync_pin = tve->di_hsync_pin;
304 imx_crtc_state->di_vsync_pin = tve->di_vsync_pin;
309 static const struct drm_connector_funcs imx_tve_connector_funcs = {
310 .fill_modes = drm_helper_probe_single_connector_modes,
311 .destroy = imx_drm_connector_destroy,
312 .reset = drm_atomic_helper_connector_reset,
313 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
314 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
317 static const struct drm_connector_helper_funcs imx_tve_connector_helper_funcs = {
318 .get_modes = imx_tve_connector_get_modes,
319 .mode_valid = imx_tve_connector_mode_valid,
322 static const struct drm_encoder_helper_funcs imx_tve_encoder_helper_funcs = {
323 .mode_set = imx_tve_encoder_mode_set,
324 .enable = imx_tve_encoder_enable,
325 .disable = imx_tve_encoder_disable,
326 .atomic_check = imx_tve_atomic_check,
329 static irqreturn_t imx_tve_irq_handler(int irq, void *data)
331 struct imx_tve *tve = data;
334 regmap_read(tve->regmap, TVE_STAT_REG, &val);
336 /* clear interrupt status register */
337 regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
342 static unsigned long clk_tve_di_recalc_rate(struct clk_hw *hw,
343 unsigned long parent_rate)
345 struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
349 ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
353 switch (val & TVE_DAC_SAMP_RATE_MASK) {
354 case TVE_DAC_DIV4_RATE:
355 return parent_rate / 4;
356 case TVE_DAC_DIV2_RATE:
357 return parent_rate / 2;
358 case TVE_DAC_FULL_RATE:
366 static long clk_tve_di_round_rate(struct clk_hw *hw, unsigned long rate,
367 unsigned long *prate)
379 static int clk_tve_di_set_rate(struct clk_hw *hw, unsigned long rate,
380 unsigned long parent_rate)
382 struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
387 div = parent_rate / rate;
389 val = TVE_DAC_DIV4_RATE;
391 val = TVE_DAC_DIV2_RATE;
393 val = TVE_DAC_FULL_RATE;
395 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
396 TVE_DAC_SAMP_RATE_MASK, val);
399 dev_err(tve->dev, "failed to set divider: %d\n", ret);
406 static const struct clk_ops clk_tve_di_ops = {
407 .round_rate = clk_tve_di_round_rate,
408 .set_rate = clk_tve_di_set_rate,
409 .recalc_rate = clk_tve_di_recalc_rate,
412 static int tve_clk_init(struct imx_tve *tve, void __iomem *base)
414 const char *tve_di_parent[1];
415 struct clk_init_data init = {
417 .ops = &clk_tve_di_ops,
422 tve_di_parent[0] = __clk_get_name(tve->clk);
423 init.parent_names = (const char **)&tve_di_parent;
425 tve->clk_hw_di.init = &init;
426 tve->di_clk = devm_clk_register(tve->dev, &tve->clk_hw_di);
427 if (IS_ERR(tve->di_clk)) {
428 dev_err(tve->dev, "failed to register TVE output clock: %ld\n",
429 PTR_ERR(tve->di_clk));
430 return PTR_ERR(tve->di_clk);
436 static void imx_tve_disable_regulator(void *data)
438 struct imx_tve *tve = data;
440 regulator_disable(tve->dac_reg);
443 static bool imx_tve_readable_reg(struct device *dev, unsigned int reg)
445 return (reg % 4 == 0) && (reg <= 0xdc);
448 static struct regmap_config tve_regmap_config = {
453 .readable_reg = imx_tve_readable_reg,
457 .max_register = 0xdc,
460 static const char * const imx_tve_modes[] = {
461 [TVE_MODE_TVOUT] = "tvout",
462 [TVE_MODE_VGA] = "vga",
465 static int of_get_tve_mode(struct device_node *np)
470 ret = of_property_read_string(np, "fsl,tve-mode", &bm);
474 for (i = 0; i < ARRAY_SIZE(imx_tve_modes); i++)
475 if (!strcasecmp(bm, imx_tve_modes[i]))
481 static int imx_tve_bind(struct device *dev, struct device *master, void *data)
483 struct drm_device *drm = data;
484 struct imx_tve *tve = dev_get_drvdata(dev);
485 struct imx_tve_encoder *tvee;
486 struct drm_encoder *encoder;
487 struct drm_connector *connector;
491 encoder_type = tve->mode == TVE_MODE_VGA ?
492 DRM_MODE_ENCODER_DAC : DRM_MODE_ENCODER_TVDAC;
494 tvee = drmm_simple_encoder_alloc(drm, struct imx_tve_encoder, encoder,
497 return PTR_ERR(tvee);
500 encoder = &tvee->encoder;
501 connector = &tvee->connector;
503 ret = imx_drm_encoder_parse_of(drm, encoder, tve->dev->of_node);
507 drm_encoder_helper_add(encoder, &imx_tve_encoder_helper_funcs);
509 drm_connector_helper_add(connector, &imx_tve_connector_helper_funcs);
510 ret = drm_connector_init_with_ddc(drm, connector,
511 &imx_tve_connector_funcs,
512 DRM_MODE_CONNECTOR_VGA, tve->ddc);
516 return drm_connector_attach_encoder(connector, encoder);
519 static const struct component_ops imx_tve_ops = {
520 .bind = imx_tve_bind,
523 static int imx_tve_probe(struct platform_device *pdev)
525 struct device *dev = &pdev->dev;
526 struct device_node *np = dev->of_node;
527 struct device_node *ddc_node;
529 struct resource *res;
535 tve = devm_kzalloc(dev, sizeof(*tve), GFP_KERNEL);
541 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
543 tve->ddc = of_find_i2c_adapter_by_node(ddc_node);
544 of_node_put(ddc_node);
547 tve->mode = of_get_tve_mode(np);
548 if (tve->mode != TVE_MODE_VGA) {
549 dev_err(dev, "only VGA mode supported, currently\n");
553 if (tve->mode == TVE_MODE_VGA) {
554 ret = of_property_read_u32(np, "fsl,hsync-pin",
558 dev_err(dev, "failed to get hsync pin\n");
562 ret = of_property_read_u32(np, "fsl,vsync-pin",
566 dev_err(dev, "failed to get vsync pin\n");
571 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
572 base = devm_ioremap_resource(dev, res);
574 return PTR_ERR(base);
576 tve_regmap_config.lock_arg = tve;
577 tve->regmap = devm_regmap_init_mmio_clk(dev, "tve", base,
579 if (IS_ERR(tve->regmap)) {
580 dev_err(dev, "failed to init regmap: %ld\n",
581 PTR_ERR(tve->regmap));
582 return PTR_ERR(tve->regmap);
585 irq = platform_get_irq(pdev, 0);
589 ret = devm_request_threaded_irq(dev, irq, NULL,
590 imx_tve_irq_handler, IRQF_ONESHOT,
593 dev_err(dev, "failed to request irq: %d\n", ret);
597 tve->dac_reg = devm_regulator_get(dev, "dac");
598 if (!IS_ERR(tve->dac_reg)) {
599 if (regulator_get_voltage(tve->dac_reg) != IMX_TVE_DAC_VOLTAGE)
600 dev_warn(dev, "dac voltage is not %d uV\n", IMX_TVE_DAC_VOLTAGE);
601 ret = regulator_enable(tve->dac_reg);
604 ret = devm_add_action_or_reset(dev, imx_tve_disable_regulator, tve);
609 tve->clk = devm_clk_get(dev, "tve");
610 if (IS_ERR(tve->clk)) {
611 dev_err(dev, "failed to get high speed tve clock: %ld\n",
613 return PTR_ERR(tve->clk);
616 /* this is the IPU DI clock input selector, can be parented to tve_di */
617 tve->di_sel_clk = devm_clk_get(dev, "di_sel");
618 if (IS_ERR(tve->di_sel_clk)) {
619 dev_err(dev, "failed to get ipu di mux clock: %ld\n",
620 PTR_ERR(tve->di_sel_clk));
621 return PTR_ERR(tve->di_sel_clk);
624 ret = tve_clk_init(tve, base);
628 ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
630 dev_err(dev, "failed to read configuration register: %d\n",
634 if (val != 0x00100000) {
635 dev_err(dev, "configuration register default value indicates this is not a TVEv2\n");
639 /* disable cable detection for VGA mode */
640 ret = regmap_write(tve->regmap, TVE_CD_CONT_REG, 0);
644 platform_set_drvdata(pdev, tve);
646 return component_add(dev, &imx_tve_ops);
649 static int imx_tve_remove(struct platform_device *pdev)
651 component_del(&pdev->dev, &imx_tve_ops);
655 static const struct of_device_id imx_tve_dt_ids[] = {
656 { .compatible = "fsl,imx53-tve", },
659 MODULE_DEVICE_TABLE(of, imx_tve_dt_ids);
661 static struct platform_driver imx_tve_driver = {
662 .probe = imx_tve_probe,
663 .remove = imx_tve_remove,
665 .of_match_table = imx_tve_dt_ids,
670 module_platform_driver(imx_tve_driver);
672 MODULE_DESCRIPTION("i.MX Television Encoder driver");
673 MODULE_AUTHOR("Philipp Zabel, Pengutronix");
674 MODULE_LICENSE("GPL");
675 MODULE_ALIAS("platform:imx-tve");