Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 ST Microelectronics |
| 3 | * |
| 4 | * Author: Lee Jones <lee.jones@linaro.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/debugfs.h> |
| 13 | #include <linux/err.h> |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 14 | #include <linux/fs.h> |
Lee Jones | a133f8b | 2015-10-20 17:11:27 +0100 | [diff] [blame] | 15 | #include <linux/io.h> |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/mailbox_client.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/platform_device.h> |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 21 | #include <linux/poll.h> |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/uaccess.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 24 | #include <linux/sched/signal.h> |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 25 | |
| 26 | #define MBOX_MAX_SIG_LEN 8 |
| 27 | #define MBOX_MAX_MSG_LEN 128 |
| 28 | #define MBOX_BYTES_PER_LINE 16 |
Lee Jones | 6c03663 | 2015-10-16 13:32:47 +0100 | [diff] [blame] | 29 | #define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2) |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 30 | #define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \ |
| 31 | (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE)) |
| 32 | |
| 33 | static struct dentry *root_debugfs_dir; |
| 34 | |
| 35 | struct mbox_test_device { |
| 36 | struct device *dev; |
Sudeep Holla | 2d74ffd | 2016-02-19 16:01:18 +0000 | [diff] [blame] | 37 | void __iomem *tx_mmio; |
| 38 | void __iomem *rx_mmio; |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 39 | struct mbox_chan *tx_channel; |
| 40 | struct mbox_chan *rx_channel; |
| 41 | char *rx_buffer; |
| 42 | char *signal; |
| 43 | char *message; |
| 44 | spinlock_t lock; |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 45 | wait_queue_head_t waitq; |
| 46 | struct fasync_struct *async_queue; |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static ssize_t mbox_test_signal_write(struct file *filp, |
| 50 | const char __user *userbuf, |
| 51 | size_t count, loff_t *ppos) |
| 52 | { |
| 53 | struct mbox_test_device *tdev = filp->private_data; |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 54 | |
| 55 | if (!tdev->tx_channel) { |
| 56 | dev_err(tdev->dev, "Channel cannot do Tx\n"); |
| 57 | return -EINVAL; |
| 58 | } |
| 59 | |
| 60 | if (count > MBOX_MAX_SIG_LEN) { |
| 61 | dev_err(tdev->dev, |
Lee Jones | 6c03663 | 2015-10-16 13:32:47 +0100 | [diff] [blame] | 62 | "Signal length %zd greater than max allowed %d\n", |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 63 | count, MBOX_MAX_SIG_LEN); |
| 64 | return -EINVAL; |
| 65 | } |
| 66 | |
Lee Jones | d1c2f87 | 2016-03-23 14:43:42 +0000 | [diff] [blame] | 67 | /* Only allocate memory if we need to */ |
| 68 | if (!tdev->signal) { |
| 69 | tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL); |
| 70 | if (!tdev->signal) |
| 71 | return -ENOMEM; |
| 72 | } |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 73 | |
Lee Jones | 17f5f28 | 2016-03-23 14:43:41 +0000 | [diff] [blame] | 74 | if (copy_from_user(tdev->signal, userbuf, count)) { |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 75 | kfree(tdev->signal); |
Lee Jones | 17f5f28 | 2016-03-23 14:43:41 +0000 | [diff] [blame] | 76 | tdev->signal = NULL; |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 77 | return -EFAULT; |
| 78 | } |
| 79 | |
Lee Jones | 17f5f28 | 2016-03-23 14:43:41 +0000 | [diff] [blame] | 80 | return count; |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static const struct file_operations mbox_test_signal_ops = { |
| 84 | .write = mbox_test_signal_write, |
| 85 | .open = simple_open, |
| 86 | .llseek = generic_file_llseek, |
| 87 | }; |
| 88 | |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 89 | static int mbox_test_message_fasync(int fd, struct file *filp, int on) |
| 90 | { |
| 91 | struct mbox_test_device *tdev = filp->private_data; |
| 92 | |
| 93 | return fasync_helper(fd, filp, on, &tdev->async_queue); |
| 94 | } |
| 95 | |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 96 | static ssize_t mbox_test_message_write(struct file *filp, |
| 97 | const char __user *userbuf, |
| 98 | size_t count, loff_t *ppos) |
| 99 | { |
| 100 | struct mbox_test_device *tdev = filp->private_data; |
| 101 | void *data; |
| 102 | int ret; |
| 103 | |
| 104 | if (!tdev->tx_channel) { |
| 105 | dev_err(tdev->dev, "Channel cannot do Tx\n"); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | |
| 109 | if (count > MBOX_MAX_MSG_LEN) { |
| 110 | dev_err(tdev->dev, |
Lee Jones | 6c03663 | 2015-10-16 13:32:47 +0100 | [diff] [blame] | 111 | "Message length %zd greater than max allowed %d\n", |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 112 | count, MBOX_MAX_MSG_LEN); |
| 113 | return -EINVAL; |
| 114 | } |
| 115 | |
| 116 | tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL); |
| 117 | if (!tdev->message) |
| 118 | return -ENOMEM; |
| 119 | |
| 120 | ret = copy_from_user(tdev->message, userbuf, count); |
| 121 | if (ret) { |
| 122 | ret = -EFAULT; |
| 123 | goto out; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * A separate signal is only of use if there is |
| 128 | * MMIO to subsequently pass the message through |
| 129 | */ |
Sudeep Holla | 2d74ffd | 2016-02-19 16:01:18 +0000 | [diff] [blame] | 130 | if (tdev->tx_mmio && tdev->signal) { |
Sudeep Holla | 27fa680 | 2016-02-19 16:01:17 +0000 | [diff] [blame] | 131 | print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS, |
| 132 | tdev->signal, MBOX_MAX_SIG_LEN); |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 133 | |
| 134 | data = tdev->signal; |
| 135 | } else |
| 136 | data = tdev->message; |
| 137 | |
Sudeep Holla | 27fa680 | 2016-02-19 16:01:17 +0000 | [diff] [blame] | 138 | print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS, |
| 139 | tdev->message, MBOX_MAX_MSG_LEN); |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 140 | |
| 141 | ret = mbox_send_message(tdev->tx_channel, data); |
| 142 | if (ret < 0) |
| 143 | dev_err(tdev->dev, "Failed to send message via mailbox\n"); |
| 144 | |
| 145 | out: |
| 146 | kfree(tdev->signal); |
| 147 | kfree(tdev->message); |
Sudeep Holla | 9ef3c51 | 2016-05-24 17:12:04 +0100 | [diff] [blame] | 148 | tdev->signal = NULL; |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 149 | |
| 150 | return ret < 0 ? ret : count; |
| 151 | } |
| 152 | |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 153 | static bool mbox_test_message_data_ready(struct mbox_test_device *tdev) |
| 154 | { |
| 155 | unsigned char data; |
| 156 | unsigned long flags; |
| 157 | |
| 158 | spin_lock_irqsave(&tdev->lock, flags); |
| 159 | data = tdev->rx_buffer[0]; |
| 160 | spin_unlock_irqrestore(&tdev->lock, flags); |
| 161 | |
| 162 | if (data != '\0') |
| 163 | return true; |
| 164 | return false; |
| 165 | } |
| 166 | |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 167 | static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf, |
| 168 | size_t count, loff_t *ppos) |
| 169 | { |
| 170 | struct mbox_test_device *tdev = filp->private_data; |
| 171 | unsigned long flags; |
| 172 | char *touser, *ptr; |
| 173 | int l = 0; |
| 174 | int ret; |
| 175 | |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 176 | DECLARE_WAITQUEUE(wait, current); |
| 177 | |
Dan Carpenter | c3ac54a | 2015-10-22 22:51:27 +0300 | [diff] [blame] | 178 | touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL); |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 179 | if (!touser) |
| 180 | return -ENOMEM; |
| 181 | |
| 182 | if (!tdev->rx_channel) { |
| 183 | ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n"); |
| 184 | ret = simple_read_from_buffer(userbuf, count, ppos, |
| 185 | touser, ret); |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 186 | goto kfree_err; |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 189 | add_wait_queue(&tdev->waitq, &wait); |
| 190 | |
| 191 | do { |
| 192 | __set_current_state(TASK_INTERRUPTIBLE); |
| 193 | |
| 194 | if (mbox_test_message_data_ready(tdev)) |
| 195 | break; |
| 196 | |
| 197 | if (filp->f_flags & O_NONBLOCK) { |
| 198 | ret = -EAGAIN; |
| 199 | goto waitq_err; |
| 200 | } |
| 201 | |
| 202 | if (signal_pending(current)) { |
| 203 | ret = -ERESTARTSYS; |
| 204 | goto waitq_err; |
| 205 | } |
| 206 | schedule(); |
| 207 | |
| 208 | } while (1); |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 209 | |
| 210 | spin_lock_irqsave(&tdev->lock, flags); |
| 211 | |
| 212 | ptr = tdev->rx_buffer; |
| 213 | while (l < MBOX_HEXDUMP_MAX_LEN) { |
| 214 | hex_dump_to_buffer(ptr, |
| 215 | MBOX_BYTES_PER_LINE, |
| 216 | MBOX_BYTES_PER_LINE, 1, touser + l, |
| 217 | MBOX_HEXDUMP_LINE_LEN, true); |
| 218 | |
| 219 | ptr += MBOX_BYTES_PER_LINE; |
| 220 | l += MBOX_HEXDUMP_LINE_LEN; |
| 221 | *(touser + (l - 1)) = '\n'; |
| 222 | } |
| 223 | *(touser + l) = '\0'; |
| 224 | |
| 225 | memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN); |
| 226 | |
| 227 | spin_unlock_irqrestore(&tdev->lock, flags); |
| 228 | |
| 229 | ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN); |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 230 | waitq_err: |
| 231 | __set_current_state(TASK_RUNNING); |
| 232 | remove_wait_queue(&tdev->waitq, &wait); |
| 233 | kfree_err: |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 234 | kfree(touser); |
| 235 | return ret; |
| 236 | } |
| 237 | |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 238 | static unsigned int |
| 239 | mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait) |
| 240 | { |
| 241 | struct mbox_test_device *tdev = filp->private_data; |
| 242 | |
| 243 | poll_wait(filp, &tdev->waitq, wait); |
| 244 | |
| 245 | if (mbox_test_message_data_ready(tdev)) |
| 246 | return POLLIN | POLLRDNORM; |
| 247 | return 0; |
| 248 | } |
| 249 | |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 250 | static const struct file_operations mbox_test_message_ops = { |
| 251 | .write = mbox_test_message_write, |
| 252 | .read = mbox_test_message_read, |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 253 | .fasync = mbox_test_message_fasync, |
| 254 | .poll = mbox_test_message_poll, |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 255 | .open = simple_open, |
| 256 | .llseek = generic_file_llseek, |
| 257 | }; |
| 258 | |
| 259 | static int mbox_test_add_debugfs(struct platform_device *pdev, |
| 260 | struct mbox_test_device *tdev) |
| 261 | { |
| 262 | if (!debugfs_initialized()) |
| 263 | return 0; |
| 264 | |
| 265 | root_debugfs_dir = debugfs_create_dir("mailbox", NULL); |
| 266 | if (!root_debugfs_dir) { |
| 267 | dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n"); |
| 268 | return -EINVAL; |
| 269 | } |
| 270 | |
| 271 | debugfs_create_file("message", 0600, root_debugfs_dir, |
| 272 | tdev, &mbox_test_message_ops); |
| 273 | |
| 274 | debugfs_create_file("signal", 0200, root_debugfs_dir, |
| 275 | tdev, &mbox_test_signal_ops); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static void mbox_test_receive_message(struct mbox_client *client, void *message) |
| 281 | { |
| 282 | struct mbox_test_device *tdev = dev_get_drvdata(client->dev); |
| 283 | unsigned long flags; |
| 284 | |
| 285 | spin_lock_irqsave(&tdev->lock, flags); |
Sudeep Holla | 2d74ffd | 2016-02-19 16:01:18 +0000 | [diff] [blame] | 286 | if (tdev->rx_mmio) { |
| 287 | memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN); |
Sudeep Holla | 27fa680 | 2016-02-19 16:01:17 +0000 | [diff] [blame] | 288 | print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS, |
| 289 | tdev->rx_buffer, MBOX_MAX_MSG_LEN); |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 290 | } else if (message) { |
Sudeep Holla | 27fa680 | 2016-02-19 16:01:17 +0000 | [diff] [blame] | 291 | print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS, |
| 292 | message, MBOX_MAX_MSG_LEN); |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 293 | memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN); |
| 294 | } |
| 295 | spin_unlock_irqrestore(&tdev->lock, flags); |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 296 | |
| 297 | wake_up_interruptible(&tdev->waitq); |
| 298 | |
| 299 | kill_fasync(&tdev->async_queue, SIGIO, POLL_IN); |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static void mbox_test_prepare_message(struct mbox_client *client, void *message) |
| 303 | { |
| 304 | struct mbox_test_device *tdev = dev_get_drvdata(client->dev); |
| 305 | |
Sudeep Holla | 2d74ffd | 2016-02-19 16:01:18 +0000 | [diff] [blame] | 306 | if (tdev->tx_mmio) { |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 307 | if (tdev->signal) |
Sudeep Holla | 2d74ffd | 2016-02-19 16:01:18 +0000 | [diff] [blame] | 308 | memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN); |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 309 | else |
Sudeep Holla | 2d74ffd | 2016-02-19 16:01:18 +0000 | [diff] [blame] | 310 | memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN); |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | static void mbox_test_message_sent(struct mbox_client *client, |
| 315 | void *message, int r) |
| 316 | { |
| 317 | if (r) |
| 318 | dev_warn(client->dev, |
| 319 | "Client: Message could not be sent: %d\n", r); |
| 320 | else |
| 321 | dev_info(client->dev, |
| 322 | "Client: Message sent\n"); |
| 323 | } |
| 324 | |
| 325 | static struct mbox_chan * |
| 326 | mbox_test_request_channel(struct platform_device *pdev, const char *name) |
| 327 | { |
| 328 | struct mbox_client *client; |
| 329 | struct mbox_chan *channel; |
| 330 | |
| 331 | client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL); |
| 332 | if (!client) |
| 333 | return ERR_PTR(-ENOMEM); |
| 334 | |
| 335 | client->dev = &pdev->dev; |
| 336 | client->rx_callback = mbox_test_receive_message; |
| 337 | client->tx_prepare = mbox_test_prepare_message; |
| 338 | client->tx_done = mbox_test_message_sent; |
| 339 | client->tx_block = true; |
| 340 | client->knows_txdone = false; |
| 341 | client->tx_tout = 500; |
| 342 | |
| 343 | channel = mbox_request_channel_byname(client, name); |
| 344 | if (IS_ERR(channel)) { |
| 345 | dev_warn(&pdev->dev, "Failed to request %s channel\n", name); |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | return channel; |
| 350 | } |
| 351 | |
| 352 | static int mbox_test_probe(struct platform_device *pdev) |
| 353 | { |
| 354 | struct mbox_test_device *tdev; |
| 355 | struct resource *res; |
Sudeep Holla | db4d22c | 2016-11-29 14:37:05 +0000 | [diff] [blame] | 356 | resource_size_t size; |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 357 | int ret; |
| 358 | |
| 359 | tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL); |
| 360 | if (!tdev) |
| 361 | return -ENOMEM; |
| 362 | |
| 363 | /* It's okay for MMIO to be NULL */ |
| 364 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Sudeep Holla | db4d22c | 2016-11-29 14:37:05 +0000 | [diff] [blame] | 365 | size = resource_size(res); |
Sudeep Holla | 2d74ffd | 2016-02-19 16:01:18 +0000 | [diff] [blame] | 366 | tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res); |
Sudeep Holla | db4d22c | 2016-11-29 14:37:05 +0000 | [diff] [blame] | 367 | if (PTR_ERR(tdev->tx_mmio) == -EBUSY) |
| 368 | /* if reserved area in SRAM, try just ioremap */ |
| 369 | tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size); |
| 370 | else if (IS_ERR(tdev->tx_mmio)) |
Sudeep Holla | 2d74ffd | 2016-02-19 16:01:18 +0000 | [diff] [blame] | 371 | tdev->tx_mmio = NULL; |
| 372 | |
| 373 | /* If specified, second reg entry is Rx MMIO */ |
| 374 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Sudeep Holla | db4d22c | 2016-11-29 14:37:05 +0000 | [diff] [blame] | 375 | size = resource_size(res); |
Sudeep Holla | 2d74ffd | 2016-02-19 16:01:18 +0000 | [diff] [blame] | 376 | tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res); |
Sudeep Holla | db4d22c | 2016-11-29 14:37:05 +0000 | [diff] [blame] | 377 | if (PTR_ERR(tdev->rx_mmio) == -EBUSY) |
| 378 | tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size); |
| 379 | else if (IS_ERR(tdev->rx_mmio)) |
Sudeep Holla | 2d74ffd | 2016-02-19 16:01:18 +0000 | [diff] [blame] | 380 | tdev->rx_mmio = tdev->tx_mmio; |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 381 | |
| 382 | tdev->tx_channel = mbox_test_request_channel(pdev, "tx"); |
| 383 | tdev->rx_channel = mbox_test_request_channel(pdev, "rx"); |
| 384 | |
Lee Jones | 6c03663 | 2015-10-16 13:32:47 +0100 | [diff] [blame] | 385 | if (!tdev->tx_channel && !tdev->rx_channel) |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 386 | return -EPROBE_DEFER; |
| 387 | |
Sudeep Holla | 2d74ffd | 2016-02-19 16:01:18 +0000 | [diff] [blame] | 388 | /* If Rx is not specified but has Rx MMIO, then Rx = Tx */ |
| 389 | if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio)) |
| 390 | tdev->rx_channel = tdev->tx_channel; |
| 391 | |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 392 | tdev->dev = &pdev->dev; |
| 393 | platform_set_drvdata(pdev, tdev); |
| 394 | |
| 395 | spin_lock_init(&tdev->lock); |
| 396 | |
| 397 | if (tdev->rx_channel) { |
| 398 | tdev->rx_buffer = devm_kzalloc(&pdev->dev, |
| 399 | MBOX_MAX_MSG_LEN, GFP_KERNEL); |
| 400 | if (!tdev->rx_buffer) |
| 401 | return -ENOMEM; |
| 402 | } |
| 403 | |
| 404 | ret = mbox_test_add_debugfs(pdev, tdev); |
| 405 | if (ret) |
| 406 | return ret; |
| 407 | |
Sudeep Holla | baef9a3 | 2016-11-29 14:37:04 +0000 | [diff] [blame] | 408 | init_waitqueue_head(&tdev->waitq); |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 409 | dev_info(&pdev->dev, "Successfully registered\n"); |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int mbox_test_remove(struct platform_device *pdev) |
| 415 | { |
| 416 | struct mbox_test_device *tdev = platform_get_drvdata(pdev); |
| 417 | |
| 418 | debugfs_remove_recursive(root_debugfs_dir); |
| 419 | |
| 420 | if (tdev->tx_channel) |
| 421 | mbox_free_channel(tdev->tx_channel); |
| 422 | if (tdev->rx_channel) |
| 423 | mbox_free_channel(tdev->rx_channel); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | static const struct of_device_id mbox_test_match[] = { |
Sudeep Holla | c428013 | 2016-02-19 16:01:16 +0000 | [diff] [blame] | 429 | { .compatible = "mailbox-test" }, |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 430 | {}, |
| 431 | }; |
Javier Martinez Canillas | f42cce3 | 2016-10-20 00:34:04 -0300 | [diff] [blame] | 432 | MODULE_DEVICE_TABLE(of, mbox_test_match); |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 433 | |
| 434 | static struct platform_driver mbox_test_driver = { |
| 435 | .driver = { |
Sudeep Holla | adf06ba | 2016-02-19 16:01:15 +0000 | [diff] [blame] | 436 | .name = "mailbox_test", |
Lee Jones | 8ea4484 | 2015-10-16 08:21:30 +0100 | [diff] [blame] | 437 | .of_match_table = mbox_test_match, |
| 438 | }, |
| 439 | .probe = mbox_test_probe, |
| 440 | .remove = mbox_test_remove, |
| 441 | }; |
| 442 | module_platform_driver(mbox_test_driver); |
| 443 | |
| 444 | MODULE_DESCRIPTION("Generic Mailbox Testing Facility"); |
| 445 | MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org"); |
| 446 | MODULE_LICENSE("GPL v2"); |