hdac_device.c 27 KB

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