blob: 6d02d68dfb4647696027fd54d060203fa26f5480 [file] [log] [blame]
Balaji T K11469e02014-02-19 20:26:40 +05301/*
2 * pbias-regulator.c
3 *
4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
5 * Author: Balaji T K <balajitk@ti.com>
6 *
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.
10 *
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.
15 */
16
17#include <linux/err.h>
18#include <linux/io.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>
27#include <linux/of.h>
28#include <linux/of_device.h>
29
30struct pbias_reg_info {
31 u32 enable;
32 u32 enable_mask;
33 u32 vmode;
34 unsigned int enable_time;
35 char *name;
36};
37
38struct pbias_regulator_data {
39 struct regulator_desc desc;
40 void __iomem *pbias_addr;
Balaji T K11469e02014-02-19 20:26:40 +053041 struct regulator_dev *dev;
42 struct regmap *syscon;
43 const struct pbias_reg_info *info;
44 int voltage;
45};
46
Axel Lin60e8c1e2014-03-08 11:56:47 +080047static const unsigned int pbias_volt_table[] = {
48 1800000,
49 3000000
50};
Balaji T K11469e02014-02-19 20:26:40 +053051
Balaji T K11469e02014-02-19 20:26:40 +053052static struct regulator_ops pbias_regulator_voltage_ops = {
Axel Lin60e8c1e2014-03-08 11:56:47 +080053 .list_voltage = regulator_list_voltage_table,
54 .get_voltage_sel = regulator_get_voltage_sel_regmap,
55 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Axel Lin75dbf0a2014-04-15 12:02:08 +080056 .enable = regulator_enable_regmap,
Axel Lin60e8c1e2014-03-08 11:56:47 +080057 .disable = regulator_disable_regmap,
Axel Lin75dbf0a2014-04-15 12:02:08 +080058 .is_enabled = regulator_is_enabled_regmap,
Balaji T K11469e02014-02-19 20:26:40 +053059};
60
61static const struct pbias_reg_info pbias_mmc_omap2430 = {
62 .enable = BIT(1),
63 .enable_mask = BIT(1),
64 .vmode = BIT(0),
65 .enable_time = 100,
66 .name = "pbias_mmc_omap2430"
67};
68
69static const struct pbias_reg_info pbias_sim_omap3 = {
70 .enable = BIT(9),
71 .enable_mask = BIT(9),
72 .vmode = BIT(8),
73 .enable_time = 100,
74 .name = "pbias_sim_omap3"
75};
76
77static const struct pbias_reg_info pbias_mmc_omap4 = {
78 .enable = BIT(26) | BIT(22),
79 .enable_mask = BIT(26) | BIT(25) | BIT(22),
80 .vmode = BIT(21),
81 .enable_time = 100,
82 .name = "pbias_mmc_omap4"
83};
84
85static const struct pbias_reg_info pbias_mmc_omap5 = {
86 .enable = BIT(27) | BIT(26),
87 .enable_mask = BIT(27) | BIT(25) | BIT(26),
88 .vmode = BIT(21),
89 .enable_time = 100,
90 .name = "pbias_mmc_omap5"
91};
92
93static struct of_regulator_match pbias_matches[] = {
94 { .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430},
95 { .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3},
96 { .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4},
97 { .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5},
98};
99#define PBIAS_NUM_REGS ARRAY_SIZE(pbias_matches)
100
101static const struct of_device_id pbias_of_match[] = {
102 { .compatible = "ti,pbias-omap", },
103 {},
104};
105MODULE_DEVICE_TABLE(of, pbias_of_match);
106
107static int pbias_regulator_probe(struct platform_device *pdev)
108{
109 struct device_node *np = pdev->dev.of_node;
110 struct pbias_regulator_data *drvdata;
111 struct resource *res;
112 struct regulator_config cfg = { };
113 struct regmap *syscon;
114 const struct pbias_reg_info *info;
115 int ret = 0;
116 int count, idx, data_idx = 0;
117
118 count = of_regulator_match(&pdev->dev, np, pbias_matches,
119 PBIAS_NUM_REGS);
120 if (count < 0)
121 return count;
122
123 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct pbias_regulator_data)
124 * count, GFP_KERNEL);
Jingoo Han0ee42bb2014-06-02 15:30:44 +0900125 if (!drvdata)
Balaji T K11469e02014-02-19 20:26:40 +0530126 return -ENOMEM;
Balaji T K11469e02014-02-19 20:26:40 +0530127
128 syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
129 if (IS_ERR(syscon))
130 return PTR_ERR(syscon);
131
Axel Lin60e8c1e2014-03-08 11:56:47 +0800132 cfg.regmap = syscon;
Balaji T K11469e02014-02-19 20:26:40 +0530133 cfg.dev = &pdev->dev;
134
135 for (idx = 0; idx < PBIAS_NUM_REGS && data_idx < count; idx++) {
136 if (!pbias_matches[idx].init_data ||
137 !pbias_matches[idx].of_node)
138 continue;
139
140 info = pbias_matches[idx].driver_data;
141 if (!info)
142 return -ENODEV;
143
144 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
145 if (!res)
146 return -EINVAL;
147
Balaji T K11469e02014-02-19 20:26:40 +0530148 drvdata[data_idx].syscon = syscon;
149 drvdata[data_idx].info = info;
150 drvdata[data_idx].desc.name = info->name;
151 drvdata[data_idx].desc.owner = THIS_MODULE;
152 drvdata[data_idx].desc.type = REGULATOR_VOLTAGE;
153 drvdata[data_idx].desc.ops = &pbias_regulator_voltage_ops;
Axel Lin60e8c1e2014-03-08 11:56:47 +0800154 drvdata[data_idx].desc.volt_table = pbias_volt_table;
Balaji T K11469e02014-02-19 20:26:40 +0530155 drvdata[data_idx].desc.n_voltages = 2;
156 drvdata[data_idx].desc.enable_time = info->enable_time;
Axel Lin60e8c1e2014-03-08 11:56:47 +0800157 drvdata[data_idx].desc.vsel_reg = res->start;
158 drvdata[data_idx].desc.vsel_mask = info->vmode;
159 drvdata[data_idx].desc.enable_reg = res->start;
160 drvdata[data_idx].desc.enable_mask = info->enable_mask;
Axel Lin75dbf0a2014-04-15 12:02:08 +0800161 drvdata[data_idx].desc.enable_val = info->enable;
Balaji T K11469e02014-02-19 20:26:40 +0530162
163 cfg.init_data = pbias_matches[idx].init_data;
164 cfg.driver_data = &drvdata[data_idx];
165 cfg.of_node = pbias_matches[idx].of_node;
166
167 drvdata[data_idx].dev = devm_regulator_register(&pdev->dev,
168 &drvdata[data_idx].desc, &cfg);
169 if (IS_ERR(drvdata[data_idx].dev)) {
170 ret = PTR_ERR(drvdata[data_idx].dev);
171 dev_err(&pdev->dev,
172 "Failed to register regulator: %d\n", ret);
173 goto err_regulator;
174 }
175 data_idx++;
176 }
177
178 platform_set_drvdata(pdev, drvdata);
179
180err_regulator:
181 return ret;
182}
183
184static struct platform_driver pbias_regulator_driver = {
185 .probe = pbias_regulator_probe,
186 .driver = {
187 .name = "pbias-regulator",
188 .owner = THIS_MODULE,
189 .of_match_table = of_match_ptr(pbias_of_match),
190 },
191};
192
193module_platform_driver(pbias_regulator_driver);
194
195MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
196MODULE_DESCRIPTION("pbias voltage regulator");
197MODULE_LICENSE("GPL");
198MODULE_ALIAS("platform:pbias-regulator");