Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2012 |
| 3 | * Henrik Nordstrom <henrik@henriknordstrom.net> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <i2c.h> |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame^] | 10 | #include <axp_pmic.h> |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 11 | |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 12 | static int axp209_write(enum axp209_reg reg, u8 val) |
| 13 | { |
| 14 | return i2c_write(0x34, reg, 1, &val, 1); |
| 15 | } |
| 16 | |
| 17 | static int axp209_read(enum axp209_reg reg, u8 *val) |
| 18 | { |
| 19 | return i2c_read(0x34, reg, 1, val, 1); |
| 20 | } |
| 21 | |
| 22 | static u8 axp209_mvolt_to_cfg(int mvolt, int min, int max, int div) |
| 23 | { |
| 24 | if (mvolt < min) |
| 25 | mvolt = min; |
| 26 | else if (mvolt > max) |
| 27 | mvolt = max; |
| 28 | |
| 29 | return (mvolt - min) / div; |
| 30 | } |
| 31 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame^] | 32 | int axp_set_dcdc2(unsigned int mvolt) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 33 | { |
| 34 | int rc; |
| 35 | u8 cfg, current; |
| 36 | |
| 37 | cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25); |
| 38 | |
| 39 | /* Do we really need to be this gentle? It has built-in voltage slope */ |
| 40 | while ((rc = axp209_read(AXP209_DCDC2_VOLTAGE, ¤t)) == 0 && |
| 41 | current != cfg) { |
| 42 | if (current < cfg) |
| 43 | current++; |
| 44 | else |
| 45 | current--; |
| 46 | |
| 47 | rc = axp209_write(AXP209_DCDC2_VOLTAGE, current); |
| 48 | if (rc) |
| 49 | break; |
| 50 | } |
| 51 | |
| 52 | return rc; |
| 53 | } |
| 54 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame^] | 55 | int axp_set_dcdc3(unsigned int mvolt) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 56 | { |
| 57 | u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25); |
| 58 | |
| 59 | return axp209_write(AXP209_DCDC3_VOLTAGE, cfg); |
| 60 | } |
| 61 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame^] | 62 | int axp_set_aldo2(unsigned int mvolt) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 63 | { |
| 64 | int rc; |
| 65 | u8 cfg, reg; |
| 66 | |
| 67 | cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100); |
| 68 | |
| 69 | rc = axp209_read(AXP209_LDO24_VOLTAGE, ®); |
| 70 | if (rc) |
| 71 | return rc; |
| 72 | |
| 73 | /* LDO2 configuration is in upper 4 bits */ |
| 74 | reg = (reg & 0x0f) | (cfg << 4); |
| 75 | return axp209_write(AXP209_LDO24_VOLTAGE, reg); |
| 76 | } |
| 77 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame^] | 78 | int axp_set_aldo3(unsigned int mvolt) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 79 | { |
| 80 | u8 cfg; |
| 81 | |
| 82 | if (mvolt == -1) |
| 83 | cfg = 0x80; /* determined by LDO3IN pin */ |
| 84 | else |
Iain Paton | 71a7b3b | 2015-03-25 16:03:26 +0000 | [diff] [blame] | 85 | cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25); |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 86 | |
| 87 | return axp209_write(AXP209_LDO3_VOLTAGE, cfg); |
| 88 | } |
| 89 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame^] | 90 | int axp_set_aldo4(unsigned int mvolt) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 91 | { |
| 92 | int rc; |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame^] | 93 | static const unsigned int vindex[] = { |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 94 | 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500, |
| 95 | 2700, 2800, 3000, 3100, 3200, 3300 |
| 96 | }; |
| 97 | u8 cfg, reg; |
| 98 | |
| 99 | /* Translate mvolt to register cfg value, requested <= selected */ |
| 100 | for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--); |
| 101 | |
| 102 | rc = axp209_read(AXP209_LDO24_VOLTAGE, ®); |
| 103 | if (rc) |
| 104 | return rc; |
| 105 | |
| 106 | /* LDO4 configuration is in lower 4 bits */ |
| 107 | reg = (reg & 0xf0) | (cfg << 0); |
| 108 | return axp209_write(AXP209_LDO24_VOLTAGE, reg); |
| 109 | } |
| 110 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame^] | 111 | int axp_init(void) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 112 | { |
| 113 | u8 ver; |
Hans de Goede | c004b9f | 2015-01-17 16:39:20 +0100 | [diff] [blame] | 114 | int i, rc; |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 115 | |
| 116 | rc = axp209_read(AXP209_CHIP_VERSION, &ver); |
| 117 | if (rc) |
| 118 | return rc; |
| 119 | |
| 120 | /* Low 4 bits is chip version */ |
| 121 | ver &= 0x0f; |
| 122 | |
| 123 | if (ver != 0x1) |
| 124 | return -1; |
| 125 | |
Hans de Goede | c004b9f | 2015-01-17 16:39:20 +0100 | [diff] [blame] | 126 | /* Mask all interrupts */ |
| 127 | for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) { |
| 128 | rc = axp209_write(i, 0); |
| 129 | if (rc) |
| 130 | return rc; |
| 131 | } |
| 132 | |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 133 | return 0; |
| 134 | } |