blob: 3aa3cce945e72dddaa043d671d518682c8de14a9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Thierry Reding6173c452014-12-09 22:25:05 -07002/*
3 * Copyright (C) 2014 NVIDIA Corporation
Thierry Reding6173c452014-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 Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glasse3f44f52017-07-25 08:30:12 -060014#include <dm/lists.h>
Thierry Reding6173c452014-12-09 22:25:05 -070015#include <power/as3722.h>
Simon Glasse3f44f52017-07-25 08:30:12 -060016#include <power/pmic.h>
Thierry Reding6173c452014-12-09 22:25:05 -070017
Simon Glasse3f44f52017-07-25 08:30:12 -060018#define AS3722_NUM_OF_REGS 0x92
Thierry Reding6173c452014-12-09 22:25:05 -070019
Simon Glasse3f44f52017-07-25 08:30:12 -060020static int as3722_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
Thierry Reding6173c452014-12-09 22:25:05 -070021{
Simon Glasse3f44f52017-07-25 08:30:12 -060022 int ret;
Thierry Reding6173c452014-12-09 22:25:05 -070023
Simon Glasse3f44f52017-07-25 08:30:12 -060024 ret = dm_i2c_read(dev, reg, buff, len);
25 if (ret < 0)
26 return ret;
Thierry Reding6173c452014-12-09 22:25:05 -070027
28 return 0;
29}
30
Simon Glasse3f44f52017-07-25 08:30:12 -060031static int as3722_write(struct udevice *dev, uint reg, const uint8_t *buff,
32 int len)
Thierry Reding6173c452014-12-09 22:25:05 -070033{
Simon Glasse3f44f52017-07-25 08:30:12 -060034 int ret;
Thierry Reding6173c452014-12-09 22:25:05 -070035
Simon Glasse3f44f52017-07-25 08:30:12 -060036 ret = dm_i2c_write(dev, reg, buff, len);
37 if (ret < 0)
38 return ret;
Thierry Reding6173c452014-12-09 22:25:05 -070039
40 return 0;
41}
42
Simon Glasse3f44f52017-07-25 08:30:12 -060043static int as3722_read_id(struct udevice *dev, uint *idp, uint *revisionp)
Thierry Reding6173c452014-12-09 22:25:05 -070044{
Simon Glasse3f44f52017-07-25 08:30:12 -060045 int ret;
Thierry Reding6173c452014-12-09 22:25:05 -070046
Simon Glasse3f44f52017-07-25 08:30:12 -060047 ret = pmic_reg_read(dev, AS3722_ASIC_ID1);
48 if (ret < 0) {
Simon Glassc83c4362018-11-18 08:14:28 -070049 pr_err("failed to read ID1 register: %d\n", ret);
Simon Glasse3f44f52017-07-25 08:30:12 -060050 return ret;
Thierry Reding6173c452014-12-09 22:25:05 -070051 }
Simon Glasse3f44f52017-07-25 08:30:12 -060052 *idp = ret;
Thierry Reding6173c452014-12-09 22:25:05 -070053
Simon Glasse3f44f52017-07-25 08:30:12 -060054 ret = pmic_reg_read(dev, AS3722_ASIC_ID2);
55 if (ret < 0) {
Simon Glassc83c4362018-11-18 08:14:28 -070056 pr_err("failed to read ID2 register: %d\n", ret);
Simon Glasse3f44f52017-07-25 08:30:12 -060057 return ret;
Thierry Reding6173c452014-12-09 22:25:05 -070058 }
Simon Glasse3f44f52017-07-25 08:30:12 -060059 *revisionp = ret;
Thierry Reding6173c452014-12-09 22:25:05 -070060
61 return 0;
62}
63
Simon Glasse3f44f52017-07-25 08:30:12 -060064/* TODO(treding@nvidia.com): Add proper regulator support to avoid this */
65int as3722_sd_set_voltage(struct udevice *dev, unsigned int sd, u8 value)
Thierry Reding6173c452014-12-09 22:25:05 -070066{
Simon Glasse3f44f52017-07-25 08:30:12 -060067 int ret;
Thierry Reding6173c452014-12-09 22:25:05 -070068
69 if (sd > 6)
70 return -EINVAL;
71
Simon Glasse3f44f52017-07-25 08:30:12 -060072 ret = pmic_reg_write(dev, AS3722_SD_VOLTAGE(sd), value);
73 if (ret < 0) {
Simon Glassc83c4362018-11-18 08:14:28 -070074 pr_err("failed to write SD%u voltage register: %d\n", sd, ret);
Simon Glasse3f44f52017-07-25 08:30:12 -060075 return ret;
Thierry Reding6173c452014-12-09 22:25:05 -070076 }
77
78 return 0;
79}
80
Simon Glasse3f44f52017-07-25 08:30:12 -060081int as3722_ldo_set_voltage(struct udevice *dev, unsigned int ldo, u8 value)
Thierry Reding6173c452014-12-09 22:25:05 -070082{
Simon Glasse3f44f52017-07-25 08:30:12 -060083 int ret;
Thierry Reding6173c452014-12-09 22:25:05 -070084
85 if (ldo > 11)
86 return -EINVAL;
87
Simon Glasse3f44f52017-07-25 08:30:12 -060088 ret = pmic_reg_write(dev, AS3722_LDO_VOLTAGE(ldo), value);
89 if (ret < 0) {
Simon Glassc83c4362018-11-18 08:14:28 -070090 pr_err("failed to write LDO%u voltage register: %d\n", ldo,
91 ret);
Simon Glasse3f44f52017-07-25 08:30:12 -060092 return ret;
Thierry Reding6173c452014-12-09 22:25:05 -070093 }
94
95 return 0;
96}
97
Simon Glasse3f44f52017-07-25 08:30:12 -060098static int as3722_probe(struct udevice *dev)
Thierry Reding6173c452014-12-09 22:25:05 -070099{
Simon Glasse3f44f52017-07-25 08:30:12 -0600100 uint id, revision;
101 int ret;
Thierry Reding6173c452014-12-09 22:25:05 -0700102
Simon Glasse3f44f52017-07-25 08:30:12 -0600103 ret = as3722_read_id(dev, &id, &revision);
104 if (ret < 0) {
Simon Glassc83c4362018-11-18 08:14:28 -0700105 pr_err("failed to read ID: %d\n", ret);
Simon Glasse3f44f52017-07-25 08:30:12 -0600106 return ret;
Thierry Reding6173c452014-12-09 22:25:05 -0700107 }
108
109 if (id != AS3722_DEVICE_ID) {
Simon Glassc83c4362018-11-18 08:14:28 -0700110 pr_err("unknown device\n");
Thierry Reding6173c452014-12-09 22:25:05 -0700111 return -ENOENT;
112 }
113
Simon Glasse3f44f52017-07-25 08:30:12 -0600114 debug("AS3722 revision %#x found on I2C bus %s\n", revision, dev->name);
Thierry Reding6173c452014-12-09 22:25:05 -0700115
116 return 0;
117}
Simon Glasse3f44f52017-07-25 08:30:12 -0600118
119#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
120static const struct pmic_child_info pmic_children_info[] = {
121 { .prefix = "sd", .driver = "as3722_stepdown"},
122 { .prefix = "ldo", .driver = "as3722_ldo"},
123 { },
124};
125
126static int as3722_bind(struct udevice *dev)
127{
128 struct udevice *gpio_dev;
129 ofnode regulators_node;
130 int children;
131 int ret;
132
133 regulators_node = dev_read_subnode(dev, "regulators");
134 if (!ofnode_valid(regulators_node)) {
135 debug("%s: %s regulators subnode not found\n", __func__,
136 dev->name);
137 return -ENXIO;
138 }
139
140 children = pmic_bind_children(dev, regulators_node, pmic_children_info);
141 if (!children)
142 debug("%s: %s - no child found\n", __func__, dev->name);
143 ret = device_bind_driver(dev, "gpio_as3722", "gpio_as3722", &gpio_dev);
144 if (ret) {
145 debug("%s: Cannot bind GPIOs (ret=%d)\n", __func__, ret);
146 return ret;
147 }
148
149 return 0;
150}
151#endif
152
153static int as3722_reg_count(struct udevice *dev)
154{
155 return AS3722_NUM_OF_REGS;
156}
157
158static struct dm_pmic_ops as3722_ops = {
159 .reg_count = as3722_reg_count,
160 .read = as3722_read,
161 .write = as3722_write,
162};
163
164static const struct udevice_id as3722_ids[] = {
165 { .compatible = "ams,as3722" },
166 { }
167};
168
169U_BOOT_DRIVER(pmic_as3722) = {
170 .name = "as3722_pmic",
171 .id = UCLASS_PMIC,
172 .of_match = as3722_ids,
173#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
174 .bind = as3722_bind,
175#endif
176 .probe = as3722_probe,
177 .ops = &as3722_ops,
178};