Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | 328bba6 | 2020-03-23 17:44:30 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2021, STMicroelectronics - All Rights Reserved |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | 328bba6 | 2020-03-23 17:44:30 +0100 | [diff] [blame] | 7 | #include <errno.h> |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <common/debug.h> |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 11 | #include <drivers/st/stpmic1.h> |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 12 | |
| 13 | #define I2C_TIMEOUT_MS 25 |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 15 | struct regul_struct { |
| 16 | const char *dt_node_name; |
| 17 | const uint16_t *voltage_table; |
| 18 | uint8_t voltage_table_size; |
| 19 | uint8_t control_reg; |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 20 | uint8_t enable_mask; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 21 | uint8_t low_power_reg; |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 22 | uint8_t pull_down_reg; |
| 23 | uint8_t pull_down; |
| 24 | uint8_t mask_reset_reg; |
| 25 | uint8_t mask_reset; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 28 | static struct i2c_handle_s *pmic_i2c_handle; |
| 29 | static uint16_t pmic_i2c_addr; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 30 | |
| 31 | /* Voltage tables in mV */ |
| 32 | static const uint16_t buck1_voltage_table[] = { |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 33 | 725, |
| 34 | 725, |
| 35 | 725, |
| 36 | 725, |
| 37 | 725, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 38 | 725, |
| 39 | 750, |
| 40 | 775, |
| 41 | 800, |
| 42 | 825, |
| 43 | 850, |
| 44 | 875, |
| 45 | 900, |
| 46 | 925, |
| 47 | 950, |
| 48 | 975, |
| 49 | 1000, |
| 50 | 1025, |
| 51 | 1050, |
| 52 | 1075, |
| 53 | 1100, |
| 54 | 1125, |
| 55 | 1150, |
| 56 | 1175, |
| 57 | 1200, |
| 58 | 1225, |
| 59 | 1250, |
| 60 | 1275, |
| 61 | 1300, |
| 62 | 1325, |
| 63 | 1350, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 64 | 1375, |
| 65 | 1400, |
| 66 | 1425, |
| 67 | 1450, |
| 68 | 1475, |
| 69 | 1500, |
| 70 | 1500, |
| 71 | 1500, |
| 72 | 1500, |
| 73 | 1500, |
| 74 | 1500, |
| 75 | 1500, |
| 76 | 1500, |
| 77 | 1500, |
| 78 | 1500, |
| 79 | 1500, |
| 80 | 1500, |
| 81 | 1500, |
| 82 | 1500, |
| 83 | 1500, |
| 84 | 1500, |
| 85 | 1500, |
| 86 | 1500, |
| 87 | 1500, |
| 88 | 1500, |
| 89 | 1500, |
| 90 | 1500, |
| 91 | 1500, |
| 92 | 1500, |
| 93 | 1500, |
| 94 | 1500, |
| 95 | 1500, |
| 96 | 1500, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | static const uint16_t buck2_voltage_table[] = { |
| 100 | 1000, |
| 101 | 1000, |
| 102 | 1000, |
| 103 | 1000, |
| 104 | 1000, |
| 105 | 1000, |
| 106 | 1000, |
| 107 | 1000, |
| 108 | 1000, |
| 109 | 1000, |
| 110 | 1000, |
| 111 | 1000, |
| 112 | 1000, |
| 113 | 1000, |
| 114 | 1000, |
| 115 | 1000, |
| 116 | 1000, |
| 117 | 1000, |
| 118 | 1050, |
| 119 | 1050, |
| 120 | 1100, |
| 121 | 1100, |
| 122 | 1150, |
| 123 | 1150, |
| 124 | 1200, |
| 125 | 1200, |
| 126 | 1250, |
| 127 | 1250, |
| 128 | 1300, |
| 129 | 1300, |
| 130 | 1350, |
| 131 | 1350, |
| 132 | 1400, |
| 133 | 1400, |
| 134 | 1450, |
| 135 | 1450, |
| 136 | 1500, |
| 137 | }; |
| 138 | |
| 139 | static const uint16_t buck3_voltage_table[] = { |
| 140 | 1000, |
| 141 | 1000, |
| 142 | 1000, |
| 143 | 1000, |
| 144 | 1000, |
| 145 | 1000, |
| 146 | 1000, |
| 147 | 1000, |
| 148 | 1000, |
| 149 | 1000, |
| 150 | 1000, |
| 151 | 1000, |
| 152 | 1000, |
| 153 | 1000, |
| 154 | 1000, |
| 155 | 1000, |
| 156 | 1000, |
| 157 | 1000, |
| 158 | 1000, |
| 159 | 1000, |
| 160 | 1100, |
| 161 | 1100, |
| 162 | 1100, |
| 163 | 1100, |
| 164 | 1200, |
| 165 | 1200, |
| 166 | 1200, |
| 167 | 1200, |
| 168 | 1300, |
| 169 | 1300, |
| 170 | 1300, |
| 171 | 1300, |
| 172 | 1400, |
| 173 | 1400, |
| 174 | 1400, |
| 175 | 1400, |
| 176 | 1500, |
| 177 | 1600, |
| 178 | 1700, |
| 179 | 1800, |
| 180 | 1900, |
| 181 | 2000, |
| 182 | 2100, |
| 183 | 2200, |
| 184 | 2300, |
| 185 | 2400, |
| 186 | 2500, |
| 187 | 2600, |
| 188 | 2700, |
| 189 | 2800, |
| 190 | 2900, |
| 191 | 3000, |
| 192 | 3100, |
| 193 | 3200, |
| 194 | 3300, |
| 195 | 3400, |
| 196 | }; |
| 197 | |
| 198 | static const uint16_t buck4_voltage_table[] = { |
| 199 | 600, |
| 200 | 625, |
| 201 | 650, |
| 202 | 675, |
| 203 | 700, |
| 204 | 725, |
| 205 | 750, |
| 206 | 775, |
| 207 | 800, |
| 208 | 825, |
| 209 | 850, |
| 210 | 875, |
| 211 | 900, |
| 212 | 925, |
| 213 | 950, |
| 214 | 975, |
| 215 | 1000, |
| 216 | 1025, |
| 217 | 1050, |
| 218 | 1075, |
| 219 | 1100, |
| 220 | 1125, |
| 221 | 1150, |
| 222 | 1175, |
| 223 | 1200, |
| 224 | 1225, |
| 225 | 1250, |
| 226 | 1275, |
| 227 | 1300, |
| 228 | 1300, |
| 229 | 1350, |
| 230 | 1350, |
| 231 | 1400, |
| 232 | 1400, |
| 233 | 1450, |
| 234 | 1450, |
| 235 | 1500, |
| 236 | 1600, |
| 237 | 1700, |
| 238 | 1800, |
| 239 | 1900, |
| 240 | 2000, |
| 241 | 2100, |
| 242 | 2200, |
| 243 | 2300, |
| 244 | 2400, |
| 245 | 2500, |
| 246 | 2600, |
| 247 | 2700, |
| 248 | 2800, |
| 249 | 2900, |
| 250 | 3000, |
| 251 | 3100, |
| 252 | 3200, |
| 253 | 3300, |
| 254 | 3400, |
| 255 | 3500, |
| 256 | 3600, |
| 257 | 3700, |
| 258 | 3800, |
| 259 | 3900, |
| 260 | }; |
| 261 | |
| 262 | static const uint16_t ldo1_voltage_table[] = { |
| 263 | 1700, |
| 264 | 1700, |
| 265 | 1700, |
| 266 | 1700, |
| 267 | 1700, |
| 268 | 1700, |
| 269 | 1700, |
| 270 | 1700, |
| 271 | 1700, |
| 272 | 1800, |
| 273 | 1900, |
| 274 | 2000, |
| 275 | 2100, |
| 276 | 2200, |
| 277 | 2300, |
| 278 | 2400, |
| 279 | 2500, |
| 280 | 2600, |
| 281 | 2700, |
| 282 | 2800, |
| 283 | 2900, |
| 284 | 3000, |
| 285 | 3100, |
| 286 | 3200, |
| 287 | 3300, |
| 288 | }; |
| 289 | |
| 290 | static const uint16_t ldo2_voltage_table[] = { |
| 291 | 1700, |
| 292 | 1700, |
| 293 | 1700, |
| 294 | 1700, |
| 295 | 1700, |
| 296 | 1700, |
| 297 | 1700, |
| 298 | 1700, |
| 299 | 1700, |
| 300 | 1800, |
| 301 | 1900, |
| 302 | 2000, |
| 303 | 2100, |
| 304 | 2200, |
| 305 | 2300, |
| 306 | 2400, |
| 307 | 2500, |
| 308 | 2600, |
| 309 | 2700, |
| 310 | 2800, |
| 311 | 2900, |
| 312 | 3000, |
| 313 | 3100, |
| 314 | 3200, |
| 315 | 3300, |
| 316 | }; |
| 317 | |
| 318 | static const uint16_t ldo3_voltage_table[] = { |
| 319 | 1700, |
| 320 | 1700, |
| 321 | 1700, |
| 322 | 1700, |
| 323 | 1700, |
| 324 | 1700, |
| 325 | 1700, |
| 326 | 1700, |
| 327 | 1700, |
| 328 | 1800, |
| 329 | 1900, |
| 330 | 2000, |
| 331 | 2100, |
| 332 | 2200, |
| 333 | 2300, |
| 334 | 2400, |
| 335 | 2500, |
| 336 | 2600, |
| 337 | 2700, |
| 338 | 2800, |
| 339 | 2900, |
| 340 | 3000, |
| 341 | 3100, |
| 342 | 3200, |
| 343 | 3300, |
| 344 | 3300, |
| 345 | 3300, |
| 346 | 3300, |
| 347 | 3300, |
| 348 | 3300, |
| 349 | 3300, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 350 | 500, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 351 | 0xFFFF, /* VREFDDR */ |
| 352 | }; |
| 353 | |
| 354 | static const uint16_t ldo5_voltage_table[] = { |
| 355 | 1700, |
| 356 | 1700, |
| 357 | 1700, |
| 358 | 1700, |
| 359 | 1700, |
| 360 | 1700, |
| 361 | 1700, |
| 362 | 1700, |
| 363 | 1700, |
| 364 | 1800, |
| 365 | 1900, |
| 366 | 2000, |
| 367 | 2100, |
| 368 | 2200, |
| 369 | 2300, |
| 370 | 2400, |
| 371 | 2500, |
| 372 | 2600, |
| 373 | 2700, |
| 374 | 2800, |
| 375 | 2900, |
| 376 | 3000, |
| 377 | 3100, |
| 378 | 3200, |
| 379 | 3300, |
| 380 | 3400, |
| 381 | 3500, |
| 382 | 3600, |
| 383 | 3700, |
| 384 | 3800, |
| 385 | 3900, |
| 386 | }; |
| 387 | |
| 388 | static const uint16_t ldo6_voltage_table[] = { |
| 389 | 900, |
| 390 | 1000, |
| 391 | 1100, |
| 392 | 1200, |
| 393 | 1300, |
| 394 | 1400, |
| 395 | 1500, |
| 396 | 1600, |
| 397 | 1700, |
| 398 | 1800, |
| 399 | 1900, |
| 400 | 2000, |
| 401 | 2100, |
| 402 | 2200, |
| 403 | 2300, |
| 404 | 2400, |
| 405 | 2500, |
| 406 | 2600, |
| 407 | 2700, |
| 408 | 2800, |
| 409 | 2900, |
| 410 | 3000, |
| 411 | 3100, |
| 412 | 3200, |
| 413 | 3300, |
| 414 | }; |
| 415 | |
| 416 | static const uint16_t ldo4_voltage_table[] = { |
| 417 | 3300, |
| 418 | }; |
| 419 | |
| 420 | static const uint16_t vref_ddr_voltage_table[] = { |
| 421 | 3300, |
| 422 | }; |
| 423 | |
| 424 | /* Table of Regulators in PMIC SoC */ |
| 425 | static const struct regul_struct regulators_table[] = { |
| 426 | { |
| 427 | .dt_node_name = "buck1", |
| 428 | .voltage_table = buck1_voltage_table, |
| 429 | .voltage_table_size = ARRAY_SIZE(buck1_voltage_table), |
| 430 | .control_reg = BUCK1_CONTROL_REG, |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 431 | .enable_mask = LDO_BUCK_ENABLE_MASK, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 432 | .low_power_reg = BUCK1_PWRCTRL_REG, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 433 | .pull_down_reg = BUCK_PULL_DOWN_REG, |
| 434 | .pull_down = BUCK1_PULL_DOWN_SHIFT, |
| 435 | .mask_reset_reg = MASK_RESET_BUCK_REG, |
| 436 | .mask_reset = BUCK1_MASK_RESET, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 437 | }, |
| 438 | { |
| 439 | .dt_node_name = "buck2", |
| 440 | .voltage_table = buck2_voltage_table, |
| 441 | .voltage_table_size = ARRAY_SIZE(buck2_voltage_table), |
| 442 | .control_reg = BUCK2_CONTROL_REG, |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 443 | .enable_mask = LDO_BUCK_ENABLE_MASK, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 444 | .low_power_reg = BUCK2_PWRCTRL_REG, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 445 | .pull_down_reg = BUCK_PULL_DOWN_REG, |
| 446 | .pull_down = BUCK2_PULL_DOWN_SHIFT, |
| 447 | .mask_reset_reg = MASK_RESET_BUCK_REG, |
| 448 | .mask_reset = BUCK2_MASK_RESET, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 449 | }, |
| 450 | { |
| 451 | .dt_node_name = "buck3", |
| 452 | .voltage_table = buck3_voltage_table, |
| 453 | .voltage_table_size = ARRAY_SIZE(buck3_voltage_table), |
| 454 | .control_reg = BUCK3_CONTROL_REG, |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 455 | .enable_mask = LDO_BUCK_ENABLE_MASK, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 456 | .low_power_reg = BUCK3_PWRCTRL_REG, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 457 | .pull_down_reg = BUCK_PULL_DOWN_REG, |
| 458 | .pull_down = BUCK3_PULL_DOWN_SHIFT, |
| 459 | .mask_reset_reg = MASK_RESET_BUCK_REG, |
| 460 | .mask_reset = BUCK3_MASK_RESET, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 461 | }, |
| 462 | { |
| 463 | .dt_node_name = "buck4", |
| 464 | .voltage_table = buck4_voltage_table, |
| 465 | .voltage_table_size = ARRAY_SIZE(buck4_voltage_table), |
| 466 | .control_reg = BUCK4_CONTROL_REG, |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 467 | .enable_mask = LDO_BUCK_ENABLE_MASK, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 468 | .low_power_reg = BUCK4_PWRCTRL_REG, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 469 | .pull_down_reg = BUCK_PULL_DOWN_REG, |
| 470 | .pull_down = BUCK4_PULL_DOWN_SHIFT, |
| 471 | .mask_reset_reg = MASK_RESET_BUCK_REG, |
| 472 | .mask_reset = BUCK4_MASK_RESET, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 473 | }, |
| 474 | { |
| 475 | .dt_node_name = "ldo1", |
| 476 | .voltage_table = ldo1_voltage_table, |
| 477 | .voltage_table_size = ARRAY_SIZE(ldo1_voltage_table), |
| 478 | .control_reg = LDO1_CONTROL_REG, |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 479 | .enable_mask = LDO_BUCK_ENABLE_MASK, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 480 | .low_power_reg = LDO1_PWRCTRL_REG, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 481 | .mask_reset_reg = MASK_RESET_LDO_REG, |
| 482 | .mask_reset = LDO1_MASK_RESET, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 483 | }, |
| 484 | { |
| 485 | .dt_node_name = "ldo2", |
| 486 | .voltage_table = ldo2_voltage_table, |
| 487 | .voltage_table_size = ARRAY_SIZE(ldo2_voltage_table), |
| 488 | .control_reg = LDO2_CONTROL_REG, |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 489 | .enable_mask = LDO_BUCK_ENABLE_MASK, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 490 | .low_power_reg = LDO2_PWRCTRL_REG, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 491 | .mask_reset_reg = MASK_RESET_LDO_REG, |
| 492 | .mask_reset = LDO2_MASK_RESET, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 493 | }, |
| 494 | { |
| 495 | .dt_node_name = "ldo3", |
| 496 | .voltage_table = ldo3_voltage_table, |
| 497 | .voltage_table_size = ARRAY_SIZE(ldo3_voltage_table), |
| 498 | .control_reg = LDO3_CONTROL_REG, |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 499 | .enable_mask = LDO_BUCK_ENABLE_MASK, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 500 | .low_power_reg = LDO3_PWRCTRL_REG, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 501 | .mask_reset_reg = MASK_RESET_LDO_REG, |
| 502 | .mask_reset = LDO3_MASK_RESET, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 503 | }, |
| 504 | { |
| 505 | .dt_node_name = "ldo4", |
| 506 | .voltage_table = ldo4_voltage_table, |
| 507 | .voltage_table_size = ARRAY_SIZE(ldo4_voltage_table), |
| 508 | .control_reg = LDO4_CONTROL_REG, |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 509 | .enable_mask = LDO_BUCK_ENABLE_MASK, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 510 | .low_power_reg = LDO4_PWRCTRL_REG, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 511 | .mask_reset_reg = MASK_RESET_LDO_REG, |
| 512 | .mask_reset = LDO4_MASK_RESET, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 513 | }, |
| 514 | { |
| 515 | .dt_node_name = "ldo5", |
| 516 | .voltage_table = ldo5_voltage_table, |
| 517 | .voltage_table_size = ARRAY_SIZE(ldo5_voltage_table), |
| 518 | .control_reg = LDO5_CONTROL_REG, |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 519 | .enable_mask = LDO_BUCK_ENABLE_MASK, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 520 | .low_power_reg = LDO5_PWRCTRL_REG, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 521 | .mask_reset_reg = MASK_RESET_LDO_REG, |
| 522 | .mask_reset = LDO5_MASK_RESET, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 523 | }, |
| 524 | { |
| 525 | .dt_node_name = "ldo6", |
| 526 | .voltage_table = ldo6_voltage_table, |
| 527 | .voltage_table_size = ARRAY_SIZE(ldo6_voltage_table), |
| 528 | .control_reg = LDO6_CONTROL_REG, |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 529 | .enable_mask = LDO_BUCK_ENABLE_MASK, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 530 | .low_power_reg = LDO6_PWRCTRL_REG, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 531 | .mask_reset_reg = MASK_RESET_LDO_REG, |
| 532 | .mask_reset = LDO6_MASK_RESET, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 533 | }, |
| 534 | { |
| 535 | .dt_node_name = "vref_ddr", |
| 536 | .voltage_table = vref_ddr_voltage_table, |
| 537 | .voltage_table_size = ARRAY_SIZE(vref_ddr_voltage_table), |
| 538 | .control_reg = VREF_DDR_CONTROL_REG, |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 539 | .enable_mask = LDO_BUCK_ENABLE_MASK, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 540 | .low_power_reg = VREF_DDR_PWRCTRL_REG, |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 541 | .mask_reset_reg = MASK_RESET_LDO_REG, |
| 542 | .mask_reset = VREF_DDR_MASK_RESET, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 543 | }, |
| 544 | }; |
| 545 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 546 | #define MAX_REGUL ARRAY_SIZE(regulators_table) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 547 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 548 | static const struct regul_struct *get_regulator_data(const char *name) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 549 | { |
| 550 | uint8_t i; |
| 551 | |
| 552 | for (i = 0 ; i < MAX_REGUL ; i++) { |
| 553 | if (strncmp(name, regulators_table[i].dt_node_name, |
| 554 | strlen(regulators_table[i].dt_node_name)) == 0) { |
| 555 | return ®ulators_table[i]; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /* Regulator not found */ |
| 560 | panic(); |
| 561 | return NULL; |
| 562 | } |
| 563 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 564 | static uint8_t voltage_to_index(const char *name, uint16_t millivolts) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 565 | { |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 566 | const struct regul_struct *regul = get_regulator_data(name); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 567 | uint8_t i; |
| 568 | |
| 569 | for (i = 0 ; i < regul->voltage_table_size ; i++) { |
| 570 | if (regul->voltage_table[i] == millivolts) { |
| 571 | return i; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /* Voltage not found */ |
| 576 | panic(); |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 581 | int stpmic1_powerctrl_on(void) |
| 582 | { |
| 583 | return stpmic1_register_update(MAIN_CONTROL_REG, PWRCTRL_PIN_VALID, |
| 584 | PWRCTRL_PIN_VALID); |
| 585 | } |
| 586 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 587 | int stpmic1_switch_off(void) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 588 | { |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 589 | return stpmic1_register_update(MAIN_CONTROL_REG, 1, |
| 590 | SOFTWARE_SWITCH_OFF_ENABLED); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 593 | int stpmic1_regulator_enable(const char *name) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 594 | { |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 595 | const struct regul_struct *regul = get_regulator_data(name); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 596 | |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 597 | return stpmic1_register_update(regul->control_reg, regul->enable_mask, |
| 598 | regul->enable_mask); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 601 | int stpmic1_regulator_disable(const char *name) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 602 | { |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 603 | const struct regul_struct *regul = get_regulator_data(name); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 604 | |
Pascal Paillet | 84bca88 | 2021-03-10 14:37:03 +0100 | [diff] [blame] | 605 | return stpmic1_register_update(regul->control_reg, 0, |
| 606 | regul->enable_mask); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 607 | } |
| 608 | |
Nicolas Le Bayon | f5188ee | 2019-09-19 11:24:50 +0200 | [diff] [blame] | 609 | bool stpmic1_is_regulator_enabled(const char *name) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 610 | { |
| 611 | uint8_t val; |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 612 | const struct regul_struct *regul = get_regulator_data(name); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 613 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 614 | if (stpmic1_register_read(regul->control_reg, &val) != 0) { |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 615 | panic(); |
| 616 | } |
| 617 | |
Nicolas Le Bayon | f5188ee | 2019-09-19 11:24:50 +0200 | [diff] [blame] | 618 | return (val & regul->enable_mask) == regul->enable_mask; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 621 | int stpmic1_regulator_voltage_set(const char *name, uint16_t millivolts) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 622 | { |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 623 | uint8_t voltage_index = voltage_to_index(name, millivolts); |
| 624 | const struct regul_struct *regul = get_regulator_data(name); |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 625 | uint8_t mask; |
| 626 | |
| 627 | /* Voltage can be set for buck<N> or ldo<N> (except ldo4) regulators */ |
| 628 | if (strncmp(name, "buck", 4) == 0) { |
| 629 | mask = BUCK_VOLTAGE_MASK; |
| 630 | } else if ((strncmp(name, "ldo", 3) == 0) && |
| 631 | (strncmp(name, "ldo4", 4) != 0)) { |
| 632 | mask = LDO_VOLTAGE_MASK; |
| 633 | } else { |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | return stpmic1_register_update(regul->control_reg, |
| 638 | voltage_index << LDO_BUCK_VOLTAGE_SHIFT, |
| 639 | mask); |
| 640 | } |
| 641 | |
| 642 | int stpmic1_regulator_pull_down_set(const char *name) |
| 643 | { |
| 644 | const struct regul_struct *regul = get_regulator_data(name); |
| 645 | |
| 646 | if (regul->pull_down_reg != 0) { |
| 647 | return stpmic1_register_update(regul->pull_down_reg, |
| 648 | BIT(regul->pull_down), |
| 649 | LDO_BUCK_PULL_DOWN_MASK << |
| 650 | regul->pull_down); |
| 651 | } |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | int stpmic1_regulator_mask_reset_set(const char *name) |
| 657 | { |
| 658 | const struct regul_struct *regul = get_regulator_data(name); |
| 659 | |
| 660 | return stpmic1_register_update(regul->mask_reset_reg, |
| 661 | BIT(regul->mask_reset), |
| 662 | LDO_BUCK_RESET_MASK << |
| 663 | regul->mask_reset); |
| 664 | } |
| 665 | |
| 666 | int stpmic1_regulator_voltage_get(const char *name) |
| 667 | { |
| 668 | const struct regul_struct *regul = get_regulator_data(name); |
| 669 | uint8_t value; |
| 670 | uint8_t mask; |
Yann Gautier | 328bba6 | 2020-03-23 17:44:30 +0100 | [diff] [blame] | 671 | int status; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 672 | |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 673 | /* Voltage can be set for buck<N> or ldo<N> (except ldo4) regulators */ |
| 674 | if (strncmp(name, "buck", 4) == 0) { |
| 675 | mask = BUCK_VOLTAGE_MASK; |
| 676 | } else if ((strncmp(name, "ldo", 3) == 0) && |
| 677 | (strncmp(name, "ldo4", 4) != 0)) { |
| 678 | mask = LDO_VOLTAGE_MASK; |
| 679 | } else { |
| 680 | return 0; |
| 681 | } |
| 682 | |
Yann Gautier | 328bba6 | 2020-03-23 17:44:30 +0100 | [diff] [blame] | 683 | status = stpmic1_register_read(regul->control_reg, &value); |
| 684 | if (status < 0) { |
| 685 | return status; |
| 686 | } |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 687 | |
| 688 | value = (value & mask) >> LDO_BUCK_VOLTAGE_SHIFT; |
| 689 | |
Yann Gautier | 328bba6 | 2020-03-23 17:44:30 +0100 | [diff] [blame] | 690 | if (value > regul->voltage_table_size) { |
| 691 | return -ERANGE; |
| 692 | } |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 693 | |
| 694 | return (int)regul->voltage_table[value]; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 695 | } |
| 696 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 697 | int stpmic1_register_read(uint8_t register_id, uint8_t *value) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 698 | { |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 699 | return stm32_i2c_mem_read(pmic_i2c_handle, pmic_i2c_addr, |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 700 | (uint16_t)register_id, |
| 701 | I2C_MEMADD_SIZE_8BIT, value, |
| 702 | 1, I2C_TIMEOUT_MS); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 703 | } |
| 704 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 705 | int stpmic1_register_write(uint8_t register_id, uint8_t value) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 706 | { |
| 707 | int status; |
| 708 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 709 | status = stm32_i2c_mem_write(pmic_i2c_handle, pmic_i2c_addr, |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 710 | (uint16_t)register_id, |
Yann Gautier | f3928f6 | 2019-02-14 11:15:03 +0100 | [diff] [blame] | 711 | I2C_MEMADD_SIZE_8BIT, &value, |
| 712 | 1, I2C_TIMEOUT_MS); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 713 | |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 714 | #if ENABLE_ASSERTIONS |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 715 | if (status != 0) { |
| 716 | return status; |
| 717 | } |
| 718 | |
| 719 | if ((register_id != WATCHDOG_CONTROL_REG) && (register_id <= 0x40U)) { |
| 720 | uint8_t readval; |
| 721 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 722 | status = stpmic1_register_read(register_id, &readval); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 723 | if (status != 0) { |
| 724 | return status; |
| 725 | } |
| 726 | |
| 727 | if (readval != value) { |
Yann Gautier | 328bba6 | 2020-03-23 17:44:30 +0100 | [diff] [blame] | 728 | return -EIO; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 729 | } |
| 730 | } |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 731 | #endif |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 732 | |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 733 | return status; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 734 | } |
| 735 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 736 | int stpmic1_register_update(uint8_t register_id, uint8_t value, uint8_t mask) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 737 | { |
| 738 | int status; |
| 739 | uint8_t val; |
| 740 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 741 | status = stpmic1_register_read(register_id, &val); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 742 | if (status != 0) { |
| 743 | return status; |
| 744 | } |
| 745 | |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 746 | val = (val & ~mask) | (value & mask); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 747 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 748 | return stpmic1_register_write(register_id, val); |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 749 | } |
| 750 | |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 751 | void stpmic1_bind_i2c(struct i2c_handle_s *i2c_handle, uint16_t i2c_addr) |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 752 | { |
Yann Gautier | a45433b | 2019-01-16 18:31:00 +0100 | [diff] [blame] | 753 | pmic_i2c_handle = i2c_handle; |
| 754 | pmic_i2c_addr = i2c_addr; |
Yann Gautier | bb836ee | 2018-07-16 17:55:07 +0200 | [diff] [blame] | 755 | } |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 756 | |
| 757 | void stpmic1_dump_regulators(void) |
| 758 | { |
| 759 | uint32_t i; |
| 760 | |
| 761 | for (i = 0U; i < MAX_REGUL; i++) { |
| 762 | const char *name __unused = regulators_table[i].dt_node_name; |
| 763 | |
| 764 | VERBOSE("PMIC regul %s: %sable, %dmV", |
| 765 | name, |
| 766 | stpmic1_is_regulator_enabled(name) ? "en" : "dis", |
| 767 | stpmic1_regulator_voltage_get(name)); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | int stpmic1_get_version(unsigned long *version) |
| 772 | { |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 773 | uint8_t read_val; |
Yann Gautier | 328bba6 | 2020-03-23 17:44:30 +0100 | [diff] [blame] | 774 | int status; |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 775 | |
Yann Gautier | 328bba6 | 2020-03-23 17:44:30 +0100 | [diff] [blame] | 776 | status = stpmic1_register_read(VERSION_STATUS_REG, &read_val); |
| 777 | if (status < 0) { |
| 778 | return status; |
Yann Gautier | ef85e53 | 2019-01-17 14:27:50 +0100 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | *version = (unsigned long)read_val; |
| 782 | |
| 783 | return 0; |
| 784 | } |