2 * mcpdm.c -- McPDM interface driver
4 * Author: Jorge Eduardo Candelaria <x0107209@ti.com>
5 * Copyright (C) 2009 - Texas Instruments, Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/device.h>
26 #include <linux/platform_device.h>
27 #include <linux/wait.h>
28 #include <linux/interrupt.h>
29 #include <linux/err.h>
30 #include <linux/clk.h>
31 #include <linux/delay.h>
33 #include <linux/irq.h>
37 static struct omap_mcpdm *mcpdm;
39 static inline void omap_mcpdm_write(u16 reg, u32 val)
41 __raw_writel(val, mcpdm->io_base + reg);
44 static inline int omap_mcpdm_read(u16 reg)
46 return __raw_readl(mcpdm->io_base + reg);
49 static void omap_mcpdm_reg_dump(void)
51 dev_dbg(mcpdm->dev, "***********************\n");
52 dev_dbg(mcpdm->dev, "IRQSTATUS_RAW: 0x%04x\n",
53 omap_mcpdm_read(MCPDM_IRQSTATUS_RAW));
54 dev_dbg(mcpdm->dev, "IRQSTATUS: 0x%04x\n",
55 omap_mcpdm_read(MCPDM_IRQSTATUS));
56 dev_dbg(mcpdm->dev, "IRQENABLE_SET: 0x%04x\n",
57 omap_mcpdm_read(MCPDM_IRQENABLE_SET));
58 dev_dbg(mcpdm->dev, "IRQENABLE_CLR: 0x%04x\n",
59 omap_mcpdm_read(MCPDM_IRQENABLE_CLR));
60 dev_dbg(mcpdm->dev, "IRQWAKE_EN: 0x%04x\n",
61 omap_mcpdm_read(MCPDM_IRQWAKE_EN));
62 dev_dbg(mcpdm->dev, "DMAENABLE_SET: 0x%04x\n",
63 omap_mcpdm_read(MCPDM_DMAENABLE_SET));
64 dev_dbg(mcpdm->dev, "DMAENABLE_CLR: 0x%04x\n",
65 omap_mcpdm_read(MCPDM_DMAENABLE_CLR));
66 dev_dbg(mcpdm->dev, "DMAWAKEEN: 0x%04x\n",
67 omap_mcpdm_read(MCPDM_DMAWAKEEN));
68 dev_dbg(mcpdm->dev, "CTRL: 0x%04x\n",
69 omap_mcpdm_read(MCPDM_CTRL));
70 dev_dbg(mcpdm->dev, "DN_DATA: 0x%04x\n",
71 omap_mcpdm_read(MCPDM_DN_DATA));
72 dev_dbg(mcpdm->dev, "UP_DATA: 0x%04x\n",
73 omap_mcpdm_read(MCPDM_UP_DATA));
74 dev_dbg(mcpdm->dev, "FIFO_CTRL_DN: 0x%04x\n",
75 omap_mcpdm_read(MCPDM_FIFO_CTRL_DN));
76 dev_dbg(mcpdm->dev, "FIFO_CTRL_UP: 0x%04x\n",
77 omap_mcpdm_read(MCPDM_FIFO_CTRL_UP));
78 dev_dbg(mcpdm->dev, "DN_OFFSET: 0x%04x\n",
79 omap_mcpdm_read(MCPDM_DN_OFFSET));
80 dev_dbg(mcpdm->dev, "***********************\n");
84 * Takes the McPDM module in and out of reset state.
85 * Uplink and downlink can be reset individually.
87 static void omap_mcpdm_reset_capture(int reset)
89 int ctrl = omap_mcpdm_read(MCPDM_CTRL);
96 omap_mcpdm_write(MCPDM_CTRL, ctrl);
99 static void omap_mcpdm_reset_playback(int reset)
101 int ctrl = omap_mcpdm_read(MCPDM_CTRL);
108 omap_mcpdm_write(MCPDM_CTRL, ctrl);
112 * Enables the transfer through the PDM interface to/from the Phoenix
113 * codec by enabling the corresponding UP or DN channels.
115 void omap_mcpdm_start(int stream)
117 int ctrl = omap_mcpdm_read(MCPDM_CTRL);
120 ctrl |= mcpdm->up_channels;
122 ctrl |= mcpdm->dn_channels;
124 omap_mcpdm_write(MCPDM_CTRL, ctrl);
128 * Disables the transfer through the PDM interface to/from the Phoenix
129 * codec by disabling the corresponding UP or DN channels.
131 void omap_mcpdm_stop(int stream)
133 int ctrl = omap_mcpdm_read(MCPDM_CTRL);
136 ctrl &= ~mcpdm->up_channels;
138 ctrl &= ~mcpdm->dn_channels;
140 omap_mcpdm_write(MCPDM_CTRL, ctrl);
144 * Configures McPDM uplink for audio recording.
145 * This function should be called before omap_mcpdm_start.
147 int omap_mcpdm_capture_open(struct omap_mcpdm_link *uplink)
155 mcpdm->uplink = uplink;
157 /* Enable irq request generation */
158 irq_mask |= uplink->irq_mask & MCPDM_UPLINK_IRQ_MASK;
159 omap_mcpdm_write(MCPDM_IRQENABLE_SET, irq_mask);
161 /* Configure uplink threshold */
162 if (uplink->threshold > UP_THRES_MAX)
163 uplink->threshold = UP_THRES_MAX;
165 omap_mcpdm_write(MCPDM_FIFO_CTRL_UP, uplink->threshold);
167 /* Configure DMA controller */
168 omap_mcpdm_write(MCPDM_DMAENABLE_SET, DMA_UP_ENABLE);
170 /* Set pdm out format */
171 ctrl = omap_mcpdm_read(MCPDM_CTRL);
172 ctrl &= ~PDMOUTFORMAT;
173 ctrl |= uplink->format & PDMOUTFORMAT;
175 /* Uplink channels */
176 mcpdm->up_channels = uplink->channels & (PDM_UP_MASK | PDM_STATUS_MASK);
178 omap_mcpdm_write(MCPDM_CTRL, ctrl);
184 * Configures McPDM downlink for audio playback.
185 * This function should be called before omap_mcpdm_start.
187 int omap_mcpdm_playback_open(struct omap_mcpdm_link *downlink)
195 mcpdm->downlink = downlink;
197 /* Enable irq request generation */
198 irq_mask |= downlink->irq_mask & MCPDM_DOWNLINK_IRQ_MASK;
199 omap_mcpdm_write(MCPDM_IRQENABLE_SET, irq_mask);
201 /* Configure uplink threshold */
202 if (downlink->threshold > DN_THRES_MAX)
203 downlink->threshold = DN_THRES_MAX;
205 omap_mcpdm_write(MCPDM_FIFO_CTRL_DN, downlink->threshold);
207 /* Enable DMA request generation */
208 omap_mcpdm_write(MCPDM_DMAENABLE_SET, DMA_DN_ENABLE);
210 /* Set pdm out format */
211 ctrl = omap_mcpdm_read(MCPDM_CTRL);
212 ctrl &= ~PDMOUTFORMAT;
213 ctrl |= downlink->format & PDMOUTFORMAT;
215 /* Downlink channels */
216 mcpdm->dn_channels = downlink->channels & (PDM_DN_MASK | PDM_CMD_MASK);
218 omap_mcpdm_write(MCPDM_CTRL, ctrl);
224 * Cleans McPDM uplink configuration.
225 * This function should be called when the stream is closed.
227 int omap_mcpdm_capture_close(struct omap_mcpdm_link *uplink)
234 /* Disable irq request generation */
235 irq_mask |= uplink->irq_mask & MCPDM_UPLINK_IRQ_MASK;
236 omap_mcpdm_write(MCPDM_IRQENABLE_CLR, irq_mask);
238 /* Disable DMA request generation */
239 omap_mcpdm_write(MCPDM_DMAENABLE_CLR, DMA_UP_ENABLE);
241 /* Clear Downlink channels */
242 mcpdm->up_channels = 0;
244 mcpdm->uplink = NULL;
250 * Cleans McPDM downlink configuration.
251 * This function should be called when the stream is closed.
253 int omap_mcpdm_playback_close(struct omap_mcpdm_link *downlink)
260 /* Disable irq request generation */
261 irq_mask |= downlink->irq_mask & MCPDM_DOWNLINK_IRQ_MASK;
262 omap_mcpdm_write(MCPDM_IRQENABLE_CLR, irq_mask);
264 /* Disable DMA request generation */
265 omap_mcpdm_write(MCPDM_DMAENABLE_CLR, DMA_DN_ENABLE);
267 /* clear Downlink channels */
268 mcpdm->dn_channels = 0;
270 mcpdm->downlink = NULL;
275 static irqreturn_t omap_mcpdm_irq_handler(int irq, void *dev_id)
277 struct omap_mcpdm *mcpdm_irq = dev_id;
280 irq_status = omap_mcpdm_read(MCPDM_IRQSTATUS);
282 /* Acknowledge irq event */
283 omap_mcpdm_write(MCPDM_IRQSTATUS, irq_status);
285 if (irq & MCPDM_DN_IRQ_FULL) {
286 dev_err(mcpdm_irq->dev, "DN FIFO error %x\n", irq_status);
287 omap_mcpdm_reset_playback(1);
288 omap_mcpdm_playback_open(mcpdm_irq->downlink);
289 omap_mcpdm_reset_playback(0);
292 if (irq & MCPDM_DN_IRQ_EMPTY) {
293 dev_err(mcpdm_irq->dev, "DN FIFO error %x\n", irq_status);
294 omap_mcpdm_reset_playback(1);
295 omap_mcpdm_playback_open(mcpdm_irq->downlink);
296 omap_mcpdm_reset_playback(0);
299 if (irq & MCPDM_DN_IRQ) {
300 dev_dbg(mcpdm_irq->dev, "DN write request\n");
303 if (irq & MCPDM_UP_IRQ_FULL) {
304 dev_err(mcpdm_irq->dev, "UP FIFO error %x\n", irq_status);
305 omap_mcpdm_reset_capture(1);
306 omap_mcpdm_capture_open(mcpdm_irq->uplink);
307 omap_mcpdm_reset_capture(0);
310 if (irq & MCPDM_UP_IRQ_EMPTY) {
311 dev_err(mcpdm_irq->dev, "UP FIFO error %x\n", irq_status);
312 omap_mcpdm_reset_capture(1);
313 omap_mcpdm_capture_open(mcpdm_irq->uplink);
314 omap_mcpdm_reset_capture(0);
317 if (irq & MCPDM_UP_IRQ) {
318 dev_dbg(mcpdm_irq->dev, "UP write request\n");
324 int omap_mcpdm_request(void)
328 clk_enable(mcpdm->clk);
330 spin_lock(&mcpdm->lock);
333 dev_err(mcpdm->dev, "McPDM interface is in use\n");
334 spin_unlock(&mcpdm->lock);
340 spin_unlock(&mcpdm->lock);
342 /* Disable lines while request is ongoing */
343 omap_mcpdm_write(MCPDM_CTRL, 0x00);
345 ret = request_irq(mcpdm->irq, omap_mcpdm_irq_handler,
346 0, "McPDM", (void *)mcpdm);
348 dev_err(mcpdm->dev, "Request for McPDM IRQ failed\n");
355 clk_disable(mcpdm->clk);
359 void omap_mcpdm_free(void)
361 spin_lock(&mcpdm->lock);
363 dev_err(mcpdm->dev, "McPDM interface is already free\n");
364 spin_unlock(&mcpdm->lock);
368 spin_unlock(&mcpdm->lock);
370 clk_disable(mcpdm->clk);
372 free_irq(mcpdm->irq, (void *)mcpdm);
375 /* Enable/disable DC offset cancelation for the analog
376 * headset path (PDM channels 1 and 2).
378 int omap_mcpdm_set_offset(int offset1, int offset2)
382 if ((offset1 > DN_OFST_MAX) || (offset2 > DN_OFST_MAX))
385 offset = (offset1 << DN_OFST_RX1) | (offset2 << DN_OFST_RX2);
387 /* offset cancellation for channel 1 */
389 offset |= DN_OFST_RX1_EN;
391 offset &= ~DN_OFST_RX1_EN;
393 /* offset cancellation for channel 2 */
395 offset |= DN_OFST_RX2_EN;
397 offset &= ~DN_OFST_RX2_EN;
399 omap_mcpdm_write(MCPDM_DN_OFFSET, offset);
404 static int __devinit omap_mcpdm_probe(struct platform_device *pdev)
406 struct resource *res;
409 mcpdm = kzalloc(sizeof(struct omap_mcpdm), GFP_KERNEL);
415 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
417 dev_err(&pdev->dev, "no resource\n");
421 spin_lock_init(&mcpdm->lock);
423 mcpdm->io_base = ioremap(res->start, resource_size(res));
424 if (!mcpdm->io_base) {
429 mcpdm->irq = platform_get_irq(pdev, 0);
431 mcpdm->clk = clk_get(&pdev->dev, "pdm_ck");
432 if (IS_ERR(mcpdm->clk)) {
433 ret = PTR_ERR(mcpdm->clk);
434 dev_err(&pdev->dev, "unable to get pdm_ck: %d\n", ret);
438 mcpdm->dev = &pdev->dev;
439 platform_set_drvdata(pdev, mcpdm);
444 iounmap(mcpdm->io_base);
451 static int __devexit omap_mcpdm_remove(struct platform_device *pdev)
453 struct omap_mcpdm *mcpdm_ptr = platform_get_drvdata(pdev);
455 platform_set_drvdata(pdev, NULL);
457 clk_put(mcpdm_ptr->clk);
459 iounmap(mcpdm_ptr->io_base);
461 mcpdm_ptr->clk = NULL;
463 mcpdm_ptr->dev = NULL;
470 static struct platform_driver omap_mcpdm_driver = {
471 .probe = omap_mcpdm_probe,
472 .remove = __devexit_p(omap_mcpdm_remove),
474 .name = "omap-mcpdm",
478 static struct platform_device *omap_mcpdm_device;
480 static int __init omap_mcpdm_init(void)
482 return platform_driver_register(&omap_mcpdm_driver);
484 arch_initcall(omap_mcpdm_init);