1b28be59aSCK Hu // SPDX-License-Identifier: GPL-2.0-only 2b28be59aSCK Hu /* 3b28be59aSCK Hu * Copyright (c) 2014 MediaTek Inc. 4b28be59aSCK Hu * Author: Jie Qiu <jie.qiu@mediatek.com> 5b28be59aSCK Hu */ 6b28be59aSCK Hu 7b28be59aSCK Hu #include "phy-mtk-hdmi.h" 8b28be59aSCK Hu 9b28be59aSCK Hu #define HDMI_CON0 0x00 10b28be59aSCK Hu #define RG_HDMITX_PLL_EN BIT(31) 11b28be59aSCK Hu #define RG_HDMITX_PLL_FBKDIV (0x7f << 24) 12b28be59aSCK Hu #define PLL_FBKDIV_SHIFT 24 13b28be59aSCK Hu #define RG_HDMITX_PLL_FBKSEL (0x3 << 22) 14b28be59aSCK Hu #define PLL_FBKSEL_SHIFT 22 15b28be59aSCK Hu #define RG_HDMITX_PLL_PREDIV (0x3 << 20) 16b28be59aSCK Hu #define PREDIV_SHIFT 20 17b28be59aSCK Hu #define RG_HDMITX_PLL_POSDIV (0x3 << 18) 18b28be59aSCK Hu #define POSDIV_SHIFT 18 19b28be59aSCK Hu #define RG_HDMITX_PLL_RST_DLY (0x3 << 16) 20b28be59aSCK Hu #define RG_HDMITX_PLL_IR (0xf << 12) 21b28be59aSCK Hu #define PLL_IR_SHIFT 12 22b28be59aSCK Hu #define RG_HDMITX_PLL_IC (0xf << 8) 23b28be59aSCK Hu #define PLL_IC_SHIFT 8 24b28be59aSCK Hu #define RG_HDMITX_PLL_BP (0xf << 4) 25b28be59aSCK Hu #define PLL_BP_SHIFT 4 26b28be59aSCK Hu #define RG_HDMITX_PLL_BR (0x3 << 2) 27b28be59aSCK Hu #define PLL_BR_SHIFT 2 28b28be59aSCK Hu #define RG_HDMITX_PLL_BC (0x3 << 0) 29b28be59aSCK Hu #define PLL_BC_SHIFT 0 30b28be59aSCK Hu #define HDMI_CON1 0x04 31b28be59aSCK Hu #define RG_HDMITX_PLL_DIVEN (0x7 << 29) 32b28be59aSCK Hu #define PLL_DIVEN_SHIFT 29 33b28be59aSCK Hu #define RG_HDMITX_PLL_AUTOK_EN BIT(28) 34b28be59aSCK Hu #define RG_HDMITX_PLL_AUTOK_KF (0x3 << 26) 35b28be59aSCK Hu #define RG_HDMITX_PLL_AUTOK_KS (0x3 << 24) 36b28be59aSCK Hu #define RG_HDMITX_PLL_AUTOK_LOAD BIT(23) 37b28be59aSCK Hu #define RG_HDMITX_PLL_BAND (0x3f << 16) 38b28be59aSCK Hu #define RG_HDMITX_PLL_REF_SEL BIT(15) 39b28be59aSCK Hu #define RG_HDMITX_PLL_BIAS_EN BIT(14) 40b28be59aSCK Hu #define RG_HDMITX_PLL_BIAS_LPF_EN BIT(13) 41b28be59aSCK Hu #define RG_HDMITX_PLL_TXDIV_EN BIT(12) 42b28be59aSCK Hu #define RG_HDMITX_PLL_TXDIV (0x3 << 10) 43b28be59aSCK Hu #define PLL_TXDIV_SHIFT 10 44b28be59aSCK Hu #define RG_HDMITX_PLL_LVROD_EN BIT(9) 45b28be59aSCK Hu #define RG_HDMITX_PLL_MONVC_EN BIT(8) 46b28be59aSCK Hu #define RG_HDMITX_PLL_MONCK_EN BIT(7) 47b28be59aSCK Hu #define RG_HDMITX_PLL_MONREF_EN BIT(6) 48b28be59aSCK Hu #define RG_HDMITX_PLL_TST_EN BIT(5) 49b28be59aSCK Hu #define RG_HDMITX_PLL_TST_CK_EN BIT(4) 50b28be59aSCK Hu #define RG_HDMITX_PLL_TST_SEL (0xf << 0) 51b28be59aSCK Hu #define HDMI_CON2 0x08 52b28be59aSCK Hu #define RGS_HDMITX_PLL_AUTOK_BAND (0x7f << 8) 53b28be59aSCK Hu #define RGS_HDMITX_PLL_AUTOK_FAIL BIT(1) 54b28be59aSCK Hu #define RG_HDMITX_EN_TX_CKLDO BIT(0) 55b28be59aSCK Hu #define HDMI_CON3 0x0c 56b28be59aSCK Hu #define RG_HDMITX_SER_EN (0xf << 28) 57b28be59aSCK Hu #define RG_HDMITX_PRD_EN (0xf << 24) 58b28be59aSCK Hu #define RG_HDMITX_PRD_IMP_EN (0xf << 20) 59b28be59aSCK Hu #define RG_HDMITX_DRV_EN (0xf << 16) 60b28be59aSCK Hu #define RG_HDMITX_DRV_IMP_EN (0xf << 12) 61b28be59aSCK Hu #define DRV_IMP_EN_SHIFT 12 62b28be59aSCK Hu #define RG_HDMITX_MHLCK_FORCE BIT(10) 63b28be59aSCK Hu #define RG_HDMITX_MHLCK_PPIX_EN BIT(9) 64b28be59aSCK Hu #define RG_HDMITX_MHLCK_EN BIT(8) 65b28be59aSCK Hu #define RG_HDMITX_SER_DIN_SEL (0xf << 4) 66b28be59aSCK Hu #define RG_HDMITX_SER_5T1_BIST_EN BIT(3) 67b28be59aSCK Hu #define RG_HDMITX_SER_BIST_TOG BIT(2) 68b28be59aSCK Hu #define RG_HDMITX_SER_DIN_TOG BIT(1) 69b28be59aSCK Hu #define RG_HDMITX_SER_CLKDIG_INV BIT(0) 70b28be59aSCK Hu #define HDMI_CON4 0x10 71b28be59aSCK Hu #define RG_HDMITX_PRD_IBIAS_CLK (0xf << 24) 72b28be59aSCK Hu #define RG_HDMITX_PRD_IBIAS_D2 (0xf << 16) 73b28be59aSCK Hu #define RG_HDMITX_PRD_IBIAS_D1 (0xf << 8) 74b28be59aSCK Hu #define RG_HDMITX_PRD_IBIAS_D0 (0xf << 0) 75b28be59aSCK Hu #define PRD_IBIAS_CLK_SHIFT 24 76b28be59aSCK Hu #define PRD_IBIAS_D2_SHIFT 16 77b28be59aSCK Hu #define PRD_IBIAS_D1_SHIFT 8 78b28be59aSCK Hu #define PRD_IBIAS_D0_SHIFT 0 79b28be59aSCK Hu #define HDMI_CON5 0x14 80b28be59aSCK Hu #define RG_HDMITX_DRV_IBIAS_CLK (0x3f << 24) 81b28be59aSCK Hu #define RG_HDMITX_DRV_IBIAS_D2 (0x3f << 16) 82b28be59aSCK Hu #define RG_HDMITX_DRV_IBIAS_D1 (0x3f << 8) 83b28be59aSCK Hu #define RG_HDMITX_DRV_IBIAS_D0 (0x3f << 0) 84b28be59aSCK Hu #define DRV_IBIAS_CLK_SHIFT 24 85b28be59aSCK Hu #define DRV_IBIAS_D2_SHIFT 16 86b28be59aSCK Hu #define DRV_IBIAS_D1_SHIFT 8 87b28be59aSCK Hu #define DRV_IBIAS_D0_SHIFT 0 88b28be59aSCK Hu #define HDMI_CON6 0x18 89b28be59aSCK Hu #define RG_HDMITX_DRV_IMP_CLK (0x3f << 24) 90b28be59aSCK Hu #define RG_HDMITX_DRV_IMP_D2 (0x3f << 16) 91b28be59aSCK Hu #define RG_HDMITX_DRV_IMP_D1 (0x3f << 8) 92b28be59aSCK Hu #define RG_HDMITX_DRV_IMP_D0 (0x3f << 0) 93b28be59aSCK Hu #define DRV_IMP_CLK_SHIFT 24 94b28be59aSCK Hu #define DRV_IMP_D2_SHIFT 16 95b28be59aSCK Hu #define DRV_IMP_D1_SHIFT 8 96b28be59aSCK Hu #define DRV_IMP_D0_SHIFT 0 97b28be59aSCK Hu #define HDMI_CON7 0x1c 98b28be59aSCK Hu #define RG_HDMITX_MHLCK_DRV_IBIAS (0x1f << 27) 99b28be59aSCK Hu #define RG_HDMITX_SER_DIN (0x3ff << 16) 100b28be59aSCK Hu #define RG_HDMITX_CHLDC_TST (0xf << 12) 101b28be59aSCK Hu #define RG_HDMITX_CHLCK_TST (0xf << 8) 102b28be59aSCK Hu #define RG_HDMITX_RESERVE (0xff << 0) 103b28be59aSCK Hu #define HDMI_CON8 0x20 104b28be59aSCK Hu #define RGS_HDMITX_2T1_LEV (0xf << 16) 105b28be59aSCK Hu #define RGS_HDMITX_2T1_EDG (0xf << 12) 106b28be59aSCK Hu #define RGS_HDMITX_5T1_LEV (0xf << 8) 107b28be59aSCK Hu #define RGS_HDMITX_5T1_EDG (0xf << 4) 108b28be59aSCK Hu #define RGS_HDMITX_PLUG_TST BIT(0) 109b28be59aSCK Hu 110b28be59aSCK Hu static int mtk_hdmi_pll_prepare(struct clk_hw *hw) 111b28be59aSCK Hu { 112b28be59aSCK Hu struct mtk_hdmi_phy *hdmi_phy = to_mtk_hdmi_phy(hw); 113b28be59aSCK Hu 114b28be59aSCK Hu mtk_hdmi_phy_set_bits(hdmi_phy, HDMI_CON1, RG_HDMITX_PLL_AUTOK_EN); 115b28be59aSCK Hu mtk_hdmi_phy_set_bits(hdmi_phy, HDMI_CON0, RG_HDMITX_PLL_POSDIV); 116b28be59aSCK Hu mtk_hdmi_phy_clear_bits(hdmi_phy, HDMI_CON3, RG_HDMITX_MHLCK_EN); 117b28be59aSCK Hu mtk_hdmi_phy_set_bits(hdmi_phy, HDMI_CON1, RG_HDMITX_PLL_BIAS_EN); 118b28be59aSCK Hu usleep_range(100, 150); 119b28be59aSCK Hu mtk_hdmi_phy_set_bits(hdmi_phy, HDMI_CON0, RG_HDMITX_PLL_EN); 120b28be59aSCK Hu usleep_range(100, 150); 121b28be59aSCK Hu mtk_hdmi_phy_set_bits(hdmi_phy, HDMI_CON1, RG_HDMITX_PLL_BIAS_LPF_EN); 122b28be59aSCK Hu mtk_hdmi_phy_set_bits(hdmi_phy, HDMI_CON1, RG_HDMITX_PLL_TXDIV_EN); 123b28be59aSCK Hu 124b28be59aSCK Hu return 0; 125b28be59aSCK Hu } 126b28be59aSCK Hu 127b28be59aSCK Hu static void mtk_hdmi_pll_unprepare(struct clk_hw *hw) 128b28be59aSCK Hu { 129b28be59aSCK Hu struct mtk_hdmi_phy *hdmi_phy = to_mtk_hdmi_phy(hw); 130b28be59aSCK Hu 131b28be59aSCK Hu mtk_hdmi_phy_clear_bits(hdmi_phy, HDMI_CON1, RG_HDMITX_PLL_TXDIV_EN); 132b28be59aSCK Hu mtk_hdmi_phy_clear_bits(hdmi_phy, HDMI_CON1, RG_HDMITX_PLL_BIAS_LPF_EN); 133b28be59aSCK Hu usleep_range(100, 150); 134b28be59aSCK Hu mtk_hdmi_phy_clear_bits(hdmi_phy, HDMI_CON0, RG_HDMITX_PLL_EN); 135b28be59aSCK Hu usleep_range(100, 150); 136b28be59aSCK Hu mtk_hdmi_phy_clear_bits(hdmi_phy, HDMI_CON1, RG_HDMITX_PLL_BIAS_EN); 137b28be59aSCK Hu mtk_hdmi_phy_clear_bits(hdmi_phy, HDMI_CON0, RG_HDMITX_PLL_POSDIV); 138b28be59aSCK Hu mtk_hdmi_phy_clear_bits(hdmi_phy, HDMI_CON1, RG_HDMITX_PLL_AUTOK_EN); 139b28be59aSCK Hu usleep_range(100, 150); 140b28be59aSCK Hu } 141b28be59aSCK Hu 142b28be59aSCK Hu static long mtk_hdmi_pll_round_rate(struct clk_hw *hw, unsigned long rate, 143b28be59aSCK Hu unsigned long *parent_rate) 144b28be59aSCK Hu { 145b28be59aSCK Hu struct mtk_hdmi_phy *hdmi_phy = to_mtk_hdmi_phy(hw); 146b28be59aSCK Hu 147b28be59aSCK Hu hdmi_phy->pll_rate = rate; 148b28be59aSCK Hu if (rate <= 74250000) 149b28be59aSCK Hu *parent_rate = rate; 150b28be59aSCK Hu else 151b28be59aSCK Hu *parent_rate = rate / 2; 152b28be59aSCK Hu 153b28be59aSCK Hu return rate; 154b28be59aSCK Hu } 155b28be59aSCK Hu 156b28be59aSCK Hu static int mtk_hdmi_pll_set_rate(struct clk_hw *hw, unsigned long rate, 157b28be59aSCK Hu unsigned long parent_rate) 158b28be59aSCK Hu { 159b28be59aSCK Hu struct mtk_hdmi_phy *hdmi_phy = to_mtk_hdmi_phy(hw); 160b28be59aSCK Hu unsigned int pre_div; 161b28be59aSCK Hu unsigned int div; 162b28be59aSCK Hu unsigned int pre_ibias; 163b28be59aSCK Hu unsigned int hdmi_ibias; 164b28be59aSCK Hu unsigned int imp_en; 165b28be59aSCK Hu 166b28be59aSCK Hu dev_dbg(hdmi_phy->dev, "%s: %lu Hz, parent: %lu Hz\n", __func__, 167b28be59aSCK Hu rate, parent_rate); 168b28be59aSCK Hu 169b28be59aSCK Hu if (rate <= 27000000) { 170b28be59aSCK Hu pre_div = 0; 171b28be59aSCK Hu div = 3; 172b28be59aSCK Hu } else if (rate <= 74250000) { 173b28be59aSCK Hu pre_div = 1; 174b28be59aSCK Hu div = 2; 175b28be59aSCK Hu } else { 176b28be59aSCK Hu pre_div = 1; 177b28be59aSCK Hu div = 1; 178b28be59aSCK Hu } 179b28be59aSCK Hu 180b28be59aSCK Hu mtk_hdmi_phy_mask(hdmi_phy, HDMI_CON0, 181b28be59aSCK Hu (pre_div << PREDIV_SHIFT), RG_HDMITX_PLL_PREDIV); 182b28be59aSCK Hu mtk_hdmi_phy_set_bits(hdmi_phy, HDMI_CON0, RG_HDMITX_PLL_POSDIV); 183b28be59aSCK Hu mtk_hdmi_phy_mask(hdmi_phy, HDMI_CON0, 184b28be59aSCK Hu (0x1 << PLL_IC_SHIFT) | (0x1 << PLL_IR_SHIFT), 185b28be59aSCK Hu RG_HDMITX_PLL_IC | RG_HDMITX_PLL_IR); 186b28be59aSCK Hu mtk_hdmi_phy_mask(hdmi_phy, HDMI_CON1, 187b28be59aSCK Hu (div << PLL_TXDIV_SHIFT), RG_HDMITX_PLL_TXDIV); 188b28be59aSCK Hu mtk_hdmi_phy_mask(hdmi_phy, HDMI_CON0, 189b28be59aSCK Hu (0x1 << PLL_FBKSEL_SHIFT) | (19 << PLL_FBKDIV_SHIFT), 190b28be59aSCK Hu RG_HDMITX_PLL_FBKSEL | RG_HDMITX_PLL_FBKDIV); 191b28be59aSCK Hu mtk_hdmi_phy_mask(hdmi_phy, HDMI_CON1, 192b28be59aSCK Hu (0x2 << PLL_DIVEN_SHIFT), RG_HDMITX_PLL_DIVEN); 193b28be59aSCK Hu mtk_hdmi_phy_mask(hdmi_phy, HDMI_CON0, 194b28be59aSCK Hu (0xc << PLL_BP_SHIFT) | (0x2 << PLL_BC_SHIFT) | 195b28be59aSCK Hu (0x1 << PLL_BR_SHIFT), 196b28be59aSCK Hu RG_HDMITX_PLL_BP | RG_HDMITX_PLL_BC | 197b28be59aSCK Hu RG_HDMITX_PLL_BR); 198b28be59aSCK Hu if (rate < 165000000) { 199b28be59aSCK Hu mtk_hdmi_phy_clear_bits(hdmi_phy, HDMI_CON3, 200b28be59aSCK Hu RG_HDMITX_PRD_IMP_EN); 201b28be59aSCK Hu pre_ibias = 0x3; 202b28be59aSCK Hu imp_en = 0x0; 203b28be59aSCK Hu hdmi_ibias = hdmi_phy->ibias; 204b28be59aSCK Hu } else { 205b28be59aSCK Hu mtk_hdmi_phy_set_bits(hdmi_phy, HDMI_CON3, 206b28be59aSCK Hu RG_HDMITX_PRD_IMP_EN); 207b28be59aSCK Hu pre_ibias = 0x6; 208b28be59aSCK Hu imp_en = 0xf; 209b28be59aSCK Hu hdmi_ibias = hdmi_phy->ibias_up; 210b28be59aSCK Hu } 211b28be59aSCK Hu mtk_hdmi_phy_mask(hdmi_phy, HDMI_CON4, 212b28be59aSCK Hu (pre_ibias << PRD_IBIAS_CLK_SHIFT) | 213b28be59aSCK Hu (pre_ibias << PRD_IBIAS_D2_SHIFT) | 214b28be59aSCK Hu (pre_ibias << PRD_IBIAS_D1_SHIFT) | 215b28be59aSCK Hu (pre_ibias << PRD_IBIAS_D0_SHIFT), 216b28be59aSCK Hu RG_HDMITX_PRD_IBIAS_CLK | 217b28be59aSCK Hu RG_HDMITX_PRD_IBIAS_D2 | 218b28be59aSCK Hu RG_HDMITX_PRD_IBIAS_D1 | 219b28be59aSCK Hu RG_HDMITX_PRD_IBIAS_D0); 220b28be59aSCK Hu mtk_hdmi_phy_mask(hdmi_phy, HDMI_CON3, 221b28be59aSCK Hu (imp_en << DRV_IMP_EN_SHIFT), 222b28be59aSCK Hu RG_HDMITX_DRV_IMP_EN); 223b28be59aSCK Hu mtk_hdmi_phy_mask(hdmi_phy, HDMI_CON6, 224b28be59aSCK Hu (hdmi_phy->drv_imp_clk << DRV_IMP_CLK_SHIFT) | 225b28be59aSCK Hu (hdmi_phy->drv_imp_d2 << DRV_IMP_D2_SHIFT) | 226b28be59aSCK Hu (hdmi_phy->drv_imp_d1 << DRV_IMP_D1_SHIFT) | 227b28be59aSCK Hu (hdmi_phy->drv_imp_d0 << DRV_IMP_D0_SHIFT), 228b28be59aSCK Hu RG_HDMITX_DRV_IMP_CLK | RG_HDMITX_DRV_IMP_D2 | 229b28be59aSCK Hu RG_HDMITX_DRV_IMP_D1 | RG_HDMITX_DRV_IMP_D0); 230b28be59aSCK Hu mtk_hdmi_phy_mask(hdmi_phy, HDMI_CON5, 231b28be59aSCK Hu (hdmi_ibias << DRV_IBIAS_CLK_SHIFT) | 232b28be59aSCK Hu (hdmi_ibias << DRV_IBIAS_D2_SHIFT) | 233b28be59aSCK Hu (hdmi_ibias << DRV_IBIAS_D1_SHIFT) | 234b28be59aSCK Hu (hdmi_ibias << DRV_IBIAS_D0_SHIFT), 235b28be59aSCK Hu RG_HDMITX_DRV_IBIAS_CLK | 236b28be59aSCK Hu RG_HDMITX_DRV_IBIAS_D2 | 237b28be59aSCK Hu RG_HDMITX_DRV_IBIAS_D1 | 238b28be59aSCK Hu RG_HDMITX_DRV_IBIAS_D0); 239b28be59aSCK Hu return 0; 240b28be59aSCK Hu } 241b28be59aSCK Hu 242b28be59aSCK Hu static unsigned long mtk_hdmi_pll_recalc_rate(struct clk_hw *hw, 243b28be59aSCK Hu unsigned long parent_rate) 244b28be59aSCK Hu { 245b28be59aSCK Hu struct mtk_hdmi_phy *hdmi_phy = to_mtk_hdmi_phy(hw); 246b28be59aSCK Hu 247b28be59aSCK Hu return hdmi_phy->pll_rate; 248b28be59aSCK Hu } 249b28be59aSCK Hu 250b28be59aSCK Hu static const struct clk_ops mtk_hdmi_phy_pll_ops = { 251b28be59aSCK Hu .prepare = mtk_hdmi_pll_prepare, 252b28be59aSCK Hu .unprepare = mtk_hdmi_pll_unprepare, 253b28be59aSCK Hu .set_rate = mtk_hdmi_pll_set_rate, 254b28be59aSCK Hu .round_rate = mtk_hdmi_pll_round_rate, 255b28be59aSCK Hu .recalc_rate = mtk_hdmi_pll_recalc_rate, 256b28be59aSCK Hu }; 257b28be59aSCK Hu 258b28be59aSCK Hu static void mtk_hdmi_phy_enable_tmds(struct mtk_hdmi_phy *hdmi_phy) 259b28be59aSCK Hu { 260b28be59aSCK Hu mtk_hdmi_phy_set_bits(hdmi_phy, HDMI_CON3, 261b28be59aSCK Hu RG_HDMITX_SER_EN | RG_HDMITX_PRD_EN | 262b28be59aSCK Hu RG_HDMITX_DRV_EN); 263b28be59aSCK Hu usleep_range(100, 150); 264b28be59aSCK Hu } 265b28be59aSCK Hu 266b28be59aSCK Hu static void mtk_hdmi_phy_disable_tmds(struct mtk_hdmi_phy *hdmi_phy) 267b28be59aSCK Hu { 268b28be59aSCK Hu mtk_hdmi_phy_clear_bits(hdmi_phy, HDMI_CON3, 269b28be59aSCK Hu RG_HDMITX_DRV_EN | RG_HDMITX_PRD_EN | 270b28be59aSCK Hu RG_HDMITX_SER_EN); 271b28be59aSCK Hu } 272b28be59aSCK Hu 273b28be59aSCK Hu struct mtk_hdmi_phy_conf mtk_hdmi_phy_8173_conf = { 274b28be59aSCK Hu .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_GATE, 275b28be59aSCK Hu .hdmi_phy_clk_ops = &mtk_hdmi_phy_pll_ops, 276b28be59aSCK Hu .hdmi_phy_enable_tmds = mtk_hdmi_phy_enable_tmds, 277b28be59aSCK Hu .hdmi_phy_disable_tmds = mtk_hdmi_phy_disable_tmds, 278b28be59aSCK Hu }; 279b28be59aSCK Hu 280b28be59aSCK Hu MODULE_AUTHOR("Jie Qiu <jie.qiu@mediatek.com>"); 281b28be59aSCK Hu MODULE_DESCRIPTION("MediaTek MT8173 HDMI PHY Driver"); 282b28be59aSCK Hu MODULE_LICENSE("GPL v2"); 283