Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 2 | /* |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 3 | * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs |
| 4 | * |
| 5 | * http://www.active-semi.com/products/power-management-units/act88xx/ |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) 2013 Atmel Corporation |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/i2c.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/regulator/driver.h> |
| 16 | #include <linux/regulator/act8865.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/of_device.h> |
Maarten ter Huurne | 2d09a79 | 2019-07-22 21:14:18 -0400 | [diff] [blame^] | 19 | #include <linux/power_supply.h> |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 20 | #include <linux/regulator/of_regulator.h> |
| 21 | #include <linux/regmap.h> |
| 22 | |
| 23 | /* |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 24 | * ACT8600 Global Register Map. |
| 25 | */ |
| 26 | #define ACT8600_SYS_MODE 0x00 |
| 27 | #define ACT8600_SYS_CTRL 0x01 |
| 28 | #define ACT8600_DCDC1_VSET 0x10 |
| 29 | #define ACT8600_DCDC1_CTRL 0x12 |
| 30 | #define ACT8600_DCDC2_VSET 0x20 |
| 31 | #define ACT8600_DCDC2_CTRL 0x22 |
| 32 | #define ACT8600_DCDC3_VSET 0x30 |
| 33 | #define ACT8600_DCDC3_CTRL 0x32 |
| 34 | #define ACT8600_SUDCDC4_VSET 0x40 |
| 35 | #define ACT8600_SUDCDC4_CTRL 0x41 |
| 36 | #define ACT8600_LDO5_VSET 0x50 |
| 37 | #define ACT8600_LDO5_CTRL 0x51 |
| 38 | #define ACT8600_LDO6_VSET 0x60 |
| 39 | #define ACT8600_LDO6_CTRL 0x61 |
| 40 | #define ACT8600_LDO7_VSET 0x70 |
| 41 | #define ACT8600_LDO7_CTRL 0x71 |
| 42 | #define ACT8600_LDO8_VSET 0x80 |
| 43 | #define ACT8600_LDO8_CTRL 0x81 |
| 44 | #define ACT8600_LDO910_CTRL 0x91 |
| 45 | #define ACT8600_APCH0 0xA1 |
| 46 | #define ACT8600_APCH1 0xA8 |
| 47 | #define ACT8600_APCH2 0xA9 |
| 48 | #define ACT8600_APCH_STAT 0xAA |
| 49 | #define ACT8600_OTG0 0xB0 |
| 50 | #define ACT8600_OTG1 0xB2 |
| 51 | |
| 52 | /* |
Beniamino Galvani | ac0c0ea | 2014-07-05 15:20:55 +0200 | [diff] [blame] | 53 | * ACT8846 Global Register Map. |
| 54 | */ |
| 55 | #define ACT8846_SYS0 0x00 |
| 56 | #define ACT8846_SYS1 0x01 |
| 57 | #define ACT8846_REG1_VSET 0x10 |
| 58 | #define ACT8846_REG1_CTRL 0x12 |
| 59 | #define ACT8846_REG2_VSET0 0x20 |
| 60 | #define ACT8846_REG2_VSET1 0x21 |
| 61 | #define ACT8846_REG2_CTRL 0x22 |
| 62 | #define ACT8846_REG3_VSET0 0x30 |
| 63 | #define ACT8846_REG3_VSET1 0x31 |
| 64 | #define ACT8846_REG3_CTRL 0x32 |
| 65 | #define ACT8846_REG4_VSET0 0x40 |
| 66 | #define ACT8846_REG4_VSET1 0x41 |
| 67 | #define ACT8846_REG4_CTRL 0x42 |
| 68 | #define ACT8846_REG5_VSET 0x50 |
| 69 | #define ACT8846_REG5_CTRL 0x51 |
| 70 | #define ACT8846_REG6_VSET 0x58 |
| 71 | #define ACT8846_REG6_CTRL 0x59 |
| 72 | #define ACT8846_REG7_VSET 0x60 |
| 73 | #define ACT8846_REG7_CTRL 0x61 |
| 74 | #define ACT8846_REG8_VSET 0x68 |
| 75 | #define ACT8846_REG8_CTRL 0x69 |
| 76 | #define ACT8846_REG9_VSET 0x70 |
| 77 | #define ACT8846_REG9_CTRL 0x71 |
| 78 | #define ACT8846_REG10_VSET 0x80 |
| 79 | #define ACT8846_REG10_CTRL 0x81 |
| 80 | #define ACT8846_REG11_VSET 0x90 |
| 81 | #define ACT8846_REG11_CTRL 0x91 |
| 82 | #define ACT8846_REG12_VSET 0xa0 |
| 83 | #define ACT8846_REG12_CTRL 0xa1 |
| 84 | #define ACT8846_REG13_CTRL 0xb1 |
Romain Perier | 2b17fa2 | 2014-10-14 06:31:10 +0000 | [diff] [blame] | 85 | #define ACT8846_GLB_OFF_CTRL 0xc3 |
| 86 | #define ACT8846_OFF_SYSMASK 0x18 |
Beniamino Galvani | ac0c0ea | 2014-07-05 15:20:55 +0200 | [diff] [blame] | 87 | |
| 88 | /* |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 89 | * ACT8865 Global Register Map. |
| 90 | */ |
| 91 | #define ACT8865_SYS_MODE 0x00 |
| 92 | #define ACT8865_SYS_CTRL 0x01 |
| 93 | #define ACT8865_DCDC1_VSET1 0x20 |
| 94 | #define ACT8865_DCDC1_VSET2 0x21 |
| 95 | #define ACT8865_DCDC1_CTRL 0x22 |
| 96 | #define ACT8865_DCDC2_VSET1 0x30 |
| 97 | #define ACT8865_DCDC2_VSET2 0x31 |
| 98 | #define ACT8865_DCDC2_CTRL 0x32 |
| 99 | #define ACT8865_DCDC3_VSET1 0x40 |
| 100 | #define ACT8865_DCDC3_VSET2 0x41 |
| 101 | #define ACT8865_DCDC3_CTRL 0x42 |
| 102 | #define ACT8865_LDO1_VSET 0x50 |
| 103 | #define ACT8865_LDO1_CTRL 0x51 |
| 104 | #define ACT8865_LDO2_VSET 0x54 |
| 105 | #define ACT8865_LDO2_CTRL 0x55 |
| 106 | #define ACT8865_LDO3_VSET 0x60 |
| 107 | #define ACT8865_LDO3_CTRL 0x61 |
| 108 | #define ACT8865_LDO4_VSET 0x64 |
| 109 | #define ACT8865_LDO4_CTRL 0x65 |
Romain Perier | 2b17fa2 | 2014-10-14 06:31:10 +0000 | [diff] [blame] | 110 | #define ACT8865_MSTROFF 0x20 |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * Field Definitions. |
| 114 | */ |
| 115 | #define ACT8865_ENA 0x80 /* ON - [7] */ |
| 116 | #define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */ |
| 117 | |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 118 | |
| 119 | #define ACT8600_LDO10_ENA 0x40 /* ON - [6] */ |
| 120 | #define ACT8600_SUDCDC_VSEL_MASK 0xFF /* SUDCDC VSET - [7:0] */ |
| 121 | |
Maarten ter Huurne | 2d09a79 | 2019-07-22 21:14:18 -0400 | [diff] [blame^] | 122 | #define ACT8600_APCH_CHG_ACIN BIT(7) |
| 123 | #define ACT8600_APCH_CHG_USB BIT(6) |
| 124 | #define ACT8600_APCH_CSTATE0 BIT(5) |
| 125 | #define ACT8600_APCH_CSTATE1 BIT(4) |
| 126 | |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 127 | /* |
| 128 | * ACT8865 voltage number |
| 129 | */ |
| 130 | #define ACT8865_VOLTAGE_NUM 64 |
Axel Lin | f01a7be | 2019-01-10 17:26:16 +0800 | [diff] [blame] | 131 | #define ACT8600_SUDCDC_VOLTAGE_NUM 256 |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 132 | |
| 133 | struct act8865 { |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 134 | struct regmap *regmap; |
Romain Perier | 2b17fa2 | 2014-10-14 06:31:10 +0000 | [diff] [blame] | 135 | int off_reg; |
| 136 | int off_mask; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 137 | }; |
| 138 | |
Maarten ter Huurne | c5c9c2d | 2016-03-17 15:05:08 +0100 | [diff] [blame] | 139 | static const struct regmap_range act8600_reg_ranges[] = { |
| 140 | regmap_reg_range(0x00, 0x01), |
| 141 | regmap_reg_range(0x10, 0x10), |
| 142 | regmap_reg_range(0x12, 0x12), |
| 143 | regmap_reg_range(0x20, 0x20), |
| 144 | regmap_reg_range(0x22, 0x22), |
| 145 | regmap_reg_range(0x30, 0x30), |
| 146 | regmap_reg_range(0x32, 0x32), |
| 147 | regmap_reg_range(0x40, 0x41), |
| 148 | regmap_reg_range(0x50, 0x51), |
| 149 | regmap_reg_range(0x60, 0x61), |
| 150 | regmap_reg_range(0x70, 0x71), |
| 151 | regmap_reg_range(0x80, 0x81), |
| 152 | regmap_reg_range(0x91, 0x91), |
| 153 | regmap_reg_range(0xA1, 0xA1), |
| 154 | regmap_reg_range(0xA8, 0xAA), |
| 155 | regmap_reg_range(0xB0, 0xB0), |
| 156 | regmap_reg_range(0xB2, 0xB2), |
| 157 | regmap_reg_range(0xC1, 0xC1), |
| 158 | }; |
| 159 | |
| 160 | static const struct regmap_range act8600_reg_ro_ranges[] = { |
| 161 | regmap_reg_range(0xAA, 0xAA), |
| 162 | regmap_reg_range(0xC1, 0xC1), |
| 163 | }; |
| 164 | |
| 165 | static const struct regmap_range act8600_reg_volatile_ranges[] = { |
| 166 | regmap_reg_range(0x00, 0x01), |
| 167 | regmap_reg_range(0x12, 0x12), |
| 168 | regmap_reg_range(0x22, 0x22), |
| 169 | regmap_reg_range(0x32, 0x32), |
| 170 | regmap_reg_range(0x41, 0x41), |
| 171 | regmap_reg_range(0x51, 0x51), |
| 172 | regmap_reg_range(0x61, 0x61), |
| 173 | regmap_reg_range(0x71, 0x71), |
| 174 | regmap_reg_range(0x81, 0x81), |
| 175 | regmap_reg_range(0xA8, 0xA8), |
| 176 | regmap_reg_range(0xAA, 0xAA), |
| 177 | regmap_reg_range(0xB0, 0xB0), |
| 178 | regmap_reg_range(0xC1, 0xC1), |
| 179 | }; |
| 180 | |
| 181 | static const struct regmap_access_table act8600_write_ranges_table = { |
| 182 | .yes_ranges = act8600_reg_ranges, |
| 183 | .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges), |
| 184 | .no_ranges = act8600_reg_ro_ranges, |
| 185 | .n_no_ranges = ARRAY_SIZE(act8600_reg_ro_ranges), |
| 186 | }; |
| 187 | |
| 188 | static const struct regmap_access_table act8600_read_ranges_table = { |
| 189 | .yes_ranges = act8600_reg_ranges, |
| 190 | .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges), |
| 191 | }; |
| 192 | |
| 193 | static const struct regmap_access_table act8600_volatile_ranges_table = { |
| 194 | .yes_ranges = act8600_reg_volatile_ranges, |
| 195 | .n_yes_ranges = ARRAY_SIZE(act8600_reg_volatile_ranges), |
| 196 | }; |
| 197 | |
| 198 | static const struct regmap_config act8600_regmap_config = { |
| 199 | .reg_bits = 8, |
| 200 | .val_bits = 8, |
| 201 | .max_register = 0xFF, |
| 202 | .wr_table = &act8600_write_ranges_table, |
| 203 | .rd_table = &act8600_read_ranges_table, |
| 204 | .volatile_table = &act8600_volatile_ranges_table, |
| 205 | }; |
| 206 | |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 207 | static const struct regmap_config act8865_regmap_config = { |
| 208 | .reg_bits = 8, |
| 209 | .val_bits = 8, |
| 210 | }; |
| 211 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 212 | static const struct regulator_linear_range act8865_voltage_ranges[] = { |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 213 | REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000), |
| 214 | REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000), |
| 215 | REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000), |
| 216 | }; |
| 217 | |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 218 | static const struct regulator_linear_range act8600_sudcdc_voltage_ranges[] = { |
| 219 | REGULATOR_LINEAR_RANGE(3000000, 0, 63, 0), |
| 220 | REGULATOR_LINEAR_RANGE(3000000, 64, 159, 100000), |
| 221 | REGULATOR_LINEAR_RANGE(12600000, 160, 191, 200000), |
Axel Lin | f01a7be | 2019-01-10 17:26:16 +0800 | [diff] [blame] | 222 | REGULATOR_LINEAR_RANGE(19000000, 192, 247, 400000), |
| 223 | REGULATOR_LINEAR_RANGE(41400000, 248, 255, 0), |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
Axel Lin | 7cae255 | 2019-03-22 14:38:20 +0800 | [diff] [blame] | 226 | static const struct regulator_ops act8865_ops = { |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 227 | .list_voltage = regulator_list_voltage_linear_range, |
| 228 | .map_voltage = regulator_map_voltage_linear_range, |
| 229 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 230 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 231 | .enable = regulator_enable_regmap, |
| 232 | .disable = regulator_disable_regmap, |
| 233 | .is_enabled = regulator_is_enabled_regmap, |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 234 | }; |
| 235 | |
Axel Lin | 7cae255 | 2019-03-22 14:38:20 +0800 | [diff] [blame] | 236 | static const struct regulator_ops act8865_ldo_ops = { |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 237 | .enable = regulator_enable_regmap, |
| 238 | .disable = regulator_disable_regmap, |
| 239 | .is_enabled = regulator_is_enabled_regmap, |
| 240 | }; |
| 241 | |
Heiko Stuebner | c8b263c | 2015-03-07 16:33:53 +0100 | [diff] [blame] | 242 | #define ACT88xx_REG(_name, _family, _id, _vsel_reg, _supply) \ |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 243 | [_family##_ID_##_id] = { \ |
| 244 | .name = _name, \ |
Axel Lin | 253c9c7 | 2019-03-22 14:38:19 +0800 | [diff] [blame] | 245 | .of_match = of_match_ptr(_name), \ |
| 246 | .regulators_node = of_match_ptr("regulators"), \ |
Heiko Stuebner | c8b263c | 2015-03-07 16:33:53 +0100 | [diff] [blame] | 247 | .supply_name = _supply, \ |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 248 | .id = _family##_ID_##_id, \ |
| 249 | .type = REGULATOR_VOLTAGE, \ |
| 250 | .ops = &act8865_ops, \ |
| 251 | .n_voltages = ACT8865_VOLTAGE_NUM, \ |
| 252 | .linear_ranges = act8865_voltage_ranges, \ |
| 253 | .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \ |
| 254 | .vsel_reg = _family##_##_id##_##_vsel_reg, \ |
| 255 | .vsel_mask = ACT8865_VSEL_MASK, \ |
| 256 | .enable_reg = _family##_##_id##_CTRL, \ |
| 257 | .enable_mask = ACT8865_ENA, \ |
| 258 | .owner = THIS_MODULE, \ |
| 259 | } |
| 260 | |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 261 | static const struct regulator_desc act8600_regulators[] = { |
Heiko Stuebner | c8b263c | 2015-03-07 16:33:53 +0100 | [diff] [blame] | 262 | ACT88xx_REG("DCDC1", ACT8600, DCDC1, VSET, "vp1"), |
| 263 | ACT88xx_REG("DCDC2", ACT8600, DCDC2, VSET, "vp2"), |
| 264 | ACT88xx_REG("DCDC3", ACT8600, DCDC3, VSET, "vp3"), |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 265 | { |
| 266 | .name = "SUDCDC_REG4", |
Axel Lin | 253c9c7 | 2019-03-22 14:38:19 +0800 | [diff] [blame] | 267 | .of_match = of_match_ptr("SUDCDC_REG4"), |
| 268 | .regulators_node = of_match_ptr("regulators"), |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 269 | .id = ACT8600_ID_SUDCDC4, |
| 270 | .ops = &act8865_ops, |
| 271 | .type = REGULATOR_VOLTAGE, |
| 272 | .n_voltages = ACT8600_SUDCDC_VOLTAGE_NUM, |
| 273 | .linear_ranges = act8600_sudcdc_voltage_ranges, |
| 274 | .n_linear_ranges = ARRAY_SIZE(act8600_sudcdc_voltage_ranges), |
| 275 | .vsel_reg = ACT8600_SUDCDC4_VSET, |
| 276 | .vsel_mask = ACT8600_SUDCDC_VSEL_MASK, |
| 277 | .enable_reg = ACT8600_SUDCDC4_CTRL, |
| 278 | .enable_mask = ACT8865_ENA, |
| 279 | .owner = THIS_MODULE, |
| 280 | }, |
Heiko Stuebner | c8b263c | 2015-03-07 16:33:53 +0100 | [diff] [blame] | 281 | ACT88xx_REG("LDO5", ACT8600, LDO5, VSET, "inl"), |
| 282 | ACT88xx_REG("LDO6", ACT8600, LDO6, VSET, "inl"), |
| 283 | ACT88xx_REG("LDO7", ACT8600, LDO7, VSET, "inl"), |
| 284 | ACT88xx_REG("LDO8", ACT8600, LDO8, VSET, "inl"), |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 285 | { |
| 286 | .name = "LDO_REG9", |
Axel Lin | 253c9c7 | 2019-03-22 14:38:19 +0800 | [diff] [blame] | 287 | .of_match = of_match_ptr("LDO_REG9"), |
| 288 | .regulators_node = of_match_ptr("regulators"), |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 289 | .id = ACT8600_ID_LDO9, |
| 290 | .ops = &act8865_ldo_ops, |
| 291 | .type = REGULATOR_VOLTAGE, |
| 292 | .n_voltages = 1, |
Maarten ter Huurne | 79b5d02 | 2016-02-28 16:53:28 +0100 | [diff] [blame] | 293 | .fixed_uV = 3300000, |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 294 | .enable_reg = ACT8600_LDO910_CTRL, |
| 295 | .enable_mask = ACT8865_ENA, |
| 296 | .owner = THIS_MODULE, |
| 297 | }, |
| 298 | { |
| 299 | .name = "LDO_REG10", |
Axel Lin | 253c9c7 | 2019-03-22 14:38:19 +0800 | [diff] [blame] | 300 | .of_match = of_match_ptr("LDO_REG10"), |
| 301 | .regulators_node = of_match_ptr("regulators"), |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 302 | .id = ACT8600_ID_LDO10, |
| 303 | .ops = &act8865_ldo_ops, |
| 304 | .type = REGULATOR_VOLTAGE, |
| 305 | .n_voltages = 1, |
| 306 | .fixed_uV = 1200000, |
| 307 | .enable_reg = ACT8600_LDO910_CTRL, |
| 308 | .enable_mask = ACT8600_LDO10_ENA, |
| 309 | .owner = THIS_MODULE, |
| 310 | }, |
| 311 | }; |
| 312 | |
Beniamino Galvani | ac0c0ea | 2014-07-05 15:20:55 +0200 | [diff] [blame] | 313 | static const struct regulator_desc act8846_regulators[] = { |
Heiko Stuebner | c8b263c | 2015-03-07 16:33:53 +0100 | [diff] [blame] | 314 | ACT88xx_REG("REG1", ACT8846, REG1, VSET, "vp1"), |
| 315 | ACT88xx_REG("REG2", ACT8846, REG2, VSET0, "vp2"), |
| 316 | ACT88xx_REG("REG3", ACT8846, REG3, VSET0, "vp3"), |
| 317 | ACT88xx_REG("REG4", ACT8846, REG4, VSET0, "vp4"), |
| 318 | ACT88xx_REG("REG5", ACT8846, REG5, VSET, "inl1"), |
| 319 | ACT88xx_REG("REG6", ACT8846, REG6, VSET, "inl1"), |
| 320 | ACT88xx_REG("REG7", ACT8846, REG7, VSET, "inl1"), |
| 321 | ACT88xx_REG("REG8", ACT8846, REG8, VSET, "inl2"), |
| 322 | ACT88xx_REG("REG9", ACT8846, REG9, VSET, "inl2"), |
| 323 | ACT88xx_REG("REG10", ACT8846, REG10, VSET, "inl3"), |
| 324 | ACT88xx_REG("REG11", ACT8846, REG11, VSET, "inl3"), |
| 325 | ACT88xx_REG("REG12", ACT8846, REG12, VSET, "inl3"), |
Beniamino Galvani | ac0c0ea | 2014-07-05 15:20:55 +0200 | [diff] [blame] | 326 | }; |
| 327 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 328 | static const struct regulator_desc act8865_regulators[] = { |
Heiko Stuebner | c8b263c | 2015-03-07 16:33:53 +0100 | [diff] [blame] | 329 | ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1, "vp1"), |
| 330 | ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1, "vp2"), |
| 331 | ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1, "vp3"), |
| 332 | ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET, "inl45"), |
| 333 | ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET, "inl45"), |
| 334 | ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET, "inl67"), |
| 335 | ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET, "inl67"), |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 336 | }; |
| 337 | |
Wenyou Yang | c86dc03 | 2015-09-30 15:25:49 +0800 | [diff] [blame] | 338 | static const struct regulator_desc act8865_alt_regulators[] = { |
| 339 | ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET2, "vp1"), |
| 340 | ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET2, "vp2"), |
| 341 | ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET2, "vp3"), |
| 342 | ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET, "inl45"), |
| 343 | ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET, "inl45"), |
| 344 | ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET, "inl67"), |
| 345 | ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET, "inl67"), |
| 346 | }; |
| 347 | |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 348 | #ifdef CONFIG_OF |
| 349 | static const struct of_device_id act8865_dt_ids[] = { |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 350 | { .compatible = "active-semi,act8600", .data = (void *)ACT8600 }, |
Beniamino Galvani | ac0c0ea | 2014-07-05 15:20:55 +0200 | [diff] [blame] | 351 | { .compatible = "active-semi,act8846", .data = (void *)ACT8846 }, |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 352 | { .compatible = "active-semi,act8865", .data = (void *)ACT8865 }, |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 353 | { } |
| 354 | }; |
| 355 | MODULE_DEVICE_TABLE(of, act8865_dt_ids); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 356 | #endif |
| 357 | |
Maarten ter Huurne | 005e4685 | 2016-03-17 15:05:07 +0100 | [diff] [blame] | 358 | static struct act8865_regulator_data *act8865_get_regulator_data( |
| 359 | int id, struct act8865_platform_data *pdata) |
Beniamino Galvani | 48a1e1b | 2014-06-22 17:31:41 +0200 | [diff] [blame] | 360 | { |
| 361 | int i; |
| 362 | |
Beniamino Galvani | 48a1e1b | 2014-06-22 17:31:41 +0200 | [diff] [blame] | 363 | for (i = 0; i < pdata->num_regulators; i++) { |
| 364 | if (pdata->regulators[i].id == id) |
Maarten ter Huurne | 005e4685 | 2016-03-17 15:05:07 +0100 | [diff] [blame] | 365 | return &pdata->regulators[i]; |
Beniamino Galvani | 48a1e1b | 2014-06-22 17:31:41 +0200 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | return NULL; |
| 369 | } |
| 370 | |
Romain Perier | 2b17fa2 | 2014-10-14 06:31:10 +0000 | [diff] [blame] | 371 | static struct i2c_client *act8865_i2c_client; |
| 372 | static void act8865_power_off(void) |
| 373 | { |
| 374 | struct act8865 *act8865; |
| 375 | |
| 376 | act8865 = i2c_get_clientdata(act8865_i2c_client); |
| 377 | regmap_write(act8865->regmap, act8865->off_reg, act8865->off_mask); |
| 378 | while (1); |
| 379 | } |
| 380 | |
Maarten ter Huurne | 2d09a79 | 2019-07-22 21:14:18 -0400 | [diff] [blame^] | 381 | static int act8600_charger_get_status(struct regmap *map) |
| 382 | { |
| 383 | unsigned int val; |
| 384 | int ret; |
| 385 | u8 state0, state1; |
| 386 | |
| 387 | ret = regmap_read(map, ACT8600_APCH_STAT, &val); |
| 388 | if (ret < 0) |
| 389 | return ret; |
| 390 | |
| 391 | state0 = val & ACT8600_APCH_CSTATE0; |
| 392 | state1 = val & ACT8600_APCH_CSTATE1; |
| 393 | |
| 394 | if (state0 && !state1) |
| 395 | return POWER_SUPPLY_STATUS_CHARGING; |
| 396 | if (!state0 && state1) |
| 397 | return POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 398 | if (!state0 && !state1) |
| 399 | return POWER_SUPPLY_STATUS_DISCHARGING; |
| 400 | |
| 401 | return POWER_SUPPLY_STATUS_UNKNOWN; |
| 402 | } |
| 403 | |
| 404 | static int act8600_charger_get_property(struct power_supply *psy, |
| 405 | enum power_supply_property psp, union power_supply_propval *val) |
| 406 | { |
| 407 | struct regmap *map = power_supply_get_drvdata(psy); |
| 408 | int ret; |
| 409 | |
| 410 | switch (psp) { |
| 411 | case POWER_SUPPLY_PROP_STATUS: |
| 412 | ret = act8600_charger_get_status(map); |
| 413 | if (ret < 0) |
| 414 | return ret; |
| 415 | |
| 416 | val->intval = ret; |
| 417 | break; |
| 418 | default: |
| 419 | return -EINVAL; |
| 420 | } |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static enum power_supply_property act8600_charger_properties[] = { |
| 426 | POWER_SUPPLY_PROP_STATUS, |
| 427 | }; |
| 428 | |
| 429 | static const struct power_supply_desc act8600_charger_desc = { |
| 430 | .name = "act8600-charger", |
| 431 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 432 | .properties = act8600_charger_properties, |
| 433 | .num_properties = ARRAY_SIZE(act8600_charger_properties), |
| 434 | .get_property = act8600_charger_get_property, |
| 435 | }; |
| 436 | |
| 437 | static int act8600_charger_probe(struct device *dev, struct regmap *regmap) |
| 438 | { |
| 439 | struct power_supply *charger; |
| 440 | struct power_supply_config cfg = { |
| 441 | .drv_data = regmap, |
| 442 | .of_node = dev->of_node, |
| 443 | }; |
| 444 | |
| 445 | charger = devm_power_supply_register(dev, &act8600_charger_desc, &cfg); |
| 446 | |
| 447 | return IS_ERR(charger) ? PTR_ERR(charger) : 0; |
| 448 | } |
| 449 | |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 450 | static int act8865_pmic_probe(struct i2c_client *client, |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 451 | const struct i2c_device_id *i2c_id) |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 452 | { |
Maarten ter Huurne | abea1bb | 2016-02-28 16:53:25 +0100 | [diff] [blame] | 453 | const struct regulator_desc *regulators; |
Axel Lin | 253c9c7 | 2019-03-22 14:38:19 +0800 | [diff] [blame] | 454 | struct act8865_platform_data *pdata = NULL; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 455 | struct device *dev = &client->dev; |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 456 | int i, ret, num_regulators; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 457 | struct act8865 *act8865; |
Maarten ter Huurne | c5c9c2d | 2016-03-17 15:05:08 +0100 | [diff] [blame] | 458 | const struct regmap_config *regmap_config; |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 459 | unsigned long type; |
Romain Perier | 2b17fa2 | 2014-10-14 06:31:10 +0000 | [diff] [blame] | 460 | int off_reg, off_mask; |
Wenyou Yang | c86dc03 | 2015-09-30 15:25:49 +0800 | [diff] [blame] | 461 | int voltage_select = 0; |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 462 | |
Axel Lin | 253c9c7 | 2019-03-22 14:38:19 +0800 | [diff] [blame] | 463 | if (dev->of_node) { |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 464 | const struct of_device_id *id; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 465 | |
| 466 | id = of_match_device(of_match_ptr(act8865_dt_ids), dev); |
| 467 | if (!id) |
| 468 | return -ENODEV; |
| 469 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 470 | type = (unsigned long) id->data; |
Wenyou Yang | c86dc03 | 2015-09-30 15:25:49 +0800 | [diff] [blame] | 471 | |
| 472 | voltage_select = !!of_get_property(dev->of_node, |
| 473 | "active-semi,vsel-high", |
| 474 | NULL); |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 475 | } else { |
| 476 | type = i2c_id->driver_data; |
Axel Lin | 253c9c7 | 2019-03-22 14:38:19 +0800 | [diff] [blame] | 477 | pdata = dev_get_platdata(dev); |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | switch (type) { |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 481 | case ACT8600: |
| 482 | regulators = act8600_regulators; |
| 483 | num_regulators = ARRAY_SIZE(act8600_regulators); |
Maarten ter Huurne | c5c9c2d | 2016-03-17 15:05:08 +0100 | [diff] [blame] | 484 | regmap_config = &act8600_regmap_config; |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 485 | off_reg = -1; |
| 486 | off_mask = -1; |
| 487 | break; |
Beniamino Galvani | ac0c0ea | 2014-07-05 15:20:55 +0200 | [diff] [blame] | 488 | case ACT8846: |
Beniamino Galvani | ac0c0ea | 2014-07-05 15:20:55 +0200 | [diff] [blame] | 489 | regulators = act8846_regulators; |
| 490 | num_regulators = ARRAY_SIZE(act8846_regulators); |
Maarten ter Huurne | c5c9c2d | 2016-03-17 15:05:08 +0100 | [diff] [blame] | 491 | regmap_config = &act8865_regmap_config; |
Romain Perier | 2b17fa2 | 2014-10-14 06:31:10 +0000 | [diff] [blame] | 492 | off_reg = ACT8846_GLB_OFF_CTRL; |
| 493 | off_mask = ACT8846_OFF_SYSMASK; |
Beniamino Galvani | ac0c0ea | 2014-07-05 15:20:55 +0200 | [diff] [blame] | 494 | break; |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 495 | case ACT8865: |
Wenyou Yang | c86dc03 | 2015-09-30 15:25:49 +0800 | [diff] [blame] | 496 | if (voltage_select) { |
| 497 | regulators = act8865_alt_regulators; |
| 498 | num_regulators = ARRAY_SIZE(act8865_alt_regulators); |
| 499 | } else { |
| 500 | regulators = act8865_regulators; |
| 501 | num_regulators = ARRAY_SIZE(act8865_regulators); |
| 502 | } |
Maarten ter Huurne | c5c9c2d | 2016-03-17 15:05:08 +0100 | [diff] [blame] | 503 | regmap_config = &act8865_regmap_config; |
Romain Perier | 2b17fa2 | 2014-10-14 06:31:10 +0000 | [diff] [blame] | 504 | off_reg = ACT8865_SYS_CTRL; |
| 505 | off_mask = ACT8865_MSTROFF; |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 506 | break; |
| 507 | default: |
| 508 | dev_err(dev, "invalid device id %lu\n", type); |
| 509 | return -EINVAL; |
| 510 | } |
| 511 | |
Wenyou Yang | f1de2c2 | 2013-12-26 14:52:43 +0800 | [diff] [blame] | 512 | act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 513 | if (!act8865) |
| 514 | return -ENOMEM; |
| 515 | |
Maarten ter Huurne | c5c9c2d | 2016-03-17 15:05:08 +0100 | [diff] [blame] | 516 | act8865->regmap = devm_regmap_init_i2c(client, regmap_config); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 517 | if (IS_ERR(act8865->regmap)) { |
Axel Lin | 40d3bc1 | 2014-06-30 15:32:09 +0800 | [diff] [blame] | 518 | ret = PTR_ERR(act8865->regmap); |
Maarten ter Huurne | de14ba6 | 2016-03-17 15:05:05 +0100 | [diff] [blame] | 519 | dev_err(dev, "Failed to allocate register map: %d\n", ret); |
Axel Lin | 40d3bc1 | 2014-06-30 15:32:09 +0800 | [diff] [blame] | 520 | return ret; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 521 | } |
| 522 | |
Romain Perier | 8f73110 | 2014-11-25 12:28:25 +0000 | [diff] [blame] | 523 | if (of_device_is_system_power_controller(dev->of_node)) { |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 524 | if (!pm_power_off && (off_reg > 0)) { |
Romain Perier | 2b17fa2 | 2014-10-14 06:31:10 +0000 | [diff] [blame] | 525 | act8865_i2c_client = client; |
| 526 | act8865->off_reg = off_reg; |
| 527 | act8865->off_mask = off_mask; |
| 528 | pm_power_off = act8865_power_off; |
| 529 | } else { |
| 530 | dev_err(dev, "Failed to set poweroff capability, already defined\n"); |
| 531 | } |
| 532 | } |
| 533 | |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 534 | /* Finally register devices */ |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 535 | for (i = 0; i < num_regulators; i++) { |
| 536 | const struct regulator_desc *desc = ®ulators[i]; |
| 537 | struct regulator_config config = { }; |
| 538 | struct regulator_dev *rdev; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 539 | |
| 540 | config.dev = dev; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 541 | config.driver_data = act8865; |
| 542 | config.regmap = act8865->regmap; |
| 543 | |
Axel Lin | 253c9c7 | 2019-03-22 14:38:19 +0800 | [diff] [blame] | 544 | if (pdata) { |
| 545 | struct act8865_regulator_data *rdata; |
| 546 | |
| 547 | rdata = act8865_get_regulator_data(desc->id, pdata); |
| 548 | if (rdata) { |
| 549 | config.init_data = rdata->init_data; |
| 550 | config.of_node = rdata->of_node; |
| 551 | } |
Maarten ter Huurne | 005e4685 | 2016-03-17 15:05:07 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Maarten ter Huurne | de14ba6 | 2016-03-17 15:05:05 +0100 | [diff] [blame] | 554 | rdev = devm_regulator_register(dev, desc, &config); |
Axel Lin | fb8eb45 | 2014-03-08 21:19:07 +0800 | [diff] [blame] | 555 | if (IS_ERR(rdev)) { |
Beniamino Galvani | 48a1e1b | 2014-06-22 17:31:41 +0200 | [diff] [blame] | 556 | dev_err(dev, "failed to register %s\n", desc->name); |
Axel Lin | fb8eb45 | 2014-03-08 21:19:07 +0800 | [diff] [blame] | 557 | return PTR_ERR(rdev); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Maarten ter Huurne | 2d09a79 | 2019-07-22 21:14:18 -0400 | [diff] [blame^] | 561 | if (type == ACT8600) { |
| 562 | ret = act8600_charger_probe(dev, act8865->regmap); |
| 563 | if (ret < 0) { |
| 564 | if (ret != -EPROBE_DEFER) |
| 565 | dev_err(dev, "Failed to probe charger"); |
| 566 | return ret; |
| 567 | } |
| 568 | } |
| 569 | |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 570 | i2c_set_clientdata(client, act8865); |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 575 | static const struct i2c_device_id act8865_ids[] = { |
Zubair Lutfullah Kakakhel | df3a950 | 2015-02-27 17:04:04 +0000 | [diff] [blame] | 576 | { .name = "act8600", .driver_data = ACT8600 }, |
Beniamino Galvani | ac0c0ea | 2014-07-05 15:20:55 +0200 | [diff] [blame] | 577 | { .name = "act8846", .driver_data = ACT8846 }, |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 578 | { .name = "act8865", .driver_data = ACT8865 }, |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 579 | { }, |
| 580 | }; |
| 581 | MODULE_DEVICE_TABLE(i2c, act8865_ids); |
| 582 | |
| 583 | static struct i2c_driver act8865_pmic_driver = { |
| 584 | .driver = { |
| 585 | .name = "act8865", |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 586 | }, |
| 587 | .probe = act8865_pmic_probe, |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 588 | .id_table = act8865_ids, |
| 589 | }; |
| 590 | |
| 591 | module_i2c_driver(act8865_pmic_driver); |
| 592 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame] | 593 | MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver"); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 594 | MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>"); |
| 595 | MODULE_LICENSE("GPL v2"); |