cs5535audio_olpc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. snd_printk(KERN_ERR "setting High Pass Filter - %d\n", err);
  35. return;
  36. }
  37. /* set Analog Input through GPIO */
  38. gpio_set_value(OLPC_GPIO_MIC_AC, on);
  39. }
  40. /*
  41. * OLPC XO-1's V_REFOUT is a mic bias enable.
  42. */
  43. void olpc_mic_bias(struct snd_ac97 *ac97, int on)
  44. {
  45. int err;
  46. if (!machine_is_olpc())
  47. return;
  48. on = on ? 0 : 1;
  49. err = snd_ac97_update_bits(ac97, AC97_AD_MISC,
  50. 1 << AC97_AD_VREFD_SHIFT, on << AC97_AD_VREFD_SHIFT);
  51. if (err < 0)
  52. snd_printk(KERN_ERR "setting MIC Bias - %d\n", err);
  53. }
  54. static int olpc_dc_info(struct snd_kcontrol *kctl,
  55. struct snd_ctl_elem_info *uinfo)
  56. {
  57. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  58. uinfo->count = 1;
  59. uinfo->value.integer.min = 0;
  60. uinfo->value.integer.max = 1;
  61. return 0;
  62. }
  63. static int olpc_dc_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
  64. {
  65. v->value.integer.value[0] = gpio_get_value(OLPC_GPIO_MIC_AC);
  66. return 0;
  67. }
  68. static int olpc_dc_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
  69. {
  70. struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
  71. olpc_analog_input(cs5535au->ac97, v->value.integer.value[0]);
  72. return 1;
  73. }
  74. static int olpc_mic_info(struct snd_kcontrol *kctl,
  75. struct snd_ctl_elem_info *uinfo)
  76. {
  77. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  78. uinfo->count = 1;
  79. uinfo->value.integer.min = 0;
  80. uinfo->value.integer.max = 1;
  81. return 0;
  82. }
  83. static int olpc_mic_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
  84. {
  85. struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
  86. struct snd_ac97 *ac97 = cs5535au->ac97;
  87. int i;
  88. i = (snd_ac97_read(ac97, AC97_AD_MISC) >> AC97_AD_VREFD_SHIFT) & 0x1;
  89. v->value.integer.value[0] = i ? 0 : 1;
  90. return 0;
  91. }
  92. static int olpc_mic_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *v)
  93. {
  94. struct cs5535audio *cs5535au = snd_kcontrol_chip(kctl);
  95. olpc_mic_bias(cs5535au->ac97, v->value.integer.value[0]);
  96. return 1;
  97. }
  98. static struct snd_kcontrol_new olpc_cs5535audio_ctls[] __devinitdata = {
  99. {
  100. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  101. .name = "DC Mode Enable",
  102. .info = olpc_dc_info,
  103. .get = olpc_dc_get,
  104. .put = olpc_dc_put,
  105. .private_value = 0,
  106. },
  107. {
  108. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  109. .name = "MIC Bias Enable",
  110. .info = olpc_mic_info,
  111. .get = olpc_mic_get,
  112. .put = olpc_mic_put,
  113. .private_value = 0,
  114. },
  115. };
  116. void __devinit olpc_prequirks(struct snd_card *card,
  117. struct snd_ac97_template *ac97)
  118. {
  119. if (!machine_is_olpc())
  120. return;
  121. /* invert EAPD if on an OLPC B3 or higher */
  122. if (olpc_board_at_least(olpc_board_pre(0xb3)))
  123. ac97->scaps |= AC97_SCAP_INV_EAPD;
  124. }
  125. int __devinit olpc_quirks(struct snd_card *card, struct snd_ac97 *ac97)
  126. {
  127. struct snd_ctl_elem_id elem;
  128. int i, err;
  129. if (!machine_is_olpc())
  130. return 0;
  131. if (gpio_request(OLPC_GPIO_MIC_AC, DRV_NAME)) {
  132. printk(KERN_ERR DRV_NAME ": unable to allocate MIC GPIO\n");
  133. return -EIO;
  134. }
  135. gpio_direction_output(OLPC_GPIO_MIC_AC, 0);
  136. /* drop the original AD1888 HPF control */
  137. memset(&elem, 0, sizeof(elem));
  138. elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  139. strncpy(elem.name, "High Pass Filter Enable", sizeof(elem.name));
  140. snd_ctl_remove_id(card, &elem);
  141. /* drop the original V_REFOUT control */
  142. memset(&elem, 0, sizeof(elem));
  143. elem.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  144. strncpy(elem.name, "V_REFOUT Enable", sizeof(elem.name));
  145. snd_ctl_remove_id(card, &elem);
  146. /* add the OLPC-specific controls */
  147. for (i = 0; i < ARRAY_SIZE(olpc_cs5535audio_ctls); i++) {
  148. err = snd_ctl_add(card, snd_ctl_new1(&olpc_cs5535audio_ctls[i],
  149. ac97->private_data));
  150. if (err < 0) {
  151. gpio_free(OLPC_GPIO_MIC_AC);
  152. return err;
  153. }
  154. }
  155. /* turn off the mic by default */
  156. olpc_mic_bias(ac97, 0);
  157. return 0;
  158. }
  159. void __devexit olpc_quirks_cleanup(void)
  160. {
  161. gpio_free(OLPC_GPIO_MIC_AC);
  162. }