hdac_regmap.c 12 KB

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