blob: 6d185719199daa32eb4eee60add55c816bdb6111 [file] [log] [blame]
Keerthy333b89b2024-05-31 11:20:41 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
4 * Keerthy <j-keerthy@ti.com>
5 */
6
7#include <dm.h>
8#include <i2c.h>
9#include <dm/device_compat.h>
10#include <power/regulator.h>
11
12#define TPS6287X_REG_VSET 0x0
13#define TPS6287X_REG_CONTROL1 0x1
14#define TPS6287X_REG_CONTROL2 0x2
15#define TPS6287X_REG_CONTROL3 0x3
16#define TPS6287X_REG_STATUS 0x4
17#define TPS6287X_REG_VSET_VSET_MASK 0xff
18#define TPS6287X_REG_CONTROL2_VRANGE_MASK 0xc
19
20struct tps6287x_regulator_config {
21 u32 vmin;
22 u32 vmax;
23};
24
25struct tps6287x_regulator_pdata {
26 u8 vsel_offset;
27 struct udevice *i2c;
28 struct tps6287x_regulator_config *config;
29};
30
31static struct tps6287x_regulator_config tps6287x_data = {
32 .vmin = 400000,
33 .vmax = 3350000,
34};
35
36static int tps6287x_regulator_set_value(struct udevice *dev, int uV)
37{
38 struct tps6287x_regulator_pdata *pdata = dev_get_plat(dev);
39 u8 regval, vset;
40 int ret;
41
42 if (uV < pdata->config->vmin || uV > pdata->config->vmax)
43 return -EINVAL;
44 /*
45 * Based on the value of VRANGE bit field of CONTROL2 reg the range
46 * varies.
47 */
48 ret = dm_i2c_read(pdata->i2c, TPS6287X_REG_CONTROL2, &regval, 1);
49 if (ret) {
50 dev_err(dev, "CTRL2 reg read failed: %d\n", ret);
51 return ret;
52 }
53
54 regval &= TPS6287X_REG_CONTROL2_VRANGE_MASK;
55 regval >>= ffs(TPS6287X_REG_CONTROL2_VRANGE_MASK) - 1;
56
57 /*
58 * VRANGE = 0. Increment step 1250 uV starting with 0 --> 400000 uV
59 * VRANGE = 1. Increment step 2500 uV starting with 0 --> 400000 uV
60 * VRANGE = 2. Increment step 5000 uV starting with 0 --> 400000 uV
61 * VRANGE = 3. Increment step 10000 uV starting with 0 --> 800000 uV
62 */
63 switch (regval) {
64 case 0:
65 vset = (uV - 400000) / 1250;
66 break;
67 case 1:
68 vset = (uV - 400000) / 2500;
69 break;
70 case 2:
71 vset = (uV - 400000) / 5000;
72 break;
73 case 3:
74 vset = (uV - 800000) / 10000;
75 break;
76 default:
77 pr_err("%s: invalid regval %d\n", dev->name, regval);
78 return -EINVAL;
79 }
80
81 return dm_i2c_write(pdata->i2c, TPS6287X_REG_VSET, &vset, 1);
82}
83
84static int tps6287x_regulator_get_value(struct udevice *dev)
85{
86 u8 regval, vset;
87 int uV;
88 int ret;
89 struct tps6287x_regulator_pdata *pdata = dev_get_plat(dev);
90
91 /*
92 * Based on the value of VRANGE bit field of CONTROL2 reg the range
93 * varies.
94 */
95 ret = dm_i2c_read(pdata->i2c, TPS6287X_REG_CONTROL2, &regval, 1);
96 if (ret) {
97 dev_err(dev, "i2c read failed: %d\n", ret);
98 return ret;
99 }
100
101 regval &= TPS6287X_REG_CONTROL2_VRANGE_MASK;
102 regval >>= ffs(TPS6287X_REG_CONTROL2_VRANGE_MASK) - 1;
103
104 ret = dm_i2c_read(pdata->i2c, TPS6287X_REG_VSET, &vset, 1);
105 if (ret) {
106 dev_err(dev, "i2c VSET read failed: %d\n", ret);
107 return ret;
108 }
109
110 /*
111 * VRANGE = 0. Increment step 1250 uV starting with 0 --> 400000 uV
112 * VRANGE = 1. Increment step 2500 uV starting with 0 --> 400000 uV
113 * VRANGE = 2. Increment step 5000 uV starting with 0 --> 400000 uV
114 * VRANGE = 3. Increment step 10000 uV starting with 0 --> 800000 uV
115 */
116 switch (regval) {
117 case 0:
118 uV = 400000 + vset * 1250;
119 break;
120 case 1:
121 uV = 400000 + vset * 2500;
122 break;
123 case 2:
124 uV = 400000 + vset * 5000;
125 break;
126 case 3:
127 uV = 800000 + vset * 10000;
128 break;
129 default:
130 pr_err("%s: invalid regval %d\n", dev->name, regval);
131 return -EINVAL;
132 }
133
134 return uV;
135}
136
137static int tps6287x_regulator_probe(struct udevice *dev)
138{
139 struct tps6287x_regulator_pdata *pdata = dev_get_plat(dev);
140 int ret, slave_id;
141
142 pdata->config = (void *)dev_get_driver_data(dev);
143
144 slave_id = devfdt_get_addr_index(dev, 0);
145
146 ret = i2c_get_chip(dev->parent, slave_id, 1, &pdata->i2c);
147 if (ret) {
148 dev_err(dev, "i2c dev get failed.\n");
149 return ret;
150 }
151
152 return 0;
153}
154
155static const struct dm_regulator_ops tps6287x_regulator_ops = {
156 .get_value = tps6287x_regulator_get_value,
157 .set_value = tps6287x_regulator_set_value,
158};
159
160static const struct udevice_id tps6287x_regulator_ids[] = {
161 { .compatible = "ti,tps62873", .data = (ulong)&tps6287x_data },
162 { },
163};
164
165U_BOOT_DRIVER(tps6287x_regulator) = {
166 .name = "tps6287x_regulator",
167 .id = UCLASS_REGULATOR,
168 .ops = &tps6287x_regulator_ops,
169 .of_match = tps6287x_regulator_ids,
170 .plat_auto = sizeof(struct tps6287x_regulator_pdata),
171 .probe = tps6287x_regulator_probe,
172};