2 * max14577.c - Regulator driver for the Maxim 14577
4 * Copyright (C) 2013 Samsung Electronics
5 * Krzysztof Kozlowski <k.kozlowski@samsung.com>
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.
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.
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>
25 struct max14577_regulator
{
27 struct max14577
*max14577
;
28 struct regulator_dev
**regulators
;
31 static int max14577_reg_is_enabled(struct regulator_dev
*rdev
)
33 int rid
= rdev_get_id(rdev
);
34 struct regmap
*rmap
= rdev
->regmap
;
38 case MAX14577_CHARGER
:
39 max14577_read_reg(rmap
, MAX14577_CHG_REG_CHG_CTRL2
, ®_data
);
40 if ((reg_data
& CHGCTRL2_MBCHOSTEN_MASK
) == 0)
42 max14577_read_reg(rmap
, MAX14577_CHG_REG_STATUS3
, ®_data
);
43 if ((reg_data
& STATUS3_CGMBC_MASK
) == 0)
45 /* MBCHOSTEN and CGMBC are on */
52 static int max14577_reg_get_current_limit(struct regulator_dev
*rdev
)
55 struct regmap
*rmap
= rdev
->regmap
;
57 if (rdev_get_id(rdev
) != MAX14577_CHARGER
)
60 max14577_read_reg(rmap
, MAX14577_CHG_REG_CHG_CTRL4
, ®_data
);
62 if ((reg_data
& CHGCTRL4_MBCICHWRCL_MASK
) == 0)
63 return MAX14577_REGULATOR_CURRENT_LIMIT_MIN
;
65 reg_data
= ((reg_data
& CHGCTRL4_MBCICHWRCH_MASK
) >>
66 CHGCTRL4_MBCICHWRCH_SHIFT
);
67 return MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START
+
68 reg_data
* MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP
;
71 static int max14577_reg_set_current_limit(struct regulator_dev
*rdev
,
72 int min_uA
, int max_uA
)
74 int i
, current_bits
= 0xf;
77 if (rdev_get_id(rdev
) != MAX14577_CHARGER
)
80 if (min_uA
> MAX14577_REGULATOR_CURRENT_LIMIT_MAX
||
81 max_uA
< MAX14577_REGULATOR_CURRENT_LIMIT_MIN
)
84 if (max_uA
< MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START
) {
85 /* Less than 200 mA, so set 90mA (turn only Low Bit off) */
86 u8 reg_data
= 0x0 << CHGCTRL4_MBCICHWRCL_SHIFT
;
87 return max14577_update_reg(rdev
->regmap
,
88 MAX14577_CHG_REG_CHG_CTRL4
,
89 CHGCTRL4_MBCICHWRCL_MASK
, reg_data
);
92 /* max_uA is in range: <LIMIT_HIGH_START, inifinite>, so search for
93 * valid current starting from LIMIT_MAX. */
94 for (i
= MAX14577_REGULATOR_CURRENT_LIMIT_MAX
;
95 i
>= MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START
;
96 i
-= MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP
) {
101 BUG_ON(current_bits
< 0); /* Cannot happen */
102 /* Turn Low Bit on (use range 200mA-950 mA) */
103 reg_data
= 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT
;
104 /* and set proper High Bits */
105 reg_data
|= current_bits
<< CHGCTRL4_MBCICHWRCH_SHIFT
;
107 return max14577_update_reg(rdev
->regmap
, MAX14577_CHG_REG_CHG_CTRL4
,
108 CHGCTRL4_MBCICHWRCL_MASK
| CHGCTRL4_MBCICHWRCH_MASK
,
112 static struct regulator_ops max14577_safeout_ops
= {
113 .is_enabled
= regulator_is_enabled_regmap
,
114 .enable
= regulator_enable_regmap
,
115 .disable
= regulator_disable_regmap
,
116 .list_voltage
= regulator_list_voltage_linear
,
119 static struct regulator_ops max14577_charger_ops
= {
120 .is_enabled
= max14577_reg_is_enabled
,
121 .enable
= regulator_enable_regmap
,
122 .disable
= regulator_disable_regmap
,
123 .get_current_limit
= max14577_reg_get_current_limit
,
124 .set_current_limit
= max14577_reg_set_current_limit
,
127 static const struct regulator_desc supported_regulators
[] = {
128 [MAX14577_SAFEOUT
] = {
130 .id
= MAX14577_SAFEOUT
,
131 .ops
= &max14577_safeout_ops
,
132 .type
= REGULATOR_VOLTAGE
,
133 .owner
= THIS_MODULE
,
135 .min_uV
= MAX14577_REGULATOR_SAFEOUT_VOLTAGE
,
136 .enable_reg
= MAX14577_REG_CONTROL2
,
137 .enable_mask
= CTRL2_SFOUTORD_MASK
,
139 [MAX14577_CHARGER
] = {
141 .id
= MAX14577_CHARGER
,
142 .ops
= &max14577_charger_ops
,
143 .type
= REGULATOR_CURRENT
,
144 .owner
= THIS_MODULE
,
145 .enable_reg
= MAX14577_CHG_REG_CHG_CTRL2
,
146 .enable_mask
= CHGCTRL2_MBCHOSTEN_MASK
,
151 static struct of_regulator_match max14577_regulator_matches
[] = {
152 { .name
= "SAFEOUT", },
153 { .name
= "CHARGER", },
156 static int max14577_regulator_dt_parse_pdata(struct platform_device
*pdev
)
159 struct device_node
*np
;
161 np
= of_get_child_by_name(pdev
->dev
.parent
->of_node
, "regulators");
163 dev_err(&pdev
->dev
, "Failed to get child OF node for regulators\n");
167 ret
= of_regulator_match(&pdev
->dev
, np
, max14577_regulator_matches
,
170 dev_err(&pdev
->dev
, "Error parsing regulator init data: %d\n", ret
);
178 static inline struct regulator_init_data
*match_init_data(int index
)
180 return max14577_regulator_matches
[index
].init_data
;
183 static inline struct device_node
*match_of_node(int index
)
185 return max14577_regulator_matches
[index
].of_node
;
187 #else /* CONFIG_OF */
188 static int max14577_regulator_dt_parse_pdata(struct platform_device
*pdev
)
192 static inline struct regulator_init_data
*match_init_data(int index
)
197 static inline struct device_node
*match_of_node(int index
)
201 #endif /* CONFIG_OF */
204 static int max14577_regulator_probe(struct platform_device
*pdev
)
206 struct max14577
*max14577
= dev_get_drvdata(pdev
->dev
.parent
);
207 struct max14577_platform_data
*pdata
= dev_get_platdata(max14577
->dev
);
209 struct regulator_config config
= {};
211 ret
= max14577_regulator_dt_parse_pdata(pdev
);
215 config
.dev
= &pdev
->dev
;
216 config
.regmap
= max14577
->regmap
;
218 for (i
= 0; i
< ARRAY_SIZE(supported_regulators
); i
++) {
219 struct regulator_dev
*regulator
;
221 * Index of supported_regulators[] is also the id and must
222 * match index of pdata->regulators[].
224 if (pdata
&& pdata
->regulators
) {
225 config
.init_data
= pdata
->regulators
[i
].initdata
;
226 config
.of_node
= pdata
->regulators
[i
].of_node
;
228 config
.init_data
= match_init_data(i
);
229 config
.of_node
= match_of_node(i
);
232 regulator
= devm_regulator_register(&pdev
->dev
,
233 &supported_regulators
[i
], &config
);
234 if (IS_ERR(regulator
)) {
235 ret
= PTR_ERR(regulator
);
237 "Regulator init failed for ID %d with error: %d\n",
246 static struct platform_driver max14577_regulator_driver
= {
248 .owner
= THIS_MODULE
,
249 .name
= "max14577-regulator",
251 .probe
= max14577_regulator_probe
,
254 static int __init
max14577_regulator_init(void)
256 BUILD_BUG_ON(MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START
+
257 MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP
* 0xf !=
258 MAX14577_REGULATOR_CURRENT_LIMIT_MAX
);
259 BUILD_BUG_ON(ARRAY_SIZE(supported_regulators
) != MAX14577_REG_MAX
);
261 return platform_driver_register(&max14577_regulator_driver
);
263 subsys_initcall(max14577_regulator_init
);
265 static void __exit
max14577_regulator_exit(void)
267 platform_driver_unregister(&max14577_regulator_driver
);
269 module_exit(max14577_regulator_exit
);
271 MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
272 MODULE_DESCRIPTION("MAXIM 14577 regulator driver");
273 MODULE_LICENSE("GPL");
274 MODULE_ALIAS("platform:max14577-regulator");