1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) 2018 Theobroma Systems Design und Consulting GmbH
14 #include <linux/bitops.h>
15 #include <power/fan53555.h>
16 #include <power/pmic.h>
17 #include <power/regulator.h>
20 * struct ic_types - definition of fan53555-family devices
22 * @die_id: Identifies the DIE_ID (lower nibble of the ID1 register)
23 * @die_rev: Identifies the DIE_REV (lower nibble of the ID2 register)
24 * @vsel_min: starting voltage (step 0) in uV
25 * @vsel_step: increment of the voltage in uV
27 * The voltage ramp (i.e. minimum voltage and step) is selected from the
28 * combination of 2 nibbles: DIE_ID and DIE_REV.
30 * See http://www.onsemi.com/pub/Collateral/FAN53555-D.pdf for details.
41 { FAN53555_VENDOR_FAIRCHILD, 0x0, 0x3, true, 600000, 10000 },
43 { FAN53555_VENDOR_FAIRCHILD, 0x0, 0xf, true, 800000, 10000 },
45 { FAN53555_VENDOR_FAIRCHILD, 0x0, 0xc, true, 600000, 12500 },
47 { FAN53555_VENDOR_FAIRCHILD, 0x1, 0x3, true, 600000, 10000 },
49 { FAN53555_VENDOR_FAIRCHILD, 0x3, 0x3, true, 600000, 10000 },
51 { FAN53555_VENDOR_FAIRCHILD, 0x4, 0xf, true, 603000, 12826 },
53 { FAN53555_VENDOR_FAIRCHILD, 0x5, 0x3, true, 600000, 10000 },
55 { FAN53555_VENDOR_FAIRCHILD, 0x8, 0x1, true, 600000, 10000 },
57 { FAN53555_VENDOR_FAIRCHILD, 0x8, 0xf, true, 600000, 10000 },
59 { FAN53555_VENDOR_FAIRCHILD, 0xc, 0xf, true, 603000, 12826 },
61 { FAN53555_VENDOR_SILERGY, 0x8, 0x0, false, 712500, 12500 },
63 { FAN53555_VENDOR_SILERGY, 0x9, 0x0, false, 712500, 12500 },
66 /* I2C-accessible byte-sized registers */
69 FAN53555_VSEL0 = 0x00,
71 /* Control register */
77 /* Monitor register */
81 struct fan53555_platdata {
82 /* Voltage setting register */
84 unsigned int sleep_reg;
88 struct fan53555_priv {
94 /* Voltage range and step(linear) */
95 unsigned int vsel_min;
96 unsigned int vsel_step;
97 /* Voltage slew rate limiting */
98 unsigned int slew_rate;
99 /* Sleep voltage cache */
100 unsigned int sleep_vol_cache;
103 static int fan53555_regulator_ofdata_to_platdata(struct udevice *dev)
105 struct fan53555_platdata *dev_pdata = dev_get_platdata(dev);
106 struct dm_regulator_uclass_platdata *uc_pdata =
107 dev_get_uclass_platdata(dev);
110 /* This is a buck regulator */
111 uc_pdata->type = REGULATOR_TYPE_BUCK;
113 sleep_vsel = dev_read_u32_default(dev, "fcs,suspend-voltage-selector",
117 * Depending on the device-tree settings, the 'normal mode'
118 * voltage is either controlled by VSEL0 or VSEL1.
120 switch (sleep_vsel) {
122 dev_pdata->sleep_reg = FAN53555_VSEL0;
123 dev_pdata->vol_reg = FAN53555_VSEL1;
126 dev_pdata->sleep_reg = FAN53555_VSEL1;
127 dev_pdata->vol_reg = FAN53555_VSEL0;
130 pr_err("%s: invalid vsel id %d\n", dev->name, sleep_vsel);
137 static int fan53555_regulator_get_value(struct udevice *dev)
139 struct fan53555_platdata *pdata = dev_get_platdata(dev);
140 struct fan53555_priv *priv = dev_get_priv(dev);
144 /* We only support a single voltage selector (i.e. 'normal' mode). */
145 reg = pmic_reg_read(dev->parent, pdata->vol_reg);
148 voltage = priv->vsel_min + (reg & 0x3f) * priv->vsel_step;
150 debug("%s: %d uV\n", __func__, voltage);
154 static int fan53555_regulator_set_value(struct udevice *dev, int uV)
156 struct fan53555_platdata *pdata = dev_get_platdata(dev);
157 struct fan53555_priv *priv = dev_get_priv(dev);
160 vol = (uV - priv->vsel_min) / priv->vsel_step;
161 debug("%s: uV=%d; writing volume %d: %02x\n",
162 __func__, uV, pdata->vol_reg, vol);
164 return pmic_clrsetbits(dev->parent, pdata->vol_reg, GENMASK(6, 0), vol);
167 static int fan53555_voltages_setup(struct udevice *dev)
169 struct fan53555_priv *priv = dev_get_priv(dev);
172 /* Init voltage range and step */
173 for (i = 0; i < ARRAY_SIZE(ic_types); ++i) {
174 if (ic_types[i].vendor != priv->vendor)
177 if (ic_types[i].die_id != priv->die_id)
180 if (ic_types[i].check_rev &&
181 ic_types[i].die_rev != priv->die_rev)
184 priv->vsel_min = ic_types[i].vsel_min;
185 priv->vsel_step = ic_types[i].vsel_step;
190 pr_err("%s: %s: die id %d rev %d not supported!\n",
191 dev->name, __func__, priv->die_id, priv->die_rev);
202 static int fan53555_probe(struct udevice *dev)
204 struct fan53555_priv *priv = dev_get_priv(dev);
207 debug("%s\n", __func__);
209 /* read chip ID1 and ID2 (two registers, starting at ID1) */
210 ID1 = pmic_reg_read(dev->parent, FAN53555_ID1);
214 ID2 = pmic_reg_read(dev->parent, FAN53555_ID2);
218 /* extract vendor, die_id and die_rev */
219 priv->vendor = dev->driver_data;
220 priv->die_id = ID1 & GENMASK(3, 0);
221 priv->die_rev = ID2 & GENMASK(3, 0);
223 if (fan53555_voltages_setup(dev) < 0)
226 debug("%s: FAN53555 option %d rev %d detected\n",
227 __func__, priv->die_id, priv->die_rev);
232 static const struct dm_regulator_ops fan53555_regulator_ops = {
233 .get_value = fan53555_regulator_get_value,
234 .set_value = fan53555_regulator_set_value,
237 U_BOOT_DRIVER(fan53555_regulator) = {
238 .name = "fan53555_regulator",
239 .id = UCLASS_REGULATOR,
240 .ops = &fan53555_regulator_ops,
241 .ofdata_to_platdata = fan53555_regulator_ofdata_to_platdata,
242 .platdata_auto = sizeof(struct fan53555_platdata),
243 .priv_auto = sizeof(struct fan53555_priv),
244 .probe = fan53555_probe,