GNU Linux-libre 4.4.288-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         }
165 }
166 EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
167
168 /**
169  * snd_hdac_device_set_chip_name - set/update the codec name
170  * @codec: the HDAC device
171  * @name: name string to set
172  *
173  * Returns 0 if the name is set or updated, or a negative error code.
174  */
175 int snd_hdac_device_set_chip_name(struct hdac_device *codec, const char *name)
176 {
177         char *newname;
178
179         if (!name)
180                 return 0;
181         newname = kstrdup(name, GFP_KERNEL);
182         if (!newname)
183                 return -ENOMEM;
184         kfree(codec->chip_name);
185         codec->chip_name = newname;
186         return 0;
187 }
188 EXPORT_SYMBOL_GPL(snd_hdac_device_set_chip_name);
189
190 /**
191  * snd_hdac_codec_modalias - give the module alias name
192  * @codec: HDAC device
193  * @buf: string buffer to store
194  * @size: string buffer size
195  *
196  * Returns the size of string, like snprintf(), or a negative error code.
197  */
198 int snd_hdac_codec_modalias(struct hdac_device *codec, char *buf, size_t size)
199 {
200         return snprintf(buf, size, "hdaudio:v%08Xr%08Xa%02X\n",
201                         codec->vendor_id, codec->revision_id, codec->type);
202 }
203 EXPORT_SYMBOL_GPL(snd_hdac_codec_modalias);
204
205 /**
206  * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
207  *      HD-audio controller
208  * @codec: the codec object
209  * @nid: NID to encode
210  * @verb: verb to encode
211  * @parm: parameter to encode
212  *
213  * Return an encoded command verb or -1 for error.
214  */
215 unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
216                                unsigned int verb, unsigned int parm)
217 {
218         u32 val, addr;
219
220         addr = codec->addr;
221         if ((addr & ~0xf) || (nid & ~0x7f) ||
222             (verb & ~0xfff) || (parm & ~0xffff)) {
223                 dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
224                         addr, nid, verb, parm);
225                 return -1;
226         }
227
228         val = addr << 28;
229         val |= (u32)nid << 20;
230         val |= verb << 8;
231         val |= parm;
232         return val;
233 }
234 EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
235
236 /**
237  * snd_hdac_exec_verb - execute an encoded verb
238  * @codec: the codec object
239  * @cmd: encoded verb to execute
240  * @flags: optional flags, pass zero for default
241  * @res: the pointer to store the result, NULL if running async
242  *
243  * Returns zero if successful, or a negative error code.
244  *
245  * This calls the exec_verb op when set in hdac_codec.  If not,
246  * call the default snd_hdac_bus_exec_verb().
247  */
248 int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
249                        unsigned int flags, unsigned int *res)
250 {
251         if (codec->exec_verb)
252                 return codec->exec_verb(codec, cmd, flags, res);
253         return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
254 }
255 EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
256
257
258 /**
259  * snd_hdac_read - execute a verb
260  * @codec: the codec object
261  * @nid: NID to execute a verb
262  * @verb: verb to execute
263  * @parm: parameter for a verb
264  * @res: the pointer to store the result, NULL if running async
265  *
266  * Returns zero if successful, or a negative error code.
267  */
268 int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
269                   unsigned int verb, unsigned int parm, unsigned int *res)
270 {
271         unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
272
273         return snd_hdac_exec_verb(codec, cmd, 0, res);
274 }
275 EXPORT_SYMBOL_GPL(snd_hdac_read);
276
277 /**
278  * _snd_hdac_read_parm - read a parmeter
279  *
280  * This function returns zero or an error unlike snd_hdac_read_parm().
281  */
282 int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
283                         unsigned int *res)
284 {
285         unsigned int cmd;
286
287         cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
288         return snd_hdac_regmap_read_raw(codec, cmd, res);
289 }
290 EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
291
292 /**
293  * snd_hdac_read_parm_uncached - read a codec parameter without caching
294  * @codec: the codec object
295  * @nid: NID to read a parameter
296  * @parm: parameter to read
297  *
298  * Returns -1 for error.  If you need to distinguish the error more
299  * strictly, use snd_hdac_read() directly.
300  */
301 int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
302                                 int parm)
303 {
304         int val;
305
306         if (codec->regmap)
307                 regcache_cache_bypass(codec->regmap, true);
308         val = snd_hdac_read_parm(codec, nid, parm);
309         if (codec->regmap)
310                 regcache_cache_bypass(codec->regmap, false);
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  */
395 int snd_hdac_refresh_widgets(struct hdac_device *codec)
396 {
397         hda_nid_t start_nid;
398         int nums;
399
400         nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
401         if (!start_nid || nums <= 0 || nums >= 0xff) {
402                 dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
403                         codec->afg);
404                 return -EINVAL;
405         }
406
407         codec->num_nodes = nums;
408         codec->start_nid = start_nid;
409         codec->end_nid = start_nid + nums;
410         return 0;
411 }
412 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
413
414 /**
415  * snd_hdac_refresh_widget_sysfs - Reset the codec widgets and reinit the
416  * codec sysfs
417  * @codec: the codec object
418  *
419  * first we need to remove sysfs, then refresh widgets and lastly
420  * recreate it
421  */
422 int snd_hdac_refresh_widget_sysfs(struct hdac_device *codec)
423 {
424         int ret;
425
426         if (device_is_registered(&codec->dev))
427                 hda_widget_sysfs_exit(codec);
428         ret = snd_hdac_refresh_widgets(codec);
429         if (ret) {
430                 dev_err(&codec->dev, "failed to refresh widget: %d\n", ret);
431                 return ret;
432         }
433         if (device_is_registered(&codec->dev)) {
434                 ret = hda_widget_sysfs_init(codec);
435                 if (ret) {
436                         dev_err(&codec->dev, "failed to init sysfs: %d\n", ret);
437                         return ret;
438                 }
439         }
440         return ret;
441 }
442 EXPORT_SYMBOL_GPL(snd_hdac_refresh_widget_sysfs);
443
444 /* return CONNLIST_LEN parameter of the given widget */
445 static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
446 {
447         unsigned int wcaps = get_wcaps(codec, nid);
448         unsigned int parm;
449
450         if (!(wcaps & AC_WCAP_CONN_LIST) &&
451             get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
452                 return 0;
453
454         parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
455         if (parm == -1)
456                 parm = 0;
457         return parm;
458 }
459
460 /**
461  * snd_hdac_get_connections - get a widget connection list
462  * @codec: the codec object
463  * @nid: NID
464  * @conn_list: the array to store the results, can be NULL
465  * @max_conns: the max size of the given array
466  *
467  * Returns the number of connected widgets, zero for no connection, or a
468  * negative error code.  When the number of elements don't fit with the
469  * given array size, it returns -ENOSPC.
470  *
471  * When @conn_list is NULL, it just checks the number of connections.
472  */
473 int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
474                              hda_nid_t *conn_list, int max_conns)
475 {
476         unsigned int parm;
477         int i, conn_len, conns, err;
478         unsigned int shift, num_elems, mask;
479         hda_nid_t prev_nid;
480         int null_count = 0;
481
482         parm = get_num_conns(codec, nid);
483         if (!parm)
484                 return 0;
485
486         if (parm & AC_CLIST_LONG) {
487                 /* long form */
488                 shift = 16;
489                 num_elems = 2;
490         } else {
491                 /* short form */
492                 shift = 8;
493                 num_elems = 4;
494         }
495         conn_len = parm & AC_CLIST_LENGTH;
496         mask = (1 << (shift-1)) - 1;
497
498         if (!conn_len)
499                 return 0; /* no connection */
500
501         if (conn_len == 1) {
502                 /* single connection */
503                 err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
504                                     &parm);
505                 if (err < 0)
506                         return err;
507                 if (conn_list)
508                         conn_list[0] = parm & mask;
509                 return 1;
510         }
511
512         /* multi connection */
513         conns = 0;
514         prev_nid = 0;
515         for (i = 0; i < conn_len; i++) {
516                 int range_val;
517                 hda_nid_t val, n;
518
519                 if (i % num_elems == 0) {
520                         err = snd_hdac_read(codec, nid,
521                                             AC_VERB_GET_CONNECT_LIST, i,
522                                             &parm);
523                         if (err < 0)
524                                 return -EIO;
525                 }
526                 range_val = !!(parm & (1 << (shift-1))); /* ranges */
527                 val = parm & mask;
528                 if (val == 0 && null_count++) {  /* no second chance */
529                         dev_dbg(&codec->dev,
530                                 "invalid CONNECT_LIST verb %x[%i]:%x\n",
531                                 nid, i, parm);
532                         return 0;
533                 }
534                 parm >>= shift;
535                 if (range_val) {
536                         /* ranges between the previous and this one */
537                         if (!prev_nid || prev_nid >= val) {
538                                 dev_warn(&codec->dev,
539                                          "invalid dep_range_val %x:%x\n",
540                                          prev_nid, val);
541                                 continue;
542                         }
543                         for (n = prev_nid + 1; n <= val; n++) {
544                                 if (conn_list) {
545                                         if (conns >= max_conns)
546                                                 return -ENOSPC;
547                                         conn_list[conns] = n;
548                                 }
549                                 conns++;
550                         }
551                 } else {
552                         if (conn_list) {
553                                 if (conns >= max_conns)
554                                         return -ENOSPC;
555                                 conn_list[conns] = val;
556                         }
557                         conns++;
558                 }
559                 prev_nid = val;
560         }
561         return conns;
562 }
563 EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
564
565 #ifdef CONFIG_PM
566 /**
567  * snd_hdac_power_up - power up the codec
568  * @codec: the codec object
569  *
570  * This function calls the runtime PM helper to power up the given codec.
571  * Unlike snd_hdac_power_up_pm(), you should call this only for the code
572  * path that isn't included in PM path.  Otherwise it gets stuck.
573  *
574  * Returns zero if successful, or a negative error code.
575  */
576 int snd_hdac_power_up(struct hdac_device *codec)
577 {
578         return pm_runtime_get_sync(&codec->dev);
579 }
580 EXPORT_SYMBOL_GPL(snd_hdac_power_up);
581
582 /**
583  * snd_hdac_power_down - power down the codec
584  * @codec: the codec object
585  *
586  * Returns zero if successful, or a negative error code.
587  */
588 int snd_hdac_power_down(struct hdac_device *codec)
589 {
590         struct device *dev = &codec->dev;
591
592         pm_runtime_mark_last_busy(dev);
593         return pm_runtime_put_autosuspend(dev);
594 }
595 EXPORT_SYMBOL_GPL(snd_hdac_power_down);
596
597 /**
598  * snd_hdac_power_up_pm - power up the codec
599  * @codec: the codec object
600  *
601  * This function can be called in a recursive code path like init code
602  * which may be called by PM suspend/resume again.  OTOH, if a power-up
603  * call must wake up the sleeper (e.g. in a kctl callback), use
604  * snd_hdac_power_up() instead.
605  *
606  * Returns zero if successful, or a negative error code.
607  */
608 int snd_hdac_power_up_pm(struct hdac_device *codec)
609 {
610         if (!atomic_inc_not_zero(&codec->in_pm))
611                 return snd_hdac_power_up(codec);
612         return 0;
613 }
614 EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
615
616 /**
617  * snd_hdac_power_down_pm - power down the codec
618  * @codec: the codec object
619  *
620  * Like snd_hdac_power_up_pm(), this function is used in a recursive
621  * code path like init code which may be called by PM suspend/resume again.
622  *
623  * Returns zero if successful, or a negative error code.
624  */
625 int snd_hdac_power_down_pm(struct hdac_device *codec)
626 {
627         if (atomic_dec_if_positive(&codec->in_pm) < 0)
628                 return snd_hdac_power_down(codec);
629         return 0;
630 }
631 EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
632 #endif
633
634 /**
635  * snd_hdac_link_power - Enable/disable the link power for a codec
636  * @codec: the codec object
637  * @bool: enable or disable the link power
638  */
639 int snd_hdac_link_power(struct hdac_device *codec, bool enable)
640 {
641         if  (!codec->link_power_control)
642                 return 0;
643
644         if  (codec->bus->ops->link_power)
645                 return codec->bus->ops->link_power(codec->bus, enable);
646         else
647                 return -EINVAL;
648 }
649 EXPORT_SYMBOL_GPL(snd_hdac_link_power);
650
651 /* codec vendor labels */
652 struct hda_vendor_id {
653         unsigned int id;
654         const char *name;
655 };
656
657 static struct hda_vendor_id hda_vendor_ids[] = {
658         { 0x1002, "ATI" },
659         { 0x1013, "Cirrus Logic" },
660         { 0x1057, "Motorola" },
661         { 0x1095, "Silicon Image" },
662         { 0x10de, "Nvidia" },
663         { 0x10ec, "Realtek" },
664         { 0x1102, "Creative" },
665         { 0x1106, "VIA" },
666         { 0x111d, "IDT" },
667         { 0x11c1, "LSI" },
668         { 0x11d4, "Analog Devices" },
669         { 0x13f6, "C-Media" },
670         { 0x14f1, "Conexant" },
671         { 0x17e8, "Chrontel" },
672         { 0x1854, "LG" },
673         { 0x1aec, "Wolfson Microelectronics" },
674         { 0x1af4, "QEMU" },
675         { 0x434d, "C-Media" },
676         { 0x8086, "Intel" },
677         { 0x8384, "SigmaTel" },
678         {} /* terminator */
679 };
680
681 /* store the codec vendor name */
682 static int get_codec_vendor_name(struct hdac_device *codec)
683 {
684         const struct hda_vendor_id *c;
685         u16 vendor_id = codec->vendor_id >> 16;
686
687         for (c = hda_vendor_ids; c->id; c++) {
688                 if (c->id == vendor_id) {
689                         codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
690                         return codec->vendor_name ? 0 : -ENOMEM;
691                 }
692         }
693
694         codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
695         return codec->vendor_name ? 0 : -ENOMEM;
696 }
697
698 /*
699  * stream formats
700  */
701 struct hda_rate_tbl {
702         unsigned int hz;
703         unsigned int alsa_bits;
704         unsigned int hda_fmt;
705 };
706
707 /* rate = base * mult / div */
708 #define HDA_RATE(base, mult, div) \
709         (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
710          (((div) - 1) << AC_FMT_DIV_SHIFT))
711
712 static struct hda_rate_tbl rate_bits[] = {
713         /* rate in Hz, ALSA rate bitmask, HDA format value */
714
715         /* autodetected value used in snd_hda_query_supported_pcm */
716         { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
717         { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
718         { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
719         { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
720         { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
721         { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
722         { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
723         { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
724         { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
725         { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
726         { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
727 #define AC_PAR_PCM_RATE_BITS    11
728         /* up to bits 10, 384kHZ isn't supported properly */
729
730         /* not autodetected value */
731         { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
732
733         { 0 } /* terminator */
734 };
735
736 /**
737  * snd_hdac_calc_stream_format - calculate the format bitset
738  * @rate: the sample rate
739  * @channels: the number of channels
740  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
741  * @maxbps: the max. bps
742  * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
743  *
744  * Calculate the format bitset from the given rate, channels and th PCM format.
745  *
746  * Return zero if invalid.
747  */
748 unsigned int snd_hdac_calc_stream_format(unsigned int rate,
749                                          unsigned int channels,
750                                          unsigned int format,
751                                          unsigned int maxbps,
752                                          unsigned short spdif_ctls)
753 {
754         int i;
755         unsigned int val = 0;
756
757         for (i = 0; rate_bits[i].hz; i++)
758                 if (rate_bits[i].hz == rate) {
759                         val = rate_bits[i].hda_fmt;
760                         break;
761                 }
762         if (!rate_bits[i].hz)
763                 return 0;
764
765         if (channels == 0 || channels > 8)
766                 return 0;
767         val |= channels - 1;
768
769         switch (snd_pcm_format_width(format)) {
770         case 8:
771                 val |= AC_FMT_BITS_8;
772                 break;
773         case 16:
774                 val |= AC_FMT_BITS_16;
775                 break;
776         case 20:
777         case 24:
778         case 32:
779                 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
780                         val |= AC_FMT_BITS_32;
781                 else if (maxbps >= 24)
782                         val |= AC_FMT_BITS_24;
783                 else
784                         val |= AC_FMT_BITS_20;
785                 break;
786         default:
787                 return 0;
788         }
789
790         if (spdif_ctls & AC_DIG1_NONAUDIO)
791                 val |= AC_FMT_TYPE_NON_PCM;
792
793         return val;
794 }
795 EXPORT_SYMBOL_GPL(snd_hdac_calc_stream_format);
796
797 static unsigned int query_pcm_param(struct hdac_device *codec, hda_nid_t nid)
798 {
799         unsigned int val = 0;
800
801         if (nid != codec->afg &&
802             (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
803                 val = snd_hdac_read_parm(codec, nid, AC_PAR_PCM);
804         if (!val || val == -1)
805                 val = snd_hdac_read_parm(codec, codec->afg, AC_PAR_PCM);
806         if (!val || val == -1)
807                 return 0;
808         return val;
809 }
810
811 static unsigned int query_stream_param(struct hdac_device *codec, hda_nid_t nid)
812 {
813         unsigned int streams = snd_hdac_read_parm(codec, nid, AC_PAR_STREAM);
814
815         if (!streams || streams == -1)
816                 streams = snd_hdac_read_parm(codec, codec->afg, AC_PAR_STREAM);
817         if (!streams || streams == -1)
818                 return 0;
819         return streams;
820 }
821
822 /**
823  * snd_hdac_query_supported_pcm - query the supported PCM rates and formats
824  * @codec: the codec object
825  * @nid: NID to query
826  * @ratesp: the pointer to store the detected rate bitflags
827  * @formatsp: the pointer to store the detected formats
828  * @bpsp: the pointer to store the detected format widths
829  *
830  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
831  * or @bsps argument is ignored.
832  *
833  * Returns 0 if successful, otherwise a negative error code.
834  */
835 int snd_hdac_query_supported_pcm(struct hdac_device *codec, hda_nid_t nid,
836                                  u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
837 {
838         unsigned int i, val, wcaps;
839
840         wcaps = get_wcaps(codec, nid);
841         val = query_pcm_param(codec, nid);
842
843         if (ratesp) {
844                 u32 rates = 0;
845                 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
846                         if (val & (1 << i))
847                                 rates |= rate_bits[i].alsa_bits;
848                 }
849                 if (rates == 0) {
850                         dev_err(&codec->dev,
851                                 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
852                                 nid, val,
853                                 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
854                         return -EIO;
855                 }
856                 *ratesp = rates;
857         }
858
859         if (formatsp || bpsp) {
860                 u64 formats = 0;
861                 unsigned int streams, bps;
862
863                 streams = query_stream_param(codec, nid);
864                 if (!streams)
865                         return -EIO;
866
867                 bps = 0;
868                 if (streams & AC_SUPFMT_PCM) {
869                         if (val & AC_SUPPCM_BITS_8) {
870                                 formats |= SNDRV_PCM_FMTBIT_U8;
871                                 bps = 8;
872                         }
873                         if (val & AC_SUPPCM_BITS_16) {
874                                 formats |= SNDRV_PCM_FMTBIT_S16_LE;
875                                 bps = 16;
876                         }
877                         if (wcaps & AC_WCAP_DIGITAL) {
878                                 if (val & AC_SUPPCM_BITS_32)
879                                         formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
880                                 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
881                                         formats |= SNDRV_PCM_FMTBIT_S32_LE;
882                                 if (val & AC_SUPPCM_BITS_24)
883                                         bps = 24;
884                                 else if (val & AC_SUPPCM_BITS_20)
885                                         bps = 20;
886                         } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
887                                           AC_SUPPCM_BITS_32)) {
888                                 formats |= SNDRV_PCM_FMTBIT_S32_LE;
889                                 if (val & AC_SUPPCM_BITS_32)
890                                         bps = 32;
891                                 else if (val & AC_SUPPCM_BITS_24)
892                                         bps = 24;
893                                 else if (val & AC_SUPPCM_BITS_20)
894                                         bps = 20;
895                         }
896                 }
897 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
898                 if (streams & AC_SUPFMT_FLOAT32) {
899                         formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
900                         if (!bps)
901                                 bps = 32;
902                 }
903 #endif
904                 if (streams == AC_SUPFMT_AC3) {
905                         /* should be exclusive */
906                         /* temporary hack: we have still no proper support
907                          * for the direct AC3 stream...
908                          */
909                         formats |= SNDRV_PCM_FMTBIT_U8;
910                         bps = 8;
911                 }
912                 if (formats == 0) {
913                         dev_err(&codec->dev,
914                                 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
915                                 nid, val,
916                                 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
917                                 streams);
918                         return -EIO;
919                 }
920                 if (formatsp)
921                         *formatsp = formats;
922                 if (bpsp)
923                         *bpsp = bps;
924         }
925
926         return 0;
927 }
928 EXPORT_SYMBOL_GPL(snd_hdac_query_supported_pcm);
929
930 /**
931  * snd_hdac_is_supported_format - Check the validity of the format
932  * @codec: the codec object
933  * @nid: NID to check
934  * @format: the HD-audio format value to check
935  *
936  * Check whether the given node supports the format value.
937  *
938  * Returns true if supported, false if not.
939  */
940 bool snd_hdac_is_supported_format(struct hdac_device *codec, hda_nid_t nid,
941                                   unsigned int format)
942 {
943         int i;
944         unsigned int val = 0, rate, stream;
945
946         val = query_pcm_param(codec, nid);
947         if (!val)
948                 return false;
949
950         rate = format & 0xff00;
951         for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
952                 if (rate_bits[i].hda_fmt == rate) {
953                         if (val & (1 << i))
954                                 break;
955                         return false;
956                 }
957         if (i >= AC_PAR_PCM_RATE_BITS)
958                 return false;
959
960         stream = query_stream_param(codec, nid);
961         if (!stream)
962                 return false;
963
964         if (stream & AC_SUPFMT_PCM) {
965                 switch (format & 0xf0) {
966                 case 0x00:
967                         if (!(val & AC_SUPPCM_BITS_8))
968                                 return false;
969                         break;
970                 case 0x10:
971                         if (!(val & AC_SUPPCM_BITS_16))
972                                 return false;
973                         break;
974                 case 0x20:
975                         if (!(val & AC_SUPPCM_BITS_20))
976                                 return false;
977                         break;
978                 case 0x30:
979                         if (!(val & AC_SUPPCM_BITS_24))
980                                 return false;
981                         break;
982                 case 0x40:
983                         if (!(val & AC_SUPPCM_BITS_32))
984                                 return false;
985                         break;
986                 default:
987                         return false;
988                 }
989         } else {
990                 /* FIXME: check for float32 and AC3? */
991         }
992
993         return true;
994 }
995 EXPORT_SYMBOL_GPL(snd_hdac_is_supported_format);
996
997 static unsigned int codec_read(struct hdac_device *hdac, hda_nid_t nid,
998                         int flags, unsigned int verb, unsigned int parm)
999 {
1000         unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1001         unsigned int res;
1002
1003         if (snd_hdac_exec_verb(hdac, cmd, flags, &res))
1004                 return -1;
1005
1006         return res;
1007 }
1008
1009 static int codec_write(struct hdac_device *hdac, hda_nid_t nid,
1010                         int flags, unsigned int verb, unsigned int parm)
1011 {
1012         unsigned int cmd = snd_hdac_make_cmd(hdac, nid, verb, parm);
1013
1014         return snd_hdac_exec_verb(hdac, cmd, flags, NULL);
1015 }
1016
1017 /**
1018  * snd_hdac_codec_read - send a command and get the response
1019  * @hdac: the HDAC device
1020  * @nid: NID to send the command
1021  * @flags: optional bit flags
1022  * @verb: the verb to send
1023  * @parm: the parameter for the verb
1024  *
1025  * Send a single command and read the corresponding response.
1026  *
1027  * Returns the obtained response value, or -1 for an error.
1028  */
1029 int snd_hdac_codec_read(struct hdac_device *hdac, hda_nid_t nid,
1030                         int flags, unsigned int verb, unsigned int parm)
1031 {
1032         return codec_read(hdac, nid, flags, verb, parm);
1033 }
1034 EXPORT_SYMBOL_GPL(snd_hdac_codec_read);
1035
1036 /**
1037  * snd_hdac_codec_write - send a single command without waiting for response
1038  * @hdac: the HDAC device
1039  * @nid: NID to send the command
1040  * @flags: optional bit flags
1041  * @verb: the verb to send
1042  * @parm: the parameter for the verb
1043  *
1044  * Send a single command without waiting for response.
1045  *
1046  * Returns 0 if successful, or a negative error code.
1047  */
1048 int snd_hdac_codec_write(struct hdac_device *hdac, hda_nid_t nid,
1049                         int flags, unsigned int verb, unsigned int parm)
1050 {
1051         return codec_write(hdac, nid, flags, verb, parm);
1052 }
1053 EXPORT_SYMBOL_GPL(snd_hdac_codec_write);
1054
1055 /**
1056  * snd_hdac_check_power_state - check whether the actual power state matches
1057  * with the target state
1058  *
1059  * @hdac: the HDAC device
1060  * @nid: NID to send the command
1061  * @target_state: target state to check for
1062  *
1063  * Return true if state matches, false if not
1064  */
1065 bool snd_hdac_check_power_state(struct hdac_device *hdac,
1066                 hda_nid_t nid, unsigned int target_state)
1067 {
1068         unsigned int state = codec_read(hdac, nid, 0,
1069                                 AC_VERB_GET_POWER_STATE, 0);
1070
1071         if (state & AC_PWRST_ERROR)
1072                 return true;
1073         state = (state >> 4) & 0x0f;
1074         return (state == target_state);
1075 }
1076 EXPORT_SYMBOL_GPL(snd_hdac_check_power_state);