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