Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 1 | /* |
| 2 | * act8865-regulator.c - Voltage regulation for the active-semi ACT8865 |
| 3 | * http://www.active-semi.com/sheets/ACT8865_Datasheet.pdf |
| 4 | * |
| 5 | * Copyright (C) 2013 Atmel Corporation |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/regulator/driver.h> |
| 24 | #include <linux/regulator/act8865.h> |
| 25 | #include <linux/of.h> |
| 26 | #include <linux/of_device.h> |
| 27 | #include <linux/regulator/of_regulator.h> |
| 28 | #include <linux/regmap.h> |
| 29 | |
| 30 | /* |
| 31 | * ACT8865 Global Register Map. |
| 32 | */ |
| 33 | #define ACT8865_SYS_MODE 0x00 |
| 34 | #define ACT8865_SYS_CTRL 0x01 |
| 35 | #define ACT8865_DCDC1_VSET1 0x20 |
| 36 | #define ACT8865_DCDC1_VSET2 0x21 |
| 37 | #define ACT8865_DCDC1_CTRL 0x22 |
| 38 | #define ACT8865_DCDC2_VSET1 0x30 |
| 39 | #define ACT8865_DCDC2_VSET2 0x31 |
| 40 | #define ACT8865_DCDC2_CTRL 0x32 |
| 41 | #define ACT8865_DCDC3_VSET1 0x40 |
| 42 | #define ACT8865_DCDC3_VSET2 0x41 |
| 43 | #define ACT8865_DCDC3_CTRL 0x42 |
| 44 | #define ACT8865_LDO1_VSET 0x50 |
| 45 | #define ACT8865_LDO1_CTRL 0x51 |
| 46 | #define ACT8865_LDO2_VSET 0x54 |
| 47 | #define ACT8865_LDO2_CTRL 0x55 |
| 48 | #define ACT8865_LDO3_VSET 0x60 |
| 49 | #define ACT8865_LDO3_CTRL 0x61 |
| 50 | #define ACT8865_LDO4_VSET 0x64 |
| 51 | #define ACT8865_LDO4_CTRL 0x65 |
| 52 | |
| 53 | /* |
| 54 | * Field Definitions. |
| 55 | */ |
| 56 | #define ACT8865_ENA 0x80 /* ON - [7] */ |
| 57 | #define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */ |
| 58 | |
| 59 | /* |
| 60 | * ACT8865 voltage number |
| 61 | */ |
| 62 | #define ACT8865_VOLTAGE_NUM 64 |
| 63 | |
| 64 | struct act8865 { |
| 65 | struct regulator_dev *rdev[ACT8865_REG_NUM]; |
| 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 | |
| 74 | static const struct regulator_linear_range act8865_volatge_ranges[] = { |
| 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 | |
| 90 | static const struct regulator_desc act8865_reg[] = { |
| 91 | { |
| 92 | .name = "DCDC_REG1", |
| 93 | .id = ACT8865_ID_DCDC1, |
| 94 | .ops = &act8865_ops, |
| 95 | .type = REGULATOR_VOLTAGE, |
| 96 | .n_voltages = ACT8865_VOLTAGE_NUM, |
| 97 | .linear_ranges = act8865_volatge_ranges, |
| 98 | .n_linear_ranges = ARRAY_SIZE(act8865_volatge_ranges), |
| 99 | .vsel_reg = ACT8865_DCDC1_VSET1, |
| 100 | .vsel_mask = ACT8865_VSEL_MASK, |
| 101 | .enable_reg = ACT8865_DCDC1_CTRL, |
| 102 | .enable_mask = ACT8865_ENA, |
| 103 | .owner = THIS_MODULE, |
| 104 | }, |
| 105 | { |
| 106 | .name = "DCDC_REG2", |
| 107 | .id = ACT8865_ID_DCDC2, |
| 108 | .ops = &act8865_ops, |
| 109 | .type = REGULATOR_VOLTAGE, |
| 110 | .n_voltages = ACT8865_VOLTAGE_NUM, |
| 111 | .linear_ranges = act8865_volatge_ranges, |
| 112 | .n_linear_ranges = ARRAY_SIZE(act8865_volatge_ranges), |
| 113 | .vsel_reg = ACT8865_DCDC2_VSET1, |
| 114 | .vsel_mask = ACT8865_VSEL_MASK, |
| 115 | .enable_reg = ACT8865_DCDC2_CTRL, |
| 116 | .enable_mask = ACT8865_ENA, |
| 117 | .owner = THIS_MODULE, |
| 118 | }, |
| 119 | { |
| 120 | .name = "DCDC_REG3", |
| 121 | .id = ACT8865_ID_DCDC3, |
| 122 | .ops = &act8865_ops, |
| 123 | .type = REGULATOR_VOLTAGE, |
| 124 | .n_voltages = ACT8865_VOLTAGE_NUM, |
| 125 | .linear_ranges = act8865_volatge_ranges, |
| 126 | .n_linear_ranges = ARRAY_SIZE(act8865_volatge_ranges), |
| 127 | .vsel_reg = ACT8865_DCDC3_VSET1, |
| 128 | .vsel_mask = ACT8865_VSEL_MASK, |
| 129 | .enable_reg = ACT8865_DCDC3_CTRL, |
| 130 | .enable_mask = ACT8865_ENA, |
| 131 | .owner = THIS_MODULE, |
| 132 | }, |
| 133 | { |
| 134 | .name = "LDO_REG1", |
| 135 | .id = ACT8865_ID_LDO1, |
| 136 | .ops = &act8865_ops, |
| 137 | .type = REGULATOR_VOLTAGE, |
| 138 | .n_voltages = ACT8865_VOLTAGE_NUM, |
| 139 | .linear_ranges = act8865_volatge_ranges, |
| 140 | .n_linear_ranges = ARRAY_SIZE(act8865_volatge_ranges), |
| 141 | .vsel_reg = ACT8865_LDO1_VSET, |
| 142 | .vsel_mask = ACT8865_VSEL_MASK, |
| 143 | .enable_reg = ACT8865_LDO1_CTRL, |
| 144 | .enable_mask = ACT8865_ENA, |
| 145 | .owner = THIS_MODULE, |
| 146 | }, |
| 147 | { |
| 148 | .name = "LDO_REG2", |
| 149 | .id = ACT8865_ID_LDO2, |
| 150 | .ops = &act8865_ops, |
| 151 | .type = REGULATOR_VOLTAGE, |
| 152 | .n_voltages = ACT8865_VOLTAGE_NUM, |
| 153 | .linear_ranges = act8865_volatge_ranges, |
| 154 | .n_linear_ranges = ARRAY_SIZE(act8865_volatge_ranges), |
| 155 | .vsel_reg = ACT8865_LDO2_VSET, |
| 156 | .vsel_mask = ACT8865_VSEL_MASK, |
| 157 | .enable_reg = ACT8865_LDO2_CTRL, |
| 158 | .enable_mask = ACT8865_ENA, |
| 159 | .owner = THIS_MODULE, |
| 160 | }, |
| 161 | { |
| 162 | .name = "LDO_REG3", |
| 163 | .id = ACT8865_ID_LDO3, |
| 164 | .ops = &act8865_ops, |
| 165 | .type = REGULATOR_VOLTAGE, |
| 166 | .n_voltages = ACT8865_VOLTAGE_NUM, |
| 167 | .linear_ranges = act8865_volatge_ranges, |
| 168 | .n_linear_ranges = ARRAY_SIZE(act8865_volatge_ranges), |
| 169 | .vsel_reg = ACT8865_LDO3_VSET, |
| 170 | .vsel_mask = ACT8865_VSEL_MASK, |
| 171 | .enable_reg = ACT8865_LDO3_CTRL, |
| 172 | .enable_mask = ACT8865_ENA, |
| 173 | .owner = THIS_MODULE, |
| 174 | }, |
| 175 | { |
| 176 | .name = "LDO_REG4", |
| 177 | .id = ACT8865_ID_LDO4, |
| 178 | .ops = &act8865_ops, |
| 179 | .type = REGULATOR_VOLTAGE, |
| 180 | .n_voltages = ACT8865_VOLTAGE_NUM, |
| 181 | .linear_ranges = act8865_volatge_ranges, |
| 182 | .n_linear_ranges = ARRAY_SIZE(act8865_volatge_ranges), |
| 183 | .vsel_reg = ACT8865_LDO4_VSET, |
| 184 | .vsel_mask = ACT8865_VSEL_MASK, |
| 185 | .enable_reg = ACT8865_LDO4_CTRL, |
| 186 | .enable_mask = ACT8865_ENA, |
| 187 | .owner = THIS_MODULE, |
| 188 | }, |
| 189 | }; |
| 190 | |
| 191 | #ifdef CONFIG_OF |
| 192 | static const struct of_device_id act8865_dt_ids[] = { |
| 193 | { .compatible = "active-semi,act8865" }, |
| 194 | { } |
| 195 | }; |
| 196 | MODULE_DEVICE_TABLE(of, act8865_dt_ids); |
| 197 | |
| 198 | static struct of_regulator_match act8865_matches[] = { |
| 199 | [ACT8865_ID_DCDC1] = { .name = "DCDC_REG1"}, |
| 200 | [ACT8865_ID_DCDC2] = { .name = "DCDC_REG2"}, |
| 201 | [ACT8865_ID_DCDC3] = { .name = "DCDC_REG3"}, |
| 202 | [ACT8865_ID_LDO1] = { .name = "LDO_REG1"}, |
| 203 | [ACT8865_ID_LDO2] = { .name = "LDO_REG2"}, |
| 204 | [ACT8865_ID_LDO3] = { .name = "LDO_REG3"}, |
| 205 | [ACT8865_ID_LDO4] = { .name = "LDO_REG4"}, |
| 206 | }; |
| 207 | |
| 208 | static int act8865_pdata_from_dt(struct device *dev, |
| 209 | struct device_node **of_node, |
| 210 | struct act8865_platform_data *pdata) |
| 211 | { |
| 212 | int matched, i; |
| 213 | struct device_node *np; |
| 214 | struct act8865_regulator_data *regulator; |
| 215 | |
Sachin Kamat | 6ea970a | 2014-02-14 17:19:54 +0530 | [diff] [blame] | 216 | np = of_get_child_by_name(dev->of_node, "regulators"); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 217 | if (!np) { |
| 218 | dev_err(dev, "missing 'regulators' subnode in DT\n"); |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
| 222 | matched = of_regulator_match(dev, np, |
| 223 | act8865_matches, ARRAY_SIZE(act8865_matches)); |
Sachin Kamat | 0079eb5 | 2014-02-17 14:33:29 +0530 | [diff] [blame] | 224 | of_node_put(np); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 225 | if (matched <= 0) |
| 226 | return matched; |
| 227 | |
| 228 | pdata->regulators = devm_kzalloc(dev, |
Wenyou Yang | d04b755 | 2013-12-31 09:27:52 +0800 | [diff] [blame] | 229 | sizeof(struct act8865_regulator_data) * |
| 230 | ARRAY_SIZE(act8865_matches), GFP_KERNEL); |
Sachin Kamat | 5ee77ef | 2014-02-18 16:11:08 +0530 | [diff] [blame^] | 231 | if (!pdata->regulators) |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 232 | return -ENOMEM; |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 233 | |
| 234 | pdata->num_regulators = matched; |
| 235 | regulator = pdata->regulators; |
| 236 | |
Wenyou Yang | d04b755 | 2013-12-31 09:27:52 +0800 | [diff] [blame] | 237 | for (i = 0; i < ARRAY_SIZE(act8865_matches); i++) { |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 238 | regulator->id = i; |
| 239 | regulator->name = act8865_matches[i].name; |
| 240 | regulator->platform_data = act8865_matches[i].init_data; |
| 241 | of_node[i] = act8865_matches[i].of_node; |
| 242 | regulator++; |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | #else |
| 248 | static inline int act8865_pdata_from_dt(struct device *dev, |
| 249 | struct device_node **of_node, |
| 250 | struct act8865_platform_data *pdata) |
| 251 | { |
| 252 | return 0; |
| 253 | } |
| 254 | #endif |
| 255 | |
| 256 | static int act8865_pmic_probe(struct i2c_client *client, |
| 257 | const struct i2c_device_id *i2c_id) |
| 258 | { |
| 259 | struct regulator_dev **rdev; |
| 260 | struct device *dev = &client->dev; |
| 261 | struct act8865_platform_data *pdata = dev_get_platdata(dev); |
| 262 | struct regulator_config config = { }; |
| 263 | struct act8865 *act8865; |
| 264 | struct device_node *of_node[ACT8865_REG_NUM]; |
| 265 | int i, id; |
| 266 | int ret = -EINVAL; |
| 267 | int error; |
| 268 | |
| 269 | if (dev->of_node && !pdata) { |
| 270 | const struct of_device_id *id; |
| 271 | struct act8865_platform_data pdata_of; |
| 272 | |
| 273 | id = of_match_device(of_match_ptr(act8865_dt_ids), dev); |
| 274 | if (!id) |
| 275 | return -ENODEV; |
| 276 | |
| 277 | ret = act8865_pdata_from_dt(dev, of_node, &pdata_of); |
| 278 | if (ret < 0) |
| 279 | return ret; |
| 280 | |
| 281 | pdata = &pdata_of; |
| 282 | } |
| 283 | |
| 284 | if (pdata->num_regulators > ACT8865_REG_NUM) { |
| 285 | dev_err(dev, "Too many regulators found!\n"); |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | |
Wenyou Yang | f1de2c2 | 2013-12-26 14:52:43 +0800 | [diff] [blame] | 289 | act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL); |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 290 | if (!act8865) |
| 291 | return -ENOMEM; |
| 292 | |
| 293 | rdev = act8865->rdev; |
| 294 | |
| 295 | act8865->regmap = devm_regmap_init_i2c(client, &act8865_regmap_config); |
| 296 | if (IS_ERR(act8865->regmap)) { |
| 297 | error = PTR_ERR(act8865->regmap); |
| 298 | dev_err(&client->dev, "Failed to allocate register map: %d\n", |
| 299 | error); |
| 300 | return error; |
| 301 | } |
| 302 | |
| 303 | /* Finally register devices */ |
Axel Lin | a8659ba | 2014-01-06 15:42:53 +0800 | [diff] [blame] | 304 | for (i = 0; i < ACT8865_REG_NUM; i++) { |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 305 | |
| 306 | id = pdata->regulators[i].id; |
| 307 | |
| 308 | config.dev = dev; |
| 309 | config.init_data = pdata->regulators[i].platform_data; |
| 310 | config.of_node = of_node[i]; |
| 311 | config.driver_data = act8865; |
| 312 | config.regmap = act8865->regmap; |
| 313 | |
| 314 | rdev[i] = devm_regulator_register(&client->dev, |
| 315 | &act8865_reg[i], &config); |
| 316 | if (IS_ERR(rdev[i])) { |
| 317 | dev_err(dev, "failed to register %s\n", |
| 318 | act8865_reg[id].name); |
| 319 | return PTR_ERR(rdev[i]); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | i2c_set_clientdata(client, act8865); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 328 | static const struct i2c_device_id act8865_ids[] = { |
| 329 | { "act8865", 0 }, |
| 330 | { }, |
| 331 | }; |
| 332 | MODULE_DEVICE_TABLE(i2c, act8865_ids); |
| 333 | |
| 334 | static struct i2c_driver act8865_pmic_driver = { |
| 335 | .driver = { |
| 336 | .name = "act8865", |
| 337 | .owner = THIS_MODULE, |
| 338 | }, |
| 339 | .probe = act8865_pmic_probe, |
Wenyou Yang | 33036f4 | 2013-12-24 10:34:28 +0800 | [diff] [blame] | 340 | .id_table = act8865_ids, |
| 341 | }; |
| 342 | |
| 343 | module_i2c_driver(act8865_pmic_driver); |
| 344 | |
| 345 | MODULE_DESCRIPTION("active-semi act8865 voltage regulator driver"); |
| 346 | MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>"); |
| 347 | MODULE_LICENSE("GPL v2"); |