hac.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/driver.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/ac97_codec.h>
  26. #include <sound/initval.h>
  27. #include <sound/soc.h>
  28. /* regs and bits */
  29. #define HACCR 0x08
  30. #define HACCSAR 0x20
  31. #define HACCSDR 0x24
  32. #define HACPCML 0x28
  33. #define HACPCMR 0x2C
  34. #define HACTIER 0x50
  35. #define HACTSR 0x54
  36. #define HACRIER 0x58
  37. #define HACRSR 0x5C
  38. #define HACACR 0x60
  39. #define CR_CR (1 << 15) /* "codec-ready" indicator */
  40. #define CR_CDRT (1 << 11) /* cold reset */
  41. #define CR_WMRT (1 << 10) /* warm reset */
  42. #define CR_B9 (1 << 9) /* the mysterious "bit 9" */
  43. #define CR_ST (1 << 5) /* AC97 link start bit */
  44. #define CSAR_RD (1 << 19) /* AC97 data read bit */
  45. #define CSAR_WR (0)
  46. #define TSR_CMDAMT (1 << 31)
  47. #define TSR_CMDDMT (1 << 30)
  48. #define RSR_STARY (1 << 22)
  49. #define RSR_STDRY (1 << 21)
  50. #define ACR_DMARX16 (1 << 30)
  51. #define ACR_DMATX16 (1 << 29)
  52. #define ACR_TX12ATOM (1 << 26)
  53. #define ACR_DMARX20 ((1 << 24) | (1 << 22))
  54. #define ACR_DMATX20 ((1 << 23) | (1 << 21))
  55. #define CSDR_SHIFT 4
  56. #define CSDR_MASK (0xffff << CSDR_SHIFT)
  57. #define CSAR_SHIFT 12
  58. #define CSAR_MASK (0x7f << CSAR_SHIFT)
  59. #define AC97_WRITE_RETRY 1
  60. #define AC97_READ_RETRY 5
  61. /* manual-suggested AC97 codec access timeouts (us) */
  62. #define TMO_E1 500 /* 21 < E1 < 1000 */
  63. #define TMO_E2 13 /* 13 < E2 */
  64. #define TMO_E3 21 /* 21 < E3 */
  65. #define TMO_E4 500 /* 21 < E4 < 1000 */
  66. struct hac_priv {
  67. unsigned long mmio; /* HAC base address */
  68. } hac_cpu_data[] = {
  69. #if defined(CONFIG_CPU_SUBTYPE_SH7760)
  70. {
  71. .mmio = 0xFE240000,
  72. },
  73. {
  74. .mmio = 0xFE250000,
  75. },
  76. #elif defined(CONFIG_CPU_SUBTYPE_SH7780)
  77. {
  78. .mmio = 0xFFE40000,
  79. },
  80. #else
  81. #error "Unsupported SuperH SoC"
  82. #endif
  83. };
  84. #define HACREG(reg) (*(unsigned long *)(hac->mmio + (reg)))
  85. /*
  86. * AC97 read/write flow as outlined in the SH7760 manual (pages 903-906)
  87. */
  88. static int hac_get_codec_data(struct hac_priv *hac, unsigned short r,
  89. unsigned short *v)
  90. {
  91. unsigned int to1, to2, i;
  92. unsigned short adr;
  93. for (i = 0; i < AC97_READ_RETRY; ++i) {
  94. *v = 0;
  95. /* wait for HAC to receive something from the codec */
  96. for (to1 = TMO_E4;
  97. to1 && !(HACREG(HACRSR) & RSR_STARY);
  98. --to1)
  99. udelay(1);
  100. for (to2 = TMO_E4;
  101. to2 && !(HACREG(HACRSR) & RSR_STDRY);
  102. --to2)
  103. udelay(1);
  104. if (!to1 && !to2)
  105. return 0; /* codec comm is down */
  106. adr = ((HACREG(HACCSAR) & CSAR_MASK) >> CSAR_SHIFT);
  107. *v = ((HACREG(HACCSDR) & CSDR_MASK) >> CSDR_SHIFT);
  108. HACREG(HACRSR) &= ~(RSR_STDRY | RSR_STARY);
  109. if (r == adr)
  110. break;
  111. /* manual says: wait at least 21 usec before retrying */
  112. udelay(21);
  113. }
  114. HACREG(HACRSR) &= ~(RSR_STDRY | RSR_STARY);
  115. return (i < AC97_READ_RETRY);
  116. }
  117. static unsigned short hac_read_codec_aux(struct hac_priv *hac,
  118. unsigned short reg)
  119. {
  120. unsigned short val;
  121. unsigned int i, to;
  122. for (i = 0; i < AC97_READ_RETRY; i++) {
  123. /* send_read_request */
  124. local_irq_disable();
  125. HACREG(HACTSR) &= ~(TSR_CMDAMT);
  126. HACREG(HACCSAR) = (reg << CSAR_SHIFT) | CSAR_RD;
  127. local_irq_enable();
  128. for (to = TMO_E3;
  129. to && !(HACREG(HACTSR) & TSR_CMDAMT);
  130. --to)
  131. udelay(1);
  132. HACREG(HACTSR) &= ~TSR_CMDAMT;
  133. val = 0;
  134. if (hac_get_codec_data(hac, reg, &val) != 0)
  135. break;
  136. }
  137. if (i == AC97_READ_RETRY)
  138. return ~0;
  139. return val;
  140. }
  141. static void hac_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  142. unsigned short val)
  143. {
  144. int unit_id = 0 /* ac97->private_data */;
  145. struct hac_priv *hac = &hac_cpu_data[unit_id];
  146. unsigned int i, to;
  147. /* write_codec_aux */
  148. for (i = 0; i < AC97_WRITE_RETRY; i++) {
  149. /* send_write_request */
  150. local_irq_disable();
  151. HACREG(HACTSR) &= ~(TSR_CMDDMT | TSR_CMDAMT);
  152. HACREG(HACCSDR) = (val << CSDR_SHIFT);
  153. HACREG(HACCSAR) = (reg << CSAR_SHIFT) & (~CSAR_RD);
  154. local_irq_enable();
  155. /* poll-wait for CMDAMT and CMDDMT */
  156. for (to = TMO_E1;
  157. to && !(HACREG(HACTSR) & (TSR_CMDAMT|TSR_CMDDMT));
  158. --to)
  159. udelay(1);
  160. HACREG(HACTSR) &= ~(TSR_CMDAMT | TSR_CMDDMT);
  161. if (to)
  162. break;
  163. /* timeout, try again */
  164. }
  165. }
  166. static unsigned short hac_ac97_read(struct snd_ac97 *ac97,
  167. unsigned short reg)
  168. {
  169. int unit_id = 0 /* ac97->private_data */;
  170. struct hac_priv *hac = &hac_cpu_data[unit_id];
  171. return hac_read_codec_aux(hac, reg);
  172. }
  173. static void hac_ac97_warmrst(struct snd_ac97 *ac97)
  174. {
  175. int unit_id = 0 /* ac97->private_data */;
  176. struct hac_priv *hac = &hac_cpu_data[unit_id];
  177. unsigned int tmo;
  178. HACREG(HACCR) = CR_WMRT | CR_ST | CR_B9;
  179. msleep(10);
  180. HACREG(HACCR) = CR_ST | CR_B9;
  181. for (tmo = 1000; (tmo > 0) && !(HACREG(HACCR) & CR_CR); tmo--)
  182. udelay(1);
  183. if (!tmo)
  184. printk(KERN_INFO "hac: reset: AC97 link down!\n");
  185. /* settings this bit lets us have a conversation with codec */
  186. HACREG(HACACR) |= ACR_TX12ATOM;
  187. }
  188. static void hac_ac97_coldrst(struct snd_ac97 *ac97)
  189. {
  190. int unit_id = 0 /* ac97->private_data */;
  191. struct hac_priv *hac;
  192. hac = &hac_cpu_data[unit_id];
  193. HACREG(HACCR) = 0;
  194. HACREG(HACCR) = CR_CDRT | CR_ST | CR_B9;
  195. msleep(10);
  196. hac_ac97_warmrst(ac97);
  197. }
  198. struct snd_ac97_bus_ops soc_ac97_ops = {
  199. .read = hac_ac97_read,
  200. .write = hac_ac97_write,
  201. .reset = hac_ac97_coldrst,
  202. .warm_reset = hac_ac97_warmrst,
  203. };
  204. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  205. static int hac_hw_params(struct snd_pcm_substream *substream,
  206. struct snd_pcm_hw_params *params)
  207. {
  208. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  209. struct hac_priv *hac = &hac_cpu_data[rtd->dai->cpu_dai->id];
  210. int d = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;
  211. switch (params->msbits) {
  212. case 16:
  213. HACREG(HACACR) |= d ? ACR_DMARX16 : ACR_DMATX16;
  214. HACREG(HACACR) &= d ? ~ACR_DMARX20 : ~ACR_DMATX20;
  215. break;
  216. case 20:
  217. HACREG(HACACR) &= d ? ~ACR_DMARX16 : ~ACR_DMATX16;
  218. HACREG(HACACR) |= d ? ACR_DMARX20 : ACR_DMATX20;
  219. break;
  220. default:
  221. pr_debug("hac: invalid depth %d bit\n", params->msbits);
  222. return -EINVAL;
  223. break;
  224. }
  225. return 0;
  226. }
  227. #define AC97_RATES \
  228. SNDRV_PCM_RATE_8000_192000
  229. #define AC97_FMTS \
  230. SNDRV_PCM_FMTBIT_S16_LE
  231. struct snd_soc_cpu_dai sh4_hac_dai[] = {
  232. {
  233. .name = "HAC0",
  234. .id = 0,
  235. .type = SND_SOC_DAI_AC97,
  236. .playback = {
  237. .rates = AC97_RATES,
  238. .formats = AC97_FMTS,
  239. .channels_min = 2,
  240. .channels_max = 2,
  241. },
  242. .capture = {
  243. .rates = AC97_RATES,
  244. .formats = AC97_FMTS,
  245. .channels_min = 2,
  246. .channels_max = 2,
  247. },
  248. .ops = {
  249. .hw_params = hac_hw_params,
  250. },
  251. },
  252. #ifdef CONFIG_CPU_SUBTYPE_SH7760
  253. {
  254. .name = "HAC1",
  255. .id = 1,
  256. .type = SND_SOC_DAI_AC97,
  257. .playback = {
  258. .rates = AC97_RATES,
  259. .formats = AC97_FMTS,
  260. .channels_min = 2,
  261. .channels_max = 2,
  262. },
  263. .capture = {
  264. .rates = AC97_RATES,
  265. .formats = AC97_FMTS,
  266. .channels_min = 2,
  267. .channels_max = 2,
  268. },
  269. .ops = {
  270. .hw_params = hac_hw_params,
  271. },
  272. },
  273. #endif
  274. };
  275. EXPORT_SYMBOL_GPL(sh4_hac_dai);
  276. MODULE_LICENSE("GPL");
  277. MODULE_DESCRIPTION("SuperH onchip HAC (AC97) audio driver");
  278. MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");