Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / mailbox / mailbox-sti.c
bloba334db5c9f1c126939873555671302b97ebecae9
1 /*
2 * STi Mailbox
4 * Copyright (C) 2015 ST Microelectronics
6 * Author: Lee Jones <lee.jones@linaro.org> for ST Microelectronics
8 * Based on the original driver written by;
9 * Alexandre Torgue, Olivier Lebreton and Loic Pallardy
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <linux/err.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/mailbox_controller.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
28 #include "mailbox.h"
30 #define STI_MBOX_INST_MAX 4 /* RAM saving: Max supported instances */
31 #define STI_MBOX_CHAN_MAX 20 /* RAM saving: Max supported channels */
33 #define STI_IRQ_VAL_OFFSET 0x04 /* Read interrupt status */
34 #define STI_IRQ_SET_OFFSET 0x24 /* Generate a Tx channel interrupt */
35 #define STI_IRQ_CLR_OFFSET 0x44 /* Clear pending Rx interrupts */
36 #define STI_ENA_VAL_OFFSET 0x64 /* Read enable status */
37 #define STI_ENA_SET_OFFSET 0x84 /* Enable a channel */
38 #define STI_ENA_CLR_OFFSET 0xa4 /* Disable a channel */
40 #define MBOX_BASE(mdev, inst) ((mdev)->base + ((inst) * 4))
42 /**
43 * STi Mailbox device data
45 * An IP Mailbox is currently composed of 4 instances
46 * Each instance is currently composed of 32 channels
47 * This means that we have 128 channels per Mailbox
48 * A channel an be used for TX or RX
50 * @dev: Device to which it is attached
51 * @mbox: Representation of a communication channel controller
52 * @base: Base address of the register mapping region
53 * @name: Name of the mailbox
54 * @enabled: Local copy of enabled channels
55 * @lock: Mutex protecting enabled status
57 struct sti_mbox_device {
58 struct device *dev;
59 struct mbox_controller *mbox;
60 void __iomem *base;
61 const char *name;
62 u32 enabled[STI_MBOX_INST_MAX];
63 spinlock_t lock;
66 /**
67 * STi Mailbox platform specific configuration
69 * @num_inst: Maximum number of instances in one HW Mailbox
70 * @num_chan: Maximum number of channel per instance
72 struct sti_mbox_pdata {
73 unsigned int num_inst;
74 unsigned int num_chan;
77 /**
78 * STi Mailbox allocated channel information
80 * @mdev: Pointer to parent Mailbox device
81 * @instance: Instance number channel resides in
82 * @channel: Channel number pertaining to this container
84 struct sti_channel {
85 struct sti_mbox_device *mdev;
86 unsigned int instance;
87 unsigned int channel;
90 static inline bool sti_mbox_channel_is_enabled(struct mbox_chan *chan)
92 struct sti_channel *chan_info = chan->con_priv;
93 struct sti_mbox_device *mdev = chan_info->mdev;
94 unsigned int instance = chan_info->instance;
95 unsigned int channel = chan_info->channel;
97 return mdev->enabled[instance] & BIT(channel);
100 static inline
101 struct mbox_chan *sti_mbox_to_channel(struct mbox_controller *mbox,
102 unsigned int instance,
103 unsigned int channel)
105 struct sti_channel *chan_info;
106 int i;
108 for (i = 0; i < mbox->num_chans; i++) {
109 chan_info = mbox->chans[i].con_priv;
110 if (chan_info &&
111 chan_info->instance == instance &&
112 chan_info->channel == channel)
113 return &mbox->chans[i];
116 dev_err(mbox->dev,
117 "Channel not registered: instance: %d channel: %d\n",
118 instance, channel);
120 return NULL;
123 static void sti_mbox_enable_channel(struct mbox_chan *chan)
125 struct sti_channel *chan_info = chan->con_priv;
126 struct sti_mbox_device *mdev = chan_info->mdev;
127 unsigned int instance = chan_info->instance;
128 unsigned int channel = chan_info->channel;
129 unsigned long flags;
130 void __iomem *base = MBOX_BASE(mdev, instance);
132 spin_lock_irqsave(&mdev->lock, flags);
133 mdev->enabled[instance] |= BIT(channel);
134 writel_relaxed(BIT(channel), base + STI_ENA_SET_OFFSET);
135 spin_unlock_irqrestore(&mdev->lock, flags);
138 static void sti_mbox_disable_channel(struct mbox_chan *chan)
140 struct sti_channel *chan_info = chan->con_priv;
141 struct sti_mbox_device *mdev = chan_info->mdev;
142 unsigned int instance = chan_info->instance;
143 unsigned int channel = chan_info->channel;
144 unsigned long flags;
145 void __iomem *base = MBOX_BASE(mdev, instance);
147 spin_lock_irqsave(&mdev->lock, flags);
148 mdev->enabled[instance] &= ~BIT(channel);
149 writel_relaxed(BIT(channel), base + STI_ENA_CLR_OFFSET);
150 spin_unlock_irqrestore(&mdev->lock, flags);
153 static void sti_mbox_clear_irq(struct mbox_chan *chan)
155 struct sti_channel *chan_info = chan->con_priv;
156 struct sti_mbox_device *mdev = chan_info->mdev;
157 unsigned int instance = chan_info->instance;
158 unsigned int channel = chan_info->channel;
159 void __iomem *base = MBOX_BASE(mdev, instance);
161 writel_relaxed(BIT(channel), base + STI_IRQ_CLR_OFFSET);
164 static struct mbox_chan *sti_mbox_irq_to_channel(struct sti_mbox_device *mdev,
165 unsigned int instance)
167 struct mbox_controller *mbox = mdev->mbox;
168 struct mbox_chan *chan = NULL;
169 unsigned int channel;
170 unsigned long bits;
171 void __iomem *base = MBOX_BASE(mdev, instance);
173 bits = readl_relaxed(base + STI_IRQ_VAL_OFFSET);
174 if (!bits)
175 /* No IRQs fired in specified instance */
176 return NULL;
178 /* An IRQ has fired, find the associated channel */
179 for (channel = 0; bits; channel++) {
180 if (!test_and_clear_bit(channel, &bits))
181 continue;
183 chan = sti_mbox_to_channel(mbox, instance, channel);
184 if (chan) {
185 dev_dbg(mbox->dev,
186 "IRQ fired on instance: %d channel: %d\n",
187 instance, channel);
188 break;
192 return chan;
195 static irqreturn_t sti_mbox_thread_handler(int irq, void *data)
197 struct sti_mbox_device *mdev = data;
198 struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);
199 struct mbox_chan *chan;
200 unsigned int instance;
202 for (instance = 0; instance < pdata->num_inst; instance++) {
203 keep_looking:
204 chan = sti_mbox_irq_to_channel(mdev, instance);
205 if (!chan)
206 continue;
208 mbox_chan_received_data(chan, NULL);
209 sti_mbox_clear_irq(chan);
210 sti_mbox_enable_channel(chan);
211 goto keep_looking;
214 return IRQ_HANDLED;
217 static irqreturn_t sti_mbox_irq_handler(int irq, void *data)
219 struct sti_mbox_device *mdev = data;
220 struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);
221 struct sti_channel *chan_info;
222 struct mbox_chan *chan;
223 unsigned int instance;
224 int ret = IRQ_NONE;
226 for (instance = 0; instance < pdata->num_inst; instance++) {
227 chan = sti_mbox_irq_to_channel(mdev, instance);
228 if (!chan)
229 continue;
230 chan_info = chan->con_priv;
232 if (!sti_mbox_channel_is_enabled(chan)) {
233 dev_warn(mdev->dev,
234 "Unexpected IRQ: %s\n"
235 " instance: %d: channel: %d [enabled: %x]\n",
236 mdev->name, chan_info->instance,
237 chan_info->channel, mdev->enabled[instance]);
239 /* Only handle IRQ if no other valid IRQs were found */
240 if (ret == IRQ_NONE)
241 ret = IRQ_HANDLED;
242 continue;
245 sti_mbox_disable_channel(chan);
246 ret = IRQ_WAKE_THREAD;
249 if (ret == IRQ_NONE)
250 dev_err(mdev->dev, "Spurious IRQ - was a channel requested?\n");
252 return ret;
255 static bool sti_mbox_tx_is_ready(struct mbox_chan *chan)
257 struct sti_channel *chan_info = chan->con_priv;
258 struct sti_mbox_device *mdev = chan_info->mdev;
259 unsigned int instance = chan_info->instance;
260 unsigned int channel = chan_info->channel;
261 void __iomem *base = MBOX_BASE(mdev, instance);
263 if (!(readl_relaxed(base + STI_ENA_VAL_OFFSET) & BIT(channel))) {
264 dev_dbg(mdev->dev, "Mbox: %s: inst: %d, chan: %d disabled\n",
265 mdev->name, instance, channel);
266 return false;
269 if (readl_relaxed(base + STI_IRQ_VAL_OFFSET) & BIT(channel)) {
270 dev_dbg(mdev->dev, "Mbox: %s: inst: %d, chan: %d not ready\n",
271 mdev->name, instance, channel);
272 return false;
275 return true;
278 static int sti_mbox_send_data(struct mbox_chan *chan, void *data)
280 struct sti_channel *chan_info = chan->con_priv;
281 struct sti_mbox_device *mdev = chan_info->mdev;
282 unsigned int instance = chan_info->instance;
283 unsigned int channel = chan_info->channel;
284 void __iomem *base = MBOX_BASE(mdev, instance);
286 /* Send event to co-processor */
287 writel_relaxed(BIT(channel), base + STI_IRQ_SET_OFFSET);
289 dev_dbg(mdev->dev,
290 "Sent via Mailbox %s: instance: %d channel: %d\n",
291 mdev->name, instance, channel);
293 return 0;
296 static int sti_mbox_startup_chan(struct mbox_chan *chan)
298 sti_mbox_clear_irq(chan);
299 sti_mbox_enable_channel(chan);
301 return 0;
304 static void sti_mbox_shutdown_chan(struct mbox_chan *chan)
306 struct sti_channel *chan_info = chan->con_priv;
307 struct mbox_controller *mbox = chan_info->mdev->mbox;
308 int i;
310 for (i = 0; i < mbox->num_chans; i++)
311 if (chan == &mbox->chans[i])
312 break;
314 if (mbox->num_chans == i) {
315 dev_warn(mbox->dev, "Request to free non-existent channel\n");
316 return;
319 /* Reset channel */
320 sti_mbox_disable_channel(chan);
321 sti_mbox_clear_irq(chan);
322 chan->con_priv = NULL;
325 static struct mbox_chan *sti_mbox_xlate(struct mbox_controller *mbox,
326 const struct of_phandle_args *spec)
328 struct sti_mbox_device *mdev = dev_get_drvdata(mbox->dev);
329 struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);
330 struct sti_channel *chan_info;
331 struct mbox_chan *chan = NULL;
332 unsigned int instance = spec->args[0];
333 unsigned int channel = spec->args[1];
334 int i;
336 /* Bounds checking */
337 if (instance >= pdata->num_inst || channel >= pdata->num_chan) {
338 dev_err(mbox->dev,
339 "Invalid channel requested instance: %d channel: %d\n",
340 instance, channel);
341 return ERR_PTR(-EINVAL);
344 for (i = 0; i < mbox->num_chans; i++) {
345 chan_info = mbox->chans[i].con_priv;
347 /* Is requested channel free? */
348 if (chan_info &&
349 mbox->dev == chan_info->mdev->dev &&
350 instance == chan_info->instance &&
351 channel == chan_info->channel) {
353 dev_err(mbox->dev, "Channel in use\n");
354 return ERR_PTR(-EBUSY);
358 * Find the first free slot, then continue checking
359 * to see if requested channel is in use
361 if (!chan && !chan_info)
362 chan = &mbox->chans[i];
365 if (!chan) {
366 dev_err(mbox->dev, "No free channels left\n");
367 return ERR_PTR(-EBUSY);
370 chan_info = devm_kzalloc(mbox->dev, sizeof(*chan_info), GFP_KERNEL);
371 if (!chan_info)
372 return ERR_PTR(-ENOMEM);
374 chan_info->mdev = mdev;
375 chan_info->instance = instance;
376 chan_info->channel = channel;
378 chan->con_priv = chan_info;
380 dev_info(mbox->dev,
381 "Mbox: %s: Created channel: instance: %d channel: %d\n",
382 mdev->name, instance, channel);
384 return chan;
387 static const struct mbox_chan_ops sti_mbox_ops = {
388 .startup = sti_mbox_startup_chan,
389 .shutdown = sti_mbox_shutdown_chan,
390 .send_data = sti_mbox_send_data,
391 .last_tx_done = sti_mbox_tx_is_ready,
394 static const struct sti_mbox_pdata mbox_stih407_pdata = {
395 .num_inst = 4,
396 .num_chan = 32,
399 static const struct of_device_id sti_mailbox_match[] = {
401 .compatible = "st,stih407-mailbox",
402 .data = (void *)&mbox_stih407_pdata
407 static int sti_mbox_probe(struct platform_device *pdev)
409 const struct of_device_id *match;
410 struct mbox_controller *mbox;
411 struct sti_mbox_device *mdev;
412 struct device_node *np = pdev->dev.of_node;
413 struct mbox_chan *chans;
414 struct resource *res;
415 int irq;
416 int ret;
418 match = of_match_device(sti_mailbox_match, &pdev->dev);
419 if (!match) {
420 dev_err(&pdev->dev, "No configuration found\n");
421 return -ENODEV;
423 pdev->dev.platform_data = (struct sti_mbox_pdata *) match->data;
425 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
426 if (!mdev)
427 return -ENOMEM;
429 platform_set_drvdata(pdev, mdev);
431 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
432 mdev->base = devm_ioremap_resource(&pdev->dev, res);
433 if (IS_ERR(mdev->base))
434 return PTR_ERR(mdev->base);
436 ret = of_property_read_string(np, "mbox-name", &mdev->name);
437 if (ret)
438 mdev->name = np->full_name;
440 mbox = devm_kzalloc(&pdev->dev, sizeof(*mbox), GFP_KERNEL);
441 if (!mbox)
442 return -ENOMEM;
444 chans = devm_kzalloc(&pdev->dev,
445 sizeof(*chans) * STI_MBOX_CHAN_MAX, GFP_KERNEL);
446 if (!chans)
447 return -ENOMEM;
449 mdev->dev = &pdev->dev;
450 mdev->mbox = mbox;
452 spin_lock_init(&mdev->lock);
454 /* STi Mailbox does not have a Tx-Done or Tx-Ready IRQ */
455 mbox->txdone_irq = false;
456 mbox->txdone_poll = true;
457 mbox->txpoll_period = 100;
458 mbox->ops = &sti_mbox_ops;
459 mbox->dev = mdev->dev;
460 mbox->of_xlate = sti_mbox_xlate;
461 mbox->chans = chans;
462 mbox->num_chans = STI_MBOX_CHAN_MAX;
464 ret = mbox_controller_register(mbox);
465 if (ret)
466 return ret;
468 /* It's okay for Tx Mailboxes to not supply IRQs */
469 irq = platform_get_irq(pdev, 0);
470 if (irq < 0) {
471 dev_info(&pdev->dev,
472 "%s: Registered Tx only Mailbox\n", mdev->name);
473 return 0;
476 ret = devm_request_threaded_irq(&pdev->dev, irq,
477 sti_mbox_irq_handler,
478 sti_mbox_thread_handler,
479 IRQF_ONESHOT, mdev->name, mdev);
480 if (ret) {
481 dev_err(&pdev->dev, "Can't claim IRQ %d\n", irq);
482 mbox_controller_unregister(mbox);
483 return -EINVAL;
486 dev_info(&pdev->dev, "%s: Registered Tx/Rx Mailbox\n", mdev->name);
488 return 0;
491 static int sti_mbox_remove(struct platform_device *pdev)
493 struct sti_mbox_device *mdev = platform_get_drvdata(pdev);
495 mbox_controller_unregister(mdev->mbox);
497 return 0;
500 static struct platform_driver sti_mbox_driver = {
501 .probe = sti_mbox_probe,
502 .remove = sti_mbox_remove,
503 .driver = {
504 .name = "sti-mailbox",
505 .of_match_table = sti_mailbox_match,
508 module_platform_driver(sti_mbox_driver);
510 MODULE_LICENSE("GPL");
511 MODULE_DESCRIPTION("STMicroelectronics Mailbox Controller");
512 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
513 MODULE_ALIAS("platform:mailbox-sti");