.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Voltage regulation driver for active-semi ACT8945A PMIC |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2015 Atmel Corporation |
---|
5 | 6 | * |
---|
6 | 7 | * Author: Wenyou Yang <wenyou.yang@atmel.com> |
---|
7 | | - * |
---|
8 | | - * This program is free software; you can redistribute it and/or |
---|
9 | | - * modify it under the terms of the GNU General Public License as |
---|
10 | | - * published by the Free Software Foundation; either version 2 of the |
---|
11 | | - * License, or (at your option) any later version. |
---|
12 | | - * |
---|
13 | 8 | */ |
---|
14 | 9 | |
---|
15 | 10 | #include <linux/module.h> |
---|
16 | 11 | #include <linux/of_device.h> |
---|
17 | 12 | #include <linux/platform_device.h> |
---|
| 13 | +#include <linux/regmap.h> |
---|
18 | 14 | #include <linux/regulator/driver.h> |
---|
19 | 15 | #include <linux/regulator/machine.h> |
---|
| 16 | +#include <dt-bindings/regulator/active-semi,8945a-regulator.h> |
---|
20 | 17 | |
---|
21 | 18 | /** |
---|
22 | 19 | * ACT8945A Global Register Map. |
---|
23 | 20 | */ |
---|
24 | 21 | #define ACT8945A_SYS_MODE 0x00 |
---|
25 | 22 | #define ACT8945A_SYS_CTRL 0x01 |
---|
| 23 | +#define ACT8945A_SYS_UNLK_REGS 0x0b |
---|
26 | 24 | #define ACT8945A_DCDC1_VSET1 0x20 |
---|
27 | 25 | #define ACT8945A_DCDC1_VSET2 0x21 |
---|
28 | 26 | #define ACT8945A_DCDC1_CTRL 0x22 |
---|
| 27 | +#define ACT8945A_DCDC1_SUS 0x24 |
---|
29 | 28 | #define ACT8945A_DCDC2_VSET1 0x30 |
---|
30 | 29 | #define ACT8945A_DCDC2_VSET2 0x31 |
---|
31 | 30 | #define ACT8945A_DCDC2_CTRL 0x32 |
---|
| 31 | +#define ACT8945A_DCDC2_SUS 0x34 |
---|
32 | 32 | #define ACT8945A_DCDC3_VSET1 0x40 |
---|
33 | 33 | #define ACT8945A_DCDC3_VSET2 0x41 |
---|
34 | 34 | #define ACT8945A_DCDC3_CTRL 0x42 |
---|
| 35 | +#define ACT8945A_DCDC3_SUS 0x44 |
---|
35 | 36 | #define ACT8945A_LDO1_VSET 0x50 |
---|
36 | 37 | #define ACT8945A_LDO1_CTRL 0x51 |
---|
| 38 | +#define ACT8945A_LDO1_SUS 0x52 |
---|
37 | 39 | #define ACT8945A_LDO2_VSET 0x54 |
---|
38 | 40 | #define ACT8945A_LDO2_CTRL 0x55 |
---|
| 41 | +#define ACT8945A_LDO2_SUS 0x56 |
---|
39 | 42 | #define ACT8945A_LDO3_VSET 0x60 |
---|
40 | 43 | #define ACT8945A_LDO3_CTRL 0x61 |
---|
| 44 | +#define ACT8945A_LDO3_SUS 0x62 |
---|
41 | 45 | #define ACT8945A_LDO4_VSET 0x64 |
---|
42 | 46 | #define ACT8945A_LDO4_CTRL 0x65 |
---|
| 47 | +#define ACT8945A_LDO4_SUS 0x66 |
---|
43 | 48 | |
---|
44 | 49 | /** |
---|
45 | 50 | * Field Definitions. |
---|
.. | .. |
---|
60 | 65 | ACT8945A_ID_LDO2, |
---|
61 | 66 | ACT8945A_ID_LDO3, |
---|
62 | 67 | ACT8945A_ID_LDO4, |
---|
63 | | - ACT8945A_REG_NUM, |
---|
| 68 | + ACT8945A_ID_MAX, |
---|
64 | 69 | }; |
---|
65 | 70 | |
---|
66 | | -static const struct regulator_linear_range act8945a_voltage_ranges[] = { |
---|
| 71 | +struct act8945a_pmic { |
---|
| 72 | + struct regmap *regmap; |
---|
| 73 | + u32 op_mode[ACT8945A_ID_MAX]; |
---|
| 74 | +}; |
---|
| 75 | + |
---|
| 76 | +static const struct linear_range act8945a_voltage_ranges[] = { |
---|
67 | 77 | REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000), |
---|
68 | 78 | REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000), |
---|
69 | 79 | REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000), |
---|
70 | 80 | }; |
---|
| 81 | + |
---|
| 82 | +static int act8945a_set_suspend_state(struct regulator_dev *rdev, bool enable) |
---|
| 83 | +{ |
---|
| 84 | + struct regmap *regmap = rdev->regmap; |
---|
| 85 | + int id = rdev_get_id(rdev); |
---|
| 86 | + int reg, val; |
---|
| 87 | + |
---|
| 88 | + switch (id) { |
---|
| 89 | + case ACT8945A_ID_DCDC1: |
---|
| 90 | + reg = ACT8945A_DCDC1_SUS; |
---|
| 91 | + val = 0xa8; |
---|
| 92 | + break; |
---|
| 93 | + case ACT8945A_ID_DCDC2: |
---|
| 94 | + reg = ACT8945A_DCDC2_SUS; |
---|
| 95 | + val = 0xa8; |
---|
| 96 | + break; |
---|
| 97 | + case ACT8945A_ID_DCDC3: |
---|
| 98 | + reg = ACT8945A_DCDC3_SUS; |
---|
| 99 | + val = 0xa8; |
---|
| 100 | + break; |
---|
| 101 | + case ACT8945A_ID_LDO1: |
---|
| 102 | + reg = ACT8945A_LDO1_SUS; |
---|
| 103 | + val = 0xe8; |
---|
| 104 | + break; |
---|
| 105 | + case ACT8945A_ID_LDO2: |
---|
| 106 | + reg = ACT8945A_LDO2_SUS; |
---|
| 107 | + val = 0xe8; |
---|
| 108 | + break; |
---|
| 109 | + case ACT8945A_ID_LDO3: |
---|
| 110 | + reg = ACT8945A_LDO3_SUS; |
---|
| 111 | + val = 0xe8; |
---|
| 112 | + break; |
---|
| 113 | + case ACT8945A_ID_LDO4: |
---|
| 114 | + reg = ACT8945A_LDO4_SUS; |
---|
| 115 | + val = 0xe8; |
---|
| 116 | + break; |
---|
| 117 | + default: |
---|
| 118 | + return -EINVAL; |
---|
| 119 | + } |
---|
| 120 | + |
---|
| 121 | + if (enable) |
---|
| 122 | + val |= BIT(4); |
---|
| 123 | + |
---|
| 124 | + /* |
---|
| 125 | + * Ask the PMIC to enable/disable this output when entering hibernate |
---|
| 126 | + * mode. |
---|
| 127 | + */ |
---|
| 128 | + return regmap_write(regmap, reg, val); |
---|
| 129 | +} |
---|
| 130 | + |
---|
| 131 | +static int act8945a_set_suspend_enable(struct regulator_dev *rdev) |
---|
| 132 | +{ |
---|
| 133 | + return act8945a_set_suspend_state(rdev, true); |
---|
| 134 | +} |
---|
| 135 | + |
---|
| 136 | +static int act8945a_set_suspend_disable(struct regulator_dev *rdev) |
---|
| 137 | +{ |
---|
| 138 | + return act8945a_set_suspend_state(rdev, false); |
---|
| 139 | +} |
---|
| 140 | + |
---|
| 141 | +static unsigned int act8945a_of_map_mode(unsigned int mode) |
---|
| 142 | +{ |
---|
| 143 | + switch (mode) { |
---|
| 144 | + case ACT8945A_REGULATOR_MODE_FIXED: |
---|
| 145 | + case ACT8945A_REGULATOR_MODE_NORMAL: |
---|
| 146 | + return REGULATOR_MODE_NORMAL; |
---|
| 147 | + case ACT8945A_REGULATOR_MODE_LOWPOWER: |
---|
| 148 | + return REGULATOR_MODE_STANDBY; |
---|
| 149 | + default: |
---|
| 150 | + return REGULATOR_MODE_INVALID; |
---|
| 151 | + } |
---|
| 152 | +} |
---|
| 153 | + |
---|
| 154 | +static int act8945a_set_mode(struct regulator_dev *rdev, unsigned int mode) |
---|
| 155 | +{ |
---|
| 156 | + struct act8945a_pmic *act8945a = rdev_get_drvdata(rdev); |
---|
| 157 | + struct regmap *regmap = rdev->regmap; |
---|
| 158 | + int id = rdev_get_id(rdev); |
---|
| 159 | + int reg, ret, val = 0; |
---|
| 160 | + |
---|
| 161 | + switch (id) { |
---|
| 162 | + case ACT8945A_ID_DCDC1: |
---|
| 163 | + reg = ACT8945A_DCDC1_CTRL; |
---|
| 164 | + break; |
---|
| 165 | + case ACT8945A_ID_DCDC2: |
---|
| 166 | + reg = ACT8945A_DCDC2_CTRL; |
---|
| 167 | + break; |
---|
| 168 | + case ACT8945A_ID_DCDC3: |
---|
| 169 | + reg = ACT8945A_DCDC3_CTRL; |
---|
| 170 | + break; |
---|
| 171 | + case ACT8945A_ID_LDO1: |
---|
| 172 | + reg = ACT8945A_LDO1_CTRL; |
---|
| 173 | + break; |
---|
| 174 | + case ACT8945A_ID_LDO2: |
---|
| 175 | + reg = ACT8945A_LDO2_CTRL; |
---|
| 176 | + break; |
---|
| 177 | + case ACT8945A_ID_LDO3: |
---|
| 178 | + reg = ACT8945A_LDO3_CTRL; |
---|
| 179 | + break; |
---|
| 180 | + case ACT8945A_ID_LDO4: |
---|
| 181 | + reg = ACT8945A_LDO4_CTRL; |
---|
| 182 | + break; |
---|
| 183 | + default: |
---|
| 184 | + return -EINVAL; |
---|
| 185 | + } |
---|
| 186 | + |
---|
| 187 | + switch (mode) { |
---|
| 188 | + case REGULATOR_MODE_STANDBY: |
---|
| 189 | + if (id > ACT8945A_ID_DCDC3) |
---|
| 190 | + val = BIT(5); |
---|
| 191 | + break; |
---|
| 192 | + case REGULATOR_MODE_NORMAL: |
---|
| 193 | + if (id <= ACT8945A_ID_DCDC3) |
---|
| 194 | + val = BIT(5); |
---|
| 195 | + break; |
---|
| 196 | + default: |
---|
| 197 | + return -EINVAL; |
---|
| 198 | + } |
---|
| 199 | + |
---|
| 200 | + ret = regmap_update_bits(regmap, reg, BIT(5), val); |
---|
| 201 | + if (ret) |
---|
| 202 | + return ret; |
---|
| 203 | + |
---|
| 204 | + act8945a->op_mode[id] = mode; |
---|
| 205 | + |
---|
| 206 | + return 0; |
---|
| 207 | +} |
---|
| 208 | + |
---|
| 209 | +static unsigned int act8945a_get_mode(struct regulator_dev *rdev) |
---|
| 210 | +{ |
---|
| 211 | + struct act8945a_pmic *act8945a = rdev_get_drvdata(rdev); |
---|
| 212 | + int id = rdev_get_id(rdev); |
---|
| 213 | + |
---|
| 214 | + if (id < ACT8945A_ID_DCDC1 || id >= ACT8945A_ID_MAX) |
---|
| 215 | + return -EINVAL; |
---|
| 216 | + |
---|
| 217 | + return act8945a->op_mode[id]; |
---|
| 218 | +} |
---|
71 | 219 | |
---|
72 | 220 | static const struct regulator_ops act8945a_ops = { |
---|
73 | 221 | .list_voltage = regulator_list_voltage_linear_range, |
---|
.. | .. |
---|
76 | 224 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
---|
77 | 225 | .enable = regulator_enable_regmap, |
---|
78 | 226 | .disable = regulator_disable_regmap, |
---|
| 227 | + .set_mode = act8945a_set_mode, |
---|
| 228 | + .get_mode = act8945a_get_mode, |
---|
79 | 229 | .is_enabled = regulator_is_enabled_regmap, |
---|
| 230 | + .set_suspend_enable = act8945a_set_suspend_enable, |
---|
| 231 | + .set_suspend_disable = act8945a_set_suspend_disable, |
---|
80 | 232 | }; |
---|
81 | 233 | |
---|
82 | 234 | #define ACT89xx_REG(_name, _family, _id, _vsel_reg, _supply) \ |
---|
.. | .. |
---|
84 | 236 | .name = _name, \ |
---|
85 | 237 | .supply_name = _supply, \ |
---|
86 | 238 | .of_match = of_match_ptr("REG_"#_id), \ |
---|
| 239 | + .of_map_mode = act8945a_of_map_mode, \ |
---|
87 | 240 | .regulators_node = of_match_ptr("regulators"), \ |
---|
88 | 241 | .id = _family##_ID_##_id, \ |
---|
89 | 242 | .type = REGULATOR_VOLTAGE, \ |
---|
.. | .. |
---|
122 | 275 | { |
---|
123 | 276 | struct regulator_config config = { }; |
---|
124 | 277 | const struct regulator_desc *regulators; |
---|
| 278 | + struct act8945a_pmic *act8945a; |
---|
125 | 279 | struct regulator_dev *rdev; |
---|
126 | 280 | int i, num_regulators; |
---|
127 | 281 | bool voltage_select; |
---|
| 282 | + |
---|
| 283 | + act8945a = devm_kzalloc(&pdev->dev, sizeof(*act8945a), GFP_KERNEL); |
---|
| 284 | + if (!act8945a) |
---|
| 285 | + return -ENOMEM; |
---|
| 286 | + |
---|
| 287 | + act8945a->regmap = dev_get_regmap(pdev->dev.parent, NULL); |
---|
| 288 | + if (!act8945a->regmap) { |
---|
| 289 | + dev_err(&pdev->dev, |
---|
| 290 | + "could not retrieve regmap from parent device\n"); |
---|
| 291 | + return -EINVAL; |
---|
| 292 | + } |
---|
128 | 293 | |
---|
129 | 294 | voltage_select = of_property_read_bool(pdev->dev.parent->of_node, |
---|
130 | 295 | "active-semi,vsel-high"); |
---|
.. | .. |
---|
139 | 304 | |
---|
140 | 305 | config.dev = &pdev->dev; |
---|
141 | 306 | config.dev->of_node = pdev->dev.parent->of_node; |
---|
| 307 | + config.driver_data = act8945a; |
---|
142 | 308 | for (i = 0; i < num_regulators; i++) { |
---|
143 | | - rdev = devm_regulator_register(&pdev->dev, ®ulators[i], &config); |
---|
| 309 | + rdev = devm_regulator_register(&pdev->dev, ®ulators[i], |
---|
| 310 | + &config); |
---|
144 | 311 | if (IS_ERR(rdev)) { |
---|
145 | 312 | dev_err(&pdev->dev, |
---|
146 | 313 | "failed to register %s regulator\n", |
---|
.. | .. |
---|
149 | 316 | } |
---|
150 | 317 | } |
---|
151 | 318 | |
---|
152 | | - return 0; |
---|
| 319 | + platform_set_drvdata(pdev, act8945a); |
---|
| 320 | + |
---|
| 321 | + /* Unlock expert registers. */ |
---|
| 322 | + return regmap_write(act8945a->regmap, ACT8945A_SYS_UNLK_REGS, 0xef); |
---|
| 323 | +} |
---|
| 324 | + |
---|
| 325 | +static int __maybe_unused act8945a_suspend(struct device *pdev) |
---|
| 326 | +{ |
---|
| 327 | + struct act8945a_pmic *act8945a = dev_get_drvdata(pdev); |
---|
| 328 | + |
---|
| 329 | + /* |
---|
| 330 | + * Ask the PMIC to enter the suspend mode on the next PWRHLD |
---|
| 331 | + * transition. |
---|
| 332 | + */ |
---|
| 333 | + return regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x42); |
---|
| 334 | +} |
---|
| 335 | + |
---|
| 336 | +static SIMPLE_DEV_PM_OPS(act8945a_pm, act8945a_suspend, NULL); |
---|
| 337 | + |
---|
| 338 | +static void act8945a_pmic_shutdown(struct platform_device *pdev) |
---|
| 339 | +{ |
---|
| 340 | + struct act8945a_pmic *act8945a = platform_get_drvdata(pdev); |
---|
| 341 | + |
---|
| 342 | + /* |
---|
| 343 | + * Ask the PMIC to shutdown everything on the next PWRHLD transition. |
---|
| 344 | + */ |
---|
| 345 | + regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x0); |
---|
153 | 346 | } |
---|
154 | 347 | |
---|
155 | 348 | static struct platform_driver act8945a_pmic_driver = { |
---|
156 | 349 | .driver = { |
---|
157 | 350 | .name = "act8945a-regulator", |
---|
| 351 | + .pm = &act8945a_pm, |
---|
158 | 352 | }, |
---|
159 | 353 | .probe = act8945a_pmic_probe, |
---|
| 354 | + .shutdown = act8945a_pmic_shutdown, |
---|
160 | 355 | }; |
---|
161 | 356 | module_platform_driver(act8945a_pmic_driver); |
---|
162 | 357 | |
---|