Lines Matching +full:diff +full:- +full:channels
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>
52 #define RCAR_LVDS_QUIRK_DUAL_LINK BIT(4) /* Supports dual-link operation */
85 iowrite32(data, lvds->mmio + reg); in rcar_lvds_write()
88 /* -----------------------------------------------------------------------------
125 unsigned long diff; member
149 * The LVDS PLL is made of a pre-divider and a multiplier (strangely in rcar_lvds_d3_e3_pll_calc()
150 * enough called M and N respectively), followed by a post-divider E. in rcar_lvds_d3_e3_pll_calc()
152 * ,-----. ,-----. ,-----. ,-----. in rcar_lvds_d3_e3_pll_calc()
153 * Fin --> | 1/M | -Fpdf-> | PFD | --> | VCO | -Fvco-> | 1/E | --> Fout in rcar_lvds_d3_e3_pll_calc()
154 * `-----' ,-> | | `-----' | `-----' in rcar_lvds_d3_e3_pll_calc()
155 * | `-----' | in rcar_lvds_d3_e3_pll_calc()
156 * | ,-----. | in rcar_lvds_d3_e3_pll_calc()
157 * `-------- | 1/N | <-------' in rcar_lvds_d3_e3_pll_calc()
158 * `-----' in rcar_lvds_d3_e3_pll_calc()
165 * ,-------. ,-----. |\ in rcar_lvds_d3_e3_pll_calc()
166 * Fout --> | 1/DIV | --> | 1/7 | --> | | in rcar_lvds_d3_e3_pll_calc()
167 * `-------' | `-----' | | --> dot clock in rcar_lvds_d3_e3_pll_calc()
168 * `------------> | | in rcar_lvds_d3_e3_pll_calc()
184 * allowed values for the pre-divider M (normal range 1-8). in rcar_lvds_d3_e3_pll_calc()
200 * 60-120). in rcar_lvds_d3_e3_pll_calc()
216 * post-divider E (normal value 1, 2 or 4). in rcar_lvds_d3_e3_pll_calc()
225 unsigned long diff; in rcar_lvds_d3_e3_pll_calc() local
235 diff = abs(fout / div - target); in rcar_lvds_d3_e3_pll_calc()
237 if (diff < pll->diff) { in rcar_lvds_d3_e3_pll_calc()
238 pll->diff = diff; in rcar_lvds_d3_e3_pll_calc()
239 pll->pll_m = m; in rcar_lvds_d3_e3_pll_calc()
240 pll->pll_n = n; in rcar_lvds_d3_e3_pll_calc()
241 pll->pll_e = e; in rcar_lvds_d3_e3_pll_calc()
242 pll->div = div; in rcar_lvds_d3_e3_pll_calc()
243 pll->clksel = clksel; in rcar_lvds_d3_e3_pll_calc()
245 if (diff == 0) in rcar_lvds_d3_e3_pll_calc()
253 output = fin * pll->pll_n / pll->pll_m / (1 << pll->pll_e) in rcar_lvds_d3_e3_pll_calc()
254 / div7 / pll->div; in rcar_lvds_d3_e3_pll_calc()
255 error = (long)(output - target) * 10000 / (long)target; in rcar_lvds_d3_e3_pll_calc()
257 dev_dbg(lvds->dev, in rcar_lvds_d3_e3_pll_calc()
258 "%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()
260 error < 0 ? -error % 100 : error % 100, in rcar_lvds_d3_e3_pll_calc()
261 pll->pll_m, pll->pll_n, pll->pll_e, pll->div); in rcar_lvds_d3_e3_pll_calc()
267 struct pll_info pll = { .diff = (unsigned long)-1 }; in __rcar_lvds_pll_setup_d3_e3()
270 rcar_lvds_d3_e3_pll_calc(lvds, lvds->clocks.dotclkin[0], freq, &pll, in __rcar_lvds_pll_setup_d3_e3()
272 rcar_lvds_d3_e3_pll_calc(lvds, lvds->clocks.dotclkin[1], freq, &pll, in __rcar_lvds_pll_setup_d3_e3()
274 rcar_lvds_d3_e3_pll_calc(lvds, lvds->clocks.extal, freq, &pll, in __rcar_lvds_pll_setup_d3_e3()
278 | LVDPLLCR_PLLN(pll.pll_n - 1) | LVDPLLCR_PLLM(pll.pll_m - 1); in __rcar_lvds_pll_setup_d3_e3()
282 | LVDPLLCR_PLLE(pll.pll_e - 1); in __rcar_lvds_pll_setup_d3_e3()
295 LVDDIV_DIVRESET | LVDDIV_DIV(pll.div - 1)); in __rcar_lvds_pll_setup_d3_e3()
305 /* -----------------------------------------------------------------------------
306 * Clock - D3/E3 only
314 if (WARN_ON(!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL))) in rcar_lvds_pclk_enable()
315 return -ENODEV; in rcar_lvds_pclk_enable()
317 dev_dbg(lvds->dev, "enabling LVDS PLL, freq=%luHz\n", freq); in rcar_lvds_pclk_enable()
319 ret = clk_prepare_enable(lvds->clocks.mod); in rcar_lvds_pclk_enable()
333 if (WARN_ON(!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL))) in rcar_lvds_pclk_disable()
336 dev_dbg(lvds->dev, "disabling LVDS PLL\n"); in rcar_lvds_pclk_disable()
340 clk_disable_unprepare(lvds->clocks.mod); in rcar_lvds_pclk_disable()
344 /* -----------------------------------------------------------------------------
358 if (!lvds->panel) in rcar_lvds_get_lvds_mode()
361 info = &connector->display_info; in rcar_lvds_get_lvds_mode()
362 if (!info->num_bus_formats || !info->bus_formats) { in rcar_lvds_get_lvds_mode()
363 dev_warn(lvds->dev, in rcar_lvds_get_lvds_mode()
368 switch (info->bus_formats[0]) { in rcar_lvds_get_lvds_mode()
377 dev_warn(lvds->dev, in rcar_lvds_get_lvds_mode()
379 info->bus_formats[0]); in rcar_lvds_get_lvds_mode()
383 if (info->bus_flags & DRM_BUS_FLAG_DATA_LSB_TO_MSB) in rcar_lvds_get_lvds_mode()
399 ret = clk_prepare_enable(lvds->clocks.mod); in __rcar_lvds_atomic_enable()
403 /* Enable the companion LVDS encoder in dual-link mode. */ in __rcar_lvds_atomic_enable()
404 if (lvds->link_type != RCAR_LVDS_SINGLE_LINK && lvds->companion) in __rcar_lvds_atomic_enable()
405 __rcar_lvds_atomic_enable(lvds->companion, state, crtc, in __rcar_lvds_atomic_enable()
409 * Hardcode the channels and control signals routing for now. in __rcar_lvds_atomic_enable()
411 * HSYNC -> CTRL0 in __rcar_lvds_atomic_enable()
412 * VSYNC -> CTRL1 in __rcar_lvds_atomic_enable()
413 * DISP -> CTRL2 in __rcar_lvds_atomic_enable()
414 * 0 -> CTRL3 in __rcar_lvds_atomic_enable()
420 if (lvds->info->quirks & RCAR_LVDS_QUIRK_LANES) in __rcar_lvds_atomic_enable()
429 if (lvds->info->quirks & RCAR_LVDS_QUIRK_DUAL_LINK) { in __rcar_lvds_atomic_enable()
432 if (lvds->link_type != RCAR_LVDS_SINGLE_LINK) { in __rcar_lvds_atomic_enable()
440 bool swap_pixels = lvds->link_type == in __rcar_lvds_atomic_enable()
445 * an LVDS dual-link connection. in __rcar_lvds_atomic_enable()
451 | (lvds->companion && swap_pixels ? in __rcar_lvds_atomic_enable()
459 * dual-link mode. in __rcar_lvds_atomic_enable()
461 if (lvds->link_type == RCAR_LVDS_SINGLE_LINK || lvds->companion) { in __rcar_lvds_atomic_enable()
465 &crtc_state->adjusted_mode; in __rcar_lvds_atomic_enable()
467 lvds->info->pll_setup(lvds, mode->clock * 1000); in __rcar_lvds_atomic_enable()
473 if (lvds->bridge.encoder) { in __rcar_lvds_atomic_enable()
480 /* Turn all the channels on. */ in __rcar_lvds_atomic_enable()
485 if (lvds->info->gen < 3) { in __rcar_lvds_atomic_enable()
491 if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL)) { in __rcar_lvds_atomic_enable()
500 if (lvds->info->quirks & RCAR_LVDS_QUIRK_PWD) { in __rcar_lvds_atomic_enable()
506 if (lvds->info->quirks & RCAR_LVDS_QUIRK_GEN3_LVEN) { in __rcar_lvds_atomic_enable()
512 if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_PWD)) in __rcar_lvds_atomic_enable()
516 if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL)) { in __rcar_lvds_atomic_enable()
529 struct drm_atomic_state *state = old_bridge_state->base.state; in rcar_lvds_atomic_enable()
534 bridge->encoder); in rcar_lvds_atomic_enable()
535 crtc = drm_atomic_get_new_connector_state(state, connector)->crtc; in rcar_lvds_atomic_enable()
549 /* Disable the companion LVDS encoder in dual-link mode. */ in rcar_lvds_atomic_disable()
550 if (lvds->link_type != RCAR_LVDS_SINGLE_LINK && lvds->companion) in rcar_lvds_atomic_disable()
551 lvds->companion->funcs->atomic_disable(lvds->companion, in rcar_lvds_atomic_disable()
554 clk_disable_unprepare(lvds->clocks.mod); in rcar_lvds_atomic_disable()
569 min_freq = lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL ? 5000 : 31000; in rcar_lvds_mode_fixup()
570 adjusted_mode->clock = clamp(adjusted_mode->clock, min_freq, 148500); in rcar_lvds_mode_fixup()
580 if (!lvds->next_bridge) in rcar_lvds_attach()
583 return drm_bridge_attach(bridge->encoder, lvds->next_bridge, bridge, in rcar_lvds_attach()
601 return lvds->link_type != RCAR_LVDS_SINGLE_LINK; in rcar_lvds_dual_link()
609 return lvds->next_bridge != NULL; in rcar_lvds_is_connected()
613 /* -----------------------------------------------------------------------------
623 struct device *dev = lvds->dev; in rcar_lvds_parse_dt_companion()
627 /* Locate the companion LVDS encoder for dual-link operation, if any. */ in rcar_lvds_parse_dt_companion()
628 companion = of_parse_phandle(dev->of_node, "renesas,companion", 0); in rcar_lvds_parse_dt_companion()
636 match = of_match_device(dev->driver->of_match_table, dev); in rcar_lvds_parse_dt_companion()
637 if (!of_device_is_compatible(companion, match->compatible)) { in rcar_lvds_parse_dt_companion()
639 ret = -ENXIO; in rcar_lvds_parse_dt_companion()
645 * dual-link mode. We do this by looking at the DT port nodes we are in rcar_lvds_parse_dt_companion()
649 port0 = of_graph_get_port_by_id(dev->of_node, 1); in rcar_lvds_parse_dt_companion()
657 lvds->link_type = RCAR_LVDS_DUAL_LINK_ODD_EVEN_PIXELS; in rcar_lvds_parse_dt_companion()
660 lvds->link_type = RCAR_LVDS_DUAL_LINK_EVEN_ODD_PIXELS; in rcar_lvds_parse_dt_companion()
664 * Early dual-link bridge specific implementations populate the in rcar_lvds_parse_dt_companion()
669 if (lvds->next_bridge->timings && in rcar_lvds_parse_dt_companion()
670 lvds->next_bridge->timings->dual_link) in rcar_lvds_parse_dt_companion()
671 lvds->link_type = RCAR_LVDS_DUAL_LINK_EVEN_ODD_PIXELS; in rcar_lvds_parse_dt_companion()
673 lvds->link_type = RCAR_LVDS_SINGLE_LINK; in rcar_lvds_parse_dt_companion()
676 if (lvds->link_type == RCAR_LVDS_SINGLE_LINK) { in rcar_lvds_parse_dt_companion()
677 dev_dbg(dev, "Single-link configuration detected\n"); in rcar_lvds_parse_dt_companion()
681 lvds->companion = of_drm_find_bridge(companion); in rcar_lvds_parse_dt_companion()
682 if (!lvds->companion) { in rcar_lvds_parse_dt_companion()
683 ret = -EPROBE_DEFER; in rcar_lvds_parse_dt_companion()
688 "Dual-link configuration detected (companion encoder %pOF)\n", in rcar_lvds_parse_dt_companion()
691 if (lvds->link_type == RCAR_LVDS_DUAL_LINK_ODD_EVEN_PIXELS) in rcar_lvds_parse_dt_companion()
703 companion_lvds = bridge_to_rcar_lvds(lvds->companion); in rcar_lvds_parse_dt_companion()
704 companion_lvds->link_type = lvds->link_type; in rcar_lvds_parse_dt_companion()
716 ret = drm_of_find_panel_or_bridge(lvds->dev->of_node, 1, 0, in rcar_lvds_parse_dt()
717 &lvds->panel, &lvds->next_bridge); in rcar_lvds_parse_dt()
721 if (lvds->panel) { in rcar_lvds_parse_dt()
722 lvds->next_bridge = devm_drm_panel_bridge_add(lvds->dev, in rcar_lvds_parse_dt()
723 lvds->panel); in rcar_lvds_parse_dt()
724 if (IS_ERR_OR_NULL(lvds->next_bridge)) { in rcar_lvds_parse_dt()
725 ret = -EINVAL; in rcar_lvds_parse_dt()
730 if (lvds->info->quirks & RCAR_LVDS_QUIRK_DUAL_LINK) in rcar_lvds_parse_dt()
740 if (lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL) in rcar_lvds_parse_dt()
741 return ret == -ENODEV ? 0 : ret; in rcar_lvds_parse_dt()
751 clk = devm_clk_get(lvds->dev, name); in rcar_lvds_get_clock()
755 if (PTR_ERR(clk) == -ENOENT && optional) in rcar_lvds_get_clock()
758 dev_err_probe(lvds->dev, PTR_ERR(clk), "failed to get %s clock\n", in rcar_lvds_get_clock()
766 lvds->clocks.mod = rcar_lvds_get_clock(lvds, NULL, false); in rcar_lvds_get_clocks()
767 if (IS_ERR(lvds->clocks.mod)) in rcar_lvds_get_clocks()
768 return PTR_ERR(lvds->clocks.mod); in rcar_lvds_get_clocks()
773 if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL)) in rcar_lvds_get_clocks()
776 lvds->clocks.extal = rcar_lvds_get_clock(lvds, "extal", true); in rcar_lvds_get_clocks()
777 if (IS_ERR(lvds->clocks.extal)) in rcar_lvds_get_clocks()
778 return PTR_ERR(lvds->clocks.extal); in rcar_lvds_get_clocks()
780 lvds->clocks.dotclkin[0] = rcar_lvds_get_clock(lvds, "dclkin.0", true); in rcar_lvds_get_clocks()
781 if (IS_ERR(lvds->clocks.dotclkin[0])) in rcar_lvds_get_clocks()
782 return PTR_ERR(lvds->clocks.dotclkin[0]); in rcar_lvds_get_clocks()
784 lvds->clocks.dotclkin[1] = rcar_lvds_get_clock(lvds, "dclkin.1", true); in rcar_lvds_get_clocks()
785 if (IS_ERR(lvds->clocks.dotclkin[1])) in rcar_lvds_get_clocks()
786 return PTR_ERR(lvds->clocks.dotclkin[1]); in rcar_lvds_get_clocks()
789 if (!lvds->clocks.extal && !lvds->clocks.dotclkin[0] && in rcar_lvds_get_clocks()
790 !lvds->clocks.dotclkin[1]) { in rcar_lvds_get_clocks()
791 dev_err(lvds->dev, in rcar_lvds_get_clocks()
793 return -EINVAL; in rcar_lvds_get_clocks()
819 lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL); in rcar_lvds_probe()
821 return -ENOMEM; in rcar_lvds_probe()
825 lvds->dev = &pdev->dev; in rcar_lvds_probe()
826 lvds->info = of_device_get_match_data(&pdev->dev); in rcar_lvds_probe()
830 lvds->info = attr->data; in rcar_lvds_probe()
836 lvds->bridge.funcs = &rcar_lvds_bridge_ops; in rcar_lvds_probe()
837 lvds->bridge.of_node = pdev->dev.of_node; in rcar_lvds_probe()
839 lvds->mmio = devm_platform_ioremap_resource(pdev, 0); in rcar_lvds_probe()
840 if (IS_ERR(lvds->mmio)) in rcar_lvds_probe()
841 return PTR_ERR(lvds->mmio); in rcar_lvds_probe()
847 drm_bridge_add(&lvds->bridge); in rcar_lvds_probe()
856 drm_bridge_remove(&lvds->bridge); in rcar_lvds_remove()
893 { .compatible = "renesas,r8a7742-lvds", .data = &rcar_lvds_gen2_info },
894 { .compatible = "renesas,r8a7743-lvds", .data = &rcar_lvds_gen2_info },
895 { .compatible = "renesas,r8a7744-lvds", .data = &rcar_lvds_gen2_info },
896 { .compatible = "renesas,r8a774a1-lvds", .data = &rcar_lvds_gen3_info },
897 { .compatible = "renesas,r8a774b1-lvds", .data = &rcar_lvds_gen3_info },
898 { .compatible = "renesas,r8a774c0-lvds", .data = &rcar_lvds_r8a77990_info },
899 { .compatible = "renesas,r8a774e1-lvds", .data = &rcar_lvds_gen3_info },
900 { .compatible = "renesas,r8a7790-lvds", .data = &rcar_lvds_gen2_info },
901 { .compatible = "renesas,r8a7791-lvds", .data = &rcar_lvds_gen2_info },
902 { .compatible = "renesas,r8a7793-lvds", .data = &rcar_lvds_gen2_info },
903 { .compatible = "renesas,r8a7795-lvds", .data = &rcar_lvds_gen3_info },
904 { .compatible = "renesas,r8a7796-lvds", .data = &rcar_lvds_gen3_info },
905 { .compatible = "renesas,r8a77961-lvds", .data = &rcar_lvds_gen3_info },
906 { .compatible = "renesas,r8a77965-lvds", .data = &rcar_lvds_gen3_info },
907 { .compatible = "renesas,r8a77970-lvds", .data = &rcar_lvds_r8a77970_info },
908 { .compatible = "renesas,r8a77980-lvds", .data = &rcar_lvds_gen3_info },
909 { .compatible = "renesas,r8a77990-lvds", .data = &rcar_lvds_r8a77990_info },
910 { .compatible = "renesas,r8a77995-lvds", .data = &rcar_lvds_r8a77995_info },
920 .name = "rcar-lvds",
928 MODULE_DESCRIPTION("Renesas R-Car LVDS Encoder Driver");