wl1273.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * ALSA SoC WL1273 codec driver
  3. *
  4. * Author: Matti Aaltonen, <matti.j.aaltonen@nokia.com>
  5. *
  6. * Copyright: (C) 2010, 2011 Nokia Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/mfd/wl1273-core.h>
  24. #include <linux/slab.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <sound/initval.h>
  29. #include "wl1273.h"
  30. enum wl1273_mode { WL1273_MODE_BT, WL1273_MODE_FM_RX, WL1273_MODE_FM_TX };
  31. /* codec private data */
  32. struct wl1273_priv {
  33. enum wl1273_mode mode;
  34. struct wl1273_core *core;
  35. unsigned int channels;
  36. };
  37. static int snd_wl1273_fm_set_i2s_mode(struct wl1273_core *core,
  38. int rate, int width)
  39. {
  40. struct device *dev = &core->client->dev;
  41. int r = 0;
  42. u16 mode;
  43. dev_dbg(dev, "rate: %d\n", rate);
  44. dev_dbg(dev, "width: %d\n", width);
  45. mutex_lock(&core->lock);
  46. mode = core->i2s_mode & ~WL1273_IS2_WIDTH & ~WL1273_IS2_RATE;
  47. switch (rate) {
  48. case 48000:
  49. mode |= WL1273_IS2_RATE_48K;
  50. break;
  51. case 44100:
  52. mode |= WL1273_IS2_RATE_44_1K;
  53. break;
  54. case 32000:
  55. mode |= WL1273_IS2_RATE_32K;
  56. break;
  57. case 22050:
  58. mode |= WL1273_IS2_RATE_22_05K;
  59. break;
  60. case 16000:
  61. mode |= WL1273_IS2_RATE_16K;
  62. break;
  63. case 12000:
  64. mode |= WL1273_IS2_RATE_12K;
  65. break;
  66. case 11025:
  67. mode |= WL1273_IS2_RATE_11_025;
  68. break;
  69. case 8000:
  70. mode |= WL1273_IS2_RATE_8K;
  71. break;
  72. default:
  73. dev_err(dev, "Sampling rate: %d not supported\n", rate);
  74. r = -EINVAL;
  75. goto out;
  76. }
  77. switch (width) {
  78. case 16:
  79. mode |= WL1273_IS2_WIDTH_32;
  80. break;
  81. case 20:
  82. mode |= WL1273_IS2_WIDTH_40;
  83. break;
  84. case 24:
  85. mode |= WL1273_IS2_WIDTH_48;
  86. break;
  87. case 25:
  88. mode |= WL1273_IS2_WIDTH_50;
  89. break;
  90. case 30:
  91. mode |= WL1273_IS2_WIDTH_60;
  92. break;
  93. case 32:
  94. mode |= WL1273_IS2_WIDTH_64;
  95. break;
  96. case 40:
  97. mode |= WL1273_IS2_WIDTH_80;
  98. break;
  99. case 48:
  100. mode |= WL1273_IS2_WIDTH_96;
  101. break;
  102. case 64:
  103. mode |= WL1273_IS2_WIDTH_128;
  104. break;
  105. default:
  106. dev_err(dev, "Data width: %d not supported\n", width);
  107. r = -EINVAL;
  108. goto out;
  109. }
  110. dev_dbg(dev, "WL1273_I2S_DEF_MODE: 0x%04x\n", WL1273_I2S_DEF_MODE);
  111. dev_dbg(dev, "core->i2s_mode: 0x%04x\n", core->i2s_mode);
  112. dev_dbg(dev, "mode: 0x%04x\n", mode);
  113. if (core->i2s_mode != mode) {
  114. r = core->write(core, WL1273_I2S_MODE_CONFIG_SET, mode);
  115. if (r)
  116. goto out;
  117. core->i2s_mode = mode;
  118. r = core->write(core, WL1273_AUDIO_ENABLE,
  119. WL1273_AUDIO_ENABLE_I2S);
  120. if (r)
  121. goto out;
  122. }
  123. out:
  124. mutex_unlock(&core->lock);
  125. return r;
  126. }
  127. static int snd_wl1273_fm_set_channel_number(struct wl1273_core *core,
  128. int channel_number)
  129. {
  130. struct device *dev = &core->client->dev;
  131. int r = 0;
  132. dev_dbg(dev, "%s\n", __func__);
  133. mutex_lock(&core->lock);
  134. if (core->channel_number == channel_number)
  135. goto out;
  136. if (channel_number == 1 && core->mode == WL1273_MODE_RX)
  137. r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
  138. else if (channel_number == 1 && core->mode == WL1273_MODE_TX)
  139. r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
  140. else if (channel_number == 2 && core->mode == WL1273_MODE_RX)
  141. r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
  142. else if (channel_number == 2 && core->mode == WL1273_MODE_TX)
  143. r = core->write(core, WL1273_MONO_SET, WL1273_TX_STEREO);
  144. else
  145. r = -EINVAL;
  146. out:
  147. mutex_unlock(&core->lock);
  148. return r;
  149. }
  150. static int snd_wl1273_get_audio_route(struct snd_kcontrol *kcontrol,
  151. struct snd_ctl_elem_value *ucontrol)
  152. {
  153. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  154. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  155. ucontrol->value.integer.value[0] = wl1273->mode;
  156. return 0;
  157. }
  158. /*
  159. * TODO: Implement the audio routing in the driver. Now this control
  160. * only indicates the setting that has been done elsewhere (in the user
  161. * space).
  162. */
  163. static const char * const wl1273_audio_route[] = { "Bt", "FmRx", "FmTx" };
  164. static int snd_wl1273_set_audio_route(struct snd_kcontrol *kcontrol,
  165. struct snd_ctl_elem_value *ucontrol)
  166. {
  167. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  168. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  169. if (wl1273->mode == ucontrol->value.integer.value[0])
  170. return 0;
  171. /* Do not allow changes while stream is running */
  172. if (codec->active)
  173. return -EPERM;
  174. if (ucontrol->value.integer.value[0] < 0 ||
  175. ucontrol->value.integer.value[0] >= ARRAY_SIZE(wl1273_audio_route))
  176. return -EINVAL;
  177. wl1273->mode = ucontrol->value.integer.value[0];
  178. return 1;
  179. }
  180. static const struct soc_enum wl1273_enum =
  181. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_route), wl1273_audio_route);
  182. static int snd_wl1273_fm_audio_get(struct snd_kcontrol *kcontrol,
  183. struct snd_ctl_elem_value *ucontrol)
  184. {
  185. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  186. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  187. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  188. ucontrol->value.integer.value[0] = wl1273->core->audio_mode;
  189. return 0;
  190. }
  191. static int snd_wl1273_fm_audio_put(struct snd_kcontrol *kcontrol,
  192. struct snd_ctl_elem_value *ucontrol)
  193. {
  194. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  195. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  196. int val, r = 0;
  197. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  198. val = ucontrol->value.integer.value[0];
  199. if (wl1273->core->audio_mode == val)
  200. return 0;
  201. r = wl1273->core->set_audio(wl1273->core, val);
  202. if (r < 0)
  203. return r;
  204. return 1;
  205. }
  206. static const char * const wl1273_audio_strings[] = { "Digital", "Analog" };
  207. static const struct soc_enum wl1273_audio_enum =
  208. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_strings),
  209. wl1273_audio_strings);
  210. static int snd_wl1273_fm_volume_get(struct snd_kcontrol *kcontrol,
  211. struct snd_ctl_elem_value *ucontrol)
  212. {
  213. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  214. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  215. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  216. ucontrol->value.integer.value[0] = wl1273->core->volume;
  217. return 0;
  218. }
  219. static int snd_wl1273_fm_volume_put(struct snd_kcontrol *kcontrol,
  220. struct snd_ctl_elem_value *ucontrol)
  221. {
  222. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  223. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  224. int r;
  225. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  226. r = wl1273->core->set_volume(wl1273->core,
  227. ucontrol->value.integer.value[0]);
  228. if (r)
  229. return r;
  230. return 1;
  231. }
  232. static const struct snd_kcontrol_new wl1273_controls[] = {
  233. SOC_ENUM_EXT("Codec Mode", wl1273_enum,
  234. snd_wl1273_get_audio_route, snd_wl1273_set_audio_route),
  235. SOC_ENUM_EXT("Audio Switch", wl1273_audio_enum,
  236. snd_wl1273_fm_audio_get, snd_wl1273_fm_audio_put),
  237. SOC_SINGLE_EXT("Volume", 0, 0, WL1273_MAX_VOLUME, 0,
  238. snd_wl1273_fm_volume_get, snd_wl1273_fm_volume_put),
  239. };
  240. static int wl1273_startup(struct snd_pcm_substream *substream,
  241. struct snd_soc_dai *dai)
  242. {
  243. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  244. struct snd_soc_codec *codec = rtd->codec;
  245. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  246. switch (wl1273->mode) {
  247. case WL1273_MODE_BT:
  248. snd_pcm_hw_constraint_minmax(substream->runtime,
  249. SNDRV_PCM_HW_PARAM_RATE,
  250. 8000, 8000);
  251. snd_pcm_hw_constraint_minmax(substream->runtime,
  252. SNDRV_PCM_HW_PARAM_CHANNELS, 1, 1);
  253. break;
  254. case WL1273_MODE_FM_RX:
  255. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  256. pr_err("Cannot play in RX mode.\n");
  257. return -EINVAL;
  258. }
  259. break;
  260. case WL1273_MODE_FM_TX:
  261. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  262. pr_err("Cannot capture in TX mode.\n");
  263. return -EINVAL;
  264. }
  265. break;
  266. default:
  267. return -EINVAL;
  268. break;
  269. }
  270. return 0;
  271. }
  272. static int wl1273_hw_params(struct snd_pcm_substream *substream,
  273. struct snd_pcm_hw_params *params,
  274. struct snd_soc_dai *dai)
  275. {
  276. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  277. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(rtd->codec);
  278. struct wl1273_core *core = wl1273->core;
  279. unsigned int rate, width, r;
  280. if (params_format(params) != SNDRV_PCM_FORMAT_S16_LE) {
  281. pr_err("Only SNDRV_PCM_FORMAT_S16_LE supported.\n");
  282. return -EINVAL;
  283. }
  284. rate = params_rate(params);
  285. width = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
  286. if (wl1273->mode == WL1273_MODE_BT) {
  287. if (rate != 8000) {
  288. pr_err("Rate %d not supported.\n", params_rate(params));
  289. return -EINVAL;
  290. }
  291. if (params_channels(params) != 1) {
  292. pr_err("Only mono supported.\n");
  293. return -EINVAL;
  294. }
  295. return 0;
  296. }
  297. if (wl1273->mode == WL1273_MODE_FM_TX &&
  298. substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  299. pr_err("Only playback supported with TX.\n");
  300. return -EINVAL;
  301. }
  302. if (wl1273->mode == WL1273_MODE_FM_RX &&
  303. substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  304. pr_err("Only capture supported with RX.\n");
  305. return -EINVAL;
  306. }
  307. if (wl1273->mode != WL1273_MODE_FM_RX &&
  308. wl1273->mode != WL1273_MODE_FM_TX) {
  309. pr_err("Unexpected mode: %d.\n", wl1273->mode);
  310. return -EINVAL;
  311. }
  312. r = snd_wl1273_fm_set_i2s_mode(core, rate, width);
  313. if (r)
  314. return r;
  315. wl1273->channels = params_channels(params);
  316. r = snd_wl1273_fm_set_channel_number(core, wl1273->channels);
  317. if (r)
  318. return r;
  319. return 0;
  320. }
  321. static struct snd_soc_dai_ops wl1273_dai_ops = {
  322. .startup = wl1273_startup,
  323. .hw_params = wl1273_hw_params,
  324. };
  325. static struct snd_soc_dai_driver wl1273_dai = {
  326. .name = "wl1273-fm",
  327. .playback = {
  328. .stream_name = "Playback",
  329. .channels_min = 1,
  330. .channels_max = 2,
  331. .rates = SNDRV_PCM_RATE_8000_48000,
  332. .formats = SNDRV_PCM_FMTBIT_S16_LE},
  333. .capture = {
  334. .stream_name = "Capture",
  335. .channels_min = 1,
  336. .channels_max = 2,
  337. .rates = SNDRV_PCM_RATE_8000_48000,
  338. .formats = SNDRV_PCM_FMTBIT_S16_LE},
  339. .ops = &wl1273_dai_ops,
  340. };
  341. /* Audio interface format for the soc_card driver */
  342. int wl1273_get_format(struct snd_soc_codec *codec, unsigned int *fmt)
  343. {
  344. struct wl1273_priv *wl1273;
  345. if (codec == NULL || fmt == NULL)
  346. return -EINVAL;
  347. wl1273 = snd_soc_codec_get_drvdata(codec);
  348. switch (wl1273->mode) {
  349. case WL1273_MODE_FM_RX:
  350. case WL1273_MODE_FM_TX:
  351. *fmt = SND_SOC_DAIFMT_I2S |
  352. SND_SOC_DAIFMT_NB_NF |
  353. SND_SOC_DAIFMT_CBM_CFM;
  354. break;
  355. case WL1273_MODE_BT:
  356. *fmt = SND_SOC_DAIFMT_DSP_A |
  357. SND_SOC_DAIFMT_IB_NF |
  358. SND_SOC_DAIFMT_CBM_CFM;
  359. break;
  360. default:
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. EXPORT_SYMBOL_GPL(wl1273_get_format);
  366. static int wl1273_probe(struct snd_soc_codec *codec)
  367. {
  368. struct wl1273_core **core = codec->dev->platform_data;
  369. struct wl1273_priv *wl1273;
  370. int r;
  371. dev_dbg(codec->dev, "%s.\n", __func__);
  372. if (!core) {
  373. dev_err(codec->dev, "Platform data is missing.\n");
  374. return -EINVAL;
  375. }
  376. wl1273 = kzalloc(sizeof(struct wl1273_priv), GFP_KERNEL);
  377. if (wl1273 == NULL) {
  378. dev_err(codec->dev, "Cannot allocate memory.\n");
  379. return -ENOMEM;
  380. }
  381. wl1273->mode = WL1273_MODE_BT;
  382. wl1273->core = *core;
  383. snd_soc_codec_set_drvdata(codec, wl1273);
  384. mutex_init(&codec->mutex);
  385. r = snd_soc_add_controls(codec, wl1273_controls,
  386. ARRAY_SIZE(wl1273_controls));
  387. if (r)
  388. kfree(wl1273);
  389. return r;
  390. }
  391. static int wl1273_remove(struct snd_soc_codec *codec)
  392. {
  393. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  394. dev_dbg(codec->dev, "%s\n", __func__);
  395. kfree(wl1273);
  396. return 0;
  397. }
  398. static struct snd_soc_codec_driver soc_codec_dev_wl1273 = {
  399. .probe = wl1273_probe,
  400. .remove = wl1273_remove,
  401. };
  402. static int __devinit wl1273_platform_probe(struct platform_device *pdev)
  403. {
  404. return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wl1273,
  405. &wl1273_dai, 1);
  406. }
  407. static int __devexit wl1273_platform_remove(struct platform_device *pdev)
  408. {
  409. snd_soc_unregister_codec(&pdev->dev);
  410. return 0;
  411. }
  412. MODULE_ALIAS("platform:wl1273-codec");
  413. static struct platform_driver wl1273_platform_driver = {
  414. .driver = {
  415. .name = "wl1273-codec",
  416. .owner = THIS_MODULE,
  417. },
  418. .probe = wl1273_platform_probe,
  419. .remove = __devexit_p(wl1273_platform_remove),
  420. };
  421. static int __init wl1273_init(void)
  422. {
  423. return platform_driver_register(&wl1273_platform_driver);
  424. }
  425. module_init(wl1273_init);
  426. static void __exit wl1273_exit(void)
  427. {
  428. platform_driver_unregister(&wl1273_platform_driver);
  429. }
  430. module_exit(wl1273_exit);
  431. MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
  432. MODULE_DESCRIPTION("ASoC WL1273 codec driver");
  433. MODULE_LICENSE("GPL");