s3c24xx_simtec.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 "s3c24xx-pcm.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_codec *codec)
  123. {
  124. if (pdata->amp_gpio > 0) {
  125. pr_debug("%s: adding amp routes\n", __func__);
  126. snd_soc_add_controls(codec, amp_unmute_controls,
  127. ARRAY_SIZE(amp_unmute_controls));
  128. }
  129. if (pdata->amp_gain[0] > 0) {
  130. pr_debug("%s: adding amp controls\n", __func__);
  131. snd_soc_add_controls(codec, amp_gain_controls,
  132. ARRAY_SIZE(amp_gain_controls));
  133. }
  134. }
  135. EXPORT_SYMBOL_GPL(simtec_audio_init);
  136. #define CODEC_CLOCK 12000000
  137. /**
  138. * simtec_hw_params - update hardware parameters
  139. * @substream: The audio substream instance.
  140. * @params: The parameters requested.
  141. *
  142. * Update the codec data routing and configuration settings
  143. * from the supplied data.
  144. */
  145. static int simtec_hw_params(struct snd_pcm_substream *substream,
  146. struct snd_pcm_hw_params *params)
  147. {
  148. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  149. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  150. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  151. int ret;
  152. /* Set the CODEC as the bus clock master, I2S */
  153. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  154. SND_SOC_DAIFMT_NB_NF |
  155. SND_SOC_DAIFMT_CBM_CFM);
  156. if (ret) {
  157. pr_err("%s: failed set cpu dai format\n", __func__);
  158. return ret;
  159. }
  160. /* Set the CODEC as the bus clock master */
  161. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  162. SND_SOC_DAIFMT_NB_NF |
  163. SND_SOC_DAIFMT_CBM_CFM);
  164. if (ret) {
  165. pr_err("%s: failed set codec dai format\n", __func__);
  166. return ret;
  167. }
  168. ret = snd_soc_dai_set_sysclk(codec_dai, 0,
  169. CODEC_CLOCK, SND_SOC_CLOCK_IN);
  170. if (ret) {
  171. pr_err( "%s: failed setting codec sysclk\n", __func__);
  172. return ret;
  173. }
  174. if (pdata->use_mpllin) {
  175. ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_MPLL,
  176. 0, SND_SOC_CLOCK_OUT);
  177. if (ret) {
  178. pr_err("%s: failed to set MPLLin as clksrc\n",
  179. __func__);
  180. return ret;
  181. }
  182. }
  183. if (pdata->output_cdclk) {
  184. int cdclk_scale;
  185. cdclk_scale = clk_get_rate(xtal_clk) / CODEC_CLOCK;
  186. cdclk_scale--;
  187. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  188. cdclk_scale);
  189. }
  190. return 0;
  191. }
  192. static int simtec_call_startup(struct s3c24xx_audio_simtec_pdata *pd)
  193. {
  194. /* call any board supplied startup code, this currently only
  195. * covers the bast/vr1000 which have a CPLD in the way of the
  196. * LRCLK */
  197. if (pd->startup)
  198. pd->startup();
  199. return 0;
  200. }
  201. static struct snd_soc_ops simtec_snd_ops = {
  202. .hw_params = simtec_hw_params,
  203. };
  204. /**
  205. * attach_gpio_amp - get and configure the necessary gpios
  206. * @dev: The device we're probing.
  207. * @pd: The platform data supplied by the board.
  208. *
  209. * If there is a GPIO based amplifier attached to the board, claim
  210. * the necessary GPIO lines for it, and set default values.
  211. */
  212. static int attach_gpio_amp(struct device *dev,
  213. struct s3c24xx_audio_simtec_pdata *pd)
  214. {
  215. int ret;
  216. /* attach gpio amp gain (if any) */
  217. if (pdata->amp_gain[0] > 0) {
  218. ret = gpio_request(pd->amp_gain[0], "gpio-amp-gain0");
  219. if (ret) {
  220. dev_err(dev, "cannot get amp gpio gain0\n");
  221. return ret;
  222. }
  223. ret = gpio_request(pd->amp_gain[1], "gpio-amp-gain1");
  224. if (ret) {
  225. dev_err(dev, "cannot get amp gpio gain1\n");
  226. gpio_free(pdata->amp_gain[0]);
  227. return ret;
  228. }
  229. gpio_direction_output(pd->amp_gain[0], 0);
  230. gpio_direction_output(pd->amp_gain[1], 0);
  231. }
  232. /* note, curently we assume GPA0 isn't valid amp */
  233. if (pdata->amp_gpio > 0) {
  234. ret = gpio_request(pd->amp_gpio, "gpio-amp");
  235. if (ret) {
  236. dev_err(dev, "cannot get amp gpio %d (%d)\n",
  237. pd->amp_gpio, ret);
  238. goto err_amp;
  239. }
  240. /* set the amp off at startup */
  241. spk_unmute_state(0);
  242. }
  243. return 0;
  244. err_amp:
  245. if (pd->amp_gain[0] > 0) {
  246. gpio_free(pd->amp_gain[0]);
  247. gpio_free(pd->amp_gain[1]);
  248. }
  249. return ret;
  250. }
  251. static void detach_gpio_amp(struct s3c24xx_audio_simtec_pdata *pd)
  252. {
  253. if (pd->amp_gain[0] > 0) {
  254. gpio_free(pd->amp_gain[0]);
  255. gpio_free(pd->amp_gain[1]);
  256. }
  257. if (pd->amp_gpio > 0)
  258. gpio_free(pd->amp_gpio);
  259. }
  260. #ifdef CONFIG_PM
  261. int simtec_audio_resume(struct device *dev)
  262. {
  263. simtec_call_startup(pdata);
  264. return 0;
  265. }
  266. struct dev_pm_ops simtec_audio_pmops = {
  267. .resume = simtec_audio_resume,
  268. };
  269. EXPORT_SYMBOL_GPL(simtec_audio_pmops);
  270. #endif
  271. int __devinit simtec_audio_core_probe(struct platform_device *pdev,
  272. struct snd_soc_device *socdev)
  273. {
  274. struct platform_device *snd_dev;
  275. int ret;
  276. socdev->card->dai_link->ops = &simtec_snd_ops;
  277. pdata = pdev->dev.platform_data;
  278. if (!pdata) {
  279. dev_err(&pdev->dev, "no platform data supplied\n");
  280. return -EINVAL;
  281. }
  282. simtec_call_startup(pdata);
  283. xtal_clk = clk_get(&pdev->dev, "xtal");
  284. if (IS_ERR(xtal_clk)) {
  285. dev_err(&pdev->dev, "could not get clkout0\n");
  286. return -EINVAL;
  287. }
  288. dev_info(&pdev->dev, "xtal rate is %ld\n", clk_get_rate(xtal_clk));
  289. ret = attach_gpio_amp(&pdev->dev, pdata);
  290. if (ret)
  291. goto err_clk;
  292. snd_dev = platform_device_alloc("soc-audio", -1);
  293. if (!snd_dev) {
  294. dev_err(&pdev->dev, "failed to alloc soc-audio devicec\n");
  295. ret = -ENOMEM;
  296. goto err_gpio;
  297. }
  298. platform_set_drvdata(snd_dev, socdev);
  299. socdev->dev = &snd_dev->dev;
  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");