hdac_device.c 27 KB

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