summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/rmi4/rmi_bus.c
blob: 6a269df4ff359d0394b552098abd3c05e7ebe64d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (c) 2011 Synaptics Incorporated
 * Copyright (c) 2011 Unixphere
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/pm.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/rmi.h>

static struct rmi_function_list {
	struct list_head list;
	struct rmi_function_handler *fh;
} rmi_supported_functions;

static int rmi_bus_match(struct device *dev, struct device_driver *driver)
{
	struct rmi_driver *rmi_driver;
	struct rmi_device *rmi_dev;
	struct rmi_device_platform_data *pdata;

	pr_info("in function ____%s____  \n", __func__);
	rmi_driver = to_rmi_driver(driver);
	rmi_dev = to_rmi_device(dev);
	pdata = to_rmi_platform_data(rmi_dev);

	pr_info("        rmi_driver->driver.name =  %s\n", rmi_driver->driver.name);
        pr_info("        device:rmi_device = 0x%x \n", rmi_dev);
	pr_info("             device:rmi_device:rmi_device_platform_data:driver_name = %s \n", pdata->driver_name);
        pr_info("                    rmi_device:driver = 0x%x \n", rmi_dev->driver);

	if (!strcmp(pdata->driver_name, rmi_driver->driver.name)) {
		rmi_dev->driver = rmi_driver;
		pr_info("             names match, so now rmi_device:driver = 0x%x \n",rmi_dev->driver);
		return 1;
	}
	pr_info("             names DO NOT match, so return nothing \n");

	return 0;
}

#ifdef CONFIG_PM
static int rmi_bus_suspend(struct device *dev)
{
#ifdef GENERIC_SUBSYS_PM_OPS
	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;

	if (pm && pm->suspend)
		return pm->suspend(dev);
#endif

	return 0;
}

static int rmi_bus_resume(struct device *dev)
{
#ifdef GENERIC_SUBSYS_PM_OPS
	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
	pr_info("in function ____%s____  \n", __func__);

	if (pm && pm->resume)
		return pm->resume(dev);
#endif

	return 0;
}
#endif

static int rmi_bus_probe(struct device *dev)
{
	struct rmi_driver *driver;
	struct rmi_device *rmi_dev = to_rmi_device(dev);

	pr_info("in function ____%s____  \n", __func__);
	driver = rmi_dev->driver;
	if (driver && driver->probe)
		return driver->probe(rmi_dev);

	return 0;
}

static int rmi_bus_remove(struct device *dev)
{
	struct rmi_driver *driver;
	struct rmi_device *rmi_dev = to_rmi_device(dev);

	pr_info("in function ____%s____  \n", __func__);
	driver = rmi_dev->driver;
	if (driver && driver->remove)
		return driver->remove(rmi_dev);

	return 0;
}

static void rmi_bus_shutdown(struct device *dev)
{
	struct rmi_driver *driver;
	struct rmi_device *rmi_dev = to_rmi_device(dev);

	driver = rmi_dev->driver;
	if (driver && driver->shutdown)
		driver->shutdown(rmi_dev);
}

static SIMPLE_DEV_PM_OPS(rmi_bus_pm_ops,
			 rmi_bus_suspend, rmi_bus_resume);

struct bus_type rmi_bus_type = {
	.name		= "rmi",
	.match		= rmi_bus_match,
	.probe		= rmi_bus_probe,
	.remove		= rmi_bus_remove,
	.shutdown	= rmi_bus_shutdown,
	.pm		= &rmi_bus_pm_ops
};

int rmi_register_phys_device(struct rmi_phys_device *phys)
{
	static int phys_device_num;
	struct rmi_device_platform_data *pdata = phys->dev->platform_data;
	struct rmi_device *rmi_dev;

	pr_info("in function ____%s____  \n", __func__);

	if (!pdata) {
		dev_err(phys->dev, "no platform data!\n");
		return -EINVAL;
	}

	rmi_dev = kzalloc(sizeof(struct rmi_device), GFP_KERNEL);
	if (!rmi_dev)
		return -ENOMEM;

	rmi_dev->phys = phys;
	rmi_dev->dev.bus = &rmi_bus_type;
	dev_set_name(&rmi_dev->dev, "sensor%02d", phys_device_num++);

	phys->rmi_dev = rmi_dev;
	pr_info("                 registering physical device:\n");
	pr_info("                 dev.init_name = \n", rmi_dev->dev.init_name);
	pr_info("                 dev.bus->name = \n", rmi_dev->dev.bus->name);
	return device_register(&rmi_dev->dev);
}
EXPORT_SYMBOL(rmi_register_phys_device);

void rmi_unregister_phys_device(struct rmi_phys_device *phys)
{
	struct rmi_device *rmi_dev = phys->rmi_dev;
	pr_info("in function ____%s____  \n", __func__);

	device_unregister(&rmi_dev->dev);
	kfree(rmi_dev);
}
EXPORT_SYMBOL(rmi_unregister_phys_device);

int rmi_register_driver(struct rmi_driver *driver)
{
	pr_info("in function ____%s____  \n", __func__);
	driver->driver.bus = &rmi_bus_type;
	return driver_register(&driver->driver);
}
EXPORT_SYMBOL(rmi_register_driver);

static int __rmi_driver_remove(struct device *dev, void *data)
{
	struct rmi_driver *driver = data;
	struct rmi_device *rmi_dev = to_rmi_device(dev);

	if (rmi_dev->driver == driver)
		rmi_dev->driver = NULL;

	return 0;
}

void rmi_unregister_driver(struct rmi_driver *driver)
{
	bus_for_each_dev(&rmi_bus_type, NULL, driver, __rmi_driver_remove);
	driver_unregister(&driver->driver);
}
EXPORT_SYMBOL(rmi_unregister_driver);

static int __rmi_bus_fh_add(struct device *dev, void *data)
{
	struct rmi_driver *driver;
	struct rmi_device *rmi_dev = to_rmi_device(dev);
	pr_info("in function ____%s____  \n", __func__);

	driver = rmi_dev->driver;
	if (driver && driver->fh_add)
		driver->fh_add(rmi_dev, data);

	return 0;
}

int rmi_register_function_driver(struct rmi_function_handler *fh)
{
	struct rmi_function_list *entry;
	struct rmi_function_handler *fh_dup;

	fh_dup = rmi_get_function_handler(fh->func);
	if (fh_dup) {
		pr_err("%s: function f%.2x already registered!\n", __func__,
			fh->func);
		return -EINVAL;
	}

	entry = kzalloc(sizeof(struct rmi_function_list), GFP_KERNEL);
	if (!entry)
		return -ENOMEM;

	entry->fh = fh;
	list_add_tail(&entry->list, &rmi_supported_functions.list);

	/* notify devices of the new function handler */
	bus_for_each_dev(&rmi_bus_type, NULL, fh, __rmi_bus_fh_add);

	return 0;
}
EXPORT_SYMBOL(rmi_register_function_driver);

static int __rmi_bus_fh_remove(struct device *dev, void *data)
{
	struct rmi_driver *driver;
	struct rmi_device *rmi_dev = to_rmi_device(dev);

	pr_info("in function ____%s____  \n", __func__);
	driver = rmi_dev->driver;
	if (driver && driver->fh_remove)
		driver->fh_remove(rmi_dev, data);

	return 0;
}

void rmi_unregister_function_driver(struct rmi_function_handler *fh)
{
	struct rmi_function_list *entry, *n;
	pr_info("in function ____%s____  \n", __func__);

	/* notify devices of the removal of the function handler */
	bus_for_each_dev(&rmi_bus_type, NULL, fh, __rmi_bus_fh_remove);

	list_for_each_entry_safe(entry, n, &rmi_supported_functions.list, list)
		if (entry->fh->func == fh->func) {
			list_del(&entry->list);
			kfree(entry);
		}
}
EXPORT_SYMBOL(rmi_unregister_function_driver);

struct rmi_function_handler *rmi_get_function_handler(int id)
{
	struct rmi_function_list *entry;
	pr_info("in function ____%s____  \n", __func__);

	list_for_each_entry(entry, &rmi_supported_functions.list, list)
		if (entry->fh->func == id)
			return entry->fh;

	return NULL;
}
EXPORT_SYMBOL(rmi_get_function_handler);

static int __init rmi_bus_init(void)
{
	int error;

	pr_info("in function ____%s____  \n", __func__);
	INIT_LIST_HEAD(&rmi_supported_functions.list);

	error = bus_register(&rmi_bus_type);
	if (error < 0) {
		pr_err("%s: error registering the RMI bus: %d\n", __func__,
		       error);
		return error;
	}
	pr_info("%s: successfully registered RMI bus.\n", __func__);

	return 0;
}

static void __exit rmi_bus_exit(void)
{
	struct rmi_function_list *entry, *n;
	pr_info("in function ____%s____  \n", __func__);

	list_for_each_entry_safe(entry, n, &rmi_supported_functions.list,
				 list) {
		list_del(&entry->list);
		kfree(entry);
	}

	bus_unregister(&rmi_bus_type);
}

module_init(rmi_bus_init);
module_exit(rmi_bus_exit);

MODULE_AUTHOR("Eric Andersson <eric.andersson@unixphere.com>");
MODULE_DESCRIPTION("RMI bus");
MODULE_LICENSE("GPL");