mmc: host: sh_mobile_sdhi: don't populate unneeded functions
[linux/fpc-iii.git] / drivers / gpu / drm / msm / hdmi / hdmi.c
blob973720792236fe4dfd68327137dbc2202501058e
1 /*
2 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
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.
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.
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/>.
19 #include <linux/of_irq.h>
20 #include <linux/of_gpio.h>
22 #include <sound/hdmi-codec.h>
23 #include "hdmi.h"
25 void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
27 uint32_t ctrl = 0;
28 unsigned long flags;
30 spin_lock_irqsave(&hdmi->reg_lock, flags);
31 if (power_on) {
32 ctrl |= HDMI_CTRL_ENABLE;
33 if (!hdmi->hdmi_mode) {
34 ctrl |= HDMI_CTRL_HDMI;
35 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
36 ctrl &= ~HDMI_CTRL_HDMI;
37 } else {
38 ctrl |= HDMI_CTRL_HDMI;
40 } else {
41 ctrl = HDMI_CTRL_HDMI;
44 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
45 spin_unlock_irqrestore(&hdmi->reg_lock, flags);
46 DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
47 power_on ? "Enable" : "Disable", ctrl);
50 static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
52 struct hdmi *hdmi = dev_id;
54 /* Process HPD: */
55 msm_hdmi_connector_irq(hdmi->connector);
57 /* Process DDC: */
58 msm_hdmi_i2c_irq(hdmi->i2c);
60 /* Process HDCP: */
61 if (hdmi->hdcp_ctrl)
62 msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
64 /* TODO audio.. */
66 return IRQ_HANDLED;
69 static void msm_hdmi_destroy(struct hdmi *hdmi)
72 * at this point, hpd has been disabled,
73 * after flush workq, it's safe to deinit hdcp
75 if (hdmi->workq) {
76 flush_workqueue(hdmi->workq);
77 destroy_workqueue(hdmi->workq);
79 msm_hdmi_hdcp_destroy(hdmi);
81 if (hdmi->phy_dev) {
82 put_device(hdmi->phy_dev);
83 hdmi->phy = NULL;
84 hdmi->phy_dev = NULL;
87 if (hdmi->i2c)
88 msm_hdmi_i2c_destroy(hdmi->i2c);
90 platform_set_drvdata(hdmi->pdev, NULL);
93 static int msm_hdmi_get_phy(struct hdmi *hdmi)
95 struct platform_device *pdev = hdmi->pdev;
96 struct platform_device *phy_pdev;
97 struct device_node *phy_node;
99 phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
100 if (!phy_node) {
101 dev_err(&pdev->dev, "cannot find phy device\n");
102 return -ENXIO;
105 phy_pdev = of_find_device_by_node(phy_node);
106 if (phy_pdev)
107 hdmi->phy = platform_get_drvdata(phy_pdev);
109 of_node_put(phy_node);
111 if (!phy_pdev || !hdmi->phy) {
112 dev_err(&pdev->dev, "phy driver is not ready\n");
113 return -EPROBE_DEFER;
116 hdmi->phy_dev = get_device(&phy_pdev->dev);
118 return 0;
121 /* construct hdmi at bind/probe time, grab all the resources. If
122 * we are to EPROBE_DEFER we want to do it here, rather than later
123 * at modeset_init() time
125 static struct hdmi *msm_hdmi_init(struct platform_device *pdev)
127 struct hdmi_platform_config *config = pdev->dev.platform_data;
128 struct hdmi *hdmi = NULL;
129 struct resource *res;
130 int i, ret;
132 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
133 if (!hdmi) {
134 ret = -ENOMEM;
135 goto fail;
138 hdmi->pdev = pdev;
139 hdmi->config = config;
140 spin_lock_init(&hdmi->reg_lock);
142 hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
143 if (IS_ERR(hdmi->mmio)) {
144 ret = PTR_ERR(hdmi->mmio);
145 goto fail;
148 /* HDCP needs physical address of hdmi register */
149 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
150 config->mmio_name);
151 hdmi->mmio_phy_addr = res->start;
153 hdmi->qfprom_mmio = msm_ioremap(pdev,
154 config->qfprom_mmio_name, "HDMI_QFPROM");
155 if (IS_ERR(hdmi->qfprom_mmio)) {
156 dev_info(&pdev->dev, "can't find qfprom resource\n");
157 hdmi->qfprom_mmio = NULL;
160 hdmi->hpd_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_regs[0]) *
161 config->hpd_reg_cnt, GFP_KERNEL);
162 if (!hdmi->hpd_regs) {
163 ret = -ENOMEM;
164 goto fail;
166 for (i = 0; i < config->hpd_reg_cnt; i++) {
167 struct regulator *reg;
169 reg = devm_regulator_get(&pdev->dev,
170 config->hpd_reg_names[i]);
171 if (IS_ERR(reg)) {
172 ret = PTR_ERR(reg);
173 dev_err(&pdev->dev, "failed to get hpd regulator: %s (%d)\n",
174 config->hpd_reg_names[i], ret);
175 goto fail;
178 hdmi->hpd_regs[i] = reg;
181 hdmi->pwr_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_regs[0]) *
182 config->pwr_reg_cnt, GFP_KERNEL);
183 if (!hdmi->pwr_regs) {
184 ret = -ENOMEM;
185 goto fail;
187 for (i = 0; i < config->pwr_reg_cnt; i++) {
188 struct regulator *reg;
190 reg = devm_regulator_get(&pdev->dev,
191 config->pwr_reg_names[i]);
192 if (IS_ERR(reg)) {
193 ret = PTR_ERR(reg);
194 dev_err(&pdev->dev, "failed to get pwr regulator: %s (%d)\n",
195 config->pwr_reg_names[i], ret);
196 goto fail;
199 hdmi->pwr_regs[i] = reg;
202 hdmi->hpd_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_clks[0]) *
203 config->hpd_clk_cnt, GFP_KERNEL);
204 if (!hdmi->hpd_clks) {
205 ret = -ENOMEM;
206 goto fail;
208 for (i = 0; i < config->hpd_clk_cnt; i++) {
209 struct clk *clk;
211 clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
212 if (IS_ERR(clk)) {
213 ret = PTR_ERR(clk);
214 dev_err(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
215 config->hpd_clk_names[i], ret);
216 goto fail;
219 hdmi->hpd_clks[i] = clk;
222 hdmi->pwr_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_clks[0]) *
223 config->pwr_clk_cnt, GFP_KERNEL);
224 if (!hdmi->pwr_clks) {
225 ret = -ENOMEM;
226 goto fail;
228 for (i = 0; i < config->pwr_clk_cnt; i++) {
229 struct clk *clk;
231 clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
232 if (IS_ERR(clk)) {
233 ret = PTR_ERR(clk);
234 dev_err(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
235 config->pwr_clk_names[i], ret);
236 goto fail;
239 hdmi->pwr_clks[i] = clk;
242 hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
244 hdmi->i2c = msm_hdmi_i2c_init(hdmi);
245 if (IS_ERR(hdmi->i2c)) {
246 ret = PTR_ERR(hdmi->i2c);
247 dev_err(&pdev->dev, "failed to get i2c: %d\n", ret);
248 hdmi->i2c = NULL;
249 goto fail;
252 ret = msm_hdmi_get_phy(hdmi);
253 if (ret) {
254 dev_err(&pdev->dev, "failed to get phy\n");
255 goto fail;
258 hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
259 if (IS_ERR(hdmi->hdcp_ctrl)) {
260 dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
261 hdmi->hdcp_ctrl = NULL;
264 return hdmi;
266 fail:
267 if (hdmi)
268 msm_hdmi_destroy(hdmi);
270 return ERR_PTR(ret);
273 /* Second part of initialization, the drm/kms level modeset_init,
274 * constructs/initializes mode objects, etc, is called from master
275 * driver (not hdmi sub-device's probe/bind!)
277 * Any resource (regulator/clk/etc) which could be missing at boot
278 * should be handled in msm_hdmi_init() so that failure happens from
279 * hdmi sub-device's probe.
281 int msm_hdmi_modeset_init(struct hdmi *hdmi,
282 struct drm_device *dev, struct drm_encoder *encoder)
284 struct msm_drm_private *priv = dev->dev_private;
285 struct platform_device *pdev = hdmi->pdev;
286 int ret;
288 hdmi->dev = dev;
289 hdmi->encoder = encoder;
291 hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
293 hdmi->bridge = msm_hdmi_bridge_init(hdmi);
294 if (IS_ERR(hdmi->bridge)) {
295 ret = PTR_ERR(hdmi->bridge);
296 dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
297 hdmi->bridge = NULL;
298 goto fail;
301 hdmi->connector = msm_hdmi_connector_init(hdmi);
302 if (IS_ERR(hdmi->connector)) {
303 ret = PTR_ERR(hdmi->connector);
304 dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
305 hdmi->connector = NULL;
306 goto fail;
309 hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
310 if (hdmi->irq < 0) {
311 ret = hdmi->irq;
312 dev_err(dev->dev, "failed to get irq: %d\n", ret);
313 goto fail;
316 ret = devm_request_irq(&pdev->dev, hdmi->irq,
317 msm_hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
318 "hdmi_isr", hdmi);
319 if (ret < 0) {
320 dev_err(dev->dev, "failed to request IRQ%u: %d\n",
321 hdmi->irq, ret);
322 goto fail;
325 encoder->bridge = hdmi->bridge;
327 priv->bridges[priv->num_bridges++] = hdmi->bridge;
328 priv->connectors[priv->num_connectors++] = hdmi->connector;
330 platform_set_drvdata(pdev, hdmi);
332 return 0;
334 fail:
335 /* bridge is normally destroyed by drm: */
336 if (hdmi->bridge) {
337 msm_hdmi_bridge_destroy(hdmi->bridge);
338 hdmi->bridge = NULL;
340 if (hdmi->connector) {
341 hdmi->connector->funcs->destroy(hdmi->connector);
342 hdmi->connector = NULL;
345 return ret;
349 * The hdmi device:
352 #define HDMI_CFG(item, entry) \
353 .item ## _names = item ##_names_ ## entry, \
354 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
356 static const char *pwr_reg_names_none[] = {};
357 static const char *hpd_reg_names_none[] = {};
359 static struct hdmi_platform_config hdmi_tx_8660_config;
361 static const char *hpd_reg_names_8960[] = {"core-vdda", "hdmi-mux"};
362 static const char *hpd_clk_names_8960[] = {"core_clk", "master_iface_clk", "slave_iface_clk"};
364 static struct hdmi_platform_config hdmi_tx_8960_config = {
365 HDMI_CFG(hpd_reg, 8960),
366 HDMI_CFG(hpd_clk, 8960),
369 static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
370 static const char *hpd_reg_names_8x74[] = {"hpd-gdsc", "hpd-5v"};
371 static const char *pwr_clk_names_8x74[] = {"extp_clk", "alt_iface_clk"};
372 static const char *hpd_clk_names_8x74[] = {"iface_clk", "core_clk", "mdp_core_clk"};
373 static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
375 static struct hdmi_platform_config hdmi_tx_8974_config = {
376 HDMI_CFG(pwr_reg, 8x74),
377 HDMI_CFG(hpd_reg, 8x74),
378 HDMI_CFG(pwr_clk, 8x74),
379 HDMI_CFG(hpd_clk, 8x74),
380 .hpd_freq = hpd_clk_freq_8x74,
383 static const char *hpd_reg_names_8084[] = {"hpd-gdsc", "hpd-5v", "hpd-5v-en"};
385 static struct hdmi_platform_config hdmi_tx_8084_config = {
386 HDMI_CFG(pwr_reg, 8x74),
387 HDMI_CFG(hpd_reg, 8084),
388 HDMI_CFG(pwr_clk, 8x74),
389 HDMI_CFG(hpd_clk, 8x74),
390 .hpd_freq = hpd_clk_freq_8x74,
393 static struct hdmi_platform_config hdmi_tx_8994_config = {
394 HDMI_CFG(pwr_reg, 8x74),
395 HDMI_CFG(hpd_reg, none),
396 HDMI_CFG(pwr_clk, 8x74),
397 HDMI_CFG(hpd_clk, 8x74),
398 .hpd_freq = hpd_clk_freq_8x74,
401 static struct hdmi_platform_config hdmi_tx_8996_config = {
402 HDMI_CFG(pwr_reg, none),
403 HDMI_CFG(hpd_reg, none),
404 HDMI_CFG(pwr_clk, 8x74),
405 HDMI_CFG(hpd_clk, 8x74),
406 .hpd_freq = hpd_clk_freq_8x74,
409 static const struct {
410 const char *name;
411 const bool output;
412 const int value;
413 const char *label;
414 } msm_hdmi_gpio_pdata[] = {
415 { "qcom,hdmi-tx-ddc-clk", true, 1, "HDMI_DDC_CLK" },
416 { "qcom,hdmi-tx-ddc-data", true, 1, "HDMI_DDC_DATA" },
417 { "qcom,hdmi-tx-hpd", false, 1, "HDMI_HPD" },
418 { "qcom,hdmi-tx-mux-en", true, 1, "HDMI_MUX_EN" },
419 { "qcom,hdmi-tx-mux-sel", true, 0, "HDMI_MUX_SEL" },
420 { "qcom,hdmi-tx-mux-lpm", true, 1, "HDMI_MUX_LPM" },
423 static int msm_hdmi_get_gpio(struct device_node *of_node, const char *name)
425 int gpio = of_get_named_gpio(of_node, name, 0);
426 if (gpio < 0) {
427 char name2[32];
428 snprintf(name2, sizeof(name2), "%s-gpio", name);
429 gpio = of_get_named_gpio(of_node, name2, 0);
430 if (gpio < 0) {
431 DBG("failed to get gpio: %s (%d)", name, gpio);
432 gpio = -1;
435 return gpio;
439 * HDMI audio codec callbacks
441 static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
442 struct hdmi_codec_daifmt *daifmt,
443 struct hdmi_codec_params *params)
445 struct hdmi *hdmi = dev_get_drvdata(dev);
446 unsigned int chan;
447 unsigned int channel_allocation = 0;
448 unsigned int rate;
449 unsigned int level_shift = 0; /* 0dB */
450 bool down_mix = false;
452 dev_dbg(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
453 params->sample_width, params->cea.channels);
455 switch (params->cea.channels) {
456 case 2:
457 /* FR and FL speakers */
458 channel_allocation = 0;
459 chan = MSM_HDMI_AUDIO_CHANNEL_2;
460 break;
461 case 4:
462 /* FC, LFE, FR and FL speakers */
463 channel_allocation = 0x3;
464 chan = MSM_HDMI_AUDIO_CHANNEL_4;
465 break;
466 case 6:
467 /* RR, RL, FC, LFE, FR and FL speakers */
468 channel_allocation = 0x0B;
469 chan = MSM_HDMI_AUDIO_CHANNEL_6;
470 break;
471 case 8:
472 /* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
473 channel_allocation = 0x1F;
474 chan = MSM_HDMI_AUDIO_CHANNEL_8;
475 break;
476 default:
477 return -EINVAL;
480 switch (params->sample_rate) {
481 case 32000:
482 rate = HDMI_SAMPLE_RATE_32KHZ;
483 break;
484 case 44100:
485 rate = HDMI_SAMPLE_RATE_44_1KHZ;
486 break;
487 case 48000:
488 rate = HDMI_SAMPLE_RATE_48KHZ;
489 break;
490 case 88200:
491 rate = HDMI_SAMPLE_RATE_88_2KHZ;
492 break;
493 case 96000:
494 rate = HDMI_SAMPLE_RATE_96KHZ;
495 break;
496 case 176400:
497 rate = HDMI_SAMPLE_RATE_176_4KHZ;
498 break;
499 case 192000:
500 rate = HDMI_SAMPLE_RATE_192KHZ;
501 break;
502 default:
503 dev_err(dev, "rate[%d] not supported!\n",
504 params->sample_rate);
505 return -EINVAL;
508 msm_hdmi_audio_set_sample_rate(hdmi, rate);
509 msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
510 level_shift, down_mix);
512 return 0;
515 static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
517 struct hdmi *hdmi = dev_get_drvdata(dev);
519 msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
522 static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
523 .hw_params = msm_hdmi_audio_hw_params,
524 .audio_shutdown = msm_hdmi_audio_shutdown,
527 static struct hdmi_codec_pdata codec_data = {
528 .ops = &msm_hdmi_audio_codec_ops,
529 .max_i2s_channels = 8,
530 .i2s = 1,
533 static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
535 hdmi->audio_pdev = platform_device_register_data(dev,
536 HDMI_CODEC_DRV_NAME,
537 PLATFORM_DEVID_AUTO,
538 &codec_data,
539 sizeof(codec_data));
540 return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
543 static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
545 struct drm_device *drm = dev_get_drvdata(master);
546 struct msm_drm_private *priv = drm->dev_private;
547 static struct hdmi_platform_config *hdmi_cfg;
548 struct hdmi *hdmi;
549 struct device_node *of_node = dev->of_node;
550 int i, err;
552 hdmi_cfg = (struct hdmi_platform_config *)
553 of_device_get_match_data(dev);
554 if (!hdmi_cfg) {
555 dev_err(dev, "unknown hdmi_cfg: %s\n", of_node->name);
556 return -ENXIO;
559 hdmi_cfg->mmio_name = "core_physical";
560 hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
562 for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
563 hdmi_cfg->gpios[i].num = msm_hdmi_get_gpio(of_node,
564 msm_hdmi_gpio_pdata[i].name);
565 hdmi_cfg->gpios[i].output = msm_hdmi_gpio_pdata[i].output;
566 hdmi_cfg->gpios[i].value = msm_hdmi_gpio_pdata[i].value;
567 hdmi_cfg->gpios[i].label = msm_hdmi_gpio_pdata[i].label;
570 dev->platform_data = hdmi_cfg;
572 hdmi = msm_hdmi_init(to_platform_device(dev));
573 if (IS_ERR(hdmi))
574 return PTR_ERR(hdmi);
575 priv->hdmi = hdmi;
577 err = msm_hdmi_register_audio_driver(hdmi, dev);
578 if (err) {
579 DRM_ERROR("Failed to attach an audio codec %d\n", err);
580 hdmi->audio_pdev = NULL;
583 return 0;
586 static void msm_hdmi_unbind(struct device *dev, struct device *master,
587 void *data)
589 struct drm_device *drm = dev_get_drvdata(master);
590 struct msm_drm_private *priv = drm->dev_private;
591 if (priv->hdmi) {
592 if (priv->hdmi->audio_pdev)
593 platform_device_unregister(priv->hdmi->audio_pdev);
595 msm_hdmi_destroy(priv->hdmi);
596 priv->hdmi = NULL;
600 static const struct component_ops msm_hdmi_ops = {
601 .bind = msm_hdmi_bind,
602 .unbind = msm_hdmi_unbind,
605 static int msm_hdmi_dev_probe(struct platform_device *pdev)
607 return component_add(&pdev->dev, &msm_hdmi_ops);
610 static int msm_hdmi_dev_remove(struct platform_device *pdev)
612 component_del(&pdev->dev, &msm_hdmi_ops);
613 return 0;
616 static const struct of_device_id msm_hdmi_dt_match[] = {
617 { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8996_config },
618 { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8994_config },
619 { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8084_config },
620 { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
621 { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
622 { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8660_config },
626 static struct platform_driver msm_hdmi_driver = {
627 .probe = msm_hdmi_dev_probe,
628 .remove = msm_hdmi_dev_remove,
629 .driver = {
630 .name = "hdmi_msm",
631 .of_match_table = msm_hdmi_dt_match,
635 void __init msm_hdmi_register(void)
637 msm_hdmi_phy_driver_register();
638 platform_driver_register(&msm_hdmi_driver);
641 void __exit msm_hdmi_unregister(void)
643 platform_driver_unregister(&msm_hdmi_driver);
644 msm_hdmi_phy_driver_unregister();