1 /*
2  * pbias-regulator.c
3  *
4  * Copyright (C) 2014 Texas Instruments Incorporated - https://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 
29 struct pbias_reg_info {
30 	u32 enable;
31 	u32 enable_mask;
32 	u32 disable_val;
33 	u32 vmode;
34 	unsigned int enable_time;
35 	char *name;
36 	const unsigned int *pbias_volt_table;
37 	int n_voltages;
38 };
39 
40 struct pbias_of_data {
41 	unsigned int offset;
42 };
43 
44 static const unsigned int pbias_volt_table_3_0V[] = {
45 	1800000,
46 	3000000
47 };
48 
49 static const unsigned int pbias_volt_table_3_3V[] = {
50 	1800000,
51 	3300000
52 };
53 
54 static const struct regulator_ops pbias_regulator_voltage_ops = {
55 	.list_voltage = regulator_list_voltage_table,
56 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
57 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
58 	.enable = regulator_enable_regmap,
59 	.disable = regulator_disable_regmap,
60 	.is_enabled = regulator_is_enabled_regmap,
61 };
62 
63 static const struct pbias_reg_info pbias_mmc_omap2430 = {
64 	.enable = BIT(1),
65 	.enable_mask = BIT(1),
66 	.vmode = BIT(0),
67 	.disable_val = 0,
68 	.enable_time = 100,
69 	.pbias_volt_table = pbias_volt_table_3_0V,
70 	.n_voltages = 2,
71 	.name = "pbias_mmc_omap2430"
72 };
73 
74 static const struct pbias_reg_info pbias_sim_omap3 = {
75 	.enable = BIT(9),
76 	.enable_mask = BIT(9),
77 	.vmode = BIT(8),
78 	.enable_time = 100,
79 	.pbias_volt_table = pbias_volt_table_3_0V,
80 	.n_voltages = 2,
81 	.name = "pbias_sim_omap3"
82 };
83 
84 static const struct pbias_reg_info pbias_mmc_omap4 = {
85 	.enable = BIT(26) | BIT(22),
86 	.enable_mask = BIT(26) | BIT(25) | BIT(22),
87 	.disable_val = BIT(25),
88 	.vmode = BIT(21),
89 	.enable_time = 100,
90 	.pbias_volt_table = pbias_volt_table_3_0V,
91 	.n_voltages = 2,
92 	.name = "pbias_mmc_omap4"
93 };
94 
95 static const struct pbias_reg_info pbias_mmc_omap5 = {
96 	.enable = BIT(27) | BIT(26),
97 	.enable_mask = BIT(27) | BIT(25) | BIT(26),
98 	.disable_val = BIT(25),
99 	.vmode = BIT(21),
100 	.enable_time = 100,
101 	.pbias_volt_table = pbias_volt_table_3_3V,
102 	.n_voltages = 2,
103 	.name = "pbias_mmc_omap5"
104 };
105 
106 static struct of_regulator_match pbias_matches[] = {
107 	{ .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430},
108 	{ .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3},
109 	{ .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4},
110 	{ .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5},
111 };
112 #define PBIAS_NUM_REGS	ARRAY_SIZE(pbias_matches)
113 
114 /* Offset from SCM general area (and syscon) base */
115 
116 static const struct pbias_of_data pbias_of_data_omap2 = {
117 	.offset = 0x230,
118 };
119 
120 static const struct pbias_of_data pbias_of_data_omap3 = {
121 	.offset = 0x2b0,
122 };
123 
124 static const struct pbias_of_data pbias_of_data_omap4 = {
125 	.offset = 0x60,
126 };
127 
128 static const struct pbias_of_data pbias_of_data_omap5 = {
129 	.offset = 0x60,
130 };
131 
132 static const struct pbias_of_data pbias_of_data_dra7 = {
133 	.offset = 0xe00,
134 };
135 
136 static const struct of_device_id pbias_of_match[] = {
137 	{ .compatible = "ti,pbias-omap", },
138 	{ .compatible = "ti,pbias-omap2", .data = &pbias_of_data_omap2, },
139 	{ .compatible = "ti,pbias-omap3", .data = &pbias_of_data_omap3, },
140 	{ .compatible = "ti,pbias-omap4", .data = &pbias_of_data_omap4, },
141 	{ .compatible = "ti,pbias-omap5", .data = &pbias_of_data_omap5, },
142 	{ .compatible = "ti,pbias-dra7", .data = &pbias_of_data_dra7, },
143 	{},
144 };
145 MODULE_DEVICE_TABLE(of, pbias_of_match);
146 
147 static int pbias_regulator_probe(struct platform_device *pdev)
148 {
149 	struct device_node *np = pdev->dev.of_node;
150 	struct resource *res;
151 	struct regulator_config cfg = { };
152 	struct regulator_desc *desc;
153 	struct regulator_dev *rdev;
154 	struct regmap *syscon;
155 	const struct pbias_reg_info *info;
156 	int ret, count, idx;
157 	const struct pbias_of_data *data;
158 	unsigned int offset;
159 
160 	count = of_regulator_match(&pdev->dev, np, pbias_matches,
161 						PBIAS_NUM_REGS);
162 	if (count < 0)
163 		return count;
164 
165 	desc = devm_kcalloc(&pdev->dev, count, sizeof(*desc), GFP_KERNEL);
166 	if (!desc)
167 		return -ENOMEM;
168 
169 	syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
170 	if (IS_ERR(syscon))
171 		return PTR_ERR(syscon);
172 
173 	data = of_device_get_match_data(&pdev->dev);
174 	if (data) {
175 		offset = data->offset;
176 	} else {
177 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
178 		if (!res)
179 			return -EINVAL;
180 
181 		offset = res->start;
182 		dev_WARN(&pdev->dev,
183 			 "using legacy dt data for pbias offset\n");
184 	}
185 
186 	cfg.regmap = syscon;
187 	cfg.dev = &pdev->dev;
188 
189 	for (idx = 0; idx < PBIAS_NUM_REGS && count; idx++) {
190 		if (!pbias_matches[idx].init_data ||
191 			!pbias_matches[idx].of_node)
192 			continue;
193 
194 		info = pbias_matches[idx].driver_data;
195 		if (!info)
196 			return -ENODEV;
197 
198 		desc->name = info->name;
199 		desc->owner = THIS_MODULE;
200 		desc->type = REGULATOR_VOLTAGE;
201 		desc->ops = &pbias_regulator_voltage_ops;
202 		desc->volt_table = info->pbias_volt_table;
203 		desc->n_voltages = info->n_voltages;
204 		desc->enable_time = info->enable_time;
205 		desc->vsel_reg = offset;
206 		desc->vsel_mask = info->vmode;
207 		desc->enable_reg = offset;
208 		desc->enable_mask = info->enable_mask;
209 		desc->enable_val = info->enable;
210 		desc->disable_val = info->disable_val;
211 
212 		cfg.init_data = pbias_matches[idx].init_data;
213 		cfg.of_node = pbias_matches[idx].of_node;
214 
215 		rdev = devm_regulator_register(&pdev->dev, desc, &cfg);
216 		if (IS_ERR(rdev)) {
217 			ret = PTR_ERR(rdev);
218 			dev_err(&pdev->dev,
219 				"Failed to register regulator: %d\n", ret);
220 			return ret;
221 		}
222 		desc++;
223 		count--;
224 	}
225 
226 	return 0;
227 }
228 
229 static struct platform_driver pbias_regulator_driver = {
230 	.probe		= pbias_regulator_probe,
231 	.driver		= {
232 		.name		= "pbias-regulator",
233 		.probe_type	= PROBE_PREFER_ASYNCHRONOUS,
234 		.of_match_table = of_match_ptr(pbias_of_match),
235 	},
236 };
237 
238 module_platform_driver(pbias_regulator_driver);
239 
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");
244