s3c24xx_simtec.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* sound/soc/samsung/s3c24xx_simtec.c
  2. *
  3. * Copyright 2009 Simtec Electronics
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/gpio.h>
  10. #include <linux/clk.h>
  11. #include <linux/module.h>
  12. #include <sound/soc.h>
  13. #include <linux/platform_data/asoc-s3c24xx_simtec.h>
  14. #include "s3c24xx-i2s.h"
  15. #include "s3c24xx_simtec.h"
  16. static struct s3c24xx_audio_simtec_pdata *pdata;
  17. static struct clk *xtal_clk;
  18. static int spk_gain;
  19. static int spk_unmute;
  20. /**
  21. * speaker_gain_get - read the speaker gain setting.
  22. * @kcontrol: The control for the speaker gain.
  23. * @ucontrol: The value that needs to be updated.
  24. *
  25. * Read the value for the AMP gain control.
  26. */
  27. static int speaker_gain_get(struct snd_kcontrol *kcontrol,
  28. struct snd_ctl_elem_value *ucontrol)
  29. {
  30. ucontrol->value.integer.value[0] = spk_gain;
  31. return 0;
  32. }
  33. /**
  34. * speaker_gain_set - set the value of the speaker amp gain
  35. * @value: The value to write.
  36. */
  37. static void speaker_gain_set(int value)
  38. {
  39. gpio_set_value_cansleep(pdata->amp_gain[0], value & 1);
  40. gpio_set_value_cansleep(pdata->amp_gain[1], value >> 1);
  41. }
  42. /**
  43. * speaker_gain_put - set the speaker gain setting.
  44. * @kcontrol: The control for the speaker gain.
  45. * @ucontrol: The value that needs to be set.
  46. *
  47. * Set the value of the speaker gain from the specified
  48. * @ucontrol setting.
  49. *
  50. * Note, if the speaker amp is muted, then we do not set a gain value
  51. * as at-least one of the ICs that is fitted will try and power up even
  52. * if the main control is set to off.
  53. */
  54. static int speaker_gain_put(struct snd_kcontrol *kcontrol,
  55. struct snd_ctl_elem_value *ucontrol)
  56. {
  57. int value = ucontrol->value.integer.value[0];
  58. spk_gain = value;
  59. if (!spk_unmute)
  60. speaker_gain_set(value);
  61. return 0;
  62. }
  63. static const struct snd_kcontrol_new amp_gain_controls[] = {
  64. SOC_SINGLE_EXT("Speaker Gain", 0, 0, 3, 0,
  65. speaker_gain_get, speaker_gain_put),
  66. };
  67. /**
  68. * spk_unmute_state - set the unmute state of the speaker
  69. * @to: zero to unmute, non-zero to ununmute.
  70. */
  71. static void spk_unmute_state(int to)
  72. {
  73. pr_debug("%s: to=%d\n", __func__, to);
  74. spk_unmute = to;
  75. gpio_set_value(pdata->amp_gpio, to);
  76. /* if we're umuting, also re-set the gain */
  77. if (to && pdata->amp_gain[0] > 0)
  78. speaker_gain_set(spk_gain);
  79. }
  80. /**
  81. * speaker_unmute_get - read the speaker unmute setting.
  82. * @kcontrol: The control for the speaker gain.
  83. * @ucontrol: The value that needs to be updated.
  84. *
  85. * Read the value for the AMP gain control.
  86. */
  87. static int speaker_unmute_get(struct snd_kcontrol *kcontrol,
  88. struct snd_ctl_elem_value *ucontrol)
  89. {
  90. ucontrol->value.integer.value[0] = spk_unmute;
  91. return 0;
  92. }
  93. /**
  94. * speaker_unmute_put - set the speaker unmute setting.
  95. * @kcontrol: The control for the speaker gain.
  96. * @ucontrol: The value that needs to be set.
  97. *
  98. * Set the value of the speaker gain from the specified
  99. * @ucontrol setting.
  100. */
  101. static int speaker_unmute_put(struct snd_kcontrol *kcontrol,
  102. struct snd_ctl_elem_value *ucontrol)
  103. {
  104. spk_unmute_state(ucontrol->value.integer.value[0]);
  105. return 0;
  106. }
  107. /* This is added as a manual control as the speaker amps create clicks
  108. * when their power state is changed, which are far more noticeable than
  109. * anything produced by the CODEC itself.
  110. */
  111. static const struct snd_kcontrol_new amp_unmute_controls[] = {
  112. SOC_SINGLE_EXT("Speaker Switch", 0, 0, 1, 0,
  113. speaker_unmute_get, speaker_unmute_put),
  114. };
  115. void simtec_audio_init(struct snd_soc_pcm_runtime *rtd)
  116. {
  117. struct snd_soc_card *card = rtd->card;
  118. if (pdata->amp_gpio > 0) {
  119. pr_debug("%s: adding amp routes\n", __func__);
  120. snd_soc_add_card_controls(card, amp_unmute_controls,
  121. ARRAY_SIZE(amp_unmute_controls));
  122. }
  123. if (pdata->amp_gain[0] > 0) {
  124. pr_debug("%s: adding amp controls\n", __func__);
  125. snd_soc_add_card_controls(card, amp_gain_controls,
  126. ARRAY_SIZE(amp_gain_controls));
  127. }
  128. }
  129. EXPORT_SYMBOL_GPL(simtec_audio_init);
  130. #define CODEC_CLOCK 12000000
  131. /**
  132. * simtec_hw_params - update hardware parameters
  133. * @substream: The audio substream instance.
  134. * @params: The parameters requested.
  135. *
  136. * Update the codec data routing and configuration settings
  137. * from the supplied data.
  138. */
  139. static int simtec_hw_params(struct snd_pcm_substream *substream,
  140. struct snd_pcm_hw_params *params)
  141. {
  142. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  143. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  144. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  145. int ret;
  146. ret = snd_soc_dai_set_sysclk(codec_dai, 0,
  147. CODEC_CLOCK, SND_SOC_CLOCK_IN);
  148. if (ret) {
  149. pr_err( "%s: failed setting codec sysclk\n", __func__);
  150. return ret;
  151. }
  152. if (pdata->use_mpllin) {
  153. ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_MPLL,
  154. 0, SND_SOC_CLOCK_OUT);
  155. if (ret) {
  156. pr_err("%s: failed to set MPLLin as clksrc\n",
  157. __func__);
  158. return ret;
  159. }
  160. }
  161. if (pdata->output_cdclk) {
  162. int cdclk_scale;
  163. cdclk_scale = clk_get_rate(xtal_clk) / CODEC_CLOCK;
  164. cdclk_scale--;
  165. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  166. cdclk_scale);
  167. }
  168. return 0;
  169. }
  170. static int simtec_call_startup(struct s3c24xx_audio_simtec_pdata *pd)
  171. {
  172. /* call any board supplied startup code, this currently only
  173. * covers the bast/vr1000 which have a CPLD in the way of the
  174. * LRCLK */
  175. if (pd->startup)
  176. pd->startup();
  177. return 0;
  178. }
  179. static struct snd_soc_ops simtec_snd_ops = {
  180. .hw_params = simtec_hw_params,
  181. };
  182. /**
  183. * attach_gpio_amp - get and configure the necessary gpios
  184. * @dev: The device we're probing.
  185. * @pd: The platform data supplied by the board.
  186. *
  187. * If there is a GPIO based amplifier attached to the board, claim
  188. * the necessary GPIO lines for it, and set default values.
  189. */
  190. static int attach_gpio_amp(struct device *dev,
  191. struct s3c24xx_audio_simtec_pdata *pd)
  192. {
  193. int ret;
  194. /* attach gpio amp gain (if any) */
  195. if (pdata->amp_gain[0] > 0) {
  196. ret = gpio_request(pd->amp_gain[0], "gpio-amp-gain0");
  197. if (ret) {
  198. dev_err(dev, "cannot get amp gpio gain0\n");
  199. return ret;
  200. }
  201. ret = gpio_request(pd->amp_gain[1], "gpio-amp-gain1");
  202. if (ret) {
  203. dev_err(dev, "cannot get amp gpio gain1\n");
  204. gpio_free(pdata->amp_gain[0]);
  205. return ret;
  206. }
  207. gpio_direction_output(pd->amp_gain[0], 0);
  208. gpio_direction_output(pd->amp_gain[1], 0);
  209. }
  210. /* note, currently we assume GPA0 isn't valid amp */
  211. if (pdata->amp_gpio > 0) {
  212. ret = gpio_request(pd->amp_gpio, "gpio-amp");
  213. if (ret) {
  214. dev_err(dev, "cannot get amp gpio %d (%d)\n",
  215. pd->amp_gpio, ret);
  216. goto err_amp;
  217. }
  218. /* set the amp off at startup */
  219. spk_unmute_state(0);
  220. }
  221. return 0;
  222. err_amp:
  223. if (pd->amp_gain[0] > 0) {
  224. gpio_free(pd->amp_gain[0]);
  225. gpio_free(pd->amp_gain[1]);
  226. }
  227. return ret;
  228. }
  229. static void detach_gpio_amp(struct s3c24xx_audio_simtec_pdata *pd)
  230. {
  231. if (pd->amp_gain[0] > 0) {
  232. gpio_free(pd->amp_gain[0]);
  233. gpio_free(pd->amp_gain[1]);
  234. }
  235. if (pd->amp_gpio > 0)
  236. gpio_free(pd->amp_gpio);
  237. }
  238. #ifdef CONFIG_PM
  239. static int simtec_audio_resume(struct device *dev)
  240. {
  241. simtec_call_startup(pdata);
  242. return 0;
  243. }
  244. const struct dev_pm_ops simtec_audio_pmops = {
  245. .resume = simtec_audio_resume,
  246. };
  247. EXPORT_SYMBOL_GPL(simtec_audio_pmops);
  248. #endif
  249. int simtec_audio_core_probe(struct platform_device *pdev,
  250. struct snd_soc_card *card)
  251. {
  252. struct platform_device *snd_dev;
  253. int ret;
  254. card->dai_link->ops = &simtec_snd_ops;
  255. card->dai_link->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  256. SND_SOC_DAIFMT_CBM_CFM;
  257. pdata = pdev->dev.platform_data;
  258. if (!pdata) {
  259. dev_err(&pdev->dev, "no platform data supplied\n");
  260. return -EINVAL;
  261. }
  262. simtec_call_startup(pdata);
  263. xtal_clk = clk_get(&pdev->dev, "xtal");
  264. if (IS_ERR(xtal_clk)) {
  265. dev_err(&pdev->dev, "could not get clkout0\n");
  266. return -EINVAL;
  267. }
  268. dev_info(&pdev->dev, "xtal rate is %ld\n", clk_get_rate(xtal_clk));
  269. ret = attach_gpio_amp(&pdev->dev, pdata);
  270. if (ret)
  271. goto err_clk;
  272. snd_dev = platform_device_alloc("soc-audio", -1);
  273. if (!snd_dev) {
  274. dev_err(&pdev->dev, "failed to alloc soc-audio devicec\n");
  275. ret = -ENOMEM;
  276. goto err_gpio;
  277. }
  278. platform_set_drvdata(snd_dev, card);
  279. ret = platform_device_add(snd_dev);
  280. if (ret) {
  281. dev_err(&pdev->dev, "failed to add soc-audio dev\n");
  282. goto err_pdev;
  283. }
  284. platform_set_drvdata(pdev, snd_dev);
  285. return 0;
  286. err_pdev:
  287. platform_device_put(snd_dev);
  288. err_gpio:
  289. detach_gpio_amp(pdata);
  290. err_clk:
  291. clk_put(xtal_clk);
  292. return ret;
  293. }
  294. EXPORT_SYMBOL_GPL(simtec_audio_core_probe);
  295. int simtec_audio_remove(struct platform_device *pdev)
  296. {
  297. struct platform_device *snd_dev = platform_get_drvdata(pdev);
  298. platform_device_unregister(snd_dev);
  299. detach_gpio_amp(pdata);
  300. clk_put(xtal_clk);
  301. return 0;
  302. }
  303. EXPORT_SYMBOL_GPL(simtec_audio_remove);
  304. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  305. MODULE_DESCRIPTION("ALSA SoC Simtec Audio common support");
  306. MODULE_LICENSE("GPL");