ak4xxx.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * ALSA driver for ICEnsemble ICE1712 (Envy24)
  3. *
  4. * AK4524 / AK4528 / AK4529 / AK4355 / AK4381 interface
  5. *
  6. * Copyright (c) 2000 Jaroslav Kysela <perex@suse.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 <sound/driver.h>
  24. #include <asm/io.h>
  25. #include <linux/delay.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/init.h>
  28. #include <sound/core.h>
  29. #include <sound/initval.h>
  30. #include "ice1712.h"
  31. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  32. MODULE_DESCRIPTION("ICEnsemble ICE17xx <-> AK4xxx AD/DA chip interface");
  33. MODULE_LICENSE("GPL");
  34. static void snd_ice1712_akm4xxx_lock(struct snd_akm4xxx *ak, int chip)
  35. {
  36. struct snd_ice1712 *ice = ak->private_data[0];
  37. snd_ice1712_save_gpio_status(ice);
  38. }
  39. static void snd_ice1712_akm4xxx_unlock(struct snd_akm4xxx *ak, int chip)
  40. {
  41. struct snd_ice1712 *ice = ak->private_data[0];
  42. snd_ice1712_restore_gpio_status(ice);
  43. }
  44. /*
  45. * write AK4xxx register
  46. */
  47. static void snd_ice1712_akm4xxx_write(struct snd_akm4xxx *ak, int chip,
  48. unsigned char addr, unsigned char data)
  49. {
  50. unsigned int tmp;
  51. int idx;
  52. unsigned int addrdata;
  53. struct snd_ak4xxx_private *priv = (void *)ak->private_value[0];
  54. struct snd_ice1712 *ice = ak->private_data[0];
  55. snd_assert(chip >= 0 && chip < 4, return);
  56. tmp = snd_ice1712_gpio_read(ice);
  57. tmp |= priv->add_flags;
  58. tmp &= ~priv->mask_flags;
  59. if (priv->cs_mask == priv->cs_addr) {
  60. if (priv->cif) {
  61. tmp |= priv->cs_mask; /* start without chip select */
  62. } else {
  63. tmp &= ~priv->cs_mask; /* chip select low */
  64. snd_ice1712_gpio_write(ice, tmp);
  65. udelay(1);
  66. }
  67. } else {
  68. /* doesn't handle cf=1 yet */
  69. tmp &= ~priv->cs_mask;
  70. tmp |= priv->cs_addr;
  71. snd_ice1712_gpio_write(ice, tmp);
  72. udelay(1);
  73. }
  74. /* build I2C address + data byte */
  75. addrdata = (priv->caddr << 6) | 0x20 | (addr & 0x1f);
  76. addrdata = (addrdata << 8) | data;
  77. for (idx = 15; idx >= 0; idx--) {
  78. /* drop clock */
  79. tmp &= ~priv->clk_mask;
  80. snd_ice1712_gpio_write(ice, tmp);
  81. udelay(1);
  82. /* set data */
  83. if (addrdata & (1 << idx))
  84. tmp |= priv->data_mask;
  85. else
  86. tmp &= ~priv->data_mask;
  87. snd_ice1712_gpio_write(ice, tmp);
  88. udelay(1);
  89. /* raise clock */
  90. tmp |= priv->clk_mask;
  91. snd_ice1712_gpio_write(ice, tmp);
  92. udelay(1);
  93. }
  94. if (priv->cs_mask == priv->cs_addr) {
  95. if (priv->cif) {
  96. /* assert a cs pulse to trigger */
  97. tmp &= ~priv->cs_mask;
  98. snd_ice1712_gpio_write(ice, tmp);
  99. udelay(1);
  100. }
  101. tmp |= priv->cs_mask; /* chip select high to trigger */
  102. } else {
  103. tmp &= ~priv->cs_mask;
  104. tmp |= priv->cs_none; /* deselect address */
  105. }
  106. snd_ice1712_gpio_write(ice, tmp);
  107. udelay(1);
  108. }
  109. /*
  110. * initialize the struct snd_akm4xxx record with the template
  111. */
  112. int snd_ice1712_akm4xxx_init(struct snd_akm4xxx *ak, const struct snd_akm4xxx *temp,
  113. const struct snd_ak4xxx_private *_priv, struct snd_ice1712 *ice)
  114. {
  115. struct snd_ak4xxx_private *priv;
  116. if (_priv != NULL) {
  117. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  118. if (priv == NULL)
  119. return -ENOMEM;
  120. *priv = *_priv;
  121. } else {
  122. priv = NULL;
  123. }
  124. *ak = *temp;
  125. ak->card = ice->card;
  126. ak->private_value[0] = (unsigned long)priv;
  127. ak->private_data[0] = ice;
  128. if (ak->ops.lock == NULL)
  129. ak->ops.lock = snd_ice1712_akm4xxx_lock;
  130. if (ak->ops.unlock == NULL)
  131. ak->ops.unlock = snd_ice1712_akm4xxx_unlock;
  132. if (ak->ops.write == NULL)
  133. ak->ops.write = snd_ice1712_akm4xxx_write;
  134. snd_akm4xxx_init(ak);
  135. return 0;
  136. }
  137. void snd_ice1712_akm4xxx_free(struct snd_ice1712 *ice)
  138. {
  139. unsigned int akidx;
  140. if (ice->akm == NULL)
  141. return;
  142. for (akidx = 0; akidx < ice->akm_codecs; akidx++) {
  143. struct snd_akm4xxx *ak = &ice->akm[akidx];
  144. kfree((void*)ak->private_value[0]);
  145. }
  146. kfree(ice->akm);
  147. }
  148. /*
  149. * build AK4xxx controls
  150. */
  151. int snd_ice1712_akm4xxx_build_controls(struct snd_ice1712 *ice)
  152. {
  153. unsigned int akidx;
  154. int err;
  155. for (akidx = 0; akidx < ice->akm_codecs; akidx++) {
  156. struct snd_akm4xxx *ak = &ice->akm[akidx];
  157. err = snd_akm4xxx_build_controls(ak);
  158. if (err < 0)
  159. return err;
  160. }
  161. return 0;
  162. }
  163. static int __init alsa_ice1712_akm4xxx_module_init(void)
  164. {
  165. return 0;
  166. }
  167. static void __exit alsa_ice1712_akm4xxx_module_exit(void)
  168. {
  169. }
  170. module_init(alsa_ice1712_akm4xxx_module_init)
  171. module_exit(alsa_ice1712_akm4xxx_module_exit)
  172. EXPORT_SYMBOL(snd_ice1712_akm4xxx_init);
  173. EXPORT_SYMBOL(snd_ice1712_akm4xxx_free);
  174. EXPORT_SYMBOL(snd_ice1712_akm4xxx_build_controls);