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_graph.h> 16 #include <linux/of_device.h> 17 #include <linux/pm_domain.h> 18 #include <linux/pm_runtime.h> 19 #include <linux/property.h> 20 #include <linux/sched.h> 21 #include <linux/serdev.h> 22 #include <linux/slab.h> 23 24 #include <linux/platform_data/x86/apple.h> 25 26 static bool is_registered; 27 static DEFINE_IDA(ctrl_ida); 28 29 static ssize_t modalias_show(struct device *dev, 30 struct device_attribute *attr, char *buf) 31 { 32 int len; 33 34 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 35 if (len != -ENODEV) 36 return len; 37 38 return of_device_modalias(dev, buf, PAGE_SIZE); 39 } 40 static DEVICE_ATTR_RO(modalias); 41 42 static struct attribute *serdev_device_attrs[] = { 43 &dev_attr_modalias.attr, 44 NULL, 45 }; 46 ATTRIBUTE_GROUPS(serdev_device); 47 48 static int serdev_device_uevent(const struct device *dev, struct kobj_uevent_env *env) 49 { 50 int rc; 51 52 /* TODO: platform modalias */ 53 54 rc = acpi_device_uevent_modalias(dev, env); 55 if (rc != -ENODEV) 56 return rc; 57 58 return of_device_uevent_modalias(dev, env); 59 } 60 61 static void serdev_device_release(struct device *dev) 62 { 63 struct serdev_device *serdev = to_serdev_device(dev); 64 kfree(serdev); 65 } 66 67 static const struct device_type serdev_device_type = { 68 .groups = serdev_device_groups, 69 .uevent = serdev_device_uevent, 70 .release = serdev_device_release, 71 }; 72 73 static bool is_serdev_device(const struct device *dev) 74 { 75 return dev->type == &serdev_device_type; 76 } 77 78 static void serdev_ctrl_release(struct device *dev) 79 { 80 struct serdev_controller *ctrl = to_serdev_controller(dev); 81 ida_free(&ctrl_ida, ctrl->nr); 82 kfree(ctrl); 83 } 84 85 static const struct device_type serdev_ctrl_type = { 86 .release = serdev_ctrl_release, 87 }; 88 89 static int serdev_device_match(struct device *dev, const struct device_driver *drv) 90 { 91 if (!is_serdev_device(dev)) 92 return 0; 93 94 /* TODO: platform matching */ 95 if (acpi_driver_match_device(dev, drv)) 96 return 1; 97 98 return of_driver_match_device(dev, drv); 99 } 100 101 /** 102 * serdev_device_add() - add a device previously constructed via serdev_device_alloc() 103 * @serdev: serdev_device to be added 104 */ 105 int serdev_device_add(struct serdev_device *serdev) 106 { 107 struct serdev_controller *ctrl = serdev->ctrl; 108 struct device *parent = serdev->dev.parent; 109 int err; 110 111 dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr); 112 113 /* Only a single slave device is currently supported. */ 114 if (ctrl->serdev) { 115 dev_err(&serdev->dev, "controller busy\n"); 116 return -EBUSY; 117 } 118 ctrl->serdev = serdev; 119 120 err = device_add(&serdev->dev); 121 if (err < 0) { 122 dev_err(&serdev->dev, "Failed to add serdev: %d\n", err); 123 goto err_clear_serdev; 124 } 125 126 dev_dbg(&serdev->dev, "serdev registered successfully\n"); 127 128 return 0; 129 130 err_clear_serdev: 131 ctrl->serdev = NULL; 132 return err; 133 } 134 EXPORT_SYMBOL_GPL(serdev_device_add); 135 136 /** 137 * serdev_device_remove(): remove an serdev device 138 * @serdev: serdev_device to be removed 139 */ 140 void serdev_device_remove(struct serdev_device *serdev) 141 { 142 struct serdev_controller *ctrl = serdev->ctrl; 143 144 device_unregister(&serdev->dev); 145 ctrl->serdev = NULL; 146 } 147 EXPORT_SYMBOL_GPL(serdev_device_remove); 148 149 int serdev_device_open(struct serdev_device *serdev) 150 { 151 struct serdev_controller *ctrl = serdev->ctrl; 152 int ret; 153 154 if (!ctrl || !ctrl->ops->open) 155 return -EINVAL; 156 157 ret = ctrl->ops->open(ctrl); 158 if (ret) 159 return ret; 160 161 ret = pm_runtime_get_sync(&ctrl->dev); 162 if (ret < 0) { 163 pm_runtime_put_noidle(&ctrl->dev); 164 goto err_close; 165 } 166 167 return 0; 168 169 err_close: 170 if (ctrl->ops->close) 171 ctrl->ops->close(ctrl); 172 173 return ret; 174 } 175 EXPORT_SYMBOL_GPL(serdev_device_open); 176 177 void serdev_device_close(struct serdev_device *serdev) 178 { 179 struct serdev_controller *ctrl = serdev->ctrl; 180 181 if (!ctrl || !ctrl->ops->close) 182 return; 183 184 pm_runtime_put(&ctrl->dev); 185 186 ctrl->ops->close(ctrl); 187 } 188 EXPORT_SYMBOL_GPL(serdev_device_close); 189 190 static void devm_serdev_device_close(void *serdev) 191 { 192 serdev_device_close(serdev); 193 } 194 195 int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev) 196 { 197 int ret; 198 199 ret = serdev_device_open(serdev); 200 if (ret) 201 return ret; 202 203 return devm_add_action_or_reset(dev, devm_serdev_device_close, serdev); 204 } 205 EXPORT_SYMBOL_GPL(devm_serdev_device_open); 206 207 void serdev_device_write_wakeup(struct serdev_device *serdev) 208 { 209 complete(&serdev->write_comp); 210 } 211 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup); 212 213 /** 214 * serdev_device_write_buf() - write data asynchronously 215 * @serdev: serdev device 216 * @buf: data to be written 217 * @count: number of bytes to write 218 * 219 * Write data to the device asynchronously. 220 * 221 * Note that any accepted data has only been buffered by the controller; use 222 * serdev_device_wait_until_sent() to make sure the controller write buffer 223 * has actually been emptied. 224 * 225 * Return: The number of bytes written (less than count if not enough room in 226 * the write buffer), or a negative errno on errors. 227 */ 228 int serdev_device_write_buf(struct serdev_device *serdev, const u8 *buf, size_t count) 229 { 230 struct serdev_controller *ctrl = serdev->ctrl; 231 232 if (!ctrl || !ctrl->ops->write_buf) 233 return -EINVAL; 234 235 return ctrl->ops->write_buf(ctrl, buf, count); 236 } 237 EXPORT_SYMBOL_GPL(serdev_device_write_buf); 238 239 /** 240 * serdev_device_write() - write data synchronously 241 * @serdev: serdev device 242 * @buf: data to be written 243 * @count: number of bytes to write 244 * @timeout: timeout in jiffies, or 0 to wait indefinitely 245 * 246 * Write data to the device synchronously by repeatedly calling 247 * serdev_device_write() until the controller has accepted all data (unless 248 * interrupted by a timeout or a signal). 249 * 250 * Note that any accepted data has only been buffered by the controller; use 251 * serdev_device_wait_until_sent() to make sure the controller write buffer 252 * has actually been emptied. 253 * 254 * Note that this function depends on serdev_device_write_wakeup() being 255 * called in the serdev driver write_wakeup() callback. 256 * 257 * Return: The number of bytes written (less than count if interrupted), 258 * -ETIMEDOUT or -ERESTARTSYS if interrupted before any bytes were written, or 259 * a negative errno on errors. 260 */ 261 ssize_t serdev_device_write(struct serdev_device *serdev, const u8 *buf, 262 size_t count, long timeout) 263 { 264 struct serdev_controller *ctrl = serdev->ctrl; 265 size_t written = 0; 266 ssize_t ret; 267 268 if (!ctrl || !ctrl->ops->write_buf || !serdev->ops->write_wakeup) 269 return -EINVAL; 270 271 if (timeout == 0) 272 timeout = MAX_SCHEDULE_TIMEOUT; 273 274 mutex_lock(&serdev->write_lock); 275 do { 276 reinit_completion(&serdev->write_comp); 277 278 ret = ctrl->ops->write_buf(ctrl, buf, count); 279 if (ret < 0) 280 break; 281 282 written += ret; 283 buf += ret; 284 count -= ret; 285 286 if (count == 0) 287 break; 288 289 timeout = wait_for_completion_interruptible_timeout(&serdev->write_comp, 290 timeout); 291 } while (timeout > 0); 292 mutex_unlock(&serdev->write_lock); 293 294 if (ret < 0) 295 return ret; 296 297 if (timeout <= 0 && written == 0) { 298 if (timeout == -ERESTARTSYS) 299 return -ERESTARTSYS; 300 else 301 return -ETIMEDOUT; 302 } 303 304 return written; 305 } 306 EXPORT_SYMBOL_GPL(serdev_device_write); 307 308 void serdev_device_write_flush(struct serdev_device *serdev) 309 { 310 struct serdev_controller *ctrl = serdev->ctrl; 311 312 if (!ctrl || !ctrl->ops->write_flush) 313 return; 314 315 ctrl->ops->write_flush(ctrl); 316 } 317 EXPORT_SYMBOL_GPL(serdev_device_write_flush); 318 319 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed) 320 { 321 struct serdev_controller *ctrl = serdev->ctrl; 322 323 if (!ctrl || !ctrl->ops->set_baudrate) 324 return 0; 325 326 return ctrl->ops->set_baudrate(ctrl, speed); 327 328 } 329 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate); 330 331 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable) 332 { 333 struct serdev_controller *ctrl = serdev->ctrl; 334 335 if (!ctrl || !ctrl->ops->set_flow_control) 336 return; 337 338 ctrl->ops->set_flow_control(ctrl, enable); 339 } 340 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control); 341 342 int serdev_device_set_parity(struct serdev_device *serdev, 343 enum serdev_parity parity) 344 { 345 struct serdev_controller *ctrl = serdev->ctrl; 346 347 if (!ctrl || !ctrl->ops->set_parity) 348 return -EOPNOTSUPP; 349 350 return ctrl->ops->set_parity(ctrl, parity); 351 } 352 EXPORT_SYMBOL_GPL(serdev_device_set_parity); 353 354 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout) 355 { 356 struct serdev_controller *ctrl = serdev->ctrl; 357 358 if (!ctrl || !ctrl->ops->wait_until_sent) 359 return; 360 361 ctrl->ops->wait_until_sent(ctrl, timeout); 362 } 363 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent); 364 365 int serdev_device_get_tiocm(struct serdev_device *serdev) 366 { 367 struct serdev_controller *ctrl = serdev->ctrl; 368 369 if (!ctrl || !ctrl->ops->get_tiocm) 370 return -EOPNOTSUPP; 371 372 return ctrl->ops->get_tiocm(ctrl); 373 } 374 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm); 375 376 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) 377 { 378 struct serdev_controller *ctrl = serdev->ctrl; 379 380 if (!ctrl || !ctrl->ops->set_tiocm) 381 return -EOPNOTSUPP; 382 383 return ctrl->ops->set_tiocm(ctrl, set, clear); 384 } 385 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm); 386 387 int serdev_device_break_ctl(struct serdev_device *serdev, int break_state) 388 { 389 struct serdev_controller *ctrl = serdev->ctrl; 390 391 if (!ctrl || !ctrl->ops->break_ctl) 392 return -EOPNOTSUPP; 393 394 return ctrl->ops->break_ctl(ctrl, break_state); 395 } 396 EXPORT_SYMBOL_GPL(serdev_device_break_ctl); 397 398 static int serdev_drv_probe(struct device *dev) 399 { 400 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); 401 int ret; 402 403 ret = dev_pm_domain_attach(dev, PD_FLAG_ATTACH_POWER_ON | 404 PD_FLAG_DETACH_POWER_OFF); 405 if (ret) 406 return ret; 407 408 return sdrv->probe(to_serdev_device(dev)); 409 } 410 411 static void serdev_drv_remove(struct device *dev) 412 { 413 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); 414 if (sdrv->remove) 415 sdrv->remove(to_serdev_device(dev)); 416 } 417 418 static void serdev_drv_shutdown(struct device *dev) 419 { 420 const struct serdev_device_driver *sdrv = 421 to_serdev_device_driver(dev->driver); 422 423 if (dev->driver && sdrv->shutdown) 424 sdrv->shutdown(to_serdev_device(dev)); 425 } 426 427 static const struct bus_type serdev_bus_type = { 428 .name = "serial", 429 .match = serdev_device_match, 430 .probe = serdev_drv_probe, 431 .remove = serdev_drv_remove, 432 .shutdown = serdev_drv_shutdown, 433 }; 434 435 /** 436 * serdev_device_alloc() - Allocate a new serdev device 437 * @ctrl: associated controller 438 * 439 * Caller is responsible for either calling serdev_device_add() to add the 440 * newly allocated controller, or calling serdev_device_put() to discard it. 441 */ 442 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl) 443 { 444 struct serdev_device *serdev; 445 446 serdev = kzalloc_obj(*serdev); 447 if (!serdev) 448 return NULL; 449 450 serdev->ctrl = ctrl; 451 device_initialize(&serdev->dev); 452 serdev->dev.parent = &ctrl->dev; 453 serdev->dev.bus = &serdev_bus_type; 454 serdev->dev.type = &serdev_device_type; 455 init_completion(&serdev->write_comp); 456 mutex_init(&serdev->write_lock); 457 return serdev; 458 } 459 EXPORT_SYMBOL_GPL(serdev_device_alloc); 460 461 /** 462 * serdev_controller_alloc() - Allocate a new serdev controller 463 * @host: serial port hardware controller device 464 * @parent: parent device 465 * @size: size of private data 466 * 467 * Caller is responsible for either calling serdev_controller_add() to add the 468 * newly allocated controller, or calling serdev_controller_put() to discard it. 469 * The allocated private data region may be accessed via 470 * serdev_controller_get_drvdata() 471 */ 472 struct serdev_controller *serdev_controller_alloc(struct device *host, 473 struct device *parent, 474 size_t size) 475 { 476 struct serdev_controller *ctrl; 477 int id; 478 479 if (WARN_ON(!parent)) 480 return NULL; 481 482 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL); 483 if (!ctrl) 484 return NULL; 485 486 id = ida_alloc(&ctrl_ida, GFP_KERNEL); 487 if (id < 0) { 488 dev_err(parent, 489 "unable to allocate serdev controller identifier.\n"); 490 goto err_free; 491 } 492 493 ctrl->nr = id; 494 495 device_initialize(&ctrl->dev); 496 ctrl->dev.type = &serdev_ctrl_type; 497 ctrl->dev.bus = &serdev_bus_type; 498 ctrl->dev.parent = parent; 499 ctrl->host = host; 500 device_set_node(&ctrl->dev, dev_fwnode(host)); 501 serdev_controller_set_drvdata(ctrl, &ctrl[1]); 502 503 dev_set_name(&ctrl->dev, "serial%d", id); 504 505 pm_runtime_no_callbacks(&ctrl->dev); 506 pm_suspend_ignore_children(&ctrl->dev, true); 507 508 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id); 509 return ctrl; 510 511 err_free: 512 kfree(ctrl); 513 514 return NULL; 515 } 516 EXPORT_SYMBOL_GPL(serdev_controller_alloc); 517 518 #ifdef CONFIG_OF 519 /** 520 * of_find_serdev_controller_by_node() - Find the serdev controller associated 521 * with the devicetree node 522 * @node: Devicetree node 523 * 524 * Return: Pointer to the serdev controller associated with the node. NULL if 525 * the controller is not found. Caller is responsible for calling 526 * serdev_controller_put() to drop the reference. 527 */ 528 struct serdev_controller *of_find_serdev_controller_by_node(struct device_node *node) 529 { 530 struct device *dev = bus_find_device_by_of_node(&serdev_bus_type, node); 531 532 return (dev && dev->type == &serdev_ctrl_type) ? to_serdev_controller(dev) : NULL; 533 } 534 EXPORT_SYMBOL_GPL(of_find_serdev_controller_by_node); 535 #endif 536 537 static int of_serdev_register_devices(struct serdev_controller *ctrl) 538 { 539 struct device_node *node; 540 struct serdev_device *serdev = NULL; 541 int err; 542 bool found = false; 543 544 for_each_available_child_of_node(ctrl->dev.of_node, node) { 545 if (!of_property_present(node, "compatible")) 546 continue; 547 548 dev_dbg(&ctrl->dev, "adding child %pOF\n", node); 549 550 serdev = serdev_device_alloc(ctrl); 551 if (!serdev) 552 continue; 553 554 device_set_node(&serdev->dev, of_fwnode_handle(node)); 555 556 err = serdev_device_add(serdev); 557 if (err) { 558 dev_err(&serdev->dev, 559 "failure adding device. status %pe\n", 560 ERR_PTR(err)); 561 serdev_device_put(serdev); 562 } else 563 found = true; 564 } 565 566 /* 567 * When the serdev controller is connected to an external connector like 568 * M.2 in DT, then the serdev devices may be created dynamically by the 569 * connector driver. 570 */ 571 if (!found && !of_graph_is_present(dev_of_node(&ctrl->dev))) 572 return -ENODEV; 573 574 return 0; 575 } 576 577 #ifdef CONFIG_ACPI 578 579 #define SERDEV_ACPI_MAX_SCAN_DEPTH 32 580 581 struct acpi_serdev_lookup { 582 acpi_handle device_handle; 583 acpi_handle controller_handle; 584 int n; 585 int index; 586 }; 587 588 /** 589 * serdev_acpi_get_uart_resource - Gets UARTSerialBus resource if type matches 590 * @ares: ACPI resource 591 * @uart: Pointer to UARTSerialBus resource will be returned here 592 * 593 * Checks if the given ACPI resource is of type UARTSerialBus. 594 * In this case, returns a pointer to it to the caller. 595 * 596 * Return: True if resource type is of UARTSerialBus, otherwise false. 597 */ 598 bool serdev_acpi_get_uart_resource(struct acpi_resource *ares, 599 struct acpi_resource_uart_serialbus **uart) 600 { 601 struct acpi_resource_uart_serialbus *sb; 602 603 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) 604 return false; 605 606 sb = &ares->data.uart_serial_bus; 607 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_UART) 608 return false; 609 610 *uart = sb; 611 return true; 612 } 613 EXPORT_SYMBOL_GPL(serdev_acpi_get_uart_resource); 614 615 static int acpi_serdev_parse_resource(struct acpi_resource *ares, void *data) 616 { 617 struct acpi_serdev_lookup *lookup = data; 618 struct acpi_resource_uart_serialbus *sb; 619 acpi_status status; 620 621 if (!serdev_acpi_get_uart_resource(ares, &sb)) 622 return 1; 623 624 if (lookup->index != -1 && lookup->n++ != lookup->index) 625 return 1; 626 627 status = acpi_get_handle(lookup->device_handle, 628 sb->resource_source.string_ptr, 629 &lookup->controller_handle); 630 if (ACPI_FAILURE(status)) 631 return 1; 632 633 /* 634 * NOTE: Ideally, we would also want to retrieve other properties here, 635 * once setting them before opening the device is supported by serdev. 636 */ 637 638 return 1; 639 } 640 641 static int acpi_serdev_do_lookup(struct acpi_device *adev, 642 struct acpi_serdev_lookup *lookup) 643 { 644 struct list_head resource_list; 645 int ret; 646 647 lookup->device_handle = acpi_device_handle(adev); 648 lookup->controller_handle = NULL; 649 lookup->n = 0; 650 651 INIT_LIST_HEAD(&resource_list); 652 ret = acpi_dev_get_resources(adev, &resource_list, 653 acpi_serdev_parse_resource, lookup); 654 acpi_dev_free_resource_list(&resource_list); 655 656 if (ret < 0) 657 return -EINVAL; 658 659 return 0; 660 } 661 662 static int acpi_serdev_check_resources(struct serdev_controller *ctrl, 663 struct acpi_device *adev) 664 { 665 struct acpi_serdev_lookup lookup; 666 int ret; 667 668 if (acpi_bus_get_status(adev) || !adev->status.present) 669 return -EINVAL; 670 671 /* Look for UARTSerialBusV2 resource */ 672 lookup.index = -1; // we only care for the last device 673 674 ret = acpi_serdev_do_lookup(adev, &lookup); 675 if (ret) 676 return ret; 677 678 /* 679 * Apple machines provide an empty resource template, so on those 680 * machines just look for immediate children with a "baud" property 681 * (from the _DSM method) instead. 682 */ 683 if (!lookup.controller_handle && x86_apple_machine && 684 !acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, NULL)) 685 acpi_get_parent(adev->handle, &lookup.controller_handle); 686 687 /* Make sure controller and ResourceSource handle match */ 688 if (!device_match_acpi_handle(ctrl->host, lookup.controller_handle)) 689 return -ENODEV; 690 691 return 0; 692 } 693 694 static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl, 695 struct acpi_device *adev) 696 { 697 struct serdev_device *serdev; 698 int err; 699 700 serdev = serdev_device_alloc(ctrl); 701 if (!serdev) { 702 dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n", 703 dev_name(&adev->dev)); 704 return AE_NO_MEMORY; 705 } 706 707 ACPI_COMPANION_SET(&serdev->dev, adev); 708 acpi_device_set_enumerated(adev); 709 710 err = serdev_device_add(serdev); 711 if (err) { 712 dev_err(&serdev->dev, 713 "failure adding ACPI serdev device. status %pe\n", 714 ERR_PTR(err)); 715 serdev_device_put(serdev); 716 } 717 718 return AE_OK; 719 } 720 721 static const struct acpi_device_id serdev_acpi_devices_blacklist[] = { 722 { "INT3511", 0 }, 723 { "INT3512", 0 }, 724 { }, 725 }; 726 727 static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level, 728 void *data, void **return_value) 729 { 730 struct acpi_device *adev = acpi_fetch_acpi_dev(handle); 731 struct serdev_controller *ctrl = data; 732 733 if (!adev || acpi_device_enumerated(adev)) 734 return AE_OK; 735 736 /* Skip if black listed */ 737 if (!acpi_match_device_ids(adev, serdev_acpi_devices_blacklist)) 738 return AE_OK; 739 740 if (acpi_serdev_check_resources(ctrl, adev)) 741 return AE_OK; 742 743 return acpi_serdev_register_device(ctrl, adev); 744 } 745 746 747 static int acpi_serdev_register_devices(struct serdev_controller *ctrl) 748 { 749 acpi_status status; 750 bool skip; 751 int ret; 752 753 if (!has_acpi_companion(ctrl->host)) 754 return -ENODEV; 755 756 /* 757 * Skip registration on boards where the ACPI tables are known to 758 * contain buggy devices. Note serdev_controller_add() must still 759 * succeed in this case, so that the proper serdev devices can be 760 * added "manually" later. 761 */ 762 ret = acpi_quirk_skip_serdev_enumeration(ctrl->host, &skip); 763 if (ret) 764 return ret; 765 if (skip) 766 return 0; 767 768 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 769 SERDEV_ACPI_MAX_SCAN_DEPTH, 770 acpi_serdev_add_device, NULL, ctrl, NULL); 771 if (ACPI_FAILURE(status)) 772 dev_warn(&ctrl->dev, "failed to enumerate serdev slaves\n"); 773 774 if (!ctrl->serdev) 775 return -ENODEV; 776 777 return 0; 778 } 779 #else 780 static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl) 781 { 782 return -ENODEV; 783 } 784 #endif /* CONFIG_ACPI */ 785 786 /** 787 * serdev_controller_add() - Add an serdev controller 788 * @ctrl: controller to be registered. 789 * 790 * Register a controller previously allocated via serdev_controller_alloc() with 791 * the serdev core. 792 */ 793 int serdev_controller_add(struct serdev_controller *ctrl) 794 { 795 int ret_of, ret_acpi, ret; 796 797 /* Can't register until after driver model init */ 798 if (WARN_ON(!is_registered)) 799 return -EAGAIN; 800 801 ret = device_add(&ctrl->dev); 802 if (ret) 803 return ret; 804 805 pm_runtime_enable(&ctrl->dev); 806 807 ret_of = of_serdev_register_devices(ctrl); 808 ret_acpi = acpi_serdev_register_devices(ctrl); 809 if (ret_of && ret_acpi) { 810 dev_dbg(&ctrl->dev, "no devices registered: of:%pe acpi:%pe\n", 811 ERR_PTR(ret_of), ERR_PTR(ret_acpi)); 812 ret = -ENODEV; 813 goto err_rpm_disable; 814 } 815 816 dev_dbg(&ctrl->dev, "serdev controller registered: dev:%p\n", &ctrl->dev); 817 return 0; 818 819 err_rpm_disable: 820 pm_runtime_disable(&ctrl->dev); 821 device_del(&ctrl->dev); 822 return ret; 823 }; 824 EXPORT_SYMBOL_GPL(serdev_controller_add); 825 826 /* Remove a device associated with a controller */ 827 static int serdev_remove_device(struct device *dev, void *data) 828 { 829 struct serdev_device *serdev = to_serdev_device(dev); 830 if (dev->type == &serdev_device_type) 831 serdev_device_remove(serdev); 832 return 0; 833 } 834 835 /** 836 * serdev_controller_remove(): remove an serdev controller 837 * @ctrl: controller to remove 838 * 839 * Remove a serdev controller. Caller is responsible for calling 840 * serdev_controller_put() to discard the allocated controller. 841 */ 842 void serdev_controller_remove(struct serdev_controller *ctrl) 843 { 844 if (!ctrl) 845 return; 846 847 device_for_each_child(&ctrl->dev, NULL, serdev_remove_device); 848 pm_runtime_disable(&ctrl->dev); 849 device_del(&ctrl->dev); 850 } 851 EXPORT_SYMBOL_GPL(serdev_controller_remove); 852 853 static void serdev_legacy_shutdown(struct serdev_device *serdev) 854 { 855 struct device *dev = &serdev->dev; 856 struct device_driver *driver = dev->driver; 857 858 driver->shutdown(dev); 859 } 860 861 /** 862 * __serdev_device_driver_register() - Register client driver with serdev core 863 * @sdrv: client driver to be associated with client-device. 864 * @owner: client driver owner to set. 865 * 866 * This API will register the client driver with the serdev framework. 867 * It is typically called from the driver's module-init function. 868 */ 869 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner) 870 { 871 sdrv->driver.bus = &serdev_bus_type; 872 sdrv->driver.owner = owner; 873 874 /* force drivers to async probe so I/O is possible in probe */ 875 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS; 876 877 if (!sdrv->shutdown && sdrv->driver.shutdown) 878 sdrv->shutdown = serdev_legacy_shutdown; 879 880 return driver_register(&sdrv->driver); 881 } 882 EXPORT_SYMBOL_GPL(__serdev_device_driver_register); 883 884 static void __exit serdev_exit(void) 885 { 886 bus_unregister(&serdev_bus_type); 887 ida_destroy(&ctrl_ida); 888 } 889 module_exit(serdev_exit); 890 891 static int __init serdev_init(void) 892 { 893 int ret; 894 895 ret = bus_register(&serdev_bus_type); 896 if (ret) 897 return ret; 898 899 is_registered = true; 900 return 0; 901 } 902 /* Must be before serial drivers register */ 903 postcore_initcall(serdev_init); 904 905 MODULE_AUTHOR("Rob Herring <robh@kernel.org>"); 906 MODULE_LICENSE("GPL v2"); 907 MODULE_DESCRIPTION("Serial attached device bus"); 908