1 /*
2  *  hdac-ext-controller.c - HD-audio extended controller functions.
3  *
4  *  Copyright (C) 2014-2015 Intel Corp
5  *  Author: Jeeja KP <jeeja.kp@intel.com>
6  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; version 2 of the License.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  General Public License for more details.
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  * maximum HDAC capablities we should parse to avoid endless looping:
27  * currently we have 4 extended caps, so this is future proof for now.
28  * extend when this limit is seen meeting in real HW
29  */
30 #define HDAC_MAX_CAPS 10
31 
32 /*
33  * processing pipe helpers - these helpers are useful for dealing with HDA
34  * new capability of processing pipelines
35  */
36 
37 /**
38  * snd_hdac_ext_bus_ppcap_enable - enable/disable processing pipe capability
39  * @ebus: HD-audio extended core bus
40  * @enable: flag to turn on/off the capability
41  */
42 void snd_hdac_ext_bus_ppcap_enable(struct hdac_bus *bus, bool enable)
43 {
44 
45 	if (!bus->ppcap) {
46 		dev_err(bus->dev, "Address of PP capability is NULL");
47 		return;
48 	}
49 
50 	if (enable)
51 		snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, 0, AZX_PPCTL_GPROCEN);
52 	else
53 		snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, AZX_PPCTL_GPROCEN, 0);
54 }
55 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_enable);
56 
57 /**
58  * snd_hdac_ext_bus_ppcap_int_enable - ppcap interrupt enable/disable
59  * @ebus: HD-audio extended core bus
60  * @enable: flag to enable/disable interrupt
61  */
62 void snd_hdac_ext_bus_ppcap_int_enable(struct hdac_bus *bus, bool enable)
63 {
64 
65 	if (!bus->ppcap) {
66 		dev_err(bus->dev, "Address of PP capability is NULL\n");
67 		return;
68 	}
69 
70 	if (enable)
71 		snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, 0, AZX_PPCTL_PIE);
72 	else
73 		snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, AZX_PPCTL_PIE, 0);
74 }
75 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_int_enable);
76 
77 /*
78  * Multilink helpers - these helpers are useful for dealing with HDA
79  * new multilink capability
80  */
81 
82 /**
83  * snd_hdac_ext_bus_get_ml_capabilities - get multilink capability
84  * @ebus: HD-audio extended core bus
85  *
86  * This will parse all links and read the mlink capabilities and add them
87  * in hlink_list of extended hdac bus
88  * Note: this will be freed on bus exit by driver
89  */
90 int snd_hdac_ext_bus_get_ml_capabilities(struct hdac_bus *bus)
91 {
92 	int idx;
93 	u32 link_count;
94 	struct hdac_ext_link *hlink;
95 
96 	link_count = readl(bus->mlcap + AZX_REG_ML_MLCD) + 1;
97 
98 	dev_dbg(bus->dev, "In %s Link count: %d\n", __func__, link_count);
99 
100 	for (idx = 0; idx < link_count; idx++) {
101 		hlink  = kzalloc(sizeof(*hlink), GFP_KERNEL);
102 		if (!hlink)
103 			return -ENOMEM;
104 		hlink->index = idx;
105 		hlink->bus = bus;
106 		hlink->ml_addr = bus->mlcap + AZX_ML_BASE +
107 					(AZX_ML_INTERVAL * idx);
108 		hlink->lcaps  = readl(hlink->ml_addr + AZX_REG_ML_LCAP);
109 		hlink->lsdiid = readw(hlink->ml_addr + AZX_REG_ML_LSDIID);
110 
111 		/* since link in On, update the ref */
112 		hlink->ref_count = 1;
113 
114 		list_add_tail(&hlink->list, &bus->hlink_list);
115 	}
116 
117 	return 0;
118 }
119 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
120 
121 /**
122  * snd_hdac_link_free_all- free hdac extended link objects
123  *
124  * @ebus: HD-audio ext core bus
125  */
126 
127 void snd_hdac_link_free_all(struct hdac_bus *bus)
128 {
129 	struct hdac_ext_link *l;
130 
131 	while (!list_empty(&bus->hlink_list)) {
132 		l = list_first_entry(&bus->hlink_list, struct hdac_ext_link, list);
133 		list_del(&l->list);
134 		kfree(l);
135 	}
136 }
137 EXPORT_SYMBOL_GPL(snd_hdac_link_free_all);
138 
139 /**
140  * snd_hdac_ext_bus_get_link_index - get link based on codec name
141  * @ebus: HD-audio extended core bus
142  * @codec_name: codec name
143  */
144 struct hdac_ext_link *snd_hdac_ext_bus_get_link(struct hdac_bus *bus,
145 						 const char *codec_name)
146 {
147 	int i;
148 	struct hdac_ext_link *hlink = NULL;
149 	int bus_idx, addr;
150 
151 	if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
152 		return NULL;
153 	if (bus->idx != bus_idx)
154 		return NULL;
155 
156 	list_for_each_entry(hlink, &bus->hlink_list, list) {
157 		for (i = 0; i < HDA_MAX_CODECS; i++) {
158 			if (hlink->lsdiid & (0x1 << addr))
159 				return hlink;
160 		}
161 	}
162 
163 	return NULL;
164 }
165 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_link);
166 
167 static int check_hdac_link_power_active(struct hdac_ext_link *link, bool enable)
168 {
169 	int timeout;
170 	u32 val;
171 	int mask = (1 << AZX_MLCTL_CPA_SHIFT);
172 
173 	udelay(3);
174 	timeout = 150;
175 
176 	do {
177 		val = readl(link->ml_addr + AZX_REG_ML_LCTL);
178 		if (enable) {
179 			if (((val & mask) >> AZX_MLCTL_CPA_SHIFT))
180 				return 0;
181 		} else {
182 			if (!((val & mask) >> AZX_MLCTL_CPA_SHIFT))
183 				return 0;
184 		}
185 		udelay(3);
186 	} while (--timeout);
187 
188 	return -EIO;
189 }
190 
191 /**
192  * snd_hdac_ext_bus_link_power_up -power up hda link
193  * @link: HD-audio extended link
194  */
195 int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *link)
196 {
197 	snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, 0, AZX_MLCTL_SPA);
198 
199 	return check_hdac_link_power_active(link, true);
200 }
201 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
202 
203 /**
204  * snd_hdac_ext_bus_link_power_down -power down hda link
205  * @link: HD-audio extended link
206  */
207 int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *link)
208 {
209 	snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
210 
211 	return check_hdac_link_power_active(link, false);
212 }
213 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
214 
215 /**
216  * snd_hdac_ext_bus_link_power_up_all -power up all hda link
217  * @ebus: HD-audio extended bus
218  */
219 int snd_hdac_ext_bus_link_power_up_all(struct hdac_bus *bus)
220 {
221 	struct hdac_ext_link *hlink = NULL;
222 	int ret;
223 
224 	list_for_each_entry(hlink, &bus->hlink_list, list) {
225 		snd_hdac_updatel(hlink->ml_addr,
226 				AZX_REG_ML_LCTL, 0, AZX_MLCTL_SPA);
227 		ret = check_hdac_link_power_active(hlink, true);
228 		if (ret < 0)
229 			return ret;
230 	}
231 
232 	return 0;
233 }
234 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
235 
236 /**
237  * snd_hdac_ext_bus_link_power_down_all -power down all hda link
238  * @ebus: HD-audio extended bus
239  */
240 int snd_hdac_ext_bus_link_power_down_all(struct hdac_bus *bus)
241 {
242 	struct hdac_ext_link *hlink = NULL;
243 	int ret;
244 
245 	list_for_each_entry(hlink, &bus->hlink_list, list) {
246 		snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
247 		ret = check_hdac_link_power_active(hlink, false);
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 *link)
258 {
259 	int ret = 0;
260 
261 	mutex_lock(&bus->lock);
262 
263 	/*
264 	 * if we move from 0 to 1, count will be 1 so power up this link
265 	 * as well, also check the dma status and trigger that
266 	 */
267 	if (++link->ref_count == 1) {
268 		if (!bus->cmd_dma_state) {
269 			snd_hdac_bus_init_cmd_io(bus);
270 			bus->cmd_dma_state = true;
271 		}
272 
273 		ret = snd_hdac_ext_bus_link_power_up(link);
274 
275 		/*
276 		 *  wait for 521usec for codec to report status
277 		 *  HDA spec section 4.3 - Codec Discovery
278 		 */
279 		udelay(521);
280 		bus->codec_mask = snd_hdac_chip_readw(bus, STATESTS);
281 		dev_dbg(bus->dev, "codec_mask = 0x%lx\n", bus->codec_mask);
282 		snd_hdac_chip_writew(bus, STATESTS, bus->codec_mask);
283 	}
284 
285 	mutex_unlock(&bus->lock);
286 	return ret;
287 }
288 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
289 
290 int snd_hdac_ext_bus_link_put(struct hdac_bus *bus,
291 				struct hdac_ext_link *link)
292 {
293 	int ret = 0;
294 	struct hdac_ext_link *hlink;
295 	bool link_up = false;
296 
297 	mutex_lock(&bus->lock);
298 
299 	/*
300 	 * if we move from 1 to 0, count will be 0
301 	 * so power down this link as well
302 	 */
303 	if (--link->ref_count == 0) {
304 		ret = snd_hdac_ext_bus_link_power_down(link);
305 
306 		/*
307 		 * now check if all links are off, if so turn off
308 		 * cmd dma as well
309 		 */
310 		list_for_each_entry(hlink, &bus->hlink_list, list) {
311 			if (hlink->ref_count) {
312 				link_up = true;
313 				break;
314 			}
315 		}
316 
317 		if (!link_up) {
318 			snd_hdac_bus_stop_cmd_io(bus);
319 			bus->cmd_dma_state = false;
320 		}
321 	}
322 
323 	mutex_unlock(&bus->lock);
324 	return ret;
325 }
326 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);
327