Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / sound / hda / ext / hdac_ext_controller.c
blobb0c0ef824d7d914f5748f01bed8616fb876f146f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * hdac-ext-controller.c - HD-audio extended controller functions.
5 * Copyright (C) 2014-2015 Intel Corp
6 * Author: Jeeja KP <jeeja.kp@intel.com>
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 * maximum HDAC capablities we should parse to avoid endless looping:
19 * currently we have 4 extended caps, so this is future proof for now.
20 * extend when this limit is seen meeting in real HW
22 #define HDAC_MAX_CAPS 10
25 * processing pipe helpers - these helpers are useful for dealing with HDA
26 * new capability of processing pipelines
29 /**
30 * snd_hdac_ext_bus_ppcap_enable - enable/disable processing pipe capability
31 * @bus: the pointer to HDAC bus object
32 * @enable: flag to turn on/off the capability
34 void snd_hdac_ext_bus_ppcap_enable(struct hdac_bus *bus, bool enable)
37 if (!bus->ppcap) {
38 dev_err(bus->dev, "Address of PP capability is NULL");
39 return;
42 if (enable)
43 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
44 AZX_PPCTL_GPROCEN, AZX_PPCTL_GPROCEN);
45 else
46 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
47 AZX_PPCTL_GPROCEN, 0);
49 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_enable);
51 /**
52 * snd_hdac_ext_bus_ppcap_int_enable - ppcap interrupt enable/disable
53 * @bus: the pointer to HDAC bus object
54 * @enable: flag to enable/disable interrupt
56 void snd_hdac_ext_bus_ppcap_int_enable(struct hdac_bus *bus, bool enable)
59 if (!bus->ppcap) {
60 dev_err(bus->dev, "Address of PP capability is NULL\n");
61 return;
64 if (enable)
65 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
66 AZX_PPCTL_PIE, AZX_PPCTL_PIE);
67 else
68 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
69 AZX_PPCTL_PIE, 0);
71 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_int_enable);
74 * Multilink helpers - these helpers are useful for dealing with HDA
75 * new multilink capability
78 /**
79 * snd_hdac_ext_bus_get_ml_capabilities - get multilink capability
80 * @bus: the pointer to HDAC bus object
82 * This will parse all links and read the mlink capabilities and add them
83 * in hlink_list of extended hdac bus
84 * Note: this will be freed on bus exit by driver
86 int snd_hdac_ext_bus_get_ml_capabilities(struct hdac_bus *bus)
88 int idx;
89 u32 link_count;
90 struct hdac_ext_link *hlink;
92 link_count = readl(bus->mlcap + AZX_REG_ML_MLCD) + 1;
94 dev_dbg(bus->dev, "In %s Link count: %d\n", __func__, link_count);
96 for (idx = 0; idx < link_count; idx++) {
97 hlink = kzalloc(sizeof(*hlink), GFP_KERNEL);
98 if (!hlink)
99 return -ENOMEM;
100 hlink->index = idx;
101 hlink->bus = bus;
102 hlink->ml_addr = bus->mlcap + AZX_ML_BASE +
103 (AZX_ML_INTERVAL * idx);
104 hlink->lcaps = readl(hlink->ml_addr + AZX_REG_ML_LCAP);
105 hlink->lsdiid = readw(hlink->ml_addr + AZX_REG_ML_LSDIID);
107 /* since link in On, update the ref */
108 hlink->ref_count = 1;
110 list_add_tail(&hlink->list, &bus->hlink_list);
113 return 0;
115 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
118 * snd_hdac_link_free_all- free hdac extended link objects
120 * @bus: the pointer to HDAC bus object
123 void snd_hdac_link_free_all(struct hdac_bus *bus)
125 struct hdac_ext_link *l;
127 while (!list_empty(&bus->hlink_list)) {
128 l = list_first_entry(&bus->hlink_list, struct hdac_ext_link, list);
129 list_del(&l->list);
130 kfree(l);
133 EXPORT_SYMBOL_GPL(snd_hdac_link_free_all);
136 * snd_hdac_ext_bus_get_link_index - get link based on codec name
137 * @bus: the pointer to HDAC bus object
138 * @codec_name: codec name
140 struct hdac_ext_link *snd_hdac_ext_bus_get_link(struct hdac_bus *bus,
141 const char *codec_name)
143 int i;
144 struct hdac_ext_link *hlink = NULL;
145 int bus_idx, addr;
147 if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
148 return NULL;
149 if (bus->idx != bus_idx)
150 return NULL;
151 if (addr < 0 || addr > 31)
152 return NULL;
154 list_for_each_entry(hlink, &bus->hlink_list, list) {
155 for (i = 0; i < HDA_MAX_CODECS; i++) {
156 if (hlink->lsdiid & (0x1 << addr))
157 return hlink;
161 return NULL;
163 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_link);
165 static int check_hdac_link_power_active(struct hdac_ext_link *link, bool enable)
167 int timeout;
168 u32 val;
169 int mask = (1 << AZX_MLCTL_CPA_SHIFT);
171 udelay(3);
172 timeout = 150;
174 do {
175 val = readl(link->ml_addr + AZX_REG_ML_LCTL);
176 if (enable) {
177 if (((val & mask) >> AZX_MLCTL_CPA_SHIFT))
178 return 0;
179 } else {
180 if (!((val & mask) >> AZX_MLCTL_CPA_SHIFT))
181 return 0;
183 udelay(3);
184 } while (--timeout);
186 return -EIO;
190 * snd_hdac_ext_bus_link_power_up -power up hda link
191 * @link: HD-audio extended link
193 int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *link)
195 snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL,
196 AZX_MLCTL_SPA, AZX_MLCTL_SPA);
198 return check_hdac_link_power_active(link, true);
200 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
203 * snd_hdac_ext_bus_link_power_down -power down hda link
204 * @link: HD-audio extended link
206 int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *link)
208 snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
210 return check_hdac_link_power_active(link, false);
212 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
215 * snd_hdac_ext_bus_link_power_up_all -power up all hda link
216 * @bus: the pointer to HDAC bus object
218 int snd_hdac_ext_bus_link_power_up_all(struct hdac_bus *bus)
220 struct hdac_ext_link *hlink = NULL;
221 int ret;
223 list_for_each_entry(hlink, &bus->hlink_list, list) {
224 snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
225 AZX_MLCTL_SPA, AZX_MLCTL_SPA);
226 ret = check_hdac_link_power_active(hlink, true);
227 if (ret < 0)
228 return ret;
231 return 0;
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;
242 int ret;
244 list_for_each_entry(hlink, &bus->hlink_list, list) {
245 snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
246 AZX_MLCTL_SPA, 0);
247 ret = check_hdac_link_power_active(hlink, false);
248 if (ret < 0)
249 return ret;
252 return 0;
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)
259 unsigned long codec_mask;
260 int ret = 0;
262 mutex_lock(&bus->lock);
265 * if we move from 0 to 1, count will be 1 so power up this link
266 * as well, also check the dma status and trigger that
268 if (++link->ref_count == 1) {
269 if (!bus->cmd_dma_state) {
270 snd_hdac_bus_init_cmd_io(bus);
271 bus->cmd_dma_state = true;
274 ret = snd_hdac_ext_bus_link_power_up(link);
277 * clear the register to invalidate all the output streams
279 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV,
280 ML_LOSIDV_STREAM_MASK, 0);
282 * wait for 521usec for codec to report status
283 * HDA spec section 4.3 - Codec Discovery
285 udelay(521);
286 codec_mask = snd_hdac_chip_readw(bus, STATESTS);
287 dev_dbg(bus->dev, "codec_mask = 0x%lx\n", codec_mask);
288 snd_hdac_chip_writew(bus, STATESTS, codec_mask);
289 if (!bus->codec_mask)
290 bus->codec_mask = codec_mask;
293 mutex_unlock(&bus->lock);
294 return ret;
296 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
298 int snd_hdac_ext_bus_link_put(struct hdac_bus *bus,
299 struct hdac_ext_link *link)
301 int ret = 0;
302 struct hdac_ext_link *hlink;
303 bool link_up = false;
305 mutex_lock(&bus->lock);
308 * if we move from 1 to 0, count will be 0
309 * so power down this link as well
311 if (--link->ref_count == 0) {
312 ret = snd_hdac_ext_bus_link_power_down(link);
315 * now check if all links are off, if so turn off
316 * cmd dma as well
318 list_for_each_entry(hlink, &bus->hlink_list, list) {
319 if (hlink->ref_count) {
320 link_up = true;
321 break;
325 if (!link_up) {
326 snd_hdac_bus_stop_cmd_io(bus);
327 bus->cmd_dma_state = false;
331 mutex_unlock(&bus->lock);
332 return ret;
334 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);