Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 |
| 4 | * Henrik Nordstrom <henrik@henriknordstrom.net> |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 5 | */ |
Hans de Goede | d728da1 | 2015-12-20 16:10:01 +0100 | [diff] [blame] | 6 | #include <command.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 7 | #include <linux/errno.h> |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 8 | #include <asm/arch/pmic_bus.h> |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 9 | #include <axp_pmic.h> |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 10 | |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 11 | static u8 axp152_mvolt_to_target(int mvolt, int min, int max, int div) |
| 12 | { |
| 13 | if (mvolt < min) |
| 14 | mvolt = min; |
| 15 | else if (mvolt > max) |
| 16 | mvolt = max; |
| 17 | |
| 18 | return (mvolt - min) / div; |
| 19 | } |
| 20 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 21 | int axp_set_dcdc2(unsigned int mvolt) |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 22 | { |
| 23 | int rc; |
| 24 | u8 current, target; |
| 25 | |
| 26 | target = axp152_mvolt_to_target(mvolt, 700, 2275, 25); |
| 27 | |
| 28 | /* Do we really need to be this gentle? It has built-in voltage slope */ |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 29 | while ((rc = pmic_bus_read(AXP152_DCDC2_VOLTAGE, ¤t)) == 0 && |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 30 | current != target) { |
| 31 | if (current < target) |
| 32 | current++; |
| 33 | else |
| 34 | current--; |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 35 | rc = pmic_bus_write(AXP152_DCDC2_VOLTAGE, current); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 36 | if (rc) |
| 37 | break; |
| 38 | } |
| 39 | return rc; |
| 40 | } |
| 41 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 42 | int axp_set_dcdc3(unsigned int mvolt) |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 43 | { |
Hans de Goede | 88ccfd8 | 2014-10-13 14:51:40 +0200 | [diff] [blame] | 44 | u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 50); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 45 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 46 | return pmic_bus_write(AXP152_DCDC3_VOLTAGE, target); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 49 | int axp_set_dcdc4(unsigned int mvolt) |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 50 | { |
| 51 | u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 25); |
| 52 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 53 | return pmic_bus_write(AXP152_DCDC4_VOLTAGE, target); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 56 | int axp_set_aldo2(unsigned int mvolt) |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 57 | { |
| 58 | u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 100); |
| 59 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 60 | return pmic_bus_write(AXP152_LDO2_VOLTAGE, target); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 63 | int axp_init(void) |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 64 | { |
| 65 | u8 ver; |
| 66 | int rc; |
| 67 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 68 | rc = pmic_bus_init(); |
| 69 | if (rc) |
| 70 | return rc; |
| 71 | |
| 72 | rc = pmic_bus_read(AXP152_CHIP_VERSION, &ver); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 73 | if (rc) |
| 74 | return rc; |
| 75 | |
| 76 | if (ver != 0x05) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 77 | return -EINVAL; |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
Hans de Goede | d728da1 | 2015-12-20 16:10:01 +0100 | [diff] [blame] | 81 | |
Samuel Holland | 41a545f | 2021-08-22 18:18:05 -0500 | [diff] [blame] | 82 | #if !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF) |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 83 | int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Hans de Goede | d728da1 | 2015-12-20 16:10:01 +0100 | [diff] [blame] | 84 | { |
| 85 | pmic_bus_write(AXP152_SHUTDOWN, AXP152_POWEROFF); |
| 86 | |
| 87 | /* infinite loop during shutdown */ |
| 88 | while (1) {} |
| 89 | |
| 90 | /* not reached */ |
| 91 | return 0; |
| 92 | } |
Samuel Holland | 41a545f | 2021-08-22 18:18:05 -0500 | [diff] [blame] | 93 | #endif |