Tom Rini | 0634fc0 | 2014-06-05 11:15:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011-2013 |
| 3 | * Texas Instruments, <www.ti.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <i2c.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 10 | #include <linux/errno.h> |
Tom Rini | 60d2f6f | 2014-06-23 16:06:29 -0400 | [diff] [blame] | 11 | #include <power/pmic.h> |
Tom Rini | 0634fc0 | 2014-06-05 11:15:29 -0400 | [diff] [blame] | 12 | #include <power/tps65218.h> |
| 13 | |
Nikita Kiryanov | a5c71ff | 2016-02-19 19:19:46 +0200 | [diff] [blame] | 14 | int tps65218_reg_read(uchar dest_reg, uchar *dest_val) |
| 15 | { |
| 16 | uchar read_val; |
| 17 | int ret; |
| 18 | |
| 19 | ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1); |
| 20 | if (ret) |
| 21 | return ret; |
| 22 | |
| 23 | *dest_val = read_val; |
| 24 | |
| 25 | return 0; |
| 26 | } |
| 27 | |
Tom Rini | 0634fc0 | 2014-06-05 11:15:29 -0400 | [diff] [blame] | 28 | /** |
| 29 | * tps65218_reg_write() - Generic function that can write a TPS65218 PMIC |
| 30 | * register or bit field regardless of protection |
| 31 | * level. |
| 32 | * |
| 33 | * @prot_level: Register password protection. Use |
| 34 | * TPS65218_PROT_LEVEL_NONE, |
| 35 | * TPS65218_PROT_LEVEL_1 or TPS65218_PROT_LEVEL_2 |
| 36 | * @dest_reg: Register address to write. |
| 37 | * @dest_val: Value to write. |
| 38 | * @mask: Bit mask (8 bits) to be applied. Function will only |
| 39 | * change bits that are set in the bit mask. |
| 40 | * |
| 41 | * @return: 0 for success, not 0 on failure, as per the i2c API |
| 42 | */ |
| 43 | int tps65218_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val, |
| 44 | uchar mask) |
| 45 | { |
| 46 | uchar read_val; |
| 47 | uchar xor_reg; |
| 48 | int ret; |
| 49 | |
| 50 | /* |
| 51 | * If we are affecting only a bit field, read dest_reg and apply the |
| 52 | * mask |
| 53 | */ |
| 54 | if (mask != TPS65218_MASK_ALL_BITS) { |
| 55 | ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1); |
| 56 | if (ret) |
| 57 | return ret; |
| 58 | read_val &= (~mask); |
| 59 | read_val |= (dest_val & mask); |
| 60 | dest_val = read_val; |
| 61 | } |
| 62 | |
| 63 | if (prot_level > 0) { |
| 64 | xor_reg = dest_reg ^ TPS65218_PASSWORD_UNLOCK; |
| 65 | ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1, |
| 66 | &xor_reg, 1); |
| 67 | if (ret) |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1); |
| 72 | if (ret) |
| 73 | return ret; |
| 74 | |
| 75 | if (prot_level == TPS65218_PROT_LEVEL_2) { |
| 76 | ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1, |
| 77 | &xor_reg, 1); |
| 78 | if (ret) |
| 79 | return ret; |
| 80 | |
| 81 | ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1); |
| 82 | if (ret) |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * tps65218_voltage_update() - Function to change a voltage level, as this |
| 91 | * is a multi-step process. |
| 92 | * @dc_cntrl_reg: DC voltage control register to change. |
| 93 | * @volt_sel: New value for the voltage register |
| 94 | * @return: 0 for success, not 0 on failure. |
| 95 | */ |
| 96 | int tps65218_voltage_update(uchar dc_cntrl_reg, uchar volt_sel) |
| 97 | { |
| 98 | if ((dc_cntrl_reg != TPS65218_DCDC1) && |
| 99 | (dc_cntrl_reg != TPS65218_DCDC2)) |
| 100 | return 1; |
| 101 | |
| 102 | /* set voltage level */ |
| 103 | if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, dc_cntrl_reg, volt_sel, |
| 104 | TPS65218_MASK_ALL_BITS)) |
| 105 | return 1; |
| 106 | |
| 107 | /* set GO bit to initiate voltage transition */ |
| 108 | if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, TPS65218_SLEW, |
| 109 | TPS65218_DCDC_GO, TPS65218_DCDC_GO)) |
| 110 | return 1; |
| 111 | |
| 112 | return 0; |
| 113 | } |
Tom Rini | 60d2f6f | 2014-06-23 16:06:29 -0400 | [diff] [blame] | 114 | |
Nikita Kiryanov | a5c71ff | 2016-02-19 19:19:46 +0200 | [diff] [blame] | 115 | /** |
| 116 | * tps65218_toggle_fseal() - Perform the sequence that toggles the FSEAL bit. |
| 117 | * |
| 118 | * @return: 0 on success, -EBADE if the sequence was broken |
| 119 | */ |
| 120 | int tps65218_toggle_fseal(void) |
| 121 | { |
| 122 | if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD, |
| 123 | 0xb1, TPS65218_MASK_ALL_BITS)) |
| 124 | return -EBADE; |
| 125 | |
| 126 | if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD, |
| 127 | 0xfe, TPS65218_MASK_ALL_BITS)) |
| 128 | return -EBADE; |
| 129 | |
| 130 | if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD, |
| 131 | 0xa3, TPS65218_MASK_ALL_BITS)) |
| 132 | return -EBADE; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * tps65218_lock_fseal() - Perform the sequence that locks the FSEAL bit to 1. |
| 139 | * |
| 140 | * The FSEAL bit prevents the PMIC from turning off DCDC5 and DCDC6. It can be |
| 141 | * toggled at most 3 times: 0->1, 1->0, and finally 0->1. After the third switch |
| 142 | * its value is locked and can only be reset by powering off the PMIC entirely. |
| 143 | * |
| 144 | * @return: 0 on success, -EBADE if the sequence was broken |
| 145 | */ |
| 146 | int tps65218_lock_fseal(void) |
| 147 | { |
| 148 | int i; |
| 149 | |
| 150 | for (i = 0; i < 3; i++) |
| 151 | if (tps65218_toggle_fseal()) |
| 152 | return -EBADE; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Tom Rini | 60d2f6f | 2014-06-23 16:06:29 -0400 | [diff] [blame] | 157 | int power_tps65218_init(unsigned char bus) |
| 158 | { |
| 159 | static const char name[] = "TPS65218_PMIC"; |
| 160 | struct pmic *p = pmic_alloc(); |
| 161 | |
| 162 | if (!p) { |
| 163 | printf("%s: POWER allocation error!\n", __func__); |
| 164 | return -ENOMEM; |
| 165 | } |
| 166 | |
| 167 | p->name = name; |
| 168 | p->interface = PMIC_I2C; |
| 169 | p->number_of_regs = TPS65218_PMIC_NUM_OF_REGS; |
| 170 | p->hw.i2c.addr = TPS65218_CHIP_PM; |
| 171 | p->hw.i2c.tx_num = 1; |
| 172 | p->bus = bus; |
| 173 | |
| 174 | return 0; |
| 175 | } |