hx4700.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. static struct snd_soc_jack hs_jack;
  27. /* Headphones jack detection DAPM pin */
  28. static struct snd_soc_jack_pin hs_jack_pin[] = {
  29. {
  30. .pin = "Headphone Jack",
  31. .mask = SND_JACK_HEADPHONE,
  32. },
  33. {
  34. .pin = "Speaker",
  35. /* disable speaker when hp jack is inserted */
  36. .mask = SND_JACK_HEADPHONE,
  37. .invert = 1,
  38. },
  39. };
  40. /* Headphones jack detection GPIO */
  41. static struct snd_soc_jack_gpio hs_jack_gpio = {
  42. .gpio = GPIO75_HX4700_EARPHONE_nDET,
  43. .invert = true,
  44. .name = "hp-gpio",
  45. .report = SND_JACK_HEADPHONE,
  46. .debounce_time = 200,
  47. };
  48. /*
  49. * iPAQ hx4700 uses I2S for capture and playback.
  50. */
  51. static int hx4700_hw_params(struct snd_pcm_substream *substream,
  52. struct snd_pcm_hw_params *params)
  53. {
  54. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  55. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  56. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  57. int ret = 0;
  58. /* set the I2S system clock as output */
  59. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  60. SND_SOC_CLOCK_OUT);
  61. if (ret < 0)
  62. return ret;
  63. /* inform codec driver about clock freq *
  64. * (PXA I2S always uses divider 256) */
  65. ret = snd_soc_dai_set_sysclk(codec_dai, 0, 256 * params_rate(params),
  66. SND_SOC_CLOCK_IN);
  67. if (ret < 0)
  68. return ret;
  69. return 0;
  70. }
  71. static const struct snd_soc_ops hx4700_ops = {
  72. .hw_params = hx4700_hw_params,
  73. };
  74. static int hx4700_spk_power(struct snd_soc_dapm_widget *w,
  75. struct snd_kcontrol *k, int event)
  76. {
  77. gpio_set_value(GPIO107_HX4700_SPK_nSD, !!SND_SOC_DAPM_EVENT_ON(event));
  78. return 0;
  79. }
  80. static int hx4700_hp_power(struct snd_soc_dapm_widget *w,
  81. struct snd_kcontrol *k, int event)
  82. {
  83. gpio_set_value(GPIO92_HX4700_HP_DRIVER, !!SND_SOC_DAPM_EVENT_ON(event));
  84. return 0;
  85. }
  86. /* hx4700 machine dapm widgets */
  87. static const struct snd_soc_dapm_widget hx4700_dapm_widgets[] = {
  88. SND_SOC_DAPM_HP("Headphone Jack", hx4700_hp_power),
  89. SND_SOC_DAPM_SPK("Speaker", hx4700_spk_power),
  90. SND_SOC_DAPM_MIC("Built-in Microphone", NULL),
  91. };
  92. /* hx4700 machine audio_map */
  93. static const struct snd_soc_dapm_route hx4700_audio_map[] = {
  94. /* Headphone connected to LOUT, ROUT */
  95. {"Headphone Jack", NULL, "LOUT"},
  96. {"Headphone Jack", NULL, "ROUT"},
  97. /* Speaker connected to MOUT2 */
  98. {"Speaker", NULL, "MOUT2"},
  99. /* Microphone connected to MICIN */
  100. {"MICIN", NULL, "Built-in Microphone"},
  101. {"AIN", NULL, "MICOUT"},
  102. };
  103. /*
  104. * Logic for a ak4641 as connected on a HP iPAQ hx4700
  105. */
  106. static int hx4700_ak4641_init(struct snd_soc_pcm_runtime *rtd)
  107. {
  108. int err;
  109. /* Jack detection API stuff */
  110. err = snd_soc_card_jack_new(rtd->card, "Headphone Jack",
  111. SND_JACK_HEADPHONE, &hs_jack, hs_jack_pin,
  112. ARRAY_SIZE(hs_jack_pin));
  113. if (err)
  114. return err;
  115. err = snd_soc_jack_add_gpios(&hs_jack, 1, &hs_jack_gpio);
  116. return err;
  117. }
  118. /* hx4700 digital audio interface glue - connects codec <--> CPU */
  119. static struct snd_soc_dai_link hx4700_dai = {
  120. .name = "ak4641",
  121. .stream_name = "AK4641",
  122. .cpu_dai_name = "pxa2xx-i2s",
  123. .codec_dai_name = "ak4641-hifi",
  124. .platform_name = "pxa-pcm-audio",
  125. .codec_name = "ak4641.0-0012",
  126. .init = hx4700_ak4641_init,
  127. .dai_fmt = SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
  128. SND_SOC_DAIFMT_CBS_CFS,
  129. .ops = &hx4700_ops,
  130. };
  131. /* hx4700 audio machine driver */
  132. static struct snd_soc_card snd_soc_card_hx4700 = {
  133. .name = "iPAQ hx4700",
  134. .owner = THIS_MODULE,
  135. .dai_link = &hx4700_dai,
  136. .num_links = 1,
  137. .dapm_widgets = hx4700_dapm_widgets,
  138. .num_dapm_widgets = ARRAY_SIZE(hx4700_dapm_widgets),
  139. .dapm_routes = hx4700_audio_map,
  140. .num_dapm_routes = ARRAY_SIZE(hx4700_audio_map),
  141. .fully_routed = true,
  142. };
  143. static struct gpio hx4700_audio_gpios[] = {
  144. { GPIO107_HX4700_SPK_nSD, GPIOF_OUT_INIT_HIGH, "SPK_POWER" },
  145. { GPIO92_HX4700_HP_DRIVER, GPIOF_OUT_INIT_LOW, "EP_POWER" },
  146. };
  147. static int hx4700_audio_probe(struct platform_device *pdev)
  148. {
  149. int ret;
  150. if (!machine_is_h4700())
  151. return -ENODEV;
  152. ret = gpio_request_array(hx4700_audio_gpios,
  153. ARRAY_SIZE(hx4700_audio_gpios));
  154. if (ret)
  155. return ret;
  156. snd_soc_card_hx4700.dev = &pdev->dev;
  157. ret = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_hx4700);
  158. if (ret)
  159. gpio_free_array(hx4700_audio_gpios,
  160. ARRAY_SIZE(hx4700_audio_gpios));
  161. return ret;
  162. }
  163. static int hx4700_audio_remove(struct platform_device *pdev)
  164. {
  165. gpio_set_value(GPIO92_HX4700_HP_DRIVER, 0);
  166. gpio_set_value(GPIO107_HX4700_SPK_nSD, 0);
  167. gpio_free_array(hx4700_audio_gpios, ARRAY_SIZE(hx4700_audio_gpios));
  168. return 0;
  169. }
  170. static struct platform_driver hx4700_audio_driver = {
  171. .driver = {
  172. .name = "hx4700-audio",
  173. .pm = &snd_soc_pm_ops,
  174. },
  175. .probe = hx4700_audio_probe,
  176. .remove = hx4700_audio_remove,
  177. };
  178. module_platform_driver(hx4700_audio_driver);
  179. MODULE_AUTHOR("Philipp Zabel");
  180. MODULE_DESCRIPTION("ALSA SoC iPAQ hx4700");
  181. MODULE_LICENSE("GPL");
  182. MODULE_ALIAS("platform:hx4700-audio");