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