wl1273.c 12 KB

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