xref: /openbmc/linux/sound/soc/sof/intel/hda-bus.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
278ad1f07SKeyon Jie //
378ad1f07SKeyon Jie // This file is provided under a dual BSD/GPLv2 license.  When using or
478ad1f07SKeyon Jie // redistributing this file, you may do so under either license.
578ad1f07SKeyon Jie //
678ad1f07SKeyon Jie // Copyright(c) 2018 Intel Corporation. All rights reserved.
778ad1f07SKeyon Jie //
878ad1f07SKeyon Jie // Authors: Keyon Jie <yang.jie@linux.intel.com>
978ad1f07SKeyon Jie 
1078ad1f07SKeyon Jie #include <linux/io.h>
1178ad1f07SKeyon Jie #include <sound/hdaudio.h>
12c99fafdfSKai Vehmanen #include <sound/hda_i915.h>
13fd572393SKai Vehmanen #include <sound/hda_codec.h>
14fd572393SKai Vehmanen #include <sound/hda_register.h>
1578ad1f07SKeyon Jie #include "../sof-priv.h"
1678ad1f07SKeyon Jie #include "hda.h"
1778ad1f07SKeyon Jie 
18d4ff1b39STakashi Iwai #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
1940073a9dSYueHaibing #include "../../codecs/hdac_hda.h"
20d4ff1b39STakashi Iwai #define sof_hda_ext_ops	snd_soc_hdac_hda_get_ops()
2178ad1f07SKeyon Jie 
update_codec_wake_enable(struct hdac_bus * bus,unsigned int addr,bool link_power)22fd572393SKai Vehmanen static void update_codec_wake_enable(struct hdac_bus *bus, unsigned int addr, bool link_power)
23fd572393SKai Vehmanen {
24fd572393SKai Vehmanen 	unsigned int mask = snd_hdac_chip_readw(bus, WAKEEN);
25fd572393SKai Vehmanen 
26fd572393SKai Vehmanen 	if (link_power)
27fd572393SKai Vehmanen 		mask &= ~BIT(addr);
28fd572393SKai Vehmanen 	else
29fd572393SKai Vehmanen 		mask |= BIT(addr);
30fd572393SKai Vehmanen 
31fd572393SKai Vehmanen 	snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, mask);
32fd572393SKai Vehmanen }
33fd572393SKai Vehmanen 
sof_hda_bus_link_power(struct hdac_device * codec,bool enable)34c99fafdfSKai Vehmanen static void sof_hda_bus_link_power(struct hdac_device *codec, bool enable)
35c99fafdfSKai Vehmanen {
36c99fafdfSKai Vehmanen 	struct hdac_bus *bus = codec->bus;
37c99fafdfSKai Vehmanen 	bool oldstate = test_bit(codec->addr, &bus->codec_powered);
38c99fafdfSKai Vehmanen 
39c99fafdfSKai Vehmanen 	snd_hdac_ext_bus_link_power(codec, enable);
40c99fafdfSKai Vehmanen 
41c99fafdfSKai Vehmanen 	if (enable == oldstate)
42c99fafdfSKai Vehmanen 		return;
43c99fafdfSKai Vehmanen 
44c99fafdfSKai Vehmanen 	/*
45c99fafdfSKai Vehmanen 	 * Both codec driver and controller can hold references to
46c99fafdfSKai Vehmanen 	 * display power. To avoid unnecessary power-up/down cycles,
47c99fafdfSKai Vehmanen 	 * controller doesn't immediately release its reference.
48c99fafdfSKai Vehmanen 	 *
49c99fafdfSKai Vehmanen 	 * If the codec driver powers down the link, release
50c99fafdfSKai Vehmanen 	 * the controller reference as well.
51c99fafdfSKai Vehmanen 	 */
52c99fafdfSKai Vehmanen 	if (codec->addr == HDA_IDISP_ADDR && !enable)
53c99fafdfSKai Vehmanen 		snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
54fd572393SKai Vehmanen 
55fd572393SKai Vehmanen 	/* WAKEEN needs to be set for disabled links */
56fd572393SKai Vehmanen 	update_codec_wake_enable(bus, codec->addr, enable);
57c99fafdfSKai Vehmanen }
58c99fafdfSKai Vehmanen 
5987fc20e4SKai Vehmanen static const struct hdac_bus_ops bus_core_ops = {
6087fc20e4SKai Vehmanen 	.command = snd_hdac_bus_send_cmd,
6187fc20e4SKai Vehmanen 	.get_response = snd_hdac_bus_get_response,
62c99fafdfSKai Vehmanen 	.link_power = sof_hda_bus_link_power,
6387fc20e4SKai Vehmanen };
6487fc20e4SKai Vehmanen #endif
6587fc20e4SKai Vehmanen 
6678ad1f07SKeyon Jie /*
6778ad1f07SKeyon Jie  * This can be used for both with/without hda link support.
6878ad1f07SKeyon Jie  */
sof_hda_bus_init(struct snd_sof_dev * sdev,struct device * dev)69afae0942SPierre-Louis Bossart void sof_hda_bus_init(struct snd_sof_dev *sdev, struct device *dev)
7078ad1f07SKeyon Jie {
71afae0942SPierre-Louis Bossart 	struct hdac_bus *bus = sof_to_bus(sdev);
72afae0942SPierre-Louis Bossart 
73*fb0bdfe2SPierre-Louis Bossart #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_LINK)
74afae0942SPierre-Louis Bossart #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
7587fc20e4SKai Vehmanen 	snd_hdac_ext_bus_init(bus, dev, &bus_core_ops, sof_hda_ext_ops);
76*fb0bdfe2SPierre-Louis Bossart #else
77*fb0bdfe2SPierre-Louis Bossart 	snd_hdac_ext_bus_init(bus, dev, NULL, NULL);
78*fb0bdfe2SPierre-Louis Bossart #endif
79*fb0bdfe2SPierre-Louis Bossart #else
80*fb0bdfe2SPierre-Louis Bossart 
8178ad1f07SKeyon Jie 	memset(bus, 0, sizeof(*bus));
8278ad1f07SKeyon Jie 	bus->dev = dev;
8378ad1f07SKeyon Jie 
8478ad1f07SKeyon Jie 	INIT_LIST_HEAD(&bus->stream_list);
8578ad1f07SKeyon Jie 
8678ad1f07SKeyon Jie 	bus->irq = -1;
87d1a6459fSZhu Yingjiang 
88d1a6459fSZhu Yingjiang 	/*
89d1a6459fSZhu Yingjiang 	 * There is only one HDA bus atm. keep the index as 0.
90d1a6459fSZhu Yingjiang 	 * Need to fix when there are more than one HDA bus.
91d1a6459fSZhu Yingjiang 	 */
92d1a6459fSZhu Yingjiang 	bus->idx = 0;
9378ad1f07SKeyon Jie 
9478ad1f07SKeyon Jie 	spin_lock_init(&bus->reg_lock);
95*fb0bdfe2SPierre-Louis Bossart #endif /* CONFIG_SND_SOC_SOF_HDA_LINK */
96afae0942SPierre-Louis Bossart }
97afae0942SPierre-Louis Bossart 
sof_hda_bus_exit(struct snd_sof_dev * sdev)98afae0942SPierre-Louis Bossart void sof_hda_bus_exit(struct snd_sof_dev *sdev)
99afae0942SPierre-Louis Bossart {
100*fb0bdfe2SPierre-Louis Bossart #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_LINK)
101afae0942SPierre-Louis Bossart 	struct hdac_bus *bus = sof_to_bus(sdev);
102afae0942SPierre-Louis Bossart 
103afae0942SPierre-Louis Bossart 	snd_hdac_ext_bus_exit(bus);
104afae0942SPierre-Louis Bossart #endif
10578ad1f07SKeyon Jie }
106