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