ts: rk29_i2c_goodix: goodix_init_panel retry write cfg info when error
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / rmi4 / rmi_bus.c
1 /*
2  * Copyright (c) 2011 Synaptics Incorporated
3  * Copyright (c) 2011 Unixphere
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <linux/pm.h>
23 #include <linux/slab.h>
24 #include <linux/list.h>
25 #include <linux/rmi.h>
26 #include <linux/types.h>
27 #ifdef CONFIG_RMI4_DEBUG
28 #include <linux/debugfs.h>
29 #endif
30 #include "rmi_driver.h"
31 DEFINE_MUTEX(rmi_bus_mutex);
32
33 static struct rmi_function_list {
34         struct list_head list;
35         struct rmi_function_handler *fh;
36 } rmi_supported_functions;
37
38 static struct rmi_character_driver_list {
39         struct list_head list;
40         struct rmi_char_driver *cd;
41 } rmi_character_drivers;
42
43 static atomic_t physical_device_count;
44
45 #ifdef CONFIG_RMI4_DEBUG
46 static struct dentry *rmi_debugfs_root;
47 #endif
48
49 static int rmi_bus_match(struct device *dev, struct device_driver *driver)
50 {
51         struct rmi_driver *rmi_driver;
52         struct rmi_device *rmi_dev;
53         struct rmi_device_platform_data *pdata;
54
55         rmi_driver = to_rmi_driver(driver);
56         rmi_dev = to_rmi_device(dev);
57         pdata = to_rmi_platform_data(rmi_dev);
58         dev_dbg(dev, "%s: Matching %s.\n", __func__, pdata->sensor_name);
59
60         if (!strcmp(pdata->driver_name, rmi_driver->driver.name)) {
61                 rmi_dev->driver = rmi_driver;
62                 dev_dbg(dev, "%s: Match %s to %s succeeded.\n", __func__,
63                         pdata->driver_name, rmi_driver->driver.name);
64                 return 1;
65         }
66
67         dev_vdbg(dev, "%s: Match %s to %s failed.\n", __func__,
68                 pdata->driver_name, rmi_driver->driver.name);
69         return 0;
70 }
71
72 #ifdef CONFIG_PM
73 static int rmi_bus_suspend(struct device *dev)
74 {
75 #ifdef GENERIC_SUBSYS_PM_OPS
76         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
77
78         if (pm && pm->suspend)
79                 return pm->suspend(dev);
80 #endif
81
82         return 0;
83 }
84
85 static int rmi_bus_resume(struct device *dev)
86 {
87 #ifdef GENERIC_SUBSYS_PM_OPS
88         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
89
90         if (pm && pm->resume)
91                 return pm->resume(dev);
92         else if (dev->driver && dev->driver->resume)
93                 return dev->driver->resume(dev);
94 #else
95         if (dev->driver && dev->driver->resume)
96                 return dev->driver->resume(dev);
97 #endif
98
99         return 0;
100 }
101 #endif
102
103 static int rmi_bus_probe(struct device *dev)
104 {
105         struct rmi_driver *driver;
106         struct rmi_device *rmi_dev = to_rmi_device(dev);
107
108         driver = rmi_dev->driver;
109         if (driver && driver->probe)
110                 return driver->probe(rmi_dev);
111
112         return 0;
113 }
114
115 static int rmi_bus_remove(struct device *dev)
116 {
117         struct rmi_driver *driver;
118         struct rmi_device *rmi_dev = to_rmi_device(dev);
119
120         driver = rmi_dev->driver;
121         if (driver && driver->remove)
122                 return driver->remove(rmi_dev);
123
124         return 0;
125 }
126
127 static void rmi_bus_shutdown(struct device *dev)
128 {
129         struct rmi_driver *driver;
130         struct rmi_device *rmi_dev = to_rmi_device(dev);
131
132         driver = rmi_dev->driver;
133         if (driver && driver->shutdown)
134                 driver->shutdown(rmi_dev);
135 }
136
137 static SIMPLE_DEV_PM_OPS(rmi_bus_pm_ops,
138                          rmi_bus_suspend, rmi_bus_resume);
139
140 struct bus_type rmi_bus_type = {
141         .name           = "rmi",
142         .match          = rmi_bus_match,
143         .probe          = rmi_bus_probe,
144         .remove         = rmi_bus_remove,
145         .shutdown       = rmi_bus_shutdown,
146         .pm             = &rmi_bus_pm_ops
147 };
148
149 static void release_rmidev_device(struct device *dev) {
150         device_unregister(dev);
151 }
152
153 int rmi_register_phys_device(struct rmi_phys_device *phys)
154 {
155         struct rmi_device_platform_data *pdata = phys->dev->platform_data;
156         struct rmi_device *rmi_dev;
157
158         if (!pdata) {
159                 dev_err(phys->dev, "no platform data!\n");
160                 return -EINVAL;
161         }
162
163         rmi_dev = kzalloc(sizeof(struct rmi_device), GFP_KERNEL);
164         if (!rmi_dev)
165                 return -ENOMEM;
166
167         rmi_dev->phys = phys;
168         rmi_dev->dev.bus = &rmi_bus_type;
169
170         rmi_dev->number = atomic_inc_return(&physical_device_count) - 1;
171         rmi_dev->dev.release = release_rmidev_device;
172
173         dev_set_name(&rmi_dev->dev, "sensor%02d", rmi_dev->number);
174         dev_dbg(phys->dev, "%s: Registered %s as %s.\n", __func__,
175                 pdata->sensor_name, dev_name(&rmi_dev->dev));
176
177 #ifdef CONFIG_RMI4_DEBUG
178         if (rmi_debugfs_root) {
179                 rmi_dev->debugfs_root = debugfs_create_dir(
180                         dev_name(&rmi_dev->dev), rmi_debugfs_root);
181                 if (!rmi_dev->debugfs_root)
182                         dev_err(&rmi_dev->dev, "Failed to create debugfs root.\n");
183         }
184 #endif
185         phys->rmi_dev = rmi_dev;
186         return device_register(&rmi_dev->dev);
187 }
188 EXPORT_SYMBOL(rmi_register_phys_device);
189
190 void rmi_unregister_phys_device(struct rmi_phys_device *phys)
191 {
192         struct rmi_device *rmi_dev = phys->rmi_dev;
193
194 #ifdef CONFIG_RMI4_DEBUG
195         if (rmi_dev->debugfs_root)
196                 debugfs_remove(rmi_dev->debugfs_root);
197 #endif
198
199         kfree(rmi_dev);
200 }
201 EXPORT_SYMBOL(rmi_unregister_phys_device);
202
203 int rmi_register_driver(struct rmi_driver *driver)
204 {
205         driver->driver.bus = &rmi_bus_type;
206         return driver_register(&driver->driver);
207 }
208 EXPORT_SYMBOL(rmi_register_driver);
209
210 static int __rmi_driver_remove(struct device *dev, void *data)
211 {
212         struct rmi_driver *driver = data;
213         struct rmi_device *rmi_dev = to_rmi_device(dev);
214
215         if (rmi_dev->driver == driver)
216                 rmi_dev->driver = NULL;
217
218         return 0;
219 }
220
221 void rmi_unregister_driver(struct rmi_driver *driver)
222 {
223         bus_for_each_dev(&rmi_bus_type, NULL, driver, __rmi_driver_remove);
224         driver_unregister(&driver->driver);
225 }
226 EXPORT_SYMBOL(rmi_unregister_driver);
227
228 static int __rmi_bus_fh_add(struct device *dev, void *data)
229 {
230         struct rmi_driver *driver;
231         struct rmi_device *rmi_dev = to_rmi_device(dev);
232
233         driver = rmi_dev->driver;
234         if (driver && driver->fh_add)
235                 driver->fh_add(rmi_dev, data);
236
237         return 0;
238 }
239
240 int rmi_register_function_driver(struct rmi_function_handler *fh)
241 {
242         struct rmi_function_list *entry;
243         struct rmi_function_handler *fh_dup;
244
245         fh_dup = rmi_get_function_handler(fh->func);
246         if (fh_dup) {
247                 pr_err("%s: function f%.2x already registered!\n", __func__,
248                         fh->func);
249                 return -EINVAL;
250         }
251
252         entry = kzalloc(sizeof(struct rmi_function_list), GFP_KERNEL);
253         if (!entry)
254                 return -ENOMEM;
255
256         entry->fh = fh;
257         INIT_LIST_HEAD(&entry->list);
258         list_add_tail(&entry->list, &rmi_supported_functions.list);
259
260         /* notify devices of the new function handler */
261         bus_for_each_dev(&rmi_bus_type, NULL, fh, __rmi_bus_fh_add);
262
263         return 0;
264 }
265 EXPORT_SYMBOL(rmi_register_function_driver);
266
267 static int __rmi_bus_fh_remove(struct device *dev, void *data)
268 {
269         struct rmi_driver *driver;
270         struct rmi_device *rmi_dev = to_rmi_device(dev);
271
272         driver = rmi_dev->driver;
273         if (driver && driver->fh_remove)
274                 driver->fh_remove(rmi_dev, data);
275
276         return 0;
277 }
278
279 void rmi_unregister_function_driver(struct rmi_function_handler *fh)
280 {
281         struct rmi_function_list *entry, *n;
282
283         /* notify devices of the removal of the function handler */
284         bus_for_each_dev(&rmi_bus_type, NULL, fh, __rmi_bus_fh_remove);
285
286         if (list_empty(&rmi_supported_functions.list))
287                 return;
288
289         list_for_each_entry_safe(entry, n, &rmi_supported_functions.list,
290                                                                         list) {
291                 if (entry->fh->func == fh->func) {
292                         list_del(&entry->list);
293                         kfree(entry);
294                 }
295         }
296
297 }
298 EXPORT_SYMBOL(rmi_unregister_function_driver);
299
300 struct rmi_function_handler *rmi_get_function_handler(int id)
301 {
302         struct rmi_function_list *entry;
303
304         if (list_empty(&rmi_supported_functions.list))
305                 return NULL;
306
307         list_for_each_entry(entry, &rmi_supported_functions.list, list)
308                 if (entry->fh->func == id)
309                         return entry->fh;
310
311         return NULL;
312 }
313 EXPORT_SYMBOL(rmi_get_function_handler);
314
315 static void rmi_release_character_device(struct device *dev)
316 {
317         dev_dbg(dev, "%s: Called.\n", __func__);
318         return;
319 }
320
321 static int rmi_register_character_device(struct device *dev, void *data)
322 {
323         struct rmi_device *rmi_dev;
324         struct rmi_char_driver *char_driver = data;
325         struct rmi_char_device *char_dev;
326         int retval;
327
328         dev_dbg(dev, "Attaching character device.\n");
329         rmi_dev = to_rmi_device(dev);
330         if (char_driver->match && !char_driver->match(rmi_dev))
331                 return 0;
332
333         if (!char_driver->init) {
334                 dev_err(dev, "ERROR: No init() function in %s.\n", __func__);
335                 return -EINVAL;
336         }
337
338         char_dev = kzalloc(sizeof(struct rmi_char_device), GFP_KERNEL);
339         if (!char_dev)
340                 return -ENOMEM;
341
342         char_dev->rmi_dev = rmi_dev;
343         char_dev->driver = char_driver;
344
345         char_dev->dev.parent = dev;
346         char_dev->dev.release = rmi_release_character_device;
347         char_dev->dev.driver = &char_driver->driver;
348         retval = device_register(&char_dev->dev);
349         if (!retval) {
350                 dev_err(dev, "Failed to register character device.\n");
351                 goto error_exit;
352         }
353
354         retval = char_driver->init(char_dev);
355         if (retval) {
356                 dev_err(dev, "Failed to initialize character device.\n");
357                 goto error_exit;
358         }
359
360         mutex_lock(&rmi_bus_mutex);
361         list_add_tail(&char_dev->list, &char_driver->devices);
362         mutex_unlock(&rmi_bus_mutex);
363         dev_info(&char_dev->dev, "Registered a device.\n");
364         return retval;
365
366 error_exit:
367         kfree(char_dev);
368         return retval;
369 }
370
371 int rmi_register_character_driver(struct rmi_char_driver *char_driver)
372 {
373         struct rmi_character_driver_list *entry;
374         int retval;
375
376         pr_debug("%s: Registering character driver %s.\n", __func__,
377                 char_driver->driver.name);
378
379         char_driver->driver.bus = &rmi_bus_type;
380         INIT_LIST_HEAD(&char_driver->devices);
381         retval = driver_register(&char_driver->driver);
382         if (retval) {
383                 pr_err("%s: Failed to register %s, code: %d.\n", __func__,
384                        char_driver->driver.name, retval);
385                 return retval;
386         }
387
388         entry = kzalloc(sizeof(struct rmi_character_driver_list), GFP_KERNEL);
389         if (!entry)
390                 return -ENOMEM;
391         entry->cd = char_driver;
392
393         mutex_lock(&rmi_bus_mutex);
394         list_add_tail(&entry->list, &rmi_character_drivers.list);
395         mutex_unlock(&rmi_bus_mutex);
396
397         /* notify devices of the removal of the function handler */
398         bus_for_each_dev(&rmi_bus_type, NULL, char_driver,
399                          rmi_register_character_device);
400
401         return 0;
402 }
403 EXPORT_SYMBOL(rmi_register_character_driver);
404
405
406 int rmi_unregister_character_driver(struct rmi_char_driver *char_driver)
407 {
408         struct rmi_character_driver_list *entry, *n;
409         struct rmi_char_device *char_dev, *m;
410         pr_debug("%s: Unregistering character driver %s.\n", __func__,
411                 char_driver->driver.name);
412
413         mutex_lock(&rmi_bus_mutex);
414         list_for_each_entry_safe(char_dev, m, &char_driver->devices,
415                                  list) {
416                 list_del(&char_dev->list);
417                 char_dev->driver->remove(char_dev);
418         }
419         list_for_each_entry_safe(entry, n, &rmi_character_drivers.list,
420                                  list) {
421                 if (entry->cd == char_driver) {
422                         list_del(&entry->list);
423                         kfree(entry);
424                 }
425         }
426         mutex_unlock(&rmi_bus_mutex);
427
428         driver_unregister(&char_driver->driver);
429
430         return 0;
431 }
432 EXPORT_SYMBOL(rmi_unregister_character_driver);
433
434 static int __init rmi_bus_init(void)
435 {
436         int error;
437
438         mutex_init(&rmi_bus_mutex);
439         INIT_LIST_HEAD(&rmi_supported_functions.list);
440         INIT_LIST_HEAD(&rmi_character_drivers.list);
441
442 #ifdef CONFIG_RMI4_DEBUG
443         rmi_debugfs_root = debugfs_create_dir(rmi_bus_type.name, NULL);
444         if (!rmi_debugfs_root)
445                 pr_err("%s: Failed to create debugfs root.\n", __func__);
446         else if (IS_ERR(rmi_debugfs_root)) {
447                 pr_err("%s: Kernel may not contain debugfs support, code=%ld\n",
448                        __func__, PTR_ERR(rmi_debugfs_root));
449                 rmi_debugfs_root = NULL;
450         }
451 #endif
452
453         error = bus_register(&rmi_bus_type);
454         if (error < 0) {
455                 pr_err("%s: error registering the RMI bus: %d\n", __func__,
456                        error);
457                 return error;
458         }
459         pr_debug("%s: successfully registered RMI bus.\n", __func__);
460
461         return 0;
462 }
463
464 static void __exit rmi_bus_exit(void)
465 {
466         /* We should only ever get here if all drivers are unloaded, so
467          * all we have to do at this point is unregister ourselves.
468          */
469 #ifdef CONFIG_RMI4_DEBUG
470         if (rmi_debugfs_root)
471                 debugfs_remove(rmi_debugfs_root);
472 #endif
473         bus_unregister(&rmi_bus_type);
474 }
475
476 module_init(rmi_bus_init);
477 module_exit(rmi_bus_exit);
478
479 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
480 MODULE_DESCRIPTION("RMI bus");
481 MODULE_LICENSE("GPL");
482 MODULE_VERSION(RMI_DRIVER_VERSION);