Linux 4.1.16
[linux/fpc-iii.git] / drivers / hsi / hsi.c
blobfe9371271ce2420db8ca4d62ce2c6d183219ec2e
1 /*
2 * HSI core.
4 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
6 * Contact: Carlos Chinea <carlos.chinea@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
22 #include <linux/hsi/hsi.h>
23 #include <linux/compiler.h>
24 #include <linux/list.h>
25 #include <linux/kobject.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
28 #include <linux/notifier.h>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31 #include "hsi_core.h"
33 static ssize_t modalias_show(struct device *dev,
34 struct device_attribute *a __maybe_unused, char *buf)
36 return sprintf(buf, "hsi:%s\n", dev_name(dev));
38 static DEVICE_ATTR_RO(modalias);
40 static struct attribute *hsi_bus_dev_attrs[] = {
41 &dev_attr_modalias.attr,
42 NULL,
44 ATTRIBUTE_GROUPS(hsi_bus_dev);
46 static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
48 add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev));
50 return 0;
53 static int hsi_bus_match(struct device *dev, struct device_driver *driver)
55 if (of_driver_match_device(dev, driver))
56 return true;
58 if (strcmp(dev_name(dev), driver->name) == 0)
59 return true;
61 return false;
64 static struct bus_type hsi_bus_type = {
65 .name = "hsi",
66 .dev_groups = hsi_bus_dev_groups,
67 .match = hsi_bus_match,
68 .uevent = hsi_bus_uevent,
71 static void hsi_client_release(struct device *dev)
73 struct hsi_client *cl = to_hsi_client(dev);
75 kfree(cl->tx_cfg.channels);
76 kfree(cl->rx_cfg.channels);
77 kfree(cl);
80 struct hsi_client *hsi_new_client(struct hsi_port *port,
81 struct hsi_board_info *info)
83 struct hsi_client *cl;
84 size_t size;
86 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
87 if (!cl)
88 return NULL;
90 cl->tx_cfg = info->tx_cfg;
91 if (cl->tx_cfg.channels) {
92 size = cl->tx_cfg.num_channels * sizeof(*cl->tx_cfg.channels);
93 cl->tx_cfg.channels = kzalloc(size , GFP_KERNEL);
94 memcpy(cl->tx_cfg.channels, info->tx_cfg.channels, size);
97 cl->rx_cfg = info->rx_cfg;
98 if (cl->rx_cfg.channels) {
99 size = cl->rx_cfg.num_channels * sizeof(*cl->rx_cfg.channels);
100 cl->rx_cfg.channels = kzalloc(size , GFP_KERNEL);
101 memcpy(cl->rx_cfg.channels, info->rx_cfg.channels, size);
104 cl->device.bus = &hsi_bus_type;
105 cl->device.parent = &port->device;
106 cl->device.release = hsi_client_release;
107 dev_set_name(&cl->device, "%s", info->name);
108 cl->device.platform_data = info->platform_data;
109 if (info->archdata)
110 cl->device.archdata = *info->archdata;
111 if (device_register(&cl->device) < 0) {
112 pr_err("hsi: failed to register client: %s\n", info->name);
113 put_device(&cl->device);
116 return cl;
118 EXPORT_SYMBOL_GPL(hsi_new_client);
120 static void hsi_scan_board_info(struct hsi_controller *hsi)
122 struct hsi_cl_info *cl_info;
123 struct hsi_port *p;
125 list_for_each_entry(cl_info, &hsi_board_list, list)
126 if (cl_info->info.hsi_id == hsi->id) {
127 p = hsi_find_port_num(hsi, cl_info->info.port);
128 if (!p)
129 continue;
130 hsi_new_client(p, &cl_info->info);
134 #ifdef CONFIG_OF
135 static struct hsi_board_info hsi_char_dev_info = {
136 .name = "hsi_char",
139 static int hsi_of_property_parse_mode(struct device_node *client, char *name,
140 unsigned int *result)
142 const char *mode;
143 int err;
145 err = of_property_read_string(client, name, &mode);
146 if (err < 0)
147 return err;
149 if (strcmp(mode, "stream") == 0)
150 *result = HSI_MODE_STREAM;
151 else if (strcmp(mode, "frame") == 0)
152 *result = HSI_MODE_FRAME;
153 else
154 return -EINVAL;
156 return 0;
159 static int hsi_of_property_parse_flow(struct device_node *client, char *name,
160 unsigned int *result)
162 const char *flow;
163 int err;
165 err = of_property_read_string(client, name, &flow);
166 if (err < 0)
167 return err;
169 if (strcmp(flow, "synchronized") == 0)
170 *result = HSI_FLOW_SYNC;
171 else if (strcmp(flow, "pipeline") == 0)
172 *result = HSI_FLOW_PIPE;
173 else
174 return -EINVAL;
176 return 0;
179 static int hsi_of_property_parse_arb_mode(struct device_node *client,
180 char *name, unsigned int *result)
182 const char *arb_mode;
183 int err;
185 err = of_property_read_string(client, name, &arb_mode);
186 if (err < 0)
187 return err;
189 if (strcmp(arb_mode, "round-robin") == 0)
190 *result = HSI_ARB_RR;
191 else if (strcmp(arb_mode, "priority") == 0)
192 *result = HSI_ARB_PRIO;
193 else
194 return -EINVAL;
196 return 0;
199 static void hsi_add_client_from_dt(struct hsi_port *port,
200 struct device_node *client)
202 struct hsi_client *cl;
203 struct hsi_channel channel;
204 struct property *prop;
205 char name[32];
206 int length, cells, err, i, max_chan, mode;
208 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
209 if (!cl)
210 return;
212 err = of_modalias_node(client, name, sizeof(name));
213 if (err)
214 goto err;
216 dev_set_name(&cl->device, "%s", name);
218 err = hsi_of_property_parse_mode(client, "hsi-mode", &mode);
219 if (err) {
220 err = hsi_of_property_parse_mode(client, "hsi-rx-mode",
221 &cl->rx_cfg.mode);
222 if (err)
223 goto err;
225 err = hsi_of_property_parse_mode(client, "hsi-tx-mode",
226 &cl->tx_cfg.mode);
227 if (err)
228 goto err;
229 } else {
230 cl->rx_cfg.mode = mode;
231 cl->tx_cfg.mode = mode;
234 err = of_property_read_u32(client, "hsi-speed-kbps",
235 &cl->tx_cfg.speed);
236 if (err)
237 goto err;
238 cl->rx_cfg.speed = cl->tx_cfg.speed;
240 err = hsi_of_property_parse_flow(client, "hsi-flow",
241 &cl->rx_cfg.flow);
242 if (err)
243 goto err;
245 err = hsi_of_property_parse_arb_mode(client, "hsi-arb-mode",
246 &cl->rx_cfg.arb_mode);
247 if (err)
248 goto err;
250 prop = of_find_property(client, "hsi-channel-ids", &length);
251 if (!prop) {
252 err = -EINVAL;
253 goto err;
256 cells = length / sizeof(u32);
258 cl->rx_cfg.num_channels = cells;
259 cl->tx_cfg.num_channels = cells;
261 cl->rx_cfg.channels = kzalloc(cells * sizeof(channel), GFP_KERNEL);
262 if (!cl->rx_cfg.channels) {
263 err = -ENOMEM;
264 goto err;
267 cl->tx_cfg.channels = kzalloc(cells * sizeof(channel), GFP_KERNEL);
268 if (!cl->tx_cfg.channels) {
269 err = -ENOMEM;
270 goto err2;
273 max_chan = 0;
274 for (i = 0; i < cells; i++) {
275 err = of_property_read_u32_index(client, "hsi-channel-ids", i,
276 &channel.id);
277 if (err)
278 goto err3;
280 err = of_property_read_string_index(client, "hsi-channel-names",
281 i, &channel.name);
282 if (err)
283 channel.name = NULL;
285 if (channel.id > max_chan)
286 max_chan = channel.id;
288 cl->rx_cfg.channels[i] = channel;
289 cl->tx_cfg.channels[i] = channel;
292 cl->rx_cfg.num_hw_channels = max_chan + 1;
293 cl->tx_cfg.num_hw_channels = max_chan + 1;
295 cl->device.bus = &hsi_bus_type;
296 cl->device.parent = &port->device;
297 cl->device.release = hsi_client_release;
298 cl->device.of_node = client;
300 if (device_register(&cl->device) < 0) {
301 pr_err("hsi: failed to register client: %s\n", name);
302 put_device(&cl->device);
303 goto err3;
306 return;
308 err3:
309 kfree(cl->tx_cfg.channels);
310 err2:
311 kfree(cl->rx_cfg.channels);
312 err:
313 kfree(cl);
314 pr_err("hsi client: missing or incorrect of property: err=%d\n", err);
317 void hsi_add_clients_from_dt(struct hsi_port *port, struct device_node *clients)
319 struct device_node *child;
321 /* register hsi-char device */
322 hsi_new_client(port, &hsi_char_dev_info);
324 for_each_available_child_of_node(clients, child)
325 hsi_add_client_from_dt(port, child);
327 EXPORT_SYMBOL_GPL(hsi_add_clients_from_dt);
328 #endif
330 int hsi_remove_client(struct device *dev, void *data __maybe_unused)
332 device_unregister(dev);
334 return 0;
336 EXPORT_SYMBOL_GPL(hsi_remove_client);
338 static int hsi_remove_port(struct device *dev, void *data __maybe_unused)
340 device_for_each_child(dev, NULL, hsi_remove_client);
341 device_unregister(dev);
343 return 0;
346 static void hsi_controller_release(struct device *dev)
348 struct hsi_controller *hsi = to_hsi_controller(dev);
350 kfree(hsi->port);
351 kfree(hsi);
354 static void hsi_port_release(struct device *dev)
356 kfree(to_hsi_port(dev));
360 * hsi_unregister_port - Unregister an HSI port
361 * @port: The HSI port to unregister
363 void hsi_port_unregister_clients(struct hsi_port *port)
365 device_for_each_child(&port->device, NULL, hsi_remove_client);
367 EXPORT_SYMBOL_GPL(hsi_port_unregister_clients);
370 * hsi_unregister_controller - Unregister an HSI controller
371 * @hsi: The HSI controller to register
373 void hsi_unregister_controller(struct hsi_controller *hsi)
375 device_for_each_child(&hsi->device, NULL, hsi_remove_port);
376 device_unregister(&hsi->device);
378 EXPORT_SYMBOL_GPL(hsi_unregister_controller);
381 * hsi_register_controller - Register an HSI controller and its ports
382 * @hsi: The HSI controller to register
384 * Returns -errno on failure, 0 on success.
386 int hsi_register_controller(struct hsi_controller *hsi)
388 unsigned int i;
389 int err;
391 err = device_add(&hsi->device);
392 if (err < 0)
393 return err;
394 for (i = 0; i < hsi->num_ports; i++) {
395 hsi->port[i]->device.parent = &hsi->device;
396 err = device_add(&hsi->port[i]->device);
397 if (err < 0)
398 goto out;
400 /* Populate HSI bus with HSI clients */
401 hsi_scan_board_info(hsi);
403 return 0;
404 out:
405 while (i-- > 0)
406 device_del(&hsi->port[i]->device);
407 device_del(&hsi->device);
409 return err;
411 EXPORT_SYMBOL_GPL(hsi_register_controller);
414 * hsi_register_client_driver - Register an HSI client to the HSI bus
415 * @drv: HSI client driver to register
417 * Returns -errno on failure, 0 on success.
419 int hsi_register_client_driver(struct hsi_client_driver *drv)
421 drv->driver.bus = &hsi_bus_type;
423 return driver_register(&drv->driver);
425 EXPORT_SYMBOL_GPL(hsi_register_client_driver);
427 static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused)
429 return 0;
432 static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
434 return 0;
438 * hsi_put_controller - Free an HSI controller
440 * @hsi: Pointer to the HSI controller to freed
442 * HSI controller drivers should only use this function if they need
443 * to free their allocated hsi_controller structures before a successful
444 * call to hsi_register_controller. Other use is not allowed.
446 void hsi_put_controller(struct hsi_controller *hsi)
448 unsigned int i;
450 if (!hsi)
451 return;
453 for (i = 0; i < hsi->num_ports; i++)
454 if (hsi->port && hsi->port[i])
455 put_device(&hsi->port[i]->device);
456 put_device(&hsi->device);
458 EXPORT_SYMBOL_GPL(hsi_put_controller);
461 * hsi_alloc_controller - Allocate an HSI controller and its ports
462 * @n_ports: Number of ports on the HSI controller
463 * @flags: Kernel allocation flags
465 * Return NULL on failure or a pointer to an hsi_controller on success.
467 struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
469 struct hsi_controller *hsi;
470 struct hsi_port **port;
471 unsigned int i;
473 if (!n_ports)
474 return NULL;
476 hsi = kzalloc(sizeof(*hsi), flags);
477 if (!hsi)
478 return NULL;
479 port = kzalloc(sizeof(*port)*n_ports, flags);
480 if (!port) {
481 kfree(hsi);
482 return NULL;
484 hsi->num_ports = n_ports;
485 hsi->port = port;
486 hsi->device.release = hsi_controller_release;
487 device_initialize(&hsi->device);
489 for (i = 0; i < n_ports; i++) {
490 port[i] = kzalloc(sizeof(**port), flags);
491 if (port[i] == NULL)
492 goto out;
493 port[i]->num = i;
494 port[i]->async = hsi_dummy_msg;
495 port[i]->setup = hsi_dummy_cl;
496 port[i]->flush = hsi_dummy_cl;
497 port[i]->start_tx = hsi_dummy_cl;
498 port[i]->stop_tx = hsi_dummy_cl;
499 port[i]->release = hsi_dummy_cl;
500 mutex_init(&port[i]->lock);
501 ATOMIC_INIT_NOTIFIER_HEAD(&port[i]->n_head);
502 dev_set_name(&port[i]->device, "port%d", i);
503 hsi->port[i]->device.release = hsi_port_release;
504 device_initialize(&hsi->port[i]->device);
507 return hsi;
508 out:
509 hsi_put_controller(hsi);
511 return NULL;
513 EXPORT_SYMBOL_GPL(hsi_alloc_controller);
516 * hsi_free_msg - Free an HSI message
517 * @msg: Pointer to the HSI message
519 * Client is responsible to free the buffers pointed by the scatterlists.
521 void hsi_free_msg(struct hsi_msg *msg)
523 if (!msg)
524 return;
525 sg_free_table(&msg->sgt);
526 kfree(msg);
528 EXPORT_SYMBOL_GPL(hsi_free_msg);
531 * hsi_alloc_msg - Allocate an HSI message
532 * @nents: Number of memory entries
533 * @flags: Kernel allocation flags
535 * nents can be 0. This mainly makes sense for read transfer.
536 * In that case, HSI drivers will call the complete callback when
537 * there is data to be read without consuming it.
539 * Return NULL on failure or a pointer to an hsi_msg on success.
541 struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags)
543 struct hsi_msg *msg;
544 int err;
546 msg = kzalloc(sizeof(*msg), flags);
547 if (!msg)
548 return NULL;
550 if (!nents)
551 return msg;
553 err = sg_alloc_table(&msg->sgt, nents, flags);
554 if (unlikely(err)) {
555 kfree(msg);
556 msg = NULL;
559 return msg;
561 EXPORT_SYMBOL_GPL(hsi_alloc_msg);
564 * hsi_async - Submit an HSI transfer to the controller
565 * @cl: HSI client sending the transfer
566 * @msg: The HSI transfer passed to controller
568 * The HSI message must have the channel, ttype, complete and destructor
569 * fields set beforehand. If nents > 0 then the client has to initialize
570 * also the scatterlists to point to the buffers to write to or read from.
572 * HSI controllers relay on pre-allocated buffers from their clients and they
573 * do not allocate buffers on their own.
575 * Once the HSI message transfer finishes, the HSI controller calls the
576 * complete callback with the status and actual_len fields of the HSI message
577 * updated. The complete callback can be called before returning from
578 * hsi_async.
580 * Returns -errno on failure or 0 on success
582 int hsi_async(struct hsi_client *cl, struct hsi_msg *msg)
584 struct hsi_port *port = hsi_get_port(cl);
586 if (!hsi_port_claimed(cl))
587 return -EACCES;
589 WARN_ON_ONCE(!msg->destructor || !msg->complete);
590 msg->cl = cl;
592 return port->async(msg);
594 EXPORT_SYMBOL_GPL(hsi_async);
597 * hsi_claim_port - Claim the HSI client's port
598 * @cl: HSI client that wants to claim its port
599 * @share: Flag to indicate if the client wants to share the port or not.
601 * Returns -errno on failure, 0 on success.
603 int hsi_claim_port(struct hsi_client *cl, unsigned int share)
605 struct hsi_port *port = hsi_get_port(cl);
606 int err = 0;
608 mutex_lock(&port->lock);
609 if ((port->claimed) && (!port->shared || !share)) {
610 err = -EBUSY;
611 goto out;
613 if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) {
614 err = -ENODEV;
615 goto out;
617 port->claimed++;
618 port->shared = !!share;
619 cl->pclaimed = 1;
620 out:
621 mutex_unlock(&port->lock);
623 return err;
625 EXPORT_SYMBOL_GPL(hsi_claim_port);
628 * hsi_release_port - Release the HSI client's port
629 * @cl: HSI client which previously claimed its port
631 void hsi_release_port(struct hsi_client *cl)
633 struct hsi_port *port = hsi_get_port(cl);
635 mutex_lock(&port->lock);
636 /* Allow HW driver to do some cleanup */
637 port->release(cl);
638 if (cl->pclaimed)
639 port->claimed--;
640 BUG_ON(port->claimed < 0);
641 cl->pclaimed = 0;
642 if (!port->claimed)
643 port->shared = 0;
644 module_put(to_hsi_controller(port->device.parent)->owner);
645 mutex_unlock(&port->lock);
647 EXPORT_SYMBOL_GPL(hsi_release_port);
649 static int hsi_event_notifier_call(struct notifier_block *nb,
650 unsigned long event, void *data __maybe_unused)
652 struct hsi_client *cl = container_of(nb, struct hsi_client, nb);
654 (*cl->ehandler)(cl, event);
656 return 0;
660 * hsi_register_port_event - Register a client to receive port events
661 * @cl: HSI client that wants to receive port events
662 * @handler: Event handler callback
664 * Clients should register a callback to be able to receive
665 * events from the ports. Registration should happen after
666 * claiming the port.
667 * The handler can be called in interrupt context.
669 * Returns -errno on error, or 0 on success.
671 int hsi_register_port_event(struct hsi_client *cl,
672 void (*handler)(struct hsi_client *, unsigned long))
674 struct hsi_port *port = hsi_get_port(cl);
676 if (!handler || cl->ehandler)
677 return -EINVAL;
678 if (!hsi_port_claimed(cl))
679 return -EACCES;
680 cl->ehandler = handler;
681 cl->nb.notifier_call = hsi_event_notifier_call;
683 return atomic_notifier_chain_register(&port->n_head, &cl->nb);
685 EXPORT_SYMBOL_GPL(hsi_register_port_event);
688 * hsi_unregister_port_event - Stop receiving port events for a client
689 * @cl: HSI client that wants to stop receiving port events
691 * Clients should call this function before releasing their associated
692 * port.
694 * Returns -errno on error, or 0 on success.
696 int hsi_unregister_port_event(struct hsi_client *cl)
698 struct hsi_port *port = hsi_get_port(cl);
699 int err;
701 WARN_ON(!hsi_port_claimed(cl));
703 err = atomic_notifier_chain_unregister(&port->n_head, &cl->nb);
704 if (!err)
705 cl->ehandler = NULL;
707 return err;
709 EXPORT_SYMBOL_GPL(hsi_unregister_port_event);
712 * hsi_event - Notifies clients about port events
713 * @port: Port where the event occurred
714 * @event: The event type
716 * Clients should not be concerned about wake line behavior. However, due
717 * to a race condition in HSI HW protocol, clients need to be notified
718 * about wake line changes, so they can implement a workaround for it.
720 * Events:
721 * HSI_EVENT_START_RX - Incoming wake line high
722 * HSI_EVENT_STOP_RX - Incoming wake line down
724 * Returns -errno on error, or 0 on success.
726 int hsi_event(struct hsi_port *port, unsigned long event)
728 return atomic_notifier_call_chain(&port->n_head, event, NULL);
730 EXPORT_SYMBOL_GPL(hsi_event);
733 * hsi_get_channel_id_by_name - acquire channel id by channel name
734 * @cl: HSI client, which uses the channel
735 * @name: name the channel is known under
737 * Clients can call this function to get the hsi channel ids similar to
738 * requesting IRQs or GPIOs by name. This function assumes the same
739 * channel configuration is used for RX and TX.
741 * Returns -errno on error or channel id on success.
743 int hsi_get_channel_id_by_name(struct hsi_client *cl, char *name)
745 int i;
747 if (!cl->rx_cfg.channels)
748 return -ENOENT;
750 for (i = 0; i < cl->rx_cfg.num_channels; i++)
751 if (!strcmp(cl->rx_cfg.channels[i].name, name))
752 return cl->rx_cfg.channels[i].id;
754 return -ENXIO;
756 EXPORT_SYMBOL_GPL(hsi_get_channel_id_by_name);
758 static int __init hsi_init(void)
760 return bus_register(&hsi_bus_type);
762 postcore_initcall(hsi_init);
764 static void __exit hsi_exit(void)
766 bus_unregister(&hsi_bus_type);
768 module_exit(hsi_exit);
770 MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
771 MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework");
772 MODULE_LICENSE("GPL v2");