cs5535audio_olpc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * OLPC XO-1 additional sound features
  3. *
  4. * Copyright © 2006 Jaya Kumar <jayakumar.lkml@gmail.com>
  5. * Copyright © 2007-2008 Andres Salomon <dilinger@debian.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <sound/core.h>
  13. #include <sound/info.h>
  14. #include <sound/control.h>
  15. #include <sound/ac97_codec.h>
  16. #include <linux/gpio.h>
  17. #include <asm/olpc.h>
  18. #include "cs5535audio.h"
  19. #define DRV_NAME "cs5535audio-olpc"
  20. /*
  21. * OLPC has an additional feature on top of the regular AD1888 codec features.
  22. * It has an Analog Input mode that is switched into (after disabling the
  23. * High Pass Filter) via GPIO. It is supported on B2 and later models.
  24. */
  25. void olpc_analog_input(struct snd_ac97 *ac97, int on)
  26. {
  27. int err;
  28. if (!machine_is_olpc())
  29. return;
  30. /* update the High Pass Filter (via AC97_AD_TEST2) */
  31. err = snd_ac97_update_bits(ac97, AC97_AD_TEST2,
  32. 1 << AC97_AD_HPFD_SHIFT, on << AC97_AD_HPFD_SHIFT);
  33. if (err < 0) {
  34. dev_err(ac97->bus->card->dev,
  35. "setting High Pass Filter - %d\n", err);
  36. return;
  37. }
  38. /* set Analog Input through GPIO */
  39. gpio_set_value(OLPC_GPIO_MIC_AC, on);
  40. }
  41. /*
  42. * OLPC XO-1's V_REFOUT is a mic bias enable.
  43. */
  44. void olpc_mic_bias(struct snd_ac97 *ac97, int on)
  45. {
  46. int err;
  47. if (!machine_is_olpc())
  48. return;
  49. on = on ? 0 : 1;
  50. err = snd_ac97_update_bits(ac97, AC97_AD_MISC,
  51. 1 << AC97_AD_VREFD_SHIFT, on << AC97_AD_VREFD_SHIFT);
  52. if (err < 0)
  53. dev_err(ac97->bus->card->dev, "setting MIC Bias - %d\n", err);
  54. }
  55. static int olpc_dc_info(struct snd_kcontrol *kctl,
  56. struct snd_ctl_elem_info *uinfo)
  57. {
  58. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  59. uinfo->count = 1;
  60. uinfo->value.integer.min = 0;
  61. uinfo->value.integer.max = 1;
  62. return 0;
  63. }
  64. static int olpc_dc_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
  65. {
  66. v->value.integer.value[0] = gpio_get_value(OLPC_GPIO_MIC_AC);
  67. return 0;
  68. }
  69. static int olpc_dc_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
  70. {
  71. struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
  72. olpc_analog_input(cs5535au->ac97, v->value.integer.value[0]);
  73. return 1;
  74. }
  75. static int olpc_mic_info(struct snd_kcontrol *kctl,
  76. struct snd_ctl_elem_info *uinfo)
  77. {
  78. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  79. uinfo->count = 1;
  80. uinfo->value.integer.min = 0;
  81. uinfo->value.integer.max = 1;
  82. return 0;
  83. }
  84. static int olpc_mic_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
  85. {
  86. struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
  87. struct snd_ac97 *ac97 = cs5535au->ac97;
  88. int i;
  89. i = (snd_ac97_read(ac97, AC97_AD_MISC) >> AC97_AD_VREFD_SHIFT) & 0x1;
  90. v->value.integer.value[0] = i ? 0 : 1;
  91. return 0;
  92. }
  93. static int olpc_mic_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
  94. {
  95. struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
  96. olpc_mic_bias(cs5535au->ac97, v->value.integer.value[0]);
  97. return 1;
  98. }
  99. static struct snd_kcontrol_new olpc_cs5535audio_ctls[] = {
  100. {
  101. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  102. .name = "DC Mode Enable",
  103. .info = olpc_dc_info,
  104. .get = olpc_dc_get,
  105. .put = olpc_dc_put,
  106. .private_value = 0,
  107. },
  108. {
  109. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  110. .name = "MIC Bias Enable",
  111. .info = olpc_mic_info,
  112. .get = olpc_mic_get,
  113. .put = olpc_mic_put,
  114. .private_value = 0,
  115. },
  116. };
  117. void olpc_prequirks(struct snd_card *card,
  118. struct snd_ac97_template *ac97)
  119. {
  120. if (!machine_is_olpc())
  121. return;
  122. /* invert EAPD if on an OLPC B3 or higher */
  123. if (olpc_board_at_least(olpc_board_pre(0xb3)))
  124. ac97->scaps |= AC97_SCAP_INV_EAPD;
  125. }
  126. int olpc_quirks(struct snd_card *card, struct snd_ac97 *ac97)
  127. {
  128. struct snd_ctl_elem_id elem;
  129. int i, err;
  130. if (!machine_is_olpc())
  131. return 0;
  132. if (gpio_request(OLPC_GPIO_MIC_AC, DRV_NAME)) {
  133. dev_err(card->dev, "unable to allocate MIC GPIO\n");
  134. return -EIO;
  135. }
  136. gpio_direction_output(OLPC_GPIO_MIC_AC, 0);
  137. /* drop the original AD1888 HPF control */
  138. memset(&elem, 0, sizeof(elem));
  139. elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  140. strlcpy(elem.name, "High Pass Filter Enable", sizeof(elem.name));
  141. snd_ctl_remove_id(card, &elem);
  142. /* drop the original V_REFOUT control */
  143. memset(&elem, 0, sizeof(elem));
  144. elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  145. strlcpy(elem.name, "V_REFOUT Enable", sizeof(elem.name));
  146. snd_ctl_remove_id(card, &elem);
  147. /* add the OLPC-specific controls */
  148. for (i = 0; i < ARRAY_SIZE(olpc_cs5535audio_ctls); i++) {
  149. err = snd_ctl_add(card, snd_ctl_new1(&olpc_cs5535audio_ctls[i],
  150. ac97->private_data));
  151. if (err < 0) {
  152. gpio_free(OLPC_GPIO_MIC_AC);
  153. return err;
  154. }
  155. }
  156. /* turn off the mic by default */
  157. olpc_mic_bias(ac97, 0);
  158. return 0;
  159. }
  160. void olpc_quirks_cleanup(void)
  161. {
  162. gpio_free(OLPC_GPIO_MIC_AC);
  163. }