1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org> 4 * 5 * Based on drivers/spmi/spmi.c: 6 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved. 7 */ 8 9 #include <linux/acpi.h> 10 #include <linux/errno.h> 11 #include <linux/idr.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/of.h> 15 #include <linux/of_device.h> 16 #include <linux/serdev.h> 17 #include <linux/slab.h> 18 19 static bool is_registered; 20 static DEFINE_IDA(ctrl_ida); 21 22 static void serdev_device_release(struct device *dev) 23 { 24 struct serdev_device *serdev = to_serdev_device(dev); 25 kfree(serdev); 26 } 27 28 static const struct device_type serdev_device_type = { 29 .release = serdev_device_release, 30 }; 31 32 static void serdev_ctrl_release(struct device *dev) 33 { 34 struct serdev_controller *ctrl = to_serdev_controller(dev); 35 ida_simple_remove(&ctrl_ida, ctrl->nr); 36 kfree(ctrl); 37 } 38 39 static const struct device_type serdev_ctrl_type = { 40 .release = serdev_ctrl_release, 41 }; 42 43 static int serdev_device_match(struct device *dev, struct device_driver *drv) 44 { 45 /* TODO: platform matching */ 46 if (acpi_driver_match_device(dev, drv)) 47 return 1; 48 49 return of_driver_match_device(dev, drv); 50 } 51 52 static int serdev_uevent(struct device *dev, struct kobj_uevent_env *env) 53 { 54 int rc; 55 56 /* TODO: platform modalias */ 57 rc = acpi_device_uevent_modalias(dev, env); 58 if (rc != -ENODEV) 59 return rc; 60 61 return of_device_uevent_modalias(dev, env); 62 } 63 64 /** 65 * serdev_device_add() - add a device previously constructed via serdev_device_alloc() 66 * @serdev: serdev_device to be added 67 */ 68 int serdev_device_add(struct serdev_device *serdev) 69 { 70 struct serdev_controller *ctrl = serdev->ctrl; 71 struct device *parent = serdev->dev.parent; 72 int err; 73 74 dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr); 75 76 /* Only a single slave device is currently supported. */ 77 if (ctrl->serdev) { 78 dev_err(&serdev->dev, "controller busy\n"); 79 return -EBUSY; 80 } 81 ctrl->serdev = serdev; 82 83 err = device_add(&serdev->dev); 84 if (err < 0) { 85 dev_err(&serdev->dev, "Can't add %s, status %d\n", 86 dev_name(&serdev->dev), err); 87 goto err_clear_serdev; 88 } 89 90 dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev)); 91 92 return 0; 93 94 err_clear_serdev: 95 ctrl->serdev = NULL; 96 return err; 97 } 98 EXPORT_SYMBOL_GPL(serdev_device_add); 99 100 /** 101 * serdev_device_remove(): remove an serdev device 102 * @serdev: serdev_device to be removed 103 */ 104 void serdev_device_remove(struct serdev_device *serdev) 105 { 106 struct serdev_controller *ctrl = serdev->ctrl; 107 108 device_unregister(&serdev->dev); 109 ctrl->serdev = NULL; 110 } 111 EXPORT_SYMBOL_GPL(serdev_device_remove); 112 113 int serdev_device_open(struct serdev_device *serdev) 114 { 115 struct serdev_controller *ctrl = serdev->ctrl; 116 117 if (!ctrl || !ctrl->ops->open) 118 return -EINVAL; 119 120 return ctrl->ops->open(ctrl); 121 } 122 EXPORT_SYMBOL_GPL(serdev_device_open); 123 124 void serdev_device_close(struct serdev_device *serdev) 125 { 126 struct serdev_controller *ctrl = serdev->ctrl; 127 128 if (!ctrl || !ctrl->ops->close) 129 return; 130 131 ctrl->ops->close(ctrl); 132 } 133 EXPORT_SYMBOL_GPL(serdev_device_close); 134 135 static void devm_serdev_device_release(struct device *dev, void *dr) 136 { 137 serdev_device_close(*(struct serdev_device **)dr); 138 } 139 140 int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev) 141 { 142 struct serdev_device **dr; 143 int ret; 144 145 dr = devres_alloc(devm_serdev_device_release, sizeof(*dr), GFP_KERNEL); 146 if (!dr) 147 return -ENOMEM; 148 149 ret = serdev_device_open(serdev); 150 if (ret) { 151 devres_free(dr); 152 return ret; 153 } 154 155 *dr = serdev; 156 devres_add(dev, dr); 157 158 return 0; 159 } 160 EXPORT_SYMBOL_GPL(devm_serdev_device_open); 161 162 void serdev_device_write_wakeup(struct serdev_device *serdev) 163 { 164 complete(&serdev->write_comp); 165 } 166 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup); 167 168 int serdev_device_write_buf(struct serdev_device *serdev, 169 const unsigned char *buf, size_t count) 170 { 171 struct serdev_controller *ctrl = serdev->ctrl; 172 173 if (!ctrl || !ctrl->ops->write_buf) 174 return -EINVAL; 175 176 return ctrl->ops->write_buf(ctrl, buf, count); 177 } 178 EXPORT_SYMBOL_GPL(serdev_device_write_buf); 179 180 int serdev_device_write(struct serdev_device *serdev, 181 const unsigned char *buf, size_t count, 182 unsigned long timeout) 183 { 184 struct serdev_controller *ctrl = serdev->ctrl; 185 int ret; 186 187 if (!ctrl || !ctrl->ops->write_buf || 188 (timeout && !serdev->ops->write_wakeup)) 189 return -EINVAL; 190 191 mutex_lock(&serdev->write_lock); 192 do { 193 reinit_completion(&serdev->write_comp); 194 195 ret = ctrl->ops->write_buf(ctrl, buf, count); 196 if (ret < 0) 197 break; 198 199 buf += ret; 200 count -= ret; 201 202 } while (count && 203 (timeout = wait_for_completion_timeout(&serdev->write_comp, 204 timeout))); 205 mutex_unlock(&serdev->write_lock); 206 return ret < 0 ? ret : (count ? -ETIMEDOUT : 0); 207 } 208 EXPORT_SYMBOL_GPL(serdev_device_write); 209 210 void serdev_device_write_flush(struct serdev_device *serdev) 211 { 212 struct serdev_controller *ctrl = serdev->ctrl; 213 214 if (!ctrl || !ctrl->ops->write_flush) 215 return; 216 217 ctrl->ops->write_flush(ctrl); 218 } 219 EXPORT_SYMBOL_GPL(serdev_device_write_flush); 220 221 int serdev_device_write_room(struct serdev_device *serdev) 222 { 223 struct serdev_controller *ctrl = serdev->ctrl; 224 225 if (!ctrl || !ctrl->ops->write_room) 226 return 0; 227 228 return serdev->ctrl->ops->write_room(ctrl); 229 } 230 EXPORT_SYMBOL_GPL(serdev_device_write_room); 231 232 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed) 233 { 234 struct serdev_controller *ctrl = serdev->ctrl; 235 236 if (!ctrl || !ctrl->ops->set_baudrate) 237 return 0; 238 239 return ctrl->ops->set_baudrate(ctrl, speed); 240 241 } 242 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate); 243 244 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable) 245 { 246 struct serdev_controller *ctrl = serdev->ctrl; 247 248 if (!ctrl || !ctrl->ops->set_flow_control) 249 return; 250 251 ctrl->ops->set_flow_control(ctrl, enable); 252 } 253 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control); 254 255 int serdev_device_set_parity(struct serdev_device *serdev, 256 enum serdev_parity parity) 257 { 258 struct serdev_controller *ctrl = serdev->ctrl; 259 260 if (!ctrl || !ctrl->ops->set_parity) 261 return -ENOTSUPP; 262 263 return ctrl->ops->set_parity(ctrl, parity); 264 } 265 EXPORT_SYMBOL_GPL(serdev_device_set_parity); 266 267 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout) 268 { 269 struct serdev_controller *ctrl = serdev->ctrl; 270 271 if (!ctrl || !ctrl->ops->wait_until_sent) 272 return; 273 274 ctrl->ops->wait_until_sent(ctrl, timeout); 275 } 276 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent); 277 278 int serdev_device_get_tiocm(struct serdev_device *serdev) 279 { 280 struct serdev_controller *ctrl = serdev->ctrl; 281 282 if (!ctrl || !ctrl->ops->get_tiocm) 283 return -ENOTSUPP; 284 285 return ctrl->ops->get_tiocm(ctrl); 286 } 287 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm); 288 289 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) 290 { 291 struct serdev_controller *ctrl = serdev->ctrl; 292 293 if (!ctrl || !ctrl->ops->set_tiocm) 294 return -ENOTSUPP; 295 296 return ctrl->ops->set_tiocm(ctrl, set, clear); 297 } 298 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm); 299 300 static int serdev_drv_probe(struct device *dev) 301 { 302 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); 303 304 return sdrv->probe(to_serdev_device(dev)); 305 } 306 307 static int serdev_drv_remove(struct device *dev) 308 { 309 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); 310 if (sdrv->remove) 311 sdrv->remove(to_serdev_device(dev)); 312 return 0; 313 } 314 315 static ssize_t modalias_show(struct device *dev, 316 struct device_attribute *attr, char *buf) 317 { 318 int len; 319 320 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 321 if (len != -ENODEV) 322 return len; 323 324 return of_device_modalias(dev, buf, PAGE_SIZE); 325 } 326 DEVICE_ATTR_RO(modalias); 327 328 static struct attribute *serdev_device_attrs[] = { 329 &dev_attr_modalias.attr, 330 NULL, 331 }; 332 ATTRIBUTE_GROUPS(serdev_device); 333 334 static struct bus_type serdev_bus_type = { 335 .name = "serial", 336 .match = serdev_device_match, 337 .probe = serdev_drv_probe, 338 .remove = serdev_drv_remove, 339 .uevent = serdev_uevent, 340 .dev_groups = serdev_device_groups, 341 }; 342 343 /** 344 * serdev_controller_alloc() - Allocate a new serdev device 345 * @ctrl: associated controller 346 * 347 * Caller is responsible for either calling serdev_device_add() to add the 348 * newly allocated controller, or calling serdev_device_put() to discard it. 349 */ 350 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl) 351 { 352 struct serdev_device *serdev; 353 354 serdev = kzalloc(sizeof(*serdev), GFP_KERNEL); 355 if (!serdev) 356 return NULL; 357 358 serdev->ctrl = ctrl; 359 device_initialize(&serdev->dev); 360 serdev->dev.parent = &ctrl->dev; 361 serdev->dev.bus = &serdev_bus_type; 362 serdev->dev.type = &serdev_device_type; 363 init_completion(&serdev->write_comp); 364 mutex_init(&serdev->write_lock); 365 return serdev; 366 } 367 EXPORT_SYMBOL_GPL(serdev_device_alloc); 368 369 /** 370 * serdev_controller_alloc() - Allocate a new serdev controller 371 * @parent: parent device 372 * @size: size of private data 373 * 374 * Caller is responsible for either calling serdev_controller_add() to add the 375 * newly allocated controller, or calling serdev_controller_put() to discard it. 376 * The allocated private data region may be accessed via 377 * serdev_controller_get_drvdata() 378 */ 379 struct serdev_controller *serdev_controller_alloc(struct device *parent, 380 size_t size) 381 { 382 struct serdev_controller *ctrl; 383 int id; 384 385 if (WARN_ON(!parent)) 386 return NULL; 387 388 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL); 389 if (!ctrl) 390 return NULL; 391 392 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL); 393 if (id < 0) { 394 dev_err(parent, 395 "unable to allocate serdev controller identifier.\n"); 396 goto err_free; 397 } 398 399 ctrl->nr = id; 400 401 device_initialize(&ctrl->dev); 402 ctrl->dev.type = &serdev_ctrl_type; 403 ctrl->dev.bus = &serdev_bus_type; 404 ctrl->dev.parent = parent; 405 ctrl->dev.of_node = parent->of_node; 406 serdev_controller_set_drvdata(ctrl, &ctrl[1]); 407 408 dev_set_name(&ctrl->dev, "serial%d", id); 409 410 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id); 411 return ctrl; 412 413 err_free: 414 kfree(ctrl); 415 416 return NULL; 417 } 418 EXPORT_SYMBOL_GPL(serdev_controller_alloc); 419 420 static int of_serdev_register_devices(struct serdev_controller *ctrl) 421 { 422 struct device_node *node; 423 struct serdev_device *serdev = NULL; 424 int err; 425 bool found = false; 426 427 for_each_available_child_of_node(ctrl->dev.of_node, node) { 428 if (!of_get_property(node, "compatible", NULL)) 429 continue; 430 431 dev_dbg(&ctrl->dev, "adding child %pOF\n", node); 432 433 serdev = serdev_device_alloc(ctrl); 434 if (!serdev) 435 continue; 436 437 serdev->dev.of_node = node; 438 439 err = serdev_device_add(serdev); 440 if (err) { 441 dev_err(&serdev->dev, 442 "failure adding device. status %d\n", err); 443 serdev_device_put(serdev); 444 } else 445 found = true; 446 } 447 if (!found) 448 return -ENODEV; 449 450 return 0; 451 } 452 453 #ifdef CONFIG_ACPI 454 static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl, 455 struct acpi_device *adev) 456 { 457 struct serdev_device *serdev = NULL; 458 int err; 459 460 if (acpi_bus_get_status(adev) || !adev->status.present || 461 acpi_device_enumerated(adev)) 462 return AE_OK; 463 464 serdev = serdev_device_alloc(ctrl); 465 if (!serdev) { 466 dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n", 467 dev_name(&adev->dev)); 468 return AE_NO_MEMORY; 469 } 470 471 ACPI_COMPANION_SET(&serdev->dev, adev); 472 acpi_device_set_enumerated(adev); 473 474 err = serdev_device_add(serdev); 475 if (err) { 476 dev_err(&serdev->dev, 477 "failure adding ACPI serdev device. status %d\n", err); 478 serdev_device_put(serdev); 479 } 480 481 return AE_OK; 482 } 483 484 static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level, 485 void *data, void **return_value) 486 { 487 struct serdev_controller *ctrl = data; 488 struct acpi_device *adev; 489 490 if (acpi_bus_get_device(handle, &adev)) 491 return AE_OK; 492 493 return acpi_serdev_register_device(ctrl, adev); 494 } 495 496 static int acpi_serdev_register_devices(struct serdev_controller *ctrl) 497 { 498 acpi_status status; 499 acpi_handle handle; 500 501 handle = ACPI_HANDLE(ctrl->dev.parent); 502 if (!handle) 503 return -ENODEV; 504 505 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, 506 acpi_serdev_add_device, NULL, ctrl, NULL); 507 if (ACPI_FAILURE(status)) 508 dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n"); 509 510 if (!ctrl->serdev) 511 return -ENODEV; 512 513 return 0; 514 } 515 #else 516 static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl) 517 { 518 return -ENODEV; 519 } 520 #endif /* CONFIG_ACPI */ 521 522 /** 523 * serdev_controller_add() - Add an serdev controller 524 * @ctrl: controller to be registered. 525 * 526 * Register a controller previously allocated via serdev_controller_alloc() with 527 * the serdev core. 528 */ 529 int serdev_controller_add(struct serdev_controller *ctrl) 530 { 531 int ret_of, ret_acpi, ret; 532 533 /* Can't register until after driver model init */ 534 if (WARN_ON(!is_registered)) 535 return -EAGAIN; 536 537 ret = device_add(&ctrl->dev); 538 if (ret) 539 return ret; 540 541 ret_of = of_serdev_register_devices(ctrl); 542 ret_acpi = acpi_serdev_register_devices(ctrl); 543 if (ret_of && ret_acpi) { 544 dev_dbg(&ctrl->dev, "no devices registered: of:%d acpi:%d\n", 545 ret_of, ret_acpi); 546 ret = -ENODEV; 547 goto out_dev_del; 548 } 549 550 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n", 551 ctrl->nr, &ctrl->dev); 552 return 0; 553 554 out_dev_del: 555 device_del(&ctrl->dev); 556 return ret; 557 }; 558 EXPORT_SYMBOL_GPL(serdev_controller_add); 559 560 /* Remove a device associated with a controller */ 561 static int serdev_remove_device(struct device *dev, void *data) 562 { 563 struct serdev_device *serdev = to_serdev_device(dev); 564 if (dev->type == &serdev_device_type) 565 serdev_device_remove(serdev); 566 return 0; 567 } 568 569 /** 570 * serdev_controller_remove(): remove an serdev controller 571 * @ctrl: controller to remove 572 * 573 * Remove a serdev controller. Caller is responsible for calling 574 * serdev_controller_put() to discard the allocated controller. 575 */ 576 void serdev_controller_remove(struct serdev_controller *ctrl) 577 { 578 int dummy; 579 580 if (!ctrl) 581 return; 582 583 dummy = device_for_each_child(&ctrl->dev, NULL, 584 serdev_remove_device); 585 device_del(&ctrl->dev); 586 } 587 EXPORT_SYMBOL_GPL(serdev_controller_remove); 588 589 /** 590 * serdev_driver_register() - Register client driver with serdev core 591 * @sdrv: client driver to be associated with client-device. 592 * 593 * This API will register the client driver with the serdev framework. 594 * It is typically called from the driver's module-init function. 595 */ 596 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner) 597 { 598 sdrv->driver.bus = &serdev_bus_type; 599 sdrv->driver.owner = owner; 600 601 /* force drivers to async probe so I/O is possible in probe */ 602 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS; 603 604 return driver_register(&sdrv->driver); 605 } 606 EXPORT_SYMBOL_GPL(__serdev_device_driver_register); 607 608 static void __exit serdev_exit(void) 609 { 610 bus_unregister(&serdev_bus_type); 611 } 612 module_exit(serdev_exit); 613 614 static int __init serdev_init(void) 615 { 616 int ret; 617 618 ret = bus_register(&serdev_bus_type); 619 if (ret) 620 return ret; 621 622 is_registered = true; 623 return 0; 624 } 625 /* Must be before serial drivers register */ 626 postcore_initcall(serdev_init); 627 628 MODULE_AUTHOR("Rob Herring <robh@kernel.org>"); 629 MODULE_LICENSE("GPL v2"); 630 MODULE_DESCRIPTION("Serial attached device bus"); 631