blob: 3aa3cce945e72dddaa043d671d518682c8de14a9 [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>
Thierry Reding09c0cf22014-12-09 22:25:05 -070015#include <power/as3722.h>
Simon Glassb3d2ed32017-07-25 08:30:12 -060016#include <power/pmic.h>
Thierry Reding09c0cf22014-12-09 22:25:05 -070017
Simon Glassb3d2ed32017-07-25 08:30:12 -060018#define AS3722_NUM_OF_REGS 0x92
Thierry Reding09c0cf22014-12-09 22:25:05 -070019
Simon Glassb3d2ed32017-07-25 08:30:12 -060020static int as3722_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
Thierry Reding09c0cf22014-12-09 22:25:05 -070021{
Simon Glassb3d2ed32017-07-25 08:30:12 -060022 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070023
Simon Glassb3d2ed32017-07-25 08:30:12 -060024 ret = dm_i2c_read(dev, reg, buff, len);
25 if (ret < 0)
26 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070027
28 return 0;
29}
30
Simon Glassb3d2ed32017-07-25 08:30:12 -060031static int as3722_write(struct udevice *dev, uint reg, const uint8_t *buff,
32 int len)
Thierry Reding09c0cf22014-12-09 22:25:05 -070033{
Simon Glassb3d2ed32017-07-25 08:30:12 -060034 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070035
Simon Glassb3d2ed32017-07-25 08:30:12 -060036 ret = dm_i2c_write(dev, reg, buff, len);
37 if (ret < 0)
38 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070039
40 return 0;
41}
42
Simon Glassb3d2ed32017-07-25 08:30:12 -060043static int as3722_read_id(struct udevice *dev, uint *idp, uint *revisionp)
Thierry Reding09c0cf22014-12-09 22:25:05 -070044{
Simon Glassb3d2ed32017-07-25 08:30:12 -060045 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070046
Simon Glassb3d2ed32017-07-25 08:30:12 -060047 ret = pmic_reg_read(dev, AS3722_ASIC_ID1);
48 if (ret < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -070049 pr_err("failed to read ID1 register: %d\n", ret);
Simon Glassb3d2ed32017-07-25 08:30:12 -060050 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070051 }
Simon Glassb3d2ed32017-07-25 08:30:12 -060052 *idp = ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070053
Simon Glassb3d2ed32017-07-25 08:30:12 -060054 ret = pmic_reg_read(dev, AS3722_ASIC_ID2);
55 if (ret < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -070056 pr_err("failed to read ID2 register: %d\n", ret);
Simon Glassb3d2ed32017-07-25 08:30:12 -060057 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070058 }
Simon Glassb3d2ed32017-07-25 08:30:12 -060059 *revisionp = ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070060
61 return 0;
62}
63
Simon Glassb3d2ed32017-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 Reding09c0cf22014-12-09 22:25:05 -070066{
Simon Glassb3d2ed32017-07-25 08:30:12 -060067 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070068
69 if (sd > 6)
70 return -EINVAL;
71
Simon Glassb3d2ed32017-07-25 08:30:12 -060072 ret = pmic_reg_write(dev, AS3722_SD_VOLTAGE(sd), value);
73 if (ret < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -070074 pr_err("failed to write SD%u voltage register: %d\n", sd, ret);
Simon Glassb3d2ed32017-07-25 08:30:12 -060075 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070076 }
77
78 return 0;
79}
80
Simon Glassb3d2ed32017-07-25 08:30:12 -060081int as3722_ldo_set_voltage(struct udevice *dev, unsigned int ldo, u8 value)
Thierry Reding09c0cf22014-12-09 22:25:05 -070082{
Simon Glassb3d2ed32017-07-25 08:30:12 -060083 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070084
85 if (ldo > 11)
86 return -EINVAL;
87
Simon Glassb3d2ed32017-07-25 08:30:12 -060088 ret = pmic_reg_write(dev, AS3722_LDO_VOLTAGE(ldo), value);
89 if (ret < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -070090 pr_err("failed to write LDO%u voltage register: %d\n", ldo,
91 ret);
Simon Glassb3d2ed32017-07-25 08:30:12 -060092 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -070093 }
94
95 return 0;
96}
97
Simon Glassb3d2ed32017-07-25 08:30:12 -060098static int as3722_probe(struct udevice *dev)
Thierry Reding09c0cf22014-12-09 22:25:05 -070099{
Simon Glassb3d2ed32017-07-25 08:30:12 -0600100 uint id, revision;
101 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700102
Simon Glassb3d2ed32017-07-25 08:30:12 -0600103 ret = as3722_read_id(dev, &id, &revision);
104 if (ret < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -0700105 pr_err("failed to read ID: %d\n", ret);
Simon Glassb3d2ed32017-07-25 08:30:12 -0600106 return ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700107 }
108
Simon Glassb3d2ed32017-07-25 08:30:12 -0600109 if (id != AS3722_DEVICE_ID) {
Simon Glass73126ac2018-11-18 08:14:28 -0700110 pr_err("unknown device\n");
Simon Glassb3d2ed32017-07-25 08:30:12 -0600111 return -ENOENT;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700112 }
113
Simon Glassb3d2ed32017-07-25 08:30:12 -0600114 debug("AS3722 revision %#x found on I2C bus %s\n", revision, dev->name);
Thierry Reding09c0cf22014-12-09 22:25:05 -0700115
116 return 0;
117}
118
Simon Glassb3d2ed32017-07-25 08:30:12 -0600119#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};
Thierry Reding09c0cf22014-12-09 22:25:05 -0700125
Simon Glassb3d2ed32017-07-25 08:30:12 -0600126static int as3722_bind(struct udevice *dev)
127{
128 struct udevice *gpio_dev;
129 ofnode regulators_node;
130 int children;
131 int ret;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700132
Simon Glassb3d2ed32017-07-25 08:30:12 -0600133 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;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700138 }
139
Simon Glassb3d2ed32017-07-25 08:30:12 -0600140 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;
Thierry Reding09c0cf22014-12-09 22:25:05 -0700147 }
148
149 return 0;
150}
Simon Glassb3d2ed32017-07-25 08:30:12 -0600151#endif
Thierry Reding09c0cf22014-12-09 22:25:05 -0700152
Simon Glassb3d2ed32017-07-25 08:30:12 -0600153static int as3722_reg_count(struct udevice *dev)
Simon Glass053da132015-04-14 21:03:26 -0600154{
Simon Glassb3d2ed32017-07-25 08:30:12 -0600155 return AS3722_NUM_OF_REGS;
Simon Glass053da132015-04-14 21:03:26 -0600156}
157
Simon Glassb3d2ed32017-07-25 08:30:12 -0600158static struct dm_pmic_ops as3722_ops = {
159 .reg_count = as3722_reg_count,
160 .read = as3722_read,
161 .write = as3722_write,
162};
Thierry Reding09c0cf22014-12-09 22:25:05 -0700163
Simon Glassb3d2ed32017-07-25 08:30:12 -0600164static const struct udevice_id as3722_ids[] = {
165 { .compatible = "ams,as3722" },
166 { }
167};
Thierry Reding09c0cf22014-12-09 22:25:05 -0700168
Simon Glassb3d2ed32017-07-25 08:30:12 -0600169U_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};