cs5535audio_olpc.c 4.5 KB

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