ak4xxx.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * ALSA driver for ICEnsemble ICE1712 (Envy24)
  3. *
  4. * AK4524 / AK4528 / AK4529 / AK4355 / AK4381 interface
  5. *
  6. * Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <asm/io.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/init.h>
  27. #include <sound/core.h>
  28. #include <sound/initval.h>
  29. #include "ice1712.h"
  30. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  31. MODULE_DESCRIPTION("ICEnsemble ICE17xx <-> AK4xxx AD/DA chip interface");
  32. MODULE_LICENSE("GPL");
  33. static void snd_ice1712_akm4xxx_lock(struct snd_akm4xxx *ak, int chip)
  34. {
  35. struct snd_ice1712 *ice = ak->private_data[0];
  36. snd_ice1712_save_gpio_status(ice);
  37. }
  38. static void snd_ice1712_akm4xxx_unlock(struct snd_akm4xxx *ak, int chip)
  39. {
  40. struct snd_ice1712 *ice = ak->private_data[0];
  41. snd_ice1712_restore_gpio_status(ice);
  42. }
  43. /*
  44. * write AK4xxx register
  45. */
  46. static void snd_ice1712_akm4xxx_write(struct snd_akm4xxx *ak, int chip,
  47. unsigned char addr, unsigned char data)
  48. {
  49. unsigned int tmp;
  50. int idx;
  51. unsigned int addrdata;
  52. struct snd_ak4xxx_private *priv = (void *)ak->private_value[0];
  53. struct snd_ice1712 *ice = ak->private_data[0];
  54. snd_assert(chip >= 0 && chip < 4, return);
  55. tmp = snd_ice1712_gpio_read(ice);
  56. tmp |= priv->add_flags;
  57. tmp &= ~priv->mask_flags;
  58. if (priv->cs_mask == priv->cs_addr) {
  59. if (priv->cif) {
  60. tmp |= priv->cs_mask; /* start without chip select */
  61. } else {
  62. tmp &= ~priv->cs_mask; /* chip select low */
  63. snd_ice1712_gpio_write(ice, tmp);
  64. udelay(1);
  65. }
  66. } else {
  67. /* doesn't handle cf=1 yet */
  68. tmp &= ~priv->cs_mask;
  69. tmp |= priv->cs_addr;
  70. snd_ice1712_gpio_write(ice, tmp);
  71. udelay(1);
  72. }
  73. /* build I2C address + data byte */
  74. addrdata = (priv->caddr << 6) | 0x20 | (addr & 0x1f);
  75. addrdata = (addrdata << 8) | data;
  76. for (idx = 15; idx >= 0; idx--) {
  77. /* drop clock */
  78. tmp &= ~priv->clk_mask;
  79. snd_ice1712_gpio_write(ice, tmp);
  80. udelay(1);
  81. /* set data */
  82. if (addrdata & (1 << idx))
  83. tmp |= priv->data_mask;
  84. else
  85. tmp &= ~priv->data_mask;
  86. snd_ice1712_gpio_write(ice, tmp);
  87. udelay(1);
  88. /* raise clock */
  89. tmp |= priv->clk_mask;
  90. snd_ice1712_gpio_write(ice, tmp);
  91. udelay(1);
  92. }
  93. if (priv->cs_mask == priv->cs_addr) {
  94. if (priv->cif) {
  95. /* assert a cs pulse to trigger */
  96. tmp &= ~priv->cs_mask;
  97. snd_ice1712_gpio_write(ice, tmp);
  98. udelay(1);
  99. }
  100. tmp |= priv->cs_mask; /* chip select high to trigger */
  101. } else {
  102. tmp &= ~priv->cs_mask;
  103. tmp |= priv->cs_none; /* deselect address */
  104. }
  105. snd_ice1712_gpio_write(ice, tmp);
  106. udelay(1);
  107. }
  108. /*
  109. * initialize the struct snd_akm4xxx record with the template
  110. */
  111. int snd_ice1712_akm4xxx_init(struct snd_akm4xxx *ak, const struct snd_akm4xxx *temp,
  112. const struct snd_ak4xxx_private *_priv, struct snd_ice1712 *ice)
  113. {
  114. struct snd_ak4xxx_private *priv;
  115. if (_priv != NULL) {
  116. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  117. if (priv == NULL)
  118. return -ENOMEM;
  119. *priv = *_priv;
  120. } else {
  121. priv = NULL;
  122. }
  123. *ak = *temp;
  124. ak->card = ice->card;
  125. ak->private_value[0] = (unsigned long)priv;
  126. ak->private_data[0] = ice;
  127. if (ak->ops.lock == NULL)
  128. ak->ops.lock = snd_ice1712_akm4xxx_lock;
  129. if (ak->ops.unlock == NULL)
  130. ak->ops.unlock = snd_ice1712_akm4xxx_unlock;
  131. if (ak->ops.write == NULL)
  132. ak->ops.write = snd_ice1712_akm4xxx_write;
  133. snd_akm4xxx_init(ak);
  134. return 0;
  135. }
  136. void snd_ice1712_akm4xxx_free(struct snd_ice1712 *ice)
  137. {
  138. unsigned int akidx;
  139. if (ice->akm == NULL)
  140. return;
  141. for (akidx = 0; akidx < ice->akm_codecs; akidx++) {
  142. struct snd_akm4xxx *ak = &ice->akm[akidx];
  143. kfree((void*)ak->private_value[0]);
  144. }
  145. kfree(ice->akm);
  146. }
  147. /*
  148. * build AK4xxx controls
  149. */
  150. int snd_ice1712_akm4xxx_build_controls(struct snd_ice1712 *ice)
  151. {
  152. unsigned int akidx;
  153. int err;
  154. for (akidx = 0; akidx < ice->akm_codecs; akidx++) {
  155. struct snd_akm4xxx *ak = &ice->akm[akidx];
  156. err = snd_akm4xxx_build_controls(ak);
  157. if (err < 0)
  158. return err;
  159. }
  160. return 0;
  161. }
  162. static int __init alsa_ice1712_akm4xxx_module_init(void)
  163. {
  164. return 0;
  165. }
  166. static void __exit alsa_ice1712_akm4xxx_module_exit(void)
  167. {
  168. }
  169. module_init(alsa_ice1712_akm4xxx_module_init)
  170. module_exit(alsa_ice1712_akm4xxx_module_exit)
  171. EXPORT_SYMBOL(snd_ice1712_akm4xxx_init);
  172. EXPORT_SYMBOL(snd_ice1712_akm4xxx_free);
  173. EXPORT_SYMBOL(snd_ice1712_akm4xxx_build_controls);