Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * OMAP mailbox driver |
| 3 | * |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 4 | * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved. |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 5 | * |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 6 | * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 20 | * 02110-1301 USA |
| 21 | * |
| 22 | */ |
| 23 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 24 | #include <linux/module.h> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/device.h> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 27 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Hiroshi DOYU | 8dff0fa | 2009-03-23 18:07:32 -0700 | [diff] [blame] | 29 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 30 | #include <plat/mailbox.h> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 31 | |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 32 | static struct workqueue_struct *mboxd; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 33 | static struct omap_mbox *mboxes; |
| 34 | static DEFINE_RWLOCK(mboxes_lock); |
| 35 | |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 36 | static int mbox_configured; |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame^] | 37 | static DEFINE_MUTEX(mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 38 | |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 39 | /* Mailbox FIFO handle functions */ |
| 40 | static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox) |
| 41 | { |
| 42 | return mbox->ops->fifo_read(mbox); |
| 43 | } |
| 44 | static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg) |
| 45 | { |
| 46 | mbox->ops->fifo_write(mbox, msg); |
| 47 | } |
| 48 | static inline int mbox_fifo_empty(struct omap_mbox *mbox) |
| 49 | { |
| 50 | return mbox->ops->fifo_empty(mbox); |
| 51 | } |
| 52 | static inline int mbox_fifo_full(struct omap_mbox *mbox) |
| 53 | { |
| 54 | return mbox->ops->fifo_full(mbox); |
| 55 | } |
| 56 | |
| 57 | /* Mailbox IRQ handle functions */ |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 58 | static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 59 | { |
| 60 | if (mbox->ops->ack_irq) |
| 61 | mbox->ops->ack_irq(mbox, irq); |
| 62 | } |
| 63 | static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 64 | { |
| 65 | return mbox->ops->is_irq(mbox, irq); |
| 66 | } |
| 67 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 68 | /* |
| 69 | * message sender |
| 70 | */ |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 71 | static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 72 | { |
| 73 | int ret = 0, i = 1000; |
| 74 | |
| 75 | while (mbox_fifo_full(mbox)) { |
| 76 | if (mbox->ops->type == OMAP_MBOX_TYPE2) |
| 77 | return -1; |
| 78 | if (--i == 0) |
| 79 | return -1; |
| 80 | udelay(1); |
| 81 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 82 | mbox_fifo_write(mbox, msg); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 83 | return ret; |
| 84 | } |
| 85 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 86 | |
C A Subramaniam | b2b6362 | 2009-11-22 10:11:20 -0800 | [diff] [blame] | 87 | int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 88 | { |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 89 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 90 | struct request *rq; |
| 91 | struct request_queue *q = mbox->txq->queue; |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 92 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 93 | rq = blk_get_request(q, WRITE, GFP_ATOMIC); |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 94 | if (unlikely(!rq)) |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 95 | return -ENOMEM; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 96 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 97 | blk_insert_request(q, rq, 0, (void *) msg); |
| 98 | tasklet_schedule(&mbox->txq->tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 99 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 100 | return 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 101 | } |
| 102 | EXPORT_SYMBOL(omap_mbox_msg_send); |
| 103 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 104 | static void mbox_tx_tasklet(unsigned long tx_data) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 105 | { |
| 106 | int ret; |
| 107 | struct request *rq; |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 108 | struct omap_mbox *mbox = (struct omap_mbox *)tx_data; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 109 | struct request_queue *q = mbox->txq->queue; |
| 110 | |
| 111 | while (1) { |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 112 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 113 | rq = blk_fetch_request(q); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 114 | |
| 115 | if (!rq) |
| 116 | break; |
| 117 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 118 | ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 119 | if (ret) { |
Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 120 | omap_mbox_enable_irq(mbox, IRQ_TX); |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 121 | blk_requeue_request(q, rq); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 122 | return; |
| 123 | } |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 124 | blk_end_request_all(rq, 0); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Message receiver(workqueue) |
| 130 | */ |
| 131 | static void mbox_rx_work(struct work_struct *work) |
| 132 | { |
| 133 | struct omap_mbox_queue *mq = |
| 134 | container_of(work, struct omap_mbox_queue, work); |
| 135 | struct omap_mbox *mbox = mq->queue->queuedata; |
| 136 | struct request_queue *q = mbox->rxq->queue; |
| 137 | struct request *rq; |
| 138 | mbox_msg_t msg; |
| 139 | unsigned long flags; |
| 140 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 141 | while (1) { |
| 142 | spin_lock_irqsave(q->queue_lock, flags); |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 143 | rq = blk_fetch_request(q); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 144 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 145 | if (!rq) |
| 146 | break; |
| 147 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 148 | msg = (mbox_msg_t)rq->special; |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 149 | blk_end_request_all(rq, 0); |
Fernando Guzman Lugo | 9caae4d | 2010-01-27 20:04:02 -0600 | [diff] [blame] | 150 | if (mbox->rxq->callback) |
| 151 | mbox->rxq->callback((void *)msg); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Mailbox interrupt handler |
| 157 | */ |
Hiroshi DOYU | bfe1f6a | 2009-11-22 10:11:18 -0800 | [diff] [blame] | 158 | static void mbox_txq_fn(struct request_queue *q) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 159 | { |
| 160 | } |
| 161 | |
Hiroshi DOYU | bfe1f6a | 2009-11-22 10:11:18 -0800 | [diff] [blame] | 162 | static void mbox_rxq_fn(struct request_queue *q) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 163 | { |
| 164 | } |
| 165 | |
| 166 | static void __mbox_tx_interrupt(struct omap_mbox *mbox) |
| 167 | { |
Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 168 | omap_mbox_disable_irq(mbox, IRQ_TX); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 169 | ack_mbox_irq(mbox, IRQ_TX); |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 170 | tasklet_schedule(&mbox->txq->tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static void __mbox_rx_interrupt(struct omap_mbox *mbox) |
| 174 | { |
| 175 | struct request *rq; |
| 176 | mbox_msg_t msg; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 177 | struct request_queue *q = mbox->rxq->queue; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 178 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 179 | while (!mbox_fifo_empty(mbox)) { |
| 180 | rq = blk_get_request(q, WRITE, GFP_ATOMIC); |
| 181 | if (unlikely(!rq)) |
| 182 | goto nomem; |
| 183 | |
| 184 | msg = mbox_fifo_read(mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 185 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 186 | |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 187 | blk_insert_request(q, rq, 0, (void *)msg); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 188 | if (mbox->ops->type == OMAP_MBOX_TYPE1) |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | /* no more messages in the fifo. clear IRQ source. */ |
| 193 | ack_mbox_irq(mbox, IRQ_RX); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 194 | nomem: |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 195 | queue_work(mboxd, &mbox->rxq->work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static irqreturn_t mbox_interrupt(int irq, void *p) |
| 199 | { |
Jeff Garzik | 2a7057e | 2007-10-26 05:40:22 -0400 | [diff] [blame] | 200 | struct omap_mbox *mbox = p; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 201 | |
| 202 | if (is_mbox_irq(mbox, IRQ_TX)) |
| 203 | __mbox_tx_interrupt(mbox); |
| 204 | |
| 205 | if (is_mbox_irq(mbox, IRQ_RX)) |
| 206 | __mbox_rx_interrupt(mbox); |
| 207 | |
| 208 | return IRQ_HANDLED; |
| 209 | } |
| 210 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 211 | static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox, |
Hiroshi DOYU | bfe1f6a | 2009-11-22 10:11:18 -0800 | [diff] [blame] | 212 | request_fn_proc *proc, |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 213 | void (*work) (struct work_struct *), |
| 214 | void (*tasklet)(unsigned long)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 215 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 216 | struct request_queue *q; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 217 | struct omap_mbox_queue *mq; |
| 218 | |
| 219 | mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL); |
| 220 | if (!mq) |
| 221 | return NULL; |
| 222 | |
| 223 | spin_lock_init(&mq->lock); |
| 224 | |
| 225 | q = blk_init_queue(proc, &mq->lock); |
| 226 | if (!q) |
| 227 | goto error; |
| 228 | q->queuedata = mbox; |
| 229 | mq->queue = q; |
| 230 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 231 | if (work) |
| 232 | INIT_WORK(&mq->work, work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 233 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 234 | if (tasklet) |
| 235 | tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 236 | return mq; |
| 237 | error: |
| 238 | kfree(mq); |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | static void mbox_queue_free(struct omap_mbox_queue *q) |
| 243 | { |
| 244 | blk_cleanup_queue(q->queue); |
| 245 | kfree(q); |
| 246 | } |
| 247 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 248 | static int omap_mbox_startup(struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 249 | { |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 250 | int ret = 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 251 | struct omap_mbox_queue *mq; |
| 252 | |
| 253 | if (likely(mbox->ops->startup)) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame^] | 254 | mutex_lock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 255 | if (!mbox_configured) |
| 256 | ret = mbox->ops->startup(mbox); |
| 257 | |
| 258 | if (unlikely(ret)) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame^] | 259 | mutex_unlock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 260 | return ret; |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 261 | } |
| 262 | mbox_configured++; |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame^] | 263 | mutex_unlock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 264 | } |
| 265 | |
C A Subramaniam | 5e68382 | 2009-11-22 10:11:23 -0800 | [diff] [blame] | 266 | ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED, |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 267 | mbox->name, mbox); |
| 268 | if (unlikely(ret)) { |
| 269 | printk(KERN_ERR |
| 270 | "failed to register mailbox interrupt:%d\n", ret); |
| 271 | goto fail_request_irq; |
| 272 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 273 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 274 | mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 275 | if (!mq) { |
| 276 | ret = -ENOMEM; |
| 277 | goto fail_alloc_txq; |
| 278 | } |
| 279 | mbox->txq = mq; |
| 280 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 281 | mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 282 | if (!mq) { |
| 283 | ret = -ENOMEM; |
| 284 | goto fail_alloc_rxq; |
| 285 | } |
| 286 | mbox->rxq = mq; |
| 287 | |
| 288 | return 0; |
| 289 | |
| 290 | fail_alloc_rxq: |
| 291 | mbox_queue_free(mbox->txq); |
| 292 | fail_alloc_txq: |
| 293 | free_irq(mbox->irq, mbox); |
| 294 | fail_request_irq: |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 295 | if (unlikely(mbox->ops->shutdown)) |
| 296 | mbox->ops->shutdown(mbox); |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | static void omap_mbox_fini(struct omap_mbox *mbox) |
| 302 | { |
Fernando Guzman Lugo | ad6d962 | 2010-02-12 19:02:32 -0600 | [diff] [blame] | 303 | free_irq(mbox->irq, mbox); |
Fernando Guzman Lugo | 0e828e8 | 2010-02-12 19:07:14 -0600 | [diff] [blame] | 304 | tasklet_kill(&mbox->txq->tasklet); |
| 305 | flush_work(&mbox->rxq->work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 306 | mbox_queue_free(mbox->txq); |
| 307 | mbox_queue_free(mbox->rxq); |
| 308 | |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 309 | if (unlikely(mbox->ops->shutdown)) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame^] | 310 | mutex_lock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 311 | if (mbox_configured > 0) |
| 312 | mbox_configured--; |
| 313 | if (!mbox_configured) |
| 314 | mbox->ops->shutdown(mbox); |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame^] | 315 | mutex_unlock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 316 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static struct omap_mbox **find_mboxes(const char *name) |
| 320 | { |
| 321 | struct omap_mbox **p; |
| 322 | |
| 323 | for (p = &mboxes; *p; p = &(*p)->next) { |
| 324 | if (strcmp((*p)->name, name) == 0) |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | return p; |
| 329 | } |
| 330 | |
| 331 | struct omap_mbox *omap_mbox_get(const char *name) |
| 332 | { |
| 333 | struct omap_mbox *mbox; |
| 334 | int ret; |
| 335 | |
| 336 | read_lock(&mboxes_lock); |
| 337 | mbox = *(find_mboxes(name)); |
| 338 | if (mbox == NULL) { |
| 339 | read_unlock(&mboxes_lock); |
| 340 | return ERR_PTR(-ENOENT); |
| 341 | } |
| 342 | |
| 343 | read_unlock(&mboxes_lock); |
| 344 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 345 | ret = omap_mbox_startup(mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 346 | if (ret) |
| 347 | return ERR_PTR(-ENODEV); |
| 348 | |
| 349 | return mbox; |
| 350 | } |
| 351 | EXPORT_SYMBOL(omap_mbox_get); |
| 352 | |
| 353 | void omap_mbox_put(struct omap_mbox *mbox) |
| 354 | { |
| 355 | omap_mbox_fini(mbox); |
| 356 | } |
| 357 | EXPORT_SYMBOL(omap_mbox_put); |
| 358 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 359 | int omap_mbox_register(struct device *parent, struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 360 | { |
| 361 | int ret = 0; |
| 362 | struct omap_mbox **tmp; |
| 363 | |
| 364 | if (!mbox) |
| 365 | return -EINVAL; |
| 366 | if (mbox->next) |
| 367 | return -EBUSY; |
| 368 | |
| 369 | write_lock(&mboxes_lock); |
| 370 | tmp = find_mboxes(mbox->name); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 371 | if (*tmp) { |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 372 | ret = -EBUSY; |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 373 | write_unlock(&mboxes_lock); |
| 374 | goto err_find; |
| 375 | } |
| 376 | *tmp = mbox; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 377 | write_unlock(&mboxes_lock); |
| 378 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 379 | return 0; |
| 380 | |
| 381 | err_find: |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 382 | return ret; |
| 383 | } |
| 384 | EXPORT_SYMBOL(omap_mbox_register); |
| 385 | |
| 386 | int omap_mbox_unregister(struct omap_mbox *mbox) |
| 387 | { |
| 388 | struct omap_mbox **tmp; |
| 389 | |
| 390 | write_lock(&mboxes_lock); |
| 391 | tmp = &mboxes; |
| 392 | while (*tmp) { |
| 393 | if (mbox == *tmp) { |
| 394 | *tmp = mbox->next; |
| 395 | mbox->next = NULL; |
| 396 | write_unlock(&mboxes_lock); |
| 397 | return 0; |
| 398 | } |
| 399 | tmp = &(*tmp)->next; |
| 400 | } |
| 401 | write_unlock(&mboxes_lock); |
| 402 | |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | EXPORT_SYMBOL(omap_mbox_unregister); |
| 406 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 407 | static int __init omap_mbox_init(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 408 | { |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 409 | mboxd = create_workqueue("mboxd"); |
| 410 | if (!mboxd) |
| 411 | return -ENOMEM; |
| 412 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 413 | return 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 414 | } |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 415 | module_init(omap_mbox_init); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 416 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 417 | static void __exit omap_mbox_exit(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 418 | { |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 419 | destroy_workqueue(mboxd); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 420 | } |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 421 | module_exit(omap_mbox_exit); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 422 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 423 | MODULE_LICENSE("GPL v2"); |
| 424 | MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging"); |
| 425 | MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU"); |