blob: 110ed50d5ce189d1d6ae9e4e822948a95c98cde8 [file] [log] [blame]
Archit Tanejac1577c12013-10-08 12:55:26 +05301/*
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 Valkeinenac9f2422013-11-14 13:46:32 +020011#define DSS_SUBSYS_NAME "HDMIPLL"
12
Archit Tanejac1577c12013-10-08 12:55:26 +053013#include <linux/kernel.h>
14#include <linux/module.h>
Archit Tanejac1577c12013-10-08 12:55:26 +053015#include <linux/err.h>
16#include <linux/io.h>
17#include <linux/platform_device.h>
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +030018#include <linux/clk.h>
19
Archit Tanejac1577c12013-10-08 12:55:26 +053020#include <video/omapdss.h>
21
22#include "dss.h"
Archit Tanejaef269582013-09-12 17:45:57 +053023#include "hdmi.h"
Archit Tanejac1577c12013-10-08 12:55:26 +053024
Archit Tanejac1577c12013-10-08 12:55:26 +053025void 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 Valkeinenc84c3a52014-10-22 15:02:17 +030041void hdmi_pll_compute(struct hdmi_pll_data *pll,
42 unsigned long target_tmds, struct dss_pll_clock_info *pi)
Archit Tanejac1577c12013-10-08 12:55:26 +053043{
Tomi Valkeinen33f13122014-09-15 15:40:47 +030044 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 Valkeinenc84c3a52014-10-22 15:02:17 +030048 unsigned long clkin;
49 const struct dss_pll_hw *hw = pll->pll.hw;
50
51 clkin = clk_get_rate(pll->pll.clkin);
Archit Tanejac1577c12013-10-08 12:55:26 +053052
Tomi Valkeinen33f13122014-09-15 15:40:47 +030053 DSSDBG("clkin %lu, target tmds %lu\n", clkin, target_tmds);
Archit Tanejac1577c12013-10-08 12:55:26 +053054
Tomi Valkeinen33f13122014-09-15 15:40:47 +030055 target_bitclk = target_tmds * 10;
Archit Tanejac1577c12013-10-08 12:55:26 +053056
Tomi Valkeinen33f13122014-09-15 15:40:47 +030057 /* Fint */
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +030058 n = DIV_ROUND_UP(clkin, hw->fint_max);
Tomi Valkeinen33f13122014-09-15 15:40:47 +030059 fint = clkin / n;
Archit Tanejac1577c12013-10-08 12:55:26 +053060
Tomi Valkeinen33f13122014-09-15 15:40:47 +030061 /* adjust m2 so that the clkdco will be high enough */
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +030062 min_dco = roundup(hw->clkdco_min, fint);
Tomi Valkeinen33f13122014-09-15 15:40:47 +030063 m2 = DIV_ROUND_UP(min_dco, target_bitclk);
64 if (m2 == 0)
65 m2 = 1;
Archit Tanejac1577c12013-10-08 12:55:26 +053066
Tomi Valkeinen33f13122014-09-15 15:40:47 +030067 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 Taneja2d64b1b2013-09-23 15:12:34 +053075 else
Tomi Valkeinen33f13122014-09-15 15:40:47 +030076 mf = (u32)div_u64(262144ull * (target_clkdco - clkdco), fint);
Archit Tanejac1577c12013-10-08 12:55:26 +053077
Tomi Valkeinen33f13122014-09-15 15:40:47 +030078 if (mf > 0)
79 clkdco += (u32)div_u64((u64)mf * fint, 262144);
Archit Tanejac1577c12013-10-08 12:55:26 +053080
Tomi Valkeinen33f13122014-09-15 15:40:47 +030081 clkout = clkdco / m2;
Archit Tanejac1577c12013-10-08 12:55:26 +053082
Tomi Valkeinen33f13122014-09-15 15:40:47 +030083 /* sigma-delta */
84 sd = DIV_ROUND_UP(fint * m, 250000000);
Archit Tanejac1577c12013-10-08 12:55:26 +053085
Tomi Valkeinen33f13122014-09-15 15:40:47 +030086 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 Valkeinenc84c3a52014-10-22 15:02:17 +030090 pi->n = n;
91 pi->m = m;
92 pi->mf = mf;
93 pi->mX[0] = m2;
94 pi->sd = sd;
Tomi Valkeinen33f13122014-09-15 15:40:47 +030095
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +030096 pi->fint = fint;
Tomi Valkeinen33f13122014-09-15 15:40:47 +030097 pi->clkdco = clkdco;
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +030098 pi->clkout[0] = clkout;
Archit Tanejac1577c12013-10-08 12:55:26 +053099}
100
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300101static int hdmi_pll_enable(struct dss_pll *dsspll)
Archit Tanejac1577c12013-10-08 12:55:26 +0530102{
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300103 struct hdmi_pll_data *pll = container_of(dsspll, struct hdmi_pll_data, pll);
Tomi Valkeinen03aafa22014-10-16 15:31:38 +0300104 struct hdmi_wp_data *wp = pll->wp;
Tomi Valkeinenf7dd8f52016-05-17 17:00:52 +0300105 int r;
Archit Tanejac1577c12013-10-08 12:55:26 +0530106
Tomi Valkeinenadb5ff82014-12-31 11:26:18 +0200107 dss_ctrl_pll_enable(DSS_PLL_HDMI, true);
108
Archit Tanejac1577c12013-10-08 12:55:26 +0530109 r = hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_BOTHON_ALLCLKS);
110 if (r)
111 return r;
112
Archit Tanejac1577c12013-10-08 12:55:26 +0530113 return 0;
114}
115
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300116static void hdmi_pll_disable(struct dss_pll *dsspll)
Archit Tanejac1577c12013-10-08 12:55:26 +0530117{
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300118 struct hdmi_pll_data *pll = container_of(dsspll, struct hdmi_pll_data, pll);
Tomi Valkeinen03aafa22014-10-16 15:31:38 +0300119 struct hdmi_wp_data *wp = pll->wp;
120
Archit Tanejac1577c12013-10-08 12:55:26 +0530121 hdmi_wp_set_pll_pwr(wp, HDMI_PLLPWRCMD_ALLOFF);
Tomi Valkeinenadb5ff82014-12-31 11:26:18 +0200122
123 dss_ctrl_pll_enable(DSS_PLL_HDMI, false);
Archit Tanejac1577c12013-10-08 12:55:26 +0530124}
125
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300126static 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 Taneja2d64b1b2013-09-23 15:12:34 +0530130};
131
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300132static const struct dss_pll_hw dss_omap4_hdmi_pll_hw = {
Tomi Valkeinen06ede3d2016-05-18 10:48:44 +0300133 .type = DSS_PLL_TYPE_B,
134
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300135 .n_max = 255,
136 .m_min = 20,
137 .m_max = 4095,
138 .mX_max = 127,
139 .fint_min = 500000,
140 .fint_max = 2500000,
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300141
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 Taneja2d64b1b2013-09-23 15:12:34 +0530155};
156
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300157static const struct dss_pll_hw dss_omap5_hdmi_pll_hw = {
Tomi Valkeinen06ede3d2016-05-18 10:48:44 +0300158 .type = DSS_PLL_TYPE_B,
159
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300160 .n_max = 255,
161 .m_min = 20,
162 .m_max = 2045,
163 .mX_max = 127,
164 .fint_min = 620000,
165 .fint_max = 2500000,
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300166
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
183static int dsi_init_pll_data(struct platform_device *pdev, struct hdmi_pll_data *hpll)
Archit Taneja2d64b1b2013-09-23 15:12:34 +0530184{
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300185 struct dss_pll *pll = &hpll->pll;
186 struct clk *clk;
187 int r;
Archit Taneja2d64b1b2013-09-23 15:12:34 +0530188
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300189 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 Taneja2d64b1b2013-09-23 15:12:34 +0530193 }
194
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300195 pll->name = "hdmi";
Tomi Valkeinen64e22ff2015-01-02 10:05:33 +0200196 pll->id = DSS_PLL_HDMI;
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300197 pll->base = hpll->base;
198 pll->clkin = clk;
199
Archit Taneja2d64b1b2013-09-23 15:12:34 +0530200 switch (omapdss_get_version()) {
201 case OMAPDSS_VER_OMAP4430_ES1:
202 case OMAPDSS_VER_OMAP4430_ES2:
203 case OMAPDSS_VER_OMAP4:
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300204 pll->hw = &dss_omap4_hdmi_pll_hw;
Archit Taneja2d64b1b2013-09-23 15:12:34 +0530205 break;
206
207 case OMAPDSS_VER_OMAP5:
Tomi Valkeinenadb5ff82014-12-31 11:26:18 +0200208 case OMAPDSS_VER_DRA7xx:
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300209 pll->hw = &dss_omap5_hdmi_pll_hw;
Archit Taneja2d64b1b2013-09-23 15:12:34 +0530210 break;
211
212 default:
213 return -ENODEV;
214 }
215
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300216 pll->ops = &dsi_pll_ops;
217
218 r = dss_pll_register(pll);
219 if (r)
220 return r;
Archit Taneja2d64b1b2013-09-23 15:12:34 +0530221
222 return 0;
223}
224
Tomi Valkeinen03aafa22014-10-16 15:31:38 +0300225int hdmi_pll_init(struct platform_device *pdev, struct hdmi_pll_data *pll,
226 struct hdmi_wp_data *wp)
Archit Tanejac1577c12013-10-08 12:55:26 +0530227{
Archit Taneja2d64b1b2013-09-23 15:12:34 +0530228 int r;
Archit Tanejac1577c12013-10-08 12:55:26 +0530229 struct resource *res;
Archit Tanejac1577c12013-10-08 12:55:26 +0530230
Tomi Valkeinen03aafa22014-10-16 15:31:38 +0300231 pll->wp = wp;
232
Tomi Valkeinen77601502013-12-17 14:41:14 +0200233 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll");
Archit Tanejac1577c12013-10-08 12:55:26 +0530234 if (!res) {
Tomi Valkeinen59b3d382014-04-28 16:11:01 +0300235 DSSERR("can't get PLL mem resource\n");
236 return -EINVAL;
Archit Tanejac1577c12013-10-08 12:55:26 +0530237 }
238
Tomi Valkeinen59b3d382014-04-28 16:11:01 +0300239 pll->base = devm_ioremap_resource(&pdev->dev, res);
Tomi Valkeinen2b22df82014-05-23 14:50:09 +0300240 if (IS_ERR(pll->base)) {
Archit Tanejac1577c12013-10-08 12:55:26 +0530241 DSSERR("can't ioremap PLLCTRL\n");
Tomi Valkeinen2b22df82014-05-23 14:50:09 +0300242 return PTR_ERR(pll->base);
Archit Tanejac1577c12013-10-08 12:55:26 +0530243 }
244
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300245 r = dsi_init_pll_data(pdev, pll);
246 if (r) {
247 DSSERR("failed to init HDMI PLL\n");
248 return r;
249 }
250
Archit Tanejac1577c12013-10-08 12:55:26 +0530251 return 0;
252}
Tomi Valkeinenc84c3a52014-10-22 15:02:17 +0300253
254void hdmi_pll_uninit(struct hdmi_pll_data *hpll)
255{
256 struct dss_pll *pll = &hpll->pll;
257
258 dss_pll_unregister(pll);
259}