Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 1 | /* |
| 2 | * max77693.c - Regulator driver for the Maxim 77693 |
| 3 | * |
| 4 | * Copyright (C) 2013 Samsung Electronics |
| 5 | * Jonghwa Lee <jonghwa3.lee@samsung.com> |
| 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 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | * This driver is based on max77686.c |
| 22 | */ |
| 23 | |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/export.h> |
| 29 | #include <linux/regulator/driver.h> |
| 30 | #include <linux/regulator/machine.h> |
| 31 | #include <linux/mfd/max77693.h> |
| 32 | #include <linux/mfd/max77693-private.h> |
| 33 | #include <linux/regulator/of_regulator.h> |
Robert Baldyga | d0540f9 | 2014-05-21 08:52:47 +0200 | [diff] [blame] | 34 | #include <linux/regmap.h> |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 35 | |
| 36 | #define CHGIN_ILIM_STEP_20mA 20000 |
| 37 | |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 38 | /* CHARGER regulator ops */ |
| 39 | /* CHARGER regulator uses two bits for enabling */ |
| 40 | static int max77693_chg_is_enabled(struct regulator_dev *rdev) |
| 41 | { |
| 42 | int ret; |
Robert Baldyga | d0540f9 | 2014-05-21 08:52:47 +0200 | [diff] [blame] | 43 | unsigned int val; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 44 | |
Robert Baldyga | d0540f9 | 2014-05-21 08:52:47 +0200 | [diff] [blame] | 45 | ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val); |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 46 | if (ret) |
| 47 | return ret; |
| 48 | |
| 49 | return (val & rdev->desc->enable_mask) == rdev->desc->enable_mask; |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA |
| 54 | * 0x00, 0x01, 0x2, 0x03 = 60 mA |
| 55 | * 0x04 ~ 0x7E = (60 + (X - 3) * 20) mA |
| 56 | */ |
| 57 | static int max77693_chg_get_current_limit(struct regulator_dev *rdev) |
| 58 | { |
| 59 | unsigned int chg_min_uA = rdev->constraints->min_uA; |
| 60 | unsigned int chg_max_uA = rdev->constraints->max_uA; |
Robert Baldyga | d0540f9 | 2014-05-21 08:52:47 +0200 | [diff] [blame] | 61 | unsigned int reg, sel; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 62 | unsigned int val; |
| 63 | int ret; |
| 64 | |
Robert Baldyga | d0540f9 | 2014-05-21 08:52:47 +0200 | [diff] [blame] | 65 | ret = regmap_read(rdev->regmap, MAX77693_CHG_REG_CHG_CNFG_09, ®); |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 66 | if (ret < 0) |
| 67 | return ret; |
| 68 | |
| 69 | sel = reg & CHG_CNFG_09_CHGIN_ILIM_MASK; |
| 70 | |
| 71 | /* the first four codes for charger current are all 60mA */ |
| 72 | if (sel <= 3) |
| 73 | sel = 0; |
| 74 | else |
| 75 | sel -= 3; |
| 76 | |
| 77 | val = chg_min_uA + CHGIN_ILIM_STEP_20mA * sel; |
| 78 | if (val > chg_max_uA) |
| 79 | return -EINVAL; |
| 80 | |
| 81 | return val; |
| 82 | } |
| 83 | |
| 84 | static int max77693_chg_set_current_limit(struct regulator_dev *rdev, |
| 85 | int min_uA, int max_uA) |
| 86 | { |
| 87 | unsigned int chg_min_uA = rdev->constraints->min_uA; |
| 88 | int sel = 0; |
| 89 | |
| 90 | while (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel < min_uA) |
| 91 | sel++; |
| 92 | |
Axel Lin | f3ecdd7 | 2013-06-29 11:37:29 +0800 | [diff] [blame] | 93 | if (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel > max_uA) |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 94 | return -EINVAL; |
| 95 | |
| 96 | /* the first four codes for charger current are all 60mA */ |
| 97 | sel += 3; |
| 98 | |
Robert Baldyga | d0540f9 | 2014-05-21 08:52:47 +0200 | [diff] [blame] | 99 | return regmap_write(rdev->regmap, |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 100 | MAX77693_CHG_REG_CHG_CNFG_09, sel); |
| 101 | } |
| 102 | /* end of CHARGER regulator ops */ |
| 103 | |
| 104 | static const unsigned int max77693_safeout_table[] = { |
| 105 | 4850000, |
| 106 | 4900000, |
| 107 | 4950000, |
| 108 | 3300000, |
| 109 | }; |
| 110 | |
| 111 | static struct regulator_ops max77693_safeout_ops = { |
| 112 | .list_voltage = regulator_list_voltage_table, |
| 113 | .is_enabled = regulator_is_enabled_regmap, |
| 114 | .enable = regulator_enable_regmap, |
| 115 | .disable = regulator_disable_regmap, |
| 116 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 117 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 118 | }; |
| 119 | |
| 120 | static struct regulator_ops max77693_charger_ops = { |
| 121 | .is_enabled = max77693_chg_is_enabled, |
| 122 | .enable = regulator_enable_regmap, |
| 123 | .disable = regulator_disable_regmap, |
| 124 | .get_current_limit = max77693_chg_get_current_limit, |
| 125 | .set_current_limit = max77693_chg_set_current_limit, |
| 126 | }; |
| 127 | |
| 128 | #define regulator_desc_esafeout(_num) { \ |
| 129 | .name = "ESAFEOUT"#_num, \ |
| 130 | .id = MAX77693_ESAFEOUT##_num, \ |
| 131 | .n_voltages = 4, \ |
| 132 | .ops = &max77693_safeout_ops, \ |
| 133 | .type = REGULATOR_VOLTAGE, \ |
Axel Lin | 52f48bf | 2013-12-24 21:37:50 +0800 | [diff] [blame] | 134 | .owner = THIS_MODULE, \ |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 135 | .volt_table = max77693_safeout_table, \ |
| 136 | .vsel_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \ |
| 137 | .vsel_mask = SAFEOUT_CTRL_SAFEOUT##_num##_MASK, \ |
| 138 | .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \ |
| 139 | .enable_mask = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \ |
| 140 | } |
| 141 | |
Krzysztof Kozlowski | 2515b24 | 2014-10-27 16:44:12 +0100 | [diff] [blame] | 142 | static const struct regulator_desc regulators[] = { |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 143 | regulator_desc_esafeout(1), |
| 144 | regulator_desc_esafeout(2), |
| 145 | { |
| 146 | .name = "CHARGER", |
| 147 | .id = MAX77693_CHARGER, |
| 148 | .ops = &max77693_charger_ops, |
| 149 | .type = REGULATOR_CURRENT, |
| 150 | .owner = THIS_MODULE, |
| 151 | .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00, |
| 152 | .enable_mask = CHG_CNFG_00_CHG_MASK | |
| 153 | CHG_CNFG_00_BUCK_MASK, |
| 154 | }, |
| 155 | }; |
| 156 | |
| 157 | #ifdef CONFIG_OF |
| 158 | static int max77693_pmic_dt_parse_rdata(struct device *dev, |
| 159 | struct max77693_regulator_data **rdata) |
| 160 | { |
| 161 | struct device_node *np; |
| 162 | struct of_regulator_match *rmatch; |
| 163 | struct max77693_regulator_data *tmp; |
| 164 | int i, matched = 0; |
| 165 | |
Sachin Kamat | 4fa43a4 | 2014-02-14 17:19:48 +0530 | [diff] [blame] | 166 | np = of_get_child_by_name(dev->parent->of_node, "regulators"); |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 167 | if (!np) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | rmatch = devm_kzalloc(dev, |
| 171 | sizeof(*rmatch) * ARRAY_SIZE(regulators), GFP_KERNEL); |
Sachin Kamat | d9aa5c5 | 2014-02-17 14:33:33 +0530 | [diff] [blame] | 172 | if (!rmatch) { |
| 173 | of_node_put(np); |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 174 | return -ENOMEM; |
Sachin Kamat | d9aa5c5 | 2014-02-17 14:33:33 +0530 | [diff] [blame] | 175 | } |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 176 | |
| 177 | for (i = 0; i < ARRAY_SIZE(regulators); i++) |
| 178 | rmatch[i].name = regulators[i].name; |
| 179 | |
| 180 | matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(regulators)); |
Sachin Kamat | d9aa5c5 | 2014-02-17 14:33:33 +0530 | [diff] [blame] | 181 | of_node_put(np); |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 182 | if (matched <= 0) |
| 183 | return matched; |
| 184 | *rdata = devm_kzalloc(dev, sizeof(**rdata) * matched, GFP_KERNEL); |
| 185 | if (!(*rdata)) |
| 186 | return -ENOMEM; |
| 187 | |
| 188 | tmp = *rdata; |
| 189 | |
Axel Lin | 8c7e7dd | 2013-06-30 00:33:52 +0800 | [diff] [blame] | 190 | for (i = 0; i < matched; i++) { |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 191 | tmp->initdata = rmatch[i].init_data; |
| 192 | tmp->of_node = rmatch[i].of_node; |
| 193 | tmp->id = regulators[i].id; |
| 194 | tmp++; |
| 195 | } |
| 196 | |
| 197 | return matched; |
| 198 | } |
| 199 | #else |
| 200 | static int max77693_pmic_dt_parse_rdata(struct device *dev, |
| 201 | struct max77693_regulator_data **rdata) |
| 202 | { |
| 203 | return 0; |
| 204 | } |
| 205 | #endif /* CONFIG_OF */ |
| 206 | |
| 207 | static int max77693_pmic_init_rdata(struct device *dev, |
| 208 | struct max77693_regulator_data **rdata) |
| 209 | { |
| 210 | struct max77693_platform_data *pdata; |
| 211 | int num_regulators = 0; |
| 212 | |
| 213 | pdata = dev_get_platdata(dev->parent); |
| 214 | if (pdata) { |
| 215 | *rdata = pdata->regulators; |
| 216 | num_regulators = pdata->num_regulators; |
| 217 | } |
| 218 | |
| 219 | if (!(*rdata) && dev->parent->of_node) |
| 220 | num_regulators = max77693_pmic_dt_parse_rdata(dev, rdata); |
| 221 | |
| 222 | return num_regulators; |
| 223 | } |
| 224 | |
| 225 | static int max77693_pmic_probe(struct platform_device *pdev) |
| 226 | { |
| 227 | struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent); |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 228 | struct max77693_regulator_data *rdata = NULL; |
Sachin Kamat | 027a2754 | 2013-09-04 11:07:56 +0530 | [diff] [blame] | 229 | int num_rdata, i; |
Krzysztof Kozlowski | ca0c37a | 2014-11-03 15:07:05 +0100 | [diff] [blame] | 230 | struct regulator_config config = { }; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 231 | |
| 232 | num_rdata = max77693_pmic_init_rdata(&pdev->dev, &rdata); |
| 233 | if (!rdata || num_rdata <= 0) { |
| 234 | dev_err(&pdev->dev, "No init data supplied.\n"); |
| 235 | return -ENODEV; |
| 236 | } |
| 237 | |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 238 | config.dev = &pdev->dev; |
| 239 | config.regmap = iodev->regmap; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 240 | |
Krzysztof Kozlowski | 640c24a | 2014-03-10 09:32:46 +0100 | [diff] [blame] | 241 | for (i = 0; i < num_rdata; i++) { |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 242 | int id = rdata[i].id; |
Krzysztof Kozlowski | 640c24a | 2014-03-10 09:32:46 +0100 | [diff] [blame] | 243 | struct regulator_dev *rdev; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 244 | |
| 245 | config.init_data = rdata[i].initdata; |
| 246 | config.of_node = rdata[i].of_node; |
| 247 | |
Krzysztof Kozlowski | 640c24a | 2014-03-10 09:32:46 +0100 | [diff] [blame] | 248 | rdev = devm_regulator_register(&pdev->dev, |
Sachin Kamat | 027a2754 | 2013-09-04 11:07:56 +0530 | [diff] [blame] | 249 | ®ulators[id], &config); |
Krzysztof Kozlowski | 640c24a | 2014-03-10 09:32:46 +0100 | [diff] [blame] | 250 | if (IS_ERR(rdev)) { |
| 251 | dev_err(&pdev->dev, |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 252 | "Failed to initialize regulator-%d\n", id); |
Krzysztof Kozlowski | 640c24a | 2014-03-10 09:32:46 +0100 | [diff] [blame] | 253 | return PTR_ERR(rdev); |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | return 0; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static const struct platform_device_id max77693_pmic_id[] = { |
| 261 | {"max77693-pmic", 0}, |
| 262 | {}, |
| 263 | }; |
| 264 | |
| 265 | MODULE_DEVICE_TABLE(platform, max77693_pmic_id); |
| 266 | |
| 267 | static struct platform_driver max77693_pmic_driver = { |
| 268 | .driver = { |
| 269 | .name = "max77693-pmic", |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 270 | }, |
| 271 | .probe = max77693_pmic_probe, |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 272 | .id_table = max77693_pmic_id, |
| 273 | }; |
| 274 | |
| 275 | module_platform_driver(max77693_pmic_driver); |
| 276 | |
| 277 | MODULE_DESCRIPTION("MAXIM MAX77693 regulator driver"); |
| 278 | MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>"); |
| 279 | MODULE_LICENSE("GPL"); |