blob: d7adea9df2a57b6be88f9fcf371b36112ba45654 [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
3 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
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 Mucklef132c6c2012-06-06 18:30:57 -070017#include <linux/export.h>
Michael Bohan08e1e902012-05-23 10:55:22 -070018#include <linux/spmi.h>
Michael Bohanc8020172012-01-23 19:17:10 -080019
20/**
Michael Bohan08e1e902012-05-23 10:55:22 -070021 * spmi_get_resource - get a resource for a device
22 * @dev: spmi device
Michael Bohan0e5534d2012-05-22 17:33:45 -070023 * @node: device node resource
Michael Bohanc8020172012-01-23 19:17:10 -080024 * @type: resource type
Michael Bohan08e1e902012-05-23 10:55:22 -070025 * @res_num: resource index
26 *
Michael Bohan0e5534d2012-05-22 17:33:45 -070027 * If 'node' is specified as NULL, then the API treats this as a special
28 * case to assume the first devnode. For configurations that do not use
29 * spmi-dev-container, there is only one node to begin with, so NULL should
30 * be passed in this case.
31 *
Michael Bohan08e1e902012-05-23 10:55:22 -070032 * Returns
33 * NULL on failure.
Michael Bohanc8020172012-01-23 19:17:10 -080034 */
Michael Bohan08e1e902012-05-23 10:55:22 -070035struct resource *spmi_get_resource(struct spmi_device *dev,
Michael Bohan0e5534d2012-05-22 17:33:45 -070036 struct spmi_resource *node,
37 unsigned int type, unsigned int res_num)
Michael Bohanc8020172012-01-23 19:17:10 -080038{
39 int i;
40
Michael Bohan0e5534d2012-05-22 17:33:45 -070041 /* if a node is not specified, default to the first node */
42 if (!node)
43 node = &dev->dev_node[0];
44
45 for (i = 0; i < node->num_resources; i++) {
46 struct resource *r = &node->resource[i];
Michael Bohanc8020172012-01-23 19:17:10 -080047
48 if (type == resource_type(r) && res_num-- == 0)
49 return r;
50 }
51 return NULL;
52}
Michael Bohan08e1e902012-05-23 10:55:22 -070053EXPORT_SYMBOL_GPL(spmi_get_resource);
Michael Bohanc8020172012-01-23 19:17:10 -080054
55/**
Michael Bohan08e1e902012-05-23 10:55:22 -070056 * spmi_get_irq - get an IRQ for a device
57 * @dev: spmi device
Michael Bohan0e5534d2012-05-22 17:33:45 -070058 * @node: device node resource
Michael Bohan08e1e902012-05-23 10:55:22 -070059 * @res_num: IRQ number index
60 *
61 * Returns
62 * -ENXIO on failure.
Michael Bohanc8020172012-01-23 19:17:10 -080063 */
Michael Bohan0e5534d2012-05-22 17:33:45 -070064int spmi_get_irq(struct spmi_device *dev, struct spmi_resource *node,
Michael Bohanc8020172012-01-23 19:17:10 -080065 unsigned int res_num)
66{
Michael Bohan0e5534d2012-05-22 17:33:45 -070067 struct resource *r = spmi_get_resource(dev, node,
Michael Bohanc8020172012-01-23 19:17:10 -080068 IORESOURCE_IRQ, res_num);
69
70 return r ? r->start : -ENXIO;
71}
Michael Bohan08e1e902012-05-23 10:55:22 -070072EXPORT_SYMBOL_GPL(spmi_get_irq);