ak4xxx.c 5.0 KB

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