Loading...
1/*
2 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
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 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/of_irq.h>
20#include <linux/of_gpio.h>
21
22#include "hdmi.h"
23
24void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
25{
26 uint32_t ctrl = 0;
27 unsigned long flags;
28
29 spin_lock_irqsave(&hdmi->reg_lock, flags);
30 if (power_on) {
31 ctrl |= HDMI_CTRL_ENABLE;
32 if (!hdmi->hdmi_mode) {
33 ctrl |= HDMI_CTRL_HDMI;
34 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
35 ctrl &= ~HDMI_CTRL_HDMI;
36 } else {
37 ctrl |= HDMI_CTRL_HDMI;
38 }
39 } else {
40 ctrl = HDMI_CTRL_HDMI;
41 }
42
43 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
44 spin_unlock_irqrestore(&hdmi->reg_lock, flags);
45 DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
46 power_on ? "Enable" : "Disable", ctrl);
47}
48
49static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
50{
51 struct hdmi *hdmi = dev_id;
52
53 /* Process HPD: */
54 msm_hdmi_connector_irq(hdmi->connector);
55
56 /* Process DDC: */
57 msm_hdmi_i2c_irq(hdmi->i2c);
58
59 /* Process HDCP: */
60 if (hdmi->hdcp_ctrl)
61 msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
62
63 /* TODO audio.. */
64
65 return IRQ_HANDLED;
66}
67
68static void msm_hdmi_destroy(struct hdmi *hdmi)
69{
70 /*
71 * at this point, hpd has been disabled,
72 * after flush workq, it's safe to deinit hdcp
73 */
74 if (hdmi->workq) {
75 flush_workqueue(hdmi->workq);
76 destroy_workqueue(hdmi->workq);
77 }
78 msm_hdmi_hdcp_destroy(hdmi);
79
80 if (hdmi->phy_dev) {
81 put_device(hdmi->phy_dev);
82 hdmi->phy = NULL;
83 hdmi->phy_dev = NULL;
84 }
85
86 if (hdmi->i2c)
87 msm_hdmi_i2c_destroy(hdmi->i2c);
88
89 platform_set_drvdata(hdmi->pdev, NULL);
90}
91
92static int msm_hdmi_get_phy(struct hdmi *hdmi)
93{
94 struct platform_device *pdev = hdmi->pdev;
95 struct platform_device *phy_pdev;
96 struct device_node *phy_node;
97
98 phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
99 if (!phy_node) {
100 dev_err(&pdev->dev, "cannot find phy device\n");
101 return -ENXIO;
102 }
103
104 phy_pdev = of_find_device_by_node(phy_node);
105 if (phy_pdev)
106 hdmi->phy = platform_get_drvdata(phy_pdev);
107
108 of_node_put(phy_node);
109
110 if (!phy_pdev || !hdmi->phy) {
111 dev_err(&pdev->dev, "phy driver is not ready\n");
112 return -EPROBE_DEFER;
113 }
114
115 hdmi->phy_dev = get_device(&phy_pdev->dev);
116
117 return 0;
118}
119
120/* construct hdmi at bind/probe time, grab all the resources. If
121 * we are to EPROBE_DEFER we want to do it here, rather than later
122 * at modeset_init() time
123 */
124static struct hdmi *msm_hdmi_init(struct platform_device *pdev)
125{
126 struct hdmi_platform_config *config = pdev->dev.platform_data;
127 struct hdmi *hdmi = NULL;
128 struct resource *res;
129 int i, ret;
130
131 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
132 if (!hdmi) {
133 ret = -ENOMEM;
134 goto fail;
135 }
136
137 hdmi->pdev = pdev;
138 hdmi->config = config;
139 spin_lock_init(&hdmi->reg_lock);
140
141 hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
142 if (IS_ERR(hdmi->mmio)) {
143 ret = PTR_ERR(hdmi->mmio);
144 goto fail;
145 }
146
147 /* HDCP needs physical address of hdmi register */
148 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
149 config->mmio_name);
150 hdmi->mmio_phy_addr = res->start;
151
152 hdmi->qfprom_mmio = msm_ioremap(pdev,
153 config->qfprom_mmio_name, "HDMI_QFPROM");
154 if (IS_ERR(hdmi->qfprom_mmio)) {
155 dev_info(&pdev->dev, "can't find qfprom resource\n");
156 hdmi->qfprom_mmio = NULL;
157 }
158
159 hdmi->hpd_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_regs[0]) *
160 config->hpd_reg_cnt, GFP_KERNEL);
161 if (!hdmi->hpd_regs) {
162 ret = -ENOMEM;
163 goto fail;
164 }
165 for (i = 0; i < config->hpd_reg_cnt; i++) {
166 struct regulator *reg;
167
168 reg = devm_regulator_get(&pdev->dev,
169 config->hpd_reg_names[i]);
170 if (IS_ERR(reg)) {
171 ret = PTR_ERR(reg);
172 dev_err(&pdev->dev, "failed to get hpd regulator: %s (%d)\n",
173 config->hpd_reg_names[i], ret);
174 goto fail;
175 }
176
177 hdmi->hpd_regs[i] = reg;
178 }
179
180 hdmi->pwr_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_regs[0]) *
181 config->pwr_reg_cnt, GFP_KERNEL);
182 if (!hdmi->pwr_regs) {
183 ret = -ENOMEM;
184 goto fail;
185 }
186 for (i = 0; i < config->pwr_reg_cnt; i++) {
187 struct regulator *reg;
188
189 reg = devm_regulator_get(&pdev->dev,
190 config->pwr_reg_names[i]);
191 if (IS_ERR(reg)) {
192 ret = PTR_ERR(reg);
193 dev_err(&pdev->dev, "failed to get pwr regulator: %s (%d)\n",
194 config->pwr_reg_names[i], ret);
195 goto fail;
196 }
197
198 hdmi->pwr_regs[i] = reg;
199 }
200
201 hdmi->hpd_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_clks[0]) *
202 config->hpd_clk_cnt, GFP_KERNEL);
203 if (!hdmi->hpd_clks) {
204 ret = -ENOMEM;
205 goto fail;
206 }
207 for (i = 0; i < config->hpd_clk_cnt; i++) {
208 struct clk *clk;
209
210 clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
211 if (IS_ERR(clk)) {
212 ret = PTR_ERR(clk);
213 dev_err(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
214 config->hpd_clk_names[i], ret);
215 goto fail;
216 }
217
218 hdmi->hpd_clks[i] = clk;
219 }
220
221 hdmi->pwr_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_clks[0]) *
222 config->pwr_clk_cnt, GFP_KERNEL);
223 if (!hdmi->pwr_clks) {
224 ret = -ENOMEM;
225 goto fail;
226 }
227 for (i = 0; i < config->pwr_clk_cnt; i++) {
228 struct clk *clk;
229
230 clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
231 if (IS_ERR(clk)) {
232 ret = PTR_ERR(clk);
233 dev_err(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
234 config->pwr_clk_names[i], ret);
235 goto fail;
236 }
237
238 hdmi->pwr_clks[i] = clk;
239 }
240
241 hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
242
243 hdmi->i2c = msm_hdmi_i2c_init(hdmi);
244 if (IS_ERR(hdmi->i2c)) {
245 ret = PTR_ERR(hdmi->i2c);
246 dev_err(&pdev->dev, "failed to get i2c: %d\n", ret);
247 hdmi->i2c = NULL;
248 goto fail;
249 }
250
251 ret = msm_hdmi_get_phy(hdmi);
252 if (ret) {
253 dev_err(&pdev->dev, "failed to get phy\n");
254 goto fail;
255 }
256
257 hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
258 if (IS_ERR(hdmi->hdcp_ctrl)) {
259 dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
260 hdmi->hdcp_ctrl = NULL;
261 }
262
263 return hdmi;
264
265fail:
266 if (hdmi)
267 msm_hdmi_destroy(hdmi);
268
269 return ERR_PTR(ret);
270}
271
272/* Second part of initialization, the drm/kms level modeset_init,
273 * constructs/initializes mode objects, etc, is called from master
274 * driver (not hdmi sub-device's probe/bind!)
275 *
276 * Any resource (regulator/clk/etc) which could be missing at boot
277 * should be handled in msm_hdmi_init() so that failure happens from
278 * hdmi sub-device's probe.
279 */
280int msm_hdmi_modeset_init(struct hdmi *hdmi,
281 struct drm_device *dev, struct drm_encoder *encoder)
282{
283 struct msm_drm_private *priv = dev->dev_private;
284 struct platform_device *pdev = hdmi->pdev;
285 int ret;
286
287 hdmi->dev = dev;
288 hdmi->encoder = encoder;
289
290 hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
291
292 hdmi->bridge = msm_hdmi_bridge_init(hdmi);
293 if (IS_ERR(hdmi->bridge)) {
294 ret = PTR_ERR(hdmi->bridge);
295 dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
296 hdmi->bridge = NULL;
297 goto fail;
298 }
299
300 hdmi->connector = msm_hdmi_connector_init(hdmi);
301 if (IS_ERR(hdmi->connector)) {
302 ret = PTR_ERR(hdmi->connector);
303 dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
304 hdmi->connector = NULL;
305 goto fail;
306 }
307
308 hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
309 if (hdmi->irq < 0) {
310 ret = hdmi->irq;
311 dev_err(dev->dev, "failed to get irq: %d\n", ret);
312 goto fail;
313 }
314
315 ret = devm_request_irq(&pdev->dev, hdmi->irq,
316 msm_hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
317 "hdmi_isr", hdmi);
318 if (ret < 0) {
319 dev_err(dev->dev, "failed to request IRQ%u: %d\n",
320 hdmi->irq, ret);
321 goto fail;
322 }
323
324 encoder->bridge = hdmi->bridge;
325
326 priv->bridges[priv->num_bridges++] = hdmi->bridge;
327 priv->connectors[priv->num_connectors++] = hdmi->connector;
328
329 platform_set_drvdata(pdev, hdmi);
330
331 return 0;
332
333fail:
334 /* bridge is normally destroyed by drm: */
335 if (hdmi->bridge) {
336 msm_hdmi_bridge_destroy(hdmi->bridge);
337 hdmi->bridge = NULL;
338 }
339 if (hdmi->connector) {
340 hdmi->connector->funcs->destroy(hdmi->connector);
341 hdmi->connector = NULL;
342 }
343
344 return ret;
345}
346
347/*
348 * The hdmi device:
349 */
350
351#define HDMI_CFG(item, entry) \
352 .item ## _names = item ##_names_ ## entry, \
353 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
354
355static const char *pwr_reg_names_none[] = {};
356static const char *hpd_reg_names_none[] = {};
357
358static struct hdmi_platform_config hdmi_tx_8660_config;
359
360static const char *hpd_reg_names_8960[] = {"core-vdda", "hdmi-mux"};
361static const char *hpd_clk_names_8960[] = {"core_clk", "master_iface_clk", "slave_iface_clk"};
362
363static struct hdmi_platform_config hdmi_tx_8960_config = {
364 HDMI_CFG(hpd_reg, 8960),
365 HDMI_CFG(hpd_clk, 8960),
366};
367
368static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
369static const char *hpd_reg_names_8x74[] = {"hpd-gdsc", "hpd-5v"};
370static const char *pwr_clk_names_8x74[] = {"extp_clk", "alt_iface_clk"};
371static const char *hpd_clk_names_8x74[] = {"iface_clk", "core_clk", "mdp_core_clk"};
372static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
373
374static struct hdmi_platform_config hdmi_tx_8974_config = {
375 HDMI_CFG(pwr_reg, 8x74),
376 HDMI_CFG(hpd_reg, 8x74),
377 HDMI_CFG(pwr_clk, 8x74),
378 HDMI_CFG(hpd_clk, 8x74),
379 .hpd_freq = hpd_clk_freq_8x74,
380};
381
382static const char *hpd_reg_names_8084[] = {"hpd-gdsc", "hpd-5v", "hpd-5v-en"};
383
384static struct hdmi_platform_config hdmi_tx_8084_config = {
385 HDMI_CFG(pwr_reg, 8x74),
386 HDMI_CFG(hpd_reg, 8084),
387 HDMI_CFG(pwr_clk, 8x74),
388 HDMI_CFG(hpd_clk, 8x74),
389 .hpd_freq = hpd_clk_freq_8x74,
390};
391
392static struct hdmi_platform_config hdmi_tx_8994_config = {
393 HDMI_CFG(pwr_reg, 8x74),
394 HDMI_CFG(hpd_reg, none),
395 HDMI_CFG(pwr_clk, 8x74),
396 HDMI_CFG(hpd_clk, 8x74),
397 .hpd_freq = hpd_clk_freq_8x74,
398};
399
400static struct hdmi_platform_config hdmi_tx_8996_config = {
401 HDMI_CFG(pwr_reg, none),
402 HDMI_CFG(hpd_reg, none),
403 HDMI_CFG(pwr_clk, 8x74),
404 HDMI_CFG(hpd_clk, 8x74),
405 .hpd_freq = hpd_clk_freq_8x74,
406};
407
408static const struct {
409 const char *name;
410 const bool output;
411 const int value;
412 const char *label;
413} msm_hdmi_gpio_pdata[] = {
414 { "qcom,hdmi-tx-ddc-clk", true, 1, "HDMI_DDC_CLK" },
415 { "qcom,hdmi-tx-ddc-data", true, 1, "HDMI_DDC_DATA" },
416 { "qcom,hdmi-tx-hpd", false, 1, "HDMI_HPD" },
417 { "qcom,hdmi-tx-mux-en", true, 1, "HDMI_MUX_EN" },
418 { "qcom,hdmi-tx-mux-sel", true, 0, "HDMI_MUX_SEL" },
419 { "qcom,hdmi-tx-mux-lpm", true, 1, "HDMI_MUX_LPM" },
420};
421
422static int msm_hdmi_get_gpio(struct device_node *of_node, const char *name)
423{
424 int gpio = of_get_named_gpio(of_node, name, 0);
425 if (gpio < 0) {
426 char name2[32];
427 snprintf(name2, sizeof(name2), "%s-gpio", name);
428 gpio = of_get_named_gpio(of_node, name2, 0);
429 if (gpio < 0) {
430 DBG("failed to get gpio: %s (%d)", name, gpio);
431 gpio = -1;
432 }
433 }
434 return gpio;
435}
436
437static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
438{
439 struct drm_device *drm = dev_get_drvdata(master);
440 struct msm_drm_private *priv = drm->dev_private;
441 static struct hdmi_platform_config *hdmi_cfg;
442 struct hdmi *hdmi;
443 struct device_node *of_node = dev->of_node;
444 int i;
445
446 hdmi_cfg = (struct hdmi_platform_config *)
447 of_device_get_match_data(dev);
448 if (!hdmi_cfg) {
449 dev_err(dev, "unknown hdmi_cfg: %s\n", of_node->name);
450 return -ENXIO;
451 }
452
453 hdmi_cfg->mmio_name = "core_physical";
454 hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
455
456 for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
457 hdmi_cfg->gpios[i].num = msm_hdmi_get_gpio(of_node,
458 msm_hdmi_gpio_pdata[i].name);
459 hdmi_cfg->gpios[i].output = msm_hdmi_gpio_pdata[i].output;
460 hdmi_cfg->gpios[i].value = msm_hdmi_gpio_pdata[i].value;
461 hdmi_cfg->gpios[i].label = msm_hdmi_gpio_pdata[i].label;
462 }
463
464 dev->platform_data = hdmi_cfg;
465
466 hdmi = msm_hdmi_init(to_platform_device(dev));
467 if (IS_ERR(hdmi))
468 return PTR_ERR(hdmi);
469 priv->hdmi = hdmi;
470
471 return 0;
472}
473
474static void msm_hdmi_unbind(struct device *dev, struct device *master,
475 void *data)
476{
477 struct drm_device *drm = dev_get_drvdata(master);
478 struct msm_drm_private *priv = drm->dev_private;
479 if (priv->hdmi) {
480 msm_hdmi_destroy(priv->hdmi);
481 priv->hdmi = NULL;
482 }
483}
484
485static const struct component_ops msm_hdmi_ops = {
486 .bind = msm_hdmi_bind,
487 .unbind = msm_hdmi_unbind,
488};
489
490static int msm_hdmi_dev_probe(struct platform_device *pdev)
491{
492 return component_add(&pdev->dev, &msm_hdmi_ops);
493}
494
495static int msm_hdmi_dev_remove(struct platform_device *pdev)
496{
497 component_del(&pdev->dev, &msm_hdmi_ops);
498 return 0;
499}
500
501static const struct of_device_id msm_hdmi_dt_match[] = {
502 { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8996_config },
503 { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8994_config },
504 { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8084_config },
505 { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
506 { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
507 { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8660_config },
508 {}
509};
510
511static struct platform_driver msm_hdmi_driver = {
512 .probe = msm_hdmi_dev_probe,
513 .remove = msm_hdmi_dev_remove,
514 .driver = {
515 .name = "hdmi_msm",
516 .of_match_table = msm_hdmi_dt_match,
517 },
518};
519
520void __init msm_hdmi_register(void)
521{
522 msm_hdmi_phy_driver_register();
523 platform_driver_register(&msm_hdmi_driver);
524}
525
526void __exit msm_hdmi_unregister(void)
527{
528 platform_driver_unregister(&msm_hdmi_driver);
529 msm_hdmi_phy_driver_unregister();
530}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
5 * Author: Rob Clark <robdclark@gmail.com>
6 */
7
8#include <linux/of_irq.h>
9#include <linux/of_gpio.h>
10
11#include <sound/hdmi-codec.h>
12#include "hdmi.h"
13
14void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
15{
16 uint32_t ctrl = 0;
17 unsigned long flags;
18
19 spin_lock_irqsave(&hdmi->reg_lock, flags);
20 if (power_on) {
21 ctrl |= HDMI_CTRL_ENABLE;
22 if (!hdmi->hdmi_mode) {
23 ctrl |= HDMI_CTRL_HDMI;
24 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
25 ctrl &= ~HDMI_CTRL_HDMI;
26 } else {
27 ctrl |= HDMI_CTRL_HDMI;
28 }
29 } else {
30 ctrl = HDMI_CTRL_HDMI;
31 }
32
33 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
34 spin_unlock_irqrestore(&hdmi->reg_lock, flags);
35 DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
36 power_on ? "Enable" : "Disable", ctrl);
37}
38
39static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
40{
41 struct hdmi *hdmi = dev_id;
42
43 /* Process HPD: */
44 msm_hdmi_connector_irq(hdmi->connector);
45
46 /* Process DDC: */
47 msm_hdmi_i2c_irq(hdmi->i2c);
48
49 /* Process HDCP: */
50 if (hdmi->hdcp_ctrl)
51 msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
52
53 /* TODO audio.. */
54
55 return IRQ_HANDLED;
56}
57
58static void msm_hdmi_destroy(struct hdmi *hdmi)
59{
60 /*
61 * at this point, hpd has been disabled,
62 * after flush workq, it's safe to deinit hdcp
63 */
64 if (hdmi->workq) {
65 flush_workqueue(hdmi->workq);
66 destroy_workqueue(hdmi->workq);
67 }
68 msm_hdmi_hdcp_destroy(hdmi);
69
70 if (hdmi->phy_dev) {
71 put_device(hdmi->phy_dev);
72 hdmi->phy = NULL;
73 hdmi->phy_dev = NULL;
74 }
75
76 if (hdmi->i2c)
77 msm_hdmi_i2c_destroy(hdmi->i2c);
78
79 platform_set_drvdata(hdmi->pdev, NULL);
80}
81
82static int msm_hdmi_get_phy(struct hdmi *hdmi)
83{
84 struct platform_device *pdev = hdmi->pdev;
85 struct platform_device *phy_pdev;
86 struct device_node *phy_node;
87
88 phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
89 if (!phy_node) {
90 DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
91 return -ENXIO;
92 }
93
94 phy_pdev = of_find_device_by_node(phy_node);
95 if (phy_pdev)
96 hdmi->phy = platform_get_drvdata(phy_pdev);
97
98 of_node_put(phy_node);
99
100 if (!phy_pdev || !hdmi->phy) {
101 DRM_DEV_ERROR(&pdev->dev, "phy driver is not ready\n");
102 return -EPROBE_DEFER;
103 }
104
105 hdmi->phy_dev = get_device(&phy_pdev->dev);
106
107 return 0;
108}
109
110/* construct hdmi at bind/probe time, grab all the resources. If
111 * we are to EPROBE_DEFER we want to do it here, rather than later
112 * at modeset_init() time
113 */
114static struct hdmi *msm_hdmi_init(struct platform_device *pdev)
115{
116 struct hdmi_platform_config *config = pdev->dev.platform_data;
117 struct hdmi *hdmi = NULL;
118 struct resource *res;
119 int i, ret;
120
121 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
122 if (!hdmi) {
123 ret = -ENOMEM;
124 goto fail;
125 }
126
127 hdmi->pdev = pdev;
128 hdmi->config = config;
129 spin_lock_init(&hdmi->reg_lock);
130
131 hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
132 if (IS_ERR(hdmi->mmio)) {
133 ret = PTR_ERR(hdmi->mmio);
134 goto fail;
135 }
136
137 /* HDCP needs physical address of hdmi register */
138 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
139 config->mmio_name);
140 hdmi->mmio_phy_addr = res->start;
141
142 hdmi->qfprom_mmio = msm_ioremap(pdev,
143 config->qfprom_mmio_name, "HDMI_QFPROM");
144 if (IS_ERR(hdmi->qfprom_mmio)) {
145 DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n");
146 hdmi->qfprom_mmio = NULL;
147 }
148
149 hdmi->hpd_regs = devm_kcalloc(&pdev->dev,
150 config->hpd_reg_cnt,
151 sizeof(hdmi->hpd_regs[0]),
152 GFP_KERNEL);
153 if (!hdmi->hpd_regs) {
154 ret = -ENOMEM;
155 goto fail;
156 }
157 for (i = 0; i < config->hpd_reg_cnt; i++) {
158 struct regulator *reg;
159
160 reg = devm_regulator_get(&pdev->dev,
161 config->hpd_reg_names[i]);
162 if (IS_ERR(reg)) {
163 ret = PTR_ERR(reg);
164 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd regulator: %s (%d)\n",
165 config->hpd_reg_names[i], ret);
166 goto fail;
167 }
168
169 hdmi->hpd_regs[i] = reg;
170 }
171
172 hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
173 config->pwr_reg_cnt,
174 sizeof(hdmi->pwr_regs[0]),
175 GFP_KERNEL);
176 if (!hdmi->pwr_regs) {
177 ret = -ENOMEM;
178 goto fail;
179 }
180 for (i = 0; i < config->pwr_reg_cnt; i++) {
181 struct regulator *reg;
182
183 reg = devm_regulator_get(&pdev->dev,
184 config->pwr_reg_names[i]);
185 if (IS_ERR(reg)) {
186 ret = PTR_ERR(reg);
187 DRM_DEV_ERROR(&pdev->dev, "failed to get pwr regulator: %s (%d)\n",
188 config->pwr_reg_names[i], ret);
189 goto fail;
190 }
191
192 hdmi->pwr_regs[i] = reg;
193 }
194
195 hdmi->hpd_clks = devm_kcalloc(&pdev->dev,
196 config->hpd_clk_cnt,
197 sizeof(hdmi->hpd_clks[0]),
198 GFP_KERNEL);
199 if (!hdmi->hpd_clks) {
200 ret = -ENOMEM;
201 goto fail;
202 }
203 for (i = 0; i < config->hpd_clk_cnt; i++) {
204 struct clk *clk;
205
206 clk = msm_clk_get(pdev, config->hpd_clk_names[i]);
207 if (IS_ERR(clk)) {
208 ret = PTR_ERR(clk);
209 DRM_DEV_ERROR(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
210 config->hpd_clk_names[i], ret);
211 goto fail;
212 }
213
214 hdmi->hpd_clks[i] = clk;
215 }
216
217 hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
218 config->pwr_clk_cnt,
219 sizeof(hdmi->pwr_clks[0]),
220 GFP_KERNEL);
221 if (!hdmi->pwr_clks) {
222 ret = -ENOMEM;
223 goto fail;
224 }
225 for (i = 0; i < config->pwr_clk_cnt; i++) {
226 struct clk *clk;
227
228 clk = msm_clk_get(pdev, config->pwr_clk_names[i]);
229 if (IS_ERR(clk)) {
230 ret = PTR_ERR(clk);
231 DRM_DEV_ERROR(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
232 config->pwr_clk_names[i], ret);
233 goto fail;
234 }
235
236 hdmi->pwr_clks[i] = clk;
237 }
238
239 pm_runtime_enable(&pdev->dev);
240
241 hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
242
243 hdmi->i2c = msm_hdmi_i2c_init(hdmi);
244 if (IS_ERR(hdmi->i2c)) {
245 ret = PTR_ERR(hdmi->i2c);
246 DRM_DEV_ERROR(&pdev->dev, "failed to get i2c: %d\n", ret);
247 hdmi->i2c = NULL;
248 goto fail;
249 }
250
251 ret = msm_hdmi_get_phy(hdmi);
252 if (ret) {
253 DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n");
254 goto fail;
255 }
256
257 hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
258 if (IS_ERR(hdmi->hdcp_ctrl)) {
259 dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
260 hdmi->hdcp_ctrl = NULL;
261 }
262
263 return hdmi;
264
265fail:
266 if (hdmi)
267 msm_hdmi_destroy(hdmi);
268
269 return ERR_PTR(ret);
270}
271
272/* Second part of initialization, the drm/kms level modeset_init,
273 * constructs/initializes mode objects, etc, is called from master
274 * driver (not hdmi sub-device's probe/bind!)
275 *
276 * Any resource (regulator/clk/etc) which could be missing at boot
277 * should be handled in msm_hdmi_init() so that failure happens from
278 * hdmi sub-device's probe.
279 */
280int msm_hdmi_modeset_init(struct hdmi *hdmi,
281 struct drm_device *dev, struct drm_encoder *encoder)
282{
283 struct msm_drm_private *priv = dev->dev_private;
284 struct platform_device *pdev = hdmi->pdev;
285 int ret;
286
287 hdmi->dev = dev;
288 hdmi->encoder = encoder;
289
290 hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
291
292 hdmi->bridge = msm_hdmi_bridge_init(hdmi);
293 if (IS_ERR(hdmi->bridge)) {
294 ret = PTR_ERR(hdmi->bridge);
295 DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret);
296 hdmi->bridge = NULL;
297 goto fail;
298 }
299
300 hdmi->connector = msm_hdmi_connector_init(hdmi);
301 if (IS_ERR(hdmi->connector)) {
302 ret = PTR_ERR(hdmi->connector);
303 DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret);
304 hdmi->connector = NULL;
305 goto fail;
306 }
307
308 hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
309 if (hdmi->irq < 0) {
310 ret = hdmi->irq;
311 DRM_DEV_ERROR(dev->dev, "failed to get irq: %d\n", ret);
312 goto fail;
313 }
314
315 ret = devm_request_irq(&pdev->dev, hdmi->irq,
316 msm_hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
317 "hdmi_isr", hdmi);
318 if (ret < 0) {
319 DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n",
320 hdmi->irq, ret);
321 goto fail;
322 }
323
324 ret = msm_hdmi_hpd_enable(hdmi->connector);
325 if (ret < 0) {
326 DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
327 goto fail;
328 }
329
330 priv->bridges[priv->num_bridges++] = hdmi->bridge;
331 priv->connectors[priv->num_connectors++] = hdmi->connector;
332
333 platform_set_drvdata(pdev, hdmi);
334
335 return 0;
336
337fail:
338 /* bridge is normally destroyed by drm: */
339 if (hdmi->bridge) {
340 msm_hdmi_bridge_destroy(hdmi->bridge);
341 hdmi->bridge = NULL;
342 }
343 if (hdmi->connector) {
344 hdmi->connector->funcs->destroy(hdmi->connector);
345 hdmi->connector = NULL;
346 }
347
348 return ret;
349}
350
351/*
352 * The hdmi device:
353 */
354
355#define HDMI_CFG(item, entry) \
356 .item ## _names = item ##_names_ ## entry, \
357 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
358
359static const char *pwr_reg_names_none[] = {};
360static const char *hpd_reg_names_none[] = {};
361
362static struct hdmi_platform_config hdmi_tx_8660_config;
363
364static const char *hpd_reg_names_8960[] = {"core-vdda", "hdmi-mux"};
365static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
366
367static struct hdmi_platform_config hdmi_tx_8960_config = {
368 HDMI_CFG(hpd_reg, 8960),
369 HDMI_CFG(hpd_clk, 8960),
370};
371
372static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
373static const char *hpd_reg_names_8x74[] = {"hpd-gdsc", "hpd-5v"};
374static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"};
375static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"};
376static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
377
378static struct hdmi_platform_config hdmi_tx_8974_config = {
379 HDMI_CFG(pwr_reg, 8x74),
380 HDMI_CFG(hpd_reg, 8x74),
381 HDMI_CFG(pwr_clk, 8x74),
382 HDMI_CFG(hpd_clk, 8x74),
383 .hpd_freq = hpd_clk_freq_8x74,
384};
385
386static const char *hpd_reg_names_8084[] = {"hpd-gdsc", "hpd-5v", "hpd-5v-en"};
387
388static struct hdmi_platform_config hdmi_tx_8084_config = {
389 HDMI_CFG(pwr_reg, 8x74),
390 HDMI_CFG(hpd_reg, 8084),
391 HDMI_CFG(pwr_clk, 8x74),
392 HDMI_CFG(hpd_clk, 8x74),
393 .hpd_freq = hpd_clk_freq_8x74,
394};
395
396static struct hdmi_platform_config hdmi_tx_8994_config = {
397 HDMI_CFG(pwr_reg, 8x74),
398 HDMI_CFG(hpd_reg, none),
399 HDMI_CFG(pwr_clk, 8x74),
400 HDMI_CFG(hpd_clk, 8x74),
401 .hpd_freq = hpd_clk_freq_8x74,
402};
403
404static struct hdmi_platform_config hdmi_tx_8996_config = {
405 HDMI_CFG(pwr_reg, none),
406 HDMI_CFG(hpd_reg, none),
407 HDMI_CFG(pwr_clk, 8x74),
408 HDMI_CFG(hpd_clk, 8x74),
409 .hpd_freq = hpd_clk_freq_8x74,
410};
411
412static const struct {
413 const char *name;
414 const bool output;
415 const int value;
416 const char *label;
417} msm_hdmi_gpio_pdata[] = {
418 { "qcom,hdmi-tx-ddc-clk", true, 1, "HDMI_DDC_CLK" },
419 { "qcom,hdmi-tx-ddc-data", true, 1, "HDMI_DDC_DATA" },
420 { "qcom,hdmi-tx-hpd", false, 1, "HDMI_HPD" },
421 { "qcom,hdmi-tx-mux-en", true, 1, "HDMI_MUX_EN" },
422 { "qcom,hdmi-tx-mux-sel", true, 0, "HDMI_MUX_SEL" },
423 { "qcom,hdmi-tx-mux-lpm", true, 1, "HDMI_MUX_LPM" },
424};
425
426/*
427 * HDMI audio codec callbacks
428 */
429static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
430 struct hdmi_codec_daifmt *daifmt,
431 struct hdmi_codec_params *params)
432{
433 struct hdmi *hdmi = dev_get_drvdata(dev);
434 unsigned int chan;
435 unsigned int channel_allocation = 0;
436 unsigned int rate;
437 unsigned int level_shift = 0; /* 0dB */
438 bool down_mix = false;
439
440 DRM_DEV_DEBUG(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
441 params->sample_width, params->cea.channels);
442
443 switch (params->cea.channels) {
444 case 2:
445 /* FR and FL speakers */
446 channel_allocation = 0;
447 chan = MSM_HDMI_AUDIO_CHANNEL_2;
448 break;
449 case 4:
450 /* FC, LFE, FR and FL speakers */
451 channel_allocation = 0x3;
452 chan = MSM_HDMI_AUDIO_CHANNEL_4;
453 break;
454 case 6:
455 /* RR, RL, FC, LFE, FR and FL speakers */
456 channel_allocation = 0x0B;
457 chan = MSM_HDMI_AUDIO_CHANNEL_6;
458 break;
459 case 8:
460 /* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
461 channel_allocation = 0x1F;
462 chan = MSM_HDMI_AUDIO_CHANNEL_8;
463 break;
464 default:
465 return -EINVAL;
466 }
467
468 switch (params->sample_rate) {
469 case 32000:
470 rate = HDMI_SAMPLE_RATE_32KHZ;
471 break;
472 case 44100:
473 rate = HDMI_SAMPLE_RATE_44_1KHZ;
474 break;
475 case 48000:
476 rate = HDMI_SAMPLE_RATE_48KHZ;
477 break;
478 case 88200:
479 rate = HDMI_SAMPLE_RATE_88_2KHZ;
480 break;
481 case 96000:
482 rate = HDMI_SAMPLE_RATE_96KHZ;
483 break;
484 case 176400:
485 rate = HDMI_SAMPLE_RATE_176_4KHZ;
486 break;
487 case 192000:
488 rate = HDMI_SAMPLE_RATE_192KHZ;
489 break;
490 default:
491 DRM_DEV_ERROR(dev, "rate[%d] not supported!\n",
492 params->sample_rate);
493 return -EINVAL;
494 }
495
496 msm_hdmi_audio_set_sample_rate(hdmi, rate);
497 msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
498 level_shift, down_mix);
499
500 return 0;
501}
502
503static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
504{
505 struct hdmi *hdmi = dev_get_drvdata(dev);
506
507 msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
508}
509
510static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
511 .hw_params = msm_hdmi_audio_hw_params,
512 .audio_shutdown = msm_hdmi_audio_shutdown,
513};
514
515static struct hdmi_codec_pdata codec_data = {
516 .ops = &msm_hdmi_audio_codec_ops,
517 .max_i2s_channels = 8,
518 .i2s = 1,
519};
520
521static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
522{
523 hdmi->audio_pdev = platform_device_register_data(dev,
524 HDMI_CODEC_DRV_NAME,
525 PLATFORM_DEVID_AUTO,
526 &codec_data,
527 sizeof(codec_data));
528 return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
529}
530
531static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
532{
533 struct drm_device *drm = dev_get_drvdata(master);
534 struct msm_drm_private *priv = drm->dev_private;
535 struct hdmi_platform_config *hdmi_cfg;
536 struct hdmi *hdmi;
537 struct device_node *of_node = dev->of_node;
538 int i, err;
539
540 hdmi_cfg = (struct hdmi_platform_config *)
541 of_device_get_match_data(dev);
542 if (!hdmi_cfg) {
543 DRM_DEV_ERROR(dev, "unknown hdmi_cfg: %pOFn\n", of_node);
544 return -ENXIO;
545 }
546
547 hdmi_cfg->mmio_name = "core_physical";
548 hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
549
550 for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
551 const char *name = msm_hdmi_gpio_pdata[i].name;
552 struct gpio_desc *gpiod;
553
554 /*
555 * We are fetching the GPIO lines "as is" since the connector
556 * code is enabling and disabling the lines. Until that point
557 * the power-on default value will be kept.
558 */
559 gpiod = devm_gpiod_get_optional(dev, name, GPIOD_ASIS);
560 /* This will catch e.g. -PROBE_DEFER */
561 if (IS_ERR(gpiod))
562 return PTR_ERR(gpiod);
563 if (!gpiod) {
564 /* Try a second time, stripping down the name */
565 char name3[32];
566
567 /*
568 * Try again after stripping out the "qcom,hdmi-tx"
569 * prefix. This is mainly to match "hpd-gpios" used
570 * in the upstream bindings.
571 */
572 if (sscanf(name, "qcom,hdmi-tx-%s", name3))
573 gpiod = devm_gpiod_get_optional(dev, name3, GPIOD_ASIS);
574 if (IS_ERR(gpiod))
575 return PTR_ERR(gpiod);
576 if (!gpiod)
577 DBG("failed to get gpio: %s", name);
578 }
579 hdmi_cfg->gpios[i].gpiod = gpiod;
580 if (gpiod)
581 gpiod_set_consumer_name(gpiod, msm_hdmi_gpio_pdata[i].label);
582 hdmi_cfg->gpios[i].output = msm_hdmi_gpio_pdata[i].output;
583 hdmi_cfg->gpios[i].value = msm_hdmi_gpio_pdata[i].value;
584 }
585
586 dev->platform_data = hdmi_cfg;
587
588 hdmi = msm_hdmi_init(to_platform_device(dev));
589 if (IS_ERR(hdmi))
590 return PTR_ERR(hdmi);
591 priv->hdmi = hdmi;
592
593 err = msm_hdmi_register_audio_driver(hdmi, dev);
594 if (err) {
595 DRM_ERROR("Failed to attach an audio codec %d\n", err);
596 hdmi->audio_pdev = NULL;
597 }
598
599 return 0;
600}
601
602static void msm_hdmi_unbind(struct device *dev, struct device *master,
603 void *data)
604{
605 struct drm_device *drm = dev_get_drvdata(master);
606 struct msm_drm_private *priv = drm->dev_private;
607 if (priv->hdmi) {
608 if (priv->hdmi->audio_pdev)
609 platform_device_unregister(priv->hdmi->audio_pdev);
610
611 msm_hdmi_destroy(priv->hdmi);
612 priv->hdmi = NULL;
613 }
614}
615
616static const struct component_ops msm_hdmi_ops = {
617 .bind = msm_hdmi_bind,
618 .unbind = msm_hdmi_unbind,
619};
620
621static int msm_hdmi_dev_probe(struct platform_device *pdev)
622{
623 return component_add(&pdev->dev, &msm_hdmi_ops);
624}
625
626static int msm_hdmi_dev_remove(struct platform_device *pdev)
627{
628 component_del(&pdev->dev, &msm_hdmi_ops);
629 return 0;
630}
631
632static const struct of_device_id msm_hdmi_dt_match[] = {
633 { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8996_config },
634 { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8994_config },
635 { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8084_config },
636 { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
637 { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
638 { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8660_config },
639 {}
640};
641
642static struct platform_driver msm_hdmi_driver = {
643 .probe = msm_hdmi_dev_probe,
644 .remove = msm_hdmi_dev_remove,
645 .driver = {
646 .name = "hdmi_msm",
647 .of_match_table = msm_hdmi_dt_match,
648 },
649};
650
651void __init msm_hdmi_register(void)
652{
653 msm_hdmi_phy_driver_register();
654 platform_driver_register(&msm_hdmi_driver);
655}
656
657void __exit msm_hdmi_unregister(void)
658{
659 platform_driver_unregister(&msm_hdmi_driver);
660 msm_hdmi_phy_driver_unregister();
661}