Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * max8952.c - Voltage and current regulation for the Maxim 8952
0004  *
0005  * Copyright (C) 2010 Samsung Electronics
0006  * MyungJoo Ham <myungjoo.ham@samsung.com>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/init.h>
0011 #include <linux/i2c.h>
0012 #include <linux/err.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/regulator/driver.h>
0015 #include <linux/regulator/max8952.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/io.h>
0018 #include <linux/of.h>
0019 #include <linux/regulator/of_regulator.h>
0020 #include <linux/slab.h>
0021 
0022 /* Registers */
0023 enum {
0024     MAX8952_REG_MODE0,
0025     MAX8952_REG_MODE1,
0026     MAX8952_REG_MODE2,
0027     MAX8952_REG_MODE3,
0028     MAX8952_REG_CONTROL,
0029     MAX8952_REG_SYNC,
0030     MAX8952_REG_RAMP,
0031     MAX8952_REG_CHIP_ID1,
0032     MAX8952_REG_CHIP_ID2,
0033 };
0034 
0035 struct max8952_data {
0036     struct i2c_client   *client;
0037     struct max8952_platform_data *pdata;
0038     struct gpio_desc *vid0_gpiod;
0039     struct gpio_desc *vid1_gpiod;
0040     bool vid0;
0041     bool vid1;
0042 };
0043 
0044 static int max8952_read_reg(struct max8952_data *max8952, u8 reg)
0045 {
0046     int ret = i2c_smbus_read_byte_data(max8952->client, reg);
0047 
0048     if (ret > 0)
0049         ret &= 0xff;
0050 
0051     return ret;
0052 }
0053 
0054 static int max8952_write_reg(struct max8952_data *max8952,
0055         u8 reg, u8 value)
0056 {
0057     return i2c_smbus_write_byte_data(max8952->client, reg, value);
0058 }
0059 
0060 static int max8952_list_voltage(struct regulator_dev *rdev,
0061         unsigned int selector)
0062 {
0063     struct max8952_data *max8952 = rdev_get_drvdata(rdev);
0064 
0065     if (rdev_get_id(rdev) != 0)
0066         return -EINVAL;
0067 
0068     return (max8952->pdata->dvs_mode[selector] * 10 + 770) * 1000;
0069 }
0070 
0071 static int max8952_get_voltage_sel(struct regulator_dev *rdev)
0072 {
0073     struct max8952_data *max8952 = rdev_get_drvdata(rdev);
0074     u8 vid = 0;
0075 
0076     if (max8952->vid0)
0077         vid += 1;
0078     if (max8952->vid1)
0079         vid += 2;
0080 
0081     return vid;
0082 }
0083 
0084 static int max8952_set_voltage_sel(struct regulator_dev *rdev,
0085                    unsigned selector)
0086 {
0087     struct max8952_data *max8952 = rdev_get_drvdata(rdev);
0088 
0089     if (!max8952->vid0_gpiod || !max8952->vid1_gpiod) {
0090         /* DVS not supported */
0091         return -EPERM;
0092     }
0093 
0094     max8952->vid0 = selector & 0x1;
0095     max8952->vid1 = (selector >> 1) & 0x1;
0096     gpiod_set_value(max8952->vid0_gpiod, max8952->vid0);
0097     gpiod_set_value(max8952->vid1_gpiod, max8952->vid1);
0098 
0099     return 0;
0100 }
0101 
0102 static const struct regulator_ops max8952_ops = {
0103     .list_voltage       = max8952_list_voltage,
0104     .get_voltage_sel    = max8952_get_voltage_sel,
0105     .set_voltage_sel    = max8952_set_voltage_sel,
0106 };
0107 
0108 static const struct regulator_desc regulator = {
0109     .name       = "MAX8952_VOUT",
0110     .id     = 0,
0111     .n_voltages = MAX8952_NUM_DVS_MODE,
0112     .ops        = &max8952_ops,
0113     .type       = REGULATOR_VOLTAGE,
0114     .owner      = THIS_MODULE,
0115 };
0116 
0117 #ifdef CONFIG_OF
0118 static const struct of_device_id max8952_dt_match[] = {
0119     { .compatible = "maxim,max8952" },
0120     {},
0121 };
0122 MODULE_DEVICE_TABLE(of, max8952_dt_match);
0123 
0124 static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
0125 {
0126     struct max8952_platform_data *pd;
0127     struct device_node *np = dev->of_node;
0128     int ret;
0129     int i;
0130 
0131     pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
0132     if (!pd)
0133         return NULL;
0134 
0135     if (of_property_read_u32(np, "max8952,default-mode", &pd->default_mode))
0136         dev_warn(dev, "Default mode not specified, assuming 0\n");
0137 
0138     ret = of_property_read_u32_array(np, "max8952,dvs-mode-microvolt",
0139                     pd->dvs_mode, ARRAY_SIZE(pd->dvs_mode));
0140     if (ret) {
0141         dev_err(dev, "max8952,dvs-mode-microvolt property not specified");
0142         return NULL;
0143     }
0144 
0145     for (i = 0; i < ARRAY_SIZE(pd->dvs_mode); ++i) {
0146         if (pd->dvs_mode[i] < 770000 || pd->dvs_mode[i] > 1400000) {
0147             dev_err(dev, "DVS voltage %d out of range\n", i);
0148             return NULL;
0149         }
0150         pd->dvs_mode[i] = (pd->dvs_mode[i] - 770000) / 10000;
0151     }
0152 
0153     if (of_property_read_u32(np, "max8952,sync-freq", &pd->sync_freq))
0154         dev_warn(dev, "max8952,sync-freq property not specified, defaulting to 26MHz\n");
0155 
0156     if (of_property_read_u32(np, "max8952,ramp-speed", &pd->ramp_speed))
0157         dev_warn(dev, "max8952,ramp-speed property not specified, defaulting to 32mV/us\n");
0158 
0159     pd->reg_data = of_get_regulator_init_data(dev, np, &regulator);
0160     if (!pd->reg_data) {
0161         dev_err(dev, "Failed to parse regulator init data\n");
0162         return NULL;
0163     }
0164 
0165     return pd;
0166 }
0167 #else
0168 static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
0169 {
0170     return NULL;
0171 }
0172 #endif
0173 
0174 static int max8952_pmic_probe(struct i2c_client *client,
0175         const struct i2c_device_id *i2c_id)
0176 {
0177     struct i2c_adapter *adapter = client->adapter;
0178     struct max8952_platform_data *pdata = dev_get_platdata(&client->dev);
0179     struct regulator_config config = { };
0180     struct max8952_data *max8952;
0181     struct regulator_dev *rdev;
0182     struct gpio_desc *gpiod;
0183     enum gpiod_flags gflags;
0184 
0185     int ret = 0;
0186 
0187     if (client->dev.of_node)
0188         pdata = max8952_parse_dt(&client->dev);
0189 
0190     if (!pdata) {
0191         dev_err(&client->dev, "Require the platform data\n");
0192         return -EINVAL;
0193     }
0194 
0195     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
0196         return -EIO;
0197 
0198     max8952 = devm_kzalloc(&client->dev, sizeof(struct max8952_data),
0199                    GFP_KERNEL);
0200     if (!max8952)
0201         return -ENOMEM;
0202 
0203     max8952->client = client;
0204     max8952->pdata = pdata;
0205 
0206     config.dev = &client->dev;
0207     config.init_data = pdata->reg_data;
0208     config.driver_data = max8952;
0209     config.of_node = client->dev.of_node;
0210 
0211     if (pdata->reg_data->constraints.boot_on)
0212         gflags = GPIOD_OUT_HIGH;
0213     else
0214         gflags = GPIOD_OUT_LOW;
0215     gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
0216     /*
0217      * Do not use devm* here: the regulator core takes over the
0218      * lifecycle management of the GPIO descriptor.
0219      */
0220     gpiod = gpiod_get_optional(&client->dev,
0221                    "max8952,en",
0222                    gflags);
0223     if (IS_ERR(gpiod))
0224         return PTR_ERR(gpiod);
0225     if (gpiod)
0226         config.ena_gpiod = gpiod;
0227 
0228     rdev = devm_regulator_register(&client->dev, &regulator, &config);
0229     if (IS_ERR(rdev)) {
0230         ret = PTR_ERR(rdev);
0231         dev_err(&client->dev, "regulator init failed (%d)\n", ret);
0232         return ret;
0233     }
0234 
0235     max8952->vid0 = pdata->default_mode & 0x1;
0236     max8952->vid1 = (pdata->default_mode >> 1) & 0x1;
0237 
0238     /* Fetch vid0 and vid1 GPIOs if available */
0239     gflags = max8952->vid0 ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
0240     max8952->vid0_gpiod = devm_gpiod_get_index_optional(&client->dev,
0241                                 "max8952,vid",
0242                                 0, gflags);
0243     if (IS_ERR(max8952->vid0_gpiod))
0244         return PTR_ERR(max8952->vid0_gpiod);
0245     gflags = max8952->vid1 ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
0246     max8952->vid1_gpiod = devm_gpiod_get_index_optional(&client->dev,
0247                                 "max8952,vid",
0248                                 1, gflags);
0249     if (IS_ERR(max8952->vid1_gpiod))
0250         return PTR_ERR(max8952->vid1_gpiod);
0251 
0252     /* If either VID GPIO is missing just disable this */
0253     if (!max8952->vid0_gpiod || !max8952->vid1_gpiod) {
0254         dev_warn(&client->dev, "VID0/1 gpio invalid: "
0255              "DVS not available.\n");
0256         max8952->vid0 = 0;
0257         max8952->vid1 = 0;
0258         /* Make sure if we have any descriptors they get set to low */
0259         if (max8952->vid0_gpiod)
0260             gpiod_set_value(max8952->vid0_gpiod, 0);
0261         if (max8952->vid1_gpiod)
0262             gpiod_set_value(max8952->vid1_gpiod, 0);
0263 
0264         /* Disable Pulldown of EN only */
0265         max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
0266 
0267         dev_err(&client->dev, "DVS modes disabled because VID0 and VID1"
0268                 " do not have proper controls.\n");
0269     } else {
0270         /*
0271          * Disable Pulldown on EN, VID0, VID1 to reduce
0272          * leakage current of MAX8952 assuming that MAX8952
0273          * is turned on (EN==1). Note that without having VID0/1
0274          * properly connected, turning pulldown off can be
0275          * problematic. Thus, turn this off only when they are
0276          * controllable by GPIO.
0277          */
0278         max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
0279     }
0280 
0281     max8952_write_reg(max8952, MAX8952_REG_MODE0,
0282             (max8952_read_reg(max8952,
0283                       MAX8952_REG_MODE0) & 0xC0) |
0284             (pdata->dvs_mode[0] & 0x3F));
0285     max8952_write_reg(max8952, MAX8952_REG_MODE1,
0286             (max8952_read_reg(max8952,
0287                       MAX8952_REG_MODE1) & 0xC0) |
0288             (pdata->dvs_mode[1] & 0x3F));
0289     max8952_write_reg(max8952, MAX8952_REG_MODE2,
0290             (max8952_read_reg(max8952,
0291                       MAX8952_REG_MODE2) & 0xC0) |
0292             (pdata->dvs_mode[2] & 0x3F));
0293     max8952_write_reg(max8952, MAX8952_REG_MODE3,
0294             (max8952_read_reg(max8952,
0295                       MAX8952_REG_MODE3) & 0xC0) |
0296             (pdata->dvs_mode[3] & 0x3F));
0297 
0298     max8952_write_reg(max8952, MAX8952_REG_SYNC,
0299             (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) |
0300             ((pdata->sync_freq & 0x3) << 6));
0301     max8952_write_reg(max8952, MAX8952_REG_RAMP,
0302             (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) |
0303             ((pdata->ramp_speed & 0x7) << 5));
0304 
0305     i2c_set_clientdata(client, max8952);
0306 
0307     return 0;
0308 }
0309 
0310 static const struct i2c_device_id max8952_ids[] = {
0311     { "max8952", 0 },
0312     { },
0313 };
0314 MODULE_DEVICE_TABLE(i2c, max8952_ids);
0315 
0316 static struct i2c_driver max8952_pmic_driver = {
0317     .probe      = max8952_pmic_probe,
0318     .driver     = {
0319         .name   = "max8952",
0320         .of_match_table = of_match_ptr(max8952_dt_match),
0321     },
0322     .id_table   = max8952_ids,
0323 };
0324 
0325 static int __init max8952_pmic_init(void)
0326 {
0327     return i2c_add_driver(&max8952_pmic_driver);
0328 }
0329 subsys_initcall(max8952_pmic_init);
0330 
0331 static void __exit max8952_pmic_exit(void)
0332 {
0333     i2c_del_driver(&max8952_pmic_driver);
0334 }
0335 module_exit(max8952_pmic_exit);
0336 
0337 MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
0338 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
0339 MODULE_LICENSE("GPL");