0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/hsi/hsi.h>
0010 #include <linux/compiler.h>
0011 #include <linux/list.h>
0012 #include <linux/kobject.h>
0013 #include <linux/slab.h>
0014 #include <linux/string.h>
0015 #include <linux/notifier.h>
0016 #include <linux/of.h>
0017 #include <linux/of_device.h>
0018 #include "hsi_core.h"
0019
0020 static ssize_t modalias_show(struct device *dev,
0021 struct device_attribute *a __maybe_unused, char *buf)
0022 {
0023 return sprintf(buf, "hsi:%s\n", dev_name(dev));
0024 }
0025 static DEVICE_ATTR_RO(modalias);
0026
0027 static struct attribute *hsi_bus_dev_attrs[] = {
0028 &dev_attr_modalias.attr,
0029 NULL,
0030 };
0031 ATTRIBUTE_GROUPS(hsi_bus_dev);
0032
0033 static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
0034 {
0035 add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev));
0036
0037 return 0;
0038 }
0039
0040 static int hsi_bus_match(struct device *dev, struct device_driver *driver)
0041 {
0042 if (of_driver_match_device(dev, driver))
0043 return true;
0044
0045 if (strcmp(dev_name(dev), driver->name) == 0)
0046 return true;
0047
0048 return false;
0049 }
0050
0051 static struct bus_type hsi_bus_type = {
0052 .name = "hsi",
0053 .dev_groups = hsi_bus_dev_groups,
0054 .match = hsi_bus_match,
0055 .uevent = hsi_bus_uevent,
0056 };
0057
0058 static void hsi_client_release(struct device *dev)
0059 {
0060 struct hsi_client *cl = to_hsi_client(dev);
0061
0062 kfree(cl->tx_cfg.channels);
0063 kfree(cl->rx_cfg.channels);
0064 kfree(cl);
0065 }
0066
0067 struct hsi_client *hsi_new_client(struct hsi_port *port,
0068 struct hsi_board_info *info)
0069 {
0070 struct hsi_client *cl;
0071 size_t size;
0072
0073 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
0074 if (!cl)
0075 goto err;
0076
0077 cl->tx_cfg = info->tx_cfg;
0078 if (cl->tx_cfg.channels) {
0079 size = cl->tx_cfg.num_channels * sizeof(*cl->tx_cfg.channels);
0080 cl->tx_cfg.channels = kmemdup(info->tx_cfg.channels, size,
0081 GFP_KERNEL);
0082 if (!cl->tx_cfg.channels)
0083 goto err_tx;
0084 }
0085
0086 cl->rx_cfg = info->rx_cfg;
0087 if (cl->rx_cfg.channels) {
0088 size = cl->rx_cfg.num_channels * sizeof(*cl->rx_cfg.channels);
0089 cl->rx_cfg.channels = kmemdup(info->rx_cfg.channels, size,
0090 GFP_KERNEL);
0091 if (!cl->rx_cfg.channels)
0092 goto err_rx;
0093 }
0094
0095 cl->device.bus = &hsi_bus_type;
0096 cl->device.parent = &port->device;
0097 cl->device.release = hsi_client_release;
0098 dev_set_name(&cl->device, "%s", info->name);
0099 cl->device.platform_data = info->platform_data;
0100 if (info->archdata)
0101 cl->device.archdata = *info->archdata;
0102 if (device_register(&cl->device) < 0) {
0103 pr_err("hsi: failed to register client: %s\n", info->name);
0104 put_device(&cl->device);
0105 goto err;
0106 }
0107
0108 return cl;
0109 err_rx:
0110 kfree(cl->tx_cfg.channels);
0111 err_tx:
0112 kfree(cl);
0113 err:
0114 return NULL;
0115 }
0116 EXPORT_SYMBOL_GPL(hsi_new_client);
0117
0118 static void hsi_scan_board_info(struct hsi_controller *hsi)
0119 {
0120 struct hsi_cl_info *cl_info;
0121 struct hsi_port *p;
0122
0123 list_for_each_entry(cl_info, &hsi_board_list, list)
0124 if (cl_info->info.hsi_id == hsi->id) {
0125 p = hsi_find_port_num(hsi, cl_info->info.port);
0126 if (!p)
0127 continue;
0128 hsi_new_client(p, &cl_info->info);
0129 }
0130 }
0131
0132 #ifdef CONFIG_OF
0133 static struct hsi_board_info hsi_char_dev_info = {
0134 .name = "hsi_char",
0135 };
0136
0137 static int hsi_of_property_parse_mode(struct device_node *client, char *name,
0138 unsigned int *result)
0139 {
0140 const char *mode;
0141 int err;
0142
0143 err = of_property_read_string(client, name, &mode);
0144 if (err < 0)
0145 return err;
0146
0147 if (strcmp(mode, "stream") == 0)
0148 *result = HSI_MODE_STREAM;
0149 else if (strcmp(mode, "frame") == 0)
0150 *result = HSI_MODE_FRAME;
0151 else
0152 return -EINVAL;
0153
0154 return 0;
0155 }
0156
0157 static int hsi_of_property_parse_flow(struct device_node *client, char *name,
0158 unsigned int *result)
0159 {
0160 const char *flow;
0161 int err;
0162
0163 err = of_property_read_string(client, name, &flow);
0164 if (err < 0)
0165 return err;
0166
0167 if (strcmp(flow, "synchronized") == 0)
0168 *result = HSI_FLOW_SYNC;
0169 else if (strcmp(flow, "pipeline") == 0)
0170 *result = HSI_FLOW_PIPE;
0171 else
0172 return -EINVAL;
0173
0174 return 0;
0175 }
0176
0177 static int hsi_of_property_parse_arb_mode(struct device_node *client,
0178 char *name, unsigned int *result)
0179 {
0180 const char *arb_mode;
0181 int err;
0182
0183 err = of_property_read_string(client, name, &arb_mode);
0184 if (err < 0)
0185 return err;
0186
0187 if (strcmp(arb_mode, "round-robin") == 0)
0188 *result = HSI_ARB_RR;
0189 else if (strcmp(arb_mode, "priority") == 0)
0190 *result = HSI_ARB_PRIO;
0191 else
0192 return -EINVAL;
0193
0194 return 0;
0195 }
0196
0197 static void hsi_add_client_from_dt(struct hsi_port *port,
0198 struct device_node *client)
0199 {
0200 struct hsi_client *cl;
0201 struct hsi_channel channel;
0202 struct property *prop;
0203 char name[32];
0204 int length, cells, err, i, max_chan, mode;
0205
0206 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
0207 if (!cl)
0208 return;
0209
0210 err = of_modalias_node(client, name, sizeof(name));
0211 if (err)
0212 goto err;
0213
0214 err = hsi_of_property_parse_mode(client, "hsi-mode", &mode);
0215 if (err) {
0216 err = hsi_of_property_parse_mode(client, "hsi-rx-mode",
0217 &cl->rx_cfg.mode);
0218 if (err)
0219 goto err;
0220
0221 err = hsi_of_property_parse_mode(client, "hsi-tx-mode",
0222 &cl->tx_cfg.mode);
0223 if (err)
0224 goto err;
0225 } else {
0226 cl->rx_cfg.mode = mode;
0227 cl->tx_cfg.mode = mode;
0228 }
0229
0230 err = of_property_read_u32(client, "hsi-speed-kbps",
0231 &cl->tx_cfg.speed);
0232 if (err)
0233 goto err;
0234 cl->rx_cfg.speed = cl->tx_cfg.speed;
0235
0236 err = hsi_of_property_parse_flow(client, "hsi-flow",
0237 &cl->rx_cfg.flow);
0238 if (err)
0239 goto err;
0240
0241 err = hsi_of_property_parse_arb_mode(client, "hsi-arb-mode",
0242 &cl->rx_cfg.arb_mode);
0243 if (err)
0244 goto err;
0245
0246 prop = of_find_property(client, "hsi-channel-ids", &length);
0247 if (!prop) {
0248 err = -EINVAL;
0249 goto err;
0250 }
0251
0252 cells = length / sizeof(u32);
0253
0254 cl->rx_cfg.num_channels = cells;
0255 cl->tx_cfg.num_channels = cells;
0256 cl->rx_cfg.channels = kcalloc(cells, sizeof(channel), GFP_KERNEL);
0257 if (!cl->rx_cfg.channels) {
0258 err = -ENOMEM;
0259 goto err;
0260 }
0261
0262 cl->tx_cfg.channels = kcalloc(cells, sizeof(channel), GFP_KERNEL);
0263 if (!cl->tx_cfg.channels) {
0264 err = -ENOMEM;
0265 goto err2;
0266 }
0267
0268 max_chan = 0;
0269 for (i = 0; i < cells; i++) {
0270 err = of_property_read_u32_index(client, "hsi-channel-ids", i,
0271 &channel.id);
0272 if (err)
0273 goto err3;
0274
0275 err = of_property_read_string_index(client, "hsi-channel-names",
0276 i, &channel.name);
0277 if (err)
0278 channel.name = NULL;
0279
0280 if (channel.id > max_chan)
0281 max_chan = channel.id;
0282
0283 cl->rx_cfg.channels[i] = channel;
0284 cl->tx_cfg.channels[i] = channel;
0285 }
0286
0287 cl->rx_cfg.num_hw_channels = max_chan + 1;
0288 cl->tx_cfg.num_hw_channels = max_chan + 1;
0289
0290 cl->device.bus = &hsi_bus_type;
0291 cl->device.parent = &port->device;
0292 cl->device.release = hsi_client_release;
0293 cl->device.of_node = client;
0294
0295 dev_set_name(&cl->device, "%s", name);
0296 if (device_register(&cl->device) < 0) {
0297 pr_err("hsi: failed to register client: %s\n", name);
0298 put_device(&cl->device);
0299 }
0300
0301 return;
0302
0303 err3:
0304 kfree(cl->tx_cfg.channels);
0305 err2:
0306 kfree(cl->rx_cfg.channels);
0307 err:
0308 kfree(cl);
0309 pr_err("hsi client: missing or incorrect of property: err=%d\n", err);
0310 }
0311
0312 void hsi_add_clients_from_dt(struct hsi_port *port, struct device_node *clients)
0313 {
0314 struct device_node *child;
0315
0316
0317 hsi_new_client(port, &hsi_char_dev_info);
0318
0319 for_each_available_child_of_node(clients, child)
0320 hsi_add_client_from_dt(port, child);
0321 }
0322 EXPORT_SYMBOL_GPL(hsi_add_clients_from_dt);
0323 #endif
0324
0325 int hsi_remove_client(struct device *dev, void *data __maybe_unused)
0326 {
0327 device_unregister(dev);
0328
0329 return 0;
0330 }
0331 EXPORT_SYMBOL_GPL(hsi_remove_client);
0332
0333 static int hsi_remove_port(struct device *dev, void *data __maybe_unused)
0334 {
0335 device_for_each_child(dev, NULL, hsi_remove_client);
0336 device_unregister(dev);
0337
0338 return 0;
0339 }
0340
0341 static void hsi_controller_release(struct device *dev)
0342 {
0343 struct hsi_controller *hsi = to_hsi_controller(dev);
0344
0345 kfree(hsi->port);
0346 kfree(hsi);
0347 }
0348
0349 static void hsi_port_release(struct device *dev)
0350 {
0351 kfree(to_hsi_port(dev));
0352 }
0353
0354
0355
0356
0357
0358 void hsi_port_unregister_clients(struct hsi_port *port)
0359 {
0360 device_for_each_child(&port->device, NULL, hsi_remove_client);
0361 }
0362 EXPORT_SYMBOL_GPL(hsi_port_unregister_clients);
0363
0364
0365
0366
0367
0368 void hsi_unregister_controller(struct hsi_controller *hsi)
0369 {
0370 device_for_each_child(&hsi->device, NULL, hsi_remove_port);
0371 device_unregister(&hsi->device);
0372 }
0373 EXPORT_SYMBOL_GPL(hsi_unregister_controller);
0374
0375
0376
0377
0378
0379
0380
0381 int hsi_register_controller(struct hsi_controller *hsi)
0382 {
0383 unsigned int i;
0384 int err;
0385
0386 err = device_add(&hsi->device);
0387 if (err < 0)
0388 return err;
0389 for (i = 0; i < hsi->num_ports; i++) {
0390 hsi->port[i]->device.parent = &hsi->device;
0391 err = device_add(&hsi->port[i]->device);
0392 if (err < 0)
0393 goto out;
0394 }
0395
0396 hsi_scan_board_info(hsi);
0397
0398 return 0;
0399 out:
0400 while (i-- > 0)
0401 device_del(&hsi->port[i]->device);
0402 device_del(&hsi->device);
0403
0404 return err;
0405 }
0406 EXPORT_SYMBOL_GPL(hsi_register_controller);
0407
0408
0409
0410
0411
0412
0413
0414 int hsi_register_client_driver(struct hsi_client_driver *drv)
0415 {
0416 drv->driver.bus = &hsi_bus_type;
0417
0418 return driver_register(&drv->driver);
0419 }
0420 EXPORT_SYMBOL_GPL(hsi_register_client_driver);
0421
0422 static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused)
0423 {
0424 return 0;
0425 }
0426
0427 static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
0428 {
0429 return 0;
0430 }
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441 void hsi_put_controller(struct hsi_controller *hsi)
0442 {
0443 unsigned int i;
0444
0445 if (!hsi)
0446 return;
0447
0448 for (i = 0; i < hsi->num_ports; i++)
0449 if (hsi->port && hsi->port[i])
0450 put_device(&hsi->port[i]->device);
0451 put_device(&hsi->device);
0452 }
0453 EXPORT_SYMBOL_GPL(hsi_put_controller);
0454
0455
0456
0457
0458
0459
0460
0461
0462 struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
0463 {
0464 struct hsi_controller *hsi;
0465 struct hsi_port **port;
0466 unsigned int i;
0467
0468 if (!n_ports)
0469 return NULL;
0470
0471 hsi = kzalloc(sizeof(*hsi), flags);
0472 if (!hsi)
0473 return NULL;
0474 port = kcalloc(n_ports, sizeof(*port), flags);
0475 if (!port) {
0476 kfree(hsi);
0477 return NULL;
0478 }
0479 hsi->num_ports = n_ports;
0480 hsi->port = port;
0481 hsi->device.release = hsi_controller_release;
0482 device_initialize(&hsi->device);
0483
0484 for (i = 0; i < n_ports; i++) {
0485 port[i] = kzalloc(sizeof(**port), flags);
0486 if (port[i] == NULL)
0487 goto out;
0488 port[i]->num = i;
0489 port[i]->async = hsi_dummy_msg;
0490 port[i]->setup = hsi_dummy_cl;
0491 port[i]->flush = hsi_dummy_cl;
0492 port[i]->start_tx = hsi_dummy_cl;
0493 port[i]->stop_tx = hsi_dummy_cl;
0494 port[i]->release = hsi_dummy_cl;
0495 mutex_init(&port[i]->lock);
0496 BLOCKING_INIT_NOTIFIER_HEAD(&port[i]->n_head);
0497 dev_set_name(&port[i]->device, "port%d", i);
0498 hsi->port[i]->device.release = hsi_port_release;
0499 device_initialize(&hsi->port[i]->device);
0500 }
0501
0502 return hsi;
0503 out:
0504 hsi_put_controller(hsi);
0505
0506 return NULL;
0507 }
0508 EXPORT_SYMBOL_GPL(hsi_alloc_controller);
0509
0510
0511
0512
0513
0514
0515
0516 void hsi_free_msg(struct hsi_msg *msg)
0517 {
0518 if (!msg)
0519 return;
0520 sg_free_table(&msg->sgt);
0521 kfree(msg);
0522 }
0523 EXPORT_SYMBOL_GPL(hsi_free_msg);
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536 struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags)
0537 {
0538 struct hsi_msg *msg;
0539 int err;
0540
0541 msg = kzalloc(sizeof(*msg), flags);
0542 if (!msg)
0543 return NULL;
0544
0545 if (!nents)
0546 return msg;
0547
0548 err = sg_alloc_table(&msg->sgt, nents, flags);
0549 if (unlikely(err)) {
0550 kfree(msg);
0551 msg = NULL;
0552 }
0553
0554 return msg;
0555 }
0556 EXPORT_SYMBOL_GPL(hsi_alloc_msg);
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577 int hsi_async(struct hsi_client *cl, struct hsi_msg *msg)
0578 {
0579 struct hsi_port *port = hsi_get_port(cl);
0580
0581 if (!hsi_port_claimed(cl))
0582 return -EACCES;
0583
0584 WARN_ON_ONCE(!msg->destructor || !msg->complete);
0585 msg->cl = cl;
0586
0587 return port->async(msg);
0588 }
0589 EXPORT_SYMBOL_GPL(hsi_async);
0590
0591
0592
0593
0594
0595
0596
0597
0598 int hsi_claim_port(struct hsi_client *cl, unsigned int share)
0599 {
0600 struct hsi_port *port = hsi_get_port(cl);
0601 int err = 0;
0602
0603 mutex_lock(&port->lock);
0604 if ((port->claimed) && (!port->shared || !share)) {
0605 err = -EBUSY;
0606 goto out;
0607 }
0608 if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) {
0609 err = -ENODEV;
0610 goto out;
0611 }
0612 port->claimed++;
0613 port->shared = !!share;
0614 cl->pclaimed = 1;
0615 out:
0616 mutex_unlock(&port->lock);
0617
0618 return err;
0619 }
0620 EXPORT_SYMBOL_GPL(hsi_claim_port);
0621
0622
0623
0624
0625
0626 void hsi_release_port(struct hsi_client *cl)
0627 {
0628 struct hsi_port *port = hsi_get_port(cl);
0629
0630 mutex_lock(&port->lock);
0631
0632 port->release(cl);
0633 if (cl->pclaimed)
0634 port->claimed--;
0635 BUG_ON(port->claimed < 0);
0636 cl->pclaimed = 0;
0637 if (!port->claimed)
0638 port->shared = 0;
0639 module_put(to_hsi_controller(port->device.parent)->owner);
0640 mutex_unlock(&port->lock);
0641 }
0642 EXPORT_SYMBOL_GPL(hsi_release_port);
0643
0644 static int hsi_event_notifier_call(struct notifier_block *nb,
0645 unsigned long event, void *data __maybe_unused)
0646 {
0647 struct hsi_client *cl = container_of(nb, struct hsi_client, nb);
0648
0649 (*cl->ehandler)(cl, event);
0650
0651 return 0;
0652 }
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666 int hsi_register_port_event(struct hsi_client *cl,
0667 void (*handler)(struct hsi_client *, unsigned long))
0668 {
0669 struct hsi_port *port = hsi_get_port(cl);
0670
0671 if (!handler || cl->ehandler)
0672 return -EINVAL;
0673 if (!hsi_port_claimed(cl))
0674 return -EACCES;
0675 cl->ehandler = handler;
0676 cl->nb.notifier_call = hsi_event_notifier_call;
0677
0678 return blocking_notifier_chain_register(&port->n_head, &cl->nb);
0679 }
0680 EXPORT_SYMBOL_GPL(hsi_register_port_event);
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691 int hsi_unregister_port_event(struct hsi_client *cl)
0692 {
0693 struct hsi_port *port = hsi_get_port(cl);
0694 int err;
0695
0696 WARN_ON(!hsi_port_claimed(cl));
0697
0698 err = blocking_notifier_chain_unregister(&port->n_head, &cl->nb);
0699 if (!err)
0700 cl->ehandler = NULL;
0701
0702 return err;
0703 }
0704 EXPORT_SYMBOL_GPL(hsi_unregister_port_event);
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721 int hsi_event(struct hsi_port *port, unsigned long event)
0722 {
0723 return blocking_notifier_call_chain(&port->n_head, event, NULL);
0724 }
0725 EXPORT_SYMBOL_GPL(hsi_event);
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738 int hsi_get_channel_id_by_name(struct hsi_client *cl, char *name)
0739 {
0740 int i;
0741
0742 if (!cl->rx_cfg.channels)
0743 return -ENOENT;
0744
0745 for (i = 0; i < cl->rx_cfg.num_channels; i++)
0746 if (!strcmp(cl->rx_cfg.channels[i].name, name))
0747 return cl->rx_cfg.channels[i].id;
0748
0749 return -ENXIO;
0750 }
0751 EXPORT_SYMBOL_GPL(hsi_get_channel_id_by_name);
0752
0753 static int __init hsi_init(void)
0754 {
0755 return bus_register(&hsi_bus_type);
0756 }
0757 postcore_initcall(hsi_init);
0758
0759 static void __exit hsi_exit(void)
0760 {
0761 bus_unregister(&hsi_bus_type);
0762 }
0763 module_exit(hsi_exit);
0764
0765 MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
0766 MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework");
0767 MODULE_LICENSE("GPL v2");