hdac_device.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 "local.h"
  13. static void setup_fg_nodes(struct hdac_device *codec);
  14. static int get_codec_vendor_name(struct hdac_device *codec);
  15. static void default_release(struct device *dev)
  16. {
  17. snd_hdac_device_exit(container_of(dev, struct hdac_device, dev));
  18. }
  19. /**
  20. * snd_hdac_device_init - initialize the HD-audio codec base device
  21. * @codec: device to initialize
  22. * @bus: but to attach
  23. * @name: device name string
  24. * @addr: codec address
  25. *
  26. * Returns zero for success or a negative error code.
  27. *
  28. * This function increments the runtime PM counter and marks it active.
  29. * The caller needs to turn it off appropriately later.
  30. *
  31. * The caller needs to set the device's release op properly by itself.
  32. */
  33. int snd_hdac_device_init(struct hdac_device *codec, struct hdac_bus *bus,
  34. const char *name, unsigned int addr)
  35. {
  36. struct device *dev;
  37. hda_nid_t fg;
  38. int err;
  39. dev = &codec->dev;
  40. device_initialize(dev);
  41. dev->parent = bus->dev;
  42. dev->bus = &snd_hda_bus_type;
  43. dev->release = default_release;
  44. dev->groups = hdac_dev_attr_groups;
  45. dev_set_name(dev, "%s", name);
  46. device_enable_async_suspend(dev);
  47. codec->bus = bus;
  48. codec->addr = addr;
  49. codec->type = HDA_DEV_CORE;
  50. pm_runtime_set_active(&codec->dev);
  51. pm_runtime_get_noresume(&codec->dev);
  52. atomic_set(&codec->in_pm, 0);
  53. err = snd_hdac_bus_add_device(bus, codec);
  54. if (err < 0)
  55. goto error;
  56. /* fill parameters */
  57. codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
  58. AC_PAR_VENDOR_ID);
  59. if (codec->vendor_id == -1) {
  60. /* read again, hopefully the access method was corrected
  61. * in the last read...
  62. */
  63. codec->vendor_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
  64. AC_PAR_VENDOR_ID);
  65. }
  66. codec->subsystem_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
  67. AC_PAR_SUBSYSTEM_ID);
  68. codec->revision_id = snd_hdac_read_parm(codec, AC_NODE_ROOT,
  69. AC_PAR_REV_ID);
  70. setup_fg_nodes(codec);
  71. if (!codec->afg && !codec->mfg) {
  72. dev_err(dev, "no AFG or MFG node found\n");
  73. err = -ENODEV;
  74. goto error;
  75. }
  76. fg = codec->afg ? codec->afg : codec->mfg;
  77. err = snd_hdac_refresh_widgets(codec);
  78. if (err < 0)
  79. goto error;
  80. codec->power_caps = snd_hdac_read_parm(codec, fg, AC_PAR_POWER_STATE);
  81. /* reread ssid if not set by parameter */
  82. if (codec->subsystem_id == -1 || codec->subsystem_id == 0)
  83. snd_hdac_read(codec, fg, AC_VERB_GET_SUBSYSTEM_ID, 0,
  84. &codec->subsystem_id);
  85. err = get_codec_vendor_name(codec);
  86. if (err < 0)
  87. goto error;
  88. codec->chip_name = kasprintf(GFP_KERNEL, "ID %x",
  89. codec->vendor_id & 0xffff);
  90. if (!codec->chip_name) {
  91. err = -ENOMEM;
  92. goto error;
  93. }
  94. return 0;
  95. error:
  96. put_device(&codec->dev);
  97. return err;
  98. }
  99. EXPORT_SYMBOL_GPL(snd_hdac_device_init);
  100. /**
  101. * snd_hdac_device_exit - clean up the HD-audio codec base device
  102. * @codec: device to clean up
  103. */
  104. void snd_hdac_device_exit(struct hdac_device *codec)
  105. {
  106. pm_runtime_put_noidle(&codec->dev);
  107. snd_hdac_bus_remove_device(codec->bus, codec);
  108. kfree(codec->vendor_name);
  109. kfree(codec->chip_name);
  110. }
  111. EXPORT_SYMBOL_GPL(snd_hdac_device_exit);
  112. /**
  113. * snd_hdac_device_register - register the hd-audio codec base device
  114. * codec: the device to register
  115. */
  116. int snd_hdac_device_register(struct hdac_device *codec)
  117. {
  118. int err;
  119. err = device_add(&codec->dev);
  120. if (err < 0)
  121. return err;
  122. err = hda_widget_sysfs_init(codec);
  123. if (err < 0) {
  124. device_del(&codec->dev);
  125. return err;
  126. }
  127. return 0;
  128. }
  129. EXPORT_SYMBOL_GPL(snd_hdac_device_register);
  130. /**
  131. * snd_hdac_device_unregister - unregister the hd-audio codec base device
  132. * codec: the device to unregister
  133. */
  134. void snd_hdac_device_unregister(struct hdac_device *codec)
  135. {
  136. if (device_is_registered(&codec->dev)) {
  137. hda_widget_sysfs_exit(codec);
  138. device_del(&codec->dev);
  139. }
  140. }
  141. EXPORT_SYMBOL_GPL(snd_hdac_device_unregister);
  142. /**
  143. * snd_hdac_make_cmd - compose a 32bit command word to be sent to the
  144. * HD-audio controller
  145. * @codec: the codec object
  146. * @nid: NID to encode
  147. * @verb: verb to encode
  148. * @parm: parameter to encode
  149. *
  150. * Return an encoded command verb or -1 for error.
  151. */
  152. unsigned int snd_hdac_make_cmd(struct hdac_device *codec, hda_nid_t nid,
  153. unsigned int verb, unsigned int parm)
  154. {
  155. u32 val, addr;
  156. addr = codec->addr;
  157. if ((addr & ~0xf) || (nid & ~0x7f) ||
  158. (verb & ~0xfff) || (parm & ~0xffff)) {
  159. dev_err(&codec->dev, "out of range cmd %x:%x:%x:%x\n",
  160. addr, nid, verb, parm);
  161. return -1;
  162. }
  163. val = addr << 28;
  164. val |= (u32)nid << 20;
  165. val |= verb << 8;
  166. val |= parm;
  167. return val;
  168. }
  169. EXPORT_SYMBOL_GPL(snd_hdac_make_cmd);
  170. /**
  171. * snd_hdac_exec_verb - execute an encoded verb
  172. * @codec: the codec object
  173. * @cmd: encoded verb to execute
  174. * @flags: optional flags, pass zero for default
  175. * @res: the pointer to store the result, NULL if running async
  176. *
  177. * Returns zero if successful, or a negative error code.
  178. *
  179. * This calls the exec_verb op when set in hdac_codec. If not,
  180. * call the default snd_hdac_bus_exec_verb().
  181. */
  182. int snd_hdac_exec_verb(struct hdac_device *codec, unsigned int cmd,
  183. unsigned int flags, unsigned int *res)
  184. {
  185. if (codec->exec_verb)
  186. return codec->exec_verb(codec, cmd, flags, res);
  187. return snd_hdac_bus_exec_verb(codec->bus, codec->addr, cmd, res);
  188. }
  189. EXPORT_SYMBOL_GPL(snd_hdac_exec_verb);
  190. /**
  191. * snd_hdac_read - execute a verb
  192. * @codec: the codec object
  193. * @nid: NID to execute a verb
  194. * @verb: verb to execute
  195. * @parm: parameter for a verb
  196. * @res: the pointer to store the result, NULL if running async
  197. *
  198. * Returns zero if successful, or a negative error code.
  199. */
  200. int snd_hdac_read(struct hdac_device *codec, hda_nid_t nid,
  201. unsigned int verb, unsigned int parm, unsigned int *res)
  202. {
  203. unsigned int cmd = snd_hdac_make_cmd(codec, nid, verb, parm);
  204. return snd_hdac_exec_verb(codec, cmd, 0, res);
  205. }
  206. EXPORT_SYMBOL_GPL(snd_hdac_read);
  207. /**
  208. * _snd_hdac_read_parm - read a parmeter
  209. *
  210. * This function returns zero or an error unlike snd_hdac_read_parm().
  211. */
  212. int _snd_hdac_read_parm(struct hdac_device *codec, hda_nid_t nid, int parm,
  213. unsigned int *res)
  214. {
  215. unsigned int cmd;
  216. cmd = snd_hdac_regmap_encode_verb(nid, AC_VERB_PARAMETERS) | parm;
  217. return snd_hdac_regmap_read_raw(codec, cmd, res);
  218. }
  219. EXPORT_SYMBOL_GPL(_snd_hdac_read_parm);
  220. /**
  221. * snd_hdac_read_parm_uncached - read a codec parameter without caching
  222. * @codec: the codec object
  223. * @nid: NID to read a parameter
  224. * @parm: parameter to read
  225. *
  226. * Returns -1 for error. If you need to distinguish the error more
  227. * strictly, use snd_hdac_read() directly.
  228. */
  229. int snd_hdac_read_parm_uncached(struct hdac_device *codec, hda_nid_t nid,
  230. int parm)
  231. {
  232. int val;
  233. if (codec->regmap)
  234. regcache_cache_bypass(codec->regmap, true);
  235. val = snd_hdac_read_parm(codec, nid, parm);
  236. if (codec->regmap)
  237. regcache_cache_bypass(codec->regmap, false);
  238. return val;
  239. }
  240. EXPORT_SYMBOL_GPL(snd_hdac_read_parm_uncached);
  241. /**
  242. * snd_hdac_override_parm - override read-only parameters
  243. * @codec: the codec object
  244. * @nid: NID for the parameter
  245. * @parm: the parameter to change
  246. * @val: the parameter value to overwrite
  247. */
  248. int snd_hdac_override_parm(struct hdac_device *codec, hda_nid_t nid,
  249. unsigned int parm, unsigned int val)
  250. {
  251. unsigned int verb = (AC_VERB_PARAMETERS << 8) | (nid << 20) | parm;
  252. int err;
  253. if (!codec->regmap)
  254. return -EINVAL;
  255. codec->caps_overwriting = true;
  256. err = snd_hdac_regmap_write_raw(codec, verb, val);
  257. codec->caps_overwriting = false;
  258. return err;
  259. }
  260. EXPORT_SYMBOL_GPL(snd_hdac_override_parm);
  261. /**
  262. * snd_hdac_get_sub_nodes - get start NID and number of subtree nodes
  263. * @codec: the codec object
  264. * @nid: NID to inspect
  265. * @start_id: the pointer to store the starting NID
  266. *
  267. * Returns the number of subtree nodes or zero if not found.
  268. * This function reads parameters always without caching.
  269. */
  270. int snd_hdac_get_sub_nodes(struct hdac_device *codec, hda_nid_t nid,
  271. hda_nid_t *start_id)
  272. {
  273. unsigned int parm;
  274. parm = snd_hdac_read_parm_uncached(codec, nid, AC_PAR_NODE_COUNT);
  275. if (parm == -1) {
  276. *start_id = 0;
  277. return 0;
  278. }
  279. *start_id = (parm >> 16) & 0x7fff;
  280. return (int)(parm & 0x7fff);
  281. }
  282. EXPORT_SYMBOL_GPL(snd_hdac_get_sub_nodes);
  283. /*
  284. * look for an AFG and MFG nodes
  285. */
  286. static void setup_fg_nodes(struct hdac_device *codec)
  287. {
  288. int i, total_nodes, function_id;
  289. hda_nid_t nid;
  290. total_nodes = snd_hdac_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  291. for (i = 0; i < total_nodes; i++, nid++) {
  292. function_id = snd_hdac_read_parm(codec, nid,
  293. AC_PAR_FUNCTION_TYPE);
  294. switch (function_id & 0xff) {
  295. case AC_GRP_AUDIO_FUNCTION:
  296. codec->afg = nid;
  297. codec->afg_function_id = function_id & 0xff;
  298. codec->afg_unsol = (function_id >> 8) & 1;
  299. break;
  300. case AC_GRP_MODEM_FUNCTION:
  301. codec->mfg = nid;
  302. codec->mfg_function_id = function_id & 0xff;
  303. codec->mfg_unsol = (function_id >> 8) & 1;
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. }
  310. /**
  311. * snd_hdac_refresh_widgets - Reset the widget start/end nodes
  312. * @codec: the codec object
  313. */
  314. int snd_hdac_refresh_widgets(struct hdac_device *codec)
  315. {
  316. hda_nid_t start_nid;
  317. int nums;
  318. nums = snd_hdac_get_sub_nodes(codec, codec->afg, &start_nid);
  319. if (!start_nid || nums <= 0 || nums >= 0xff) {
  320. dev_err(&codec->dev, "cannot read sub nodes for FG 0x%02x\n",
  321. codec->afg);
  322. return -EINVAL;
  323. }
  324. codec->num_nodes = nums;
  325. codec->start_nid = start_nid;
  326. codec->end_nid = start_nid + nums;
  327. return 0;
  328. }
  329. EXPORT_SYMBOL_GPL(snd_hdac_refresh_widgets);
  330. /* return CONNLIST_LEN parameter of the given widget */
  331. static unsigned int get_num_conns(struct hdac_device *codec, hda_nid_t nid)
  332. {
  333. unsigned int wcaps = get_wcaps(codec, nid);
  334. unsigned int parm;
  335. if (!(wcaps & AC_WCAP_CONN_LIST) &&
  336. get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
  337. return 0;
  338. parm = snd_hdac_read_parm(codec, nid, AC_PAR_CONNLIST_LEN);
  339. if (parm == -1)
  340. parm = 0;
  341. return parm;
  342. }
  343. /**
  344. * snd_hdac_get_connections - get a widget connection list
  345. * @codec: the codec object
  346. * @nid: NID
  347. * @conn_list: the array to store the results, can be NULL
  348. * @max_conns: the max size of the given array
  349. *
  350. * Returns the number of connected widgets, zero for no connection, or a
  351. * negative error code. When the number of elements don't fit with the
  352. * given array size, it returns -ENOSPC.
  353. *
  354. * When @conn_list is NULL, it just checks the number of connections.
  355. */
  356. int snd_hdac_get_connections(struct hdac_device *codec, hda_nid_t nid,
  357. hda_nid_t *conn_list, int max_conns)
  358. {
  359. unsigned int parm;
  360. int i, conn_len, conns, err;
  361. unsigned int shift, num_elems, mask;
  362. hda_nid_t prev_nid;
  363. int null_count = 0;
  364. parm = get_num_conns(codec, nid);
  365. if (!parm)
  366. return 0;
  367. if (parm & AC_CLIST_LONG) {
  368. /* long form */
  369. shift = 16;
  370. num_elems = 2;
  371. } else {
  372. /* short form */
  373. shift = 8;
  374. num_elems = 4;
  375. }
  376. conn_len = parm & AC_CLIST_LENGTH;
  377. mask = (1 << (shift-1)) - 1;
  378. if (!conn_len)
  379. return 0; /* no connection */
  380. if (conn_len == 1) {
  381. /* single connection */
  382. err = snd_hdac_read(codec, nid, AC_VERB_GET_CONNECT_LIST, 0,
  383. &parm);
  384. if (err < 0)
  385. return err;
  386. if (conn_list)
  387. conn_list[0] = parm & mask;
  388. return 1;
  389. }
  390. /* multi connection */
  391. conns = 0;
  392. prev_nid = 0;
  393. for (i = 0; i < conn_len; i++) {
  394. int range_val;
  395. hda_nid_t val, n;
  396. if (i % num_elems == 0) {
  397. err = snd_hdac_read(codec, nid,
  398. AC_VERB_GET_CONNECT_LIST, i,
  399. &parm);
  400. if (err < 0)
  401. return -EIO;
  402. }
  403. range_val = !!(parm & (1 << (shift-1))); /* ranges */
  404. val = parm & mask;
  405. if (val == 0 && null_count++) { /* no second chance */
  406. dev_dbg(&codec->dev,
  407. "invalid CONNECT_LIST verb %x[%i]:%x\n",
  408. nid, i, parm);
  409. return 0;
  410. }
  411. parm >>= shift;
  412. if (range_val) {
  413. /* ranges between the previous and this one */
  414. if (!prev_nid || prev_nid >= val) {
  415. dev_warn(&codec->dev,
  416. "invalid dep_range_val %x:%x\n",
  417. prev_nid, val);
  418. continue;
  419. }
  420. for (n = prev_nid + 1; n <= val; n++) {
  421. if (conn_list) {
  422. if (conns >= max_conns)
  423. return -ENOSPC;
  424. conn_list[conns] = n;
  425. }
  426. conns++;
  427. }
  428. } else {
  429. if (conn_list) {
  430. if (conns >= max_conns)
  431. return -ENOSPC;
  432. conn_list[conns] = val;
  433. }
  434. conns++;
  435. }
  436. prev_nid = val;
  437. }
  438. return conns;
  439. }
  440. EXPORT_SYMBOL_GPL(snd_hdac_get_connections);
  441. #ifdef CONFIG_PM
  442. /**
  443. * snd_hdac_power_up - power up the codec
  444. * @codec: the codec object
  445. *
  446. * This function calls the runtime PM helper to power up the given codec.
  447. * Unlike snd_hdac_power_up_pm(), you should call this only for the code
  448. * path that isn't included in PM path. Otherwise it gets stuck.
  449. */
  450. void snd_hdac_power_up(struct hdac_device *codec)
  451. {
  452. pm_runtime_get_sync(&codec->dev);
  453. }
  454. EXPORT_SYMBOL_GPL(snd_hdac_power_up);
  455. /**
  456. * snd_hdac_power_down - power down the codec
  457. * @codec: the codec object
  458. */
  459. void snd_hdac_power_down(struct hdac_device *codec)
  460. {
  461. struct device *dev = &codec->dev;
  462. pm_runtime_mark_last_busy(dev);
  463. pm_runtime_put_autosuspend(dev);
  464. }
  465. EXPORT_SYMBOL_GPL(snd_hdac_power_down);
  466. /**
  467. * snd_hdac_power_up_pm - power up the codec
  468. * @codec: the codec object
  469. *
  470. * This function can be called in a recursive code path like init code
  471. * which may be called by PM suspend/resume again. OTOH, if a power-up
  472. * call must wake up the sleeper (e.g. in a kctl callback), use
  473. * snd_hdac_power_up() instead.
  474. */
  475. void snd_hdac_power_up_pm(struct hdac_device *codec)
  476. {
  477. if (!atomic_inc_not_zero(&codec->in_pm))
  478. snd_hdac_power_up(codec);
  479. }
  480. EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
  481. /**
  482. * snd_hdac_power_down_pm - power down the codec
  483. * @codec: the codec object
  484. *
  485. * Like snd_hdac_power_up_pm(), this function is used in a recursive
  486. * code path like init code which may be called by PM suspend/resume again.
  487. */
  488. void snd_hdac_power_down_pm(struct hdac_device *codec)
  489. {
  490. if (atomic_dec_if_positive(&codec->in_pm) < 0)
  491. snd_hdac_power_down(codec);
  492. }
  493. EXPORT_SYMBOL_GPL(snd_hdac_power_down_pm);
  494. #endif
  495. /* codec vendor labels */
  496. struct hda_vendor_id {
  497. unsigned int id;
  498. const char *name;
  499. };
  500. static struct hda_vendor_id hda_vendor_ids[] = {
  501. { 0x1002, "ATI" },
  502. { 0x1013, "Cirrus Logic" },
  503. { 0x1057, "Motorola" },
  504. { 0x1095, "Silicon Image" },
  505. { 0x10de, "Nvidia" },
  506. { 0x10ec, "Realtek" },
  507. { 0x1102, "Creative" },
  508. { 0x1106, "VIA" },
  509. { 0x111d, "IDT" },
  510. { 0x11c1, "LSI" },
  511. { 0x11d4, "Analog Devices" },
  512. { 0x13f6, "C-Media" },
  513. { 0x14f1, "Conexant" },
  514. { 0x17e8, "Chrontel" },
  515. { 0x1854, "LG" },
  516. { 0x1aec, "Wolfson Microelectronics" },
  517. { 0x1af4, "QEMU" },
  518. { 0x434d, "C-Media" },
  519. { 0x8086, "Intel" },
  520. { 0x8384, "SigmaTel" },
  521. {} /* terminator */
  522. };
  523. /* store the codec vendor name */
  524. static int get_codec_vendor_name(struct hdac_device *codec)
  525. {
  526. const struct hda_vendor_id *c;
  527. u16 vendor_id = codec->vendor_id >> 16;
  528. for (c = hda_vendor_ids; c->id; c++) {
  529. if (c->id == vendor_id) {
  530. codec->vendor_name = kstrdup(c->name, GFP_KERNEL);
  531. return codec->vendor_name ? 0 : -ENOMEM;
  532. }
  533. }
  534. codec->vendor_name = kasprintf(GFP_KERNEL, "Generic %04x", vendor_id);
  535. return codec->vendor_name ? 0 : -ENOMEM;
  536. }