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