Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * max77802.c - Regulator driver for the Maxim 77802 |
| 3 | * |
| 4 | * Copyright (C) 2013-2014 Google, Inc |
| 5 | * Simon Glass <sjg@chromium.org> |
| 6 | * |
| 7 | * Copyright (C) 2012 Samsung Electronics |
| 8 | * Chiwoong Byun <woong.byun@smasung.com> |
| 9 | * Jonghwa Lee <jonghwa3.lee@samsung.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * This driver is based on max8997.c |
| 22 | */ |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/bug.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/gpio.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/gpio/consumer.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/regulator/driver.h> |
| 32 | #include <linux/regulator/machine.h> |
| 33 | #include <linux/regulator/of_regulator.h> |
| 34 | #include <linux/mfd/max77686.h> |
| 35 | #include <linux/mfd/max77686-private.h> |
| 36 | |
| 37 | /* Default ramp delay in case it is not manually set */ |
| 38 | #define MAX77802_RAMP_DELAY 100000 /* uV/us */ |
| 39 | |
| 40 | #define MAX77802_OPMODE_SHIFT_LDO 6 |
| 41 | #define MAX77802_OPMODE_BUCK234_SHIFT 4 |
| 42 | #define MAX77802_OPMODE_MASK 0x3 |
| 43 | |
| 44 | #define MAX77802_VSEL_MASK 0x3F |
| 45 | #define MAX77802_DVS_VSEL_MASK 0xFF |
| 46 | |
| 47 | #define MAX77802_RAMP_RATE_MASK_2BIT 0xC0 |
| 48 | #define MAX77802_RAMP_RATE_SHIFT_2BIT 6 |
| 49 | #define MAX77802_RAMP_RATE_MASK_4BIT 0xF0 |
| 50 | #define MAX77802_RAMP_RATE_SHIFT_4BIT 4 |
| 51 | |
Javier Martinez Canillas | e6fb2aa | 2014-10-15 18:20:31 +0200 | [diff] [blame^] | 52 | #define MAX77802_OFF_PWRREQ 0x1 |
| 53 | |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 54 | /* MAX77802 has two register formats: 2-bit and 4-bit */ |
| 55 | static const unsigned int ramp_table_77802_2bit[] = { |
| 56 | 12500, |
| 57 | 25000, |
| 58 | 50000, |
| 59 | 100000, |
| 60 | }; |
| 61 | |
| 62 | static unsigned int ramp_table_77802_4bit[] = { |
| 63 | 1000, 2000, 3030, 4000, |
| 64 | 5000, 5880, 7140, 8330, |
| 65 | 9090, 10000, 11110, 12500, |
| 66 | 16670, 25000, 50000, 100000, |
| 67 | }; |
| 68 | |
| 69 | struct max77802_regulator_prv { |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 70 | unsigned int opmode[MAX77802_REG_MAX]; |
| 71 | }; |
| 72 | |
| 73 | static int max77802_get_opmode_shift(int id) |
| 74 | { |
| 75 | if (id == MAX77802_BUCK1 || (id >= MAX77802_BUCK5 && |
| 76 | id <= MAX77802_BUCK10)) |
| 77 | return 0; |
| 78 | |
| 79 | if (id >= MAX77802_BUCK2 && id <= MAX77802_BUCK4) |
| 80 | return MAX77802_OPMODE_BUCK234_SHIFT; |
| 81 | |
| 82 | if (id >= MAX77802_LDO1 && id <= MAX77802_LDO35) |
| 83 | return MAX77802_OPMODE_SHIFT_LDO; |
| 84 | |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | |
Javier Martinez Canillas | e6fb2aa | 2014-10-15 18:20:31 +0200 | [diff] [blame^] | 88 | /** |
| 89 | * max77802_set_suspend_disable - Disable the regulator during system suspend |
| 90 | * @rdev: regulator to mark as disabled |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 91 | * |
Javier Martinez Canillas | e6fb2aa | 2014-10-15 18:20:31 +0200 | [diff] [blame^] | 92 | * All regulators expect LDO 1, 3, 20 and 21 support OFF by PWRREQ. |
| 93 | * Configure the regulator so the PMIC will turn it OFF during system suspend. |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 94 | */ |
Javier Martinez Canillas | e6fb2aa | 2014-10-15 18:20:31 +0200 | [diff] [blame^] | 95 | static int max77802_set_suspend_disable(struct regulator_dev *rdev) |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 96 | { |
Javier Martinez Canillas | e6fb2aa | 2014-10-15 18:20:31 +0200 | [diff] [blame^] | 97 | unsigned int val = MAX77802_OFF_PWRREQ; |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 98 | struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev); |
| 99 | int id = rdev_get_id(rdev); |
| 100 | int shift = max77802_get_opmode_shift(id); |
| 101 | |
| 102 | max77802->opmode[id] = val; |
| 103 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 104 | rdev->desc->enable_mask, val << shift); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state |
| 109 | * (Enable Control Logic1 by PWRREQ) |
| 110 | * |
| 111 | * LDOs 2, 4-19, 22-35. |
| 112 | * |
| 113 | */ |
| 114 | static int max77802_ldo_set_suspend_mode_logic1(struct regulator_dev *rdev, |
| 115 | unsigned int mode) |
| 116 | { |
| 117 | struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev); |
| 118 | int id = rdev_get_id(rdev); |
| 119 | unsigned int val; |
| 120 | int shift = max77802_get_opmode_shift(id); |
| 121 | |
| 122 | switch (mode) { |
| 123 | case REGULATOR_MODE_IDLE: /* ON in LP Mode */ |
| 124 | val = MAX77802_OPMODE_LP; |
| 125 | break; |
| 126 | case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */ |
| 127 | val = MAX77802_OPMODE_NORMAL; |
| 128 | break; |
| 129 | case REGULATOR_MODE_STANDBY: /* ON/OFF by PWRREQ */ |
| 130 | val = MAX77802_OPMODE_STANDBY; |
| 131 | break; |
| 132 | default: |
| 133 | dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n", |
| 134 | rdev->desc->name, mode); |
| 135 | return -EINVAL; |
| 136 | } |
| 137 | |
Axel Lin | da0bb55 | 2014-09-05 09:17:41 +0800 | [diff] [blame] | 138 | max77802->opmode[id] = val; |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 139 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 140 | rdev->desc->enable_mask, val << shift); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Mode 1 (Output[ON/OFF] by PWRREQ) is not supported on some LDOs |
| 145 | * (Enable Control Logic2 by PWRREQ) |
| 146 | * |
| 147 | * LDOs 1, 20, 21, and 3, |
| 148 | * |
| 149 | */ |
| 150 | static int max77802_ldo_set_suspend_mode_logic2(struct regulator_dev *rdev, |
| 151 | unsigned int mode) |
| 152 | { |
| 153 | struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev); |
| 154 | int id = rdev_get_id(rdev); |
| 155 | unsigned int val; |
| 156 | int shift = max77802_get_opmode_shift(id); |
| 157 | |
| 158 | switch (mode) { |
| 159 | case REGULATOR_MODE_IDLE: /* ON in LP Mode */ |
| 160 | val = MAX77802_OPMODE_LP; |
| 161 | break; |
| 162 | case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */ |
| 163 | val = MAX77802_OPMODE_NORMAL; |
| 164 | break; |
| 165 | default: |
| 166 | dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n", |
| 167 | rdev->desc->name, mode); |
| 168 | return -EINVAL; |
| 169 | } |
| 170 | |
Axel Lin | da0bb55 | 2014-09-05 09:17:41 +0800 | [diff] [blame] | 171 | max77802->opmode[id] = val; |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 172 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 173 | rdev->desc->enable_mask, val << shift); |
| 174 | } |
| 175 | |
| 176 | static int max77802_enable(struct regulator_dev *rdev) |
| 177 | { |
| 178 | struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev); |
| 179 | int id = rdev_get_id(rdev); |
| 180 | int shift = max77802_get_opmode_shift(id); |
| 181 | |
Javier Martinez Canillas | e6fb2aa | 2014-10-15 18:20:31 +0200 | [diff] [blame^] | 182 | if (max77802->opmode[id] == MAX77802_OFF_PWRREQ) |
| 183 | max77802->opmode[id] = MAX77802_OPMODE_NORMAL; |
| 184 | |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 185 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 186 | rdev->desc->enable_mask, |
| 187 | max77802->opmode[id] << shift); |
| 188 | } |
| 189 | |
| 190 | static int max77802_find_ramp_value(struct regulator_dev *rdev, |
| 191 | const unsigned int limits[], int size, |
| 192 | unsigned int ramp_delay) |
| 193 | { |
| 194 | int i; |
| 195 | |
| 196 | for (i = 0; i < size; i++) { |
| 197 | if (ramp_delay <= limits[i]) |
| 198 | return i; |
| 199 | } |
| 200 | |
| 201 | /* Use maximum value for no ramp control */ |
| 202 | dev_warn(&rdev->dev, "%s: ramp_delay: %d not supported, setting 100000\n", |
| 203 | rdev->desc->name, ramp_delay); |
| 204 | return size - 1; |
| 205 | } |
| 206 | |
| 207 | /* Used for BUCKs 2-4 */ |
| 208 | static int max77802_set_ramp_delay_2bit(struct regulator_dev *rdev, |
| 209 | int ramp_delay) |
| 210 | { |
| 211 | int id = rdev_get_id(rdev); |
| 212 | unsigned int ramp_value; |
| 213 | |
| 214 | if (id > MAX77802_BUCK4) { |
| 215 | dev_warn(&rdev->dev, |
| 216 | "%s: regulator: ramp delay not supported\n", |
| 217 | rdev->desc->name); |
| 218 | return -EINVAL; |
| 219 | } |
| 220 | ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_2bit, |
| 221 | ARRAY_SIZE(ramp_table_77802_2bit), ramp_delay); |
| 222 | |
| 223 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 224 | MAX77802_RAMP_RATE_MASK_2BIT, |
| 225 | ramp_value << MAX77802_RAMP_RATE_SHIFT_2BIT); |
| 226 | } |
| 227 | |
| 228 | /* For BUCK1, 6 */ |
| 229 | static int max77802_set_ramp_delay_4bit(struct regulator_dev *rdev, |
| 230 | int ramp_delay) |
| 231 | { |
| 232 | unsigned int ramp_value; |
| 233 | |
| 234 | ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_4bit, |
| 235 | ARRAY_SIZE(ramp_table_77802_4bit), ramp_delay); |
| 236 | |
| 237 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 238 | MAX77802_RAMP_RATE_MASK_4BIT, |
| 239 | ramp_value << MAX77802_RAMP_RATE_SHIFT_4BIT); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * LDOs 2, 4-19, 22-35 |
| 244 | */ |
| 245 | static struct regulator_ops max77802_ldo_ops_logic1 = { |
| 246 | .list_voltage = regulator_list_voltage_linear, |
| 247 | .map_voltage = regulator_map_voltage_linear, |
| 248 | .is_enabled = regulator_is_enabled_regmap, |
| 249 | .enable = max77802_enable, |
| 250 | .disable = regulator_disable_regmap, |
| 251 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 252 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 253 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
Javier Martinez Canillas | e6fb2aa | 2014-10-15 18:20:31 +0200 | [diff] [blame^] | 254 | .set_suspend_enable = max77802_enable, |
| 255 | .set_suspend_disable = max77802_set_suspend_disable, |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 256 | .set_suspend_mode = max77802_ldo_set_suspend_mode_logic1, |
| 257 | }; |
| 258 | |
| 259 | /* |
| 260 | * LDOs 1, 20, 21, 3 |
| 261 | */ |
| 262 | static struct regulator_ops max77802_ldo_ops_logic2 = { |
| 263 | .list_voltage = regulator_list_voltage_linear, |
| 264 | .map_voltage = regulator_map_voltage_linear, |
| 265 | .is_enabled = regulator_is_enabled_regmap, |
| 266 | .enable = max77802_enable, |
| 267 | .disable = regulator_disable_regmap, |
| 268 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 269 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 270 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
| 271 | .set_suspend_mode = max77802_ldo_set_suspend_mode_logic2, |
| 272 | }; |
| 273 | |
| 274 | /* BUCKS 1, 6 */ |
| 275 | static struct regulator_ops max77802_buck_16_dvs_ops = { |
| 276 | .list_voltage = regulator_list_voltage_linear, |
| 277 | .map_voltage = regulator_map_voltage_linear, |
| 278 | .is_enabled = regulator_is_enabled_regmap, |
| 279 | .enable = max77802_enable, |
| 280 | .disable = regulator_disable_regmap, |
| 281 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 282 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 283 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
| 284 | .set_ramp_delay = max77802_set_ramp_delay_4bit, |
Javier Martinez Canillas | e6fb2aa | 2014-10-15 18:20:31 +0200 | [diff] [blame^] | 285 | .set_suspend_enable = max77802_enable, |
| 286 | .set_suspend_disable = max77802_set_suspend_disable, |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | /* BUCKs 2-4, 5, 7-10 */ |
| 290 | static struct regulator_ops max77802_buck_dvs_ops = { |
| 291 | .list_voltage = regulator_list_voltage_linear, |
| 292 | .map_voltage = regulator_map_voltage_linear, |
| 293 | .is_enabled = regulator_is_enabled_regmap, |
| 294 | .enable = max77802_enable, |
| 295 | .disable = regulator_disable_regmap, |
| 296 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 297 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 298 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
| 299 | .set_ramp_delay = max77802_set_ramp_delay_2bit, |
Javier Martinez Canillas | e6fb2aa | 2014-10-15 18:20:31 +0200 | [diff] [blame^] | 300 | .set_suspend_enable = max77802_enable, |
| 301 | .set_suspend_disable = max77802_set_suspend_disable, |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | /* LDOs 3-7, 9-14, 18-26, 28, 29, 32-34 */ |
| 305 | #define regulator_77802_desc_p_ldo(num, supply, log) { \ |
| 306 | .name = "LDO"#num, \ |
| 307 | .id = MAX77802_LDO##num, \ |
| 308 | .supply_name = "inl"#supply, \ |
| 309 | .ops = &max77802_ldo_ops_logic##log, \ |
| 310 | .type = REGULATOR_VOLTAGE, \ |
| 311 | .owner = THIS_MODULE, \ |
| 312 | .min_uV = 800000, \ |
| 313 | .uV_step = 50000, \ |
| 314 | .ramp_delay = MAX77802_RAMP_DELAY, \ |
| 315 | .n_voltages = 1 << 6, \ |
| 316 | .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \ |
| 317 | .vsel_mask = MAX77802_VSEL_MASK, \ |
| 318 | .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \ |
| 319 | .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \ |
| 320 | } |
| 321 | |
| 322 | /* LDOs 1, 2, 8, 15, 17, 27, 30, 35 */ |
| 323 | #define regulator_77802_desc_n_ldo(num, supply, log) { \ |
| 324 | .name = "LDO"#num, \ |
| 325 | .id = MAX77802_LDO##num, \ |
| 326 | .supply_name = "inl"#supply, \ |
| 327 | .ops = &max77802_ldo_ops_logic##log, \ |
| 328 | .type = REGULATOR_VOLTAGE, \ |
| 329 | .owner = THIS_MODULE, \ |
| 330 | .min_uV = 800000, \ |
| 331 | .uV_step = 25000, \ |
| 332 | .ramp_delay = MAX77802_RAMP_DELAY, \ |
| 333 | .n_voltages = 1 << 6, \ |
| 334 | .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \ |
| 335 | .vsel_mask = MAX77802_VSEL_MASK, \ |
| 336 | .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \ |
| 337 | .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \ |
| 338 | } |
| 339 | |
| 340 | /* BUCKs 1, 6 */ |
| 341 | #define regulator_77802_desc_16_buck(num) { \ |
| 342 | .name = "BUCK"#num, \ |
| 343 | .id = MAX77802_BUCK##num, \ |
| 344 | .supply_name = "inb"#num, \ |
| 345 | .ops = &max77802_buck_16_dvs_ops, \ |
| 346 | .type = REGULATOR_VOLTAGE, \ |
| 347 | .owner = THIS_MODULE, \ |
| 348 | .min_uV = 612500, \ |
| 349 | .uV_step = 6250, \ |
| 350 | .ramp_delay = MAX77802_RAMP_DELAY, \ |
| 351 | .n_voltages = 1 << 8, \ |
| 352 | .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \ |
| 353 | .vsel_mask = MAX77802_DVS_VSEL_MASK, \ |
| 354 | .enable_reg = MAX77802_REG_BUCK ## num ## CTRL, \ |
| 355 | .enable_mask = MAX77802_OPMODE_MASK, \ |
| 356 | } |
| 357 | |
| 358 | /* BUCKS 2-4 */ |
| 359 | #define regulator_77802_desc_234_buck(num) { \ |
| 360 | .name = "BUCK"#num, \ |
| 361 | .id = MAX77802_BUCK##num, \ |
| 362 | .supply_name = "inb"#num, \ |
| 363 | .ops = &max77802_buck_dvs_ops, \ |
| 364 | .type = REGULATOR_VOLTAGE, \ |
| 365 | .owner = THIS_MODULE, \ |
| 366 | .min_uV = 600000, \ |
| 367 | .uV_step = 6250, \ |
| 368 | .ramp_delay = MAX77802_RAMP_DELAY, \ |
| 369 | .n_voltages = 0x91, \ |
| 370 | .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \ |
| 371 | .vsel_mask = MAX77802_DVS_VSEL_MASK, \ |
| 372 | .enable_reg = MAX77802_REG_BUCK ## num ## CTRL1, \ |
| 373 | .enable_mask = MAX77802_OPMODE_MASK << \ |
| 374 | MAX77802_OPMODE_BUCK234_SHIFT, \ |
| 375 | } |
| 376 | |
| 377 | /* BUCK 5 */ |
| 378 | #define regulator_77802_desc_buck5(num) { \ |
| 379 | .name = "BUCK"#num, \ |
| 380 | .id = MAX77802_BUCK##num, \ |
| 381 | .supply_name = "inb"#num, \ |
| 382 | .ops = &max77802_buck_dvs_ops, \ |
| 383 | .type = REGULATOR_VOLTAGE, \ |
| 384 | .owner = THIS_MODULE, \ |
| 385 | .min_uV = 750000, \ |
| 386 | .uV_step = 50000, \ |
| 387 | .ramp_delay = MAX77802_RAMP_DELAY, \ |
| 388 | .n_voltages = 1 << 6, \ |
| 389 | .vsel_reg = MAX77802_REG_BUCK5OUT, \ |
| 390 | .vsel_mask = MAX77802_VSEL_MASK, \ |
| 391 | .enable_reg = MAX77802_REG_BUCK5CTRL, \ |
| 392 | .enable_mask = MAX77802_OPMODE_MASK, \ |
| 393 | } |
| 394 | |
| 395 | /* BUCKs 7-10 */ |
| 396 | #define regulator_77802_desc_buck7_10(num) { \ |
| 397 | .name = "BUCK"#num, \ |
| 398 | .id = MAX77802_BUCK##num, \ |
| 399 | .supply_name = "inb"#num, \ |
| 400 | .ops = &max77802_buck_dvs_ops, \ |
| 401 | .type = REGULATOR_VOLTAGE, \ |
| 402 | .owner = THIS_MODULE, \ |
| 403 | .min_uV = 750000, \ |
| 404 | .uV_step = 50000, \ |
| 405 | .ramp_delay = MAX77802_RAMP_DELAY, \ |
| 406 | .n_voltages = 1 << 6, \ |
| 407 | .vsel_reg = MAX77802_REG_BUCK7OUT + (num - 7) * 3, \ |
| 408 | .vsel_mask = MAX77802_VSEL_MASK, \ |
| 409 | .enable_reg = MAX77802_REG_BUCK7CTRL + (num - 7) * 3, \ |
| 410 | .enable_mask = MAX77802_OPMODE_MASK, \ |
| 411 | } |
| 412 | |
| 413 | static struct regulator_desc regulators[] = { |
| 414 | regulator_77802_desc_16_buck(1), |
| 415 | regulator_77802_desc_234_buck(2), |
| 416 | regulator_77802_desc_234_buck(3), |
| 417 | regulator_77802_desc_234_buck(4), |
| 418 | regulator_77802_desc_buck5(5), |
| 419 | regulator_77802_desc_16_buck(6), |
| 420 | regulator_77802_desc_buck7_10(7), |
| 421 | regulator_77802_desc_buck7_10(8), |
| 422 | regulator_77802_desc_buck7_10(9), |
| 423 | regulator_77802_desc_buck7_10(10), |
| 424 | regulator_77802_desc_n_ldo(1, 10, 2), |
| 425 | regulator_77802_desc_n_ldo(2, 10, 1), |
| 426 | regulator_77802_desc_p_ldo(3, 3, 2), |
| 427 | regulator_77802_desc_p_ldo(4, 6, 1), |
| 428 | regulator_77802_desc_p_ldo(5, 3, 1), |
| 429 | regulator_77802_desc_p_ldo(6, 3, 1), |
| 430 | regulator_77802_desc_p_ldo(7, 3, 1), |
| 431 | regulator_77802_desc_n_ldo(8, 1, 1), |
| 432 | regulator_77802_desc_p_ldo(9, 5, 1), |
| 433 | regulator_77802_desc_p_ldo(10, 4, 1), |
| 434 | regulator_77802_desc_p_ldo(11, 4, 1), |
| 435 | regulator_77802_desc_p_ldo(12, 9, 1), |
| 436 | regulator_77802_desc_p_ldo(13, 4, 1), |
| 437 | regulator_77802_desc_p_ldo(14, 4, 1), |
| 438 | regulator_77802_desc_n_ldo(15, 1, 1), |
| 439 | regulator_77802_desc_n_ldo(17, 2, 1), |
| 440 | regulator_77802_desc_p_ldo(18, 7, 1), |
| 441 | regulator_77802_desc_p_ldo(19, 5, 1), |
| 442 | regulator_77802_desc_p_ldo(20, 7, 2), |
| 443 | regulator_77802_desc_p_ldo(21, 6, 2), |
| 444 | regulator_77802_desc_p_ldo(23, 9, 1), |
| 445 | regulator_77802_desc_p_ldo(24, 6, 1), |
| 446 | regulator_77802_desc_p_ldo(25, 9, 1), |
| 447 | regulator_77802_desc_p_ldo(26, 9, 1), |
| 448 | regulator_77802_desc_n_ldo(27, 2, 1), |
| 449 | regulator_77802_desc_p_ldo(28, 7, 1), |
| 450 | regulator_77802_desc_p_ldo(29, 7, 1), |
| 451 | regulator_77802_desc_n_ldo(30, 2, 1), |
| 452 | regulator_77802_desc_p_ldo(32, 9, 1), |
| 453 | regulator_77802_desc_p_ldo(33, 6, 1), |
| 454 | regulator_77802_desc_p_ldo(34, 9, 1), |
| 455 | regulator_77802_desc_n_ldo(35, 2, 1), |
| 456 | }; |
| 457 | |
| 458 | #ifdef CONFIG_OF |
| 459 | static int max77802_pmic_dt_parse_pdata(struct platform_device *pdev, |
| 460 | struct max77686_platform_data *pdata) |
| 461 | { |
| 462 | struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent); |
| 463 | struct device_node *pmic_np, *regulators_np; |
| 464 | struct max77686_regulator_data *rdata; |
| 465 | struct of_regulator_match rmatch; |
| 466 | unsigned int i; |
| 467 | |
| 468 | pmic_np = iodev->dev->of_node; |
| 469 | regulators_np = of_get_child_by_name(pmic_np, "regulators"); |
| 470 | if (!regulators_np) { |
| 471 | dev_err(&pdev->dev, "could not find regulators sub-node\n"); |
| 472 | return -EINVAL; |
| 473 | } |
| 474 | |
| 475 | pdata->num_regulators = ARRAY_SIZE(regulators); |
| 476 | rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) * |
| 477 | pdata->num_regulators, GFP_KERNEL); |
| 478 | if (!rdata) { |
| 479 | of_node_put(regulators_np); |
| 480 | return -ENOMEM; |
| 481 | } |
| 482 | |
| 483 | for (i = 0; i < pdata->num_regulators; i++) { |
| 484 | rmatch.name = regulators[i].name; |
| 485 | rmatch.init_data = NULL; |
| 486 | rmatch.of_node = NULL; |
| 487 | if (of_regulator_match(&pdev->dev, regulators_np, &rmatch, |
| 488 | 1) != 1) { |
| 489 | dev_warn(&pdev->dev, "No matching regulator for '%s'\n", |
| 490 | rmatch.name); |
| 491 | continue; |
| 492 | } |
| 493 | rdata[i].initdata = rmatch.init_data; |
| 494 | rdata[i].of_node = rmatch.of_node; |
| 495 | rdata[i].id = regulators[i].id; |
| 496 | } |
| 497 | |
| 498 | pdata->regulators = rdata; |
| 499 | of_node_put(regulators_np); |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | #else |
| 504 | static int max77802_pmic_dt_parse_pdata(struct platform_device *pdev, |
| 505 | struct max77686_platform_data *pdata) |
| 506 | { |
| 507 | return 0; |
| 508 | } |
| 509 | #endif /* CONFIG_OF */ |
| 510 | |
| 511 | static int max77802_pmic_probe(struct platform_device *pdev) |
| 512 | { |
| 513 | struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent); |
| 514 | struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev); |
| 515 | struct max77802_regulator_prv *max77802; |
| 516 | int i, ret = 0, val; |
| 517 | struct regulator_config config = { }; |
| 518 | |
| 519 | /* This is allocated by the MFD driver */ |
| 520 | if (!pdata) { |
| 521 | dev_err(&pdev->dev, "no platform data found for regulator\n"); |
| 522 | return -ENODEV; |
| 523 | } |
| 524 | |
| 525 | max77802 = devm_kzalloc(&pdev->dev, |
| 526 | sizeof(struct max77802_regulator_prv), |
| 527 | GFP_KERNEL); |
| 528 | if (!max77802) |
| 529 | return -ENOMEM; |
| 530 | |
| 531 | if (iodev->dev->of_node) { |
| 532 | ret = max77802_pmic_dt_parse_pdata(pdev, pdata); |
| 533 | if (ret) |
| 534 | return ret; |
| 535 | } |
| 536 | |
| 537 | config.dev = iodev->dev; |
| 538 | config.regmap = iodev->regmap; |
| 539 | config.driver_data = max77802; |
| 540 | platform_set_drvdata(pdev, max77802); |
| 541 | |
| 542 | for (i = 0; i < MAX77802_REG_MAX; i++) { |
| 543 | struct regulator_dev *rdev; |
| 544 | int id = pdata->regulators[i].id; |
| 545 | int shift = max77802_get_opmode_shift(id); |
| 546 | |
| 547 | config.init_data = pdata->regulators[i].initdata; |
| 548 | config.of_node = pdata->regulators[i].of_node; |
| 549 | |
| 550 | ret = regmap_read(iodev->regmap, regulators[i].enable_reg, &val); |
Javier Martinez Canillas | aea8dfb | 2014-08-26 13:37:41 +0200 | [diff] [blame] | 551 | val = val >> shift & MAX77802_OPMODE_MASK; |
| 552 | |
| 553 | /* |
| 554 | * If the regulator is disabled and the system warm rebooted, |
| 555 | * the hardware reports OFF as the regulator operating mode. |
| 556 | * Default to operating mode NORMAL in that case. |
| 557 | */ |
| 558 | if (val == MAX77802_OPMODE_OFF) |
| 559 | max77802->opmode[id] = MAX77802_OPMODE_NORMAL; |
| 560 | else |
| 561 | max77802->opmode[id] = val; |
Javier Martinez Canillas | e6f2f80 | 2014-08-18 10:32:41 +0200 | [diff] [blame] | 562 | |
| 563 | rdev = devm_regulator_register(&pdev->dev, |
| 564 | ®ulators[i], &config); |
| 565 | if (IS_ERR(rdev)) { |
| 566 | dev_err(&pdev->dev, |
| 567 | "regulator init failed for %d\n", i); |
| 568 | return PTR_ERR(rdev); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static const struct platform_device_id max77802_pmic_id[] = { |
| 576 | {"max77802-pmic", 0}, |
| 577 | { }, |
| 578 | }; |
| 579 | MODULE_DEVICE_TABLE(platform, max77802_pmic_id); |
| 580 | |
| 581 | static struct platform_driver max77802_pmic_driver = { |
| 582 | .driver = { |
| 583 | .name = "max77802-pmic", |
| 584 | .owner = THIS_MODULE, |
| 585 | }, |
| 586 | .probe = max77802_pmic_probe, |
| 587 | .id_table = max77802_pmic_id, |
| 588 | }; |
| 589 | |
| 590 | module_platform_driver(max77802_pmic_driver); |
| 591 | |
| 592 | MODULE_DESCRIPTION("MAXIM 77802 Regulator Driver"); |
| 593 | MODULE_AUTHOR("Simon Glass <sjg@chromium.org>"); |
| 594 | MODULE_LICENSE("GPL"); |