Lars-Peter Clausen | 8ef997b | 2012-09-11 19:56:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License version 2 as |
| 4 | * published by the Free Software Foundation. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/clk.h> |
| 8 | #include <linux/device.h> |
| 9 | #include <linux/export.h> |
| 10 | #include <linux/gfp.h> |
| 11 | |
| 12 | static void devm_clk_release(struct device *dev, void *res) |
| 13 | { |
| 14 | clk_put(*(struct clk **)res); |
| 15 | } |
| 16 | |
| 17 | struct clk *devm_clk_get(struct device *dev, const char *id) |
| 18 | { |
| 19 | struct clk **ptr, *clk; |
| 20 | |
| 21 | ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL); |
| 22 | if (!ptr) |
| 23 | return ERR_PTR(-ENOMEM); |
| 24 | |
| 25 | clk = clk_get(dev, id); |
| 26 | if (!IS_ERR(clk)) { |
| 27 | *ptr = clk; |
| 28 | devres_add(dev, ptr); |
| 29 | } else { |
| 30 | devres_free(ptr); |
| 31 | } |
| 32 | |
| 33 | return clk; |
| 34 | } |
| 35 | EXPORT_SYMBOL(devm_clk_get); |
| 36 | |
Dong Aisheng | 618aee0 | 2017-05-19 21:49:05 +0800 | [diff] [blame] | 37 | struct clk_bulk_devres { |
| 38 | struct clk_bulk_data *clks; |
| 39 | int num_clks; |
| 40 | }; |
| 41 | |
| 42 | static void devm_clk_bulk_release(struct device *dev, void *res) |
| 43 | { |
| 44 | struct clk_bulk_devres *devres = res; |
| 45 | |
| 46 | clk_bulk_put(devres->num_clks, devres->clks); |
| 47 | } |
| 48 | |
| 49 | int __must_check devm_clk_bulk_get(struct device *dev, int num_clks, |
| 50 | struct clk_bulk_data *clks) |
| 51 | { |
| 52 | struct clk_bulk_devres *devres; |
| 53 | int ret; |
| 54 | |
| 55 | devres = devres_alloc(devm_clk_bulk_release, |
| 56 | sizeof(*devres), GFP_KERNEL); |
| 57 | if (!devres) |
| 58 | return -ENOMEM; |
| 59 | |
| 60 | ret = clk_bulk_get(dev, num_clks, clks); |
| 61 | if (!ret) { |
| 62 | devres->clks = clks; |
| 63 | devres->num_clks = num_clks; |
| 64 | devres_add(dev, devres); |
| 65 | } else { |
| 66 | devres_free(devres); |
| 67 | } |
| 68 | |
| 69 | return ret; |
| 70 | } |
| 71 | EXPORT_SYMBOL_GPL(devm_clk_bulk_get); |
| 72 | |
Lars-Peter Clausen | 8ef997b | 2012-09-11 19:56:23 +0100 | [diff] [blame] | 73 | static int devm_clk_match(struct device *dev, void *res, void *data) |
| 74 | { |
| 75 | struct clk **c = res; |
| 76 | if (!c || !*c) { |
| 77 | WARN_ON(!c || !*c); |
| 78 | return 0; |
| 79 | } |
| 80 | return *c == data; |
| 81 | } |
| 82 | |
| 83 | void devm_clk_put(struct device *dev, struct clk *clk) |
| 84 | { |
| 85 | int ret; |
| 86 | |
Mark Brown | 20332ff | 2012-09-19 12:43:21 +0100 | [diff] [blame] | 87 | ret = devres_release(dev, devm_clk_release, devm_clk_match, clk); |
Lars-Peter Clausen | 8ef997b | 2012-09-11 19:56:23 +0100 | [diff] [blame] | 88 | |
| 89 | WARN_ON(ret); |
| 90 | } |
| 91 | EXPORT_SYMBOL(devm_clk_put); |
Kuninori Morimoto | 71a2f11 | 2016-12-05 05:23:20 +0000 | [diff] [blame] | 92 | |
| 93 | struct clk *devm_get_clk_from_child(struct device *dev, |
| 94 | struct device_node *np, const char *con_id) |
| 95 | { |
| 96 | struct clk **ptr, *clk; |
| 97 | |
| 98 | ptr = devres_alloc(devm_clk_release, sizeof(*ptr), GFP_KERNEL); |
| 99 | if (!ptr) |
| 100 | return ERR_PTR(-ENOMEM); |
| 101 | |
| 102 | clk = of_clk_get_by_name(np, con_id); |
| 103 | if (!IS_ERR(clk)) { |
| 104 | *ptr = clk; |
| 105 | devres_add(dev, ptr); |
| 106 | } else { |
| 107 | devres_free(ptr); |
| 108 | } |
| 109 | |
| 110 | return clk; |
| 111 | } |
| 112 | EXPORT_SYMBOL(devm_get_clk_from_child); |