blob: 928f03707451ecde15f046574ed8f1b4e841ee9a [file] [log] [blame]
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -06001/*
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -06002 * mcpdm.c -- McPDM interface driver
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -06003 *
4 * Author: Jorge Eduardo Candelaria <x0107209@ti.com>
5 * Copyright (C) 2009 - Texas Instruments, Inc.
6 *
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.
10 *
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.
15 *
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
19 * 02110-1301 USA
20 *
21 */
22
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -060029#include <linux/interrupt.h>
30#include <linux/err.h>
31#include <linux/clk.h>
32#include <linux/delay.h>
33#include <linux/io.h>
34#include <linux/irq.h>
35
36#include "mcpdm.h"
37
38static struct omap_mcpdm *mcpdm;
39
40static inline void omap_mcpdm_write(u16 reg, u32 val)
41{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -060042 __raw_writel(val, mcpdm->io_base + reg);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -060043}
44
45static inline int omap_mcpdm_read(u16 reg)
46{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -060047 return __raw_readl(mcpdm->io_base + reg);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -060048}
49
50static void omap_mcpdm_reg_dump(void)
51{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -060052 dev_dbg(mcpdm->dev, "***********************\n");
53 dev_dbg(mcpdm->dev, "IRQSTATUS_RAW: 0x%04x\n",
54 omap_mcpdm_read(MCPDM_IRQSTATUS_RAW));
55 dev_dbg(mcpdm->dev, "IRQSTATUS: 0x%04x\n",
56 omap_mcpdm_read(MCPDM_IRQSTATUS));
57 dev_dbg(mcpdm->dev, "IRQENABLE_SET: 0x%04x\n",
58 omap_mcpdm_read(MCPDM_IRQENABLE_SET));
59 dev_dbg(mcpdm->dev, "IRQENABLE_CLR: 0x%04x\n",
60 omap_mcpdm_read(MCPDM_IRQENABLE_CLR));
61 dev_dbg(mcpdm->dev, "IRQWAKE_EN: 0x%04x\n",
62 omap_mcpdm_read(MCPDM_IRQWAKE_EN));
63 dev_dbg(mcpdm->dev, "DMAENABLE_SET: 0x%04x\n",
64 omap_mcpdm_read(MCPDM_DMAENABLE_SET));
65 dev_dbg(mcpdm->dev, "DMAENABLE_CLR: 0x%04x\n",
66 omap_mcpdm_read(MCPDM_DMAENABLE_CLR));
67 dev_dbg(mcpdm->dev, "DMAWAKEEN: 0x%04x\n",
68 omap_mcpdm_read(MCPDM_DMAWAKEEN));
69 dev_dbg(mcpdm->dev, "CTRL: 0x%04x\n",
70 omap_mcpdm_read(MCPDM_CTRL));
71 dev_dbg(mcpdm->dev, "DN_DATA: 0x%04x\n",
72 omap_mcpdm_read(MCPDM_DN_DATA));
73 dev_dbg(mcpdm->dev, "UP_DATA: 0x%04x\n",
74 omap_mcpdm_read(MCPDM_UP_DATA));
75 dev_dbg(mcpdm->dev, "FIFO_CTRL_DN: 0x%04x\n",
76 omap_mcpdm_read(MCPDM_FIFO_CTRL_DN));
77 dev_dbg(mcpdm->dev, "FIFO_CTRL_UP: 0x%04x\n",
78 omap_mcpdm_read(MCPDM_FIFO_CTRL_UP));
79 dev_dbg(mcpdm->dev, "DN_OFFSET: 0x%04x\n",
80 omap_mcpdm_read(MCPDM_DN_OFFSET));
81 dev_dbg(mcpdm->dev, "***********************\n");
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -060082}
83
84/*
85 * Takes the McPDM module in and out of reset state.
86 * Uplink and downlink can be reset individually.
87 */
88static void omap_mcpdm_reset_capture(int reset)
89{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -060090 int ctrl = omap_mcpdm_read(MCPDM_CTRL);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -060091
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -060092 if (reset)
93 ctrl |= SW_UP_RST;
94 else
95 ctrl &= ~SW_UP_RST;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -060096
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -060097 omap_mcpdm_write(MCPDM_CTRL, ctrl);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -060098}
99
100static void omap_mcpdm_reset_playback(int reset)
101{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600102 int ctrl = omap_mcpdm_read(MCPDM_CTRL);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600103
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600104 if (reset)
105 ctrl |= SW_DN_RST;
106 else
107 ctrl &= ~SW_DN_RST;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600108
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600109 omap_mcpdm_write(MCPDM_CTRL, ctrl);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600110}
111
112/*
113 * Enables the transfer through the PDM interface to/from the Phoenix
114 * codec by enabling the corresponding UP or DN channels.
115 */
116void omap_mcpdm_start(int stream)
117{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600118 int ctrl = omap_mcpdm_read(MCPDM_CTRL);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600119
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600120 if (stream)
121 ctrl |= mcpdm->up_channels;
122 else
123 ctrl |= mcpdm->dn_channels;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600124
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600125 omap_mcpdm_write(MCPDM_CTRL, ctrl);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600126}
127
128/*
129 * Disables the transfer through the PDM interface to/from the Phoenix
130 * codec by disabling the corresponding UP or DN channels.
131 */
132void omap_mcpdm_stop(int stream)
133{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600134 int ctrl = omap_mcpdm_read(MCPDM_CTRL);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600135
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600136 if (stream)
137 ctrl &= ~mcpdm->up_channels;
138 else
139 ctrl &= ~mcpdm->dn_channels;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600140
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600141 omap_mcpdm_write(MCPDM_CTRL, ctrl);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600142}
143
144/*
145 * Configures McPDM uplink for audio recording.
146 * This function should be called before omap_mcpdm_start.
147 */
148int omap_mcpdm_capture_open(struct omap_mcpdm_link *uplink)
149{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600150 int irq_mask = 0;
151 int ctrl;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600152
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600153 if (!uplink)
154 return -EINVAL;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600155
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600156 mcpdm->uplink = uplink;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600157
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600158 /* Enable irq request generation */
159 irq_mask |= uplink->irq_mask & MCPDM_UPLINK_IRQ_MASK;
160 omap_mcpdm_write(MCPDM_IRQENABLE_SET, irq_mask);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600161
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600162 /* Configure uplink threshold */
163 if (uplink->threshold > UP_THRES_MAX)
164 uplink->threshold = UP_THRES_MAX;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600165
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600166 omap_mcpdm_write(MCPDM_FIFO_CTRL_UP, uplink->threshold);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600167
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600168 /* Configure DMA controller */
169 omap_mcpdm_write(MCPDM_DMAENABLE_SET, DMA_UP_ENABLE);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600170
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600171 /* Set pdm out format */
172 ctrl = omap_mcpdm_read(MCPDM_CTRL);
173 ctrl &= ~PDMOUTFORMAT;
174 ctrl |= uplink->format & PDMOUTFORMAT;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600175
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600176 /* Uplink channels */
177 mcpdm->up_channels = uplink->channels & (PDM_UP_MASK | PDM_STATUS_MASK);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600178
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600179 omap_mcpdm_write(MCPDM_CTRL, ctrl);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600180
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600181 return 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600182}
183
184/*
185 * Configures McPDM downlink for audio playback.
186 * This function should be called before omap_mcpdm_start.
187 */
188int omap_mcpdm_playback_open(struct omap_mcpdm_link *downlink)
189{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600190 int irq_mask = 0;
191 int ctrl;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600192
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600193 if (!downlink)
194 return -EINVAL;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600195
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600196 mcpdm->downlink = downlink;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600197
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600198 /* Enable irq request generation */
199 irq_mask |= downlink->irq_mask & MCPDM_DOWNLINK_IRQ_MASK;
200 omap_mcpdm_write(MCPDM_IRQENABLE_SET, irq_mask);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600201
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600202 /* Configure uplink threshold */
203 if (downlink->threshold > DN_THRES_MAX)
204 downlink->threshold = DN_THRES_MAX;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600205
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600206 omap_mcpdm_write(MCPDM_FIFO_CTRL_DN, downlink->threshold);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600207
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600208 /* Enable DMA request generation */
209 omap_mcpdm_write(MCPDM_DMAENABLE_SET, DMA_DN_ENABLE);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600210
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600211 /* Set pdm out format */
212 ctrl = omap_mcpdm_read(MCPDM_CTRL);
213 ctrl &= ~PDMOUTFORMAT;
214 ctrl |= downlink->format & PDMOUTFORMAT;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600215
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600216 /* Downlink channels */
217 mcpdm->dn_channels = downlink->channels & (PDM_DN_MASK | PDM_CMD_MASK);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600218
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600219 omap_mcpdm_write(MCPDM_CTRL, ctrl);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600220
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600221 return 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600222}
223
224/*
225 * Cleans McPDM uplink configuration.
226 * This function should be called when the stream is closed.
227 */
228int omap_mcpdm_capture_close(struct omap_mcpdm_link *uplink)
229{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600230 int irq_mask = 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600231
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600232 if (!uplink)
233 return -EINVAL;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600234
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600235 /* Disable irq request generation */
236 irq_mask |= uplink->irq_mask & MCPDM_UPLINK_IRQ_MASK;
237 omap_mcpdm_write(MCPDM_IRQENABLE_CLR, irq_mask);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600238
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600239 /* Disable DMA request generation */
240 omap_mcpdm_write(MCPDM_DMAENABLE_CLR, DMA_UP_ENABLE);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600241
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600242 /* Clear Downlink channels */
243 mcpdm->up_channels = 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600244
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600245 mcpdm->uplink = NULL;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600246
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600247 return 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600248}
249
250/*
251 * Cleans McPDM downlink configuration.
252 * This function should be called when the stream is closed.
253 */
254int omap_mcpdm_playback_close(struct omap_mcpdm_link *downlink)
255{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600256 int irq_mask = 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600257
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600258 if (!downlink)
259 return -EINVAL;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600260
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600261 /* Disable irq request generation */
262 irq_mask |= downlink->irq_mask & MCPDM_DOWNLINK_IRQ_MASK;
263 omap_mcpdm_write(MCPDM_IRQENABLE_CLR, irq_mask);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600264
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600265 /* Disable DMA request generation */
266 omap_mcpdm_write(MCPDM_DMAENABLE_CLR, DMA_DN_ENABLE);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600267
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600268 /* clear Downlink channels */
269 mcpdm->dn_channels = 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600270
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600271 mcpdm->downlink = NULL;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600272
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600273 return 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600274}
275
276static irqreturn_t omap_mcpdm_irq_handler(int irq, void *dev_id)
277{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600278 struct omap_mcpdm *mcpdm_irq = dev_id;
279 int irq_status;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600280
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600281 irq_status = omap_mcpdm_read(MCPDM_IRQSTATUS);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600282
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600283 /* Acknowledge irq event */
284 omap_mcpdm_write(MCPDM_IRQSTATUS, irq_status);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600285
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600286 if (irq & MCPDM_DN_IRQ_FULL) {
287 dev_err(mcpdm_irq->dev, "DN FIFO error %x\n", irq_status);
288 omap_mcpdm_reset_playback(1);
289 omap_mcpdm_playback_open(mcpdm_irq->downlink);
290 omap_mcpdm_reset_playback(0);
291 }
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600292
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600293 if (irq & MCPDM_DN_IRQ_EMPTY) {
294 dev_err(mcpdm_irq->dev, "DN FIFO error %x\n", irq_status);
295 omap_mcpdm_reset_playback(1);
296 omap_mcpdm_playback_open(mcpdm_irq->downlink);
297 omap_mcpdm_reset_playback(0);
298 }
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600299
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600300 if (irq & MCPDM_DN_IRQ) {
301 dev_dbg(mcpdm_irq->dev, "DN write request\n");
302 }
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600303
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600304 if (irq & MCPDM_UP_IRQ_FULL) {
305 dev_err(mcpdm_irq->dev, "UP FIFO error %x\n", irq_status);
306 omap_mcpdm_reset_capture(1);
307 omap_mcpdm_capture_open(mcpdm_irq->uplink);
308 omap_mcpdm_reset_capture(0);
309 }
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600310
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600311 if (irq & MCPDM_UP_IRQ_EMPTY) {
312 dev_err(mcpdm_irq->dev, "UP FIFO error %x\n", irq_status);
313 omap_mcpdm_reset_capture(1);
314 omap_mcpdm_capture_open(mcpdm_irq->uplink);
315 omap_mcpdm_reset_capture(0);
316 }
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600317
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600318 if (irq & MCPDM_UP_IRQ) {
319 dev_dbg(mcpdm_irq->dev, "UP write request\n");
320 }
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600321
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600322 return IRQ_HANDLED;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600323}
324
325int omap_mcpdm_request(void)
326{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600327 int ret;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600328
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600329 clk_enable(mcpdm->clk);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600330
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600331 spin_lock(&mcpdm->lock);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600332
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600333 if (!mcpdm->free) {
334 dev_err(mcpdm->dev, "McPDM interface is in use\n");
335 spin_unlock(&mcpdm->lock);
336 ret = -EBUSY;
337 goto err;
338 }
339 mcpdm->free = 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600340
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600341 spin_unlock(&mcpdm->lock);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600342
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600343 /* Disable lines while request is ongoing */
344 omap_mcpdm_write(MCPDM_CTRL, 0x00);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600345
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600346 ret = request_irq(mcpdm->irq, omap_mcpdm_irq_handler,
347 0, "McPDM", (void *)mcpdm);
348 if (ret) {
349 dev_err(mcpdm->dev, "Request for McPDM IRQ failed\n");
350 goto err;
351 }
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600352
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600353 return 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600354
355err:
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600356 clk_disable(mcpdm->clk);
357 return ret;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600358}
359
360void omap_mcpdm_free(void)
361{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600362 spin_lock(&mcpdm->lock);
363 if (mcpdm->free) {
364 dev_err(mcpdm->dev, "McPDM interface is already free\n");
365 spin_unlock(&mcpdm->lock);
366 return;
367 }
368 mcpdm->free = 1;
369 spin_unlock(&mcpdm->lock);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600370
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600371 clk_disable(mcpdm->clk);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600372
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600373 free_irq(mcpdm->irq, (void *)mcpdm);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600374}
375
376/* Enable/disable DC offset cancelation for the analog
377 * headset path (PDM channels 1 and 2).
378 */
379int omap_mcpdm_set_offset(int offset1, int offset2)
380{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600381 int offset;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600382
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600383 if ((offset1 > DN_OFST_MAX) || (offset2 > DN_OFST_MAX))
384 return -EINVAL;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600385
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600386 offset = (offset1 << DN_OFST_RX1) | (offset2 << DN_OFST_RX2);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600387
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600388 /* offset cancellation for channel 1 */
389 if (offset1)
390 offset |= DN_OFST_RX1_EN;
391 else
392 offset &= ~DN_OFST_RX1_EN;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600393
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600394 /* offset cancellation for channel 2 */
395 if (offset2)
396 offset |= DN_OFST_RX2_EN;
397 else
398 offset &= ~DN_OFST_RX2_EN;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600399
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600400 omap_mcpdm_write(MCPDM_DN_OFFSET, offset);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600401
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600402 return 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600403}
404
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000405int __devinit omap_mcpdm_probe(struct platform_device *pdev)
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600406{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600407 struct resource *res;
408 int ret = 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600409
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600410 mcpdm = kzalloc(sizeof(struct omap_mcpdm), GFP_KERNEL);
411 if (!mcpdm) {
412 ret = -ENOMEM;
413 goto exit;
414 }
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600415
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600416 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
417 if (res == NULL) {
418 dev_err(&pdev->dev, "no resource\n");
419 goto err_resource;
420 }
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600421
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600422 spin_lock_init(&mcpdm->lock);
423 mcpdm->free = 1;
424 mcpdm->io_base = ioremap(res->start, resource_size(res));
425 if (!mcpdm->io_base) {
426 ret = -ENOMEM;
427 goto err_resource;
428 }
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600429
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600430 mcpdm->irq = platform_get_irq(pdev, 0);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600431
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600432 mcpdm->clk = clk_get(&pdev->dev, "pdm_ck");
433 if (IS_ERR(mcpdm->clk)) {
434 ret = PTR_ERR(mcpdm->clk);
435 dev_err(&pdev->dev, "unable to get pdm_ck: %d\n", ret);
436 goto err_clk;
437 }
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600438
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600439 mcpdm->dev = &pdev->dev;
440 platform_set_drvdata(pdev, mcpdm);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600441
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600442 return 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600443
444err_clk:
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600445 iounmap(mcpdm->io_base);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600446err_resource:
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600447 kfree(mcpdm);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600448exit:
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600449 return ret;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600450}
451
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000452int __devexit omap_mcpdm_remove(struct platform_device *pdev)
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600453{
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600454 struct omap_mcpdm *mcpdm_ptr = platform_get_drvdata(pdev);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600455
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600456 platform_set_drvdata(pdev, NULL);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600457
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600458 clk_put(mcpdm_ptr->clk);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600459
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600460 iounmap(mcpdm_ptr->io_base);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600461
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600462 mcpdm_ptr->clk = NULL;
463 mcpdm_ptr->free = 0;
464 mcpdm_ptr->dev = NULL;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600465
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600466 kfree(mcpdm_ptr);
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600467
Jorge Eduardo Candelaria9fc71e82010-03-18 19:45:41 -0600468 return 0;
Candelaria Villareal, Jorgeb3b0b452010-02-22 17:17:21 -0600469}
470