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