1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * hdac-ext-controller.c - HD-audio extended controller functions.
4 *
5 * Copyright (C) 2014-2015 Intel Corp
6 * Author: Jeeja KP <jeeja.kp@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 */
11
12 #include <linux/bitfield.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <sound/hda_register.h>
16 #include <sound/hdaudio_ext.h>
17
18 /*
19 * processing pipe helpers - these helpers are useful for dealing with HDA
20 * new capability of processing pipelines
21 */
22
23 /**
24 * snd_hdac_ext_bus_ppcap_enable - enable/disable processing pipe capability
25 * @bus: the pointer to HDAC bus object
26 * @enable: flag to turn on/off the capability
27 */
snd_hdac_ext_bus_ppcap_enable(struct hdac_bus * bus,bool enable)28 void snd_hdac_ext_bus_ppcap_enable(struct hdac_bus *bus, bool enable)
29 {
30
31 if (!bus->ppcap) {
32 dev_err(bus->dev, "Address of PP capability is NULL");
33 return;
34 }
35
36 if (enable)
37 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
38 AZX_PPCTL_GPROCEN, AZX_PPCTL_GPROCEN);
39 else
40 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
41 AZX_PPCTL_GPROCEN, 0);
42 }
43 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_enable);
44
45 /**
46 * snd_hdac_ext_bus_ppcap_int_enable - ppcap interrupt enable/disable
47 * @bus: the pointer to HDAC bus object
48 * @enable: flag to enable/disable interrupt
49 */
snd_hdac_ext_bus_ppcap_int_enable(struct hdac_bus * bus,bool enable)50 void snd_hdac_ext_bus_ppcap_int_enable(struct hdac_bus *bus, bool enable)
51 {
52
53 if (!bus->ppcap) {
54 dev_err(bus->dev, "Address of PP capability is NULL\n");
55 return;
56 }
57
58 if (enable)
59 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
60 AZX_PPCTL_PIE, AZX_PPCTL_PIE);
61 else
62 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
63 AZX_PPCTL_PIE, 0);
64 }
65 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_int_enable);
66
67 /*
68 * Multilink helpers - these helpers are useful for dealing with HDA
69 * new multilink capability
70 */
71
72 /**
73 * snd_hdac_ext_bus_get_ml_capabilities - get multilink capability
74 * @bus: the pointer to HDAC bus object
75 *
76 * This will parse all links and read the mlink capabilities and add them
77 * in hlink_list of extended hdac bus
78 * Note: this will be freed on bus exit by driver
79 */
snd_hdac_ext_bus_get_ml_capabilities(struct hdac_bus * bus)80 int snd_hdac_ext_bus_get_ml_capabilities(struct hdac_bus *bus)
81 {
82 int idx;
83 u32 link_count;
84 struct hdac_ext_link *hlink;
85 u32 leptr;
86
87 link_count = readl(bus->mlcap + AZX_REG_ML_MLCD) + 1;
88
89 dev_dbg(bus->dev, "In %s Link count: %d\n", __func__, link_count);
90
91 for (idx = 0; idx < link_count; idx++) {
92 hlink = kzalloc_obj(*hlink);
93 if (!hlink) {
94 snd_hdac_ext_link_free_all(bus);
95 return -ENOMEM;
96 }
97 hlink->index = idx;
98 hlink->bus = bus;
99 hlink->ml_addr = bus->mlcap + AZX_ML_BASE +
100 (AZX_ML_INTERVAL * idx);
101 hlink->lcaps = readl(hlink->ml_addr + AZX_REG_ML_LCAP);
102 hlink->lsdiid = readw(hlink->ml_addr + AZX_REG_ML_LSDIID);
103 hlink->slcount = FIELD_GET(AZX_ML_HDA_LCAP_SLCOUNT, hlink->lcaps) + 1;
104
105 if (hdac_ext_link_alt(hlink)) {
106 leptr = readl(hlink->ml_addr + AZX_REG_ML_LEPTR);
107 hlink->id = FIELD_GET(AZX_REG_ML_LEPTR_ID, leptr);
108 }
109
110 /* since link in On, update the ref */
111 hlink->ref_count = 1;
112
113 list_add_tail(&hlink->list, &bus->hlink_list);
114 }
115
116 return 0;
117 }
118 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
119
120 /**
121 * snd_hdac_ext_link_free_all- free hdac extended link objects
122 *
123 * @bus: the pointer to HDAC bus object
124 */
125
snd_hdac_ext_link_free_all(struct hdac_bus * bus)126 void snd_hdac_ext_link_free_all(struct hdac_bus *bus)
127 {
128 struct hdac_ext_link *hlink;
129
130 while (!list_empty(&bus->hlink_list)) {
131 hlink = list_first_entry(&bus->hlink_list, struct hdac_ext_link, list);
132 list_del(&hlink->list);
133 kfree(hlink);
134 }
135 }
136 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_free_all);
137
snd_hdac_ext_bus_get_hlink_by_id(struct hdac_bus * bus,u32 id)138 struct hdac_ext_link *snd_hdac_ext_bus_get_hlink_by_id(struct hdac_bus *bus, u32 id)
139 {
140 struct hdac_ext_link *hlink;
141
142 list_for_each_entry(hlink, &bus->hlink_list, list)
143 if (hdac_ext_link_alt(hlink) && hlink->id == id)
144 return hlink;
145 return NULL;
146 }
147 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_hlink_by_id);
148
149 /**
150 * snd_hdac_ext_bus_get_hlink_by_addr - get hlink at specified address
151 * @bus: hlink's parent bus device
152 * @addr: codec device address
153 *
154 * Returns hlink object or NULL if matching hlink is not found.
155 */
snd_hdac_ext_bus_get_hlink_by_addr(struct hdac_bus * bus,int addr)156 struct hdac_ext_link *snd_hdac_ext_bus_get_hlink_by_addr(struct hdac_bus *bus, int addr)
157 {
158 struct hdac_ext_link *hlink;
159
160 list_for_each_entry(hlink, &bus->hlink_list, list)
161 if (hlink->lsdiid & (0x1 << addr))
162 return hlink;
163 return NULL;
164 }
165 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_hlink_by_addr);
166
167 /**
168 * snd_hdac_ext_bus_get_hlink_by_name - get hlink based on codec name
169 * @bus: the pointer to HDAC bus object
170 * @codec_name: codec name
171 */
snd_hdac_ext_bus_get_hlink_by_name(struct hdac_bus * bus,const char * codec_name)172 struct hdac_ext_link *snd_hdac_ext_bus_get_hlink_by_name(struct hdac_bus *bus,
173 const char *codec_name)
174 {
175 int bus_idx, addr;
176
177 if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
178 return NULL;
179 if (bus->idx != bus_idx)
180 return NULL;
181 if (addr < 0 || addr > 31)
182 return NULL;
183
184 return snd_hdac_ext_bus_get_hlink_by_addr(bus, addr);
185 }
186 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_hlink_by_name);
187
check_hdac_link_power_active(struct hdac_ext_link * hlink,bool enable)188 static int check_hdac_link_power_active(struct hdac_ext_link *hlink, bool enable)
189 {
190 int timeout;
191 u32 val;
192 int mask = (1 << AZX_ML_LCTL_CPA_SHIFT);
193
194 udelay(3);
195 timeout = 150;
196
197 do {
198 val = readl(hlink->ml_addr + AZX_REG_ML_LCTL);
199 if (enable) {
200 if (((val & mask) >> AZX_ML_LCTL_CPA_SHIFT))
201 return 0;
202 } else {
203 if (!((val & mask) >> AZX_ML_LCTL_CPA_SHIFT))
204 return 0;
205 }
206 udelay(3);
207 } while (--timeout);
208
209 return -EIO;
210 }
211
212 /**
213 * snd_hdac_ext_bus_link_power_up -power up hda link
214 * @hlink: HD-audio extended link
215 */
snd_hdac_ext_bus_link_power_up(struct hdac_ext_link * hlink)216 int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *hlink)
217 {
218 snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
219 AZX_ML_LCTL_SPA, AZX_ML_LCTL_SPA);
220
221 return check_hdac_link_power_active(hlink, true);
222 }
223 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
224
225 /**
226 * snd_hdac_ext_bus_link_power_down -power down hda link
227 * @hlink: HD-audio extended link
228 */
snd_hdac_ext_bus_link_power_down(struct hdac_ext_link * hlink)229 int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *hlink)
230 {
231 snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL, AZX_ML_LCTL_SPA, 0);
232
233 return check_hdac_link_power_active(hlink, false);
234 }
235 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
236
237 /**
238 * snd_hdac_ext_bus_link_power_up_all -power up all hda link
239 * @bus: the pointer to HDAC bus object
240 */
snd_hdac_ext_bus_link_power_up_all(struct hdac_bus * bus)241 int snd_hdac_ext_bus_link_power_up_all(struct hdac_bus *bus)
242 {
243 struct hdac_ext_link *hlink = NULL;
244 int ret;
245
246 list_for_each_entry(hlink, &bus->hlink_list, list) {
247 ret = snd_hdac_ext_bus_link_power_up(hlink);
248 if (ret < 0)
249 return ret;
250 }
251
252 return 0;
253 }
254 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
255
256 /**
257 * snd_hdac_ext_bus_link_power_down_all -power down all hda link
258 * @bus: the pointer to HDAC bus object
259 */
snd_hdac_ext_bus_link_power_down_all(struct hdac_bus * bus)260 int snd_hdac_ext_bus_link_power_down_all(struct hdac_bus *bus)
261 {
262 struct hdac_ext_link *hlink = NULL;
263 int ret;
264
265 list_for_each_entry(hlink, &bus->hlink_list, list) {
266 ret = snd_hdac_ext_bus_link_power_down(hlink);
267 if (ret < 0)
268 return ret;
269 }
270
271 return 0;
272 }
273 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
274
275 /**
276 * snd_hdac_ext_bus_link_set_stream_id - maps stream id to link output
277 * @link: HD-audio ext link to set up
278 * @stream: stream id
279 */
snd_hdac_ext_bus_link_set_stream_id(struct hdac_ext_link * link,int stream)280 void snd_hdac_ext_bus_link_set_stream_id(struct hdac_ext_link *link,
281 int stream)
282 {
283 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 1 << stream);
284 }
285 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_set_stream_id);
286
287 /**
288 * snd_hdac_ext_bus_link_clear_stream_id - maps stream id to link output
289 * @link: HD-audio ext link to set up
290 * @stream: stream id
291 */
snd_hdac_ext_bus_link_clear_stream_id(struct hdac_ext_link * link,int stream)292 void snd_hdac_ext_bus_link_clear_stream_id(struct hdac_ext_link *link,
293 int stream)
294 {
295 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 0);
296 }
297 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_clear_stream_id);
298
snd_hdac_ext_bus_link_get(struct hdac_bus * bus,struct hdac_ext_link * hlink)299 int snd_hdac_ext_bus_link_get(struct hdac_bus *bus,
300 struct hdac_ext_link *hlink)
301 {
302 unsigned long codec_mask;
303 int ret = 0;
304
305 guard(mutex)(&bus->lock);
306
307 /*
308 * if we move from 0 to 1, count will be 1 so power up this link
309 * as well, also check the dma status and trigger that
310 */
311 if (++hlink->ref_count == 1) {
312 if (!bus->cmd_dma_state) {
313 snd_hdac_bus_init_cmd_io(bus);
314 bus->cmd_dma_state = true;
315 }
316
317 ret = snd_hdac_ext_bus_link_power_up(hlink);
318
319 /*
320 * clear the register to invalidate all the output streams
321 */
322 snd_hdac_updatew(hlink->ml_addr, AZX_REG_ML_LOSIDV,
323 AZX_ML_LOSIDV_STREAM_MASK, 0);
324 /*
325 * wait for 521usec for codec to report status
326 * HDA spec section 4.3 - Codec Discovery
327 */
328 udelay(521);
329 codec_mask = snd_hdac_chip_readw(bus, STATESTS);
330 dev_dbg(bus->dev, "codec_mask = 0x%lx\n", codec_mask);
331 snd_hdac_chip_writew(bus, STATESTS, codec_mask);
332 if (!bus->codec_mask)
333 bus->codec_mask = codec_mask;
334 }
335
336 return ret;
337 }
338 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
339
snd_hdac_ext_bus_link_put(struct hdac_bus * bus,struct hdac_ext_link * hlink)340 int snd_hdac_ext_bus_link_put(struct hdac_bus *bus,
341 struct hdac_ext_link *hlink)
342 {
343 int ret = 0;
344 struct hdac_ext_link *hlink_tmp;
345 bool link_up = false;
346
347 guard(mutex)(&bus->lock);
348
349 /*
350 * if we move from 1 to 0, count will be 0
351 * so power down this link as well
352 */
353 if (--hlink->ref_count == 0) {
354 ret = snd_hdac_ext_bus_link_power_down(hlink);
355
356 /*
357 * now check if all links are off, if so turn off
358 * cmd dma as well
359 */
360 list_for_each_entry(hlink_tmp, &bus->hlink_list, list) {
361 if (hlink_tmp->ref_count) {
362 link_up = true;
363 break;
364 }
365 }
366
367 if (!link_up) {
368 snd_hdac_bus_stop_cmd_io(bus);
369 bus->cmd_dma_state = false;
370 }
371 }
372
373 return ret;
374 }
375 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);
376
hdac_ext_codec_link_up(struct hdac_device * codec)377 static void hdac_ext_codec_link_up(struct hdac_device *codec)
378 {
379 const char *devname = dev_name(&codec->dev);
380 struct hdac_ext_link *hlink =
381 snd_hdac_ext_bus_get_hlink_by_name(codec->bus, devname);
382
383 if (hlink)
384 snd_hdac_ext_bus_link_get(codec->bus, hlink);
385 }
386
hdac_ext_codec_link_down(struct hdac_device * codec)387 static void hdac_ext_codec_link_down(struct hdac_device *codec)
388 {
389 const char *devname = dev_name(&codec->dev);
390 struct hdac_ext_link *hlink =
391 snd_hdac_ext_bus_get_hlink_by_name(codec->bus, devname);
392
393 if (hlink)
394 snd_hdac_ext_bus_link_put(codec->bus, hlink);
395 }
396
snd_hdac_ext_bus_link_power(struct hdac_device * codec,bool enable)397 void snd_hdac_ext_bus_link_power(struct hdac_device *codec, bool enable)
398 {
399 struct hdac_bus *bus = codec->bus;
400 bool oldstate = test_bit(codec->addr, &bus->codec_powered);
401
402 if (enable == oldstate)
403 return;
404
405 snd_hdac_bus_link_power(codec, enable);
406
407 if (enable)
408 hdac_ext_codec_link_up(codec);
409 else
410 hdac_ext_codec_link_down(codec);
411 }
412 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power);
413