wl1273.c 12 KB

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