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