of_regulator.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * OF helpers for regulator framework
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Rajendra Nayak <rnayak@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/of.h>
  15. #include <linux/string.h>
  16. #include <linux/regulator/machine.h>
  17. static void of_get_regulation_constraints(struct device_node *np,
  18. struct regulator_init_data **init_data)
  19. {
  20. const __be32 *min_uV, *max_uV, *uV_offset;
  21. const __be32 *min_uA, *max_uA;
  22. struct regulation_constraints *constraints = &(*init_data)->constraints;
  23. constraints->name = of_get_property(np, "regulator-name", NULL);
  24. min_uV = of_get_property(np, "regulator-min-microvolt", NULL);
  25. if (min_uV)
  26. constraints->min_uV = be32_to_cpu(*min_uV);
  27. max_uV = of_get_property(np, "regulator-max-microvolt", NULL);
  28. if (max_uV)
  29. constraints->max_uV = be32_to_cpu(*max_uV);
  30. /* Voltage change possible? */
  31. if (constraints->min_uV != constraints->max_uV)
  32. constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  33. /* Only one voltage? Then make sure it's set. */
  34. if (min_uV && max_uV && constraints->min_uV == constraints->max_uV)
  35. constraints->apply_uV = true;
  36. uV_offset = of_get_property(np, "regulator-microvolt-offset", NULL);
  37. if (uV_offset)
  38. constraints->uV_offset = be32_to_cpu(*uV_offset);
  39. min_uA = of_get_property(np, "regulator-min-microamp", NULL);
  40. if (min_uA)
  41. constraints->min_uA = be32_to_cpu(*min_uA);
  42. max_uA = of_get_property(np, "regulator-max-microamp", NULL);
  43. if (max_uA)
  44. constraints->max_uA = be32_to_cpu(*max_uA);
  45. /* Current change possible? */
  46. if (constraints->min_uA != constraints->max_uA)
  47. constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
  48. if (of_find_property(np, "regulator-boot-on", NULL))
  49. constraints->boot_on = true;
  50. if (of_find_property(np, "regulator-always-on", NULL))
  51. constraints->always_on = true;
  52. else /* status change should be possible if not always on. */
  53. constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
  54. }
  55. static const char *consumer_supply_prop_name = "qcom,consumer-supplies";
  56. #define MAX_DEV_NAME_LEN 256
  57. /*
  58. * Fill in regulator init_data based on qcom legacy requirements.
  59. */
  60. static int of_get_qcom_regulator_init_data(struct device *dev,
  61. struct regulator_init_data **init_data)
  62. {
  63. struct device_node *node = dev->of_node;
  64. struct regulator_consumer_supply *consumer_supplies;
  65. int i, rc, num_consumer_supplies, array_len;
  66. array_len = of_property_count_strings(node, consumer_supply_prop_name);
  67. if (array_len > 0) {
  68. /* Array length must be divisible by 2. */
  69. if (array_len & 1) {
  70. dev_err(dev, "error: %s device node property value "
  71. "contains an odd number of elements: %d\n",
  72. consumer_supply_prop_name, array_len);
  73. return -EINVAL;
  74. }
  75. num_consumer_supplies = array_len / 2;
  76. consumer_supplies = devm_kzalloc(dev,
  77. sizeof(struct regulator_consumer_supply)
  78. * num_consumer_supplies, GFP_KERNEL);
  79. if (consumer_supplies == NULL) {
  80. dev_err(dev, "devm_kzalloc failed\n");
  81. return -ENOMEM;
  82. }
  83. for (i = 0; i < num_consumer_supplies; i++) {
  84. rc = of_property_read_string_index(node,
  85. consumer_supply_prop_name, i * 2,
  86. &consumer_supplies[i].supply);
  87. if (rc) {
  88. dev_err(dev, "of_property_read_string_index "
  89. "failed, rc=%d\n", rc);
  90. devm_kfree(dev, consumer_supplies);
  91. return rc;
  92. }
  93. rc = of_property_read_string_index(node,
  94. consumer_supply_prop_name, (i * 2) + 1,
  95. &consumer_supplies[i].dev_name);
  96. if (rc) {
  97. dev_err(dev, "of_property_read_string_index "
  98. "failed, rc=%d\n", rc);
  99. devm_kfree(dev, consumer_supplies);
  100. return rc;
  101. }
  102. /* Treat dev_name = "" as a wildcard. */
  103. if (strnlen(consumer_supplies[i].dev_name,
  104. MAX_DEV_NAME_LEN) == 0)
  105. consumer_supplies[i].dev_name = NULL;
  106. }
  107. (*init_data)->consumer_supplies = consumer_supplies;
  108. (*init_data)->num_consumer_supplies = num_consumer_supplies;
  109. }
  110. return 0;
  111. }
  112. /**
  113. * of_get_regulator_init_data - extract regulator_init_data structure info
  114. * @dev: device requesting for regulator_init_data
  115. *
  116. * Populates regulator_init_data structure by extracting data from device
  117. * tree node, returns a pointer to the populated struture or NULL if memory
  118. * alloc fails.
  119. */
  120. struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
  121. struct device_node *node)
  122. {
  123. struct regulator_init_data *init_data;
  124. int rc;
  125. if (!node)
  126. return NULL;
  127. init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
  128. if (!init_data)
  129. return NULL; /* Out of memory? */
  130. of_get_regulation_constraints(node, &init_data);
  131. rc = of_get_qcom_regulator_init_data(dev, &init_data);
  132. if (rc) {
  133. devm_kfree(dev, init_data);
  134. return NULL;
  135. }
  136. return init_data;
  137. }
  138. EXPORT_SYMBOL_GPL(of_get_regulator_init_data);