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 void serdev_device_write_wakeup(struct serdev_device *serdev) 136 { 137 complete(&serdev->write_comp); 138 } 139 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup); 140 141 int serdev_device_write_buf(struct serdev_device *serdev, 142 const unsigned char *buf, size_t count) 143 { 144 struct serdev_controller *ctrl = serdev->ctrl; 145 146 if (!ctrl || !ctrl->ops->write_buf) 147 return -EINVAL; 148 149 return ctrl->ops->write_buf(ctrl, buf, count); 150 } 151 EXPORT_SYMBOL_GPL(serdev_device_write_buf); 152 153 int serdev_device_write(struct serdev_device *serdev, 154 const unsigned char *buf, size_t count, 155 unsigned long timeout) 156 { 157 struct serdev_controller *ctrl = serdev->ctrl; 158 int ret; 159 160 if (!ctrl || !ctrl->ops->write_buf || 161 (timeout && !serdev->ops->write_wakeup)) 162 return -EINVAL; 163 164 mutex_lock(&serdev->write_lock); 165 do { 166 reinit_completion(&serdev->write_comp); 167 168 ret = ctrl->ops->write_buf(ctrl, buf, count); 169 if (ret < 0) 170 break; 171 172 buf += ret; 173 count -= ret; 174 175 } while (count && 176 (timeout = wait_for_completion_timeout(&serdev->write_comp, 177 timeout))); 178 mutex_unlock(&serdev->write_lock); 179 return ret < 0 ? ret : (count ? -ETIMEDOUT : 0); 180 } 181 EXPORT_SYMBOL_GPL(serdev_device_write); 182 183 void serdev_device_write_flush(struct serdev_device *serdev) 184 { 185 struct serdev_controller *ctrl = serdev->ctrl; 186 187 if (!ctrl || !ctrl->ops->write_flush) 188 return; 189 190 ctrl->ops->write_flush(ctrl); 191 } 192 EXPORT_SYMBOL_GPL(serdev_device_write_flush); 193 194 int serdev_device_write_room(struct serdev_device *serdev) 195 { 196 struct serdev_controller *ctrl = serdev->ctrl; 197 198 if (!ctrl || !ctrl->ops->write_room) 199 return 0; 200 201 return serdev->ctrl->ops->write_room(ctrl); 202 } 203 EXPORT_SYMBOL_GPL(serdev_device_write_room); 204 205 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed) 206 { 207 struct serdev_controller *ctrl = serdev->ctrl; 208 209 if (!ctrl || !ctrl->ops->set_baudrate) 210 return 0; 211 212 return ctrl->ops->set_baudrate(ctrl, speed); 213 214 } 215 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate); 216 217 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable) 218 { 219 struct serdev_controller *ctrl = serdev->ctrl; 220 221 if (!ctrl || !ctrl->ops->set_flow_control) 222 return; 223 224 ctrl->ops->set_flow_control(ctrl, enable); 225 } 226 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control); 227 228 int serdev_device_set_parity(struct serdev_device *serdev, 229 enum serdev_parity parity) 230 { 231 struct serdev_controller *ctrl = serdev->ctrl; 232 233 if (!ctrl || !ctrl->ops->set_parity) 234 return -ENOTSUPP; 235 236 return ctrl->ops->set_parity(ctrl, parity); 237 } 238 EXPORT_SYMBOL_GPL(serdev_device_set_parity); 239 240 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout) 241 { 242 struct serdev_controller *ctrl = serdev->ctrl; 243 244 if (!ctrl || !ctrl->ops->wait_until_sent) 245 return; 246 247 ctrl->ops->wait_until_sent(ctrl, timeout); 248 } 249 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent); 250 251 int serdev_device_get_tiocm(struct serdev_device *serdev) 252 { 253 struct serdev_controller *ctrl = serdev->ctrl; 254 255 if (!ctrl || !ctrl->ops->get_tiocm) 256 return -ENOTSUPP; 257 258 return ctrl->ops->get_tiocm(ctrl); 259 } 260 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm); 261 262 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) 263 { 264 struct serdev_controller *ctrl = serdev->ctrl; 265 266 if (!ctrl || !ctrl->ops->set_tiocm) 267 return -ENOTSUPP; 268 269 return ctrl->ops->set_tiocm(ctrl, set, clear); 270 } 271 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm); 272 273 static int serdev_drv_probe(struct device *dev) 274 { 275 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); 276 277 return sdrv->probe(to_serdev_device(dev)); 278 } 279 280 static int serdev_drv_remove(struct device *dev) 281 { 282 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver); 283 284 sdrv->remove(to_serdev_device(dev)); 285 return 0; 286 } 287 288 static ssize_t modalias_show(struct device *dev, 289 struct device_attribute *attr, char *buf) 290 { 291 int len; 292 293 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 294 if (len != -ENODEV) 295 return len; 296 297 return of_device_modalias(dev, buf, PAGE_SIZE); 298 } 299 DEVICE_ATTR_RO(modalias); 300 301 static struct attribute *serdev_device_attrs[] = { 302 &dev_attr_modalias.attr, 303 NULL, 304 }; 305 ATTRIBUTE_GROUPS(serdev_device); 306 307 static struct bus_type serdev_bus_type = { 308 .name = "serial", 309 .match = serdev_device_match, 310 .probe = serdev_drv_probe, 311 .remove = serdev_drv_remove, 312 .uevent = serdev_uevent, 313 .dev_groups = serdev_device_groups, 314 }; 315 316 /** 317 * serdev_controller_alloc() - Allocate a new serdev device 318 * @ctrl: associated controller 319 * 320 * Caller is responsible for either calling serdev_device_add() to add the 321 * newly allocated controller, or calling serdev_device_put() to discard it. 322 */ 323 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl) 324 { 325 struct serdev_device *serdev; 326 327 serdev = kzalloc(sizeof(*serdev), GFP_KERNEL); 328 if (!serdev) 329 return NULL; 330 331 serdev->ctrl = ctrl; 332 device_initialize(&serdev->dev); 333 serdev->dev.parent = &ctrl->dev; 334 serdev->dev.bus = &serdev_bus_type; 335 serdev->dev.type = &serdev_device_type; 336 init_completion(&serdev->write_comp); 337 mutex_init(&serdev->write_lock); 338 return serdev; 339 } 340 EXPORT_SYMBOL_GPL(serdev_device_alloc); 341 342 /** 343 * serdev_controller_alloc() - Allocate a new serdev controller 344 * @parent: parent device 345 * @size: size of private data 346 * 347 * Caller is responsible for either calling serdev_controller_add() to add the 348 * newly allocated controller, or calling serdev_controller_put() to discard it. 349 * The allocated private data region may be accessed via 350 * serdev_controller_get_drvdata() 351 */ 352 struct serdev_controller *serdev_controller_alloc(struct device *parent, 353 size_t size) 354 { 355 struct serdev_controller *ctrl; 356 int id; 357 358 if (WARN_ON(!parent)) 359 return NULL; 360 361 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL); 362 if (!ctrl) 363 return NULL; 364 365 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL); 366 if (id < 0) { 367 dev_err(parent, 368 "unable to allocate serdev controller identifier.\n"); 369 goto err_free; 370 } 371 372 ctrl->nr = id; 373 374 device_initialize(&ctrl->dev); 375 ctrl->dev.type = &serdev_ctrl_type; 376 ctrl->dev.bus = &serdev_bus_type; 377 ctrl->dev.parent = parent; 378 ctrl->dev.of_node = parent->of_node; 379 serdev_controller_set_drvdata(ctrl, &ctrl[1]); 380 381 dev_set_name(&ctrl->dev, "serial%d", id); 382 383 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id); 384 return ctrl; 385 386 err_free: 387 kfree(ctrl); 388 389 return NULL; 390 } 391 EXPORT_SYMBOL_GPL(serdev_controller_alloc); 392 393 static int of_serdev_register_devices(struct serdev_controller *ctrl) 394 { 395 struct device_node *node; 396 struct serdev_device *serdev = NULL; 397 int err; 398 bool found = false; 399 400 for_each_available_child_of_node(ctrl->dev.of_node, node) { 401 if (!of_get_property(node, "compatible", NULL)) 402 continue; 403 404 dev_dbg(&ctrl->dev, "adding child %pOF\n", node); 405 406 serdev = serdev_device_alloc(ctrl); 407 if (!serdev) 408 continue; 409 410 serdev->dev.of_node = node; 411 412 err = serdev_device_add(serdev); 413 if (err) { 414 dev_err(&serdev->dev, 415 "failure adding device. status %d\n", err); 416 serdev_device_put(serdev); 417 } else 418 found = true; 419 } 420 if (!found) 421 return -ENODEV; 422 423 return 0; 424 } 425 426 #ifdef CONFIG_ACPI 427 static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl, 428 struct acpi_device *adev) 429 { 430 struct serdev_device *serdev = NULL; 431 int err; 432 433 if (acpi_bus_get_status(adev) || !adev->status.present || 434 acpi_device_enumerated(adev)) 435 return AE_OK; 436 437 serdev = serdev_device_alloc(ctrl); 438 if (!serdev) { 439 dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n", 440 dev_name(&adev->dev)); 441 return AE_NO_MEMORY; 442 } 443 444 ACPI_COMPANION_SET(&serdev->dev, adev); 445 acpi_device_set_enumerated(adev); 446 447 err = serdev_device_add(serdev); 448 if (err) { 449 dev_err(&serdev->dev, 450 "failure adding ACPI serdev device. status %d\n", err); 451 serdev_device_put(serdev); 452 } 453 454 return AE_OK; 455 } 456 457 static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level, 458 void *data, void **return_value) 459 { 460 struct serdev_controller *ctrl = data; 461 struct acpi_device *adev; 462 463 if (acpi_bus_get_device(handle, &adev)) 464 return AE_OK; 465 466 return acpi_serdev_register_device(ctrl, adev); 467 } 468 469 static int acpi_serdev_register_devices(struct serdev_controller *ctrl) 470 { 471 acpi_status status; 472 acpi_handle handle; 473 474 handle = ACPI_HANDLE(ctrl->dev.parent); 475 if (!handle) 476 return -ENODEV; 477 478 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, 479 acpi_serdev_add_device, NULL, ctrl, NULL); 480 if (ACPI_FAILURE(status)) 481 dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n"); 482 483 if (!ctrl->serdev) 484 return -ENODEV; 485 486 return 0; 487 } 488 #else 489 static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl) 490 { 491 return -ENODEV; 492 } 493 #endif /* CONFIG_ACPI */ 494 495 /** 496 * serdev_controller_add() - Add an serdev controller 497 * @ctrl: controller to be registered. 498 * 499 * Register a controller previously allocated via serdev_controller_alloc() with 500 * the serdev core. 501 */ 502 int serdev_controller_add(struct serdev_controller *ctrl) 503 { 504 int ret_of, ret_acpi, ret; 505 506 /* Can't register until after driver model init */ 507 if (WARN_ON(!is_registered)) 508 return -EAGAIN; 509 510 ret = device_add(&ctrl->dev); 511 if (ret) 512 return ret; 513 514 ret_of = of_serdev_register_devices(ctrl); 515 ret_acpi = acpi_serdev_register_devices(ctrl); 516 if (ret_of && ret_acpi) { 517 dev_dbg(&ctrl->dev, "no devices registered: of:%d acpi:%d\n", 518 ret_of, ret_acpi); 519 ret = -ENODEV; 520 goto out_dev_del; 521 } 522 523 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n", 524 ctrl->nr, &ctrl->dev); 525 return 0; 526 527 out_dev_del: 528 device_del(&ctrl->dev); 529 return ret; 530 }; 531 EXPORT_SYMBOL_GPL(serdev_controller_add); 532 533 /* Remove a device associated with a controller */ 534 static int serdev_remove_device(struct device *dev, void *data) 535 { 536 struct serdev_device *serdev = to_serdev_device(dev); 537 if (dev->type == &serdev_device_type) 538 serdev_device_remove(serdev); 539 return 0; 540 } 541 542 /** 543 * serdev_controller_remove(): remove an serdev controller 544 * @ctrl: controller to remove 545 * 546 * Remove a serdev controller. Caller is responsible for calling 547 * serdev_controller_put() to discard the allocated controller. 548 */ 549 void serdev_controller_remove(struct serdev_controller *ctrl) 550 { 551 int dummy; 552 553 if (!ctrl) 554 return; 555 556 dummy = device_for_each_child(&ctrl->dev, NULL, 557 serdev_remove_device); 558 device_del(&ctrl->dev); 559 } 560 EXPORT_SYMBOL_GPL(serdev_controller_remove); 561 562 /** 563 * serdev_driver_register() - Register client driver with serdev core 564 * @sdrv: client driver to be associated with client-device. 565 * 566 * This API will register the client driver with the serdev framework. 567 * It is typically called from the driver's module-init function. 568 */ 569 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner) 570 { 571 sdrv->driver.bus = &serdev_bus_type; 572 sdrv->driver.owner = owner; 573 574 /* force drivers to async probe so I/O is possible in probe */ 575 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS; 576 577 return driver_register(&sdrv->driver); 578 } 579 EXPORT_SYMBOL_GPL(__serdev_device_driver_register); 580 581 static void __exit serdev_exit(void) 582 { 583 bus_unregister(&serdev_bus_type); 584 } 585 module_exit(serdev_exit); 586 587 static int __init serdev_init(void) 588 { 589 int ret; 590 591 ret = bus_register(&serdev_bus_type); 592 if (ret) 593 return ret; 594 595 is_registered = true; 596 return 0; 597 } 598 /* Must be before serial drivers register */ 599 postcore_initcall(serdev_init); 600 601 MODULE_AUTHOR("Rob Herring <robh@kernel.org>"); 602 MODULE_LICENSE("GPL v2"); 603 MODULE_DESCRIPTION("Serial attached device bus"); 604