blob: bc7f4751bf9ca0a5eca167029187f2fdb2280c7e [file] [log] [blame]
Krzysztof Kozlowski5e9384c2018-08-07 18:18:25 +02001// SPDX-License-Identifier: GPL-2.0+
2//
3// max14577.c - Regulator driver for the Maxim 14577/77836
4//
5// Copyright (C) 2013,2014 Samsung Electronics
6// Krzysztof Kozlowski <krzk@kernel.org>
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +01007
8#include <linux/module.h>
9#include <linux/platform_device.h>
10#include <linux/regulator/driver.h>
11#include <linux/mfd/max14577.h>
12#include <linux/mfd/max14577-private.h>
13#include <linux/regulator/of_regulator.h>
14
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010015static int max14577_reg_is_enabled(struct regulator_dev *rdev)
16{
17 int rid = rdev_get_id(rdev);
18 struct regmap *rmap = rdev->regmap;
19 u8 reg_data;
20
21 switch (rid) {
22 case MAX14577_CHARGER:
23 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
24 if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
25 return 0;
26 max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
27 if ((reg_data & STATUS3_CGMBC_MASK) == 0)
28 return 0;
29 /* MBCHOSTEN and CGMBC are on */
30 return 1;
31 default:
32 return -EINVAL;
33 }
34}
35
36static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
37{
38 u8 reg_data;
39 struct regmap *rmap = rdev->regmap;
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +020040 struct max14577 *max14577 = rdev_get_drvdata(rdev);
41 const struct maxim_charger_current *limits =
42 &maxim_charger_currents[max14577->dev_type];
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010043
44 if (rdev_get_id(rdev) != MAX14577_CHARGER)
45 return -EINVAL;
46
47 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, &reg_data);
48
49 if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +020050 return limits->min;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010051
52 reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
53 CHGCTRL4_MBCICHWRCH_SHIFT);
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +020054 return limits->high_start + reg_data * limits->high_step;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010055}
56
57static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
58 int min_uA, int max_uA)
59{
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010060 u8 reg_data;
Krzysztof Kozlowskib8f139f2014-09-12 08:53:56 +020061 int ret;
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +020062 struct max14577 *max14577 = rdev_get_drvdata(rdev);
63 const struct maxim_charger_current *limits =
64 &maxim_charger_currents[max14577->dev_type];
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010065
66 if (rdev_get_id(rdev) != MAX14577_CHARGER)
67 return -EINVAL;
68
Krzysztof Kozlowskib8f139f2014-09-12 08:53:56 +020069 ret = maxim_charger_calc_reg_current(limits, min_uA, max_uA, &reg_data);
70 if (ret)
71 return ret;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010072
73 return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
74 CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
75 reg_data);
76}
77
Bhumika Goyal18c6c422017-01-28 19:48:28 +053078static const struct regulator_ops max14577_safeout_ops = {
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010079 .is_enabled = regulator_is_enabled_regmap,
80 .enable = regulator_enable_regmap,
81 .disable = regulator_disable_regmap,
82 .list_voltage = regulator_list_voltage_linear,
83};
84
Bhumika Goyal18c6c422017-01-28 19:48:28 +053085static const struct regulator_ops max14577_charger_ops = {
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010086 .is_enabled = max14577_reg_is_enabled,
87 .enable = regulator_enable_regmap,
88 .disable = regulator_disable_regmap,
89 .get_current_limit = max14577_reg_get_current_limit,
90 .set_current_limit = max14577_reg_set_current_limit,
91};
92
Krzysztof Kozlowskicab344d2015-04-18 20:27:17 +090093#define MAX14577_SAFEOUT_REG { \
94 .name = "SAFEOUT", \
95 .of_match = of_match_ptr("SAFEOUT"), \
96 .regulators_node = of_match_ptr("regulators"), \
97 .id = MAX14577_SAFEOUT, \
98 .ops = &max14577_safeout_ops, \
99 .type = REGULATOR_VOLTAGE, \
100 .owner = THIS_MODULE, \
101 .n_voltages = 1, \
102 .min_uV = MAX14577_REGULATOR_SAFEOUT_VOLTAGE, \
103 .enable_reg = MAX14577_REG_CONTROL2, \
104 .enable_mask = CTRL2_SFOUTORD_MASK, \
105}
106#define MAX14577_CHARGER_REG { \
107 .name = "CHARGER", \
108 .of_match = of_match_ptr("CHARGER"), \
109 .regulators_node = of_match_ptr("regulators"), \
110 .id = MAX14577_CHARGER, \
111 .ops = &max14577_charger_ops, \
112 .type = REGULATOR_CURRENT, \
113 .owner = THIS_MODULE, \
114 .enable_reg = MAX14577_CHG_REG_CHG_CTRL2, \
115 .enable_mask = CHGCTRL2_MBCHOSTEN_MASK, \
116}
117
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200118static const struct regulator_desc max14577_supported_regulators[] = {
Krzysztof Kozlowskicab344d2015-04-18 20:27:17 +0900119 [MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG,
120 [MAX14577_CHARGER] = MAX14577_CHARGER_REG,
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100121};
122
Bhumika Goyal18c6c422017-01-28 19:48:28 +0530123static const struct regulator_ops max77836_ldo_ops = {
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200124 .is_enabled = regulator_is_enabled_regmap,
125 .enable = regulator_enable_regmap,
126 .disable = regulator_disable_regmap,
127 .list_voltage = regulator_list_voltage_linear,
128 .map_voltage = regulator_map_voltage_linear,
129 .get_voltage_sel = regulator_get_voltage_sel_regmap,
130 .set_voltage_sel = regulator_set_voltage_sel_regmap,
131 /* TODO: add .set_suspend_mode */
132};
133
Krzysztof Kozlowskicab344d2015-04-18 20:27:17 +0900134#define MAX77836_LDO_REG(num) { \
135 .name = "LDO" # num, \
136 .of_match = of_match_ptr("LDO" # num), \
137 .regulators_node = of_match_ptr("regulators"), \
138 .id = MAX77836_LDO ## num, \
139 .ops = &max77836_ldo_ops, \
140 .type = REGULATOR_VOLTAGE, \
141 .owner = THIS_MODULE, \
142 .n_voltages = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM, \
143 .min_uV = MAX77836_REGULATOR_LDO_VOLTAGE_MIN, \
144 .uV_step = MAX77836_REGULATOR_LDO_VOLTAGE_STEP, \
145 .enable_reg = MAX77836_LDO_REG_CNFG1_LDO ## num, \
146 .enable_mask = MAX77836_CNFG1_LDO_PWRMD_MASK, \
147 .vsel_reg = MAX77836_LDO_REG_CNFG1_LDO ## num, \
148 .vsel_mask = MAX77836_CNFG1_LDO_TV_MASK, \
149}
150
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200151static const struct regulator_desc max77836_supported_regulators[] = {
Krzysztof Kozlowskicab344d2015-04-18 20:27:17 +0900152 [MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG,
153 [MAX14577_CHARGER] = MAX14577_CHARGER_REG,
154 [MAX77836_LDO1] = MAX77836_LDO_REG(1),
155 [MAX77836_LDO2] = MAX77836_LDO_REG(2),
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200156};
157
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100158#ifdef CONFIG_OF
159static struct of_regulator_match max14577_regulator_matches[] = {
160 { .name = "SAFEOUT", },
161 { .name = "CHARGER", },
162};
163
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200164static struct of_regulator_match max77836_regulator_matches[] = {
165 { .name = "SAFEOUT", },
166 { .name = "CHARGER", },
167 { .name = "LDO1", },
168 { .name = "LDO2", },
169};
170
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200171static inline struct regulator_init_data *match_init_data(int index,
172 enum maxim_device_type dev_type)
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100173{
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200174 switch (dev_type) {
175 case MAXIM_DEVICE_TYPE_MAX77836:
176 return max77836_regulator_matches[index].init_data;
177
178 case MAXIM_DEVICE_TYPE_MAX14577:
179 default:
180 return max14577_regulator_matches[index].init_data;
181 }
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100182}
183
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200184static inline struct device_node *match_of_node(int index,
185 enum maxim_device_type dev_type)
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100186{
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200187 switch (dev_type) {
188 case MAXIM_DEVICE_TYPE_MAX77836:
189 return max77836_regulator_matches[index].of_node;
190
191 case MAXIM_DEVICE_TYPE_MAX14577:
192 default:
193 return max14577_regulator_matches[index].of_node;
194 }
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100195}
196#else /* CONFIG_OF */
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200197static inline struct regulator_init_data *match_init_data(int index,
198 enum maxim_device_type dev_type)
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100199{
200 return NULL;
201}
202
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200203static inline struct device_node *match_of_node(int index,
204 enum maxim_device_type dev_type)
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100205{
206 return NULL;
207}
208#endif /* CONFIG_OF */
209
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200210/**
211 * Registers for regulators of max77836 use different I2C slave addresses so
212 * different regmaps must be used for them.
213 *
214 * Returns proper regmap for accessing regulator passed by id.
215 */
216static struct regmap *max14577_get_regmap(struct max14577 *max14577,
217 int reg_id)
218{
219 switch (max14577->dev_type) {
220 case MAXIM_DEVICE_TYPE_MAX77836:
221 switch (reg_id) {
222 case MAX77836_SAFEOUT ... MAX77836_CHARGER:
223 return max14577->regmap;
224 default:
225 /* MAX77836_LDO1 ... MAX77836_LDO2 */
226 return max14577->regmap_pmic;
227 }
228
229 case MAXIM_DEVICE_TYPE_MAX14577:
230 default:
231 return max14577->regmap;
232 }
233}
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100234
235static int max14577_regulator_probe(struct platform_device *pdev)
236{
237 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
238 struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
Beomho Seoe951cee2014-12-19 18:04:46 +0900239 int i, ret = 0;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100240 struct regulator_config config = {};
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200241 const struct regulator_desc *supported_regulators;
242 unsigned int supported_regulators_size;
243 enum maxim_device_type dev_type = max14577->dev_type;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100244
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200245 switch (dev_type) {
246 case MAXIM_DEVICE_TYPE_MAX77836:
247 supported_regulators = max77836_supported_regulators;
248 supported_regulators_size = ARRAY_SIZE(max77836_supported_regulators);
249 break;
250 case MAXIM_DEVICE_TYPE_MAX14577:
251 default:
252 supported_regulators = max14577_supported_regulators;
253 supported_regulators_size = ARRAY_SIZE(max14577_supported_regulators);
254 }
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100255
Beomho Seoe951cee2014-12-19 18:04:46 +0900256 config.dev = max14577->dev;
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200257 config.driver_data = max14577;
258
259 for (i = 0; i < supported_regulators_size; i++) {
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100260 struct regulator_dev *regulator;
261 /*
262 * Index of supported_regulators[] is also the id and must
263 * match index of pdata->regulators[].
264 */
265 if (pdata && pdata->regulators) {
266 config.init_data = pdata->regulators[i].initdata;
267 config.of_node = pdata->regulators[i].of_node;
268 } else {
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200269 config.init_data = match_init_data(i, dev_type);
270 config.of_node = match_of_node(i, dev_type);
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100271 }
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200272 config.regmap = max14577_get_regmap(max14577,
273 supported_regulators[i].id);
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100274
275 regulator = devm_regulator_register(&pdev->dev,
276 &supported_regulators[i], &config);
277 if (IS_ERR(regulator)) {
278 ret = PTR_ERR(regulator);
279 dev_err(&pdev->dev,
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200280 "Regulator init failed for %d/%s with error: %d\n",
281 i, supported_regulators[i].name, ret);
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100282 return ret;
283 }
284 }
285
286 return ret;
287}
288
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200289static const struct platform_device_id max14577_regulator_id[] = {
290 { "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577, },
291 { "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836, },
292 { }
293};
294MODULE_DEVICE_TABLE(platform, max14577_regulator_id);
295
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100296static struct platform_driver max14577_regulator_driver = {
297 .driver = {
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100298 .name = "max14577-regulator",
299 },
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200300 .probe = max14577_regulator_probe,
301 .id_table = max14577_regulator_id,
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100302};
303
304static int __init max14577_regulator_init(void)
305{
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200306 BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators) != MAX14577_REGULATOR_NUM);
307 BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators) != MAX77836_REGULATOR_NUM);
308
309 BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN +
310 (MAX77836_REGULATOR_LDO_VOLTAGE_STEP *
311 (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM - 1)) !=
312 MAX77836_REGULATOR_LDO_VOLTAGE_MAX);
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100313
314 return platform_driver_register(&max14577_regulator_driver);
315}
316subsys_initcall(max14577_regulator_init);
317
318static void __exit max14577_regulator_exit(void)
319{
320 platform_driver_unregister(&max14577_regulator_driver);
321}
322module_exit(max14577_regulator_exit);
323
Krzysztof Kozlowskicea8aa32016-08-17 14:07:46 +0200324MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200325MODULE_DESCRIPTION("Maxim 14577/77836 regulator driver");
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100326MODULE_LICENSE("GPL");
Axel Lin9f45a3d2013-12-23 14:50:53 +0800327MODULE_ALIAS("platform:max14577-regulator");