s3c24xx_simtec.c 9.1 KB

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