omap-dmic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * omap-dmic.c -- OMAP ASoC DMIC DAI driver
  3. *
  4. * Copyright (C) 2010 - 2011 Texas Instruments
  5. *
  6. * Author: David Lambert <dlambert@ti.com>
  7. * Misael Lopez Cruz <misael.lopez@ti.com>
  8. * Liam Girdwood <lrg@ti.com>
  9. * Peter Ujfalusi <peter.ujfalusi@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * version 2 as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23. * 02110-1301 USA
  24. *
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/err.h>
  30. #include <linux/clk.h>
  31. #include <linux/io.h>
  32. #include <linux/slab.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/of_device.h>
  35. #include <sound/core.h>
  36. #include <sound/pcm.h>
  37. #include <sound/pcm_params.h>
  38. #include <sound/initval.h>
  39. #include <sound/soc.h>
  40. #include <sound/dmaengine_pcm.h>
  41. #include "omap-dmic.h"
  42. #include "sdma-pcm.h"
  43. struct omap_dmic {
  44. struct device *dev;
  45. void __iomem *io_base;
  46. struct clk *fclk;
  47. int fclk_freq;
  48. int out_freq;
  49. int clk_div;
  50. int sysclk;
  51. int threshold;
  52. u32 ch_enabled;
  53. bool active;
  54. struct mutex mutex;
  55. struct snd_dmaengine_dai_dma_data dma_data;
  56. };
  57. static inline void omap_dmic_write(struct omap_dmic *dmic, u16 reg, u32 val)
  58. {
  59. writel_relaxed(val, dmic->io_base + reg);
  60. }
  61. static inline int omap_dmic_read(struct omap_dmic *dmic, u16 reg)
  62. {
  63. return readl_relaxed(dmic->io_base + reg);
  64. }
  65. static inline void omap_dmic_start(struct omap_dmic *dmic)
  66. {
  67. u32 ctrl = omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG);
  68. /* Configure DMA controller */
  69. omap_dmic_write(dmic, OMAP_DMIC_DMAENABLE_SET_REG,
  70. OMAP_DMIC_DMA_ENABLE);
  71. omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG, ctrl | dmic->ch_enabled);
  72. }
  73. static inline void omap_dmic_stop(struct omap_dmic *dmic)
  74. {
  75. u32 ctrl = omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG);
  76. omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG,
  77. ctrl & ~OMAP_DMIC_UP_ENABLE_MASK);
  78. /* Disable DMA request generation */
  79. omap_dmic_write(dmic, OMAP_DMIC_DMAENABLE_CLR_REG,
  80. OMAP_DMIC_DMA_ENABLE);
  81. }
  82. static inline int dmic_is_enabled(struct omap_dmic *dmic)
  83. {
  84. return omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG) &
  85. OMAP_DMIC_UP_ENABLE_MASK;
  86. }
  87. static int omap_dmic_dai_startup(struct snd_pcm_substream *substream,
  88. struct snd_soc_dai *dai)
  89. {
  90. struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
  91. int ret = 0;
  92. mutex_lock(&dmic->mutex);
  93. if (!dai->active)
  94. dmic->active = 1;
  95. else
  96. ret = -EBUSY;
  97. mutex_unlock(&dmic->mutex);
  98. return ret;
  99. }
  100. static void omap_dmic_dai_shutdown(struct snd_pcm_substream *substream,
  101. struct snd_soc_dai *dai)
  102. {
  103. struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
  104. mutex_lock(&dmic->mutex);
  105. if (!dai->active)
  106. dmic->active = 0;
  107. mutex_unlock(&dmic->mutex);
  108. }
  109. static int omap_dmic_select_divider(struct omap_dmic *dmic, int sample_rate)
  110. {
  111. int divider = -EINVAL;
  112. /*
  113. * 192KHz rate is only supported with 19.2MHz/3.84MHz clock
  114. * configuration.
  115. */
  116. if (sample_rate == 192000) {
  117. if (dmic->fclk_freq == 19200000 && dmic->out_freq == 3840000)
  118. divider = 0x6; /* Divider: 5 (192KHz sampling rate) */
  119. else
  120. dev_err(dmic->dev,
  121. "invalid clock configuration for 192KHz\n");
  122. return divider;
  123. }
  124. switch (dmic->out_freq) {
  125. case 1536000:
  126. if (dmic->fclk_freq != 24576000)
  127. goto div_err;
  128. divider = 0x4; /* Divider: 16 */
  129. break;
  130. case 2400000:
  131. switch (dmic->fclk_freq) {
  132. case 12000000:
  133. divider = 0x5; /* Divider: 5 */
  134. break;
  135. case 19200000:
  136. divider = 0x0; /* Divider: 8 */
  137. break;
  138. case 24000000:
  139. divider = 0x2; /* Divider: 10 */
  140. break;
  141. default:
  142. goto div_err;
  143. }
  144. break;
  145. case 3072000:
  146. if (dmic->fclk_freq != 24576000)
  147. goto div_err;
  148. divider = 0x3; /* Divider: 8 */
  149. break;
  150. case 3840000:
  151. if (dmic->fclk_freq != 19200000)
  152. goto div_err;
  153. divider = 0x1; /* Divider: 5 (96KHz sampling rate) */
  154. break;
  155. default:
  156. dev_err(dmic->dev, "invalid out frequency: %dHz\n",
  157. dmic->out_freq);
  158. break;
  159. }
  160. return divider;
  161. div_err:
  162. dev_err(dmic->dev, "invalid out frequency %dHz for %dHz input\n",
  163. dmic->out_freq, dmic->fclk_freq);
  164. return -EINVAL;
  165. }
  166. static int omap_dmic_dai_hw_params(struct snd_pcm_substream *substream,
  167. struct snd_pcm_hw_params *params,
  168. struct snd_soc_dai *dai)
  169. {
  170. struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
  171. struct snd_dmaengine_dai_dma_data *dma_data;
  172. int channels;
  173. dmic->clk_div = omap_dmic_select_divider(dmic, params_rate(params));
  174. if (dmic->clk_div < 0) {
  175. dev_err(dmic->dev, "no valid divider for %dHz from %dHz\n",
  176. dmic->out_freq, dmic->fclk_freq);
  177. return -EINVAL;
  178. }
  179. dmic->ch_enabled = 0;
  180. channels = params_channels(params);
  181. switch (channels) {
  182. case 6:
  183. dmic->ch_enabled |= OMAP_DMIC_UP3_ENABLE;
  184. /* fall through */
  185. case 4:
  186. dmic->ch_enabled |= OMAP_DMIC_UP2_ENABLE;
  187. /* fall through */
  188. case 2:
  189. dmic->ch_enabled |= OMAP_DMIC_UP1_ENABLE;
  190. break;
  191. default:
  192. dev_err(dmic->dev, "invalid number of legacy channels\n");
  193. return -EINVAL;
  194. }
  195. /* packet size is threshold * channels */
  196. dma_data = snd_soc_dai_get_dma_data(dai, substream);
  197. dma_data->maxburst = dmic->threshold * channels;
  198. return 0;
  199. }
  200. static int omap_dmic_dai_prepare(struct snd_pcm_substream *substream,
  201. struct snd_soc_dai *dai)
  202. {
  203. struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
  204. u32 ctrl;
  205. /* Configure uplink threshold */
  206. omap_dmic_write(dmic, OMAP_DMIC_FIFO_CTRL_REG, dmic->threshold);
  207. ctrl = omap_dmic_read(dmic, OMAP_DMIC_CTRL_REG);
  208. /* Set dmic out format */
  209. ctrl &= ~(OMAP_DMIC_FORMAT | OMAP_DMIC_POLAR_MASK);
  210. ctrl |= (OMAP_DMICOUTFORMAT_LJUST | OMAP_DMIC_POLAR1 |
  211. OMAP_DMIC_POLAR2 | OMAP_DMIC_POLAR3);
  212. /* Configure dmic clock divider */
  213. ctrl &= ~OMAP_DMIC_CLK_DIV_MASK;
  214. ctrl |= OMAP_DMIC_CLK_DIV(dmic->clk_div);
  215. omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG, ctrl);
  216. omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG,
  217. ctrl | OMAP_DMICOUTFORMAT_LJUST | OMAP_DMIC_POLAR1 |
  218. OMAP_DMIC_POLAR2 | OMAP_DMIC_POLAR3);
  219. return 0;
  220. }
  221. static int omap_dmic_dai_trigger(struct snd_pcm_substream *substream,
  222. int cmd, struct snd_soc_dai *dai)
  223. {
  224. struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
  225. switch (cmd) {
  226. case SNDRV_PCM_TRIGGER_START:
  227. omap_dmic_start(dmic);
  228. break;
  229. case SNDRV_PCM_TRIGGER_STOP:
  230. omap_dmic_stop(dmic);
  231. break;
  232. default:
  233. break;
  234. }
  235. return 0;
  236. }
  237. static int omap_dmic_select_fclk(struct omap_dmic *dmic, int clk_id,
  238. unsigned int freq)
  239. {
  240. struct clk *parent_clk, *mux;
  241. char *parent_clk_name;
  242. int ret = 0;
  243. switch (freq) {
  244. case 12000000:
  245. case 19200000:
  246. case 24000000:
  247. case 24576000:
  248. break;
  249. default:
  250. dev_err(dmic->dev, "invalid input frequency: %dHz\n", freq);
  251. dmic->fclk_freq = 0;
  252. return -EINVAL;
  253. }
  254. if (dmic->sysclk == clk_id) {
  255. dmic->fclk_freq = freq;
  256. return 0;
  257. }
  258. /* re-parent not allowed if a stream is ongoing */
  259. if (dmic->active && dmic_is_enabled(dmic)) {
  260. dev_err(dmic->dev, "can't re-parent when DMIC active\n");
  261. return -EBUSY;
  262. }
  263. switch (clk_id) {
  264. case OMAP_DMIC_SYSCLK_PAD_CLKS:
  265. parent_clk_name = "pad_clks_ck";
  266. break;
  267. case OMAP_DMIC_SYSCLK_SLIMBLUS_CLKS:
  268. parent_clk_name = "slimbus_clk";
  269. break;
  270. case OMAP_DMIC_SYSCLK_SYNC_MUX_CLKS:
  271. parent_clk_name = "dmic_sync_mux_ck";
  272. break;
  273. default:
  274. dev_err(dmic->dev, "fclk clk_id (%d) not supported\n", clk_id);
  275. return -EINVAL;
  276. }
  277. parent_clk = clk_get(dmic->dev, parent_clk_name);
  278. if (IS_ERR(parent_clk)) {
  279. dev_err(dmic->dev, "can't get %s\n", parent_clk_name);
  280. return -ENODEV;
  281. }
  282. mux = clk_get_parent(dmic->fclk);
  283. if (IS_ERR(mux)) {
  284. dev_err(dmic->dev, "can't get fck mux parent\n");
  285. clk_put(parent_clk);
  286. return -ENODEV;
  287. }
  288. mutex_lock(&dmic->mutex);
  289. if (dmic->active) {
  290. /* disable clock while reparenting */
  291. pm_runtime_put_sync(dmic->dev);
  292. ret = clk_set_parent(mux, parent_clk);
  293. pm_runtime_get_sync(dmic->dev);
  294. } else {
  295. ret = clk_set_parent(mux, parent_clk);
  296. }
  297. mutex_unlock(&dmic->mutex);
  298. if (ret < 0) {
  299. dev_err(dmic->dev, "re-parent failed\n");
  300. goto err_busy;
  301. }
  302. dmic->sysclk = clk_id;
  303. dmic->fclk_freq = freq;
  304. err_busy:
  305. clk_put(mux);
  306. clk_put(parent_clk);
  307. return ret;
  308. }
  309. static int omap_dmic_select_outclk(struct omap_dmic *dmic, int clk_id,
  310. unsigned int freq)
  311. {
  312. int ret = 0;
  313. if (clk_id != OMAP_DMIC_ABE_DMIC_CLK) {
  314. dev_err(dmic->dev, "output clk_id (%d) not supported\n",
  315. clk_id);
  316. return -EINVAL;
  317. }
  318. switch (freq) {
  319. case 1536000:
  320. case 2400000:
  321. case 3072000:
  322. case 3840000:
  323. dmic->out_freq = freq;
  324. break;
  325. default:
  326. dev_err(dmic->dev, "invalid out frequency: %dHz\n", freq);
  327. dmic->out_freq = 0;
  328. ret = -EINVAL;
  329. }
  330. return ret;
  331. }
  332. static int omap_dmic_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
  333. unsigned int freq, int dir)
  334. {
  335. struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
  336. if (dir == SND_SOC_CLOCK_IN)
  337. return omap_dmic_select_fclk(dmic, clk_id, freq);
  338. else if (dir == SND_SOC_CLOCK_OUT)
  339. return omap_dmic_select_outclk(dmic, clk_id, freq);
  340. dev_err(dmic->dev, "invalid clock direction (%d)\n", dir);
  341. return -EINVAL;
  342. }
  343. static const struct snd_soc_dai_ops omap_dmic_dai_ops = {
  344. .startup = omap_dmic_dai_startup,
  345. .shutdown = omap_dmic_dai_shutdown,
  346. .hw_params = omap_dmic_dai_hw_params,
  347. .prepare = omap_dmic_dai_prepare,
  348. .trigger = omap_dmic_dai_trigger,
  349. .set_sysclk = omap_dmic_set_dai_sysclk,
  350. };
  351. static int omap_dmic_probe(struct snd_soc_dai *dai)
  352. {
  353. struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
  354. pm_runtime_enable(dmic->dev);
  355. /* Disable lines while request is ongoing */
  356. pm_runtime_get_sync(dmic->dev);
  357. omap_dmic_write(dmic, OMAP_DMIC_CTRL_REG, 0x00);
  358. pm_runtime_put_sync(dmic->dev);
  359. /* Configure DMIC threshold value */
  360. dmic->threshold = OMAP_DMIC_THRES_MAX - 3;
  361. snd_soc_dai_init_dma_data(dai, NULL, &dmic->dma_data);
  362. return 0;
  363. }
  364. static int omap_dmic_remove(struct snd_soc_dai *dai)
  365. {
  366. struct omap_dmic *dmic = snd_soc_dai_get_drvdata(dai);
  367. pm_runtime_disable(dmic->dev);
  368. return 0;
  369. }
  370. static struct snd_soc_dai_driver omap_dmic_dai = {
  371. .name = "omap-dmic",
  372. .probe = omap_dmic_probe,
  373. .remove = omap_dmic_remove,
  374. .capture = {
  375. .channels_min = 2,
  376. .channels_max = 6,
  377. .rates = SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000,
  378. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  379. .sig_bits = 24,
  380. },
  381. .ops = &omap_dmic_dai_ops,
  382. };
  383. static const struct snd_soc_component_driver omap_dmic_component = {
  384. .name = "omap-dmic",
  385. };
  386. static int asoc_dmic_probe(struct platform_device *pdev)
  387. {
  388. struct omap_dmic *dmic;
  389. struct resource *res;
  390. int ret;
  391. dmic = devm_kzalloc(&pdev->dev, sizeof(struct omap_dmic), GFP_KERNEL);
  392. if (!dmic)
  393. return -ENOMEM;
  394. platform_set_drvdata(pdev, dmic);
  395. dmic->dev = &pdev->dev;
  396. dmic->sysclk = OMAP_DMIC_SYSCLK_SYNC_MUX_CLKS;
  397. mutex_init(&dmic->mutex);
  398. dmic->fclk = devm_clk_get(dmic->dev, "fck");
  399. if (IS_ERR(dmic->fclk)) {
  400. dev_err(dmic->dev, "cant get fck\n");
  401. return -ENODEV;
  402. }
  403. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
  404. if (!res) {
  405. dev_err(dmic->dev, "invalid dma memory resource\n");
  406. return -ENODEV;
  407. }
  408. dmic->dma_data.addr = res->start + OMAP_DMIC_DATA_REG;
  409. dmic->dma_data.filter_data = "up_link";
  410. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
  411. dmic->io_base = devm_ioremap_resource(&pdev->dev, res);
  412. if (IS_ERR(dmic->io_base))
  413. return PTR_ERR(dmic->io_base);
  414. ret = devm_snd_soc_register_component(&pdev->dev,
  415. &omap_dmic_component,
  416. &omap_dmic_dai, 1);
  417. if (ret)
  418. return ret;
  419. ret = sdma_pcm_platform_register(&pdev->dev, NULL, "up_link");
  420. if (ret)
  421. return ret;
  422. return 0;
  423. }
  424. static const struct of_device_id omap_dmic_of_match[] = {
  425. { .compatible = "ti,omap4-dmic", },
  426. { }
  427. };
  428. MODULE_DEVICE_TABLE(of, omap_dmic_of_match);
  429. static struct platform_driver asoc_dmic_driver = {
  430. .driver = {
  431. .name = "omap-dmic",
  432. .of_match_table = omap_dmic_of_match,
  433. },
  434. .probe = asoc_dmic_probe,
  435. };
  436. module_platform_driver(asoc_dmic_driver);
  437. MODULE_ALIAS("platform:omap-dmic");
  438. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  439. MODULE_DESCRIPTION("OMAP DMIC ASoC Interface");
  440. MODULE_LICENSE("GPL");