GNU Linux-libre 4.19.264-gnu1
[releases.git] / sound / hda / ext / hdac_ext_controller.c
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,
52                                  AZX_PPCTL_GPROCEN, AZX_PPCTL_GPROCEN);
53         else
54                 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
55                                  AZX_PPCTL_GPROCEN, 0);
56 }
57 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_enable);
58
59 /**
60  * snd_hdac_ext_bus_ppcap_int_enable - ppcap interrupt enable/disable
61  * @ebus: HD-audio extended core bus
62  * @enable: flag to enable/disable interrupt
63  */
64 void snd_hdac_ext_bus_ppcap_int_enable(struct hdac_bus *bus, bool enable)
65 {
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,
74                                  AZX_PPCTL_PIE, AZX_PPCTL_PIE);
75         else
76                 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL,
77                                  AZX_PPCTL_PIE, 0);
78 }
79 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_ppcap_int_enable);
80
81 /*
82  * Multilink helpers - these helpers are useful for dealing with HDA
83  * new multilink capability
84  */
85
86 /**
87  * snd_hdac_ext_bus_get_ml_capabilities - get multilink capability
88  * @ebus: HD-audio extended core bus
89  *
90  * This will parse all links and read the mlink capabilities and add them
91  * in hlink_list of extended hdac bus
92  * Note: this will be freed on bus exit by driver
93  */
94 int snd_hdac_ext_bus_get_ml_capabilities(struct hdac_bus *bus)
95 {
96         int idx;
97         u32 link_count;
98         struct hdac_ext_link *hlink;
99
100         link_count = readl(bus->mlcap + AZX_REG_ML_MLCD) + 1;
101
102         dev_dbg(bus->dev, "In %s Link count: %d\n", __func__, link_count);
103
104         for (idx = 0; idx < link_count; idx++) {
105                 hlink  = kzalloc(sizeof(*hlink), GFP_KERNEL);
106                 if (!hlink)
107                         return -ENOMEM;
108                 hlink->index = idx;
109                 hlink->bus = bus;
110                 hlink->ml_addr = bus->mlcap + AZX_ML_BASE +
111                                         (AZX_ML_INTERVAL * idx);
112                 hlink->lcaps  = readl(hlink->ml_addr + AZX_REG_ML_LCAP);
113                 hlink->lsdiid = readw(hlink->ml_addr + AZX_REG_ML_LSDIID);
114
115                 /* since link in On, update the ref */
116                 hlink->ref_count = 1;
117
118                 list_add_tail(&hlink->list, &bus->hlink_list);
119         }
120
121         return 0;
122 }
123 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_ml_capabilities);
124
125 /**
126  * snd_hdac_link_free_all- free hdac extended link objects
127  *
128  * @ebus: HD-audio ext core bus
129  */
130
131 void snd_hdac_link_free_all(struct hdac_bus *bus)
132 {
133         struct hdac_ext_link *l;
134
135         while (!list_empty(&bus->hlink_list)) {
136                 l = list_first_entry(&bus->hlink_list, struct hdac_ext_link, list);
137                 list_del(&l->list);
138                 kfree(l);
139         }
140 }
141 EXPORT_SYMBOL_GPL(snd_hdac_link_free_all);
142
143 /**
144  * snd_hdac_ext_bus_get_link_index - get link based on codec name
145  * @ebus: HD-audio extended core bus
146  * @codec_name: codec name
147  */
148 struct hdac_ext_link *snd_hdac_ext_bus_get_link(struct hdac_bus *bus,
149                                                  const char *codec_name)
150 {
151         int i;
152         struct hdac_ext_link *hlink = NULL;
153         int bus_idx, addr;
154
155         if (sscanf(codec_name, "ehdaudio%dD%d", &bus_idx, &addr) != 2)
156                 return NULL;
157         if (bus->idx != bus_idx)
158                 return NULL;
159         if (addr < 0 || addr > 31)
160                 return NULL;
161
162         list_for_each_entry(hlink, &bus->hlink_list, list) {
163                 for (i = 0; i < HDA_MAX_CODECS; i++) {
164                         if (hlink->lsdiid & (0x1 << addr))
165                                 return hlink;
166                 }
167         }
168
169         return NULL;
170 }
171 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_get_link);
172
173 static int check_hdac_link_power_active(struct hdac_ext_link *link, bool enable)
174 {
175         int timeout;
176         u32 val;
177         int mask = (1 << AZX_MLCTL_CPA_SHIFT);
178
179         udelay(3);
180         timeout = 150;
181
182         do {
183                 val = readl(link->ml_addr + AZX_REG_ML_LCTL);
184                 if (enable) {
185                         if (((val & mask) >> AZX_MLCTL_CPA_SHIFT))
186                                 return 0;
187                 } else {
188                         if (!((val & mask) >> AZX_MLCTL_CPA_SHIFT))
189                                 return 0;
190                 }
191                 udelay(3);
192         } while (--timeout);
193
194         return -EIO;
195 }
196
197 /**
198  * snd_hdac_ext_bus_link_power_up -power up hda link
199  * @link: HD-audio extended link
200  */
201 int snd_hdac_ext_bus_link_power_up(struct hdac_ext_link *link)
202 {
203         snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL,
204                          AZX_MLCTL_SPA, AZX_MLCTL_SPA);
205
206         return check_hdac_link_power_active(link, true);
207 }
208 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up);
209
210 /**
211  * snd_hdac_ext_bus_link_power_down -power down hda link
212  * @link: HD-audio extended link
213  */
214 int snd_hdac_ext_bus_link_power_down(struct hdac_ext_link *link)
215 {
216         snd_hdac_updatel(link->ml_addr, AZX_REG_ML_LCTL, AZX_MLCTL_SPA, 0);
217
218         return check_hdac_link_power_active(link, false);
219 }
220 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down);
221
222 /**
223  * snd_hdac_ext_bus_link_power_up_all -power up all hda link
224  * @ebus: HD-audio extended bus
225  */
226 int snd_hdac_ext_bus_link_power_up_all(struct hdac_bus *bus)
227 {
228         struct hdac_ext_link *hlink = NULL;
229         int ret;
230
231         list_for_each_entry(hlink, &bus->hlink_list, list) {
232                 snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
233                                  AZX_MLCTL_SPA, AZX_MLCTL_SPA);
234                 ret = check_hdac_link_power_active(hlink, true);
235                 if (ret < 0)
236                         return ret;
237         }
238
239         return 0;
240 }
241 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_up_all);
242
243 /**
244  * snd_hdac_ext_bus_link_power_down_all -power down all hda link
245  * @ebus: HD-audio extended bus
246  */
247 int snd_hdac_ext_bus_link_power_down_all(struct hdac_bus *bus)
248 {
249         struct hdac_ext_link *hlink = NULL;
250         int ret;
251
252         list_for_each_entry(hlink, &bus->hlink_list, list) {
253                 snd_hdac_updatel(hlink->ml_addr, AZX_REG_ML_LCTL,
254                                  AZX_MLCTL_SPA, 0);
255                 ret = check_hdac_link_power_active(hlink, false);
256                 if (ret < 0)
257                         return ret;
258         }
259
260         return 0;
261 }
262 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_power_down_all);
263
264 int snd_hdac_ext_bus_link_get(struct hdac_bus *bus,
265                                 struct hdac_ext_link *link)
266 {
267         unsigned long codec_mask;
268         int ret = 0;
269
270         mutex_lock(&bus->lock);
271
272         /*
273          * if we move from 0 to 1, count will be 1 so power up this link
274          * as well, also check the dma status and trigger that
275          */
276         if (++link->ref_count == 1) {
277                 if (!bus->cmd_dma_state) {
278                         snd_hdac_bus_init_cmd_io(bus);
279                         bus->cmd_dma_state = true;
280                 }
281
282                 ret = snd_hdac_ext_bus_link_power_up(link);
283
284                 /*
285                  *  wait for 521usec for codec to report status
286                  *  HDA spec section 4.3 - Codec Discovery
287                  */
288                 udelay(521);
289                 codec_mask = snd_hdac_chip_readw(bus, STATESTS);
290                 dev_dbg(bus->dev, "codec_mask = 0x%lx\n", codec_mask);
291                 snd_hdac_chip_writew(bus, STATESTS, codec_mask);
292                 if (!bus->codec_mask)
293                         bus->codec_mask = codec_mask;
294         }
295
296         mutex_unlock(&bus->lock);
297         return ret;
298 }
299 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_get);
300
301 int snd_hdac_ext_bus_link_put(struct hdac_bus *bus,
302                                 struct hdac_ext_link *link)
303 {
304         int ret = 0;
305         struct hdac_ext_link *hlink;
306         bool link_up = false;
307
308         mutex_lock(&bus->lock);
309
310         /*
311          * if we move from 1 to 0, count will be 0
312          * so power down this link as well
313          */
314         if (--link->ref_count == 0) {
315                 ret = snd_hdac_ext_bus_link_power_down(link);
316
317                 /*
318                  * now check if all links are off, if so turn off
319                  * cmd dma as well
320                  */
321                 list_for_each_entry(hlink, &bus->hlink_list, list) {
322                         if (hlink->ref_count) {
323                                 link_up = true;
324                                 break;
325                         }
326                 }
327
328                 if (!link_up) {
329                         snd_hdac_bus_stop_cmd_io(bus);
330                         bus->cmd_dma_state = false;
331                 }
332         }
333
334         mutex_unlock(&bus->lock);
335         return ret;
336 }
337 EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_link_put);