s3c24xx_simtec.c 9.3 KB

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