blob: d79a646b9042cde5786e87ded1b9d51a2093feb1 [file] [log] [blame]
Hiroshi DOYU340a6142006-12-07 15:43:59 -08001/*
2 * OMAP mailbox driver
3 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07004 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
Hiroshi DOYU340a6142006-12-07 15:43:59 -08005 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07006 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
Hiroshi DOYU340a6142006-12-07 15:43:59 -08007 *
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 DOYU340a6142006-12-07 15:43:59 -080024#include <linux/interrupt.h>
Felipe Contrerasb3e69142010-06-11 15:51:49 +000025#include <linux/spinlock.h>
26#include <linux/mutex.h>
Hiroshi DOYU340a6142006-12-07 15:43:59 -080027#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +000029#include <linux/kfifo.h>
30#include <linux/err.h>
Kanigeri, Hari58256302010-11-29 20:24:14 +000031#include <linux/notifier.h>
Paul Gortmaker73017a52011-07-31 16:14:14 -040032#include <linux/module.h>
Hiroshi DOYU8dff0fa2009-03-23 18:07:32 -070033
Suman Annac869c752013-03-12 17:55:29 -050034#include "omap-mbox.h"
Hiroshi DOYU340a6142006-12-07 15:43:59 -080035
Felipe Contreras9c80c8c2010-06-11 15:51:46 +000036static struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -080037
C A Subramaniam5f00ec62009-11-22 10:11:22 -080038static int mbox_configured;
Hiroshi DOYU72b917e2010-02-18 00:48:55 -060039static DEFINE_MUTEX(mbox_configured_lock);
C A Subramaniam5f00ec62009-11-22 10:11:22 -080040
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +000041static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
42module_param(mbox_kfifo_size, uint, S_IRUGO);
43MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
44
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -070045/* Mailbox FIFO handle functions */
46static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
47{
48 return mbox->ops->fifo_read(mbox);
49}
50static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
51{
52 mbox->ops->fifo_write(mbox, msg);
53}
54static inline int mbox_fifo_empty(struct omap_mbox *mbox)
55{
56 return mbox->ops->fifo_empty(mbox);
57}
58static 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 DOYU9ae0ee02009-03-23 18:07:26 -070064static 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}
69static 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 DOYU340a6142006-12-07 15:43:59 -080074/*
75 * message sender
76 */
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +000077static int __mbox_poll_for_space(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080078{
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 DOYU340a6142006-12-07 15:43:59 -080088 return ret;
89}
90
C A Subramaniamb2b63622009-11-22 10:11:20 -080091int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a6142006-12-07 15:43:59 -080092{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +000093 struct omap_mbox_queue *mq = mbox->txq;
94 int ret = 0, len;
C A Subramaniam5ed8d322009-11-22 10:11:24 -080095
Kanigeri, Haria42743c2010-11-29 20:24:13 +000096 spin_lock_bh(&mq->lock);
Tejun Heoec247512009-04-23 11:05:20 +090097
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +000098 if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
99 ret = -ENOMEM;
100 goto out;
101 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800102
Kanigeri, Haria42743c2010-11-29 20:24:13 +0000103 if (kfifo_is_empty(&mq->fifo) && !__mbox_poll_for_space(mbox)) {
104 mbox_fifo_write(mbox, msg);
105 goto out;
106 }
107
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000108 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
109 WARN_ON(len != sizeof(msg));
110
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800111 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800112
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000113out:
Kanigeri, Haria42743c2010-11-29 20:24:13 +0000114 spin_unlock_bh(&mq->lock);
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000115 return ret;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800116}
117EXPORT_SYMBOL(omap_mbox_msg_send);
118
Suman Annac869c752013-03-12 17:55:29 -0500119void 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}
128EXPORT_SYMBOL(omap_mbox_save_ctx);
129
130void 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}
139EXPORT_SYMBOL(omap_mbox_restore_ctx);
140
141void omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
142{
143 mbox->ops->enable_irq(mbox, irq);
144}
145EXPORT_SYMBOL(omap_mbox_enable_irq);
146
147void omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
148{
149 mbox->ops->disable_irq(mbox, irq);
150}
151EXPORT_SYMBOL(omap_mbox_disable_irq);
152
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800153static void mbox_tx_tasklet(unsigned long tx_data)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800154{
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800155 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000156 struct omap_mbox_queue *mq = mbox->txq;
157 mbox_msg_t msg;
158 int ret;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800159
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000160 while (kfifo_len(&mq->fifo)) {
161 if (__mbox_poll_for_space(mbox)) {
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800162 omap_mbox_enable_irq(mbox, IRQ_TX);
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000163 break;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800164 }
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000165
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 DOYU340a6142006-12-07 15:43:59 -0800171 }
172}
173
174/*
175 * Message receiver(workqueue)
176 */
177static 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 DOYU340a6142006-12-07 15:43:59 -0800181 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000182 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800183
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000184 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 DOYU340a6142006-12-07 15:43:59 -0800187
Kanigeri, Hari58256302010-11-29 20:24:14 +0000188 blocking_notifier_call_chain(&mq->mbox->notifier, len,
189 (void *)msg);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000190 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 DOYU340a6142006-12-07 15:43:59 -0800196 }
197}
198
199/*
200 * Mailbox interrupt handler
201 */
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800202static void __mbox_tx_interrupt(struct omap_mbox *mbox)
203{
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800204 omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800205 ack_mbox_irq(mbox, IRQ_TX);
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800206 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800207}
208
209static void __mbox_rx_interrupt(struct omap_mbox *mbox)
210{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000211 struct omap_mbox_queue *mq = mbox->rxq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800212 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000213 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800214
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800215 while (!mbox_fifo_empty(mbox)) {
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000216 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600217 omap_mbox_disable_irq(mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000218 mq->full = true;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800219 goto nomem;
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600220 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800221
222 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800223
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000224 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
225 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800226
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800227 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 DOYUf48cca82009-03-23 18:07:24 -0700233nomem:
Tejun Heoc4873002011-01-26 12:12:50 +0100234 schedule_work(&mbox->rxq->work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800235}
236
237static irqreturn_t mbox_interrupt(int irq, void *p)
238{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400239 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800240
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 DOYU340a6142006-12-07 15:43:59 -0800250static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800251 void (*work) (struct work_struct *),
252 void (*tasklet)(unsigned long))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800253{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800254 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-Cohenb5bebe42010-05-05 15:33:09 +0000262 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800263 goto error;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800264
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800265 if (work)
266 INIT_WORK(&mq->work, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800267
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800268 if (tasklet)
269 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800270 return mq;
271error:
272 kfree(mq);
273 return NULL;
274}
275
276static void mbox_queue_free(struct omap_mbox_queue *q)
277{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000278 kfifo_free(&q->fifo);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800279 kfree(q);
280}
281
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800282static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800283{
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800284 int ret = 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800285 struct omap_mbox_queue *mq;
286
Kanigeri, Hari58256302010-11-29 20:24:14 +0000287 mutex_lock(&mbox_configured_lock);
288 if (!mbox_configured++) {
289 if (likely(mbox->ops->startup)) {
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800290 ret = mbox->ops->startup(mbox);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000291 if (unlikely(ret))
292 goto fail_startup;
293 } else
294 goto fail_startup;
295 }
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800296
Kanigeri, Hari58256302010-11-29 20:24:14 +0000297 if (!mbox->use_count++) {
Kanigeri, Hari58256302010-11-29 20:24:14 +0000298 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 DOYU340a6142006-12-07 15:43:59 -0800304
Kanigeri, Hari58256302010-11-29 20:24:14 +0000305 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 Annaecf305c2013-02-01 20:37:06 -0600312 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 Gutierrez1d8a0e92012-05-13 15:33:04 +0300319
320 omap_mbox_enable_irq(mbox, IRQ_RX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800321 }
Kanigeri, Hari58256302010-11-29 20:24:14 +0000322 mutex_unlock(&mbox_configured_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800323 return 0;
324
Suman Annaecf305c2013-02-01 20:37:06 -0600325fail_request_irq:
326 mbox_queue_free(mbox->rxq);
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000327fail_alloc_rxq:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800328 mbox_queue_free(mbox->txq);
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000329fail_alloc_txq:
Ohad Ben-Cohen01072d82010-05-05 15:33:08 +0000330 if (mbox->ops->shutdown)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800331 mbox->ops->shutdown(mbox);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000332 mbox->use_count--;
333fail_startup:
334 mbox_configured--;
335 mutex_unlock(&mbox_configured_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800336 return ret;
337}
338
339static void omap_mbox_fini(struct omap_mbox *mbox)
340{
Kanigeri, Hari58256302010-11-29 20:24:14 +0000341 mutex_lock(&mbox_configured_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800342
Kanigeri, Hari58256302010-11-29 20:24:14 +0000343 if (!--mbox->use_count) {
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300344 omap_mbox_disable_irq(mbox, IRQ_RX);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000345 free_irq(mbox->irq, mbox);
346 tasklet_kill(&mbox->txq->tasklet);
Tejun Heo43829732012-08-20 14:51:24 -0700347 flush_work(&mbox->rxq->work);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000348 mbox_queue_free(mbox->txq);
349 mbox_queue_free(mbox->rxq);
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800350 }
Kanigeri, Hari58256302010-11-29 20:24:14 +0000351
352 if (likely(mbox->ops->shutdown)) {
353 if (!--mbox_configured)
354 mbox->ops->shutdown(mbox);
355 }
356
357 mutex_unlock(&mbox_configured_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800358}
359
Kanigeri, Hari58256302010-11-29 20:24:14 +0000360struct omap_mbox *omap_mbox_get(const char *name, struct notifier_block *nb)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800361{
Kevin Hilmanc0377322011-02-11 19:56:43 +0000362 struct omap_mbox *_mbox, *mbox = NULL;
363 int i, ret;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800364
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000365 if (!mboxes)
366 return ERR_PTR(-EINVAL);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800367
Kevin Hilmanc0377322011-02-11 19:56:43 +0000368 for (i = 0; (_mbox = mboxes[i]); i++) {
369 if (!strcmp(_mbox->name, name)) {
370 mbox = _mbox;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000371 break;
Kevin Hilmanc0377322011-02-11 19:56:43 +0000372 }
373 }
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000374
375 if (!mbox)
376 return ERR_PTR(-ENOENT);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800377
Kanigeri, Hari58256302010-11-29 20:24:14 +0000378 if (nb)
379 blocking_notifier_chain_register(&mbox->notifier, nb);
380
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300381 ret = omap_mbox_startup(mbox);
382 if (ret) {
383 blocking_notifier_chain_unregister(&mbox->notifier, nb);
384 return ERR_PTR(-ENODEV);
385 }
386
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800387 return mbox;
388}
389EXPORT_SYMBOL(omap_mbox_get);
390
Kanigeri, Hari58256302010-11-29 20:24:14 +0000391void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800392{
Kanigeri, Hari58256302010-11-29 20:24:14 +0000393 blocking_notifier_chain_unregister(&mbox->notifier, nb);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800394 omap_mbox_fini(mbox);
395}
396EXPORT_SYMBOL(omap_mbox_put);
397
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300398static struct class omap_mbox_class = { .name = "mbox", };
399
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000400int omap_mbox_register(struct device *parent, struct omap_mbox **list)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800401{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000402 int ret;
403 int i;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800404
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000405 mboxes = list;
406 if (!mboxes)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800407 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800408
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000409 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, Hari58256302010-11-29 20:24:14 +0000417
418 BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700419 }
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700420 return 0;
421
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000422err_out:
423 while (i--)
424 device_unregister(mboxes[i]->dev);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800425 return ret;
426}
427EXPORT_SYMBOL(omap_mbox_register);
428
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000429int omap_mbox_unregister(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800430{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000431 int i;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800432
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000433 if (!mboxes)
434 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800435
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000436 for (i = 0; mboxes[i]; i++)
437 device_unregister(mboxes[i]->dev);
438 mboxes = NULL;
439 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800440}
441EXPORT_SYMBOL(omap_mbox_unregister);
442
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800443static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800444{
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300445 int err;
446
447 err = class_register(&omap_mbox_class);
448 if (err)
449 return err;
450
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000451 /* kfifo size sanity check: alignment and minimal size */
452 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000453 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
454 sizeof(mbox_msg_t));
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000455
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800456 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800457}
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300458subsys_initcall(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800459
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800460static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800461{
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300462 class_unregister(&omap_mbox_class);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800463}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800464module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800465
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700466MODULE_LICENSE("GPL v2");
467MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
Ohad Ben-Cohenf3753252010-05-05 15:33:07 +0000468MODULE_AUTHOR("Toshihiro Kobayashi");
469MODULE_AUTHOR("Hiroshi DOYU");