| 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // max8998.c - Voltage regulator driver for the Maxim 8998 |
| 4 | // |
| 5 | // Copyright (C) 2009-2010 Samsung Electronics |
| 6 | // Kyungmin Park <kyungmin.park@samsung.com> |
| 7 | // Marek Szyprowski <m.szyprowski@samsung.com> |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/i2c.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/bits.h> |
| 14 | #include <linux/gpio/consumer.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/regulator/driver.h> |
| 21 | #include <linux/regulator/of_regulator.h> |
| 22 | #include <linux/mfd/max8998.h> |
| 23 | #include <linux/mfd/max8998-private.h> |
| 24 | |
| 25 | struct max8998_data { |
| 26 | struct device *dev; |
| 27 | struct max8998_dev *iodev; |
| 28 | int num_regulators; |
| 29 | u8 buck1_vol[4]; /* voltages for selection */ |
| 30 | u8 buck2_vol[2]; |
| 31 | unsigned int buck1_idx; /* index to last changed voltage */ |
| 32 | /* value in a set */ |
| 33 | unsigned int buck2_idx; |
| 34 | struct gpio_desc *buck1_gpio1; |
| 35 | struct gpio_desc *buck1_gpio2; |
| 36 | struct gpio_desc *buck2_gpio; |
| 37 | }; |
| 38 | |
| 39 | static const unsigned int charger_current_table[] = { |
| 40 | 90000, 380000, 475000, 550000, 570000, 600000, 700000, 800000, |
| 41 | }; |
| 42 | |
| 43 | static int max8998_get_enable_register(struct regulator_dev *rdev, |
| 44 | int *reg, int *shift) |
| 45 | { |
| 46 | int ldo = rdev_get_id(rdev); |
| 47 | |
| 48 | switch (ldo) { |
| 49 | case MAX8998_LDO2 ... MAX8998_LDO5: |
| 50 | *reg = MAX8998_REG_ONOFF1; |
| 51 | *shift = 3 - (ldo - MAX8998_LDO2); |
| 52 | break; |
| 53 | case MAX8998_LDO6 ... MAX8998_LDO13: |
| 54 | *reg = MAX8998_REG_ONOFF2; |
| 55 | *shift = 7 - (ldo - MAX8998_LDO6); |
| 56 | break; |
| 57 | case MAX8998_LDO14 ... MAX8998_LDO17: |
| 58 | *reg = MAX8998_REG_ONOFF3; |
| 59 | *shift = 7 - (ldo - MAX8998_LDO14); |
| 60 | break; |
| 61 | case MAX8998_BUCK1 ... MAX8998_BUCK4: |
| 62 | *reg = MAX8998_REG_ONOFF1; |
| 63 | *shift = 7 - (ldo - MAX8998_BUCK1); |
| 64 | break; |
| 65 | case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG: |
| 66 | *reg = MAX8998_REG_ONOFF4; |
| 67 | *shift = 7 - (ldo - MAX8998_EN32KHZ_AP); |
| 68 | break; |
| 69 | case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2: |
| 70 | *reg = MAX8998_REG_CHGR2; |
| 71 | *shift = 7 - (ldo - MAX8998_ESAFEOUT1); |
| 72 | break; |
| 73 | case MAX8998_CHARGER: |
| 74 | *reg = MAX8998_REG_CHGR2; |
| 75 | *shift = 0; |
| 76 | break; |
| 77 | default: |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int max8998_ldo_is_enabled(struct regulator_dev *rdev) |
| 85 | { |
| 86 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 87 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 88 | int ret, reg, shift = 8; |
| 89 | u8 val; |
| 90 | |
| 91 | ret = max8998_get_enable_register(rdev, reg: ®, shift: &shift); |
| 92 | if (ret) |
| 93 | return ret; |
| 94 | |
| 95 | ret = max8998_read_reg(i2c, reg, dest: &val); |
| 96 | if (ret) |
| 97 | return ret; |
| 98 | |
| 99 | return val & (1 << shift); |
| 100 | } |
| 101 | |
| 102 | static int max8998_ldo_is_enabled_inverted(struct regulator_dev *rdev) |
| 103 | { |
| 104 | return (!max8998_ldo_is_enabled(rdev)); |
| 105 | } |
| 106 | |
| 107 | static int max8998_ldo_enable(struct regulator_dev *rdev) |
| 108 | { |
| 109 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 110 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 111 | int reg, shift = 8, ret; |
| 112 | |
| 113 | ret = max8998_get_enable_register(rdev, reg: ®, shift: &shift); |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | |
| 117 | return max8998_update_reg(i2c, reg, val: 1<<shift, mask: 1<<shift); |
| 118 | } |
| 119 | |
| 120 | static int max8998_ldo_disable(struct regulator_dev *rdev) |
| 121 | { |
| 122 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 123 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 124 | int reg, shift = 8, ret; |
| 125 | |
| 126 | ret = max8998_get_enable_register(rdev, reg: ®, shift: &shift); |
| 127 | if (ret) |
| 128 | return ret; |
| 129 | |
| 130 | return max8998_update_reg(i2c, reg, val: 0, mask: 1<<shift); |
| 131 | } |
| 132 | |
| 133 | static int max8998_get_voltage_register(struct regulator_dev *rdev, |
| 134 | int *_reg, int *_shift, int *_mask) |
| 135 | { |
| 136 | int ldo = rdev_get_id(rdev); |
| 137 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 138 | int reg, shift = 0, mask = 0xff; |
| 139 | |
| 140 | switch (ldo) { |
| 141 | case MAX8998_LDO2 ... MAX8998_LDO3: |
| 142 | reg = MAX8998_REG_LDO2_LDO3; |
| 143 | mask = 0xf; |
| 144 | if (ldo == MAX8998_LDO2) |
| 145 | shift = 4; |
| 146 | else |
| 147 | shift = 0; |
| 148 | break; |
| 149 | case MAX8998_LDO4 ... MAX8998_LDO7: |
| 150 | reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4); |
| 151 | break; |
| 152 | case MAX8998_LDO8 ... MAX8998_LDO9: |
| 153 | reg = MAX8998_REG_LDO8_LDO9; |
| 154 | mask = 0xf; |
| 155 | if (ldo == MAX8998_LDO8) |
| 156 | shift = 4; |
| 157 | else |
| 158 | shift = 0; |
| 159 | break; |
| 160 | case MAX8998_LDO10 ... MAX8998_LDO11: |
| 161 | reg = MAX8998_REG_LDO10_LDO11; |
| 162 | if (ldo == MAX8998_LDO10) { |
| 163 | shift = 5; |
| 164 | mask = 0x7; |
| 165 | } else { |
| 166 | shift = 0; |
| 167 | mask = 0x1f; |
| 168 | } |
| 169 | break; |
| 170 | case MAX8998_LDO12 ... MAX8998_LDO17: |
| 171 | reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12); |
| 172 | break; |
| 173 | case MAX8998_BUCK1: |
| 174 | reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx; |
| 175 | break; |
| 176 | case MAX8998_BUCK2: |
| 177 | reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx; |
| 178 | break; |
| 179 | case MAX8998_BUCK3: |
| 180 | reg = MAX8998_REG_BUCK3; |
| 181 | break; |
| 182 | case MAX8998_BUCK4: |
| 183 | reg = MAX8998_REG_BUCK4; |
| 184 | break; |
| 185 | default: |
| 186 | return -EINVAL; |
| 187 | } |
| 188 | |
| 189 | *_reg = reg; |
| 190 | *_shift = shift; |
| 191 | *_mask = mask; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int max8998_get_voltage_sel(struct regulator_dev *rdev) |
| 197 | { |
| 198 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 199 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 200 | int reg, shift = 0, mask, ret; |
| 201 | u8 val; |
| 202 | |
| 203 | ret = max8998_get_voltage_register(rdev, reg: ®, shift: &shift, mask: &mask); |
| 204 | if (ret) |
| 205 | return ret; |
| 206 | |
| 207 | ret = max8998_read_reg(i2c, reg, dest: &val); |
| 208 | if (ret) |
| 209 | return ret; |
| 210 | |
| 211 | val >>= shift; |
| 212 | val &= mask; |
| 213 | |
| 214 | return val; |
| 215 | } |
| 216 | |
| 217 | static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev, |
| 218 | unsigned selector) |
| 219 | { |
| 220 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 221 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 222 | int reg, shift = 0, mask, ret; |
| 223 | |
| 224 | ret = max8998_get_voltage_register(rdev, reg: ®, shift: &shift, mask: &mask); |
| 225 | if (ret) |
| 226 | return ret; |
| 227 | |
| 228 | ret = max8998_update_reg(i2c, reg, val: selector<<shift, mask: mask<<shift); |
| 229 | |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static inline void buck1_gpio_set(struct gpio_desc *gpio1, struct gpio_desc *gpio2, int v) |
| 234 | { |
| 235 | gpiod_set_value(desc: gpio1, value: v & 0x1); |
| 236 | gpiod_set_value(desc: gpio2, value: (v >> 1) & 0x1); |
| 237 | } |
| 238 | |
| 239 | static inline void buck2_gpio_set(struct gpio_desc *gpio, int v) |
| 240 | { |
| 241 | gpiod_set_value(desc: gpio, value: v & 0x1); |
| 242 | } |
| 243 | |
| 244 | static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev, |
| 245 | unsigned selector) |
| 246 | { |
| 247 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 248 | struct max8998_platform_data *pdata = max8998->iodev->pdata; |
| 249 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 250 | int buck = rdev_get_id(rdev); |
| 251 | int reg, shift = 0, mask, ret, j; |
| 252 | static u8 buck1_last_val; |
| 253 | |
| 254 | ret = max8998_get_voltage_register(rdev, reg: ®, shift: &shift, mask: &mask); |
| 255 | if (ret) |
| 256 | return ret; |
| 257 | |
| 258 | switch (buck) { |
| 259 | case MAX8998_BUCK1: |
| 260 | dev_dbg(max8998->dev, |
| 261 | "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n" |
| 262 | "buck1_vol3:%d, buck1_vol4:%d\n" , |
| 263 | selector, max8998->buck1_vol[0], max8998->buck1_vol[1], |
| 264 | max8998->buck1_vol[2], max8998->buck1_vol[3]); |
| 265 | |
| 266 | if (max8998->buck1_gpio1 && max8998->buck1_gpio2) { |
| 267 | |
| 268 | /* check if requested voltage */ |
| 269 | /* value is already defined */ |
| 270 | for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) { |
| 271 | if (max8998->buck1_vol[j] == selector) { |
| 272 | max8998->buck1_idx = j; |
| 273 | buck1_gpio_set(gpio1: max8998->buck1_gpio1, |
| 274 | gpio2: max8998->buck1_gpio2, v: j); |
| 275 | goto buck1_exit; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if (pdata->buck_voltage_lock) |
| 280 | return -EINVAL; |
| 281 | |
| 282 | /* no predefine regulator found */ |
| 283 | max8998->buck1_idx = (buck1_last_val % 2) + 2; |
| 284 | dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n" , |
| 285 | max8998->buck1_idx); |
| 286 | max8998->buck1_vol[max8998->buck1_idx] = selector; |
| 287 | ret = max8998_get_voltage_register(rdev, reg: ®, |
| 288 | shift: &shift, |
| 289 | mask: &mask); |
| 290 | ret = max8998_write_reg(i2c, reg, value: selector); |
| 291 | buck1_gpio_set(gpio1: max8998->buck1_gpio1, |
| 292 | gpio2: max8998->buck1_gpio2, v: max8998->buck1_idx); |
| 293 | buck1_last_val++; |
| 294 | buck1_exit: |
| 295 | dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n" , |
| 296 | i2c->name, gpiod_get_value(max8998->buck1_gpio1), |
| 297 | gpiod_get_value(max8998->buck1_gpio2)); |
| 298 | break; |
| 299 | } else { |
| 300 | ret = max8998_write_reg(i2c, reg, value: selector); |
| 301 | } |
| 302 | break; |
| 303 | |
| 304 | case MAX8998_BUCK2: |
| 305 | dev_dbg(max8998->dev, |
| 306 | "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n" , |
| 307 | selector, max8998->buck2_vol[0], max8998->buck2_vol[1]); |
| 308 | if (max8998->buck2_gpio) { |
| 309 | /* check if requested voltage */ |
| 310 | /* value is already defined */ |
| 311 | for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) { |
| 312 | if (max8998->buck2_vol[j] == selector) { |
| 313 | max8998->buck2_idx = j; |
| 314 | buck2_gpio_set(gpio: max8998->buck2_gpio, v: j); |
| 315 | goto buck2_exit; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | if (pdata->buck_voltage_lock) |
| 320 | return -EINVAL; |
| 321 | |
| 322 | max8998_get_voltage_register(rdev, |
| 323 | reg: ®, shift: &shift, mask: &mask); |
| 324 | ret = max8998_write_reg(i2c, reg, value: selector); |
| 325 | max8998->buck2_vol[max8998->buck2_idx] = selector; |
| 326 | buck2_gpio_set(gpio: max8998->buck2_gpio, v: max8998->buck2_idx); |
| 327 | buck2_exit: |
| 328 | dev_dbg(max8998->dev, "%s: SET3:%d\n" , i2c->name, |
| 329 | gpiod_get_value(max8998->buck2_gpio)); |
| 330 | } else { |
| 331 | ret = max8998_write_reg(i2c, reg, value: selector); |
| 332 | } |
| 333 | break; |
| 334 | |
| 335 | case MAX8998_BUCK3: |
| 336 | case MAX8998_BUCK4: |
| 337 | ret = max8998_update_reg(i2c, reg, val: selector<<shift, |
| 338 | mask: mask<<shift); |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev, |
| 346 | unsigned int old_selector, |
| 347 | unsigned int new_selector) |
| 348 | { |
| 349 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 350 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 351 | int buck = rdev_get_id(rdev); |
| 352 | u8 val = 0; |
| 353 | int difference, ret; |
| 354 | |
| 355 | if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4) |
| 356 | return -EINVAL; |
| 357 | |
| 358 | /* Voltage stabilization */ |
| 359 | ret = max8998_read_reg(i2c, reg: MAX8998_REG_ONOFF4, dest: &val); |
| 360 | if (ret) |
| 361 | return ret; |
| 362 | |
| 363 | /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */ |
| 364 | /* MAX8998 has ENRAMP bit implemented, so test it*/ |
| 365 | if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP)) |
| 366 | return 0; |
| 367 | |
| 368 | difference = (new_selector - old_selector) * rdev->desc->uV_step / 1000; |
| 369 | if (difference > 0) |
| 370 | return DIV_ROUND_UP(difference, (val & 0x0f) + 1); |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | static int max8998_set_current_limit(struct regulator_dev *rdev, |
| 376 | int min_uA, int max_uA) |
| 377 | { |
| 378 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 379 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 380 | unsigned int n_currents = rdev->desc->n_current_limits; |
| 381 | int i, sel = -1; |
| 382 | |
| 383 | if (n_currents == 0) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | if (rdev->desc->curr_table) { |
| 387 | const unsigned int *curr_table = rdev->desc->curr_table; |
| 388 | bool ascend = curr_table[n_currents - 1] > curr_table[0]; |
| 389 | |
| 390 | /* search for closest to maximum */ |
| 391 | if (ascend) { |
| 392 | for (i = n_currents - 1; i >= 0; i--) { |
| 393 | if (min_uA <= curr_table[i] && |
| 394 | curr_table[i] <= max_uA) { |
| 395 | sel = i; |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | } else { |
| 400 | for (i = 0; i < n_currents; i++) { |
| 401 | if (min_uA <= curr_table[i] && |
| 402 | curr_table[i] <= max_uA) { |
| 403 | sel = i; |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | if (sel < 0) |
| 411 | return -EINVAL; |
| 412 | |
| 413 | sel <<= ffs(rdev->desc->csel_mask) - 1; |
| 414 | |
| 415 | return max8998_update_reg(i2c, reg: rdev->desc->csel_reg, |
| 416 | val: sel, mask: rdev->desc->csel_mask); |
| 417 | } |
| 418 | |
| 419 | static int max8998_get_current_limit(struct regulator_dev *rdev) |
| 420 | { |
| 421 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 422 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 423 | u8 val; |
| 424 | int ret; |
| 425 | |
| 426 | ret = max8998_read_reg(i2c, reg: rdev->desc->csel_reg, dest: &val); |
| 427 | if (ret != 0) |
| 428 | return ret; |
| 429 | |
| 430 | val &= rdev->desc->csel_mask; |
| 431 | val >>= ffs(rdev->desc->csel_mask) - 1; |
| 432 | |
| 433 | if (rdev->desc->curr_table) { |
| 434 | if (val >= rdev->desc->n_current_limits) |
| 435 | return -EINVAL; |
| 436 | |
| 437 | return rdev->desc->curr_table[val]; |
| 438 | } |
| 439 | |
| 440 | return -EINVAL; |
| 441 | } |
| 442 | |
| 443 | static const struct regulator_ops max8998_ldo_ops = { |
| 444 | .list_voltage = regulator_list_voltage_linear, |
| 445 | .map_voltage = regulator_map_voltage_linear, |
| 446 | .is_enabled = max8998_ldo_is_enabled, |
| 447 | .enable = max8998_ldo_enable, |
| 448 | .disable = max8998_ldo_disable, |
| 449 | .get_voltage_sel = max8998_get_voltage_sel, |
| 450 | .set_voltage_sel = max8998_set_voltage_ldo_sel, |
| 451 | }; |
| 452 | |
| 453 | static const struct regulator_ops max8998_buck_ops = { |
| 454 | .list_voltage = regulator_list_voltage_linear, |
| 455 | .map_voltage = regulator_map_voltage_linear, |
| 456 | .is_enabled = max8998_ldo_is_enabled, |
| 457 | .enable = max8998_ldo_enable, |
| 458 | .disable = max8998_ldo_disable, |
| 459 | .get_voltage_sel = max8998_get_voltage_sel, |
| 460 | .set_voltage_sel = max8998_set_voltage_buck_sel, |
| 461 | .set_voltage_time_sel = max8998_set_voltage_buck_time_sel, |
| 462 | }; |
| 463 | |
| 464 | static const struct regulator_ops max8998_charger_ops = { |
| 465 | .set_current_limit = max8998_set_current_limit, |
| 466 | .get_current_limit = max8998_get_current_limit, |
| 467 | .is_enabled = max8998_ldo_is_enabled_inverted, |
| 468 | /* Swapped as register is inverted */ |
| 469 | .enable = max8998_ldo_disable, |
| 470 | .disable = max8998_ldo_enable, |
| 471 | }; |
| 472 | |
| 473 | static const struct regulator_ops max8998_others_ops = { |
| 474 | .is_enabled = max8998_ldo_is_enabled, |
| 475 | .enable = max8998_ldo_enable, |
| 476 | .disable = max8998_ldo_disable, |
| 477 | }; |
| 478 | |
| 479 | #define MAX8998_LINEAR_REG(_name, _ops, _min, _step, _max) \ |
| 480 | { \ |
| 481 | .name = #_name, \ |
| 482 | .id = MAX8998_##_name, \ |
| 483 | .ops = _ops, \ |
| 484 | .min_uV = (_min), \ |
| 485 | .uV_step = (_step), \ |
| 486 | .n_voltages = ((_max) - (_min)) / (_step) + 1, \ |
| 487 | .type = REGULATOR_VOLTAGE, \ |
| 488 | .owner = THIS_MODULE, \ |
| 489 | } |
| 490 | |
| 491 | #define MAX8998_CURRENT_REG(_name, _ops, _table, _reg, _mask) \ |
| 492 | { \ |
| 493 | .name = #_name, \ |
| 494 | .id = MAX8998_##_name, \ |
| 495 | .ops = _ops, \ |
| 496 | .curr_table = _table, \ |
| 497 | .n_current_limits = ARRAY_SIZE(_table), \ |
| 498 | .csel_reg = _reg, \ |
| 499 | .csel_mask = _mask, \ |
| 500 | .type = REGULATOR_CURRENT, \ |
| 501 | .owner = THIS_MODULE, \ |
| 502 | } |
| 503 | |
| 504 | #define MAX8998_OTHERS_REG(_name, _id) \ |
| 505 | { \ |
| 506 | .name = #_name, \ |
| 507 | .id = _id, \ |
| 508 | .ops = &max8998_others_ops, \ |
| 509 | .type = REGULATOR_VOLTAGE, \ |
| 510 | .owner = THIS_MODULE, \ |
| 511 | } |
| 512 | |
| 513 | static const struct regulator_desc regulators[] = { |
| 514 | MAX8998_LINEAR_REG(LDO2, &max8998_ldo_ops, 800000, 50000, 1300000), |
| 515 | MAX8998_LINEAR_REG(LDO3, &max8998_ldo_ops, 800000, 50000, 1300000), |
| 516 | MAX8998_LINEAR_REG(LDO4, &max8998_ldo_ops, 1600000, 100000, 3600000), |
| 517 | MAX8998_LINEAR_REG(LDO5, &max8998_ldo_ops, 1600000, 100000, 3600000), |
| 518 | MAX8998_LINEAR_REG(LDO6, &max8998_ldo_ops, 1600000, 100000, 3600000), |
| 519 | MAX8998_LINEAR_REG(LDO7, &max8998_ldo_ops, 1600000, 100000, 3600000), |
| 520 | MAX8998_LINEAR_REG(LDO8, &max8998_ldo_ops, 3000000, 100000, 3600000), |
| 521 | MAX8998_LINEAR_REG(LDO9, &max8998_ldo_ops, 2800000, 100000, 3100000), |
| 522 | MAX8998_LINEAR_REG(LDO10, &max8998_ldo_ops, 950000, 50000, 1300000), |
| 523 | MAX8998_LINEAR_REG(LDO11, &max8998_ldo_ops, 1600000, 100000, 3600000), |
| 524 | MAX8998_LINEAR_REG(LDO12, &max8998_ldo_ops, 800000, 100000, 3300000), |
| 525 | MAX8998_LINEAR_REG(LDO13, &max8998_ldo_ops, 800000, 100000, 3300000), |
| 526 | MAX8998_LINEAR_REG(LDO14, &max8998_ldo_ops, 1200000, 100000, 3300000), |
| 527 | MAX8998_LINEAR_REG(LDO15, &max8998_ldo_ops, 1200000, 100000, 3300000), |
| 528 | MAX8998_LINEAR_REG(LDO16, &max8998_ldo_ops, 1600000, 100000, 3600000), |
| 529 | MAX8998_LINEAR_REG(LDO17, &max8998_ldo_ops, 1600000, 100000, 3600000), |
| 530 | MAX8998_LINEAR_REG(BUCK1, &max8998_buck_ops, 750000, 25000, 1525000), |
| 531 | MAX8998_LINEAR_REG(BUCK2, &max8998_buck_ops, 750000, 25000, 1525000), |
| 532 | MAX8998_LINEAR_REG(BUCK3, &max8998_buck_ops, 1600000, 100000, 3600000), |
| 533 | MAX8998_LINEAR_REG(BUCK4, &max8998_buck_ops, 800000, 100000, 2300000), |
| 534 | MAX8998_OTHERS_REG(EN32KHz-AP, MAX8998_EN32KHZ_AP), |
| 535 | MAX8998_OTHERS_REG(EN32KHz-CP, MAX8998_EN32KHZ_CP), |
| 536 | MAX8998_OTHERS_REG(ENVICHG, MAX8998_ENVICHG), |
| 537 | MAX8998_OTHERS_REG(ESAFEOUT1, MAX8998_ESAFEOUT1), |
| 538 | MAX8998_OTHERS_REG(ESAFEOUT2, MAX8998_ESAFEOUT2), |
| 539 | MAX8998_CURRENT_REG(CHARGER, &max8998_charger_ops, |
| 540 | charger_current_table, MAX8998_REG_CHGR1, 0x7), |
| 541 | }; |
| 542 | |
| 543 | static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev, |
| 544 | struct max8998_platform_data *pdata) |
| 545 | { |
| 546 | struct device_node *pmic_np = iodev->dev->of_node; |
| 547 | struct device_node *regulators_np, *reg_np; |
| 548 | struct max8998_regulator_data *rdata; |
| 549 | unsigned int i; |
| 550 | int ret; |
| 551 | |
| 552 | regulators_np = of_get_child_by_name(node: pmic_np, name: "regulators" ); |
| 553 | if (!regulators_np) { |
| 554 | dev_err(iodev->dev, "could not find regulators sub-node\n" ); |
| 555 | return -EINVAL; |
| 556 | } |
| 557 | |
| 558 | /* count the number of regulators to be supported in pmic */ |
| 559 | pdata->num_regulators = of_get_child_count(np: regulators_np); |
| 560 | |
| 561 | rdata = devm_kcalloc(dev: iodev->dev, |
| 562 | n: pdata->num_regulators, size: sizeof(*rdata), |
| 563 | GFP_KERNEL); |
| 564 | if (!rdata) { |
| 565 | of_node_put(node: regulators_np); |
| 566 | return -ENOMEM; |
| 567 | } |
| 568 | |
| 569 | pdata->regulators = rdata; |
| 570 | for (i = 0; i < ARRAY_SIZE(regulators); ++i) { |
| 571 | reg_np = of_get_child_by_name(node: regulators_np, |
| 572 | name: regulators[i].name); |
| 573 | if (!reg_np) |
| 574 | continue; |
| 575 | |
| 576 | rdata->id = regulators[i].id; |
| 577 | rdata->initdata = of_get_regulator_init_data(dev: iodev->dev, |
| 578 | node: reg_np, |
| 579 | desc: ®ulators[i]); |
| 580 | rdata->reg_node = reg_np; |
| 581 | ++rdata; |
| 582 | } |
| 583 | pdata->num_regulators = rdata - pdata->regulators; |
| 584 | |
| 585 | of_node_put(node: reg_np); |
| 586 | of_node_put(node: regulators_np); |
| 587 | |
| 588 | pdata->buck_voltage_lock = of_property_read_bool(np: pmic_np, propname: "max8998,pmic-buck-voltage-lock" ); |
| 589 | |
| 590 | ret = of_property_read_u32(np: pmic_np, |
| 591 | propname: "max8998,pmic-buck1-default-dvs-idx" , |
| 592 | out_value: &pdata->buck1_default_idx); |
| 593 | if (!ret && pdata->buck1_default_idx >= 4) { |
| 594 | pdata->buck1_default_idx = 0; |
| 595 | dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n" ); |
| 596 | } |
| 597 | |
| 598 | ret = of_property_read_u32(np: pmic_np, |
| 599 | propname: "max8998,pmic-buck2-default-dvs-idx" , |
| 600 | out_value: &pdata->buck2_default_idx); |
| 601 | if (!ret && pdata->buck2_default_idx >= 2) { |
| 602 | pdata->buck2_default_idx = 0; |
| 603 | dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n" ); |
| 604 | } |
| 605 | |
| 606 | ret = of_property_read_u32_array(np: pmic_np, |
| 607 | propname: "max8998,pmic-buck1-dvs-voltage" , |
| 608 | out_values: pdata->buck1_voltage, |
| 609 | ARRAY_SIZE(pdata->buck1_voltage)); |
| 610 | if (ret) { |
| 611 | dev_err(iodev->dev, "buck1 voltages not specified\n" ); |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | |
| 615 | ret = of_property_read_u32_array(np: pmic_np, |
| 616 | propname: "max8998,pmic-buck2-dvs-voltage" , |
| 617 | out_values: pdata->buck2_voltage, |
| 618 | ARRAY_SIZE(pdata->buck2_voltage)); |
| 619 | if (ret) { |
| 620 | dev_err(iodev->dev, "buck2 voltages not specified\n" ); |
| 621 | return -EINVAL; |
| 622 | } |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static int max8998_pmic_probe(struct platform_device *pdev) |
| 628 | { |
| 629 | struct max8998_dev *iodev = dev_get_drvdata(dev: pdev->dev.parent); |
| 630 | struct max8998_platform_data *pdata = iodev->pdata; |
| 631 | struct regulator_config config = { }; |
| 632 | struct regulator_dev *rdev; |
| 633 | struct max8998_data *max8998; |
| 634 | struct i2c_client *i2c; |
| 635 | enum gpiod_flags flags; |
| 636 | int i, ret; |
| 637 | unsigned int v; |
| 638 | |
| 639 | if (!pdata) { |
| 640 | dev_err(pdev->dev.parent, "No platform init data supplied\n" ); |
| 641 | return -ENODEV; |
| 642 | } |
| 643 | |
| 644 | if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) { |
| 645 | ret = max8998_pmic_dt_parse_pdata(iodev, pdata); |
| 646 | if (ret) |
| 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | max8998 = devm_kzalloc(dev: &pdev->dev, size: sizeof(struct max8998_data), |
| 651 | GFP_KERNEL); |
| 652 | if (!max8998) |
| 653 | return -ENOMEM; |
| 654 | |
| 655 | max8998->dev = &pdev->dev; |
| 656 | max8998->iodev = iodev; |
| 657 | max8998->num_regulators = pdata->num_regulators; |
| 658 | platform_set_drvdata(pdev, data: max8998); |
| 659 | i2c = max8998->iodev->i2c; |
| 660 | |
| 661 | max8998->buck1_idx = pdata->buck1_default_idx; |
| 662 | max8998->buck2_idx = pdata->buck2_default_idx; |
| 663 | |
| 664 | /* Check if MAX8998 voltage selection GPIOs are defined */ |
| 665 | flags = (max8998->buck1_idx & BIT(0)) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; |
| 666 | max8998->buck1_gpio1 = devm_gpiod_get_index_optional(dev: iodev->dev, |
| 667 | con_id: "max8998,pmic-buck1-dvs" , |
| 668 | index: 0, |
| 669 | flags); |
| 670 | if (IS_ERR(ptr: max8998->buck1_gpio1)) |
| 671 | return dev_err_probe(dev: &pdev->dev, err: PTR_ERR(ptr: max8998->buck1_gpio1), |
| 672 | fmt: "could not get BUCK1 GPIO1\n" ); |
| 673 | gpiod_set_consumer_name(desc: max8998->buck1_gpio1, name: "MAX8998 BUCK1_SET1" ); |
| 674 | |
| 675 | flags = (max8998->buck1_idx & BIT(1)) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; |
| 676 | max8998->buck1_gpio2 = devm_gpiod_get_index_optional(dev: iodev->dev, |
| 677 | con_id: "max8998,pmic-buck1-dvs" , |
| 678 | index: 1, |
| 679 | flags); |
| 680 | if (IS_ERR(ptr: max8998->buck1_gpio2)) |
| 681 | return dev_err_probe(dev: &pdev->dev, err: PTR_ERR(ptr: max8998->buck1_gpio2), |
| 682 | fmt: "could not get BUCK1 GPIO2\n" ); |
| 683 | gpiod_set_consumer_name(desc: max8998->buck1_gpio1, name: "MAX8998 BUCK1_SET2" ); |
| 684 | |
| 685 | flags = (max8998->buck2_idx & BIT(0)) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; |
| 686 | max8998->buck2_gpio = devm_gpiod_get_index_optional(dev: iodev->dev, |
| 687 | con_id: "max8998,pmic-buck2-dvs" , |
| 688 | index: 0, |
| 689 | flags); |
| 690 | if (IS_ERR(ptr: max8998->buck2_gpio)) |
| 691 | return dev_err_probe(dev: &pdev->dev, err: PTR_ERR(ptr: max8998->buck2_gpio), |
| 692 | fmt: "could not get BUCK2 GPIO\n" ); |
| 693 | gpiod_set_consumer_name(desc: max8998->buck1_gpio1, name: "MAX8998 BUCK2_SET3" ); |
| 694 | |
| 695 | if (max8998->buck1_gpio1 && max8998->buck1_gpio2) { |
| 696 | /* Set predefined values for BUCK1 registers */ |
| 697 | for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) { |
| 698 | int index = MAX8998_BUCK1 - MAX8998_LDO2; |
| 699 | |
| 700 | i = 0; |
| 701 | while (regulators[index].min_uV + |
| 702 | regulators[index].uV_step * i |
| 703 | < pdata->buck1_voltage[v]) |
| 704 | i++; |
| 705 | |
| 706 | max8998->buck1_vol[v] = i; |
| 707 | ret = max8998_write_reg(i2c, |
| 708 | reg: MAX8998_REG_BUCK1_VOLTAGE1 + v, value: i); |
| 709 | if (ret) |
| 710 | return ret; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | if (max8998->buck2_gpio) { |
| 715 | /* Set predefined values for BUCK2 registers */ |
| 716 | for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) { |
| 717 | int index = MAX8998_BUCK2 - MAX8998_LDO2; |
| 718 | |
| 719 | i = 0; |
| 720 | while (regulators[index].min_uV + |
| 721 | regulators[index].uV_step * i |
| 722 | < pdata->buck2_voltage[v]) |
| 723 | i++; |
| 724 | |
| 725 | max8998->buck2_vol[v] = i; |
| 726 | ret = max8998_write_reg(i2c, |
| 727 | reg: MAX8998_REG_BUCK2_VOLTAGE1 + v, value: i); |
| 728 | if (ret) |
| 729 | return ret; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | for (i = 0; i < pdata->num_regulators; i++) { |
| 734 | int index = pdata->regulators[i].id - MAX8998_LDO2; |
| 735 | |
| 736 | config.dev = max8998->dev; |
| 737 | config.of_node = pdata->regulators[i].reg_node; |
| 738 | config.init_data = pdata->regulators[i].initdata; |
| 739 | config.driver_data = max8998; |
| 740 | |
| 741 | rdev = devm_regulator_register(dev: &pdev->dev, regulator_desc: ®ulators[index], |
| 742 | config: &config); |
| 743 | if (IS_ERR(ptr: rdev)) { |
| 744 | ret = PTR_ERR(ptr: rdev); |
| 745 | dev_err(max8998->dev, "regulator %s init failed (%d)\n" , |
| 746 | regulators[index].name, ret); |
| 747 | return ret; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | static const struct platform_device_id max8998_pmic_id[] = { |
| 755 | { "max8998-pmic" , TYPE_MAX8998 }, |
| 756 | { "lp3974-pmic" , TYPE_LP3974 }, |
| 757 | { } |
| 758 | }; |
| 759 | MODULE_DEVICE_TABLE(platform, max8998_pmic_id); |
| 760 | |
| 761 | static struct platform_driver max8998_pmic_driver = { |
| 762 | .driver = { |
| 763 | .name = "max8998-pmic" , |
| 764 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 765 | }, |
| 766 | .probe = max8998_pmic_probe, |
| 767 | .id_table = max8998_pmic_id, |
| 768 | }; |
| 769 | |
| 770 | static int __init max8998_pmic_init(void) |
| 771 | { |
| 772 | return platform_driver_register(&max8998_pmic_driver); |
| 773 | } |
| 774 | subsys_initcall(max8998_pmic_init); |
| 775 | |
| 776 | static void __exit max8998_pmic_cleanup(void) |
| 777 | { |
| 778 | platform_driver_unregister(&max8998_pmic_driver); |
| 779 | } |
| 780 | module_exit(max8998_pmic_cleanup); |
| 781 | |
| 782 | MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver" ); |
| 783 | MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>" ); |
| 784 | MODULE_LICENSE("GPL" ); |
| 785 | |