Milo Kim | 3a8d1a7 | 2015-11-26 15:57:05 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * TI LM363X Regulator Driver |
| 3 | * |
| 4 | * Copyright 2015 Texas Instruments |
| 5 | * |
| 6 | * Author: Milo Kim <milo.kim@ti.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/mfd/ti-lmu.h> |
| 16 | #include <linux/mfd/ti-lmu-register.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/of_gpio.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/regulator/driver.h> |
| 22 | #include <linux/regulator/of_regulator.h> |
| 23 | #include <linux/slab.h> |
| 24 | |
| 25 | /* LM3631 */ |
| 26 | #define LM3631_BOOST_VSEL_MAX 0x25 |
| 27 | #define LM3631_LDO_VSEL_MAX 0x28 |
| 28 | #define LM3631_CONT_VSEL_MAX 0x03 |
| 29 | #define LM3631_VBOOST_MIN 4500000 |
| 30 | #define LM3631_VCONT_MIN 1800000 |
| 31 | #define LM3631_VLDO_MIN 4000000 |
| 32 | #define ENABLE_TIME_USEC 1000 |
| 33 | |
| 34 | /* LM3632 */ |
| 35 | #define LM3632_BOOST_VSEL_MAX 0x26 |
| 36 | #define LM3632_LDO_VSEL_MAX 0x29 |
| 37 | #define LM3632_VBOOST_MIN 4500000 |
| 38 | #define LM3632_VLDO_MIN 4000000 |
| 39 | |
| 40 | /* Common */ |
| 41 | #define LM363X_STEP_50mV 50000 |
| 42 | #define LM363X_STEP_500mV 500000 |
| 43 | |
| 44 | struct lm363x_regulator { |
| 45 | struct regmap *regmap; |
| 46 | struct regulator_dev *regulator; |
| 47 | }; |
| 48 | |
| 49 | const int ldo_cont_enable_time[] = { |
| 50 | 0, 2000, 5000, 10000, 20000, 50000, 100000, 200000, |
| 51 | }; |
| 52 | |
| 53 | static int lm363x_regulator_enable_time(struct regulator_dev *rdev) |
| 54 | { |
| 55 | struct lm363x_regulator *lm363x_regulator = rdev_get_drvdata(rdev); |
| 56 | enum lm363x_regulator_id id = rdev_get_id(rdev); |
| 57 | u8 val, addr, mask; |
| 58 | |
| 59 | switch (id) { |
| 60 | case LM3631_LDO_CONT: |
| 61 | addr = LM3631_REG_ENTIME_VCONT; |
| 62 | mask = LM3631_ENTIME_CONT_MASK; |
| 63 | break; |
| 64 | case LM3631_LDO_OREF: |
| 65 | addr = LM3631_REG_ENTIME_VOREF; |
| 66 | mask = LM3631_ENTIME_MASK; |
| 67 | break; |
| 68 | case LM3631_LDO_POS: |
| 69 | addr = LM3631_REG_ENTIME_VPOS; |
| 70 | mask = LM3631_ENTIME_MASK; |
| 71 | break; |
| 72 | case LM3631_LDO_NEG: |
| 73 | addr = LM3631_REG_ENTIME_VNEG; |
| 74 | mask = LM3631_ENTIME_MASK; |
| 75 | break; |
| 76 | default: |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | if (regmap_read(lm363x_regulator->regmap, addr, (unsigned int *)&val)) |
| 81 | return -EINVAL; |
| 82 | |
| 83 | val = (val & mask) >> LM3631_ENTIME_SHIFT; |
| 84 | |
| 85 | if (id == LM3631_LDO_CONT) |
| 86 | return ldo_cont_enable_time[val]; |
| 87 | else |
| 88 | return ENABLE_TIME_USEC * val; |
| 89 | } |
| 90 | |
| 91 | static struct regulator_ops lm363x_boost_voltage_table_ops = { |
| 92 | .list_voltage = regulator_list_voltage_linear, |
| 93 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 94 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 95 | }; |
| 96 | |
| 97 | static struct regulator_ops lm363x_regulator_voltage_table_ops = { |
| 98 | .list_voltage = regulator_list_voltage_linear, |
| 99 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 100 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 101 | .enable = regulator_enable_regmap, |
| 102 | .disable = regulator_disable_regmap, |
| 103 | .is_enabled = regulator_is_enabled_regmap, |
| 104 | .enable_time = lm363x_regulator_enable_time, |
| 105 | }; |
| 106 | |
| 107 | static const struct regulator_desc lm363x_regulator_desc[] = { |
| 108 | /* LM3631 */ |
| 109 | { |
| 110 | .name = "vboost", |
| 111 | .of_match = "vboost", |
| 112 | .id = LM3631_BOOST, |
| 113 | .ops = &lm363x_boost_voltage_table_ops, |
| 114 | .n_voltages = LM3631_BOOST_VSEL_MAX + 1, |
| 115 | .min_uV = LM3631_VBOOST_MIN, |
| 116 | .uV_step = LM363X_STEP_50mV, |
| 117 | .type = REGULATOR_VOLTAGE, |
| 118 | .owner = THIS_MODULE, |
| 119 | .vsel_reg = LM3631_REG_VOUT_BOOST, |
| 120 | .vsel_mask = LM3631_VOUT_MASK, |
| 121 | }, |
| 122 | { |
| 123 | .name = "ldo_cont", |
| 124 | .of_match = "vcont", |
| 125 | .id = LM3631_LDO_CONT, |
| 126 | .ops = &lm363x_regulator_voltage_table_ops, |
| 127 | .n_voltages = LM3631_CONT_VSEL_MAX + 1, |
| 128 | .min_uV = LM3631_VCONT_MIN, |
| 129 | .uV_step = LM363X_STEP_500mV, |
| 130 | .type = REGULATOR_VOLTAGE, |
| 131 | .owner = THIS_MODULE, |
| 132 | .vsel_reg = LM3631_REG_VOUT_CONT, |
| 133 | .vsel_mask = LM3631_VOUT_CONT_MASK, |
| 134 | .enable_reg = LM3631_REG_LDO_CTRL2, |
| 135 | .enable_mask = LM3631_EN_CONT_MASK, |
| 136 | }, |
| 137 | { |
| 138 | .name = "ldo_oref", |
| 139 | .of_match = "voref", |
| 140 | .id = LM3631_LDO_OREF, |
| 141 | .ops = &lm363x_regulator_voltage_table_ops, |
| 142 | .n_voltages = LM3631_LDO_VSEL_MAX + 1, |
| 143 | .min_uV = LM3631_VLDO_MIN, |
| 144 | .uV_step = LM363X_STEP_50mV, |
| 145 | .type = REGULATOR_VOLTAGE, |
| 146 | .owner = THIS_MODULE, |
| 147 | .vsel_reg = LM3631_REG_VOUT_OREF, |
| 148 | .vsel_mask = LM3631_VOUT_MASK, |
| 149 | .enable_reg = LM3631_REG_LDO_CTRL1, |
| 150 | .enable_mask = LM3631_EN_OREF_MASK, |
| 151 | }, |
| 152 | { |
| 153 | .name = "ldo_vpos", |
| 154 | .of_match = "vpos", |
| 155 | .id = LM3631_LDO_POS, |
| 156 | .ops = &lm363x_regulator_voltage_table_ops, |
| 157 | .n_voltages = LM3631_LDO_VSEL_MAX + 1, |
| 158 | .min_uV = LM3631_VLDO_MIN, |
| 159 | .uV_step = LM363X_STEP_50mV, |
| 160 | .type = REGULATOR_VOLTAGE, |
| 161 | .owner = THIS_MODULE, |
| 162 | .vsel_reg = LM3631_REG_VOUT_POS, |
| 163 | .vsel_mask = LM3631_VOUT_MASK, |
| 164 | .enable_reg = LM3631_REG_LDO_CTRL1, |
| 165 | .enable_mask = LM3631_EN_VPOS_MASK, |
| 166 | }, |
| 167 | { |
| 168 | .name = "ldo_vneg", |
| 169 | .of_match = "vneg", |
| 170 | .id = LM3631_LDO_NEG, |
| 171 | .ops = &lm363x_regulator_voltage_table_ops, |
| 172 | .n_voltages = LM3631_LDO_VSEL_MAX + 1, |
| 173 | .min_uV = LM3631_VLDO_MIN, |
| 174 | .uV_step = LM363X_STEP_50mV, |
| 175 | .type = REGULATOR_VOLTAGE, |
| 176 | .owner = THIS_MODULE, |
| 177 | .vsel_reg = LM3631_REG_VOUT_NEG, |
| 178 | .vsel_mask = LM3631_VOUT_MASK, |
| 179 | .enable_reg = LM3631_REG_LDO_CTRL1, |
| 180 | .enable_mask = LM3631_EN_VNEG_MASK, |
| 181 | }, |
| 182 | /* LM3632 */ |
| 183 | { |
| 184 | .name = "vboost", |
| 185 | .of_match = "vboost", |
| 186 | .id = LM3632_BOOST, |
| 187 | .ops = &lm363x_boost_voltage_table_ops, |
| 188 | .n_voltages = LM3632_BOOST_VSEL_MAX + 1, |
| 189 | .min_uV = LM3632_VBOOST_MIN, |
| 190 | .uV_step = LM363X_STEP_50mV, |
| 191 | .type = REGULATOR_VOLTAGE, |
| 192 | .owner = THIS_MODULE, |
| 193 | .vsel_reg = LM3632_REG_VOUT_BOOST, |
| 194 | .vsel_mask = LM3632_VOUT_MASK, |
| 195 | }, |
| 196 | { |
| 197 | .name = "ldo_vpos", |
| 198 | .of_match = "vpos", |
| 199 | .id = LM3632_LDO_POS, |
| 200 | .ops = &lm363x_regulator_voltage_table_ops, |
| 201 | .n_voltages = LM3632_LDO_VSEL_MAX + 1, |
| 202 | .min_uV = LM3632_VLDO_MIN, |
| 203 | .uV_step = LM363X_STEP_50mV, |
| 204 | .type = REGULATOR_VOLTAGE, |
| 205 | .owner = THIS_MODULE, |
| 206 | .vsel_reg = LM3632_REG_VOUT_POS, |
| 207 | .vsel_mask = LM3632_VOUT_MASK, |
| 208 | .enable_reg = LM3632_REG_BIAS_CONFIG, |
| 209 | .enable_mask = LM3632_EN_VPOS_MASK, |
| 210 | }, |
| 211 | { |
| 212 | .name = "ldo_vneg", |
| 213 | .of_match = "vneg", |
| 214 | .id = LM3632_LDO_NEG, |
| 215 | .ops = &lm363x_regulator_voltage_table_ops, |
| 216 | .n_voltages = LM3632_LDO_VSEL_MAX + 1, |
| 217 | .min_uV = LM3632_VLDO_MIN, |
| 218 | .uV_step = LM363X_STEP_50mV, |
| 219 | .type = REGULATOR_VOLTAGE, |
| 220 | .owner = THIS_MODULE, |
| 221 | .vsel_reg = LM3632_REG_VOUT_NEG, |
| 222 | .vsel_mask = LM3632_VOUT_MASK, |
| 223 | .enable_reg = LM3632_REG_BIAS_CONFIG, |
| 224 | .enable_mask = LM3632_EN_VNEG_MASK, |
| 225 | }, |
| 226 | }; |
| 227 | |
| 228 | static int lm363x_regulator_of_get_enable_gpio(struct device_node *np, int id) |
| 229 | { |
| 230 | /* |
| 231 | * Check LCM_EN1/2_GPIO is configured. |
| 232 | * Those pins are used for enabling VPOS/VNEG LDOs. |
| 233 | */ |
| 234 | switch (id) { |
| 235 | case LM3632_LDO_POS: |
| 236 | return of_get_named_gpio(np, "ti,lcm-en1-gpio", 0); |
| 237 | case LM3632_LDO_NEG: |
| 238 | return of_get_named_gpio(np, "ti,lcm-en2-gpio", 0); |
| 239 | default: |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static int lm363x_regulator_probe(struct platform_device *pdev) |
| 245 | { |
| 246 | struct ti_lmu *lmu = dev_get_drvdata(pdev->dev.parent); |
| 247 | struct lm363x_regulator *lm363x_regulator; |
| 248 | struct regmap *regmap = lmu->regmap; |
| 249 | struct regulator_config cfg = { }; |
| 250 | struct regulator_dev *rdev; |
| 251 | struct device *dev = &pdev->dev; |
| 252 | int id = pdev->id; |
| 253 | int ret, ena_gpio; |
| 254 | |
| 255 | lm363x_regulator = devm_kzalloc(dev, sizeof(*lm363x_regulator), |
| 256 | GFP_KERNEL); |
| 257 | if (!lm363x_regulator) |
| 258 | return -ENOMEM; |
| 259 | |
| 260 | lm363x_regulator->regmap = regmap; |
| 261 | |
| 262 | cfg.dev = dev; |
| 263 | cfg.driver_data = lm363x_regulator; |
| 264 | cfg.regmap = regmap; |
| 265 | |
| 266 | /* |
| 267 | * LM3632 LDOs can be controlled by external pin. |
| 268 | * Register update is required if the pin is used. |
| 269 | */ |
| 270 | ena_gpio = lm363x_regulator_of_get_enable_gpio(dev->of_node, id); |
| 271 | if (gpio_is_valid(ena_gpio)) { |
| 272 | cfg.ena_gpio = ena_gpio; |
| 273 | cfg.ena_gpio_flags = GPIOF_OUT_INIT_LOW; |
| 274 | |
| 275 | ret = regmap_update_bits(regmap, LM3632_REG_BIAS_CONFIG, |
| 276 | LM3632_EXT_EN_MASK, |
| 277 | LM3632_EXT_EN_MASK); |
| 278 | if (ret) { |
| 279 | dev_err(dev, "External pin err: %d\n", ret); |
| 280 | return ret; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | rdev = devm_regulator_register(dev, &lm363x_regulator_desc[id], &cfg); |
| 285 | if (IS_ERR(rdev)) { |
| 286 | ret = PTR_ERR(rdev); |
| 287 | dev_err(dev, "[%d] regulator register err: %d\n", id, ret); |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | lm363x_regulator->regulator = rdev; |
| 292 | platform_set_drvdata(pdev, lm363x_regulator); |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static struct platform_driver lm363x_regulator_driver = { |
| 298 | .probe = lm363x_regulator_probe, |
| 299 | .driver = { |
| 300 | .name = "lm363x-regulator", |
| 301 | }, |
| 302 | }; |
| 303 | |
| 304 | module_platform_driver(lm363x_regulator_driver); |
| 305 | |
| 306 | MODULE_DESCRIPTION("TI LM363X Regulator Driver"); |
| 307 | MODULE_AUTHOR("Milo Kim"); |
| 308 | MODULE_LICENSE("GPL v2"); |
| 309 | MODULE_ALIAS("platform:lm363x-regulator"); |