|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Copyright (c) 2021 Rockchip Electronics Co. Ltd. |
| 4 | + * |
| 5 | + * Author: Shunqing Chen <[email protected]> |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/err.h> |
| 9 | +#include <linux/gpio/consumer.h> |
| 10 | +#include <linux/i2c.h> |
| 11 | +#include <linux/init.h> |
| 12 | +#include <linux/module.h> |
| 13 | +#include <linux/of.h> |
| 14 | +#include <linux/of_gpio.h> |
| 15 | +#include <linux/regmap.h> |
| 16 | +#include <linux/regulator/driver.h> |
| 17 | +#include <linux/regulator/machine.h> |
| 18 | +#include <linux/regulator/of_regulator.h> |
| 19 | + |
| 20 | +#define WL2868C_DEVICE_D1 0x00 |
| 21 | +#define WL2868C_DEVICE_D2 0x01 |
| 22 | +#define WL2868C_DISCHARGE_RESISTORS 0x02 |
| 23 | +#define WL2868C_LDO1_VOUT 0x03 |
| 24 | +#define WL2868C_LDO2_VOUT 0x04 |
| 25 | +#define WL2868C_LDO3_VOUT 0x05 |
| 26 | +#define WL2868C_LDO4_VOUT 0x06 |
| 27 | +#define WL2868C_LDO5_VOUT 0x07 |
| 28 | +#define WL2868C_LDO6_VOUT 0x08 |
| 29 | +#define WL2868C_LDO7_VOUT 0x09 |
| 30 | +#define WL2868C_LDO1_LDO2_SEQ 0x0a |
| 31 | +#define WL2868C_LDO3_LDO4_SEQ 0x0b |
| 32 | +#define WL2868C_LDO5_LDO6_SEQ 0x0c |
| 33 | +#define WL2868C_LDO7_SEQ 0x0d |
| 34 | +#define WL2868C_LDO_EN 0x0e |
| 35 | +#define WL2868C_SEQ_STATUS 0x0f |
| 36 | +#define WL2868C_LDO1_STATUS 0x10 |
| 37 | +#define WL2868C_LDO1_OCP_CTL 0x11 |
| 38 | +#define WL2868C_LDO2_STATUS 0x12 |
| 39 | +#define WL2868C_LDO2_OCP_CTL 0x13 |
| 40 | +#define WL2868C_LDO3_STATUS 0x14 |
| 41 | +#define WL2868C_LDO3_OCP_CTL 0x15 |
| 42 | +#define WL2868C_LDO4_STATUS 0x16 |
| 43 | +#define WL2868C_LDO4_OCP_CTL 0x17 |
| 44 | +#define WL2868C_LDO5_STATUS 0x18 |
| 45 | +#define WL2868C_LDO5_OCP_CTL 0x19 |
| 46 | +#define WL2868C_LDO6_STATUS 0x1a |
| 47 | +#define WL2868C_LDO6_OCP_CTL 0x1b |
| 48 | +#define WL2868C_LDO7_STATUS 0x1c |
| 49 | +#define WL2868C_LDO7_OCP_CTL 0x1d |
| 50 | +#define WL2868C_REPROGRAMMABLE_I2C_ADDR 0x1e |
| 51 | +#define RESERVED_1 0x1f |
| 52 | +#define INT_LATCHED_CLR 0x20 |
| 53 | +#define INT_EN_SET 0x21 |
| 54 | +#define INT_LATCHED_STS 0x22 |
| 55 | +#define INT_PENDING_STS 0x23 |
| 56 | +#define UVLO_CTL 0x24 |
| 57 | +#define RESERVED_2 0x25 |
| 58 | + |
| 59 | +#define WL2868C_VSEL_MASK 0xff |
| 60 | + |
| 61 | +enum wl2868c_regulators { |
| 62 | + WL2868C_REGULATOR_LDO1 = 0, |
| 63 | + WL2868C_REGULATOR_LDO2, |
| 64 | + WL2868C_REGULATOR_LDO3, |
| 65 | + WL2868C_REGULATOR_LDO4, |
| 66 | + WL2868C_REGULATOR_LDO5, |
| 67 | + WL2868C_REGULATOR_LDO6, |
| 68 | + WL2868C_REGULATOR_LDO7, |
| 69 | + WL2868C_MAX_REGULATORS, |
| 70 | +}; |
| 71 | + |
| 72 | +struct wl2868c { |
| 73 | + struct device *dev; |
| 74 | + struct regmap *regmap; |
| 75 | + struct regulator_dev *rdev; |
| 76 | + struct gpio_desc *reset_gpio; |
| 77 | + int min_dropout_uv; |
| 78 | +}; |
| 79 | + |
| 80 | +static const struct regulator_ops wl2868c_reg_ops = { |
| 81 | + .list_voltage = regulator_list_voltage_linear, |
| 82 | + .map_voltage = regulator_map_voltage_linear, |
| 83 | + .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 84 | + .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 85 | + .enable = regulator_enable_regmap, |
| 86 | + .disable = regulator_disable_regmap, |
| 87 | + .is_enabled = regulator_is_enabled_regmap, |
| 88 | +}; |
| 89 | + |
| 90 | +#define WL2868C_DESC(_id, _match, _supply, _min, _max, _step, _vreg, \ |
| 91 | + _vmask, _ereg, _emask, _enval, _disval) \ |
| 92 | + { \ |
| 93 | + .name = (_match), \ |
| 94 | + .supply_name = (_supply), \ |
| 95 | + .of_match = of_match_ptr(_match), \ |
| 96 | + .regulators_node = of_match_ptr("regulators"), \ |
| 97 | + .type = REGULATOR_VOLTAGE, \ |
| 98 | + .id = (_id), \ |
| 99 | + .n_voltages = (((_max) - (_min)) / (_step) + 1), \ |
| 100 | + .owner = THIS_MODULE, \ |
| 101 | + .min_uV = (_min) * 1000, \ |
| 102 | + .uV_step = (_step) * 1000, \ |
| 103 | + .vsel_reg = (_vreg), \ |
| 104 | + .vsel_mask = (_vmask), \ |
| 105 | + .enable_reg = (_ereg), \ |
| 106 | + .enable_mask = (_emask), \ |
| 107 | + .enable_val = (_enval), \ |
| 108 | + .disable_val = (_disval), \ |
| 109 | + .ops = &wl2868c_reg_ops, \ |
| 110 | + } |
| 111 | + |
| 112 | +static const struct regulator_desc wl2868c_reg[] = { |
| 113 | + WL2868C_DESC(WL2868C_REGULATOR_LDO1, "WL_LDO1", "ldo1", 496, 2536, 8, |
| 114 | + WL2868C_LDO1_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(0), BIT(0), 0), |
| 115 | + WL2868C_DESC(WL2868C_REGULATOR_LDO2, "WL_LDO2", "ldo2", 496, 2536, 8, |
| 116 | + WL2868C_LDO2_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(1), BIT(1), 0), |
| 117 | + WL2868C_DESC(WL2868C_REGULATOR_LDO3, "WL_LDO3", "ldo3", 1504, 3544, 8, |
| 118 | + WL2868C_LDO3_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(2), BIT(2), 0), |
| 119 | + WL2868C_DESC(WL2868C_REGULATOR_LDO4, "WL_LDO4", "ldo4", 1504, 3544, 8, |
| 120 | + WL2868C_LDO4_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(3), BIT(3), 0), |
| 121 | + WL2868C_DESC(WL2868C_REGULATOR_LDO5, "WL_LDO5", "ldo5", 1504, 3544, 8, |
| 122 | + WL2868C_LDO5_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(4), BIT(4), 0), |
| 123 | + WL2868C_DESC(WL2868C_REGULATOR_LDO6, "WL_LDO6", "ldo6", 1504, 3544, 8, |
| 124 | + WL2868C_LDO6_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(5), BIT(5), 0), |
| 125 | + WL2868C_DESC(WL2868C_REGULATOR_LDO7, "WL_LDO7", "ldo7", 1504, 3544, 8, |
| 126 | + WL2868C_LDO7_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(6), BIT(6), 0), |
| 127 | +}; |
| 128 | + |
| 129 | +static const struct regmap_range wl2868c_writeable_ranges[] = { |
| 130 | + regmap_reg_range(WL2868C_DISCHARGE_RESISTORS, WL2868C_SEQ_STATUS), |
| 131 | + regmap_reg_range(WL2868C_LDO1_OCP_CTL, WL2868C_LDO1_OCP_CTL), |
| 132 | + regmap_reg_range(WL2868C_LDO2_OCP_CTL, WL2868C_LDO2_OCP_CTL), |
| 133 | + regmap_reg_range(WL2868C_LDO3_OCP_CTL, WL2868C_LDO3_OCP_CTL), |
| 134 | + regmap_reg_range(WL2868C_LDO4_OCP_CTL, WL2868C_LDO4_OCP_CTL), |
| 135 | + regmap_reg_range(WL2868C_LDO5_OCP_CTL, WL2868C_LDO5_OCP_CTL), |
| 136 | + regmap_reg_range(WL2868C_LDO6_OCP_CTL, WL2868C_LDO6_OCP_CTL), |
| 137 | + regmap_reg_range(WL2868C_LDO7_OCP_CTL, WL2868C_REPROGRAMMABLE_I2C_ADDR), |
| 138 | + regmap_reg_range(INT_LATCHED_CLR, INT_LATCHED_CLR), |
| 139 | + regmap_reg_range(INT_EN_SET, INT_EN_SET), |
| 140 | + regmap_reg_range(UVLO_CTL, UVLO_CTL), |
| 141 | +}; |
| 142 | + |
| 143 | +static const struct regmap_range wl2868c_readable_ranges[] = { |
| 144 | + regmap_reg_range(WL2868C_DEVICE_D1, RESERVED_2), |
| 145 | +}; |
| 146 | + |
| 147 | +static const struct regmap_range wl2868c_volatile_ranges[] = { |
| 148 | + regmap_reg_range(WL2868C_DISCHARGE_RESISTORS, WL2868C_SEQ_STATUS), |
| 149 | + regmap_reg_range(WL2868C_LDO1_OCP_CTL, WL2868C_LDO1_OCP_CTL), |
| 150 | + regmap_reg_range(WL2868C_LDO2_OCP_CTL, WL2868C_LDO2_OCP_CTL), |
| 151 | + regmap_reg_range(WL2868C_LDO3_OCP_CTL, WL2868C_LDO3_OCP_CTL), |
| 152 | + regmap_reg_range(WL2868C_LDO4_OCP_CTL, WL2868C_LDO4_OCP_CTL), |
| 153 | + regmap_reg_range(WL2868C_LDO5_OCP_CTL, WL2868C_LDO5_OCP_CTL), |
| 154 | + regmap_reg_range(WL2868C_LDO6_OCP_CTL, WL2868C_LDO6_OCP_CTL), |
| 155 | + regmap_reg_range(WL2868C_LDO7_OCP_CTL, WL2868C_REPROGRAMMABLE_I2C_ADDR), |
| 156 | + regmap_reg_range(INT_LATCHED_CLR, INT_LATCHED_CLR), |
| 157 | + regmap_reg_range(INT_EN_SET, INT_EN_SET), |
| 158 | + regmap_reg_range(UVLO_CTL, UVLO_CTL), |
| 159 | +}; |
| 160 | + |
| 161 | +static const struct regmap_access_table wl2868c_writeable_table = { |
| 162 | + .yes_ranges = wl2868c_writeable_ranges, |
| 163 | + .n_yes_ranges = ARRAY_SIZE(wl2868c_writeable_ranges), |
| 164 | +}; |
| 165 | + |
| 166 | +static const struct regmap_access_table wl2868c_readable_table = { |
| 167 | + .yes_ranges = wl2868c_readable_ranges, |
| 168 | + .n_yes_ranges = ARRAY_SIZE(wl2868c_readable_ranges), |
| 169 | +}; |
| 170 | + |
| 171 | +static const struct regmap_access_table wl2868c_volatile_table = { |
| 172 | + .yes_ranges = wl2868c_volatile_ranges, |
| 173 | + .n_yes_ranges = ARRAY_SIZE(wl2868c_volatile_ranges), |
| 174 | +}; |
| 175 | + |
| 176 | +static const struct regmap_config wl2868c_regmap_config = { |
| 177 | + .reg_bits = 8, |
| 178 | + .val_bits = 8, |
| 179 | + .max_register = RESERVED_2, |
| 180 | + .wr_table = &wl2868c_writeable_table, |
| 181 | + .rd_table = &wl2868c_readable_table, |
| 182 | + .cache_type = REGCACHE_RBTREE, |
| 183 | + .volatile_table = &wl2868c_volatile_table, |
| 184 | +}; |
| 185 | + |
| 186 | +static int wl2868c_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 187 | +{ |
| 188 | + struct device *dev = &client->dev; |
| 189 | + struct regulator_config config = {}; |
| 190 | + struct regulator_dev *rdev; |
| 191 | + const struct regulator_desc *regulators; |
| 192 | + struct wl2868c *wl2868c; |
| 193 | + int ret, i; |
| 194 | + |
| 195 | + wl2868c = devm_kzalloc(dev, sizeof(struct wl2868c), GFP_KERNEL); |
| 196 | + if (!wl2868c) |
| 197 | + return -ENOMEM; |
| 198 | + |
| 199 | + wl2868c->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); |
| 200 | + if (IS_ERR(wl2868c->reset_gpio)) { |
| 201 | + ret = PTR_ERR(wl2868c->reset_gpio); |
| 202 | + dev_err(dev, "failed to request reset GPIO: %d\n", ret); |
| 203 | + return ret; |
| 204 | + } |
| 205 | + |
| 206 | + gpiod_set_value_cansleep(wl2868c->reset_gpio, 0); |
| 207 | + usleep_range(10000, 11000); |
| 208 | + gpiod_set_value_cansleep(wl2868c->reset_gpio, 1); |
| 209 | + usleep_range(10000, 11000); |
| 210 | + gpiod_set_value_cansleep(wl2868c->reset_gpio, 0); |
| 211 | + usleep_range(10000, 11000); |
| 212 | + |
| 213 | + i2c_set_clientdata(client, wl2868c); |
| 214 | + wl2868c->dev = dev; |
| 215 | + wl2868c->regmap = devm_regmap_init_i2c(client, &wl2868c_regmap_config); |
| 216 | + if (IS_ERR(wl2868c->regmap)) { |
| 217 | + ret = PTR_ERR(wl2868c->regmap); |
| 218 | + dev_err(dev, "Failed to allocate register map: %d\n", ret); |
| 219 | + return ret; |
| 220 | + } |
| 221 | + |
| 222 | + config.dev = &client->dev; |
| 223 | + config.regmap = wl2868c->regmap; |
| 224 | + regulators = wl2868c_reg; |
| 225 | + /* Instantiate the regulators */ |
| 226 | + for (i = 0; i < WL2868C_MAX_REGULATORS; i++) { |
| 227 | + rdev = devm_regulator_register(&client->dev, |
| 228 | + ®ulators[i], &config); |
| 229 | + if (IS_ERR(rdev)) { |
| 230 | + dev_err(&client->dev, |
| 231 | + "failed to register %d regulator\n", i); |
| 232 | + return PTR_ERR(rdev); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + return 0; |
| 237 | +} |
| 238 | + |
| 239 | +static void wl2868c_regulator_shutdown(struct i2c_client *client) |
| 240 | +{ |
| 241 | + struct wl2868c *wl2868c = i2c_get_clientdata(client); |
| 242 | + |
| 243 | + if (system_state == SYSTEM_POWER_OFF) |
| 244 | + regmap_write(wl2868c->regmap, WL2868C_LDO_EN, 0x80); |
| 245 | +} |
| 246 | + |
| 247 | +static const struct i2c_device_id wl2868c_i2c_id[] = { |
| 248 | + { "wl2868c", 0 }, |
| 249 | + { } |
| 250 | +}; |
| 251 | + |
| 252 | +MODULE_DEVICE_TABLE(i2c, i2c_device_id); |
| 253 | + |
| 254 | +static const struct of_device_id wl2868c_of_match[] = { |
| 255 | + { .compatible = "willsemi,wl2868c" }, |
| 256 | + {} |
| 257 | +}; |
| 258 | +MODULE_DEVICE_TABLE(of, wl2868c_of_match); |
| 259 | + |
| 260 | +static struct i2c_driver wl2868c_i2c_driver = { |
| 261 | + .driver = { |
| 262 | + .name = "wl2868c", |
| 263 | + .of_match_table = of_match_ptr(wl2868c_of_match), |
| 264 | + }, |
| 265 | + .id_table = wl2868c_i2c_id, |
| 266 | + .probe = wl2868c_i2c_probe, |
| 267 | + .shutdown = wl2868c_regulator_shutdown, |
| 268 | +}; |
| 269 | + |
| 270 | +module_i2c_driver(wl2868c_i2c_driver); |
| 271 | + |
| 272 | +MODULE_DESCRIPTION("WL2868C regulator driver"); |
| 273 | +MODULE_AUTHOR( "Shunqing Chen <[email protected]>"); |
| 274 | +MODULE_LICENSE("GPL"); |
0 commit comments