hac.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. static struct snd_ac97_bus_ops hac_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. static int hac_hw_params(struct snd_pcm_substream *substream,
  202. struct snd_pcm_hw_params *params,
  203. struct snd_soc_dai *dai)
  204. {
  205. struct hac_priv *hac = &hac_cpu_data[dai->id];
  206. int d = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;
  207. switch (params->msbits) {
  208. case 16:
  209. HACREG(HACACR) |= d ? ACR_DMARX16 : ACR_DMATX16;
  210. HACREG(HACACR) &= d ? ~ACR_DMARX20 : ~ACR_DMATX20;
  211. break;
  212. case 20:
  213. HACREG(HACACR) &= d ? ~ACR_DMARX16 : ~ACR_DMATX16;
  214. HACREG(HACACR) |= d ? ACR_DMARX20 : ACR_DMATX20;
  215. break;
  216. default:
  217. pr_debug("hac: invalid depth %d bit\n", params->msbits);
  218. return -EINVAL;
  219. break;
  220. }
  221. return 0;
  222. }
  223. #define AC97_RATES \
  224. SNDRV_PCM_RATE_8000_192000
  225. #define AC97_FMTS \
  226. SNDRV_PCM_FMTBIT_S16_LE
  227. static const struct snd_soc_dai_ops hac_dai_ops = {
  228. .hw_params = hac_hw_params,
  229. };
  230. static struct snd_soc_dai_driver sh4_hac_dai[] = {
  231. {
  232. .name = "hac-dai.0",
  233. .bus_control = true,
  234. .playback = {
  235. .rates = AC97_RATES,
  236. .formats = AC97_FMTS,
  237. .channels_min = 2,
  238. .channels_max = 2,
  239. },
  240. .capture = {
  241. .rates = AC97_RATES,
  242. .formats = AC97_FMTS,
  243. .channels_min = 2,
  244. .channels_max = 2,
  245. },
  246. .ops = &hac_dai_ops,
  247. },
  248. #ifdef CONFIG_CPU_SUBTYPE_SH7760
  249. {
  250. .name = "hac-dai.1",
  251. .id = 1,
  252. .playback = {
  253. .rates = AC97_RATES,
  254. .formats = AC97_FMTS,
  255. .channels_min = 2,
  256. .channels_max = 2,
  257. },
  258. .capture = {
  259. .rates = AC97_RATES,
  260. .formats = AC97_FMTS,
  261. .channels_min = 2,
  262. .channels_max = 2,
  263. },
  264. .ops = &hac_dai_ops,
  265. },
  266. #endif
  267. };
  268. static const struct snd_soc_component_driver sh4_hac_component = {
  269. .name = "sh4-hac",
  270. };
  271. static int hac_soc_platform_probe(struct platform_device *pdev)
  272. {
  273. ret = snd_soc_set_ac97_ops(&hac_ac97_ops);
  274. if (ret != 0)
  275. return ret;
  276. return snd_soc_register_component(&pdev->dev, &sh4_hac_component,
  277. sh4_hac_dai, ARRAY_SIZE(sh4_hac_dai));
  278. }
  279. static int hac_soc_platform_remove(struct platform_device *pdev)
  280. {
  281. snd_soc_unregister_component(&pdev->dev);
  282. snd_soc_set_ac97_ops(NULL);
  283. return 0;
  284. }
  285. static struct platform_driver hac_pcm_driver = {
  286. .driver = {
  287. .name = "hac-pcm-audio",
  288. },
  289. .probe = hac_soc_platform_probe,
  290. .remove = hac_soc_platform_remove,
  291. };
  292. module_platform_driver(hac_pcm_driver);
  293. MODULE_LICENSE("GPL");
  294. MODULE_DESCRIPTION("SuperH onchip HAC (AC97) audio driver");
  295. MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");