hdac_regmap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Regmap support for HD-audio verbs
  3. *
  4. * A virtual register is translated to one or more hda verbs for write,
  5. * vice versa for read.
  6. *
  7. * A few limitations:
  8. * - Provided for not all verbs but only subset standard non-volatile verbs.
  9. * - For reading, only AC_VERB_GET_* variants can be used.
  10. * - For writing, mapped to the *corresponding* AC_VERB_SET_* variants,
  11. * so can't handle asymmetric verbs for read and write
  12. */
  13. #include <linux/slab.h>
  14. #include <linux/device.h>
  15. #include <linux/regmap.h>
  16. #include <linux/export.h>
  17. #include <linux/pm.h>
  18. #include <linux/pm_runtime.h>
  19. #include <sound/core.h>
  20. #include <sound/hdaudio.h>
  21. #include <sound/hda_regmap.h>
  22. static int codec_pm_lock(struct hdac_device *codec)
  23. {
  24. return snd_hdac_keep_power_up(codec);
  25. }
  26. static void codec_pm_unlock(struct hdac_device *codec, int lock)
  27. {
  28. if (lock == 1)
  29. snd_hdac_power_down_pm(codec);
  30. }
  31. #define get_verb(reg) (((reg) >> 8) & 0xfff)
  32. static bool hda_volatile_reg(struct device *dev, unsigned int reg)
  33. {
  34. struct hdac_device *codec = dev_to_hdac_dev(dev);
  35. unsigned int verb = get_verb(reg);
  36. switch (verb) {
  37. case AC_VERB_GET_PROC_COEF:
  38. return !codec->cache_coef;
  39. case AC_VERB_GET_COEF_INDEX:
  40. case AC_VERB_GET_PROC_STATE:
  41. case AC_VERB_GET_POWER_STATE:
  42. case AC_VERB_GET_PIN_SENSE:
  43. case AC_VERB_GET_HDMI_DIP_SIZE:
  44. case AC_VERB_GET_HDMI_ELDD:
  45. case AC_VERB_GET_HDMI_DIP_INDEX:
  46. case AC_VERB_GET_HDMI_DIP_DATA:
  47. case AC_VERB_GET_HDMI_DIP_XMIT:
  48. case AC_VERB_GET_HDMI_CP_CTRL:
  49. case AC_VERB_GET_HDMI_CHAN_SLOT:
  50. case AC_VERB_GET_DEVICE_SEL:
  51. case AC_VERB_GET_DEVICE_LIST: /* read-only volatile */
  52. return true;
  53. }
  54. return false;
  55. }
  56. static bool hda_writeable_reg(struct device *dev, unsigned int reg)
  57. {
  58. struct hdac_device *codec = dev_to_hdac_dev(dev);
  59. unsigned int verb = get_verb(reg);
  60. int i;
  61. for (i = 0; i < codec->vendor_verbs.used; i++) {
  62. unsigned int *v = snd_array_elem(&codec->vendor_verbs, i);
  63. if (verb == *v)
  64. return true;
  65. }
  66. if (codec->caps_overwriting)
  67. return true;
  68. switch (verb & 0xf00) {
  69. case AC_VERB_GET_STREAM_FORMAT:
  70. case AC_VERB_GET_AMP_GAIN_MUTE:
  71. return true;
  72. case AC_VERB_GET_PROC_COEF:
  73. return codec->cache_coef;
  74. case 0xf00:
  75. break;
  76. default:
  77. return false;
  78. }
  79. switch (verb) {
  80. case AC_VERB_GET_CONNECT_SEL:
  81. case AC_VERB_GET_SDI_SELECT:
  82. case AC_VERB_GET_PIN_WIDGET_CONTROL:
  83. case AC_VERB_GET_UNSOLICITED_RESPONSE: /* only as SET_UNSOLICITED_ENABLE */
  84. case AC_VERB_GET_BEEP_CONTROL:
  85. case AC_VERB_GET_EAPD_BTLENABLE:
  86. case AC_VERB_GET_DIGI_CONVERT_1:
  87. case AC_VERB_GET_DIGI_CONVERT_2: /* only for beep control */
  88. case AC_VERB_GET_VOLUME_KNOB_CONTROL:
  89. case AC_VERB_GET_GPIO_MASK:
  90. case AC_VERB_GET_GPIO_DIRECTION:
  91. case AC_VERB_GET_GPIO_DATA: /* not for volatile read */
  92. case AC_VERB_GET_GPIO_WAKE_MASK:
  93. case AC_VERB_GET_GPIO_UNSOLICITED_RSP_MASK:
  94. case AC_VERB_GET_GPIO_STICKY_MASK:
  95. return true;
  96. }
  97. return false;
  98. }
  99. static bool hda_readable_reg(struct device *dev, unsigned int reg)
  100. {
  101. struct hdac_device *codec = dev_to_hdac_dev(dev);
  102. unsigned int verb = get_verb(reg);
  103. if (codec->caps_overwriting)
  104. return true;
  105. switch (verb) {
  106. case AC_VERB_PARAMETERS:
  107. case AC_VERB_GET_CONNECT_LIST:
  108. case AC_VERB_GET_SUBSYSTEM_ID:
  109. return true;
  110. /* below are basically writable, but disabled for reducing unnecessary
  111. * writes at sync
  112. */
  113. case AC_VERB_GET_CONFIG_DEFAULT: /* usually just read */
  114. case AC_VERB_GET_CONV: /* managed in PCM code */
  115. case AC_VERB_GET_CVT_CHAN_COUNT: /* managed in HDMI CA code */
  116. return true;
  117. }
  118. return hda_writeable_reg(dev, reg);
  119. }
  120. /*
  121. * Stereo amp pseudo register:
  122. * for making easier to handle the stereo volume control, we provide a
  123. * fake register to deal both left and right channels by a single
  124. * (pseudo) register access. A verb consisting of SET_AMP_GAIN with
  125. * *both* SET_LEFT and SET_RIGHT bits takes a 16bit value, the lower 8bit
  126. * for the left and the upper 8bit for the right channel.
  127. */
  128. static bool is_stereo_amp_verb(unsigned int reg)
  129. {
  130. if (((reg >> 8) & 0x700) != AC_VERB_SET_AMP_GAIN_MUTE)
  131. return false;
  132. return (reg & (AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT)) ==
  133. (AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT);
  134. }
  135. /* read a pseudo stereo amp register (16bit left+right) */
  136. static int hda_reg_read_stereo_amp(struct hdac_device *codec,
  137. unsigned int reg, unsigned int *val)
  138. {
  139. unsigned int left, right;
  140. int err;
  141. reg &= ~(AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT);
  142. err = snd_hdac_exec_verb(codec, reg | AC_AMP_GET_LEFT, 0, &left);
  143. if (err < 0)
  144. return err;
  145. err = snd_hdac_exec_verb(codec, reg | AC_AMP_GET_RIGHT, 0, &right);
  146. if (err < 0)
  147. return err;
  148. *val = left | (right << 8);
  149. return 0;
  150. }
  151. /* write a pseudo stereo amp register (16bit left+right) */
  152. static int hda_reg_write_stereo_amp(struct hdac_device *codec,
  153. unsigned int reg, unsigned int val)
  154. {
  155. int err;
  156. unsigned int verb, left, right;
  157. verb = AC_VERB_SET_AMP_GAIN_MUTE << 8;
  158. if (reg & AC_AMP_GET_OUTPUT)
  159. verb |= AC_AMP_SET_OUTPUT;
  160. else
  161. verb |= AC_AMP_SET_INPUT | ((reg & 0xf) << 8);
  162. reg = (reg & ~0xfffff) | verb;
  163. left = val & 0xff;
  164. right = (val >> 8) & 0xff;
  165. if (left == right) {
  166. reg |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
  167. return snd_hdac_exec_verb(codec, reg | left, 0, NULL);
  168. }
  169. err = snd_hdac_exec_verb(codec, reg | AC_AMP_SET_LEFT | left, 0, NULL);
  170. if (err < 0)
  171. return err;
  172. err = snd_hdac_exec_verb(codec, reg | AC_AMP_SET_RIGHT | right, 0, NULL);
  173. if (err < 0)
  174. return err;
  175. return 0;
  176. }
  177. /* read a pseudo coef register (16bit) */
  178. static int hda_reg_read_coef(struct hdac_device *codec, unsigned int reg,
  179. unsigned int *val)
  180. {
  181. unsigned int verb;
  182. int err;
  183. if (!codec->cache_coef)
  184. return -EINVAL;
  185. /* LSB 8bit = coef index */
  186. verb = (reg & ~0xfff00) | (AC_VERB_SET_COEF_INDEX << 8);
  187. err = snd_hdac_exec_verb(codec, verb, 0, NULL);
  188. if (err < 0)
  189. return err;
  190. verb = (reg & ~0xfffff) | (AC_VERB_GET_COEF_INDEX << 8);
  191. return snd_hdac_exec_verb(codec, verb, 0, val);
  192. }
  193. /* write a pseudo coef register (16bit) */
  194. static int hda_reg_write_coef(struct hdac_device *codec, unsigned int reg,
  195. unsigned int val)
  196. {
  197. unsigned int verb;
  198. int err;
  199. if (!codec->cache_coef)
  200. return -EINVAL;
  201. /* LSB 8bit = coef index */
  202. verb = (reg & ~0xfff00) | (AC_VERB_SET_COEF_INDEX << 8);
  203. err = snd_hdac_exec_verb(codec, verb, 0, NULL);
  204. if (err < 0)
  205. return err;
  206. verb = (reg & ~0xfffff) | (AC_VERB_GET_COEF_INDEX << 8) |
  207. (val & 0xffff);
  208. return snd_hdac_exec_verb(codec, verb, 0, NULL);
  209. }
  210. static int hda_reg_read(void *context, unsigned int reg, unsigned int *val)
  211. {
  212. struct hdac_device *codec = context;
  213. int verb = get_verb(reg);
  214. int err;
  215. int pm_lock = 0;
  216. if (verb != AC_VERB_GET_POWER_STATE) {
  217. pm_lock = codec_pm_lock(codec);
  218. if (pm_lock < 0)
  219. return -EAGAIN;
  220. }
  221. reg |= (codec->addr << 28);
  222. if (is_stereo_amp_verb(reg)) {
  223. err = hda_reg_read_stereo_amp(codec, reg, val);
  224. goto out;
  225. }
  226. if (verb == AC_VERB_GET_PROC_COEF) {
  227. err = hda_reg_read_coef(codec, reg, val);
  228. goto out;
  229. }
  230. if ((verb & 0x700) == AC_VERB_SET_AMP_GAIN_MUTE)
  231. reg &= ~AC_AMP_FAKE_MUTE;
  232. err = snd_hdac_exec_verb(codec, reg, 0, val);
  233. if (err < 0)
  234. goto out;
  235. /* special handling for asymmetric reads */
  236. if (verb == AC_VERB_GET_POWER_STATE) {
  237. if (*val & AC_PWRST_ERROR)
  238. *val = -1;
  239. else /* take only the actual state */
  240. *val = (*val >> 4) & 0x0f;
  241. }
  242. out:
  243. codec_pm_unlock(codec, pm_lock);
  244. return err;
  245. }
  246. static int hda_reg_write(void *context, unsigned int reg, unsigned int val)
  247. {
  248. struct hdac_device *codec = context;
  249. unsigned int verb;
  250. int i, bytes, err;
  251. int pm_lock = 0;
  252. if (codec->caps_overwriting)
  253. return 0;
  254. reg &= ~0x00080000U; /* drop GET bit */
  255. reg |= (codec->addr << 28);
  256. verb = get_verb(reg);
  257. if (verb != AC_VERB_SET_POWER_STATE) {
  258. pm_lock = codec_pm_lock(codec);
  259. if (pm_lock < 0)
  260. return codec->lazy_cache ? 0 : -EAGAIN;
  261. }
  262. if (is_stereo_amp_verb(reg)) {
  263. err = hda_reg_write_stereo_amp(codec, reg, val);
  264. goto out;
  265. }
  266. if (verb == AC_VERB_SET_PROC_COEF) {
  267. err = hda_reg_write_coef(codec, reg, val);
  268. goto out;
  269. }
  270. switch (verb & 0xf00) {
  271. case AC_VERB_SET_AMP_GAIN_MUTE:
  272. if ((reg & AC_AMP_FAKE_MUTE) && (val & AC_AMP_MUTE))
  273. val = 0;
  274. verb = AC_VERB_SET_AMP_GAIN_MUTE;
  275. if (reg & AC_AMP_GET_LEFT)
  276. verb |= AC_AMP_SET_LEFT >> 8;
  277. else
  278. verb |= AC_AMP_SET_RIGHT >> 8;
  279. if (reg & AC_AMP_GET_OUTPUT) {
  280. verb |= AC_AMP_SET_OUTPUT >> 8;
  281. } else {
  282. verb |= AC_AMP_SET_INPUT >> 8;
  283. verb |= reg & 0xf;
  284. }
  285. break;
  286. }
  287. switch (verb) {
  288. case AC_VERB_SET_DIGI_CONVERT_1:
  289. bytes = 2;
  290. break;
  291. case AC_VERB_SET_CONFIG_DEFAULT_BYTES_0:
  292. bytes = 4;
  293. break;
  294. default:
  295. bytes = 1;
  296. break;
  297. }
  298. for (i = 0; i < bytes; i++) {
  299. reg &= ~0xfffff;
  300. reg |= (verb + i) << 8 | ((val >> (8 * i)) & 0xff);
  301. err = snd_hdac_exec_verb(codec, reg, 0, NULL);
  302. if (err < 0)
  303. goto out;
  304. }
  305. out:
  306. codec_pm_unlock(codec, pm_lock);
  307. return err;
  308. }
  309. static const struct regmap_config hda_regmap_cfg = {
  310. .name = "hdaudio",
  311. .reg_bits = 32,
  312. .val_bits = 32,
  313. .max_register = 0xfffffff,
  314. .writeable_reg = hda_writeable_reg,
  315. .readable_reg = hda_readable_reg,
  316. .volatile_reg = hda_volatile_reg,
  317. .cache_type = REGCACHE_RBTREE,
  318. .reg_read = hda_reg_read,
  319. .reg_write = hda_reg_write,
  320. .use_single_rw = true,
  321. };
  322. /**
  323. * snd_hdac_regmap_init - Initialize regmap for HDA register accesses
  324. * @codec: the codec object
  325. *
  326. * Returns zero for success or a negative error code.
  327. */
  328. int snd_hdac_regmap_init(struct hdac_device *codec)
  329. {
  330. struct regmap *regmap;
  331. regmap = regmap_init(&codec->dev, NULL, codec, &hda_regmap_cfg);
  332. if (IS_ERR(regmap))
  333. return PTR_ERR(regmap);
  334. codec->regmap = regmap;
  335. snd_array_init(&codec->vendor_verbs, sizeof(unsigned int), 8);
  336. return 0;
  337. }
  338. EXPORT_SYMBOL_GPL(snd_hdac_regmap_init);
  339. /**
  340. * snd_hdac_regmap_init - Release the regmap from HDA codec
  341. * @codec: the codec object
  342. */
  343. void snd_hdac_regmap_exit(struct hdac_device *codec)
  344. {
  345. if (codec->regmap) {
  346. regmap_exit(codec->regmap);
  347. codec->regmap = NULL;
  348. snd_array_free(&codec->vendor_verbs);
  349. }
  350. }
  351. EXPORT_SYMBOL_GPL(snd_hdac_regmap_exit);
  352. /**
  353. * snd_hdac_regmap_add_vendor_verb - add a vendor-specific verb to regmap
  354. * @codec: the codec object
  355. * @verb: verb to allow accessing via regmap
  356. *
  357. * Returns zero for success or a negative error code.
  358. */
  359. int snd_hdac_regmap_add_vendor_verb(struct hdac_device *codec,
  360. unsigned int verb)
  361. {
  362. unsigned int *p = snd_array_new(&codec->vendor_verbs);
  363. if (!p)
  364. return -ENOMEM;
  365. *p = verb | 0x800; /* set GET bit */
  366. return 0;
  367. }
  368. EXPORT_SYMBOL_GPL(snd_hdac_regmap_add_vendor_verb);
  369. /*
  370. * helper functions
  371. */
  372. /* write a pseudo-register value (w/o power sequence) */
  373. static int reg_raw_write(struct hdac_device *codec, unsigned int reg,
  374. unsigned int val)
  375. {
  376. if (!codec->regmap)
  377. return hda_reg_write(codec, reg, val);
  378. else
  379. return regmap_write(codec->regmap, reg, val);
  380. }
  381. /**
  382. * snd_hdac_regmap_write_raw - write a pseudo register with power mgmt
  383. * @codec: the codec object
  384. * @reg: pseudo register
  385. * @val: value to write
  386. *
  387. * Returns zero if successful or a negative error code.
  388. */
  389. int snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg,
  390. unsigned int val)
  391. {
  392. int err;
  393. err = reg_raw_write(codec, reg, val);
  394. if (err == -EAGAIN) {
  395. err = snd_hdac_power_up_pm(codec);
  396. if (err >= 0)
  397. err = reg_raw_write(codec, reg, val);
  398. snd_hdac_power_down_pm(codec);
  399. }
  400. return err;
  401. }
  402. EXPORT_SYMBOL_GPL(snd_hdac_regmap_write_raw);
  403. static int reg_raw_read(struct hdac_device *codec, unsigned int reg,
  404. unsigned int *val, bool uncached)
  405. {
  406. if (uncached || !codec->regmap)
  407. return hda_reg_read(codec, reg, val);
  408. else
  409. return regmap_read(codec->regmap, reg, val);
  410. }
  411. static int __snd_hdac_regmap_read_raw(struct hdac_device *codec,
  412. unsigned int reg, unsigned int *val,
  413. bool uncached)
  414. {
  415. int err;
  416. err = reg_raw_read(codec, reg, val, uncached);
  417. if (err == -EAGAIN) {
  418. err = snd_hdac_power_up_pm(codec);
  419. if (err >= 0)
  420. err = reg_raw_read(codec, reg, val, uncached);
  421. snd_hdac_power_down_pm(codec);
  422. }
  423. return err;
  424. }
  425. /**
  426. * snd_hdac_regmap_read_raw - read a pseudo register with power mgmt
  427. * @codec: the codec object
  428. * @reg: pseudo register
  429. * @val: pointer to store the read value
  430. *
  431. * Returns zero if successful or a negative error code.
  432. */
  433. int snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg,
  434. unsigned int *val)
  435. {
  436. return __snd_hdac_regmap_read_raw(codec, reg, val, false);
  437. }
  438. EXPORT_SYMBOL_GPL(snd_hdac_regmap_read_raw);
  439. /* Works like snd_hdac_regmap_read_raw(), but this doesn't read from the
  440. * cache but always via hda verbs.
  441. */
  442. int snd_hdac_regmap_read_raw_uncached(struct hdac_device *codec,
  443. unsigned int reg, unsigned int *val)
  444. {
  445. return __snd_hdac_regmap_read_raw(codec, reg, val, true);
  446. }
  447. /**
  448. * snd_hdac_regmap_update_raw - update a pseudo register with power mgmt
  449. * @codec: the codec object
  450. * @reg: pseudo register
  451. * @mask: bit mask to udpate
  452. * @val: value to update
  453. *
  454. * Returns zero if successful or a negative error code.
  455. */
  456. int snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg,
  457. unsigned int mask, unsigned int val)
  458. {
  459. unsigned int orig;
  460. int err;
  461. val &= mask;
  462. err = snd_hdac_regmap_read_raw(codec, reg, &orig);
  463. if (err < 0)
  464. return err;
  465. val |= orig & ~mask;
  466. if (val == orig)
  467. return 0;
  468. err = snd_hdac_regmap_write_raw(codec, reg, val);
  469. if (err < 0)
  470. return err;
  471. return 1;
  472. }
  473. EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw);