blob: df9bb66a7f9ca2d5605010f01d24edf1a3a079a1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Philip, Avinash09ee9e82013-08-30 16:28:43 -04002/*
3 * (C) Copyright 2011-2013
4 * Texas Instruments, <www.ti.com>
Philip, Avinash09ee9e82013-08-30 16:28:43 -04005 */
6
Philip, Avinash09ee9e82013-08-30 16:28:43 -04007#include <i2c.h>
8#include <power/tps65910.h>
9
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020010struct udevice *tps65910_dev __section(".data") = NULL;
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +010011
12static inline int tps65910_read_reg(int addr, uchar *buf)
13{
Igor Opaniukf7c91762021-02-09 13:52:45 +020014#if !CONFIG_IS_ENABLED(DM_I2C)
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +010015 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
27static inline int tps65910_write_reg(int addr, uchar *buf)
28{
Igor Opaniukf7c91762021-02-09 13:52:45 +020029#if !CONFIG_IS_ENABLED(DM_I2C)
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +010030 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
36int power_tps65910_init(unsigned char bus)
37{
Igor Opaniukf7c91762021-02-09 13:52:45 +020038#if CONFIG_IS_ENABLED(DM_I2C)
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +010039 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, Avinash09ee9e82013-08-30 16:28:43 -040051/*
52 * tps65910_set_i2c_control() - Set the TPS65910 to be controlled via the I2C
Wolfgang Denk62fb2b42021-09-27 17:42:39 +020053 * interface.
Philip, Avinash09ee9e82013-08-30 16:28:43 -040054 * @return: 0 on success, not 0 on failure
55 */
56int 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 Hiblot77a13972018-12-07 14:50:46 +010062 ret = tps65910_read_reg(TPS65910_DEVCTRL_REG, &buf);
Philip, Avinash09ee9e82013-08-30 16:28:43 -040063
64 if (ret)
65 return ret;
66
67 buf |= TPS65910_DEVCTRL_REG_SR_CTL_I2C_SEL_CTL_I2C;
68
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +010069 return tps65910_write_reg(TPS65910_DEVCTRL_REG, &buf);
Philip, Avinash09ee9e82013-08-30 16:28:43 -040070}
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 */
78int 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 Hiblot77a13972018-12-07 14:50:46 +010090 ret = tps65910_read_reg(reg_offset, &buf);
Philip, Avinash09ee9e82013-08-30 16:28:43 -040091 if (ret)
92 return ret;
93
94 buf &= ~TPS65910_OP_REG_CMD_MASK;
95
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +010096 ret = tps65910_write_reg(reg_offset, &buf);
Philip, Avinash09ee9e82013-08-30 16:28:43 -040097 if (ret)
98 return ret;
99
100 /* Configure VDDx OP Voltage */
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +0100101 ret = tps65910_read_reg(reg_offset, &buf);
Philip, Avinash09ee9e82013-08-30 16:28:43 -0400102 if (ret)
103 return ret;
104
105 buf &= ~TPS65910_OP_REG_SEL_MASK;
106 buf |= vddx_op_vol_sel;
107
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +0100108 ret = tps65910_write_reg(reg_offset, &buf);
Philip, Avinash09ee9e82013-08-30 16:28:43 -0400109 if (ret)
110 return ret;
111
Jean-Jacques Hiblot77a13972018-12-07 14:50:46 +0100112 ret = tps65910_read_reg(reg_offset, &buf);
Philip, Avinash09ee9e82013-08-30 16:28:43 -0400113 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}