Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 1 | /* |
| 2 | * da9052-regulator.c: Regulator driver for DA9052 |
| 3 | * |
| 4 | * Copyright(c) 2011 Dialog Semiconductor Ltd. |
| 5 | * |
| 6 | * Author: David Dajun Chen <dchen@diasemi.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/moduleparam.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/regulator/driver.h> |
| 21 | #include <linux/regulator/machine.h> |
Ying-Chun Liu (PaulLiu) | 88c84c1 | 2012-04-13 21:37:41 +0800 | [diff] [blame] | 22 | #ifdef CONFIG_OF |
Mark Brown | cd22a96 | 2012-04-16 09:54:24 +0100 | [diff] [blame] | 23 | #include <linux/of.h> |
Ying-Chun Liu (PaulLiu) | 88c84c1 | 2012-04-13 21:37:41 +0800 | [diff] [blame] | 24 | #include <linux/regulator/of_regulator.h> |
| 25 | #endif |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 26 | |
| 27 | #include <linux/mfd/da9052/da9052.h> |
| 28 | #include <linux/mfd/da9052/reg.h> |
| 29 | #include <linux/mfd/da9052/pdata.h> |
| 30 | |
| 31 | /* Buck step size */ |
| 32 | #define DA9052_BUCK_PERI_3uV_STEP 100000 |
| 33 | #define DA9052_BUCK_PERI_REG_MAP_UPTO_3uV 24 |
| 34 | #define DA9052_CONST_3uV 3000000 |
| 35 | |
| 36 | #define DA9052_MIN_UA 0 |
| 37 | #define DA9052_MAX_UA 3 |
| 38 | #define DA9052_CURRENT_RANGE 4 |
| 39 | |
| 40 | /* Bit masks */ |
| 41 | #define DA9052_BUCK_ILIM_MASK_EVEN 0x0c |
| 42 | #define DA9052_BUCK_ILIM_MASK_ODD 0xc0 |
| 43 | |
Axel Lin | 9210f05 | 2012-03-15 19:58:39 +0800 | [diff] [blame] | 44 | /* DA9052 REGULATOR IDs */ |
| 45 | #define DA9052_ID_BUCK1 0 |
| 46 | #define DA9052_ID_BUCK2 1 |
| 47 | #define DA9052_ID_BUCK3 2 |
| 48 | #define DA9052_ID_BUCK4 3 |
| 49 | #define DA9052_ID_LDO1 4 |
| 50 | #define DA9052_ID_LDO2 5 |
| 51 | #define DA9052_ID_LDO3 6 |
| 52 | #define DA9052_ID_LDO4 7 |
| 53 | #define DA9052_ID_LDO5 8 |
| 54 | #define DA9052_ID_LDO6 9 |
| 55 | #define DA9052_ID_LDO7 10 |
| 56 | #define DA9052_ID_LDO8 11 |
| 57 | #define DA9052_ID_LDO9 12 |
| 58 | #define DA9052_ID_LDO10 13 |
| 59 | |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 60 | static const u32 da9052_current_limits[3][4] = { |
| 61 | {700000, 800000, 1000000, 1200000}, /* DA9052-BC BUCKs */ |
| 62 | {1600000, 2000000, 2400000, 3000000}, /* DA9053-AA/Bx BUCK-CORE */ |
| 63 | {800000, 1000000, 1200000, 1500000}, /* DA9053-AA/Bx BUCK-PRO, |
| 64 | * BUCK-MEM and BUCK-PERI |
| 65 | */ |
| 66 | }; |
| 67 | |
| 68 | struct da9052_regulator_info { |
| 69 | struct regulator_desc reg_desc; |
| 70 | int step_uV; |
| 71 | int min_uV; |
| 72 | int max_uV; |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | struct da9052_regulator { |
| 76 | struct da9052 *da9052; |
| 77 | struct da9052_regulator_info *info; |
| 78 | struct regulator_dev *rdev; |
| 79 | }; |
| 80 | |
| 81 | static int verify_range(struct da9052_regulator_info *info, |
| 82 | int min_uV, int max_uV) |
| 83 | { |
| 84 | if (min_uV > info->max_uV || max_uV < info->min_uV) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 90 | static int da9052_dcdc_get_current_limit(struct regulator_dev *rdev) |
| 91 | { |
| 92 | struct da9052_regulator *regulator = rdev_get_drvdata(rdev); |
| 93 | int offset = rdev_get_id(rdev); |
| 94 | int ret, row = 2; |
| 95 | |
| 96 | ret = da9052_reg_read(regulator->da9052, DA9052_BUCKA_REG + offset/2); |
| 97 | if (ret < 0) |
| 98 | return ret; |
| 99 | |
| 100 | /* Determine the even or odd position of the buck current limit |
| 101 | * register field |
| 102 | */ |
| 103 | if (offset % 2 == 0) |
| 104 | ret = (ret & DA9052_BUCK_ILIM_MASK_EVEN) >> 2; |
| 105 | else |
| 106 | ret = (ret & DA9052_BUCK_ILIM_MASK_ODD) >> 6; |
| 107 | |
| 108 | /* Select the appropriate current limit range */ |
| 109 | if (regulator->da9052->chip_id == DA9052) |
| 110 | row = 0; |
| 111 | else if (offset == 0) |
| 112 | row = 1; |
| 113 | |
| 114 | return da9052_current_limits[row][ret]; |
| 115 | } |
| 116 | |
| 117 | static int da9052_dcdc_set_current_limit(struct regulator_dev *rdev, int min_uA, |
| 118 | int max_uA) |
| 119 | { |
| 120 | struct da9052_regulator *regulator = rdev_get_drvdata(rdev); |
| 121 | int offset = rdev_get_id(rdev); |
| 122 | int reg_val = 0; |
| 123 | int i, row = 2; |
| 124 | |
| 125 | /* Select the appropriate current limit range */ |
| 126 | if (regulator->da9052->chip_id == DA9052) |
| 127 | row = 0; |
| 128 | else if (offset == 0) |
| 129 | row = 1; |
| 130 | |
Axel Lin | 19d23c2 | 2012-08-08 20:23:54 +0800 | [diff] [blame] | 131 | for (i = DA9052_CURRENT_RANGE - 1; i >= 0; i--) { |
Axel Lin | 1e369bc | 2012-11-26 15:23:38 +0800 | [diff] [blame] | 132 | if ((min_uA <= da9052_current_limits[row][i]) && |
| 133 | (da9052_current_limits[row][i] <= max_uA)) { |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 134 | reg_val = i; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
Axel Lin | 1e369bc | 2012-11-26 15:23:38 +0800 | [diff] [blame] | 139 | if (i < 0) |
| 140 | return -EINVAL; |
| 141 | |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 142 | /* Determine the even or odd position of the buck current limit |
| 143 | * register field |
| 144 | */ |
| 145 | if (offset % 2 == 0) |
| 146 | return da9052_reg_update(regulator->da9052, |
| 147 | DA9052_BUCKA_REG + offset/2, |
| 148 | DA9052_BUCK_ILIM_MASK_EVEN, |
| 149 | reg_val << 2); |
| 150 | else |
| 151 | return da9052_reg_update(regulator->da9052, |
| 152 | DA9052_BUCKA_REG + offset/2, |
| 153 | DA9052_BUCK_ILIM_MASK_ODD, |
| 154 | reg_val << 6); |
| 155 | } |
| 156 | |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 157 | static int da9052_list_voltage(struct regulator_dev *rdev, |
| 158 | unsigned int selector) |
| 159 | { |
| 160 | struct da9052_regulator *regulator = rdev_get_drvdata(rdev); |
| 161 | struct da9052_regulator_info *info = regulator->info; |
Axel Lin | 0ec446e | 2012-03-15 20:00:07 +0800 | [diff] [blame] | 162 | int id = rdev_get_id(rdev); |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 163 | int volt_uV; |
| 164 | |
Axel Lin | 0ec446e | 2012-03-15 20:00:07 +0800 | [diff] [blame] | 165 | if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052) |
| 166 | && (selector >= DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)) { |
| 167 | volt_uV = ((DA9052_BUCK_PERI_REG_MAP_UPTO_3uV * info->step_uV) |
| 168 | + info->min_uV); |
| 169 | volt_uV += (selector - DA9052_BUCK_PERI_REG_MAP_UPTO_3uV) |
| 170 | * (DA9052_BUCK_PERI_3uV_STEP); |
| 171 | } else { |
| 172 | volt_uV = (selector * info->step_uV) + info->min_uV; |
| 173 | } |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 174 | |
| 175 | if (volt_uV > info->max_uV) |
| 176 | return -EINVAL; |
| 177 | |
| 178 | return volt_uV; |
| 179 | } |
| 180 | |
Axel Lin | 4923b48b | 2012-05-16 13:12:35 +0800 | [diff] [blame] | 181 | static int da9052_map_voltage(struct regulator_dev *rdev, |
| 182 | int min_uV, int max_uV) |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 183 | { |
| 184 | struct da9052_regulator *regulator = rdev_get_drvdata(rdev); |
| 185 | struct da9052_regulator_info *info = regulator->info; |
Axel Lin | 0ec446e | 2012-03-15 20:00:07 +0800 | [diff] [blame] | 186 | int id = rdev_get_id(rdev); |
Axel Lin | 4923b48b | 2012-05-16 13:12:35 +0800 | [diff] [blame] | 187 | int ret, sel; |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 188 | |
| 189 | ret = verify_range(info, min_uV, max_uV); |
| 190 | if (ret < 0) |
| 191 | return ret; |
| 192 | |
| 193 | if (min_uV < info->min_uV) |
| 194 | min_uV = info->min_uV; |
| 195 | |
Axel Lin | 0ec446e | 2012-03-15 20:00:07 +0800 | [diff] [blame] | 196 | if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052) |
| 197 | && (min_uV >= DA9052_CONST_3uV)) { |
Axel Lin | 4923b48b | 2012-05-16 13:12:35 +0800 | [diff] [blame] | 198 | sel = DA9052_BUCK_PERI_REG_MAP_UPTO_3uV + |
| 199 | DIV_ROUND_UP(min_uV - DA9052_CONST_3uV, |
| 200 | DA9052_BUCK_PERI_3uV_STEP); |
Axel Lin | 0ec446e | 2012-03-15 20:00:07 +0800 | [diff] [blame] | 201 | } else { |
Axel Lin | 4923b48b | 2012-05-16 13:12:35 +0800 | [diff] [blame] | 202 | sel = DIV_ROUND_UP(min_uV - info->min_uV, info->step_uV); |
Axel Lin | 0ec446e | 2012-03-15 20:00:07 +0800 | [diff] [blame] | 203 | } |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 204 | |
Axel Lin | 4923b48b | 2012-05-16 13:12:35 +0800 | [diff] [blame] | 205 | ret = da9052_list_voltage(rdev, sel); |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 206 | if (ret < 0) |
| 207 | return ret; |
| 208 | |
Axel Lin | 4923b48b | 2012-05-16 13:12:35 +0800 | [diff] [blame] | 209 | return sel; |
| 210 | } |
| 211 | |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 212 | static struct regulator_ops da9052_dcdc_ops = { |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 213 | .get_current_limit = da9052_dcdc_get_current_limit, |
| 214 | .set_current_limit = da9052_dcdc_set_current_limit, |
| 215 | |
| 216 | .list_voltage = da9052_list_voltage, |
Axel Lin | 4923b48b | 2012-05-16 13:12:35 +0800 | [diff] [blame] | 217 | .map_voltage = da9052_map_voltage, |
Axel Lin | 09812bc | 2012-04-24 09:54:38 +0800 | [diff] [blame] | 218 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
Axel Lin | 68f7506 | 2012-12-18 09:32:52 +0800 | [diff] [blame] | 219 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Axel Lin | 0d481f7 | 2012-04-17 11:34:32 +0800 | [diff] [blame] | 220 | .is_enabled = regulator_is_enabled_regmap, |
| 221 | .enable = regulator_enable_regmap, |
| 222 | .disable = regulator_disable_regmap, |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 223 | }; |
| 224 | |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 225 | static struct regulator_ops da9052_ldo_ops = { |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 226 | .list_voltage = da9052_list_voltage, |
Axel Lin | 4923b48b | 2012-05-16 13:12:35 +0800 | [diff] [blame] | 227 | .map_voltage = da9052_map_voltage, |
Axel Lin | 09812bc | 2012-04-24 09:54:38 +0800 | [diff] [blame] | 228 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
Axel Lin | 68f7506 | 2012-12-18 09:32:52 +0800 | [diff] [blame] | 229 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Axel Lin | 0d481f7 | 2012-04-17 11:34:32 +0800 | [diff] [blame] | 230 | .is_enabled = regulator_is_enabled_regmap, |
| 231 | .enable = regulator_enable_regmap, |
| 232 | .disable = regulator_disable_regmap, |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 233 | }; |
| 234 | |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 235 | #define DA9052_LDO(_id, step, min, max, sbits, ebits, abits) \ |
| 236 | {\ |
| 237 | .reg_desc = {\ |
Axel Lin | 9210f05 | 2012-03-15 19:58:39 +0800 | [diff] [blame] | 238 | .name = #_id,\ |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 239 | .ops = &da9052_ldo_ops,\ |
| 240 | .type = REGULATOR_VOLTAGE,\ |
Axel Lin | 9210f05 | 2012-03-15 19:58:39 +0800 | [diff] [blame] | 241 | .id = DA9052_ID_##_id,\ |
Axel Lin | 7b95765 | 2012-03-05 20:04:32 +0800 | [diff] [blame] | 242 | .n_voltages = (max - min) / step + 1, \ |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 243 | .owner = THIS_MODULE,\ |
Axel Lin | 09812bc | 2012-04-24 09:54:38 +0800 | [diff] [blame] | 244 | .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \ |
| 245 | .vsel_mask = (1 << (sbits)) - 1,\ |
Axel Lin | 68f7506 | 2012-12-18 09:32:52 +0800 | [diff] [blame] | 246 | .apply_reg = DA9052_SUPPLY_REG, \ |
| 247 | .apply_bit = (abits), \ |
Axel Lin | 0d481f7 | 2012-04-17 11:34:32 +0800 | [diff] [blame] | 248 | .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \ |
| 249 | .enable_mask = 1 << (ebits),\ |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 250 | },\ |
| 251 | .min_uV = (min) * 1000,\ |
| 252 | .max_uV = (max) * 1000,\ |
| 253 | .step_uV = (step) * 1000,\ |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | #define DA9052_DCDC(_id, step, min, max, sbits, ebits, abits) \ |
| 257 | {\ |
| 258 | .reg_desc = {\ |
Axel Lin | 9210f05 | 2012-03-15 19:58:39 +0800 | [diff] [blame] | 259 | .name = #_id,\ |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 260 | .ops = &da9052_dcdc_ops,\ |
| 261 | .type = REGULATOR_VOLTAGE,\ |
Axel Lin | 9210f05 | 2012-03-15 19:58:39 +0800 | [diff] [blame] | 262 | .id = DA9052_ID_##_id,\ |
Axel Lin | 7b95765 | 2012-03-05 20:04:32 +0800 | [diff] [blame] | 263 | .n_voltages = (max - min) / step + 1, \ |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 264 | .owner = THIS_MODULE,\ |
Axel Lin | 09812bc | 2012-04-24 09:54:38 +0800 | [diff] [blame] | 265 | .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \ |
| 266 | .vsel_mask = (1 << (sbits)) - 1,\ |
Axel Lin | 68f7506 | 2012-12-18 09:32:52 +0800 | [diff] [blame] | 267 | .apply_reg = DA9052_SUPPLY_REG, \ |
| 268 | .apply_bit = (abits), \ |
Axel Lin | 0d481f7 | 2012-04-17 11:34:32 +0800 | [diff] [blame] | 269 | .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \ |
| 270 | .enable_mask = 1 << (ebits),\ |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 271 | },\ |
| 272 | .min_uV = (min) * 1000,\ |
| 273 | .max_uV = (max) * 1000,\ |
| 274 | .step_uV = (step) * 1000,\ |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 275 | } |
| 276 | |
Axel Lin | 6242eae | 2011-12-20 17:12:00 +0800 | [diff] [blame] | 277 | static struct da9052_regulator_info da9052_regulator_info[] = { |
Axel Lin | 9210f05 | 2012-03-15 19:58:39 +0800 | [diff] [blame] | 278 | DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO), |
| 279 | DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO), |
| 280 | DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO), |
Axel Lin | 0ec446e | 2012-03-15 20:00:07 +0800 | [diff] [blame] | 281 | DA9052_DCDC(BUCK4, 50, 1800, 3600, 5, 6, 0), |
Axel Lin | 9210f05 | 2012-03-15 19:58:39 +0800 | [diff] [blame] | 282 | DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0), |
Axel Lin | 0ec446e | 2012-03-15 20:00:07 +0800 | [diff] [blame] | 283 | DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO), |
| 284 | DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO), |
Axel Lin | 9210f05 | 2012-03-15 19:58:39 +0800 | [diff] [blame] | 285 | DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0), |
| 286 | DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0), |
| 287 | DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0), |
| 288 | DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0), |
| 289 | DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0), |
| 290 | DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0), |
| 291 | DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0), |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 292 | }; |
| 293 | |
Axel Lin | 6242eae | 2011-12-20 17:12:00 +0800 | [diff] [blame] | 294 | static struct da9052_regulator_info da9053_regulator_info[] = { |
Axel Lin | 9210f05 | 2012-03-15 19:58:39 +0800 | [diff] [blame] | 295 | DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO), |
| 296 | DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO), |
| 297 | DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO), |
Axel Lin | 0ec446e | 2012-03-15 20:00:07 +0800 | [diff] [blame] | 298 | DA9052_DCDC(BUCK4, 25, 925, 2500, 6, 6, 0), |
Axel Lin | 9210f05 | 2012-03-15 19:58:39 +0800 | [diff] [blame] | 299 | DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0), |
Axel Lin | 0ec446e | 2012-03-15 20:00:07 +0800 | [diff] [blame] | 300 | DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO), |
| 301 | DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO), |
Axel Lin | 9210f05 | 2012-03-15 19:58:39 +0800 | [diff] [blame] | 302 | DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0), |
| 303 | DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0), |
| 304 | DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0), |
| 305 | DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0), |
| 306 | DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0), |
| 307 | DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0), |
| 308 | DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0), |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | static inline struct da9052_regulator_info *find_regulator_info(u8 chip_id, |
| 312 | int id) |
| 313 | { |
| 314 | struct da9052_regulator_info *info; |
| 315 | int i; |
| 316 | |
Ashish Jangam | 984b5a6 | 2011-12-15 18:59:53 +0530 | [diff] [blame] | 317 | switch (chip_id) { |
| 318 | case DA9052: |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 319 | for (i = 0; i < ARRAY_SIZE(da9052_regulator_info); i++) { |
| 320 | info = &da9052_regulator_info[i]; |
| 321 | if (info->reg_desc.id == id) |
| 322 | return info; |
| 323 | } |
Ashish Jangam | 984b5a6 | 2011-12-15 18:59:53 +0530 | [diff] [blame] | 324 | break; |
| 325 | case DA9053_AA: |
| 326 | case DA9053_BA: |
| 327 | case DA9053_BB: |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 328 | for (i = 0; i < ARRAY_SIZE(da9053_regulator_info); i++) { |
| 329 | info = &da9053_regulator_info[i]; |
| 330 | if (info->reg_desc.id == id) |
| 331 | return info; |
| 332 | } |
Ashish Jangam | 984b5a6 | 2011-12-15 18:59:53 +0530 | [diff] [blame] | 333 | break; |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | return NULL; |
| 337 | } |
| 338 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 339 | static int da9052_regulator_probe(struct platform_device *pdev) |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 340 | { |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 341 | struct regulator_config config = { }; |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 342 | struct da9052_regulator *regulator; |
| 343 | struct da9052 *da9052; |
| 344 | struct da9052_pdata *pdata; |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 345 | |
Ashish Jangam | 984b5a6 | 2011-12-15 18:59:53 +0530 | [diff] [blame] | 346 | regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9052_regulator), |
| 347 | GFP_KERNEL); |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 348 | if (!regulator) |
| 349 | return -ENOMEM; |
| 350 | |
| 351 | da9052 = dev_get_drvdata(pdev->dev.parent); |
| 352 | pdata = da9052->dev->platform_data; |
| 353 | regulator->da9052 = da9052; |
| 354 | |
| 355 | regulator->info = find_regulator_info(regulator->da9052->chip_id, |
| 356 | pdev->id); |
| 357 | if (regulator->info == NULL) { |
| 358 | dev_err(&pdev->dev, "invalid regulator ID specified\n"); |
Axel Lin | 7eb6444 | 2012-04-05 12:08:58 +0800 | [diff] [blame] | 359 | return -EINVAL; |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 360 | } |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 361 | |
| 362 | config.dev = &pdev->dev; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 363 | config.driver_data = regulator; |
Axel Lin | 0d481f7 | 2012-04-17 11:34:32 +0800 | [diff] [blame] | 364 | config.regmap = da9052->regmap; |
Ying-Chun Liu (PaulLiu) | 88c84c1 | 2012-04-13 21:37:41 +0800 | [diff] [blame] | 365 | if (pdata && pdata->regulators) { |
| 366 | config.init_data = pdata->regulators[pdev->id]; |
| 367 | } else { |
| 368 | #ifdef CONFIG_OF |
Axel Lin | c92f5dd | 2013-01-27 21:16:56 +0800 | [diff] [blame] | 369 | struct device_node *nproot, *np; |
Ying-Chun Liu (PaulLiu) | 88c84c1 | 2012-04-13 21:37:41 +0800 | [diff] [blame] | 370 | |
Axel Lin | c92f5dd | 2013-01-27 21:16:56 +0800 | [diff] [blame] | 371 | nproot = of_node_get(da9052->dev->of_node); |
Ying-Chun Liu (PaulLiu) | 88c84c1 | 2012-04-13 21:37:41 +0800 | [diff] [blame] | 372 | if (!nproot) |
| 373 | return -ENODEV; |
| 374 | |
| 375 | nproot = of_find_node_by_name(nproot, "regulators"); |
| 376 | if (!nproot) |
| 377 | return -ENODEV; |
| 378 | |
Axel Lin | b86bbdd | 2012-07-07 09:46:45 +0800 | [diff] [blame] | 379 | for_each_child_of_node(nproot, np) { |
Ying-Chun Liu (PaulLiu) | 88c84c1 | 2012-04-13 21:37:41 +0800 | [diff] [blame] | 380 | if (!of_node_cmp(np->name, |
| 381 | regulator->info->reg_desc.name)) { |
| 382 | config.init_data = of_get_regulator_init_data( |
| 383 | &pdev->dev, np); |
Axel Lin | e76b9cc | 2012-07-12 22:23:25 +0800 | [diff] [blame] | 384 | config.of_node = np; |
Ying-Chun Liu (PaulLiu) | 88c84c1 | 2012-04-13 21:37:41 +0800 | [diff] [blame] | 385 | break; |
| 386 | } |
| 387 | } |
Axel Lin | c92f5dd | 2013-01-27 21:16:56 +0800 | [diff] [blame] | 388 | of_node_put(nproot); |
Ying-Chun Liu (PaulLiu) | 88c84c1 | 2012-04-13 21:37:41 +0800 | [diff] [blame] | 389 | #endif |
| 390 | } |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 391 | |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 392 | regulator->rdev = regulator_register(®ulator->info->reg_desc, |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 393 | &config); |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 394 | if (IS_ERR(regulator->rdev)) { |
| 395 | dev_err(&pdev->dev, "failed to register regulator %s\n", |
| 396 | regulator->info->reg_desc.name); |
Axel Lin | 7eb6444 | 2012-04-05 12:08:58 +0800 | [diff] [blame] | 397 | return PTR_ERR(regulator->rdev); |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | platform_set_drvdata(pdev, regulator); |
| 401 | |
| 402 | return 0; |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 403 | } |
| 404 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame] | 405 | static int da9052_regulator_remove(struct platform_device *pdev) |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 406 | { |
| 407 | struct da9052_regulator *regulator = platform_get_drvdata(pdev); |
| 408 | |
| 409 | regulator_unregister(regulator->rdev); |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static struct platform_driver da9052_regulator_driver = { |
| 414 | .probe = da9052_regulator_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 415 | .remove = da9052_regulator_remove, |
Ashish Jangam | 08bf1c0 | 2011-12-09 19:48:20 +0530 | [diff] [blame] | 416 | .driver = { |
| 417 | .name = "da9052-regulator", |
| 418 | .owner = THIS_MODULE, |
| 419 | }, |
| 420 | }; |
| 421 | |
| 422 | static int __init da9052_regulator_init(void) |
| 423 | { |
| 424 | return platform_driver_register(&da9052_regulator_driver); |
| 425 | } |
| 426 | subsys_initcall(da9052_regulator_init); |
| 427 | |
| 428 | static void __exit da9052_regulator_exit(void) |
| 429 | { |
| 430 | platform_driver_unregister(&da9052_regulator_driver); |
| 431 | } |
| 432 | module_exit(da9052_regulator_exit); |
| 433 | |
| 434 | MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); |
| 435 | MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9052 PMIC"); |
| 436 | MODULE_LICENSE("GPL"); |
| 437 | MODULE_ALIAS("platform:da9052-regulator"); |