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