linux/drivers/regulator/max14577.c
<<
>>
Prefs
   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
  25static 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        struct max14577 *max14577 = rdev_get_drvdata(rdev);
  51        const struct maxim_charger_current *limits =
  52                &maxim_charger_currents[max14577->dev_type];
  53
  54        if (rdev_get_id(rdev) != MAX14577_CHARGER)
  55                return -EINVAL;
  56
  57        max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, &reg_data);
  58
  59        if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
  60                return limits->min;
  61
  62        reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
  63                        CHGCTRL4_MBCICHWRCH_SHIFT);
  64        return limits->high_start + reg_data * limits->high_step;
  65}
  66
  67static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
  68                int min_uA, int max_uA)
  69{
  70        u8 reg_data;
  71        int ret;
  72        struct max14577 *max14577 = rdev_get_drvdata(rdev);
  73        const struct maxim_charger_current *limits =
  74                &maxim_charger_currents[max14577->dev_type];
  75
  76        if (rdev_get_id(rdev) != MAX14577_CHARGER)
  77                return -EINVAL;
  78
  79        ret = maxim_charger_calc_reg_current(limits, min_uA, max_uA, &reg_data);
  80        if (ret)
  81                return ret;
  82
  83        return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
  84                        CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
  85                        reg_data);
  86}
  87
  88static struct regulator_ops max14577_safeout_ops = {
  89        .is_enabled             = regulator_is_enabled_regmap,
  90        .enable                 = regulator_enable_regmap,
  91        .disable                = regulator_disable_regmap,
  92        .list_voltage           = regulator_list_voltage_linear,
  93};
  94
  95static struct regulator_ops max14577_charger_ops = {
  96        .is_enabled             = max14577_reg_is_enabled,
  97        .enable                 = regulator_enable_regmap,
  98        .disable                = regulator_disable_regmap,
  99        .get_current_limit      = max14577_reg_get_current_limit,
 100        .set_current_limit      = max14577_reg_set_current_limit,
 101};
 102
 103static const struct regulator_desc max14577_supported_regulators[] = {
 104        [MAX14577_SAFEOUT] = {
 105                .name           = "SAFEOUT",
 106                .id             = MAX14577_SAFEOUT,
 107                .ops            = &max14577_safeout_ops,
 108                .type           = REGULATOR_VOLTAGE,
 109                .owner          = THIS_MODULE,
 110                .n_voltages     = 1,
 111                .min_uV         = MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
 112                .enable_reg     = MAX14577_REG_CONTROL2,
 113                .enable_mask    = CTRL2_SFOUTORD_MASK,
 114        },
 115        [MAX14577_CHARGER] = {
 116                .name           = "CHARGER",
 117                .id             = MAX14577_CHARGER,
 118                .ops            = &max14577_charger_ops,
 119                .type           = REGULATOR_CURRENT,
 120                .owner          = THIS_MODULE,
 121                .enable_reg     = MAX14577_CHG_REG_CHG_CTRL2,
 122                .enable_mask    = CHGCTRL2_MBCHOSTEN_MASK,
 123        },
 124};
 125
 126static struct regulator_ops max77836_ldo_ops = {
 127        .is_enabled             = regulator_is_enabled_regmap,
 128        .enable                 = regulator_enable_regmap,
 129        .disable                = regulator_disable_regmap,
 130        .list_voltage           = regulator_list_voltage_linear,
 131        .map_voltage            = regulator_map_voltage_linear,
 132        .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 133        .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 134        /* TODO: add .set_suspend_mode */
 135};
 136
 137static const struct regulator_desc max77836_supported_regulators[] = {
 138        [MAX14577_SAFEOUT] = {
 139                .name           = "SAFEOUT",
 140                .id             = MAX14577_SAFEOUT,
 141                .ops            = &max14577_safeout_ops,
 142                .type           = REGULATOR_VOLTAGE,
 143                .owner          = THIS_MODULE,
 144                .n_voltages     = 1,
 145                .min_uV         = MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
 146                .enable_reg     = MAX14577_REG_CONTROL2,
 147                .enable_mask    = CTRL2_SFOUTORD_MASK,
 148        },
 149        [MAX14577_CHARGER] = {
 150                .name           = "CHARGER",
 151                .id             = MAX14577_CHARGER,
 152                .ops            = &max14577_charger_ops,
 153                .type           = REGULATOR_CURRENT,
 154                .owner          = THIS_MODULE,
 155                .enable_reg     = MAX14577_CHG_REG_CHG_CTRL2,
 156                .enable_mask    = CHGCTRL2_MBCHOSTEN_MASK,
 157        },
 158        [MAX77836_LDO1] = {
 159                .name           = "LDO1",
 160                .id             = MAX77836_LDO1,
 161                .ops            = &max77836_ldo_ops,
 162                .type           = REGULATOR_VOLTAGE,
 163                .owner          = THIS_MODULE,
 164                .n_voltages     = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM,
 165                .min_uV         = MAX77836_REGULATOR_LDO_VOLTAGE_MIN,
 166                .uV_step        = MAX77836_REGULATOR_LDO_VOLTAGE_STEP,
 167                .enable_reg     = MAX77836_LDO_REG_CNFG1_LDO1,
 168                .enable_mask    = MAX77836_CNFG1_LDO_PWRMD_MASK,
 169                .vsel_reg       = MAX77836_LDO_REG_CNFG1_LDO1,
 170                .vsel_mask      = MAX77836_CNFG1_LDO_TV_MASK,
 171        },
 172        [MAX77836_LDO2] = {
 173                .name           = "LDO2",
 174                .id             = MAX77836_LDO2,
 175                .ops            = &max77836_ldo_ops,
 176                .type           = REGULATOR_VOLTAGE,
 177                .owner          = THIS_MODULE,
 178                .n_voltages     = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM,
 179                .min_uV         = MAX77836_REGULATOR_LDO_VOLTAGE_MIN,
 180                .uV_step        = MAX77836_REGULATOR_LDO_VOLTAGE_STEP,
 181                .enable_reg     = MAX77836_LDO_REG_CNFG1_LDO2,
 182                .enable_mask    = MAX77836_CNFG1_LDO_PWRMD_MASK,
 183                .vsel_reg       = MAX77836_LDO_REG_CNFG1_LDO2,
 184                .vsel_mask      = MAX77836_CNFG1_LDO_TV_MASK,
 185        },
 186};
 187
 188#ifdef CONFIG_OF
 189static struct of_regulator_match max14577_regulator_matches[] = {
 190        { .name = "SAFEOUT", },
 191        { .name = "CHARGER", },
 192};
 193
 194static struct of_regulator_match max77836_regulator_matches[] = {
 195        { .name = "SAFEOUT", },
 196        { .name = "CHARGER", },
 197        { .name = "LDO1", },
 198        { .name = "LDO2", },
 199};
 200
 201static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev,
 202                enum maxim_device_type dev_type)
 203{
 204        int ret;
 205        struct device_node *np;
 206        struct of_regulator_match *regulator_matches;
 207        unsigned int regulator_matches_size;
 208
 209        np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
 210        if (!np) {
 211                dev_err(&pdev->dev, "Failed to get child OF node for regulators\n");
 212                return -EINVAL;
 213        }
 214
 215        switch (dev_type) {
 216        case MAXIM_DEVICE_TYPE_MAX77836:
 217                regulator_matches = max77836_regulator_matches;
 218                regulator_matches_size = ARRAY_SIZE(max77836_regulator_matches);
 219                break;
 220        case MAXIM_DEVICE_TYPE_MAX14577:
 221        default:
 222                regulator_matches = max14577_regulator_matches;
 223                regulator_matches_size = ARRAY_SIZE(max14577_regulator_matches);
 224        }
 225
 226        ret = of_regulator_match(&pdev->dev, np, regulator_matches,
 227                        regulator_matches_size);
 228        if (ret < 0)
 229                dev_err(&pdev->dev, "Error parsing regulator init data: %d\n", ret);
 230        else
 231                ret = 0;
 232
 233        of_node_put(np);
 234
 235        return ret;
 236}
 237
 238static inline struct regulator_init_data *match_init_data(int index,
 239                enum maxim_device_type dev_type)
 240{
 241        switch (dev_type) {
 242        case MAXIM_DEVICE_TYPE_MAX77836:
 243                return max77836_regulator_matches[index].init_data;
 244
 245        case MAXIM_DEVICE_TYPE_MAX14577:
 246        default:
 247                return max14577_regulator_matches[index].init_data;
 248        }
 249}
 250
 251static inline struct device_node *match_of_node(int index,
 252                enum maxim_device_type dev_type)
 253{
 254        switch (dev_type) {
 255        case MAXIM_DEVICE_TYPE_MAX77836:
 256                return max77836_regulator_matches[index].of_node;
 257
 258        case MAXIM_DEVICE_TYPE_MAX14577:
 259        default:
 260                return max14577_regulator_matches[index].of_node;
 261        }
 262}
 263#else /* CONFIG_OF */
 264static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev,
 265                enum maxim_device_type dev_type)
 266{
 267        return 0;
 268}
 269static inline struct regulator_init_data *match_init_data(int index,
 270                enum maxim_device_type dev_type)
 271{
 272        return NULL;
 273}
 274
 275static inline struct device_node *match_of_node(int index,
 276                enum maxim_device_type dev_type)
 277{
 278        return NULL;
 279}
 280#endif /* CONFIG_OF */
 281
 282/**
 283 * Registers for regulators of max77836 use different I2C slave addresses so
 284 * different regmaps must be used for them.
 285 *
 286 * Returns proper regmap for accessing regulator passed by id.
 287 */
 288static struct regmap *max14577_get_regmap(struct max14577 *max14577,
 289                int reg_id)
 290{
 291        switch (max14577->dev_type) {
 292        case MAXIM_DEVICE_TYPE_MAX77836:
 293                switch (reg_id) {
 294                case MAX77836_SAFEOUT ... MAX77836_CHARGER:
 295                        return max14577->regmap;
 296                default:
 297                        /* MAX77836_LDO1 ... MAX77836_LDO2 */
 298                        return max14577->regmap_pmic;
 299                }
 300
 301        case MAXIM_DEVICE_TYPE_MAX14577:
 302        default:
 303                return max14577->regmap;
 304        }
 305}
 306
 307static int max14577_regulator_probe(struct platform_device *pdev)
 308{
 309        struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
 310        struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
 311        int i, ret;
 312        struct regulator_config config = {};
 313        const struct regulator_desc *supported_regulators;
 314        unsigned int supported_regulators_size;
 315        enum maxim_device_type dev_type = max14577->dev_type;
 316
 317        ret = max14577_regulator_dt_parse_pdata(pdev, dev_type);
 318        if (ret)
 319                return ret;
 320
 321        switch (dev_type) {
 322        case MAXIM_DEVICE_TYPE_MAX77836:
 323                supported_regulators = max77836_supported_regulators;
 324                supported_regulators_size = ARRAY_SIZE(max77836_supported_regulators);
 325                break;
 326        case MAXIM_DEVICE_TYPE_MAX14577:
 327        default:
 328                supported_regulators = max14577_supported_regulators;
 329                supported_regulators_size = ARRAY_SIZE(max14577_supported_regulators);
 330        }
 331
 332        config.dev = &pdev->dev;
 333        config.driver_data = max14577;
 334
 335        for (i = 0; i < supported_regulators_size; i++) {
 336                struct regulator_dev *regulator;
 337                /*
 338                 * Index of supported_regulators[] is also the id and must
 339                 * match index of pdata->regulators[].
 340                 */
 341                if (pdata && pdata->regulators) {
 342                        config.init_data = pdata->regulators[i].initdata;
 343                        config.of_node = pdata->regulators[i].of_node;
 344                } else {
 345                        config.init_data = match_init_data(i, dev_type);
 346                        config.of_node = match_of_node(i, dev_type);
 347                }
 348                config.regmap = max14577_get_regmap(max14577,
 349                                supported_regulators[i].id);
 350
 351                regulator = devm_regulator_register(&pdev->dev,
 352                                &supported_regulators[i], &config);
 353                if (IS_ERR(regulator)) {
 354                        ret = PTR_ERR(regulator);
 355                        dev_err(&pdev->dev,
 356                                        "Regulator init failed for %d/%s with error: %d\n",
 357                                        i, supported_regulators[i].name, ret);
 358                        return ret;
 359                }
 360        }
 361
 362        return ret;
 363}
 364
 365static const struct platform_device_id max14577_regulator_id[] = {
 366        { "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577, },
 367        { "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836, },
 368        { }
 369};
 370MODULE_DEVICE_TABLE(platform, max14577_regulator_id);
 371
 372static struct platform_driver max14577_regulator_driver = {
 373        .driver = {
 374                   .name = "max14577-regulator",
 375                   },
 376        .probe          = max14577_regulator_probe,
 377        .id_table       = max14577_regulator_id,
 378};
 379
 380static int __init max14577_regulator_init(void)
 381{
 382        BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators) != MAX14577_REGULATOR_NUM);
 383        BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators) != MAX77836_REGULATOR_NUM);
 384
 385        BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN +
 386                        (MAX77836_REGULATOR_LDO_VOLTAGE_STEP *
 387                          (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM - 1)) !=
 388                        MAX77836_REGULATOR_LDO_VOLTAGE_MAX);
 389
 390        return platform_driver_register(&max14577_regulator_driver);
 391}
 392subsys_initcall(max14577_regulator_init);
 393
 394static void __exit max14577_regulator_exit(void)
 395{
 396        platform_driver_unregister(&max14577_regulator_driver);
 397}
 398module_exit(max14577_regulator_exit);
 399
 400MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
 401MODULE_DESCRIPTION("Maxim 14577/77836 regulator driver");
 402MODULE_LICENSE("GPL");
 403MODULE_ALIAS("platform:max14577-regulator");
 404
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.