wl1273.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 <linux/module.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/initval.h>
  30. #include "wl1273.h"
  31. enum wl1273_mode { WL1273_MODE_BT, WL1273_MODE_FM_RX, WL1273_MODE_FM_TX };
  32. /* codec private data */
  33. struct wl1273_priv {
  34. enum wl1273_mode mode;
  35. struct wl1273_core *core;
  36. unsigned int channels;
  37. };
  38. static int snd_wl1273_fm_set_i2s_mode(struct wl1273_core *core,
  39. int rate, int width)
  40. {
  41. struct device *dev = &core->client->dev;
  42. int r = 0;
  43. u16 mode;
  44. dev_dbg(dev, "rate: %d\n", rate);
  45. dev_dbg(dev, "width: %d\n", width);
  46. mutex_lock(&core->lock);
  47. mode = core->i2s_mode & ~WL1273_IS2_WIDTH & ~WL1273_IS2_RATE;
  48. switch (rate) {
  49. case 48000:
  50. mode |= WL1273_IS2_RATE_48K;
  51. break;
  52. case 44100:
  53. mode |= WL1273_IS2_RATE_44_1K;
  54. break;
  55. case 32000:
  56. mode |= WL1273_IS2_RATE_32K;
  57. break;
  58. case 22050:
  59. mode |= WL1273_IS2_RATE_22_05K;
  60. break;
  61. case 16000:
  62. mode |= WL1273_IS2_RATE_16K;
  63. break;
  64. case 12000:
  65. mode |= WL1273_IS2_RATE_12K;
  66. break;
  67. case 11025:
  68. mode |= WL1273_IS2_RATE_11_025;
  69. break;
  70. case 8000:
  71. mode |= WL1273_IS2_RATE_8K;
  72. break;
  73. default:
  74. dev_err(dev, "Sampling rate: %d not supported\n", rate);
  75. r = -EINVAL;
  76. goto out;
  77. }
  78. switch (width) {
  79. case 16:
  80. mode |= WL1273_IS2_WIDTH_32;
  81. break;
  82. case 20:
  83. mode |= WL1273_IS2_WIDTH_40;
  84. break;
  85. case 24:
  86. mode |= WL1273_IS2_WIDTH_48;
  87. break;
  88. case 25:
  89. mode |= WL1273_IS2_WIDTH_50;
  90. break;
  91. case 30:
  92. mode |= WL1273_IS2_WIDTH_60;
  93. break;
  94. case 32:
  95. mode |= WL1273_IS2_WIDTH_64;
  96. break;
  97. case 40:
  98. mode |= WL1273_IS2_WIDTH_80;
  99. break;
  100. case 48:
  101. mode |= WL1273_IS2_WIDTH_96;
  102. break;
  103. case 64:
  104. mode |= WL1273_IS2_WIDTH_128;
  105. break;
  106. default:
  107. dev_err(dev, "Data width: %d not supported\n", width);
  108. r = -EINVAL;
  109. goto out;
  110. }
  111. dev_dbg(dev, "WL1273_I2S_DEF_MODE: 0x%04x\n", WL1273_I2S_DEF_MODE);
  112. dev_dbg(dev, "core->i2s_mode: 0x%04x\n", core->i2s_mode);
  113. dev_dbg(dev, "mode: 0x%04x\n", mode);
  114. if (core->i2s_mode != mode) {
  115. r = core->write(core, WL1273_I2S_MODE_CONFIG_SET, mode);
  116. if (r)
  117. goto out;
  118. core->i2s_mode = mode;
  119. r = core->write(core, WL1273_AUDIO_ENABLE,
  120. WL1273_AUDIO_ENABLE_I2S);
  121. if (r)
  122. goto out;
  123. }
  124. out:
  125. mutex_unlock(&core->lock);
  126. return r;
  127. }
  128. static int snd_wl1273_fm_set_channel_number(struct wl1273_core *core,
  129. int channel_number)
  130. {
  131. struct device *dev = &core->client->dev;
  132. int r = 0;
  133. dev_dbg(dev, "%s\n", __func__);
  134. mutex_lock(&core->lock);
  135. if (core->channel_number == channel_number)
  136. goto out;
  137. if (channel_number == 1 && core->mode == WL1273_MODE_RX)
  138. r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
  139. else if (channel_number == 1 && core->mode == WL1273_MODE_TX)
  140. r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
  141. else if (channel_number == 2 && core->mode == WL1273_MODE_RX)
  142. r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
  143. else if (channel_number == 2 && core->mode == WL1273_MODE_TX)
  144. r = core->write(core, WL1273_MONO_SET, WL1273_TX_STEREO);
  145. else
  146. r = -EINVAL;
  147. out:
  148. mutex_unlock(&core->lock);
  149. return r;
  150. }
  151. static int snd_wl1273_get_audio_route(struct snd_kcontrol *kcontrol,
  152. struct snd_ctl_elem_value *ucontrol)
  153. {
  154. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  155. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  156. ucontrol->value.enumerated.item[0] = wl1273->mode;
  157. return 0;
  158. }
  159. /*
  160. * TODO: Implement the audio routing in the driver. Now this control
  161. * only indicates the setting that has been done elsewhere (in the user
  162. * space).
  163. */
  164. static const char * const wl1273_audio_route[] = { "Bt", "FmRx", "FmTx" };
  165. static int snd_wl1273_set_audio_route(struct snd_kcontrol *kcontrol,
  166. struct snd_ctl_elem_value *ucontrol)
  167. {
  168. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  169. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  170. if (wl1273->mode == ucontrol->value.enumerated.item[0])
  171. return 0;
  172. /* Do not allow changes while stream is running */
  173. if (snd_soc_codec_is_active(codec))
  174. return -EPERM;
  175. if (ucontrol->value.enumerated.item[0] >= ARRAY_SIZE(wl1273_audio_route))
  176. return -EINVAL;
  177. wl1273->mode = ucontrol->value.enumerated.item[0];
  178. return 1;
  179. }
  180. static SOC_ENUM_SINGLE_EXT_DECL(wl1273_enum, wl1273_audio_route);
  181. static int snd_wl1273_fm_audio_get(struct snd_kcontrol *kcontrol,
  182. struct snd_ctl_elem_value *ucontrol)
  183. {
  184. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  185. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  186. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  187. ucontrol->value.enumerated.item[0] = wl1273->core->audio_mode;
  188. return 0;
  189. }
  190. static int snd_wl1273_fm_audio_put(struct snd_kcontrol *kcontrol,
  191. struct snd_ctl_elem_value *ucontrol)
  192. {
  193. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  194. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  195. int val, r = 0;
  196. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  197. val = ucontrol->value.enumerated.item[0];
  198. if (wl1273->core->audio_mode == val)
  199. return 0;
  200. r = wl1273->core->set_audio(wl1273->core, val);
  201. if (r < 0)
  202. return r;
  203. return 1;
  204. }
  205. static const char * const wl1273_audio_strings[] = { "Digital", "Analog" };
  206. static SOC_ENUM_SINGLE_EXT_DECL(wl1273_audio_enum, wl1273_audio_strings);
  207. static int snd_wl1273_fm_volume_get(struct snd_kcontrol *kcontrol,
  208. struct snd_ctl_elem_value *ucontrol)
  209. {
  210. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  211. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  212. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  213. ucontrol->value.integer.value[0] = wl1273->core->volume;
  214. return 0;
  215. }
  216. static int snd_wl1273_fm_volume_put(struct snd_kcontrol *kcontrol,
  217. struct snd_ctl_elem_value *ucontrol)
  218. {
  219. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  220. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  221. int r;
  222. dev_dbg(codec->dev, "%s: enter.\n", __func__);
  223. r = wl1273->core->set_volume(wl1273->core,
  224. ucontrol->value.integer.value[0]);
  225. if (r)
  226. return r;
  227. return 1;
  228. }
  229. static const struct snd_kcontrol_new wl1273_controls[] = {
  230. SOC_ENUM_EXT("Codec Mode", wl1273_enum,
  231. snd_wl1273_get_audio_route, snd_wl1273_set_audio_route),
  232. SOC_ENUM_EXT("Audio Switch", wl1273_audio_enum,
  233. snd_wl1273_fm_audio_get, snd_wl1273_fm_audio_put),
  234. SOC_SINGLE_EXT("Volume", 0, 0, WL1273_MAX_VOLUME, 0,
  235. snd_wl1273_fm_volume_get, snd_wl1273_fm_volume_put),
  236. };
  237. static const struct snd_soc_dapm_widget wl1273_dapm_widgets[] = {
  238. SND_SOC_DAPM_INPUT("RX"),
  239. SND_SOC_DAPM_OUTPUT("TX"),
  240. };
  241. static const struct snd_soc_dapm_route wl1273_dapm_routes[] = {
  242. { "Capture", NULL, "RX" },
  243. { "TX", NULL, "Playback" },
  244. };
  245. static int wl1273_startup(struct snd_pcm_substream *substream,
  246. struct snd_soc_dai *dai)
  247. {
  248. struct snd_soc_codec *codec = dai->codec;
  249. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
  250. switch (wl1273->mode) {
  251. case WL1273_MODE_BT:
  252. snd_pcm_hw_constraint_single(substream->runtime,
  253. SNDRV_PCM_HW_PARAM_RATE, 8000);
  254. snd_pcm_hw_constraint_single(substream->runtime,
  255. SNDRV_PCM_HW_PARAM_CHANNELS, 1);
  256. break;
  257. case WL1273_MODE_FM_RX:
  258. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  259. pr_err("Cannot play in RX mode.\n");
  260. return -EINVAL;
  261. }
  262. break;
  263. case WL1273_MODE_FM_TX:
  264. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  265. pr_err("Cannot capture in TX mode.\n");
  266. return -EINVAL;
  267. }
  268. break;
  269. default:
  270. return -EINVAL;
  271. break;
  272. }
  273. return 0;
  274. }
  275. static int wl1273_hw_params(struct snd_pcm_substream *substream,
  276. struct snd_pcm_hw_params *params,
  277. struct snd_soc_dai *dai)
  278. {
  279. struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(dai->codec);
  280. struct wl1273_core *core = wl1273->core;
  281. unsigned int rate, width, r;
  282. if (params_width(params) != 16) {
  283. dev_err(dai->dev, "%d bits/sample not supported\n",
  284. params_width(params));
  285. return -EINVAL;
  286. }
  287. rate = params_rate(params);
  288. width = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
  289. if (wl1273->mode == WL1273_MODE_BT) {
  290. if (rate != 8000) {
  291. pr_err("Rate %d not supported.\n", params_rate(params));
  292. return -EINVAL;
  293. }
  294. if (params_channels(params) != 1) {
  295. pr_err("Only mono supported.\n");
  296. return -EINVAL;
  297. }
  298. return 0;
  299. }
  300. if (wl1273->mode == WL1273_MODE_FM_TX &&
  301. substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  302. pr_err("Only playback supported with TX.\n");
  303. return -EINVAL;
  304. }
  305. if (wl1273->mode == WL1273_MODE_FM_RX &&
  306. substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  307. pr_err("Only capture supported with RX.\n");
  308. return -EINVAL;
  309. }
  310. if (wl1273->mode != WL1273_MODE_FM_RX &&
  311. wl1273->mode != WL1273_MODE_FM_TX) {
  312. pr_err("Unexpected mode: %d.\n", wl1273->mode);
  313. return -EINVAL;
  314. }
  315. r = snd_wl1273_fm_set_i2s_mode(core, rate, width);
  316. if (r)
  317. return r;
  318. wl1273->channels = params_channels(params);
  319. r = snd_wl1273_fm_set_channel_number(core, wl1273->channels);
  320. if (r)
  321. return r;
  322. return 0;
  323. }
  324. static const struct snd_soc_dai_ops wl1273_dai_ops = {
  325. .startup = wl1273_startup,
  326. .hw_params = wl1273_hw_params,
  327. };
  328. static struct snd_soc_dai_driver wl1273_dai = {
  329. .name = "wl1273-fm",
  330. .playback = {
  331. .stream_name = "Playback",
  332. .channels_min = 1,
  333. .channels_max = 2,
  334. .rates = SNDRV_PCM_RATE_8000_48000,
  335. .formats = SNDRV_PCM_FMTBIT_S16_LE},
  336. .capture = {
  337. .stream_name = "Capture",
  338. .channels_min = 1,
  339. .channels_max = 2,
  340. .rates = SNDRV_PCM_RATE_8000_48000,
  341. .formats = SNDRV_PCM_FMTBIT_S16_LE},
  342. .ops = &wl1273_dai_ops,
  343. };
  344. /* Audio interface format for the soc_card driver */
  345. int wl1273_get_format(struct snd_soc_codec *codec, unsigned int *fmt)
  346. {
  347. struct wl1273_priv *wl1273;
  348. if (codec == NULL || fmt == NULL)
  349. return -EINVAL;
  350. wl1273 = snd_soc_codec_get_drvdata(codec);
  351. switch (wl1273->mode) {
  352. case WL1273_MODE_FM_RX:
  353. case WL1273_MODE_FM_TX:
  354. *fmt = SND_SOC_DAIFMT_I2S |
  355. SND_SOC_DAIFMT_NB_NF |
  356. SND_SOC_DAIFMT_CBM_CFM;
  357. break;
  358. case WL1273_MODE_BT:
  359. *fmt = SND_SOC_DAIFMT_DSP_A |
  360. SND_SOC_DAIFMT_IB_NF |
  361. SND_SOC_DAIFMT_CBM_CFM;
  362. break;
  363. default:
  364. return -EINVAL;
  365. }
  366. return 0;
  367. }
  368. EXPORT_SYMBOL_GPL(wl1273_get_format);
  369. static int wl1273_probe(struct snd_soc_codec *codec)
  370. {
  371. struct wl1273_core **core = codec->dev->platform_data;
  372. struct wl1273_priv *wl1273;
  373. dev_dbg(codec->dev, "%s.\n", __func__);
  374. if (!core) {
  375. dev_err(codec->dev, "Platform data is missing.\n");
  376. return -EINVAL;
  377. }
  378. wl1273 = kzalloc(sizeof(struct wl1273_priv), GFP_KERNEL);
  379. if (!wl1273)
  380. return -ENOMEM;
  381. wl1273->mode = WL1273_MODE_BT;
  382. wl1273->core = *core;
  383. snd_soc_codec_set_drvdata(codec, wl1273);
  384. return 0;
  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. .component_driver = {
  397. .controls = wl1273_controls,
  398. .num_controls = ARRAY_SIZE(wl1273_controls),
  399. .dapm_widgets = wl1273_dapm_widgets,
  400. .num_dapm_widgets = ARRAY_SIZE(wl1273_dapm_widgets),
  401. .dapm_routes = wl1273_dapm_routes,
  402. .num_dapm_routes = ARRAY_SIZE(wl1273_dapm_routes),
  403. },
  404. };
  405. static int wl1273_platform_probe(struct platform_device *pdev)
  406. {
  407. return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wl1273,
  408. &wl1273_dai, 1);
  409. }
  410. static int wl1273_platform_remove(struct platform_device *pdev)
  411. {
  412. snd_soc_unregister_codec(&pdev->dev);
  413. return 0;
  414. }
  415. MODULE_ALIAS("platform:wl1273-codec");
  416. static struct platform_driver wl1273_platform_driver = {
  417. .driver = {
  418. .name = "wl1273-codec",
  419. },
  420. .probe = wl1273_platform_probe,
  421. .remove = wl1273_platform_remove,
  422. };
  423. module_platform_driver(wl1273_platform_driver);
  424. MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
  425. MODULE_DESCRIPTION("ASoC WL1273 codec driver");
  426. MODULE_LICENSE("GPL");