2 * Copyright (C) 2014 NVIDIA Corporation
4 * SPDX-License-Identifier: GPL-2.0+
7 #define pr_fmt(fmt) "as3722: " fmt
15 #include <power/as3722.h>
17 #define AS3722_SD_VOLTAGE(n) (0x00 + (n))
18 #define AS3722_GPIO_CONTROL(n) (0x08 + (n))
19 #define AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDH (1 << 0)
20 #define AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDL (7 << 0)
21 #define AS3722_GPIO_CONTROL_INVERT (1 << 7)
22 #define AS3722_LDO_VOLTAGE(n) (0x10 + (n))
23 #define AS3722_GPIO_SIGNAL_OUT 0x20
24 #define AS3722_SD_CONTROL 0x4d
25 #define AS3722_LDO_CONTROL 0x4e
26 #define AS3722_ASIC_ID1 0x90
27 #define AS3722_DEVICE_ID 0x0c
28 #define AS3722_ASIC_ID2 0x91
30 int as3722_read(struct udevice *pmic, u8 reg, u8 *value)
34 err = dm_i2c_read(pmic, reg, value, 1);
41 int as3722_write(struct udevice *pmic, u8 reg, u8 value)
45 err = dm_i2c_write(pmic, reg, &value, 1);
52 static int as3722_read_id(struct udevice *pmic, u8 *id, u8 *revision)
56 err = as3722_read(pmic, AS3722_ASIC_ID1, id);
58 error("failed to read ID1 register: %d", err);
62 err = as3722_read(pmic, AS3722_ASIC_ID2, revision);
64 error("failed to read ID2 register: %d", err);
71 int as3722_sd_enable(struct udevice *pmic, unsigned int sd)
79 err = as3722_read(pmic, AS3722_SD_CONTROL, &value);
81 error("failed to read SD control register: %d", err);
87 err = as3722_write(pmic, AS3722_SD_CONTROL, value);
89 error("failed to write SD control register: %d", err);
96 int as3722_sd_set_voltage(struct udevice *pmic, unsigned int sd, u8 value)
103 err = as3722_write(pmic, AS3722_SD_VOLTAGE(sd), value);
105 error("failed to write SD%u voltage register: %d", sd, err);
112 int as3722_ldo_enable(struct udevice *pmic, unsigned int ldo)
120 err = as3722_read(pmic, AS3722_LDO_CONTROL, &value);
122 error("failed to read LDO control register: %d", err);
128 err = as3722_write(pmic, AS3722_LDO_CONTROL, value);
130 error("failed to write LDO control register: %d", err);
137 int as3722_ldo_set_voltage(struct udevice *pmic, unsigned int ldo, u8 value)
144 err = as3722_write(pmic, AS3722_LDO_VOLTAGE(ldo), value);
146 error("failed to write LDO%u voltage register: %d", ldo,
154 int as3722_gpio_configure(struct udevice *pmic, unsigned int gpio,
160 if (flags & AS3722_GPIO_OUTPUT_VDDH)
161 value |= AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDH;
163 if (flags & AS3722_GPIO_INVERT)
164 value |= AS3722_GPIO_CONTROL_INVERT;
166 err = as3722_write(pmic, AS3722_GPIO_CONTROL(gpio), value);
168 error("failed to configure GPIO#%u: %d", gpio, err);
175 static int as3722_gpio_set(struct udevice *pmic, unsigned int gpio,
185 err = as3722_read(pmic, AS3722_GPIO_SIGNAL_OUT, &value);
187 error("failed to read GPIO signal out register: %d", err);
192 value &= ~(1 << gpio);
199 err = as3722_write(pmic, AS3722_GPIO_SIGNAL_OUT, value);
201 error("failed to set GPIO#%u %s: %d", gpio, l, err);
208 int as3722_gpio_direction_output(struct udevice *pmic, unsigned int gpio,
218 value = AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDL;
220 value = AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDH;
222 err = as3722_write(pmic, AS3722_GPIO_CONTROL(gpio), value);
224 error("failed to configure GPIO#%u as output: %d", gpio, err);
228 err = as3722_gpio_set(pmic, gpio, level);
230 error("failed to set GPIO#%u high: %d", gpio, err);
237 /* Temporary function until we get the pmic framework */
238 int as3722_get(struct udevice **devp)
243 return i2c_get_chip_for_busnum(bus, address, 1, devp);
246 int as3722_init(struct udevice **devp)
248 struct udevice *pmic;
250 const unsigned int bus = 0;
251 const unsigned int address = 0x40;
254 err = i2c_get_chip_for_busnum(bus, address, 1, &pmic);
257 err = as3722_read_id(pmic, &id, &revision);
259 error("failed to read ID: %d", err);
263 if (id != AS3722_DEVICE_ID) {
264 error("unknown device");
268 debug("AS3722 revision %#x found on I2C bus %u, address %#x\n",
269 revision, bus, address);