0001
0002
0003
0004
0005
0006
0007 #include <linux/kernel.h>
0008 #include <linux/device.h>
0009 #include <linux/irq.h>
0010 #include <linux/irqdomain.h>
0011 #include <linux/list.h>
0012 #include <linux/pm.h>
0013 #include <linux/rmi.h>
0014 #include <linux/slab.h>
0015 #include <linux/types.h>
0016 #include <linux/of.h>
0017 #include "rmi_bus.h"
0018 #include "rmi_driver.h"
0019
0020 static int debug_flags;
0021 module_param(debug_flags, int, 0644);
0022 MODULE_PARM_DESC(debug_flags, "control debugging information");
0023
0024 void rmi_dbg(int flags, struct device *dev, const char *fmt, ...)
0025 {
0026 struct va_format vaf;
0027 va_list args;
0028
0029 if (flags & debug_flags) {
0030 va_start(args, fmt);
0031
0032 vaf.fmt = fmt;
0033 vaf.va = &args;
0034
0035 dev_printk(KERN_DEBUG, dev, "%pV", &vaf);
0036
0037 va_end(args);
0038 }
0039 }
0040 EXPORT_SYMBOL_GPL(rmi_dbg);
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 static void rmi_release_device(struct device *dev)
0051 {
0052 struct rmi_device *rmi_dev = to_rmi_device(dev);
0053
0054 kfree(rmi_dev);
0055 }
0056
0057 static const struct device_type rmi_device_type = {
0058 .name = "rmi4_sensor",
0059 .release = rmi_release_device,
0060 };
0061
0062 bool rmi_is_physical_device(struct device *dev)
0063 {
0064 return dev->type == &rmi_device_type;
0065 }
0066
0067
0068
0069
0070
0071
0072
0073
0074 int rmi_register_transport_device(struct rmi_transport_dev *xport)
0075 {
0076 static atomic_t transport_device_count = ATOMIC_INIT(0);
0077 struct rmi_device *rmi_dev;
0078 int error;
0079
0080 rmi_dev = kzalloc(sizeof(struct rmi_device), GFP_KERNEL);
0081 if (!rmi_dev)
0082 return -ENOMEM;
0083
0084 device_initialize(&rmi_dev->dev);
0085
0086 rmi_dev->xport = xport;
0087 rmi_dev->number = atomic_inc_return(&transport_device_count) - 1;
0088
0089 dev_set_name(&rmi_dev->dev, "rmi4-%02d", rmi_dev->number);
0090
0091 rmi_dev->dev.bus = &rmi_bus_type;
0092 rmi_dev->dev.type = &rmi_device_type;
0093 rmi_dev->dev.parent = xport->dev;
0094
0095 xport->rmi_dev = rmi_dev;
0096
0097 error = device_add(&rmi_dev->dev);
0098 if (error)
0099 goto err_put_device;
0100
0101 rmi_dbg(RMI_DEBUG_CORE, xport->dev,
0102 "%s: Registered %s as %s.\n", __func__,
0103 dev_name(rmi_dev->xport->dev), dev_name(&rmi_dev->dev));
0104
0105 return 0;
0106
0107 err_put_device:
0108 put_device(&rmi_dev->dev);
0109 return error;
0110 }
0111 EXPORT_SYMBOL_GPL(rmi_register_transport_device);
0112
0113
0114
0115
0116
0117
0118 void rmi_unregister_transport_device(struct rmi_transport_dev *xport)
0119 {
0120 struct rmi_device *rmi_dev = xport->rmi_dev;
0121
0122 device_del(&rmi_dev->dev);
0123 put_device(&rmi_dev->dev);
0124 }
0125 EXPORT_SYMBOL(rmi_unregister_transport_device);
0126
0127
0128
0129
0130 static void rmi_release_function(struct device *dev)
0131 {
0132 struct rmi_function *fn = to_rmi_function(dev);
0133
0134 kfree(fn);
0135 }
0136
0137 static const struct device_type rmi_function_type = {
0138 .name = "rmi4_function",
0139 .release = rmi_release_function,
0140 };
0141
0142 bool rmi_is_function_device(struct device *dev)
0143 {
0144 return dev->type == &rmi_function_type;
0145 }
0146
0147 static int rmi_function_match(struct device *dev, struct device_driver *drv)
0148 {
0149 struct rmi_function_handler *handler = to_rmi_function_handler(drv);
0150 struct rmi_function *fn = to_rmi_function(dev);
0151
0152 return fn->fd.function_number == handler->func;
0153 }
0154
0155 #ifdef CONFIG_OF
0156 static void rmi_function_of_probe(struct rmi_function *fn)
0157 {
0158 char of_name[9];
0159 struct device_node *node = fn->rmi_dev->xport->dev->of_node;
0160
0161 snprintf(of_name, sizeof(of_name), "rmi4-f%02x",
0162 fn->fd.function_number);
0163 fn->dev.of_node = of_get_child_by_name(node, of_name);
0164 }
0165 #else
0166 static inline void rmi_function_of_probe(struct rmi_function *fn)
0167 {}
0168 #endif
0169
0170 static struct irq_chip rmi_irq_chip = {
0171 .name = "rmi4",
0172 };
0173
0174 static int rmi_create_function_irq(struct rmi_function *fn,
0175 struct rmi_function_handler *handler)
0176 {
0177 struct rmi_driver_data *drvdata = dev_get_drvdata(&fn->rmi_dev->dev);
0178 int i, error;
0179
0180 for (i = 0; i < fn->num_of_irqs; i++) {
0181 set_bit(fn->irq_pos + i, fn->irq_mask);
0182
0183 fn->irq[i] = irq_create_mapping(drvdata->irqdomain,
0184 fn->irq_pos + i);
0185
0186 irq_set_chip_data(fn->irq[i], fn);
0187 irq_set_chip_and_handler(fn->irq[i], &rmi_irq_chip,
0188 handle_simple_irq);
0189 irq_set_nested_thread(fn->irq[i], 1);
0190
0191 error = devm_request_threaded_irq(&fn->dev, fn->irq[i], NULL,
0192 handler->attention, IRQF_ONESHOT,
0193 dev_name(&fn->dev), fn);
0194 if (error) {
0195 dev_err(&fn->dev, "Error %d registering IRQ\n", error);
0196 return error;
0197 }
0198 }
0199
0200 return 0;
0201 }
0202
0203 static int rmi_function_probe(struct device *dev)
0204 {
0205 struct rmi_function *fn = to_rmi_function(dev);
0206 struct rmi_function_handler *handler =
0207 to_rmi_function_handler(dev->driver);
0208 int error;
0209
0210 rmi_function_of_probe(fn);
0211
0212 if (handler->probe) {
0213 error = handler->probe(fn);
0214 if (error)
0215 return error;
0216 }
0217
0218 if (fn->num_of_irqs && handler->attention) {
0219 error = rmi_create_function_irq(fn, handler);
0220 if (error)
0221 return error;
0222 }
0223
0224 return 0;
0225 }
0226
0227 static int rmi_function_remove(struct device *dev)
0228 {
0229 struct rmi_function *fn = to_rmi_function(dev);
0230 struct rmi_function_handler *handler =
0231 to_rmi_function_handler(dev->driver);
0232
0233 if (handler->remove)
0234 handler->remove(fn);
0235
0236 return 0;
0237 }
0238
0239 int rmi_register_function(struct rmi_function *fn)
0240 {
0241 struct rmi_device *rmi_dev = fn->rmi_dev;
0242 int error;
0243
0244 device_initialize(&fn->dev);
0245
0246 dev_set_name(&fn->dev, "%s.fn%02x",
0247 dev_name(&rmi_dev->dev), fn->fd.function_number);
0248
0249 fn->dev.parent = &rmi_dev->dev;
0250 fn->dev.type = &rmi_function_type;
0251 fn->dev.bus = &rmi_bus_type;
0252
0253 error = device_add(&fn->dev);
0254 if (error) {
0255 dev_err(&rmi_dev->dev,
0256 "Failed device_register function device %s\n",
0257 dev_name(&fn->dev));
0258 goto err_put_device;
0259 }
0260
0261 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Registered F%02X.\n",
0262 fn->fd.function_number);
0263
0264 return 0;
0265
0266 err_put_device:
0267 put_device(&fn->dev);
0268 return error;
0269 }
0270
0271 void rmi_unregister_function(struct rmi_function *fn)
0272 {
0273 int i;
0274
0275 rmi_dbg(RMI_DEBUG_CORE, &fn->dev, "Unregistering F%02X.\n",
0276 fn->fd.function_number);
0277
0278 device_del(&fn->dev);
0279 of_node_put(fn->dev.of_node);
0280 put_device(&fn->dev);
0281
0282 for (i = 0; i < fn->num_of_irqs; i++)
0283 irq_dispose_mapping(fn->irq[i]);
0284
0285 }
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297 int __rmi_register_function_handler(struct rmi_function_handler *handler,
0298 struct module *owner,
0299 const char *mod_name)
0300 {
0301 struct device_driver *driver = &handler->driver;
0302 int error;
0303
0304 driver->bus = &rmi_bus_type;
0305 driver->owner = owner;
0306 driver->mod_name = mod_name;
0307 driver->probe = rmi_function_probe;
0308 driver->remove = rmi_function_remove;
0309
0310 error = driver_register(driver);
0311 if (error) {
0312 pr_err("driver_register() failed for %s, error: %d\n",
0313 driver->name, error);
0314 return error;
0315 }
0316
0317 return 0;
0318 }
0319 EXPORT_SYMBOL_GPL(__rmi_register_function_handler);
0320
0321
0322
0323
0324
0325
0326
0327
0328 void rmi_unregister_function_handler(struct rmi_function_handler *handler)
0329 {
0330 driver_unregister(&handler->driver);
0331 }
0332 EXPORT_SYMBOL_GPL(rmi_unregister_function_handler);
0333
0334
0335
0336 static int rmi_bus_match(struct device *dev, struct device_driver *drv)
0337 {
0338 bool physical = rmi_is_physical_device(dev);
0339
0340
0341 if (physical != rmi_is_physical_driver(drv))
0342 return 0;
0343
0344 return physical || rmi_function_match(dev, drv);
0345 }
0346
0347 struct bus_type rmi_bus_type = {
0348 .match = rmi_bus_match,
0349 .name = "rmi4",
0350 };
0351
0352 static struct rmi_function_handler *fn_handlers[] = {
0353 &rmi_f01_handler,
0354 #ifdef CONFIG_RMI4_F03
0355 &rmi_f03_handler,
0356 #endif
0357 #ifdef CONFIG_RMI4_F11
0358 &rmi_f11_handler,
0359 #endif
0360 #ifdef CONFIG_RMI4_F12
0361 &rmi_f12_handler,
0362 #endif
0363 #ifdef CONFIG_RMI4_F30
0364 &rmi_f30_handler,
0365 #endif
0366 #ifdef CONFIG_RMI4_F34
0367 &rmi_f34_handler,
0368 #endif
0369 #ifdef CONFIG_RMI4_F3A
0370 &rmi_f3a_handler,
0371 #endif
0372 #ifdef CONFIG_RMI4_F54
0373 &rmi_f54_handler,
0374 #endif
0375 #ifdef CONFIG_RMI4_F55
0376 &rmi_f55_handler,
0377 #endif
0378 };
0379
0380 static void __rmi_unregister_function_handlers(int start_idx)
0381 {
0382 int i;
0383
0384 for (i = start_idx; i >= 0; i--)
0385 rmi_unregister_function_handler(fn_handlers[i]);
0386 }
0387
0388 static void rmi_unregister_function_handlers(void)
0389 {
0390 __rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers) - 1);
0391 }
0392
0393 static int rmi_register_function_handlers(void)
0394 {
0395 int ret;
0396 int i;
0397
0398 for (i = 0; i < ARRAY_SIZE(fn_handlers); i++) {
0399 ret = rmi_register_function_handler(fn_handlers[i]);
0400 if (ret) {
0401 pr_err("%s: error registering the RMI F%02x handler: %d\n",
0402 __func__, fn_handlers[i]->func, ret);
0403 goto err_unregister_function_handlers;
0404 }
0405 }
0406
0407 return 0;
0408
0409 err_unregister_function_handlers:
0410 __rmi_unregister_function_handlers(i - 1);
0411 return ret;
0412 }
0413
0414 int rmi_of_property_read_u32(struct device *dev, u32 *result,
0415 const char *prop, bool optional)
0416 {
0417 int retval;
0418 u32 val = 0;
0419
0420 retval = of_property_read_u32(dev->of_node, prop, &val);
0421 if (retval && (!optional && retval == -EINVAL)) {
0422 dev_err(dev, "Failed to get %s value: %d\n",
0423 prop, retval);
0424 return retval;
0425 }
0426 *result = val;
0427
0428 return 0;
0429 }
0430 EXPORT_SYMBOL_GPL(rmi_of_property_read_u32);
0431
0432 static int __init rmi_bus_init(void)
0433 {
0434 int error;
0435
0436 error = bus_register(&rmi_bus_type);
0437 if (error) {
0438 pr_err("%s: error registering the RMI bus: %d\n",
0439 __func__, error);
0440 return error;
0441 }
0442
0443 error = rmi_register_function_handlers();
0444 if (error)
0445 goto err_unregister_bus;
0446
0447 error = rmi_register_physical_driver();
0448 if (error) {
0449 pr_err("%s: error registering the RMI physical driver: %d\n",
0450 __func__, error);
0451 goto err_unregister_bus;
0452 }
0453
0454 return 0;
0455
0456 err_unregister_bus:
0457 bus_unregister(&rmi_bus_type);
0458 return error;
0459 }
0460 module_init(rmi_bus_init);
0461
0462 static void __exit rmi_bus_exit(void)
0463 {
0464
0465
0466
0467
0468
0469 rmi_unregister_physical_driver();
0470 rmi_unregister_function_handlers();
0471 bus_unregister(&rmi_bus_type);
0472 }
0473 module_exit(rmi_bus_exit);
0474
0475 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
0476 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com");
0477 MODULE_DESCRIPTION("RMI bus");
0478 MODULE_LICENSE("GPL");