linux/drivers/input/rmi4/rmi_bus.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (c) 2011-2016 Synaptics Incorporated
   4 * Copyright (c) 2011 Unixphere
   5 */
   6
   7#include <linux/kernel.h>
   8#include <linux/device.h>
   9#include <linux/irq.h>
  10#include <linux/irqdomain.h>
  11#include <linux/list.h>
  12#include <linux/pm.h>
  13#include <linux/rmi.h>
  14#include <linux/slab.h>
  15#include <linux/types.h>
  16#include <linux/of.h>
  17#include "rmi_bus.h"
  18#include "rmi_driver.h"
  19
  20static int debug_flags;
  21module_param(debug_flags, int, 0644);
  22MODULE_PARM_DESC(debug_flags, "control debugging information");
  23
  24void rmi_dbg(int flags, struct device *dev, const char *fmt, ...)
  25{
  26        struct va_format vaf;
  27        va_list args;
  28
  29        if (flags & debug_flags) {
  30                va_start(args, fmt);
  31
  32                vaf.fmt = fmt;
  33                vaf.va = &args;
  34
  35                dev_printk(KERN_DEBUG, dev, "%pV", &vaf);
  36
  37                va_end(args);
  38        }
  39}
  40EXPORT_SYMBOL_GPL(rmi_dbg);
  41
  42/*
  43 * RMI Physical devices
  44 *
  45 * Physical RMI device consists of several functions serving particular
  46 * purpose. For example F11 is a 2D touch sensor while F01 is a generic
  47 * function present in every RMI device.
  48 */
  49
  50static void rmi_release_device(struct device *dev)
  51{
  52        struct rmi_device *rmi_dev = to_rmi_device(dev);
  53
  54        kfree(rmi_dev);
  55}
  56
  57static const struct device_type rmi_device_type = {
  58        .name           = "rmi4_sensor",
  59        .release        = rmi_release_device,
  60};
  61
  62bool rmi_is_physical_device(struct device *dev)
  63{
  64        return dev->type == &rmi_device_type;
  65}
  66
  67/**
  68 * rmi_register_transport_device - register a transport device connection
  69 * on the RMI bus.  Transport drivers provide communication from the devices
  70 * on a bus (such as SPI, I2C, and so on) to the RMI4 sensor.
  71 *
  72 * @xport: the transport device to register
  73 */
  74int rmi_register_transport_device(struct rmi_transport_dev *xport)
  75{
  76        static atomic_t transport_device_count = ATOMIC_INIT(0);
  77        struct rmi_device *rmi_dev;
  78        int error;
  79
  80        rmi_dev = kzalloc(sizeof(struct rmi_device), GFP_KERNEL);
  81        if (!rmi_dev)
  82                return -ENOMEM;
  83
  84        device_initialize(&rmi_dev->dev);
  85
  86        rmi_dev->xport = xport;
  87        rmi_dev->number = atomic_inc_return(&transport_device_count) - 1;
  88
  89        dev_set_name(&rmi_dev->dev, "rmi4-%02d", rmi_dev->number);
  90
  91        rmi_dev->dev.bus = &rmi_bus_type;
  92        rmi_dev->dev.type = &rmi_device_type;
  93
  94        xport->rmi_dev = rmi_dev;
  95
  96        error = device_add(&rmi_dev->dev);
  97        if (error)
  98                goto err_put_device;
  99
 100        rmi_dbg(RMI_DEBUG_CORE, xport->dev,
 101                "%s: Registered %s as %s.\n", __func__,
 102                dev_name(rmi_dev->xport->dev), dev_name(&rmi_dev->dev));
 103
 104        return 0;
 105
 106err_put_device:
 107        put_device(&rmi_dev->dev);
 108        return error;
 109}
 110EXPORT_SYMBOL_GPL(rmi_register_transport_device);
 111
 112/**
 113 * rmi_unregister_transport_device - unregister a transport device connection
 114 * @xport: the transport driver to unregister
 115 *
 116 */
 117void rmi_unregister_transport_device(struct rmi_transport_dev *xport)
 118{
 119        struct rmi_device *rmi_dev = xport->rmi_dev;
 120
 121        device_del(&rmi_dev->dev);
 122        put_device(&rmi_dev->dev);
 123}
 124EXPORT_SYMBOL(rmi_unregister_transport_device);
 125
 126
 127/* Function specific stuff */
 128
 129static void rmi_release_function(struct device *dev)
 130{
 131        struct rmi_function *fn = to_rmi_function(dev);
 132
 133        kfree(fn);
 134}
 135
 136static const struct device_type rmi_function_type = {
 137        .name           = "rmi4_function",
 138        .release        = rmi_release_function,
 139};
 140
 141bool rmi_is_function_device(struct device *dev)
 142{
 143        return dev->type == &rmi_function_type;
 144}
 145
 146static int rmi_function_match(struct device *dev, struct device_driver *drv)
 147{
 148        struct rmi_function_handler *handler = to_rmi_function_handler(drv);
 149        struct rmi_function *fn = to_rmi_function(dev);
 150
 151        return fn->fd.function_number == handler->func;
 152}
 153
 154#ifdef CONFIG_OF
 155static void rmi_function_of_probe(struct rmi_function *fn)
 156{
 157        char of_name[9];
 158        struct device_node *node = fn->rmi_dev->xport->dev->of_node;
 159
 160        snprintf(of_name, sizeof(of_name), "rmi4-f%02x",
 161                fn->fd.function_number);
 162        fn->dev.of_node = of_get_child_by_name(node, of_name);
 163}
 164#else
 165static inline void rmi_function_of_probe(struct rmi_function *fn)
 166{}
 167#endif
 168
 169static struct irq_chip rmi_irq_chip = {
 170        .name = "rmi4",
 171};
 172
 173static int rmi_create_function_irq(struct rmi_function *fn,
 174                                   struct rmi_function_handler *handler)
 175{
 176        struct rmi_driver_data *drvdata = dev_get_drvdata(&fn->rmi_dev->dev);
 177        int i, error;
 178
 179        for (i = 0; i < fn->num_of_irqs; i++) {
 180                set_bit(fn->irq_pos + i, fn->irq_mask);
 181
 182                fn->irq[i] = irq_create_mapping(drvdata->irqdomain,
 183                                                fn->irq_pos + i);
 184
 185                irq_set_chip_data(fn->irq[i], fn);
 186                irq_set_chip_and_handler(fn->irq[i], &rmi_irq_chip,
 187                                         handle_simple_irq);
 188                irq_set_nested_thread(fn->irq[i], 1);
 189
 190                error = devm_request_threaded_irq(&fn->dev, fn->irq[i], NULL,
 191                                        handler->attention, IRQF_ONESHOT,
 192                                        dev_name(&fn->dev), fn);
 193                if (error) {
 194                        dev_err(&fn->dev, "Error %d registering IRQ\n", error);
 195                        return error;
 196                }
 197        }
 198
 199        return 0;
 200}
 201
 202static int rmi_function_probe(struct device *dev)
 203{
 204        struct rmi_function *fn = to_rmi_function(dev);
 205        struct rmi_function_handler *handler =
 206                                        to_rmi_function_handler(dev->driver);
 207        int error;
 208
 209        rmi_function_of_probe(fn);
 210
 211        if (handler->probe) {
 212                error = handler->probe(fn);
 213                if (error)
 214                        return error;
 215        }
 216
 217        if (fn->num_of_irqs && handler->attention) {
 218                error = rmi_create_function_irq(fn, handler);
 219                if (error)
 220                        return error;
 221        }
 222
 223        return 0;
 224}
 225
 226static int rmi_function_remove(struct device *dev)
 227{
 228        struct rmi_function *fn = to_rmi_function(dev);
 229        struct rmi_function_handler *handler =
 230                                        to_rmi_function_handler(dev->driver);
 231
 232        if (handler->remove)
 233                handler->remove(fn);
 234
 235        return 0;
 236}
 237
 238int rmi_register_function(struct rmi_function *fn)
 239{
 240        struct rmi_device *rmi_dev = fn->rmi_dev;
 241        int error;
 242
 243        device_initialize(&fn->dev);
 244
 245        dev_set_name(&fn->dev, "%s.fn%02x",
 246                     dev_name(&rmi_dev->dev), fn->fd.function_number);
 247
 248        fn->dev.parent = &rmi_dev->dev;
 249        fn->dev.type = &rmi_function_type;
 250        fn->dev.bus = &rmi_bus_type;
 251
 252        error = device_add(&fn->dev);
 253        if (error) {
 254                dev_err(&rmi_dev->dev,
 255                        "Failed device_register function device %s\n",
 256                        dev_name(&fn->dev));
 257                goto err_put_device;
 258        }
 259
 260        rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Registered F%02X.\n",
 261                        fn->fd.function_number);
 262
 263        return 0;
 264
 265err_put_device:
 266        put_device(&fn->dev);
 267        return error;
 268}
 269
 270void rmi_unregister_function(struct rmi_function *fn)
 271{
 272        int i;
 273
 274        rmi_dbg(RMI_DEBUG_CORE, &fn->dev, "Unregistering F%02X.\n",
 275                        fn->fd.function_number);
 276
 277        device_del(&fn->dev);
 278        of_node_put(fn->dev.of_node);
 279        put_device(&fn->dev);
 280
 281        for (i = 0; i < fn->num_of_irqs; i++)
 282                irq_dispose_mapping(fn->irq[i]);
 283
 284}
 285
 286/**
 287 * rmi_register_function_handler - register a handler for an RMI function
 288 * @handler: RMI handler that should be registered.
 289 * @module: pointer to module that implements the handler
 290 * @mod_name: name of the module implementing the handler
 291 *
 292 * This function performs additional setup of RMI function handler and
 293 * registers it with the RMI core so that it can be bound to
 294 * RMI function devices.
 295 */
 296int __rmi_register_function_handler(struct rmi_function_handler *handler,
 297                                     struct module *owner,
 298                                     const char *mod_name)
 299{
 300        struct device_driver *driver = &handler->driver;
 301        int error;
 302
 303        driver->bus = &rmi_bus_type;
 304        driver->owner = owner;
 305        driver->mod_name = mod_name;
 306        driver->probe = rmi_function_probe;
 307        driver->remove = rmi_function_remove;
 308
 309        error = driver_register(driver);
 310        if (error) {
 311                pr_err("driver_register() failed for %s, error: %d\n",
 312                        driver->name, error);
 313                return error;
 314        }
 315
 316        return 0;
 317}
 318EXPORT_SYMBOL_GPL(__rmi_register_function_handler);
 319
 320/**
 321 * rmi_unregister_function_handler - unregister given RMI function handler
 322 * @handler: RMI handler that should be unregistered.
 323 *
 324 * This function unregisters given function handler from RMI core which
 325 * causes it to be unbound from the function devices.
 326 */
 327void rmi_unregister_function_handler(struct rmi_function_handler *handler)
 328{
 329        driver_unregister(&handler->driver);
 330}
 331EXPORT_SYMBOL_GPL(rmi_unregister_function_handler);
 332
 333/* Bus specific stuff */
 334
 335static int rmi_bus_match(struct device *dev, struct device_driver *drv)
 336{
 337        bool physical = rmi_is_physical_device(dev);
 338
 339        /* First see if types are not compatible */
 340        if (physical != rmi_is_physical_driver(drv))
 341                return 0;
 342
 343        return physical || rmi_function_match(dev, drv);
 344}
 345
 346struct bus_type rmi_bus_type = {
 347        .match          = rmi_bus_match,
 348        .name           = "rmi4",
 349};
 350
 351static struct rmi_function_handler *fn_handlers[] = {
 352        &rmi_f01_handler,
 353#ifdef CONFIG_RMI4_F03
 354        &rmi_f03_handler,
 355#endif
 356#ifdef CONFIG_RMI4_F11
 357        &rmi_f11_handler,
 358#endif
 359#ifdef CONFIG_RMI4_F12
 360        &rmi_f12_handler,
 361#endif
 362#ifdef CONFIG_RMI4_F30
 363        &rmi_f30_handler,
 364#endif
 365#ifdef CONFIG_RMI4_F34
 366        &rmi_f34_handler,
 367#endif
 368#ifdef CONFIG_RMI4_F54
 369        &rmi_f54_handler,
 370#endif
 371#ifdef CONFIG_RMI4_F55
 372        &rmi_f55_handler,
 373#endif
 374};
 375
 376static void __rmi_unregister_function_handlers(int start_idx)
 377{
 378        int i;
 379
 380        for (i = start_idx; i >= 0; i--)
 381                rmi_unregister_function_handler(fn_handlers[i]);
 382}
 383
 384static void rmi_unregister_function_handlers(void)
 385{
 386        __rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers) - 1);
 387}
 388
 389static int rmi_register_function_handlers(void)
 390{
 391        int ret;
 392        int i;
 393
 394        for (i = 0; i < ARRAY_SIZE(fn_handlers); i++)   {
 395                ret = rmi_register_function_handler(fn_handlers[i]);
 396                if (ret) {
 397                        pr_err("%s: error registering the RMI F%02x handler: %d\n",
 398                                __func__, fn_handlers[i]->func, ret);
 399                        goto err_unregister_function_handlers;
 400                }
 401        }
 402
 403        return 0;
 404
 405err_unregister_function_handlers:
 406        __rmi_unregister_function_handlers(i - 1);
 407        return ret;
 408}
 409
 410int rmi_of_property_read_u32(struct device *dev, u32 *result,
 411                                const char *prop, bool optional)
 412{
 413        int retval;
 414        u32 val = 0;
 415
 416        retval = of_property_read_u32(dev->of_node, prop, &val);
 417        if (retval && (!optional && retval == -EINVAL)) {
 418                dev_err(dev, "Failed to get %s value: %d\n",
 419                        prop, retval);
 420                return retval;
 421        }
 422        *result = val;
 423
 424        return 0;
 425}
 426EXPORT_SYMBOL_GPL(rmi_of_property_read_u32);
 427
 428static int __init rmi_bus_init(void)
 429{
 430        int error;
 431
 432        error = bus_register(&rmi_bus_type);
 433        if (error) {
 434                pr_err("%s: error registering the RMI bus: %d\n",
 435                        __func__, error);
 436                return error;
 437        }
 438
 439        error = rmi_register_function_handlers();
 440        if (error)
 441                goto err_unregister_bus;
 442
 443        error = rmi_register_physical_driver();
 444        if (error) {
 445                pr_err("%s: error registering the RMI physical driver: %d\n",
 446                        __func__, error);
 447                goto err_unregister_bus;
 448        }
 449
 450        return 0;
 451
 452err_unregister_bus:
 453        bus_unregister(&rmi_bus_type);
 454        return error;
 455}
 456module_init(rmi_bus_init);
 457
 458static void __exit rmi_bus_exit(void)
 459{
 460        /*
 461         * We should only ever get here if all drivers are unloaded, so
 462         * all we have to do at this point is unregister ourselves.
 463         */
 464
 465        rmi_unregister_physical_driver();
 466        rmi_unregister_function_handlers();
 467        bus_unregister(&rmi_bus_type);
 468}
 469module_exit(rmi_bus_exit);
 470
 471MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
 472MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com");
 473MODULE_DESCRIPTION("RMI bus");
 474MODULE_LICENSE("GPL");
 475