Version:  2.0.40 2.2.26 2.4.37 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17

Linux/drivers/regulator/max14577.c

  1 /*
  2  * max14577.c - Regulator driver for the Maxim 14577/77836
  3  *
  4  * Copyright (C) 2013,2014 Samsung Electronics
  5  * 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 
 25 /*
 26  * Valid limits of current for max14577 and max77836 chargers.
 27  * They must correspond to MBCICHWRCL and MBCICHWRCH fields in CHGCTRL4
 28  * register for given chipset.
 29  */
 30 struct maxim_charger_current {
 31         /* Minimal current, set in CHGCTRL4/MBCICHWRCL, uA */
 32         unsigned int min;
 33         /*
 34          * Minimal current when high setting is active,
 35          * set in CHGCTRL4/MBCICHWRCH, uA
 36          */
 37         unsigned int high_start;
 38         /* Value of one step in high setting, uA */
 39         unsigned int high_step;
 40         /* Maximum current of high setting, uA */
 41         unsigned int max;
 42 };
 43 
 44 /* Table of valid charger currents for different Maxim chipsets */
 45 static const struct maxim_charger_current maxim_charger_currents[] = {
 46         [MAXIM_DEVICE_TYPE_UNKNOWN] = { 0, 0, 0, 0 },
 47         [MAXIM_DEVICE_TYPE_MAX14577] = {
 48                 .min            = MAX14577_REGULATOR_CURRENT_LIMIT_MIN,
 49                 .high_start     = MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START,
 50                 .high_step      = MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP,
 51                 .max            = MAX14577_REGULATOR_CURRENT_LIMIT_MAX,
 52         },
 53         [MAXIM_DEVICE_TYPE_MAX77836] = {
 54                 .min            = MAX77836_REGULATOR_CURRENT_LIMIT_MIN,
 55                 .high_start     = MAX77836_REGULATOR_CURRENT_LIMIT_HIGH_START,
 56                 .high_step      = MAX77836_REGULATOR_CURRENT_LIMIT_HIGH_STEP,
 57                 .max            = MAX77836_REGULATOR_CURRENT_LIMIT_MAX,
 58         },
 59 };
 60 
 61 static int max14577_reg_is_enabled(struct regulator_dev *rdev)
 62 {
 63         int rid = rdev_get_id(rdev);
 64         struct regmap *rmap = rdev->regmap;
 65         u8 reg_data;
 66 
 67         switch (rid) {
 68         case MAX14577_CHARGER:
 69                 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
 70                 if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
 71                         return 0;
 72                 max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
 73                 if ((reg_data & STATUS3_CGMBC_MASK) == 0)
 74                         return 0;
 75                 /* MBCHOSTEN and CGMBC are on */
 76                 return 1;
 77         default:
 78                 return -EINVAL;
 79         }
 80 }
 81 
 82 static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
 83 {
 84         u8 reg_data;
 85         struct regmap *rmap = rdev->regmap;
 86         struct max14577 *max14577 = rdev_get_drvdata(rdev);
 87         const struct maxim_charger_current *limits =
 88                 &maxim_charger_currents[max14577->dev_type];
 89 
 90         if (rdev_get_id(rdev) != MAX14577_CHARGER)
 91                 return -EINVAL;
 92 
 93         max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, &reg_data);
 94 
 95         if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
 96                 return limits->min;
 97 
 98         reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
 99                         CHGCTRL4_MBCICHWRCH_SHIFT);
100         return limits->high_start + reg_data * limits->high_step;
101 }
102 
103 static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
104                 int min_uA, int max_uA)
105 {
106         int i, current_bits = 0xf;
107         u8 reg_data;
108         struct max14577 *max14577 = rdev_get_drvdata(rdev);
109         const struct maxim_charger_current *limits =
110                 &maxim_charger_currents[max14577->dev_type];
111 
112         if (rdev_get_id(rdev) != MAX14577_CHARGER)
113                 return -EINVAL;
114 
115         if (min_uA > limits->max || max_uA < limits->min)
116                 return -EINVAL;
117 
118         if (max_uA < limits->high_start) {
119                 /*
120                  * Less than high_start,
121                  * so set the minimal current (turn only Low Bit off)
122                  */
123                 u8 reg_data = 0x0 << CHGCTRL4_MBCICHWRCL_SHIFT;
124                 return max14577_update_reg(rdev->regmap,
125                                 MAX14577_CHG_REG_CHG_CTRL4,
126                                 CHGCTRL4_MBCICHWRCL_MASK, reg_data);
127         }
128 
129         /*
130          * max_uA is in range: <high_start, inifinite>, so search for
131          * valid current starting from maximum current.
132          */
133         for (i = limits->max; i >= limits->high_start; i -= limits->high_step) {
134                 if (i <= max_uA)
135                         break;
136                 current_bits--;
137         }
138         BUG_ON(current_bits < 0); /* Cannot happen */
139 
140         /* Turn Low Bit on (use range high_start-max)... */
141         reg_data = 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT;
142         /* and set proper High Bits */
143         reg_data |= current_bits << CHGCTRL4_MBCICHWRCH_SHIFT;
144 
145         return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
146                         CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
147                         reg_data);
148 }
149 
150 static struct regulator_ops max14577_safeout_ops = {
151         .is_enabled             = regulator_is_enabled_regmap,
152         .enable                 = regulator_enable_regmap,
153         .disable                = regulator_disable_regmap,
154         .list_voltage           = regulator_list_voltage_linear,
155 };
156 
157 static struct regulator_ops max14577_charger_ops = {
158         .is_enabled             = max14577_reg_is_enabled,
159         .enable                 = regulator_enable_regmap,
160         .disable                = regulator_disable_regmap,
161         .get_current_limit      = max14577_reg_get_current_limit,
162         .set_current_limit      = max14577_reg_set_current_limit,
163 };
164 
165 static const struct regulator_desc max14577_supported_regulators[] = {
166         [MAX14577_SAFEOUT] = {
167                 .name           = "SAFEOUT",
168                 .id             = MAX14577_SAFEOUT,
169                 .ops            = &max14577_safeout_ops,
170                 .type           = REGULATOR_VOLTAGE,
171                 .owner          = THIS_MODULE,
172                 .n_voltages     = 1,
173                 .min_uV         = MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
174                 .enable_reg     = MAX14577_REG_CONTROL2,
175                 .enable_mask    = CTRL2_SFOUTORD_MASK,
176         },
177         [MAX14577_CHARGER] = {
178                 .name           = "CHARGER",
179                 .id             = MAX14577_CHARGER,
180                 .ops            = &max14577_charger_ops,
181                 .type           = REGULATOR_CURRENT,
182                 .owner          = THIS_MODULE,
183                 .enable_reg     = MAX14577_CHG_REG_CHG_CTRL2,
184                 .enable_mask    = CHGCTRL2_MBCHOSTEN_MASK,
185         },
186 };
187 
188 static struct regulator_ops max77836_ldo_ops = {
189         .is_enabled             = regulator_is_enabled_regmap,
190         .enable                 = regulator_enable_regmap,
191         .disable                = regulator_disable_regmap,
192         .list_voltage           = regulator_list_voltage_linear,
193         .map_voltage            = regulator_map_voltage_linear,
194         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
195         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
196         /* TODO: add .set_suspend_mode */
197 };
198 
199 static const struct regulator_desc max77836_supported_regulators[] = {
200         [MAX14577_SAFEOUT] = {
201                 .name           = "SAFEOUT",
202                 .id             = MAX14577_SAFEOUT,
203                 .ops            = &max14577_safeout_ops,
204                 .type           = REGULATOR_VOLTAGE,
205                 .owner          = THIS_MODULE,
206                 .n_voltages     = 1,
207                 .min_uV         = MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
208                 .enable_reg     = MAX14577_REG_CONTROL2,
209                 .enable_mask    = CTRL2_SFOUTORD_MASK,
210         },
211         [MAX14577_CHARGER] = {
212                 .name           = "CHARGER",
213                 .id             = MAX14577_CHARGER,
214                 .ops            = &max14577_charger_ops,
215                 .type           = REGULATOR_CURRENT,
216                 .owner          = THIS_MODULE,
217                 .enable_reg     = MAX14577_CHG_REG_CHG_CTRL2,
218                 .enable_mask    = CHGCTRL2_MBCHOSTEN_MASK,
219         },
220         [MAX77836_LDO1] = {
221                 .name           = "LDO1",
222                 .id             = MAX77836_LDO1,
223                 .ops            = &max77836_ldo_ops,
224                 .type           = REGULATOR_VOLTAGE,
225                 .owner          = THIS_MODULE,
226                 .n_voltages     = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM,
227                 .min_uV         = MAX77836_REGULATOR_LDO_VOLTAGE_MIN,
228                 .uV_step        = MAX77836_REGULATOR_LDO_VOLTAGE_STEP,
229                 .enable_reg     = MAX77836_LDO_REG_CNFG1_LDO1,
230                 .enable_mask    = MAX77836_CNFG1_LDO_PWRMD_MASK,
231                 .vsel_reg       = MAX77836_LDO_REG_CNFG1_LDO1,
232                 .vsel_mask      = MAX77836_CNFG1_LDO_TV_MASK,
233         },
234         [MAX77836_LDO2] = {
235                 .name           = "LDO2",
236                 .id             = MAX77836_LDO2,
237                 .ops            = &max77836_ldo_ops,
238                 .type           = REGULATOR_VOLTAGE,
239                 .owner          = THIS_MODULE,
240                 .n_voltages     = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM,
241                 .min_uV         = MAX77836_REGULATOR_LDO_VOLTAGE_MIN,
242                 .uV_step        = MAX77836_REGULATOR_LDO_VOLTAGE_STEP,
243                 .enable_reg     = MAX77836_LDO_REG_CNFG1_LDO2,
244                 .enable_mask    = MAX77836_CNFG1_LDO_PWRMD_MASK,
245                 .vsel_reg       = MAX77836_LDO_REG_CNFG1_LDO2,
246                 .vsel_mask      = MAX77836_CNFG1_LDO_TV_MASK,
247         },
248 };
249 
250 #ifdef CONFIG_OF
251 static struct of_regulator_match max14577_regulator_matches[] = {
252         { .name = "SAFEOUT", },
253         { .name = "CHARGER", },
254 };
255 
256 static struct of_regulator_match max77836_regulator_matches[] = {
257         { .name = "SAFEOUT", },
258         { .name = "CHARGER", },
259         { .name = "LDO1", },
260         { .name = "LDO2", },
261 };
262 
263 static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev,
264                 enum maxim_device_type dev_type)
265 {
266         int ret;
267         struct device_node *np;
268         struct of_regulator_match *regulator_matches;
269         unsigned int regulator_matches_size;
270 
271         np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
272         if (!np) {
273                 dev_err(&pdev->dev, "Failed to get child OF node for regulators\n");
274                 return -EINVAL;
275         }
276 
277         switch (dev_type) {
278         case MAXIM_DEVICE_TYPE_MAX77836:
279                 regulator_matches = max77836_regulator_matches;
280                 regulator_matches_size = ARRAY_SIZE(max77836_regulator_matches);
281                 break;
282         case MAXIM_DEVICE_TYPE_MAX14577:
283         default:
284                 regulator_matches = max14577_regulator_matches;
285                 regulator_matches_size = ARRAY_SIZE(max14577_regulator_matches);
286         }
287 
288         ret = of_regulator_match(&pdev->dev, np, regulator_matches,
289                         regulator_matches_size);
290         if (ret < 0)
291                 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n", ret);
292         else
293                 ret = 0;
294 
295         of_node_put(np);
296 
297         return ret;
298 }
299 
300 static inline struct regulator_init_data *match_init_data(int index,
301                 enum maxim_device_type dev_type)
302 {
303         switch (dev_type) {
304         case MAXIM_DEVICE_TYPE_MAX77836:
305                 return max77836_regulator_matches[index].init_data;
306 
307         case MAXIM_DEVICE_TYPE_MAX14577:
308         default:
309                 return max14577_regulator_matches[index].init_data;
310         }
311 }
312 
313 static inline struct device_node *match_of_node(int index,
314                 enum maxim_device_type dev_type)
315 {
316         switch (dev_type) {
317         case MAXIM_DEVICE_TYPE_MAX77836:
318                 return max77836_regulator_matches[index].of_node;
319 
320         case MAXIM_DEVICE_TYPE_MAX14577:
321         default:
322                 return max14577_regulator_matches[index].of_node;
323         }
324 }
325 #else /* CONFIG_OF */
326 static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev,
327                 enum maxim_device_type dev_type)
328 {
329         return 0;
330 }
331 static inline struct regulator_init_data *match_init_data(int index,
332                 enum maxim_device_type dev_type)
333 {
334         return NULL;
335 }
336 
337 static inline struct device_node *match_of_node(int index,
338                 enum maxim_device_type dev_type)
339 {
340         return NULL;
341 }
342 #endif /* CONFIG_OF */
343 
344 /**
345  * Registers for regulators of max77836 use different I2C slave addresses so
346  * different regmaps must be used for them.
347  *
348  * Returns proper regmap for accessing regulator passed by id.
349  */
350 static struct regmap *max14577_get_regmap(struct max14577 *max14577,
351                 int reg_id)
352 {
353         switch (max14577->dev_type) {
354         case MAXIM_DEVICE_TYPE_MAX77836:
355                 switch (reg_id) {
356                 case MAX77836_SAFEOUT ... MAX77836_CHARGER:
357                         return max14577->regmap;
358                 default:
359                         /* MAX77836_LDO1 ... MAX77836_LDO2 */
360                         return max14577->regmap_pmic;
361                 }
362 
363         case MAXIM_DEVICE_TYPE_MAX14577:
364         default:
365                 return max14577->regmap;
366         }
367 }
368 
369 static int max14577_regulator_probe(struct platform_device *pdev)
370 {
371         struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
372         struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
373         int i, ret;
374         struct regulator_config config = {};
375         const struct regulator_desc *supported_regulators;
376         unsigned int supported_regulators_size;
377         enum maxim_device_type dev_type = max14577->dev_type;
378 
379         ret = max14577_regulator_dt_parse_pdata(pdev, dev_type);
380         if (ret)
381                 return ret;
382 
383         switch (dev_type) {
384         case MAXIM_DEVICE_TYPE_MAX77836:
385                 supported_regulators = max77836_supported_regulators;
386                 supported_regulators_size = ARRAY_SIZE(max77836_supported_regulators);
387                 break;
388         case MAXIM_DEVICE_TYPE_MAX14577:
389         default:
390                 supported_regulators = max14577_supported_regulators;
391                 supported_regulators_size = ARRAY_SIZE(max14577_supported_regulators);
392         }
393 
394         config.dev = &pdev->dev;
395         config.driver_data = max14577;
396 
397         for (i = 0; i < supported_regulators_size; i++) {
398                 struct regulator_dev *regulator;
399                 /*
400                  * Index of supported_regulators[] is also the id and must
401                  * match index of pdata->regulators[].
402                  */
403                 if (pdata && pdata->regulators) {
404                         config.init_data = pdata->regulators[i].initdata;
405                         config.of_node = pdata->regulators[i].of_node;
406                 } else {
407                         config.init_data = match_init_data(i, dev_type);
408                         config.of_node = match_of_node(i, dev_type);
409                 }
410                 config.regmap = max14577_get_regmap(max14577,
411                                 supported_regulators[i].id);
412 
413                 regulator = devm_regulator_register(&pdev->dev,
414                                 &supported_regulators[i], &config);
415                 if (IS_ERR(regulator)) {
416                         ret = PTR_ERR(regulator);
417                         dev_err(&pdev->dev,
418                                         "Regulator init failed for %d/%s with error: %d\n",
419                                         i, supported_regulators[i].name, ret);
420                         return ret;
421                 }
422         }
423 
424         return ret;
425 }
426 
427 static const struct platform_device_id max14577_regulator_id[] = {
428         { "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577, },
429         { "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836, },
430         { }
431 };
432 MODULE_DEVICE_TABLE(platform, max14577_regulator_id);
433 
434 static struct platform_driver max14577_regulator_driver = {
435         .driver = {
436                    .owner = THIS_MODULE,
437                    .name = "max14577-regulator",
438                    },
439         .probe          = max14577_regulator_probe,
440         .id_table       = max14577_regulator_id,
441 };
442 
443 static int __init max14577_regulator_init(void)
444 {
445         /* Check for valid values for charger */
446         BUILD_BUG_ON(MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START +
447                         MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP * 0xf !=
448                         MAX14577_REGULATOR_CURRENT_LIMIT_MAX);
449         BUILD_BUG_ON(MAX77836_REGULATOR_CURRENT_LIMIT_HIGH_START +
450                         MAX77836_REGULATOR_CURRENT_LIMIT_HIGH_STEP * 0xf !=
451                         MAX77836_REGULATOR_CURRENT_LIMIT_MAX);
452         /* Valid charger current values must be provided for each chipset */
453         BUILD_BUG_ON(ARRAY_SIZE(maxim_charger_currents) != MAXIM_DEVICE_TYPE_NUM);
454 
455         BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators) != MAX14577_REGULATOR_NUM);
456         BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators) != MAX77836_REGULATOR_NUM);
457 
458         BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN +
459                         (MAX77836_REGULATOR_LDO_VOLTAGE_STEP *
460                           (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM - 1)) !=
461                         MAX77836_REGULATOR_LDO_VOLTAGE_MAX);
462 
463         return platform_driver_register(&max14577_regulator_driver);
464 }
465 subsys_initcall(max14577_regulator_init);
466 
467 static void __exit max14577_regulator_exit(void)
468 {
469         platform_driver_unregister(&max14577_regulator_driver);
470 }
471 module_exit(max14577_regulator_exit);
472 
473 MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
474 MODULE_DESCRIPTION("Maxim 14577/77836 regulator driver");
475 MODULE_LICENSE("GPL");
476 MODULE_ALIAS("platform:max14577-regulator");
477 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us