omap-mcpdm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * omap-mcpdm.c -- OMAP ALSA SoC DAI driver using McPDM port
  3. *
  4. * Copyright (C) 2009 - 2011 Texas Instruments
  5. *
  6. * Author: Misael Lopez Cruz <misael.lopez@ti.com>
  7. * Contact: Jorge Eduardo Candelaria <x0107209@ti.com>
  8. * Margarita Olaya <magi.olaya@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/interrupt.h>
  30. #include <linux/err.h>
  31. #include <linux/io.h>
  32. #include <linux/irq.h>
  33. #include <linux/slab.h>
  34. #include <linux/pm_runtime.h>
  35. #include <linux/of_device.h>
  36. #include <sound/core.h>
  37. #include <sound/pcm.h>
  38. #include <sound/pcm_params.h>
  39. #include <sound/soc.h>
  40. #include <plat/dma.h>
  41. #include <plat/omap_hwmod.h>
  42. #include "omap-mcpdm.h"
  43. #include "omap-pcm.h"
  44. struct omap_mcpdm {
  45. struct device *dev;
  46. unsigned long phys_base;
  47. void __iomem *io_base;
  48. int irq;
  49. struct mutex mutex;
  50. /* channel data */
  51. u32 dn_channels;
  52. u32 up_channels;
  53. /* McPDM FIFO thresholds */
  54. u32 dn_threshold;
  55. u32 up_threshold;
  56. /* McPDM dn offsets for rx1, and 2 channels */
  57. u32 dn_rx_offset;
  58. };
  59. /*
  60. * Stream DMA parameters
  61. */
  62. static struct omap_pcm_dma_data omap_mcpdm_dai_dma_params[] = {
  63. {
  64. .name = "Audio playback",
  65. .dma_req = OMAP44XX_DMA_MCPDM_DL,
  66. .data_type = OMAP_DMA_DATA_TYPE_S32,
  67. .sync_mode = OMAP_DMA_SYNC_PACKET,
  68. .port_addr = OMAP44XX_MCPDM_L3_BASE + MCPDM_REG_DN_DATA,
  69. },
  70. {
  71. .name = "Audio capture",
  72. .dma_req = OMAP44XX_DMA_MCPDM_UP,
  73. .data_type = OMAP_DMA_DATA_TYPE_S32,
  74. .sync_mode = OMAP_DMA_SYNC_PACKET,
  75. .port_addr = OMAP44XX_MCPDM_L3_BASE + MCPDM_REG_UP_DATA,
  76. },
  77. };
  78. static inline void omap_mcpdm_write(struct omap_mcpdm *mcpdm, u16 reg, u32 val)
  79. {
  80. __raw_writel(val, mcpdm->io_base + reg);
  81. }
  82. static inline int omap_mcpdm_read(struct omap_mcpdm *mcpdm, u16 reg)
  83. {
  84. return __raw_readl(mcpdm->io_base + reg);
  85. }
  86. #ifdef DEBUG
  87. static void omap_mcpdm_reg_dump(struct omap_mcpdm *mcpdm)
  88. {
  89. dev_dbg(mcpdm->dev, "***********************\n");
  90. dev_dbg(mcpdm->dev, "IRQSTATUS_RAW: 0x%04x\n",
  91. omap_mcpdm_read(mcpdm, MCPDM_REG_IRQSTATUS_RAW));
  92. dev_dbg(mcpdm->dev, "IRQSTATUS: 0x%04x\n",
  93. omap_mcpdm_read(mcpdm, MCPDM_REG_IRQSTATUS));
  94. dev_dbg(mcpdm->dev, "IRQENABLE_SET: 0x%04x\n",
  95. omap_mcpdm_read(mcpdm, MCPDM_REG_IRQENABLE_SET));
  96. dev_dbg(mcpdm->dev, "IRQENABLE_CLR: 0x%04x\n",
  97. omap_mcpdm_read(mcpdm, MCPDM_REG_IRQENABLE_CLR));
  98. dev_dbg(mcpdm->dev, "IRQWAKE_EN: 0x%04x\n",
  99. omap_mcpdm_read(mcpdm, MCPDM_REG_IRQWAKE_EN));
  100. dev_dbg(mcpdm->dev, "DMAENABLE_SET: 0x%04x\n",
  101. omap_mcpdm_read(mcpdm, MCPDM_REG_DMAENABLE_SET));
  102. dev_dbg(mcpdm->dev, "DMAENABLE_CLR: 0x%04x\n",
  103. omap_mcpdm_read(mcpdm, MCPDM_REG_DMAENABLE_CLR));
  104. dev_dbg(mcpdm->dev, "DMAWAKEEN: 0x%04x\n",
  105. omap_mcpdm_read(mcpdm, MCPDM_REG_DMAWAKEEN));
  106. dev_dbg(mcpdm->dev, "CTRL: 0x%04x\n",
  107. omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL));
  108. dev_dbg(mcpdm->dev, "DN_DATA: 0x%04x\n",
  109. omap_mcpdm_read(mcpdm, MCPDM_REG_DN_DATA));
  110. dev_dbg(mcpdm->dev, "UP_DATA: 0x%04x\n",
  111. omap_mcpdm_read(mcpdm, MCPDM_REG_UP_DATA));
  112. dev_dbg(mcpdm->dev, "FIFO_CTRL_DN: 0x%04x\n",
  113. omap_mcpdm_read(mcpdm, MCPDM_REG_FIFO_CTRL_DN));
  114. dev_dbg(mcpdm->dev, "FIFO_CTRL_UP: 0x%04x\n",
  115. omap_mcpdm_read(mcpdm, MCPDM_REG_FIFO_CTRL_UP));
  116. dev_dbg(mcpdm->dev, "***********************\n");
  117. }
  118. #else
  119. static void omap_mcpdm_reg_dump(struct omap_mcpdm *mcpdm) {}
  120. #endif
  121. /*
  122. * Enables the transfer through the PDM interface to/from the Phoenix
  123. * codec by enabling the corresponding UP or DN channels.
  124. */
  125. static void omap_mcpdm_start(struct omap_mcpdm *mcpdm)
  126. {
  127. u32 ctrl = omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL);
  128. ctrl |= (MCPDM_SW_DN_RST | MCPDM_SW_UP_RST);
  129. omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl);
  130. ctrl |= mcpdm->dn_channels | mcpdm->up_channels;
  131. omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl);
  132. ctrl &= ~(MCPDM_SW_DN_RST | MCPDM_SW_UP_RST);
  133. omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl);
  134. }
  135. /*
  136. * Disables the transfer through the PDM interface to/from the Phoenix
  137. * codec by disabling the corresponding UP or DN channels.
  138. */
  139. static void omap_mcpdm_stop(struct omap_mcpdm *mcpdm)
  140. {
  141. u32 ctrl = omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL);
  142. ctrl |= (MCPDM_SW_DN_RST | MCPDM_SW_UP_RST);
  143. omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl);
  144. ctrl &= ~(mcpdm->dn_channels | mcpdm->up_channels);
  145. omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl);
  146. ctrl &= ~(MCPDM_SW_DN_RST | MCPDM_SW_UP_RST);
  147. omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl);
  148. }
  149. /*
  150. * Is the physical McPDM interface active.
  151. */
  152. static inline int omap_mcpdm_active(struct omap_mcpdm *mcpdm)
  153. {
  154. return omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL) &
  155. (MCPDM_PDM_DN_MASK | MCPDM_PDM_UP_MASK);
  156. }
  157. /*
  158. * Configures McPDM uplink, and downlink for audio.
  159. * This function should be called before omap_mcpdm_start.
  160. */
  161. static void omap_mcpdm_open_streams(struct omap_mcpdm *mcpdm)
  162. {
  163. omap_mcpdm_write(mcpdm, MCPDM_REG_IRQENABLE_SET,
  164. MCPDM_DN_IRQ_EMPTY | MCPDM_DN_IRQ_FULL |
  165. MCPDM_UP_IRQ_EMPTY | MCPDM_UP_IRQ_FULL);
  166. /* Enable DN RX1/2 offset cancellation feature, if configured */
  167. if (mcpdm->dn_rx_offset) {
  168. u32 dn_offset = mcpdm->dn_rx_offset;
  169. omap_mcpdm_write(mcpdm, MCPDM_REG_DN_OFFSET, dn_offset);
  170. dn_offset |= (MCPDM_DN_OFST_RX1_EN | MCPDM_DN_OFST_RX2_EN);
  171. omap_mcpdm_write(mcpdm, MCPDM_REG_DN_OFFSET, dn_offset);
  172. }
  173. omap_mcpdm_write(mcpdm, MCPDM_REG_FIFO_CTRL_DN, mcpdm->dn_threshold);
  174. omap_mcpdm_write(mcpdm, MCPDM_REG_FIFO_CTRL_UP, mcpdm->up_threshold);
  175. omap_mcpdm_write(mcpdm, MCPDM_REG_DMAENABLE_SET,
  176. MCPDM_DMA_DN_ENABLE | MCPDM_DMA_UP_ENABLE);
  177. }
  178. /*
  179. * Cleans McPDM uplink, and downlink configuration.
  180. * This function should be called when the stream is closed.
  181. */
  182. static void omap_mcpdm_close_streams(struct omap_mcpdm *mcpdm)
  183. {
  184. /* Disable irq request generation for downlink */
  185. omap_mcpdm_write(mcpdm, MCPDM_REG_IRQENABLE_CLR,
  186. MCPDM_DN_IRQ_EMPTY | MCPDM_DN_IRQ_FULL);
  187. /* Disable DMA request generation for downlink */
  188. omap_mcpdm_write(mcpdm, MCPDM_REG_DMAENABLE_CLR, MCPDM_DMA_DN_ENABLE);
  189. /* Disable irq request generation for uplink */
  190. omap_mcpdm_write(mcpdm, MCPDM_REG_IRQENABLE_CLR,
  191. MCPDM_UP_IRQ_EMPTY | MCPDM_UP_IRQ_FULL);
  192. /* Disable DMA request generation for uplink */
  193. omap_mcpdm_write(mcpdm, MCPDM_REG_DMAENABLE_CLR, MCPDM_DMA_UP_ENABLE);
  194. /* Disable RX1/2 offset cancellation */
  195. if (mcpdm->dn_rx_offset)
  196. omap_mcpdm_write(mcpdm, MCPDM_REG_DN_OFFSET, 0);
  197. }
  198. static irqreturn_t omap_mcpdm_irq_handler(int irq, void *dev_id)
  199. {
  200. struct omap_mcpdm *mcpdm = dev_id;
  201. int irq_status;
  202. irq_status = omap_mcpdm_read(mcpdm, MCPDM_REG_IRQSTATUS);
  203. /* Acknowledge irq event */
  204. omap_mcpdm_write(mcpdm, MCPDM_REG_IRQSTATUS, irq_status);
  205. if (irq_status & MCPDM_DN_IRQ_FULL)
  206. dev_dbg(mcpdm->dev, "DN (playback) FIFO Full\n");
  207. if (irq_status & MCPDM_DN_IRQ_EMPTY)
  208. dev_dbg(mcpdm->dev, "DN (playback) FIFO Empty\n");
  209. if (irq_status & MCPDM_DN_IRQ)
  210. dev_dbg(mcpdm->dev, "DN (playback) write request\n");
  211. if (irq_status & MCPDM_UP_IRQ_FULL)
  212. dev_dbg(mcpdm->dev, "UP (capture) FIFO Full\n");
  213. if (irq_status & MCPDM_UP_IRQ_EMPTY)
  214. dev_dbg(mcpdm->dev, "UP (capture) FIFO Empty\n");
  215. if (irq_status & MCPDM_UP_IRQ)
  216. dev_dbg(mcpdm->dev, "UP (capture) write request\n");
  217. return IRQ_HANDLED;
  218. }
  219. static int omap_mcpdm_dai_startup(struct snd_pcm_substream *substream,
  220. struct snd_soc_dai *dai)
  221. {
  222. struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
  223. mutex_lock(&mcpdm->mutex);
  224. if (!dai->active) {
  225. /* Enable watch dog for ES above ES 1.0 to avoid saturation */
  226. if (omap_rev() != OMAP4430_REV_ES1_0) {
  227. u32 ctrl = omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL);
  228. omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL,
  229. ctrl | MCPDM_WD_EN);
  230. }
  231. omap_mcpdm_open_streams(mcpdm);
  232. }
  233. mutex_unlock(&mcpdm->mutex);
  234. return 0;
  235. }
  236. static void omap_mcpdm_dai_shutdown(struct snd_pcm_substream *substream,
  237. struct snd_soc_dai *dai)
  238. {
  239. struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
  240. mutex_lock(&mcpdm->mutex);
  241. if (!dai->active) {
  242. if (omap_mcpdm_active(mcpdm)) {
  243. omap_mcpdm_stop(mcpdm);
  244. omap_mcpdm_close_streams(mcpdm);
  245. }
  246. }
  247. mutex_unlock(&mcpdm->mutex);
  248. }
  249. static int omap_mcpdm_dai_hw_params(struct snd_pcm_substream *substream,
  250. struct snd_pcm_hw_params *params,
  251. struct snd_soc_dai *dai)
  252. {
  253. struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
  254. int stream = substream->stream;
  255. struct omap_pcm_dma_data *dma_data;
  256. int channels;
  257. int link_mask = 0;
  258. channels = params_channels(params);
  259. switch (channels) {
  260. case 5:
  261. if (stream == SNDRV_PCM_STREAM_CAPTURE)
  262. /* up to 3 channels for capture */
  263. return -EINVAL;
  264. link_mask |= 1 << 4;
  265. case 4:
  266. if (stream == SNDRV_PCM_STREAM_CAPTURE)
  267. /* up to 3 channels for capture */
  268. return -EINVAL;
  269. link_mask |= 1 << 3;
  270. case 3:
  271. link_mask |= 1 << 2;
  272. case 2:
  273. link_mask |= 1 << 1;
  274. case 1:
  275. link_mask |= 1 << 0;
  276. break;
  277. default:
  278. /* unsupported number of channels */
  279. return -EINVAL;
  280. }
  281. dma_data = &omap_mcpdm_dai_dma_params[stream];
  282. /* Configure McPDM channels, and DMA packet size */
  283. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  284. mcpdm->dn_channels = link_mask << 3;
  285. dma_data->packet_size =
  286. (MCPDM_DN_THRES_MAX - mcpdm->dn_threshold) * channels;
  287. } else {
  288. mcpdm->up_channels = link_mask << 0;
  289. dma_data->packet_size = mcpdm->up_threshold * channels;
  290. }
  291. snd_soc_dai_set_dma_data(dai, substream, dma_data);
  292. return 0;
  293. }
  294. static int omap_mcpdm_prepare(struct snd_pcm_substream *substream,
  295. struct snd_soc_dai *dai)
  296. {
  297. struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
  298. if (!omap_mcpdm_active(mcpdm)) {
  299. omap_mcpdm_start(mcpdm);
  300. omap_mcpdm_reg_dump(mcpdm);
  301. }
  302. return 0;
  303. }
  304. static const struct snd_soc_dai_ops omap_mcpdm_dai_ops = {
  305. .startup = omap_mcpdm_dai_startup,
  306. .shutdown = omap_mcpdm_dai_shutdown,
  307. .hw_params = omap_mcpdm_dai_hw_params,
  308. .prepare = omap_mcpdm_prepare,
  309. };
  310. static int omap_mcpdm_probe(struct snd_soc_dai *dai)
  311. {
  312. struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
  313. int ret;
  314. pm_runtime_enable(mcpdm->dev);
  315. /* Disable lines while request is ongoing */
  316. pm_runtime_get_sync(mcpdm->dev);
  317. omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, 0x00);
  318. ret = request_irq(mcpdm->irq, omap_mcpdm_irq_handler,
  319. 0, "McPDM", (void *)mcpdm);
  320. pm_runtime_put_sync(mcpdm->dev);
  321. if (ret) {
  322. dev_err(mcpdm->dev, "Request for IRQ failed\n");
  323. pm_runtime_disable(mcpdm->dev);
  324. }
  325. /* Configure McPDM threshold values */
  326. mcpdm->dn_threshold = 2;
  327. mcpdm->up_threshold = MCPDM_UP_THRES_MAX - 3;
  328. return ret;
  329. }
  330. static int omap_mcpdm_remove(struct snd_soc_dai *dai)
  331. {
  332. struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
  333. free_irq(mcpdm->irq, (void *)mcpdm);
  334. pm_runtime_disable(mcpdm->dev);
  335. return 0;
  336. }
  337. #define OMAP_MCPDM_RATES (SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  338. #define OMAP_MCPDM_FORMATS SNDRV_PCM_FMTBIT_S32_LE
  339. static struct snd_soc_dai_driver omap_mcpdm_dai = {
  340. .probe = omap_mcpdm_probe,
  341. .remove = omap_mcpdm_remove,
  342. .probe_order = SND_SOC_COMP_ORDER_LATE,
  343. .remove_order = SND_SOC_COMP_ORDER_EARLY,
  344. .playback = {
  345. .channels_min = 1,
  346. .channels_max = 5,
  347. .rates = OMAP_MCPDM_RATES,
  348. .formats = OMAP_MCPDM_FORMATS,
  349. .sig_bits = 24,
  350. },
  351. .capture = {
  352. .channels_min = 1,
  353. .channels_max = 3,
  354. .rates = OMAP_MCPDM_RATES,
  355. .formats = OMAP_MCPDM_FORMATS,
  356. .sig_bits = 24,
  357. },
  358. .ops = &omap_mcpdm_dai_ops,
  359. };
  360. void omap_mcpdm_configure_dn_offsets(struct snd_soc_pcm_runtime *rtd,
  361. u8 rx1, u8 rx2)
  362. {
  363. struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  364. mcpdm->dn_rx_offset = MCPDM_DNOFST_RX1(rx1) | MCPDM_DNOFST_RX2(rx2);
  365. }
  366. EXPORT_SYMBOL_GPL(omap_mcpdm_configure_dn_offsets);
  367. static __devinit int asoc_mcpdm_probe(struct platform_device *pdev)
  368. {
  369. struct omap_mcpdm *mcpdm;
  370. struct resource *res;
  371. int ret = 0;
  372. mcpdm = kzalloc(sizeof(struct omap_mcpdm), GFP_KERNEL);
  373. if (!mcpdm)
  374. return -ENOMEM;
  375. platform_set_drvdata(pdev, mcpdm);
  376. mutex_init(&mcpdm->mutex);
  377. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  378. if (res == NULL) {
  379. dev_err(&pdev->dev, "no resource\n");
  380. goto err_res;
  381. }
  382. if (!request_mem_region(res->start, resource_size(res), "McPDM")) {
  383. ret = -EBUSY;
  384. goto err_res;
  385. }
  386. mcpdm->io_base = ioremap(res->start, resource_size(res));
  387. if (!mcpdm->io_base) {
  388. ret = -ENOMEM;
  389. goto err_iomap;
  390. }
  391. mcpdm->irq = platform_get_irq(pdev, 0);
  392. if (mcpdm->irq < 0) {
  393. ret = mcpdm->irq;
  394. goto err_irq;
  395. }
  396. mcpdm->dev = &pdev->dev;
  397. ret = snd_soc_register_dai(&pdev->dev, &omap_mcpdm_dai);
  398. if (!ret)
  399. return 0;
  400. err_irq:
  401. iounmap(mcpdm->io_base);
  402. err_iomap:
  403. release_mem_region(res->start, resource_size(res));
  404. err_res:
  405. kfree(mcpdm);
  406. return ret;
  407. }
  408. static int __devexit asoc_mcpdm_remove(struct platform_device *pdev)
  409. {
  410. struct omap_mcpdm *mcpdm = platform_get_drvdata(pdev);
  411. struct resource *res;
  412. snd_soc_unregister_dai(&pdev->dev);
  413. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  414. iounmap(mcpdm->io_base);
  415. release_mem_region(res->start, resource_size(res));
  416. kfree(mcpdm);
  417. return 0;
  418. }
  419. static const struct of_device_id omap_mcpdm_of_match[] = {
  420. { .compatible = "ti,omap4-mcpdm", },
  421. { }
  422. };
  423. MODULE_DEVICE_TABLE(of, omap_mcpdm_of_match);
  424. static struct platform_driver asoc_mcpdm_driver = {
  425. .driver = {
  426. .name = "omap-mcpdm",
  427. .owner = THIS_MODULE,
  428. .of_match_table = omap_mcpdm_of_match,
  429. },
  430. .probe = asoc_mcpdm_probe,
  431. .remove = __devexit_p(asoc_mcpdm_remove),
  432. };
  433. module_platform_driver(asoc_mcpdm_driver);
  434. MODULE_ALIAS("platform:omap-mcpdm");
  435. MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
  436. MODULE_DESCRIPTION("OMAP PDM SoC Interface");
  437. MODULE_LICENSE("GPL");