hdac_device.c 27 KB

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