Kuninori Morimoto | ef4de050 | 2018-11-08 06:45:01 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 2 | /* |
| 3 | * AS3711 PMIC regulator driver, using DCDC Step Down and LDO supplies |
| 4 | * |
| 5 | * Copyright (C) 2012 Renesas Electronics Corporation |
| 6 | * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de> |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/err.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/mfd/as3711.h> |
| 12 | #include <linux/module.h> |
Guennadi Liakhovetski | 416d675 | 2013-03-22 17:15:48 +0100 | [diff] [blame] | 13 | #include <linux/of.h> |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/regmap.h> |
| 16 | #include <linux/regulator/driver.h> |
Guennadi Liakhovetski | 416d675 | 2013-03-22 17:15:48 +0100 | [diff] [blame] | 17 | #include <linux/regulator/of_regulator.h> |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 18 | #include <linux/slab.h> |
| 19 | |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 20 | /* |
| 21 | * The regulator API supports 4 modes of operataion: FAST, NORMAL, IDLE and |
| 22 | * STANDBY. We map them in the following way to AS3711 SD1-4 DCDC modes: |
| 23 | * FAST: sdX_fast=1 |
| 24 | * NORMAL: low_noise=1 |
| 25 | * IDLE: low_noise=0 |
| 26 | */ |
| 27 | |
| 28 | static int as3711_set_mode_sd(struct regulator_dev *rdev, unsigned int mode) |
| 29 | { |
| 30 | unsigned int fast_bit = rdev->desc->enable_mask, |
| 31 | low_noise_bit = fast_bit << 4; |
| 32 | u8 val; |
| 33 | |
| 34 | switch (mode) { |
| 35 | case REGULATOR_MODE_FAST: |
| 36 | val = fast_bit | low_noise_bit; |
| 37 | break; |
| 38 | case REGULATOR_MODE_NORMAL: |
| 39 | val = low_noise_bit; |
| 40 | break; |
| 41 | case REGULATOR_MODE_IDLE: |
| 42 | val = 0; |
| 43 | break; |
| 44 | default: |
| 45 | return -EINVAL; |
| 46 | } |
| 47 | |
| 48 | return regmap_update_bits(rdev->regmap, AS3711_SD_CONTROL_1, |
| 49 | low_noise_bit | fast_bit, val); |
| 50 | } |
| 51 | |
| 52 | static unsigned int as3711_get_mode_sd(struct regulator_dev *rdev) |
| 53 | { |
| 54 | unsigned int fast_bit = rdev->desc->enable_mask, |
| 55 | low_noise_bit = fast_bit << 4, mask = fast_bit | low_noise_bit; |
| 56 | unsigned int val; |
| 57 | int ret = regmap_read(rdev->regmap, AS3711_SD_CONTROL_1, &val); |
| 58 | |
| 59 | if (ret < 0) |
| 60 | return ret; |
| 61 | |
| 62 | if ((val & mask) == mask) |
| 63 | return REGULATOR_MODE_FAST; |
| 64 | |
| 65 | if ((val & mask) == low_noise_bit) |
| 66 | return REGULATOR_MODE_NORMAL; |
| 67 | |
| 68 | if (!(val & mask)) |
| 69 | return REGULATOR_MODE_IDLE; |
| 70 | |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | |
Bhumika Goyal | 1bacf46 | 2017-01-28 19:25:00 +0530 | [diff] [blame] | 74 | static const struct regulator_ops as3711_sd_ops = { |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 75 | .is_enabled = regulator_is_enabled_regmap, |
| 76 | .enable = regulator_enable_regmap, |
| 77 | .disable = regulator_disable_regmap, |
| 78 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 79 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Axel Lin | 9234c63 | 2013-07-16 16:34:39 +0800 | [diff] [blame] | 80 | .list_voltage = regulator_list_voltage_linear_range, |
| 81 | .map_voltage = regulator_map_voltage_linear_range, |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 82 | .get_mode = as3711_get_mode_sd, |
| 83 | .set_mode = as3711_set_mode_sd, |
| 84 | }; |
| 85 | |
Bhumika Goyal | 1bacf46 | 2017-01-28 19:25:00 +0530 | [diff] [blame] | 86 | static const struct regulator_ops as3711_aldo_ops = { |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 87 | .is_enabled = regulator_is_enabled_regmap, |
| 88 | .enable = regulator_enable_regmap, |
| 89 | .disable = regulator_disable_regmap, |
| 90 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 91 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Axel Lin | 9234c63 | 2013-07-16 16:34:39 +0800 | [diff] [blame] | 92 | .list_voltage = regulator_list_voltage_linear_range, |
| 93 | .map_voltage = regulator_map_voltage_linear_range, |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
Bhumika Goyal | 1bacf46 | 2017-01-28 19:25:00 +0530 | [diff] [blame] | 96 | static const struct regulator_ops as3711_dldo_ops = { |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 97 | .is_enabled = regulator_is_enabled_regmap, |
| 98 | .enable = regulator_enable_regmap, |
| 99 | .disable = regulator_disable_regmap, |
| 100 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 101 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Axel Lin | 9234c63 | 2013-07-16 16:34:39 +0800 | [diff] [blame] | 102 | .list_voltage = regulator_list_voltage_linear_range, |
| 103 | .map_voltage = regulator_map_voltage_linear_range, |
| 104 | }; |
| 105 | |
Matti Vaittinen | 60ab7f4 | 2020-05-08 18:43:36 +0300 | [diff] [blame] | 106 | static const struct linear_range as3711_sd_ranges[] = { |
Axel Lin | 8828bae | 2013-10-11 09:32:18 +0800 | [diff] [blame] | 107 | REGULATOR_LINEAR_RANGE(612500, 0x1, 0x40, 12500), |
| 108 | REGULATOR_LINEAR_RANGE(1425000, 0x41, 0x70, 25000), |
| 109 | REGULATOR_LINEAR_RANGE(2650000, 0x71, 0x7f, 50000), |
Axel Lin | 9234c63 | 2013-07-16 16:34:39 +0800 | [diff] [blame] | 110 | }; |
| 111 | |
Matti Vaittinen | 60ab7f4 | 2020-05-08 18:43:36 +0300 | [diff] [blame] | 112 | static const struct linear_range as3711_aldo_ranges[] = { |
Axel Lin | 8828bae | 2013-10-11 09:32:18 +0800 | [diff] [blame] | 113 | REGULATOR_LINEAR_RANGE(1200000, 0, 0xf, 50000), |
| 114 | REGULATOR_LINEAR_RANGE(1800000, 0x10, 0x1f, 100000), |
Axel Lin | 9234c63 | 2013-07-16 16:34:39 +0800 | [diff] [blame] | 115 | }; |
| 116 | |
Matti Vaittinen | 60ab7f4 | 2020-05-08 18:43:36 +0300 | [diff] [blame] | 117 | static const struct linear_range as3711_dldo_ranges[] = { |
Axel Lin | 8828bae | 2013-10-11 09:32:18 +0800 | [diff] [blame] | 118 | REGULATOR_LINEAR_RANGE(900000, 0, 0x10, 50000), |
| 119 | REGULATOR_LINEAR_RANGE(1750000, 0x20, 0x3f, 50000), |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
Axel Lin | 192264e | 2014-09-07 18:34:51 +0800 | [diff] [blame] | 122 | #define AS3711_REG(_id, _en_reg, _en_bit, _vmask, _sfx) \ |
| 123 | [AS3711_REGULATOR_ ## _id] = { \ |
Axel Lin | 192264e | 2014-09-07 18:34:51 +0800 | [diff] [blame] | 124 | .name = "as3711-regulator-" # _id, \ |
| 125 | .id = AS3711_REGULATOR_ ## _id, \ |
| 126 | .n_voltages = (_vmask + 1), \ |
| 127 | .ops = &as3711_ ## _sfx ## _ops, \ |
| 128 | .type = REGULATOR_VOLTAGE, \ |
| 129 | .owner = THIS_MODULE, \ |
| 130 | .vsel_reg = AS3711_ ## _id ## _VOLTAGE, \ |
| 131 | .vsel_mask = _vmask, \ |
| 132 | .enable_reg = AS3711_ ## _en_reg, \ |
| 133 | .enable_mask = BIT(_en_bit), \ |
| 134 | .linear_ranges = as3711_ ## _sfx ## _ranges, \ |
| 135 | .n_linear_ranges = ARRAY_SIZE(as3711_ ## _sfx ## _ranges), \ |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Axel Lin | f668a1db | 2019-03-06 09:01:02 +0800 | [diff] [blame] | 138 | static const struct regulator_desc as3711_reg_desc[] = { |
Axel Lin | 192264e | 2014-09-07 18:34:51 +0800 | [diff] [blame] | 139 | AS3711_REG(SD_1, SD_CONTROL, 0, 0x7f, sd), |
| 140 | AS3711_REG(SD_2, SD_CONTROL, 1, 0x7f, sd), |
| 141 | AS3711_REG(SD_3, SD_CONTROL, 2, 0x7f, sd), |
| 142 | AS3711_REG(SD_4, SD_CONTROL, 3, 0x7f, sd), |
| 143 | AS3711_REG(LDO_1, LDO_1_VOLTAGE, 7, 0x1f, aldo), |
| 144 | AS3711_REG(LDO_2, LDO_2_VOLTAGE, 7, 0x1f, aldo), |
| 145 | AS3711_REG(LDO_3, LDO_3_VOLTAGE, 7, 0x3f, dldo), |
| 146 | AS3711_REG(LDO_4, LDO_4_VOLTAGE, 7, 0x3f, dldo), |
| 147 | AS3711_REG(LDO_5, LDO_5_VOLTAGE, 7, 0x3f, dldo), |
| 148 | AS3711_REG(LDO_6, LDO_6_VOLTAGE, 7, 0x3f, dldo), |
| 149 | AS3711_REG(LDO_7, LDO_7_VOLTAGE, 7, 0x3f, dldo), |
| 150 | AS3711_REG(LDO_8, LDO_8_VOLTAGE, 7, 0x3f, dldo), |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 151 | /* StepUp output voltage depends on supplying regulator */ |
| 152 | }; |
| 153 | |
Axel Lin | f668a1db | 2019-03-06 09:01:02 +0800 | [diff] [blame] | 154 | #define AS3711_REGULATOR_NUM ARRAY_SIZE(as3711_reg_desc) |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 155 | |
Axel Lin | d6c7d73 | 2013-04-17 16:01:22 +0800 | [diff] [blame] | 156 | static struct of_regulator_match |
| 157 | as3711_regulator_matches[AS3711_REGULATOR_NUM] = { |
| 158 | [AS3711_REGULATOR_SD_1] = { .name = "sd1" }, |
| 159 | [AS3711_REGULATOR_SD_2] = { .name = "sd2" }, |
| 160 | [AS3711_REGULATOR_SD_3] = { .name = "sd3" }, |
| 161 | [AS3711_REGULATOR_SD_4] = { .name = "sd4" }, |
| 162 | [AS3711_REGULATOR_LDO_1] = { .name = "ldo1" }, |
| 163 | [AS3711_REGULATOR_LDO_2] = { .name = "ldo2" }, |
| 164 | [AS3711_REGULATOR_LDO_3] = { .name = "ldo3" }, |
| 165 | [AS3711_REGULATOR_LDO_4] = { .name = "ldo4" }, |
| 166 | [AS3711_REGULATOR_LDO_5] = { .name = "ldo5" }, |
| 167 | [AS3711_REGULATOR_LDO_6] = { .name = "ldo6" }, |
| 168 | [AS3711_REGULATOR_LDO_7] = { .name = "ldo7" }, |
| 169 | [AS3711_REGULATOR_LDO_8] = { .name = "ldo8" }, |
Guennadi Liakhovetski | 416d675 | 2013-03-22 17:15:48 +0100 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | static int as3711_regulator_parse_dt(struct device *dev, |
| 173 | struct device_node **of_node, const int count) |
| 174 | { |
| 175 | struct as3711_regulator_pdata *pdata = dev_get_platdata(dev); |
| 176 | struct device_node *regulators = |
Sachin Kamat | eecb02c | 2014-02-14 17:19:55 +0530 | [diff] [blame] | 177 | of_get_child_by_name(dev->parent->of_node, "regulators"); |
Axel Lin | d6c7d73 | 2013-04-17 16:01:22 +0800 | [diff] [blame] | 178 | struct of_regulator_match *match; |
Guennadi Liakhovetski | 416d675 | 2013-03-22 17:15:48 +0100 | [diff] [blame] | 179 | int ret, i; |
| 180 | |
| 181 | if (!regulators) { |
| 182 | dev_err(dev, "regulator node not found\n"); |
| 183 | return -ENODEV; |
| 184 | } |
| 185 | |
Axel Lin | d6c7d73 | 2013-04-17 16:01:22 +0800 | [diff] [blame] | 186 | ret = of_regulator_match(dev->parent, regulators, |
| 187 | as3711_regulator_matches, count); |
Guennadi Liakhovetski | 416d675 | 2013-03-22 17:15:48 +0100 | [diff] [blame] | 188 | of_node_put(regulators); |
| 189 | if (ret < 0) { |
| 190 | dev_err(dev, "Error parsing regulator init data: %d\n", ret); |
| 191 | return ret; |
| 192 | } |
| 193 | |
Axel Lin | d6c7d73 | 2013-04-17 16:01:22 +0800 | [diff] [blame] | 194 | for (i = 0, match = as3711_regulator_matches; i < count; i++, match++) |
Guennadi Liakhovetski | 416d675 | 2013-03-22 17:15:48 +0100 | [diff] [blame] | 195 | if (match->of_node) { |
| 196 | pdata->init_data[i] = match->init_data; |
| 197 | of_node[i] = match->of_node; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 203 | static int as3711_regulator_probe(struct platform_device *pdev) |
| 204 | { |
| 205 | struct as3711_regulator_pdata *pdata = dev_get_platdata(&pdev->dev); |
| 206 | struct as3711 *as3711 = dev_get_drvdata(pdev->dev.parent); |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 207 | struct regulator_config config = {.dev = &pdev->dev,}; |
Guennadi Liakhovetski | 416d675 | 2013-03-22 17:15:48 +0100 | [diff] [blame] | 208 | struct device_node *of_node[AS3711_REGULATOR_NUM] = {}; |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 209 | struct regulator_dev *rdev; |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 210 | int ret; |
| 211 | int id; |
| 212 | |
Guennadi Liakhovetski | 416d675 | 2013-03-22 17:15:48 +0100 | [diff] [blame] | 213 | if (!pdata) { |
| 214 | dev_err(&pdev->dev, "No platform data...\n"); |
| 215 | return -ENODEV; |
| 216 | } |
| 217 | |
| 218 | if (pdev->dev.parent->of_node) { |
| 219 | ret = as3711_regulator_parse_dt(&pdev->dev, of_node, AS3711_REGULATOR_NUM); |
| 220 | if (ret < 0) { |
| 221 | dev_err(&pdev->dev, "DT parsing failed: %d\n", ret); |
| 222 | return ret; |
| 223 | } |
| 224 | } |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 225 | |
Axel Lin | f668a1db | 2019-03-06 09:01:02 +0800 | [diff] [blame] | 226 | for (id = 0; id < AS3711_REGULATOR_NUM; id++) { |
Axel Lin | f7e9e52 | 2014-02-06 14:47:41 +0800 | [diff] [blame] | 227 | config.init_data = pdata->init_data[id]; |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 228 | config.regmap = as3711->regmap; |
Guennadi Liakhovetski | 416d675 | 2013-03-22 17:15:48 +0100 | [diff] [blame] | 229 | config.of_node = of_node[id]; |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 230 | |
Axel Lin | f668a1db | 2019-03-06 09:01:02 +0800 | [diff] [blame] | 231 | rdev = devm_regulator_register(&pdev->dev, &as3711_reg_desc[id], |
| 232 | &config); |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 233 | if (IS_ERR(rdev)) { |
| 234 | dev_err(&pdev->dev, "Failed to register regulator %s\n", |
Axel Lin | f668a1db | 2019-03-06 09:01:02 +0800 | [diff] [blame] | 235 | as3711_reg_desc[id].name); |
Axel Lin | 27447c9 | 2013-09-03 14:22:00 +0800 | [diff] [blame] | 236 | return PTR_ERR(rdev); |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 237 | } |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 238 | } |
Axel Lin | f668a1db | 2019-03-06 09:01:02 +0800 | [diff] [blame] | 239 | |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 240 | return 0; |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static struct platform_driver as3711_regulator_driver = { |
| 244 | .driver = { |
| 245 | .name = "as3711-regulator", |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 246 | }, |
| 247 | .probe = as3711_regulator_probe, |
Guennadi Liakhovetski | f1e64f9 | 2012-11-22 11:12:08 +0100 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | static int __init as3711_regulator_init(void) |
| 251 | { |
| 252 | return platform_driver_register(&as3711_regulator_driver); |
| 253 | } |
| 254 | subsys_initcall(as3711_regulator_init); |
| 255 | |
| 256 | static void __exit as3711_regulator_exit(void) |
| 257 | { |
| 258 | platform_driver_unregister(&as3711_regulator_driver); |
| 259 | } |
| 260 | module_exit(as3711_regulator_exit); |
| 261 | |
| 262 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); |
| 263 | MODULE_DESCRIPTION("AS3711 regulator driver"); |
| 264 | MODULE_ALIAS("platform:as3711-regulator"); |
| 265 | MODULE_LICENSE("GPL v2"); |