Hans de Goede | 2428920 | 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> |
| 8 | #include <i2c.h> |
| 9 | #include <axp152.h> |
| 10 | |
Hans de Goede | 2428920 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 11 | static int axp152_write(enum axp152_reg reg, u8 val) |
| 12 | { |
| 13 | return i2c_write(0x30, reg, 1, &val, 1); |
| 14 | } |
| 15 | |
| 16 | static int axp152_read(enum axp152_reg reg, u8 *val) |
| 17 | { |
| 18 | return i2c_read(0x30, reg, 1, val, 1); |
| 19 | } |
| 20 | |
| 21 | static u8 axp152_mvolt_to_target(int mvolt, int min, int max, int div) |
| 22 | { |
| 23 | if (mvolt < min) |
| 24 | mvolt = min; |
| 25 | else if (mvolt > max) |
| 26 | mvolt = max; |
| 27 | |
| 28 | return (mvolt - min) / div; |
| 29 | } |
| 30 | |
| 31 | int axp152_set_dcdc2(int mvolt) |
| 32 | { |
| 33 | int rc; |
| 34 | u8 current, target; |
| 35 | |
| 36 | target = axp152_mvolt_to_target(mvolt, 700, 2275, 25); |
| 37 | |
| 38 | /* Do we really need to be this gentle? It has built-in voltage slope */ |
| 39 | while ((rc = axp152_read(AXP152_DCDC2_VOLTAGE, ¤t)) == 0 && |
| 40 | current != target) { |
| 41 | if (current < target) |
| 42 | current++; |
| 43 | else |
| 44 | current--; |
| 45 | rc = axp152_write(AXP152_DCDC2_VOLTAGE, current); |
| 46 | if (rc) |
| 47 | break; |
| 48 | } |
| 49 | return rc; |
| 50 | } |
| 51 | |
| 52 | int axp152_set_dcdc3(int mvolt) |
| 53 | { |
Hans de Goede | 74bf796 | 2014-10-13 14:51:40 +0200 | [diff] [blame] | 54 | u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 50); |
Hans de Goede | 2428920 | 2014-06-13 22:55:51 +0200 | [diff] [blame] | 55 | |
| 56 | return axp152_write(AXP152_DCDC3_VOLTAGE, target); |
| 57 | } |
| 58 | |
| 59 | int axp152_set_dcdc4(int mvolt) |
| 60 | { |
| 61 | u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 25); |
| 62 | |
| 63 | return axp152_write(AXP152_DCDC4_VOLTAGE, target); |
| 64 | } |
| 65 | |
| 66 | int axp152_set_ldo2(int mvolt) |
| 67 | { |
| 68 | u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 100); |
| 69 | |
| 70 | return axp152_write(AXP152_LDO2_VOLTAGE, target); |
| 71 | } |
| 72 | |
| 73 | int axp152_init(void) |
| 74 | { |
| 75 | u8 ver; |
| 76 | int rc; |
| 77 | |
| 78 | rc = axp152_read(AXP152_CHIP_VERSION, &ver); |
| 79 | if (rc) |
| 80 | return rc; |
| 81 | |
| 82 | if (ver != 0x05) |
| 83 | return -1; |
| 84 | |
| 85 | return 0; |
| 86 | } |