Lines Matching +full:msm8998 +full:- +full:qmp +full:- +full:usb3 +full:- +full:phy
1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/clk-provider.h>
17 #include <linux/phy/phy.h>
26 #include "phy-qcom-qmp-common.h"
28 #include "phy-qcom-qmp.h"
29 #include "phy-qcom-qmp-pcs-misc-v3.h"
33 /* set of registers with offsets different per-PHY */
298 /* struct qmp_phy_cfg - per-PHY initialization config */
302 /* Init sequence for PHY blocks - serdes, tx, rx, pcs */
348 struct phy *phy;
380 /* list of clocks required by phy */
387 "phy", "common",
391 "phy_phy", "phy",
396 "vdda-phy", "vdda-pll",
457 static int qmp_usbc_init(struct phy *phy)
459 struct qmp_usbc *qmp = phy_get_drvdata(phy);
460 const struct qmp_phy_cfg *cfg = qmp->cfg;
461 void __iomem *pcs = qmp->pcs;
465 ret = regulator_bulk_enable(cfg->num_vregs, qmp->vregs);
467 dev_err(qmp->dev, "failed to enable regulators, err=%d\n", ret);
471 ret = reset_control_bulk_assert(qmp->num_resets, qmp->resets);
473 dev_err(qmp->dev, "reset assert failed\n");
477 ret = reset_control_bulk_deassert(qmp->num_resets, qmp->resets);
479 dev_err(qmp->dev, "reset deassert failed\n");
483 ret = clk_bulk_prepare_enable(qmp->num_clks, qmp->clks);
487 qphy_setbits(pcs, cfg->regs[QPHY_PCS_POWER_DOWN_CONTROL], SW_PWRDN);
493 if (qmp->orientation == TYPEC_ORIENTATION_REVERSE)
495 writel(val, qmp->pcs_misc);
500 reset_control_bulk_assert(qmp->num_resets, qmp->resets);
502 regulator_bulk_disable(cfg->num_vregs, qmp->vregs);
507 static int qmp_usbc_exit(struct phy *phy)
509 struct qmp_usbc *qmp = phy_get_drvdata(phy);
510 const struct qmp_phy_cfg *cfg = qmp->cfg;
512 reset_control_bulk_assert(qmp->num_resets, qmp->resets);
514 clk_bulk_disable_unprepare(qmp->num_clks, qmp->clks);
516 regulator_bulk_disable(cfg->num_vregs, qmp->vregs);
521 static int qmp_usbc_power_on(struct phy *phy)
523 struct qmp_usbc *qmp = phy_get_drvdata(phy);
524 const struct qmp_phy_cfg *cfg = qmp->cfg;
529 qmp_configure(qmp->dev, qmp->serdes, cfg->serdes_tbl,
530 cfg->serdes_tbl_num);
532 ret = clk_prepare_enable(qmp->pipe_clk);
534 dev_err(qmp->dev, "pipe_clk enable failed err=%d\n", ret);
539 qmp_configure_lane(qmp->dev, qmp->tx, cfg->tx_tbl, cfg->tx_tbl_num, 1);
540 qmp_configure_lane(qmp->dev, qmp->rx, cfg->rx_tbl, cfg->rx_tbl_num, 1);
542 qmp_configure_lane(qmp->dev, qmp->tx2, cfg->tx_tbl, cfg->tx_tbl_num, 2);
543 qmp_configure_lane(qmp->dev, qmp->rx2, cfg->rx_tbl, cfg->rx_tbl_num, 2);
545 qmp_configure(qmp->dev, qmp->pcs, cfg->pcs_tbl, cfg->pcs_tbl_num);
547 /* Pull PHY out of reset state */
548 qphy_clrbits(qmp->pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
550 /* start SerDes and Phy-Coding-Sublayer */
551 qphy_setbits(qmp->pcs, cfg->regs[QPHY_START_CTRL], SERDES_START | PCS_START);
553 status = qmp->pcs + cfg->regs[QPHY_PCS_STATUS];
557 dev_err(qmp->dev, "phy initialization timed-out\n");
564 clk_disable_unprepare(qmp->pipe_clk);
569 static int qmp_usbc_power_off(struct phy *phy)
571 struct qmp_usbc *qmp = phy_get_drvdata(phy);
572 const struct qmp_phy_cfg *cfg = qmp->cfg;
574 clk_disable_unprepare(qmp->pipe_clk);
576 /* PHY reset */
577 qphy_setbits(qmp->pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
579 /* stop SerDes and Phy-Coding-Sublayer */
580 qphy_clrbits(qmp->pcs, cfg->regs[QPHY_START_CTRL],
583 /* Put PHY into POWER DOWN state: active low */
584 qphy_clrbits(qmp->pcs, cfg->regs[QPHY_PCS_POWER_DOWN_CONTROL],
590 static int qmp_usbc_enable(struct phy *phy)
592 struct qmp_usbc *qmp = phy_get_drvdata(phy);
595 mutex_lock(&qmp->phy_mutex);
597 ret = qmp_usbc_init(phy);
601 ret = qmp_usbc_power_on(phy);
603 qmp_usbc_exit(phy);
607 qmp->usb_init_count++;
609 mutex_unlock(&qmp->phy_mutex);
614 static int qmp_usbc_disable(struct phy *phy)
616 struct qmp_usbc *qmp = phy_get_drvdata(phy);
619 qmp->usb_init_count--;
620 ret = qmp_usbc_power_off(phy);
623 return qmp_usbc_exit(phy);
626 static int qmp_usbc_set_mode(struct phy *phy, enum phy_mode mode, int submode)
628 struct qmp_usbc *qmp = phy_get_drvdata(phy);
630 qmp->mode = mode;
642 static void qmp_usbc_enable_autonomous_mode(struct qmp_usbc *qmp)
644 const struct qmp_phy_cfg *cfg = qmp->cfg;
645 void __iomem *pcs = qmp->pcs;
648 if (qmp->mode == PHY_MODE_USB_HOST_SS ||
649 qmp->mode == PHY_MODE_USB_DEVICE_SS)
655 qphy_setbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
657 qphy_clrbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
659 qphy_clrbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL],
662 /* Enable required PHY autonomous mode interrupts */
663 qphy_setbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL], intr_mask);
666 if (qmp->tcsr_map && qmp->vls_clamp_reg)
667 regmap_write(qmp->tcsr_map, qmp->vls_clamp_reg, 1);
670 static void qmp_usbc_disable_autonomous_mode(struct qmp_usbc *qmp)
672 const struct qmp_phy_cfg *cfg = qmp->cfg;
673 void __iomem *pcs = qmp->pcs;
676 if (qmp->tcsr_map && qmp->vls_clamp_reg)
677 regmap_write(qmp->tcsr_map, qmp->vls_clamp_reg, 0);
679 qphy_clrbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL],
682 qphy_setbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
684 qphy_clrbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
689 struct qmp_usbc *qmp = dev_get_drvdata(dev);
691 dev_vdbg(dev, "Suspending QMP phy, mode:%d\n", qmp->mode);
693 if (!qmp->phy->init_count) {
694 dev_vdbg(dev, "PHY not initialized, bailing out\n");
698 qmp_usbc_enable_autonomous_mode(qmp);
700 clk_disable_unprepare(qmp->pipe_clk);
701 clk_bulk_disable_unprepare(qmp->num_clks, qmp->clks);
708 struct qmp_usbc *qmp = dev_get_drvdata(dev);
711 dev_vdbg(dev, "Resuming QMP phy, mode:%d\n", qmp->mode);
713 if (!qmp->phy->init_count) {
714 dev_vdbg(dev, "PHY not initialized, bailing out\n");
718 ret = clk_bulk_prepare_enable(qmp->num_clks, qmp->clks);
722 ret = clk_prepare_enable(qmp->pipe_clk);
725 clk_bulk_disable_unprepare(qmp->num_clks, qmp->clks);
729 qmp_usbc_disable_autonomous_mode(qmp);
739 static int qmp_usbc_vreg_init(struct qmp_usbc *qmp)
741 const struct qmp_phy_cfg *cfg = qmp->cfg;
742 struct device *dev = qmp->dev;
743 int num = cfg->num_vregs;
746 qmp->vregs = devm_kcalloc(dev, num, sizeof(*qmp->vregs), GFP_KERNEL);
747 if (!qmp->vregs)
748 return -ENOMEM;
751 qmp->vregs[i].supply = cfg->vreg_list[i];
753 return devm_regulator_bulk_get(dev, num, qmp->vregs);
756 static int qmp_usbc_reset_init(struct qmp_usbc *qmp,
760 struct device *dev = qmp->dev;
764 qmp->resets = devm_kcalloc(dev, num_resets,
765 sizeof(*qmp->resets), GFP_KERNEL);
766 if (!qmp->resets)
767 return -ENOMEM;
770 qmp->resets[i].id = reset_list[i];
772 qmp->num_resets = num_resets;
774 ret = devm_reset_control_bulk_get_exclusive(dev, num_resets, qmp->resets);
781 static int qmp_usbc_clk_init(struct qmp_usbc *qmp)
783 struct device *dev = qmp->dev;
787 qmp->clks = devm_kcalloc(dev, num, sizeof(*qmp->clks), GFP_KERNEL);
788 if (!qmp->clks)
789 return -ENOMEM;
792 qmp->clks[i].id = qmp_usbc_phy_clk_l[i];
794 qmp->num_clks = num;
796 return devm_clk_bulk_get_optional(dev, num, qmp->clks);
807 * The <s>_pipe_clksrc generated by PHY goes to the GCC that gate
809 * by the PHY driver for its operations.
814 * +---------------+
815 * | PHY block |<<---------------------------------------+
817 * | +-------+ | +-----+ |
818 * I/P---^-->| PLL |---^--->pipe_clksrc--->| GCC |--->pipe_clk---+
819 * clk | +-------+ | +-----+
820 * +---------------+
822 static int phy_pipe_clk_register(struct qmp_usbc *qmp, struct device_node *np)
824 struct clk_fixed_rate *fixed = &qmp->pipe_clk_fixed;
828 ret = of_property_read_string(np, "clock-output-names", &init.name);
830 dev_err(qmp->dev, "%pOFn: No clock-output-names\n", np);
836 /* controllers using QMP phys use 125MHz pipe clock interface */
837 fixed->fixed_rate = 125000000;
838 fixed->hw.init = &init;
840 ret = devm_clk_hw_register(qmp->dev, &fixed->hw);
844 ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &fixed->hw);
852 return devm_add_action_or_reset(qmp->dev, phy_clk_release_provider, np);
859 struct qmp_usbc *qmp = typec_switch_get_drvdata(sw);
861 if (orientation == qmp->orientation || orientation == TYPEC_ORIENTATION_NONE)
864 mutex_lock(&qmp->phy_mutex);
865 qmp->orientation = orientation;
867 if (qmp->usb_init_count) {
868 qmp_usbc_power_off(qmp->phy);
869 qmp_usbc_exit(qmp->phy);
871 qmp_usbc_init(qmp->phy);
872 qmp_usbc_power_on(qmp->phy);
875 mutex_unlock(&qmp->phy_mutex);
882 struct qmp_usbc *qmp = data;
884 typec_switch_unregister(qmp->sw);
887 static int qmp_usbc_typec_switch_register(struct qmp_usbc *qmp)
890 struct device *dev = qmp->dev;
892 sw_desc.drvdata = qmp;
893 sw_desc.fwnode = dev->fwnode;
895 qmp->sw = typec_switch_register(dev, &sw_desc);
896 if (IS_ERR(qmp->sw)) {
897 dev_err(dev, "Unable to register typec switch: %pe\n", qmp->sw);
898 return PTR_ERR(qmp->sw);
901 return devm_add_action_or_reset(dev, qmp_usbc_typec_unregister, qmp);
904 static int qmp_usbc_typec_switch_register(struct qmp_usbc *qmp)
910 static int qmp_usbc_parse_dt_legacy(struct qmp_usbc *qmp, struct device_node *np)
912 struct platform_device *pdev = to_platform_device(qmp->dev);
913 struct device *dev = qmp->dev;
916 qmp->serdes = devm_platform_ioremap_resource(pdev, 0);
917 if (IS_ERR(qmp->serdes))
918 return PTR_ERR(qmp->serdes);
921 * Get memory resources for the PHY:
922 * Resources are indexed as: tx -> 0; rx -> 1; pcs -> 2.
923 * For dual lane PHYs: tx2 -> 3, rx2 -> 4, pcs_misc (optional) -> 5
924 * For single lane PHYs: pcs_misc (optional) -> 3.
926 qmp->tx = devm_of_iomap(dev, np, 0, NULL);
927 if (IS_ERR(qmp->tx))
928 return PTR_ERR(qmp->tx);
930 qmp->rx = devm_of_iomap(dev, np, 1, NULL);
931 if (IS_ERR(qmp->rx))
932 return PTR_ERR(qmp->rx);
934 qmp->pcs = devm_of_iomap(dev, np, 2, NULL);
935 if (IS_ERR(qmp->pcs))
936 return PTR_ERR(qmp->pcs);
938 qmp->tx2 = devm_of_iomap(dev, np, 3, NULL);
939 if (IS_ERR(qmp->tx2))
940 return PTR_ERR(qmp->tx2);
942 qmp->rx2 = devm_of_iomap(dev, np, 4, NULL);
943 if (IS_ERR(qmp->rx2))
944 return PTR_ERR(qmp->rx2);
946 qmp->pcs_misc = devm_of_iomap(dev, np, 5, NULL);
947 if (IS_ERR(qmp->pcs_misc)) {
948 dev_vdbg(dev, "PHY pcs_misc-reg not used\n");
949 qmp->pcs_misc = NULL;
952 qmp->pipe_clk = devm_get_clk_from_child(dev, np, NULL);
953 if (IS_ERR(qmp->pipe_clk)) {
954 return dev_err_probe(dev, PTR_ERR(qmp->pipe_clk),
958 ret = devm_clk_bulk_get_all(qmp->dev, &qmp->clks);
962 qmp->num_clks = ret;
964 ret = qmp_usbc_reset_init(qmp, usb3phy_legacy_reset_l,
972 static int qmp_usbc_parse_dt(struct qmp_usbc *qmp)
974 struct platform_device *pdev = to_platform_device(qmp->dev);
975 const struct qmp_phy_cfg *cfg = qmp->cfg;
976 const struct qmp_usbc_offsets *offs = cfg->offsets;
977 struct device *dev = qmp->dev;
982 return -EINVAL;
988 qmp->serdes = base + offs->serdes;
989 qmp->pcs = base + offs->pcs;
990 if (offs->pcs_misc)
991 qmp->pcs_misc = base + offs->pcs_misc;
992 qmp->tx = base + offs->tx;
993 qmp->rx = base + offs->rx;
995 qmp->tx2 = base + offs->tx2;
996 qmp->rx2 = base + offs->rx2;
998 ret = qmp_usbc_clk_init(qmp);
1002 qmp->pipe_clk = devm_clk_get(dev, "pipe");
1003 if (IS_ERR(qmp->pipe_clk)) {
1004 return dev_err_probe(dev, PTR_ERR(qmp->pipe_clk),
1008 ret = qmp_usbc_reset_init(qmp, usb3phy_reset_l,
1016 static int qmp_usbc_parse_vls_clamp(struct qmp_usbc *qmp)
1019 struct device *dev = qmp->dev;
1023 ret = of_parse_phandle_with_fixed_args(dev->of_node, "qcom,tcsr-reg", 1, 0,
1025 if (ret == -ENOENT)
1028 return dev_err_probe(dev, ret, "Failed to parse qcom,tcsr-reg\n");
1030 qmp->tcsr_map = syscon_node_to_regmap(tcsr_args.np);
1032 if (IS_ERR(qmp->tcsr_map))
1033 return PTR_ERR(qmp->tcsr_map);
1035 qmp->vls_clamp_reg = tcsr_args.args[0];
1042 struct device *dev = &pdev->dev;
1045 struct qmp_usbc *qmp;
1048 qmp = devm_kzalloc(dev, sizeof(*qmp), GFP_KERNEL);
1049 if (!qmp)
1050 return -ENOMEM;
1052 qmp->dev = dev;
1053 dev_set_drvdata(dev, qmp);
1055 qmp->orientation = TYPEC_ORIENTATION_NORMAL;
1057 qmp->cfg = of_device_get_match_data(dev);
1058 if (!qmp->cfg)
1059 return -EINVAL;
1061 mutex_init(&qmp->phy_mutex);
1063 ret = qmp_usbc_vreg_init(qmp);
1067 ret = qmp_usbc_typec_switch_register(qmp);
1071 ret = qmp_usbc_parse_vls_clamp(qmp);
1076 np = of_get_child_by_name(dev->of_node, "phy");
1078 ret = qmp_usbc_parse_dt_legacy(qmp, np);
1080 np = of_node_get(dev->of_node);
1081 ret = qmp_usbc_parse_dt(qmp);
1096 ret = phy_pipe_clk_register(qmp, np);
1100 qmp->phy = devm_phy_create(dev, np, &qmp_usbc_phy_ops);
1101 if (IS_ERR(qmp->phy)) {
1102 ret = PTR_ERR(qmp->phy);
1103 dev_err(dev, "failed to create PHY: %d\n", ret);
1107 phy_set_drvdata(qmp->phy, qmp);
1122 .compatible = "qcom,msm8998-qmp-usb3-phy",
1125 .compatible = "qcom,qcm2290-qmp-usb3-phy",
1128 .compatible = "qcom,qcs615-qmp-usb3-phy",
1131 .compatible = "qcom,sdm660-qmp-usb3-phy",
1134 .compatible = "qcom,sm6115-qmp-usb3-phy",
1144 .name = "qcom-qmp-usbc-phy",
1153 MODULE_DESCRIPTION("Qualcomm QMP USB-C PHY driver");