blob: 7f7aadef8a8258101a63e57a14ed09b7ecc09893 [file] [log] [blame]
Rob Clarkc8afe682013-06-26 12:44:06 -04001/*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "hdmi.h"
19
Rob Clarkc8afe682013-06-26 12:44:06 -040020void hdmi_set_mode(struct hdmi *hdmi, bool power_on)
21{
22 uint32_t ctrl = 0;
23
24 if (power_on) {
25 ctrl |= HDMI_CTRL_ENABLE;
26 if (!hdmi->hdmi_mode) {
27 ctrl |= HDMI_CTRL_HDMI;
28 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
29 ctrl &= ~HDMI_CTRL_HDMI;
30 } else {
31 ctrl |= HDMI_CTRL_HDMI;
32 }
33 } else {
34 ctrl = HDMI_CTRL_HDMI;
35 }
36
37 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
38 DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
39 power_on ? "Enable" : "Disable", ctrl);
40}
41
Rob Clarkdada25b2013-12-01 12:12:54 -050042irqreturn_t hdmi_irq(int irq, void *dev_id)
Rob Clarkc8afe682013-06-26 12:44:06 -040043{
44 struct hdmi *hdmi = dev_id;
45
46 /* Process HPD: */
47 hdmi_connector_irq(hdmi->connector);
48
49 /* Process DDC: */
50 hdmi_i2c_irq(hdmi->i2c);
51
52 /* TODO audio.. */
53
54 return IRQ_HANDLED;
55}
56
Rob Clarka3376e32013-08-30 13:02:15 -040057void hdmi_destroy(struct kref *kref)
Rob Clarkc8afe682013-06-26 12:44:06 -040058{
Rob Clarka3376e32013-08-30 13:02:15 -040059 struct hdmi *hdmi = container_of(kref, struct hdmi, refcount);
Rob Clarkc8afe682013-06-26 12:44:06 -040060 struct hdmi_phy *phy = hdmi->phy;
61
62 if (phy)
63 phy->funcs->destroy(phy);
64
65 if (hdmi->i2c)
66 hdmi_i2c_destroy(hdmi->i2c);
67
Rob Clarkc0c0d9e2013-12-11 14:44:02 -050068 platform_set_drvdata(hdmi->pdev, NULL);
Rob Clarkc8afe682013-06-26 12:44:06 -040069}
70
71/* initialize connector */
Rob Clarkdada25b2013-12-01 12:12:54 -050072struct hdmi *hdmi_init(struct drm_device *dev, struct drm_encoder *encoder)
Rob Clarkc8afe682013-06-26 12:44:06 -040073{
Rob Clarka3376e32013-08-30 13:02:15 -040074 struct hdmi *hdmi = NULL;
75 struct msm_drm_private *priv = dev->dev_private;
Rob Clark060530f2014-03-03 14:19:12 -050076 struct platform_device *pdev = priv->hdmi_pdev;
Rob Clarkc8afe682013-06-26 12:44:06 -040077 struct hdmi_platform_config *config;
Rob Clarkdada25b2013-12-01 12:12:54 -050078 int i, ret;
Rob Clarkc8afe682013-06-26 12:44:06 -040079
80 if (!pdev) {
81 dev_err(dev->dev, "no hdmi device\n");
82 ret = -ENXIO;
83 goto fail;
84 }
85
86 config = pdev->dev.platform_data;
87
Rob Clarka3376e32013-08-30 13:02:15 -040088 hdmi = kzalloc(sizeof(*hdmi), GFP_KERNEL);
89 if (!hdmi) {
90 ret = -ENOMEM;
91 goto fail;
92 }
93
94 kref_init(&hdmi->refcount);
95
Rob Clarkc8afe682013-06-26 12:44:06 -040096 hdmi->dev = dev;
97 hdmi->pdev = pdev;
Rob Clarkdada25b2013-12-01 12:12:54 -050098 hdmi->config = config;
Rob Clarka3376e32013-08-30 13:02:15 -040099 hdmi->encoder = encoder;
Rob Clarkc8afe682013-06-26 12:44:06 -0400100
Rob Clarkc0c0d9e2013-12-11 14:44:02 -0500101 hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
102
Rob Clarkc8afe682013-06-26 12:44:06 -0400103 /* not sure about which phy maps to which msm.. probably I miss some */
104 if (config->phy_init)
105 hdmi->phy = config->phy_init(hdmi);
106 else
107 hdmi->phy = ERR_PTR(-ENXIO);
108
109 if (IS_ERR(hdmi->phy)) {
110 ret = PTR_ERR(hdmi->phy);
111 dev_err(dev->dev, "failed to load phy: %d\n", ret);
112 hdmi->phy = NULL;
113 goto fail;
114 }
115
Rob Clarkdada25b2013-12-01 12:12:54 -0500116 hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
Rob Clarkc8afe682013-06-26 12:44:06 -0400117 if (IS_ERR(hdmi->mmio)) {
118 ret = PTR_ERR(hdmi->mmio);
119 goto fail;
120 }
121
Rob Clarkdada25b2013-12-01 12:12:54 -0500122 BUG_ON(config->hpd_reg_cnt > ARRAY_SIZE(hdmi->hpd_regs));
123 for (i = 0; i < config->hpd_reg_cnt; i++) {
124 struct regulator *reg;
125
126 reg = devm_regulator_get(&pdev->dev, config->hpd_reg_names[i]);
127 if (IS_ERR(reg)) {
128 ret = PTR_ERR(reg);
129 dev_err(dev->dev, "failed to get hpd regulator: %s (%d)\n",
130 config->hpd_reg_names[i], ret);
131 goto fail;
132 }
133
134 hdmi->hpd_regs[i] = reg;
Rob Clarkc8afe682013-06-26 12:44:06 -0400135 }
136
Rob Clarkdada25b2013-12-01 12:12:54 -0500137 BUG_ON(config->pwr_reg_cnt > ARRAY_SIZE(hdmi->pwr_regs));
138 for (i = 0; i < config->pwr_reg_cnt; i++) {
139 struct regulator *reg;
Rob Clarkc8afe682013-06-26 12:44:06 -0400140
Rob Clarkdada25b2013-12-01 12:12:54 -0500141 reg = devm_regulator_get(&pdev->dev, config->pwr_reg_names[i]);
142 if (IS_ERR(reg)) {
143 ret = PTR_ERR(reg);
144 dev_err(dev->dev, "failed to get pwr regulator: %s (%d)\n",
145 config->pwr_reg_names[i], ret);
146 goto fail;
147 }
148
149 hdmi->pwr_regs[i] = reg;
Rob Clarkc8afe682013-06-26 12:44:06 -0400150 }
151
Rob Clarkdada25b2013-12-01 12:12:54 -0500152 BUG_ON(config->hpd_clk_cnt > ARRAY_SIZE(hdmi->hpd_clks));
153 for (i = 0; i < config->hpd_clk_cnt; i++) {
154 struct clk *clk;
155
156 clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
157 if (IS_ERR(clk)) {
158 ret = PTR_ERR(clk);
159 dev_err(dev->dev, "failed to get hpd clk: %s (%d)\n",
160 config->hpd_clk_names[i], ret);
161 goto fail;
162 }
163
164 hdmi->hpd_clks[i] = clk;
Rob Clarkc8afe682013-06-26 12:44:06 -0400165 }
166
Rob Clarkdada25b2013-12-01 12:12:54 -0500167 BUG_ON(config->pwr_clk_cnt > ARRAY_SIZE(hdmi->pwr_clks));
168 for (i = 0; i < config->pwr_clk_cnt; i++) {
169 struct clk *clk;
170
171 clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
172 if (IS_ERR(clk)) {
173 ret = PTR_ERR(clk);
174 dev_err(dev->dev, "failed to get pwr clk: %s (%d)\n",
175 config->pwr_clk_names[i], ret);
176 goto fail;
177 }
178
179 hdmi->pwr_clks[i] = clk;
Rob Clarkc8afe682013-06-26 12:44:06 -0400180 }
181
182 hdmi->i2c = hdmi_i2c_init(hdmi);
183 if (IS_ERR(hdmi->i2c)) {
184 ret = PTR_ERR(hdmi->i2c);
185 dev_err(dev->dev, "failed to get i2c: %d\n", ret);
186 hdmi->i2c = NULL;
187 goto fail;
188 }
189
Rob Clarka3376e32013-08-30 13:02:15 -0400190 hdmi->bridge = hdmi_bridge_init(hdmi);
191 if (IS_ERR(hdmi->bridge)) {
192 ret = PTR_ERR(hdmi->bridge);
193 dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
194 hdmi->bridge = NULL;
195 goto fail;
196 }
197
198 hdmi->connector = hdmi_connector_init(hdmi);
199 if (IS_ERR(hdmi->connector)) {
200 ret = PTR_ERR(hdmi->connector);
201 dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
202 hdmi->connector = NULL;
203 goto fail;
204 }
205
Rob Clarkdada25b2013-12-01 12:12:54 -0500206 if (!config->shared_irq) {
207 hdmi->irq = platform_get_irq(pdev, 0);
208 if (hdmi->irq < 0) {
209 ret = hdmi->irq;
210 dev_err(dev->dev, "failed to get irq: %d\n", ret);
211 goto fail;
212 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400213
Rob Clarkdada25b2013-12-01 12:12:54 -0500214 ret = devm_request_threaded_irq(&pdev->dev, hdmi->irq,
215 NULL, hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
216 "hdmi_isr", hdmi);
217 if (ret < 0) {
218 dev_err(dev->dev, "failed to request IRQ%u: %d\n",
219 hdmi->irq, ret);
220 goto fail;
221 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400222 }
223
Rob Clarka3376e32013-08-30 13:02:15 -0400224 encoder->bridge = hdmi->bridge;
225
226 priv->bridges[priv->num_bridges++] = hdmi->bridge;
227 priv->connectors[priv->num_connectors++] = hdmi->connector;
228
Rob Clarkc0c0d9e2013-12-11 14:44:02 -0500229 platform_set_drvdata(pdev, hdmi);
230
Rob Clarkdada25b2013-12-01 12:12:54 -0500231 return hdmi;
Rob Clarkc8afe682013-06-26 12:44:06 -0400232
233fail:
Rob Clarka3376e32013-08-30 13:02:15 -0400234 if (hdmi) {
235 /* bridge/connector are normally destroyed by drm: */
236 if (hdmi->bridge)
237 hdmi->bridge->funcs->destroy(hdmi->bridge);
238 if (hdmi->connector)
239 hdmi->connector->funcs->destroy(hdmi->connector);
240 hdmi_destroy(&hdmi->refcount);
241 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400242
Rob Clarkdada25b2013-12-01 12:12:54 -0500243 return ERR_PTR(ret);
Rob Clarkc8afe682013-06-26 12:44:06 -0400244}
245
246/*
247 * The hdmi device:
248 */
249
Rob Clarkdada25b2013-12-01 12:12:54 -0500250#include <linux/of_gpio.h>
251
Rob Clark060530f2014-03-03 14:19:12 -0500252static void set_hdmi_pdev(struct drm_device *dev,
253 struct platform_device *pdev)
254{
255 struct msm_drm_private *priv = dev->dev_private;
256 priv->hdmi_pdev = pdev;
257}
258
259static int hdmi_bind(struct device *dev, struct device *master, void *data)
Rob Clarkc8afe682013-06-26 12:44:06 -0400260{
261 static struct hdmi_platform_config config = {};
262#ifdef CONFIG_OF
Rob Clark060530f2014-03-03 14:19:12 -0500263 struct device_node *of_node = dev->of_node;
Rob Clarkdada25b2013-12-01 12:12:54 -0500264
265 int get_gpio(const char *name)
266 {
267 int gpio = of_get_named_gpio(of_node, name, 0);
268 if (gpio < 0) {
Rob Clark060530f2014-03-03 14:19:12 -0500269 dev_err(dev, "failed to get gpio: %s (%d)\n",
Rob Clarkdada25b2013-12-01 12:12:54 -0500270 name, gpio);
271 gpio = -1;
272 }
273 return gpio;
274 }
275
276 /* TODO actually use DT.. */
277 static const char *hpd_reg_names[] = {"hpd-gdsc", "hpd-5v"};
278 static const char *pwr_reg_names[] = {"core-vdda", "core-vcc"};
279 static const char *hpd_clk_names[] = {"iface_clk", "core_clk", "mdp_core_clk"};
Stephane Viaub77f47e2014-06-06 10:03:32 -0400280 static unsigned long hpd_clk_freq[] = {0, 19200000, 0};
Rob Clarkdada25b2013-12-01 12:12:54 -0500281 static const char *pwr_clk_names[] = {"extp_clk", "alt_iface_clk"};
282
283 config.phy_init = hdmi_phy_8x74_init;
284 config.mmio_name = "core_physical";
285 config.hpd_reg_names = hpd_reg_names;
286 config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
287 config.pwr_reg_names = pwr_reg_names;
288 config.pwr_reg_cnt = ARRAY_SIZE(pwr_reg_names);
289 config.hpd_clk_names = hpd_clk_names;
Stephane Viaub77f47e2014-06-06 10:03:32 -0400290 config.hpd_freq = hpd_clk_freq;
Rob Clarkdada25b2013-12-01 12:12:54 -0500291 config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
292 config.pwr_clk_names = pwr_clk_names;
293 config.pwr_clk_cnt = ARRAY_SIZE(pwr_clk_names);
294 config.ddc_clk_gpio = get_gpio("qcom,hdmi-tx-ddc-clk");
295 config.ddc_data_gpio = get_gpio("qcom,hdmi-tx-ddc-data");
296 config.hpd_gpio = get_gpio("qcom,hdmi-tx-hpd");
297 config.mux_en_gpio = get_gpio("qcom,hdmi-tx-mux-en");
298 config.mux_sel_gpio = get_gpio("qcom,hdmi-tx-mux-sel");
299 config.shared_irq = true;
300
Rob Clarkc8afe682013-06-26 12:44:06 -0400301#else
Rob Clarkdada25b2013-12-01 12:12:54 -0500302 static const char *hpd_clk_names[] = {
303 "core_clk", "master_iface_clk", "slave_iface_clk",
304 };
Rob Clarkc8afe682013-06-26 12:44:06 -0400305 if (cpu_is_apq8064()) {
Rob Clarkdada25b2013-12-01 12:12:54 -0500306 static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
Rob Clarkc8afe682013-06-26 12:44:06 -0400307 config.phy_init = hdmi_phy_8960_init;
Rob Clarkdada25b2013-12-01 12:12:54 -0500308 config.mmio_name = "hdmi_msm_hdmi_addr";
309 config.hpd_reg_names = hpd_reg_names;
310 config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
311 config.hpd_clk_names = hpd_clk_names;
312 config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
Rob Clarkc8afe682013-06-26 12:44:06 -0400313 config.ddc_clk_gpio = 70;
314 config.ddc_data_gpio = 71;
315 config.hpd_gpio = 72;
Rob Clarkdada25b2013-12-01 12:12:54 -0500316 config.mux_en_gpio = -1;
Rob Clarkc0c0d9e2013-12-11 14:44:02 -0500317 config.mux_sel_gpio = -1;
Rob Clarke529c7e2013-11-16 13:07:31 -0500318 } else if (cpu_is_msm8960() || cpu_is_msm8960ab()) {
Rob Clarkdada25b2013-12-01 12:12:54 -0500319 static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
Rob Clarkc8afe682013-06-26 12:44:06 -0400320 config.phy_init = hdmi_phy_8960_init;
Rob Clarkdada25b2013-12-01 12:12:54 -0500321 config.mmio_name = "hdmi_msm_hdmi_addr";
322 config.hpd_reg_names = hpd_reg_names;
323 config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
324 config.hpd_clk_names = hpd_clk_names;
325 config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
Rob Clarkc8afe682013-06-26 12:44:06 -0400326 config.ddc_clk_gpio = 100;
327 config.ddc_data_gpio = 101;
328 config.hpd_gpio = 102;
Rob Clarkdada25b2013-12-01 12:12:54 -0500329 config.mux_en_gpio = -1;
330 config.mux_sel_gpio = -1;
Rob Clarkc8afe682013-06-26 12:44:06 -0400331 } else if (cpu_is_msm8x60()) {
Rob Clarkdada25b2013-12-01 12:12:54 -0500332 static const char *hpd_reg_names[] = {
333 "8901_hdmi_mvs", "8901_mpp0"
334 };
Rob Clarkc8afe682013-06-26 12:44:06 -0400335 config.phy_init = hdmi_phy_8x60_init;
Rob Clarkdada25b2013-12-01 12:12:54 -0500336 config.mmio_name = "hdmi_msm_hdmi_addr";
337 config.hpd_reg_names = hpd_reg_names;
338 config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
339 config.hpd_clk_names = hpd_clk_names;
340 config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
Rob Clarkc8afe682013-06-26 12:44:06 -0400341 config.ddc_clk_gpio = 170;
342 config.ddc_data_gpio = 171;
343 config.hpd_gpio = 172;
Rob Clarkdada25b2013-12-01 12:12:54 -0500344 config.mux_en_gpio = -1;
345 config.mux_sel_gpio = -1;
Rob Clarkc8afe682013-06-26 12:44:06 -0400346 }
347#endif
Rob Clark060530f2014-03-03 14:19:12 -0500348 dev->platform_data = &config;
349 set_hdmi_pdev(dev_get_drvdata(master), to_platform_device(dev));
Rob Clarkc8afe682013-06-26 12:44:06 -0400350 return 0;
351}
352
Rob Clark060530f2014-03-03 14:19:12 -0500353static void hdmi_unbind(struct device *dev, struct device *master,
354 void *data)
355{
356 set_hdmi_pdev(dev_get_drvdata(master), NULL);
357}
358
359static const struct component_ops hdmi_ops = {
360 .bind = hdmi_bind,
361 .unbind = hdmi_unbind,
362};
363
364static int hdmi_dev_probe(struct platform_device *pdev)
365{
366 return component_add(&pdev->dev, &hdmi_ops);
367}
368
Rob Clarkc8afe682013-06-26 12:44:06 -0400369static int hdmi_dev_remove(struct platform_device *pdev)
370{
Rob Clark060530f2014-03-03 14:19:12 -0500371 component_del(&pdev->dev, &hdmi_ops);
Rob Clarkc8afe682013-06-26 12:44:06 -0400372 return 0;
373}
374
Rob Clarkdada25b2013-12-01 12:12:54 -0500375static const struct of_device_id dt_match[] = {
376 { .compatible = "qcom,hdmi-tx" },
377 {}
378};
Rob Clarkdada25b2013-12-01 12:12:54 -0500379
Rob Clarkc8afe682013-06-26 12:44:06 -0400380static struct platform_driver hdmi_driver = {
381 .probe = hdmi_dev_probe,
382 .remove = hdmi_dev_remove,
Rob Clarkdada25b2013-12-01 12:12:54 -0500383 .driver = {
384 .name = "hdmi_msm",
385 .of_match_table = dt_match,
386 },
Rob Clarkc8afe682013-06-26 12:44:06 -0400387};
388
389void __init hdmi_register(void)
390{
391 platform_driver_register(&hdmi_driver);
392}
393
394void __exit hdmi_unregister(void)
395{
396 platform_driver_unregister(&hdmi_driver);
397}