Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2012 |
| 3 | * Henrik Nordstrom <henrik@henriknordstrom.net> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | #include <common.h> |
Hans de Goede | d728da1 | 2015-12-20 16:10:01 +0100 | [diff] [blame] | 8 | #include <command.h> |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 9 | #include <asm/arch/pmic_bus.h> |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 10 | #include <axp_pmic.h> |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 11 | |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 12 | static u8 axp152_mvolt_to_target(int mvolt, int min, int max, int div) |
| 13 | { |
| 14 | if (mvolt < min) |
| 15 | mvolt = min; |
| 16 | else if (mvolt > max) |
| 17 | mvolt = max; |
| 18 | |
| 19 | return (mvolt - min) / div; |
| 20 | } |
| 21 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 22 | int axp_set_dcdc2(unsigned int mvolt) |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 23 | { |
| 24 | int rc; |
| 25 | u8 current, target; |
| 26 | |
| 27 | target = axp152_mvolt_to_target(mvolt, 700, 2275, 25); |
| 28 | |
| 29 | /* 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] | 30 | while ((rc = pmic_bus_read(AXP152_DCDC2_VOLTAGE, ¤t)) == 0 && |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 31 | current != target) { |
| 32 | if (current < target) |
| 33 | current++; |
| 34 | else |
| 35 | current--; |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 36 | rc = pmic_bus_write(AXP152_DCDC2_VOLTAGE, current); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 37 | if (rc) |
| 38 | break; |
| 39 | } |
| 40 | return rc; |
| 41 | } |
| 42 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 43 | int axp_set_dcdc3(unsigned int mvolt) |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 44 | { |
Hans de Goede | 88ccfd8 | 2014-10-13 14:51:40 +0200 | [diff] [blame] | 45 | u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 50); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 46 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 47 | return pmic_bus_write(AXP152_DCDC3_VOLTAGE, target); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 50 | int axp_set_dcdc4(unsigned int mvolt) |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 51 | { |
| 52 | u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 25); |
| 53 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 54 | return pmic_bus_write(AXP152_DCDC4_VOLTAGE, target); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 57 | int axp_set_aldo2(unsigned int mvolt) |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 58 | { |
| 59 | u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 100); |
| 60 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 61 | return pmic_bus_write(AXP152_LDO2_VOLTAGE, target); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 64 | int axp_init(void) |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 65 | { |
| 66 | u8 ver; |
| 67 | int rc; |
| 68 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 69 | rc = pmic_bus_init(); |
| 70 | if (rc) |
| 71 | return rc; |
| 72 | |
| 73 | rc = pmic_bus_read(AXP152_CHIP_VERSION, &ver); |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 74 | if (rc) |
| 75 | return rc; |
| 76 | |
| 77 | if (ver != 0x05) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 78 | return -EINVAL; |
Hans de Goede | 1f24736 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
Hans de Goede | d728da1 | 2015-12-20 16:10:01 +0100 | [diff] [blame] | 82 | |
| 83 | int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 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 | } |