Abhimanyu Kapur | c75b2e1 | 2016-02-22 18:15:13 -0800 | [diff] [blame^] | 1 | /* Copyright (c) 2014, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/esoc_client.h> |
| 14 | #include <linux/of.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include "esoc.h" |
| 17 | |
| 18 | static DEFINE_SPINLOCK(notify_lock); |
| 19 | static ATOMIC_NOTIFIER_HEAD(client_notify); |
| 20 | |
| 21 | static void devm_esoc_desc_release(struct device *dev, void *res) |
| 22 | { |
| 23 | struct esoc_desc *esoc_desc = res; |
| 24 | |
| 25 | kfree(esoc_desc->name); |
| 26 | kfree(esoc_desc->link); |
| 27 | put_esoc_clink(esoc_desc->priv); |
| 28 | } |
| 29 | |
| 30 | static int devm_esoc_desc_match(struct device *dev, void *res, void *data) |
| 31 | { |
| 32 | struct esoc_desc *esoc_desc = res; |
| 33 | return esoc_desc == data; |
| 34 | } |
| 35 | |
| 36 | struct esoc_desc *devm_register_esoc_client(struct device *dev, |
| 37 | const char *name) |
| 38 | { |
| 39 | int ret, index; |
| 40 | const char *client_desc; |
| 41 | char *esoc_prop; |
| 42 | const __be32 *parp; |
| 43 | struct device_node *esoc_node; |
| 44 | struct device_node *np = dev->of_node; |
| 45 | struct esoc_clink *esoc_clink; |
| 46 | struct esoc_desc *desc; |
| 47 | char *esoc_name, *esoc_link; |
| 48 | |
| 49 | for (index = 0;; index++) { |
| 50 | esoc_prop = kasprintf(GFP_KERNEL, "esoc-%d", index); |
| 51 | parp = of_get_property(np, esoc_prop, NULL); |
| 52 | if (parp == NULL) { |
| 53 | dev_err(dev, "esoc device not present\n"); |
| 54 | kfree(esoc_prop); |
| 55 | return NULL; |
| 56 | } |
| 57 | ret = of_property_read_string_index(np, "esoc-names", index, |
| 58 | &client_desc); |
| 59 | if (ret) { |
| 60 | dev_err(dev, "cannot find matching string\n"); |
| 61 | kfree(esoc_prop); |
| 62 | return NULL; |
| 63 | } |
| 64 | if (strcmp(client_desc, name)) { |
| 65 | kfree(esoc_prop); |
| 66 | continue; |
| 67 | } |
| 68 | kfree(esoc_prop); |
| 69 | esoc_node = of_find_node_by_phandle(be32_to_cpup(parp)); |
| 70 | esoc_clink = get_esoc_clink_by_node(esoc_node); |
| 71 | if (IS_ERR_OR_NULL(esoc_clink)) { |
| 72 | dev_err(dev, "matching esoc clink not present\n"); |
| 73 | return ERR_PTR(-EPROBE_DEFER); |
| 74 | } |
| 75 | esoc_name = kasprintf(GFP_KERNEL, "esoc%d", |
| 76 | esoc_clink->id); |
| 77 | if (IS_ERR_OR_NULL(esoc_name)) { |
| 78 | dev_err(dev, "unable to allocate esoc name\n"); |
| 79 | return ERR_PTR(-ENOMEM); |
| 80 | } |
| 81 | esoc_link = kasprintf(GFP_KERNEL, "%s", esoc_clink->link_name); |
| 82 | if (IS_ERR_OR_NULL(esoc_link)) { |
| 83 | dev_err(dev, "unable to allocate esoc link name\n"); |
| 84 | kfree(esoc_name); |
| 85 | return ERR_PTR(-ENOMEM); |
| 86 | } |
| 87 | desc = devres_alloc(devm_esoc_desc_release, |
| 88 | sizeof(*desc), GFP_KERNEL); |
| 89 | if (IS_ERR_OR_NULL(desc)) { |
| 90 | kfree(esoc_name); |
| 91 | kfree(esoc_link); |
| 92 | dev_err(dev, "unable to allocate esoc descriptor\n"); |
| 93 | return ERR_PTR(-ENOMEM); |
| 94 | } |
| 95 | desc->name = esoc_name; |
| 96 | desc->link = esoc_link; |
| 97 | desc->priv = esoc_clink; |
| 98 | devres_add(dev, desc); |
| 99 | return desc; |
| 100 | } |
| 101 | return NULL; |
| 102 | } |
| 103 | EXPORT_SYMBOL(devm_register_esoc_client); |
| 104 | |
| 105 | void devm_unregister_esoc_client(struct device *dev, |
| 106 | struct esoc_desc *esoc_desc) |
| 107 | { |
| 108 | int ret; |
| 109 | |
| 110 | ret = devres_release(dev, devm_esoc_desc_release, |
| 111 | devm_esoc_desc_match, esoc_desc); |
| 112 | WARN_ON(ret); |
| 113 | } |
| 114 | EXPORT_SYMBOL(devm_unregister_esoc_client); |
| 115 | |
| 116 | int esoc_register_client_notifier(struct notifier_block *nb) |
| 117 | { |
| 118 | return atomic_notifier_chain_register(&client_notify, nb); |
| 119 | } |
| 120 | EXPORT_SYMBOL(esoc_register_client_notifier); |
| 121 | |
| 122 | void notify_esoc_clients(struct esoc_clink *esoc_clink, unsigned long evt) |
| 123 | { |
| 124 | unsigned int id; |
| 125 | unsigned long flags; |
| 126 | |
| 127 | spin_lock_irqsave(¬ify_lock, flags); |
| 128 | id = esoc_clink->id; |
| 129 | atomic_notifier_call_chain(&client_notify, evt, &id); |
| 130 | spin_unlock_irqrestore(¬ify_lock, flags); |
| 131 | } |
| 132 | EXPORT_SYMBOL(notify_esoc_clients); |