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