blob: dbd55023779777df6dbd387be3e5a840fefcbdaf [file] [log] [blame]
Philipp Tomsichb7f57e12018-11-30 20:00:08 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) 2018 Theobroma Systems Design und Consulting GmbH
4 */
5
6#include <common.h>
7#include <bitfield.h>
8#include <errno.h>
9#include <dm.h>
10#include <fdtdec.h>
11#include <i2c.h>
12#include <asm/gpio.h>
13#include <power/pmic.h>
14#include <power/regulator.h>
15
16/**
17 * struct ic_types - definition of fan53555-family devices
18 *
19 * @die_id: Identifies the DIE_ID (lower nibble of the ID1 register)
20 * @die_rev: Identifies the DIE_REV (lower nibble of the ID2 register)
21 * @vsel_min: starting voltage (step 0) in uV
22 * @vsel_step: increment of the voltage in uV
23 *
24 * The voltage ramp (i.e. minimum voltage and step) is selected from the
25 * combination of 2 nibbles: DIE_ID and DIE_REV.
26 *
27 * See http://www.onsemi.com/pub/Collateral/FAN53555-D.pdf for details.
28 */
29static const struct {
30 u8 die_id;
31 u8 die_rev;
32 u32 vsel_min;
33 u32 vsel_step;
34} ic_types[] = {
35 { 0x0, 0x3, 600000, 10000 }, /* Option 00 */
36 { 0x0, 0xf, 800000, 10000 }, /* Option 13 */
37 { 0x0, 0xc, 600000, 12500 }, /* Option 23 */
38 { 0x1, 0x3, 600000, 10000 }, /* Option 01 */
39 { 0x3, 0x3, 600000, 10000 }, /* Option 03 */
40 { 0x4, 0xf, 603000, 12826 }, /* Option 04 */
41 { 0x5, 0x3, 600000, 10000 }, /* Option 05 */
42 { 0x8, 0x1, 600000, 10000 }, /* Option 08 */
43 { 0x8, 0xf, 600000, 10000 }, /* Option 08 */
44 { 0xc, 0xf, 603000, 12826 }, /* Option 09 */
45};
46
47/* I2C-accessible byte-sized registers */
48enum {
49 /* Voltage setting */
50 FAN53555_VSEL0 = 0x00,
51 FAN53555_VSEL1,
52 /* Control register */
53 FAN53555_CONTROL,
54 /* IC Type */
55 FAN53555_ID1,
56 /* IC mask version */
57 FAN53555_ID2,
58 /* Monitor register */
59 FAN53555_MONITOR,
60};
61
62struct fan53555_platdata {
63 /* Voltage setting register */
64 unsigned int vol_reg;
65 unsigned int sleep_reg;
66
67};
68
69struct fan53555_priv {
70 /* IC Vendor */
71 unsigned int vendor;
72 /* IC Type and Rev */
73 unsigned int die_id;
74 unsigned int die_rev;
75 /* Voltage range and step(linear) */
76 unsigned int vsel_min;
77 unsigned int vsel_step;
78 /* Voltage slew rate limiting */
79 unsigned int slew_rate;
80 /* Sleep voltage cache */
81 unsigned int sleep_vol_cache;
82};
83
84static int fan53555_regulator_ofdata_to_platdata(struct udevice *dev)
85{
86 struct fan53555_platdata *dev_pdata = dev_get_platdata(dev);
87 struct dm_regulator_uclass_platdata *uc_pdata =
88 dev_get_uclass_platdata(dev);
89 u32 sleep_vsel;
90
91 /* This is a buck regulator */
92 uc_pdata->type = REGULATOR_TYPE_BUCK;
93
94 sleep_vsel = dev_read_u32_default(dev, "fcs,suspend-voltage-selector",
95 FAN53555_VSEL1);
96
97 /*
98 * Depending on the device-tree settings, the 'normal mode'
99 * voltage is either controlled by VSEL0 or VSEL1.
100 */
101 switch (sleep_vsel) {
102 case FAN53555_VSEL0:
103 dev_pdata->sleep_reg = FAN53555_VSEL0;
104 dev_pdata->vol_reg = FAN53555_VSEL1;
105 break;
106 case FAN53555_VSEL1:
107 dev_pdata->sleep_reg = FAN53555_VSEL1;
108 dev_pdata->vol_reg = FAN53555_VSEL0;
109 break;
110 default:
111 pr_err("%s: invalid vsel id %d\n", dev->name, sleep_vsel);
112 return -EINVAL;
113 }
114
115 return 0;
116}
117
118static int fan53555_regulator_get_value(struct udevice *dev)
119{
120 struct fan53555_platdata *pdata = dev_get_platdata(dev);
121 struct fan53555_priv *priv = dev_get_priv(dev);
122 int reg;
123 int voltage;
124
125 /* We only support a single voltage selector (i.e. 'normal' mode). */
126 reg = pmic_reg_read(dev->parent, pdata->vol_reg);
127 if (reg < 0)
128 return reg;
129 voltage = priv->vsel_min + (reg & 0x3f) * priv->vsel_step;
130
131 debug("%s: %d uV\n", __func__, voltage);
132 return voltage;
133}
134
135static int fan53555_regulator_set_value(struct udevice *dev, int uV)
136{
137 struct fan53555_platdata *pdata = dev_get_platdata(dev);
138 struct fan53555_priv *priv = dev_get_priv(dev);
139 u8 vol;
140
141 vol = (uV - priv->vsel_min) / priv->vsel_step;
142 debug("%s: uV=%d; writing volume %d: %02x\n",
143 __func__, uV, pdata->vol_reg, vol);
144
145 return pmic_clrsetbits(dev, pdata->vol_reg, GENMASK(6, 0), vol);
146}
147
148static int fan53555_voltages_setup(struct udevice *dev)
149{
150 struct fan53555_priv *priv = dev_get_priv(dev);
151 int i;
152
153 /* Init voltage range and step */
154 for (i = 0; i < ARRAY_SIZE(ic_types); ++i) {
155 if (ic_types[i].die_id != priv->die_id)
156 continue;
157
158 if (ic_types[i].die_rev != priv->die_rev)
159 continue;
160
161 priv->vsel_min = ic_types[i].vsel_min;
162 priv->vsel_step = ic_types[i].vsel_step;
163
164 return 0;
165 }
166
167 pr_err("%s: %s: die id %d rev %d not supported!\n",
168 dev->name, __func__, priv->die_id, priv->die_rev);
169 return -EINVAL;
170}
171
172enum {
173 DIE_ID_SHIFT = 0,
174 DIE_ID_WIDTH = 4,
175 DIE_REV_SHIFT = 0,
176 DIE_REV_WIDTH = 4,
177};
178
179static int fan53555_probe(struct udevice *dev)
180{
181 struct fan53555_priv *priv = dev_get_priv(dev);
182 int ID1, ID2;
183
184 debug("%s\n", __func__);
185
186 /* read chip ID1 and ID2 (two registers, starting at ID1) */
187 ID1 = pmic_reg_read(dev->parent, FAN53555_ID1);
188 if (ID1 < 0)
189 return ID1;
190
191 ID2 = pmic_reg_read(dev->parent, FAN53555_ID2);
192 if (ID2 < 0)
193 return ID2;
194
195 /* extract vendor, die_id and die_rev */
196 priv->vendor = bitfield_extract(ID1, 5, 3);
197 priv->die_id = ID1 & GENMASK(3, 0);
198 priv->die_rev = ID2 & GENMASK(3, 0);
199
200 if (fan53555_voltages_setup(dev) < 0)
201 return -ENODATA;
202
203 debug("%s: FAN53555 option %d rev %d detected\n",
204 __func__, priv->die_id, priv->die_rev);
205
206 return 0;
207}
208
209static const struct dm_regulator_ops fan53555_regulator_ops = {
210 .get_value = fan53555_regulator_get_value,
211 .set_value = fan53555_regulator_set_value,
212};
213
214U_BOOT_DRIVER(fan53555_regulator) = {
215 .name = "fan53555_regulator",
216 .id = UCLASS_REGULATOR,
217 .ops = &fan53555_regulator_ops,
218 .ofdata_to_platdata = fan53555_regulator_ofdata_to_platdata,
219 .platdata_auto_alloc_size = sizeof(struct fan53555_platdata),
220 .priv_auto_alloc_size = sizeof(struct fan53555_priv),
221 .probe = fan53555_probe,
222};