1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <sound/hda_register.h>
23#include <sound/hdaudio_ext.h>
24
25
26
27
28
29
30#define HDAC_MAX_CAPS 10
31
32
33
34
35
36
37
38
39
40
41
42void snd_hdac_ext_bus_ppcap_enable(struct hdac_ext_bus *ebus, bool enable)
43{
44 struct hdac_bus *bus = &ebus->bus;
45
46 if (!bus->ppcap) {
47 dev_err(bus->dev, "Address of PP capability is NULL");
48 return;
49 }
50
51 if (enable)
52 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, 0, AZX_PPCTL_GPROCEN);
53 else
54 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, AZX_PPCTL_GPROCEN, 0);
55}
56EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_enable);
57
58
59
60
61
62
63void snd_hdac_ext_bus_ppcap_int_enable(struct hdac_ext_bus *ebus, bool enable)
64{
65 struct hdac_bus *bus = &ebus->bus;
66
67 if (!bus->ppcap) {
68 dev_err(bus->dev, "Address of PP capability is NULL\n");
69 return;
70 }
71
72 if (enable)
73 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, 0, AZX_PPCTL_PIE);
74 else
75 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, AZX_PPCTL_PIE, 0);
76}
77EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_int_enable);
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92int snd_hdac_ext_bus_get_ml_capabilities(struct hdac_ext_bus *ebus)
93{
94 int idx;
95 u32 link_count;
96 struct hdac_ext_link *hlink;
97 struct hdac_bus *bus = &ebus->bus;
98
99 link_count = readl(bus->mlcap + AZX_REG_ML_MLCD) + 1;
100
101 dev_dbg(bus->dev, "In %s Link count: %d\n", __func__, link_count);
102
103 for (idx = 0; idx < link_count; idx++) {
104 hlink = kzalloc(sizeof(*hlink), GFP_KERNEL);
105 if (!hlink)
106 return -ENOMEM;
107 hlink->index = idx;
108 hlink->bus = bus;
109 hlink->ml_addr = bus->mlcap + AZX_ML_BASE +
110 (AZX_ML_INTERVAL * idx);
111 hlink->lcaps = readl(hlink->ml_addr + AZX_REG_ML_LCAP);
112 hlink->lsdiid = readw(hlink->ml_addr + AZX_REG_ML_LSDIID);
113
114
115 hlink->ref_count = 1;
116
117 list_add_tail(&hlink->list, &ebus->hlink_list);
118 }
119
120 return 0;
121}
122EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
123
124
125
126
127
128
129
130void snd_hdac_link_free_all(struct hdac_ext_bus *ebus)
131{
132 struct hdac_ext_link *l;
133
134 while (!list_empty(&ebus->hlink_list)) {
135 l = list_first_entry(&ebus->hlink_list, struct hdac_ext_link, list);
136 list_del(&l->list);
137 kfree(l);
138 }
139}
140EXPORT_SYMBOL_GPL(snd_hdac_link_free_all);
141
142
143
144
145
146
147struct hdac_ext_link *snd_hdac_ext_bus_get_link(struct hdac_ext_bus *ebus,
148 const char *codec_name)
149{
150 int i;
151 struct hdac_ext_link *hlink = NULL;
152 int bus_idx, addr;
153
154 if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
155 return NULL;
156 if (ebus->idx != bus_idx)
157 return NULL;
158
159 list_for_each_entry(hlink, &ebus->hlink_list, list) {
160 for (i = 0; i < HDA_MAX_CODECS; i++) {
161 if (hlink->lsdiid & (0x1 << addr))
162 return hlink;
163 }
164 }
165
166 return NULL;
167}
168EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_link);
169
170static int check_hdac_link_power_active(struct hdac_ext_link *link, bool enable)
171{
172 int timeout;
173 u32 val;
174 int mask = (1 << AZX_MLCTL_CPA_SHIFT);
175
176 udelay(3);
177 timeout = 150;
178
179 do {
180 val = readl(link->ml_addr + AZX_REG_ML_LCTL);
181 if (enable) {
182 if (((val & mask) >> AZX_MLCTL_CPA_SHIFT))
183 return 0;
184 } else {
185 if (!((val & mask) >> AZX_MLCTL_CPA_SHIFT))
186 return 0;
187 }
188 udelay(3);
189 } while (--timeout);
190
191 return -EIO;
192}
193
194
195
196
197
198int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *link)
199{
200 snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, 0, AZX_MLCTL_SPA);
201
202 return check_hdac_link_power_active(link, true);
203}
204EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
205
206
207
208
209
210int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *link)
211{
212 snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
213
214 return check_hdac_link_power_active(link, false);
215}
216EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
217
218
219
220
221
222int snd_hdac_ext_bus_link_power_up_all(struct hdac_ext_bus *ebus)
223{
224 struct hdac_ext_link *hlink = NULL;
225 int ret;
226
227 list_for_each_entry(hlink, &ebus->hlink_list, list) {
228 snd_hdac_updatel(hlink->ml_addr,
229 AZX_REG_ML_LCTL, 0, AZX_MLCTL_SPA);
230 ret = check_hdac_link_power_active(hlink, true);
231 if (ret < 0)
232 return ret;
233 }
234
235 return 0;
236}
237EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
238
239
240
241
242
243int snd_hdac_ext_bus_link_power_down_all(struct hdac_ext_bus *ebus)
244{
245 struct hdac_ext_link *hlink = NULL;
246 int ret;
247
248 list_for_each_entry(hlink, &ebus->hlink_list, list) {
249 snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
250 ret = check_hdac_link_power_active(hlink, false);
251 if (ret < 0)
252 return ret;
253 }
254
255 return 0;
256}
257EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
258
259int snd_hdac_ext_bus_link_get(struct hdac_ext_bus *ebus,
260 struct hdac_ext_link *link)
261{
262 int ret = 0;
263
264 mutex_lock(&ebus->lock);
265
266
267
268
269
270 if (++link->ref_count == 1) {
271 if (!ebus->cmd_dma_state) {
272 snd_hdac_bus_init_cmd_io(&ebus->bus);
273 ebus->cmd_dma_state = true;
274 }
275
276 ret = snd_hdac_ext_bus_link_power_up(link);
277 }
278
279 mutex_unlock(&ebus->lock);
280 return ret;
281}
282EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
283
284int snd_hdac_ext_bus_link_put(struct hdac_ext_bus *ebus,
285 struct hdac_ext_link *link)
286{
287 int ret = 0;
288 struct hdac_ext_link *hlink;
289 bool link_up = false;
290
291 mutex_lock(&ebus->lock);
292
293
294
295
296
297 if (--link->ref_count == 0) {
298 ret = snd_hdac_ext_bus_link_power_down(link);
299
300
301
302
303
304 list_for_each_entry(hlink, &ebus->hlink_list, list) {
305 if (hlink->ref_count) {
306 link_up = true;
307 break;
308 }
309 }
310
311 if (!link_up) {
312 snd_hdac_bus_stop_cmd_io(&ebus->bus);
313 ebus->cmd_dma_state = false;
314 }
315 }
316
317 mutex_unlock(&ebus->lock);
318 return ret;
319}
320EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);
321