wl1273.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * ALSA SoC WL1273 codec driver
  3. *
  4. * Author: Matti Aaltonen, <matti.j.aaltonen@nokia.com>
  5. *
  6. * Copyright: (C) 2010 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. static const char *wl1273_audio_route[] = { "Bt", "FmRx", "FmTx" };
  159. static int snd_wl1273_set_audio_route(struct snd_kcontrol *kcontrol,
  160. struct snd_ctl_elem_value *ucontrol)
  161. {
  162. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  163. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  164. if (wl1273->mode == ucontrol->value.integer.value[0])
  165. return 0;
  166. /* Do not allow changes while stream is running */
  167. if (codec->active)
  168. return -EPERM;
  169. if (ucontrol->value.integer.value[0] < 0 ||
  170. ucontrol->value.integer.value[0] >= ARRAY_SIZE(wl1273_audio_route))
  171. return -EINVAL;
  172. wl1273->mode = ucontrol->value.integer.value[0];
  173. return 1;
  174. }
  175. static const struct soc_enum wl1273_enum =
  176. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_route), wl1273_audio_route);
  177. static int snd_wl1273_fm_audio_get(struct snd_kcontrol *kcontrol,
  178. struct snd_ctl_elem_value *ucontrol)
  179. {
  180. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  181. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  182. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  183. ucontrol->value.integer.value[0] = wl1273->core->audio_mode;
  184. return 0;
  185. }
  186. static int snd_wl1273_fm_audio_put(struct snd_kcontrol *kcontrol,
  187. struct snd_ctl_elem_value *ucontrol)
  188. {
  189. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  190. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  191. int val, r = 0;
  192. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  193. val = ucontrol->value.integer.value[0];
  194. if (wl1273->core->audio_mode == val)
  195. return 0;
  196. r = wl1273->core->set_audio(wl1273->core, val);
  197. if (r < 0)
  198. return r;
  199. return 1;
  200. }
  201. static const char *wl1273_audio_strings[] = { "Digital", "Analog" };
  202. static const struct soc_enum wl1273_audio_enum =
  203. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_strings),
  204. wl1273_audio_strings);
  205. static int snd_wl1273_fm_volume_get(struct snd_kcontrol *kcontrol,
  206. struct snd_ctl_elem_value *ucontrol)
  207. {
  208. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  209. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  210. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  211. ucontrol->value.integer.value[0] = wl1273->core->volume;
  212. return 0;
  213. }
  214. static int snd_wl1273_fm_volume_put(struct snd_kcontrol *kcontrol,
  215. struct snd_ctl_elem_value *ucontrol)
  216. {
  217. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  218. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  219. int r;
  220. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  221. r = wl1273->core->set_volume(wl1273->core,
  222. ucontrol->value.integer.value[0]);
  223. if (r)
  224. return r;
  225. return 1;
  226. }
  227. static const struct snd_kcontrol_new wl1273_controls[] = {
  228. SOC_ENUM_EXT("Codec Mode", wl1273_enum,
  229. snd_wl1273_get_audio_route, snd_wl1273_set_audio_route),
  230. SOC_ENUM_EXT("Audio Switch", wl1273_audio_enum,
  231. snd_wl1273_fm_audio_get, snd_wl1273_fm_audio_put),
  232. SOC_SINGLE_EXT("Volume", 0, 0, WL1273_MAX_VOLUME, 0,
  233. snd_wl1273_fm_volume_get, snd_wl1273_fm_volume_put),
  234. };
  235. static int wl1273_startup(struct snd_pcm_substream *substream,
  236. struct snd_soc_dai *dai)
  237. {
  238. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  239. struct snd_soc_codec *codec = rtd->codec;
  240. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  241. switch (wl1273->mode) {
  242. case WL1273_MODE_BT:
  243. snd_pcm_hw_constraint_minmax(substream->runtime,
  244. SNDRV_PCM_HW_PARAM_RATE,
  245. 8000, 8000);
  246. snd_pcm_hw_constraint_minmax(substream->runtime,
  247. SNDRV_PCM_HW_PARAM_CHANNELS, 1, 1);
  248. break;
  249. case WL1273_MODE_FM_RX:
  250. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  251. pr_err("Cannot play in RX mode.\n");
  252. return -EINVAL;
  253. }
  254. break;
  255. case WL1273_MODE_FM_TX:
  256. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  257. pr_err("Cannot capture in TX mode.\n");
  258. return -EINVAL;
  259. }
  260. break;
  261. default:
  262. return -EINVAL;
  263. break;
  264. }
  265. return 0;
  266. }
  267. static int wl1273_hw_params(struct snd_pcm_substream *substream,
  268. struct snd_pcm_hw_params *params,
  269. struct snd_soc_dai *dai)
  270. {
  271. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  272. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(rtd->codec);
  273. struct wl1273_core *core = wl1273->core;
  274. unsigned int rate, width, r;
  275. if (params_format(params) != SNDRV_PCM_FORMAT_S16_LE) {
  276. pr_err("Only SNDRV_PCM_FORMAT_S16_LE supported.\n");
  277. return -EINVAL;
  278. }
  279. rate = params_rate(params);
  280. width = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
  281. if (wl1273->mode == WL1273_MODE_BT) {
  282. if (rate != 8000) {
  283. pr_err("Rate %d not supported.\n", params_rate(params));
  284. return -EINVAL;
  285. }
  286. if (params_channels(params) != 1) {
  287. pr_err("Only mono supported.\n");
  288. return -EINVAL;
  289. }
  290. return 0;
  291. }
  292. if (wl1273->mode == WL1273_MODE_FM_TX &&
  293. substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  294. pr_err("Only playback supported with TX.\n");
  295. return -EINVAL;
  296. }
  297. if (wl1273->mode == WL1273_MODE_FM_RX &&
  298. substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  299. pr_err("Only capture supported with RX.\n");
  300. return -EINVAL;
  301. }
  302. if (wl1273->mode != WL1273_MODE_FM_RX &&
  303. wl1273->mode != WL1273_MODE_FM_TX) {
  304. pr_err("Unexpected mode: %d.\n", wl1273->mode);
  305. return -EINVAL;
  306. }
  307. r = snd_wl1273_fm_set_i2s_mode(core, rate, width);
  308. if (r)
  309. return r;
  310. wl1273->channels = params_channels(params);
  311. r = snd_wl1273_fm_set_channel_number(core, wl1273->channels);
  312. if (r)
  313. return r;
  314. return 0;
  315. }
  316. static struct snd_soc_dai_ops wl1273_dai_ops = {
  317. .startup = wl1273_startup,
  318. .hw_params = wl1273_hw_params,
  319. };
  320. static struct snd_soc_dai_driver wl1273_dai = {
  321. .name = "wl1273-fm",
  322. .playback = {
  323. .stream_name = "Playback",
  324. .channels_min = 1,
  325. .channels_max = 2,
  326. .rates = SNDRV_PCM_RATE_8000_48000,
  327. .formats = SNDRV_PCM_FMTBIT_S16_LE},
  328. .capture = {
  329. .stream_name = "Capture",
  330. .channels_min = 1,
  331. .channels_max = 2,
  332. .rates = SNDRV_PCM_RATE_8000_48000,
  333. .formats = SNDRV_PCM_FMTBIT_S16_LE},
  334. .ops = &wl1273_dai_ops,
  335. };
  336. /* Audio interface format for the soc_card driver */
  337. int wl1273_get_format(struct snd_soc_codec *codec, unsigned int *fmt)
  338. {
  339. struct wl1273_priv *wl1273;
  340. if (codec == NULL || fmt == NULL)
  341. return -EINVAL;
  342. wl1273 = snd_soc_codec_get_drvdata(codec);
  343. switch (wl1273->mode) {
  344. case WL1273_MODE_FM_RX:
  345. case WL1273_MODE_FM_TX:
  346. *fmt = SND_SOC_DAIFMT_I2S |
  347. SND_SOC_DAIFMT_NB_NF |
  348. SND_SOC_DAIFMT_CBM_CFM;
  349. break;
  350. case WL1273_MODE_BT:
  351. *fmt = SND_SOC_DAIFMT_DSP_A |
  352. SND_SOC_DAIFMT_IB_NF |
  353. SND_SOC_DAIFMT_CBM_CFM;
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. return 0;
  359. }
  360. EXPORT_SYMBOL_GPL(wl1273_get_format);
  361. static int wl1273_probe(struct snd_soc_codec *codec)
  362. {
  363. struct wl1273_core **core = codec->dev->platform_data;
  364. struct wl1273_priv *wl1273;
  365. int r;
  366. dev_dbg(codec->dev, "%s.\n", __func__);
  367. if (!core) {
  368. dev_err(codec->dev, "Platform data is missing.\n");
  369. return -EINVAL;
  370. }
  371. wl1273 = kzalloc(sizeof(struct wl1273_priv), GFP_KERNEL);
  372. if (wl1273 == NULL) {
  373. dev_err(codec->dev, "Cannot allocate memory.\n");
  374. return -ENOMEM;
  375. }
  376. wl1273->mode = WL1273_MODE_BT;
  377. wl1273->core = *core;
  378. snd_soc_codec_set_drvdata(codec, wl1273);
  379. mutex_init(&codec->mutex);
  380. r = snd_soc_add_controls(codec, wl1273_controls,
  381. ARRAY_SIZE(wl1273_controls));
  382. if (r)
  383. kfree(wl1273);
  384. return r;
  385. }
  386. static int wl1273_remove(struct snd_soc_codec *codec)
  387. {
  388. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  389. dev_dbg(codec->dev, "%s\n", __func__);
  390. kfree(wl1273);
  391. return 0;
  392. }
  393. static struct snd_soc_codec_driver soc_codec_dev_wl1273 = {
  394. .probe = wl1273_probe,
  395. .remove = wl1273_remove,
  396. };
  397. static int __devinit wl1273_platform_probe(struct platform_device *pdev)
  398. {
  399. return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wl1273,
  400. &wl1273_dai, 1);
  401. }
  402. static int __devexit wl1273_platform_remove(struct platform_device *pdev)
  403. {
  404. snd_soc_unregister_codec(&pdev->dev);
  405. return 0;
  406. }
  407. MODULE_ALIAS("platform:wl1273-codec");
  408. static struct platform_driver wl1273_platform_driver = {
  409. .driver = {
  410. .name = "wl1273-codec",
  411. .owner = THIS_MODULE,
  412. },
  413. .probe = wl1273_platform_probe,
  414. .remove = __devexit_p(wl1273_platform_remove),
  415. };
  416. static int __init wl1273_init(void)
  417. {
  418. return platform_driver_register(&wl1273_platform_driver);
  419. }
  420. module_init(wl1273_init);
  421. static void __exit wl1273_exit(void)
  422. {
  423. platform_driver_unregister(&wl1273_platform_driver);
  424. }
  425. module_exit(wl1273_exit);
  426. MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
  427. MODULE_DESCRIPTION("ASoC WL1273 codec driver");
  428. MODULE_LICENSE("GPL");