Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 1 | /* |
| 2 | * HDMI PLL |
| 3 | * |
| 4 | * Copyright (C) 2013 Texas Instruments Incorporated |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published by |
| 8 | * the Free Software Foundation. |
| 9 | */ |
| 10 | |
Tomi Valkeinen | ac9f242 | 2013-11-14 13:46:32 +0200 | [diff] [blame] | 11 | #define DSS_SUBSYS_NAME "HDMIPLL" |
| 12 | |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/platform_device.h> |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 18 | #include <linux/clk.h> |
| 19 | |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 20 | #include <video/omapdss.h> |
| 21 | |
| 22 | #include "dss.h" |
Archit Taneja | ef26958 | 2013-09-12 17:45:57 +0530 | [diff] [blame] | 23 | #include "hdmi.h" |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 24 | |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 25 | void hdmi_pll_dump(struct hdmi_pll_data *pll, struct seq_file *s) |
| 26 | { |
| 27 | #define DUMPPLL(r) seq_printf(s, "%-35s %08x\n", #r,\ |
| 28 | hdmi_read_reg(pll->base, r)) |
| 29 | |
| 30 | DUMPPLL(PLLCTRL_PLL_CONTROL); |
| 31 | DUMPPLL(PLLCTRL_PLL_STATUS); |
| 32 | DUMPPLL(PLLCTRL_PLL_GO); |
| 33 | DUMPPLL(PLLCTRL_CFG1); |
| 34 | DUMPPLL(PLLCTRL_CFG2); |
| 35 | DUMPPLL(PLLCTRL_CFG3); |
| 36 | DUMPPLL(PLLCTRL_SSC_CFG1); |
| 37 | DUMPPLL(PLLCTRL_SSC_CFG2); |
| 38 | DUMPPLL(PLLCTRL_CFG4); |
| 39 | } |
| 40 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 41 | void hdmi_pll_compute(struct hdmi_pll_data *pll, |
| 42 | unsigned long target_tmds, struct dss_pll_clock_info *pi) |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 43 | { |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 44 | unsigned long fint, clkdco, clkout; |
| 45 | unsigned long target_bitclk, target_clkdco; |
| 46 | unsigned long min_dco; |
| 47 | unsigned n, m, mf, m2, sd; |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 48 | unsigned long clkin; |
| 49 | const struct dss_pll_hw *hw = pll->pll.hw; |
| 50 | |
| 51 | clkin = clk_get_rate(pll->pll.clkin); |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 52 | |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 53 | DSSDBG("clkin %lu, target tmds %lu\n", clkin, target_tmds); |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 54 | |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 55 | target_bitclk = target_tmds * 10; |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 56 | |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 57 | /* Fint */ |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 58 | n = DIV_ROUND_UP(clkin, hw->fint_max); |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 59 | fint = clkin / n; |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 60 | |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 61 | /* adjust m2 so that the clkdco will be high enough */ |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 62 | min_dco = roundup(hw->clkdco_min, fint); |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 63 | m2 = DIV_ROUND_UP(min_dco, target_bitclk); |
| 64 | if (m2 == 0) |
| 65 | m2 = 1; |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 66 | |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 67 | target_clkdco = target_bitclk * m2; |
| 68 | m = target_clkdco / fint; |
| 69 | |
| 70 | clkdco = fint * m; |
| 71 | |
| 72 | /* adjust clkdco with fractional mf */ |
| 73 | if (WARN_ON(target_clkdco - clkdco > fint)) |
| 74 | mf = 0; |
Archit Taneja | 2d64b1b | 2013-09-23 15:12:34 +0530 | [diff] [blame] | 75 | else |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 76 | mf = (u32)div_u64(262144ull * (target_clkdco - clkdco), fint); |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 77 | |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 78 | if (mf > 0) |
| 79 | clkdco += (u32)div_u64((u64)mf * fint, 262144); |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 80 | |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 81 | clkout = clkdco / m2; |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 82 | |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 83 | /* sigma-delta */ |
| 84 | sd = DIV_ROUND_UP(fint * m, 250000000); |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 85 | |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 86 | DSSDBG("N = %u, M = %u, M.f = %u, M2 = %u, SD = %u\n", |
| 87 | n, m, mf, m2, sd); |
| 88 | DSSDBG("Fint %lu, clkdco %lu, clkout %lu\n", fint, clkdco, clkout); |
| 89 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 90 | pi->n = n; |
| 91 | pi->m = m; |
| 92 | pi->mf = mf; |
| 93 | pi->mX[0] = m2; |
| 94 | pi->sd = sd; |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 95 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 96 | pi->fint = fint; |
Tomi Valkeinen | 33f1312 | 2014-09-15 15:40:47 +0300 | [diff] [blame] | 97 | pi->clkdco = clkdco; |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 98 | pi->clkout[0] = clkout; |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 99 | } |
| 100 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 101 | static int hdmi_pll_enable(struct dss_pll *dsspll) |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 102 | { |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 103 | struct hdmi_pll_data *pll = container_of(dsspll, struct hdmi_pll_data, pll); |
Tomi Valkeinen | 03aafa2 | 2014-10-16 15:31:38 +0300 | [diff] [blame] | 104 | struct hdmi_wp_data *wp = pll->wp; |
Tomi Valkeinen | f7dd8f5 | 2016-05-17 17:00:52 +0300 | [diff] [blame] | 105 | int r; |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 106 | |
Tomi Valkeinen | adb5ff8 | 2014-12-31 11:26:18 +0200 | [diff] [blame] | 107 | dss_ctrl_pll_enable(DSS_PLL_HDMI, true); |
| 108 | |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 109 | r = hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_BOTHON_ALLCLKS); |
| 110 | if (r) |
| 111 | return r; |
| 112 | |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 116 | static void hdmi_pll_disable(struct dss_pll *dsspll) |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 117 | { |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 118 | struct hdmi_pll_data *pll = container_of(dsspll, struct hdmi_pll_data, pll); |
Tomi Valkeinen | 03aafa2 | 2014-10-16 15:31:38 +0300 | [diff] [blame] | 119 | struct hdmi_wp_data *wp = pll->wp; |
| 120 | |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 121 | hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_ALLOFF); |
Tomi Valkeinen | adb5ff8 | 2014-12-31 11:26:18 +0200 | [diff] [blame] | 122 | |
| 123 | dss_ctrl_pll_enable(DSS_PLL_HDMI, false); |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 124 | } |
| 125 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 126 | static const struct dss_pll_ops dsi_pll_ops = { |
| 127 | .enable = hdmi_pll_enable, |
| 128 | .disable = hdmi_pll_disable, |
| 129 | .set_config = dss_pll_write_config_type_b, |
Archit Taneja | 2d64b1b | 2013-09-23 15:12:34 +0530 | [diff] [blame] | 130 | }; |
| 131 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 132 | static const struct dss_pll_hw dss_omap4_hdmi_pll_hw = { |
Tomi Valkeinen | 06ede3d | 2016-05-18 10:48:44 +0300 | [diff] [blame^] | 133 | .type = DSS_PLL_TYPE_B, |
| 134 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 135 | .n_max = 255, |
| 136 | .m_min = 20, |
| 137 | .m_max = 4095, |
| 138 | .mX_max = 127, |
| 139 | .fint_min = 500000, |
| 140 | .fint_max = 2500000, |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 141 | |
| 142 | .clkdco_min = 500000000, |
| 143 | .clkdco_low = 1000000000, |
| 144 | .clkdco_max = 2000000000, |
| 145 | |
| 146 | .n_msb = 8, |
| 147 | .n_lsb = 1, |
| 148 | .m_msb = 20, |
| 149 | .m_lsb = 9, |
| 150 | |
| 151 | .mX_msb[0] = 24, |
| 152 | .mX_lsb[0] = 18, |
| 153 | |
| 154 | .has_selfreqdco = true, |
Archit Taneja | 2d64b1b | 2013-09-23 15:12:34 +0530 | [diff] [blame] | 155 | }; |
| 156 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 157 | static const struct dss_pll_hw dss_omap5_hdmi_pll_hw = { |
Tomi Valkeinen | 06ede3d | 2016-05-18 10:48:44 +0300 | [diff] [blame^] | 158 | .type = DSS_PLL_TYPE_B, |
| 159 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 160 | .n_max = 255, |
| 161 | .m_min = 20, |
| 162 | .m_max = 2045, |
| 163 | .mX_max = 127, |
| 164 | .fint_min = 620000, |
| 165 | .fint_max = 2500000, |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 166 | |
| 167 | .clkdco_min = 750000000, |
| 168 | .clkdco_low = 1500000000, |
| 169 | .clkdco_max = 2500000000UL, |
| 170 | |
| 171 | .n_msb = 8, |
| 172 | .n_lsb = 1, |
| 173 | .m_msb = 20, |
| 174 | .m_lsb = 9, |
| 175 | |
| 176 | .mX_msb[0] = 24, |
| 177 | .mX_lsb[0] = 18, |
| 178 | |
| 179 | .has_selfreqdco = true, |
| 180 | .has_refsel = true, |
| 181 | }; |
| 182 | |
| 183 | static int dsi_init_pll_data(struct platform_device *pdev, struct hdmi_pll_data *hpll) |
Archit Taneja | 2d64b1b | 2013-09-23 15:12:34 +0530 | [diff] [blame] | 184 | { |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 185 | struct dss_pll *pll = &hpll->pll; |
| 186 | struct clk *clk; |
| 187 | int r; |
Archit Taneja | 2d64b1b | 2013-09-23 15:12:34 +0530 | [diff] [blame] | 188 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 189 | clk = devm_clk_get(&pdev->dev, "sys_clk"); |
| 190 | if (IS_ERR(clk)) { |
| 191 | DSSERR("can't get sys_clk\n"); |
| 192 | return PTR_ERR(clk); |
Archit Taneja | 2d64b1b | 2013-09-23 15:12:34 +0530 | [diff] [blame] | 193 | } |
| 194 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 195 | pll->name = "hdmi"; |
Tomi Valkeinen | 64e22ff | 2015-01-02 10:05:33 +0200 | [diff] [blame] | 196 | pll->id = DSS_PLL_HDMI; |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 197 | pll->base = hpll->base; |
| 198 | pll->clkin = clk; |
| 199 | |
Archit Taneja | 2d64b1b | 2013-09-23 15:12:34 +0530 | [diff] [blame] | 200 | switch (omapdss_get_version()) { |
| 201 | case OMAPDSS_VER_OMAP4430_ES1: |
| 202 | case OMAPDSS_VER_OMAP4430_ES2: |
| 203 | case OMAPDSS_VER_OMAP4: |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 204 | pll->hw = &dss_omap4_hdmi_pll_hw; |
Archit Taneja | 2d64b1b | 2013-09-23 15:12:34 +0530 | [diff] [blame] | 205 | break; |
| 206 | |
| 207 | case OMAPDSS_VER_OMAP5: |
Tomi Valkeinen | adb5ff8 | 2014-12-31 11:26:18 +0200 | [diff] [blame] | 208 | case OMAPDSS_VER_DRA7xx: |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 209 | pll->hw = &dss_omap5_hdmi_pll_hw; |
Archit Taneja | 2d64b1b | 2013-09-23 15:12:34 +0530 | [diff] [blame] | 210 | break; |
| 211 | |
| 212 | default: |
| 213 | return -ENODEV; |
| 214 | } |
| 215 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 216 | pll->ops = &dsi_pll_ops; |
| 217 | |
| 218 | r = dss_pll_register(pll); |
| 219 | if (r) |
| 220 | return r; |
Archit Taneja | 2d64b1b | 2013-09-23 15:12:34 +0530 | [diff] [blame] | 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Tomi Valkeinen | 03aafa2 | 2014-10-16 15:31:38 +0300 | [diff] [blame] | 225 | int hdmi_pll_init(struct platform_device *pdev, struct hdmi_pll_data *pll, |
| 226 | struct hdmi_wp_data *wp) |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 227 | { |
Archit Taneja | 2d64b1b | 2013-09-23 15:12:34 +0530 | [diff] [blame] | 228 | int r; |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 229 | struct resource *res; |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 230 | |
Tomi Valkeinen | 03aafa2 | 2014-10-16 15:31:38 +0300 | [diff] [blame] | 231 | pll->wp = wp; |
| 232 | |
Tomi Valkeinen | 7760150 | 2013-12-17 14:41:14 +0200 | [diff] [blame] | 233 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll"); |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 234 | if (!res) { |
Tomi Valkeinen | 59b3d38 | 2014-04-28 16:11:01 +0300 | [diff] [blame] | 235 | DSSERR("can't get PLL mem resource\n"); |
| 236 | return -EINVAL; |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 237 | } |
| 238 | |
Tomi Valkeinen | 59b3d38 | 2014-04-28 16:11:01 +0300 | [diff] [blame] | 239 | pll->base = devm_ioremap_resource(&pdev->dev, res); |
Tomi Valkeinen | 2b22df8 | 2014-05-23 14:50:09 +0300 | [diff] [blame] | 240 | if (IS_ERR(pll->base)) { |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 241 | DSSERR("can't ioremap PLLCTRL\n"); |
Tomi Valkeinen | 2b22df8 | 2014-05-23 14:50:09 +0300 | [diff] [blame] | 242 | return PTR_ERR(pll->base); |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 243 | } |
| 244 | |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 245 | r = dsi_init_pll_data(pdev, pll); |
| 246 | if (r) { |
| 247 | DSSERR("failed to init HDMI PLL\n"); |
| 248 | return r; |
| 249 | } |
| 250 | |
Archit Taneja | c1577c1 | 2013-10-08 12:55:26 +0530 | [diff] [blame] | 251 | return 0; |
| 252 | } |
Tomi Valkeinen | c84c3a5 | 2014-10-22 15:02:17 +0300 | [diff] [blame] | 253 | |
| 254 | void hdmi_pll_uninit(struct hdmi_pll_data *hpll) |
| 255 | { |
| 256 | struct dss_pll *pll = &hpll->pll; |
| 257 | |
| 258 | dss_pll_unregister(pll); |
| 259 | } |