omap-mcpdm.c 14 KB

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