]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/regulator/max77693.c
regulator: tps65217: Allow missing init_data for diagnostics
[karo-tx-linux.git] / drivers / regulator / max77693.c
1 /*
2  * max77693.c - Regulator driver for the Maxim 77693
3  *
4  * Copyright (C) 2013 Samsung Electronics
5  * Jonghwa Lee <jonghwa3.lee@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  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * This driver is based on max77686.c
22  */
23
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/platform_device.h>
27 #include <linux/module.h>
28 #include <linux/export.h>
29 #include <linux/regulator/driver.h>
30 #include <linux/regulator/machine.h>
31 #include <linux/mfd/max77693.h>
32 #include <linux/mfd/max77693-private.h>
33 #include <linux/regulator/of_regulator.h>
34
35 #define CHGIN_ILIM_STEP_20mA                    20000
36
37 struct max77693_pmic_dev {
38         struct device *dev;
39         struct max77693_dev *iodev;
40         int num_regulators;
41         struct regulator_dev **rdev;
42 };
43
44 /* CHARGER regulator ops */
45 /* CHARGER regulator uses two bits for enabling */
46 static int max77693_chg_is_enabled(struct regulator_dev *rdev)
47 {
48         int ret;
49         u8 val;
50
51         ret = max77693_read_reg(rdev->regmap, rdev->desc->enable_reg, &val);
52         if (ret)
53                 return ret;
54
55         return (val & rdev->desc->enable_mask) == rdev->desc->enable_mask;
56 }
57
58 /*
59  * CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
60  * 0x00, 0x01, 0x2, 0x03        = 60 mA
61  * 0x04 ~ 0x7E                  = (60 + (X - 3) * 20) mA
62  */
63 static int max77693_chg_get_current_limit(struct regulator_dev *rdev)
64 {
65         unsigned int chg_min_uA = rdev->constraints->min_uA;
66         unsigned int chg_max_uA = rdev->constraints->max_uA;
67         u8 reg, sel;
68         unsigned int val;
69         int ret;
70
71         ret = max77693_read_reg(rdev->regmap,
72                                 MAX77693_CHG_REG_CHG_CNFG_09, &reg);
73         if (ret < 0)
74                 return ret;
75
76         sel = reg & CHG_CNFG_09_CHGIN_ILIM_MASK;
77
78         /* the first four codes for charger current are all 60mA */
79         if (sel <= 3)
80                 sel = 0;
81         else
82                 sel -= 3;
83
84         val = chg_min_uA + CHGIN_ILIM_STEP_20mA * sel;
85         if (val > chg_max_uA)
86                 return -EINVAL;
87
88         return val;
89 }
90
91 static int max77693_chg_set_current_limit(struct regulator_dev *rdev,
92                                                 int min_uA, int max_uA)
93 {
94         unsigned int chg_min_uA = rdev->constraints->min_uA;
95         int sel = 0;
96
97         while (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel < min_uA)
98                 sel++;
99
100         if (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel > max_uA)
101                 return -EINVAL;
102
103         /* the first four codes for charger current are all 60mA */
104         sel += 3;
105
106         return max77693_write_reg(rdev->regmap,
107                                 MAX77693_CHG_REG_CHG_CNFG_09, sel);
108 }
109 /* end of CHARGER regulator ops */
110
111 static const unsigned int max77693_safeout_table[] = {
112         4850000,
113         4900000,
114         4950000,
115         3300000,
116 };
117
118 static struct regulator_ops max77693_safeout_ops = {
119         .list_voltage           = regulator_list_voltage_table,
120         .is_enabled             = regulator_is_enabled_regmap,
121         .enable                 = regulator_enable_regmap,
122         .disable                = regulator_disable_regmap,
123         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
124         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
125 };
126
127 static struct regulator_ops max77693_charger_ops = {
128         .is_enabled             = max77693_chg_is_enabled,
129         .enable                 = regulator_enable_regmap,
130         .disable                = regulator_disable_regmap,
131         .get_current_limit      = max77693_chg_get_current_limit,
132         .set_current_limit      = max77693_chg_set_current_limit,
133 };
134
135 #define regulator_desc_esafeout(_num)   {                       \
136         .name           = "ESAFEOUT"#_num,                      \
137         .id             = MAX77693_ESAFEOUT##_num,              \
138         .n_voltages     = 4,                                    \
139         .ops            = &max77693_safeout_ops,                \
140         .type           = REGULATOR_VOLTAGE,                    \
141         .owner          = THIS_MODULE,                          \
142         .volt_table     = max77693_safeout_table,               \
143         .vsel_reg       = MAX77693_CHG_REG_SAFEOUT_CTRL,        \
144         .vsel_mask      = SAFEOUT_CTRL_SAFEOUT##_num##_MASK,    \
145         .enable_reg     = MAX77693_CHG_REG_SAFEOUT_CTRL,        \
146         .enable_mask    = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \
147 }
148
149 static struct regulator_desc regulators[] = {
150         regulator_desc_esafeout(1),
151         regulator_desc_esafeout(2),
152         {
153                 .name = "CHARGER",
154                 .id = MAX77693_CHARGER,
155                 .ops = &max77693_charger_ops,
156                 .type = REGULATOR_CURRENT,
157                 .owner = THIS_MODULE,
158                 .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
159                 .enable_mask = CHG_CNFG_00_CHG_MASK |
160                                 CHG_CNFG_00_BUCK_MASK,
161         },
162 };
163
164 #ifdef CONFIG_OF
165 static int max77693_pmic_dt_parse_rdata(struct device *dev,
166                                         struct max77693_regulator_data **rdata)
167 {
168         struct device_node *np;
169         struct of_regulator_match *rmatch;
170         struct max77693_regulator_data *tmp;
171         int i, matched = 0;
172
173         np = of_find_node_by_name(dev->parent->of_node, "regulators");
174         if (!np)
175                 return -EINVAL;
176
177         rmatch = devm_kzalloc(dev,
178                  sizeof(*rmatch) * ARRAY_SIZE(regulators), GFP_KERNEL);
179         if (!rmatch)
180                 return -ENOMEM;
181
182         for (i = 0; i < ARRAY_SIZE(regulators); i++)
183                 rmatch[i].name = regulators[i].name;
184
185         matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(regulators));
186         if (matched <= 0)
187                 return matched;
188         *rdata = devm_kzalloc(dev, sizeof(**rdata) * matched, GFP_KERNEL);
189         if (!(*rdata))
190                 return -ENOMEM;
191
192         tmp = *rdata;
193
194         for (i = 0; i < matched; i++) {
195                 tmp->initdata = rmatch[i].init_data;
196                 tmp->of_node = rmatch[i].of_node;
197                 tmp->id = regulators[i].id;
198                 tmp++;
199         }
200
201         return matched;
202 }
203 #else
204 static int max77693_pmic_dt_parse_rdata(struct device *dev,
205                                         struct max77693_regulator_data **rdata)
206 {
207         return 0;
208 }
209 #endif /* CONFIG_OF */
210
211 static int max77693_pmic_init_rdata(struct device *dev,
212                                     struct max77693_regulator_data **rdata)
213 {
214         struct max77693_platform_data *pdata;
215         int num_regulators = 0;
216
217         pdata = dev_get_platdata(dev->parent);
218         if (pdata) {
219                 *rdata = pdata->regulators;
220                 num_regulators = pdata->num_regulators;
221         }
222
223         if (!(*rdata) && dev->parent->of_node)
224                 num_regulators = max77693_pmic_dt_parse_rdata(dev, rdata);
225
226         return num_regulators;
227 }
228
229 static int max77693_pmic_probe(struct platform_device *pdev)
230 {
231         struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
232         struct max77693_pmic_dev *max77693_pmic;
233         struct max77693_regulator_data *rdata = NULL;
234         int num_rdata, i;
235         struct regulator_config config;
236
237         num_rdata = max77693_pmic_init_rdata(&pdev->dev, &rdata);
238         if (!rdata || num_rdata <= 0) {
239                 dev_err(&pdev->dev, "No init data supplied.\n");
240                 return -ENODEV;
241         }
242
243         max77693_pmic = devm_kzalloc(&pdev->dev,
244                                 sizeof(struct max77693_pmic_dev),
245                                 GFP_KERNEL);
246         if (!max77693_pmic)
247                 return -ENOMEM;
248
249         max77693_pmic->rdev = devm_kzalloc(&pdev->dev,
250                                 sizeof(struct regulator_dev *) * num_rdata,
251                                 GFP_KERNEL);
252         if (!max77693_pmic->rdev)
253                 return -ENOMEM;
254
255         max77693_pmic->dev = &pdev->dev;
256         max77693_pmic->iodev = iodev;
257         max77693_pmic->num_regulators = num_rdata;
258
259         config.dev = &pdev->dev;
260         config.regmap = iodev->regmap;
261         config.driver_data = max77693_pmic;
262         platform_set_drvdata(pdev, max77693_pmic);
263
264         for (i = 0; i < max77693_pmic->num_regulators; i++) {
265                 int id = rdata[i].id;
266
267                 config.init_data = rdata[i].initdata;
268                 config.of_node = rdata[i].of_node;
269
270                 max77693_pmic->rdev[i] = devm_regulator_register(&pdev->dev,
271                                                 &regulators[id], &config);
272                 if (IS_ERR(max77693_pmic->rdev[i])) {
273                         dev_err(max77693_pmic->dev,
274                                 "Failed to initialize regulator-%d\n", id);
275                         return PTR_ERR(max77693_pmic->rdev[i]);
276                 }
277         }
278
279         return 0;
280 }
281
282 static const struct platform_device_id max77693_pmic_id[] = {
283         {"max77693-pmic", 0},
284         {},
285 };
286
287 MODULE_DEVICE_TABLE(platform, max77693_pmic_id);
288
289 static struct platform_driver max77693_pmic_driver = {
290         .driver = {
291                    .name = "max77693-pmic",
292                    .owner = THIS_MODULE,
293                    },
294         .probe = max77693_pmic_probe,
295         .id_table = max77693_pmic_id,
296 };
297
298 module_platform_driver(max77693_pmic_driver);
299
300 MODULE_DESCRIPTION("MAXIM MAX77693 regulator driver");
301 MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
302 MODULE_LICENSE("GPL");