1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) 2018 Theobroma Systems Design und Consulting GmbH
13 #include <power/pmic.h>
14 #include <power/regulator.h>
17 * struct ic_types - definition of fan53555-family devices
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
24 * The voltage ramp (i.e. minimum voltage and step) is selected from the
25 * combination of 2 nibbles: DIE_ID and DIE_REV.
27 * See http://www.onsemi.com/pub/Collateral/FAN53555-D.pdf for details.
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 */
47 /* I2C-accessible byte-sized registers */
50 FAN53555_VSEL0 = 0x00,
52 /* Control register */
58 /* Monitor register */
62 struct fan53555_platdata {
63 /* Voltage setting register */
65 unsigned int sleep_reg;
69 struct fan53555_priv {
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;
84 static int fan53555_regulator_ofdata_to_platdata(struct udevice *dev)
86 struct fan53555_platdata *dev_pdata = dev_get_platdata(dev);
87 struct dm_regulator_uclass_platdata *uc_pdata =
88 dev_get_uclass_platdata(dev);
91 /* This is a buck regulator */
92 uc_pdata->type = REGULATOR_TYPE_BUCK;
94 sleep_vsel = dev_read_u32_default(dev, "fcs,suspend-voltage-selector",
98 * Depending on the device-tree settings, the 'normal mode'
99 * voltage is either controlled by VSEL0 or VSEL1.
101 switch (sleep_vsel) {
103 dev_pdata->sleep_reg = FAN53555_VSEL0;
104 dev_pdata->vol_reg = FAN53555_VSEL1;
107 dev_pdata->sleep_reg = FAN53555_VSEL1;
108 dev_pdata->vol_reg = FAN53555_VSEL0;
111 pr_err("%s: invalid vsel id %d\n", dev->name, sleep_vsel);
118 static int fan53555_regulator_get_value(struct udevice *dev)
120 struct fan53555_platdata *pdata = dev_get_platdata(dev);
121 struct fan53555_priv *priv = dev_get_priv(dev);
125 /* We only support a single voltage selector (i.e. 'normal' mode). */
126 reg = pmic_reg_read(dev->parent, pdata->vol_reg);
129 voltage = priv->vsel_min + (reg & 0x3f) * priv->vsel_step;
131 debug("%s: %d uV\n", __func__, voltage);
135 static int fan53555_regulator_set_value(struct udevice *dev, int uV)
137 struct fan53555_platdata *pdata = dev_get_platdata(dev);
138 struct fan53555_priv *priv = dev_get_priv(dev);
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);
145 return pmic_clrsetbits(dev, pdata->vol_reg, GENMASK(6, 0), vol);
148 static int fan53555_voltages_setup(struct udevice *dev)
150 struct fan53555_priv *priv = dev_get_priv(dev);
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)
158 if (ic_types[i].die_rev != priv->die_rev)
161 priv->vsel_min = ic_types[i].vsel_min;
162 priv->vsel_step = ic_types[i].vsel_step;
167 pr_err("%s: %s: die id %d rev %d not supported!\n",
168 dev->name, __func__, priv->die_id, priv->die_rev);
179 static int fan53555_probe(struct udevice *dev)
181 struct fan53555_priv *priv = dev_get_priv(dev);
184 debug("%s\n", __func__);
186 /* read chip ID1 and ID2 (two registers, starting at ID1) */
187 ID1 = pmic_reg_read(dev->parent, FAN53555_ID1);
191 ID2 = pmic_reg_read(dev->parent, FAN53555_ID2);
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);
200 if (fan53555_voltages_setup(dev) < 0)
203 debug("%s: FAN53555 option %d rev %d detected\n",
204 __func__, priv->die_id, priv->die_rev);
209 static const struct dm_regulator_ops fan53555_regulator_ops = {
210 .get_value = fan53555_regulator_get_value,
211 .set_value = fan53555_regulator_set_value,
214 U_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,