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