Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * TI LMU (Lighting Management Unit) Core Driver
0004  *
0005  * Copyright 2017 Texas Instruments
0006  *
0007  * Author: Milo Kim <milo.kim@ti.com>
0008  */
0009 
0010 #include <linux/delay.h>
0011 #include <linux/err.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/i2c.h>
0014 #include <linux/kernel.h>
0015 #include <linux/mfd/core.h>
0016 #include <linux/mfd/ti-lmu.h>
0017 #include <linux/mfd/ti-lmu-register.h>
0018 #include <linux/module.h>
0019 #include <linux/of.h>
0020 #include <linux/of_device.h>
0021 #include <linux/slab.h>
0022 
0023 struct ti_lmu_data {
0024     const struct mfd_cell *cells;
0025     int num_cells;
0026     unsigned int max_register;
0027 };
0028 
0029 static int ti_lmu_enable_hw(struct ti_lmu *lmu, enum ti_lmu_id id)
0030 {
0031     if (lmu->en_gpio)
0032         gpiod_set_value(lmu->en_gpio, 1);
0033 
0034     /* Delay about 1ms after HW enable pin control */
0035     usleep_range(1000, 1500);
0036 
0037     /* LM3631 has additional power up sequence - enable LCD_EN bit. */
0038     if (id == LM3631) {
0039         return regmap_update_bits(lmu->regmap, LM3631_REG_DEVCTRL,
0040                       LM3631_LCD_EN_MASK,
0041                       LM3631_LCD_EN_MASK);
0042     }
0043 
0044     return 0;
0045 }
0046 
0047 static void ti_lmu_disable_hw(void *data)
0048 {
0049     struct ti_lmu *lmu = data;
0050     if (lmu->en_gpio)
0051         gpiod_set_value(lmu->en_gpio, 0);
0052 }
0053 
0054 #define LM363X_REGULATOR(_id)           \
0055 {                       \
0056     .name          = "lm363x-regulator",    \
0057     .id            = _id,           \
0058     .of_compatible = "ti,lm363x-regulator", \
0059 }                       \
0060 
0061 static const struct mfd_cell lm3631_devices[] = {
0062     LM363X_REGULATOR(LM3631_BOOST),
0063     LM363X_REGULATOR(LM3631_LDO_CONT),
0064     LM363X_REGULATOR(LM3631_LDO_OREF),
0065     LM363X_REGULATOR(LM3631_LDO_POS),
0066     LM363X_REGULATOR(LM3631_LDO_NEG),
0067     {
0068         .name          = "ti-lmu-backlight",
0069         .id            = LM3631,
0070         .of_compatible = "ti,lm3631-backlight",
0071     },
0072 };
0073 
0074 static const struct mfd_cell lm3632_devices[] = {
0075     LM363X_REGULATOR(LM3632_BOOST),
0076     LM363X_REGULATOR(LM3632_LDO_POS),
0077     LM363X_REGULATOR(LM3632_LDO_NEG),
0078     {
0079         .name          = "ti-lmu-backlight",
0080         .id            = LM3632,
0081         .of_compatible = "ti,lm3632-backlight",
0082     },
0083 };
0084 
0085 static const struct mfd_cell lm3633_devices[] = {
0086     {
0087         .name          = "ti-lmu-backlight",
0088         .id            = LM3633,
0089         .of_compatible = "ti,lm3633-backlight",
0090     },
0091     {
0092         .name          = "lm3633-leds",
0093         .of_compatible = "ti,lm3633-leds",
0094     },
0095     /* Monitoring driver for open/short circuit detection */
0096     {
0097         .name          = "ti-lmu-fault-monitor",
0098         .id            = LM3633,
0099         .of_compatible = "ti,lm3633-fault-monitor",
0100     },
0101 };
0102 
0103 static const struct mfd_cell lm3695_devices[] = {
0104     {
0105         .name          = "ti-lmu-backlight",
0106         .id            = LM3695,
0107         .of_compatible = "ti,lm3695-backlight",
0108     },
0109 };
0110 
0111 static const struct mfd_cell lm36274_devices[] = {
0112     LM363X_REGULATOR(LM36274_BOOST),
0113     LM363X_REGULATOR(LM36274_LDO_POS),
0114     LM363X_REGULATOR(LM36274_LDO_NEG),
0115     {
0116         .name          = "lm36274-leds",
0117         .id            = LM36274,
0118         .of_compatible = "ti,lm36274-backlight",
0119     },
0120 };
0121 
0122 #define TI_LMU_DATA(chip, max_reg)      \
0123 static const struct ti_lmu_data chip##_data =   \
0124 {                       \
0125     .cells = chip##_devices,        \
0126     .num_cells = ARRAY_SIZE(chip##_devices),\
0127     .max_register = max_reg,        \
0128 }                       \
0129 
0130 TI_LMU_DATA(lm3631, LM3631_MAX_REG);
0131 TI_LMU_DATA(lm3632, LM3632_MAX_REG);
0132 TI_LMU_DATA(lm3633, LM3633_MAX_REG);
0133 TI_LMU_DATA(lm3695, LM3695_MAX_REG);
0134 TI_LMU_DATA(lm36274, LM36274_MAX_REG);
0135 
0136 static int ti_lmu_probe(struct i2c_client *cl, const struct i2c_device_id *id)
0137 {
0138     struct device *dev = &cl->dev;
0139     const struct ti_lmu_data *data;
0140     struct regmap_config regmap_cfg;
0141     struct ti_lmu *lmu;
0142     int ret;
0143 
0144     /*
0145      * Get device specific data from of_match table.
0146      * This data is defined by using TI_LMU_DATA() macro.
0147      */
0148     data = of_device_get_match_data(dev);
0149     if (!data)
0150         return -ENODEV;
0151 
0152     lmu = devm_kzalloc(dev, sizeof(*lmu), GFP_KERNEL);
0153     if (!lmu)
0154         return -ENOMEM;
0155 
0156     lmu->dev = &cl->dev;
0157 
0158     /* Setup regmap */
0159     memset(&regmap_cfg, 0, sizeof(struct regmap_config));
0160     regmap_cfg.reg_bits = 8;
0161     regmap_cfg.val_bits = 8;
0162     regmap_cfg.name = id->name;
0163     regmap_cfg.max_register = data->max_register;
0164 
0165     lmu->regmap = devm_regmap_init_i2c(cl, &regmap_cfg);
0166     if (IS_ERR(lmu->regmap))
0167         return PTR_ERR(lmu->regmap);
0168 
0169     /* HW enable pin control and additional power up sequence if required */
0170     lmu->en_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH);
0171     if (IS_ERR(lmu->en_gpio)) {
0172         ret = PTR_ERR(lmu->en_gpio);
0173         dev_err(dev, "Can not request enable GPIO: %d\n", ret);
0174         return ret;
0175     }
0176 
0177     ret = ti_lmu_enable_hw(lmu, id->driver_data);
0178     if (ret)
0179         return ret;
0180 
0181     ret = devm_add_action_or_reset(dev, ti_lmu_disable_hw, lmu);
0182     if (ret)
0183         return ret;
0184 
0185     /*
0186      * Fault circuit(open/short) can be detected by ti-lmu-fault-monitor.
0187      * After fault detection is done, some devices should re-initialize
0188      * configuration. The notifier enables such kind of handling.
0189      */
0190     BLOCKING_INIT_NOTIFIER_HEAD(&lmu->notifier);
0191 
0192     i2c_set_clientdata(cl, lmu);
0193 
0194     return devm_mfd_add_devices(lmu->dev, 0, data->cells,
0195                     data->num_cells, NULL, 0, NULL);
0196 }
0197 
0198 static const struct of_device_id ti_lmu_of_match[] = {
0199     { .compatible = "ti,lm3631", .data = &lm3631_data },
0200     { .compatible = "ti,lm3632", .data = &lm3632_data },
0201     { .compatible = "ti,lm3633", .data = &lm3633_data },
0202     { .compatible = "ti,lm3695", .data = &lm3695_data },
0203     { .compatible = "ti,lm36274", .data = &lm36274_data },
0204     { }
0205 };
0206 MODULE_DEVICE_TABLE(of, ti_lmu_of_match);
0207 
0208 static const struct i2c_device_id ti_lmu_ids[] = {
0209     { "lm3631", LM3631 },
0210     { "lm3632", LM3632 },
0211     { "lm3633", LM3633 },
0212     { "lm3695", LM3695 },
0213     { "lm36274", LM36274 },
0214     { }
0215 };
0216 MODULE_DEVICE_TABLE(i2c, ti_lmu_ids);
0217 
0218 static struct i2c_driver ti_lmu_driver = {
0219     .probe = ti_lmu_probe,
0220     .driver = {
0221         .name = "ti-lmu",
0222         .of_match_table = ti_lmu_of_match,
0223     },
0224     .id_table = ti_lmu_ids,
0225 };
0226 
0227 module_i2c_driver(ti_lmu_driver);
0228 
0229 MODULE_DESCRIPTION("TI LMU MFD Core Driver");
0230 MODULE_AUTHOR("Milo Kim");
0231 MODULE_LICENSE("GPL v2");