^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) // Device driver for regulators in Hi6421V530 IC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Copyright (c) <2017> HiSilicon Technologies Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) // http://www.hisilicon.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) // Copyright (c) <2017> Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) // https://www.linaro.org
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) // Author: Wang Xiaoyin <hw.wangxiaoyin@hisilicon.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) // Guodong Xu <guodong.xu@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mfd/hi6421-pmic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/regulator/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * struct hi6421v530_regulator_info - hi6421v530 regulator information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @desc: regulator description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @mode_mask: ECO mode bitmask of LDOs; for BUCKs, this masks sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @eco_microamp: eco mode load upper limit (in uA), valid for LDOs only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct hi6421v530_regulator_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct regulator_desc rdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u8 mode_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u32 eco_microamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* HI6421v530 regulators */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) enum hi6421v530_regulator_id {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) HI6421V530_LDO3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) HI6421V530_LDO9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) HI6421V530_LDO11,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) HI6421V530_LDO15,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) HI6421V530_LDO16,
^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 const unsigned int ldo_3_voltages[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) 1800000, 1825000, 1850000, 1875000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) 1900000, 1925000, 1950000, 1975000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) 2000000, 2025000, 2050000, 2075000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) 2100000, 2125000, 2150000, 2200000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static const unsigned int ldo_9_11_voltages[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) 1750000, 1800000, 1825000, 2800000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) 2850000, 2950000, 3000000, 3300000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static const unsigned int ldo_15_16_voltages[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) 1750000, 1800000, 2400000, 2600000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) 2700000, 2850000, 2950000, 3000000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static const struct regulator_ops hi6421v530_ldo_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define HI6421V530_LDO_ENABLE_TIME (350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * _id - LDO id name string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * v_table - voltage table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * vreg - voltage select register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * vmask - voltage select mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * ereg - enable register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * emask - enable mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * odelay - off/on delay time in uS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * ecomask - eco mode mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * ecoamp - eco mode load uppler limit in uA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define HI6421V530_LDO(_ID, v_table, vreg, vmask, ereg, emask, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) odelay, ecomask, ecoamp) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .rdesc = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .name = #_ID, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .of_match = of_match_ptr(#_ID), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .regulators_node = of_match_ptr("regulators"), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .ops = &hi6421v530_ldo_ops, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .type = REGULATOR_VOLTAGE, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .id = HI6421V530_##_ID, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .owner = THIS_MODULE, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .n_voltages = ARRAY_SIZE(v_table), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .volt_table = v_table, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .vsel_reg = HI6421_REG_TO_BUS_ADDR(vreg), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .vsel_mask = vmask, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .enable_reg = HI6421_REG_TO_BUS_ADDR(ereg), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .enable_mask = emask, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .enable_time = HI6421V530_LDO_ENABLE_TIME, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .off_on_delay = odelay, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .mode_mask = ecomask, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .eco_microamp = ecoamp, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* HI6421V530 regulator information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static struct hi6421v530_regulator_info hi6421v530_regulator_info[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) HI6421V530_LDO(LDO3, ldo_3_voltages, 0x061, 0xf, 0x060, 0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 20000, 0x6, 8000),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) HI6421V530_LDO(LDO9, ldo_9_11_voltages, 0x06b, 0x7, 0x06a, 0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 40000, 0x6, 8000),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) HI6421V530_LDO(LDO11, ldo_9_11_voltages, 0x06f, 0x7, 0x06e, 0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 40000, 0x6, 8000),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) HI6421V530_LDO(LDO15, ldo_15_16_voltages, 0x077, 0x7, 0x076, 0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 40000, 0x6, 8000),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) HI6421V530_LDO(LDO16, ldo_15_16_voltages, 0x079, 0x7, 0x078, 0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 40000, 0x6, 8000),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static unsigned int hi6421v530_regulator_ldo_get_mode(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct regulator_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct hi6421v530_regulator_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned int reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) info = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) regmap_read(rdev->regmap, rdev->desc->enable_reg, ®_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (reg_val & (info->mode_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return REGULATOR_MODE_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return REGULATOR_MODE_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static int hi6421v530_regulator_ldo_set_mode(struct regulator_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct hi6421v530_regulator_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) unsigned int new_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) info = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) switch (mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) case REGULATOR_MODE_NORMAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) new_mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) case REGULATOR_MODE_IDLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) new_mode = info->mode_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) info->mode_mask, new_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static const struct regulator_ops hi6421v530_ldo_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .is_enabled = regulator_is_enabled_regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) .enable = regulator_enable_regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) .disable = regulator_disable_regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) .list_voltage = regulator_list_voltage_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .map_voltage = regulator_map_voltage_ascend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .get_voltage_sel = regulator_get_voltage_sel_regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .set_voltage_sel = regulator_set_voltage_sel_regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .get_mode = hi6421v530_regulator_ldo_get_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .set_mode = hi6421v530_regulator_ldo_set_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int hi6421v530_regulator_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct hi6421_pmic *pmic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct regulator_dev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct regulator_config config = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) pmic = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!pmic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) dev_err(&pdev->dev, "no pmic in the regulator parent node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) for (i = 0; i < ARRAY_SIZE(hi6421v530_regulator_info); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) config.dev = pdev->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) config.regmap = pmic->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) config.driver_data = &hi6421v530_regulator_info[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) rdev = devm_regulator_register(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) &hi6421v530_regulator_info[i].rdesc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) &config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (IS_ERR(rdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) dev_err(&pdev->dev, "failed to register regulator %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) hi6421v530_regulator_info[i].rdesc.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return PTR_ERR(rdev);
^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) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static const struct platform_device_id hi6421v530_regulator_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) { .name = "hi6421v530-regulator" },
^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_DEVICE_TABLE(platform, hi6421v530_regulator_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static struct platform_driver hi6421v530_regulator_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) .id_table = hi6421v530_regulator_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) .name = "hi6421v530-regulator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) .probe = hi6421v530_regulator_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) module_platform_driver(hi6421v530_regulator_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) MODULE_AUTHOR("Wang Xiaoyin <hw.wangxiaoyin@hisilicon.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) MODULE_DESCRIPTION("Hi6421v530 regulator driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) MODULE_LICENSE("GPL v2");