Blame view
drivers/gpio/gpio-imx-rpmsg.c
10.4 KB
f3462feb1 gpio: imx-rpmsg: ... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* * Copyright 2017 NXP * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/bitops.h> #include <linux/err.h> #include <linux/gpio.h> #include <linux/imx_rpmsg.h> #include <linux/init.h> |
056c18286 gpio: imx-rpmsg: ... |
21 |
#include <linux/irqdomain.h> |
f3462feb1 gpio: imx-rpmsg: ... |
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include <linux/of.h> #include <linux/of_device.h> #include <linux/platform_device.h> #include <linux/pm_qos.h> #include <linux/rpmsg.h> #include <linux/virtio.h> #define IMX_RPMSG_GPIO_PER_PORT 32 #define RPMSG_TIMEOUT 1000 enum gpio_input_trigger_type { GPIO_RPMSG_TRI_IGNORE, GPIO_RPMSG_TRI_RISING, GPIO_RPMSG_TRI_FALLING, GPIO_RPMSG_TRI_BOTH_EDGE, GPIO_RPMSG_TRI_LOW_LEVEL, GPIO_RPMSG_TRI_HIGH_LEVEL, }; enum gpio_rpmsg_header_type { GPIO_RPMSG_SETUP, GPIO_RPMSG_REPLY, GPIO_RPMSG_NOTIFY, }; enum gpio_rpmsg_header_cmd { GPIO_RPMSG_INPUT_INIT, GPIO_RPMSG_OUTPUT_INIT, GPIO_RPMSG_INPUT_GET, }; struct gpio_rpmsg_data { struct imx_rpmsg_head header; u8 pin_idx; u8 port_idx; union { u8 event; u8 retcode; u8 value; } out; union { u8 wakeup; u8 value; } in; } __packed __aligned(8); struct imx_rpmsg_gpio_port { struct gpio_chip gc; |
056c18286 gpio: imx-rpmsg: ... |
70 71 |
struct irq_chip chip; struct irq_domain *domain; |
f3462feb1 gpio: imx-rpmsg: ... |
72 |
struct gpio_rpmsg_data msg; |
056c18286 gpio: imx-rpmsg: ... |
73 |
u32 irq_type[IMX_RPMSG_GPIO_PER_PORT]; |
f3462feb1 gpio: imx-rpmsg: ... |
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
int idx; }; struct imx_gpio_rpmsg_info { struct rpmsg_device *rpdev; struct gpio_rpmsg_data *notify_msg; struct gpio_rpmsg_data *reply_msg; struct pm_qos_request pm_qos_req; struct completion cmd_complete; struct mutex lock; }; static struct imx_gpio_rpmsg_info gpio_rpmsg; static int gpio_send_message(struct imx_rpmsg_gpio_port *port, |
056c18286 gpio: imx-rpmsg: ... |
89 90 91 |
struct gpio_rpmsg_data *msg, struct imx_gpio_rpmsg_info *info, bool sync) |
f3462feb1 gpio: imx-rpmsg: ... |
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
{ int err; if (!info->rpdev) { dev_dbg(&info->rpdev->dev, "rpmsg channel not ready, m4 image ready? "); return -EINVAL; } mutex_lock(&info->lock); pm_qos_add_request(&info->pm_qos_req, PM_QOS_CPU_DMA_LATENCY, 0); reinit_completion(&info->cmd_complete); err = rpmsg_send(info->rpdev->ept, (void *)msg, sizeof(struct gpio_rpmsg_data)); if (err) { dev_err(&info->rpdev->dev, "rpmsg_send failed: %d ", err); goto err_out; } |
056c18286 gpio: imx-rpmsg: ... |
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
if (sync) { err = wait_for_completion_timeout(&info->cmd_complete, msecs_to_jiffies(RPMSG_TIMEOUT)); if (!err) { dev_err(&info->rpdev->dev, "rpmsg_send timeout! "); err = -ETIMEDOUT; goto err_out; } if (info->reply_msg->out.retcode != 0) { dev_err(&info->rpdev->dev, "rpmsg not ack %d! ", info->reply_msg->out.retcode); err = -EINVAL; goto err_out; } /* copy the reply message */ memcpy(&port->msg, info->reply_msg, sizeof(*info->reply_msg)); err = 0; |
f3462feb1 gpio: imx-rpmsg: ... |
138 |
} |
f3462feb1 gpio: imx-rpmsg: ... |
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
err_out: pm_qos_remove_request(&info->pm_qos_req); mutex_unlock(&info->lock); return err; } static int gpio_rpmsg_cb(struct rpmsg_device *rpdev, void *data, int len, void *priv, u32 src) { struct gpio_rpmsg_data *msg = (struct gpio_rpmsg_data *)data; if (msg->header.type == GPIO_RPMSG_REPLY) { gpio_rpmsg.reply_msg = msg; complete(&gpio_rpmsg.cmd_complete); } else if (msg->header.type == GPIO_RPMSG_NOTIFY) { gpio_rpmsg.notify_msg = msg; /* TBD for interrupt handler */ } else dev_err(&gpio_rpmsg.rpdev->dev, "wrong command type! "); return 0; } static int imx_rpmsg_gpio_get(struct gpio_chip *gc, unsigned int gpio) { struct imx_rpmsg_gpio_port *port = gpiochip_get_data(gc); struct gpio_rpmsg_data msg; int ret; memset(&msg, 0, sizeof(struct gpio_rpmsg_data)); msg.header.cate = IMX_RPMSG_GPIO; msg.header.major = IMX_RMPSG_MAJOR; msg.header.minor = IMX_RMPSG_MINOR; msg.header.type = GPIO_RPMSG_SETUP; msg.header.cmd = GPIO_RPMSG_INPUT_GET; msg.pin_idx = gpio; msg.port_idx = port->idx; |
056c18286 gpio: imx-rpmsg: ... |
178 |
ret = gpio_send_message(port, &msg, &gpio_rpmsg, true); |
f3462feb1 gpio: imx-rpmsg: ... |
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
if (!ret) return !!port->msg.in.value; return ret; } static int imx_rpmsg_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio) { struct imx_rpmsg_gpio_port *port = gpiochip_get_data(gc); struct gpio_rpmsg_data msg; memset(&msg, 0, sizeof(struct gpio_rpmsg_data)); msg.header.cate = IMX_RPMSG_GPIO; msg.header.major = IMX_RMPSG_MAJOR; msg.header.minor = IMX_RMPSG_MINOR; msg.header.type = GPIO_RPMSG_SETUP; msg.header.cmd = GPIO_RPMSG_INPUT_INIT; msg.pin_idx = gpio; msg.port_idx = port->idx; /* TBD: get event trigger and wakeup from GPIO descriptor */ msg.out.event = GPIO_RPMSG_TRI_IGNORE; msg.in.wakeup = 0; |
056c18286 gpio: imx-rpmsg: ... |
203 |
return gpio_send_message(port, &msg, &gpio_rpmsg, true); |
f3462feb1 gpio: imx-rpmsg: ... |
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
} static inline void imx_rpmsg_gpio_direction_output_init(struct gpio_chip *gc, unsigned int gpio, int val, struct gpio_rpmsg_data *msg) { struct imx_rpmsg_gpio_port *port = gpiochip_get_data(gc); msg->header.cate = IMX_RPMSG_GPIO; msg->header.major = IMX_RMPSG_MAJOR; msg->header.minor = IMX_RMPSG_MINOR; msg->header.type = GPIO_RPMSG_SETUP; msg->header.cmd = GPIO_RPMSG_OUTPUT_INIT; msg->pin_idx = gpio; msg->port_idx = port->idx; msg->out.value = val; } static void imx_rpmsg_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val) { struct imx_rpmsg_gpio_port *port = gpiochip_get_data(gc); struct gpio_rpmsg_data msg; memset(&msg, 0, sizeof(struct gpio_rpmsg_data)); imx_rpmsg_gpio_direction_output_init(gc, gpio, val, &msg); |
056c18286 gpio: imx-rpmsg: ... |
228 |
gpio_send_message(port, &msg, &gpio_rpmsg, true); |
f3462feb1 gpio: imx-rpmsg: ... |
229 230 231 232 233 234 235 236 237 238 |
} static int imx_rpmsg_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio, int val) { struct imx_rpmsg_gpio_port *port = gpiochip_get_data(gc); struct gpio_rpmsg_data msg; memset(&msg, 0, sizeof(struct gpio_rpmsg_data)); imx_rpmsg_gpio_direction_output_init(gc, gpio, val, &msg); |
056c18286 gpio: imx-rpmsg: ... |
239 |
return gpio_send_message(port, &msg, &gpio_rpmsg, true); |
f3462feb1 gpio: imx-rpmsg: ... |
240 |
} |
056c18286 gpio: imx-rpmsg: ... |
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
static int imx_rpmsg_irq_set_type(struct irq_data *d, u32 type) { struct imx_rpmsg_gpio_port *port = irq_data_get_irq_chip_data(d); u32 gpio_idx = d->hwirq; int edge = 0; int ret = 0; switch (type) { case IRQ_TYPE_EDGE_RISING: edge = GPIO_RPMSG_TRI_RISING; break; case IRQ_TYPE_EDGE_FALLING: edge = GPIO_RPMSG_TRI_FALLING; break; case IRQ_TYPE_EDGE_BOTH: edge = GPIO_RPMSG_TRI_BOTH_EDGE; break; case IRQ_TYPE_LEVEL_LOW: edge = GPIO_RPMSG_TRI_LOW_LEVEL; break; case IRQ_TYPE_LEVEL_HIGH: edge = GPIO_RPMSG_TRI_HIGH_LEVEL; break; default: ret = -EINVAL; break; } port->irq_type[gpio_idx] = edge; return ret; } static int imx_rpmsg_irq_set_wake(struct irq_data *d, u32 enable) { struct imx_rpmsg_gpio_port *port = irq_data_get_irq_chip_data(d); struct gpio_rpmsg_data msg; u32 gpio_idx = d->hwirq; memset(&msg, 0, sizeof(struct gpio_rpmsg_data)); msg.header.cate = IMX_RPMSG_GPIO; msg.header.major = IMX_RMPSG_MAJOR; msg.header.minor = IMX_RMPSG_MINOR; msg.header.type = GPIO_RPMSG_SETUP; msg.header.cmd = GPIO_RPMSG_INPUT_INIT; msg.pin_idx = gpio_idx; msg.port_idx = port->idx; /* set wakeup trigger source, * if not set irq type, then use high level as trigger type */ msg.out.event = port->irq_type[gpio_idx]; if (!msg.out.event) msg.out.event = GPIO_RPMSG_TRI_HIGH_LEVEL; msg.in.wakeup = enable; /* here should be atomic context */ gpio_send_message(port, &msg, &gpio_rpmsg, false); return 0; } static void imx_rpmsg_unmask_irq(struct irq_data *d) { /* No need to implement the callback */ } static void imx_rpmsg_mask_irq(struct irq_data *d) { /* No need to implement the callback */ } static struct irq_chip imx_rpmsg_irq_chip = { .irq_mask = imx_rpmsg_mask_irq, .irq_unmask = imx_rpmsg_unmask_irq, .irq_set_wake = imx_rpmsg_irq_set_wake, .irq_set_type = imx_rpmsg_irq_set_type, }; |
f3462feb1 gpio: imx-rpmsg: ... |
319 320 321 322 323 324 |
static int imx_rpmsg_gpio_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; struct device_node *np = dev->of_node; struct imx_rpmsg_gpio_port *port; struct gpio_chip *gc; |
056c18286 gpio: imx-rpmsg: ... |
325 |
int i, irq_base; |
f3462feb1 gpio: imx-rpmsg: ... |
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
int ret; port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL); if (!port) return -ENOMEM; ret = of_property_read_u32(np, "port_idx", &port->idx); if (ret) return ret; gc = &port->gc; gc->of_node = np; gc->parent = dev; gc->label = "imx-rpmsg-gpio"; gc->ngpio = IMX_RPMSG_GPIO_PER_PORT; gc->base = of_alias_get_id(np, "gpio") * IMX_RPMSG_GPIO_PER_PORT; gc->direction_input = imx_rpmsg_gpio_direction_input; gc->direction_output = imx_rpmsg_gpio_direction_output; gc->get = imx_rpmsg_gpio_get; gc->set = imx_rpmsg_gpio_set; platform_set_drvdata(pdev, port); ret = devm_gpiochip_add_data(dev, gc, port); if (ret < 0) return ret; |
056c18286 gpio: imx-rpmsg: ... |
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
/* generate one new irq domain */ port->chip = imx_rpmsg_irq_chip; port->chip.name = kasprintf(GFP_KERNEL, "rpmsg-irq-port-%d", port->idx); port->chip.parent_device = NULL; irq_base = irq_alloc_descs(-1, 0, IMX_RPMSG_GPIO_PER_PORT, numa_node_id()); WARN_ON(irq_base < 0); port->domain = irq_domain_add_legacy(np, IMX_RPMSG_GPIO_PER_PORT, irq_base, 0, &irq_domain_simple_ops, port); WARN_ON(!port->domain); for (i = irq_base; i < irq_base + IMX_RPMSG_GPIO_PER_PORT; i++) { irq_set_chip_and_handler(i, &port->chip, handle_level_irq); irq_set_chip_data(i, port); irq_clear_status_flags(i, IRQ_NOREQUEST); irq_set_probe(i); } |
f3462feb1 gpio: imx-rpmsg: ... |
372 373 |
return 0; } |
f3462feb1 gpio: imx-rpmsg: ... |
374 375 376 377 378 379 380 381 382 383 384 385 |
static const struct of_device_id imx_rpmsg_gpio_dt_ids[] = { { .compatible = "fsl,imx-rpmsg-gpio" }, { /* sentinel */ } }; static struct platform_driver imx_rpmsg_gpio_driver = { .driver = { .name = "gpio-imx-rpmsg", .of_match_table = imx_rpmsg_gpio_dt_ids, }, .probe = imx_rpmsg_gpio_probe, }; |
61212b715 gpio: imx-rpmsg: ... |
386 |
static int gpio_rpmsg_probe(struct rpmsg_device *rpdev) |
f3462feb1 gpio: imx-rpmsg: ... |
387 |
{ |
61212b715 gpio: imx-rpmsg: ... |
388 389 390 391 |
gpio_rpmsg.rpdev = rpdev; dev_info(&rpdev->dev, "new channel: 0x%x -> 0x%x! ", rpdev->src, rpdev->dst); |
f3462feb1 gpio: imx-rpmsg: ... |
392 |
|
61212b715 gpio: imx-rpmsg: ... |
393 394 |
init_completion(&gpio_rpmsg.cmd_complete); mutex_init(&gpio_rpmsg.lock); |
f3462feb1 gpio: imx-rpmsg: ... |
395 396 397 |
return platform_driver_register(&imx_rpmsg_gpio_driver); } |
61212b715 gpio: imx-rpmsg: ... |
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
static struct rpmsg_device_id gpio_rpmsg_id_table[] = { { .name = "rpmsg-io-channel" }, {}, }; static struct rpmsg_driver gpio_rpmsg_driver = { .drv.name = "gpio_rpmsg", .drv.owner = THIS_MODULE, .id_table = gpio_rpmsg_id_table, .probe = gpio_rpmsg_probe, .callback = gpio_rpmsg_cb, }; static int __init gpio_imx_rpmsg_init(void) { return register_rpmsg_driver(&gpio_rpmsg_driver); } |
f3462feb1 gpio: imx-rpmsg: ... |
417 418 419 420 421 |
device_initcall(gpio_imx_rpmsg_init); MODULE_AUTHOR("NXP Semiconductor"); MODULE_DESCRIPTION("NXP i.MX7ULP rpmsg gpio driver"); MODULE_LICENSE("GPL v2"); |