Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org> |
| 3 | * |
| 4 | * Based on drivers/spmi/spmi.c: |
| 5 | * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 and |
| 9 | * only version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/idr.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/serdev.h> |
| 24 | #include <linux/slab.h> |
| 25 | |
| 26 | static bool is_registered; |
| 27 | static DEFINE_IDA(ctrl_ida); |
| 28 | |
| 29 | static void serdev_device_release(struct device *dev) |
| 30 | { |
| 31 | struct serdev_device *serdev = to_serdev_device(dev); |
| 32 | kfree(serdev); |
| 33 | } |
| 34 | |
| 35 | static const struct device_type serdev_device_type = { |
| 36 | .release = serdev_device_release, |
| 37 | }; |
| 38 | |
| 39 | static void serdev_ctrl_release(struct device *dev) |
| 40 | { |
| 41 | struct serdev_controller *ctrl = to_serdev_controller(dev); |
| 42 | ida_simple_remove(&ctrl_ida, ctrl->nr); |
| 43 | kfree(ctrl); |
| 44 | } |
| 45 | |
| 46 | static const struct device_type serdev_ctrl_type = { |
| 47 | .release = serdev_ctrl_release, |
| 48 | }; |
| 49 | |
| 50 | static int serdev_device_match(struct device *dev, struct device_driver *drv) |
| 51 | { |
| 52 | /* TODO: ACPI and platform matching */ |
| 53 | return of_driver_match_device(dev, drv); |
| 54 | } |
| 55 | |
| 56 | static int serdev_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 57 | { |
| 58 | /* TODO: ACPI and platform modalias */ |
| 59 | return of_device_uevent_modalias(dev, env); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * serdev_device_add() - add a device previously constructed via serdev_device_alloc() |
| 64 | * @serdev: serdev_device to be added |
| 65 | */ |
| 66 | int serdev_device_add(struct serdev_device *serdev) |
| 67 | { |
| 68 | struct device *parent = serdev->dev.parent; |
| 69 | int err; |
| 70 | |
| 71 | dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr); |
| 72 | |
| 73 | err = device_add(&serdev->dev); |
| 74 | if (err < 0) { |
| 75 | dev_err(&serdev->dev, "Can't add %s, status %d\n", |
| 76 | dev_name(&serdev->dev), err); |
| 77 | goto err_device_add; |
| 78 | } |
| 79 | |
| 80 | dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev)); |
| 81 | |
| 82 | err_device_add: |
| 83 | return err; |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(serdev_device_add); |
| 86 | |
| 87 | /** |
| 88 | * serdev_device_remove(): remove an serdev device |
| 89 | * @serdev: serdev_device to be removed |
| 90 | */ |
| 91 | void serdev_device_remove(struct serdev_device *serdev) |
| 92 | { |
| 93 | device_unregister(&serdev->dev); |
| 94 | } |
| 95 | EXPORT_SYMBOL_GPL(serdev_device_remove); |
| 96 | |
| 97 | int serdev_device_open(struct serdev_device *serdev) |
| 98 | { |
| 99 | struct serdev_controller *ctrl = serdev->ctrl; |
| 100 | |
| 101 | if (!ctrl || !ctrl->ops->open) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | return ctrl->ops->open(ctrl); |
| 105 | } |
| 106 | EXPORT_SYMBOL_GPL(serdev_device_open); |
| 107 | |
| 108 | void serdev_device_close(struct serdev_device *serdev) |
| 109 | { |
| 110 | struct serdev_controller *ctrl = serdev->ctrl; |
| 111 | |
| 112 | if (!ctrl || !ctrl->ops->close) |
| 113 | return; |
| 114 | |
| 115 | ctrl->ops->close(ctrl); |
| 116 | } |
| 117 | EXPORT_SYMBOL_GPL(serdev_device_close); |
| 118 | |
Andrey Smirnov | 6fe729c | 2017-04-04 07:22:33 -0700 | [diff] [blame^] | 119 | void serdev_device_write_wakeup(struct serdev_device *serdev) |
| 120 | { |
| 121 | complete(&serdev->write_comp); |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(serdev_device_write_wakeup); |
| 124 | |
| 125 | int serdev_device_write(struct serdev_device *serdev, |
| 126 | const unsigned char *buf, size_t count, |
| 127 | unsigned long timeout) |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 128 | { |
| 129 | struct serdev_controller *ctrl = serdev->ctrl; |
Andrey Smirnov | 6fe729c | 2017-04-04 07:22:33 -0700 | [diff] [blame^] | 130 | int ret; |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 131 | |
Andrey Smirnov | 6fe729c | 2017-04-04 07:22:33 -0700 | [diff] [blame^] | 132 | if (!ctrl || !ctrl->ops->write_buf || |
| 133 | (timeout && !serdev->ops->write_wakeup)) |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 134 | return -EINVAL; |
| 135 | |
Andrey Smirnov | 6fe729c | 2017-04-04 07:22:33 -0700 | [diff] [blame^] | 136 | mutex_lock(&serdev->write_lock); |
| 137 | do { |
| 138 | reinit_completion(&serdev->write_comp); |
| 139 | |
| 140 | ret = ctrl->ops->write_buf(ctrl, buf, count); |
| 141 | if (ret < 0) |
| 142 | break; |
| 143 | |
| 144 | buf += ret; |
| 145 | count -= ret; |
| 146 | |
| 147 | } while (count && |
| 148 | (timeout = wait_for_completion_timeout(&serdev->write_comp, |
| 149 | timeout))); |
| 150 | mutex_unlock(&serdev->write_lock); |
| 151 | return ret < 0 ? ret : (count ? -ETIMEDOUT : 0); |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 152 | } |
Andrey Smirnov | 6fe729c | 2017-04-04 07:22:33 -0700 | [diff] [blame^] | 153 | EXPORT_SYMBOL_GPL(serdev_device_write); |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 154 | |
| 155 | void serdev_device_write_flush(struct serdev_device *serdev) |
| 156 | { |
| 157 | struct serdev_controller *ctrl = serdev->ctrl; |
| 158 | |
| 159 | if (!ctrl || !ctrl->ops->write_flush) |
| 160 | return; |
| 161 | |
| 162 | ctrl->ops->write_flush(ctrl); |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(serdev_device_write_flush); |
| 165 | |
| 166 | int serdev_device_write_room(struct serdev_device *serdev) |
| 167 | { |
| 168 | struct serdev_controller *ctrl = serdev->ctrl; |
| 169 | |
| 170 | if (!ctrl || !ctrl->ops->write_room) |
| 171 | return 0; |
| 172 | |
| 173 | return serdev->ctrl->ops->write_room(ctrl); |
| 174 | } |
| 175 | EXPORT_SYMBOL_GPL(serdev_device_write_room); |
| 176 | |
| 177 | unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed) |
| 178 | { |
| 179 | struct serdev_controller *ctrl = serdev->ctrl; |
| 180 | |
| 181 | if (!ctrl || !ctrl->ops->set_baudrate) |
| 182 | return 0; |
| 183 | |
| 184 | return ctrl->ops->set_baudrate(ctrl, speed); |
| 185 | |
| 186 | } |
| 187 | EXPORT_SYMBOL_GPL(serdev_device_set_baudrate); |
| 188 | |
| 189 | void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable) |
| 190 | { |
| 191 | struct serdev_controller *ctrl = serdev->ctrl; |
| 192 | |
| 193 | if (!ctrl || !ctrl->ops->set_flow_control) |
| 194 | return; |
| 195 | |
| 196 | ctrl->ops->set_flow_control(ctrl, enable); |
| 197 | } |
| 198 | EXPORT_SYMBOL_GPL(serdev_device_set_flow_control); |
| 199 | |
| 200 | static int serdev_drv_probe(struct device *dev) |
| 201 | { |
| 202 | const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); |
| 203 | |
| 204 | return sdrv->probe(to_serdev_device(dev)); |
| 205 | } |
| 206 | |
| 207 | static int serdev_drv_remove(struct device *dev) |
| 208 | { |
| 209 | const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); |
| 210 | |
| 211 | sdrv->remove(to_serdev_device(dev)); |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static ssize_t modalias_show(struct device *dev, |
| 216 | struct device_attribute *attr, char *buf) |
| 217 | { |
| 218 | ssize_t len = of_device_get_modalias(dev, buf, PAGE_SIZE - 2); |
| 219 | buf[len] = '\n'; |
| 220 | buf[len+1] = 0; |
| 221 | return len+1; |
| 222 | } |
| 223 | |
| 224 | static struct device_attribute serdev_device_attrs[] = { |
| 225 | __ATTR_RO(modalias), |
| 226 | __ATTR_NULL |
| 227 | }; |
| 228 | |
| 229 | static struct bus_type serdev_bus_type = { |
| 230 | .name = "serial", |
| 231 | .match = serdev_device_match, |
| 232 | .probe = serdev_drv_probe, |
| 233 | .remove = serdev_drv_remove, |
| 234 | .uevent = serdev_uevent, |
| 235 | .dev_attrs = serdev_device_attrs, |
| 236 | }; |
| 237 | |
| 238 | /** |
| 239 | * serdev_controller_alloc() - Allocate a new serdev device |
| 240 | * @ctrl: associated controller |
| 241 | * |
| 242 | * Caller is responsible for either calling serdev_device_add() to add the |
| 243 | * newly allocated controller, or calling serdev_device_put() to discard it. |
| 244 | */ |
| 245 | struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl) |
| 246 | { |
| 247 | struct serdev_device *serdev; |
| 248 | |
| 249 | serdev = kzalloc(sizeof(*serdev), GFP_KERNEL); |
| 250 | if (!serdev) |
| 251 | return NULL; |
| 252 | |
| 253 | serdev->ctrl = ctrl; |
| 254 | ctrl->serdev = serdev; |
| 255 | device_initialize(&serdev->dev); |
| 256 | serdev->dev.parent = &ctrl->dev; |
| 257 | serdev->dev.bus = &serdev_bus_type; |
| 258 | serdev->dev.type = &serdev_device_type; |
Andrey Smirnov | 6fe729c | 2017-04-04 07:22:33 -0700 | [diff] [blame^] | 259 | init_completion(&serdev->write_comp); |
| 260 | mutex_init(&serdev->write_lock); |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 261 | return serdev; |
| 262 | } |
| 263 | EXPORT_SYMBOL_GPL(serdev_device_alloc); |
| 264 | |
| 265 | /** |
| 266 | * serdev_controller_alloc() - Allocate a new serdev controller |
| 267 | * @parent: parent device |
| 268 | * @size: size of private data |
| 269 | * |
| 270 | * Caller is responsible for either calling serdev_controller_add() to add the |
| 271 | * newly allocated controller, or calling serdev_controller_put() to discard it. |
| 272 | * The allocated private data region may be accessed via |
| 273 | * serdev_controller_get_drvdata() |
| 274 | */ |
| 275 | struct serdev_controller *serdev_controller_alloc(struct device *parent, |
| 276 | size_t size) |
| 277 | { |
| 278 | struct serdev_controller *ctrl; |
| 279 | int id; |
| 280 | |
| 281 | if (WARN_ON(!parent)) |
| 282 | return NULL; |
| 283 | |
| 284 | ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL); |
| 285 | if (!ctrl) |
| 286 | return NULL; |
| 287 | |
| 288 | device_initialize(&ctrl->dev); |
| 289 | ctrl->dev.type = &serdev_ctrl_type; |
| 290 | ctrl->dev.bus = &serdev_bus_type; |
| 291 | ctrl->dev.parent = parent; |
| 292 | ctrl->dev.of_node = parent->of_node; |
| 293 | serdev_controller_set_drvdata(ctrl, &ctrl[1]); |
| 294 | |
| 295 | id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL); |
| 296 | if (id < 0) { |
| 297 | dev_err(parent, |
| 298 | "unable to allocate serdev controller identifier.\n"); |
| 299 | serdev_controller_put(ctrl); |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | ctrl->nr = id; |
| 304 | dev_set_name(&ctrl->dev, "serial%d", id); |
| 305 | |
| 306 | dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id); |
| 307 | return ctrl; |
| 308 | } |
| 309 | EXPORT_SYMBOL_GPL(serdev_controller_alloc); |
| 310 | |
| 311 | static int of_serdev_register_devices(struct serdev_controller *ctrl) |
| 312 | { |
| 313 | struct device_node *node; |
| 314 | struct serdev_device *serdev = NULL; |
| 315 | int err; |
| 316 | bool found = false; |
| 317 | |
| 318 | for_each_available_child_of_node(ctrl->dev.of_node, node) { |
| 319 | if (!of_get_property(node, "compatible", NULL)) |
| 320 | continue; |
| 321 | |
| 322 | dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name); |
| 323 | |
| 324 | serdev = serdev_device_alloc(ctrl); |
| 325 | if (!serdev) |
| 326 | continue; |
| 327 | |
| 328 | serdev->dev.of_node = node; |
| 329 | |
| 330 | err = serdev_device_add(serdev); |
| 331 | if (err) { |
| 332 | dev_err(&serdev->dev, |
| 333 | "failure adding device. status %d\n", err); |
| 334 | serdev_device_put(serdev); |
| 335 | } else |
| 336 | found = true; |
| 337 | } |
| 338 | if (!found) |
| 339 | return -ENODEV; |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * serdev_controller_add() - Add an serdev controller |
| 346 | * @ctrl: controller to be registered. |
| 347 | * |
| 348 | * Register a controller previously allocated via serdev_controller_alloc() with |
| 349 | * the serdev core. |
| 350 | */ |
| 351 | int serdev_controller_add(struct serdev_controller *ctrl) |
| 352 | { |
| 353 | int ret; |
| 354 | |
| 355 | /* Can't register until after driver model init */ |
| 356 | if (WARN_ON(!is_registered)) |
| 357 | return -EAGAIN; |
| 358 | |
| 359 | ret = device_add(&ctrl->dev); |
| 360 | if (ret) |
| 361 | return ret; |
| 362 | |
| 363 | ret = of_serdev_register_devices(ctrl); |
| 364 | if (ret) |
| 365 | goto out_dev_del; |
| 366 | |
| 367 | dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n", |
| 368 | ctrl->nr, &ctrl->dev); |
| 369 | return 0; |
| 370 | |
| 371 | out_dev_del: |
| 372 | device_del(&ctrl->dev); |
| 373 | return ret; |
| 374 | }; |
| 375 | EXPORT_SYMBOL_GPL(serdev_controller_add); |
| 376 | |
| 377 | /* Remove a device associated with a controller */ |
| 378 | static int serdev_remove_device(struct device *dev, void *data) |
| 379 | { |
| 380 | struct serdev_device *serdev = to_serdev_device(dev); |
| 381 | if (dev->type == &serdev_device_type) |
| 382 | serdev_device_remove(serdev); |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * serdev_controller_remove(): remove an serdev controller |
| 388 | * @ctrl: controller to remove |
| 389 | * |
| 390 | * Remove a serdev controller. Caller is responsible for calling |
| 391 | * serdev_controller_put() to discard the allocated controller. |
| 392 | */ |
| 393 | void serdev_controller_remove(struct serdev_controller *ctrl) |
| 394 | { |
| 395 | int dummy; |
| 396 | |
| 397 | if (!ctrl) |
| 398 | return; |
| 399 | |
| 400 | dummy = device_for_each_child(&ctrl->dev, NULL, |
| 401 | serdev_remove_device); |
| 402 | device_del(&ctrl->dev); |
| 403 | } |
| 404 | EXPORT_SYMBOL_GPL(serdev_controller_remove); |
| 405 | |
| 406 | /** |
| 407 | * serdev_driver_register() - Register client driver with serdev core |
| 408 | * @sdrv: client driver to be associated with client-device. |
| 409 | * |
| 410 | * This API will register the client driver with the serdev framework. |
| 411 | * It is typically called from the driver's module-init function. |
| 412 | */ |
| 413 | int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner) |
| 414 | { |
| 415 | sdrv->driver.bus = &serdev_bus_type; |
| 416 | sdrv->driver.owner = owner; |
| 417 | |
| 418 | /* force drivers to async probe so I/O is possible in probe */ |
| 419 | sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS; |
| 420 | |
| 421 | return driver_register(&sdrv->driver); |
| 422 | } |
| 423 | EXPORT_SYMBOL_GPL(__serdev_device_driver_register); |
| 424 | |
| 425 | static void __exit serdev_exit(void) |
| 426 | { |
| 427 | bus_unregister(&serdev_bus_type); |
| 428 | } |
| 429 | module_exit(serdev_exit); |
| 430 | |
| 431 | static int __init serdev_init(void) |
| 432 | { |
| 433 | int ret; |
| 434 | |
| 435 | ret = bus_register(&serdev_bus_type); |
| 436 | if (ret) |
| 437 | return ret; |
| 438 | |
| 439 | is_registered = true; |
| 440 | return 0; |
| 441 | } |
| 442 | /* Must be before serial drivers register */ |
| 443 | postcore_initcall(serdev_init); |
| 444 | |
| 445 | MODULE_AUTHOR("Rob Herring <robh@kernel.org>"); |
| 446 | MODULE_LICENSE("GPL v2"); |
| 447 | MODULE_DESCRIPTION("Serial attached device bus"); |