1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * HSI core. 4 * 5 * Copyright (C) 2010 Nokia Corporation. All rights reserved. 6 * 7 * Contact: Carlos Chinea <carlos.chinea@nokia.com> 8 */ 9 #include <linux/hsi/hsi.h> 10 #include <linux/compiler.h> 11 #include <linux/list.h> 12 #include <linux/kobject.h> 13 #include <linux/slab.h> 14 #include <linux/string.h> 15 #include <linux/notifier.h> 16 #include <linux/of.h> 17 #include <linux/of_device.h> 18 #include "hsi_core.h" 19 20 static ssize_t modalias_show(struct device *dev, 21 struct device_attribute *a __maybe_unused, char *buf) 22 { 23 return sprintf(buf, "hsi:%s\n", dev_name(dev)); 24 } 25 static DEVICE_ATTR_RO(modalias); 26 27 static struct attribute *hsi_bus_dev_attrs[] = { 28 &dev_attr_modalias.attr, 29 NULL, 30 }; 31 ATTRIBUTE_GROUPS(hsi_bus_dev); 32 33 static int hsi_bus_uevent(const struct device *dev, struct kobj_uevent_env *env) 34 { 35 add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev)); 36 37 return 0; 38 } 39 40 static int hsi_bus_match(struct device *dev, const struct device_driver *driver) 41 { 42 if (of_driver_match_device(dev, driver)) 43 return true; 44 45 if (strcmp(dev_name(dev), driver->name) == 0) 46 return true; 47 48 return false; 49 } 50 51 static const struct bus_type hsi_bus_type = { 52 .name = "hsi", 53 .dev_groups = hsi_bus_dev_groups, 54 .match = hsi_bus_match, 55 .uevent = hsi_bus_uevent, 56 }; 57 58 static void hsi_client_release(struct device *dev) 59 { 60 struct hsi_client *cl = to_hsi_client(dev); 61 62 kfree(cl->tx_cfg.channels); 63 kfree(cl->rx_cfg.channels); 64 kfree(cl); 65 } 66 67 struct hsi_client *hsi_new_client(struct hsi_port *port, 68 struct hsi_board_info *info) 69 { 70 struct hsi_client *cl; 71 size_t size; 72 73 cl = kzalloc_obj(*cl); 74 if (!cl) 75 goto err; 76 77 cl->tx_cfg = info->tx_cfg; 78 if (cl->tx_cfg.channels) { 79 size = cl->tx_cfg.num_channels * sizeof(*cl->tx_cfg.channels); 80 cl->tx_cfg.channels = kmemdup(info->tx_cfg.channels, size, 81 GFP_KERNEL); 82 if (!cl->tx_cfg.channels) 83 goto err_tx; 84 } 85 86 cl->rx_cfg = info->rx_cfg; 87 if (cl->rx_cfg.channels) { 88 size = cl->rx_cfg.num_channels * sizeof(*cl->rx_cfg.channels); 89 cl->rx_cfg.channels = kmemdup(info->rx_cfg.channels, size, 90 GFP_KERNEL); 91 if (!cl->rx_cfg.channels) 92 goto err_rx; 93 } 94 95 cl->device.bus = &hsi_bus_type; 96 cl->device.parent = &port->device; 97 cl->device.release = hsi_client_release; 98 dev_set_name(&cl->device, "%s", info->name); 99 cl->device.platform_data = info->platform_data; 100 if (info->archdata) 101 cl->device.archdata = *info->archdata; 102 if (device_register(&cl->device) < 0) { 103 pr_err("hsi: failed to register client: %s\n", info->name); 104 put_device(&cl->device); 105 goto err; 106 } 107 108 return cl; 109 err_rx: 110 kfree(cl->tx_cfg.channels); 111 err_tx: 112 kfree(cl); 113 err: 114 return NULL; 115 } 116 EXPORT_SYMBOL_GPL(hsi_new_client); 117 118 static void hsi_scan_board_info(struct hsi_controller *hsi) 119 { 120 struct hsi_cl_info *cl_info; 121 struct hsi_port *p; 122 123 list_for_each_entry(cl_info, &hsi_board_list, list) 124 if (cl_info->info.hsi_id == hsi->id) { 125 p = hsi_find_port_num(hsi, cl_info->info.port); 126 if (!p) 127 continue; 128 hsi_new_client(p, &cl_info->info); 129 } 130 } 131 132 #ifdef CONFIG_OF 133 static struct hsi_board_info hsi_char_dev_info = { 134 .name = "hsi_char", 135 }; 136 137 static int hsi_of_property_parse_mode(struct device_node *client, char *name, 138 unsigned int *result) 139 { 140 const char *mode; 141 int err; 142 143 err = of_property_read_string(client, name, &mode); 144 if (err < 0) 145 return err; 146 147 if (strcmp(mode, "stream") == 0) 148 *result = HSI_MODE_STREAM; 149 else if (strcmp(mode, "frame") == 0) 150 *result = HSI_MODE_FRAME; 151 else 152 return -EINVAL; 153 154 return 0; 155 } 156 157 static int hsi_of_property_parse_flow(struct device_node *client, char *name, 158 unsigned int *result) 159 { 160 const char *flow; 161 int err; 162 163 err = of_property_read_string(client, name, &flow); 164 if (err < 0) 165 return err; 166 167 if (strcmp(flow, "synchronized") == 0) 168 *result = HSI_FLOW_SYNC; 169 else if (strcmp(flow, "pipeline") == 0) 170 *result = HSI_FLOW_PIPE; 171 else 172 return -EINVAL; 173 174 return 0; 175 } 176 177 static int hsi_of_property_parse_arb_mode(struct device_node *client, 178 char *name, unsigned int *result) 179 { 180 const char *arb_mode; 181 int err; 182 183 err = of_property_read_string(client, name, &arb_mode); 184 if (err < 0) 185 return err; 186 187 if (strcmp(arb_mode, "round-robin") == 0) 188 *result = HSI_ARB_RR; 189 else if (strcmp(arb_mode, "priority") == 0) 190 *result = HSI_ARB_PRIO; 191 else 192 return -EINVAL; 193 194 return 0; 195 } 196 197 static void hsi_add_client_from_dt(struct hsi_port *port, 198 struct device_node *client) 199 { 200 struct hsi_client *cl; 201 struct hsi_channel channel; 202 struct property *prop; 203 char name[32]; 204 int length, cells, err, i, max_chan, mode; 205 206 cl = kzalloc_obj(*cl); 207 if (!cl) 208 return; 209 210 err = of_alias_from_compatible(client, name, sizeof(name)); 211 if (err) 212 goto err; 213 214 err = hsi_of_property_parse_mode(client, "hsi-mode", &mode); 215 if (err) { 216 err = hsi_of_property_parse_mode(client, "hsi-rx-mode", 217 &cl->rx_cfg.mode); 218 if (err) 219 goto err; 220 221 err = hsi_of_property_parse_mode(client, "hsi-tx-mode", 222 &cl->tx_cfg.mode); 223 if (err) 224 goto err; 225 } else { 226 cl->rx_cfg.mode = mode; 227 cl->tx_cfg.mode = mode; 228 } 229 230 err = of_property_read_u32(client, "hsi-speed-kbps", 231 &cl->tx_cfg.speed); 232 if (err) 233 goto err; 234 cl->rx_cfg.speed = cl->tx_cfg.speed; 235 236 err = hsi_of_property_parse_flow(client, "hsi-flow", 237 &cl->rx_cfg.flow); 238 if (err) 239 goto err; 240 241 err = hsi_of_property_parse_arb_mode(client, "hsi-arb-mode", 242 &cl->rx_cfg.arb_mode); 243 if (err) 244 goto err; 245 246 prop = of_find_property(client, "hsi-channel-ids", &length); 247 if (!prop) { 248 err = -EINVAL; 249 goto err; 250 } 251 252 cells = length / sizeof(u32); 253 254 cl->rx_cfg.num_channels = cells; 255 cl->tx_cfg.num_channels = cells; 256 cl->rx_cfg.channels = kzalloc_objs(channel, cells); 257 if (!cl->rx_cfg.channels) { 258 err = -ENOMEM; 259 goto err; 260 } 261 262 cl->tx_cfg.channels = kzalloc_objs(channel, cells); 263 if (!cl->tx_cfg.channels) { 264 err = -ENOMEM; 265 goto err2; 266 } 267 268 max_chan = 0; 269 for (i = 0; i < cells; i++) { 270 err = of_property_read_u32_index(client, "hsi-channel-ids", i, 271 &channel.id); 272 if (err) 273 goto err3; 274 275 err = of_property_read_string_index(client, "hsi-channel-names", 276 i, &channel.name); 277 if (err) 278 channel.name = NULL; 279 280 if (channel.id > max_chan) 281 max_chan = channel.id; 282 283 cl->rx_cfg.channels[i] = channel; 284 cl->tx_cfg.channels[i] = channel; 285 } 286 287 cl->rx_cfg.num_hw_channels = max_chan + 1; 288 cl->tx_cfg.num_hw_channels = max_chan + 1; 289 290 cl->device.bus = &hsi_bus_type; 291 cl->device.parent = &port->device; 292 cl->device.release = hsi_client_release; 293 cl->device.of_node = client; 294 295 dev_set_name(&cl->device, "%s", name); 296 if (device_register(&cl->device) < 0) { 297 pr_err("hsi: failed to register client: %s\n", name); 298 put_device(&cl->device); 299 } 300 301 return; 302 303 err3: 304 kfree(cl->tx_cfg.channels); 305 err2: 306 kfree(cl->rx_cfg.channels); 307 err: 308 kfree(cl); 309 pr_err("hsi client: missing or incorrect of property: err=%d\n", err); 310 } 311 312 void hsi_add_clients_from_dt(struct hsi_port *port, struct device_node *clients) 313 { 314 struct device_node *child; 315 316 /* register hsi-char device */ 317 hsi_new_client(port, &hsi_char_dev_info); 318 319 for_each_available_child_of_node(clients, child) 320 hsi_add_client_from_dt(port, child); 321 } 322 EXPORT_SYMBOL_GPL(hsi_add_clients_from_dt); 323 #endif 324 325 int hsi_remove_client(struct device *dev, void *data __maybe_unused) 326 { 327 device_unregister(dev); 328 329 return 0; 330 } 331 EXPORT_SYMBOL_GPL(hsi_remove_client); 332 333 static int hsi_remove_port(struct device *dev, void *data __maybe_unused) 334 { 335 device_for_each_child(dev, NULL, hsi_remove_client); 336 device_unregister(dev); 337 338 return 0; 339 } 340 341 static void hsi_controller_release(struct device *dev) 342 { 343 struct hsi_controller *hsi = to_hsi_controller(dev); 344 345 kfree(hsi); 346 } 347 348 static void hsi_port_release(struct device *dev) 349 { 350 kfree(to_hsi_port(dev)); 351 } 352 353 /** 354 * hsi_port_unregister_clients - Unregister an HSI port 355 * @port: The HSI port to unregister 356 */ 357 void hsi_port_unregister_clients(struct hsi_port *port) 358 { 359 device_for_each_child(&port->device, NULL, hsi_remove_client); 360 } 361 EXPORT_SYMBOL_GPL(hsi_port_unregister_clients); 362 363 /** 364 * hsi_unregister_controller - Unregister an HSI controller 365 * @hsi: The HSI controller to register 366 */ 367 void hsi_unregister_controller(struct hsi_controller *hsi) 368 { 369 device_for_each_child(&hsi->device, NULL, hsi_remove_port); 370 device_unregister(&hsi->device); 371 } 372 EXPORT_SYMBOL_GPL(hsi_unregister_controller); 373 374 /** 375 * hsi_register_controller - Register an HSI controller and its ports 376 * @hsi: The HSI controller to register 377 * 378 * Returns -errno on failure, 0 on success. 379 */ 380 int hsi_register_controller(struct hsi_controller *hsi) 381 { 382 unsigned int i; 383 int err; 384 385 err = device_add(&hsi->device); 386 if (err < 0) 387 return err; 388 for (i = 0; i < hsi->num_ports; i++) { 389 hsi->port[i]->device.parent = &hsi->device; 390 err = device_add(&hsi->port[i]->device); 391 if (err < 0) 392 goto out; 393 } 394 /* Populate HSI bus with HSI clients */ 395 hsi_scan_board_info(hsi); 396 397 return 0; 398 out: 399 while (i-- > 0) 400 device_del(&hsi->port[i]->device); 401 device_del(&hsi->device); 402 403 return err; 404 } 405 EXPORT_SYMBOL_GPL(hsi_register_controller); 406 407 /** 408 * hsi_register_client_driver - Register an HSI client to the HSI bus 409 * @drv: HSI client driver to register 410 * 411 * Returns -errno on failure, 0 on success. 412 */ 413 int hsi_register_client_driver(struct hsi_client_driver *drv) 414 { 415 drv->driver.bus = &hsi_bus_type; 416 417 return driver_register(&drv->driver); 418 } 419 EXPORT_SYMBOL_GPL(hsi_register_client_driver); 420 421 static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused) 422 { 423 return 0; 424 } 425 426 static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused) 427 { 428 return 0; 429 } 430 431 /** 432 * hsi_put_controller - Free an HSI controller 433 * 434 * @hsi: Pointer to the HSI controller to freed 435 * 436 * HSI controller drivers should only use this function if they need 437 * to free their allocated hsi_controller structures before a successful 438 * call to hsi_register_controller. Other use is not allowed. 439 */ 440 void hsi_put_controller(struct hsi_controller *hsi) 441 { 442 unsigned int i; 443 444 if (!hsi) 445 return; 446 447 for (i = 0; i < hsi->num_ports; i++) 448 if (hsi->port[i]) 449 put_device(&hsi->port[i]->device); 450 put_device(&hsi->device); 451 } 452 EXPORT_SYMBOL_GPL(hsi_put_controller); 453 454 /** 455 * hsi_alloc_controller - Allocate an HSI controller and its ports 456 * @n_ports: Number of ports on the HSI controller 457 * @flags: Kernel allocation flags 458 * 459 * Return NULL on failure or a pointer to an hsi_controller on success. 460 */ 461 struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags) 462 { 463 struct hsi_controller *hsi; 464 unsigned int i; 465 466 if (!n_ports) 467 return NULL; 468 469 hsi = kzalloc_flex(*hsi, port, n_ports, flags); 470 if (!hsi) 471 return NULL; 472 473 hsi->num_ports = n_ports; 474 hsi->device.release = hsi_controller_release; 475 device_initialize(&hsi->device); 476 477 for (i = 0; i < n_ports; i++) { 478 hsi->port[i] = kzalloc_obj(**hsi->port, flags); 479 if (hsi->port[i] == NULL) 480 goto out; 481 hsi->port[i]->num = i; 482 hsi->port[i]->async = hsi_dummy_msg; 483 hsi->port[i]->setup = hsi_dummy_cl; 484 hsi->port[i]->flush = hsi_dummy_cl; 485 hsi->port[i]->start_tx = hsi_dummy_cl; 486 hsi->port[i]->stop_tx = hsi_dummy_cl; 487 hsi->port[i]->release = hsi_dummy_cl; 488 mutex_init(&hsi->port[i]->lock); 489 BLOCKING_INIT_NOTIFIER_HEAD(&hsi->port[i]->n_head); 490 dev_set_name(&hsi->port[i]->device, "port%d", i); 491 hsi->port[i]->device.release = hsi_port_release; 492 device_initialize(&hsi->port[i]->device); 493 } 494 495 return hsi; 496 out: 497 hsi_put_controller(hsi); 498 499 return NULL; 500 } 501 EXPORT_SYMBOL_GPL(hsi_alloc_controller); 502 503 /** 504 * hsi_free_msg - Free an HSI message 505 * @msg: Pointer to the HSI message 506 * 507 * Client is responsible to free the buffers pointed by the scatterlists. 508 */ 509 void hsi_free_msg(struct hsi_msg *msg) 510 { 511 if (!msg) 512 return; 513 sg_free_table(&msg->sgt); 514 kfree(msg); 515 } 516 EXPORT_SYMBOL_GPL(hsi_free_msg); 517 518 /** 519 * hsi_alloc_msg - Allocate an HSI message 520 * @nents: Number of memory entries 521 * @flags: Kernel allocation flags 522 * 523 * nents can be 0. This mainly makes sense for read transfer. 524 * In that case, HSI drivers will call the complete callback when 525 * there is data to be read without consuming it. 526 * 527 * Return NULL on failure or a pointer to an hsi_msg on success. 528 */ 529 struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags) 530 { 531 struct hsi_msg *msg; 532 int err; 533 534 msg = kzalloc_obj(*msg, flags); 535 if (!msg) 536 return NULL; 537 538 if (!nents) 539 return msg; 540 541 err = sg_alloc_table(&msg->sgt, nents, flags); 542 if (unlikely(err)) { 543 kfree(msg); 544 msg = NULL; 545 } 546 547 return msg; 548 } 549 EXPORT_SYMBOL_GPL(hsi_alloc_msg); 550 551 /** 552 * hsi_async - Submit an HSI transfer to the controller 553 * @cl: HSI client sending the transfer 554 * @msg: The HSI transfer passed to controller 555 * 556 * The HSI message must have the channel, ttype, complete and destructor 557 * fields set beforehand. If nents > 0 then the client has to initialize 558 * also the scatterlists to point to the buffers to write to or read from. 559 * 560 * HSI controllers relay on pre-allocated buffers from their clients and they 561 * do not allocate buffers on their own. 562 * 563 * Once the HSI message transfer finishes, the HSI controller calls the 564 * complete callback with the status and actual_len fields of the HSI message 565 * updated. The complete callback can be called before returning from 566 * hsi_async. 567 * 568 * Returns -errno on failure or 0 on success 569 */ 570 int hsi_async(struct hsi_client *cl, struct hsi_msg *msg) 571 { 572 struct hsi_port *port = hsi_get_port(cl); 573 574 if (!hsi_port_claimed(cl)) 575 return -EACCES; 576 577 WARN_ON_ONCE(!msg->destructor || !msg->complete); 578 msg->cl = cl; 579 580 return port->async(msg); 581 } 582 EXPORT_SYMBOL_GPL(hsi_async); 583 584 /** 585 * hsi_claim_port - Claim the HSI client's port 586 * @cl: HSI client that wants to claim its port 587 * @share: Flag to indicate if the client wants to share the port or not. 588 * 589 * Returns -errno on failure, 0 on success. 590 */ 591 int hsi_claim_port(struct hsi_client *cl, unsigned int share) 592 { 593 struct hsi_port *port = hsi_get_port(cl); 594 int err = 0; 595 596 mutex_lock(&port->lock); 597 if ((port->claimed) && (!port->shared || !share)) { 598 err = -EBUSY; 599 goto out; 600 } 601 if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) { 602 err = -ENODEV; 603 goto out; 604 } 605 port->claimed++; 606 port->shared = !!share; 607 cl->pclaimed = 1; 608 out: 609 mutex_unlock(&port->lock); 610 611 return err; 612 } 613 EXPORT_SYMBOL_GPL(hsi_claim_port); 614 615 /** 616 * hsi_release_port - Release the HSI client's port 617 * @cl: HSI client which previously claimed its port 618 */ 619 void hsi_release_port(struct hsi_client *cl) 620 { 621 struct hsi_port *port = hsi_get_port(cl); 622 623 mutex_lock(&port->lock); 624 /* Allow HW driver to do some cleanup */ 625 port->release(cl); 626 if (cl->pclaimed) 627 port->claimed--; 628 BUG_ON(port->claimed < 0); 629 cl->pclaimed = 0; 630 if (!port->claimed) 631 port->shared = 0; 632 module_put(to_hsi_controller(port->device.parent)->owner); 633 mutex_unlock(&port->lock); 634 } 635 EXPORT_SYMBOL_GPL(hsi_release_port); 636 637 static int hsi_event_notifier_call(struct notifier_block *nb, 638 unsigned long event, void *data __maybe_unused) 639 { 640 struct hsi_client *cl = container_of(nb, struct hsi_client, nb); 641 642 (*cl->ehandler)(cl, event); 643 644 return 0; 645 } 646 647 /** 648 * hsi_register_port_event - Register a client to receive port events 649 * @cl: HSI client that wants to receive port events 650 * @handler: Event handler callback 651 * 652 * Clients should register a callback to be able to receive 653 * events from the ports. Registration should happen after 654 * claiming the port. 655 * The handler can be called in interrupt context. 656 * 657 * Returns -errno on error, or 0 on success. 658 */ 659 int hsi_register_port_event(struct hsi_client *cl, 660 void (*handler)(struct hsi_client *, unsigned long)) 661 { 662 struct hsi_port *port = hsi_get_port(cl); 663 664 if (!handler || cl->ehandler) 665 return -EINVAL; 666 if (!hsi_port_claimed(cl)) 667 return -EACCES; 668 cl->ehandler = handler; 669 cl->nb.notifier_call = hsi_event_notifier_call; 670 671 return blocking_notifier_chain_register(&port->n_head, &cl->nb); 672 } 673 EXPORT_SYMBOL_GPL(hsi_register_port_event); 674 675 /** 676 * hsi_unregister_port_event - Stop receiving port events for a client 677 * @cl: HSI client that wants to stop receiving port events 678 * 679 * Clients should call this function before releasing their associated 680 * port. 681 * 682 * Returns -errno on error, or 0 on success. 683 */ 684 int hsi_unregister_port_event(struct hsi_client *cl) 685 { 686 struct hsi_port *port = hsi_get_port(cl); 687 int err; 688 689 WARN_ON(!hsi_port_claimed(cl)); 690 691 err = blocking_notifier_chain_unregister(&port->n_head, &cl->nb); 692 if (!err) 693 cl->ehandler = NULL; 694 695 return err; 696 } 697 EXPORT_SYMBOL_GPL(hsi_unregister_port_event); 698 699 /** 700 * hsi_event - Notifies clients about port events 701 * @port: Port where the event occurred 702 * @event: The event type 703 * 704 * Clients should not be concerned about wake line behavior. However, due 705 * to a race condition in HSI HW protocol, clients need to be notified 706 * about wake line changes, so they can implement a workaround for it. 707 * 708 * Events: 709 * HSI_EVENT_START_RX - Incoming wake line high 710 * HSI_EVENT_STOP_RX - Incoming wake line down 711 * 712 * Returns -errno on error, or 0 on success. 713 */ 714 int hsi_event(struct hsi_port *port, unsigned long event) 715 { 716 return blocking_notifier_call_chain(&port->n_head, event, NULL); 717 } 718 EXPORT_SYMBOL_GPL(hsi_event); 719 720 /** 721 * hsi_get_channel_id_by_name - acquire channel id by channel name 722 * @cl: HSI client, which uses the channel 723 * @name: name the channel is known under 724 * 725 * Clients can call this function to get the hsi channel ids similar to 726 * requesting IRQs or GPIOs by name. This function assumes the same 727 * channel configuration is used for RX and TX. 728 * 729 * Returns -errno on error or channel id on success. 730 */ 731 int hsi_get_channel_id_by_name(struct hsi_client *cl, char *name) 732 { 733 int i; 734 735 if (!cl->rx_cfg.channels) 736 return -ENOENT; 737 738 for (i = 0; i < cl->rx_cfg.num_channels; i++) 739 if (!strcmp(cl->rx_cfg.channels[i].name, name)) 740 return cl->rx_cfg.channels[i].id; 741 742 return -ENXIO; 743 } 744 EXPORT_SYMBOL_GPL(hsi_get_channel_id_by_name); 745 746 static int __init hsi_init(void) 747 { 748 return bus_register(&hsi_bus_type); 749 } 750 postcore_initcall(hsi_init); 751 752 static void __exit hsi_exit(void) 753 { 754 bus_unregister(&hsi_bus_type); 755 } 756 module_exit(hsi_exit); 757 758 MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>"); 759 MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework"); 760 MODULE_LICENSE("GPL v2"); 761