Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 1 | /* |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 2 | * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs |
| 3 | * |
| 4 | * http://www.active-semi.com/products/power-management-units/act88xx/ |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2013 Atmel Corporation |
| 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 as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/regulator/driver.h> |
| 25 | #include <linux/regulator/act8865.h> |
| 26 | #include <linux/of.h> |
| 27 | #include <linux/of_device.h> |
| 28 | #include <linux/regulator/of_regulator.h> |
| 29 | #include <linux/regmap.h> |
| 30 | |
| 31 | /* |
| 32 | * ACT8865 Global Register Map. |
| 33 | */ |
| 34 | #define ACT8865_SYS_MODE 0x00 |
| 35 | #define ACT8865_SYS_CTRL 0x01 |
| 36 | #define ACT8865_DCDC1_VSET1 0x20 |
| 37 | #define ACT8865_DCDC1_VSET2 0x21 |
| 38 | #define ACT8865_DCDC1_CTRL 0x22 |
| 39 | #define ACT8865_DCDC2_VSET1 0x30 |
| 40 | #define ACT8865_DCDC2_VSET2 0x31 |
| 41 | #define ACT8865_DCDC2_CTRL 0x32 |
| 42 | #define ACT8865_DCDC3_VSET1 0x40 |
| 43 | #define ACT8865_DCDC3_VSET2 0x41 |
| 44 | #define ACT8865_DCDC3_CTRL 0x42 |
| 45 | #define ACT8865_LDO1_VSET 0x50 |
| 46 | #define ACT8865_LDO1_CTRL 0x51 |
| 47 | #define ACT8865_LDO2_VSET 0x54 |
| 48 | #define ACT8865_LDO2_CTRL 0x55 |
| 49 | #define ACT8865_LDO3_VSET 0x60 |
| 50 | #define ACT8865_LDO3_CTRL 0x61 |
| 51 | #define ACT8865_LDO4_VSET 0x64 |
| 52 | #define ACT8865_LDO4_CTRL 0x65 |
| 53 | |
| 54 | /* |
| 55 | * Field Definitions. |
| 56 | */ |
| 57 | #define ACT8865_ENA 0x80 /* ON - [7] */ |
| 58 | #define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */ |
| 59 | |
| 60 | /* |
| 61 | * ACT8865 voltage number |
| 62 | */ |
| 63 | #define ACT8865_VOLTAGE_NUM 64 |
| 64 | |
| 65 | struct act8865 { |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 66 | struct regmap *regmap; |
| 67 | }; |
| 68 | |
| 69 | static const struct regmap_config act8865_regmap_config = { |
| 70 | .reg_bits = 8, |
| 71 | .val_bits = 8, |
| 72 | }; |
| 73 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 74 | static const struct regulator_linear_range act8865_voltage_ranges[] = { |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 75 | REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000), |
| 76 | REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000), |
| 77 | REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000), |
| 78 | }; |
| 79 | |
| 80 | static struct regulator_ops act8865_ops = { |
| 81 | .list_voltage = regulator_list_voltage_linear_range, |
| 82 | .map_voltage = regulator_map_voltage_linear_range, |
| 83 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 84 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 85 | .enable = regulator_enable_regmap, |
| 86 | .disable = regulator_disable_regmap, |
| 87 | .is_enabled = regulator_is_enabled_regmap, |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 88 | }; |
| 89 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 90 | #define ACT88xx_REG(_name, _family, _id, _vsel_reg) \ |
| 91 | [_family##_ID_##_id] = { \ |
| 92 | .name = _name, \ |
| 93 | .id = _family##_ID_##_id, \ |
| 94 | .type = REGULATOR_VOLTAGE, \ |
| 95 | .ops = &act8865_ops, \ |
| 96 | .n_voltages = ACT8865_VOLTAGE_NUM, \ |
| 97 | .linear_ranges = act8865_voltage_ranges, \ |
| 98 | .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \ |
| 99 | .vsel_reg = _family##_##_id##_##_vsel_reg, \ |
| 100 | .vsel_mask = ACT8865_VSEL_MASK, \ |
| 101 | .enable_reg = _family##_##_id##_CTRL, \ |
| 102 | .enable_mask = ACT8865_ENA, \ |
| 103 | .owner = THIS_MODULE, \ |
| 104 | } |
| 105 | |
| 106 | static const struct regulator_desc act8865_regulators[] = { |
| 107 | ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1), |
| 108 | ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1), |
| 109 | ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1), |
| 110 | ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET), |
| 111 | ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET), |
| 112 | ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET), |
| 113 | ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET), |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | #ifdef CONFIG_OF |
| 117 | static const struct of_device_id act8865_dt_ids[] = { |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 118 | { .compatible = "active-semi,act8865", .data = (void *)ACT8865 }, |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 119 | { } |
| 120 | }; |
| 121 | MODULE_DEVICE_TABLE(of, act8865_dt_ids); |
| 122 | |
| 123 | static struct of_regulator_match act8865_matches[] = { |
| 124 | [ACT8865_ID_DCDC1] = { .name = "DCDC_REG1"}, |
| 125 | [ACT8865_ID_DCDC2] = { .name = "DCDC_REG2"}, |
| 126 | [ACT8865_ID_DCDC3] = { .name = "DCDC_REG3"}, |
| 127 | [ACT8865_ID_LDO1] = { .name = "LDO_REG1"}, |
| 128 | [ACT8865_ID_LDO2] = { .name = "LDO_REG2"}, |
| 129 | [ACT8865_ID_LDO3] = { .name = "LDO_REG3"}, |
| 130 | [ACT8865_ID_LDO4] = { .name = "LDO_REG4"}, |
| 131 | }; |
| 132 | |
| 133 | static int act8865_pdata_from_dt(struct device *dev, |
| 134 | struct device_node **of_node, |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 135 | struct act8865_platform_data *pdata, |
| 136 | struct of_regulator_match *matches, |
| 137 | int num_matches) |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 138 | { |
| 139 | int matched, i; |
| 140 | struct device_node *np; |
| 141 | struct act8865_regulator_data *regulator; |
| 142 | |
Sachin Kamat | 6ea970a | 2014-02-14 17:19:54 +0530 | [diff] [blame] | 143 | np = of_get_child_by_name(dev->of_node, "regulators"); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 144 | if (!np) { |
| 145 | dev_err(dev, "missing 'regulators' subnode in DT\n"); |
| 146 | return -EINVAL; |
| 147 | } |
| 148 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 149 | matched = of_regulator_match(dev, np, matches, num_matches); |
Sachin Kamat | 0079eb5 | 2014-02-17 14:33:29 +0530 | [diff] [blame] | 150 | of_node_put(np); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 151 | if (matched <= 0) |
| 152 | return matched; |
| 153 | |
| 154 | pdata->regulators = devm_kzalloc(dev, |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 155 | sizeof(struct act8865_regulator_data) * |
| 156 | num_matches, GFP_KERNEL); |
Sachin Kamat | 5ee77ef | 2014-02-18 16:11:08 +0530 | [diff] [blame] | 157 | if (!pdata->regulators) |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 158 | return -ENOMEM; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 159 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 160 | pdata->num_regulators = num_matches; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 161 | regulator = pdata->regulators; |
| 162 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 163 | for (i = 0; i < num_matches; i++) { |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 164 | regulator->id = i; |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 165 | regulator->name = matches[i].name; |
| 166 | regulator->platform_data = matches[i].init_data; |
| 167 | of_node[i] = matches[i].of_node; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 168 | regulator++; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | #else |
| 174 | static inline int act8865_pdata_from_dt(struct device *dev, |
| 175 | struct device_node **of_node, |
| 176 | struct act8865_platform_data *pdata) |
| 177 | { |
| 178 | return 0; |
| 179 | } |
| 180 | #endif |
| 181 | |
Beniamino Galvani | 48a1e1b | 2014-06-22 17:31:41 +0200 | [diff] [blame] | 182 | static struct regulator_init_data |
| 183 | *act8865_get_init_data(int id, struct act8865_platform_data *pdata) |
| 184 | { |
| 185 | int i; |
| 186 | |
| 187 | if (!pdata) |
| 188 | return NULL; |
| 189 | |
| 190 | for (i = 0; i < pdata->num_regulators; i++) { |
| 191 | if (pdata->regulators[i].id == id) |
| 192 | return pdata->regulators[i].platform_data; |
| 193 | } |
| 194 | |
| 195 | return NULL; |
| 196 | } |
| 197 | |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 198 | static int act8865_pmic_probe(struct i2c_client *client, |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 199 | const struct i2c_device_id *i2c_id) |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 200 | { |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 201 | static const struct regulator_desc *regulators; |
| 202 | struct act8865_platform_data pdata_of, *pdata; |
| 203 | struct of_regulator_match *matches; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 204 | struct device *dev = &client->dev; |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 205 | struct device_node **of_node; |
| 206 | int i, ret, num_regulators; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 207 | struct act8865 *act8865; |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 208 | unsigned long type; |
| 209 | |
| 210 | pdata = dev_get_platdata(dev); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 211 | |
| 212 | if (dev->of_node && !pdata) { |
| 213 | const struct of_device_id *id; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 214 | |
| 215 | id = of_match_device(of_match_ptr(act8865_dt_ids), dev); |
| 216 | if (!id) |
| 217 | return -ENODEV; |
| 218 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 219 | type = (unsigned long) id->data; |
| 220 | } else { |
| 221 | type = i2c_id->driver_data; |
| 222 | } |
| 223 | |
| 224 | switch (type) { |
| 225 | case ACT8865: |
| 226 | matches = act8865_matches; |
| 227 | regulators = act8865_regulators; |
| 228 | num_regulators = ARRAY_SIZE(act8865_regulators); |
| 229 | break; |
| 230 | default: |
| 231 | dev_err(dev, "invalid device id %lu\n", type); |
| 232 | return -EINVAL; |
| 233 | } |
| 234 | |
| 235 | of_node = devm_kzalloc(dev, sizeof(struct device_node *) * |
| 236 | num_regulators, GFP_KERNEL); |
| 237 | if (!of_node) |
| 238 | return -ENOMEM; |
| 239 | |
| 240 | if (dev->of_node && !pdata) { |
| 241 | ret = act8865_pdata_from_dt(dev, of_node, &pdata_of, matches, |
| 242 | num_regulators); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 243 | if (ret < 0) |
| 244 | return ret; |
| 245 | |
| 246 | pdata = &pdata_of; |
| 247 | } |
| 248 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 249 | if (pdata->num_regulators > num_regulators) { |
| 250 | dev_err(dev, "too many regulators: %d\n", |
| 251 | pdata->num_regulators); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 252 | return -EINVAL; |
| 253 | } |
| 254 | |
Wenyou Yang | f1de2c2 | 2013-12-26 14:52:43 +0800 | [diff] [blame] | 255 | act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 256 | if (!act8865) |
| 257 | return -ENOMEM; |
| 258 | |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 259 | act8865->regmap = devm_regmap_init_i2c(client, &act8865_regmap_config); |
| 260 | if (IS_ERR(act8865->regmap)) { |
Axel Lin | 40d3bc1 | 2014-06-30 15:32:09 +0800 | [diff] [blame] | 261 | ret = PTR_ERR(act8865->regmap); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 262 | dev_err(&client->dev, "Failed to allocate register map: %d\n", |
Axel Lin | 40d3bc1 | 2014-06-30 15:32:09 +0800 | [diff] [blame] | 263 | ret); |
| 264 | return ret; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* Finally register devices */ |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 268 | for (i = 0; i < num_regulators; i++) { |
| 269 | const struct regulator_desc *desc = ®ulators[i]; |
| 270 | struct regulator_config config = { }; |
| 271 | struct regulator_dev *rdev; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 272 | |
| 273 | config.dev = dev; |
Beniamino Galvani | 48a1e1b | 2014-06-22 17:31:41 +0200 | [diff] [blame] | 274 | config.init_data = act8865_get_init_data(desc->id, pdata); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 275 | config.of_node = of_node[i]; |
| 276 | config.driver_data = act8865; |
| 277 | config.regmap = act8865->regmap; |
| 278 | |
Beniamino Galvani | 48a1e1b | 2014-06-22 17:31:41 +0200 | [diff] [blame] | 279 | rdev = devm_regulator_register(&client->dev, desc, &config); |
Axel Lin | fb8eb45 | 2014-03-08 21:19:07 +0800 | [diff] [blame] | 280 | if (IS_ERR(rdev)) { |
Beniamino Galvani | 48a1e1b | 2014-06-22 17:31:41 +0200 | [diff] [blame] | 281 | dev_err(dev, "failed to register %s\n", desc->name); |
Axel Lin | fb8eb45 | 2014-03-08 21:19:07 +0800 | [diff] [blame] | 282 | return PTR_ERR(rdev); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | i2c_set_clientdata(client, act8865); |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 287 | devm_kfree(dev, of_node); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 292 | static const struct i2c_device_id act8865_ids[] = { |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 293 | { .name = "act8865", .driver_data = ACT8865 }, |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 294 | { }, |
| 295 | }; |
| 296 | MODULE_DEVICE_TABLE(i2c, act8865_ids); |
| 297 | |
| 298 | static struct i2c_driver act8865_pmic_driver = { |
| 299 | .driver = { |
| 300 | .name = "act8865", |
| 301 | .owner = THIS_MODULE, |
| 302 | }, |
| 303 | .probe = act8865_pmic_probe, |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 304 | .id_table = act8865_ids, |
| 305 | }; |
| 306 | |
| 307 | module_i2c_driver(act8865_pmic_driver); |
| 308 | |
Beniamino Galvani | 50a03e3 | 2014-07-05 15:20:54 +0200 | [diff] [blame^] | 309 | MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver"); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 310 | MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>"); |
| 311 | MODULE_LICENSE("GPL v2"); |