Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2011-2013 |
| 4 | * Texas Instruments, <www.ti.com> |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 7 | #include <i2c.h> |
| 8 | #include <power/tps65910.h> |
| 9 | |
Marek BehĂșn | 4bebdd3 | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 10 | struct udevice *tps65910_dev __section(".data") = NULL; |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 11 | |
| 12 | static inline int tps65910_read_reg(int addr, uchar *buf) |
| 13 | { |
Igor Opaniuk | f7c9176 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 14 | #if !CONFIG_IS_ENABLED(DM_I2C) |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 15 | return i2c_read(TPS65910_CTRL_I2C_ADDR, addr, 1, buf, 1); |
| 16 | #else |
| 17 | int rc; |
| 18 | |
| 19 | rc = dm_i2c_reg_read(tps65910_dev, addr); |
| 20 | if (rc < 0) |
| 21 | return rc; |
| 22 | *buf = (uchar)rc; |
| 23 | return 0; |
| 24 | #endif |
| 25 | } |
| 26 | |
| 27 | static inline int tps65910_write_reg(int addr, uchar *buf) |
| 28 | { |
Igor Opaniuk | f7c9176 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 29 | #if !CONFIG_IS_ENABLED(DM_I2C) |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 30 | return i2c_write(TPS65910_CTRL_I2C_ADDR, addr, 1, buf, 1); |
| 31 | #else |
| 32 | return dm_i2c_reg_write(tps65910_dev, addr, *buf); |
| 33 | #endif |
| 34 | } |
| 35 | |
| 36 | int power_tps65910_init(unsigned char bus) |
| 37 | { |
Igor Opaniuk | f7c9176 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 38 | #if CONFIG_IS_ENABLED(DM_I2C) |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 39 | struct udevice *dev = NULL; |
| 40 | int rc; |
| 41 | |
| 42 | rc = i2c_get_chip_for_busnum(bus, TPS65910_CTRL_I2C_ADDR, 1, &dev); |
| 43 | |
| 44 | if (rc) |
| 45 | return rc; |
| 46 | tps65910_dev = dev; |
| 47 | #endif |
| 48 | return 0; |
| 49 | } |
| 50 | |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 51 | /* |
| 52 | * tps65910_set_i2c_control() - Set the TPS65910 to be controlled via the I2C |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 53 | * interface. |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 54 | * @return: 0 on success, not 0 on failure |
| 55 | */ |
| 56 | int tps65910_set_i2c_control(void) |
| 57 | { |
| 58 | int ret; |
| 59 | uchar buf; |
| 60 | |
| 61 | /* VDD1/2 voltage selection register access by control i/f */ |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 62 | ret = tps65910_read_reg(TPS65910_DEVCTRL_REG, &buf); |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 63 | |
| 64 | if (ret) |
| 65 | return ret; |
| 66 | |
| 67 | buf |= TPS65910_DEVCTRL_REG_SR_CTL_I2C_SEL_CTL_I2C; |
| 68 | |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 69 | return tps65910_write_reg(TPS65910_DEVCTRL_REG, &buf); |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* |
| 73 | * tps65910_voltage_update() - Voltage switching for MPU frequency switching. |
| 74 | * @module: mpu - 0, core - 1 |
| 75 | * @vddx_op_vol_sel: vdd voltage to set |
| 76 | * @return: 0 on success, not 0 on failure |
| 77 | */ |
| 78 | int tps65910_voltage_update(unsigned int module, unsigned char vddx_op_vol_sel) |
| 79 | { |
| 80 | uchar buf; |
| 81 | unsigned int reg_offset; |
| 82 | int ret; |
| 83 | |
| 84 | if (module == MPU) |
| 85 | reg_offset = TPS65910_VDD1_OP_REG; |
| 86 | else |
| 87 | reg_offset = TPS65910_VDD2_OP_REG; |
| 88 | |
| 89 | /* Select VDDx OP */ |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 90 | ret = tps65910_read_reg(reg_offset, &buf); |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 91 | if (ret) |
| 92 | return ret; |
| 93 | |
| 94 | buf &= ~TPS65910_OP_REG_CMD_MASK; |
| 95 | |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 96 | ret = tps65910_write_reg(reg_offset, &buf); |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 97 | if (ret) |
| 98 | return ret; |
| 99 | |
| 100 | /* Configure VDDx OP Voltage */ |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 101 | ret = tps65910_read_reg(reg_offset, &buf); |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 102 | if (ret) |
| 103 | return ret; |
| 104 | |
| 105 | buf &= ~TPS65910_OP_REG_SEL_MASK; |
| 106 | buf |= vddx_op_vol_sel; |
| 107 | |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 108 | ret = tps65910_write_reg(reg_offset, &buf); |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 109 | if (ret) |
| 110 | return ret; |
| 111 | |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 112 | ret = tps65910_read_reg(reg_offset, &buf); |
Philip, Avinash | 09ee9e8 | 2013-08-30 16:28:43 -0400 | [diff] [blame] | 113 | if (ret) |
| 114 | return ret; |
| 115 | |
| 116 | if ((buf & TPS65910_OP_REG_SEL_MASK) != vddx_op_vol_sel) |
| 117 | return 1; |
| 118 | |
| 119 | return 0; |
| 120 | } |