ak4xxx.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/slab.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@perex.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. if (snd_BUG_ON(chip < 0 || chip >= 4))
  56. return;
  57. tmp = snd_ice1712_gpio_read(ice);
  58. tmp |= priv->add_flags;
  59. tmp &= ~priv->mask_flags;
  60. if (priv->cs_mask == priv->cs_addr) {
  61. if (priv->cif) {
  62. tmp |= priv->cs_mask; /* start without chip select */
  63. } else {
  64. tmp &= ~priv->cs_mask; /* chip select low */
  65. snd_ice1712_gpio_write(ice, tmp);
  66. udelay(1);
  67. }
  68. } else {
  69. /* doesn't handle cf=1 yet */
  70. tmp &= ~priv->cs_mask;
  71. tmp |= priv->cs_addr;
  72. snd_ice1712_gpio_write(ice, tmp);
  73. udelay(1);
  74. }
  75. /* build I2C address + data byte */
  76. addrdata = (priv->caddr << 6) | 0x20 | (addr & 0x1f);
  77. addrdata = (addrdata << 8) | data;
  78. for (idx = 15; idx >= 0; idx--) {
  79. /* drop clock */
  80. tmp &= ~priv->clk_mask;
  81. snd_ice1712_gpio_write(ice, tmp);
  82. udelay(1);
  83. /* set data */
  84. if (addrdata & (1 << idx))
  85. tmp |= priv->data_mask;
  86. else
  87. tmp &= ~priv->data_mask;
  88. snd_ice1712_gpio_write(ice, tmp);
  89. udelay(1);
  90. /* raise clock */
  91. tmp |= priv->clk_mask;
  92. snd_ice1712_gpio_write(ice, tmp);
  93. udelay(1);
  94. }
  95. if (priv->cs_mask == priv->cs_addr) {
  96. if (priv->cif) {
  97. /* assert a cs pulse to trigger */
  98. tmp &= ~priv->cs_mask;
  99. snd_ice1712_gpio_write(ice, tmp);
  100. udelay(1);
  101. }
  102. tmp |= priv->cs_mask; /* chip select high to trigger */
  103. } else {
  104. tmp &= ~priv->cs_mask;
  105. tmp |= priv->cs_none; /* deselect address */
  106. }
  107. snd_ice1712_gpio_write(ice, tmp);
  108. udelay(1);
  109. }
  110. /*
  111. * initialize the struct snd_akm4xxx record with the template
  112. */
  113. int snd_ice1712_akm4xxx_init(struct snd_akm4xxx *ak, const struct snd_akm4xxx *temp,
  114. const struct snd_ak4xxx_private *_priv, struct snd_ice1712 *ice)
  115. {
  116. struct snd_ak4xxx_private *priv;
  117. if (_priv != NULL) {
  118. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  119. if (priv == NULL)
  120. return -ENOMEM;
  121. *priv = *_priv;
  122. } else {
  123. priv = NULL;
  124. }
  125. *ak = *temp;
  126. ak->card = ice->card;
  127. ak->private_value[0] = (unsigned long)priv;
  128. ak->private_data[0] = ice;
  129. if (ak->ops.lock == NULL)
  130. ak->ops.lock = snd_ice1712_akm4xxx_lock;
  131. if (ak->ops.unlock == NULL)
  132. ak->ops.unlock = snd_ice1712_akm4xxx_unlock;
  133. if (ak->ops.write == NULL)
  134. ak->ops.write = snd_ice1712_akm4xxx_write;
  135. snd_akm4xxx_init(ak);
  136. return 0;
  137. }
  138. void snd_ice1712_akm4xxx_free(struct snd_ice1712 *ice)
  139. {
  140. unsigned int akidx;
  141. if (ice->akm == NULL)
  142. return;
  143. for (akidx = 0; akidx < ice->akm_codecs; akidx++) {
  144. struct snd_akm4xxx *ak = &ice->akm[akidx];
  145. kfree((void*)ak->private_value[0]);
  146. }
  147. kfree(ice->akm);
  148. }
  149. /*
  150. * build AK4xxx controls
  151. */
  152. int snd_ice1712_akm4xxx_build_controls(struct snd_ice1712 *ice)
  153. {
  154. unsigned int akidx;
  155. int err;
  156. for (akidx = 0; akidx < ice->akm_codecs; akidx++) {
  157. struct snd_akm4xxx *ak = &ice->akm[akidx];
  158. err = snd_akm4xxx_build_controls(ak);
  159. if (err < 0)
  160. return err;
  161. }
  162. return 0;
  163. }
  164. static int __init alsa_ice1712_akm4xxx_module_init(void)
  165. {
  166. return 0;
  167. }
  168. static void __exit alsa_ice1712_akm4xxx_module_exit(void)
  169. {
  170. }
  171. module_init(alsa_ice1712_akm4xxx_module_init)
  172. module_exit(alsa_ice1712_akm4xxx_module_exit)
  173. EXPORT_SYMBOL(snd_ice1712_akm4xxx_init);
  174. EXPORT_SYMBOL(snd_ice1712_akm4xxx_free);
  175. EXPORT_SYMBOL(snd_ice1712_akm4xxx_build_controls);