hx4700.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. int err;
  110. /* Jack detection API stuff */
  111. err = snd_soc_card_jack_new(rtd->card, "Headphone Jack",
  112. SND_JACK_HEADPHONE, &hs_jack, hs_jack_pin,
  113. ARRAY_SIZE(hs_jack_pin));
  114. if (err)
  115. return err;
  116. err = snd_soc_jack_add_gpios(&hs_jack, 1, &hs_jack_gpio);
  117. return err;
  118. }
  119. static int hx4700_card_remove(struct snd_soc_card *card)
  120. {
  121. snd_soc_jack_free_gpios(&hs_jack, 1, &hs_jack_gpio);
  122. return 0;
  123. }
  124. /* hx4700 digital audio interface glue - connects codec <--> CPU */
  125. static struct snd_soc_dai_link hx4700_dai = {
  126. .name = "ak4641",
  127. .stream_name = "AK4641",
  128. .cpu_dai_name = "pxa2xx-i2s",
  129. .codec_dai_name = "ak4641-hifi",
  130. .platform_name = "pxa-pcm-audio",
  131. .codec_name = "ak4641.0-0012",
  132. .init = hx4700_ak4641_init,
  133. .dai_fmt = SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
  134. SND_SOC_DAIFMT_CBS_CFS,
  135. .ops = &hx4700_ops,
  136. };
  137. /* hx4700 audio machine driver */
  138. static struct snd_soc_card snd_soc_card_hx4700 = {
  139. .name = "iPAQ hx4700",
  140. .owner = THIS_MODULE,
  141. .remove = hx4700_card_remove,
  142. .dai_link = &hx4700_dai,
  143. .num_links = 1,
  144. .dapm_widgets = hx4700_dapm_widgets,
  145. .num_dapm_widgets = ARRAY_SIZE(hx4700_dapm_widgets),
  146. .dapm_routes = hx4700_audio_map,
  147. .num_dapm_routes = ARRAY_SIZE(hx4700_audio_map),
  148. .fully_routed = true,
  149. };
  150. static struct gpio hx4700_audio_gpios[] = {
  151. { GPIO107_HX4700_SPK_nSD, GPIOF_OUT_INIT_HIGH, "SPK_POWER" },
  152. { GPIO92_HX4700_HP_DRIVER, GPIOF_OUT_INIT_LOW, "EP_POWER" },
  153. };
  154. static int hx4700_audio_probe(struct platform_device *pdev)
  155. {
  156. int ret;
  157. if (!machine_is_h4700())
  158. return -ENODEV;
  159. ret = gpio_request_array(hx4700_audio_gpios,
  160. ARRAY_SIZE(hx4700_audio_gpios));
  161. if (ret)
  162. return ret;
  163. snd_soc_card_hx4700.dev = &pdev->dev;
  164. ret = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_hx4700);
  165. if (ret)
  166. gpio_free_array(hx4700_audio_gpios,
  167. ARRAY_SIZE(hx4700_audio_gpios));
  168. return ret;
  169. }
  170. static int hx4700_audio_remove(struct platform_device *pdev)
  171. {
  172. gpio_set_value(GPIO92_HX4700_HP_DRIVER, 0);
  173. gpio_set_value(GPIO107_HX4700_SPK_nSD, 0);
  174. gpio_free_array(hx4700_audio_gpios, ARRAY_SIZE(hx4700_audio_gpios));
  175. return 0;
  176. }
  177. static struct platform_driver hx4700_audio_driver = {
  178. .driver = {
  179. .name = "hx4700-audio",
  180. .pm = &snd_soc_pm_ops,
  181. },
  182. .probe = hx4700_audio_probe,
  183. .remove = hx4700_audio_remove,
  184. };
  185. module_platform_driver(hx4700_audio_driver);
  186. MODULE_AUTHOR("Philipp Zabel");
  187. MODULE_DESCRIPTION("ALSA SoC iPAQ hx4700");
  188. MODULE_LICENSE("GPL");
  189. MODULE_ALIAS("platform:hx4700-audio");