^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // wm8994-regulator.c -- Regulator driver for the WM8994
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Copyright 2009 Wolfson Microelectronics PLC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/regulator/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/regulator/machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mfd/wm8994/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/mfd/wm8994/registers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/mfd/wm8994/pdata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct wm8994_ldo {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct regulator_dev *regulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct wm8994 *wm8994;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct regulator_consumer_supply supply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct regulator_init_data init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define WM8994_LDO1_MAX_SELECTOR 0x7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define WM8994_LDO2_MAX_SELECTOR 0x3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static const struct regulator_ops wm8994_ldo1_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) .list_voltage = regulator_list_voltage_linear,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) .map_voltage = regulator_map_voltage_linear,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .get_voltage_sel = regulator_get_voltage_sel_regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .set_voltage_sel = regulator_set_voltage_sel_regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned int selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (selector > WM8994_LDO2_MAX_SELECTOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) switch (ldo->wm8994->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) case WM8994:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return (selector * 100000) + 900000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) case WM8958:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return (selector * 100000) + 1000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) case WM1811:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) switch (selector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return (selector * 100000) + 950000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static const struct regulator_ops wm8994_ldo2_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .list_voltage = wm8994_ldo2_list_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .get_voltage_sel = regulator_get_voltage_sel_regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .set_voltage_sel = regulator_set_voltage_sel_regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static const struct regulator_desc wm8994_ldo_desc[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .name = "LDO1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .id = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .type = REGULATOR_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .vsel_reg = WM8994_LDO_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .vsel_mask = WM8994_LDO1_VSEL_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .ops = &wm8994_ldo1_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .min_uV = 2400000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .uV_step = 100000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .enable_time = 3000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .name = "LDO2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .id = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .type = REGULATOR_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .vsel_reg = WM8994_LDO_2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .vsel_mask = WM8994_LDO2_VSEL_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .ops = &wm8994_ldo2_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .enable_time = 3000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static const struct regulator_consumer_supply wm8994_ldo_consumer[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) { .supply = "AVDD1" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) { .supply = "DCVDD" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static const struct regulator_init_data wm8994_ldo_default[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .constraints = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .valid_ops_mask = REGULATOR_CHANGE_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .num_consumer_supplies = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .constraints = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .valid_ops_mask = REGULATOR_CHANGE_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .num_consumer_supplies = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int wm8994_ldo_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct wm8994_pdata *pdata = dev_get_platdata(wm8994->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int id = pdev->id % ARRAY_SIZE(pdata->ldo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct regulator_config config = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct wm8994_ldo *ldo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct gpio_desc *gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!ldo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ldo->wm8994 = wm8994;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ldo->supply = wm8994_ldo_consumer[id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ldo->supply.dev_name = dev_name(wm8994->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) config.dev = wm8994->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) config.driver_data = ldo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) config.regmap = wm8994->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) config.init_data = &ldo->init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * Look up LDO enable GPIO from the parent device node, we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * use devm because the regulator core will free the GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) gpiod = gpiod_get_optional(pdev->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) id ? "wlf,ldo2ena" : "wlf,ldo1ena",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) GPIOD_OUT_LOW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) GPIOD_FLAGS_BIT_NONEXCLUSIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (IS_ERR(gpiod))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return PTR_ERR(gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) config.ena_gpiod = gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* Use default constraints if none set up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!pdata || !pdata->ldo[id].init_data || wm8994->dev->of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) dev_dbg(wm8994->dev, "Using default init data, supply %s %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) ldo->supply.dev_name, ldo->supply.supply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ldo->init_data = wm8994_ldo_default[id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ldo->init_data.consumer_supplies = &ldo->supply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (!gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ldo->init_data.constraints.valid_ops_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ldo->init_data = *pdata->ldo[id].init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * At this point the GPIO descriptor is handled over to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * regulator core and we need not worry about it on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * error path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) ldo->regulator = devm_regulator_register(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) &wm8994_ldo_desc[id],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) &config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (IS_ERR(ldo->regulator)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = PTR_ERR(ldo->regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) id + 1, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) platform_set_drvdata(pdev, ldo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static struct platform_driver wm8994_ldo_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) .probe = wm8994_ldo_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) .name = "wm8994-ldo",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) module_platform_driver(wm8994_ldo_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /* Module information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) MODULE_DESCRIPTION("WM8994 LDO driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) MODULE_ALIAS("platform:wm8994-ldo");