hdac_device.c 28 KB

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