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 | |
| 119 | int serdev_device_write_buf(struct serdev_device *serdev, |
| 120 | const unsigned char *buf, size_t count) |
| 121 | { |
| 122 | struct serdev_controller *ctrl = serdev->ctrl; |
| 123 | |
| 124 | if (!ctrl || !ctrl->ops->write_buf) |
| 125 | return -EINVAL; |
| 126 | |
| 127 | return ctrl->ops->write_buf(ctrl, buf, count); |
| 128 | } |
| 129 | EXPORT_SYMBOL_GPL(serdev_device_write_buf); |
| 130 | |
| 131 | void serdev_device_write_flush(struct serdev_device *serdev) |
| 132 | { |
| 133 | struct serdev_controller *ctrl = serdev->ctrl; |
| 134 | |
| 135 | if (!ctrl || !ctrl->ops->write_flush) |
| 136 | return; |
| 137 | |
| 138 | ctrl->ops->write_flush(ctrl); |
| 139 | } |
| 140 | EXPORT_SYMBOL_GPL(serdev_device_write_flush); |
| 141 | |
| 142 | int serdev_device_write_room(struct serdev_device *serdev) |
| 143 | { |
| 144 | struct serdev_controller *ctrl = serdev->ctrl; |
| 145 | |
| 146 | if (!ctrl || !ctrl->ops->write_room) |
| 147 | return 0; |
| 148 | |
| 149 | return serdev->ctrl->ops->write_room(ctrl); |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(serdev_device_write_room); |
| 152 | |
| 153 | unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed) |
| 154 | { |
| 155 | struct serdev_controller *ctrl = serdev->ctrl; |
| 156 | |
| 157 | if (!ctrl || !ctrl->ops->set_baudrate) |
| 158 | return 0; |
| 159 | |
| 160 | return ctrl->ops->set_baudrate(ctrl, speed); |
| 161 | |
| 162 | } |
| 163 | EXPORT_SYMBOL_GPL(serdev_device_set_baudrate); |
| 164 | |
| 165 | void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable) |
| 166 | { |
| 167 | struct serdev_controller *ctrl = serdev->ctrl; |
| 168 | |
| 169 | if (!ctrl || !ctrl->ops->set_flow_control) |
| 170 | return; |
| 171 | |
| 172 | ctrl->ops->set_flow_control(ctrl, enable); |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(serdev_device_set_flow_control); |
| 175 | |
Sebastian Reichel | b3f80c8 | 2017-03-28 17:59:31 +0200 | [diff] [blame^] | 176 | void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout) |
| 177 | { |
| 178 | struct serdev_controller *ctrl = serdev->ctrl; |
| 179 | |
| 180 | if (!ctrl || !ctrl->ops->wait_until_sent) |
| 181 | return; |
| 182 | |
| 183 | ctrl->ops->wait_until_sent(ctrl, timeout); |
| 184 | } |
| 185 | EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent); |
| 186 | |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 187 | static int serdev_drv_probe(struct device *dev) |
| 188 | { |
| 189 | const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); |
| 190 | |
| 191 | return sdrv->probe(to_serdev_device(dev)); |
| 192 | } |
| 193 | |
| 194 | static int serdev_drv_remove(struct device *dev) |
| 195 | { |
| 196 | const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); |
| 197 | |
| 198 | sdrv->remove(to_serdev_device(dev)); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static ssize_t modalias_show(struct device *dev, |
| 203 | struct device_attribute *attr, char *buf) |
| 204 | { |
| 205 | ssize_t len = of_device_get_modalias(dev, buf, PAGE_SIZE - 2); |
| 206 | buf[len] = '\n'; |
| 207 | buf[len+1] = 0; |
| 208 | return len+1; |
| 209 | } |
| 210 | |
| 211 | static struct device_attribute serdev_device_attrs[] = { |
| 212 | __ATTR_RO(modalias), |
| 213 | __ATTR_NULL |
| 214 | }; |
| 215 | |
| 216 | static struct bus_type serdev_bus_type = { |
| 217 | .name = "serial", |
| 218 | .match = serdev_device_match, |
| 219 | .probe = serdev_drv_probe, |
| 220 | .remove = serdev_drv_remove, |
| 221 | .uevent = serdev_uevent, |
| 222 | .dev_attrs = serdev_device_attrs, |
| 223 | }; |
| 224 | |
| 225 | /** |
| 226 | * serdev_controller_alloc() - Allocate a new serdev device |
| 227 | * @ctrl: associated controller |
| 228 | * |
| 229 | * Caller is responsible for either calling serdev_device_add() to add the |
| 230 | * newly allocated controller, or calling serdev_device_put() to discard it. |
| 231 | */ |
| 232 | struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl) |
| 233 | { |
| 234 | struct serdev_device *serdev; |
| 235 | |
| 236 | serdev = kzalloc(sizeof(*serdev), GFP_KERNEL); |
| 237 | if (!serdev) |
| 238 | return NULL; |
| 239 | |
| 240 | serdev->ctrl = ctrl; |
| 241 | ctrl->serdev = serdev; |
| 242 | device_initialize(&serdev->dev); |
| 243 | serdev->dev.parent = &ctrl->dev; |
| 244 | serdev->dev.bus = &serdev_bus_type; |
| 245 | serdev->dev.type = &serdev_device_type; |
| 246 | return serdev; |
| 247 | } |
| 248 | EXPORT_SYMBOL_GPL(serdev_device_alloc); |
| 249 | |
| 250 | /** |
| 251 | * serdev_controller_alloc() - Allocate a new serdev controller |
| 252 | * @parent: parent device |
| 253 | * @size: size of private data |
| 254 | * |
| 255 | * Caller is responsible for either calling serdev_controller_add() to add the |
| 256 | * newly allocated controller, or calling serdev_controller_put() to discard it. |
| 257 | * The allocated private data region may be accessed via |
| 258 | * serdev_controller_get_drvdata() |
| 259 | */ |
| 260 | struct serdev_controller *serdev_controller_alloc(struct device *parent, |
| 261 | size_t size) |
| 262 | { |
| 263 | struct serdev_controller *ctrl; |
| 264 | int id; |
| 265 | |
| 266 | if (WARN_ON(!parent)) |
| 267 | return NULL; |
| 268 | |
| 269 | ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL); |
| 270 | if (!ctrl) |
| 271 | return NULL; |
| 272 | |
| 273 | device_initialize(&ctrl->dev); |
| 274 | ctrl->dev.type = &serdev_ctrl_type; |
| 275 | ctrl->dev.bus = &serdev_bus_type; |
| 276 | ctrl->dev.parent = parent; |
| 277 | ctrl->dev.of_node = parent->of_node; |
| 278 | serdev_controller_set_drvdata(ctrl, &ctrl[1]); |
| 279 | |
| 280 | id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL); |
| 281 | if (id < 0) { |
| 282 | dev_err(parent, |
| 283 | "unable to allocate serdev controller identifier.\n"); |
| 284 | serdev_controller_put(ctrl); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | ctrl->nr = id; |
| 289 | dev_set_name(&ctrl->dev, "serial%d", id); |
| 290 | |
| 291 | dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id); |
| 292 | return ctrl; |
| 293 | } |
| 294 | EXPORT_SYMBOL_GPL(serdev_controller_alloc); |
| 295 | |
| 296 | static int of_serdev_register_devices(struct serdev_controller *ctrl) |
| 297 | { |
| 298 | struct device_node *node; |
| 299 | struct serdev_device *serdev = NULL; |
| 300 | int err; |
| 301 | bool found = false; |
| 302 | |
| 303 | for_each_available_child_of_node(ctrl->dev.of_node, node) { |
| 304 | if (!of_get_property(node, "compatible", NULL)) |
| 305 | continue; |
| 306 | |
| 307 | dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name); |
| 308 | |
| 309 | serdev = serdev_device_alloc(ctrl); |
| 310 | if (!serdev) |
| 311 | continue; |
| 312 | |
| 313 | serdev->dev.of_node = node; |
| 314 | |
| 315 | err = serdev_device_add(serdev); |
| 316 | if (err) { |
| 317 | dev_err(&serdev->dev, |
| 318 | "failure adding device. status %d\n", err); |
| 319 | serdev_device_put(serdev); |
| 320 | } else |
| 321 | found = true; |
| 322 | } |
| 323 | if (!found) |
| 324 | return -ENODEV; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * serdev_controller_add() - Add an serdev controller |
| 331 | * @ctrl: controller to be registered. |
| 332 | * |
| 333 | * Register a controller previously allocated via serdev_controller_alloc() with |
| 334 | * the serdev core. |
| 335 | */ |
| 336 | int serdev_controller_add(struct serdev_controller *ctrl) |
| 337 | { |
| 338 | int ret; |
| 339 | |
| 340 | /* Can't register until after driver model init */ |
| 341 | if (WARN_ON(!is_registered)) |
| 342 | return -EAGAIN; |
| 343 | |
| 344 | ret = device_add(&ctrl->dev); |
| 345 | if (ret) |
| 346 | return ret; |
| 347 | |
| 348 | ret = of_serdev_register_devices(ctrl); |
| 349 | if (ret) |
| 350 | goto out_dev_del; |
| 351 | |
| 352 | dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n", |
| 353 | ctrl->nr, &ctrl->dev); |
| 354 | return 0; |
| 355 | |
| 356 | out_dev_del: |
| 357 | device_del(&ctrl->dev); |
| 358 | return ret; |
| 359 | }; |
| 360 | EXPORT_SYMBOL_GPL(serdev_controller_add); |
| 361 | |
| 362 | /* Remove a device associated with a controller */ |
| 363 | static int serdev_remove_device(struct device *dev, void *data) |
| 364 | { |
| 365 | struct serdev_device *serdev = to_serdev_device(dev); |
| 366 | if (dev->type == &serdev_device_type) |
| 367 | serdev_device_remove(serdev); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * serdev_controller_remove(): remove an serdev controller |
| 373 | * @ctrl: controller to remove |
| 374 | * |
| 375 | * Remove a serdev controller. Caller is responsible for calling |
| 376 | * serdev_controller_put() to discard the allocated controller. |
| 377 | */ |
| 378 | void serdev_controller_remove(struct serdev_controller *ctrl) |
| 379 | { |
| 380 | int dummy; |
| 381 | |
| 382 | if (!ctrl) |
| 383 | return; |
| 384 | |
| 385 | dummy = device_for_each_child(&ctrl->dev, NULL, |
| 386 | serdev_remove_device); |
| 387 | device_del(&ctrl->dev); |
| 388 | } |
| 389 | EXPORT_SYMBOL_GPL(serdev_controller_remove); |
| 390 | |
| 391 | /** |
| 392 | * serdev_driver_register() - Register client driver with serdev core |
| 393 | * @sdrv: client driver to be associated with client-device. |
| 394 | * |
| 395 | * This API will register the client driver with the serdev framework. |
| 396 | * It is typically called from the driver's module-init function. |
| 397 | */ |
| 398 | int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner) |
| 399 | { |
| 400 | sdrv->driver.bus = &serdev_bus_type; |
| 401 | sdrv->driver.owner = owner; |
| 402 | |
| 403 | /* force drivers to async probe so I/O is possible in probe */ |
| 404 | sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS; |
| 405 | |
| 406 | return driver_register(&sdrv->driver); |
| 407 | } |
| 408 | EXPORT_SYMBOL_GPL(__serdev_device_driver_register); |
| 409 | |
| 410 | static void __exit serdev_exit(void) |
| 411 | { |
| 412 | bus_unregister(&serdev_bus_type); |
| 413 | } |
| 414 | module_exit(serdev_exit); |
| 415 | |
| 416 | static int __init serdev_init(void) |
| 417 | { |
| 418 | int ret; |
| 419 | |
| 420 | ret = bus_register(&serdev_bus_type); |
| 421 | if (ret) |
| 422 | return ret; |
| 423 | |
| 424 | is_registered = true; |
| 425 | return 0; |
| 426 | } |
| 427 | /* Must be before serial drivers register */ |
| 428 | postcore_initcall(serdev_init); |
| 429 | |
| 430 | MODULE_AUTHOR("Rob Herring <robh@kernel.org>"); |
| 431 | MODULE_LICENSE("GPL v2"); |
| 432 | MODULE_DESCRIPTION("Serial attached device bus"); |