hac.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Hitachi Audio Controller (AC97) support for SH7760/SH7780
  3. *
  4. * Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
  5. * licensed under the terms outlined in the file COPYING at the root
  6. * of the linux kernel sources.
  7. *
  8. * dont forget to set IPSEL/OMSEL register bits (in your board code) to
  9. * enable HAC output pins!
  10. */
  11. /* BIG FAT FIXME: although the SH7760 has 2 independent AC97 units, only
  12. * the FIRST can be used since ASoC does not pass any information to the
  13. * ac97_read/write() functions regarding WHICH unit to use. You'll have
  14. * to edit the code a bit to use the other AC97 unit. --mlau
  15. */
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/wait.h>
  21. #include <linux/delay.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/ac97_codec.h>
  25. #include <sound/initval.h>
  26. #include <sound/soc.h>
  27. /* regs and bits */
  28. #define HACCR 0x08
  29. #define HACCSAR 0x20
  30. #define HACCSDR 0x24
  31. #define HACPCML 0x28
  32. #define HACPCMR 0x2C
  33. #define HACTIER 0x50
  34. #define HACTSR 0x54
  35. #define HACRIER 0x58
  36. #define HACRSR 0x5C
  37. #define HACACR 0x60
  38. #define CR_CR (1 << 15) /* "codec-ready" indicator */
  39. #define CR_CDRT (1 << 11) /* cold reset */
  40. #define CR_WMRT (1 << 10) /* warm reset */
  41. #define CR_B9 (1 << 9) /* the mysterious "bit 9" */
  42. #define CR_ST (1 << 5) /* AC97 link start bit */
  43. #define CSAR_RD (1 << 19) /* AC97 data read bit */
  44. #define CSAR_WR (0)
  45. #define TSR_CMDAMT (1 << 31)
  46. #define TSR_CMDDMT (1 << 30)
  47. #define RSR_STARY (1 << 22)
  48. #define RSR_STDRY (1 << 21)
  49. #define ACR_DMARX16 (1 << 30)
  50. #define ACR_DMATX16 (1 << 29)
  51. #define ACR_TX12ATOM (1 << 26)
  52. #define ACR_DMARX20 ((1 << 24) | (1 << 22))
  53. #define ACR_DMATX20 ((1 << 23) | (1 << 21))
  54. #define CSDR_SHIFT 4
  55. #define CSDR_MASK (0xffff << CSDR_SHIFT)
  56. #define CSAR_SHIFT 12
  57. #define CSAR_MASK (0x7f << CSAR_SHIFT)
  58. #define AC97_WRITE_RETRY 1
  59. #define AC97_READ_RETRY 5
  60. /* manual-suggested AC97 codec access timeouts (us) */
  61. #define TMO_E1 500 /* 21 < E1 < 1000 */
  62. #define TMO_E2 13 /* 13 < E2 */
  63. #define TMO_E3 21 /* 21 < E3 */
  64. #define TMO_E4 500 /* 21 < E4 < 1000 */
  65. struct hac_priv {
  66. unsigned long mmio; /* HAC base address */
  67. } hac_cpu_data[] = {
  68. #if defined(CONFIG_CPU_SUBTYPE_SH7760)
  69. {
  70. .mmio = 0xFE240000,
  71. },
  72. {
  73. .mmio = 0xFE250000,
  74. },
  75. #elif defined(CONFIG_CPU_SUBTYPE_SH7780)
  76. {
  77. .mmio = 0xFFE40000,
  78. },
  79. #else
  80. #error "Unsupported SuperH SoC"
  81. #endif
  82. };
  83. #define HACREG(reg) (*(unsigned long *)(hac->mmio + (reg)))
  84. /*
  85. * AC97 read/write flow as outlined in the SH7760 manual (pages 903-906)
  86. */
  87. static int hac_get_codec_data(struct hac_priv *hac, unsigned short r,
  88. unsigned short *v)
  89. {
  90. unsigned int to1, to2, i;
  91. unsigned short adr;
  92. for (i = AC97_READ_RETRY; i; i--) {
  93. *v = 0;
  94. /* wait for HAC to receive something from the codec */
  95. for (to1 = TMO_E4;
  96. to1 && !(HACREG(HACRSR) & RSR_STARY);
  97. --to1)
  98. udelay(1);
  99. for (to2 = TMO_E4;
  100. to2 && !(HACREG(HACRSR) & RSR_STDRY);
  101. --to2)
  102. udelay(1);
  103. if (!to1 && !to2)
  104. return 0; /* codec comm is down */
  105. adr = ((HACREG(HACCSAR) & CSAR_MASK) >> CSAR_SHIFT);
  106. *v = ((HACREG(HACCSDR) & CSDR_MASK) >> CSDR_SHIFT);
  107. HACREG(HACRSR) &= ~(RSR_STDRY | RSR_STARY);
  108. if (r == adr)
  109. break;
  110. /* manual says: wait at least 21 usec before retrying */
  111. udelay(21);
  112. }
  113. HACREG(HACRSR) &= ~(RSR_STDRY | RSR_STARY);
  114. return i;
  115. }
  116. static unsigned short hac_read_codec_aux(struct hac_priv *hac,
  117. unsigned short reg)
  118. {
  119. unsigned short val;
  120. unsigned int i, to;
  121. for (i = AC97_READ_RETRY; i; i--) {
  122. /* send_read_request */
  123. local_irq_disable();
  124. HACREG(HACTSR) &= ~(TSR_CMDAMT);
  125. HACREG(HACCSAR) = (reg << CSAR_SHIFT) | CSAR_RD;
  126. local_irq_enable();
  127. for (to = TMO_E3;
  128. to && !(HACREG(HACTSR) & TSR_CMDAMT);
  129. --to)
  130. udelay(1);
  131. HACREG(HACTSR) &= ~TSR_CMDAMT;
  132. val = 0;
  133. if (hac_get_codec_data(hac, reg, &val) != 0)
  134. break;
  135. }
  136. return i ? val : ~0;
  137. }
  138. static void hac_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  139. unsigned short val)
  140. {
  141. int unit_id = 0 /* ac97->private_data */;
  142. struct hac_priv *hac = &hac_cpu_data[unit_id];
  143. unsigned int i, to;
  144. /* write_codec_aux */
  145. for (i = AC97_WRITE_RETRY; i; i--) {
  146. /* send_write_request */
  147. local_irq_disable();
  148. HACREG(HACTSR) &= ~(TSR_CMDDMT | TSR_CMDAMT);
  149. HACREG(HACCSDR) = (val << CSDR_SHIFT);
  150. HACREG(HACCSAR) = (reg << CSAR_SHIFT) & (~CSAR_RD);
  151. local_irq_enable();
  152. /* poll-wait for CMDAMT and CMDDMT */
  153. for (to = TMO_E1;
  154. to && !(HACREG(HACTSR) & (TSR_CMDAMT|TSR_CMDDMT));
  155. --to)
  156. udelay(1);
  157. HACREG(HACTSR) &= ~(TSR_CMDAMT | TSR_CMDDMT);
  158. if (to)
  159. break;
  160. /* timeout, try again */
  161. }
  162. }
  163. static unsigned short hac_ac97_read(struct snd_ac97 *ac97,
  164. unsigned short reg)
  165. {
  166. int unit_id = 0 /* ac97->private_data */;
  167. struct hac_priv *hac = &hac_cpu_data[unit_id];
  168. return hac_read_codec_aux(hac, reg);
  169. }
  170. static void hac_ac97_warmrst(struct snd_ac97 *ac97)
  171. {
  172. int unit_id = 0 /* ac97->private_data */;
  173. struct hac_priv *hac = &hac_cpu_data[unit_id];
  174. unsigned int tmo;
  175. HACREG(HACCR) = CR_WMRT | CR_ST | CR_B9;
  176. msleep(10);
  177. HACREG(HACCR) = CR_ST | CR_B9;
  178. for (tmo = 1000; (tmo > 0) && !(HACREG(HACCR) & CR_CR); tmo--)
  179. udelay(1);
  180. if (!tmo)
  181. printk(KERN_INFO "hac: reset: AC97 link down!\n");
  182. /* settings this bit lets us have a conversation with codec */
  183. HACREG(HACACR) |= ACR_TX12ATOM;
  184. }
  185. static void hac_ac97_coldrst(struct snd_ac97 *ac97)
  186. {
  187. int unit_id = 0 /* ac97->private_data */;
  188. struct hac_priv *hac;
  189. hac = &hac_cpu_data[unit_id];
  190. HACREG(HACCR) = 0;
  191. HACREG(HACCR) = CR_CDRT | CR_ST | CR_B9;
  192. msleep(10);
  193. hac_ac97_warmrst(ac97);
  194. }
  195. struct snd_ac97_bus_ops soc_ac97_ops = {
  196. .read = hac_ac97_read,
  197. .write = hac_ac97_write,
  198. .reset = hac_ac97_coldrst,
  199. .warm_reset = hac_ac97_warmrst,
  200. };
  201. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  202. static int hac_hw_params(struct snd_pcm_substream *substream,
  203. struct snd_pcm_hw_params *params,
  204. struct snd_soc_dai *dai)
  205. {
  206. struct hac_priv *hac = &hac_cpu_data[dai->id];
  207. int d = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;
  208. switch (params->msbits) {
  209. case 16:
  210. HACREG(HACACR) |= d ? ACR_DMARX16 : ACR_DMATX16;
  211. HACREG(HACACR) &= d ? ~ACR_DMARX20 : ~ACR_DMATX20;
  212. break;
  213. case 20:
  214. HACREG(HACACR) &= d ? ~ACR_DMARX16 : ~ACR_DMATX16;
  215. HACREG(HACACR) |= d ? ACR_DMARX20 : ACR_DMATX20;
  216. break;
  217. default:
  218. pr_debug("hac: invalid depth %d bit\n", params->msbits);
  219. return -EINVAL;
  220. break;
  221. }
  222. return 0;
  223. }
  224. #define AC97_RATES \
  225. SNDRV_PCM_RATE_8000_192000
  226. #define AC97_FMTS \
  227. SNDRV_PCM_FMTBIT_S16_LE
  228. static struct snd_soc_dai_ops hac_dai_ops = {
  229. .hw_params = hac_hw_params,
  230. };
  231. static struct snd_soc_dai_driver sh4_hac_dai[] = {
  232. {
  233. .name = "hac-dai.0",
  234. .ac97_control = 1,
  235. .playback = {
  236. .rates = AC97_RATES,
  237. .formats = AC97_FMTS,
  238. .channels_min = 2,
  239. .channels_max = 2,
  240. },
  241. .capture = {
  242. .rates = AC97_RATES,
  243. .formats = AC97_FMTS,
  244. .channels_min = 2,
  245. .channels_max = 2,
  246. },
  247. .ops = &hac_dai_ops,
  248. },
  249. #ifdef CONFIG_CPU_SUBTYPE_SH7760
  250. {
  251. .name = "hac-dai.1",
  252. .id = 1,
  253. .playback = {
  254. .rates = AC97_RATES,
  255. .formats = AC97_FMTS,
  256. .channels_min = 2,
  257. .channels_max = 2,
  258. },
  259. .capture = {
  260. .rates = AC97_RATES,
  261. .formats = AC97_FMTS,
  262. .channels_min = 2,
  263. .channels_max = 2,
  264. },
  265. .ops = &hac_dai_ops,
  266. },
  267. #endif
  268. };
  269. static int __devinit hac_soc_platform_probe(struct platform_device *pdev)
  270. {
  271. return snd_soc_register_dais(&pdev->dev, sh4_hac_dai,
  272. ARRAY_SIZE(sh4_hac_dai));
  273. }
  274. static int __devexit hac_soc_platform_remove(struct platform_device *pdev)
  275. {
  276. snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(sh4_hac_dai));
  277. return 0;
  278. }
  279. static struct platform_driver hac_pcm_driver = {
  280. .driver = {
  281. .name = "hac-pcm-audio",
  282. .owner = THIS_MODULE,
  283. },
  284. .probe = hac_soc_platform_probe,
  285. .remove = __devexit_p(hac_soc_platform_remove),
  286. };
  287. static int __init sh4_hac_pcm_init(void)
  288. {
  289. return platform_driver_register(&hac_pcm_driver);
  290. }
  291. module_init(sh4_hac_pcm_init);
  292. static void __exit sh4_hac_pcm_exit(void)
  293. {
  294. platform_driver_unregister(&hac_pcm_driver);
  295. }
  296. module_exit(sh4_hac_pcm_exit);
  297. MODULE_LICENSE("GPL");
  298. MODULE_DESCRIPTION("SuperH onchip HAC (AC97) audio driver");
  299. MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");