Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 1 | /* |
Greg Kroah-Hartman | 4efe606 | 2014-11-17 16:55:54 -0800 | [diff] [blame] | 2 | * Greybus Vibrator protocol driver. |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2014 Google Inc. |
Alex Elder | a46e967 | 2014-12-12 12:08:42 -0600 | [diff] [blame] | 5 | * Copyright 2014 Linaro Ltd. |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 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> |
Greg Kroah-Hartman | 396671b | 2014-11-17 17:12:50 -0800 | [diff] [blame] | 15 | #include <linux/idr.h> |
Ann Chen | 633e45e | 2016-07-22 15:33:55 +0800 | [diff] [blame] | 16 | #include <linux/pm_runtime.h> |
| 17 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 18 | #include "greybus.h" |
| 19 | |
| 20 | struct gb_vibrator_device { |
| 21 | struct gb_connection *connection; |
| 22 | struct device *dev; |
Greg Kroah-Hartman | 396671b | 2014-11-17 17:12:50 -0800 | [diff] [blame] | 23 | int minor; /* vibrator minor number */ |
Ann Chen | 0900845a | 2016-08-17 16:38:56 +0800 | [diff] [blame^] | 24 | struct delayed_work delayed_work; |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 25 | }; |
| 26 | |
Alex Elder | 6d65337 | 2015-05-07 13:03:52 -0500 | [diff] [blame] | 27 | /* Greybus Vibrator operation types */ |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 28 | #define GB_VIBRATOR_TYPE_ON 0x02 |
| 29 | #define GB_VIBRATOR_TYPE_OFF 0x03 |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 30 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 31 | static int turn_off(struct gb_vibrator_device *vib) |
| 32 | { |
Ann Chen | 633e45e | 2016-07-22 15:33:55 +0800 | [diff] [blame] | 33 | struct gb_bundle *bundle = vib->connection->bundle; |
| 34 | int ret; |
| 35 | |
Ann Chen | 633e45e | 2016-07-22 15:33:55 +0800 | [diff] [blame] | 36 | ret = gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_OFF, |
| 37 | NULL, 0, NULL, 0); |
| 38 | |
| 39 | gb_pm_runtime_put_autosuspend(bundle); |
| 40 | |
| 41 | return ret; |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Ann Chen | 0900845a | 2016-08-17 16:38:56 +0800 | [diff] [blame^] | 44 | static int turn_on(struct gb_vibrator_device *vib, u16 timeout_ms) |
| 45 | { |
| 46 | struct gb_bundle *bundle = vib->connection->bundle; |
| 47 | int ret; |
| 48 | |
| 49 | ret = gb_pm_runtime_get_sync(bundle); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | |
| 53 | /* Vibrator was switched ON earlier */ |
| 54 | if (cancel_delayed_work_sync(&vib->delayed_work)) |
| 55 | turn_off(vib); |
| 56 | |
| 57 | ret = gb_operation_sync(vib->connection, GB_VIBRATOR_TYPE_ON, |
| 58 | NULL, 0, NULL, 0); |
| 59 | if (ret) { |
| 60 | gb_pm_runtime_put_autosuspend(bundle); |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | schedule_delayed_work(&vib->delayed_work, msecs_to_jiffies(timeout_ms)); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static void gb_vibrator_worker(struct work_struct *work) |
| 70 | { |
| 71 | struct delayed_work *delayed_work = to_delayed_work(work); |
| 72 | struct gb_vibrator_device *vib = |
| 73 | container_of(delayed_work, struct gb_vibrator_device, delayed_work); |
| 74 | |
| 75 | turn_off(vib); |
| 76 | } |
| 77 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 78 | static ssize_t timeout_store(struct device *dev, struct device_attribute *attr, |
| 79 | const char *buf, size_t count) |
| 80 | { |
| 81 | struct gb_vibrator_device *vib = dev_get_drvdata(dev); |
| 82 | unsigned long val; |
| 83 | int retval; |
| 84 | |
| 85 | retval = kstrtoul(buf, 10, &val); |
| 86 | if (retval < 0) { |
| 87 | dev_err(dev, "could not parse timeout value %d\n", retval); |
| 88 | return retval; |
| 89 | } |
| 90 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 91 | if (val) |
| 92 | retval = turn_on(vib, (u16)val); |
| 93 | else |
| 94 | retval = turn_off(vib); |
| 95 | if (retval) |
| 96 | return retval; |
| 97 | |
| 98 | return count; |
| 99 | } |
| 100 | static DEVICE_ATTR_WO(timeout); |
| 101 | |
| 102 | static struct attribute *vibrator_attrs[] = { |
| 103 | &dev_attr_timeout.attr, |
| 104 | NULL, |
| 105 | }; |
| 106 | ATTRIBUTE_GROUPS(vibrator); |
| 107 | |
| 108 | static struct class vibrator_class = { |
| 109 | .name = "vibrator", |
| 110 | .owner = THIS_MODULE, |
| 111 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0) |
| 112 | .dev_groups = vibrator_groups, |
| 113 | #endif |
| 114 | }; |
| 115 | |
Greg Kroah-Hartman | 6b17492 | 2015-05-14 10:39:35 -0700 | [diff] [blame] | 116 | static DEFINE_IDA(minors); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 117 | |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 118 | static int gb_vibrator_probe(struct gb_bundle *bundle, |
| 119 | const struct greybus_bundle_id *id) |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 120 | { |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 121 | struct greybus_descriptor_cport *cport_desc; |
| 122 | struct gb_connection *connection; |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 123 | struct gb_vibrator_device *vib; |
| 124 | struct device *dev; |
| 125 | int retval; |
| 126 | |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 127 | if (bundle->num_cports != 1) |
| 128 | return -ENODEV; |
| 129 | |
| 130 | cport_desc = &bundle->cport_desc[0]; |
| 131 | if (cport_desc->protocol_id != GREYBUS_PROTOCOL_VIBRATOR) |
| 132 | return -ENODEV; |
| 133 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 134 | vib = kzalloc(sizeof(*vib), GFP_KERNEL); |
| 135 | if (!vib) |
| 136 | return -ENOMEM; |
| 137 | |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 138 | connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id), |
| 139 | NULL); |
| 140 | if (IS_ERR(connection)) { |
| 141 | retval = PTR_ERR(connection); |
| 142 | goto err_free_vib; |
| 143 | } |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 144 | gb_connection_set_data(connection, vib); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 145 | |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 146 | vib->connection = connection; |
| 147 | |
| 148 | greybus_set_drvdata(bundle, vib); |
| 149 | |
| 150 | retval = gb_connection_enable(connection); |
| 151 | if (retval) |
| 152 | goto err_connection_destroy; |
| 153 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 154 | /* |
Greg Kroah-Hartman | 396671b | 2014-11-17 17:12:50 -0800 | [diff] [blame] | 155 | * For now we create a device in sysfs for the vibrator, but odds are |
| 156 | * there is a "real" device somewhere in the kernel for this, but I |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 157 | * can't find it at the moment... |
| 158 | */ |
Greg Kroah-Hartman | 6b17492 | 2015-05-14 10:39:35 -0700 | [diff] [blame] | 159 | vib->minor = ida_simple_get(&minors, 0, 0, GFP_KERNEL); |
Greg Kroah-Hartman | 396671b | 2014-11-17 17:12:50 -0800 | [diff] [blame] | 160 | if (vib->minor < 0) { |
| 161 | retval = vib->minor; |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 162 | goto err_connection_disable; |
Greg Kroah-Hartman | 396671b | 2014-11-17 17:12:50 -0800 | [diff] [blame] | 163 | } |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 164 | dev = device_create(&vibrator_class, &bundle->dev, |
Greg Kroah-Hartman | 5ae2f55 | 2015-10-14 11:12:06 -0700 | [diff] [blame] | 165 | MKDEV(0, 0), vib, "vibrator%d", vib->minor); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 166 | if (IS_ERR(dev)) { |
| 167 | retval = -EINVAL; |
Greg Kroah-Hartman | 6b17492 | 2015-05-14 10:39:35 -0700 | [diff] [blame] | 168 | goto err_ida_remove; |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 169 | } |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 170 | vib->dev = dev; |
| 171 | |
| 172 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0) |
| 173 | /* |
Greg Kroah-Hartman | 4efe606 | 2014-11-17 16:55:54 -0800 | [diff] [blame] | 174 | * Newer kernels handle this in a race-free manner, by the dev_groups |
| 175 | * field in the struct class up above. But for older kernels, we need |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 176 | * to "open code this :( |
| 177 | */ |
| 178 | retval = sysfs_create_group(&dev->kobj, vibrator_groups[0]); |
| 179 | if (retval) { |
| 180 | device_unregister(dev); |
Greg Kroah-Hartman | 6b17492 | 2015-05-14 10:39:35 -0700 | [diff] [blame] | 181 | goto err_ida_remove; |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 182 | } |
| 183 | #endif |
| 184 | |
Ann Chen | 0900845a | 2016-08-17 16:38:56 +0800 | [diff] [blame^] | 185 | INIT_DELAYED_WORK(&vib->delayed_work, gb_vibrator_worker); |
| 186 | |
Ann Chen | 633e45e | 2016-07-22 15:33:55 +0800 | [diff] [blame] | 187 | gb_pm_runtime_put_autosuspend(bundle); |
| 188 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 189 | return 0; |
| 190 | |
Greg Kroah-Hartman | 6b17492 | 2015-05-14 10:39:35 -0700 | [diff] [blame] | 191 | err_ida_remove: |
| 192 | ida_simple_remove(&minors, vib->minor); |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 193 | err_connection_disable: |
| 194 | gb_connection_disable(connection); |
| 195 | err_connection_destroy: |
| 196 | gb_connection_destroy(connection); |
| 197 | err_free_vib: |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 198 | kfree(vib); |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 199 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 200 | return retval; |
| 201 | } |
| 202 | |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 203 | static void gb_vibrator_disconnect(struct gb_bundle *bundle) |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 204 | { |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 205 | struct gb_vibrator_device *vib = greybus_get_drvdata(bundle); |
Ann Chen | 633e45e | 2016-07-22 15:33:55 +0800 | [diff] [blame] | 206 | int ret; |
| 207 | |
| 208 | ret = gb_pm_runtime_get_sync(bundle); |
| 209 | if (ret) |
| 210 | gb_pm_runtime_get_noresume(bundle); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 211 | |
Ann Chen | 0900845a | 2016-08-17 16:38:56 +0800 | [diff] [blame^] | 212 | if (cancel_delayed_work_sync(&vib->delayed_work)) |
| 213 | turn_off(vib); |
| 214 | |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 215 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(3,11,0) |
| 216 | sysfs_remove_group(&vib->dev->kobj, vibrator_groups[0]); |
| 217 | #endif |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 218 | device_unregister(vib->dev); |
Johan Hovold | d7849bf | 2015-09-14 20:19:02 +0200 | [diff] [blame] | 219 | ida_simple_remove(&minors, vib->minor); |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 220 | gb_connection_disable(vib->connection); |
| 221 | gb_connection_destroy(vib->connection); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 222 | kfree(vib); |
| 223 | } |
| 224 | |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 225 | static const struct greybus_bundle_id gb_vibrator_id_table[] = { |
| 226 | { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_VIBRATOR) }, |
| 227 | { } |
| 228 | }; |
| 229 | MODULE_DEVICE_TABLE(greybus, gb_vibrator_id_table); |
| 230 | |
| 231 | static struct greybus_driver gb_vibrator_driver = { |
| 232 | .name = "vibrator", |
| 233 | .probe = gb_vibrator_probe, |
| 234 | .disconnect = gb_vibrator_disconnect, |
| 235 | .id_table = gb_vibrator_id_table, |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 236 | }; |
| 237 | |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 238 | static __init int gb_vibrator_init(void) |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 239 | { |
| 240 | int retval; |
| 241 | |
| 242 | retval = class_register(&vibrator_class); |
| 243 | if (retval) |
| 244 | return retval; |
| 245 | |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 246 | retval = greybus_register(&gb_vibrator_driver); |
Johan Hovold | d4efa68 | 2015-10-13 19:10:27 +0200 | [diff] [blame] | 247 | if (retval) |
| 248 | goto err_class_unregister; |
| 249 | |
| 250 | return 0; |
| 251 | |
| 252 | err_class_unregister: |
| 253 | class_unregister(&vibrator_class); |
| 254 | |
| 255 | return retval; |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 256 | } |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 257 | module_init(gb_vibrator_init); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 258 | |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 259 | static __exit void gb_vibrator_exit(void) |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 260 | { |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 261 | greybus_deregister(&gb_vibrator_driver); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 262 | class_unregister(&vibrator_class); |
Greg Kroah-Hartman | 5c1ac69 | 2015-07-08 10:44:09 -0700 | [diff] [blame] | 263 | ida_destroy(&minors); |
Greg Kroah-Hartman | ac4029f | 2014-11-17 16:03:34 -0800 | [diff] [blame] | 264 | } |
Johan Hovold | e0deb07 | 2016-01-21 17:34:24 +0100 | [diff] [blame] | 265 | module_exit(gb_vibrator_exit); |
Greg Kroah-Hartman | 66b676f | 2014-12-24 13:01:41 -0800 | [diff] [blame] | 266 | |
| 267 | MODULE_LICENSE("GPL v2"); |