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