0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/mfd/mc13xxx.h>
0015 #include <linux/regulator/machine.h>
0016 #include <linux/regulator/driver.h>
0017 #include <linux/regulator/of_regulator.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/kernel.h>
0020 #include <linux/slab.h>
0021 #include <linux/init.h>
0022 #include <linux/err.h>
0023 #include <linux/module.h>
0024 #include <linux/of.h>
0025 #include "mc13xxx.h"
0026
0027 static int mc13xxx_regulator_enable(struct regulator_dev *rdev)
0028 {
0029 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
0030 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
0031 int id = rdev_get_id(rdev);
0032
0033 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
0034
0035 return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
0036 mc13xxx_regulators[id].enable_bit,
0037 mc13xxx_regulators[id].enable_bit);
0038 }
0039
0040 static int mc13xxx_regulator_disable(struct regulator_dev *rdev)
0041 {
0042 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
0043 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
0044 int id = rdev_get_id(rdev);
0045
0046 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
0047
0048 return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
0049 mc13xxx_regulators[id].enable_bit, 0);
0050 }
0051
0052 static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev)
0053 {
0054 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
0055 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
0056 int ret, id = rdev_get_id(rdev);
0057 unsigned int val;
0058
0059 ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
0060 if (ret)
0061 return ret;
0062
0063 return (val & mc13xxx_regulators[id].enable_bit) != 0;
0064 }
0065
0066 static int mc13xxx_regulator_set_voltage_sel(struct regulator_dev *rdev,
0067 unsigned selector)
0068 {
0069 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
0070 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
0071 int id = rdev_get_id(rdev);
0072
0073 return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
0074 mc13xxx_regulators[id].vsel_mask,
0075 selector << mc13xxx_regulators[id].vsel_shift);
0076 }
0077
0078 static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
0079 {
0080 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
0081 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
0082 int ret, id = rdev_get_id(rdev);
0083 unsigned int val;
0084
0085 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
0086
0087 ret = mc13xxx_reg_read(priv->mc13xxx,
0088 mc13xxx_regulators[id].vsel_reg, &val);
0089 if (ret)
0090 return ret;
0091
0092 val = (val & mc13xxx_regulators[id].vsel_mask)
0093 >> mc13xxx_regulators[id].vsel_shift;
0094
0095 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
0096
0097 BUG_ON(val >= mc13xxx_regulators[id].desc.n_voltages);
0098
0099 return rdev->desc->volt_table[val];
0100 }
0101
0102 const struct regulator_ops mc13xxx_regulator_ops = {
0103 .enable = mc13xxx_regulator_enable,
0104 .disable = mc13xxx_regulator_disable,
0105 .is_enabled = mc13xxx_regulator_is_enabled,
0106 .list_voltage = regulator_list_voltage_table,
0107 .set_voltage_sel = mc13xxx_regulator_set_voltage_sel,
0108 .get_voltage = mc13xxx_regulator_get_voltage,
0109 };
0110 EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops);
0111
0112 int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
0113 int max_uV, unsigned *selector)
0114 {
0115 int id = rdev_get_id(rdev);
0116
0117 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
0118 __func__, id, min_uV, max_uV);
0119
0120 if (min_uV <= rdev->desc->volt_table[0] &&
0121 rdev->desc->volt_table[0] <= max_uV) {
0122 *selector = 0;
0123 return 0;
0124 } else {
0125 return -EINVAL;
0126 }
0127 }
0128 EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage);
0129
0130 const struct regulator_ops mc13xxx_fixed_regulator_ops = {
0131 .enable = mc13xxx_regulator_enable,
0132 .disable = mc13xxx_regulator_disable,
0133 .is_enabled = mc13xxx_regulator_is_enabled,
0134 .list_voltage = regulator_list_voltage_table,
0135 .set_voltage = mc13xxx_fixed_regulator_set_voltage,
0136 };
0137 EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops);
0138
0139 #ifdef CONFIG_OF
0140 int mc13xxx_get_num_regulators_dt(struct platform_device *pdev)
0141 {
0142 struct device_node *parent;
0143 int num;
0144
0145 if (!pdev->dev.parent->of_node)
0146 return -ENODEV;
0147
0148 parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
0149 if (!parent)
0150 return -ENODEV;
0151
0152 num = of_get_child_count(parent);
0153 of_node_put(parent);
0154 return num;
0155 }
0156 EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt);
0157
0158 struct mc13xxx_regulator_init_data *mc13xxx_parse_regulators_dt(
0159 struct platform_device *pdev, struct mc13xxx_regulator *regulators,
0160 int num_regulators)
0161 {
0162 struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev);
0163 struct mc13xxx_regulator_init_data *data, *p;
0164 struct device_node *parent, *child;
0165 int i, parsed = 0;
0166
0167 if (!pdev->dev.parent->of_node)
0168 return NULL;
0169
0170 parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
0171 if (!parent)
0172 return NULL;
0173
0174 data = devm_kcalloc(&pdev->dev, priv->num_regulators, sizeof(*data),
0175 GFP_KERNEL);
0176 if (!data) {
0177 of_node_put(parent);
0178 return NULL;
0179 }
0180
0181 p = data;
0182
0183 for_each_child_of_node(parent, child) {
0184 int found = 0;
0185
0186 for (i = 0; i < num_regulators; i++) {
0187 if (!regulators[i].desc.name)
0188 continue;
0189 if (of_node_name_eq(child,
0190 regulators[i].desc.name)) {
0191 p->id = i;
0192 p->init_data = of_get_regulator_init_data(
0193 &pdev->dev, child,
0194 ®ulators[i].desc);
0195 p->node = child;
0196 p++;
0197
0198 parsed++;
0199 found = 1;
0200 break;
0201 }
0202 }
0203
0204 if (!found)
0205 dev_warn(&pdev->dev,
0206 "Unknown regulator: %pOFn\n", child);
0207 }
0208 of_node_put(parent);
0209
0210 priv->num_regulators = parsed;
0211
0212 return data;
0213 }
0214 EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt);
0215 #endif
0216
0217 MODULE_LICENSE("GPL v2");
0218 MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
0219 MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
0220 MODULE_ALIAS("mc13xxx-regulator-core");