GNU Linux-libre 4.19.295-gnu1
[releases.git] / sound / hda / hdac_device.c
1 /*
2  * HD-audio codec core device
3  */
4
5 #include <linux/init.h>
6 #include <linux/delay.h>
7 #include <linux/device.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/export.h>
11 #include <linux/pm_runtime.h>
12 #include <sound/hdaudio.h>
13 #include <sound/hda_regmap.h>
14 #include <sound/pcm.h>
15 #include "local.h"
16
17 static void setup_fg_nodes(struct hdac_device *codec);
18 static int get_codec_vendor_name(struct hdac_device *codec);
19
20 static void default_release(struct device *dev)
21 {
22         snd_hdac_device_exit(container_of(dev, struct hdac_device, dev));
23 }
24
25 /**
26  * snd_hdac_device_init - initialize the HD-audio codec base device
27  * @codec: device to initialize
28  * @bus: but to attach
29  * @name: device name string
30  * @addr: codec address
31  *
32  * Returns zero for success or a negative error code.
33  *
34  * This function increments the runtime PM counter and marks it active.
35  * The caller needs to turn it off appropriately later.
36  *
37  * The caller needs to set the device's release op properly by itself.
38  */
39 int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
40                          const char *name, unsigned int addr)
41 {
42         struct device *dev;
43         hda_nid_t fg;
44         int err;
45
46         dev = &codec->dev;
47         device_initialize(dev);
48         dev->parent = bus->dev;
49         dev->bus = &snd_hda_bus_type;
50         dev->release = default_release;
51         dev->groups = hdac_dev_attr_groups;
52         dev_set_name(dev, "%s", name);
53         device_enable_async_suspend(dev);
54
55         codec->bus = bus;
56         codec->addr = addr;
57         codec->type = HDA_DEV_CORE;
58         pm_runtime_set_active(&codec->dev);
59         pm_runtime_get_noresume(&codec->dev);
60         atomic_set(&codec->in_pm, 0);
61
62         err = snd_hdac_bus_add_device(bus, codec);
63         if (err < 0)
64                 goto error;
65
66         /* fill parameters */
67         codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
68                                               AC_PAR_VENDOR_ID);
69         if (codec->vendor_id == -1) {
70                 /* read again, hopefully the access method was corrected
71                  * in the last read...
72                  */
73                 codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
74                                                       AC_PAR_VENDOR_ID);
75         }
76
77         codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
78                                                  AC_PAR_SUBSYSTEM_ID);
79         codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
80                                                 AC_PAR_REV_ID);
81
82         setup_fg_nodes(codec);
83         if (!codec->afg && !codec->mfg) {
84                 dev_err(dev, "no AFG or MFG node found\n");
85                 err = -ENODEV;
86                 goto error;
87         }
88
89         fg = codec->afg ? codec->afg : codec->mfg;
90
91         err = snd_hdac_refresh_widgets(codec, false);
92         if (err < 0)
93                 goto error;
94
95         codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
96         /* reread ssid if not set by parameter */
97         if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
98                 snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
99                               &codec->subsystem_id);
100
101         err = get_codec_vendor_name(codec);
102         if (err < 0)
103                 goto error;
104
105         codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
106                                      codec->vendor_id & 0xffff);
107         if (!codec->chip_name) {
108                 err = -ENOMEM;
109                 goto error;
110         }
111
112         return 0;
113
114  error:
115         put_device(&codec->dev);
116         return err;
117 }
118 EXPORT_SYMBOL_GPL(snd_hdac_device_init);
119
120 /**
121  * snd_hdac_device_exit - clean up the HD-audio codec base device
122  * @codec: device to clean up
123  */
124 void snd_hdac_device_exit(struct hdac_device *codec)
125 {
126         pm_runtime_put_noidle(&codec->dev);
127         /* keep balance of runtime PM child_count in parent device */
128         pm_runtime_set_suspended(&codec->dev);
129         snd_hdac_bus_remove_device(codec->bus, codec);
130         kfree(codec->vendor_name);
131         kfree(codec->chip_name);
132 }
133 EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
134
135 /**
136  * snd_hdac_device_register - register the hd-audio codec base device
137  * codec: the device to register
138  */
139 int snd_hdac_device_register(struct hdac_device *codec)
140 {
141         int err;
142
143         err = device_add(&codec->dev);
144         if (err < 0)
145                 return err;
146         err = hda_widget_sysfs_init(codec);
147         if (err < 0) {
148                 device_del(&codec->dev);
149                 return err;
150         }
151
152         return 0;
153 }
154 EXPORT_SYMBOL_GPL(snd_hdac_device_register);
155
156 /**
157  * snd_hdac_device_unregister - unregister the hd-audio codec base device
158  * codec: the device to unregister
159  */
160 void snd_hdac_device_unregister(struct hdac_device *codec)
161 {
162         if (device_is_registered(&codec->dev)) {
163                 hda_widget_sysfs_exit(codec);
164                 device_del(&codec->dev);
165                 snd_hdac_bus_remove_device(codec->bus, codec);
166         }
167 }
168 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
169
170 /**
171  * snd_hdac_device_set_chip_name - set/update the codec name
172  * @codec: the HDAC device
173  * @name: name string to set
174  *
175  * Returns 0 if the name is set or updated, or a negative error code.
176  */
177 int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
178 {
179         char *newname;
180
181         if (!name)
182                 return 0;
183         newname = kstrdup(name, GFP_KERNEL);
184         if (!newname)
185                 return -ENOMEM;
186         kfree(codec->chip_name);
187         codec->chip_name = newname;
188         return 0;
189 }
190 EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
191
192 /**
193  * snd_hdac_codec_modalias - give the module alias name
194  * @codec: HDAC device
195  * @buf: string buffer to store
196  * @size: string buffer size
197  *
198  * Returns the size of string, like snprintf(), or a negative error code.
199  */
200 int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
201 {
202         return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
203                         codec->vendor_id, codec->revision_id, codec->type);
204 }
205 EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
206
207 /**
208  * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
209  *      HD-audio controller
210  * @codec: the codec object
211  * @nid: NID to encode
212  * @verb: verb to encode
213  * @parm: parameter to encode
214  *
215  * Return an encoded command verb or -1 for error.
216  */
217 unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
218                                unsigned int verb, unsigned int parm)
219 {
220         u32 val, addr;
221
222         addr = codec->addr;
223         if ((addr & ~0xf) || (nid & ~0x7f) ||
224             (verb & ~0xfff) || (parm & ~0xffff)) {
225                 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
226                         addr, nid, verb, parm);
227                 return -1;
228         }
229
230         val = addr << 28;
231         val |= (u32)nid << 20;
232         val |= verb << 8;
233         val |= parm;
234         return val;
235 }
236 EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
237
238 /**
239  * snd_hdac_exec_verb - execute an encoded verb
240  * @codec: the codec object
241  * @cmd: encoded verb to execute
242  * @flags: optional flags, pass zero for default
243  * @res: the pointer to store the result, NULL if running async
244  *
245  * Returns zero if successful, or a negative error code.
246  *
247  * This calls the exec_verb op when set in hdac_codec.  If not,
248  * call the default snd_hdac_bus_exec_verb().
249  */
250 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
251                        unsigned int flags, unsigned int *res)
252 {
253         if (codec->exec_verb)
254                 return codec->exec_verb(codec, cmd, flags, res);
255         return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
256 }
257 EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
258
259
260 /**
261  * snd_hdac_read - execute a verb
262  * @codec: the codec object
263  * @nid: NID to execute a verb
264  * @verb: verb to execute
265  * @parm: parameter for a verb
266  * @res: the pointer to store the result, NULL if running async
267  *
268  * Returns zero if successful, or a negative error code.
269  */
270 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
271                   unsigned int verb, unsigned int parm, unsigned int *res)
272 {
273         unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
274
275         return snd_hdac_exec_verb(codec, cmd, 0, res);
276 }
277 EXPORT_SYMBOL_GPL(snd_hdac_read);
278
279 /**
280  * _snd_hdac_read_parm - read a parmeter
281  *
282  * This function returns zero or an error unlike snd_hdac_read_parm().
283  */
284 int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
285                         unsigned int *res)
286 {
287         unsigned int cmd;
288
289         cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
290         return snd_hdac_regmap_read_raw(codec, cmd, res);
291 }
292 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
293
294 /**
295  * snd_hdac_read_parm_uncached - read a codec parameter without caching
296  * @codec: the codec object
297  * @nid: NID to read a parameter
298  * @parm: parameter to read
299  *
300  * Returns -1 for error.  If you need to distinguish the error more
301  * strictly, use snd_hdac_read() directly.
302  */
303 int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
304                                 int parm)
305 {
306         unsigned int cmd, val;
307
308         cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
309         if (snd_hdac_regmap_read_raw_uncached(codec, cmd, &val) < 0)
310                 return -1;
311         return val;
312 }
313 EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
314
315 /**
316  * snd_hdac_override_parm - override read-only parameters
317  * @codec: the codec object
318  * @nid: NID for the parameter
319  * @parm: the parameter to change
320  * @val: the parameter value to overwrite
321  */
322 int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
323                            unsigned int parm, unsigned int val)
324 {
325         unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
326         int err;
327
328         if (!codec->regmap)
329                 return -EINVAL;
330
331         codec->caps_overwriting = true;
332         err = snd_hdac_regmap_write_raw(codec, verb, val);
333         codec->caps_overwriting = false;
334         return err;
335 }
336 EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
337
338 /**
339  * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
340  * @codec: the codec object
341  * @nid: NID to inspect
342  * @start_id: the pointer to store the starting NID
343  *
344  * Returns the number of subtree nodes or zero if not found.
345  * This function reads parameters always without caching.
346  */
347 int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
348                            hda_nid_t *start_id)
349 {
350         unsigned int parm;
351
352         parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
353         if (parm == -1) {
354                 *start_id = 0;
355                 return 0;
356         }
357         *start_id = (parm >> 16) & 0x7fff;
358         return (int)(parm & 0x7fff);
359 }
360 EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
361
362 /*
363  * look for an AFG and MFG nodes
364  */
365 static void setup_fg_nodes(struct hdac_device *codec)
366 {
367         int i, total_nodes, function_id;
368         hda_nid_t nid;
369
370         total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
371         for (i = 0; i < total_nodes; i++, nid++) {
372                 function_id = snd_hdac_read_parm(codec, nid,
373                                                  AC_PAR_FUNCTION_TYPE);
374                 switch (function_id & 0xff) {
375                 case AC_GRP_AUDIO_FUNCTION:
376                         codec->afg = nid;
377                         codec->afg_function_id = function_id & 0xff;
378                         codec->afg_unsol = (function_id >> 8) & 1;
379                         break;
380                 case AC_GRP_MODEM_FUNCTION:
381                         codec->mfg = nid;
382                         codec->mfg_function_id = function_id & 0xff;
383                         codec->mfg_unsol = (function_id >> 8) & 1;
384                         break;
385                 default:
386                         break;
387                 }
388         }
389 }
390
391 /**
392  * snd_hdac_refresh_widgets - Reset the widget start/end nodes
393  * @codec: the codec object
394  * @sysfs: re-initialize sysfs tree, too
395  */
396 int snd_hdac_refresh_widgets(struct hdac_device *codec, bool sysfs)
397 {
398         hda_nid_t start_nid;
399         int nums, err;
400
401         nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
402         if (!start_nid || nums <= 0 || nums >= 0xff) {
403                 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
404                         codec->afg);
405                 return -EINVAL;
406         }
407
408         if (sysfs) {
409                 err = hda_widget_sysfs_reinit(codec, start_nid, nums);
410                 if (err < 0)
411                         return err;
412         }
413
414         codec->num_nodes = nums;
415         codec->start_nid = start_nid;
416         codec->end_nid = start_nid + nums;
417         return 0;
418 }
419 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
420
421 /* return CONNLIST_LEN parameter of the given widget */
422 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
423 {
424         unsigned int wcaps = get_wcaps(codec, nid);
425         unsigned int parm;
426
427         if (!(wcaps & AC_WCAP_CONN_LIST) &&
428             get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
429                 return 0;
430
431         parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
432         if (parm == -1)
433                 parm = 0;
434         return parm;
435 }
436
437 /**
438  * snd_hdac_get_connections - get a widget connection list
439  * @codec: the codec object
440  * @nid: NID
441  * @conn_list: the array to store the results, can be NULL
442  * @max_conns: the max size of the given array
443  *
444  * Returns the number of connected widgets, zero for no connection, or a
445  * negative error code.  When the number of elements don't fit with the
446  * given array size, it returns -ENOSPC.
447  *
448  * When @conn_list is NULL, it just checks the number of connections.
449  */
450 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
451                              hda_nid_t *conn_list, int max_conns)
452 {
453         unsigned int parm;
454         int i, conn_len, conns, err;
455         unsigned int shift, num_elems, mask;
456         hda_nid_t prev_nid;
457         int null_count = 0;
458
459         parm = get_num_conns(codec, nid);
460         if (!parm)
461                 return 0;
462
463         if (parm & AC_CLIST_LONG) {
464                 /* long form */
465                 shift = 16;
466                 num_elems = 2;
467         } else {
468                 /* short form */
469                 shift = 8;
470                 num_elems = 4;
471         }
472         conn_len = parm & AC_CLIST_LENGTH;
473         mask = (1 << (shift-1)) - 1;
474
475         if (!conn_len)
476                 return 0; /* no connection */
477
478         if (conn_len == 1) {
479                 /* single connection */
480                 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
481                                     &parm);
482                 if (err < 0)
483                         return err;
484                 if (conn_list)
485                         conn_list[0] = parm & mask;
486                 return 1;
487         }
488
489         /* multi connection */
490         conns = 0;
491         prev_nid = 0;
492         for (i = 0; i < conn_len; i++) {
493                 int range_val;
494                 hda_nid_t val, n;
495
496                 if (i % num_elems == 0) {
497                         err = snd_hdac_read(codec, nid,
498                                             AC_VERB_GET_CONNECT_LIST, i,
499                                             &parm);
500                         if (err < 0)
501                                 return -EIO;
502                 }
503                 range_val = !!(parm & (1 << (shift-1))); /* ranges */
504                 val = parm & mask;
505                 if (val == 0 && null_count++) {  /* no second chance */
506                         dev_dbg(&codec->dev,
507                                 "invalid CONNECT_LIST verb %x[%i]:%x\n",
508                                 nid, i, parm);
509                         return 0;
510                 }
511                 parm >>= shift;
512                 if (range_val) {
513                         /* ranges between the previous and this one */
514                         if (!prev_nid || prev_nid >= val) {
515                                 dev_warn(&codec->dev,
516                                          "invalid dep_range_val %x:%x\n",
517                                          prev_nid, val);
518                                 continue;
519                         }
520                         for (n = prev_nid + 1; n <= val; n++) {
521                                 if (conn_list) {
522                                         if (conns >= max_conns)
523                                                 return -ENOSPC;
524                                         conn_list[conns] = n;
525                                 }
526                                 conns++;
527                         }
528                 } else {
529                         if (conn_list) {
530                                 if (conns >= max_conns)
531                                         return -ENOSPC;
532                                 conn_list[conns] = val;
533                         }
534                         conns++;
535                 }
536                 prev_nid = val;
537         }
538         return conns;
539 }
540 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
541
542 #ifdef CONFIG_PM
543 /**
544  * snd_hdac_power_up - power up the codec
545  * @codec: the codec object
546  *
547  * This function calls the runtime PM helper to power up the given codec.
548  * Unlike snd_hdac_power_up_pm(), you should call this only for the code
549  * path that isn't included in PM path.  Otherwise it gets stuck.
550  *
551  * Returns zero if successful, or a negative error code.
552  */
553 int snd_hdac_power_up(struct hdac_device *codec)
554 {
555         return pm_runtime_get_sync(&codec->dev);
556 }
557 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
558
559 /**
560  * snd_hdac_power_down - power down the codec
561  * @codec: the codec object
562  *
563  * Returns zero if successful, or a negative error code.
564  */
565 int snd_hdac_power_down(struct hdac_device *codec)
566 {
567         struct device *dev = &codec->dev;
568
569         pm_runtime_mark_last_busy(dev);
570         return pm_runtime_put_autosuspend(dev);
571 }
572 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
573
574 /**
575  * snd_hdac_power_up_pm - power up the codec
576  * @codec: the codec object
577  *
578  * This function can be called in a recursive code path like init code
579  * which may be called by PM suspend/resume again.  OTOH, if a power-up
580  * call must wake up the sleeper (e.g. in a kctl callback), use
581  * snd_hdac_power_up() instead.
582  *
583  * Returns zero if successful, or a negative error code.
584  */
585 int snd_hdac_power_up_pm(struct hdac_device *codec)
586 {
587         if (!atomic_inc_not_zero(&codec->in_pm))
588                 return snd_hdac_power_up(codec);
589         return 0;
590 }
591 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
592
593 /* like snd_hdac_power_up_pm(), but only increment the pm count when
594  * already powered up.  Returns -1 if not powered up, 1 if incremented
595  * or 0 if unchanged.  Only used in hdac_regmap.c
596  */
597 int snd_hdac_keep_power_up(struct hdac_device *codec)
598 {
599         if (!atomic_inc_not_zero(&codec->in_pm)) {
600                 int ret = pm_runtime_get_if_in_use(&codec->dev);
601                 if (!ret)
602                         return -1;
603                 if (ret < 0)
604                         return 0;
605         }
606         return 1;
607 }
608
609 /**
610  * snd_hdac_power_down_pm - power down the codec
611  * @codec: the codec object
612  *
613  * Like snd_hdac_power_up_pm(), this function is used in a recursive
614  * code path like init code which may be called by PM suspend/resume again.
615  *
616  * Returns zero if successful, or a negative error code.
617  */
618 int snd_hdac_power_down_pm(struct hdac_device *codec)
619 {
620         if (atomic_dec_if_positive(&codec->in_pm) < 0)
621                 return snd_hdac_power_down(codec);
622         return 0;
623 }
624 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
625 #endif
626
627 /**
628  * snd_hdac_link_power - Enable/disable the link power for a codec
629  * @codec: the codec object
630  * @bool: enable or disable the link power
631  */
632 int snd_hdac_link_power(struct hdac_device *codec, bool enable)
633 {
634         if  (!codec->link_power_control)
635                 return 0;
636
637         if  (codec->bus->ops->link_power)
638                 return codec->bus->ops->link_power(codec->bus, enable);
639         else
640                 return -EINVAL;
641 }
642 EXPORT_SYMBOL_GPL(snd_hdac_link_power);
643
644 /* codec vendor labels */
645 struct hda_vendor_id {
646         unsigned int id;
647         const char *name;
648 };
649
650 static struct hda_vendor_id hda_vendor_ids[] = {
651         { 0x1002, "ATI" },
652         { 0x1013, "Cirrus Logic" },
653         { 0x1057, "Motorola" },
654         { 0x1095, "Silicon Image" },
655         { 0x10de, "Nvidia" },
656         { 0x10ec, "Realtek" },
657         { 0x1102, "Creative" },
658         { 0x1106, "VIA" },
659         { 0x111d, "IDT" },
660         { 0x11c1, "LSI" },
661         { 0x11d4, "Analog Devices" },
662         { 0x13f6, "C-Media" },
663         { 0x14f1, "Conexant" },
664         { 0x17e8, "Chrontel" },
665         { 0x1854, "LG" },
666         { 0x1aec, "Wolfson Microelectronics" },
667         { 0x1af4, "QEMU" },
668         { 0x434d, "C-Media" },
669         { 0x8086, "Intel" },
670         { 0x8384, "SigmaTel" },
671         {} /* terminator */
672 };
673
674 /* store the codec vendor name */
675 static int get_codec_vendor_name(struct hdac_device *codec)
676 {
677         const struct hda_vendor_id *c;
678         u16 vendor_id = codec->vendor_id >> 16;
679
680         for (c = hda_vendor_ids; c->id; c++) {
681                 if (c->id == vendor_id) {
682                         codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
683                         return codec->vendor_name ? 0 : -ENOMEM;
684                 }
685         }
686
687         codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
688         return codec->vendor_name ? 0 : -ENOMEM;
689 }
690
691 /*
692  * stream formats
693  */
694 struct hda_rate_tbl {
695         unsigned int hz;
696         unsigned int alsa_bits;
697         unsigned int hda_fmt;
698 };
699
700 /* rate = base * mult / div */
701 #define HDA_RATE(base, mult, div) \
702         (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
703          (((div) - 1) << AC_FMT_DIV_SHIFT))
704
705 static struct hda_rate_tbl rate_bits[] = {
706         /* rate in Hz, ALSA rate bitmask, HDA format value */
707
708         /* autodetected value used in snd_hda_query_supported_pcm */
709         { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
710         { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
711         { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
712         { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
713         { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
714         { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
715         { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
716         { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
717         { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
718         { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
719         { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
720 #define AC_PAR_PCM_RATE_BITS    11
721         /* up to bits 10, 384kHZ isn't supported properly */
722
723         /* not autodetected value */
724         { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
725
726         { 0 } /* terminator */
727 };
728
729 /**
730  * snd_hdac_calc_stream_format - calculate the format bitset
731  * @rate: the sample rate
732  * @channels: the number of channels
733  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
734  * @maxbps: the max. bps
735  * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
736  *
737  * Calculate the format bitset from the given rate, channels and th PCM format.
738  *
739  * Return zero if invalid.
740  */
741 unsigned int snd_hdac_calc_stream_format(unsigned int rate,
742                                          unsigned int channels,
743                                          snd_pcm_format_t format,
744                                          unsigned int maxbps,
745                                          unsigned short spdif_ctls)
746 {
747         int i;
748         unsigned int val = 0;
749
750         for (i = 0; rate_bits[i].hz; i++)
751                 if (rate_bits[i].hz == rate) {
752                         val = rate_bits[i].hda_fmt;
753                         break;
754                 }
755         if (!rate_bits[i].hz)
756                 return 0;
757
758         if (channels == 0 || channels > 8)
759                 return 0;
760         val |= channels - 1;
761
762         switch (snd_pcm_format_width(format)) {
763         case 8:
764                 val |= AC_FMT_BITS_8;
765                 break;
766         case 16:
767                 val |= AC_FMT_BITS_16;
768                 break;
769         case 20:
770         case 24:
771         case 32:
772                 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
773                         val |= AC_FMT_BITS_32;
774                 else if (maxbps >= 24)
775                         val |= AC_FMT_BITS_24;
776                 else
777                         val |= AC_FMT_BITS_20;
778                 break;
779         default:
780                 return 0;
781         }
782
783         if (spdif_ctls & AC_DIG1_NONAUDIO)
784                 val |= AC_FMT_TYPE_NON_PCM;
785
786         return val;
787 }
788 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
789
790 static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
791 {
792         unsigned int val = 0;
793
794         if (nid != codec->afg &&
795             (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
796                 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
797         if (!val || val == -1)
798                 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
799         if (!val || val == -1)
800                 return 0;
801         return val;
802 }
803
804 static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
805 {
806         unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
807
808         if (!streams || streams == -1)
809                 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
810         if (!streams || streams == -1)
811                 return 0;
812         return streams;
813 }
814
815 /**
816  * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
817  * @codec: the codec object
818  * @nid: NID to query
819  * @ratesp: the pointer to store the detected rate bitflags
820  * @formatsp: the pointer to store the detected formats
821  * @bpsp: the pointer to store the detected format widths
822  *
823  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
824  * or @bsps argument is ignored.
825  *
826  * Returns 0 if successful, otherwise a negative error code.
827  */
828 int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
829                                  u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
830 {
831         unsigned int i, val, wcaps;
832
833         wcaps = get_wcaps(codec, nid);
834         val = query_pcm_param(codec, nid);
835
836         if (ratesp) {
837                 u32 rates = 0;
838                 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
839                         if (val & (1 << i))
840                                 rates |= rate_bits[i].alsa_bits;
841                 }
842                 if (rates == 0) {
843                         dev_err(&codec->dev,
844                                 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
845                                 nid, val,
846                                 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
847                         return -EIO;
848                 }
849                 *ratesp = rates;
850         }
851
852         if (formatsp || bpsp) {
853                 u64 formats = 0;
854                 unsigned int streams, bps;
855
856                 streams = query_stream_param(codec, nid);
857                 if (!streams)
858                         return -EIO;
859
860                 bps = 0;
861                 if (streams & AC_SUPFMT_PCM) {
862                         if (val & AC_SUPPCM_BITS_8) {
863                                 formats |= SNDRV_PCM_FMTBIT_U8;
864                                 bps = 8;
865                         }
866                         if (val & AC_SUPPCM_BITS_16) {
867                                 formats |= SNDRV_PCM_FMTBIT_S16_LE;
868                                 bps = 16;
869                         }
870                         if (wcaps & AC_WCAP_DIGITAL) {
871                                 if (val & AC_SUPPCM_BITS_32)
872                                         formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
873                                 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
874                                         formats |= SNDRV_PCM_FMTBIT_S32_LE;
875                                 if (val & AC_SUPPCM_BITS_24)
876                                         bps = 24;
877                                 else if (val & AC_SUPPCM_BITS_20)
878                                         bps = 20;
879                         } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
880                                           AC_SUPPCM_BITS_32)) {
881                                 formats |= SNDRV_PCM_FMTBIT_S32_LE;
882                                 if (val & AC_SUPPCM_BITS_32)
883                                         bps = 32;
884                                 else if (val & AC_SUPPCM_BITS_24)
885                                         bps = 24;
886                                 else if (val & AC_SUPPCM_BITS_20)
887                                         bps = 20;
888                         }
889                 }
890 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
891                 if (streams & AC_SUPFMT_FLOAT32) {
892                         formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
893                         if (!bps)
894                                 bps = 32;
895                 }
896 #endif
897                 if (streams == AC_SUPFMT_AC3) {
898                         /* should be exclusive */
899                         /* temporary hack: we have still no proper support
900                          * for the direct AC3 stream...
901                          */
902                         formats |= SNDRV_PCM_FMTBIT_U8;
903                         bps = 8;
904                 }
905                 if (formats == 0) {
906                         dev_err(&codec->dev,
907                                 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
908                                 nid, val,
909                                 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
910                                 streams);
911                         return -EIO;
912                 }
913                 if (formatsp)
914                         *formatsp = formats;
915                 if (bpsp)
916                         *bpsp = bps;
917         }
918
919         return 0;
920 }
921 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
922
923 /**
924  * snd_hdac_is_supported_format - Check the validity of the format
925  * @codec: the codec object
926  * @nid: NID to check
927  * @format: the HD-audio format value to check
928  *
929  * Check whether the given node supports the format value.
930  *
931  * Returns true if supported, false if not.
932  */
933 bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
934                                   unsigned int format)
935 {
936         int i;
937         unsigned int val = 0, rate, stream;
938
939         val = query_pcm_param(codec, nid);
940         if (!val)
941                 return false;
942
943         rate = format & 0xff00;
944         for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
945                 if (rate_bits[i].hda_fmt == rate) {
946                         if (val & (1 << i))
947                                 break;
948                         return false;
949                 }
950         if (i >= AC_PAR_PCM_RATE_BITS)
951                 return false;
952
953         stream = query_stream_param(codec, nid);
954         if (!stream)
955                 return false;
956
957         if (stream & AC_SUPFMT_PCM) {
958                 switch (format & 0xf0) {
959                 case 0x00:
960                         if (!(val & AC_SUPPCM_BITS_8))
961                                 return false;
962                         break;
963                 case 0x10:
964                         if (!(val & AC_SUPPCM_BITS_16))
965                                 return false;
966                         break;
967                 case 0x20:
968                         if (!(val & AC_SUPPCM_BITS_20))
969                                 return false;
970                         break;
971                 case 0x30:
972                         if (!(val & AC_SUPPCM_BITS_24))
973                                 return false;
974                         break;
975                 case 0x40:
976                         if (!(val & AC_SUPPCM_BITS_32))
977                                 return false;
978                         break;
979                 default:
980                         return false;
981                 }
982         } else {
983                 /* FIXME: check for float32 and AC3? */
984         }
985
986         return true;
987 }
988 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
989
990 static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
991                         int flags, unsigned int verb, unsigned int parm)
992 {
993         unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
994         unsigned int res;
995
996         if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
997                 return -1;
998
999         return res;
1000 }
1001
1002 static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
1003                         int flags, unsigned int verb, unsigned int parm)
1004 {
1005         unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1006
1007         return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
1008 }
1009
1010 /**
1011  * snd_hdac_codec_read - send a command and get the response
1012  * @hdac: the HDAC device
1013  * @nid: NID to send the command
1014  * @flags: optional bit flags
1015  * @verb: the verb to send
1016  * @parm: the parameter for the verb
1017  *
1018  * Send a single command and read the corresponding response.
1019  *
1020  * Returns the obtained response value, or -1 for an error.
1021  */
1022 int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1023                         int flags, unsigned int verb, unsigned int parm)
1024 {
1025         return codec_read(hdac, nid, flags, verb, parm);
1026 }
1027 EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1028
1029 /**
1030  * snd_hdac_codec_write - send a single command without waiting for response
1031  * @hdac: the HDAC device
1032  * @nid: NID to send the command
1033  * @flags: optional bit flags
1034  * @verb: the verb to send
1035  * @parm: the parameter for the verb
1036  *
1037  * Send a single command without waiting for response.
1038  *
1039  * Returns 0 if successful, or a negative error code.
1040  */
1041 int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1042                         int flags, unsigned int verb, unsigned int parm)
1043 {
1044         return codec_write(hdac, nid, flags, verb, parm);
1045 }
1046 EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1047
1048 /**
1049  * snd_hdac_check_power_state - check whether the actual power state matches
1050  * with the target state
1051  *
1052  * @hdac: the HDAC device
1053  * @nid: NID to send the command
1054  * @target_state: target state to check for
1055  *
1056  * Return true if state matches, false if not
1057  */
1058 bool snd_hdac_check_power_state(struct hdac_device *hdac,
1059                 hda_nid_t nid, unsigned int target_state)
1060 {
1061         unsigned int state = codec_read(hdac, nid, 0,
1062                                 AC_VERB_GET_POWER_STATE, 0);
1063
1064         if (state & AC_PWRST_ERROR)
1065                 return true;
1066         state = (state >> 4) & 0x0f;
1067         return (state == target_state);
1068 }
1069 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);
1070 /**
1071  * snd_hdac_sync_power_state - wait until actual power state matches
1072  * with the target state
1073  *
1074  * @hdac: the HDAC device
1075  * @nid: NID to send the command
1076  * @target_state: target state to check for
1077  *
1078  * Return power state or PS_ERROR if codec rejects GET verb.
1079  */
1080 unsigned int snd_hdac_sync_power_state(struct hdac_device *codec,
1081                         hda_nid_t nid, unsigned int power_state)
1082 {
1083         unsigned long end_time = jiffies + msecs_to_jiffies(500);
1084         unsigned int state, actual_state, count;
1085
1086         for (count = 0; count < 500; count++) {
1087                 state = snd_hdac_codec_read(codec, nid, 0,
1088                                 AC_VERB_GET_POWER_STATE, 0);
1089                 if (state & AC_PWRST_ERROR) {
1090                         msleep(20);
1091                         break;
1092                 }
1093                 actual_state = (state >> 4) & 0x0f;
1094                 if (actual_state == power_state)
1095                         break;
1096                 if (time_after_eq(jiffies, end_time))
1097                         break;
1098                 /* wait until the codec reachs to the target state */
1099                 msleep(1);
1100         }
1101         return state;
1102 }
1103 EXPORT_SYMBOL_GPL(snd_hdac_sync_power_state);