1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2012-2013
4  * Texas Instruments, <www.ti.com>
5  */
6 #include <config.h>
7 #include <palmas.h>
8 
palmas_init_settings(void)9 void palmas_init_settings(void)
10 {
11 #ifdef CONFIG_PALMAS_SMPS7_FPWM
12 	int err;
13 	/*
14 	 * Set SMPS7 (1.8 V I/O supply on platforms with TWL6035/37) to
15 	 * forced PWM mode. This reduces noise (but affects efficiency).
16 	 */
17 	u8 val = SMPS_MODE_SLP_FPWM | SMPS_MODE_ACT_FPWM;
18 	err = palmas_i2c_write_u8(TWL603X_CHIP_P1, SMPS7_CTRL, val);
19 	if (err)
20 		printf("palmas: could not force PWM for SMPS7: err = %d\n",
21 		       err);
22 #endif
23 }
24 
25 #if defined(CONFIG_OMAP54XX)
lp873x_mmc1_poweron_ldo(uint voltage)26 int lp873x_mmc1_poweron_ldo(uint voltage)
27 {
28 	if (palmas_i2c_write_u8(LP873X_LDO1_ADDR, LP873X_LDO1_VOLTAGE,
29 				voltage)) {
30 		printf("lp873x: could not set LDO1 voltage.\n");
31 		return 1;
32 	}
33 	/* TURN ON LDO1 */
34 	if (palmas_i2c_write_u8(LP873X_LDO1_ADDR, LP873X_LDO1_CTRL,
35 				LP873X_LDO_CTRL_EN | LP873X_LDO_CTRL_RDIS_EN)) {
36 		printf("lp873x: could not turn on LDO1.\n");
37 		return 1;
38 	}
39 	return 0;
40 
41 }
42 #endif
43 
palmas_mmc1_poweron_ldo(uint ldo_volt,uint ldo_ctrl,uint voltage)44 int palmas_mmc1_poweron_ldo(uint ldo_volt, uint ldo_ctrl, uint voltage)
45 {
46 	u8 val = 0;
47 
48 #if defined(CONFIG_DRA7XX)
49 	int ret;
50 
51 	ret = palmas_i2c_write_u8(TPS65903X_CHIP_P1, ldo_volt, voltage);
52 	if (ret) {
53 		printf("tps65903x: could not set LDO1 voltage.\n");
54 		return ret;
55 	}
56 	/* TURN ON LDO1 */
57 	val = RSC_MODE_SLEEP | RSC_MODE_ACTIVE;
58 	ret = palmas_i2c_write_u8(TPS65903X_CHIP_P1, ldo_ctrl, val);
59 	if (ret) {
60 		printf("tps65903x: could not turn on LDO1.\n");
61 		return ret;
62 	}
63 	return 0;
64 #else
65 	/*
66 	 * We assume that this is a OMAP543X + TWL603X board:
67 	 * Set TWL6035/37 LDO9 to 3.0 V
68 	 */
69 	val = LDO_VOLT_3V0;
70 	return twl603x_mmc1_set_ldo9(val);
71 #endif
72 }
73 
74 /*
75  * On some OMAP5 + TWL603X hardware the SD card socket and LDO9_IN are
76  * powered by an external 3.3 V regulator, while the output of LDO9
77  * supplies VDDS_SDCARD for the OMAP5 interface only. This implies that
78  * LDO9 could be set to 'bypass' mode when required (e.g. for 3.3 V cards).
79  */
twl603x_mmc1_set_ldo9(u8 vsel)80 int twl603x_mmc1_set_ldo9(u8 vsel)
81 {
82 	u8 cval = 0, vval = 0;	/* Off by default */
83 	int err;
84 
85 	if (vsel) {
86 		/* Turn on */
87 		if (vsel > LDO_VOLT_3V3) {
88 			/* Put LDO9 in bypass */
89 			cval = LDO9_BYP_EN | RSC_MODE_SLEEP | RSC_MODE_ACTIVE;
90 			vval = LDO_VOLT_3V3;
91 		} else {
92 			cval = RSC_MODE_SLEEP | RSC_MODE_ACTIVE;
93 			vval = vsel & 0x3f;
94 		}
95 	}
96 	err = palmas_i2c_write_u8(TWL603X_CHIP_P1, LDO9_VOLTAGE, vval);
97 	if (err) {
98 		printf("twl603x: could not set LDO9 %s: err = %d\n",
99 		       vsel > LDO_VOLT_3V3 ? "bypass" : "voltage", err);
100 		return err;
101 	}
102 	err = palmas_i2c_write_u8(TWL603X_CHIP_P1, LDO9_CTRL, cval);
103 	if (err)
104 		printf("twl603x: could not turn %s LDO9: err = %d\n",
105 		       cval ? "on" : "off", err);
106 	return err;
107 }
108 
109 #ifdef CONFIG_PALMAS_AUDPWR
110 /*
111  * Turn audio codec power and 32 kHz clock on/off. Use for
112  * testing OMAP543X + TWL603X + TWL604X boards only.
113  */
twl603x_audio_power(u8 on)114 int twl603x_audio_power(u8 on)
115 {
116 	u8 cval = 0, vval = 0, c32k = 0;
117 	int err;
118 
119 	if (on) {
120 		vval = SMPS_VOLT_2V1;
121 		cval = SMPS_MODE_SLP_AUTO | SMPS_MODE_ACT_AUTO;
122 		c32k = RSC_MODE_SLEEP | RSC_MODE_ACTIVE;
123 	}
124 	/* Set SMPS9 to 2.1 V (for TWL604x), or to 0 (off) */
125 	err = palmas_i2c_write_u8(TWL603X_CHIP_P1, SMPS9_VOLTAGE, vval);
126 	if (err) {
127 		printf("twl603x: could not set SMPS9 voltage: err = %d\n",
128 		       err);
129 		return err;
130 	}
131 	/* Turn on or off SMPS9 */
132 	err = palmas_i2c_write_u8(TWL603X_CHIP_P1, SMPS9_CTRL, cval);
133 	if (err) {
134 		printf("twl603x: could not turn SMPS9 %s: err = %d\n",
135 		       cval ? "on" : "off", err);
136 		return err;
137 	}
138 	/* Output 32 kHz clock on or off */
139 	err = palmas_i2c_write_u8(TWL603X_CHIP_P1, CLK32KGAUDIO_CTRL, c32k);
140 	if (err)
141 		printf("twl603x: could not turn CLK32KGAUDIO %s: err = %d\n",
142 		       c32k ? "on" : "off", err);
143 	return err;
144 }
145 #endif
146 
147 #ifdef CONFIG_PALMAS_USB_SS_PWR
148 /**
149  * @brief palmas_enable_ss_ldo - Configure EVM board specific configurations
150  * for the USB Super speed SMPS10 regulator.
151  *
152  * @return 0
153  */
palmas_enable_ss_ldo(void)154 int palmas_enable_ss_ldo(void)
155 {
156 	/* Enable smps10 regulator  */
157 	return palmas_i2c_write_u8(TWL603X_CHIP_P1, SMPS10_CTRL,
158 				SMPS10_MODE_ACTIVE_D);
159 }
160 #endif
161 
162 /*
163  * Enable/disable back-up battery (or super cap) charging on TWL6035/37.
164  * Please use defined BB_xxx values.
165  */
twl603x_enable_bb_charge(u8 bb_fields)166 int twl603x_enable_bb_charge(u8 bb_fields)
167 {
168 	u8 val = bb_fields & 0x0f;
169 	int err;
170 
171 	val |= (VRTC_EN_SLP | VRTC_EN_OFF | VRTC_PWEN);
172 	err = palmas_i2c_write_u8(TWL603X_CHIP_P1, BB_VRTC_CTRL, val);
173 	if (err)
174 		printf("twl603x: could not set BB_VRTC_CTRL to 0x%02x: err = %d\n",
175 		       val, err);
176 	return err;
177 }
178 
179 #ifdef CONFIG_DM_I2C
palmas_i2c_write_u8(u8 chip_no,u8 reg,u8 val)180 int palmas_i2c_write_u8(u8 chip_no, u8 reg, u8 val)
181 {
182 	struct udevice *dev;
183 	int ret;
184 
185 	ret = i2c_get_chip_for_busnum(0, chip_no, 1, &dev);
186 	if (ret) {
187 		pr_err("unable to get I2C bus. ret %d\n", ret);
188 		return ret;
189 	}
190 	ret = dm_i2c_reg_write(dev, reg, val);
191 	if (ret) {
192 		pr_err("writing to palmas failed. ret %d\n", ret);
193 		return ret;
194 	}
195 	return 0;
196 }
197 
palmas_i2c_read_u8(u8 chip_no,u8 reg,u8 * valp)198 int palmas_i2c_read_u8(u8 chip_no, u8 reg, u8 *valp)
199 {
200 	struct udevice *dev;
201 	int ret;
202 
203 	ret = i2c_get_chip_for_busnum(0, chip_no, 1, &dev);
204 	if (ret) {
205 		pr_err("unable to get I2C bus. ret %d\n", ret);
206 		return ret;
207 	}
208 	ret = dm_i2c_reg_read(dev, reg);
209 	if (ret < 0) {
210 		pr_err("reading from palmas failed. ret %d\n", ret);
211 		return ret;
212 	}
213 	*valp = (u8)ret;
214 	return 0;
215 }
216 #endif
217