hx4700.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * SoC audio for HP iPAQ hx4700
  3. *
  4. * Copyright (c) 2009 Philipp Zabel
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/timer.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/delay.h>
  17. #include <linux/gpio.h>
  18. #include <sound/core.h>
  19. #include <sound/jack.h>
  20. #include <sound/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc.h>
  23. #include <mach/hx4700.h>
  24. #include <asm/mach-types.h>
  25. #include "pxa2xx-i2s.h"
  26. #include "../codecs/ak4641.h"
  27. static struct snd_soc_jack hs_jack;
  28. /* Headphones jack detection DAPM pin */
  29. static struct snd_soc_jack_pin hs_jack_pin[] = {
  30. {
  31. .pin = "Headphone Jack",
  32. .mask = SND_JACK_HEADPHONE,
  33. },
  34. {
  35. .pin = "Speaker",
  36. /* disable speaker when hp jack is inserted */
  37. .mask = SND_JACK_HEADPHONE,
  38. .invert = 1,
  39. },
  40. };
  41. /* Headphones jack detection GPIO */
  42. static struct snd_soc_jack_gpio hs_jack_gpio = {
  43. .gpio = GPIO75_HX4700_EARPHONE_nDET,
  44. .invert = true,
  45. .name = "hp-gpio",
  46. .report = SND_JACK_HEADPHONE,
  47. .debounce_time = 200,
  48. };
  49. /*
  50. * iPAQ hx4700 uses I2S for capture and playback.
  51. */
  52. static int hx4700_hw_params(struct snd_pcm_substream *substream,
  53. struct snd_pcm_hw_params *params)
  54. {
  55. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  56. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  57. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  58. int ret = 0;
  59. /* set the I2S system clock as output */
  60. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  61. SND_SOC_CLOCK_OUT);
  62. if (ret < 0)
  63. return ret;
  64. /* inform codec driver about clock freq *
  65. * (PXA I2S always uses divider 256) */
  66. ret = snd_soc_dai_set_sysclk(codec_dai, 0, 256 * params_rate(params),
  67. SND_SOC_CLOCK_IN);
  68. if (ret < 0)
  69. return ret;
  70. return 0;
  71. }
  72. static struct snd_soc_ops hx4700_ops = {
  73. .hw_params = hx4700_hw_params,
  74. };
  75. static int hx4700_spk_power(struct snd_soc_dapm_widget *w,
  76. struct snd_kcontrol *k, int event)
  77. {
  78. gpio_set_value(GPIO107_HX4700_SPK_nSD, !!SND_SOC_DAPM_EVENT_ON(event));
  79. return 0;
  80. }
  81. static int hx4700_hp_power(struct snd_soc_dapm_widget *w,
  82. struct snd_kcontrol *k, int event)
  83. {
  84. gpio_set_value(GPIO92_HX4700_HP_DRIVER, !!SND_SOC_DAPM_EVENT_ON(event));
  85. return 0;
  86. }
  87. /* hx4700 machine dapm widgets */
  88. static const struct snd_soc_dapm_widget hx4700_dapm_widgets[] = {
  89. SND_SOC_DAPM_HP("Headphone Jack", hx4700_hp_power),
  90. SND_SOC_DAPM_SPK("Speaker", hx4700_spk_power),
  91. SND_SOC_DAPM_MIC("Built-in Microphone", NULL),
  92. };
  93. /* hx4700 machine audio_map */
  94. static const struct snd_soc_dapm_route hx4700_audio_map[] = {
  95. /* Headphone connected to LOUT, ROUT */
  96. {"Headphone Jack", NULL, "LOUT"},
  97. {"Headphone Jack", NULL, "ROUT"},
  98. /* Speaker connected to MOUT2 */
  99. {"Speaker", NULL, "MOUT2"},
  100. /* Microphone connected to MICIN */
  101. {"MICIN", NULL, "Built-in Microphone"},
  102. {"AIN", NULL, "MICOUT"},
  103. };
  104. /*
  105. * Logic for a ak4641 as connected on a HP iPAQ hx4700
  106. */
  107. static int hx4700_ak4641_init(struct snd_soc_pcm_runtime *rtd)
  108. {
  109. struct snd_soc_codec *codec = rtd->codec;
  110. struct snd_soc_dapm_context *dapm = &codec->dapm;
  111. int err;
  112. /* NC codec pins */
  113. /* FIXME: is anything connected here? */
  114. snd_soc_dapm_nc_pin(dapm, "MOUT1");
  115. snd_soc_dapm_nc_pin(dapm, "MICEXT");
  116. snd_soc_dapm_nc_pin(dapm, "AUX");
  117. /* Jack detection API stuff */
  118. err = snd_soc_jack_new(codec, "Headphone Jack",
  119. SND_JACK_HEADPHONE, &hs_jack);
  120. if (err)
  121. return err;
  122. err = snd_soc_jack_add_pins(&hs_jack, ARRAY_SIZE(hs_jack_pin),
  123. hs_jack_pin);
  124. if (err)
  125. return err;
  126. err = snd_soc_jack_add_gpios(&hs_jack, 1, &hs_jack_gpio);
  127. return err;
  128. }
  129. /* hx4700 digital audio interface glue - connects codec <--> CPU */
  130. static struct snd_soc_dai_link hx4700_dai = {
  131. .name = "ak4641",
  132. .stream_name = "AK4641",
  133. .cpu_dai_name = "pxa2xx-i2s",
  134. .codec_dai_name = "ak4641-hifi",
  135. .platform_name = "pxa-pcm-audio",
  136. .codec_name = "ak4641.0-0012",
  137. .init = hx4700_ak4641_init,
  138. .dai_fmt = SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
  139. SND_SOC_DAIFMT_CBS_CFS,
  140. .ops = &hx4700_ops,
  141. };
  142. /* hx4700 audio machine driver */
  143. static struct snd_soc_card snd_soc_card_hx4700 = {
  144. .name = "iPAQ hx4700",
  145. .owner = THIS_MODULE,
  146. .dai_link = &hx4700_dai,
  147. .num_links = 1,
  148. .dapm_widgets = hx4700_dapm_widgets,
  149. .num_dapm_widgets = ARRAY_SIZE(hx4700_dapm_widgets),
  150. .dapm_routes = hx4700_audio_map,
  151. .num_dapm_routes = ARRAY_SIZE(hx4700_audio_map),
  152. };
  153. static struct gpio hx4700_audio_gpios[] = {
  154. { GPIO107_HX4700_SPK_nSD, GPIOF_OUT_INIT_HIGH, "SPK_POWER" },
  155. { GPIO92_HX4700_HP_DRIVER, GPIOF_OUT_INIT_LOW, "EP_POWER" },
  156. };
  157. static int __devinit hx4700_audio_probe(struct platform_device *pdev)
  158. {
  159. int ret;
  160. if (!machine_is_h4700())
  161. return -ENODEV;
  162. ret = gpio_request_array(hx4700_audio_gpios,
  163. ARRAY_SIZE(hx4700_audio_gpios));
  164. if (ret)
  165. return ret;
  166. snd_soc_card_hx4700.dev = &pdev->dev;
  167. ret = snd_soc_register_card(&snd_soc_card_hx4700);
  168. if (ret)
  169. gpio_free_array(hx4700_audio_gpios,
  170. ARRAY_SIZE(hx4700_audio_gpios));
  171. return ret;
  172. }
  173. static int __devexit hx4700_audio_remove(struct platform_device *pdev)
  174. {
  175. snd_soc_jack_free_gpios(&hs_jack, 1, &hs_jack_gpio);
  176. snd_soc_unregister_card(&snd_soc_card_hx4700);
  177. gpio_set_value(GPIO92_HX4700_HP_DRIVER, 0);
  178. gpio_set_value(GPIO107_HX4700_SPK_nSD, 0);
  179. gpio_free_array(hx4700_audio_gpios, ARRAY_SIZE(hx4700_audio_gpios));
  180. return 0;
  181. }
  182. static struct platform_driver hx4700_audio_driver = {
  183. .driver = {
  184. .name = "hx4700-audio",
  185. .owner = THIS_MODULE,
  186. .pm = &snd_soc_pm_ops,
  187. },
  188. .probe = hx4700_audio_probe,
  189. .remove = __devexit_p(hx4700_audio_remove),
  190. };
  191. module_platform_driver(hx4700_audio_driver);
  192. MODULE_AUTHOR("Philipp Zabel");
  193. MODULE_DESCRIPTION("ALSA SoC iPAQ hx4700");
  194. MODULE_LICENSE("GPL");
  195. MODULE_ALIAS("platform:hx4700-audio");