1 // SPDX-License-Identifier: GPL-2.0-only
3 * hdac-ext-controller.c - HD-audio extended controller functions.
5 * Copyright (C) 2014-2015 Intel Corp
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <sound/hda_register.h>
15 #include <sound/hdaudio_ext.h>
18 * processing pipe helpers - these helpers are useful for dealing with HDA
19 * new capability of processing pipelines
23 * snd_hdac_ext_bus_ppcap_enable - enable/disable processing pipe capability
24 * @bus: the pointer to HDAC bus object
25 * @enable: flag to turn on/off the capability
27 void snd_hdac_ext_bus_ppcap_enable(struct hdac_bus *bus, bool enable)
31 dev_err(bus->dev, "Address of PP capability is NULL");
36 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
37 AZX_PPCTL_GPROCEN, AZX_PPCTL_GPROCEN);
39 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
40 AZX_PPCTL_GPROCEN, 0);
42 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_enable);
45 * snd_hdac_ext_bus_ppcap_int_enable - ppcap interrupt enable/disable
46 * @bus: the pointer to HDAC bus object
47 * @enable: flag to enable/disable interrupt
49 void snd_hdac_ext_bus_ppcap_int_enable(struct hdac_bus *bus, bool enable)
53 dev_err(bus->dev, "Address of PP capability is NULL\n");
58 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
59 AZX_PPCTL_PIE, AZX_PPCTL_PIE);
61 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
64 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_int_enable);
67 * Multilink helpers - these helpers are useful for dealing with HDA
68 * new multilink capability
72 * snd_hdac_ext_bus_get_ml_capabilities - get multilink capability
73 * @bus: the pointer to HDAC bus object
75 * This will parse all links and read the mlink capabilities and add them
76 * in hlink_list of extended hdac bus
77 * Note: this will be freed on bus exit by driver
79 int snd_hdac_ext_bus_get_ml_capabilities(struct hdac_bus *bus)
83 struct hdac_ext_link *hlink;
85 link_count = readl(bus->mlcap + AZX_REG_ML_MLCD) + 1;
87 dev_dbg(bus->dev, "In %s Link count: %d\n", __func__, link_count);
89 for (idx = 0; idx < link_count; idx++) {
90 hlink = kzalloc(sizeof(*hlink), GFP_KERNEL);
95 hlink->ml_addr = bus->mlcap + AZX_ML_BASE +
96 (AZX_ML_INTERVAL * idx);
97 hlink->lcaps = readl(hlink->ml_addr + AZX_REG_ML_LCAP);
98 hlink->lsdiid = readw(hlink->ml_addr + AZX_REG_ML_LSDIID);
100 /* since link in On, update the ref */
101 hlink->ref_count = 1;
103 list_add_tail(&hlink->list, &bus->hlink_list);
108 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
111 * snd_hdac_ext_link_free_all- free hdac extended link objects
113 * @bus: the pointer to HDAC bus object
116 void snd_hdac_ext_link_free_all(struct hdac_bus *bus)
118 struct hdac_ext_link *hlink;
120 while (!list_empty(&bus->hlink_list)) {
121 hlink = list_first_entry(&bus->hlink_list, struct hdac_ext_link, list);
122 list_del(&hlink->list);
126 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_free_all);
129 * snd_hdac_ext_bus_get_hlink_by_addr - get hlink at specified address
130 * @bus: hlink's parent bus device
131 * @addr: codec device address
133 * Returns hlink object or NULL if matching hlink is not found.
135 struct hdac_ext_link *snd_hdac_ext_bus_get_hlink_by_addr(struct hdac_bus *bus, int addr)
137 struct hdac_ext_link *hlink;
139 list_for_each_entry(hlink, &bus->hlink_list, list)
140 if (hlink->lsdiid & (0x1 << addr))
144 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_hlink_by_addr);
147 * snd_hdac_ext_bus_get_hlink_by_name - get hlink based on codec name
148 * @bus: the pointer to HDAC bus object
149 * @codec_name: codec name
151 struct hdac_ext_link *snd_hdac_ext_bus_get_hlink_by_name(struct hdac_bus *bus,
152 const char *codec_name)
156 if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
158 if (bus->idx != bus_idx)
160 if (addr < 0 || addr > 31)
163 return snd_hdac_ext_bus_get_hlink_by_addr(bus, addr);
165 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_hlink_by_name);
167 static int check_hdac_link_power_active(struct hdac_ext_link *hlink, bool enable)
171 int mask = (1 << AZX_ML_LCTL_CPA_SHIFT);
177 val = readl(hlink->ml_addr + AZX_REG_ML_LCTL);
179 if (((val & mask) >> AZX_ML_LCTL_CPA_SHIFT))
182 if (!((val & mask) >> AZX_ML_LCTL_CPA_SHIFT))
192 * snd_hdac_ext_bus_link_power_up -power up hda link
193 * @hlink: HD-audio extended link
195 int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *hlink)
197 snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
198 AZX_ML_LCTL_SPA, AZX_ML_LCTL_SPA);
200 return check_hdac_link_power_active(hlink, true);
202 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
205 * snd_hdac_ext_bus_link_power_down -power down hda link
206 * @hlink: HD-audio extended link
208 int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *hlink)
210 snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL, AZX_ML_LCTL_SPA, 0);
212 return check_hdac_link_power_active(hlink, false);
214 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
217 * snd_hdac_ext_bus_link_power_up_all -power up all hda link
218 * @bus: the pointer to HDAC bus object
220 int snd_hdac_ext_bus_link_power_up_all(struct hdac_bus *bus)
222 struct hdac_ext_link *hlink = NULL;
225 list_for_each_entry(hlink, &bus->hlink_list, list) {
226 ret = snd_hdac_ext_bus_link_power_up(hlink);
233 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
236 * snd_hdac_ext_bus_link_power_down_all -power down all hda link
237 * @bus: the pointer to HDAC bus object
239 int snd_hdac_ext_bus_link_power_down_all(struct hdac_bus *bus)
241 struct hdac_ext_link *hlink = NULL;
244 list_for_each_entry(hlink, &bus->hlink_list, list) {
245 ret = snd_hdac_ext_bus_link_power_down(hlink);
252 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
255 * snd_hdac_ext_bus_link_set_stream_id - maps stream id to link output
256 * @link: HD-audio ext link to set up
259 void snd_hdac_ext_bus_link_set_stream_id(struct hdac_ext_link *link,
262 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 1 << stream);
264 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_set_stream_id);
267 * snd_hdac_ext_bus_link_clear_stream_id - maps stream id to link output
268 * @link: HD-audio ext link to set up
271 void snd_hdac_ext_bus_link_clear_stream_id(struct hdac_ext_link *link,
274 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 0);
276 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_clear_stream_id);
278 int snd_hdac_ext_bus_link_get(struct hdac_bus *bus,
279 struct hdac_ext_link *hlink)
281 unsigned long codec_mask;
284 mutex_lock(&bus->lock);
287 * if we move from 0 to 1, count will be 1 so power up this link
288 * as well, also check the dma status and trigger that
290 if (++hlink->ref_count == 1) {
291 if (!bus->cmd_dma_state) {
292 snd_hdac_bus_init_cmd_io(bus);
293 bus->cmd_dma_state = true;
296 ret = snd_hdac_ext_bus_link_power_up(hlink);
299 * clear the register to invalidate all the output streams
301 snd_hdac_updatew(hlink->ml_addr, AZX_REG_ML_LOSIDV,
302 AZX_ML_LOSIDV_STREAM_MASK, 0);
304 * wait for 521usec for codec to report status
305 * HDA spec section 4.3 - Codec Discovery
308 codec_mask = snd_hdac_chip_readw(bus, STATESTS);
309 dev_dbg(bus->dev, "codec_mask = 0x%lx\n", codec_mask);
310 snd_hdac_chip_writew(bus, STATESTS, codec_mask);
311 if (!bus->codec_mask)
312 bus->codec_mask = codec_mask;
315 mutex_unlock(&bus->lock);
318 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
320 int snd_hdac_ext_bus_link_put(struct hdac_bus *bus,
321 struct hdac_ext_link *hlink)
324 struct hdac_ext_link *hlink_tmp;
325 bool link_up = false;
327 mutex_lock(&bus->lock);
330 * if we move from 1 to 0, count will be 0
331 * so power down this link as well
333 if (--hlink->ref_count == 0) {
334 ret = snd_hdac_ext_bus_link_power_down(hlink);
337 * now check if all links are off, if so turn off
340 list_for_each_entry(hlink_tmp, &bus->hlink_list, list) {
341 if (hlink_tmp->ref_count) {
348 snd_hdac_bus_stop_cmd_io(bus);
349 bus->cmd_dma_state = false;
353 mutex_unlock(&bus->lock);
356 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);
358 static void hdac_ext_codec_link_up(struct hdac_device *codec)
360 const char *devname = dev_name(&codec->dev);
361 struct hdac_ext_link *hlink =
362 snd_hdac_ext_bus_get_hlink_by_name(codec->bus, devname);
365 snd_hdac_ext_bus_link_get(codec->bus, hlink);
368 static void hdac_ext_codec_link_down(struct hdac_device *codec)
370 const char *devname = dev_name(&codec->dev);
371 struct hdac_ext_link *hlink =
372 snd_hdac_ext_bus_get_hlink_by_name(codec->bus, devname);
375 snd_hdac_ext_bus_link_put(codec->bus, hlink);
378 void snd_hdac_ext_bus_link_power(struct hdac_device *codec, bool enable)
380 struct hdac_bus *bus = codec->bus;
381 bool oldstate = test_bit(codec->addr, &bus->codec_powered);
383 if (enable == oldstate)
386 snd_hdac_bus_link_power(codec, enable);
389 hdac_ext_codec_link_up(codec);
391 hdac_ext_codec_link_down(codec);
393 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power);