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 | /* |
| 39 | * CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA |
| 40 | * 0x00, 0x01, 0x2, 0x03 = 60 mA |
| 41 | * 0x04 ~ 0x7E = (60 + (X - 3) * 20) mA |
| 42 | */ |
| 43 | static int max77693_chg_get_current_limit(struct regulator_dev *rdev) |
| 44 | { |
| 45 | unsigned int chg_min_uA = rdev->constraints->min_uA; |
| 46 | unsigned int chg_max_uA = rdev->constraints->max_uA; |
Robert Baldyga | d0540f9 | 2014-05-21 08:52:47 +0200 | [diff] [blame] | 47 | unsigned int reg, sel; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 48 | unsigned int val; |
| 49 | int ret; |
| 50 | |
Robert Baldyga | d0540f9 | 2014-05-21 08:52:47 +0200 | [diff] [blame] | 51 | ret = regmap_read(rdev->regmap, MAX77693_CHG_REG_CHG_CNFG_09, ®); |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 52 | if (ret < 0) |
| 53 | return ret; |
| 54 | |
| 55 | sel = reg & CHG_CNFG_09_CHGIN_ILIM_MASK; |
| 56 | |
| 57 | /* the first four codes for charger current are all 60mA */ |
| 58 | if (sel <= 3) |
| 59 | sel = 0; |
| 60 | else |
| 61 | sel -= 3; |
| 62 | |
| 63 | val = chg_min_uA + CHGIN_ILIM_STEP_20mA * sel; |
| 64 | if (val > chg_max_uA) |
| 65 | return -EINVAL; |
| 66 | |
| 67 | return val; |
| 68 | } |
| 69 | |
| 70 | static int max77693_chg_set_current_limit(struct regulator_dev *rdev, |
| 71 | int min_uA, int max_uA) |
| 72 | { |
| 73 | unsigned int chg_min_uA = rdev->constraints->min_uA; |
| 74 | int sel = 0; |
| 75 | |
| 76 | while (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel < min_uA) |
| 77 | sel++; |
| 78 | |
Axel Lin | f3ecdd7 | 2013-06-29 11:37:29 +0800 | [diff] [blame] | 79 | if (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel > max_uA) |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 80 | return -EINVAL; |
| 81 | |
| 82 | /* the first four codes for charger current are all 60mA */ |
| 83 | sel += 3; |
| 84 | |
Robert Baldyga | d0540f9 | 2014-05-21 08:52:47 +0200 | [diff] [blame] | 85 | return regmap_write(rdev->regmap, |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 86 | MAX77693_CHG_REG_CHG_CNFG_09, sel); |
| 87 | } |
| 88 | /* end of CHARGER regulator ops */ |
| 89 | |
| 90 | static const unsigned int max77693_safeout_table[] = { |
| 91 | 4850000, |
| 92 | 4900000, |
| 93 | 4950000, |
| 94 | 3300000, |
| 95 | }; |
| 96 | |
| 97 | static struct regulator_ops max77693_safeout_ops = { |
| 98 | .list_voltage = regulator_list_voltage_table, |
| 99 | .is_enabled = regulator_is_enabled_regmap, |
| 100 | .enable = regulator_enable_regmap, |
| 101 | .disable = regulator_disable_regmap, |
| 102 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 103 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 104 | }; |
| 105 | |
| 106 | static struct regulator_ops max77693_charger_ops = { |
Krzysztof Kozlowski | 39d2330 | 2015-04-29 19:58:28 +0900 | [diff] [blame] | 107 | .is_enabled = regulator_is_enabled_regmap, |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 108 | .enable = regulator_enable_regmap, |
| 109 | .disable = regulator_disable_regmap, |
| 110 | .get_current_limit = max77693_chg_get_current_limit, |
| 111 | .set_current_limit = max77693_chg_set_current_limit, |
| 112 | }; |
| 113 | |
| 114 | #define regulator_desc_esafeout(_num) { \ |
| 115 | .name = "ESAFEOUT"#_num, \ |
| 116 | .id = MAX77693_ESAFEOUT##_num, \ |
Krzysztof Kozlowski | 222d0f0 | 2015-03-26 11:35:58 +0100 | [diff] [blame] | 117 | .of_match = of_match_ptr("ESAFEOUT"#_num), \ |
| 118 | .regulators_node = of_match_ptr("regulators"), \ |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 119 | .n_voltages = 4, \ |
| 120 | .ops = &max77693_safeout_ops, \ |
| 121 | .type = REGULATOR_VOLTAGE, \ |
Axel Lin | 52f48bf | 2013-12-24 21:37:50 +0800 | [diff] [blame] | 122 | .owner = THIS_MODULE, \ |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 123 | .volt_table = max77693_safeout_table, \ |
| 124 | .vsel_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \ |
| 125 | .vsel_mask = SAFEOUT_CTRL_SAFEOUT##_num##_MASK, \ |
| 126 | .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \ |
| 127 | .enable_mask = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \ |
| 128 | } |
| 129 | |
Krzysztof Kozlowski | 2515b24 | 2014-10-27 16:44:12 +0100 | [diff] [blame] | 130 | static const struct regulator_desc regulators[] = { |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 131 | regulator_desc_esafeout(1), |
| 132 | regulator_desc_esafeout(2), |
| 133 | { |
| 134 | .name = "CHARGER", |
| 135 | .id = MAX77693_CHARGER, |
Krzysztof Kozlowski | 222d0f0 | 2015-03-26 11:35:58 +0100 | [diff] [blame] | 136 | .of_match = of_match_ptr("CHARGER"), |
| 137 | .regulators_node = of_match_ptr("regulators"), |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 138 | .ops = &max77693_charger_ops, |
| 139 | .type = REGULATOR_CURRENT, |
| 140 | .owner = THIS_MODULE, |
| 141 | .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00, |
| 142 | .enable_mask = CHG_CNFG_00_CHG_MASK | |
| 143 | CHG_CNFG_00_BUCK_MASK, |
Krzysztof Kozlowski | 39d2330 | 2015-04-29 19:58:28 +0900 | [diff] [blame] | 144 | .enable_val = CHG_CNFG_00_CHG_MASK | CHG_CNFG_00_BUCK_MASK, |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 145 | }, |
| 146 | }; |
| 147 | |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 148 | static int max77693_pmic_probe(struct platform_device *pdev) |
| 149 | { |
| 150 | struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent); |
Krzysztof Kozlowski | 222d0f0 | 2015-03-26 11:35:58 +0100 | [diff] [blame] | 151 | int i; |
Krzysztof Kozlowski | ca0c37a | 2014-11-03 15:07:05 +0100 | [diff] [blame] | 152 | struct regulator_config config = { }; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 153 | |
Krzysztof Kozlowski | 222d0f0 | 2015-03-26 11:35:58 +0100 | [diff] [blame] | 154 | config.dev = iodev->dev; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 155 | config.regmap = iodev->regmap; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 156 | |
Krzysztof Kozlowski | 222d0f0 | 2015-03-26 11:35:58 +0100 | [diff] [blame] | 157 | for (i = 0; i < ARRAY_SIZE(regulators); i++) { |
Krzysztof Kozlowski | 640c24a | 2014-03-10 09:32:46 +0100 | [diff] [blame] | 158 | struct regulator_dev *rdev; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 159 | |
Krzysztof Kozlowski | 640c24a | 2014-03-10 09:32:46 +0100 | [diff] [blame] | 160 | rdev = devm_regulator_register(&pdev->dev, |
Krzysztof Kozlowski | 222d0f0 | 2015-03-26 11:35:58 +0100 | [diff] [blame] | 161 | ®ulators[i], &config); |
Krzysztof Kozlowski | 640c24a | 2014-03-10 09:32:46 +0100 | [diff] [blame] | 162 | if (IS_ERR(rdev)) { |
| 163 | dev_err(&pdev->dev, |
Krzysztof Kozlowski | 222d0f0 | 2015-03-26 11:35:58 +0100 | [diff] [blame] | 164 | "Failed to initialize regulator-%d\n", i); |
Krzysztof Kozlowski | 640c24a | 2014-03-10 09:32:46 +0100 | [diff] [blame] | 165 | return PTR_ERR(rdev); |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | return 0; |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static const struct platform_device_id max77693_pmic_id[] = { |
| 173 | {"max77693-pmic", 0}, |
| 174 | {}, |
| 175 | }; |
| 176 | |
| 177 | MODULE_DEVICE_TABLE(platform, max77693_pmic_id); |
| 178 | |
| 179 | static struct platform_driver max77693_pmic_driver = { |
| 180 | .driver = { |
| 181 | .name = "max77693-pmic", |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 182 | }, |
| 183 | .probe = max77693_pmic_probe, |
Jonghwa Lee | 80b022e | 2013-06-25 10:08:38 +0900 | [diff] [blame] | 184 | .id_table = max77693_pmic_id, |
| 185 | }; |
| 186 | |
| 187 | module_platform_driver(max77693_pmic_driver); |
| 188 | |
| 189 | MODULE_DESCRIPTION("MAXIM MAX77693 regulator driver"); |
| 190 | MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>"); |
| 191 | MODULE_LICENSE("GPL"); |