Charles Keepax | bef9391 | 2018-10-08 14:25:41 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Charles Keepax | d8b2a8e | 2018-10-19 13:44:38 +0100 | [diff] [blame] | 2 | // |
| 3 | // Lochnagar regulator driver |
| 4 | // |
| 5 | // Copyright (c) 2017-2018 Cirrus Logic, Inc. and |
| 6 | // Cirrus Logic International Semiconductor Ltd. |
| 7 | // |
| 8 | // Author: Charles Keepax <ckeepax@opensource.cirrus.com> |
Charles Keepax | bef9391 | 2018-10-08 14:25:41 +0100 | [diff] [blame] | 9 | |
| 10 | #include <linux/bitops.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/mutex.h> |
| 15 | #include <linux/of.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/regmap.h> |
| 18 | #include <linux/regulator/driver.h> |
| 19 | #include <linux/regulator/machine.h> |
| 20 | #include <linux/regulator/of_regulator.h> |
| 21 | |
| 22 | #include <linux/mfd/lochnagar.h> |
Charles Keepax | fa2bb8b | 2018-11-08 10:14:00 +0000 | [diff] [blame^] | 23 | #include <linux/mfd/lochnagar1_regs.h> |
| 24 | #include <linux/mfd/lochnagar2_regs.h> |
Charles Keepax | bef9391 | 2018-10-08 14:25:41 +0100 | [diff] [blame] | 25 | |
| 26 | static const struct regulator_ops lochnagar_micvdd_ops = { |
| 27 | .enable = regulator_enable_regmap, |
| 28 | .disable = regulator_disable_regmap, |
| 29 | .is_enabled = regulator_is_enabled_regmap, |
| 30 | |
| 31 | .list_voltage = regulator_list_voltage_linear_range, |
| 32 | .map_voltage = regulator_map_voltage_linear_range, |
| 33 | |
| 34 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 35 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 36 | }; |
| 37 | |
| 38 | static const struct regulator_linear_range lochnagar_micvdd_ranges[] = { |
| 39 | REGULATOR_LINEAR_RANGE(1000000, 0, 0xC, 50000), |
| 40 | REGULATOR_LINEAR_RANGE(1700000, 0xD, 0x1F, 100000), |
| 41 | }; |
| 42 | |
| 43 | static int lochnagar_micbias_enable(struct regulator_dev *rdev) |
| 44 | { |
| 45 | struct lochnagar *lochnagar = rdev_get_drvdata(rdev); |
| 46 | int ret; |
| 47 | |
| 48 | mutex_lock(&lochnagar->analogue_config_lock); |
| 49 | |
| 50 | ret = regulator_enable_regmap(rdev); |
| 51 | if (ret < 0) |
| 52 | goto err; |
| 53 | |
| 54 | ret = lochnagar_update_config(lochnagar); |
| 55 | |
| 56 | err: |
| 57 | mutex_unlock(&lochnagar->analogue_config_lock); |
| 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | static int lochnagar_micbias_disable(struct regulator_dev *rdev) |
| 63 | { |
| 64 | struct lochnagar *lochnagar = rdev_get_drvdata(rdev); |
| 65 | int ret; |
| 66 | |
| 67 | mutex_lock(&lochnagar->analogue_config_lock); |
| 68 | |
| 69 | ret = regulator_disable_regmap(rdev); |
| 70 | if (ret < 0) |
| 71 | goto err; |
| 72 | |
| 73 | ret = lochnagar_update_config(lochnagar); |
| 74 | |
| 75 | err: |
| 76 | mutex_unlock(&lochnagar->analogue_config_lock); |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | static const struct regulator_ops lochnagar_micbias_ops = { |
| 82 | .enable = lochnagar_micbias_enable, |
| 83 | .disable = lochnagar_micbias_disable, |
| 84 | .is_enabled = regulator_is_enabled_regmap, |
| 85 | }; |
| 86 | |
| 87 | static const struct regulator_ops lochnagar_vddcore_ops = { |
| 88 | .enable = regulator_enable_regmap, |
| 89 | .disable = regulator_disable_regmap, |
| 90 | .is_enabled = regulator_is_enabled_regmap, |
| 91 | |
| 92 | .list_voltage = regulator_list_voltage_linear_range, |
| 93 | .map_voltage = regulator_map_voltage_linear_range, |
| 94 | |
| 95 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 96 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 97 | }; |
| 98 | |
| 99 | static const struct regulator_linear_range lochnagar_vddcore_ranges[] = { |
| 100 | REGULATOR_LINEAR_RANGE(600000, 0x8, 0x41, 12500), |
| 101 | }; |
| 102 | |
| 103 | enum lochnagar_regulators { |
| 104 | LOCHNAGAR_MICVDD, |
| 105 | LOCHNAGAR_MIC1VDD, |
| 106 | LOCHNAGAR_MIC2VDD, |
| 107 | LOCHNAGAR_VDDCORE, |
| 108 | }; |
| 109 | |
| 110 | static int lochnagar_micbias_of_parse(struct device_node *np, |
| 111 | const struct regulator_desc *desc, |
| 112 | struct regulator_config *config) |
| 113 | { |
| 114 | struct lochnagar *lochnagar = config->driver_data; |
| 115 | int shift = (desc->id - LOCHNAGAR_MIC1VDD) * |
| 116 | LOCHNAGAR2_P2_MICBIAS_SRC_SHIFT; |
| 117 | int mask = LOCHNAGAR2_P1_MICBIAS_SRC_MASK << shift; |
| 118 | unsigned int val; |
| 119 | int ret; |
| 120 | |
| 121 | ret = of_property_read_u32(np, "cirrus,micbias-input", &val); |
| 122 | if (ret >= 0) { |
| 123 | mutex_lock(&lochnagar->analogue_config_lock); |
| 124 | ret = regmap_update_bits(lochnagar->regmap, |
| 125 | LOCHNAGAR2_ANALOGUE_PATH_CTRL2, |
| 126 | mask, val << shift); |
| 127 | mutex_unlock(&lochnagar->analogue_config_lock); |
| 128 | if (ret < 0) { |
| 129 | dev_err(lochnagar->dev, |
| 130 | "Failed to update micbias source: %d\n", ret); |
| 131 | return ret; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static const struct regulator_desc lochnagar_regulators[] = { |
| 139 | [LOCHNAGAR_MICVDD] = { |
| 140 | .name = "MICVDD", |
| 141 | .supply_name = "SYSVDD", |
| 142 | .type = REGULATOR_VOLTAGE, |
| 143 | .n_voltages = 32, |
| 144 | .ops = &lochnagar_micvdd_ops, |
| 145 | |
| 146 | .id = LOCHNAGAR_MICVDD, |
| 147 | .of_match = of_match_ptr("MICVDD"), |
| 148 | |
| 149 | .enable_reg = LOCHNAGAR2_MICVDD_CTRL1, |
| 150 | .enable_mask = LOCHNAGAR2_MICVDD_REG_ENA_MASK, |
| 151 | .vsel_reg = LOCHNAGAR2_MICVDD_CTRL2, |
| 152 | .vsel_mask = LOCHNAGAR2_MICVDD_VSEL_MASK, |
| 153 | |
| 154 | .linear_ranges = lochnagar_micvdd_ranges, |
| 155 | .n_linear_ranges = ARRAY_SIZE(lochnagar_micvdd_ranges), |
| 156 | |
| 157 | .enable_time = 3000, |
| 158 | .ramp_delay = 1000, |
| 159 | |
| 160 | .owner = THIS_MODULE, |
| 161 | }, |
| 162 | [LOCHNAGAR_MIC1VDD] = { |
| 163 | .name = "MIC1VDD", |
| 164 | .supply_name = "MICBIAS1", |
| 165 | .type = REGULATOR_VOLTAGE, |
| 166 | .ops = &lochnagar_micbias_ops, |
| 167 | |
| 168 | .id = LOCHNAGAR_MIC1VDD, |
| 169 | .of_match = of_match_ptr("MIC1VDD"), |
| 170 | .of_parse_cb = lochnagar_micbias_of_parse, |
| 171 | |
| 172 | .enable_reg = LOCHNAGAR2_ANALOGUE_PATH_CTRL2, |
| 173 | .enable_mask = LOCHNAGAR2_P1_INPUT_BIAS_ENA_MASK, |
| 174 | |
| 175 | .owner = THIS_MODULE, |
| 176 | }, |
| 177 | [LOCHNAGAR_MIC2VDD] = { |
| 178 | .name = "MIC2VDD", |
| 179 | .supply_name = "MICBIAS2", |
| 180 | .type = REGULATOR_VOLTAGE, |
| 181 | .ops = &lochnagar_micbias_ops, |
| 182 | |
| 183 | .id = LOCHNAGAR_MIC2VDD, |
| 184 | .of_match = of_match_ptr("MIC2VDD"), |
| 185 | .of_parse_cb = lochnagar_micbias_of_parse, |
| 186 | |
| 187 | .enable_reg = LOCHNAGAR2_ANALOGUE_PATH_CTRL2, |
| 188 | .enable_mask = LOCHNAGAR2_P2_INPUT_BIAS_ENA_MASK, |
| 189 | |
| 190 | .owner = THIS_MODULE, |
| 191 | }, |
| 192 | [LOCHNAGAR_VDDCORE] = { |
| 193 | .name = "VDDCORE", |
| 194 | .supply_name = "SYSVDD", |
| 195 | .type = REGULATOR_VOLTAGE, |
| 196 | .n_voltages = 57, |
| 197 | .ops = &lochnagar_vddcore_ops, |
| 198 | |
| 199 | .id = LOCHNAGAR_VDDCORE, |
| 200 | .of_match = of_match_ptr("VDDCORE"), |
| 201 | |
| 202 | .enable_reg = LOCHNAGAR2_VDDCORE_CDC_CTRL1, |
| 203 | .enable_mask = LOCHNAGAR2_VDDCORE_CDC_REG_ENA_MASK, |
| 204 | .vsel_reg = LOCHNAGAR2_VDDCORE_CDC_CTRL2, |
| 205 | .vsel_mask = LOCHNAGAR2_VDDCORE_CDC_VSEL_MASK, |
| 206 | |
| 207 | .linear_ranges = lochnagar_vddcore_ranges, |
| 208 | .n_linear_ranges = ARRAY_SIZE(lochnagar_vddcore_ranges), |
| 209 | |
| 210 | .enable_time = 3000, |
| 211 | .ramp_delay = 1000, |
| 212 | |
| 213 | .owner = THIS_MODULE, |
| 214 | }, |
| 215 | }; |
| 216 | |
| 217 | static int lochnagar_regulator_probe(struct platform_device *pdev) |
| 218 | { |
| 219 | struct device *dev = &pdev->dev; |
| 220 | struct lochnagar *lochnagar = dev_get_drvdata(dev->parent); |
| 221 | struct regulator_config config = { }; |
| 222 | struct regulator_dev *rdev; |
| 223 | int ret, i; |
| 224 | |
| 225 | config.dev = lochnagar->dev; |
| 226 | config.regmap = lochnagar->regmap; |
| 227 | config.driver_data = lochnagar; |
| 228 | |
| 229 | for (i = 0; i < ARRAY_SIZE(lochnagar_regulators); i++) { |
| 230 | const struct regulator_desc *desc = &lochnagar_regulators[i]; |
| 231 | |
| 232 | rdev = devm_regulator_register(dev, desc, &config); |
| 233 | if (IS_ERR(rdev)) { |
| 234 | ret = PTR_ERR(rdev); |
| 235 | dev_err(dev, "Failed to register %s regulator: %d\n", |
| 236 | desc->name, ret); |
| 237 | return ret; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static struct platform_driver lochnagar_regulator_driver = { |
| 245 | .driver = { |
| 246 | .name = "lochnagar-regulator", |
| 247 | }, |
| 248 | |
| 249 | .probe = lochnagar_regulator_probe, |
| 250 | }; |
| 251 | module_platform_driver(lochnagar_regulator_driver); |
| 252 | |
| 253 | MODULE_AUTHOR("Charles Keepax <ckeepax@opensource.cirrus.com>"); |
| 254 | MODULE_DESCRIPTION("Regulator driver for Cirrus Logic Lochnagar Board"); |
| 255 | MODULE_LICENSE("GPL v2"); |
| 256 | MODULE_ALIAS("platform:lochnagar-regulator"); |