blob: f4fd0d3cfa6ed4ea5d8e0c3fa4c38748c13968f8 [file] [log] [blame]
Jaewon Kim9dfffb72015-02-05 15:35:58 +09001/*
2 * max77843.c - Regulator driver for the Maxim MAX77843
3 *
4 * Copyright (C) 2015 Samsung Electronics
5 * Author: Jaewon Kim <jaewon02.kim@samsung.com>
6 * Author: Beomho Seo <beomho.seo@samsung.com>
7 *
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
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/regulator/driver.h>
17#include <linux/regulator/machine.h>
18#include <linux/mfd/max77843-private.h>
19#include <linux/regulator/of_regulator.h>
20
21enum max77843_regulator_type {
22 MAX77843_SAFEOUT1 = 0,
23 MAX77843_SAFEOUT2,
24 MAX77843_CHARGER,
25
26 MAX77843_NUM,
27};
28
29static const unsigned int max77843_safeout_voltage_table[] = {
30 4850000,
31 4900000,
32 4950000,
33 3300000,
34};
35
Jaewon Kim9dfffb72015-02-05 15:35:58 +090036static int max77843_reg_get_current_limit(struct regulator_dev *rdev)
37{
38 struct regmap *regmap = rdev->regmap;
39 unsigned int chg_min_uA = rdev->constraints->min_uA;
40 unsigned int chg_max_uA = rdev->constraints->max_uA;
41 unsigned int val;
42 int ret;
43 unsigned int reg, sel;
44
45 ret = regmap_read(regmap, MAX77843_CHG_REG_CHG_CNFG_02, &reg);
46 if (ret) {
47 dev_err(&rdev->dev, "Failed to read charger register\n");
48 return ret;
49 }
50
51 sel = reg & MAX77843_CHG_FAST_CHG_CURRENT_MASK;
52
53 if (sel < 0x03)
54 sel = 0;
55 else
56 sel -= 2;
57
58 val = chg_min_uA + MAX77843_CHG_FAST_CHG_CURRENT_STEP * sel;
59 if (val > chg_max_uA)
60 return -EINVAL;
61
62 return val;
63}
64
65static int max77843_reg_set_current_limit(struct regulator_dev *rdev,
66 int min_uA, int max_uA)
67{
68 struct regmap *regmap = rdev->regmap;
69 unsigned int chg_min_uA = rdev->constraints->min_uA;
70 int sel = 0;
71
72 while (chg_min_uA + MAX77843_CHG_FAST_CHG_CURRENT_STEP * sel < min_uA)
73 sel++;
74
75 if (chg_min_uA + MAX77843_CHG_FAST_CHG_CURRENT_STEP * sel > max_uA)
76 return -EINVAL;
77
78 sel += 2;
79
80 return regmap_write(regmap, MAX77843_CHG_REG_CHG_CNFG_02, sel);
81}
82
83static struct regulator_ops max77843_charger_ops = {
Axel Lin6986f442015-05-01 14:17:21 +080084 .is_enabled = regulator_is_enabled_regmap,
Jaewon Kim9dfffb72015-02-05 15:35:58 +090085 .enable = regulator_enable_regmap,
86 .disable = regulator_disable_regmap,
87 .get_current_limit = max77843_reg_get_current_limit,
88 .set_current_limit = max77843_reg_set_current_limit,
89};
90
91static struct regulator_ops max77843_regulator_ops = {
92 .is_enabled = regulator_is_enabled_regmap,
93 .enable = regulator_enable_regmap,
94 .disable = regulator_disable_regmap,
95 .list_voltage = regulator_list_voltage_table,
96 .get_voltage_sel = regulator_get_voltage_sel_regmap,
97 .set_voltage_sel = regulator_set_voltage_sel_regmap,
98};
99
Krzysztof Kozlowskiec03c082015-04-18 20:27:18 +0900100#define MAX77843_SAFEOUT(num) { \
101 .name = "SAFEOUT" # num, \
102 .id = MAX77843_SAFEOUT ## num, \
103 .ops = &max77843_regulator_ops, \
104 .of_match = of_match_ptr("SAFEOUT" # num), \
105 .regulators_node = of_match_ptr("regulators"), \
106 .type = REGULATOR_VOLTAGE, \
107 .owner = THIS_MODULE, \
108 .n_voltages = ARRAY_SIZE(max77843_safeout_voltage_table), \
109 .volt_table = max77843_safeout_voltage_table, \
110 .enable_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
111 .enable_mask = MAX77843_REG_SAFEOUTCTRL_ENSAFEOUT ## num, \
112 .vsel_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
113 .vsel_mask = MAX77843_REG_SAFEOUTCTRL_SAFEOUT ## num ## _MASK, \
114}
115
Jaewon Kim9dfffb72015-02-05 15:35:58 +0900116static const struct regulator_desc max77843_supported_regulators[] = {
Krzysztof Kozlowskiec03c082015-04-18 20:27:18 +0900117 [MAX77843_SAFEOUT1] = MAX77843_SAFEOUT(1),
118 [MAX77843_SAFEOUT2] = MAX77843_SAFEOUT(2),
Jaewon Kim9dfffb72015-02-05 15:35:58 +0900119 [MAX77843_CHARGER] = {
120 .name = "CHARGER",
121 .id = MAX77843_CHARGER,
122 .ops = &max77843_charger_ops,
123 .of_match = of_match_ptr("CHARGER"),
124 .regulators_node = of_match_ptr("regulators"),
125 .type = REGULATOR_CURRENT,
126 .owner = THIS_MODULE,
127 .enable_reg = MAX77843_CHG_REG_CHG_CNFG_00,
Axel Linb7c960d2015-05-01 14:16:39 +0800128 .enable_mask = MAX77843_CHG_MASK | MAX77843_CHG_BUCK_MASK,
Axel Lin6986f442015-05-01 14:17:21 +0800129 .enable_val = MAX77843_CHG_MASK | MAX77843_CHG_BUCK_MASK,
Jaewon Kim9dfffb72015-02-05 15:35:58 +0900130 },
131};
132
133static struct regmap *max77843_get_regmap(struct max77843 *max77843, int reg_id)
134{
135 switch (reg_id) {
136 case MAX77843_SAFEOUT1:
137 case MAX77843_SAFEOUT2:
138 return max77843->regmap;
139 case MAX77843_CHARGER:
140 return max77843->regmap_chg;
141 default:
142 return max77843->regmap;
143 }
144}
145
146static int max77843_regulator_probe(struct platform_device *pdev)
147{
148 struct max77843 *max77843 = dev_get_drvdata(pdev->dev.parent);
149 struct regulator_config config = {};
150 int i;
151
152 config.dev = max77843->dev;
153 config.driver_data = max77843;
154
155 for (i = 0; i < ARRAY_SIZE(max77843_supported_regulators); i++) {
156 struct regulator_dev *regulator;
157
158 config.regmap = max77843_get_regmap(max77843,
159 max77843_supported_regulators[i].id);
160
161 regulator = devm_regulator_register(&pdev->dev,
162 &max77843_supported_regulators[i], &config);
163 if (IS_ERR(regulator)) {
164 dev_err(&pdev->dev,
165 "Failed to regiser regulator-%d\n", i);
166 return PTR_ERR(regulator);
167 }
168 }
169
170 return 0;
171}
172
173static const struct platform_device_id max77843_regulator_id[] = {
174 { "max77843-regulator", },
175 { /* sentinel */ },
176};
177
178static struct platform_driver max77843_regulator_driver = {
179 .driver = {
180 .name = "max77843-regulator",
181 },
182 .probe = max77843_regulator_probe,
183 .id_table = max77843_regulator_id,
184};
185
186static int __init max77843_regulator_init(void)
187{
188 return platform_driver_register(&max77843_regulator_driver);
189}
190subsys_initcall(max77843_regulator_init);
191
192static void __exit max77843_regulator_exit(void)
193{
194 platform_driver_unregister(&max77843_regulator_driver);
195}
196module_exit(max77843_regulator_exit);
197
198MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
199MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
200MODULE_DESCRIPTION("Maxim MAX77843 regulator driver");
201MODULE_LICENSE("GPL");