omap-dmic.c 12 KB

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