1 /* 2 * Copyright (c) 2011-2016 Synaptics Incorporated 3 * Copyright (c) 2011 Unixphere 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 as published by 7 * the Free Software Foundation. 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/device.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 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(sizeof(struct rmi_device), GFP_KERNEL); 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 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 137 static struct device_type rmi_function_type = { 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 155 #ifdef CONFIG_OF 156 static void rmi_function_of_probe(struct rmi_function *fn) 157 { 158 char of_name[9]; 159 struct device_node *node = fn->rmi_dev->xport->dev->of_node; 160 161 snprintf(of_name, sizeof(of_name), "rmi4-f%02x", 162 fn->fd.function_number); 163 fn->dev.of_node = of_get_child_by_name(node, of_name); 164 } 165 #else 166 static inline void rmi_function_of_probe(struct rmi_function *fn) 167 {} 168 #endif 169 170 static int rmi_function_probe(struct device *dev) 171 { 172 struct rmi_function *fn = to_rmi_function(dev); 173 struct rmi_function_handler *handler = 174 to_rmi_function_handler(dev->driver); 175 int error; 176 177 rmi_function_of_probe(fn); 178 179 if (handler->probe) { 180 error = handler->probe(fn); 181 return error; 182 } 183 184 return 0; 185 } 186 187 static int rmi_function_remove(struct device *dev) 188 { 189 struct rmi_function *fn = to_rmi_function(dev); 190 struct rmi_function_handler *handler = 191 to_rmi_function_handler(dev->driver); 192 193 if (handler->remove) 194 handler->remove(fn); 195 196 return 0; 197 } 198 199 int rmi_register_function(struct rmi_function *fn) 200 { 201 struct rmi_device *rmi_dev = fn->rmi_dev; 202 int error; 203 204 device_initialize(&fn->dev); 205 206 dev_set_name(&fn->dev, "%s.fn%02x", 207 dev_name(&rmi_dev->dev), fn->fd.function_number); 208 209 fn->dev.parent = &rmi_dev->dev; 210 fn->dev.type = &rmi_function_type; 211 fn->dev.bus = &rmi_bus_type; 212 213 error = device_add(&fn->dev); 214 if (error) { 215 dev_err(&rmi_dev->dev, 216 "Failed device_register function device %s\n", 217 dev_name(&fn->dev)); 218 goto err_put_device; 219 } 220 221 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Registered F%02X.\n", 222 fn->fd.function_number); 223 224 return 0; 225 226 err_put_device: 227 put_device(&fn->dev); 228 return error; 229 } 230 231 void rmi_unregister_function(struct rmi_function *fn) 232 { 233 device_del(&fn->dev); 234 of_node_put(fn->dev.of_node); 235 put_device(&fn->dev); 236 } 237 238 /** 239 * rmi_register_function_handler - register a handler for an RMI function 240 * @handler: RMI handler that should be registered. 241 * @module: pointer to module that implements the handler 242 * @mod_name: name of the module implementing the handler 243 * 244 * This function performs additional setup of RMI function handler and 245 * registers it with the RMI core so that it can be bound to 246 * RMI function devices. 247 */ 248 int __rmi_register_function_handler(struct rmi_function_handler *handler, 249 struct module *owner, 250 const char *mod_name) 251 { 252 struct device_driver *driver = &handler->driver; 253 int error; 254 255 driver->bus = &rmi_bus_type; 256 driver->owner = owner; 257 driver->mod_name = mod_name; 258 driver->probe = rmi_function_probe; 259 driver->remove = rmi_function_remove; 260 261 error = driver_register(&handler->driver); 262 if (error) { 263 pr_err("driver_register() failed for %s, error: %d\n", 264 handler->driver.name, error); 265 return error; 266 } 267 268 return 0; 269 } 270 EXPORT_SYMBOL_GPL(__rmi_register_function_handler); 271 272 /** 273 * rmi_unregister_function_handler - unregister given RMI function handler 274 * @handler: RMI handler that should be unregistered. 275 * 276 * This function unregisters given function handler from RMI core which 277 * causes it to be unbound from the function devices. 278 */ 279 void rmi_unregister_function_handler(struct rmi_function_handler *handler) 280 { 281 driver_unregister(&handler->driver); 282 } 283 EXPORT_SYMBOL_GPL(rmi_unregister_function_handler); 284 285 /* Bus specific stuff */ 286 287 static int rmi_bus_match(struct device *dev, struct device_driver *drv) 288 { 289 bool physical = rmi_is_physical_device(dev); 290 291 /* First see if types are not compatible */ 292 if (physical != rmi_is_physical_driver(drv)) 293 return 0; 294 295 return physical || rmi_function_match(dev, drv); 296 } 297 298 struct bus_type rmi_bus_type = { 299 .match = rmi_bus_match, 300 .name = "rmi4", 301 }; 302 303 static struct rmi_function_handler *fn_handlers[] = { 304 &rmi_f01_handler, 305 #ifdef CONFIG_RMI4_F11 306 &rmi_f11_handler, 307 #endif 308 #ifdef CONFIG_RMI4_F12 309 &rmi_f12_handler, 310 #endif 311 #ifdef CONFIG_RMI4_F30 312 &rmi_f30_handler, 313 #endif 314 #ifdef CONFIG_RMI4_F54 315 &rmi_f54_handler, 316 #endif 317 }; 318 319 static void __rmi_unregister_function_handlers(int start_idx) 320 { 321 int i; 322 323 for (i = start_idx; i >= 0; i--) 324 rmi_unregister_function_handler(fn_handlers[i]); 325 } 326 327 static void rmi_unregister_function_handlers(void) 328 { 329 __rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers) - 1); 330 } 331 332 static int rmi_register_function_handlers(void) 333 { 334 int ret; 335 int i; 336 337 for (i = 0; i < ARRAY_SIZE(fn_handlers); i++) { 338 ret = rmi_register_function_handler(fn_handlers[i]); 339 if (ret) { 340 pr_err("%s: error registering the RMI F%02x handler: %d\n", 341 __func__, fn_handlers[i]->func, ret); 342 goto err_unregister_function_handlers; 343 } 344 } 345 346 return 0; 347 348 err_unregister_function_handlers: 349 __rmi_unregister_function_handlers(i - 1); 350 return ret; 351 } 352 353 int rmi_of_property_read_u32(struct device *dev, u32 *result, 354 const char *prop, bool optional) 355 { 356 int retval; 357 u32 val = 0; 358 359 retval = of_property_read_u32(dev->of_node, prop, &val); 360 if (retval && (!optional && retval == -EINVAL)) { 361 dev_err(dev, "Failed to get %s value: %d\n", 362 prop, retval); 363 return retval; 364 } 365 *result = val; 366 367 return 0; 368 } 369 EXPORT_SYMBOL_GPL(rmi_of_property_read_u32); 370 371 static int __init rmi_bus_init(void) 372 { 373 int error; 374 375 error = bus_register(&rmi_bus_type); 376 if (error) { 377 pr_err("%s: error registering the RMI bus: %d\n", 378 __func__, error); 379 return error; 380 } 381 382 error = rmi_register_function_handlers(); 383 if (error) 384 goto err_unregister_bus; 385 386 error = rmi_register_physical_driver(); 387 if (error) { 388 pr_err("%s: error registering the RMI physical driver: %d\n", 389 __func__, error); 390 goto err_unregister_bus; 391 } 392 393 return 0; 394 395 err_unregister_bus: 396 bus_unregister(&rmi_bus_type); 397 return error; 398 } 399 module_init(rmi_bus_init); 400 401 static void __exit rmi_bus_exit(void) 402 { 403 /* 404 * We should only ever get here if all drivers are unloaded, so 405 * all we have to do at this point is unregister ourselves. 406 */ 407 408 rmi_unregister_physical_driver(); 409 rmi_unregister_function_handlers(); 410 bus_unregister(&rmi_bus_type); 411 } 412 module_exit(rmi_bus_exit); 413 414 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com"); 415 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com"); 416 MODULE_DESCRIPTION("RMI bus"); 417 MODULE_LICENSE("GPL"); 418 MODULE_VERSION(RMI_DRIVER_VERSION); 419