blob: d2e06fdb0be88856a723347b63320c2530b0da67 [file] [log] [blame]
Michael Bohanc8020172012-01-23 19:17:10 -08001/* Copyright (c) 2002-3 Patrick Mochel
2 * Copyright (c) 2002-3 Open Source Development Labs
Duy Truong790f06d2013-02-13 16:38:12 -08003 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
Michael Bohanc8020172012-01-23 19:17:10 -08004 *
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 Mucklef132c6c2012-06-06 18:30:57 -070017#include <linux/export.h>
Michael Bohan08e1e902012-05-23 10:55:22 -070018#include <linux/spmi.h>
Michael Bohana6a354d2012-05-21 17:47:11 -070019#include <linux/string.h>
Michael Bohanc8020172012-01-23 19:17:10 -080020
21/**
Michael Bohan08e1e902012-05-23 10:55:22 -070022 * spmi_get_resource - get a resource for a device
23 * @dev: spmi device
Michael Bohan0e5534d2012-05-22 17:33:45 -070024 * @node: device node resource
Michael Bohanc8020172012-01-23 19:17:10 -080025 * @type: resource type
Michael Bohan08e1e902012-05-23 10:55:22 -070026 * @res_num: resource index
27 *
Michael Bohan0e5534d2012-05-22 17:33:45 -070028 * 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 Bohan08e1e902012-05-23 10:55:22 -070033 * Returns
34 * NULL on failure.
Michael Bohanc8020172012-01-23 19:17:10 -080035 */
Michael Bohan08e1e902012-05-23 10:55:22 -070036struct resource *spmi_get_resource(struct spmi_device *dev,
Michael Bohan0e5534d2012-05-22 17:33:45 -070037 struct spmi_resource *node,
38 unsigned int type, unsigned int res_num)
Michael Bohanc8020172012-01-23 19:17:10 -080039{
40 int i;
41
Michael Bohan0e5534d2012-05-22 17:33:45 -070042 /* if a node is not specified, default to the first node */
43 if (!node)
Michael Bohan978d3352012-05-25 15:02:38 -070044 node = &dev->res;
Michael Bohan0e5534d2012-05-22 17:33:45 -070045
46 for (i = 0; i < node->num_resources; i++) {
47 struct resource *r = &node->resource[i];
Michael Bohanc8020172012-01-23 19:17:10 -080048
49 if (type == resource_type(r) && res_num-- == 0)
50 return r;
51 }
52 return NULL;
53}
Michael Bohan08e1e902012-05-23 10:55:22 -070054EXPORT_SYMBOL_GPL(spmi_get_resource);
Michael Bohanc8020172012-01-23 19:17:10 -080055
Michael Bohana6a354d2012-05-21 17:47:11 -070056#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 */
65struct 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 Bohan978d3352012-05-25 15:02:38 -070074 node = &dev->res;
Michael Bohana6a354d2012-05-21 17:47:11 -070075
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}
85EXPORT_SYMBOL_GPL(spmi_get_resource_byname);
86
Michael Bohanc8020172012-01-23 19:17:10 -080087/**
Michael Bohan08e1e902012-05-23 10:55:22 -070088 * spmi_get_irq - get an IRQ for a device
89 * @dev: spmi device
Michael Bohan0e5534d2012-05-22 17:33:45 -070090 * @node: device node resource
Michael Bohan08e1e902012-05-23 10:55:22 -070091 * @res_num: IRQ number index
92 *
93 * Returns
94 * -ENXIO on failure.
Michael Bohanc8020172012-01-23 19:17:10 -080095 */
Michael Bohan0e5534d2012-05-22 17:33:45 -070096int spmi_get_irq(struct spmi_device *dev, struct spmi_resource *node,
Michael Bohanc8020172012-01-23 19:17:10 -080097 unsigned int res_num)
98{
Michael Bohan0e5534d2012-05-22 17:33:45 -070099 struct resource *r = spmi_get_resource(dev, node,
Michael Bohanc8020172012-01-23 19:17:10 -0800100 IORESOURCE_IRQ, res_num);
101
102 return r ? r->start : -ENXIO;
103}
Michael Bohan08e1e902012-05-23 10:55:22 -0700104EXPORT_SYMBOL_GPL(spmi_get_irq);
Michael Bohana6a354d2012-05-21 17:47:11 -0700105
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 */
114int 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}
121EXPORT_SYMBOL_GPL(spmi_get_irq_byname);
Michael Bohan8d6aae32012-05-22 15:41:06 -0700122
123/*
Michael Bohan978d3352012-05-25 15:02:38 -0700124 * spmi_get_container_dev_byname - get a device node resource
Michael Bohan8d6aae32012-05-22 15:41:06 -0700125 * @dev: spmi device handle
126 * @label: device name to lookup
127 *
Michael Bohan978d3352012-05-25 15:02:38 -0700128 * 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 Bohan8d6aae32012-05-22 15:41:06 -0700133 */
134struct 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}
151EXPORT_SYMBOL(spmi_get_dev_container_byname);