Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Regulator Driver for Freescale MC13xxx PMIC |
| 3 | * |
| 4 | * Copyright 2010 Yong Shen <yong.shen@linaro.org> |
| 5 | * |
| 6 | * Based on mc13783 regulator driver : |
| 7 | * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> |
| 8 | * Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * Regs infos taken from mc13xxx drivers from freescale and mc13xxx.pdf file |
| 15 | * from freescale |
| 16 | */ |
| 17 | |
| 18 | #include <linux/mfd/mc13xxx.h> |
| 19 | #include <linux/regulator/machine.h> |
| 20 | #include <linux/regulator/driver.h> |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 21 | #include <linux/regulator/of_regulator.h> |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/err.h> |
Paul Gortmaker | 65602c3 | 2011-07-17 16:28:23 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 28 | #include <linux/of.h> |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 29 | #include "mc13xxx.h" |
| 30 | |
| 31 | static int mc13xxx_regulator_enable(struct regulator_dev *rdev) |
| 32 | { |
| 33 | struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); |
| 34 | struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; |
| 35 | int id = rdev_get_id(rdev); |
| 36 | int ret; |
| 37 | |
| 38 | dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); |
| 39 | |
| 40 | mc13xxx_lock(priv->mc13xxx); |
| 41 | ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg, |
| 42 | mc13xxx_regulators[id].enable_bit, |
| 43 | mc13xxx_regulators[id].enable_bit); |
| 44 | mc13xxx_unlock(priv->mc13xxx); |
| 45 | |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | static int mc13xxx_regulator_disable(struct regulator_dev *rdev) |
| 50 | { |
| 51 | struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); |
| 52 | struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; |
| 53 | int id = rdev_get_id(rdev); |
| 54 | int ret; |
| 55 | |
| 56 | dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); |
| 57 | |
| 58 | mc13xxx_lock(priv->mc13xxx); |
| 59 | ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg, |
| 60 | mc13xxx_regulators[id].enable_bit, 0); |
| 61 | mc13xxx_unlock(priv->mc13xxx); |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev) |
| 67 | { |
| 68 | struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); |
| 69 | struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; |
| 70 | int ret, id = rdev_get_id(rdev); |
| 71 | unsigned int val; |
| 72 | |
| 73 | mc13xxx_lock(priv->mc13xxx); |
| 74 | ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val); |
| 75 | mc13xxx_unlock(priv->mc13xxx); |
| 76 | |
| 77 | if (ret) |
| 78 | return ret; |
| 79 | |
| 80 | return (val & mc13xxx_regulators[id].enable_bit) != 0; |
| 81 | } |
| 82 | |
Axel Lin | 939b62d | 2012-03-20 10:46:35 +0800 | [diff] [blame] | 83 | static int mc13xxx_regulator_set_voltage_sel(struct regulator_dev *rdev, |
| 84 | unsigned selector) |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 85 | { |
| 86 | struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); |
| 87 | struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; |
Axel Lin | 939b62d | 2012-03-20 10:46:35 +0800 | [diff] [blame] | 88 | int id = rdev_get_id(rdev); |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 89 | int ret; |
| 90 | |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 91 | mc13xxx_lock(priv->mc13xxx); |
| 92 | ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg, |
| 93 | mc13xxx_regulators[id].vsel_mask, |
Axel Lin | 939b62d | 2012-03-20 10:46:35 +0800 | [diff] [blame] | 94 | selector << mc13xxx_regulators[id].vsel_shift); |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 95 | mc13xxx_unlock(priv->mc13xxx); |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev) |
| 101 | { |
| 102 | struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); |
| 103 | struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; |
| 104 | int ret, id = rdev_get_id(rdev); |
| 105 | unsigned int val; |
| 106 | |
| 107 | dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); |
| 108 | |
| 109 | mc13xxx_lock(priv->mc13xxx); |
| 110 | ret = mc13xxx_reg_read(priv->mc13xxx, |
| 111 | mc13xxx_regulators[id].vsel_reg, &val); |
| 112 | mc13xxx_unlock(priv->mc13xxx); |
| 113 | |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | |
| 117 | val = (val & mc13xxx_regulators[id].vsel_mask) |
| 118 | >> mc13xxx_regulators[id].vsel_shift; |
| 119 | |
| 120 | dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val); |
| 121 | |
Axel Lin | 3c24019 | 2011-05-18 20:56:45 +0800 | [diff] [blame] | 122 | BUG_ON(val >= mc13xxx_regulators[id].desc.n_voltages); |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 123 | |
Axel Lin | 34e74f3 | 2012-06-08 15:41:48 +0800 | [diff] [blame] | 124 | return rdev->desc->volt_table[val]; |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | struct regulator_ops mc13xxx_regulator_ops = { |
| 128 | .enable = mc13xxx_regulator_enable, |
| 129 | .disable = mc13xxx_regulator_disable, |
| 130 | .is_enabled = mc13xxx_regulator_is_enabled, |
Axel Lin | 34e74f3 | 2012-06-08 15:41:48 +0800 | [diff] [blame] | 131 | .list_voltage = regulator_list_voltage_table, |
Axel Lin | 939b62d | 2012-03-20 10:46:35 +0800 | [diff] [blame] | 132 | .set_voltage_sel = mc13xxx_regulator_set_voltage_sel, |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 133 | .get_voltage = mc13xxx_regulator_get_voltage, |
| 134 | }; |
Mark Brown | 4d7071f | 2010-12-15 14:10:25 +0000 | [diff] [blame] | 135 | EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops); |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 136 | |
| 137 | int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV, |
| 138 | int max_uV, unsigned *selector) |
| 139 | { |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 140 | int id = rdev_get_id(rdev); |
| 141 | |
| 142 | dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n", |
| 143 | __func__, id, min_uV, max_uV); |
| 144 | |
Axel Lin | 34e74f3 | 2012-06-08 15:41:48 +0800 | [diff] [blame] | 145 | if (min_uV <= rdev->desc->volt_table[0] && |
Axel Lin | 13407ea | 2012-07-13 23:01:14 +0800 | [diff] [blame] | 146 | rdev->desc->volt_table[0] <= max_uV) { |
| 147 | *selector = 0; |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 148 | return 0; |
Axel Lin | 13407ea | 2012-07-13 23:01:14 +0800 | [diff] [blame] | 149 | } else { |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 150 | return -EINVAL; |
Axel Lin | 13407ea | 2012-07-13 23:01:14 +0800 | [diff] [blame] | 151 | } |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 152 | } |
Mark Brown | 4d7071f | 2010-12-15 14:10:25 +0000 | [diff] [blame] | 153 | EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage); |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 154 | |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 155 | struct regulator_ops mc13xxx_fixed_regulator_ops = { |
| 156 | .enable = mc13xxx_regulator_enable, |
| 157 | .disable = mc13xxx_regulator_disable, |
| 158 | .is_enabled = mc13xxx_regulator_is_enabled, |
Axel Lin | 34e74f3 | 2012-06-08 15:41:48 +0800 | [diff] [blame] | 159 | .list_voltage = regulator_list_voltage_table, |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 160 | .set_voltage = mc13xxx_fixed_regulator_set_voltage, |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 161 | }; |
Mark Brown | 4d7071f | 2010-12-15 14:10:25 +0000 | [diff] [blame] | 162 | EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops); |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 163 | |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 164 | #ifdef CONFIG_OF |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 165 | int mc13xxx_get_num_regulators_dt(struct platform_device *pdev) |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 166 | { |
Axel Lin | 86f6673 | 2013-01-30 20:54:49 +0800 | [diff] [blame] | 167 | struct device_node *parent; |
| 168 | int num; |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 169 | |
Alexander Shiyan | b431e69 | 2014-03-02 11:44:35 +0400 | [diff] [blame] | 170 | if (!pdev->dev.parent->of_node) |
Sachin Kamat | bf7f882 | 2014-02-25 16:49:27 +0530 | [diff] [blame] | 171 | return -ENODEV; |
| 172 | |
Alexander Shiyan | b431e69 | 2014-03-02 11:44:35 +0400 | [diff] [blame] | 173 | parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators"); |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 174 | if (!parent) |
| 175 | return -ENODEV; |
| 176 | |
Axel Lin | 86f6673 | 2013-01-30 20:54:49 +0800 | [diff] [blame] | 177 | num = of_get_child_count(parent); |
Axel Lin | c92f5dd | 2013-01-27 21:16:56 +0800 | [diff] [blame] | 178 | of_node_put(parent); |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 179 | return num; |
| 180 | } |
David Miller | 5326916 | 2012-02-09 16:43:01 -0500 | [diff] [blame] | 181 | EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt); |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 182 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 183 | struct mc13xxx_regulator_init_data *mc13xxx_parse_regulators_dt( |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 184 | struct platform_device *pdev, struct mc13xxx_regulator *regulators, |
Alexander Shiyan | eb0d8e7 | 2013-04-27 10:29:24 +0400 | [diff] [blame] | 185 | int num_regulators) |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 186 | { |
| 187 | struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev); |
| 188 | struct mc13xxx_regulator_init_data *data, *p; |
| 189 | struct device_node *parent, *child; |
Matt Sealey | 2c8a5dc | 2013-01-21 12:25:45 -0600 | [diff] [blame] | 190 | int i, parsed = 0; |
| 191 | |
Alexander Shiyan | b431e69 | 2014-03-02 11:44:35 +0400 | [diff] [blame] | 192 | if (!pdev->dev.parent->of_node) |
Sachin Kamat | bf7f882 | 2014-02-25 16:49:27 +0530 | [diff] [blame] | 193 | return NULL; |
| 194 | |
Alexander Shiyan | b431e69 | 2014-03-02 11:44:35 +0400 | [diff] [blame] | 195 | parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators"); |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 196 | if (!parent) |
| 197 | return NULL; |
| 198 | |
| 199 | data = devm_kzalloc(&pdev->dev, sizeof(*data) * priv->num_regulators, |
| 200 | GFP_KERNEL); |
Axel Lin | c92f5dd | 2013-01-27 21:16:56 +0800 | [diff] [blame] | 201 | if (!data) { |
| 202 | of_node_put(parent); |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 203 | return NULL; |
Axel Lin | c92f5dd | 2013-01-27 21:16:56 +0800 | [diff] [blame] | 204 | } |
| 205 | |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 206 | p = data; |
| 207 | |
| 208 | for_each_child_of_node(parent, child) { |
Alexander Shiyan | eb0d8e7 | 2013-04-27 10:29:24 +0400 | [diff] [blame] | 209 | int found = 0; |
| 210 | |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 211 | for (i = 0; i < num_regulators; i++) { |
Alexander Shiyan | eb0d8e7 | 2013-04-27 10:29:24 +0400 | [diff] [blame] | 212 | if (!regulators[i].desc.name) |
| 213 | continue; |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 214 | if (!of_node_cmp(child->name, |
| 215 | regulators[i].desc.name)) { |
| 216 | p->id = i; |
| 217 | p->init_data = of_get_regulator_init_data( |
| 218 | &pdev->dev, child); |
| 219 | p->node = child; |
| 220 | p++; |
Matt Sealey | 2c8a5dc | 2013-01-21 12:25:45 -0600 | [diff] [blame] | 221 | |
| 222 | parsed++; |
Alexander Shiyan | eb0d8e7 | 2013-04-27 10:29:24 +0400 | [diff] [blame] | 223 | found = 1; |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | } |
Alexander Shiyan | eb0d8e7 | 2013-04-27 10:29:24 +0400 | [diff] [blame] | 227 | |
| 228 | if (!found) |
| 229 | dev_warn(&pdev->dev, |
| 230 | "Unknown regulator: %s\n", child->name); |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 231 | } |
Axel Lin | c92f5dd | 2013-01-27 21:16:56 +0800 | [diff] [blame] | 232 | of_node_put(parent); |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 233 | |
Alexander Shiyan | eb0d8e7 | 2013-04-27 10:29:24 +0400 | [diff] [blame] | 234 | priv->num_regulators = parsed; |
| 235 | |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 236 | return data; |
| 237 | } |
David Miller | 5326916 | 2012-02-09 16:43:01 -0500 | [diff] [blame] | 238 | EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt); |
Shawn Guo | 93bcb23 | 2011-12-21 23:00:46 +0800 | [diff] [blame] | 239 | #endif |
| 240 | |
Yong Shen | 167e3d8 | 2010-12-14 14:00:54 +0800 | [diff] [blame] | 241 | MODULE_LICENSE("GPL v2"); |
| 242 | MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>"); |
| 243 | MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC"); |
| 244 | MODULE_ALIAS("mc13xxx-regulator-core"); |