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