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