| 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 |
int rmi_register_function(struct rmi_function *fn) |
| 241 |
{ |
| 242 |
struct rmi_device *rmi_dev = fn->rmi_dev; |
| 243 |
int error; |
| 244 |
|
| 245 |
device_initialize(&fn->dev); |
| 246 |
|
| 247 |
dev_set_name(&fn->dev, "%s.fn%02x", |
| 248 |
dev_name(&rmi_dev->dev), fn->fd.function_number); |
| 249 |
|
| 250 |
fn->dev.parent = &rmi_dev->dev; |
| 251 |
fn->dev.type = &rmi_function_type; |
| 252 |
fn->dev.bus = &rmi_bus_type; |
| 253 |
|
| 254 |
error = device_add(&fn->dev); |
| 255 |
if (error) { |
| 256 |
dev_err(&rmi_dev->dev, |
| 257 |
"Failed device_register function device %s\n", |
| 258 |
dev_name(&fn->dev)); |
| 259 |
goto err_put_device; |
| 260 |
} |
| 261 |
|
| 262 |
rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Registered F%02X.\n", |
| 263 |
fn->fd.function_number); |
| 264 |
|
| 265 |
return 0; |
| 266 |
|
| 267 |
err_put_device: |
| 268 |
put_device(&fn->dev); |
| 269 |
return error; |
| 270 |
} |
| 271 |
|
| 272 |
void rmi_unregister_function(struct rmi_function *fn) |
| 273 |
{ |
| 274 |
int i; |
| 275 |
|
| 276 |
rmi_dbg(RMI_DEBUG_CORE, &fn->dev, "Unregistering F%02X.\n", |
| 277 |
fn->fd.function_number); |
| 278 |
|
| 279 |
device_del(&fn->dev); |
| 280 |
of_node_put(fn->dev.of_node); |
| 281 |
|
| 282 |
for (i = 0; i < fn->num_of_irqs; i++) |
| 283 |
irq_dispose_mapping(fn->irq[i]); |
| 284 |
|
| 285 |
put_device(&fn->dev); |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* __rmi_register_function_handler - register a handler for an RMI function |
| 290 |
* @handler: RMI handler that should be registered. |
| 291 |
* @owner: pointer to module that implements the handler |
| 292 |
* @mod_name: name of the module implementing the handler |
| 293 |
* |
| 294 |
* This function performs additional setup of RMI function handler and |
| 295 |
* registers it with the RMI core so that it can be bound to |
| 296 |
* RMI function devices. |
| 297 |
*/ |
| 298 |
int __rmi_register_function_handler(struct rmi_function_handler *handler, |
| 299 |
struct module *owner, |
| 300 |
const char *mod_name) |
| 301 |
{ |
| 302 |
struct device_driver *driver = &handler->driver; |
| 303 |
int error; |
| 304 |
|
| 305 |
driver->bus = &rmi_bus_type; |
| 306 |
driver->owner = owner; |
| 307 |
driver->mod_name = mod_name; |
| 308 |
driver->probe = rmi_function_probe; |
| 309 |
driver->remove = rmi_function_remove; |
| 310 |
|
| 311 |
error = driver_register(driver); |
| 312 |
if (error) { |
| 313 |
pr_err("driver_register() failed for %s, error: %d\n", |
| 314 |
driver->name, error); |
| 315 |
return error; |
| 316 |
} |
| 317 |
|
| 318 |
return 0; |
| 319 |
} |
| 320 |
EXPORT_SYMBOL_GPL(__rmi_register_function_handler); |
| 321 |
|
| 322 |
/** |
| 323 |
* rmi_unregister_function_handler - unregister given RMI function handler |
| 324 |
* @handler: RMI handler that should be unregistered. |
| 325 |
* |
| 326 |
* This function unregisters given function handler from RMI core which |
| 327 |
* causes it to be unbound from the function devices. |
| 328 |
*/ |
| 329 |
void rmi_unregister_function_handler(struct rmi_function_handler *handler) |
| 330 |
{ |
| 331 |
driver_unregister(&handler->driver); |
| 332 |
} |
| 333 |
EXPORT_SYMBOL_GPL(rmi_unregister_function_handler); |
| 334 |
|
| 335 |
/* Bus specific stuff */ |
| 336 |
|
| 337 |
static int rmi_bus_match(struct device *dev, const struct device_driver *drv) |
| 338 |
{ |
| 339 |
bool physical = rmi_is_physical_device(dev); |
| 340 |
|
| 341 |
/* First see if types are not compatible */ |
| 342 |
if (physical != rmi_is_physical_driver(drv)) |
| 343 |
return 0; |
| 344 |
|
| 345 |
return physical || rmi_function_match(dev, drv); |
| 346 |
} |
| 347 |
|
| 348 |
const struct bus_type rmi_bus_type = { |
| 349 |
.match = rmi_bus_match, |
| 350 |
.name = "rmi4", |
| 351 |
}; |
| 352 |
|
| 353 |
static struct rmi_function_handler *fn_handlers[] = { |
| 354 |
&rmi_f01_handler, |
| 355 |
#ifdef CONFIG_RMI4_F03 |
| 356 |
&rmi_f03_handler, |
| 357 |
#endif |
| 358 |
#ifdef CONFIG_RMI4_F11 |
| 359 |
&rmi_f11_handler, |
| 360 |
#endif |
| 361 |
#ifdef CONFIG_RMI4_F12 |
| 362 |
&rmi_f12_handler, |
| 363 |
#endif |
| 364 |
#ifdef CONFIG_RMI4_F1A |
| 365 |
&rmi_f1a_handler, |
| 366 |
#endif |
| 367 |
#ifdef CONFIG_RMI4_F21 |
| 368 |
&rmi_f21_handler, |
| 369 |
#endif |
| 370 |
#ifdef CONFIG_RMI4_F30 |
| 371 |
&rmi_f30_handler, |
| 372 |
#endif |
| 373 |
#ifdef CONFIG_RMI4_F34 |
| 374 |
&rmi_f34_handler, |
| 375 |
#endif |
| 376 |
#ifdef CONFIG_RMI4_F3A |
| 377 |
&rmi_f3a_handler, |
| 378 |
#endif |
| 379 |
#ifdef CONFIG_RMI4_F54 |
| 380 |
&rmi_f54_handler, |
| 381 |
#endif |
| 382 |
#ifdef CONFIG_RMI4_F55 |
| 383 |
&rmi_f55_handler, |
| 384 |
#endif |
| 385 |
}; |
| 386 |
|
| 387 |
static void __rmi_unregister_function_handlers(int start_idx) |
| 388 |
{ |
| 389 |
int i; |
| 390 |
|
| 391 |
for (i = start_idx; i >= 0; i--) |
| 392 |
rmi_unregister_function_handler(fn_handlers[i]); |
| 393 |
} |
| 394 |
|
| 395 |
static void rmi_unregister_function_handlers(void) |
| 396 |
{ |
| 397 |
__rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers) - 1); |
| 398 |
} |
| 399 |
|
| 400 |
static int rmi_register_function_handlers(void) |
| 401 |
{ |
| 402 |
int ret; |
| 403 |
int i; |
| 404 |
|
| 405 |
for (i = 0; i < ARRAY_SIZE(fn_handlers); i++) { |
| 406 |
ret = rmi_register_function_handler(fn_handlers[i]); |
| 407 |
if (ret) { |
| 408 |
pr_err("%s: error registering the RMI F%02x handler: %d\n", |
| 409 |
__func__, fn_handlers[i]->func, ret); |
| 410 |
goto err_unregister_function_handlers; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
return 0; |
| 415 |
|
| 416 |
err_unregister_function_handlers: |
| 417 |
__rmi_unregister_function_handlers(i - 1); |
| 418 |
return ret; |
| 419 |
} |
| 420 |
|
| 421 |
int rmi_of_property_read_u32(struct device *dev, u32 *result, |
| 422 |
const char *prop, bool optional) |
| 423 |
{ |
| 424 |
int retval; |
| 425 |
u32 val = 0; |
| 426 |
|
| 427 |
retval = of_property_read_u32(dev->of_node, prop, &val); |
| 428 |
if (retval && (!optional && retval == -EINVAL)) { |
| 429 |
dev_err(dev, "Failed to get %s value: %d\n", |
| 430 |
prop, retval); |
| 431 |
return retval; |
| 432 |
} |
| 433 |
*result = val; |
| 434 |
|
| 435 |
return 0; |
| 436 |
} |
| 437 |
EXPORT_SYMBOL_GPL(rmi_of_property_read_u32); |
| 438 |
|
| 439 |
static int __init rmi_bus_init(void) |
| 440 |
{ |
| 441 |
int error; |
| 442 |
|
| 443 |
error = bus_register(&rmi_bus_type); |
| 444 |
if (error) { |
| 445 |
pr_err("%s: error registering the RMI bus: %d\n", |
| 446 |
__func__, error); |
| 447 |
return error; |
| 448 |
} |
| 449 |
|
| 450 |
error = rmi_register_function_handlers(); |
| 451 |
if (error) |
| 452 |
goto err_unregister_bus; |
| 453 |
|
| 454 |
error = rmi_register_physical_driver(); |
| 455 |
if (error) { |
| 456 |
pr_err("%s: error registering the RMI physical driver: %d\n", |
| 457 |
__func__, error); |
| 458 |
goto err_unregister_bus; |
| 459 |
} |
| 460 |
|
| 461 |
return 0; |
| 462 |
|
| 463 |
err_unregister_bus: |
| 464 |
bus_unregister(&rmi_bus_type); |
| 465 |
return error; |
| 466 |
} |
| 467 |
module_init(rmi_bus_init); |
| 468 |
|
| 469 |
static void __exit rmi_bus_exit(void) |
| 470 |
{ |
| 471 |
/* |
| 472 |
* We should only ever get here if all drivers are unloaded, so |
| 473 |
* all we have to do at this point is unregister ourselves. |
| 474 |
*/ |
| 475 |
|
| 476 |
rmi_unregister_physical_driver(); |
| 477 |
rmi_unregister_function_handlers(); |
| 478 |
bus_unregister(&rmi_bus_type); |
| 479 |
} |
| 480 |
module_exit(rmi_bus_exit); |
| 481 |
|
| 482 |
MODULE_AUTHOR("Christopher Heiny <[email protected]"); |
| 483 |
MODULE_AUTHOR("Andrew Duggan <[email protected]"); |
| 484 |
MODULE_DESCRIPTION("RMI bus"); |
| 485 |
MODULE_LICENSE("GPL"); |