Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved. |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/module.h> |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 24 | #include <linux/mfd/syscon.h> |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 25 | #include <linux/err.h> |
| 26 | #include <linux/io.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/of.h> |
| 29 | #include <linux/of_address.h> |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 30 | #include <linux/regmap.h> |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 31 | #include <linux/regulator/driver.h> |
| 32 | #include <linux/regulator/of_regulator.h> |
| 33 | |
| 34 | struct anatop_regulator { |
| 35 | const char *name; |
| 36 | u32 control_reg; |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 37 | struct regmap *anatop; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 38 | int vol_bit_shift; |
| 39 | int vol_bit_width; |
| 40 | int min_bit_val; |
| 41 | int min_voltage; |
| 42 | int max_voltage; |
| 43 | struct regulator_desc rdesc; |
| 44 | struct regulator_init_data *initdata; |
| 45 | }; |
| 46 | |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 47 | static int anatop_regmap_set_voltage_sel(struct regulator_dev *reg, |
| 48 | unsigned selector) |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 49 | { |
| 50 | struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg); |
Axel Lin | d01c3a1 | 2012-06-03 23:02:34 +0800 | [diff] [blame] | 51 | u32 val, mask; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 52 | |
| 53 | if (!anatop_reg->control_reg) |
| 54 | return -ENOTSUPP; |
| 55 | |
Axel Lin | d01c3a1 | 2012-06-03 23:02:34 +0800 | [diff] [blame] | 56 | val = anatop_reg->min_bit_val + selector; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 57 | dev_dbg(®->dev, "%s: calculated val %d\n", __func__, val); |
Richard Zhao | b09530e | 2012-05-13 09:18:02 +0800 | [diff] [blame] | 58 | mask = ((1 << anatop_reg->vol_bit_width) - 1) << |
| 59 | anatop_reg->vol_bit_shift; |
| 60 | val <<= anatop_reg->vol_bit_shift; |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 61 | regmap_update_bits(anatop_reg->anatop, anatop_reg->control_reg, |
| 62 | mask, val); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 67 | static int anatop_regmap_get_voltage_sel(struct regulator_dev *reg) |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 68 | { |
| 69 | struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg); |
Axel Lin | 3e2a928 | 2012-07-17 00:10:42 +0800 | [diff] [blame] | 70 | u32 val, mask; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 71 | |
| 72 | if (!anatop_reg->control_reg) |
| 73 | return -ENOTSUPP; |
| 74 | |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 75 | regmap_read(anatop_reg->anatop, anatop_reg->control_reg, &val); |
Axel Lin | 3e2a928 | 2012-07-17 00:10:42 +0800 | [diff] [blame] | 76 | mask = ((1 << anatop_reg->vol_bit_width) - 1) << |
Richard Zhao | b09530e | 2012-05-13 09:18:02 +0800 | [diff] [blame] | 77 | anatop_reg->vol_bit_shift; |
Axel Lin | 3e2a928 | 2012-07-17 00:10:42 +0800 | [diff] [blame] | 78 | val = (val & mask) >> anatop_reg->vol_bit_shift; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 79 | |
| 80 | return val - anatop_reg->min_bit_val; |
| 81 | } |
| 82 | |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 83 | static struct regulator_ops anatop_rops = { |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 84 | .set_voltage_sel = anatop_regmap_set_voltage_sel, |
| 85 | .get_voltage_sel = anatop_regmap_get_voltage_sel, |
Axel Lin | d01c3a1 | 2012-06-03 23:02:34 +0800 | [diff] [blame] | 86 | .list_voltage = regulator_list_voltage_linear, |
| 87 | .map_voltage = regulator_map_voltage_linear, |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | static int __devinit anatop_regulator_probe(struct platform_device *pdev) |
| 91 | { |
| 92 | struct device *dev = &pdev->dev; |
| 93 | struct device_node *np = dev->of_node; |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 94 | struct device_node *anatop_np; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 95 | struct regulator_desc *rdesc; |
| 96 | struct regulator_dev *rdev; |
| 97 | struct anatop_regulator *sreg; |
| 98 | struct regulator_init_data *initdata; |
Axel Lin | d914d81 | 2012-04-10 22:45:01 +0800 | [diff] [blame] | 99 | struct regulator_config config = { }; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 100 | int ret = 0; |
| 101 | |
| 102 | initdata = of_get_regulator_init_data(dev, np); |
| 103 | sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL); |
| 104 | if (!sreg) |
| 105 | return -ENOMEM; |
| 106 | sreg->initdata = initdata; |
| 107 | sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL), |
| 108 | GFP_KERNEL); |
| 109 | rdesc = &sreg->rdesc; |
| 110 | memset(rdesc, 0, sizeof(*rdesc)); |
| 111 | rdesc->name = sreg->name; |
| 112 | rdesc->ops = &anatop_rops; |
| 113 | rdesc->type = REGULATOR_VOLTAGE; |
| 114 | rdesc->owner = THIS_MODULE; |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 115 | |
| 116 | anatop_np = of_get_parent(np); |
| 117 | if (!anatop_np) |
| 118 | return -ENODEV; |
| 119 | sreg->anatop = syscon_node_to_regmap(anatop_np); |
| 120 | of_node_put(anatop_np); |
| 121 | if (IS_ERR(sreg->anatop)) |
| 122 | return PTR_ERR(sreg->anatop); |
| 123 | |
Ying-Chun Liu (PaulLiu) | 2f2cc27 | 2012-03-27 15:54:01 +0800 | [diff] [blame] | 124 | ret = of_property_read_u32(np, "anatop-reg-offset", |
| 125 | &sreg->control_reg); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 126 | if (ret) { |
Ying-Chun Liu (PaulLiu) | 2f2cc27 | 2012-03-27 15:54:01 +0800 | [diff] [blame] | 127 | dev_err(dev, "no anatop-reg-offset property set\n"); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 128 | goto anatop_probe_end; |
| 129 | } |
| 130 | ret = of_property_read_u32(np, "anatop-vol-bit-width", |
| 131 | &sreg->vol_bit_width); |
| 132 | if (ret) { |
| 133 | dev_err(dev, "no anatop-vol-bit-width property set\n"); |
| 134 | goto anatop_probe_end; |
| 135 | } |
| 136 | ret = of_property_read_u32(np, "anatop-vol-bit-shift", |
| 137 | &sreg->vol_bit_shift); |
| 138 | if (ret) { |
| 139 | dev_err(dev, "no anatop-vol-bit-shift property set\n"); |
| 140 | goto anatop_probe_end; |
| 141 | } |
| 142 | ret = of_property_read_u32(np, "anatop-min-bit-val", |
| 143 | &sreg->min_bit_val); |
| 144 | if (ret) { |
| 145 | dev_err(dev, "no anatop-min-bit-val property set\n"); |
| 146 | goto anatop_probe_end; |
| 147 | } |
| 148 | ret = of_property_read_u32(np, "anatop-min-voltage", |
| 149 | &sreg->min_voltage); |
| 150 | if (ret) { |
| 151 | dev_err(dev, "no anatop-min-voltage property set\n"); |
| 152 | goto anatop_probe_end; |
| 153 | } |
| 154 | ret = of_property_read_u32(np, "anatop-max-voltage", |
| 155 | &sreg->max_voltage); |
| 156 | if (ret) { |
| 157 | dev_err(dev, "no anatop-max-voltage property set\n"); |
| 158 | goto anatop_probe_end; |
| 159 | } |
| 160 | |
| 161 | rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage) |
| 162 | / 25000 + 1; |
Axel Lin | 0713e6a | 2012-05-14 11:06:44 +0800 | [diff] [blame] | 163 | rdesc->min_uV = sreg->min_voltage; |
| 164 | rdesc->uV_step = 25000; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 165 | |
Axel Lin | d914d81 | 2012-04-10 22:45:01 +0800 | [diff] [blame] | 166 | config.dev = &pdev->dev; |
| 167 | config.init_data = initdata; |
| 168 | config.driver_data = sreg; |
| 169 | config.of_node = pdev->dev.of_node; |
| 170 | |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 171 | /* register regulator */ |
Axel Lin | d914d81 | 2012-04-10 22:45:01 +0800 | [diff] [blame] | 172 | rdev = regulator_register(rdesc, &config); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 173 | if (IS_ERR(rdev)) { |
| 174 | dev_err(dev, "failed to register %s\n", |
| 175 | rdesc->name); |
| 176 | ret = PTR_ERR(rdev); |
| 177 | goto anatop_probe_end; |
| 178 | } |
| 179 | |
| 180 | platform_set_drvdata(pdev, rdev); |
| 181 | |
| 182 | anatop_probe_end: |
| 183 | if (ret) |
| 184 | kfree(sreg->name); |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static int __devexit anatop_regulator_remove(struct platform_device *pdev) |
| 190 | { |
| 191 | struct regulator_dev *rdev = platform_get_drvdata(pdev); |
| 192 | struct anatop_regulator *sreg = rdev_get_drvdata(rdev); |
| 193 | const char *name = sreg->name; |
| 194 | |
| 195 | regulator_unregister(rdev); |
| 196 | kfree(name); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static struct of_device_id __devinitdata of_anatop_regulator_match_tbl[] = { |
| 202 | { .compatible = "fsl,anatop-regulator", }, |
| 203 | { /* end */ } |
| 204 | }; |
| 205 | |
Shawn Guo | c0d78c2 | 2012-04-02 14:57:01 +0800 | [diff] [blame] | 206 | static struct platform_driver anatop_regulator_driver = { |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 207 | .driver = { |
| 208 | .name = "anatop_regulator", |
| 209 | .owner = THIS_MODULE, |
| 210 | .of_match_table = of_anatop_regulator_match_tbl, |
| 211 | }, |
| 212 | .probe = anatop_regulator_probe, |
Axel Lin | 0c09d31 | 2012-05-23 09:08:08 +0800 | [diff] [blame] | 213 | .remove = __devexit_p(anatop_regulator_remove), |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | static int __init anatop_regulator_init(void) |
| 217 | { |
Shawn Guo | c0d78c2 | 2012-04-02 14:57:01 +0800 | [diff] [blame] | 218 | return platform_driver_register(&anatop_regulator_driver); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 219 | } |
| 220 | postcore_initcall(anatop_regulator_init); |
| 221 | |
| 222 | static void __exit anatop_regulator_exit(void) |
| 223 | { |
Shawn Guo | c0d78c2 | 2012-04-02 14:57:01 +0800 | [diff] [blame] | 224 | platform_driver_unregister(&anatop_regulator_driver); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 225 | } |
| 226 | module_exit(anatop_regulator_exit); |
| 227 | |
| 228 | MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, " |
| 229 | "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>"); |
| 230 | MODULE_DESCRIPTION("ANATOP Regulator driver"); |
| 231 | MODULE_LICENSE("GPL v2"); |