blob: 2614cf9dafb008d602c4bd8f93a10e8bb980196b [file] [log] [blame]
Archit Taneja5cac5ae2013-10-08 13:07:00 +05301/*
2 * HDMI PHY
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
11#include <linux/kernel.h>
Archit Taneja5cac5ae2013-10-08 13:07:00 +053012#include <linux/err.h>
13#include <linux/io.h>
14#include <linux/platform_device.h>
Archit Taneja19289fd2013-09-23 12:58:52 +053015#include <linux/slab.h>
Archit Taneja5cac5ae2013-10-08 13:07:00 +053016#include <video/omapdss.h>
17
18#include "dss.h"
Archit Tanejaef269582013-09-12 17:45:57 +053019#include "hdmi.h"
Archit Taneja5cac5ae2013-10-08 13:07:00 +053020
Archit Taneja19289fd2013-09-23 12:58:52 +053021struct hdmi_phy_features {
22 bool bist_ctrl;
23 bool calc_freqout;
24 bool ldo_voltage;
25 unsigned long dcofreq_min;
26 unsigned long max_phy;
27};
28
29static const struct hdmi_phy_features *phy_feat;
30
Archit Taneja5cac5ae2013-10-08 13:07:00 +053031void hdmi_phy_dump(struct hdmi_phy_data *phy, struct seq_file *s)
32{
33#define DUMPPHY(r) seq_printf(s, "%-35s %08x\n", #r,\
34 hdmi_read_reg(phy->base, r))
35
36 DUMPPHY(HDMI_TXPHY_TX_CTRL);
37 DUMPPHY(HDMI_TXPHY_DIGITAL_CTRL);
38 DUMPPHY(HDMI_TXPHY_POWER_CTRL);
39 DUMPPHY(HDMI_TXPHY_PAD_CFG_CTRL);
Archit Taneja19289fd2013-09-23 12:58:52 +053040 if (phy_feat->bist_ctrl)
41 DUMPPHY(HDMI_TXPHY_BIST_CONTROL);
Archit Taneja5cac5ae2013-10-08 13:07:00 +053042}
43
Tomi Valkeinen2f5dc672014-04-17 12:54:02 +030044int hdmi_phy_parse_lanes(struct hdmi_phy_data *phy, const u32 *lanes)
45{
46 int i;
47
48 for (i = 0; i < 8; i += 2) {
49 u8 lane, pol;
50 int dx, dy;
51
52 dx = lanes[i];
53 dy = lanes[i + 1];
54
55 if (dx < 0 || dx >= 8)
56 return -EINVAL;
57
58 if (dy < 0 || dy >= 8)
59 return -EINVAL;
60
61 if (dx & 1) {
62 if (dy != dx - 1)
63 return -EINVAL;
64 pol = 1;
65 } else {
66 if (dy != dx + 1)
67 return -EINVAL;
68 pol = 0;
69 }
70
71 lane = dx / 2;
72
73 phy->lane_function[lane] = i / 2;
74 phy->lane_polarity[lane] = pol;
75 }
76
77 return 0;
78}
79
80static void hdmi_phy_configure_lanes(struct hdmi_phy_data *phy)
81{
82 static const u16 pad_cfg_list[] = {
83 0x0123,
84 0x0132,
85 0x0312,
86 0x0321,
87 0x0231,
88 0x0213,
89 0x1023,
90 0x1032,
91 0x3012,
92 0x3021,
93 0x2031,
94 0x2013,
95 0x1203,
96 0x1302,
97 0x3102,
98 0x3201,
99 0x2301,
100 0x2103,
101 0x1230,
102 0x1320,
103 0x3120,
104 0x3210,
105 0x2310,
106 0x2130,
107 };
108
109 u16 lane_cfg = 0;
110 int i;
111 unsigned lane_cfg_val;
112 u16 pol_val = 0;
113
114 for (i = 0; i < 4; ++i)
115 lane_cfg |= phy->lane_function[i] << ((3 - i) * 4);
116
117 pol_val |= phy->lane_polarity[0] << 0;
118 pol_val |= phy->lane_polarity[1] << 3;
119 pol_val |= phy->lane_polarity[2] << 2;
120 pol_val |= phy->lane_polarity[3] << 1;
121
122 for (i = 0; i < ARRAY_SIZE(pad_cfg_list); ++i)
123 if (pad_cfg_list[i] == lane_cfg)
124 break;
125
126 if (WARN_ON(i == ARRAY_SIZE(pad_cfg_list)))
127 i = 0;
128
129 lane_cfg_val = i;
130
131 REG_FLD_MOD(phy->base, HDMI_TXPHY_PAD_CFG_CTRL, lane_cfg_val, 26, 22);
132 REG_FLD_MOD(phy->base, HDMI_TXPHY_PAD_CFG_CTRL, pol_val, 30, 27);
133}
134
Tomi Valkeinendcf5f722013-10-28 11:47:34 +0200135int hdmi_phy_configure(struct hdmi_phy_data *phy, struct hdmi_config *cfg)
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530136{
Archit Taneja19289fd2013-09-23 12:58:52 +0530137 u8 freqout;
138
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530139 /*
140 * Read address 0 in order to get the SCP reset done completed
141 * Dummy access performed to make sure reset is done
142 */
143 hdmi_read_reg(phy->base, HDMI_TXPHY_TX_CTRL);
144
145 /*
Archit Taneja19289fd2013-09-23 12:58:52 +0530146 * In OMAP5+, the HFBITCLK must be divided by 2 before issuing the
147 * HDMI_PHYPWRCMD_LDOON command.
148 */
149 if (phy_feat->bist_ctrl)
150 REG_FLD_MOD(phy->base, HDMI_TXPHY_BIST_CONTROL, 1, 11, 11);
151
152 if (phy_feat->calc_freqout) {
153 /* DCOCLK/10 is pixel clock, compare pclk with DCOCLK_MIN/10 */
154 u32 dco_min = phy_feat->dcofreq_min / 10;
155 u32 pclk = cfg->timings.pixelclock;
156
157 if (pclk < dco_min)
158 freqout = 0;
159 else if ((pclk >= dco_min) && (pclk < phy_feat->max_phy))
160 freqout = 1;
161 else
162 freqout = 2;
163 } else {
164 freqout = 1;
165 }
166
167 /*
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530168 * Write to phy address 0 to configure the clock
169 * use HFBITCLK write HDMI_TXPHY_TX_CONTROL_FREQOUT field
170 */
Archit Taneja19289fd2013-09-23 12:58:52 +0530171 REG_FLD_MOD(phy->base, HDMI_TXPHY_TX_CTRL, freqout, 31, 30);
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530172
173 /* Write to phy address 1 to start HDMI line (TXVALID and TMDSCLKEN) */
174 hdmi_write_reg(phy->base, HDMI_TXPHY_DIGITAL_CTRL, 0xF0000000);
175
176 /* Setup max LDO voltage */
Archit Taneja19289fd2013-09-23 12:58:52 +0530177 if (phy_feat->ldo_voltage)
178 REG_FLD_MOD(phy->base, HDMI_TXPHY_POWER_CTRL, 0xB, 3, 0);
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530179
Tomi Valkeinen2f5dc672014-04-17 12:54:02 +0300180 hdmi_phy_configure_lanes(phy);
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530181
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530182 return 0;
183}
184
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530185#define PHY_OFFSET 0x300
186#define PHY_SIZE 0x100
187
Archit Taneja19289fd2013-09-23 12:58:52 +0530188static const struct hdmi_phy_features omap44xx_phy_feats = {
189 .bist_ctrl = false,
190 .calc_freqout = false,
191 .ldo_voltage = true,
192 .dcofreq_min = 500000000,
193 .max_phy = 185675000,
194};
195
196static const struct hdmi_phy_features omap54xx_phy_feats = {
197 .bist_ctrl = true,
198 .calc_freqout = true,
199 .ldo_voltage = false,
200 .dcofreq_min = 750000000,
201 .max_phy = 186000000,
202};
203
204static int hdmi_phy_init_features(struct platform_device *pdev)
205{
206 struct hdmi_phy_features *dst;
207 const struct hdmi_phy_features *src;
208
209 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
210 if (!dst) {
211 dev_err(&pdev->dev, "Failed to allocate HDMI PHY Features\n");
212 return -ENOMEM;
213 }
214
215 switch (omapdss_get_version()) {
216 case OMAPDSS_VER_OMAP4430_ES1:
217 case OMAPDSS_VER_OMAP4430_ES2:
218 case OMAPDSS_VER_OMAP4:
219 src = &omap44xx_phy_feats;
220 break;
221
222 case OMAPDSS_VER_OMAP5:
223 src = &omap54xx_phy_feats;
224 break;
225
226 default:
227 return -ENODEV;
228 }
229
230 memcpy(dst, src, sizeof(*dst));
231 phy_feat = dst;
232
233 return 0;
234}
235
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530236int hdmi_phy_init(struct platform_device *pdev, struct hdmi_phy_data *phy)
237{
Archit Taneja19289fd2013-09-23 12:58:52 +0530238 int r;
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530239 struct resource *res;
240 struct resource temp_res;
241
Archit Taneja19289fd2013-09-23 12:58:52 +0530242 r = hdmi_phy_init_features(pdev);
243 if (r)
244 return r;
245
Tomi Valkeinen77601502013-12-17 14:41:14 +0200246 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530247 if (!res) {
248 DSSDBG("can't get PHY mem resource by name\n");
249 /*
250 * if hwmod/DT doesn't have the memory resource information
251 * split into HDMI sub blocks by name, we try again by getting
252 * the platform's first resource. this code will be removed when
253 * the driver can get the mem resources by name
254 */
255 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
256 if (!res) {
257 DSSERR("can't get PHY mem resource\n");
258 return -EINVAL;
259 }
260
261 temp_res.start = res->start + PHY_OFFSET;
262 temp_res.end = temp_res.start + PHY_SIZE - 1;
263 res = &temp_res;
264 }
265
266 phy->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
267 if (!phy->base) {
268 DSSERR("can't ioremap TX PHY\n");
269 return -ENOMEM;
270 }
271
Archit Taneja5cac5ae2013-10-08 13:07:00 +0530272 return 0;
273}