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