blob: bcc7ee1292764711e64f8ffabc60628a24c8121a [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.
Suman Anna5040f532014-06-24 19:43:41 -05005 * Copyright (C) 2013-2014 Texas Instruments Inc.
Hiroshi DOYU340a6142006-12-07 15:43:59 -08006 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07007 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
Suman Anna5040f532014-06-24 19:43:41 -05008 * Suman Anna <s-anna@ti.com>
Hiroshi DOYU340a6142006-12-07 15:43:59 -08009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 *
24 */
25
Hiroshi DOYU340a6142006-12-07 15:43:59 -080026#include <linux/interrupt.h>
Felipe Contrerasb3e69142010-06-11 15:51:49 +000027#include <linux/spinlock.h>
28#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +000030#include <linux/kfifo.h>
31#include <linux/err.h>
Kanigeri, Hari58256302010-11-29 20:24:14 +000032#include <linux/notifier.h>
Paul Gortmaker73017a52011-07-31 16:14:14 -040033#include <linux/module.h>
Suman Anna75288cc2014-09-10 14:20:59 -050034#include <linux/of_device.h>
Suman Anna5040f532014-06-24 19:43:41 -050035#include <linux/platform_device.h>
36#include <linux/pm_runtime.h>
37#include <linux/platform_data/mailbox-omap.h>
38#include <linux/omap-mailbox.h>
Hiroshi DOYU8dff0fa2009-03-23 18:07:32 -070039
Suman Anna5040f532014-06-24 19:43:41 -050040#define MAILBOX_REVISION 0x000
41#define MAILBOX_MESSAGE(m) (0x040 + 4 * (m))
42#define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m))
43#define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m))
Hiroshi DOYU340a6142006-12-07 15:43:59 -080044
Suman Anna5040f532014-06-24 19:43:41 -050045#define OMAP2_MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u))
46#define OMAP2_MAILBOX_IRQENABLE(u) (0x104 + 8 * (u))
47
48#define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 0x10 * (u))
49#define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 0x10 * (u))
50#define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 0x10 * (u))
51
52#define MAILBOX_IRQSTATUS(type, u) (type ? OMAP4_MAILBOX_IRQSTATUS(u) : \
53 OMAP2_MAILBOX_IRQSTATUS(u))
54#define MAILBOX_IRQENABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE(u) : \
55 OMAP2_MAILBOX_IRQENABLE(u))
56#define MAILBOX_IRQDISABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE_CLR(u) \
57 : OMAP2_MAILBOX_IRQENABLE(u))
58
59#define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m)))
60#define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1))
61
62#define MBOX_REG_SIZE 0x120
63
64#define OMAP4_MBOX_REG_SIZE 0x130
65
66#define MBOX_NR_REGS (MBOX_REG_SIZE / sizeof(u32))
67#define OMAP4_MBOX_NR_REGS (OMAP4_MBOX_REG_SIZE / sizeof(u32))
68
69struct omap_mbox_fifo {
70 unsigned long msg;
71 unsigned long fifo_stat;
72 unsigned long msg_stat;
Suman Anna5040f532014-06-24 19:43:41 -050073 unsigned long irqenable;
74 unsigned long irqstatus;
Suman Anna5040f532014-06-24 19:43:41 -050075 unsigned long irqdisable;
Suman Annabe3322e2014-06-24 19:43:42 -050076 u32 intr_bit;
Suman Anna5040f532014-06-24 19:43:41 -050077};
78
79struct omap_mbox_queue {
80 spinlock_t lock;
81 struct kfifo fifo;
82 struct work_struct work;
83 struct tasklet_struct tasklet;
84 struct omap_mbox *mbox;
85 bool full;
86};
87
Suman Anna72c1c812014-06-24 19:43:43 -050088struct omap_mbox_device {
89 struct device *dev;
90 struct mutex cfg_lock;
91 void __iomem *mbox_base;
92 u32 num_users;
93 u32 num_fifos;
94 struct omap_mbox **mboxes;
95 struct list_head elem;
96};
97
Suman Anna75288cc2014-09-10 14:20:59 -050098struct omap_mbox_fifo_info {
99 int tx_id;
100 int tx_usr;
101 int tx_irq;
102
103 int rx_id;
104 int rx_usr;
105 int rx_irq;
106
107 const char *name;
108};
109
Suman Anna5040f532014-06-24 19:43:41 -0500110struct omap_mbox {
111 const char *name;
112 int irq;
113 struct omap_mbox_queue *txq, *rxq;
114 struct device *dev;
Suman Anna72c1c812014-06-24 19:43:43 -0500115 struct omap_mbox_device *parent;
Suman Annabe3322e2014-06-24 19:43:42 -0500116 struct omap_mbox_fifo tx_fifo;
117 struct omap_mbox_fifo rx_fifo;
118 u32 ctx[OMAP4_MBOX_NR_REGS];
119 u32 intr_type;
Suman Anna5040f532014-06-24 19:43:41 -0500120 int use_count;
121 struct blocking_notifier_head notifier;
122};
123
Suman Anna72c1c812014-06-24 19:43:43 -0500124/* global variables for the mailbox devices */
125static DEFINE_MUTEX(omap_mbox_devices_lock);
126static LIST_HEAD(omap_mbox_devices);
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800127
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000128static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
129module_param(mbox_kfifo_size, uint, S_IRUGO);
130MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
131
Suman Anna72c1c812014-06-24 19:43:43 -0500132static inline
133unsigned int mbox_read_reg(struct omap_mbox_device *mdev, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500134{
Suman Anna72c1c812014-06-24 19:43:43 -0500135 return __raw_readl(mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500136}
137
Suman Anna72c1c812014-06-24 19:43:43 -0500138static inline
139void mbox_write_reg(struct omap_mbox_device *mdev, u32 val, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500140{
Suman Anna72c1c812014-06-24 19:43:43 -0500141 __raw_writel(val, mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500142}
143
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700144/* Mailbox FIFO handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500145static mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700146{
Suman Annabe3322e2014-06-24 19:43:42 -0500147 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500148 return (mbox_msg_t) mbox_read_reg(mbox->parent, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700149}
Suman Anna5040f532014-06-24 19:43:41 -0500150
151static void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700152{
Suman Annabe3322e2014-06-24 19:43:42 -0500153 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500154 mbox_write_reg(mbox->parent, msg, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700155}
Suman Anna5040f532014-06-24 19:43:41 -0500156
157static int mbox_fifo_empty(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700158{
Suman Annabe3322e2014-06-24 19:43:42 -0500159 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500160 return (mbox_read_reg(mbox->parent, fifo->msg_stat) == 0);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700161}
Suman Anna5040f532014-06-24 19:43:41 -0500162
163static int mbox_fifo_full(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700164{
Suman Annabe3322e2014-06-24 19:43:42 -0500165 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500166 return mbox_read_reg(mbox->parent, fifo->fifo_stat);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700167}
168
169/* Mailbox IRQ handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500170static void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700171{
Suman Annabe3322e2014-06-24 19:43:42 -0500172 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
173 &mbox->tx_fifo : &mbox->rx_fifo;
174 u32 bit = fifo->intr_bit;
175 u32 irqstatus = fifo->irqstatus;
Suman Anna5040f532014-06-24 19:43:41 -0500176
Suman Anna72c1c812014-06-24 19:43:43 -0500177 mbox_write_reg(mbox->parent, bit, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500178
179 /* Flush posted write for irq status to avoid spurious interrupts */
Suman Anna72c1c812014-06-24 19:43:43 -0500180 mbox_read_reg(mbox->parent, irqstatus);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700181}
Suman Anna5040f532014-06-24 19:43:41 -0500182
183static int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700184{
Suman Annabe3322e2014-06-24 19:43:42 -0500185 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
186 &mbox->tx_fifo : &mbox->rx_fifo;
187 u32 bit = fifo->intr_bit;
188 u32 irqenable = fifo->irqenable;
189 u32 irqstatus = fifo->irqstatus;
190
Suman Anna72c1c812014-06-24 19:43:43 -0500191 u32 enable = mbox_read_reg(mbox->parent, irqenable);
192 u32 status = mbox_read_reg(mbox->parent, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500193
194 return (int)(enable & status & bit);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700195}
196
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800197/*
198 * message sender
199 */
C A Subramaniamb2b63622009-11-22 10:11:20 -0800200int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800201{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000202 struct omap_mbox_queue *mq = mbox->txq;
203 int ret = 0, len;
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800204
Kanigeri, Haria42743c2010-11-29 20:24:13 +0000205 spin_lock_bh(&mq->lock);
Tejun Heoec247512009-04-23 11:05:20 +0900206
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000207 if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
208 ret = -ENOMEM;
209 goto out;
210 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800211
Suman Annafe714a42014-06-24 19:43:39 -0500212 if (kfifo_is_empty(&mq->fifo) && !mbox_fifo_full(mbox)) {
Kanigeri, Haria42743c2010-11-29 20:24:13 +0000213 mbox_fifo_write(mbox, msg);
214 goto out;
215 }
216
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000217 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
218 WARN_ON(len != sizeof(msg));
219
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800220 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800221
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000222out:
Kanigeri, Haria42743c2010-11-29 20:24:13 +0000223 spin_unlock_bh(&mq->lock);
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000224 return ret;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800225}
226EXPORT_SYMBOL(omap_mbox_msg_send);
227
Suman Annac869c752013-03-12 17:55:29 -0500228void omap_mbox_save_ctx(struct omap_mbox *mbox)
229{
Suman Anna5040f532014-06-24 19:43:41 -0500230 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500231 int nr_regs;
Suman Annac869c752013-03-12 17:55:29 -0500232
Suman Annabe3322e2014-06-24 19:43:42 -0500233 if (mbox->intr_type)
Suman Anna5040f532014-06-24 19:43:41 -0500234 nr_regs = OMAP4_MBOX_NR_REGS;
235 else
236 nr_regs = MBOX_NR_REGS;
237 for (i = 0; i < nr_regs; i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500238 mbox->ctx[i] = mbox_read_reg(mbox->parent, i * sizeof(u32));
Suman Anna5040f532014-06-24 19:43:41 -0500239
240 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
Suman Annabe3322e2014-06-24 19:43:42 -0500241 i, mbox->ctx[i]);
Suman Anna5040f532014-06-24 19:43:41 -0500242 }
Suman Annac869c752013-03-12 17:55:29 -0500243}
244EXPORT_SYMBOL(omap_mbox_save_ctx);
245
246void omap_mbox_restore_ctx(struct omap_mbox *mbox)
247{
Suman Anna5040f532014-06-24 19:43:41 -0500248 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500249 int nr_regs;
Suman Annac869c752013-03-12 17:55:29 -0500250
Suman Annabe3322e2014-06-24 19:43:42 -0500251 if (mbox->intr_type)
Suman Anna5040f532014-06-24 19:43:41 -0500252 nr_regs = OMAP4_MBOX_NR_REGS;
253 else
254 nr_regs = MBOX_NR_REGS;
255 for (i = 0; i < nr_regs; i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500256 mbox_write_reg(mbox->parent, mbox->ctx[i], i * sizeof(u32));
Suman Anna5040f532014-06-24 19:43:41 -0500257
258 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
Suman Annabe3322e2014-06-24 19:43:42 -0500259 i, mbox->ctx[i]);
Suman Anna5040f532014-06-24 19:43:41 -0500260 }
Suman Annac869c752013-03-12 17:55:29 -0500261}
262EXPORT_SYMBOL(omap_mbox_restore_ctx);
263
264void omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
265{
Suman Annabe3322e2014-06-24 19:43:42 -0500266 u32 l;
267 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
268 &mbox->tx_fifo : &mbox->rx_fifo;
269 u32 bit = fifo->intr_bit;
270 u32 irqenable = fifo->irqenable;
Suman Anna5040f532014-06-24 19:43:41 -0500271
Suman Anna72c1c812014-06-24 19:43:43 -0500272 l = mbox_read_reg(mbox->parent, irqenable);
Suman Anna5040f532014-06-24 19:43:41 -0500273 l |= bit;
Suman Anna72c1c812014-06-24 19:43:43 -0500274 mbox_write_reg(mbox->parent, l, irqenable);
Suman Annac869c752013-03-12 17:55:29 -0500275}
276EXPORT_SYMBOL(omap_mbox_enable_irq);
277
278void omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
279{
Suman Annabe3322e2014-06-24 19:43:42 -0500280 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
281 &mbox->tx_fifo : &mbox->rx_fifo;
282 u32 bit = fifo->intr_bit;
283 u32 irqdisable = fifo->irqdisable;
Suman Anna5040f532014-06-24 19:43:41 -0500284
285 /*
286 * Read and update the interrupt configuration register for pre-OMAP4.
287 * OMAP4 and later SoCs have a dedicated interrupt disabling register.
288 */
Suman Annabe3322e2014-06-24 19:43:42 -0500289 if (!mbox->intr_type)
Suman Anna72c1c812014-06-24 19:43:43 -0500290 bit = mbox_read_reg(mbox->parent, irqdisable) & ~bit;
Suman Anna5040f532014-06-24 19:43:41 -0500291
Suman Anna72c1c812014-06-24 19:43:43 -0500292 mbox_write_reg(mbox->parent, bit, irqdisable);
Suman Annac869c752013-03-12 17:55:29 -0500293}
294EXPORT_SYMBOL(omap_mbox_disable_irq);
295
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800296static void mbox_tx_tasklet(unsigned long tx_data)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800297{
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800298 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000299 struct omap_mbox_queue *mq = mbox->txq;
300 mbox_msg_t msg;
301 int ret;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800302
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000303 while (kfifo_len(&mq->fifo)) {
Suman Annafe714a42014-06-24 19:43:39 -0500304 if (mbox_fifo_full(mbox)) {
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800305 omap_mbox_enable_irq(mbox, IRQ_TX);
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000306 break;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800307 }
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000308
309 ret = kfifo_out(&mq->fifo, (unsigned char *)&msg,
310 sizeof(msg));
311 WARN_ON(ret != sizeof(msg));
312
313 mbox_fifo_write(mbox, msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800314 }
315}
316
317/*
318 * Message receiver(workqueue)
319 */
320static void mbox_rx_work(struct work_struct *work)
321{
322 struct omap_mbox_queue *mq =
323 container_of(work, struct omap_mbox_queue, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800324 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000325 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800326
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000327 while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
328 len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
329 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800330
Kanigeri, Hari58256302010-11-29 20:24:14 +0000331 blocking_notifier_call_chain(&mq->mbox->notifier, len,
332 (void *)msg);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000333 spin_lock_irq(&mq->lock);
334 if (mq->full) {
335 mq->full = false;
336 omap_mbox_enable_irq(mq->mbox, IRQ_RX);
337 }
338 spin_unlock_irq(&mq->lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800339 }
340}
341
342/*
343 * Mailbox interrupt handler
344 */
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800345static void __mbox_tx_interrupt(struct omap_mbox *mbox)
346{
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800347 omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800348 ack_mbox_irq(mbox, IRQ_TX);
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800349 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800350}
351
352static void __mbox_rx_interrupt(struct omap_mbox *mbox)
353{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000354 struct omap_mbox_queue *mq = mbox->rxq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800355 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000356 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800357
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800358 while (!mbox_fifo_empty(mbox)) {
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000359 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600360 omap_mbox_disable_irq(mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000361 mq->full = true;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800362 goto nomem;
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600363 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800364
365 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800366
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000367 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
368 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800369 }
370
371 /* no more messages in the fifo. clear IRQ source. */
372 ack_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700373nomem:
Tejun Heoc4873002011-01-26 12:12:50 +0100374 schedule_work(&mbox->rxq->work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800375}
376
377static irqreturn_t mbox_interrupt(int irq, void *p)
378{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400379 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800380
381 if (is_mbox_irq(mbox, IRQ_TX))
382 __mbox_tx_interrupt(mbox);
383
384 if (is_mbox_irq(mbox, IRQ_RX))
385 __mbox_rx_interrupt(mbox);
386
387 return IRQ_HANDLED;
388}
389
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800390static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800391 void (*work) (struct work_struct *),
392 void (*tasklet)(unsigned long))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800393{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800394 struct omap_mbox_queue *mq;
395
396 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
397 if (!mq)
398 return NULL;
399
400 spin_lock_init(&mq->lock);
401
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000402 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800403 goto error;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800404
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800405 if (work)
406 INIT_WORK(&mq->work, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800407
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800408 if (tasklet)
409 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800410 return mq;
411error:
412 kfree(mq);
413 return NULL;
414}
415
416static void mbox_queue_free(struct omap_mbox_queue *q)
417{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000418 kfifo_free(&q->fifo);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800419 kfree(q);
420}
421
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800422static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800423{
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800424 int ret = 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800425 struct omap_mbox_queue *mq;
Suman Anna72c1c812014-06-24 19:43:43 -0500426 struct omap_mbox_device *mdev = mbox->parent;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800427
Suman Anna72c1c812014-06-24 19:43:43 -0500428 mutex_lock(&mdev->cfg_lock);
429 ret = pm_runtime_get_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500430 if (unlikely(ret < 0))
431 goto fail_startup;
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800432
Kanigeri, Hari58256302010-11-29 20:24:14 +0000433 if (!mbox->use_count++) {
Kanigeri, Hari58256302010-11-29 20:24:14 +0000434 mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet);
435 if (!mq) {
436 ret = -ENOMEM;
437 goto fail_alloc_txq;
438 }
439 mbox->txq = mq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800440
Kanigeri, Hari58256302010-11-29 20:24:14 +0000441 mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL);
442 if (!mq) {
443 ret = -ENOMEM;
444 goto fail_alloc_rxq;
445 }
446 mbox->rxq = mq;
447 mq->mbox = mbox;
Suman Annaecf305c2013-02-01 20:37:06 -0600448 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
449 mbox->name, mbox);
450 if (unlikely(ret)) {
451 pr_err("failed to register mailbox interrupt:%d\n",
452 ret);
453 goto fail_request_irq;
454 }
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300455
456 omap_mbox_enable_irq(mbox, IRQ_RX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800457 }
Suman Anna72c1c812014-06-24 19:43:43 -0500458 mutex_unlock(&mdev->cfg_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800459 return 0;
460
Suman Annaecf305c2013-02-01 20:37:06 -0600461fail_request_irq:
462 mbox_queue_free(mbox->rxq);
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000463fail_alloc_rxq:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800464 mbox_queue_free(mbox->txq);
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000465fail_alloc_txq:
Suman Anna72c1c812014-06-24 19:43:43 -0500466 pm_runtime_put_sync(mdev->dev);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000467 mbox->use_count--;
468fail_startup:
Suman Anna72c1c812014-06-24 19:43:43 -0500469 mutex_unlock(&mdev->cfg_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800470 return ret;
471}
472
473static void omap_mbox_fini(struct omap_mbox *mbox)
474{
Suman Anna72c1c812014-06-24 19:43:43 -0500475 struct omap_mbox_device *mdev = mbox->parent;
476
477 mutex_lock(&mdev->cfg_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800478
Kanigeri, Hari58256302010-11-29 20:24:14 +0000479 if (!--mbox->use_count) {
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300480 omap_mbox_disable_irq(mbox, IRQ_RX);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000481 free_irq(mbox->irq, mbox);
482 tasklet_kill(&mbox->txq->tasklet);
Tejun Heo43829732012-08-20 14:51:24 -0700483 flush_work(&mbox->rxq->work);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000484 mbox_queue_free(mbox->txq);
485 mbox_queue_free(mbox->rxq);
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800486 }
Kanigeri, Hari58256302010-11-29 20:24:14 +0000487
Suman Anna72c1c812014-06-24 19:43:43 -0500488 pm_runtime_put_sync(mdev->dev);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000489
Suman Anna72c1c812014-06-24 19:43:43 -0500490 mutex_unlock(&mdev->cfg_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800491}
492
Suman Anna72c1c812014-06-24 19:43:43 -0500493static struct omap_mbox *omap_mbox_device_find(struct omap_mbox_device *mdev,
494 const char *mbox_name)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800495{
Kevin Hilmanc0377322011-02-11 19:56:43 +0000496 struct omap_mbox *_mbox, *mbox = NULL;
Suman Anna72c1c812014-06-24 19:43:43 -0500497 struct omap_mbox **mboxes = mdev->mboxes;
498 int i;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800499
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000500 if (!mboxes)
Suman Anna72c1c812014-06-24 19:43:43 -0500501 return NULL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800502
Kevin Hilmanc0377322011-02-11 19:56:43 +0000503 for (i = 0; (_mbox = mboxes[i]); i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500504 if (!strcmp(_mbox->name, mbox_name)) {
Kevin Hilmanc0377322011-02-11 19:56:43 +0000505 mbox = _mbox;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000506 break;
Kevin Hilmanc0377322011-02-11 19:56:43 +0000507 }
508 }
Suman Anna72c1c812014-06-24 19:43:43 -0500509 return mbox;
510}
511
512struct omap_mbox *omap_mbox_get(const char *name, struct notifier_block *nb)
513{
514 struct omap_mbox *mbox = NULL;
515 struct omap_mbox_device *mdev;
516 int ret;
517
518 mutex_lock(&omap_mbox_devices_lock);
519 list_for_each_entry(mdev, &omap_mbox_devices, elem) {
520 mbox = omap_mbox_device_find(mdev, name);
521 if (mbox)
522 break;
523 }
524 mutex_unlock(&omap_mbox_devices_lock);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000525
526 if (!mbox)
527 return ERR_PTR(-ENOENT);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800528
Kanigeri, Hari58256302010-11-29 20:24:14 +0000529 if (nb)
530 blocking_notifier_chain_register(&mbox->notifier, nb);
531
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300532 ret = omap_mbox_startup(mbox);
533 if (ret) {
534 blocking_notifier_chain_unregister(&mbox->notifier, nb);
535 return ERR_PTR(-ENODEV);
536 }
537
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800538 return mbox;
539}
540EXPORT_SYMBOL(omap_mbox_get);
541
Kanigeri, Hari58256302010-11-29 20:24:14 +0000542void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800543{
Kanigeri, Hari58256302010-11-29 20:24:14 +0000544 blocking_notifier_chain_unregister(&mbox->notifier, nb);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800545 omap_mbox_fini(mbox);
546}
547EXPORT_SYMBOL(omap_mbox_put);
548
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300549static struct class omap_mbox_class = { .name = "mbox", };
550
Suman Anna72c1c812014-06-24 19:43:43 -0500551static int omap_mbox_register(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800552{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000553 int ret;
554 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500555 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800556
Suman Anna72c1c812014-06-24 19:43:43 -0500557 if (!mdev || !mdev->mboxes)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800558 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800559
Suman Anna72c1c812014-06-24 19:43:43 -0500560 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000561 for (i = 0; mboxes[i]; i++) {
562 struct omap_mbox *mbox = mboxes[i];
563 mbox->dev = device_create(&omap_mbox_class,
Suman Anna72c1c812014-06-24 19:43:43 -0500564 mdev->dev, 0, mbox, "%s", mbox->name);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000565 if (IS_ERR(mbox->dev)) {
566 ret = PTR_ERR(mbox->dev);
567 goto err_out;
568 }
Kanigeri, Hari58256302010-11-29 20:24:14 +0000569
570 BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700571 }
Suman Anna72c1c812014-06-24 19:43:43 -0500572
573 mutex_lock(&omap_mbox_devices_lock);
574 list_add(&mdev->elem, &omap_mbox_devices);
575 mutex_unlock(&omap_mbox_devices_lock);
576
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700577 return 0;
578
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000579err_out:
580 while (i--)
581 device_unregister(mboxes[i]->dev);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800582 return ret;
583}
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800584
Suman Anna72c1c812014-06-24 19:43:43 -0500585static int omap_mbox_unregister(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800586{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000587 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500588 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800589
Suman Anna72c1c812014-06-24 19:43:43 -0500590 if (!mdev || !mdev->mboxes)
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000591 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800592
Suman Anna72c1c812014-06-24 19:43:43 -0500593 mutex_lock(&omap_mbox_devices_lock);
594 list_del(&mdev->elem);
595 mutex_unlock(&omap_mbox_devices_lock);
596
597 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000598 for (i = 0; mboxes[i]; i++)
599 device_unregister(mboxes[i]->dev);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000600 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800601}
Suman Anna5040f532014-06-24 19:43:41 -0500602
Suman Anna75288cc2014-09-10 14:20:59 -0500603static const struct of_device_id omap_mailbox_of_match[] = {
604 {
605 .compatible = "ti,omap2-mailbox",
606 .data = (void *)MBOX_INTR_CFG_TYPE1,
607 },
608 {
609 .compatible = "ti,omap3-mailbox",
610 .data = (void *)MBOX_INTR_CFG_TYPE1,
611 },
612 {
613 .compatible = "ti,omap4-mailbox",
614 .data = (void *)MBOX_INTR_CFG_TYPE2,
615 },
616 {
617 /* end */
618 },
619};
620MODULE_DEVICE_TABLE(of, omap_mailbox_of_match);
621
Suman Anna5040f532014-06-24 19:43:41 -0500622static int omap_mbox_probe(struct platform_device *pdev)
623{
624 struct resource *mem;
625 int ret;
626 struct omap_mbox **list, *mbox, *mboxblk;
Suman Anna5040f532014-06-24 19:43:41 -0500627 struct omap_mbox_pdata *pdata = pdev->dev.platform_data;
Suman Anna75288cc2014-09-10 14:20:59 -0500628 struct omap_mbox_dev_info *info = NULL;
629 struct omap_mbox_fifo_info *finfo, *finfoblk;
Suman Anna72c1c812014-06-24 19:43:43 -0500630 struct omap_mbox_device *mdev;
Suman Annabe3322e2014-06-24 19:43:42 -0500631 struct omap_mbox_fifo *fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500632 struct device_node *node = pdev->dev.of_node;
633 struct device_node *child;
634 const struct of_device_id *match;
635 u32 intr_type, info_count;
636 u32 num_users, num_fifos;
637 u32 tmp[3];
Suman Anna5040f532014-06-24 19:43:41 -0500638 u32 l;
639 int i;
640
Suman Anna75288cc2014-09-10 14:20:59 -0500641 if (!node && (!pdata || !pdata->info_cnt || !pdata->info)) {
Suman Anna5040f532014-06-24 19:43:41 -0500642 pr_err("%s: platform not supported\n", __func__);
643 return -ENODEV;
644 }
645
Suman Anna75288cc2014-09-10 14:20:59 -0500646 if (node) {
647 match = of_match_device(omap_mailbox_of_match, &pdev->dev);
648 if (!match)
649 return -ENODEV;
650 intr_type = (u32)match->data;
651
652 if (of_property_read_u32(node, "ti,mbox-num-users",
653 &num_users))
654 return -ENODEV;
655
656 if (of_property_read_u32(node, "ti,mbox-num-fifos",
657 &num_fifos))
658 return -ENODEV;
659
660 info_count = of_get_available_child_count(node);
661 if (!info_count) {
662 dev_err(&pdev->dev, "no available mbox devices found\n");
663 return -ENODEV;
664 }
665 } else { /* non-DT device creation */
666 info_count = pdata->info_cnt;
667 info = pdata->info;
668 intr_type = pdata->intr_type;
669 num_users = pdata->num_users;
670 num_fifos = pdata->num_fifos;
671 }
672
673 finfoblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*finfoblk),
674 GFP_KERNEL);
675 if (!finfoblk)
676 return -ENOMEM;
677
678 finfo = finfoblk;
679 child = NULL;
680 for (i = 0; i < info_count; i++, finfo++) {
681 if (node) {
682 child = of_get_next_available_child(node, child);
683 ret = of_property_read_u32_array(child, "ti,mbox-tx",
684 tmp, ARRAY_SIZE(tmp));
685 if (ret)
686 return ret;
687 finfo->tx_id = tmp[0];
688 finfo->tx_irq = tmp[1];
689 finfo->tx_usr = tmp[2];
690
691 ret = of_property_read_u32_array(child, "ti,mbox-rx",
692 tmp, ARRAY_SIZE(tmp));
693 if (ret)
694 return ret;
695 finfo->rx_id = tmp[0];
696 finfo->rx_irq = tmp[1];
697 finfo->rx_usr = tmp[2];
698
699 finfo->name = child->name;
700 } else {
701 finfo->tx_id = info->tx_id;
702 finfo->rx_id = info->rx_id;
703 finfo->tx_usr = info->usr_id;
704 finfo->tx_irq = info->irq_id;
705 finfo->rx_usr = info->usr_id;
706 finfo->rx_irq = info->irq_id;
707 finfo->name = info->name;
708 info++;
709 }
710 if (finfo->tx_id >= num_fifos || finfo->rx_id >= num_fifos ||
711 finfo->tx_usr >= num_users || finfo->rx_usr >= num_users)
712 return -EINVAL;
713 }
714
Suman Anna72c1c812014-06-24 19:43:43 -0500715 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
716 if (!mdev)
717 return -ENOMEM;
718
719 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
720 mdev->mbox_base = devm_ioremap_resource(&pdev->dev, mem);
721 if (IS_ERR(mdev->mbox_base))
722 return PTR_ERR(mdev->mbox_base);
723
Suman Anna5040f532014-06-24 19:43:41 -0500724 /* allocate one extra for marking end of list */
Suman Anna75288cc2014-09-10 14:20:59 -0500725 list = devm_kzalloc(&pdev->dev, (info_count + 1) * sizeof(*list),
Suman Anna5040f532014-06-24 19:43:41 -0500726 GFP_KERNEL);
727 if (!list)
728 return -ENOMEM;
729
Suman Anna75288cc2014-09-10 14:20:59 -0500730 mboxblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*mbox),
Suman Anna5040f532014-06-24 19:43:41 -0500731 GFP_KERNEL);
732 if (!mboxblk)
733 return -ENOMEM;
734
Suman Anna5040f532014-06-24 19:43:41 -0500735 mbox = mboxblk;
Suman Anna75288cc2014-09-10 14:20:59 -0500736 finfo = finfoblk;
737 for (i = 0; i < info_count; i++, finfo++) {
Suman Annabe3322e2014-06-24 19:43:42 -0500738 fifo = &mbox->tx_fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500739 fifo->msg = MAILBOX_MESSAGE(finfo->tx_id);
740 fifo->fifo_stat = MAILBOX_FIFOSTATUS(finfo->tx_id);
741 fifo->intr_bit = MAILBOX_IRQ_NOTFULL(finfo->tx_id);
742 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->tx_usr);
743 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->tx_usr);
744 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->tx_usr);
Suman Anna5040f532014-06-24 19:43:41 -0500745
Suman Annabe3322e2014-06-24 19:43:42 -0500746 fifo = &mbox->rx_fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500747 fifo->msg = MAILBOX_MESSAGE(finfo->rx_id);
748 fifo->msg_stat = MAILBOX_MSGSTATUS(finfo->rx_id);
749 fifo->intr_bit = MAILBOX_IRQ_NEWMSG(finfo->rx_id);
750 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->rx_usr);
751 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->rx_usr);
752 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->rx_usr);
Suman Annabe3322e2014-06-24 19:43:42 -0500753
754 mbox->intr_type = intr_type;
755
Suman Anna72c1c812014-06-24 19:43:43 -0500756 mbox->parent = mdev;
Suman Anna75288cc2014-09-10 14:20:59 -0500757 mbox->name = finfo->name;
758 mbox->irq = platform_get_irq(pdev, finfo->tx_irq);
Suman Anna5040f532014-06-24 19:43:41 -0500759 if (mbox->irq < 0)
760 return mbox->irq;
761 list[i] = mbox++;
762 }
763
Suman Anna72c1c812014-06-24 19:43:43 -0500764 mutex_init(&mdev->cfg_lock);
765 mdev->dev = &pdev->dev;
Suman Anna75288cc2014-09-10 14:20:59 -0500766 mdev->num_users = num_users;
767 mdev->num_fifos = num_fifos;
Suman Anna72c1c812014-06-24 19:43:43 -0500768 mdev->mboxes = list;
769 ret = omap_mbox_register(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500770 if (ret)
771 return ret;
772
Suman Anna72c1c812014-06-24 19:43:43 -0500773 platform_set_drvdata(pdev, mdev);
774 pm_runtime_enable(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500775
Suman Anna72c1c812014-06-24 19:43:43 -0500776 ret = pm_runtime_get_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500777 if (ret < 0) {
Suman Anna72c1c812014-06-24 19:43:43 -0500778 pm_runtime_put_noidle(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500779 goto unregister;
780 }
781
782 /*
783 * just print the raw revision register, the format is not
784 * uniform across all SoCs
785 */
Suman Anna72c1c812014-06-24 19:43:43 -0500786 l = mbox_read_reg(mdev, MAILBOX_REVISION);
787 dev_info(mdev->dev, "omap mailbox rev 0x%x\n", l);
Suman Anna5040f532014-06-24 19:43:41 -0500788
Suman Anna72c1c812014-06-24 19:43:43 -0500789 ret = pm_runtime_put_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500790 if (ret < 0)
791 goto unregister;
792
Suman Anna75288cc2014-09-10 14:20:59 -0500793 devm_kfree(&pdev->dev, finfoblk);
Suman Anna5040f532014-06-24 19:43:41 -0500794 return 0;
795
796unregister:
Suman Anna72c1c812014-06-24 19:43:43 -0500797 pm_runtime_disable(mdev->dev);
798 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500799 return ret;
800}
801
802static int omap_mbox_remove(struct platform_device *pdev)
803{
Suman Anna72c1c812014-06-24 19:43:43 -0500804 struct omap_mbox_device *mdev = platform_get_drvdata(pdev);
805
806 pm_runtime_disable(mdev->dev);
807 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500808
809 return 0;
810}
811
812static struct platform_driver omap_mbox_driver = {
813 .probe = omap_mbox_probe,
814 .remove = omap_mbox_remove,
815 .driver = {
816 .name = "omap-mailbox",
817 .owner = THIS_MODULE,
Suman Anna75288cc2014-09-10 14:20:59 -0500818 .of_match_table = of_match_ptr(omap_mailbox_of_match),
Suman Anna5040f532014-06-24 19:43:41 -0500819 },
820};
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800821
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800822static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800823{
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300824 int err;
825
826 err = class_register(&omap_mbox_class);
827 if (err)
828 return err;
829
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000830 /* kfifo size sanity check: alignment and minimal size */
831 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000832 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
833 sizeof(mbox_msg_t));
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000834
Suman Anna5040f532014-06-24 19:43:41 -0500835 return platform_driver_register(&omap_mbox_driver);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800836}
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300837subsys_initcall(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800838
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800839static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800840{
Suman Anna5040f532014-06-24 19:43:41 -0500841 platform_driver_unregister(&omap_mbox_driver);
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300842 class_unregister(&omap_mbox_class);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800843}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800844module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800845
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700846MODULE_LICENSE("GPL v2");
847MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
Ohad Ben-Cohenf3753252010-05-05 15:33:07 +0000848MODULE_AUTHOR("Toshihiro Kobayashi");
849MODULE_AUTHOR("Hiroshi DOYU");