hdac_device.c 29 KB

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