1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
7 * Contact: Carlos Chinea <carlos.chinea@nokia.com>
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>
17 #include <linux/of_device.h>
20 static ssize_t modalias_show(struct device *dev,
21 struct device_attribute *a __maybe_unused, char *buf)
23 return sprintf(buf, "hsi:%s\n", dev_name(dev));
25 static DEVICE_ATTR_RO(modalias);
27 static struct attribute *hsi_bus_dev_attrs[] = {
28 &dev_attr_modalias.attr,
31 ATTRIBUTE_GROUPS(hsi_bus_dev);
33 static int hsi_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
35 add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev));
40 static int hsi_bus_match(struct device *dev, struct device_driver *driver)
42 if (of_driver_match_device(dev, driver))
45 if (strcmp(dev_name(dev), driver->name) == 0)
51 static struct bus_type hsi_bus_type = {
53 .dev_groups = hsi_bus_dev_groups,
54 .match = hsi_bus_match,
55 .uevent = hsi_bus_uevent,
58 static void hsi_client_release(struct device *dev)
60 struct hsi_client *cl = to_hsi_client(dev);
62 kfree(cl->tx_cfg.channels);
63 kfree(cl->rx_cfg.channels);
67 struct hsi_client *hsi_new_client(struct hsi_port *port,
68 struct hsi_board_info *info)
70 struct hsi_client *cl;
73 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
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,
82 if (!cl->tx_cfg.channels)
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,
91 if (!cl->rx_cfg.channels)
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;
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);
110 kfree(cl->tx_cfg.channels);
116 EXPORT_SYMBOL_GPL(hsi_new_client);
118 static void hsi_scan_board_info(struct hsi_controller *hsi)
120 struct hsi_cl_info *cl_info;
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);
128 hsi_new_client(p, &cl_info->info);
133 static struct hsi_board_info hsi_char_dev_info = {
137 static int hsi_of_property_parse_mode(struct device_node *client, char *name,
138 unsigned int *result)
143 err = of_property_read_string(client, name, &mode);
147 if (strcmp(mode, "stream") == 0)
148 *result = HSI_MODE_STREAM;
149 else if (strcmp(mode, "frame") == 0)
150 *result = HSI_MODE_FRAME;
157 static int hsi_of_property_parse_flow(struct device_node *client, char *name,
158 unsigned int *result)
163 err = of_property_read_string(client, name, &flow);
167 if (strcmp(flow, "synchronized") == 0)
168 *result = HSI_FLOW_SYNC;
169 else if (strcmp(flow, "pipeline") == 0)
170 *result = HSI_FLOW_PIPE;
177 static int hsi_of_property_parse_arb_mode(struct device_node *client,
178 char *name, unsigned int *result)
180 const char *arb_mode;
183 err = of_property_read_string(client, name, &arb_mode);
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;
197 static void hsi_add_client_from_dt(struct hsi_port *port,
198 struct device_node *client)
200 struct hsi_client *cl;
201 struct hsi_channel channel;
202 struct property *prop;
204 int length, cells, err, i, max_chan, mode;
206 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
210 err = of_alias_from_compatible(client, name, sizeof(name));
214 err = hsi_of_property_parse_mode(client, "hsi-mode", &mode);
216 err = hsi_of_property_parse_mode(client, "hsi-rx-mode",
221 err = hsi_of_property_parse_mode(client, "hsi-tx-mode",
226 cl->rx_cfg.mode = mode;
227 cl->tx_cfg.mode = mode;
230 err = of_property_read_u32(client, "hsi-speed-kbps",
234 cl->rx_cfg.speed = cl->tx_cfg.speed;
236 err = hsi_of_property_parse_flow(client, "hsi-flow",
241 err = hsi_of_property_parse_arb_mode(client, "hsi-arb-mode",
242 &cl->rx_cfg.arb_mode);
246 prop = of_find_property(client, "hsi-channel-ids", &length);
252 cells = length / sizeof(u32);
254 cl->rx_cfg.num_channels = cells;
255 cl->tx_cfg.num_channels = cells;
256 cl->rx_cfg.channels = kcalloc(cells, sizeof(channel), GFP_KERNEL);
257 if (!cl->rx_cfg.channels) {
262 cl->tx_cfg.channels = kcalloc(cells, sizeof(channel), GFP_KERNEL);
263 if (!cl->tx_cfg.channels) {
269 for (i = 0; i < cells; i++) {
270 err = of_property_read_u32_index(client, "hsi-channel-ids", i,
275 err = of_property_read_string_index(client, "hsi-channel-names",
280 if (channel.id > max_chan)
281 max_chan = channel.id;
283 cl->rx_cfg.channels[i] = channel;
284 cl->tx_cfg.channels[i] = channel;
287 cl->rx_cfg.num_hw_channels = max_chan + 1;
288 cl->tx_cfg.num_hw_channels = max_chan + 1;
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;
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);
304 kfree(cl->tx_cfg.channels);
306 kfree(cl->rx_cfg.channels);
309 pr_err("hsi client: missing or incorrect of property: err=%d\n", err);
312 void hsi_add_clients_from_dt(struct hsi_port *port, struct device_node *clients)
314 struct device_node *child;
316 /* register hsi-char device */
317 hsi_new_client(port, &hsi_char_dev_info);
319 for_each_available_child_of_node(clients, child)
320 hsi_add_client_from_dt(port, child);
322 EXPORT_SYMBOL_GPL(hsi_add_clients_from_dt);
325 int hsi_remove_client(struct device *dev, void *data __maybe_unused)
327 device_unregister(dev);
331 EXPORT_SYMBOL_GPL(hsi_remove_client);
333 static int hsi_remove_port(struct device *dev, void *data __maybe_unused)
335 device_for_each_child(dev, NULL, hsi_remove_client);
336 device_unregister(dev);
341 static void hsi_controller_release(struct device *dev)
343 struct hsi_controller *hsi = to_hsi_controller(dev);
349 static void hsi_port_release(struct device *dev)
351 kfree(to_hsi_port(dev));
355 * hsi_port_unregister_clients - Unregister an HSI port
356 * @port: The HSI port to unregister
358 void hsi_port_unregister_clients(struct hsi_port *port)
360 device_for_each_child(&port->device, NULL, hsi_remove_client);
362 EXPORT_SYMBOL_GPL(hsi_port_unregister_clients);
365 * hsi_unregister_controller - Unregister an HSI controller
366 * @hsi: The HSI controller to register
368 void hsi_unregister_controller(struct hsi_controller *hsi)
370 device_for_each_child(&hsi->device, NULL, hsi_remove_port);
371 device_unregister(&hsi->device);
373 EXPORT_SYMBOL_GPL(hsi_unregister_controller);
376 * hsi_register_controller - Register an HSI controller and its ports
377 * @hsi: The HSI controller to register
379 * Returns -errno on failure, 0 on success.
381 int hsi_register_controller(struct hsi_controller *hsi)
386 err = device_add(&hsi->device);
389 for (i = 0; i < hsi->num_ports; i++) {
390 hsi->port[i]->device.parent = &hsi->device;
391 err = device_add(&hsi->port[i]->device);
395 /* Populate HSI bus with HSI clients */
396 hsi_scan_board_info(hsi);
401 device_del(&hsi->port[i]->device);
402 device_del(&hsi->device);
406 EXPORT_SYMBOL_GPL(hsi_register_controller);
409 * hsi_register_client_driver - Register an HSI client to the HSI bus
410 * @drv: HSI client driver to register
412 * Returns -errno on failure, 0 on success.
414 int hsi_register_client_driver(struct hsi_client_driver *drv)
416 drv->driver.bus = &hsi_bus_type;
418 return driver_register(&drv->driver);
420 EXPORT_SYMBOL_GPL(hsi_register_client_driver);
422 static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused)
427 static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
433 * hsi_put_controller - Free an HSI controller
435 * @hsi: Pointer to the HSI controller to freed
437 * HSI controller drivers should only use this function if they need
438 * to free their allocated hsi_controller structures before a successful
439 * call to hsi_register_controller. Other use is not allowed.
441 void hsi_put_controller(struct hsi_controller *hsi)
448 for (i = 0; i < hsi->num_ports; i++)
449 if (hsi->port && hsi->port[i])
450 put_device(&hsi->port[i]->device);
451 put_device(&hsi->device);
453 EXPORT_SYMBOL_GPL(hsi_put_controller);
456 * hsi_alloc_controller - Allocate an HSI controller and its ports
457 * @n_ports: Number of ports on the HSI controller
458 * @flags: Kernel allocation flags
460 * Return NULL on failure or a pointer to an hsi_controller on success.
462 struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
464 struct hsi_controller *hsi;
465 struct hsi_port **port;
471 hsi = kzalloc(sizeof(*hsi), flags);
474 port = kcalloc(n_ports, sizeof(*port), flags);
479 hsi->num_ports = n_ports;
481 hsi->device.release = hsi_controller_release;
482 device_initialize(&hsi->device);
484 for (i = 0; i < n_ports; i++) {
485 port[i] = kzalloc(sizeof(**port), flags);
489 port[i]->async = hsi_dummy_msg;
490 port[i]->setup = hsi_dummy_cl;
491 port[i]->flush = hsi_dummy_cl;
492 port[i]->start_tx = hsi_dummy_cl;
493 port[i]->stop_tx = hsi_dummy_cl;
494 port[i]->release = hsi_dummy_cl;
495 mutex_init(&port[i]->lock);
496 BLOCKING_INIT_NOTIFIER_HEAD(&port[i]->n_head);
497 dev_set_name(&port[i]->device, "port%d", i);
498 hsi->port[i]->device.release = hsi_port_release;
499 device_initialize(&hsi->port[i]->device);
504 hsi_put_controller(hsi);
508 EXPORT_SYMBOL_GPL(hsi_alloc_controller);
511 * hsi_free_msg - Free an HSI message
512 * @msg: Pointer to the HSI message
514 * Client is responsible to free the buffers pointed by the scatterlists.
516 void hsi_free_msg(struct hsi_msg *msg)
520 sg_free_table(&msg->sgt);
523 EXPORT_SYMBOL_GPL(hsi_free_msg);
526 * hsi_alloc_msg - Allocate an HSI message
527 * @nents: Number of memory entries
528 * @flags: Kernel allocation flags
530 * nents can be 0. This mainly makes sense for read transfer.
531 * In that case, HSI drivers will call the complete callback when
532 * there is data to be read without consuming it.
534 * Return NULL on failure or a pointer to an hsi_msg on success.
536 struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags)
541 msg = kzalloc(sizeof(*msg), flags);
548 err = sg_alloc_table(&msg->sgt, nents, flags);
556 EXPORT_SYMBOL_GPL(hsi_alloc_msg);
559 * hsi_async - Submit an HSI transfer to the controller
560 * @cl: HSI client sending the transfer
561 * @msg: The HSI transfer passed to controller
563 * The HSI message must have the channel, ttype, complete and destructor
564 * fields set beforehand. If nents > 0 then the client has to initialize
565 * also the scatterlists to point to the buffers to write to or read from.
567 * HSI controllers relay on pre-allocated buffers from their clients and they
568 * do not allocate buffers on their own.
570 * Once the HSI message transfer finishes, the HSI controller calls the
571 * complete callback with the status and actual_len fields of the HSI message
572 * updated. The complete callback can be called before returning from
575 * Returns -errno on failure or 0 on success
577 int hsi_async(struct hsi_client *cl, struct hsi_msg *msg)
579 struct hsi_port *port = hsi_get_port(cl);
581 if (!hsi_port_claimed(cl))
584 WARN_ON_ONCE(!msg->destructor || !msg->complete);
587 return port->async(msg);
589 EXPORT_SYMBOL_GPL(hsi_async);
592 * hsi_claim_port - Claim the HSI client's port
593 * @cl: HSI client that wants to claim its port
594 * @share: Flag to indicate if the client wants to share the port or not.
596 * Returns -errno on failure, 0 on success.
598 int hsi_claim_port(struct hsi_client *cl, unsigned int share)
600 struct hsi_port *port = hsi_get_port(cl);
603 mutex_lock(&port->lock);
604 if ((port->claimed) && (!port->shared || !share)) {
608 if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) {
613 port->shared = !!share;
616 mutex_unlock(&port->lock);
620 EXPORT_SYMBOL_GPL(hsi_claim_port);
623 * hsi_release_port - Release the HSI client's port
624 * @cl: HSI client which previously claimed its port
626 void hsi_release_port(struct hsi_client *cl)
628 struct hsi_port *port = hsi_get_port(cl);
630 mutex_lock(&port->lock);
631 /* Allow HW driver to do some cleanup */
635 BUG_ON(port->claimed < 0);
639 module_put(to_hsi_controller(port->device.parent)->owner);
640 mutex_unlock(&port->lock);
642 EXPORT_SYMBOL_GPL(hsi_release_port);
644 static int hsi_event_notifier_call(struct notifier_block *nb,
645 unsigned long event, void *data __maybe_unused)
647 struct hsi_client *cl = container_of(nb, struct hsi_client, nb);
649 (*cl->ehandler)(cl, event);
655 * hsi_register_port_event - Register a client to receive port events
656 * @cl: HSI client that wants to receive port events
657 * @handler: Event handler callback
659 * Clients should register a callback to be able to receive
660 * events from the ports. Registration should happen after
662 * The handler can be called in interrupt context.
664 * Returns -errno on error, or 0 on success.
666 int hsi_register_port_event(struct hsi_client *cl,
667 void (*handler)(struct hsi_client *, unsigned long))
669 struct hsi_port *port = hsi_get_port(cl);
671 if (!handler || cl->ehandler)
673 if (!hsi_port_claimed(cl))
675 cl->ehandler = handler;
676 cl->nb.notifier_call = hsi_event_notifier_call;
678 return blocking_notifier_chain_register(&port->n_head, &cl->nb);
680 EXPORT_SYMBOL_GPL(hsi_register_port_event);
683 * hsi_unregister_port_event - Stop receiving port events for a client
684 * @cl: HSI client that wants to stop receiving port events
686 * Clients should call this function before releasing their associated
689 * Returns -errno on error, or 0 on success.
691 int hsi_unregister_port_event(struct hsi_client *cl)
693 struct hsi_port *port = hsi_get_port(cl);
696 WARN_ON(!hsi_port_claimed(cl));
698 err = blocking_notifier_chain_unregister(&port->n_head, &cl->nb);
704 EXPORT_SYMBOL_GPL(hsi_unregister_port_event);
707 * hsi_event - Notifies clients about port events
708 * @port: Port where the event occurred
709 * @event: The event type
711 * Clients should not be concerned about wake line behavior. However, due
712 * to a race condition in HSI HW protocol, clients need to be notified
713 * about wake line changes, so they can implement a workaround for it.
716 * HSI_EVENT_START_RX - Incoming wake line high
717 * HSI_EVENT_STOP_RX - Incoming wake line down
719 * Returns -errno on error, or 0 on success.
721 int hsi_event(struct hsi_port *port, unsigned long event)
723 return blocking_notifier_call_chain(&port->n_head, event, NULL);
725 EXPORT_SYMBOL_GPL(hsi_event);
728 * hsi_get_channel_id_by_name - acquire channel id by channel name
729 * @cl: HSI client, which uses the channel
730 * @name: name the channel is known under
732 * Clients can call this function to get the hsi channel ids similar to
733 * requesting IRQs or GPIOs by name. This function assumes the same
734 * channel configuration is used for RX and TX.
736 * Returns -errno on error or channel id on success.
738 int hsi_get_channel_id_by_name(struct hsi_client *cl, char *name)
742 if (!cl->rx_cfg.channels)
745 for (i = 0; i < cl->rx_cfg.num_channels; i++)
746 if (!strcmp(cl->rx_cfg.channels[i].name, name))
747 return cl->rx_cfg.channels[i].id;
751 EXPORT_SYMBOL_GPL(hsi_get_channel_id_by_name);
753 static int __init hsi_init(void)
755 return bus_register(&hsi_bus_type);
757 postcore_initcall(hsi_init);
759 static void __exit hsi_exit(void)
761 bus_unregister(&hsi_bus_type);
763 module_exit(hsi_exit);
765 MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
766 MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework");
767 MODULE_LICENSE("GPL v2");