blob: c7dd9705d18960edf4bc4d54a0c0300cde7dc5a7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Thierry Reding09c0cf22014-12-09 22:25:05 -07002/*
3 * Copyright (C) 2014 NVIDIA Corporation
Thierry Reding09c0cf22014-12-09 22:25:05 -07004 */
5
6#define pr_fmt(fmt) "as3722: " fmt
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <fdtdec.h>
12#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glassb3d2ed32017-07-25 08:30:12 -060014#include <dm/lists.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060015#include <linux/printk.h>
Thierry Reding09c0cf22014-12-09 22:25:05 -070016#include <power/as3722.h>
Simon Glassb3d2ed32017-07-25 08:30:12 -060017#include <power/pmic.h>
Thierry Reding09c0cf22014-12-09 22:25:05 -070018
Simon Glassb3d2ed32017-07-25 08:30:12 -060019#define AS3722_NUM_OF_REGS 0x92
Thierry Reding09c0cf22014-12-09 22:25:05 -070020
Simon Glassb3d2ed32017-07-25 08:30:12 -060021static int as3722_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
Thierry Reding09c0cf22014-12-09 22:25:05 -070022{
Simon Glassb3d2ed32017-07-25 08:30:12 -060023 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070024
Simon Glassb3d2ed32017-07-25 08:30:12 -060025 ret = dm_i2c_read(dev, reg, buff, len);
26 if (ret < 0)
27 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070028
29 return 0;
30}
31
Simon Glassb3d2ed32017-07-25 08:30:12 -060032static int as3722_write(struct udevice *dev, uint reg, const uint8_t *buff,
33 int len)
Thierry Reding09c0cf22014-12-09 22:25:05 -070034{
Simon Glassb3d2ed32017-07-25 08:30:12 -060035 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070036
Simon Glassb3d2ed32017-07-25 08:30:12 -060037 ret = dm_i2c_write(dev, reg, buff, len);
38 if (ret < 0)
39 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070040
41 return 0;
42}
43
Simon Glassb3d2ed32017-07-25 08:30:12 -060044static int as3722_read_id(struct udevice *dev, uint *idp, uint *revisionp)
Thierry Reding09c0cf22014-12-09 22:25:05 -070045{
Simon Glassb3d2ed32017-07-25 08:30:12 -060046 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070047
Simon Glassb3d2ed32017-07-25 08:30:12 -060048 ret = pmic_reg_read(dev, AS3722_ASIC_ID1);
49 if (ret < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -070050 pr_err("failed to read ID1 register: %d\n", ret);
Simon Glassb3d2ed32017-07-25 08:30:12 -060051 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070052 }
Simon Glassb3d2ed32017-07-25 08:30:12 -060053 *idp = ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070054
Simon Glassb3d2ed32017-07-25 08:30:12 -060055 ret = pmic_reg_read(dev, AS3722_ASIC_ID2);
56 if (ret < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -070057 pr_err("failed to read ID2 register: %d\n", ret);
Simon Glassb3d2ed32017-07-25 08:30:12 -060058 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070059 }
Simon Glassb3d2ed32017-07-25 08:30:12 -060060 *revisionp = ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070061
62 return 0;
63}
64
Simon Glassb3d2ed32017-07-25 08:30:12 -060065/* TODO(treding@nvidia.com): Add proper regulator support to avoid this */
66int as3722_sd_set_voltage(struct udevice *dev, unsigned int sd, u8 value)
Thierry Reding09c0cf22014-12-09 22:25:05 -070067{
Simon Glassb3d2ed32017-07-25 08:30:12 -060068 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070069
70 if (sd > 6)
71 return -EINVAL;
72
Simon Glassb3d2ed32017-07-25 08:30:12 -060073 ret = pmic_reg_write(dev, AS3722_SD_VOLTAGE(sd), value);
74 if (ret < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -070075 pr_err("failed to write SD%u voltage register: %d\n", sd, ret);
Simon Glassb3d2ed32017-07-25 08:30:12 -060076 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070077 }
78
79 return 0;
80}
81
Simon Glassb3d2ed32017-07-25 08:30:12 -060082int as3722_ldo_set_voltage(struct udevice *dev, unsigned int ldo, u8 value)
Thierry Reding09c0cf22014-12-09 22:25:05 -070083{
Simon Glassb3d2ed32017-07-25 08:30:12 -060084 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070085
86 if (ldo > 11)
87 return -EINVAL;
88
Simon Glassb3d2ed32017-07-25 08:30:12 -060089 ret = pmic_reg_write(dev, AS3722_LDO_VOLTAGE(ldo), value);
90 if (ret < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -070091 pr_err("failed to write LDO%u voltage register: %d\n", ldo,
92 ret);
Simon Glassb3d2ed32017-07-25 08:30:12 -060093 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070094 }
95
96 return 0;
97}
98
Simon Glassb3d2ed32017-07-25 08:30:12 -060099static int as3722_probe(struct udevice *dev)
Thierry Reding09c0cf22014-12-09 22:25:05 -0700100{
Simon Glassb3d2ed32017-07-25 08:30:12 -0600101 uint id, revision;
102 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700103
Simon Glassb3d2ed32017-07-25 08:30:12 -0600104 ret = as3722_read_id(dev, &id, &revision);
105 if (ret < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -0700106 pr_err("failed to read ID: %d\n", ret);
Simon Glassb3d2ed32017-07-25 08:30:12 -0600107 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700108 }
109
Simon Glassb3d2ed32017-07-25 08:30:12 -0600110 if (id != AS3722_DEVICE_ID) {
Simon Glass73126ac2018-11-18 08:14:28 -0700111 pr_err("unknown device\n");
Simon Glassb3d2ed32017-07-25 08:30:12 -0600112 return -ENOENT;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700113 }
114
Simon Glassb3d2ed32017-07-25 08:30:12 -0600115 debug("AS3722 revision %#x found on I2C bus %s\n", revision, dev->name);
Thierry Reding09c0cf22014-12-09 22:25:05 -0700116
117 return 0;
118}
119
Simon Glassb3d2ed32017-07-25 08:30:12 -0600120#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
121static const struct pmic_child_info pmic_children_info[] = {
122 { .prefix = "sd", .driver = "as3722_stepdown"},
123 { .prefix = "ldo", .driver = "as3722_ldo"},
124 { },
125};
Thierry Reding09c0cf22014-12-09 22:25:05 -0700126
Simon Glassb3d2ed32017-07-25 08:30:12 -0600127static int as3722_bind(struct udevice *dev)
128{
129 struct udevice *gpio_dev;
130 ofnode regulators_node;
131 int children;
132 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700133
Simon Glassb3d2ed32017-07-25 08:30:12 -0600134 regulators_node = dev_read_subnode(dev, "regulators");
135 if (!ofnode_valid(regulators_node)) {
136 debug("%s: %s regulators subnode not found\n", __func__,
137 dev->name);
138 return -ENXIO;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700139 }
140
Simon Glassb3d2ed32017-07-25 08:30:12 -0600141 children = pmic_bind_children(dev, regulators_node, pmic_children_info);
142 if (!children)
143 debug("%s: %s - no child found\n", __func__, dev->name);
144 ret = device_bind_driver(dev, "gpio_as3722", "gpio_as3722", &gpio_dev);
145 if (ret) {
146 debug("%s: Cannot bind GPIOs (ret=%d)\n", __func__, ret);
147 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700148 }
149
150 return 0;
151}
Simon Glassb3d2ed32017-07-25 08:30:12 -0600152#endif
Thierry Reding09c0cf22014-12-09 22:25:05 -0700153
Simon Glassb3d2ed32017-07-25 08:30:12 -0600154static int as3722_reg_count(struct udevice *dev)
Simon Glass053da132015-04-14 21:03:26 -0600155{
Simon Glassb3d2ed32017-07-25 08:30:12 -0600156 return AS3722_NUM_OF_REGS;
Simon Glass053da132015-04-14 21:03:26 -0600157}
158
Simon Glassb3d2ed32017-07-25 08:30:12 -0600159static struct dm_pmic_ops as3722_ops = {
160 .reg_count = as3722_reg_count,
161 .read = as3722_read,
162 .write = as3722_write,
163};
Thierry Reding09c0cf22014-12-09 22:25:05 -0700164
Simon Glassb3d2ed32017-07-25 08:30:12 -0600165static const struct udevice_id as3722_ids[] = {
166 { .compatible = "ams,as3722" },
167 { }
168};
Thierry Reding09c0cf22014-12-09 22:25:05 -0700169
Simon Glassb3d2ed32017-07-25 08:30:12 -0600170U_BOOT_DRIVER(pmic_as3722) = {
171 .name = "as3722_pmic",
172 .id = UCLASS_PMIC,
173 .of_match = as3722_ids,
174#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
175 .bind = as3722_bind,
176#endif
177 .probe = as3722_probe,
178 .ops = &as3722_ops,
179};