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/delay.h>
13 #include <linux/slab.h>
14 #include <sound/hda_register.h>
15 #include <sound/hdaudio_ext.h>
16 
17 /*
18  * processing pipe helpers - these helpers are useful for dealing with HDA
19  * new capability of processing pipelines
20  */
21 
22 /**
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
26  */
27 void snd_hdac_ext_bus_ppcap_enable(struct hdac_bus *bus, bool enable)
28 {
29 
30 	if (!bus->ppcap) {
31 		dev_err(bus->dev, "Address of PP capability is NULL");
32 		return;
33 	}
34 
35 	if (enable)
36 		snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
37 				 AZX_PPCTL_GPROCEN, AZX_PPCTL_GPROCEN);
38 	else
39 		snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
40 				 AZX_PPCTL_GPROCEN, 0);
41 }
42 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_enable);
43 
44 /**
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
48  */
49 void snd_hdac_ext_bus_ppcap_int_enable(struct hdac_bus *bus, bool enable)
50 {
51 
52 	if (!bus->ppcap) {
53 		dev_err(bus->dev, "Address of PP capability is NULL\n");
54 		return;
55 	}
56 
57 	if (enable)
58 		snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
59 				 AZX_PPCTL_PIE, AZX_PPCTL_PIE);
60 	else
61 		snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
62 				 AZX_PPCTL_PIE, 0);
63 }
64 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_int_enable);
65 
66 /*
67  * Multilink helpers - these helpers are useful for dealing with HDA
68  * new multilink capability
69  */
70 
71 /**
72  * snd_hdac_ext_bus_get_ml_capabilities - get multilink capability
73  * @bus: the pointer to HDAC bus object
74  *
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
78  */
79 int snd_hdac_ext_bus_get_ml_capabilities(struct hdac_bus *bus)
80 {
81 	int idx;
82 	u32 link_count;
83 	struct hdac_ext_link *hlink;
84 
85 	link_count = readl(bus->mlcap + AZX_REG_ML_MLCD) + 1;
86 
87 	dev_dbg(bus->dev, "In %s Link count: %d\n", __func__, link_count);
88 
89 	for (idx = 0; idx < link_count; idx++) {
90 		hlink  = kzalloc(sizeof(*hlink), GFP_KERNEL);
91 		if (!hlink)
92 			return -ENOMEM;
93 		hlink->index = idx;
94 		hlink->bus = bus;
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);
99 
100 		/* since link in On, update the ref */
101 		hlink->ref_count = 1;
102 
103 		list_add_tail(&hlink->list, &bus->hlink_list);
104 	}
105 
106 	return 0;
107 }
108 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
109 
110 /**
111  * snd_hdac_link_free_all- free hdac extended link objects
112  *
113  * @bus: the pointer to HDAC bus object
114  */
115 
116 void snd_hdac_link_free_all(struct hdac_bus *bus)
117 {
118 	struct hdac_ext_link *l;
119 
120 	while (!list_empty(&bus->hlink_list)) {
121 		l = list_first_entry(&bus->hlink_list, struct hdac_ext_link, list);
122 		list_del(&l->list);
123 		kfree(l);
124 	}
125 }
126 EXPORT_SYMBOL_GPL(snd_hdac_link_free_all);
127 
128 /**
129  * snd_hdac_ext_bus_link_at - get link at specified address
130  * @bus: link's parent bus device
131  * @addr: codec device address
132  *
133  * Returns link object or NULL if matching link is not found.
134  */
135 struct hdac_ext_link *snd_hdac_ext_bus_link_at(struct hdac_bus *bus, int addr)
136 {
137 	struct hdac_ext_link *hlink;
138 	int i;
139 
140 	list_for_each_entry(hlink, &bus->hlink_list, list)
141 		for (i = 0; i < HDA_MAX_CODECS; i++)
142 			if (hlink->lsdiid & (0x1 << addr))
143 				return hlink;
144 	return NULL;
145 }
146 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_at);
147 
148 /**
149  * snd_hdac_ext_bus_get_link - get link based on codec name
150  * @bus: the pointer to HDAC bus object
151  * @codec_name: codec name
152  */
153 struct hdac_ext_link *snd_hdac_ext_bus_get_link(struct hdac_bus *bus,
154 						 const char *codec_name)
155 {
156 	int bus_idx, addr;
157 
158 	if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
159 		return NULL;
160 	if (bus->idx != bus_idx)
161 		return NULL;
162 	if (addr < 0 || addr > 31)
163 		return NULL;
164 
165 	return snd_hdac_ext_bus_link_at(bus, addr);
166 }
167 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_link);
168 
169 static int check_hdac_link_power_active(struct hdac_ext_link *link, bool enable)
170 {
171 	int timeout;
172 	u32 val;
173 	int mask = (1 << AZX_MLCTL_CPA_SHIFT);
174 
175 	udelay(3);
176 	timeout = 150;
177 
178 	do {
179 		val = readl(link->ml_addr + AZX_REG_ML_LCTL);
180 		if (enable) {
181 			if (((val & mask) >> AZX_MLCTL_CPA_SHIFT))
182 				return 0;
183 		} else {
184 			if (!((val & mask) >> AZX_MLCTL_CPA_SHIFT))
185 				return 0;
186 		}
187 		udelay(3);
188 	} while (--timeout);
189 
190 	return -EIO;
191 }
192 
193 /**
194  * snd_hdac_ext_bus_link_power_up -power up hda link
195  * @link: HD-audio extended link
196  */
197 int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *link)
198 {
199 	snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL,
200 			 AZX_MLCTL_SPA, AZX_MLCTL_SPA);
201 
202 	return check_hdac_link_power_active(link, true);
203 }
204 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
205 
206 /**
207  * snd_hdac_ext_bus_link_power_down -power down hda link
208  * @link: HD-audio extended link
209  */
210 int 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 }
216 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
217 
218 /**
219  * snd_hdac_ext_bus_link_power_up_all -power up all hda link
220  * @bus: the pointer to HDAC bus object
221  */
222 int snd_hdac_ext_bus_link_power_up_all(struct hdac_bus *bus)
223 {
224 	struct hdac_ext_link *hlink = NULL;
225 	int ret;
226 
227 	list_for_each_entry(hlink, &bus->hlink_list, list) {
228 		snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
229 				 AZX_MLCTL_SPA, 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 }
237 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
238 
239 /**
240  * snd_hdac_ext_bus_link_power_down_all -power down all hda link
241  * @bus: the pointer to HDAC bus object
242  */
243 int snd_hdac_ext_bus_link_power_down_all(struct hdac_bus *bus)
244 {
245 	struct hdac_ext_link *hlink = NULL;
246 	int ret;
247 
248 	list_for_each_entry(hlink, &bus->hlink_list, list) {
249 		snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
250 				 AZX_MLCTL_SPA, 0);
251 		ret = check_hdac_link_power_active(hlink, false);
252 		if (ret < 0)
253 			return ret;
254 	}
255 
256 	return 0;
257 }
258 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
259 
260 int snd_hdac_ext_bus_link_get(struct hdac_bus *bus,
261 				struct hdac_ext_link *link)
262 {
263 	unsigned long codec_mask;
264 	int ret = 0;
265 
266 	mutex_lock(&bus->lock);
267 
268 	/*
269 	 * if we move from 0 to 1, count will be 1 so power up this link
270 	 * as well, also check the dma status and trigger that
271 	 */
272 	if (++link->ref_count == 1) {
273 		if (!bus->cmd_dma_state) {
274 			snd_hdac_bus_init_cmd_io(bus);
275 			bus->cmd_dma_state = true;
276 		}
277 
278 		ret = snd_hdac_ext_bus_link_power_up(link);
279 
280 		/*
281 		 * clear the register to invalidate all the output streams
282 		 */
283 		snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV,
284 				 ML_LOSIDV_STREAM_MASK, 0);
285 		/*
286 		 *  wait for 521usec for codec to report status
287 		 *  HDA spec section 4.3 - Codec Discovery
288 		 */
289 		udelay(521);
290 		codec_mask = snd_hdac_chip_readw(bus, STATESTS);
291 		dev_dbg(bus->dev, "codec_mask = 0x%lx\n", codec_mask);
292 		snd_hdac_chip_writew(bus, STATESTS, codec_mask);
293 		if (!bus->codec_mask)
294 			bus->codec_mask = codec_mask;
295 	}
296 
297 	mutex_unlock(&bus->lock);
298 	return ret;
299 }
300 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
301 
302 int snd_hdac_ext_bus_link_put(struct hdac_bus *bus,
303 				struct hdac_ext_link *link)
304 {
305 	int ret = 0;
306 	struct hdac_ext_link *hlink;
307 	bool link_up = false;
308 
309 	mutex_lock(&bus->lock);
310 
311 	/*
312 	 * if we move from 1 to 0, count will be 0
313 	 * so power down this link as well
314 	 */
315 	if (--link->ref_count == 0) {
316 		ret = snd_hdac_ext_bus_link_power_down(link);
317 
318 		/*
319 		 * now check if all links are off, if so turn off
320 		 * cmd dma as well
321 		 */
322 		list_for_each_entry(hlink, &bus->hlink_list, list) {
323 			if (hlink->ref_count) {
324 				link_up = true;
325 				break;
326 			}
327 		}
328 
329 		if (!link_up) {
330 			snd_hdac_bus_stop_cmd_io(bus);
331 			bus->cmd_dma_state = false;
332 		}
333 	}
334 
335 	mutex_unlock(&bus->lock);
336 	return ret;
337 }
338 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);
339 
340 static void hdac_ext_codec_link_up(struct hdac_device *codec)
341 {
342 	const char *devname = dev_name(&codec->dev);
343 	struct hdac_ext_link *hlink =
344 		snd_hdac_ext_bus_get_link(codec->bus, devname);
345 
346 	if (hlink)
347 		snd_hdac_ext_bus_link_get(codec->bus, hlink);
348 }
349 
350 static void hdac_ext_codec_link_down(struct hdac_device *codec)
351 {
352 	const char *devname = dev_name(&codec->dev);
353 	struct hdac_ext_link *hlink =
354 		snd_hdac_ext_bus_get_link(codec->bus, devname);
355 
356 	if (hlink)
357 		snd_hdac_ext_bus_link_put(codec->bus, hlink);
358 }
359 
360 void snd_hdac_ext_bus_link_power(struct hdac_device *codec, bool enable)
361 {
362 	struct hdac_bus *bus = codec->bus;
363 	bool oldstate = test_bit(codec->addr, &bus->codec_powered);
364 
365 	if (enable == oldstate)
366 		return;
367 
368 	snd_hdac_bus_link_power(codec, enable);
369 
370 	if (enable)
371 		hdac_ext_codec_link_up(codec);
372 	else
373 		hdac_ext_codec_link_down(codec);
374 }
375 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power);
376