ak4xxx.c 4.1 KB

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