blob: ed60baaeceeca79cc9ba2b9c770973d5039da3fa [file] [log] [blame]
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +01001/*
2 * max14577.c - Regulator driver for the Maxim 14577
3 *
Krzysztof Kozlowski4d047d62014-01-28 13:18:25 +01004 * Copyright (C) 2013,2014 Samsung Electronics
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +01005 * Krzysztof Kozlowski <k.kozlowski@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
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/regulator/driver.h>
21#include <linux/mfd/max14577.h>
22#include <linux/mfd/max14577-private.h>
23#include <linux/regulator/of_regulator.h>
24
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010025static int max14577_reg_is_enabled(struct regulator_dev *rdev)
26{
27 int rid = rdev_get_id(rdev);
28 struct regmap *rmap = rdev->regmap;
29 u8 reg_data;
30
31 switch (rid) {
32 case MAX14577_CHARGER:
33 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
34 if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
35 return 0;
36 max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
37 if ((reg_data & STATUS3_CGMBC_MASK) == 0)
38 return 0;
39 /* MBCHOSTEN and CGMBC are on */
40 return 1;
41 default:
42 return -EINVAL;
43 }
44}
45
46static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
47{
48 u8 reg_data;
49 struct regmap *rmap = rdev->regmap;
50
51 if (rdev_get_id(rdev) != MAX14577_CHARGER)
52 return -EINVAL;
53
54 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, &reg_data);
55
56 if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
57 return MAX14577_REGULATOR_CURRENT_LIMIT_MIN;
58
59 reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
60 CHGCTRL4_MBCICHWRCH_SHIFT);
61 return MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START +
62 reg_data * MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP;
63}
64
65static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
66 int min_uA, int max_uA)
67{
68 int i, current_bits = 0xf;
69 u8 reg_data;
70
71 if (rdev_get_id(rdev) != MAX14577_CHARGER)
72 return -EINVAL;
73
74 if (min_uA > MAX14577_REGULATOR_CURRENT_LIMIT_MAX ||
75 max_uA < MAX14577_REGULATOR_CURRENT_LIMIT_MIN)
76 return -EINVAL;
77
78 if (max_uA < MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START) {
79 /* Less than 200 mA, so set 90mA (turn only Low Bit off) */
80 u8 reg_data = 0x0 << CHGCTRL4_MBCICHWRCL_SHIFT;
81 return max14577_update_reg(rdev->regmap,
82 MAX14577_CHG_REG_CHG_CTRL4,
83 CHGCTRL4_MBCICHWRCL_MASK, reg_data);
84 }
85
86 /* max_uA is in range: <LIMIT_HIGH_START, inifinite>, so search for
87 * valid current starting from LIMIT_MAX. */
88 for (i = MAX14577_REGULATOR_CURRENT_LIMIT_MAX;
89 i >= MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START;
90 i -= MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP) {
91 if (i <= max_uA)
92 break;
93 current_bits--;
94 }
95 BUG_ON(current_bits < 0); /* Cannot happen */
96 /* Turn Low Bit on (use range 200mA-950 mA) */
97 reg_data = 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT;
98 /* and set proper High Bits */
99 reg_data |= current_bits << CHGCTRL4_MBCICHWRCH_SHIFT;
100
101 return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
102 CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
103 reg_data);
104}
105
106static struct regulator_ops max14577_safeout_ops = {
107 .is_enabled = regulator_is_enabled_regmap,
108 .enable = regulator_enable_regmap,
109 .disable = regulator_disable_regmap,
110 .list_voltage = regulator_list_voltage_linear,
111};
112
113static struct regulator_ops max14577_charger_ops = {
114 .is_enabled = max14577_reg_is_enabled,
115 .enable = regulator_enable_regmap,
116 .disable = regulator_disable_regmap,
117 .get_current_limit = max14577_reg_get_current_limit,
118 .set_current_limit = max14577_reg_set_current_limit,
119};
120
121static const struct regulator_desc supported_regulators[] = {
122 [MAX14577_SAFEOUT] = {
123 .name = "SAFEOUT",
124 .id = MAX14577_SAFEOUT,
125 .ops = &max14577_safeout_ops,
126 .type = REGULATOR_VOLTAGE,
127 .owner = THIS_MODULE,
128 .n_voltages = 1,
129 .min_uV = MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
130 .enable_reg = MAX14577_REG_CONTROL2,
131 .enable_mask = CTRL2_SFOUTORD_MASK,
132 },
133 [MAX14577_CHARGER] = {
134 .name = "CHARGER",
135 .id = MAX14577_CHARGER,
136 .ops = &max14577_charger_ops,
137 .type = REGULATOR_CURRENT,
138 .owner = THIS_MODULE,
139 .enable_reg = MAX14577_CHG_REG_CHG_CTRL2,
140 .enable_mask = CHGCTRL2_MBCHOSTEN_MASK,
141 },
142};
143
144#ifdef CONFIG_OF
145static struct of_regulator_match max14577_regulator_matches[] = {
146 { .name = "SAFEOUT", },
147 { .name = "CHARGER", },
148};
149
150static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev)
151{
152 int ret;
153 struct device_node *np;
154
155 np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
156 if (!np) {
157 dev_err(&pdev->dev, "Failed to get child OF node for regulators\n");
158 return -EINVAL;
159 }
160
161 ret = of_regulator_match(&pdev->dev, np, max14577_regulator_matches,
162 MAX14577_REG_MAX);
Krzysztof Kozlowski08221fc2014-02-21 13:12:38 +0100163 if (ret < 0)
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100164 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n", ret);
Krzysztof Kozlowski08221fc2014-02-21 13:12:38 +0100165 else
166 ret = 0;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100167
Krzysztof Kozlowski667a6b72014-02-05 17:56:27 +0100168 of_node_put(np);
169
170 return ret;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100171}
172
173static inline struct regulator_init_data *match_init_data(int index)
174{
175 return max14577_regulator_matches[index].init_data;
176}
177
178static inline struct device_node *match_of_node(int index)
179{
180 return max14577_regulator_matches[index].of_node;
181}
182#else /* CONFIG_OF */
183static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev)
184{
185 return 0;
186}
187static inline struct regulator_init_data *match_init_data(int index)
188{
189 return NULL;
190}
191
192static inline struct device_node *match_of_node(int index)
193{
194 return NULL;
195}
196#endif /* CONFIG_OF */
197
198
199static int max14577_regulator_probe(struct platform_device *pdev)
200{
201 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
202 struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
203 int i, ret;
204 struct regulator_config config = {};
205
206 ret = max14577_regulator_dt_parse_pdata(pdev);
207 if (ret)
208 return ret;
209
210 config.dev = &pdev->dev;
211 config.regmap = max14577->regmap;
212
213 for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
214 struct regulator_dev *regulator;
215 /*
216 * Index of supported_regulators[] is also the id and must
217 * match index of pdata->regulators[].
218 */
219 if (pdata && pdata->regulators) {
220 config.init_data = pdata->regulators[i].initdata;
221 config.of_node = pdata->regulators[i].of_node;
222 } else {
223 config.init_data = match_init_data(i);
224 config.of_node = match_of_node(i);
225 }
226
227 regulator = devm_regulator_register(&pdev->dev,
228 &supported_regulators[i], &config);
229 if (IS_ERR(regulator)) {
230 ret = PTR_ERR(regulator);
231 dev_err(&pdev->dev,
232 "Regulator init failed for ID %d with error: %d\n",
233 i, ret);
234 return ret;
235 }
236 }
237
238 return ret;
239}
240
241static struct platform_driver max14577_regulator_driver = {
242 .driver = {
243 .owner = THIS_MODULE,
244 .name = "max14577-regulator",
245 },
246 .probe = max14577_regulator_probe,
247};
248
249static int __init max14577_regulator_init(void)
250{
251 BUILD_BUG_ON(MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START +
252 MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP * 0xf !=
253 MAX14577_REGULATOR_CURRENT_LIMIT_MAX);
254 BUILD_BUG_ON(ARRAY_SIZE(supported_regulators) != MAX14577_REG_MAX);
255
256 return platform_driver_register(&max14577_regulator_driver);
257}
258subsys_initcall(max14577_regulator_init);
259
260static void __exit max14577_regulator_exit(void)
261{
262 platform_driver_unregister(&max14577_regulator_driver);
263}
264module_exit(max14577_regulator_exit);
265
266MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
267MODULE_DESCRIPTION("MAXIM 14577 regulator driver");
268MODULE_LICENSE("GPL");
Axel Lin9f45a3d2013-12-23 14:50:53 +0800269MODULE_ALIAS("platform:max14577-regulator");