Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Tom Rix | cd78263 | 2009-06-28 12:52:29 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2009 Wind River Systems, Inc. |
| 4 | * Tom Rix <Tom.Rix at windriver.com> |
| 5 | * |
Tom Rix | 2c15513 | 2009-06-28 12:52:30 -0500 | [diff] [blame] | 6 | * twl4030_power_reset_init is derived from code on omapzoom, |
| 7 | * git://git.omapzoom.com/repo/u-boot.git |
Tom Rix | cd78263 | 2009-06-28 12:52:29 -0500 | [diff] [blame] | 8 | * |
| 9 | * Copyright (C) 2007-2009 Texas Instruments, Inc. |
Tom Rix | 2c15513 | 2009-06-28 12:52:30 -0500 | [diff] [blame] | 10 | * |
| 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 Rix | cd78263 | 2009-06-28 12:52:29 -0500 | [diff] [blame] | 23 | */ |
| 24 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 25 | #include <command.h> |
Tom Rix | cd78263 | 2009-06-28 12:52:29 -0500 | [diff] [blame] | 26 | #include <twl4030.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 27 | #include <linux/delay.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 28 | #include <linux/printk.h> |
Tom Rix | cd78263 | 2009-06-28 12:52:29 -0500 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * Power Reset |
| 32 | */ |
| 33 | void twl4030_power_reset_init(void) |
| 34 | { |
| 35 | u8 val = 0; |
Nishanth Menon | b29c2f0 | 2013-03-26 05:20:50 +0000 | [diff] [blame] | 36 | if (twl4030_i2c_read_u8(TWL4030_CHIP_PM_MASTER, |
| 37 | TWL4030_PM_MASTER_P1_SW_EVENTS, &val)) { |
Tom Rix | cd78263 | 2009-06-28 12:52:29 -0500 | [diff] [blame] | 38 | 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 Menon | 0208aaf | 2013-03-26 05:20:49 +0000 | [diff] [blame] | 42 | if (twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER, |
| 43 | TWL4030_PM_MASTER_P1_SW_EVENTS, val)) { |
Tom Rix | cd78263 | 2009-06-28 12:52:29 -0500 | [diff] [blame] | 44 | printf("Error:TWL4030: failed to write the power register\n"); |
| 45 | printf("Could not initialize hardware reset\n"); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
Tom Rix | 2c15513 | 2009-06-28 12:52:30 -0500 | [diff] [blame] | 50 | /* |
Paul Kocialkowski | 6dc443e | 2015-07-20 15:17:07 +0200 | [diff] [blame] | 51 | * Power off |
| 52 | */ |
| 53 | void 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 Sakoman | 5a0a82f | 2010-08-10 12:58:39 -0700 | [diff] [blame] | 111 | * Set Device Group and Voltage |
Tom Rix | 2c15513 | 2009-06-28 12:52:30 -0500 | [diff] [blame] | 112 | */ |
Steve Sakoman | 5a0a82f | 2010-08-10 12:58:39 -0700 | [diff] [blame] | 113 | void twl4030_pmrecv_vsel_cfg(u8 vsel_reg, u8 vsel_val, |
| 114 | u8 dev_grp, u8 dev_grp_sel) |
| 115 | { |
Grazvydas Ignotas | 61712bc | 2012-03-19 03:37:40 +0000 | [diff] [blame] | 116 | int ret; |
Steve Sakoman | 5a0a82f | 2010-08-10 12:58:39 -0700 | [diff] [blame] | 117 | |
| 118 | /* Select the Voltage */ |
Nishanth Menon | 0208aaf | 2013-03-26 05:20:49 +0000 | [diff] [blame] | 119 | ret = twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER, vsel_reg, |
| 120 | vsel_val); |
Grazvydas Ignotas | 61712bc | 2012-03-19 03:37:40 +0000 | [diff] [blame] | 121 | if (ret != 0) { |
Peter Meerwald | dfe3610 | 2012-11-19 23:13:04 +0000 | [diff] [blame] | 122 | printf("Could not write vsel to reg %02x (%d)\n", |
Grazvydas Ignotas | 61712bc | 2012-03-19 03:37:40 +0000 | [diff] [blame] | 123 | vsel_reg, ret); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | /* Select the Device Group (enable the supply if dev_grp_sel != 0) */ |
Nishanth Menon | 0208aaf | 2013-03-26 05:20:49 +0000 | [diff] [blame] | 128 | ret = twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER, dev_grp, |
| 129 | dev_grp_sel); |
Grazvydas Ignotas | 61712bc | 2012-03-19 03:37:40 +0000 | [diff] [blame] | 130 | if (ret != 0) |
Peter Meerwald | dfe3610 | 2012-11-19 23:13:04 +0000 | [diff] [blame] | 131 | printf("Could not write grp_sel to reg %02x (%d)\n", |
Grazvydas Ignotas | 61712bc | 2012-03-19 03:37:40 +0000 | [diff] [blame] | 132 | dev_grp, ret); |
Steve Sakoman | 5a0a82f | 2010-08-10 12:58:39 -0700 | [diff] [blame] | 133 | } |
Tom Rix | 2c15513 | 2009-06-28 12:52:30 -0500 | [diff] [blame] | 134 | |
| 135 | void twl4030_power_init(void) |
| 136 | { |
Tom Rix | 2c15513 | 2009-06-28 12:52:30 -0500 | [diff] [blame] | 137 | /* set VAUX3 to 2.8V */ |
Steve Sakoman | 5a0a82f | 2010-08-10 12:58:39 -0700 | [diff] [blame] | 138 | 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 Rix | 2c15513 | 2009-06-28 12:52:30 -0500 | [diff] [blame] | 142 | |
| 143 | /* set VPLL2 to 1.8V */ |
Steve Sakoman | 5a0a82f | 2010-08-10 12:58:39 -0700 | [diff] [blame] | 144 | 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 Rix | 2c15513 | 2009-06-28 12:52:30 -0500 | [diff] [blame] | 148 | |
| 149 | /* set VDAC to 1.8V */ |
Steve Sakoman | 5a0a82f | 2010-08-10 12:58:39 -0700 | [diff] [blame] | 150 | 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 Rix | 2c15513 | 2009-06-28 12:52:30 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Paul Kocialkowski | f3e85e4 | 2014-11-08 20:55:46 +0100 | [diff] [blame] | 156 | void twl4030_power_mmc_init(int dev_index) |
Tom Rix | fccc0fc | 2009-06-28 12:52:31 -0500 | [diff] [blame] | 157 | { |
Paul Kocialkowski | f3e85e4 | 2014-11-08 20:55:46 +0100 | [diff] [blame] | 158 | 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 Kocialkowski | 2ed8c87 | 2014-10-28 18:14:23 +0100 | [diff] [blame] | 164 | |
Paul Kocialkowski | f3e85e4 | 2014-11-08 20:55:46 +0100 | [diff] [blame] | 165 | 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 Rix | fccc0fc | 2009-06-28 12:52:31 -0500 | [diff] [blame] | 175 | } |
Adam Ford | 4c2fb5f | 2017-04-24 13:34:43 -0500 | [diff] [blame] | 176 | |
| 177 | #ifdef CONFIG_CMD_POWEROFF |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 178 | int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Adam Ford | 4c2fb5f | 2017-04-24 13:34:43 -0500 | [diff] [blame] | 179 | { |
| 180 | twl4030_power_off(); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | #endif |
Jean-Jacques Hiblot | fb1b771 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 185 | |
Igor Opaniuk | 2147a16 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 186 | #if CONFIG_IS_ENABLED(DM_I2C) |
Jean-Jacques Hiblot | fb1b771 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 187 | int 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ár | 4fcc084 | 2020-10-26 22:36:15 +0100 | [diff] [blame] | 205 | int twl4030_i2c_read(u8 chip_no, u8 reg, u8 *valp, int len) |
Jean-Jacques Hiblot | fb1b771 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 206 | { |
| 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ár | 4fcc084 | 2020-10-26 22:36:15 +0100 | [diff] [blame] | 215 | ret = dm_i2c_read(dev, reg, valp, len); |
| 216 | if (ret) { |
Jean-Jacques Hiblot | fb1b771 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 217 | pr_err("reading from twl4030 failed. ret %d\n", ret); |
| 218 | return ret; |
| 219 | } |
Jean-Jacques Hiblot | fb1b771 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | #endif |