4 * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com/
5 * Author: Balaji T K <balajitk@ti.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/err.h>
19 #include <linux/module.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/platform_device.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/machine.h>
24 #include <linux/regulator/of_regulator.h>
25 #include <linux/regmap.h>
26 #include <linux/slab.h>
28 #include <linux/of_device.h>
30 struct pbias_reg_info {
35 unsigned int enable_time;
37 const unsigned int *pbias_volt_table;
41 struct pbias_of_data {
45 static const unsigned int pbias_volt_table_3_0V[] = {
50 static const unsigned int pbias_volt_table_3_3V[] = {
55 static const struct regulator_ops pbias_regulator_voltage_ops = {
56 .list_voltage = regulator_list_voltage_table,
57 .get_voltage_sel = regulator_get_voltage_sel_regmap,
58 .set_voltage_sel = regulator_set_voltage_sel_regmap,
59 .enable = regulator_enable_regmap,
60 .disable = regulator_disable_regmap,
61 .is_enabled = regulator_is_enabled_regmap,
64 static const struct pbias_reg_info pbias_mmc_omap2430 = {
66 .enable_mask = BIT(1),
70 .pbias_volt_table = pbias_volt_table_3_0V,
72 .name = "pbias_mmc_omap2430"
75 static const struct pbias_reg_info pbias_sim_omap3 = {
77 .enable_mask = BIT(9),
80 .pbias_volt_table = pbias_volt_table_3_0V,
82 .name = "pbias_sim_omap3"
85 static const struct pbias_reg_info pbias_mmc_omap4 = {
86 .enable = BIT(26) | BIT(22),
87 .enable_mask = BIT(26) | BIT(25) | BIT(22),
88 .disable_val = BIT(25),
91 .pbias_volt_table = pbias_volt_table_3_0V,
93 .name = "pbias_mmc_omap4"
96 static const struct pbias_reg_info pbias_mmc_omap5 = {
97 .enable = BIT(27) | BIT(26),
98 .enable_mask = BIT(27) | BIT(25) | BIT(26),
99 .disable_val = BIT(25),
102 .pbias_volt_table = pbias_volt_table_3_3V,
104 .name = "pbias_mmc_omap5"
107 static struct of_regulator_match pbias_matches[] = {
108 { .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430},
109 { .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3},
110 { .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4},
111 { .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5},
113 #define PBIAS_NUM_REGS ARRAY_SIZE(pbias_matches)
115 /* Offset from SCM general area (and syscon) base */
117 static const struct pbias_of_data pbias_of_data_omap2 = {
121 static const struct pbias_of_data pbias_of_data_omap3 = {
125 static const struct pbias_of_data pbias_of_data_omap4 = {
129 static const struct pbias_of_data pbias_of_data_omap5 = {
133 static const struct pbias_of_data pbias_of_data_dra7 = {
137 static const struct of_device_id pbias_of_match[] = {
138 { .compatible = "ti,pbias-omap", },
139 { .compatible = "ti,pbias-omap2", .data = &pbias_of_data_omap2, },
140 { .compatible = "ti,pbias-omap3", .data = &pbias_of_data_omap3, },
141 { .compatible = "ti,pbias-omap4", .data = &pbias_of_data_omap4, },
142 { .compatible = "ti,pbias-omap5", .data = &pbias_of_data_omap5, },
143 { .compatible = "ti,pbias-dra7", .data = &pbias_of_data_dra7, },
146 MODULE_DEVICE_TABLE(of, pbias_of_match);
148 static int pbias_regulator_probe(struct platform_device *pdev)
150 struct device_node *np = pdev->dev.of_node;
151 struct resource *res;
152 struct regulator_config cfg = { };
153 struct regulator_desc *desc;
154 struct regulator_dev *rdev;
155 struct regmap *syscon;
156 const struct pbias_reg_info *info;
158 const struct pbias_of_data *data;
161 count = of_regulator_match(&pdev->dev, np, pbias_matches,
166 desc = devm_kcalloc(&pdev->dev, count, sizeof(*desc), GFP_KERNEL);
170 syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
172 return PTR_ERR(syscon);
174 data = of_device_get_match_data(&pdev->dev);
176 offset = data->offset;
178 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
184 "using legacy dt data for pbias offset\n");
188 cfg.dev = &pdev->dev;
190 for (idx = 0; idx < PBIAS_NUM_REGS && count; idx++) {
191 if (!pbias_matches[idx].init_data ||
192 !pbias_matches[idx].of_node)
195 info = pbias_matches[idx].driver_data;
199 desc->name = info->name;
200 desc->owner = THIS_MODULE;
201 desc->type = REGULATOR_VOLTAGE;
202 desc->ops = &pbias_regulator_voltage_ops;
203 desc->volt_table = info->pbias_volt_table;
204 desc->n_voltages = info->n_voltages;
205 desc->enable_time = info->enable_time;
206 desc->vsel_reg = offset;
207 desc->vsel_mask = info->vmode;
208 desc->enable_reg = offset;
209 desc->enable_mask = info->enable_mask;
210 desc->enable_val = info->enable;
211 desc->disable_val = info->disable_val;
213 cfg.init_data = pbias_matches[idx].init_data;
214 cfg.of_node = pbias_matches[idx].of_node;
216 rdev = devm_regulator_register(&pdev->dev, desc, &cfg);
220 "Failed to register regulator: %d\n", ret);
230 static struct platform_driver pbias_regulator_driver = {
231 .probe = pbias_regulator_probe,
233 .name = "pbias-regulator",
234 .of_match_table = of_match_ptr(pbias_of_match),
238 module_platform_driver(pbias_regulator_driver);
240 MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
241 MODULE_DESCRIPTION("pbias voltage regulator");
242 MODULE_LICENSE("GPL");
243 MODULE_ALIAS("platform:pbias-regulator");