blob: cfbb9512e48623429899cbe0785332b0a1171734 [file] [log] [blame]
Jonghwa Lee80b022e2013-06-25 10:08:38 +09001/*
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +09002 * max77693.c - Regulator driver for the Maxim 77693 and 77843
Jonghwa Lee80b022e2013-06-25 10:08:38 +09003 *
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +09004 * Copyright (C) 2013-2015 Samsung Electronics
Jonghwa Lee80b022e2013-06-25 10:08:38 +09005 * Jonghwa Lee <jonghwa3.lee@samsung.com>
Krzysztof Kozlowskicea8aa32016-08-17 14:07:46 +02006 * Krzysztof Kozlowski <krzk@kernel.org>
Jonghwa Lee80b022e2013-06-25 10:08:38 +09007 *
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 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * This driver is based on max77686.c
23 */
24
25#include <linux/err.h>
26#include <linux/slab.h>
27#include <linux/platform_device.h>
28#include <linux/module.h>
29#include <linux/export.h>
30#include <linux/regulator/driver.h>
31#include <linux/regulator/machine.h>
32#include <linux/mfd/max77693.h>
Krzysztof Kozlowski61b305c2015-07-15 21:59:50 +090033#include <linux/mfd/max77693-common.h>
Jonghwa Lee80b022e2013-06-25 10:08:38 +090034#include <linux/mfd/max77693-private.h>
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +090035#include <linux/mfd/max77843-private.h>
Jonghwa Lee80b022e2013-06-25 10:08:38 +090036#include <linux/regulator/of_regulator.h>
Robert Baldygad0540f92014-05-21 08:52:47 +020037#include <linux/regmap.h>
Jonghwa Lee80b022e2013-06-25 10:08:38 +090038
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +090039/*
40 * ID for MAX77843 regulators.
41 * There is no need for such for MAX77693.
42 */
43enum max77843_regulator_type {
44 MAX77843_SAFEOUT1 = 0,
45 MAX77843_SAFEOUT2,
46 MAX77843_CHARGER,
47
48 MAX77843_NUM,
49};
50
51/* Register differences between chargers: MAX77693 and MAX77843 */
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +090052struct chg_reg_data {
53 unsigned int linear_reg;
54 unsigned int linear_mask;
55 unsigned int uA_step;
56 unsigned int min_sel;
57};
Jonghwa Lee80b022e2013-06-25 10:08:38 +090058
Jonghwa Lee80b022e2013-06-25 10:08:38 +090059/*
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +090060 * MAX77693 CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
Jonghwa Lee80b022e2013-06-25 10:08:38 +090061 * 0x00, 0x01, 0x2, 0x03 = 60 mA
62 * 0x04 ~ 0x7E = (60 + (X - 3) * 20) mA
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +090063 * Actually for MAX77693 the driver manipulates the maximum input current,
64 * not the fast charge current (output). This should be fixed.
65 *
66 * On MAX77843 the calculation formula is the same (except values).
67 * Fortunately it properly manipulates the fast charge current.
Jonghwa Lee80b022e2013-06-25 10:08:38 +090068 */
69static int max77693_chg_get_current_limit(struct regulator_dev *rdev)
70{
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +090071 const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
Jonghwa Lee80b022e2013-06-25 10:08:38 +090072 unsigned int chg_min_uA = rdev->constraints->min_uA;
73 unsigned int chg_max_uA = rdev->constraints->max_uA;
Robert Baldygad0540f92014-05-21 08:52:47 +020074 unsigned int reg, sel;
Jonghwa Lee80b022e2013-06-25 10:08:38 +090075 unsigned int val;
76 int ret;
77
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +090078 ret = regmap_read(rdev->regmap, reg_data->linear_reg, &reg);
Jonghwa Lee80b022e2013-06-25 10:08:38 +090079 if (ret < 0)
80 return ret;
81
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +090082 sel = reg & reg_data->linear_mask;
Jonghwa Lee80b022e2013-06-25 10:08:38 +090083
84 /* the first four codes for charger current are all 60mA */
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +090085 if (sel <= reg_data->min_sel)
Jonghwa Lee80b022e2013-06-25 10:08:38 +090086 sel = 0;
87 else
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +090088 sel -= reg_data->min_sel;
Jonghwa Lee80b022e2013-06-25 10:08:38 +090089
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +090090 val = chg_min_uA + reg_data->uA_step * sel;
Jonghwa Lee80b022e2013-06-25 10:08:38 +090091 if (val > chg_max_uA)
92 return -EINVAL;
93
94 return val;
95}
96
97static int max77693_chg_set_current_limit(struct regulator_dev *rdev,
98 int min_uA, int max_uA)
99{
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +0900100 const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900101 unsigned int chg_min_uA = rdev->constraints->min_uA;
102 int sel = 0;
103
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +0900104 while (chg_min_uA + reg_data->uA_step * sel < min_uA)
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900105 sel++;
106
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +0900107 if (chg_min_uA + reg_data->uA_step * sel > max_uA)
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900108 return -EINVAL;
109
110 /* the first four codes for charger current are all 60mA */
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +0900111 sel += reg_data->min_sel;
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900112
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +0900113 return regmap_write(rdev->regmap, reg_data->linear_reg, sel);
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900114}
115/* end of CHARGER regulator ops */
116
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +0900117/* Returns regmap suitable for given regulator on chosen device */
118static struct regmap *max77693_get_regmap(enum max77693_types type,
119 struct max77693_dev *max77693,
120 int reg_id)
121{
122 if (type == TYPE_MAX77693)
123 return max77693->regmap;
124
125 /* Else: TYPE_MAX77843 */
126 switch (reg_id) {
127 case MAX77843_SAFEOUT1:
128 case MAX77843_SAFEOUT2:
129 return max77693->regmap;
130 case MAX77843_CHARGER:
131 return max77693->regmap_chg;
132 default:
133 return max77693->regmap;
134 }
135}
136
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900137static const unsigned int max77693_safeout_table[] = {
138 4850000,
139 4900000,
140 4950000,
141 3300000,
142};
143
144static struct regulator_ops max77693_safeout_ops = {
145 .list_voltage = regulator_list_voltage_table,
146 .is_enabled = regulator_is_enabled_regmap,
147 .enable = regulator_enable_regmap,
148 .disable = regulator_disable_regmap,
149 .get_voltage_sel = regulator_get_voltage_sel_regmap,
150 .set_voltage_sel = regulator_set_voltage_sel_regmap,
151};
152
153static struct regulator_ops max77693_charger_ops = {
Krzysztof Kozlowski39d23302015-04-29 19:58:28 +0900154 .is_enabled = regulator_is_enabled_regmap,
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900155 .enable = regulator_enable_regmap,
156 .disable = regulator_disable_regmap,
157 .get_current_limit = max77693_chg_get_current_limit,
158 .set_current_limit = max77693_chg_set_current_limit,
159};
160
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +0900161#define max77693_regulator_desc_esafeout(_num) { \
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900162 .name = "ESAFEOUT"#_num, \
163 .id = MAX77693_ESAFEOUT##_num, \
Krzysztof Kozlowski222d0f02015-03-26 11:35:58 +0100164 .of_match = of_match_ptr("ESAFEOUT"#_num), \
165 .regulators_node = of_match_ptr("regulators"), \
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900166 .n_voltages = 4, \
167 .ops = &max77693_safeout_ops, \
168 .type = REGULATOR_VOLTAGE, \
Axel Lin52f48bf2013-12-24 21:37:50 +0800169 .owner = THIS_MODULE, \
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900170 .volt_table = max77693_safeout_table, \
171 .vsel_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
172 .vsel_mask = SAFEOUT_CTRL_SAFEOUT##_num##_MASK, \
173 .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
174 .enable_mask = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \
175}
176
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +0900177static const struct regulator_desc max77693_supported_regulators[] = {
178 max77693_regulator_desc_esafeout(1),
179 max77693_regulator_desc_esafeout(2),
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900180 {
181 .name = "CHARGER",
182 .id = MAX77693_CHARGER,
Krzysztof Kozlowski222d0f02015-03-26 11:35:58 +0100183 .of_match = of_match_ptr("CHARGER"),
184 .regulators_node = of_match_ptr("regulators"),
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900185 .ops = &max77693_charger_ops,
186 .type = REGULATOR_CURRENT,
187 .owner = THIS_MODULE,
188 .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
189 .enable_mask = CHG_CNFG_00_CHG_MASK |
190 CHG_CNFG_00_BUCK_MASK,
Krzysztof Kozlowski39d23302015-04-29 19:58:28 +0900191 .enable_val = CHG_CNFG_00_CHG_MASK | CHG_CNFG_00_BUCK_MASK,
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900192 },
193};
194
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +0900195static const struct chg_reg_data max77693_chg_reg_data = {
196 .linear_reg = MAX77693_CHG_REG_CHG_CNFG_09,
197 .linear_mask = CHG_CNFG_09_CHGIN_ILIM_MASK,
198 .uA_step = 20000,
199 .min_sel = 3,
200};
201
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +0900202#define max77843_regulator_desc_esafeout(num) { \
203 .name = "SAFEOUT" # num, \
204 .id = MAX77843_SAFEOUT ## num, \
205 .ops = &max77693_safeout_ops, \
206 .of_match = of_match_ptr("SAFEOUT" # num), \
207 .regulators_node = of_match_ptr("regulators"), \
208 .type = REGULATOR_VOLTAGE, \
209 .owner = THIS_MODULE, \
210 .n_voltages = ARRAY_SIZE(max77693_safeout_table), \
211 .volt_table = max77693_safeout_table, \
212 .enable_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
213 .enable_mask = MAX77843_REG_SAFEOUTCTRL_ENSAFEOUT ## num, \
214 .vsel_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
215 .vsel_mask = MAX77843_REG_SAFEOUTCTRL_SAFEOUT ## num ## _MASK, \
216}
217
218static const struct regulator_desc max77843_supported_regulators[] = {
219 [MAX77843_SAFEOUT1] = max77843_regulator_desc_esafeout(1),
220 [MAX77843_SAFEOUT2] = max77843_regulator_desc_esafeout(2),
221 [MAX77843_CHARGER] = {
222 .name = "CHARGER",
223 .id = MAX77843_CHARGER,
224 .ops = &max77693_charger_ops,
225 .of_match = of_match_ptr("CHARGER"),
226 .regulators_node = of_match_ptr("regulators"),
227 .type = REGULATOR_CURRENT,
228 .owner = THIS_MODULE,
229 .enable_reg = MAX77843_CHG_REG_CHG_CNFG_00,
230 .enable_mask = MAX77843_CHG_MASK,
231 .enable_val = MAX77843_CHG_MASK,
232 },
233};
234
235static const struct chg_reg_data max77843_chg_reg_data = {
236 .linear_reg = MAX77843_CHG_REG_CHG_CNFG_02,
237 .linear_mask = MAX77843_CHG_FAST_CHG_CURRENT_MASK,
238 .uA_step = MAX77843_CHG_FAST_CHG_CURRENT_STEP,
239 .min_sel = 2,
240};
241
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900242static int max77693_pmic_probe(struct platform_device *pdev)
243{
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +0900244 enum max77693_types type = platform_get_device_id(pdev)->driver_data;
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900245 struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +0900246 const struct regulator_desc *regulators;
247 unsigned int regulators_size;
Krzysztof Kozlowski222d0f02015-03-26 11:35:58 +0100248 int i;
Krzysztof Kozlowskica0c37a2014-11-03 15:07:05 +0100249 struct regulator_config config = { };
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900250
Krzysztof Kozlowski222d0f02015-03-26 11:35:58 +0100251 config.dev = iodev->dev;
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900252
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +0900253 switch (type) {
254 case TYPE_MAX77693:
255 regulators = max77693_supported_regulators;
256 regulators_size = ARRAY_SIZE(max77693_supported_regulators);
257 config.driver_data = (void *)&max77693_chg_reg_data;
258 break;
259 case TYPE_MAX77843:
260 regulators = max77843_supported_regulators;
261 regulators_size = ARRAY_SIZE(max77843_supported_regulators);
262 config.driver_data = (void *)&max77843_chg_reg_data;
263 break;
264 default:
265 dev_err(&pdev->dev, "Unsupported device type: %u\n", type);
266 return -ENODEV;
267 }
268
269 for (i = 0; i < regulators_size; i++) {
Krzysztof Kozlowski640c24a2014-03-10 09:32:46 +0100270 struct regulator_dev *rdev;
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900271
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +0900272 config.regmap = max77693_get_regmap(type, iodev,
273 regulators[i].id);
274
Krzysztof Kozlowski640c24a2014-03-10 09:32:46 +0100275 rdev = devm_regulator_register(&pdev->dev,
Krzysztof Kozlowski222d0f02015-03-26 11:35:58 +0100276 &regulators[i], &config);
Krzysztof Kozlowski640c24a2014-03-10 09:32:46 +0100277 if (IS_ERR(rdev)) {
278 dev_err(&pdev->dev,
Krzysztof Kozlowski222d0f02015-03-26 11:35:58 +0100279 "Failed to initialize regulator-%d\n", i);
Krzysztof Kozlowski640c24a2014-03-10 09:32:46 +0100280 return PTR_ERR(rdev);
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900281 }
282 }
283
284 return 0;
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900285}
286
287static const struct platform_device_id max77693_pmic_id[] = {
Krzysztof Kozlowski5b5e7712015-07-15 21:59:49 +0900288 { "max77693-pmic", TYPE_MAX77693 },
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +0900289 { "max77843-regulator", TYPE_MAX77843 },
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900290 {},
291};
292
293MODULE_DEVICE_TABLE(platform, max77693_pmic_id);
294
295static struct platform_driver max77693_pmic_driver = {
296 .driver = {
297 .name = "max77693-pmic",
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900298 },
299 .probe = max77693_pmic_probe,
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900300 .id_table = max77693_pmic_id,
301};
302
Marek Szyprowskiee3010d2015-08-21 14:38:39 +0200303static int __init max77693_pmic_init(void)
304{
305 return platform_driver_register(&max77693_pmic_driver);
306}
307subsys_initcall(max77693_pmic_init);
308
309static void __exit max77693_pmic_cleanup(void)
310{
311 platform_driver_unregister(&max77693_pmic_driver);
312}
313module_exit(max77693_pmic_cleanup);
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900314
Krzysztof Kozlowski9e9a08e2015-07-15 21:59:54 +0900315MODULE_DESCRIPTION("MAXIM 77693/77843 regulator driver");
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900316MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
Krzysztof Kozlowskicea8aa32016-08-17 14:07:46 +0200317MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
Jonghwa Lee80b022e2013-06-25 10:08:38 +0900318MODULE_LICENSE("GPL");