Lines Matching +full:keep +full:- +full:pll +full:- +full:enabled

1 // SPDX-License-Identifier: GPL-2.0
3 * R-Car LVDS Encoder
5 * Copyright (C) 2013-2018 Renesas Electronics Corporation
13 #include <linux/media-bus-format.h>
37 /* Keep in sync with the LVDCR0.LVMD hardware register values. */
53 #define RCAR_LVDS_QUIRK_EXT_PLL BIT(3) /* Has extended PLL */
54 #define RCAR_LVDS_QUIRK_DUAL_LINK BIT(4) /* Supports dual-link operation */
88 return ioread32(lvds->mmio + reg); in rcar_lvds_read()
93 iowrite32(data, lvds->mmio + reg); in rcar_lvds_write()
96 /* -----------------------------------------------------------------------------
97 * PLL Setup
142 unsigned long target, struct pll_info *pll, in rcar_lvds_d3_e3_pll_calc() argument
157 * The LVDS PLL is made of a pre-divider and a multiplier (strangely in rcar_lvds_d3_e3_pll_calc()
158 * enough called M and N respectively), followed by a post-divider E. in rcar_lvds_d3_e3_pll_calc()
160 * ,-----. ,-----. ,-----. ,-----. in rcar_lvds_d3_e3_pll_calc()
161 * Fin --> | 1/M | -Fpdf-> | PFD | --> | VCO | -Fvco-> | 1/E | --> Fout in rcar_lvds_d3_e3_pll_calc()
162 * `-----' ,-> | | `-----' | `-----' in rcar_lvds_d3_e3_pll_calc()
163 * | `-----' | in rcar_lvds_d3_e3_pll_calc()
164 * | ,-----. | in rcar_lvds_d3_e3_pll_calc()
165 * `-------- | 1/N | <-------' in rcar_lvds_d3_e3_pll_calc()
166 * `-----' in rcar_lvds_d3_e3_pll_calc()
168 * The clock output by the PLL is then further divided by a programmable in rcar_lvds_d3_e3_pll_calc()
173 * ,-------. ,-----. |\ in rcar_lvds_d3_e3_pll_calc()
174 * Fout --> | 1/DIV | --> | 1/7 | --> | | in rcar_lvds_d3_e3_pll_calc()
175 * `-------' | `-----' | | --> dot clock in rcar_lvds_d3_e3_pll_calc()
176 * `------------> | | in rcar_lvds_d3_e3_pll_calc()
179 * The /7 divider is optional, it is enabled when the LVDS PLL is used in rcar_lvds_d3_e3_pll_calc()
183 * The PLL allowed input frequency range is 12 MHz to 192 MHz. in rcar_lvds_d3_e3_pll_calc()
192 * allowed values for the pre-divider M (normal range 1-8). in rcar_lvds_d3_e3_pll_calc()
208 * 60-120). in rcar_lvds_d3_e3_pll_calc()
224 * post-divider E (normal value 1, 2 or 4). in rcar_lvds_d3_e3_pll_calc()
238 * the PLL, followed by a an optional fixed /7 in rcar_lvds_d3_e3_pll_calc()
243 diff = abs(fout / div - target); in rcar_lvds_d3_e3_pll_calc()
245 if (diff < pll->diff) { in rcar_lvds_d3_e3_pll_calc()
246 pll->diff = diff; in rcar_lvds_d3_e3_pll_calc()
247 pll->pll_m = m; in rcar_lvds_d3_e3_pll_calc()
248 pll->pll_n = n; in rcar_lvds_d3_e3_pll_calc()
249 pll->pll_e = e; in rcar_lvds_d3_e3_pll_calc()
250 pll->div = div; in rcar_lvds_d3_e3_pll_calc()
251 pll->clksel = clksel; in rcar_lvds_d3_e3_pll_calc()
261 output = fin * pll->pll_n / pll->pll_m / (1 << pll->pll_e) in rcar_lvds_d3_e3_pll_calc()
262 / div7 / pll->div; in rcar_lvds_d3_e3_pll_calc()
263 error = (long)(output - target) * 10000 / (long)target; in rcar_lvds_d3_e3_pll_calc()
265 dev_dbg(lvds->dev, in rcar_lvds_d3_e3_pll_calc()
266 "%pC %lu Hz -> Fout %lu Hz (target %lu Hz, error %d.%02u%%), PLL M/N/E/DIV %u/%u/%u/%u\n", in rcar_lvds_d3_e3_pll_calc()
268 error < 0 ? -error % 100 : error % 100, in rcar_lvds_d3_e3_pll_calc()
269 pll->pll_m, pll->pll_n, pll->pll_e, pll->div); in rcar_lvds_d3_e3_pll_calc()
275 struct pll_info pll = { .diff = (unsigned long)-1 }; in rcar_lvds_pll_setup_d3_e3() local
278 rcar_lvds_d3_e3_pll_calc(lvds, lvds->clocks.dotclkin[0], freq, &pll, in rcar_lvds_pll_setup_d3_e3()
280 rcar_lvds_d3_e3_pll_calc(lvds, lvds->clocks.dotclkin[1], freq, &pll, in rcar_lvds_pll_setup_d3_e3()
282 rcar_lvds_d3_e3_pll_calc(lvds, lvds->clocks.extal, freq, &pll, in rcar_lvds_pll_setup_d3_e3()
285 lvdpllcr = LVDPLLCR_PLLON | pll.clksel | LVDPLLCR_CLKOUT in rcar_lvds_pll_setup_d3_e3()
286 | LVDPLLCR_PLLN(pll.pll_n - 1) | LVDPLLCR_PLLM(pll.pll_m - 1); in rcar_lvds_pll_setup_d3_e3()
288 if (pll.pll_e > 0) in rcar_lvds_pll_setup_d3_e3()
290 | LVDPLLCR_PLLE(pll.pll_e - 1); in rcar_lvds_pll_setup_d3_e3()
297 if (pll.div > 1) in rcar_lvds_pll_setup_d3_e3()
303 LVDDIV_DIVRESET | LVDDIV_DIV(pll.div - 1)); in rcar_lvds_pll_setup_d3_e3()
308 /* -----------------------------------------------------------------------------
322 if (!lvds->panel) in rcar_lvds_get_lvds_mode()
325 info = &connector->display_info; in rcar_lvds_get_lvds_mode()
326 if (!info->num_bus_formats || !info->bus_formats) { in rcar_lvds_get_lvds_mode()
327 dev_warn(lvds->dev, in rcar_lvds_get_lvds_mode()
332 switch (info->bus_formats[0]) { in rcar_lvds_get_lvds_mode()
341 dev_warn(lvds->dev, in rcar_lvds_get_lvds_mode()
343 info->bus_formats[0]); in rcar_lvds_get_lvds_mode()
347 if (info->bus_flags & DRM_BUS_FLAG_DATA_LSB_TO_MSB) in rcar_lvds_get_lvds_mode()
363 ret = pm_runtime_resume_and_get(lvds->dev); in rcar_lvds_enable()
367 /* Enable the companion LVDS encoder in dual-link mode. */ in rcar_lvds_enable()
368 if (lvds->link_type != RCAR_LVDS_SINGLE_LINK && lvds->companion) in rcar_lvds_enable()
369 rcar_lvds_enable(lvds->companion, state, crtc, connector); in rcar_lvds_enable()
374 * HSYNC -> CTRL0 in rcar_lvds_enable()
375 * VSYNC -> CTRL1 in rcar_lvds_enable()
376 * DISP -> CTRL2 in rcar_lvds_enable()
377 * 0 -> CTRL3 in rcar_lvds_enable()
383 if (lvds->info->quirks & RCAR_LVDS_QUIRK_LANES) in rcar_lvds_enable()
392 if (lvds->info->quirks & RCAR_LVDS_QUIRK_DUAL_LINK) { in rcar_lvds_enable()
395 if (lvds->link_type != RCAR_LVDS_SINGLE_LINK) { in rcar_lvds_enable()
403 bool swap_pixels = lvds->link_type == in rcar_lvds_enable()
408 * an LVDS dual-link connection. in rcar_lvds_enable()
414 | (lvds->companion && swap_pixels ? in rcar_lvds_enable()
421 * PLL clock configuration on all instances but the companion in in rcar_lvds_enable()
422 * dual-link mode. in rcar_lvds_enable()
424 * The extended PLL has been turned on by an explicit call to in rcar_lvds_enable()
427 if ((lvds->link_type == RCAR_LVDS_SINGLE_LINK || lvds->companion) && in rcar_lvds_enable()
428 !(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL)) { in rcar_lvds_enable()
432 &crtc_state->adjusted_mode; in rcar_lvds_enable()
434 lvds->info->pll_setup(lvds, mode->clock * 1000); in rcar_lvds_enable()
440 if (lvds->bridge.encoder) { in rcar_lvds_enable()
452 if (lvds->info->gen < 3) { in rcar_lvds_enable()
458 if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL)) { in rcar_lvds_enable()
460 * Turn the PLL on (simple PLL only, extended PLL is fully in rcar_lvds_enable()
467 if (lvds->info->quirks & RCAR_LVDS_QUIRK_PWD) { in rcar_lvds_enable()
473 if (lvds->info->quirks & RCAR_LVDS_QUIRK_GEN3_LVEN) { in rcar_lvds_enable()
479 if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_PWD)) in rcar_lvds_enable()
483 if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL)) { in rcar_lvds_enable()
484 /* Wait for the PLL startup delay (simple PLL only). */ in rcar_lvds_enable()
508 if (lvds->info->quirks & RCAR_LVDS_QUIRK_GEN3_LVEN) { in rcar_lvds_disable()
513 if (lvds->info->quirks & RCAR_LVDS_QUIRK_PWD) { in rcar_lvds_disable()
518 if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL)) { in rcar_lvds_disable()
526 /* The extended PLL is turned off in rcar_lvds_pclk_disable(). */ in rcar_lvds_disable()
527 if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL)) in rcar_lvds_disable()
530 /* Disable the companion LVDS encoder in dual-link mode. */ in rcar_lvds_disable()
531 if (lvds->link_type != RCAR_LVDS_SINGLE_LINK && lvds->companion) in rcar_lvds_disable()
532 rcar_lvds_disable(lvds->companion); in rcar_lvds_disable()
534 pm_runtime_put_sync(lvds->dev); in rcar_lvds_disable()
537 /* -----------------------------------------------------------------------------
538 * Clock - D3/E3 only
547 if (WARN_ON(!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL))) in rcar_lvds_pclk_enable()
548 return -ENODEV; in rcar_lvds_pclk_enable()
550 dev_dbg(lvds->dev, "enabling LVDS PLL, freq=%luHz\n", freq); in rcar_lvds_pclk_enable()
552 ret = pm_runtime_resume_and_get(lvds->dev); in rcar_lvds_pclk_enable()
566 if (WARN_ON(!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL))) in rcar_lvds_pclk_disable()
569 dev_dbg(lvds->dev, "disabling LVDS PLL\n"); in rcar_lvds_pclk_disable()
576 pm_runtime_put_sync(lvds->dev); in rcar_lvds_pclk_disable()
580 /* -----------------------------------------------------------------------------
587 struct drm_atomic_state *state = old_bridge_state->base.state; in rcar_lvds_atomic_enable()
592 bridge->encoder); in rcar_lvds_atomic_enable()
593 crtc = drm_atomic_get_new_connector_state(state, connector)->crtc; in rcar_lvds_atomic_enable()
607 * unknown. Keep the encoder enabled, it will be disabled by an explicit in rcar_lvds_atomic_disable()
613 if (lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL) in rcar_lvds_atomic_disable()
631 min_freq = lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL ? 5000 : 31000; in rcar_lvds_mode_fixup()
632 adjusted_mode->clock = clamp(adjusted_mode->clock, min_freq, 148500); in rcar_lvds_mode_fixup()
642 if (!lvds->next_bridge) in rcar_lvds_attach()
645 return drm_bridge_attach(bridge->encoder, lvds->next_bridge, bridge, in rcar_lvds_attach()
663 return lvds->link_type != RCAR_LVDS_SINGLE_LINK; in rcar_lvds_dual_link()
671 return lvds->next_bridge != NULL; in rcar_lvds_is_connected()
675 /* -----------------------------------------------------------------------------
685 struct device *dev = lvds->dev; in rcar_lvds_parse_dt_companion()
689 /* Locate the companion LVDS encoder for dual-link operation, if any. */ in rcar_lvds_parse_dt_companion()
690 companion = of_parse_phandle(dev->of_node, "renesas,companion", 0); in rcar_lvds_parse_dt_companion()
698 match = of_match_device(dev->driver->of_match_table, dev); in rcar_lvds_parse_dt_companion()
699 if (!of_device_is_compatible(companion, match->compatible)) { in rcar_lvds_parse_dt_companion()
701 ret = -ENXIO; in rcar_lvds_parse_dt_companion()
707 * dual-link mode. We do this by looking at the DT port nodes we are in rcar_lvds_parse_dt_companion()
711 port0 = of_graph_get_port_by_id(dev->of_node, 1); in rcar_lvds_parse_dt_companion()
719 lvds->link_type = RCAR_LVDS_DUAL_LINK_ODD_EVEN_PIXELS; in rcar_lvds_parse_dt_companion()
722 lvds->link_type = RCAR_LVDS_DUAL_LINK_EVEN_ODD_PIXELS; in rcar_lvds_parse_dt_companion()
726 * Early dual-link bridge specific implementations populate the in rcar_lvds_parse_dt_companion()
731 if (lvds->next_bridge->timings && in rcar_lvds_parse_dt_companion()
732 lvds->next_bridge->timings->dual_link) in rcar_lvds_parse_dt_companion()
733 lvds->link_type = RCAR_LVDS_DUAL_LINK_EVEN_ODD_PIXELS; in rcar_lvds_parse_dt_companion()
735 lvds->link_type = RCAR_LVDS_SINGLE_LINK; in rcar_lvds_parse_dt_companion()
738 if (lvds->link_type == RCAR_LVDS_SINGLE_LINK) { in rcar_lvds_parse_dt_companion()
739 dev_dbg(dev, "Single-link configuration detected\n"); in rcar_lvds_parse_dt_companion()
743 lvds->companion = of_drm_find_bridge(companion); in rcar_lvds_parse_dt_companion()
744 if (!lvds->companion) { in rcar_lvds_parse_dt_companion()
745 ret = -EPROBE_DEFER; in rcar_lvds_parse_dt_companion()
750 "Dual-link configuration detected (companion encoder %pOF)\n", in rcar_lvds_parse_dt_companion()
753 if (lvds->link_type == RCAR_LVDS_DUAL_LINK_ODD_EVEN_PIXELS) in rcar_lvds_parse_dt_companion()
765 companion_lvds = bridge_to_rcar_lvds(lvds->companion); in rcar_lvds_parse_dt_companion()
766 companion_lvds->link_type = lvds->link_type; in rcar_lvds_parse_dt_companion()
778 ret = drm_of_find_panel_or_bridge(lvds->dev->of_node, 1, 0, in rcar_lvds_parse_dt()
779 &lvds->panel, &lvds->next_bridge); in rcar_lvds_parse_dt()
783 if (lvds->panel) { in rcar_lvds_parse_dt()
784 lvds->next_bridge = devm_drm_panel_bridge_add(lvds->dev, in rcar_lvds_parse_dt()
785 lvds->panel); in rcar_lvds_parse_dt()
786 if (IS_ERR_OR_NULL(lvds->next_bridge)) { in rcar_lvds_parse_dt()
787 ret = -EINVAL; in rcar_lvds_parse_dt()
792 if (lvds->info->quirks & RCAR_LVDS_QUIRK_DUAL_LINK) in rcar_lvds_parse_dt()
802 if (lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL) in rcar_lvds_parse_dt()
803 return ret == -ENODEV ? 0 : ret; in rcar_lvds_parse_dt()
813 clk = devm_clk_get(lvds->dev, name); in rcar_lvds_get_clock()
817 if (PTR_ERR(clk) == -ENOENT && optional) in rcar_lvds_get_clock()
820 dev_err_probe(lvds->dev, PTR_ERR(clk), "failed to get %s clock\n", in rcar_lvds_get_clock()
828 lvds->clocks.mod = rcar_lvds_get_clock(lvds, NULL, false); in rcar_lvds_get_clocks()
829 if (IS_ERR(lvds->clocks.mod)) in rcar_lvds_get_clocks()
830 return PTR_ERR(lvds->clocks.mod); in rcar_lvds_get_clocks()
833 * LVDS encoders without an extended PLL have no external clock inputs. in rcar_lvds_get_clocks()
835 if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL)) in rcar_lvds_get_clocks()
838 lvds->clocks.extal = rcar_lvds_get_clock(lvds, "extal", true); in rcar_lvds_get_clocks()
839 if (IS_ERR(lvds->clocks.extal)) in rcar_lvds_get_clocks()
840 return PTR_ERR(lvds->clocks.extal); in rcar_lvds_get_clocks()
842 lvds->clocks.dotclkin[0] = rcar_lvds_get_clock(lvds, "dclkin.0", true); in rcar_lvds_get_clocks()
843 if (IS_ERR(lvds->clocks.dotclkin[0])) in rcar_lvds_get_clocks()
844 return PTR_ERR(lvds->clocks.dotclkin[0]); in rcar_lvds_get_clocks()
846 lvds->clocks.dotclkin[1] = rcar_lvds_get_clock(lvds, "dclkin.1", true); in rcar_lvds_get_clocks()
847 if (IS_ERR(lvds->clocks.dotclkin[1])) in rcar_lvds_get_clocks()
848 return PTR_ERR(lvds->clocks.dotclkin[1]); in rcar_lvds_get_clocks()
850 /* At least one input to the PLL must be available. */ in rcar_lvds_get_clocks()
851 if (!lvds->clocks.extal && !lvds->clocks.dotclkin[0] && in rcar_lvds_get_clocks()
852 !lvds->clocks.dotclkin[1]) { in rcar_lvds_get_clocks()
853 dev_err(lvds->dev, in rcar_lvds_get_clocks()
855 return -EINVAL; in rcar_lvds_get_clocks()
881 lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL); in rcar_lvds_probe()
883 return -ENOMEM; in rcar_lvds_probe()
887 lvds->dev = &pdev->dev; in rcar_lvds_probe()
888 lvds->info = of_device_get_match_data(&pdev->dev); in rcar_lvds_probe()
892 lvds->info = attr->data; in rcar_lvds_probe()
898 lvds->bridge.funcs = &rcar_lvds_bridge_ops; in rcar_lvds_probe()
899 lvds->bridge.of_node = pdev->dev.of_node; in rcar_lvds_probe()
901 lvds->mmio = devm_platform_ioremap_resource(pdev, 0); in rcar_lvds_probe()
902 if (IS_ERR(lvds->mmio)) in rcar_lvds_probe()
903 return PTR_ERR(lvds->mmio); in rcar_lvds_probe()
909 lvds->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); in rcar_lvds_probe()
910 if (IS_ERR(lvds->rstc)) in rcar_lvds_probe()
911 return dev_err_probe(&pdev->dev, PTR_ERR(lvds->rstc), in rcar_lvds_probe()
914 pm_runtime_enable(&pdev->dev); in rcar_lvds_probe()
916 drm_bridge_add(&lvds->bridge); in rcar_lvds_probe()
925 drm_bridge_remove(&lvds->bridge); in rcar_lvds_remove()
927 pm_runtime_disable(&pdev->dev); in rcar_lvds_remove()
960 { .compatible = "renesas,r8a7742-lvds", .data = &rcar_lvds_gen2_info },
961 { .compatible = "renesas,r8a7743-lvds", .data = &rcar_lvds_gen2_info },
962 { .compatible = "renesas,r8a7744-lvds", .data = &rcar_lvds_gen2_info },
963 { .compatible = "renesas,r8a774a1-lvds", .data = &rcar_lvds_gen3_info },
964 { .compatible = "renesas,r8a774b1-lvds", .data = &rcar_lvds_gen3_info },
965 { .compatible = "renesas,r8a774c0-lvds", .data = &rcar_lvds_r8a77990_info },
966 { .compatible = "renesas,r8a774e1-lvds", .data = &rcar_lvds_gen3_info },
967 { .compatible = "renesas,r8a7790-lvds", .data = &rcar_lvds_gen2_info },
968 { .compatible = "renesas,r8a7791-lvds", .data = &rcar_lvds_gen2_info },
969 { .compatible = "renesas,r8a7793-lvds", .data = &rcar_lvds_gen2_info },
970 { .compatible = "renesas,r8a7795-lvds", .data = &rcar_lvds_gen3_info },
971 { .compatible = "renesas,r8a7796-lvds", .data = &rcar_lvds_gen3_info },
972 { .compatible = "renesas,r8a77961-lvds", .data = &rcar_lvds_gen3_info },
973 { .compatible = "renesas,r8a77965-lvds", .data = &rcar_lvds_gen3_info },
974 { .compatible = "renesas,r8a77970-lvds", .data = &rcar_lvds_r8a77970_info },
975 { .compatible = "renesas,r8a77980-lvds", .data = &rcar_lvds_gen3_info },
976 { .compatible = "renesas,r8a77990-lvds", .data = &rcar_lvds_r8a77990_info },
977 { .compatible = "renesas,r8a77995-lvds", .data = &rcar_lvds_r8a77995_info },
987 clk_disable_unprepare(lvds->clocks.mod); in rcar_lvds_runtime_suspend()
989 reset_control_assert(lvds->rstc); in rcar_lvds_runtime_suspend()
999 ret = reset_control_deassert(lvds->rstc); in rcar_lvds_runtime_resume()
1003 ret = clk_prepare_enable(lvds->clocks.mod); in rcar_lvds_runtime_resume()
1010 reset_control_assert(lvds->rstc); in rcar_lvds_runtime_resume()
1023 .name = "rcar-lvds",
1032 MODULE_DESCRIPTION("Renesas R-Car LVDS Encoder Driver");