2 * hdac-ext-controller.c - HD-audio extended controller functions.
4 * Copyright (C) 2014-2015 Intel Corp
5 * Author: Jeeja KP <jeeja.kp@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <sound/hda_register.h>
23 #include <sound/hdaudio_ext.h>
26 * maximum HDAC capablities we should parse to avoid endless looping:
27 * currently we have 4 extended caps, so this is future proof for now.
28 * extend when this limit is seen meeting in real HW
30 #define HDAC_MAX_CAPS 10
33 * processing pipe helpers - these helpers are useful for dealing with HDA
34 * new capability of processing pipelines
38 * snd_hdac_ext_bus_ppcap_enable - enable/disable processing pipe capability
39 * @ebus: HD-audio extended core bus
40 * @enable: flag to turn on/off the capability
42 void snd_hdac_ext_bus_ppcap_enable(struct hdac_bus *bus, bool enable)
46 dev_err(bus->dev, "Address of PP capability is NULL");
51 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, 0, AZX_PPCTL_GPROCEN);
53 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, AZX_PPCTL_GPROCEN, 0);
55 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_enable);
58 * snd_hdac_ext_bus_ppcap_int_enable - ppcap interrupt enable/disable
59 * @ebus: HD-audio extended core bus
60 * @enable: flag to enable/disable interrupt
62 void snd_hdac_ext_bus_ppcap_int_enable(struct hdac_bus *bus, bool enable)
66 dev_err(bus->dev, "Address of PP capability is NULL\n");
71 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, 0, AZX_PPCTL_PIE);
73 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, AZX_PPCTL_PIE, 0);
75 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_int_enable);
78 * Multilink helpers - these helpers are useful for dealing with HDA
79 * new multilink capability
83 * snd_hdac_ext_bus_get_ml_capabilities - get multilink capability
84 * @ebus: HD-audio extended core bus
86 * This will parse all links and read the mlink capabilities and add them
87 * in hlink_list of extended hdac bus
88 * Note: this will be freed on bus exit by driver
90 int snd_hdac_ext_bus_get_ml_capabilities(struct hdac_bus *bus)
94 struct hdac_ext_link *hlink;
96 link_count = readl(bus->mlcap + AZX_REG_ML_MLCD) + 1;
98 dev_dbg(bus->dev, "In %s Link count: %d\n", __func__, link_count);
100 for (idx = 0; idx < link_count; idx++) {
101 hlink = kzalloc(sizeof(*hlink), GFP_KERNEL);
106 hlink->ml_addr = bus->mlcap + AZX_ML_BASE +
107 (AZX_ML_INTERVAL * idx);
108 hlink->lcaps = readl(hlink->ml_addr + AZX_REG_ML_LCAP);
109 hlink->lsdiid = readw(hlink->ml_addr + AZX_REG_ML_LSDIID);
111 /* since link in On, update the ref */
112 hlink->ref_count = 1;
114 list_add_tail(&hlink->list, &bus->hlink_list);
119 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
122 * snd_hdac_link_free_all- free hdac extended link objects
124 * @ebus: HD-audio ext core bus
127 void snd_hdac_link_free_all(struct hdac_bus *bus)
129 struct hdac_ext_link *l;
131 while (!list_empty(&bus->hlink_list)) {
132 l = list_first_entry(&bus->hlink_list, struct hdac_ext_link, list);
137 EXPORT_SYMBOL_GPL(snd_hdac_link_free_all);
140 * snd_hdac_ext_bus_get_link_index - get link based on codec name
141 * @ebus: HD-audio extended core bus
142 * @codec_name: codec name
144 struct hdac_ext_link *snd_hdac_ext_bus_get_link(struct hdac_bus *bus,
145 const char *codec_name)
148 struct hdac_ext_link *hlink = NULL;
151 if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
153 if (bus->idx != bus_idx)
156 list_for_each_entry(hlink, &bus->hlink_list, list) {
157 for (i = 0; i < HDA_MAX_CODECS; i++) {
158 if (hlink->lsdiid & (0x1 << addr))
165 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_link);
167 static int check_hdac_link_power_active(struct hdac_ext_link *link, bool enable)
171 int mask = (1 << AZX_MLCTL_CPA_SHIFT);
177 val = readl(link->ml_addr + AZX_REG_ML_LCTL);
179 if (((val & mask) >> AZX_MLCTL_CPA_SHIFT))
182 if (!((val & mask) >> AZX_MLCTL_CPA_SHIFT))
192 * snd_hdac_ext_bus_link_power_up -power up hda link
193 * @link: HD-audio extended link
195 int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *link)
197 snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, 0, AZX_MLCTL_SPA);
199 return check_hdac_link_power_active(link, true);
201 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
204 * snd_hdac_ext_bus_link_power_down -power down hda link
205 * @link: HD-audio extended link
207 int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *link)
209 snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
211 return check_hdac_link_power_active(link, false);
213 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
216 * snd_hdac_ext_bus_link_power_up_all -power up all hda link
217 * @ebus: HD-audio extended bus
219 int snd_hdac_ext_bus_link_power_up_all(struct hdac_bus *bus)
221 struct hdac_ext_link *hlink = NULL;
224 list_for_each_entry(hlink, &bus->hlink_list, list) {
225 snd_hdac_updatel(hlink->ml_addr,
226 AZX_REG_ML_LCTL, 0, AZX_MLCTL_SPA);
227 ret = check_hdac_link_power_active(hlink, true);
234 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
237 * snd_hdac_ext_bus_link_power_down_all -power down all hda link
238 * @ebus: HD-audio extended bus
240 int snd_hdac_ext_bus_link_power_down_all(struct hdac_bus *bus)
242 struct hdac_ext_link *hlink = NULL;
245 list_for_each_entry(hlink, &bus->hlink_list, list) {
246 snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
247 ret = check_hdac_link_power_active(hlink, false);
254 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
256 int snd_hdac_ext_bus_link_get(struct hdac_bus *bus,
257 struct hdac_ext_link *link)
261 mutex_lock(&bus->lock);
264 * if we move from 0 to 1, count will be 1 so power up this link
265 * as well, also check the dma status and trigger that
267 if (++link->ref_count == 1) {
268 if (!bus->cmd_dma_state) {
269 snd_hdac_bus_init_cmd_io(bus);
270 bus->cmd_dma_state = true;
273 ret = snd_hdac_ext_bus_link_power_up(link);
276 * wait for 521usec for codec to report status
277 * HDA spec section 4.3 - Codec Discovery
280 bus->codec_mask = snd_hdac_chip_readw(bus, STATESTS);
281 dev_dbg(bus->dev, "codec_mask = 0x%lx\n", bus->codec_mask);
282 snd_hdac_chip_writew(bus, STATESTS, bus->codec_mask);
285 mutex_unlock(&bus->lock);
288 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
290 int snd_hdac_ext_bus_link_put(struct hdac_bus *bus,
291 struct hdac_ext_link *link)
294 struct hdac_ext_link *hlink;
295 bool link_up = false;
297 mutex_lock(&bus->lock);
300 * if we move from 1 to 0, count will be 0
301 * so power down this link as well
303 if (--link->ref_count == 0) {
304 ret = snd_hdac_ext_bus_link_power_down(link);
307 * now check if all links are off, if so turn off
310 list_for_each_entry(hlink, &bus->hlink_list, list) {
311 if (hlink->ref_count) {
318 snd_hdac_bus_stop_cmd_io(bus);
319 bus->cmd_dma_state = false;
323 mutex_unlock(&bus->lock);
326 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);