s3c24xx_simtec.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <plat/audio-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. /* Set the CODEC as the bus clock master, I2S */
  147. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  148. SND_SOC_DAIFMT_NB_NF |
  149. SND_SOC_DAIFMT_CBM_CFM);
  150. if (ret) {
  151. pr_err("%s: failed set cpu dai format\n", __func__);
  152. return ret;
  153. }
  154. /* Set the CODEC as the bus clock master */
  155. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  156. SND_SOC_DAIFMT_NB_NF |
  157. SND_SOC_DAIFMT_CBM_CFM);
  158. if (ret) {
  159. pr_err("%s: failed set codec dai format\n", __func__);
  160. return ret;
  161. }
  162. ret = snd_soc_dai_set_sysclk(codec_dai, 0,
  163. CODEC_CLOCK, SND_SOC_CLOCK_IN);
  164. if (ret) {
  165. pr_err( "%s: failed setting codec sysclk\n", __func__);
  166. return ret;
  167. }
  168. if (pdata->use_mpllin) {
  169. ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_MPLL,
  170. 0, SND_SOC_CLOCK_OUT);
  171. if (ret) {
  172. pr_err("%s: failed to set MPLLin as clksrc\n",
  173. __func__);
  174. return ret;
  175. }
  176. }
  177. if (pdata->output_cdclk) {
  178. int cdclk_scale;
  179. cdclk_scale = clk_get_rate(xtal_clk) / CODEC_CLOCK;
  180. cdclk_scale--;
  181. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  182. cdclk_scale);
  183. }
  184. return 0;
  185. }
  186. static int simtec_call_startup(struct s3c24xx_audio_simtec_pdata *pd)
  187. {
  188. /* call any board supplied startup code, this currently only
  189. * covers the bast/vr1000 which have a CPLD in the way of the
  190. * LRCLK */
  191. if (pd->startup)
  192. pd->startup();
  193. return 0;
  194. }
  195. static struct snd_soc_ops simtec_snd_ops = {
  196. .hw_params = simtec_hw_params,
  197. };
  198. /**
  199. * attach_gpio_amp - get and configure the necessary gpios
  200. * @dev: The device we're probing.
  201. * @pd: The platform data supplied by the board.
  202. *
  203. * If there is a GPIO based amplifier attached to the board, claim
  204. * the necessary GPIO lines for it, and set default values.
  205. */
  206. static int attach_gpio_amp(struct device *dev,
  207. struct s3c24xx_audio_simtec_pdata *pd)
  208. {
  209. int ret;
  210. /* attach gpio amp gain (if any) */
  211. if (pdata->amp_gain[0] > 0) {
  212. ret = gpio_request(pd->amp_gain[0], "gpio-amp-gain0");
  213. if (ret) {
  214. dev_err(dev, "cannot get amp gpio gain0\n");
  215. return ret;
  216. }
  217. ret = gpio_request(pd->amp_gain[1], "gpio-amp-gain1");
  218. if (ret) {
  219. dev_err(dev, "cannot get amp gpio gain1\n");
  220. gpio_free(pdata->amp_gain[0]);
  221. return ret;
  222. }
  223. gpio_direction_output(pd->amp_gain[0], 0);
  224. gpio_direction_output(pd->amp_gain[1], 0);
  225. }
  226. /* note, currently we assume GPA0 isn't valid amp */
  227. if (pdata->amp_gpio > 0) {
  228. ret = gpio_request(pd->amp_gpio, "gpio-amp");
  229. if (ret) {
  230. dev_err(dev, "cannot get amp gpio %d (%d)\n",
  231. pd->amp_gpio, ret);
  232. goto err_amp;
  233. }
  234. /* set the amp off at startup */
  235. spk_unmute_state(0);
  236. }
  237. return 0;
  238. err_amp:
  239. if (pd->amp_gain[0] > 0) {
  240. gpio_free(pd->amp_gain[0]);
  241. gpio_free(pd->amp_gain[1]);
  242. }
  243. return ret;
  244. }
  245. static void detach_gpio_amp(struct s3c24xx_audio_simtec_pdata *pd)
  246. {
  247. if (pd->amp_gain[0] > 0) {
  248. gpio_free(pd->amp_gain[0]);
  249. gpio_free(pd->amp_gain[1]);
  250. }
  251. if (pd->amp_gpio > 0)
  252. gpio_free(pd->amp_gpio);
  253. }
  254. #ifdef CONFIG_PM
  255. static int simtec_audio_resume(struct device *dev)
  256. {
  257. simtec_call_startup(pdata);
  258. return 0;
  259. }
  260. const struct dev_pm_ops simtec_audio_pmops = {
  261. .resume = simtec_audio_resume,
  262. };
  263. EXPORT_SYMBOL_GPL(simtec_audio_pmops);
  264. #endif
  265. int __devinit simtec_audio_core_probe(struct platform_device *pdev,
  266. struct snd_soc_card *card)
  267. {
  268. struct platform_device *snd_dev;
  269. int ret;
  270. card->dai_link->ops = &simtec_snd_ops;
  271. pdata = pdev->dev.platform_data;
  272. if (!pdata) {
  273. dev_err(&pdev->dev, "no platform data supplied\n");
  274. return -EINVAL;
  275. }
  276. simtec_call_startup(pdata);
  277. xtal_clk = clk_get(&pdev->dev, "xtal");
  278. if (IS_ERR(xtal_clk)) {
  279. dev_err(&pdev->dev, "could not get clkout0\n");
  280. return -EINVAL;
  281. }
  282. dev_info(&pdev->dev, "xtal rate is %ld\n", clk_get_rate(xtal_clk));
  283. ret = attach_gpio_amp(&pdev->dev, pdata);
  284. if (ret)
  285. goto err_clk;
  286. snd_dev = platform_device_alloc("soc-audio", -1);
  287. if (!snd_dev) {
  288. dev_err(&pdev->dev, "failed to alloc soc-audio devicec\n");
  289. ret = -ENOMEM;
  290. goto err_gpio;
  291. }
  292. platform_set_drvdata(snd_dev, card);
  293. ret = platform_device_add(snd_dev);
  294. if (ret) {
  295. dev_err(&pdev->dev, "failed to add soc-audio dev\n");
  296. goto err_pdev;
  297. }
  298. platform_set_drvdata(pdev, snd_dev);
  299. return 0;
  300. err_pdev:
  301. platform_device_put(snd_dev);
  302. err_gpio:
  303. detach_gpio_amp(pdata);
  304. err_clk:
  305. clk_put(xtal_clk);
  306. return ret;
  307. }
  308. EXPORT_SYMBOL_GPL(simtec_audio_core_probe);
  309. int __devexit simtec_audio_remove(struct platform_device *pdev)
  310. {
  311. struct platform_device *snd_dev = platform_get_drvdata(pdev);
  312. platform_device_unregister(snd_dev);
  313. detach_gpio_amp(pdata);
  314. clk_put(xtal_clk);
  315. return 0;
  316. }
  317. EXPORT_SYMBOL_GPL(simtec_audio_remove);
  318. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  319. MODULE_DESCRIPTION("ALSA SoC Simtec Audio common support");
  320. MODULE_LICENSE("GPL");