aw2-saa7146.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*****************************************************************************
  3. *
  4. * Copyright (C) 2008 Cedric Bregardis <[email protected]> and
  5. * Jean-Christian Hassler <[email protected]>
  6. *
  7. * This file is part of the Audiowerk2 ALSA driver
  8. *
  9. *****************************************************************************/
  10. #define AW2_SAA7146_M
  11. #include <linux/init.h>
  12. #include <linux/pci.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <sound/core.h>
  17. #include <sound/initval.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include "saa7146.h"
  21. #include "aw2-saa7146.h"
  22. #include "aw2-tsl.c"
  23. #define WRITEREG(value, addr) writel((value), chip->base_addr + (addr))
  24. #define READREG(addr) readl(chip->base_addr + (addr))
  25. static struct snd_aw2_saa7146_cb_param
  26. arr_substream_it_playback_cb[NB_STREAM_PLAYBACK];
  27. static struct snd_aw2_saa7146_cb_param
  28. arr_substream_it_capture_cb[NB_STREAM_CAPTURE];
  29. static int snd_aw2_saa7146_get_limit(int size);
  30. /* chip-specific destructor */
  31. int snd_aw2_saa7146_free(struct snd_aw2_saa7146 *chip)
  32. {
  33. /* disable all irqs */
  34. WRITEREG(0, IER);
  35. /* reset saa7146 */
  36. WRITEREG((MRST_N << 16), MC1);
  37. /* Unset base addr */
  38. chip->base_addr = NULL;
  39. return 0;
  40. }
  41. void snd_aw2_saa7146_setup(struct snd_aw2_saa7146 *chip,
  42. void __iomem *pci_base_addr)
  43. {
  44. /* set PCI burst/threshold
  45. Burst length definition
  46. VALUE BURST LENGTH
  47. 000 1 Dword
  48. 001 2 Dwords
  49. 010 4 Dwords
  50. 011 8 Dwords
  51. 100 16 Dwords
  52. 101 32 Dwords
  53. 110 64 Dwords
  54. 111 128 Dwords
  55. Threshold definition
  56. VALUE WRITE MODE READ MODE
  57. 00 1 Dword of valid data 1 empty Dword
  58. 01 4 Dwords of valid data 4 empty Dwords
  59. 10 8 Dwords of valid data 8 empty Dwords
  60. 11 16 Dwords of valid data 16 empty Dwords */
  61. unsigned int acon2;
  62. unsigned int acon1 = 0;
  63. int i;
  64. /* Set base addr */
  65. chip->base_addr = pci_base_addr;
  66. /* disable all irqs */
  67. WRITEREG(0, IER);
  68. /* reset saa7146 */
  69. WRITEREG((MRST_N << 16), MC1);
  70. /* enable audio interface */
  71. #ifdef __BIG_ENDIAN
  72. acon1 |= A1_SWAP;
  73. acon1 |= A2_SWAP;
  74. #endif
  75. /* WS0_CTRL, WS0_SYNC: input TSL1, I2S */
  76. /* At initialization WS1 and WS2 are disabled (configured as input) */
  77. acon1 |= 0 * WS1_CTRL;
  78. acon1 |= 0 * WS2_CTRL;
  79. /* WS4 is not used. So it must not restart A2.
  80. This is why it is configured as output (force to low) */
  81. acon1 |= 3 * WS4_CTRL;
  82. /* WS3_CTRL, WS3_SYNC: output TSL2, I2S */
  83. acon1 |= 2 * WS3_CTRL;
  84. /* A1 and A2 are active and asynchronous */
  85. acon1 |= 3 * AUDIO_MODE;
  86. WRITEREG(acon1, ACON1);
  87. /* The following comes from original windows driver.
  88. It is needed to have a correct behavior of input and output
  89. simultenously, but I don't know why ! */
  90. WRITEREG(3 * (BurstA1_in) + 3 * (ThreshA1_in) +
  91. 3 * (BurstA1_out) + 3 * (ThreshA1_out) +
  92. 3 * (BurstA2_out) + 3 * (ThreshA2_out), PCI_BT_A);
  93. /* enable audio port pins */
  94. WRITEREG((EAP << 16) | EAP, MC1);
  95. /* enable I2C */
  96. WRITEREG((EI2C << 16) | EI2C, MC1);
  97. /* enable interrupts */
  98. WRITEREG(A1_out | A2_out | A1_in | IIC_S | IIC_E, IER);
  99. /* audio configuration */
  100. acon2 = A2_CLKSRC | BCLK1_OEN;
  101. WRITEREG(acon2, ACON2);
  102. /* By default use analog input */
  103. snd_aw2_saa7146_use_digital_input(chip, 0);
  104. /* TSL setup */
  105. for (i = 0; i < 8; ++i) {
  106. WRITEREG(tsl1[i], TSL1 + (i * 4));
  107. WRITEREG(tsl2[i], TSL2 + (i * 4));
  108. }
  109. }
  110. void snd_aw2_saa7146_pcm_init_playback(struct snd_aw2_saa7146 *chip,
  111. int stream_number,
  112. unsigned long dma_addr,
  113. unsigned long period_size,
  114. unsigned long buffer_size)
  115. {
  116. unsigned long dw_page, dw_limit;
  117. /* Configure DMA for substream
  118. Configuration informations: ALSA has allocated continuous memory
  119. pages. So we don't need to use MMU of saa7146.
  120. */
  121. /* No MMU -> nothing to do with PageA1, we only configure the limit of
  122. PageAx_out register */
  123. /* Disable MMU */
  124. dw_page = (0L << 11);
  125. /* Configure Limit for DMA access.
  126. The limit register defines an address limit, which generates
  127. an interrupt if passed by the actual PCI address pointer.
  128. '0001' means an interrupt will be generated if the lower
  129. 6 bits (64 bytes) of the PCI address are zero. '0010'
  130. defines a limit of 128 bytes, '0011' one of 256 bytes, and
  131. so on up to 1 Mbyte defined by '1111'. This interrupt range
  132. can be calculated as follows:
  133. Range = 2^(5 + Limit) bytes.
  134. */
  135. dw_limit = snd_aw2_saa7146_get_limit(period_size);
  136. dw_page |= (dw_limit << 4);
  137. if (stream_number == 0) {
  138. WRITEREG(dw_page, PageA2_out);
  139. /* Base address for DMA transfert. */
  140. /* This address has been reserved by ALSA. */
  141. /* This is a physical address */
  142. WRITEREG(dma_addr, BaseA2_out);
  143. /* Define upper limit for DMA access */
  144. WRITEREG(dma_addr + buffer_size, ProtA2_out);
  145. } else if (stream_number == 1) {
  146. WRITEREG(dw_page, PageA1_out);
  147. /* Base address for DMA transfert. */
  148. /* This address has been reserved by ALSA. */
  149. /* This is a physical address */
  150. WRITEREG(dma_addr, BaseA1_out);
  151. /* Define upper limit for DMA access */
  152. WRITEREG(dma_addr + buffer_size, ProtA1_out);
  153. } else {
  154. pr_err("aw2: snd_aw2_saa7146_pcm_init_playback: "
  155. "Substream number is not 0 or 1 -> not managed\n");
  156. }
  157. }
  158. void snd_aw2_saa7146_pcm_init_capture(struct snd_aw2_saa7146 *chip,
  159. int stream_number, unsigned long dma_addr,
  160. unsigned long period_size,
  161. unsigned long buffer_size)
  162. {
  163. unsigned long dw_page, dw_limit;
  164. /* Configure DMA for substream
  165. Configuration informations: ALSA has allocated continuous memory
  166. pages. So we don't need to use MMU of saa7146.
  167. */
  168. /* No MMU -> nothing to do with PageA1, we only configure the limit of
  169. PageAx_out register */
  170. /* Disable MMU */
  171. dw_page = (0L << 11);
  172. /* Configure Limit for DMA access.
  173. The limit register defines an address limit, which generates
  174. an interrupt if passed by the actual PCI address pointer.
  175. '0001' means an interrupt will be generated if the lower
  176. 6 bits (64 bytes) of the PCI address are zero. '0010'
  177. defines a limit of 128 bytes, '0011' one of 256 bytes, and
  178. so on up to 1 Mbyte defined by '1111'. This interrupt range
  179. can be calculated as follows:
  180. Range = 2^(5 + Limit) bytes.
  181. */
  182. dw_limit = snd_aw2_saa7146_get_limit(period_size);
  183. dw_page |= (dw_limit << 4);
  184. if (stream_number == 0) {
  185. WRITEREG(dw_page, PageA1_in);
  186. /* Base address for DMA transfert. */
  187. /* This address has been reserved by ALSA. */
  188. /* This is a physical address */
  189. WRITEREG(dma_addr, BaseA1_in);
  190. /* Define upper limit for DMA access */
  191. WRITEREG(dma_addr + buffer_size, ProtA1_in);
  192. } else {
  193. pr_err("aw2: snd_aw2_saa7146_pcm_init_capture: "
  194. "Substream number is not 0 -> not managed\n");
  195. }
  196. }
  197. void snd_aw2_saa7146_define_it_playback_callback(unsigned int stream_number,
  198. snd_aw2_saa7146_it_cb
  199. p_it_callback,
  200. void *p_callback_param)
  201. {
  202. if (stream_number < NB_STREAM_PLAYBACK) {
  203. arr_substream_it_playback_cb[stream_number].p_it_callback =
  204. (snd_aw2_saa7146_it_cb) p_it_callback;
  205. arr_substream_it_playback_cb[stream_number].p_callback_param =
  206. (void *)p_callback_param;
  207. }
  208. }
  209. void snd_aw2_saa7146_define_it_capture_callback(unsigned int stream_number,
  210. snd_aw2_saa7146_it_cb
  211. p_it_callback,
  212. void *p_callback_param)
  213. {
  214. if (stream_number < NB_STREAM_CAPTURE) {
  215. arr_substream_it_capture_cb[stream_number].p_it_callback =
  216. (snd_aw2_saa7146_it_cb) p_it_callback;
  217. arr_substream_it_capture_cb[stream_number].p_callback_param =
  218. (void *)p_callback_param;
  219. }
  220. }
  221. void snd_aw2_saa7146_pcm_trigger_start_playback(struct snd_aw2_saa7146 *chip,
  222. int stream_number)
  223. {
  224. unsigned int acon1 = 0;
  225. /* In aw8 driver, dma transfert is always active. It is
  226. started and stopped in a larger "space" */
  227. acon1 = READREG(ACON1);
  228. if (stream_number == 0) {
  229. WRITEREG((TR_E_A2_OUT << 16) | TR_E_A2_OUT, MC1);
  230. /* WS2_CTRL, WS2_SYNC: output TSL2, I2S */
  231. acon1 |= 2 * WS2_CTRL;
  232. WRITEREG(acon1, ACON1);
  233. } else if (stream_number == 1) {
  234. WRITEREG((TR_E_A1_OUT << 16) | TR_E_A1_OUT, MC1);
  235. /* WS1_CTRL, WS1_SYNC: output TSL1, I2S */
  236. acon1 |= 1 * WS1_CTRL;
  237. WRITEREG(acon1, ACON1);
  238. }
  239. }
  240. void snd_aw2_saa7146_pcm_trigger_stop_playback(struct snd_aw2_saa7146 *chip,
  241. int stream_number)
  242. {
  243. unsigned int acon1 = 0;
  244. acon1 = READREG(ACON1);
  245. if (stream_number == 0) {
  246. /* WS2_CTRL, WS2_SYNC: output TSL2, I2S */
  247. acon1 &= ~(3 * WS2_CTRL);
  248. WRITEREG(acon1, ACON1);
  249. WRITEREG((TR_E_A2_OUT << 16), MC1);
  250. } else if (stream_number == 1) {
  251. /* WS1_CTRL, WS1_SYNC: output TSL1, I2S */
  252. acon1 &= ~(3 * WS1_CTRL);
  253. WRITEREG(acon1, ACON1);
  254. WRITEREG((TR_E_A1_OUT << 16), MC1);
  255. }
  256. }
  257. void snd_aw2_saa7146_pcm_trigger_start_capture(struct snd_aw2_saa7146 *chip,
  258. int stream_number)
  259. {
  260. /* In aw8 driver, dma transfert is always active. It is
  261. started and stopped in a larger "space" */
  262. if (stream_number == 0)
  263. WRITEREG((TR_E_A1_IN << 16) | TR_E_A1_IN, MC1);
  264. }
  265. void snd_aw2_saa7146_pcm_trigger_stop_capture(struct snd_aw2_saa7146 *chip,
  266. int stream_number)
  267. {
  268. if (stream_number == 0)
  269. WRITEREG((TR_E_A1_IN << 16), MC1);
  270. }
  271. irqreturn_t snd_aw2_saa7146_interrupt(int irq, void *dev_id)
  272. {
  273. unsigned int isr;
  274. __always_unused unsigned int iicsta;
  275. struct snd_aw2_saa7146 *chip = dev_id;
  276. isr = READREG(ISR);
  277. if (!isr)
  278. return IRQ_NONE;
  279. WRITEREG(isr, ISR);
  280. if (isr & (IIC_S | IIC_E)) {
  281. iicsta = READREG(IICSTA);
  282. WRITEREG(0x100, IICSTA);
  283. }
  284. if (isr & A1_out) {
  285. if (arr_substream_it_playback_cb[1].p_it_callback != NULL) {
  286. arr_substream_it_playback_cb[1].
  287. p_it_callback(arr_substream_it_playback_cb[1].
  288. p_callback_param);
  289. }
  290. }
  291. if (isr & A2_out) {
  292. if (arr_substream_it_playback_cb[0].p_it_callback != NULL) {
  293. arr_substream_it_playback_cb[0].
  294. p_it_callback(arr_substream_it_playback_cb[0].
  295. p_callback_param);
  296. }
  297. }
  298. if (isr & A1_in) {
  299. if (arr_substream_it_capture_cb[0].p_it_callback != NULL) {
  300. arr_substream_it_capture_cb[0].
  301. p_it_callback(arr_substream_it_capture_cb[0].
  302. p_callback_param);
  303. }
  304. }
  305. return IRQ_HANDLED;
  306. }
  307. unsigned int snd_aw2_saa7146_get_hw_ptr_playback(struct snd_aw2_saa7146 *chip,
  308. int stream_number,
  309. unsigned char *start_addr,
  310. unsigned int buffer_size)
  311. {
  312. long pci_adp = 0;
  313. size_t ptr = 0;
  314. if (stream_number == 0) {
  315. pci_adp = READREG(PCI_ADP3);
  316. ptr = pci_adp - (long)start_addr;
  317. if (ptr == buffer_size)
  318. ptr = 0;
  319. }
  320. if (stream_number == 1) {
  321. pci_adp = READREG(PCI_ADP1);
  322. ptr = pci_adp - (size_t) start_addr;
  323. if (ptr == buffer_size)
  324. ptr = 0;
  325. }
  326. return ptr;
  327. }
  328. unsigned int snd_aw2_saa7146_get_hw_ptr_capture(struct snd_aw2_saa7146 *chip,
  329. int stream_number,
  330. unsigned char *start_addr,
  331. unsigned int buffer_size)
  332. {
  333. size_t pci_adp = 0;
  334. size_t ptr = 0;
  335. if (stream_number == 0) {
  336. pci_adp = READREG(PCI_ADP2);
  337. ptr = pci_adp - (size_t) start_addr;
  338. if (ptr == buffer_size)
  339. ptr = 0;
  340. }
  341. return ptr;
  342. }
  343. void snd_aw2_saa7146_use_digital_input(struct snd_aw2_saa7146 *chip,
  344. int use_digital)
  345. {
  346. /* FIXME: switch between analog and digital input does not always work.
  347. It can produce a kind of white noise. It seams that received data
  348. are inverted sometime (endian inversion). Why ? I don't know, maybe
  349. a problem of synchronization... However for the time being I have
  350. not found the problem. Workaround: switch again (and again) between
  351. digital and analog input until it works. */
  352. if (use_digital)
  353. WRITEREG(0x40, GPIO_CTRL);
  354. else
  355. WRITEREG(0x50, GPIO_CTRL);
  356. }
  357. int snd_aw2_saa7146_is_using_digital_input(struct snd_aw2_saa7146 *chip)
  358. {
  359. unsigned int reg_val = READREG(GPIO_CTRL);
  360. if ((reg_val & 0xFF) == 0x40)
  361. return 1;
  362. else
  363. return 0;
  364. }
  365. static int snd_aw2_saa7146_get_limit(int size)
  366. {
  367. int limitsize = 32;
  368. int limit = 0;
  369. while (limitsize < size) {
  370. limitsize *= 2;
  371. limit++;
  372. }
  373. return limit;
  374. }