blob: a968cad509c2368b0699b184d1af1e58516dc84e [file] [log] [blame]
Rob Clarkc8afe682013-06-26 12:44:06 -04001/*
Stephane Viau5eba5d82015-01-07 16:27:27 -05002 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
Rob Clarkc8afe682013-06-26 12:44:06 -04003 * 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
Rob Clarkf6a8eac2014-11-17 15:28:07 -050019#include <linux/of_irq.h>
Archit Taneja1fd6a442015-10-30 12:35:55 +053020#include <linux/of_gpio.h>
21
Srinivas Kandagatlaf1427012016-06-10 10:45:56 +010022#include <sound/hdmi-codec.h>
Rob Clarkc8afe682013-06-26 12:44:06 -040023#include "hdmi.h"
24
Arnd Bergmannfcda50c2016-02-22 22:08:35 +010025void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
Rob Clarkc8afe682013-06-26 12:44:06 -040026{
27 uint32_t ctrl = 0;
jilai wangc6a57a52015-04-02 17:49:01 -040028 unsigned long flags;
Rob Clarkc8afe682013-06-26 12:44:06 -040029
jilai wangc6a57a52015-04-02 17:49:01 -040030 spin_lock_irqsave(&hdmi->reg_lock, flags);
Rob Clarkc8afe682013-06-26 12:44:06 -040031 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;
39 }
40 } else {
41 ctrl = HDMI_CTRL_HDMI;
42 }
43
44 hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
jilai wangc6a57a52015-04-02 17:49:01 -040045 spin_unlock_irqrestore(&hdmi->reg_lock, flags);
Rob Clarkc8afe682013-06-26 12:44:06 -040046 DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
47 power_on ? "Enable" : "Disable", ctrl);
48}
49
Arnd Bergmannfcda50c2016-02-22 22:08:35 +010050static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
Rob Clarkc8afe682013-06-26 12:44:06 -040051{
52 struct hdmi *hdmi = dev_id;
53
54 /* Process HPD: */
Arnd Bergmannfcda50c2016-02-22 22:08:35 +010055 msm_hdmi_connector_irq(hdmi->connector);
Rob Clarkc8afe682013-06-26 12:44:06 -040056
57 /* Process DDC: */
Arnd Bergmannfcda50c2016-02-22 22:08:35 +010058 msm_hdmi_i2c_irq(hdmi->i2c);
Rob Clarkc8afe682013-06-26 12:44:06 -040059
jilai wangc6a57a52015-04-02 17:49:01 -040060 /* Process HDCP: */
61 if (hdmi->hdcp_ctrl)
Arnd Bergmannfcda50c2016-02-22 22:08:35 +010062 msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
jilai wangc6a57a52015-04-02 17:49:01 -040063
Rob Clarkc8afe682013-06-26 12:44:06 -040064 /* TODO audio.. */
65
66 return IRQ_HANDLED;
67}
68
Arnd Bergmannfcda50c2016-02-22 22:08:35 +010069static void msm_hdmi_destroy(struct hdmi *hdmi)
Rob Clarkc8afe682013-06-26 12:44:06 -040070{
jilai wangc6a57a52015-04-02 17:49:01 -040071 /*
72 * at this point, hpd has been disabled,
73 * after flush workq, it's safe to deinit hdcp
74 */
75 if (hdmi->workq) {
76 flush_workqueue(hdmi->workq);
77 destroy_workqueue(hdmi->workq);
78 }
Arnd Bergmannfcda50c2016-02-22 22:08:35 +010079 msm_hdmi_hdcp_destroy(hdmi);
Rob Clarkc8afe682013-06-26 12:44:06 -040080
Archit Tanejae00012b2016-02-25 11:22:40 +053081 if (hdmi->phy_dev) {
82 put_device(hdmi->phy_dev);
83 hdmi->phy = NULL;
84 hdmi->phy_dev = NULL;
85 }
86
Rob Clarkc8afe682013-06-26 12:44:06 -040087 if (hdmi->i2c)
Arnd Bergmannfcda50c2016-02-22 22:08:35 +010088 msm_hdmi_i2c_destroy(hdmi->i2c);
Rob Clarkc8afe682013-06-26 12:44:06 -040089
Rob Clarkc0c0d9e2013-12-11 14:44:02 -050090 platform_set_drvdata(hdmi->pdev, NULL);
Rob Clarkc8afe682013-06-26 12:44:06 -040091}
92
Arnd Bergmannfcda50c2016-02-22 22:08:35 +010093static int msm_hdmi_get_phy(struct hdmi *hdmi)
Archit Tanejae00012b2016-02-25 11:22:40 +053094{
95 struct platform_device *pdev = hdmi->pdev;
96 struct platform_device *phy_pdev;
97 struct device_node *phy_node;
98
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;
103 }
104
105 phy_pdev = of_find_device_by_node(phy_node);
106 if (phy_pdev)
107 hdmi->phy = platform_get_drvdata(phy_pdev);
108
109 of_node_put(phy_node);
110
111 if (!phy_pdev || !hdmi->phy) {
112 dev_err(&pdev->dev, "phy driver is not ready\n");
113 return -EPROBE_DEFER;
114 }
115
116 hdmi->phy_dev = get_device(&phy_pdev->dev);
117
118 return 0;
119}
120
Rob Clark067fef32014-11-04 13:33:14 -0500121/* 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
124 */
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100125static struct hdmi *msm_hdmi_init(struct platform_device *pdev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400126{
Rob Clark067fef32014-11-04 13:33:14 -0500127 struct hdmi_platform_config *config = pdev->dev.platform_data;
Rob Clarka3376e32013-08-30 13:02:15 -0400128 struct hdmi *hdmi = NULL;
jilai wangc6a57a52015-04-02 17:49:01 -0400129 struct resource *res;
Rob Clarkdada25b2013-12-01 12:12:54 -0500130 int i, ret;
Rob Clarkc8afe682013-06-26 12:44:06 -0400131
Rob Clark067fef32014-11-04 13:33:14 -0500132 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
Rob Clarka3376e32013-08-30 13:02:15 -0400133 if (!hdmi) {
134 ret = -ENOMEM;
135 goto fail;
136 }
137
Rob Clarkc8afe682013-06-26 12:44:06 -0400138 hdmi->pdev = pdev;
Rob Clarkdada25b2013-12-01 12:12:54 -0500139 hdmi->config = config;
jilai wangc6a57a52015-04-02 17:49:01 -0400140 spin_lock_init(&hdmi->reg_lock);
Rob Clarkc0c0d9e2013-12-11 14:44:02 -0500141
Rob Clarkdada25b2013-12-01 12:12:54 -0500142 hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
Rob Clarkc8afe682013-06-26 12:44:06 -0400143 if (IS_ERR(hdmi->mmio)) {
144 ret = PTR_ERR(hdmi->mmio);
145 goto fail;
146 }
147
jilai wangc6a57a52015-04-02 17:49:01 -0400148 /* 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;
152
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;
158 }
159
Stephane Viau447fa522015-01-13 14:33:40 -0500160 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;
165 }
Rob Clarkdada25b2013-12-01 12:12:54 -0500166 for (i = 0; i < config->hpd_reg_cnt; i++) {
167 struct regulator *reg;
168
Rob Clark3e875992014-08-01 13:08:11 -0400169 reg = devm_regulator_get(&pdev->dev,
Rob Clark41e69772013-12-15 16:23:05 -0500170 config->hpd_reg_names[i]);
Rob Clarkdada25b2013-12-01 12:12:54 -0500171 if (IS_ERR(reg)) {
172 ret = PTR_ERR(reg);
Rob Clark067fef32014-11-04 13:33:14 -0500173 dev_err(&pdev->dev, "failed to get hpd regulator: %s (%d)\n",
Rob Clarkdada25b2013-12-01 12:12:54 -0500174 config->hpd_reg_names[i], ret);
175 goto fail;
176 }
177
178 hdmi->hpd_regs[i] = reg;
Rob Clarkc8afe682013-06-26 12:44:06 -0400179 }
180
Stephane Viau447fa522015-01-13 14:33:40 -0500181 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;
186 }
Rob Clarkdada25b2013-12-01 12:12:54 -0500187 for (i = 0; i < config->pwr_reg_cnt; i++) {
188 struct regulator *reg;
Rob Clarkc8afe682013-06-26 12:44:06 -0400189
Rob Clark3e875992014-08-01 13:08:11 -0400190 reg = devm_regulator_get(&pdev->dev,
Rob Clark41e69772013-12-15 16:23:05 -0500191 config->pwr_reg_names[i]);
Rob Clarkdada25b2013-12-01 12:12:54 -0500192 if (IS_ERR(reg)) {
193 ret = PTR_ERR(reg);
Rob Clark067fef32014-11-04 13:33:14 -0500194 dev_err(&pdev->dev, "failed to get pwr regulator: %s (%d)\n",
Rob Clarkdada25b2013-12-01 12:12:54 -0500195 config->pwr_reg_names[i], ret);
196 goto fail;
197 }
198
199 hdmi->pwr_regs[i] = reg;
Rob Clarkc8afe682013-06-26 12:44:06 -0400200 }
201
Stephane Viau447fa522015-01-13 14:33:40 -0500202 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;
207 }
Rob Clarkdada25b2013-12-01 12:12:54 -0500208 for (i = 0; i < config->hpd_clk_cnt; i++) {
209 struct clk *clk;
210
211 clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
212 if (IS_ERR(clk)) {
213 ret = PTR_ERR(clk);
Rob Clark067fef32014-11-04 13:33:14 -0500214 dev_err(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
Rob Clarkdada25b2013-12-01 12:12:54 -0500215 config->hpd_clk_names[i], ret);
216 goto fail;
217 }
218
219 hdmi->hpd_clks[i] = clk;
Rob Clarkc8afe682013-06-26 12:44:06 -0400220 }
221
Stephane Viau447fa522015-01-13 14:33:40 -0500222 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;
227 }
Rob Clarkdada25b2013-12-01 12:12:54 -0500228 for (i = 0; i < config->pwr_clk_cnt; i++) {
229 struct clk *clk;
230
231 clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
232 if (IS_ERR(clk)) {
233 ret = PTR_ERR(clk);
Rob Clark067fef32014-11-04 13:33:14 -0500234 dev_err(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
Rob Clarkdada25b2013-12-01 12:12:54 -0500235 config->pwr_clk_names[i], ret);
236 goto fail;
237 }
238
239 hdmi->pwr_clks[i] = clk;
Rob Clarkc8afe682013-06-26 12:44:06 -0400240 }
241
jilai wangc6a57a52015-04-02 17:49:01 -0400242 hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
243
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100244 hdmi->i2c = msm_hdmi_i2c_init(hdmi);
Rob Clarkc8afe682013-06-26 12:44:06 -0400245 if (IS_ERR(hdmi->i2c)) {
246 ret = PTR_ERR(hdmi->i2c);
Rob Clark067fef32014-11-04 13:33:14 -0500247 dev_err(&pdev->dev, "failed to get i2c: %d\n", ret);
Rob Clarkc8afe682013-06-26 12:44:06 -0400248 hdmi->i2c = NULL;
249 goto fail;
250 }
251
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100252 ret = msm_hdmi_get_phy(hdmi);
Archit Tanejae00012b2016-02-25 11:22:40 +0530253 if (ret) {
254 dev_err(&pdev->dev, "failed to get phy\n");
255 goto fail;
256 }
257
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100258 hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
jilai wangc6a57a52015-04-02 17:49:01 -0400259 if (IS_ERR(hdmi->hdcp_ctrl)) {
260 dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
261 hdmi->hdcp_ctrl = NULL;
262 }
263
Rob Clark067fef32014-11-04 13:33:14 -0500264 return hdmi;
265
266fail:
267 if (hdmi)
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100268 msm_hdmi_destroy(hdmi);
Rob Clark067fef32014-11-04 13:33:14 -0500269
270 return ERR_PTR(ret);
271}
272
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!)
276 *
277 * Any resource (regulator/clk/etc) which could be missing at boot
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100278 * should be handled in msm_hdmi_init() so that failure happens from
Rob Clark067fef32014-11-04 13:33:14 -0500279 * hdmi sub-device's probe.
280 */
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100281int msm_hdmi_modeset_init(struct hdmi *hdmi,
Rob Clark067fef32014-11-04 13:33:14 -0500282 struct drm_device *dev, struct drm_encoder *encoder)
283{
284 struct msm_drm_private *priv = dev->dev_private;
285 struct platform_device *pdev = hdmi->pdev;
Rob Clark067fef32014-11-04 13:33:14 -0500286 int ret;
287
288 hdmi->dev = dev;
289 hdmi->encoder = encoder;
290
291 hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
292
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100293 hdmi->bridge = msm_hdmi_bridge_init(hdmi);
Rob Clarka3376e32013-08-30 13:02:15 -0400294 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;
299 }
300
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100301 hdmi->connector = msm_hdmi_connector_init(hdmi);
Rob Clarka3376e32013-08-30 13:02:15 -0400302 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;
307 }
308
Rob Clarkf6a8eac2014-11-17 15:28:07 -0500309 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;
314 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400315
Rob Clarkf6a8eac2014-11-17 15:28:07 -0500316 ret = devm_request_irq(&pdev->dev, hdmi->irq,
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100317 msm_hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
Rob Clarkf6a8eac2014-11-17 15:28:07 -0500318 "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;
Rob Clarkc8afe682013-06-26 12:44:06 -0400323 }
324
Rob Clarka3376e32013-08-30 13:02:15 -0400325 encoder->bridge = hdmi->bridge;
326
327 priv->bridges[priv->num_bridges++] = hdmi->bridge;
328 priv->connectors[priv->num_connectors++] = hdmi->connector;
329
Rob Clarkc0c0d9e2013-12-11 14:44:02 -0500330 platform_set_drvdata(pdev, hdmi);
331
Rob Clark067fef32014-11-04 13:33:14 -0500332 return 0;
Rob Clarkc8afe682013-06-26 12:44:06 -0400333
334fail:
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530335 /* bridge is normally destroyed by drm: */
Rob Clark067fef32014-11-04 13:33:14 -0500336 if (hdmi->bridge) {
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100337 msm_hdmi_bridge_destroy(hdmi->bridge);
Rob Clark067fef32014-11-04 13:33:14 -0500338 hdmi->bridge = NULL;
339 }
340 if (hdmi->connector) {
341 hdmi->connector->funcs->destroy(hdmi->connector);
342 hdmi->connector = NULL;
Rob Clarka3376e32013-08-30 13:02:15 -0400343 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400344
Rob Clark067fef32014-11-04 13:33:14 -0500345 return ret;
Rob Clarkc8afe682013-06-26 12:44:06 -0400346}
347
348/*
349 * The hdmi device:
350 */
351
Stephane Viau5eba5d82015-01-07 16:27:27 -0500352#define HDMI_CFG(item, entry) \
353 .item ## _names = item ##_names_ ## entry, \
354 .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
355
Stephane Viau0afbe592015-09-15 08:41:49 -0400356static const char *pwr_reg_names_none[] = {};
357static const char *hpd_reg_names_none[] = {};
358
Archit Tanejaba3d7bf2016-02-25 11:22:41 +0530359static struct hdmi_platform_config hdmi_tx_8660_config;
Stephane Viau5eba5d82015-01-07 16:27:27 -0500360
361static const char *hpd_reg_names_8960[] = {"core-vdda", "hdmi-mux"};
362static const char *hpd_clk_names_8960[] = {"core_clk", "master_iface_clk", "slave_iface_clk"};
363
364static struct hdmi_platform_config hdmi_tx_8960_config = {
Stephane Viau5eba5d82015-01-07 16:27:27 -0500365 HDMI_CFG(hpd_reg, 8960),
366 HDMI_CFG(hpd_clk, 8960),
367};
368
369static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
370static const char *hpd_reg_names_8x74[] = {"hpd-gdsc", "hpd-5v"};
371static const char *pwr_clk_names_8x74[] = {"extp_clk", "alt_iface_clk"};
372static const char *hpd_clk_names_8x74[] = {"iface_clk", "core_clk", "mdp_core_clk"};
373static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
374
Rob Clark5cf3a452015-07-27 20:52:50 -0400375static struct hdmi_platform_config hdmi_tx_8974_config = {
Stephane Viau5eba5d82015-01-07 16:27:27 -0500376 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,
381};
382
383static const char *hpd_reg_names_8084[] = {"hpd-gdsc", "hpd-5v", "hpd-5v-en"};
384
385static struct hdmi_platform_config hdmi_tx_8084_config = {
Stephane Viau5eba5d82015-01-07 16:27:27 -0500386 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,
391};
392
Rob Clark5cf3a452015-07-27 20:52:50 -0400393static struct hdmi_platform_config hdmi_tx_8994_config = {
Stephane Viau3a84f842015-06-19 16:04:47 -0400394 HDMI_CFG(pwr_reg, 8x74),
Stephane Viau0afbe592015-09-15 08:41:49 -0400395 HDMI_CFG(hpd_reg, none),
396 HDMI_CFG(pwr_clk, 8x74),
397 HDMI_CFG(hpd_clk, 8x74),
398 .hpd_freq = hpd_clk_freq_8x74,
399};
400
401static struct hdmi_platform_config hdmi_tx_8996_config = {
Stephane Viau0afbe592015-09-15 08:41:49 -0400402 HDMI_CFG(pwr_reg, none),
403 HDMI_CFG(hpd_reg, none),
Stephane Viau3a84f842015-06-19 16:04:47 -0400404 HDMI_CFG(pwr_clk, 8x74),
405 HDMI_CFG(hpd_clk, 8x74),
406 .hpd_freq = hpd_clk_freq_8x74,
407};
408
Archit Tanejadc50f7822016-02-25 11:22:36 +0530409static const struct {
410 const char *name;
411 const bool output;
412 const int value;
413 const char *label;
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100414} msm_hdmi_gpio_pdata[] = {
Archit Tanejadc50f7822016-02-25 11:22:36 +0530415 { "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" },
421};
422
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100423static int msm_hdmi_get_gpio(struct device_node *of_node, const char *name)
Mark Charleboisfc886102014-08-29 11:05:50 -0700424{
Archit Taneja5f6f5e02016-09-13 20:51:35 +0530425 int gpio;
426
427 /* try with the gpio names as in the table (downstream bindings) */
428 gpio = of_get_named_gpio(of_node, name, 0);
Mark Charleboisfc886102014-08-29 11:05:50 -0700429 if (gpio < 0) {
430 char name2[32];
Archit Taneja5f6f5e02016-09-13 20:51:35 +0530431
432 /* try with the gpio names as in the upstream bindings */
433 snprintf(name2, sizeof(name2), "%s-gpios", name);
Mark Charleboisfc886102014-08-29 11:05:50 -0700434 gpio = of_get_named_gpio(of_node, name2, 0);
435 if (gpio < 0) {
Archit Taneja5f6f5e02016-09-13 20:51:35 +0530436 char name3[32];
437
438 /*
439 * try again after stripping out the "qcom,hdmi-tx"
440 * prefix. This is mainly to match "hpd-gpios" used
441 * in the upstream bindings
442 */
443 if (sscanf(name2, "qcom,hdmi-tx-%s", name3))
444 gpio = of_get_named_gpio(of_node, name3, 0);
445 }
446
447 if (gpio < 0) {
Stephane Viau3a84f842015-06-19 16:04:47 -0400448 DBG("failed to get gpio: %s (%d)", name, gpio);
Mark Charleboisfc886102014-08-29 11:05:50 -0700449 gpio = -1;
450 }
451 }
452 return gpio;
453}
Mark Charleboisfc886102014-08-29 11:05:50 -0700454
Srinivas Kandagatlaf1427012016-06-10 10:45:56 +0100455/*
456 * HDMI audio codec callbacks
457 */
458static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
459 struct hdmi_codec_daifmt *daifmt,
460 struct hdmi_codec_params *params)
461{
462 struct hdmi *hdmi = dev_get_drvdata(dev);
463 unsigned int chan;
464 unsigned int channel_allocation = 0;
465 unsigned int rate;
466 unsigned int level_shift = 0; /* 0dB */
467 bool down_mix = false;
468
469 dev_dbg(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
470 params->sample_width, params->cea.channels);
471
472 switch (params->cea.channels) {
473 case 2:
474 /* FR and FL speakers */
475 channel_allocation = 0;
476 chan = MSM_HDMI_AUDIO_CHANNEL_2;
477 break;
478 case 4:
479 /* FC, LFE, FR and FL speakers */
480 channel_allocation = 0x3;
481 chan = MSM_HDMI_AUDIO_CHANNEL_4;
482 break;
483 case 6:
484 /* RR, RL, FC, LFE, FR and FL speakers */
485 channel_allocation = 0x0B;
486 chan = MSM_HDMI_AUDIO_CHANNEL_6;
487 break;
488 case 8:
489 /* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
490 channel_allocation = 0x1F;
491 chan = MSM_HDMI_AUDIO_CHANNEL_8;
492 break;
493 default:
494 return -EINVAL;
495 }
496
497 switch (params->sample_rate) {
498 case 32000:
499 rate = HDMI_SAMPLE_RATE_32KHZ;
500 break;
501 case 44100:
502 rate = HDMI_SAMPLE_RATE_44_1KHZ;
503 break;
504 case 48000:
505 rate = HDMI_SAMPLE_RATE_48KHZ;
506 break;
507 case 88200:
508 rate = HDMI_SAMPLE_RATE_88_2KHZ;
509 break;
510 case 96000:
511 rate = HDMI_SAMPLE_RATE_96KHZ;
512 break;
513 case 176400:
514 rate = HDMI_SAMPLE_RATE_176_4KHZ;
515 break;
516 case 192000:
517 rate = HDMI_SAMPLE_RATE_192KHZ;
518 break;
519 default:
520 dev_err(dev, "rate[%d] not supported!\n",
521 params->sample_rate);
522 return -EINVAL;
523 }
524
525 msm_hdmi_audio_set_sample_rate(hdmi, rate);
526 msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
527 level_shift, down_mix);
528
529 return 0;
530}
531
532static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
533{
534 struct hdmi *hdmi = dev_get_drvdata(dev);
535
536 msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
537}
538
539static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
540 .hw_params = msm_hdmi_audio_hw_params,
541 .audio_shutdown = msm_hdmi_audio_shutdown,
542};
543
544static struct hdmi_codec_pdata codec_data = {
545 .ops = &msm_hdmi_audio_codec_ops,
546 .max_i2s_channels = 8,
547 .i2s = 1,
548};
549
550static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
551{
552 hdmi->audio_pdev = platform_device_register_data(dev,
553 HDMI_CODEC_DRV_NAME,
554 PLATFORM_DEVID_AUTO,
555 &codec_data,
556 sizeof(codec_data));
Wei Yongjun06f32172016-07-12 11:06:52 +0000557 return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
Srinivas Kandagatlaf1427012016-06-10 10:45:56 +0100558}
559
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100560static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
Rob Clarkc8afe682013-06-26 12:44:06 -0400561{
Rob Clarkd1a717b2014-11-18 08:40:44 -0500562 struct drm_device *drm = dev_get_drvdata(master);
563 struct msm_drm_private *priv = drm->dev_private;
Stephane Viau5eba5d82015-01-07 16:27:27 -0500564 static struct hdmi_platform_config *hdmi_cfg;
Rob Clark067fef32014-11-04 13:33:14 -0500565 struct hdmi *hdmi;
Rob Clark060530f2014-03-03 14:19:12 -0500566 struct device_node *of_node = dev->of_node;
Srinivas Kandagatlaf1427012016-06-10 10:45:56 +0100567 int i, err;
Rob Clarkdada25b2013-12-01 12:12:54 -0500568
Archit Taneja1fd6a442015-10-30 12:35:55 +0530569 hdmi_cfg = (struct hdmi_platform_config *)
570 of_device_get_match_data(dev);
571 if (!hdmi_cfg) {
572 dev_err(dev, "unknown hdmi_cfg: %s\n", of_node->name);
Stephane Viau5eba5d82015-01-07 16:27:27 -0500573 return -ENXIO;
Rob Clark41e69772013-12-15 16:23:05 -0500574 }
Rob Clarkdada25b2013-12-01 12:12:54 -0500575
Stephane Viau5eba5d82015-01-07 16:27:27 -0500576 hdmi_cfg->mmio_name = "core_physical";
jilai wangc6a57a52015-04-02 17:49:01 -0400577 hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
Archit Tanejadc50f7822016-02-25 11:22:36 +0530578
579 for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100580 hdmi_cfg->gpios[i].num = msm_hdmi_get_gpio(of_node,
581 msm_hdmi_gpio_pdata[i].name);
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 hdmi_cfg->gpios[i].label = msm_hdmi_gpio_pdata[i].label;
Archit Tanejadc50f7822016-02-25 11:22:36 +0530585 }
Rob Clarkdada25b2013-12-01 12:12:54 -0500586
Stephane Viau5eba5d82015-01-07 16:27:27 -0500587 dev->platform_data = hdmi_cfg;
588
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100589 hdmi = msm_hdmi_init(to_platform_device(dev));
Rob Clark067fef32014-11-04 13:33:14 -0500590 if (IS_ERR(hdmi))
591 return PTR_ERR(hdmi);
Rob Clarkd1a717b2014-11-18 08:40:44 -0500592 priv->hdmi = hdmi;
Stephane Viau5eba5d82015-01-07 16:27:27 -0500593
Srinivas Kandagatlaf1427012016-06-10 10:45:56 +0100594 err = msm_hdmi_register_audio_driver(hdmi, dev);
595 if (err) {
596 DRM_ERROR("Failed to attach an audio codec %d\n", err);
597 hdmi->audio_pdev = NULL;
598 }
599
Rob Clarkc8afe682013-06-26 12:44:06 -0400600 return 0;
601}
602
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100603static void msm_hdmi_unbind(struct device *dev, struct device *master,
Rob Clark060530f2014-03-03 14:19:12 -0500604 void *data)
605{
Rob Clarkd1a717b2014-11-18 08:40:44 -0500606 struct drm_device *drm = dev_get_drvdata(master);
607 struct msm_drm_private *priv = drm->dev_private;
608 if (priv->hdmi) {
Srinivas Kandagatlaf1427012016-06-10 10:45:56 +0100609 if (priv->hdmi->audio_pdev)
610 platform_device_unregister(priv->hdmi->audio_pdev);
611
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100612 msm_hdmi_destroy(priv->hdmi);
Rob Clarkd1a717b2014-11-18 08:40:44 -0500613 priv->hdmi = NULL;
614 }
Rob Clark060530f2014-03-03 14:19:12 -0500615}
616
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100617static const struct component_ops msm_hdmi_ops = {
618 .bind = msm_hdmi_bind,
619 .unbind = msm_hdmi_unbind,
Rob Clark060530f2014-03-03 14:19:12 -0500620};
621
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100622static int msm_hdmi_dev_probe(struct platform_device *pdev)
Rob Clark060530f2014-03-03 14:19:12 -0500623{
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100624 return component_add(&pdev->dev, &msm_hdmi_ops);
Rob Clark060530f2014-03-03 14:19:12 -0500625}
626
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100627static int msm_hdmi_dev_remove(struct platform_device *pdev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400628{
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100629 component_del(&pdev->dev, &msm_hdmi_ops);
Rob Clarkc8afe682013-06-26 12:44:06 -0400630 return 0;
631}
632
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100633static const struct of_device_id msm_hdmi_dt_match[] = {
Archit Taneja1fd6a442015-10-30 12:35:55 +0530634 { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8996_config },
635 { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8994_config },
636 { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8084_config },
637 { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
638 { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
639 { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8660_config },
640 {}
641};
642
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100643static struct platform_driver msm_hdmi_driver = {
644 .probe = msm_hdmi_dev_probe,
645 .remove = msm_hdmi_dev_remove,
Rob Clarkdada25b2013-12-01 12:12:54 -0500646 .driver = {
647 .name = "hdmi_msm",
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100648 .of_match_table = msm_hdmi_dt_match,
Rob Clarkdada25b2013-12-01 12:12:54 -0500649 },
Rob Clarkc8afe682013-06-26 12:44:06 -0400650};
651
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100652void __init msm_hdmi_register(void)
Rob Clarkc8afe682013-06-26 12:44:06 -0400653{
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100654 msm_hdmi_phy_driver_register();
655 platform_driver_register(&msm_hdmi_driver);
Rob Clarkc8afe682013-06-26 12:44:06 -0400656}
657
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100658void __exit msm_hdmi_unregister(void)
Rob Clarkc8afe682013-06-26 12:44:06 -0400659{
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100660 platform_driver_unregister(&msm_hdmi_driver);
661 msm_hdmi_phy_driver_unregister();
Rob Clarkc8afe682013-06-26 12:44:06 -0400662}