hx4700.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 codec DAI configuration */
  60. ret = snd_soc_dai_set_fmt(codec_dai,
  61. SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
  62. SND_SOC_DAIFMT_CBS_CFS);
  63. if (ret < 0)
  64. return ret;
  65. /* set cpu DAI configuration */
  66. ret = snd_soc_dai_set_fmt(cpu_dai,
  67. SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
  68. SND_SOC_DAIFMT_CBS_CFS);
  69. if (ret < 0)
  70. return ret;
  71. /* set the I2S system clock as output */
  72. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  73. SND_SOC_CLOCK_OUT);
  74. if (ret < 0)
  75. return ret;
  76. /* inform codec driver about clock freq *
  77. * (PXA I2S always uses divider 256) */
  78. ret = snd_soc_dai_set_sysclk(codec_dai, 0, 256 * params_rate(params),
  79. SND_SOC_CLOCK_IN);
  80. if (ret < 0)
  81. return ret;
  82. return 0;
  83. }
  84. static struct snd_soc_ops hx4700_ops = {
  85. .hw_params = hx4700_hw_params,
  86. };
  87. static int hx4700_spk_power(struct snd_soc_dapm_widget *w,
  88. struct snd_kcontrol *k, int event)
  89. {
  90. gpio_set_value(GPIO107_HX4700_SPK_nSD, !!SND_SOC_DAPM_EVENT_ON(event));
  91. return 0;
  92. }
  93. static int hx4700_hp_power(struct snd_soc_dapm_widget *w,
  94. struct snd_kcontrol *k, int event)
  95. {
  96. gpio_set_value(GPIO92_HX4700_HP_DRIVER, !!SND_SOC_DAPM_EVENT_ON(event));
  97. return 0;
  98. }
  99. /* hx4700 machine dapm widgets */
  100. static const struct snd_soc_dapm_widget hx4700_dapm_widgets[] = {
  101. SND_SOC_DAPM_HP("Headphone Jack", hx4700_hp_power),
  102. SND_SOC_DAPM_SPK("Speaker", hx4700_spk_power),
  103. SND_SOC_DAPM_MIC("Built-in Microphone", NULL),
  104. };
  105. /* hx4700 machine audio_map */
  106. static const struct snd_soc_dapm_route hx4700_audio_map[] = {
  107. /* Headphone connected to LOUT, ROUT */
  108. {"Headphone Jack", NULL, "LOUT"},
  109. {"Headphone Jack", NULL, "ROUT"},
  110. /* Speaker connected to MOUT2 */
  111. {"Speaker", NULL, "MOUT2"},
  112. /* Microphone connected to MICIN */
  113. {"MICIN", NULL, "Built-in Microphone"},
  114. {"AIN", NULL, "MICOUT"},
  115. };
  116. /*
  117. * Logic for a ak4641 as connected on a HP iPAQ hx4700
  118. */
  119. static int hx4700_ak4641_init(struct snd_soc_pcm_runtime *rtd)
  120. {
  121. struct snd_soc_codec *codec = rtd->codec;
  122. struct snd_soc_dapm_context *dapm = &codec->dapm;
  123. int err;
  124. /* NC codec pins */
  125. /* FIXME: is anything connected here? */
  126. snd_soc_dapm_nc_pin(dapm, "MOUT1");
  127. snd_soc_dapm_nc_pin(dapm, "MICEXT");
  128. snd_soc_dapm_nc_pin(dapm, "AUX");
  129. /* Jack detection API stuff */
  130. err = snd_soc_jack_new(codec, "Headphone Jack",
  131. SND_JACK_HEADPHONE, &hs_jack);
  132. if (err)
  133. return err;
  134. err = snd_soc_jack_add_pins(&hs_jack, ARRAY_SIZE(hs_jack_pin),
  135. hs_jack_pin);
  136. if (err)
  137. return err;
  138. err = snd_soc_jack_add_gpios(&hs_jack, 1, &hs_jack_gpio);
  139. return err;
  140. }
  141. /* hx4700 digital audio interface glue - connects codec <--> CPU */
  142. static struct snd_soc_dai_link hx4700_dai = {
  143. .name = "ak4641",
  144. .stream_name = "AK4641",
  145. .cpu_dai_name = "pxa2xx-i2s",
  146. .codec_dai_name = "ak4641-hifi",
  147. .platform_name = "pxa-pcm-audio",
  148. .codec_name = "ak4641.0-0012",
  149. .init = hx4700_ak4641_init,
  150. .ops = &hx4700_ops,
  151. };
  152. /* hx4700 audio machine driver */
  153. static struct snd_soc_card snd_soc_card_hx4700 = {
  154. .name = "iPAQ hx4700",
  155. .dai_link = &hx4700_dai,
  156. .num_links = 1,
  157. .dapm_widgets = hx4700_dapm_widgets,
  158. .num_dapm_widgets = ARRAY_SIZE(hx4700_dapm_widgets),
  159. .dapm_routes = hx4700_audio_map,
  160. .num_dapm_routes = ARRAY_SIZE(hx4700_audio_map),
  161. };
  162. static struct gpio hx4700_audio_gpios[] = {
  163. { GPIO107_HX4700_SPK_nSD, GPIOF_OUT_INIT_HIGH, "SPK_POWER" },
  164. { GPIO92_HX4700_HP_DRIVER, GPIOF_OUT_INIT_LOW, "EP_POWER" },
  165. };
  166. static int __devinit hx4700_audio_probe(struct platform_device *pdev)
  167. {
  168. int ret;
  169. if (!machine_is_h4700())
  170. return -ENODEV;
  171. ret = gpio_request_array(hx4700_audio_gpios,
  172. ARRAY_SIZE(hx4700_audio_gpios));
  173. if (ret)
  174. return ret;
  175. snd_soc_card_hx4700.dev = &pdev->dev;
  176. ret = snd_soc_register_card(&snd_soc_card_hx4700);
  177. if (ret)
  178. return ret;
  179. return 0;
  180. }
  181. static int __devexit hx4700_audio_remove(struct platform_device *pdev)
  182. {
  183. snd_soc_jack_free_gpios(&hs_jack, 1, &hs_jack_gpio);
  184. snd_soc_unregister_card(&snd_soc_card_hx4700);
  185. gpio_set_value(GPIO92_HX4700_HP_DRIVER, 0);
  186. gpio_set_value(GPIO107_HX4700_SPK_nSD, 0);
  187. gpio_free_array(hx4700_audio_gpios, ARRAY_SIZE(hx4700_audio_gpios));
  188. return 0;
  189. }
  190. static struct platform_driver hx4700_audio_driver = {
  191. .driver = {
  192. .name = "hx4700-audio",
  193. .owner = THIS_MODULE,
  194. .pm = &snd_soc_pm_ops,
  195. },
  196. .probe = hx4700_audio_probe,
  197. .remove = __devexit_p(hx4700_audio_remove),
  198. };
  199. static int __init hx4700_modinit(void)
  200. {
  201. return platform_driver_register(&hx4700_audio_driver);
  202. }
  203. module_init(hx4700_modinit);
  204. static void __exit hx4700_modexit(void)
  205. {
  206. platform_driver_unregister(&hx4700_audio_driver);
  207. }
  208. module_exit(hx4700_modexit);
  209. MODULE_AUTHOR("Philipp Zabel");
  210. MODULE_DESCRIPTION("ALSA SoC iPAQ hx4700");
  211. MODULE_LICENSE("GPL");
  212. MODULE_ALIAS("platform:hx4700-audio");