Merge branch 'i2c/for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[deliverable/linux.git] / drivers / mailbox / omap-mailbox.c
CommitLineData
340a614a
HD
1/*
2 * OMAP mailbox driver
3 *
f48cca87 4 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
340a614a 5 *
f48cca87 6 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
340a614a
HD
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
340a614a 24#include <linux/interrupt.h>
b3e69146
FC
25#include <linux/spinlock.h>
26#include <linux/mutex.h>
340a614a 27#include <linux/delay.h>
5a0e3ad6 28#include <linux/slab.h>
b5bebe41
OBC
29#include <linux/kfifo.h>
30#include <linux/err.h>
58256307 31#include <linux/notifier.h>
73017a54 32#include <linux/module.h>
8dff0fa5 33
c869c75c 34#include "omap-mbox.h"
340a614a 35
9c80c8cd 36static struct omap_mbox **mboxes;
340a614a 37
5f00ec64 38static int mbox_configured;
72b917ef 39static DEFINE_MUTEX(mbox_configured_lock);
5f00ec64 40
b5bebe41
OBC
41static 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
9ae0ee00
HD
45/* 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 */
9ae0ee00
HD
64static 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
340a614a
HD
74/*
75 * message sender
76 */
b5bebe41 77static int __mbox_poll_for_space(struct omap_mbox *mbox)
340a614a
HD
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 }
340a614a
HD
88 return ret;
89}
90
b2b6362e 91int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
340a614a 92{
b5bebe41
OBC
93 struct omap_mbox_queue *mq = mbox->txq;
94 int ret = 0, len;
5ed8d32e 95
a42743c2 96 spin_lock_bh(&mq->lock);
ec24751a 97
b5bebe41
OBC
98 if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
99 ret = -ENOMEM;
100 goto out;
101 }
102
a42743c2
KH
103 if (kfifo_is_empty(&mq->fifo) && !__mbox_poll_for_space(mbox)) {
104 mbox_fifo_write(mbox, msg);
105 goto out;
106 }
107
b5bebe41
OBC
108 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
109 WARN_ON(len != sizeof(msg));
340a614a 110
5ed8d32e 111 tasklet_schedule(&mbox->txq->tasklet);
340a614a 112
b5bebe41 113out:
a42743c2 114 spin_unlock_bh(&mq->lock);
b5bebe41 115 return ret;
340a614a
HD
116}
117EXPORT_SYMBOL(omap_mbox_msg_send);
118
c869c75c
SA
119void 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
5ed8d32e 153static void mbox_tx_tasklet(unsigned long tx_data)
340a614a 154{
5ed8d32e 155 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
b5bebe41
OBC
156 struct omap_mbox_queue *mq = mbox->txq;
157 mbox_msg_t msg;
158 int ret;
340a614a 159
b5bebe41
OBC
160 while (kfifo_len(&mq->fifo)) {
161 if (__mbox_poll_for_space(mbox)) {
eb18858e 162 omap_mbox_enable_irq(mbox, IRQ_TX);
b5bebe41 163 break;
340a614a 164 }
b5bebe41
OBC
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);
340a614a
HD
171 }
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);
340a614a 181 mbox_msg_t msg;
b5bebe41
OBC
182 int len;
183
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));
340a614a 187
58256307
KH
188 blocking_notifier_call_chain(&mq->mbox->notifier, len,
189 (void *)msg);
d2295042
FGL
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);
340a614a
HD
196 }
197}
198
199/*
200 * Mailbox interrupt handler
201 */
340a614a
HD
202static void __mbox_tx_interrupt(struct omap_mbox *mbox)
203{
eb18858e 204 omap_mbox_disable_irq(mbox, IRQ_TX);
340a614a 205 ack_mbox_irq(mbox, IRQ_TX);
5ed8d32e 206 tasklet_schedule(&mbox->txq->tasklet);
340a614a
HD
207}
208
209static void __mbox_rx_interrupt(struct omap_mbox *mbox)
210{
b5bebe41 211 struct omap_mbox_queue *mq = mbox->rxq;
340a614a 212 mbox_msg_t msg;
b5bebe41 213 int len;
340a614a 214
340a614a 215 while (!mbox_fifo_empty(mbox)) {
b5bebe41 216 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
1ea5d6d1 217 omap_mbox_disable_irq(mbox, IRQ_RX);
d2295042 218 mq->full = true;
340a614a 219 goto nomem;
1ea5d6d1 220 }
340a614a
HD
221
222 msg = mbox_fifo_read(mbox);
340a614a 223
b5bebe41
OBC
224 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
225 WARN_ON(len != sizeof(msg));
340a614a 226
340a614a
HD
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);
f48cca87 233nomem:
c4873005 234 schedule_work(&mbox->rxq->work);
340a614a
HD
235}
236
237static irqreturn_t mbox_interrupt(int irq, void *p)
238{
2a7057e3 239 struct omap_mbox *mbox = p;
340a614a
HD
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
340a614a 250static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
5ed8d32e
S
251 void (*work) (struct work_struct *),
252 void (*tasklet)(unsigned long))
340a614a 253{
340a614a
HD
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
b5bebe41 262 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
340a614a 263 goto error;
340a614a 264
5ed8d32e
S
265 if (work)
266 INIT_WORK(&mq->work, work);
340a614a 267
5ed8d32e
S
268 if (tasklet)
269 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
340a614a
HD
270 return mq;
271error:
272 kfree(mq);
273 return NULL;
274}
275
276static void mbox_queue_free(struct omap_mbox_queue *q)
277{
b5bebe41 278 kfifo_free(&q->fifo);
340a614a
HD
279 kfree(q);
280}
281
c7c158e5 282static int omap_mbox_startup(struct omap_mbox *mbox)
340a614a 283{
5f00ec64 284 int ret = 0;
340a614a
HD
285 struct omap_mbox_queue *mq;
286
58256307
KH
287 mutex_lock(&mbox_configured_lock);
288 if (!mbox_configured++) {
289 if (likely(mbox->ops->startup)) {
5f00ec64 290 ret = mbox->ops->startup(mbox);
58256307
KH
291 if (unlikely(ret))
292 goto fail_startup;
293 } else
294 goto fail_startup;
340a614a
HD
295 }
296
58256307 297 if (!mbox->use_count++) {
58256307
KH
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;
340a614a 304
58256307
KH
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;
ecf305cf
SA
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 }
1d8a0e96
JG
319
320 omap_mbox_enable_irq(mbox, IRQ_RX);
340a614a 321 }
58256307 322 mutex_unlock(&mbox_configured_lock);
340a614a
HD
323 return 0;
324
ecf305cf
SA
325fail_request_irq:
326 mbox_queue_free(mbox->rxq);
ab66ac30 327fail_alloc_rxq:
340a614a 328 mbox_queue_free(mbox->txq);
ab66ac30 329fail_alloc_txq:
01072d8f 330 if (mbox->ops->shutdown)
340a614a 331 mbox->ops->shutdown(mbox);
58256307
KH
332 mbox->use_count--;
333fail_startup:
334 mbox_configured--;
335 mutex_unlock(&mbox_configured_lock);
340a614a
HD
336 return ret;
337}
338
339static void omap_mbox_fini(struct omap_mbox *mbox)
340{
58256307
KH
341 mutex_lock(&mbox_configured_lock);
342
343 if (!--mbox->use_count) {
1d8a0e96 344 omap_mbox_disable_irq(mbox, IRQ_RX);
58256307
KH
345 free_irq(mbox->irq, mbox);
346 tasklet_kill(&mbox->txq->tasklet);
43829731 347 flush_work(&mbox->rxq->work);
58256307
KH
348 mbox_queue_free(mbox->txq);
349 mbox_queue_free(mbox->rxq);
350 }
340a614a 351
58256307
KH
352 if (likely(mbox->ops->shutdown)) {
353 if (!--mbox_configured)
5f00ec64 354 mbox->ops->shutdown(mbox);
5f00ec64 355 }
58256307
KH
356
357 mutex_unlock(&mbox_configured_lock);
340a614a
HD
358}
359
58256307 360struct omap_mbox *omap_mbox_get(const char *name, struct notifier_block *nb)
340a614a 361{
c0377320
KH
362 struct omap_mbox *_mbox, *mbox = NULL;
363 int i, ret;
340a614a 364
9c80c8cd
FC
365 if (!mboxes)
366 return ERR_PTR(-EINVAL);
340a614a 367
c0377320
KH
368 for (i = 0; (_mbox = mboxes[i]); i++) {
369 if (!strcmp(_mbox->name, name)) {
370 mbox = _mbox;
9c80c8cd 371 break;
c0377320
KH
372 }
373 }
9c80c8cd
FC
374
375 if (!mbox)
376 return ERR_PTR(-ENOENT);
340a614a 377
58256307
KH
378 if (nb)
379 blocking_notifier_chain_register(&mbox->notifier, nb);
380
1d8a0e96
JG
381 ret = omap_mbox_startup(mbox);
382 if (ret) {
383 blocking_notifier_chain_unregister(&mbox->notifier, nb);
384 return ERR_PTR(-ENODEV);
385 }
386
340a614a
HD
387 return mbox;
388}
389EXPORT_SYMBOL(omap_mbox_get);
390
58256307 391void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb)
340a614a 392{
58256307 393 blocking_notifier_chain_unregister(&mbox->notifier, nb);
340a614a
HD
394 omap_mbox_fini(mbox);
395}
396EXPORT_SYMBOL(omap_mbox_put);
397
6b233985
HD
398static struct class omap_mbox_class = { .name = "mbox", };
399
9c80c8cd 400int omap_mbox_register(struct device *parent, struct omap_mbox **list)
340a614a 401{
9c80c8cd
FC
402 int ret;
403 int i;
340a614a 404
9c80c8cd
FC
405 mboxes = list;
406 if (!mboxes)
340a614a 407 return -EINVAL;
340a614a 408
9c80c8cd
FC
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 }
58256307
KH
417
418 BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier);
9c80c8cd 419 }
f48cca87
HD
420 return 0;
421
9c80c8cd
FC
422err_out:
423 while (i--)
424 device_unregister(mboxes[i]->dev);
340a614a
HD
425 return ret;
426}
427EXPORT_SYMBOL(omap_mbox_register);
428
9c80c8cd 429int omap_mbox_unregister(void)
340a614a 430{
9c80c8cd 431 int i;
340a614a 432
9c80c8cd
FC
433 if (!mboxes)
434 return -EINVAL;
435
436 for (i = 0; mboxes[i]; i++)
437 device_unregister(mboxes[i]->dev);
438 mboxes = NULL;
439 return 0;
340a614a
HD
440}
441EXPORT_SYMBOL(omap_mbox_unregister);
442
c7c158e5 443static int __init omap_mbox_init(void)
340a614a 444{
6b233985
HD
445 int err;
446
447 err = class_register(&omap_mbox_class);
448 if (err)
449 return err;
450
b5bebe41
OBC
451 /* kfifo size sanity check: alignment and minimal size */
452 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
ab66ac30
KH
453 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
454 sizeof(mbox_msg_t));
b5bebe41 455
c7c158e5 456 return 0;
340a614a 457}
6b233985 458subsys_initcall(omap_mbox_init);
340a614a 459
c7c158e5 460static void __exit omap_mbox_exit(void)
340a614a 461{
6b233985 462 class_unregister(&omap_mbox_class);
340a614a 463}
c7c158e5 464module_exit(omap_mbox_exit);
340a614a 465
f48cca87
HD
466MODULE_LICENSE("GPL v2");
467MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
f375325a
OBC
468MODULE_AUTHOR("Toshihiro Kobayashi");
469MODULE_AUTHOR("Hiroshi DOYU");
This page took 0.444141 seconds and 5 git commands to generate.