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/interrupt.h> |
Felipe Contreras | b3e6914 | 2010-06-11 15:51:49 +0000 | [diff] [blame] | 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/mutex.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> |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 29 | #include <linux/kfifo.h> |
| 30 | #include <linux/err.h> |
Hiroshi DOYU | 8dff0fa | 2009-03-23 18:07:32 -0700 | [diff] [blame] | 31 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 32 | #include <plat/mailbox.h> |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 33 | |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 34 | static struct workqueue_struct *mboxd; |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 35 | static struct omap_mbox **mboxes; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 36 | |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 37 | static int mbox_configured; |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 38 | static DEFINE_MUTEX(mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 39 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 40 | static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE; |
| 41 | module_param(mbox_kfifo_size, uint, S_IRUGO); |
| 42 | MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)"); |
| 43 | |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 44 | /* Mailbox FIFO handle functions */ |
| 45 | static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox) |
| 46 | { |
| 47 | return mbox->ops->fifo_read(mbox); |
| 48 | } |
| 49 | static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg) |
| 50 | { |
| 51 | mbox->ops->fifo_write(mbox, msg); |
| 52 | } |
| 53 | static inline int mbox_fifo_empty(struct omap_mbox *mbox) |
| 54 | { |
| 55 | return mbox->ops->fifo_empty(mbox); |
| 56 | } |
| 57 | static inline int mbox_fifo_full(struct omap_mbox *mbox) |
| 58 | { |
| 59 | return mbox->ops->fifo_full(mbox); |
| 60 | } |
| 61 | |
| 62 | /* Mailbox IRQ handle functions */ |
Hiroshi DOYU | 9ae0ee0 | 2009-03-23 18:07:26 -0700 | [diff] [blame] | 63 | static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 64 | { |
| 65 | if (mbox->ops->ack_irq) |
| 66 | mbox->ops->ack_irq(mbox, irq); |
| 67 | } |
| 68 | static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq) |
| 69 | { |
| 70 | return mbox->ops->is_irq(mbox, irq); |
| 71 | } |
| 72 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 73 | /* |
| 74 | * message sender |
| 75 | */ |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 76 | static int __mbox_poll_for_space(struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 77 | { |
| 78 | int ret = 0, i = 1000; |
| 79 | |
| 80 | while (mbox_fifo_full(mbox)) { |
| 81 | if (mbox->ops->type == OMAP_MBOX_TYPE2) |
| 82 | return -1; |
| 83 | if (--i == 0) |
| 84 | return -1; |
| 85 | udelay(1); |
| 86 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 87 | return ret; |
| 88 | } |
| 89 | |
C A Subramaniam | b2b6362 | 2009-11-22 10:11:20 -0800 | [diff] [blame] | 90 | 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] | 91 | { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 92 | struct omap_mbox_queue *mq = mbox->txq; |
| 93 | int ret = 0, len; |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 94 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 95 | spin_lock(&mq->lock); |
Tejun Heo | ec24751 | 2009-04-23 11:05:20 +0900 | [diff] [blame] | 96 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 97 | if (kfifo_avail(&mq->fifo) < sizeof(msg)) { |
| 98 | ret = -ENOMEM; |
| 99 | goto out; |
| 100 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 101 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 102 | len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
| 103 | WARN_ON(len != sizeof(msg)); |
| 104 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 105 | tasklet_schedule(&mbox->txq->tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 106 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 107 | out: |
| 108 | spin_unlock(&mq->lock); |
| 109 | return ret; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 110 | } |
| 111 | EXPORT_SYMBOL(omap_mbox_msg_send); |
| 112 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 113 | static void mbox_tx_tasklet(unsigned long tx_data) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 114 | { |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 115 | struct omap_mbox *mbox = (struct omap_mbox *)tx_data; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 116 | struct omap_mbox_queue *mq = mbox->txq; |
| 117 | mbox_msg_t msg; |
| 118 | int ret; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 119 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 120 | while (kfifo_len(&mq->fifo)) { |
| 121 | if (__mbox_poll_for_space(mbox)) { |
Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 122 | omap_mbox_enable_irq(mbox, IRQ_TX); |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 123 | break; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 124 | } |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 125 | |
| 126 | ret = kfifo_out(&mq->fifo, (unsigned char *)&msg, |
| 127 | sizeof(msg)); |
| 128 | WARN_ON(ret != sizeof(msg)); |
| 129 | |
| 130 | mbox_fifo_write(mbox, msg); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Message receiver(workqueue) |
| 136 | */ |
| 137 | static void mbox_rx_work(struct work_struct *work) |
| 138 | { |
| 139 | struct omap_mbox_queue *mq = |
| 140 | container_of(work, struct omap_mbox_queue, work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 141 | mbox_msg_t msg; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 142 | int len; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 143 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 144 | while (kfifo_len(&mq->fifo) >= sizeof(msg)) { |
| 145 | len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
| 146 | WARN_ON(len != sizeof(msg)); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 147 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 148 | if (mq->callback) |
| 149 | mq->callback((void *)msg); |
Fernando Guzman Lugo | d229504 | 2010-11-29 20:24:11 +0000 | [diff] [blame^] | 150 | spin_lock_irq(&mq->lock); |
| 151 | if (mq->full) { |
| 152 | mq->full = false; |
| 153 | omap_mbox_enable_irq(mq->mbox, IRQ_RX); |
| 154 | } |
| 155 | spin_unlock_irq(&mq->lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Mailbox interrupt handler |
| 161 | */ |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 162 | static void __mbox_tx_interrupt(struct omap_mbox *mbox) |
| 163 | { |
Hiroshi DOYU | eb18858 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 164 | omap_mbox_disable_irq(mbox, IRQ_TX); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 165 | ack_mbox_irq(mbox, IRQ_TX); |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 166 | tasklet_schedule(&mbox->txq->tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void __mbox_rx_interrupt(struct omap_mbox *mbox) |
| 170 | { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 171 | struct omap_mbox_queue *mq = mbox->rxq; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 172 | mbox_msg_t msg; |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 173 | int len; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 174 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 175 | while (!mbox_fifo_empty(mbox)) { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 176 | if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) { |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 177 | omap_mbox_disable_irq(mbox, IRQ_RX); |
Fernando Guzman Lugo | d229504 | 2010-11-29 20:24:11 +0000 | [diff] [blame^] | 178 | mq->full = true; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 179 | goto nomem; |
Fernando Guzman Lugo | 1ea5d6d | 2010-02-08 13:35:40 -0600 | [diff] [blame] | 180 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 181 | |
| 182 | msg = mbox_fifo_read(mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 183 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 184 | len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg)); |
| 185 | WARN_ON(len != sizeof(msg)); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 186 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 187 | if (mbox->ops->type == OMAP_MBOX_TYPE1) |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | /* no more messages in the fifo. clear IRQ source. */ |
| 192 | ack_mbox_irq(mbox, IRQ_RX); |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 193 | nomem: |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 194 | queue_work(mboxd, &mbox->rxq->work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static irqreturn_t mbox_interrupt(int irq, void *p) |
| 198 | { |
Jeff Garzik | 2a7057e | 2007-10-26 05:40:22 -0400 | [diff] [blame] | 199 | struct omap_mbox *mbox = p; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 200 | |
| 201 | if (is_mbox_irq(mbox, IRQ_TX)) |
| 202 | __mbox_tx_interrupt(mbox); |
| 203 | |
| 204 | if (is_mbox_irq(mbox, IRQ_RX)) |
| 205 | __mbox_rx_interrupt(mbox); |
| 206 | |
| 207 | return IRQ_HANDLED; |
| 208 | } |
| 209 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 210 | static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox, |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 211 | void (*work) (struct work_struct *), |
| 212 | void (*tasklet)(unsigned long)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 213 | { |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 214 | struct omap_mbox_queue *mq; |
| 215 | |
| 216 | mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL); |
| 217 | if (!mq) |
| 218 | return NULL; |
| 219 | |
| 220 | spin_lock_init(&mq->lock); |
| 221 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 222 | if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL)) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 223 | goto error; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 224 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 225 | if (work) |
| 226 | INIT_WORK(&mq->work, work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 227 | |
C A Subramaniam | 5ed8d32 | 2009-11-22 10:11:24 -0800 | [diff] [blame] | 228 | if (tasklet) |
| 229 | tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 230 | return mq; |
| 231 | error: |
| 232 | kfree(mq); |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | static void mbox_queue_free(struct omap_mbox_queue *q) |
| 237 | { |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 238 | kfifo_free(&q->fifo); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 239 | kfree(q); |
| 240 | } |
| 241 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 242 | static int omap_mbox_startup(struct omap_mbox *mbox) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 243 | { |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 244 | int ret = 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 245 | struct omap_mbox_queue *mq; |
| 246 | |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame] | 247 | if (mbox->ops->startup) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 248 | mutex_lock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 249 | if (!mbox_configured) |
| 250 | ret = mbox->ops->startup(mbox); |
| 251 | |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame] | 252 | if (ret) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 253 | mutex_unlock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 254 | return ret; |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 255 | } |
| 256 | mbox_configured++; |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 257 | mutex_unlock(&mbox_configured_lock); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 258 | } |
| 259 | |
C A Subramaniam | 5e68382 | 2009-11-22 10:11:23 -0800 | [diff] [blame] | 260 | ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED, |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 261 | mbox->name, mbox); |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame] | 262 | if (ret) { |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 263 | printk(KERN_ERR |
| 264 | "failed to register mailbox interrupt:%d\n", ret); |
| 265 | goto fail_request_irq; |
| 266 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 267 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 268 | mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 269 | if (!mq) { |
| 270 | ret = -ENOMEM; |
| 271 | goto fail_alloc_txq; |
| 272 | } |
| 273 | mbox->txq = mq; |
| 274 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 275 | mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 276 | if (!mq) { |
| 277 | ret = -ENOMEM; |
| 278 | goto fail_alloc_rxq; |
| 279 | } |
| 280 | mbox->rxq = mq; |
| 281 | |
| 282 | return 0; |
| 283 | |
| 284 | fail_alloc_rxq: |
| 285 | mbox_queue_free(mbox->txq); |
| 286 | fail_alloc_txq: |
| 287 | free_irq(mbox->irq, mbox); |
| 288 | fail_request_irq: |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame] | 289 | if (mbox->ops->shutdown) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 290 | mbox->ops->shutdown(mbox); |
| 291 | |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | static void omap_mbox_fini(struct omap_mbox *mbox) |
| 296 | { |
Fernando Guzman Lugo | ad6d962 | 2010-02-12 19:02:32 -0600 | [diff] [blame] | 297 | free_irq(mbox->irq, mbox); |
Fernando Guzman Lugo | 0e828e8 | 2010-02-12 19:07:14 -0600 | [diff] [blame] | 298 | tasklet_kill(&mbox->txq->tasklet); |
| 299 | flush_work(&mbox->rxq->work); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 300 | mbox_queue_free(mbox->txq); |
| 301 | mbox_queue_free(mbox->rxq); |
| 302 | |
Ohad Ben-Cohen | 01072d8 | 2010-05-05 15:33:08 +0000 | [diff] [blame] | 303 | if (mbox->ops->shutdown) { |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 304 | mutex_lock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 305 | if (mbox_configured > 0) |
| 306 | mbox_configured--; |
| 307 | if (!mbox_configured) |
| 308 | mbox->ops->shutdown(mbox); |
Hiroshi DOYU | 72b917e | 2010-02-18 00:48:55 -0600 | [diff] [blame] | 309 | mutex_unlock(&mbox_configured_lock); |
C A Subramaniam | 5f00ec6 | 2009-11-22 10:11:22 -0800 | [diff] [blame] | 310 | } |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 313 | struct omap_mbox *omap_mbox_get(const char *name) |
| 314 | { |
| 315 | struct omap_mbox *mbox; |
| 316 | int ret; |
| 317 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 318 | if (!mboxes) |
| 319 | return ERR_PTR(-EINVAL); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 320 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 321 | for (mbox = *mboxes; mbox; mbox++) |
| 322 | if (!strcmp(mbox->name, name)) |
| 323 | break; |
| 324 | |
| 325 | if (!mbox) |
| 326 | return ERR_PTR(-ENOENT); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 327 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 328 | ret = omap_mbox_startup(mbox); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 329 | if (ret) |
| 330 | return ERR_PTR(-ENODEV); |
| 331 | |
| 332 | return mbox; |
| 333 | } |
| 334 | EXPORT_SYMBOL(omap_mbox_get); |
| 335 | |
| 336 | void omap_mbox_put(struct omap_mbox *mbox) |
| 337 | { |
| 338 | omap_mbox_fini(mbox); |
| 339 | } |
| 340 | EXPORT_SYMBOL(omap_mbox_put); |
| 341 | |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 342 | static struct class omap_mbox_class = { .name = "mbox", }; |
| 343 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 344 | int omap_mbox_register(struct device *parent, struct omap_mbox **list) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 345 | { |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 346 | int ret; |
| 347 | int i; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 348 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 349 | mboxes = list; |
| 350 | if (!mboxes) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 351 | return -EINVAL; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 352 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 353 | for (i = 0; mboxes[i]; i++) { |
| 354 | struct omap_mbox *mbox = mboxes[i]; |
| 355 | mbox->dev = device_create(&omap_mbox_class, |
| 356 | parent, 0, mbox, "%s", mbox->name); |
| 357 | if (IS_ERR(mbox->dev)) { |
| 358 | ret = PTR_ERR(mbox->dev); |
| 359 | goto err_out; |
| 360 | } |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 361 | } |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 362 | return 0; |
| 363 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 364 | err_out: |
| 365 | while (i--) |
| 366 | device_unregister(mboxes[i]->dev); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 367 | return ret; |
| 368 | } |
| 369 | EXPORT_SYMBOL(omap_mbox_register); |
| 370 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 371 | int omap_mbox_unregister(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 372 | { |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 373 | int i; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 374 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 375 | if (!mboxes) |
| 376 | return -EINVAL; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 377 | |
Felipe Contreras | 9c80c8c | 2010-06-11 15:51:46 +0000 | [diff] [blame] | 378 | for (i = 0; mboxes[i]; i++) |
| 379 | device_unregister(mboxes[i]->dev); |
| 380 | mboxes = NULL; |
| 381 | return 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 382 | } |
| 383 | EXPORT_SYMBOL(omap_mbox_unregister); |
| 384 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 385 | static int __init omap_mbox_init(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 386 | { |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 387 | int err; |
| 388 | |
| 389 | err = class_register(&omap_mbox_class); |
| 390 | if (err) |
| 391 | return err; |
| 392 | |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 393 | mboxd = create_workqueue("mboxd"); |
| 394 | if (!mboxd) |
| 395 | return -ENOMEM; |
| 396 | |
Ohad Ben-Cohen | b5bebe4 | 2010-05-05 15:33:09 +0000 | [diff] [blame] | 397 | /* kfifo size sanity check: alignment and minimal size */ |
| 398 | mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t)); |
| 399 | mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size, sizeof(mbox_msg_t)); |
| 400 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 401 | return 0; |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 402 | } |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 403 | subsys_initcall(omap_mbox_init); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 404 | |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 405 | static void __exit omap_mbox_exit(void) |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 406 | { |
Rob Clark | 8250a5c | 2010-01-04 19:22:03 +0530 | [diff] [blame] | 407 | destroy_workqueue(mboxd); |
Hiroshi DOYU | 6b23398 | 2010-05-18 16:15:32 +0300 | [diff] [blame] | 408 | class_unregister(&omap_mbox_class); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 409 | } |
Hiroshi DOYU | c7c158e | 2009-11-22 10:11:19 -0800 | [diff] [blame] | 410 | module_exit(omap_mbox_exit); |
Hiroshi DOYU | 340a614 | 2006-12-07 15:43:59 -0800 | [diff] [blame] | 411 | |
Hiroshi DOYU | f48cca8 | 2009-03-23 18:07:24 -0700 | [diff] [blame] | 412 | MODULE_LICENSE("GPL v2"); |
| 413 | MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging"); |
Ohad Ben-Cohen | f375325 | 2010-05-05 15:33:07 +0000 | [diff] [blame] | 414 | MODULE_AUTHOR("Toshihiro Kobayashi"); |
| 415 | MODULE_AUTHOR("Hiroshi DOYU"); |