blob: 0c7c3960904cb96c6dbce96ba051a7c52bc11385 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Rixcd782632009-06-28 12:52:29 -05002/*
3 * Copyright (c) 2009 Wind River Systems, Inc.
4 * Tom Rix <Tom.Rix at windriver.com>
5 *
Tom Rix2c155132009-06-28 12:52:30 -05006 * twl4030_power_reset_init is derived from code on omapzoom,
7 * git://git.omapzoom.com/repo/u-boot.git
Tom Rixcd782632009-06-28 12:52:29 -05008 *
9 * Copyright (C) 2007-2009 Texas Instruments, Inc.
Tom Rix2c155132009-06-28 12:52:30 -050010 *
11 * twl4030_power_init is from cpu/omap3/common.c, power_init_r
12 *
13 * (C) Copyright 2004-2008
14 * Texas Instruments, <www.ti.com>
15 *
16 * Author :
17 * Sunil Kumar <sunilsaini05 at gmail.com>
18 * Shashi Ranjan <shashiranjanmca05 at gmail.com>
19 *
20 * Derived from Beagle Board and 3430 SDP code by
21 * Richard Woodruff <r-woodruff2 at ti.com>
22 * Syed Mohammed Khasim <khasim at ti.com>
Tom Rixcd782632009-06-28 12:52:29 -050023 */
24
Simon Glass09140112020-05-10 11:40:03 -060025#include <command.h>
Tom Rixcd782632009-06-28 12:52:29 -050026#include <twl4030.h>
Simon Glassc05ed002020-05-10 11:40:11 -060027#include <linux/delay.h>
Simon Glass1e94b462023-09-14 18:21:46 -060028#include <linux/printk.h>
Tom Rixcd782632009-06-28 12:52:29 -050029
30/*
31 * Power Reset
32 */
33void twl4030_power_reset_init(void)
34{
35 u8 val = 0;
Nishanth Menonb29c2f02013-03-26 05:20:50 +000036 if (twl4030_i2c_read_u8(TWL4030_CHIP_PM_MASTER,
37 TWL4030_PM_MASTER_P1_SW_EVENTS, &val)) {
Tom Rixcd782632009-06-28 12:52:29 -050038 printf("Error:TWL4030: failed to read the power register\n");
39 printf("Could not initialize hardware reset\n");
40 } else {
41 val |= TWL4030_PM_MASTER_SW_EVENTS_STOPON_PWRON;
Nishanth Menon0208aaf2013-03-26 05:20:49 +000042 if (twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
43 TWL4030_PM_MASTER_P1_SW_EVENTS, val)) {
Tom Rixcd782632009-06-28 12:52:29 -050044 printf("Error:TWL4030: failed to write the power register\n");
45 printf("Could not initialize hardware reset\n");
46 }
47 }
48}
49
Tom Rix2c155132009-06-28 12:52:30 -050050/*
Paul Kocialkowski6dc443e2015-07-20 15:17:07 +020051 * Power off
52 */
53void twl4030_power_off(void)
54{
55 u8 data;
56
57 /* PM master unlock (CFG and TST keys) */
58
59 data = 0xCE;
60 twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
61 TWL4030_PM_MASTER_PROTECT_KEY, data);
62 data = 0xEC;
63 twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
64 TWL4030_PM_MASTER_PROTECT_KEY, data);
65
66 /* VBAT start disable */
67
68 twl4030_i2c_read_u8(TWL4030_CHIP_PM_MASTER,
69 TWL4030_PM_MASTER_CFG_P1_TRANSITION, &data);
70 data &= ~TWL4030_PM_MASTER_CFG_TRANSITION_STARTON_VBAT;
71 twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
72 TWL4030_PM_MASTER_CFG_P1_TRANSITION, data);
73
74 twl4030_i2c_read_u8(TWL4030_CHIP_PM_MASTER,
75 TWL4030_PM_MASTER_CFG_P2_TRANSITION, &data);
76 data &= ~TWL4030_PM_MASTER_CFG_TRANSITION_STARTON_VBAT;
77 twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
78 TWL4030_PM_MASTER_CFG_P2_TRANSITION, data);
79
80 twl4030_i2c_read_u8(TWL4030_CHIP_PM_MASTER,
81 TWL4030_PM_MASTER_CFG_P3_TRANSITION, &data);
82 data &= ~TWL4030_PM_MASTER_CFG_TRANSITION_STARTON_VBAT;
83 twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
84 TWL4030_PM_MASTER_CFG_P3_TRANSITION, data);
85
86 /* High jitter for PWRANA2 */
87
88 twl4030_i2c_read_u8(TWL4030_CHIP_PM_MASTER,
89 TWL4030_PM_MASTER_CFG_PWRANA2, &data);
90 data &= ~(TWL4030_PM_MASTER_CFG_PWRANA2_LOJIT0_LOWV |
91 TWL4030_PM_MASTER_CFG_PWRANA2_LOJIT1_LOWV);
92 twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
93 TWL4030_PM_MASTER_CFG_PWRANA2, data);
94
95 /* PM master lock */
96
97 data = 0xFF;
98 twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
99 TWL4030_PM_MASTER_PROTECT_KEY, data);
100
101 /* Power off */
102
103 twl4030_i2c_read_u8(TWL4030_CHIP_PM_MASTER,
104 TWL4030_PM_MASTER_P1_SW_EVENTS, &data);
105 data |= TWL4030_PM_MASTER_SW_EVENTS_DEVOFF;
106 twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
107 TWL4030_PM_MASTER_P1_SW_EVENTS, data);
108}
109
110/*
Steve Sakoman5a0a82f2010-08-10 12:58:39 -0700111 * Set Device Group and Voltage
Tom Rix2c155132009-06-28 12:52:30 -0500112 */
Steve Sakoman5a0a82f2010-08-10 12:58:39 -0700113void twl4030_pmrecv_vsel_cfg(u8 vsel_reg, u8 vsel_val,
114 u8 dev_grp, u8 dev_grp_sel)
115{
Grazvydas Ignotas61712bc2012-03-19 03:37:40 +0000116 int ret;
Steve Sakoman5a0a82f2010-08-10 12:58:39 -0700117
118 /* Select the Voltage */
Nishanth Menon0208aaf2013-03-26 05:20:49 +0000119 ret = twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER, vsel_reg,
120 vsel_val);
Grazvydas Ignotas61712bc2012-03-19 03:37:40 +0000121 if (ret != 0) {
Peter Meerwalddfe36102012-11-19 23:13:04 +0000122 printf("Could not write vsel to reg %02x (%d)\n",
Grazvydas Ignotas61712bc2012-03-19 03:37:40 +0000123 vsel_reg, ret);
124 return;
125 }
126
127 /* Select the Device Group (enable the supply if dev_grp_sel != 0) */
Nishanth Menon0208aaf2013-03-26 05:20:49 +0000128 ret = twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER, dev_grp,
129 dev_grp_sel);
Grazvydas Ignotas61712bc2012-03-19 03:37:40 +0000130 if (ret != 0)
Peter Meerwalddfe36102012-11-19 23:13:04 +0000131 printf("Could not write grp_sel to reg %02x (%d)\n",
Grazvydas Ignotas61712bc2012-03-19 03:37:40 +0000132 dev_grp, ret);
Steve Sakoman5a0a82f2010-08-10 12:58:39 -0700133}
Tom Rix2c155132009-06-28 12:52:30 -0500134
135void twl4030_power_init(void)
136{
Tom Rix2c155132009-06-28 12:52:30 -0500137 /* set VAUX3 to 2.8V */
Steve Sakoman5a0a82f2010-08-10 12:58:39 -0700138 twl4030_pmrecv_vsel_cfg(TWL4030_PM_RECEIVER_VAUX3_DEDICATED,
139 TWL4030_PM_RECEIVER_VAUX3_VSEL_28,
140 TWL4030_PM_RECEIVER_VAUX3_DEV_GRP,
141 TWL4030_PM_RECEIVER_DEV_GRP_P1);
Tom Rix2c155132009-06-28 12:52:30 -0500142
143 /* set VPLL2 to 1.8V */
Steve Sakoman5a0a82f2010-08-10 12:58:39 -0700144 twl4030_pmrecv_vsel_cfg(TWL4030_PM_RECEIVER_VPLL2_DEDICATED,
145 TWL4030_PM_RECEIVER_VPLL2_VSEL_18,
146 TWL4030_PM_RECEIVER_VPLL2_DEV_GRP,
147 TWL4030_PM_RECEIVER_DEV_GRP_ALL);
Tom Rix2c155132009-06-28 12:52:30 -0500148
149 /* set VDAC to 1.8V */
Steve Sakoman5a0a82f2010-08-10 12:58:39 -0700150 twl4030_pmrecv_vsel_cfg(TWL4030_PM_RECEIVER_VDAC_DEDICATED,
151 TWL4030_PM_RECEIVER_VDAC_VSEL_18,
152 TWL4030_PM_RECEIVER_VDAC_DEV_GRP,
153 TWL4030_PM_RECEIVER_DEV_GRP_P1);
Tom Rix2c155132009-06-28 12:52:30 -0500154}
155
Paul Kocialkowskif3e85e42014-11-08 20:55:46 +0100156void twl4030_power_mmc_init(int dev_index)
Tom Rixfccc0fc2009-06-28 12:52:31 -0500157{
Paul Kocialkowskif3e85e42014-11-08 20:55:46 +0100158 if (dev_index == 0) {
159 /* Set VMMC1 to 3.15 Volts */
160 twl4030_pmrecv_vsel_cfg(TWL4030_PM_RECEIVER_VMMC1_DEDICATED,
161 TWL4030_PM_RECEIVER_VMMC1_VSEL_32,
162 TWL4030_PM_RECEIVER_VMMC1_DEV_GRP,
163 TWL4030_PM_RECEIVER_DEV_GRP_P1);
Paul Kocialkowski2ed8c872014-10-28 18:14:23 +0100164
Paul Kocialkowskif3e85e42014-11-08 20:55:46 +0100165 mdelay(100); /* ramp-up delay from Linux code */
166 } else if (dev_index == 1) {
167 /* Set VMMC2 to 3.15 Volts */
168 twl4030_pmrecv_vsel_cfg(TWL4030_PM_RECEIVER_VMMC2_DEDICATED,
169 TWL4030_PM_RECEIVER_VMMC2_VSEL_32,
170 TWL4030_PM_RECEIVER_VMMC2_DEV_GRP,
171 TWL4030_PM_RECEIVER_DEV_GRP_P1);
172
173 mdelay(100); /* ramp-up delay from Linux code */
174 }
Tom Rixfccc0fc2009-06-28 12:52:31 -0500175}
Adam Ford4c2fb5f2017-04-24 13:34:43 -0500176
177#ifdef CONFIG_CMD_POWEROFF
Simon Glass09140112020-05-10 11:40:03 -0600178int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Adam Ford4c2fb5f2017-04-24 13:34:43 -0500179{
180 twl4030_power_off();
181
182 return 0;
183}
184#endif
Jean-Jacques Hiblotfb1b7712018-12-07 14:50:46 +0100185
Igor Opaniuk2147a162021-02-09 13:52:45 +0200186#if CONFIG_IS_ENABLED(DM_I2C)
Jean-Jacques Hiblotfb1b7712018-12-07 14:50:46 +0100187int twl4030_i2c_write_u8(u8 chip_no, u8 reg, u8 val)
188{
189 struct udevice *dev;
190 int ret;
191
192 ret = i2c_get_chip_for_busnum(0, chip_no, 1, &dev);
193 if (ret) {
194 pr_err("unable to get I2C bus. ret %d\n", ret);
195 return ret;
196 }
197 ret = dm_i2c_reg_write(dev, reg, val);
198 if (ret) {
199 pr_err("writing to twl4030 failed. ret %d\n", ret);
200 return ret;
201 }
202 return 0;
203}
204
Pali Rohár4fcc0842020-10-26 22:36:15 +0100205int twl4030_i2c_read(u8 chip_no, u8 reg, u8 *valp, int len)
Jean-Jacques Hiblotfb1b7712018-12-07 14:50:46 +0100206{
207 struct udevice *dev;
208 int ret;
209
210 ret = i2c_get_chip_for_busnum(0, chip_no, 1, &dev);
211 if (ret) {
212 pr_err("unable to get I2C bus. ret %d\n", ret);
213 return ret;
214 }
Pali Rohár4fcc0842020-10-26 22:36:15 +0100215 ret = dm_i2c_read(dev, reg, valp, len);
216 if (ret) {
Jean-Jacques Hiblotfb1b7712018-12-07 14:50:46 +0100217 pr_err("reading from twl4030 failed. ret %d\n", ret);
218 return ret;
219 }
Jean-Jacques Hiblotfb1b7712018-12-07 14:50:46 +0100220 return 0;
221}
222#endif