Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * max77686.c - Regulator driver for the Maxim 77686 |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics |
| 5 | * Chiwoong Byun <woong.byun@smasung.com> |
| 6 | * Jonghwa Lee <jonghwa3.lee@samsung.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 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | * This driver is based on max8997.c |
| 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/bug.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/gpio.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/regulator/driver.h> |
| 33 | #include <linux/regulator/machine.h> |
Yadwinder Singh Brar | 4706fca | 2012-07-05 09:28:25 +0530 | [diff] [blame] | 34 | #include <linux/regulator/of_regulator.h> |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 35 | #include <linux/mfd/max77686.h> |
| 36 | #include <linux/mfd/max77686-private.h> |
| 37 | |
| 38 | #define MAX77686_LDO_MINUV 800000 |
| 39 | #define MAX77686_LDO_UVSTEP 50000 |
| 40 | #define MAX77686_LDO_LOW_MINUV 800000 |
| 41 | #define MAX77686_LDO_LOW_UVSTEP 25000 |
| 42 | #define MAX77686_BUCK_MINUV 750000 |
| 43 | #define MAX77686_BUCK_UVSTEP 50000 |
Yadwinder Singh Brar | 852abad | 2012-06-19 13:23:42 +0530 | [diff] [blame] | 44 | #define MAX77686_RAMP_DELAY 100000 /* uV/us */ |
| 45 | #define MAX77686_DVS_RAMP_DELAY 27500 /* uV/us */ |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 46 | #define MAX77686_DVS_MINUV 600000 |
| 47 | #define MAX77686_DVS_UVSTEP 12500 |
| 48 | |
| 49 | #define MAX77686_OPMODE_SHIFT 6 |
| 50 | #define MAX77686_OPMODE_BUCK234_SHIFT 4 |
| 51 | #define MAX77686_OPMODE_MASK 0x3 |
| 52 | |
| 53 | #define MAX77686_VSEL_MASK 0x3F |
| 54 | #define MAX77686_DVS_VSEL_MASK 0xFF |
| 55 | |
| 56 | #define MAX77686_RAMP_RATE_MASK 0xC0 |
| 57 | |
| 58 | #define MAX77686_REGULATORS MAX77686_REG_MAX |
| 59 | #define MAX77686_LDOS 26 |
| 60 | |
| 61 | enum max77686_ramp_rate { |
| 62 | RAMP_RATE_13P75MV, |
| 63 | RAMP_RATE_27P5MV, |
| 64 | RAMP_RATE_55MV, |
| 65 | RAMP_RATE_NO_CTRL, /* 100mV/us */ |
| 66 | }; |
| 67 | |
| 68 | struct max77686_data { |
Axel Lin | cb44cde | 2012-08-05 10:11:16 +0800 | [diff] [blame] | 69 | struct regulator_dev *rdev[MAX77686_REGULATORS]; |
Yadwinder Singh Brar | 6d2c896 | 2012-10-16 17:24:18 +0530 | [diff] [blame] | 70 | unsigned int opmode[MAX77686_REGULATORS]; |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 71 | }; |
| 72 | |
Yadwinder Singh Brar | 15282ba | 2012-10-16 17:24:19 +0530 | [diff] [blame] | 73 | /* Some BUCKS supports Normal[ON/OFF] mode during suspend */ |
| 74 | static int max77686_buck_set_suspend_disable(struct regulator_dev *rdev) |
| 75 | { |
| 76 | unsigned int val; |
| 77 | struct max77686_data *max77686 = rdev_get_drvdata(rdev); |
| 78 | |
| 79 | if (rdev->desc->id == MAX77686_BUCK1) |
| 80 | val = 0x1; |
| 81 | else |
| 82 | val = 0x1 << MAX77686_OPMODE_BUCK234_SHIFT; |
| 83 | |
| 84 | max77686->opmode[rdev->desc->id] = val; |
| 85 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 86 | rdev->desc->enable_mask, |
| 87 | val); |
| 88 | } |
| 89 | |
| 90 | /* Some LDOs supports [LPM/Normal]ON mode during suspend state */ |
| 91 | static int max77686_set_suspend_mode(struct regulator_dev *rdev, |
| 92 | unsigned int mode) |
| 93 | { |
| 94 | struct max77686_data *max77686 = rdev_get_drvdata(rdev); |
| 95 | unsigned int val; |
| 96 | |
| 97 | /* BUCK[5-9] doesn't support this feature */ |
| 98 | if (rdev->desc->id >= MAX77686_BUCK5) |
| 99 | return 0; |
| 100 | |
| 101 | switch (mode) { |
| 102 | case REGULATOR_MODE_IDLE: /* ON in LP Mode */ |
| 103 | val = 0x2 << MAX77686_OPMODE_SHIFT; |
| 104 | break; |
| 105 | case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */ |
| 106 | val = 0x3 << MAX77686_OPMODE_SHIFT; |
| 107 | break; |
| 108 | default: |
| 109 | pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n", |
| 110 | rdev->desc->name, mode); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
| 114 | max77686->opmode[rdev->desc->id] = val; |
| 115 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 116 | rdev->desc->enable_mask, |
| 117 | val); |
| 118 | } |
| 119 | |
| 120 | /* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */ |
| 121 | static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev, |
| 122 | unsigned int mode) |
| 123 | { |
| 124 | unsigned int val; |
| 125 | struct max77686_data *max77686 = rdev_get_drvdata(rdev); |
| 126 | |
| 127 | switch (mode) { |
| 128 | case REGULATOR_MODE_STANDBY: /* switch off */ |
| 129 | val = 0x1 << MAX77686_OPMODE_SHIFT; |
| 130 | break; |
| 131 | case REGULATOR_MODE_IDLE: /* ON in LP Mode */ |
| 132 | val = 0x2 << MAX77686_OPMODE_SHIFT; |
| 133 | break; |
| 134 | case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */ |
| 135 | val = 0x3 << MAX77686_OPMODE_SHIFT; |
| 136 | break; |
| 137 | default: |
| 138 | pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n", |
| 139 | rdev->desc->name, mode); |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | |
| 143 | max77686->opmode[rdev->desc->id] = val; |
| 144 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 145 | rdev->desc->enable_mask, |
| 146 | val); |
| 147 | } |
| 148 | |
Yadwinder Singh Brar | 38d3403 | 2012-10-22 09:39:28 +0530 | [diff] [blame] | 149 | static int max77686_enable(struct regulator_dev *rdev) |
Yadwinder Singh Brar | 6d2c896 | 2012-10-16 17:24:18 +0530 | [diff] [blame] | 150 | { |
| 151 | struct max77686_data *max77686 = rdev_get_drvdata(rdev); |
| 152 | |
| 153 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 154 | rdev->desc->enable_mask, |
| 155 | max77686->opmode[rdev->desc->id]); |
| 156 | } |
| 157 | |
Yadwinder Singh Brar | f503071 | 2012-06-20 10:50:51 +0530 | [diff] [blame] | 158 | static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay) |
| 159 | { |
| 160 | unsigned int ramp_value = RAMP_RATE_NO_CTRL; |
| 161 | |
| 162 | switch (ramp_delay) { |
| 163 | case 1 ... 13750: |
| 164 | ramp_value = RAMP_RATE_13P75MV; |
| 165 | break; |
| 166 | case 13751 ... 27500: |
| 167 | ramp_value = RAMP_RATE_27P5MV; |
| 168 | break; |
| 169 | case 27501 ... 55000: |
| 170 | ramp_value = RAMP_RATE_55MV; |
| 171 | break; |
| 172 | case 55001 ... 100000: |
| 173 | break; |
| 174 | default: |
| 175 | pr_warn("%s: ramp_delay: %d not supported, setting 100000\n", |
| 176 | rdev->desc->name, ramp_delay); |
| 177 | } |
| 178 | |
| 179 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 180 | MAX77686_RAMP_RATE_MASK, ramp_value << 6); |
| 181 | } |
| 182 | |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 183 | static struct regulator_ops max77686_ops = { |
| 184 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 2e3f7f2 | 2012-06-03 22:46:14 +0800 | [diff] [blame] | 185 | .map_voltage = regulator_map_voltage_linear, |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 186 | .is_enabled = regulator_is_enabled_regmap, |
Yadwinder Singh Brar | 6d2c896 | 2012-10-16 17:24:18 +0530 | [diff] [blame] | 187 | .enable = max77686_enable, |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 188 | .disable = regulator_disable_regmap, |
| 189 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 190 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Yadwinder Singh Brar | 852abad | 2012-06-19 13:23:42 +0530 | [diff] [blame] | 191 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
Yadwinder Singh Brar | 15282ba | 2012-10-16 17:24:19 +0530 | [diff] [blame] | 192 | .set_suspend_mode = max77686_set_suspend_mode, |
| 193 | }; |
| 194 | |
| 195 | static struct regulator_ops max77686_ldo_ops = { |
| 196 | .list_voltage = regulator_list_voltage_linear, |
| 197 | .map_voltage = regulator_map_voltage_linear, |
| 198 | .is_enabled = regulator_is_enabled_regmap, |
| 199 | .enable = max77686_enable, |
| 200 | .disable = regulator_disable_regmap, |
| 201 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 202 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 203 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
| 204 | .set_suspend_mode = max77686_ldo_set_suspend_mode, |
| 205 | }; |
| 206 | |
| 207 | static struct regulator_ops max77686_buck1_ops = { |
| 208 | .list_voltage = regulator_list_voltage_linear, |
| 209 | .map_voltage = regulator_map_voltage_linear, |
| 210 | .is_enabled = regulator_is_enabled_regmap, |
| 211 | .enable = max77686_enable, |
| 212 | .disable = regulator_disable_regmap, |
| 213 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 214 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 215 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
| 216 | .set_suspend_disable = max77686_buck_set_suspend_disable, |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | static struct regulator_ops max77686_buck_dvs_ops = { |
| 220 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 2e3f7f2 | 2012-06-03 22:46:14 +0800 | [diff] [blame] | 221 | .map_voltage = regulator_map_voltage_linear, |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 222 | .is_enabled = regulator_is_enabled_regmap, |
Yadwinder Singh Brar | 6d2c896 | 2012-10-16 17:24:18 +0530 | [diff] [blame] | 223 | .enable = max77686_enable, |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 224 | .disable = regulator_disable_regmap, |
| 225 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 226 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Yadwinder Singh Brar | 852abad | 2012-06-19 13:23:42 +0530 | [diff] [blame] | 227 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
Yadwinder Singh Brar | f503071 | 2012-06-20 10:50:51 +0530 | [diff] [blame] | 228 | .set_ramp_delay = max77686_set_ramp_delay, |
Yadwinder Singh Brar | 15282ba | 2012-10-16 17:24:19 +0530 | [diff] [blame] | 229 | .set_suspend_disable = max77686_buck_set_suspend_disable, |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | #define regulator_desc_ldo(num) { \ |
| 233 | .name = "LDO"#num, \ |
| 234 | .id = MAX77686_LDO##num, \ |
| 235 | .ops = &max77686_ops, \ |
| 236 | .type = REGULATOR_VOLTAGE, \ |
| 237 | .owner = THIS_MODULE, \ |
| 238 | .min_uV = MAX77686_LDO_MINUV, \ |
| 239 | .uV_step = MAX77686_LDO_UVSTEP, \ |
Yadwinder Singh Brar | 852abad | 2012-06-19 13:23:42 +0530 | [diff] [blame] | 240 | .ramp_delay = MAX77686_RAMP_DELAY, \ |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 241 | .n_voltages = MAX77686_VSEL_MASK + 1, \ |
| 242 | .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ |
| 243 | .vsel_mask = MAX77686_VSEL_MASK, \ |
| 244 | .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ |
| 245 | .enable_mask = MAX77686_OPMODE_MASK \ |
| 246 | << MAX77686_OPMODE_SHIFT, \ |
| 247 | } |
Yadwinder Singh Brar | 15282ba | 2012-10-16 17:24:19 +0530 | [diff] [blame] | 248 | #define regulator_desc_lpm_ldo(num) { \ |
| 249 | .name = "LDO"#num, \ |
| 250 | .id = MAX77686_LDO##num, \ |
| 251 | .ops = &max77686_ldo_ops, \ |
| 252 | .type = REGULATOR_VOLTAGE, \ |
| 253 | .owner = THIS_MODULE, \ |
| 254 | .min_uV = MAX77686_LDO_MINUV, \ |
| 255 | .uV_step = MAX77686_LDO_UVSTEP, \ |
| 256 | .ramp_delay = MAX77686_RAMP_DELAY, \ |
| 257 | .n_voltages = MAX77686_VSEL_MASK + 1, \ |
| 258 | .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ |
| 259 | .vsel_mask = MAX77686_VSEL_MASK, \ |
| 260 | .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ |
| 261 | .enable_mask = MAX77686_OPMODE_MASK \ |
| 262 | << MAX77686_OPMODE_SHIFT, \ |
| 263 | } |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 264 | #define regulator_desc_ldo_low(num) { \ |
| 265 | .name = "LDO"#num, \ |
| 266 | .id = MAX77686_LDO##num, \ |
Yadwinder Singh Brar | 15282ba | 2012-10-16 17:24:19 +0530 | [diff] [blame] | 267 | .ops = &max77686_ldo_ops, \ |
| 268 | .type = REGULATOR_VOLTAGE, \ |
| 269 | .owner = THIS_MODULE, \ |
| 270 | .min_uV = MAX77686_LDO_LOW_MINUV, \ |
| 271 | .uV_step = MAX77686_LDO_LOW_UVSTEP, \ |
| 272 | .ramp_delay = MAX77686_RAMP_DELAY, \ |
| 273 | .n_voltages = MAX77686_VSEL_MASK + 1, \ |
| 274 | .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ |
| 275 | .vsel_mask = MAX77686_VSEL_MASK, \ |
| 276 | .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ |
| 277 | .enable_mask = MAX77686_OPMODE_MASK \ |
| 278 | << MAX77686_OPMODE_SHIFT, \ |
| 279 | } |
| 280 | #define regulator_desc_ldo1_low(num) { \ |
| 281 | .name = "LDO"#num, \ |
| 282 | .id = MAX77686_LDO##num, \ |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 283 | .ops = &max77686_ops, \ |
| 284 | .type = REGULATOR_VOLTAGE, \ |
| 285 | .owner = THIS_MODULE, \ |
| 286 | .min_uV = MAX77686_LDO_LOW_MINUV, \ |
| 287 | .uV_step = MAX77686_LDO_LOW_UVSTEP, \ |
Yadwinder Singh Brar | 852abad | 2012-06-19 13:23:42 +0530 | [diff] [blame] | 288 | .ramp_delay = MAX77686_RAMP_DELAY, \ |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 289 | .n_voltages = MAX77686_VSEL_MASK + 1, \ |
| 290 | .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ |
| 291 | .vsel_mask = MAX77686_VSEL_MASK, \ |
| 292 | .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ |
| 293 | .enable_mask = MAX77686_OPMODE_MASK \ |
| 294 | << MAX77686_OPMODE_SHIFT, \ |
| 295 | } |
| 296 | #define regulator_desc_buck(num) { \ |
| 297 | .name = "BUCK"#num, \ |
| 298 | .id = MAX77686_BUCK##num, \ |
| 299 | .ops = &max77686_ops, \ |
| 300 | .type = REGULATOR_VOLTAGE, \ |
| 301 | .owner = THIS_MODULE, \ |
| 302 | .min_uV = MAX77686_BUCK_MINUV, \ |
| 303 | .uV_step = MAX77686_BUCK_UVSTEP, \ |
Yadwinder Singh Brar | 852abad | 2012-06-19 13:23:42 +0530 | [diff] [blame] | 304 | .ramp_delay = MAX77686_RAMP_DELAY, \ |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 305 | .n_voltages = MAX77686_VSEL_MASK + 1, \ |
| 306 | .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \ |
| 307 | .vsel_mask = MAX77686_VSEL_MASK, \ |
| 308 | .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \ |
| 309 | .enable_mask = MAX77686_OPMODE_MASK, \ |
| 310 | } |
| 311 | #define regulator_desc_buck1(num) { \ |
| 312 | .name = "BUCK"#num, \ |
| 313 | .id = MAX77686_BUCK##num, \ |
Yadwinder Singh Brar | 15282ba | 2012-10-16 17:24:19 +0530 | [diff] [blame] | 314 | .ops = &max77686_buck1_ops, \ |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 315 | .type = REGULATOR_VOLTAGE, \ |
| 316 | .owner = THIS_MODULE, \ |
| 317 | .min_uV = MAX77686_BUCK_MINUV, \ |
| 318 | .uV_step = MAX77686_BUCK_UVSTEP, \ |
Yadwinder Singh Brar | 852abad | 2012-06-19 13:23:42 +0530 | [diff] [blame] | 319 | .ramp_delay = MAX77686_RAMP_DELAY, \ |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 320 | .n_voltages = MAX77686_VSEL_MASK + 1, \ |
| 321 | .vsel_reg = MAX77686_REG_BUCK1OUT, \ |
| 322 | .vsel_mask = MAX77686_VSEL_MASK, \ |
| 323 | .enable_reg = MAX77686_REG_BUCK1CTRL, \ |
| 324 | .enable_mask = MAX77686_OPMODE_MASK, \ |
| 325 | } |
| 326 | #define regulator_desc_buck_dvs(num) { \ |
| 327 | .name = "BUCK"#num, \ |
| 328 | .id = MAX77686_BUCK##num, \ |
Axel Lin | 74adfee5 | 2012-06-04 10:19:18 +0800 | [diff] [blame] | 329 | .ops = &max77686_buck_dvs_ops, \ |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 330 | .type = REGULATOR_VOLTAGE, \ |
| 331 | .owner = THIS_MODULE, \ |
| 332 | .min_uV = MAX77686_DVS_MINUV, \ |
| 333 | .uV_step = MAX77686_DVS_UVSTEP, \ |
Yadwinder Singh Brar | 852abad | 2012-06-19 13:23:42 +0530 | [diff] [blame] | 334 | .ramp_delay = MAX77686_DVS_RAMP_DELAY, \ |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 335 | .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \ |
| 336 | .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \ |
| 337 | .vsel_mask = MAX77686_DVS_VSEL_MASK, \ |
| 338 | .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \ |
| 339 | .enable_mask = MAX77686_OPMODE_MASK \ |
| 340 | << MAX77686_OPMODE_BUCK234_SHIFT, \ |
| 341 | } |
| 342 | |
| 343 | static struct regulator_desc regulators[] = { |
Yadwinder Singh Brar | 15282ba | 2012-10-16 17:24:19 +0530 | [diff] [blame] | 344 | regulator_desc_ldo1_low(1), |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 345 | regulator_desc_ldo_low(2), |
| 346 | regulator_desc_ldo(3), |
| 347 | regulator_desc_ldo(4), |
| 348 | regulator_desc_ldo(5), |
| 349 | regulator_desc_ldo_low(6), |
| 350 | regulator_desc_ldo_low(7), |
| 351 | regulator_desc_ldo_low(8), |
| 352 | regulator_desc_ldo(9), |
Yadwinder Singh Brar | 15282ba | 2012-10-16 17:24:19 +0530 | [diff] [blame] | 353 | regulator_desc_lpm_ldo(10), |
| 354 | regulator_desc_lpm_ldo(11), |
| 355 | regulator_desc_lpm_ldo(12), |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 356 | regulator_desc_ldo(13), |
Yadwinder Singh Brar | 15282ba | 2012-10-16 17:24:19 +0530 | [diff] [blame] | 357 | regulator_desc_lpm_ldo(14), |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 358 | regulator_desc_ldo_low(15), |
Yadwinder Singh Brar | 15282ba | 2012-10-16 17:24:19 +0530 | [diff] [blame] | 359 | regulator_desc_lpm_ldo(16), |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 360 | regulator_desc_ldo(17), |
| 361 | regulator_desc_ldo(18), |
| 362 | regulator_desc_ldo(19), |
| 363 | regulator_desc_ldo(20), |
| 364 | regulator_desc_ldo(21), |
| 365 | regulator_desc_ldo(22), |
| 366 | regulator_desc_ldo(23), |
| 367 | regulator_desc_ldo(24), |
| 368 | regulator_desc_ldo(25), |
| 369 | regulator_desc_ldo(26), |
| 370 | regulator_desc_buck1(1), |
| 371 | regulator_desc_buck_dvs(2), |
| 372 | regulator_desc_buck_dvs(3), |
| 373 | regulator_desc_buck_dvs(4), |
| 374 | regulator_desc_buck(5), |
| 375 | regulator_desc_buck(6), |
| 376 | regulator_desc_buck(7), |
| 377 | regulator_desc_buck(8), |
| 378 | regulator_desc_buck(9), |
| 379 | }; |
| 380 | |
Yadwinder Singh Brar | 4706fca | 2012-07-05 09:28:25 +0530 | [diff] [blame] | 381 | #ifdef CONFIG_OF |
| 382 | static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev, |
| 383 | struct max77686_platform_data *pdata) |
| 384 | { |
| 385 | struct device_node *pmic_np, *regulators_np; |
| 386 | struct max77686_regulator_data *rdata; |
| 387 | struct of_regulator_match rmatch; |
| 388 | unsigned int i; |
| 389 | |
| 390 | pmic_np = iodev->dev->of_node; |
| 391 | regulators_np = of_find_node_by_name(pmic_np, "voltage-regulators"); |
| 392 | if (!regulators_np) { |
| 393 | dev_err(iodev->dev, "could not find regulators sub-node\n"); |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | |
| 397 | pdata->num_regulators = ARRAY_SIZE(regulators); |
| 398 | rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) * |
| 399 | pdata->num_regulators, GFP_KERNEL); |
| 400 | if (!rdata) { |
| 401 | dev_err(iodev->dev, |
| 402 | "could not allocate memory for regulator data\n"); |
| 403 | return -ENOMEM; |
| 404 | } |
| 405 | |
| 406 | for (i = 0; i < pdata->num_regulators; i++) { |
| 407 | rmatch.name = regulators[i].name; |
| 408 | rmatch.init_data = NULL; |
Yadwinder Singh Brar | d1ef065 | 2012-07-06 14:50:08 +0530 | [diff] [blame] | 409 | rmatch.of_node = NULL; |
Yadwinder Singh Brar | 4706fca | 2012-07-05 09:28:25 +0530 | [diff] [blame] | 410 | of_regulator_match(iodev->dev, regulators_np, &rmatch, 1); |
| 411 | rdata[i].initdata = rmatch.init_data; |
Axel Lin | 2c58e26 | 2012-08-05 10:09:57 +0800 | [diff] [blame] | 412 | rdata[i].of_node = rmatch.of_node; |
Yadwinder Singh Brar | 4706fca | 2012-07-05 09:28:25 +0530 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | pdata->regulators = rdata; |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | #else |
| 420 | static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev, |
| 421 | struct max77686_platform_data *pdata) |
| 422 | { |
| 423 | return 0; |
| 424 | } |
| 425 | #endif /* CONFIG_OF */ |
| 426 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 427 | static int max77686_pmic_probe(struct platform_device *pdev) |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 428 | { |
| 429 | struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent); |
| 430 | struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev); |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 431 | struct max77686_data *max77686; |
Axel Lin | cb44cde | 2012-08-05 10:11:16 +0800 | [diff] [blame] | 432 | int i, ret = 0; |
Axel Lin | c59d2a6 | 2012-06-20 15:01:25 +0800 | [diff] [blame] | 433 | struct regulator_config config = { }; |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 434 | |
| 435 | dev_dbg(&pdev->dev, "%s\n", __func__); |
| 436 | |
Yadwinder Singh Brar | 4706fca | 2012-07-05 09:28:25 +0530 | [diff] [blame] | 437 | if (!pdata) { |
| 438 | dev_err(&pdev->dev, "no platform data found for regulator\n"); |
| 439 | return -ENODEV; |
| 440 | } |
| 441 | |
| 442 | if (iodev->dev->of_node) { |
| 443 | ret = max77686_pmic_dt_parse_pdata(iodev, pdata); |
| 444 | if (ret) |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | if (pdata->num_regulators != MAX77686_REGULATORS) { |
Axel Lin | ab0d1cb | 2012-06-08 12:03:04 +0800 | [diff] [blame] | 449 | dev_err(&pdev->dev, |
| 450 | "Invalid initial data for regulator's initialiation\n"); |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 454 | max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data), |
| 455 | GFP_KERNEL); |
| 456 | if (!max77686) |
| 457 | return -ENOMEM; |
| 458 | |
Yadwinder Singh Brar | f503071 | 2012-06-20 10:50:51 +0530 | [diff] [blame] | 459 | config.dev = &pdev->dev; |
| 460 | config.regmap = iodev->regmap; |
Yadwinder Singh Brar | 6d2c896 | 2012-10-16 17:24:18 +0530 | [diff] [blame] | 461 | config.driver_data = max77686; |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 462 | platform_set_drvdata(pdev, max77686); |
| 463 | |
Axel Lin | ab0d1cb | 2012-06-08 12:03:04 +0800 | [diff] [blame] | 464 | for (i = 0; i < MAX77686_REGULATORS; i++) { |
Axel Lin | ab0d1cb | 2012-06-08 12:03:04 +0800 | [diff] [blame] | 465 | config.init_data = pdata->regulators[i].initdata; |
Axel Lin | 2c58e26 | 2012-08-05 10:09:57 +0800 | [diff] [blame] | 466 | config.of_node = pdata->regulators[i].of_node; |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 467 | |
Yadwinder Singh Brar | 6d2c896 | 2012-10-16 17:24:18 +0530 | [diff] [blame] | 468 | max77686->opmode[i] = regulators[i].enable_mask; |
Axel Lin | cb44cde | 2012-08-05 10:11:16 +0800 | [diff] [blame] | 469 | max77686->rdev[i] = regulator_register(®ulators[i], &config); |
| 470 | if (IS_ERR(max77686->rdev[i])) { |
| 471 | ret = PTR_ERR(max77686->rdev[i]); |
Yadwinder Singh Brar | f503071 | 2012-06-20 10:50:51 +0530 | [diff] [blame] | 472 | dev_err(&pdev->dev, |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 473 | "regulator init failed for %d\n", i); |
Axel Lin | cb44cde | 2012-08-05 10:11:16 +0800 | [diff] [blame] | 474 | max77686->rdev[i] = NULL; |
| 475 | goto err; |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 476 | } |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 477 | } |
Axel Lin | ab0d1cb | 2012-06-08 12:03:04 +0800 | [diff] [blame] | 478 | |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 479 | return 0; |
| 480 | err: |
Axel Lin | ab0d1cb | 2012-06-08 12:03:04 +0800 | [diff] [blame] | 481 | while (--i >= 0) |
Axel Lin | cb44cde | 2012-08-05 10:11:16 +0800 | [diff] [blame] | 482 | regulator_unregister(max77686->rdev[i]); |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 483 | return ret; |
| 484 | } |
| 485 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame] | 486 | static int max77686_pmic_remove(struct platform_device *pdev) |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 487 | { |
| 488 | struct max77686_data *max77686 = platform_get_drvdata(pdev); |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 489 | int i; |
| 490 | |
| 491 | for (i = 0; i < MAX77686_REGULATORS; i++) |
Axel Lin | cb44cde | 2012-08-05 10:11:16 +0800 | [diff] [blame] | 492 | regulator_unregister(max77686->rdev[i]); |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | static const struct platform_device_id max77686_pmic_id[] = { |
| 498 | {"max77686-pmic", 0}, |
| 499 | { }, |
| 500 | }; |
| 501 | MODULE_DEVICE_TABLE(platform, max77686_pmic_id); |
| 502 | |
| 503 | static struct platform_driver max77686_pmic_driver = { |
| 504 | .driver = { |
| 505 | .name = "max77686-pmic", |
| 506 | .owner = THIS_MODULE, |
| 507 | }, |
| 508 | .probe = max77686_pmic_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 509 | .remove = max77686_pmic_remove, |
Jonghwa Lee | 133d401 | 2012-06-01 13:17:14 +0900 | [diff] [blame] | 510 | .id_table = max77686_pmic_id, |
| 511 | }; |
| 512 | |
| 513 | static int __init max77686_pmic_init(void) |
| 514 | { |
| 515 | return platform_driver_register(&max77686_pmic_driver); |
| 516 | } |
| 517 | subsys_initcall(max77686_pmic_init); |
| 518 | |
| 519 | static void __exit max77686_pmic_cleanup(void) |
| 520 | { |
| 521 | platform_driver_unregister(&max77686_pmic_driver); |
| 522 | } |
| 523 | module_exit(max77686_pmic_cleanup); |
| 524 | |
| 525 | MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver"); |
| 526 | MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>"); |
| 527 | MODULE_LICENSE("GPL"); |