|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Driver for regulators found in the SpacemiT P1 PMIC |
| 4 | + * |
| 5 | + * Copyright (C) 2025 by RISCstar Solutions Corporation. All rights reserved. |
| 6 | + * Derived from code from SpacemiT. |
| 7 | + * Copyright (c) 2023, SPACEMIT Co., Ltd |
| 8 | + */ |
| 9 | + |
| 10 | +#include <linux/array_size.h> |
| 11 | +#include <linux/bits.h> |
| 12 | +#include <linux/device.h> |
| 13 | +#include <linux/linear_range.h> |
| 14 | +#include <linux/module.h> |
| 15 | +#include <linux/of.h> |
| 16 | +#include <linux/platform_device.h> |
| 17 | +#include <linux/regulator/driver.h> |
| 18 | + |
| 19 | +#define MOD_NAME "spacemit-p1-regulator" |
| 20 | + |
| 21 | +enum p1_regulator_id { |
| 22 | + P1_BUCK1, |
| 23 | + P1_BUCK2, |
| 24 | + P1_BUCK3, |
| 25 | + P1_BUCK4, |
| 26 | + P1_BUCK5, |
| 27 | + P1_BUCK6, |
| 28 | + |
| 29 | + P1_ALDO1, |
| 30 | + P1_ALDO2, |
| 31 | + P1_ALDO3, |
| 32 | + P1_ALDO4, |
| 33 | + |
| 34 | + P1_DLDO1, |
| 35 | + P1_DLDO2, |
| 36 | + P1_DLDO3, |
| 37 | + P1_DLDO4, |
| 38 | + P1_DLDO5, |
| 39 | + P1_DLDO6, |
| 40 | + P1_DLDO7, |
| 41 | +}; |
| 42 | + |
| 43 | +static const struct regulator_ops p1_regulator_ops = { |
| 44 | + .list_voltage = regulator_list_voltage_linear_range, |
| 45 | + .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 46 | + .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 47 | + .set_voltage_time_sel = regulator_set_voltage_time_sel, |
| 48 | + .enable = regulator_enable_regmap, |
| 49 | + .disable = regulator_disable_regmap, |
| 50 | + .is_enabled = regulator_is_enabled_regmap, |
| 51 | +}; |
| 52 | + |
| 53 | +/* Selector value 255 can be used to disable the buck converter on sleep */ |
| 54 | +static const struct linear_range p1_buck_ranges[] = { |
| 55 | + REGULATOR_LINEAR_RANGE(500000, 0, 170, 5000), |
| 56 | + REGULATOR_LINEAR_RANGE(1375000, 171, 254, 25000), |
| 57 | +}; |
| 58 | + |
| 59 | +/* Selector value 0 can be used for suspend */ |
| 60 | +static const struct linear_range p1_ldo_ranges[] = { |
| 61 | + REGULATOR_LINEAR_RANGE(500000, 11, 127, 25000), |
| 62 | +}; |
| 63 | + |
| 64 | +/* These define the voltage selector field for buck and LDO regulators */ |
| 65 | +#define BUCK_MASK GENMASK(7, 0) |
| 66 | +#define LDO_MASK GENMASK(6, 0) |
| 67 | + |
| 68 | +#define P1_ID(_TYPE, _n) P1_ ## _TYPE ## _n |
| 69 | +#define P1_ENABLE_REG(_off, _n) ((_off) + 3 * ((_n) - 1)) |
| 70 | + |
| 71 | +#define P1_REG_DESC(_TYPE, _type, _n, _s, _off, _mask, _nv, _ranges) \ |
| 72 | + { \ |
| 73 | + .name = #_type #_n, \ |
| 74 | + .supply_name = _s, \ |
| 75 | + .of_match = of_match_ptr(#_type #_n), \ |
| 76 | + .regulators_node = of_match_ptr("regulators"), \ |
| 77 | + .id = P1_ID(_TYPE, _n), \ |
| 78 | + .n_voltages = _nv, \ |
| 79 | + .ops = &p1_regulator_ops, \ |
| 80 | + .owner = THIS_MODULE, \ |
| 81 | + .linear_ranges = _ranges, \ |
| 82 | + .n_linear_ranges = ARRAY_SIZE(_ranges), \ |
| 83 | + .vsel_reg = P1_ENABLE_REG(_off, _n) + 1, \ |
| 84 | + .vsel_mask = _mask, \ |
| 85 | + .enable_reg = P1_ENABLE_REG(_off, _n), \ |
| 86 | + .enable_mask = BIT(0), \ |
| 87 | + } |
| 88 | + |
| 89 | +#define P1_BUCK_DESC(_n) \ |
| 90 | + P1_REG_DESC(BUCK, buck, _n, "vcc", 0x47, BUCK_MASK, 254, p1_buck_ranges) |
| 91 | + |
| 92 | +#define P1_ALDO_DESC(_n) \ |
| 93 | + P1_REG_DESC(ALDO, aldo, _n, "vcc", 0x5b, LDO_MASK, 117, p1_ldo_ranges) |
| 94 | + |
| 95 | +#define P1_DLDO_DESC(_n) \ |
| 96 | + P1_REG_DESC(DLDO, dldo, _n, "buck5", 0x67, LDO_MASK, 117, p1_ldo_ranges) |
| 97 | + |
| 98 | +static const struct regulator_desc p1_regulator_desc[] = { |
| 99 | + P1_BUCK_DESC(1), |
| 100 | + P1_BUCK_DESC(2), |
| 101 | + P1_BUCK_DESC(3), |
| 102 | + P1_BUCK_DESC(4), |
| 103 | + P1_BUCK_DESC(5), |
| 104 | + P1_BUCK_DESC(6), |
| 105 | + |
| 106 | + P1_ALDO_DESC(1), |
| 107 | + P1_ALDO_DESC(2), |
| 108 | + P1_ALDO_DESC(3), |
| 109 | + P1_ALDO_DESC(4), |
| 110 | + |
| 111 | + P1_DLDO_DESC(1), |
| 112 | + P1_DLDO_DESC(2), |
| 113 | + P1_DLDO_DESC(3), |
| 114 | + P1_DLDO_DESC(4), |
| 115 | + P1_DLDO_DESC(5), |
| 116 | + P1_DLDO_DESC(6), |
| 117 | + P1_DLDO_DESC(7), |
| 118 | +}; |
| 119 | + |
| 120 | +static int p1_regulator_probe(struct platform_device *pdev) |
| 121 | +{ |
| 122 | + struct regulator_config config = { }; |
| 123 | + struct device *dev = &pdev->dev; |
| 124 | + u32 i; |
| 125 | + |
| 126 | + /* |
| 127 | + * The parent device (PMIC) owns the regmap. Since we don't |
| 128 | + * provide one in the config structure, that one will be used. |
| 129 | + */ |
| 130 | + config.dev = dev->parent; |
| 131 | + |
| 132 | + for (i = 0; i < ARRAY_SIZE(p1_regulator_desc); i++) { |
| 133 | + const struct regulator_desc *desc = &p1_regulator_desc[i]; |
| 134 | + struct regulator_dev *rdev; |
| 135 | + |
| 136 | + rdev = devm_regulator_register(dev, desc, &config); |
| 137 | + if (IS_ERR(rdev)) |
| 138 | + return dev_err_probe(dev, PTR_ERR(rdev), |
| 139 | + "error registering regulator %s\n", |
| 140 | + desc->name); |
| 141 | + } |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +static struct platform_driver p1_regulator_driver = { |
| 147 | + .probe = p1_regulator_probe, |
| 148 | + .driver = { |
| 149 | + .name = MOD_NAME, |
| 150 | + }, |
| 151 | +}; |
| 152 | + |
| 153 | +module_platform_driver(p1_regulator_driver); |
| 154 | + |
| 155 | +MODULE_DESCRIPTION("SpacemiT P1 regulator driver"); |
| 156 | +MODULE_LICENSE("GPL"); |
| 157 | +MODULE_ALIAS("platform:" MOD_NAME); |
0 commit comments