spmi-resources.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (c) 2002-3 Patrick Mochel
  2. * Copyright (c) 2002-3 Open Source Development Labs
  3. * Copyright (c) 2012, The Linux Foundation. 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. #include <linux/export.h>
  17. #include <linux/spmi.h>
  18. #include <linux/string.h>
  19. /**
  20. * spmi_get_resource - get a resource for a device
  21. * @dev: spmi device
  22. * @node: device node resource
  23. * @type: resource type
  24. * @res_num: resource index
  25. *
  26. * If 'node' is specified as NULL, then the API treats this as a special
  27. * case to assume the first devnode. For configurations that do not use
  28. * spmi-dev-container, there is only one node to begin with, so NULL should
  29. * be passed in this case.
  30. *
  31. * Returns
  32. * NULL on failure.
  33. */
  34. struct resource *spmi_get_resource(struct spmi_device *dev,
  35. struct spmi_resource *node,
  36. unsigned int type, unsigned int res_num)
  37. {
  38. int i;
  39. /* if a node is not specified, default to the first node */
  40. if (!node)
  41. node = &dev->res;
  42. for (i = 0; i < node->num_resources; i++) {
  43. struct resource *r = &node->resource[i];
  44. if (type == resource_type(r) && res_num-- == 0)
  45. return r;
  46. }
  47. return NULL;
  48. }
  49. EXPORT_SYMBOL_GPL(spmi_get_resource);
  50. #define SPMI_MAX_RES_NAME 256
  51. /**
  52. * spmi_get_resource_byname - get a resource for a device given a name
  53. * @dev: spmi device handle
  54. * @node: device node resource
  55. * @type: resource type
  56. * @name: resource name to lookup
  57. */
  58. struct resource *spmi_get_resource_byname(struct spmi_device *dev,
  59. struct spmi_resource *node,
  60. unsigned int type,
  61. const char *name)
  62. {
  63. int i;
  64. /* if a node is not specified, default to the first node */
  65. if (!node)
  66. node = &dev->res;
  67. for (i = 0; i < node->num_resources; i++) {
  68. struct resource *r = &node->resource[i];
  69. if (type == resource_type(r) && r->name &&
  70. !strncmp(r->name, name, SPMI_MAX_RES_NAME))
  71. return r;
  72. }
  73. return NULL;
  74. }
  75. EXPORT_SYMBOL_GPL(spmi_get_resource_byname);
  76. /**
  77. * spmi_get_irq - get an IRQ for a device
  78. * @dev: spmi device
  79. * @node: device node resource
  80. * @res_num: IRQ number index
  81. *
  82. * Returns
  83. * -ENXIO on failure.
  84. */
  85. int spmi_get_irq(struct spmi_device *dev, struct spmi_resource *node,
  86. unsigned int res_num)
  87. {
  88. struct resource *r = spmi_get_resource(dev, node,
  89. IORESOURCE_IRQ, res_num);
  90. return r ? r->start : -ENXIO;
  91. }
  92. EXPORT_SYMBOL_GPL(spmi_get_irq);
  93. /**
  94. * spmi_get_irq_byname - get an IRQ for a device given a name
  95. * @dev: spmi device handle
  96. * @node: device node resource
  97. * @name: resource name to lookup
  98. *
  99. * Returns -ENXIO on failure
  100. */
  101. int spmi_get_irq_byname(struct spmi_device *dev,
  102. struct spmi_resource *node, const char *name)
  103. {
  104. struct resource *r = spmi_get_resource_byname(dev, node,
  105. IORESOURCE_IRQ, name);
  106. return r ? r->start : -ENXIO;
  107. }
  108. EXPORT_SYMBOL_GPL(spmi_get_irq_byname);
  109. /*
  110. * spmi_get_container_dev_byname - get a device node resource
  111. * @dev: spmi device handle
  112. * @label: device name to lookup
  113. *
  114. * Only useable in spmi-dev-container configurations. Given a name,
  115. * find the associated spmi_resource that matches the name.
  116. *
  117. * Return NULL if the spmi_device is not a dev-container,
  118. * or if the lookup fails.
  119. */
  120. struct spmi_resource *spmi_get_dev_container_byname(struct spmi_device *dev,
  121. const char *label)
  122. {
  123. int i;
  124. if (!label)
  125. return NULL;
  126. for (i = 0; i < dev->num_dev_node; i++) {
  127. struct spmi_resource *r = &dev->dev_node[i];
  128. if (r && r->label && !strncmp(r->label,
  129. label, SPMI_MAX_RES_NAME))
  130. return r;
  131. }
  132. return NULL;
  133. }
  134. EXPORT_SYMBOL(spmi_get_dev_container_byname);