Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/regulator/aat2870-regulator.c |
| 3 | * |
| 4 | * Copyright (c) 2011, NVIDIA Corporation. |
| 5 | * Author: Jin Park <jinyoungp@nvidia.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 | * 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/err.h> |
Randy Dunlap | 3a7d021 | 2011-07-30 06:13:09 +0200 | [diff] [blame] | 25 | #include <linux/module.h> |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/regulator/driver.h> |
| 29 | #include <linux/regulator/machine.h> |
| 30 | #include <linux/mfd/aat2870.h> |
| 31 | |
| 32 | struct aat2870_regulator { |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 33 | struct aat2870_data *aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 34 | struct regulator_desc desc; |
| 35 | |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 36 | u8 enable_addr; |
| 37 | u8 enable_shift; |
| 38 | u8 enable_mask; |
| 39 | |
| 40 | u8 voltage_addr; |
| 41 | u8 voltage_shift; |
| 42 | u8 voltage_mask; |
| 43 | }; |
| 44 | |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 45 | static int aat2870_ldo_set_voltage_sel(struct regulator_dev *rdev, |
| 46 | unsigned selector) |
| 47 | { |
| 48 | struct aat2870_regulator *ri = rdev_get_drvdata(rdev); |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 49 | struct aat2870_data *aat2870 = ri->aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 50 | |
| 51 | return aat2870->update(aat2870, ri->voltage_addr, ri->voltage_mask, |
Axel Lin | a5228d2 | 2011-11-28 14:07:48 +0800 | [diff] [blame] | 52 | selector << ri->voltage_shift); |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int aat2870_ldo_get_voltage_sel(struct regulator_dev *rdev) |
| 56 | { |
| 57 | struct aat2870_regulator *ri = rdev_get_drvdata(rdev); |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 58 | struct aat2870_data *aat2870 = ri->aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 59 | u8 val; |
| 60 | int ret; |
| 61 | |
| 62 | ret = aat2870->read(aat2870, ri->voltage_addr, &val); |
| 63 | if (ret) |
| 64 | return ret; |
| 65 | |
| 66 | return (val & ri->voltage_mask) >> ri->voltage_shift; |
| 67 | } |
| 68 | |
| 69 | static int aat2870_ldo_enable(struct regulator_dev *rdev) |
| 70 | { |
| 71 | struct aat2870_regulator *ri = rdev_get_drvdata(rdev); |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 72 | struct aat2870_data *aat2870 = ri->aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 73 | |
| 74 | return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask, |
| 75 | ri->enable_mask); |
| 76 | } |
| 77 | |
| 78 | static int aat2870_ldo_disable(struct regulator_dev *rdev) |
| 79 | { |
| 80 | struct aat2870_regulator *ri = rdev_get_drvdata(rdev); |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 81 | struct aat2870_data *aat2870 = ri->aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 82 | |
| 83 | return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask, 0); |
| 84 | } |
| 85 | |
| 86 | static int aat2870_ldo_is_enabled(struct regulator_dev *rdev) |
| 87 | { |
| 88 | struct aat2870_regulator *ri = rdev_get_drvdata(rdev); |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 89 | struct aat2870_data *aat2870 = ri->aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 90 | u8 val; |
| 91 | int ret; |
| 92 | |
| 93 | ret = aat2870->read(aat2870, ri->enable_addr, &val); |
| 94 | if (ret) |
| 95 | return ret; |
| 96 | |
| 97 | return val & ri->enable_mask ? 1 : 0; |
| 98 | } |
| 99 | |
| 100 | static struct regulator_ops aat2870_ldo_ops = { |
Axel Lin | 4506c6d | 2012-07-06 19:14:32 +0800 | [diff] [blame] | 101 | .list_voltage = regulator_list_voltage_table, |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 102 | .set_voltage_sel = aat2870_ldo_set_voltage_sel, |
| 103 | .get_voltage_sel = aat2870_ldo_get_voltage_sel, |
| 104 | .enable = aat2870_ldo_enable, |
| 105 | .disable = aat2870_ldo_disable, |
| 106 | .is_enabled = aat2870_ldo_is_enabled, |
| 107 | }; |
| 108 | |
Axel Lin | 4506c6d | 2012-07-06 19:14:32 +0800 | [diff] [blame] | 109 | static const unsigned int aat2870_ldo_voltages[] = { |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 110 | 1200000, 1300000, 1500000, 1600000, |
| 111 | 1800000, 2000000, 2200000, 2500000, |
| 112 | 2600000, 2700000, 2800000, 2900000, |
| 113 | 3000000, 3100000, 3200000, 3300000, |
| 114 | }; |
| 115 | |
| 116 | #define AAT2870_LDO(ids) \ |
| 117 | { \ |
| 118 | .desc = { \ |
| 119 | .name = #ids, \ |
| 120 | .id = AAT2870_ID_##ids, \ |
| 121 | .n_voltages = ARRAY_SIZE(aat2870_ldo_voltages), \ |
Axel Lin | 4506c6d | 2012-07-06 19:14:32 +0800 | [diff] [blame] | 122 | .volt_table = aat2870_ldo_voltages, \ |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 123 | .ops = &aat2870_ldo_ops, \ |
| 124 | .type = REGULATOR_VOLTAGE, \ |
| 125 | .owner = THIS_MODULE, \ |
| 126 | }, \ |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static struct aat2870_regulator aat2870_regulators[] = { |
| 130 | AAT2870_LDO(LDOA), |
| 131 | AAT2870_LDO(LDOB), |
| 132 | AAT2870_LDO(LDOC), |
| 133 | AAT2870_LDO(LDOD), |
| 134 | }; |
| 135 | |
| 136 | static struct aat2870_regulator *aat2870_get_regulator(int id) |
| 137 | { |
| 138 | struct aat2870_regulator *ri = NULL; |
| 139 | int i; |
| 140 | |
| 141 | for (i = 0; i < ARRAY_SIZE(aat2870_regulators); i++) { |
| 142 | ri = &aat2870_regulators[i]; |
| 143 | if (ri->desc.id == id) |
| 144 | break; |
| 145 | } |
| 146 | |
Axel Lin | d4d6373 | 2011-11-28 14:06:31 +0800 | [diff] [blame] | 147 | if (i == ARRAY_SIZE(aat2870_regulators)) |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 148 | return NULL; |
| 149 | |
| 150 | ri->enable_addr = AAT2870_LDO_EN; |
| 151 | ri->enable_shift = id - AAT2870_ID_LDOA; |
| 152 | ri->enable_mask = 0x1 << ri->enable_shift; |
| 153 | |
| 154 | ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ? |
| 155 | AAT2870_LDO_CD : AAT2870_LDO_AB; |
| 156 | ri->voltage_shift = (id - AAT2870_ID_LDOA) % 2 ? 0 : 4; |
| 157 | ri->voltage_mask = 0xF << ri->voltage_shift; |
| 158 | |
| 159 | return ri; |
| 160 | } |
| 161 | |
| 162 | static int aat2870_regulator_probe(struct platform_device *pdev) |
| 163 | { |
| 164 | struct aat2870_regulator *ri; |
Mark Brown | e4c5288 | 2012-09-10 12:07:23 +0800 | [diff] [blame] | 165 | struct regulator_config config = { }; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 166 | struct regulator_dev *rdev; |
| 167 | |
| 168 | ri = aat2870_get_regulator(pdev->id); |
| 169 | if (!ri) { |
| 170 | dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id); |
| 171 | return -EINVAL; |
| 172 | } |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 173 | ri->aat2870 = dev_get_drvdata(pdev->dev.parent); |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 174 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 175 | config.dev = &pdev->dev; |
| 176 | config.driver_data = ri; |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 177 | config.init_data = dev_get_platdata(&pdev->dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 178 | |
| 179 | rdev = regulator_register(&ri->desc, &config); |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 180 | if (IS_ERR(rdev)) { |
| 181 | dev_err(&pdev->dev, "Failed to register regulator %s\n", |
| 182 | ri->desc.name); |
| 183 | return PTR_ERR(rdev); |
| 184 | } |
| 185 | platform_set_drvdata(pdev, rdev); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame] | 190 | static int aat2870_regulator_remove(struct platform_device *pdev) |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 191 | { |
| 192 | struct regulator_dev *rdev = platform_get_drvdata(pdev); |
| 193 | |
| 194 | regulator_unregister(rdev); |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static struct platform_driver aat2870_regulator_driver = { |
| 199 | .driver = { |
| 200 | .name = "aat2870-regulator", |
| 201 | .owner = THIS_MODULE, |
| 202 | }, |
| 203 | .probe = aat2870_regulator_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 204 | .remove = aat2870_regulator_remove, |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static int __init aat2870_regulator_init(void) |
| 208 | { |
| 209 | return platform_driver_register(&aat2870_regulator_driver); |
| 210 | } |
| 211 | subsys_initcall(aat2870_regulator_init); |
| 212 | |
| 213 | static void __exit aat2870_regulator_exit(void) |
| 214 | { |
| 215 | platform_driver_unregister(&aat2870_regulator_driver); |
| 216 | } |
| 217 | module_exit(aat2870_regulator_exit); |
| 218 | |
| 219 | MODULE_DESCRIPTION("AnalogicTech AAT2870 Regulator"); |
| 220 | MODULE_LICENSE("GPL"); |
| 221 | MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>"); |
Mark Brown | ad9c5ff | 2012-03-14 17:44:56 +0000 | [diff] [blame] | 222 | MODULE_ALIAS("platform:aat2870-regulator"); |