Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * pbias-regulator.c
0003  *
0004  * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com/
0005  * Author: Balaji T K <balajitk@ti.com>
0006  *
0007  * This program is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU General Public License as
0009  * published by the Free Software Foundation version 2.
0010  *
0011  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
0012  * kind, whether express or implied; without even the implied warranty
0013  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  */
0016 
0017 #include <linux/err.h>
0018 #include <linux/io.h>
0019 #include <linux/module.h>
0020 #include <linux/mfd/syscon.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/regulator/driver.h>
0023 #include <linux/regulator/machine.h>
0024 #include <linux/regulator/of_regulator.h>
0025 #include <linux/regmap.h>
0026 #include <linux/slab.h>
0027 #include <linux/of.h>
0028 #include <linux/of_device.h>
0029 
0030 struct pbias_reg_info {
0031     u32 enable;
0032     u32 enable_mask;
0033     u32 disable_val;
0034     u32 vmode;
0035     unsigned int enable_time;
0036     char *name;
0037     const unsigned int *pbias_volt_table;
0038     int n_voltages;
0039 };
0040 
0041 struct pbias_of_data {
0042     unsigned int offset;
0043 };
0044 
0045 static const unsigned int pbias_volt_table_3_0V[] = {
0046     1800000,
0047     3000000
0048 };
0049 
0050 static const unsigned int pbias_volt_table_3_3V[] = {
0051     1800000,
0052     3300000
0053 };
0054 
0055 static const struct regulator_ops pbias_regulator_voltage_ops = {
0056     .list_voltage = regulator_list_voltage_table,
0057     .get_voltage_sel = regulator_get_voltage_sel_regmap,
0058     .set_voltage_sel = regulator_set_voltage_sel_regmap,
0059     .enable = regulator_enable_regmap,
0060     .disable = regulator_disable_regmap,
0061     .is_enabled = regulator_is_enabled_regmap,
0062 };
0063 
0064 static const struct pbias_reg_info pbias_mmc_omap2430 = {
0065     .enable = BIT(1),
0066     .enable_mask = BIT(1),
0067     .vmode = BIT(0),
0068     .disable_val = 0,
0069     .enable_time = 100,
0070     .pbias_volt_table = pbias_volt_table_3_0V,
0071     .n_voltages = 2,
0072     .name = "pbias_mmc_omap2430"
0073 };
0074 
0075 static const struct pbias_reg_info pbias_sim_omap3 = {
0076     .enable = BIT(9),
0077     .enable_mask = BIT(9),
0078     .vmode = BIT(8),
0079     .enable_time = 100,
0080     .pbias_volt_table = pbias_volt_table_3_0V,
0081     .n_voltages = 2,
0082     .name = "pbias_sim_omap3"
0083 };
0084 
0085 static const struct pbias_reg_info pbias_mmc_omap4 = {
0086     .enable = BIT(26) | BIT(22),
0087     .enable_mask = BIT(26) | BIT(25) | BIT(22),
0088     .disable_val = BIT(25),
0089     .vmode = BIT(21),
0090     .enable_time = 100,
0091     .pbias_volt_table = pbias_volt_table_3_0V,
0092     .n_voltages = 2,
0093     .name = "pbias_mmc_omap4"
0094 };
0095 
0096 static const struct pbias_reg_info pbias_mmc_omap5 = {
0097     .enable = BIT(27) | BIT(26),
0098     .enable_mask = BIT(27) | BIT(25) | BIT(26),
0099     .disable_val = BIT(25),
0100     .vmode = BIT(21),
0101     .enable_time = 100,
0102     .pbias_volt_table = pbias_volt_table_3_3V,
0103     .n_voltages = 2,
0104     .name = "pbias_mmc_omap5"
0105 };
0106 
0107 static struct of_regulator_match pbias_matches[] = {
0108     { .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430},
0109     { .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3},
0110     { .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4},
0111     { .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5},
0112 };
0113 #define PBIAS_NUM_REGS  ARRAY_SIZE(pbias_matches)
0114 
0115 /* Offset from SCM general area (and syscon) base */
0116 
0117 static const struct pbias_of_data pbias_of_data_omap2 = {
0118     .offset = 0x230,
0119 };
0120 
0121 static const struct pbias_of_data pbias_of_data_omap3 = {
0122     .offset = 0x2b0,
0123 };
0124 
0125 static const struct pbias_of_data pbias_of_data_omap4 = {
0126     .offset = 0x60,
0127 };
0128 
0129 static const struct pbias_of_data pbias_of_data_omap5 = {
0130     .offset = 0x60,
0131 };
0132 
0133 static const struct pbias_of_data pbias_of_data_dra7 = {
0134     .offset = 0xe00,
0135 };
0136 
0137 static const struct of_device_id pbias_of_match[] = {
0138     { .compatible = "ti,pbias-omap", },
0139     { .compatible = "ti,pbias-omap2", .data = &pbias_of_data_omap2, },
0140     { .compatible = "ti,pbias-omap3", .data = &pbias_of_data_omap3, },
0141     { .compatible = "ti,pbias-omap4", .data = &pbias_of_data_omap4, },
0142     { .compatible = "ti,pbias-omap5", .data = &pbias_of_data_omap5, },
0143     { .compatible = "ti,pbias-dra7", .data = &pbias_of_data_dra7, },
0144     {},
0145 };
0146 MODULE_DEVICE_TABLE(of, pbias_of_match);
0147 
0148 static int pbias_regulator_probe(struct platform_device *pdev)
0149 {
0150     struct device_node *np = pdev->dev.of_node;
0151     struct resource *res;
0152     struct regulator_config cfg = { };
0153     struct regulator_desc *desc;
0154     struct regulator_dev *rdev;
0155     struct regmap *syscon;
0156     const struct pbias_reg_info *info;
0157     int ret, count, idx;
0158     const struct pbias_of_data *data;
0159     unsigned int offset;
0160 
0161     count = of_regulator_match(&pdev->dev, np, pbias_matches,
0162                         PBIAS_NUM_REGS);
0163     if (count < 0)
0164         return count;
0165 
0166     desc = devm_kcalloc(&pdev->dev, count, sizeof(*desc), GFP_KERNEL);
0167     if (!desc)
0168         return -ENOMEM;
0169 
0170     syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
0171     if (IS_ERR(syscon))
0172         return PTR_ERR(syscon);
0173 
0174     data = of_device_get_match_data(&pdev->dev);
0175     if (data) {
0176         offset = data->offset;
0177     } else {
0178         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0179         if (!res)
0180             return -EINVAL;
0181 
0182         offset = res->start;
0183         dev_WARN(&pdev->dev,
0184              "using legacy dt data for pbias offset\n");
0185     }
0186 
0187     cfg.regmap = syscon;
0188     cfg.dev = &pdev->dev;
0189 
0190     for (idx = 0; idx < PBIAS_NUM_REGS && count; idx++) {
0191         if (!pbias_matches[idx].init_data ||
0192             !pbias_matches[idx].of_node)
0193             continue;
0194 
0195         info = pbias_matches[idx].driver_data;
0196         if (!info)
0197             return -ENODEV;
0198 
0199         desc->name = info->name;
0200         desc->owner = THIS_MODULE;
0201         desc->type = REGULATOR_VOLTAGE;
0202         desc->ops = &pbias_regulator_voltage_ops;
0203         desc->volt_table = info->pbias_volt_table;
0204         desc->n_voltages = info->n_voltages;
0205         desc->enable_time = info->enable_time;
0206         desc->vsel_reg = offset;
0207         desc->vsel_mask = info->vmode;
0208         desc->enable_reg = offset;
0209         desc->enable_mask = info->enable_mask;
0210         desc->enable_val = info->enable;
0211         desc->disable_val = info->disable_val;
0212 
0213         cfg.init_data = pbias_matches[idx].init_data;
0214         cfg.of_node = pbias_matches[idx].of_node;
0215 
0216         rdev = devm_regulator_register(&pdev->dev, desc, &cfg);
0217         if (IS_ERR(rdev)) {
0218             ret = PTR_ERR(rdev);
0219             dev_err(&pdev->dev,
0220                 "Failed to register regulator: %d\n", ret);
0221             return ret;
0222         }
0223         desc++;
0224         count--;
0225     }
0226 
0227     return 0;
0228 }
0229 
0230 static struct platform_driver pbias_regulator_driver = {
0231     .probe      = pbias_regulator_probe,
0232     .driver     = {
0233         .name       = "pbias-regulator",
0234         .of_match_table = of_match_ptr(pbias_of_match),
0235     },
0236 };
0237 
0238 module_platform_driver(pbias_regulator_driver);
0239 
0240 MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
0241 MODULE_DESCRIPTION("pbias voltage regulator");
0242 MODULE_LICENSE("GPL");
0243 MODULE_ALIAS("platform:pbias-regulator");