1 // SPDX-License-Identifier: GPL-2.0
3 * Greybus Vibrator protocol driver.
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/device.h>
13 #include <linux/kdev_t.h>
14 #include <linux/idr.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/greybus.h>
18 struct gb_vibrator_device {
19 struct gb_connection *connection;
21 int minor; /* vibrator minor number */
22 struct delayed_work delayed_work;
25 /* Greybus Vibrator operation types */
26 #define GB_VIBRATOR_TYPE_ON 0x02
27 #define GB_VIBRATOR_TYPE_OFF 0x03
29 static int turn_off(struct gb_vibrator_device *vib)
31 struct gb_bundle *bundle = vib->connection->bundle;
34 ret = gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_OFF,
37 gb_pm_runtime_put_autosuspend(bundle);
42 static int turn_on(struct gb_vibrator_device *vib, u16 timeout_ms)
44 struct gb_bundle *bundle = vib->connection->bundle;
47 ret = gb_pm_runtime_get_sync(bundle);
51 /* Vibrator was switched ON earlier */
52 if (cancel_delayed_work_sync(&vib->delayed_work))
55 ret = gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_ON,
58 gb_pm_runtime_put_autosuspend(bundle);
62 schedule_delayed_work(&vib->delayed_work, msecs_to_jiffies(timeout_ms));
67 static void gb_vibrator_worker(struct work_struct *work)
69 struct delayed_work *delayed_work = to_delayed_work(work);
70 struct gb_vibrator_device *vib =
71 container_of(delayed_work,
72 struct gb_vibrator_device,
78 static ssize_t timeout_store(struct device *dev, struct device_attribute *attr,
79 const char *buf, size_t count)
81 struct gb_vibrator_device *vib = dev_get_drvdata(dev);
85 retval = kstrtoul(buf, 10, &val);
87 dev_err(dev, "could not parse timeout value %d\n", retval);
92 retval = turn_on(vib, (u16)val);
94 retval = turn_off(vib);
100 static DEVICE_ATTR_WO(timeout);
102 static struct attribute *vibrator_attrs[] = {
103 &dev_attr_timeout.attr,
106 ATTRIBUTE_GROUPS(vibrator);
108 static struct class vibrator_class = {
110 .dev_groups = vibrator_groups,
113 static DEFINE_IDA(minors);
115 static int gb_vibrator_probe(struct gb_bundle *bundle,
116 const struct greybus_bundle_id *id)
118 struct greybus_descriptor_cport *cport_desc;
119 struct gb_connection *connection;
120 struct gb_vibrator_device *vib;
124 if (bundle->num_cports != 1)
127 cport_desc = &bundle->cport_desc[0];
128 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_VIBRATOR)
131 vib = kzalloc(sizeof(*vib), GFP_KERNEL);
135 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
137 if (IS_ERR(connection)) {
138 retval = PTR_ERR(connection);
141 gb_connection_set_data(connection, vib);
143 vib->connection = connection;
145 greybus_set_drvdata(bundle, vib);
147 retval = gb_connection_enable(connection);
149 goto err_connection_destroy;
152 * For now we create a device in sysfs for the vibrator, but odds are
153 * there is a "real" device somewhere in the kernel for this, but I
154 * can't find it at the moment...
156 vib->minor = ida_simple_get(&minors, 0, 0, GFP_KERNEL);
157 if (vib->minor < 0) {
159 goto err_connection_disable;
161 dev = device_create(&vibrator_class, &bundle->dev,
162 MKDEV(0, 0), vib, "vibrator%d", vib->minor);
169 INIT_DELAYED_WORK(&vib->delayed_work, gb_vibrator_worker);
171 gb_pm_runtime_put_autosuspend(bundle);
176 ida_simple_remove(&minors, vib->minor);
177 err_connection_disable:
178 gb_connection_disable(connection);
179 err_connection_destroy:
180 gb_connection_destroy(connection);
187 static void gb_vibrator_disconnect(struct gb_bundle *bundle)
189 struct gb_vibrator_device *vib = greybus_get_drvdata(bundle);
192 ret = gb_pm_runtime_get_sync(bundle);
194 gb_pm_runtime_get_noresume(bundle);
196 if (cancel_delayed_work_sync(&vib->delayed_work))
199 device_unregister(vib->dev);
200 ida_simple_remove(&minors, vib->minor);
201 gb_connection_disable(vib->connection);
202 gb_connection_destroy(vib->connection);
206 static const struct greybus_bundle_id gb_vibrator_id_table[] = {
207 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_VIBRATOR) },
210 MODULE_DEVICE_TABLE(greybus, gb_vibrator_id_table);
212 static struct greybus_driver gb_vibrator_driver = {
214 .probe = gb_vibrator_probe,
215 .disconnect = gb_vibrator_disconnect,
216 .id_table = gb_vibrator_id_table,
219 static __init int gb_vibrator_init(void)
223 retval = class_register(&vibrator_class);
227 retval = greybus_register(&gb_vibrator_driver);
229 goto err_class_unregister;
233 err_class_unregister:
234 class_unregister(&vibrator_class);
238 module_init(gb_vibrator_init);
240 static __exit void gb_vibrator_exit(void)
242 greybus_deregister(&gb_vibrator_driver);
243 class_unregister(&vibrator_class);
244 ida_destroy(&minors);
246 module_exit(gb_vibrator_exit);
248 MODULE_LICENSE("GPL v2");