hdac_device.c 27 KB

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