Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2002-3 Patrick Mochel |
| 2 | * Copyright (c) 2002-3 Open Source Development Labs |
Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 3 | * Copyright (c) 2012, The Linux Foundation. All rights reserved. |
Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 and |
| 7 | * only version 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * Resource handling based on platform.c. |
| 15 | */ |
| 16 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 17 | #include <linux/export.h> |
Michael Bohan | 08e1e90 | 2012-05-23 10:55:22 -0700 | [diff] [blame] | 18 | #include <linux/spmi.h> |
Michael Bohan | a6a354d | 2012-05-21 17:47:11 -0700 | [diff] [blame] | 19 | #include <linux/string.h> |
Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 20 | |
| 21 | /** |
Michael Bohan | 08e1e90 | 2012-05-23 10:55:22 -0700 | [diff] [blame] | 22 | * spmi_get_resource - get a resource for a device |
| 23 | * @dev: spmi device |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 24 | * @node: device node resource |
Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 25 | * @type: resource type |
Michael Bohan | 08e1e90 | 2012-05-23 10:55:22 -0700 | [diff] [blame] | 26 | * @res_num: resource index |
| 27 | * |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 28 | * If 'node' is specified as NULL, then the API treats this as a special |
| 29 | * case to assume the first devnode. For configurations that do not use |
| 30 | * spmi-dev-container, there is only one node to begin with, so NULL should |
| 31 | * be passed in this case. |
| 32 | * |
Michael Bohan | 08e1e90 | 2012-05-23 10:55:22 -0700 | [diff] [blame] | 33 | * Returns |
| 34 | * NULL on failure. |
Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 35 | */ |
Michael Bohan | 08e1e90 | 2012-05-23 10:55:22 -0700 | [diff] [blame] | 36 | struct resource *spmi_get_resource(struct spmi_device *dev, |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 37 | struct spmi_resource *node, |
| 38 | unsigned int type, unsigned int res_num) |
Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 39 | { |
| 40 | int i; |
| 41 | |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 42 | /* if a node is not specified, default to the first node */ |
| 43 | if (!node) |
Michael Bohan | 978d335 | 2012-05-25 15:02:38 -0700 | [diff] [blame] | 44 | node = &dev->res; |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 45 | |
| 46 | for (i = 0; i < node->num_resources; i++) { |
| 47 | struct resource *r = &node->resource[i]; |
Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 48 | |
| 49 | if (type == resource_type(r) && res_num-- == 0) |
| 50 | return r; |
| 51 | } |
| 52 | return NULL; |
| 53 | } |
Michael Bohan | 08e1e90 | 2012-05-23 10:55:22 -0700 | [diff] [blame] | 54 | EXPORT_SYMBOL_GPL(spmi_get_resource); |
Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 55 | |
Michael Bohan | a6a354d | 2012-05-21 17:47:11 -0700 | [diff] [blame] | 56 | #define SPMI_MAX_RES_NAME 256 |
| 57 | |
| 58 | /** |
| 59 | * spmi_get_resource_byname - get a resource for a device given a name |
| 60 | * @dev: spmi device handle |
| 61 | * @node: device node resource |
| 62 | * @type: resource type |
| 63 | * @name: resource name to lookup |
| 64 | */ |
| 65 | struct resource *spmi_get_resource_byname(struct spmi_device *dev, |
| 66 | struct spmi_resource *node, |
| 67 | unsigned int type, |
| 68 | const char *name) |
| 69 | { |
| 70 | int i; |
| 71 | |
| 72 | /* if a node is not specified, default to the first node */ |
| 73 | if (!node) |
Michael Bohan | 978d335 | 2012-05-25 15:02:38 -0700 | [diff] [blame] | 74 | node = &dev->res; |
Michael Bohan | a6a354d | 2012-05-21 17:47:11 -0700 | [diff] [blame] | 75 | |
| 76 | for (i = 0; i < node->num_resources; i++) { |
| 77 | struct resource *r = &node->resource[i]; |
| 78 | |
| 79 | if (type == resource_type(r) && r->name && |
| 80 | !strncmp(r->name, name, SPMI_MAX_RES_NAME)) |
| 81 | return r; |
| 82 | } |
| 83 | return NULL; |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(spmi_get_resource_byname); |
| 86 | |
Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 87 | /** |
Michael Bohan | 08e1e90 | 2012-05-23 10:55:22 -0700 | [diff] [blame] | 88 | * spmi_get_irq - get an IRQ for a device |
| 89 | * @dev: spmi device |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 90 | * @node: device node resource |
Michael Bohan | 08e1e90 | 2012-05-23 10:55:22 -0700 | [diff] [blame] | 91 | * @res_num: IRQ number index |
| 92 | * |
| 93 | * Returns |
| 94 | * -ENXIO on failure. |
Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 95 | */ |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 96 | int spmi_get_irq(struct spmi_device *dev, struct spmi_resource *node, |
Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 97 | unsigned int res_num) |
| 98 | { |
Michael Bohan | 0e5534d | 2012-05-22 17:33:45 -0700 | [diff] [blame] | 99 | struct resource *r = spmi_get_resource(dev, node, |
Michael Bohan | c802017 | 2012-01-23 19:17:10 -0800 | [diff] [blame] | 100 | IORESOURCE_IRQ, res_num); |
| 101 | |
| 102 | return r ? r->start : -ENXIO; |
| 103 | } |
Michael Bohan | 08e1e90 | 2012-05-23 10:55:22 -0700 | [diff] [blame] | 104 | EXPORT_SYMBOL_GPL(spmi_get_irq); |
Michael Bohan | a6a354d | 2012-05-21 17:47:11 -0700 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * spmi_get_irq_byname - get an IRQ for a device given a name |
| 108 | * @dev: spmi device handle |
| 109 | * @node: device node resource |
| 110 | * @name: resource name to lookup |
| 111 | * |
| 112 | * Returns -ENXIO on failure |
| 113 | */ |
| 114 | int spmi_get_irq_byname(struct spmi_device *dev, |
| 115 | struct spmi_resource *node, const char *name) |
| 116 | { |
| 117 | struct resource *r = spmi_get_resource_byname(dev, node, |
| 118 | IORESOURCE_IRQ, name); |
| 119 | return r ? r->start : -ENXIO; |
| 120 | } |
| 121 | EXPORT_SYMBOL_GPL(spmi_get_irq_byname); |
Michael Bohan | 8d6aae3 | 2012-05-22 15:41:06 -0700 | [diff] [blame] | 122 | |
| 123 | /* |
Michael Bohan | 978d335 | 2012-05-25 15:02:38 -0700 | [diff] [blame] | 124 | * spmi_get_container_dev_byname - get a device node resource |
Michael Bohan | 8d6aae3 | 2012-05-22 15:41:06 -0700 | [diff] [blame] | 125 | * @dev: spmi device handle |
| 126 | * @label: device name to lookup |
| 127 | * |
Michael Bohan | 978d335 | 2012-05-25 15:02:38 -0700 | [diff] [blame] | 128 | * Only useable in spmi-dev-container configurations. Given a name, |
| 129 | * find the associated spmi_resource that matches the name. |
| 130 | * |
| 131 | * Return NULL if the spmi_device is not a dev-container, |
| 132 | * or if the lookup fails. |
Michael Bohan | 8d6aae3 | 2012-05-22 15:41:06 -0700 | [diff] [blame] | 133 | */ |
| 134 | struct spmi_resource *spmi_get_dev_container_byname(struct spmi_device *dev, |
| 135 | const char *label) |
| 136 | { |
| 137 | int i; |
| 138 | |
| 139 | if (!label) |
| 140 | return NULL; |
| 141 | |
| 142 | for (i = 0; i < dev->num_dev_node; i++) { |
| 143 | struct spmi_resource *r = &dev->dev_node[i]; |
| 144 | |
| 145 | if (r && r->label && !strncmp(r->label, |
| 146 | label, SPMI_MAX_RES_NAME)) |
| 147 | return r; |
| 148 | } |
| 149 | return NULL; |
| 150 | } |
| 151 | EXPORT_SYMBOL(spmi_get_dev_container_byname); |