hdac_device.c 22 KB

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