1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014-2019, Toradex AG
7 * Helpers for Freescale PMIC PF0100
12 #include <asm/arch/imx-regs.h>
13 #include <asm/arch/iomux.h>
14 #include <asm/arch/mx6-pins.h>
16 #include <asm/mach-imx/iomux-v3.h>
18 #include "pf0100_otp.inc"
21 /* define for PMIC register dump */
24 /* use Apalis GPIO1 to switch on VPGM, ON: 1 */
25 static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = {
26 MX6_PAD_NANDF_D4__GPIO2_IO04 | MUX_PAD_CTRL(NO_PAD_CTRL),
27 # define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 4)
30 unsigned pmic_init(void)
33 struct udevice *dev = NULL;
34 unsigned programmed = 0;
36 uchar devid, revid, val;
39 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
41 printf("failed to get device for PMIC at address 0x%x\n",
45 /* get device ident */
46 if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) {
47 puts("i2c pmic devid read failed\n");
50 if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) {
51 puts("i2c pmic revid read failed\n");
54 printf("device id: 0x%.2x, revision id: 0x%.2x, ", devid, revid);
56 /* get device programmed state */
57 val = PFUZE100_PAGE_REGISTER_PAGE1;
58 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
59 puts("i2c write failed\n");
62 if (dm_i2c_read(dev, PFUZE100_FUSE_POR1, &val, 1) < 0) {
63 puts("i2c fuse_por read failed\n");
66 if (val & PFUZE100_FUSE_POR_M)
69 if (dm_i2c_read(dev, PFUZE100_FUSE_POR2, &val, 1) < 0) {
70 puts("i2c fuse_por read failed\n");
73 if (val & PFUZE100_FUSE_POR_M)
76 if (dm_i2c_read(dev, PFUZE100_FUSE_POR3, &val, 1) < 0) {
77 puts("i2c fuse_por read failed\n");
80 if (val & PFUZE100_FUSE_POR_M)
85 puts("not programmed\n");
91 puts("undefined programming state\n");
95 /* The following is needed during production */
96 if (programmed != 3) {
97 /* set VGEN1 to 1.2V */
98 val = PFUZE100_VGEN1_VAL;
99 if (dm_i2c_write(dev, PFUZE100_VGEN1CTL, &val, 1)) {
100 puts("i2c write failed\n");
104 /* set SWBST to 5.0V */
105 val = PFUZE100_SWBST_VAL;
106 if (dm_i2c_write(dev, PFUZE100_SWBSTCTL, &val, 1))
107 puts("i2c write failed\n");
114 for (i = 0; i < 16; i++)
116 for (j = 0; j < 0x80; ) {
118 for (i = 0; i < 16; i++) {
119 dm_i2c_read(dev, j + i, &val, 1);
120 printf("\t%2x", val);
124 printf("\nEXT Page 1");
126 val = PFUZE100_PAGE_REGISTER_PAGE1;
127 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
128 puts("i2c write failed\n");
132 for (j = 0x80; j < 0x100; ) {
134 for (i = 0; i < 16; i++) {
135 dm_i2c_read(dev, j + i, &val, 1);
136 printf("\t%2x", val);
140 printf("\nEXT Page 2");
142 val = PFUZE100_PAGE_REGISTER_PAGE2;
143 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
144 puts("i2c write failed\n");
148 for (j = 0x80; j < 0x100; ) {
150 for (i = 0; i < 16; i++) {
151 dm_i2c_read(dev, j + i, &val, 1);
152 printf("\t%2x", val);
163 #ifndef CONFIG_SPL_BUILD
164 static int pf0100_prog(void)
167 struct udevice *dev = NULL;
168 unsigned char bus = 1;
172 if (pmic_init() == 3) {
173 puts("PMIC already programmed, exiting\n");
174 return CMD_RET_FAILURE;
176 /* set up gpio to manipulate vprog, initially off */
177 imx_iomux_v3_setup_multiple_pads(pmic_prog_pads,
178 ARRAY_SIZE(pmic_prog_pads));
179 gpio_direction_output(PMIC_PROG_VOLTAGE, 0);
181 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
183 printf("failed to get device for PMIC at address 0x%x\n",
185 return CMD_RET_FAILURE;
188 for (i = 0; i < ARRAY_SIZE(pmic_otp_prog); i++) {
189 switch (pmic_otp_prog[i].cmd) {
191 val = (unsigned char) (pmic_otp_prog[i].value & 0xff);
192 if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
193 printf("i2c write failed, reg 0x%2x, value 0x%2x\n",
194 pmic_otp_prog[i].reg, val);
195 return CMD_RET_FAILURE;
199 udelay(pmic_otp_prog[i].value * 1000);
202 gpio_direction_output(PMIC_PROG_VOLTAGE,
203 pmic_otp_prog[i].value);
210 return CMD_RET_SUCCESS;
213 static int do_pf0100_prog(cmd_tbl_t *cmdtp, int flag, int argc,
217 puts("Programming PMIC OTP...");
219 if (ret == CMD_RET_SUCCESS)
227 pf0100_otp_prog, 1, 0, do_pf0100_prog,
228 "Program the OTP fuses on the PMIC PF0100",
231 #endif /* CONFIG_SPL_BUILD */