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 *hlink;
119 
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);
123 		kfree(hlink);
124 	}
125 }
126 EXPORT_SYMBOL_GPL(snd_hdac_link_free_all);
127 
128 /**
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
132  *
133  * Returns hlink object or NULL if matching hlink is not found.
134  */
135 struct hdac_ext_link *snd_hdac_ext_bus_get_hlink_by_addr(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_get_hlink_by_addr);
147 
148 /**
149  * snd_hdac_ext_bus_get_hlink_by_name - get hlink 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_hlink_by_name(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_get_hlink_by_addr(bus, addr);
166 }
167 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_hlink_by_name);
168 
169 static int check_hdac_link_power_active(struct hdac_ext_link *hlink, bool enable)
170 {
171 	int timeout;
172 	u32 val;
173 	int mask = (1 << AZX_ML_LCTL_CPA_SHIFT);
174 
175 	udelay(3);
176 	timeout = 150;
177 
178 	do {
179 		val = readl(hlink->ml_addr + AZX_REG_ML_LCTL);
180 		if (enable) {
181 			if (((val & mask) >> AZX_ML_LCTL_CPA_SHIFT))
182 				return 0;
183 		} else {
184 			if (!((val & mask) >> AZX_ML_LCTL_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  * @hlink: HD-audio extended link
196  */
197 int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *hlink)
198 {
199 	snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
200 			 AZX_ML_LCTL_SPA, AZX_ML_LCTL_SPA);
201 
202 	return check_hdac_link_power_active(hlink, 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  * @hlink: HD-audio extended link
209  */
210 int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *hlink)
211 {
212 	snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL, AZX_ML_LCTL_SPA, 0);
213 
214 	return check_hdac_link_power_active(hlink, 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 		ret = snd_hdac_ext_bus_link_power_up(hlink);
229 		if (ret < 0)
230 			return ret;
231 	}
232 
233 	return 0;
234 }
235 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
236 
237 /**
238  * snd_hdac_ext_bus_link_power_down_all -power down all hda link
239  * @bus: the pointer to HDAC bus object
240  */
241 int snd_hdac_ext_bus_link_power_down_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_down(hlink);
248 		if (ret < 0)
249 			return ret;
250 	}
251 
252 	return 0;
253 }
254 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
255 
256 int snd_hdac_ext_bus_link_get(struct hdac_bus *bus,
257 				struct hdac_ext_link *hlink)
258 {
259 	unsigned long codec_mask;
260 	int ret = 0;
261 
262 	mutex_lock(&bus->lock);
263 
264 	/*
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
267 	 */
268 	if (++hlink->ref_count == 1) {
269 		if (!bus->cmd_dma_state) {
270 			snd_hdac_bus_init_cmd_io(bus);
271 			bus->cmd_dma_state = true;
272 		}
273 
274 		ret = snd_hdac_ext_bus_link_power_up(hlink);
275 
276 		/*
277 		 * clear the register to invalidate all the output streams
278 		 */
279 		snd_hdac_updatew(hlink->ml_addr, AZX_REG_ML_LOSIDV,
280 				 AZX_ML_LOSIDV_STREAM_MASK, 0);
281 		/*
282 		 *  wait for 521usec for codec to report status
283 		 *  HDA spec section 4.3 - Codec Discovery
284 		 */
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;
291 	}
292 
293 	mutex_unlock(&bus->lock);
294 	return ret;
295 }
296 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
297 
298 int snd_hdac_ext_bus_link_put(struct hdac_bus *bus,
299 			      struct hdac_ext_link *hlink)
300 {
301 	int ret = 0;
302 	struct hdac_ext_link *hlink_tmp;
303 	bool link_up = false;
304 
305 	mutex_lock(&bus->lock);
306 
307 	/*
308 	 * if we move from 1 to 0, count will be 0
309 	 * so power down this link as well
310 	 */
311 	if (--hlink->ref_count == 0) {
312 		ret = snd_hdac_ext_bus_link_power_down(hlink);
313 
314 		/*
315 		 * now check if all links are off, if so turn off
316 		 * cmd dma as well
317 		 */
318 		list_for_each_entry(hlink_tmp, &bus->hlink_list, list) {
319 			if (hlink_tmp->ref_count) {
320 				link_up = true;
321 				break;
322 			}
323 		}
324 
325 		if (!link_up) {
326 			snd_hdac_bus_stop_cmd_io(bus);
327 			bus->cmd_dma_state = false;
328 		}
329 	}
330 
331 	mutex_unlock(&bus->lock);
332 	return ret;
333 }
334 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);
335 
336 static void hdac_ext_codec_link_up(struct hdac_device *codec)
337 {
338 	const char *devname = dev_name(&codec->dev);
339 	struct hdac_ext_link *hlink =
340 		snd_hdac_ext_bus_get_hlink_by_name(codec->bus, devname);
341 
342 	if (hlink)
343 		snd_hdac_ext_bus_link_get(codec->bus, hlink);
344 }
345 
346 static void hdac_ext_codec_link_down(struct hdac_device *codec)
347 {
348 	const char *devname = dev_name(&codec->dev);
349 	struct hdac_ext_link *hlink =
350 		snd_hdac_ext_bus_get_hlink_by_name(codec->bus, devname);
351 
352 	if (hlink)
353 		snd_hdac_ext_bus_link_put(codec->bus, hlink);
354 }
355 
356 void snd_hdac_ext_bus_link_power(struct hdac_device *codec, bool enable)
357 {
358 	struct hdac_bus *bus = codec->bus;
359 	bool oldstate = test_bit(codec->addr, &bus->codec_powered);
360 
361 	if (enable == oldstate)
362 		return;
363 
364 	snd_hdac_bus_link_power(codec, enable);
365 
366 	if (enable)
367 		hdac_ext_codec_link_up(codec);
368 	else
369 		hdac_ext_codec_link_down(codec);
370 }
371 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power);
372