Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 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 | |
Axel Lin | e69af5e | 2011-11-26 18:50:58 +0800 | [diff] [blame] | 13 | #include <linux/module.h> |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <linux/of.h> |
David Collins | 4475476 | 2012-05-01 15:43:13 -0700 | [diff] [blame] | 16 | #include <linux/string.h> |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 17 | #include <linux/regulator/machine.h> |
| 18 | |
| 19 | static void of_get_regulation_constraints(struct device_node *np, |
| 20 | struct regulator_init_data **init_data) |
| 21 | { |
| 22 | const __be32 *min_uV, *max_uV, *uV_offset; |
| 23 | const __be32 *min_uA, *max_uA; |
| 24 | struct regulation_constraints *constraints = &(*init_data)->constraints; |
| 25 | |
| 26 | constraints->name = of_get_property(np, "regulator-name", NULL); |
| 27 | |
| 28 | min_uV = of_get_property(np, "regulator-min-microvolt", NULL); |
| 29 | if (min_uV) |
| 30 | constraints->min_uV = be32_to_cpu(*min_uV); |
| 31 | max_uV = of_get_property(np, "regulator-max-microvolt", NULL); |
| 32 | if (max_uV) |
| 33 | constraints->max_uV = be32_to_cpu(*max_uV); |
| 34 | |
| 35 | /* Voltage change possible? */ |
| 36 | if (constraints->min_uV != constraints->max_uV) |
| 37 | constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE; |
Mark Brown | ab62aa9 | 2011-12-05 10:58:41 +0000 | [diff] [blame] | 38 | /* Only one voltage? Then make sure it's set. */ |
Karol Lewandowski | 8a09304 | 2012-01-25 10:31:45 +0100 | [diff] [blame] | 39 | if (min_uV && max_uV && constraints->min_uV == constraints->max_uV) |
Mark Brown | ab62aa9 | 2011-12-05 10:58:41 +0000 | [diff] [blame] | 40 | constraints->apply_uV = true; |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 41 | |
| 42 | uV_offset = of_get_property(np, "regulator-microvolt-offset", NULL); |
| 43 | if (uV_offset) |
| 44 | constraints->uV_offset = be32_to_cpu(*uV_offset); |
| 45 | min_uA = of_get_property(np, "regulator-min-microamp", NULL); |
| 46 | if (min_uA) |
| 47 | constraints->min_uA = be32_to_cpu(*min_uA); |
| 48 | max_uA = of_get_property(np, "regulator-max-microamp", NULL); |
| 49 | if (max_uA) |
| 50 | constraints->max_uA = be32_to_cpu(*max_uA); |
| 51 | |
| 52 | /* Current change possible? */ |
| 53 | if (constraints->min_uA != constraints->max_uA) |
| 54 | constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT; |
| 55 | |
| 56 | if (of_find_property(np, "regulator-boot-on", NULL)) |
| 57 | constraints->boot_on = true; |
| 58 | |
| 59 | if (of_find_property(np, "regulator-always-on", NULL)) |
| 60 | constraints->always_on = true; |
| 61 | else /* status change should be possible if not always on. */ |
| 62 | constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS; |
| 63 | } |
| 64 | |
David Collins | 4475476 | 2012-05-01 15:43:13 -0700 | [diff] [blame] | 65 | static const char *consumer_supply_prop_name = "qcom,consumer-supplies"; |
| 66 | #define MAX_DEV_NAME_LEN 256 |
| 67 | /* |
| 68 | * Fill in regulator init_data based on qcom legacy requirements. |
| 69 | */ |
| 70 | static int of_get_qcom_regulator_init_data(struct device *dev, |
| 71 | struct regulator_init_data **init_data) |
| 72 | { |
| 73 | struct device_node *node = dev->of_node; |
| 74 | struct regulator_consumer_supply *consumer_supplies; |
| 75 | int i, rc, num_consumer_supplies, array_len; |
| 76 | |
| 77 | array_len = of_property_count_strings(node, consumer_supply_prop_name); |
| 78 | if (array_len > 0) { |
| 79 | /* Array length must be divisible by 2. */ |
| 80 | if (array_len & 1) { |
| 81 | dev_err(dev, "error: %s device node property value " |
| 82 | "contains an odd number of elements: %d\n", |
| 83 | consumer_supply_prop_name, array_len); |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | num_consumer_supplies = array_len / 2; |
| 87 | |
| 88 | consumer_supplies = devm_kzalloc(dev, |
| 89 | sizeof(struct regulator_consumer_supply) |
| 90 | * num_consumer_supplies, GFP_KERNEL); |
| 91 | if (consumer_supplies == NULL) { |
| 92 | dev_err(dev, "devm_kzalloc failed\n"); |
| 93 | return -ENOMEM; |
| 94 | } |
| 95 | |
| 96 | for (i = 0; i < num_consumer_supplies; i++) { |
| 97 | rc = of_property_read_string_index(node, |
| 98 | consumer_supply_prop_name, i * 2, |
| 99 | &consumer_supplies[i].supply); |
| 100 | if (rc) { |
| 101 | dev_err(dev, "of_property_read_string_index " |
| 102 | "failed, rc=%d\n", rc); |
| 103 | devm_kfree(dev, consumer_supplies); |
| 104 | return rc; |
| 105 | } |
| 106 | |
| 107 | rc = of_property_read_string_index(node, |
| 108 | consumer_supply_prop_name, (i * 2) + 1, |
| 109 | &consumer_supplies[i].dev_name); |
| 110 | if (rc) { |
| 111 | dev_err(dev, "of_property_read_string_index " |
| 112 | "failed, rc=%d\n", rc); |
| 113 | devm_kfree(dev, consumer_supplies); |
| 114 | return rc; |
| 115 | } |
| 116 | |
| 117 | /* Treat dev_name = "" as a wildcard. */ |
| 118 | if (strnlen(consumer_supplies[i].dev_name, |
| 119 | MAX_DEV_NAME_LEN) == 0) |
| 120 | consumer_supplies[i].dev_name = NULL; |
| 121 | } |
| 122 | |
| 123 | (*init_data)->consumer_supplies = consumer_supplies; |
| 124 | (*init_data)->num_consumer_supplies = num_consumer_supplies; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 130 | /** |
| 131 | * of_get_regulator_init_data - extract regulator_init_data structure info |
| 132 | * @dev: device requesting for regulator_init_data |
| 133 | * |
| 134 | * Populates regulator_init_data structure by extracting data from device |
| 135 | * tree node, returns a pointer to the populated struture or NULL if memory |
| 136 | * alloc fails. |
| 137 | */ |
Shawn Guo | d9a861c | 2011-12-01 17:21:06 +0800 | [diff] [blame] | 138 | struct regulator_init_data *of_get_regulator_init_data(struct device *dev, |
| 139 | struct device_node *node) |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 140 | { |
| 141 | struct regulator_init_data *init_data; |
David Collins | 4475476 | 2012-05-01 15:43:13 -0700 | [diff] [blame] | 142 | int rc; |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 143 | |
Shawn Guo | d9a861c | 2011-12-01 17:21:06 +0800 | [diff] [blame] | 144 | if (!node) |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 145 | return NULL; |
| 146 | |
| 147 | init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL); |
| 148 | if (!init_data) |
| 149 | return NULL; /* Out of memory? */ |
| 150 | |
Shawn Guo | d9a861c | 2011-12-01 17:21:06 +0800 | [diff] [blame] | 151 | of_get_regulation_constraints(node, &init_data); |
David Collins | 4475476 | 2012-05-01 15:43:13 -0700 | [diff] [blame] | 152 | rc = of_get_qcom_regulator_init_data(dev, &init_data); |
| 153 | if (rc) { |
| 154 | devm_kfree(dev, init_data); |
| 155 | return NULL; |
| 156 | } |
| 157 | |
Rajendra Nayak | 8f446e6 | 2011-11-18 16:47:17 +0530 | [diff] [blame] | 158 | return init_data; |
| 159 | } |
Axel Lin | e69af5e | 2011-11-26 18:50:58 +0800 | [diff] [blame] | 160 | EXPORT_SYMBOL_GPL(of_get_regulator_init_data); |