2 * Greybus Vibrator protocol driver.
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/device.h>
14 #include <linux/kdev_t.h>
15 #include <linux/idr.h>
16 #include <linux/pm_runtime.h>
20 struct gb_vibrator_device {
21 struct gb_connection *connection;
23 int minor; /* vibrator minor number */
26 /* Greybus Vibrator operation types */
27 #define GB_VIBRATOR_TYPE_ON 0x02
28 #define GB_VIBRATOR_TYPE_OFF 0x03
30 struct gb_vibrator_on_request {
34 static int turn_on(struct gb_vibrator_device *vib, u16 timeout_ms)
36 struct gb_vibrator_on_request request;
37 struct gb_bundle *bundle = vib->connection->bundle;
40 ret = gb_pm_runtime_get_sync(bundle);
44 request.timeout_ms = cpu_to_le16(timeout_ms);
45 ret = gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_ON,
46 &request, sizeof(request), NULL, 0);
48 gb_pm_runtime_put_autosuspend(bundle);
53 static int turn_off(struct gb_vibrator_device *vib)
55 struct gb_bundle *bundle = vib->connection->bundle;
58 ret = gb_pm_runtime_get_sync(bundle);
62 ret = gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_OFF,
65 gb_pm_runtime_put_autosuspend(bundle);
70 static ssize_t timeout_store(struct device *dev, struct device_attribute *attr,
71 const char *buf, size_t count)
73 struct gb_vibrator_device *vib = dev_get_drvdata(dev);
77 retval = kstrtoul(buf, 10, &val);
79 dev_err(dev, "could not parse timeout value %d\n", retval);
84 retval = turn_on(vib, (u16)val);
86 retval = turn_off(vib);
92 static DEVICE_ATTR_WO(timeout);
94 static struct attribute *vibrator_attrs[] = {
95 &dev_attr_timeout.attr,
98 ATTRIBUTE_GROUPS(vibrator);
100 static struct class vibrator_class = {
102 .owner = THIS_MODULE,
103 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
104 .dev_groups = vibrator_groups,
108 static DEFINE_IDA(minors);
110 static int gb_vibrator_probe(struct gb_bundle *bundle,
111 const struct greybus_bundle_id *id)
113 struct greybus_descriptor_cport *cport_desc;
114 struct gb_connection *connection;
115 struct gb_vibrator_device *vib;
119 if (bundle->num_cports != 1)
122 cport_desc = &bundle->cport_desc[0];
123 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_VIBRATOR)
126 vib = kzalloc(sizeof(*vib), GFP_KERNEL);
130 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
132 if (IS_ERR(connection)) {
133 retval = PTR_ERR(connection);
136 gb_connection_set_data(connection, vib);
138 vib->connection = connection;
140 greybus_set_drvdata(bundle, vib);
142 retval = gb_connection_enable(connection);
144 goto err_connection_destroy;
147 * For now we create a device in sysfs for the vibrator, but odds are
148 * there is a "real" device somewhere in the kernel for this, but I
149 * can't find it at the moment...
151 vib->minor = ida_simple_get(&minors, 0, 0, GFP_KERNEL);
152 if (vib->minor < 0) {
154 goto err_connection_disable;
156 dev = device_create(&vibrator_class, &bundle->dev,
157 MKDEV(0, 0), vib, "vibrator%d", vib->minor);
164 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0)
166 * Newer kernels handle this in a race-free manner, by the dev_groups
167 * field in the struct class up above. But for older kernels, we need
168 * to "open code this :(
170 retval = sysfs_create_group(&dev->kobj, vibrator_groups[0]);
172 device_unregister(dev);
177 gb_pm_runtime_put_autosuspend(bundle);
182 ida_simple_remove(&minors, vib->minor);
183 err_connection_disable:
184 gb_connection_disable(connection);
185 err_connection_destroy:
186 gb_connection_destroy(connection);
193 static void gb_vibrator_disconnect(struct gb_bundle *bundle)
195 struct gb_vibrator_device *vib = greybus_get_drvdata(bundle);
198 ret = gb_pm_runtime_get_sync(bundle);
200 gb_pm_runtime_get_noresume(bundle);
202 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0)
203 sysfs_remove_group(&vib->dev->kobj, vibrator_groups[0]);
205 device_unregister(vib->dev);
206 ida_simple_remove(&minors, vib->minor);
207 gb_connection_disable(vib->connection);
208 gb_connection_destroy(vib->connection);
212 static const struct greybus_bundle_id gb_vibrator_id_table[] = {
213 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_VIBRATOR) },
216 MODULE_DEVICE_TABLE(greybus, gb_vibrator_id_table);
218 static struct greybus_driver gb_vibrator_driver = {
220 .probe = gb_vibrator_probe,
221 .disconnect = gb_vibrator_disconnect,
222 .id_table = gb_vibrator_id_table,
225 static __init int gb_vibrator_init(void)
229 retval = class_register(&vibrator_class);
233 retval = greybus_register(&gb_vibrator_driver);
235 goto err_class_unregister;
239 err_class_unregister:
240 class_unregister(&vibrator_class);
244 module_init(gb_vibrator_init);
246 static __exit void gb_vibrator_exit(void)
248 greybus_deregister(&gb_vibrator_driver);
249 class_unregister(&vibrator_class);
250 ida_destroy(&minors);
252 module_exit(gb_vibrator_exit);
254 MODULE_LICENSE("GPL v2");