111469e0bSBalaji T K /*
211469e0bSBalaji T K  * pbias-regulator.c
311469e0bSBalaji T K  *
42ca76b3eSAlexander A. Klimov  * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com/
511469e0bSBalaji T K  * Author: Balaji T K <balajitk@ti.com>
611469e0bSBalaji T K  *
711469e0bSBalaji T K  * This program is free software; you can redistribute it and/or
811469e0bSBalaji T K  * modify it under the terms of the GNU General Public License as
911469e0bSBalaji T K  * published by the Free Software Foundation version 2.
1011469e0bSBalaji T K  *
1111469e0bSBalaji T K  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
1211469e0bSBalaji T K  * kind, whether express or implied; without even the implied warranty
1311469e0bSBalaji T K  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1411469e0bSBalaji T K  * GNU General Public License for more details.
1511469e0bSBalaji T K  */
1611469e0bSBalaji T K 
1711469e0bSBalaji T K #include <linux/err.h>
1811469e0bSBalaji T K #include <linux/io.h>
1911469e0bSBalaji T K #include <linux/module.h>
2011469e0bSBalaji T K #include <linux/mfd/syscon.h>
2111469e0bSBalaji T K #include <linux/platform_device.h>
2211469e0bSBalaji T K #include <linux/regulator/driver.h>
2311469e0bSBalaji T K #include <linux/regulator/machine.h>
2411469e0bSBalaji T K #include <linux/regulator/of_regulator.h>
2511469e0bSBalaji T K #include <linux/regmap.h>
2611469e0bSBalaji T K #include <linux/slab.h>
2711469e0bSBalaji T K #include <linux/of.h>
2811469e0bSBalaji T K 
2911469e0bSBalaji T K struct pbias_reg_info {
3011469e0bSBalaji T K 	u32 enable;
3111469e0bSBalaji T K 	u32 enable_mask;
32c329061bSKishon Vijay Abraham I 	u32 disable_val;
3311469e0bSBalaji T K 	u32 vmode;
3411469e0bSBalaji T K 	unsigned int enable_time;
3511469e0bSBalaji T K 	char *name;
3627eae9d4SRavikumar Kattekola 	const unsigned int *pbias_volt_table;
3727eae9d4SRavikumar Kattekola 	int n_voltages;
3811469e0bSBalaji T K };
3911469e0bSBalaji T K 
40b9c93646SKishon Vijay Abraham I struct pbias_of_data {
41b9c93646SKishon Vijay Abraham I 	unsigned int offset;
42b9c93646SKishon Vijay Abraham I };
43b9c93646SKishon Vijay Abraham I 
4427eae9d4SRavikumar Kattekola static const unsigned int pbias_volt_table_3_0V[] = {
4560e8c1e3SAxel Lin 	1800000,
4660e8c1e3SAxel Lin 	3000000
4760e8c1e3SAxel Lin };
4811469e0bSBalaji T K 
4927eae9d4SRavikumar Kattekola static const unsigned int pbias_volt_table_3_3V[] = {
5027eae9d4SRavikumar Kattekola 	1800000,
5127eae9d4SRavikumar Kattekola 	3300000
5227eae9d4SRavikumar Kattekola };
5327eae9d4SRavikumar Kattekola 
54a180df71SBhumika Goyal static const struct regulator_ops pbias_regulator_voltage_ops = {
5560e8c1e3SAxel Lin 	.list_voltage = regulator_list_voltage_table,
5660e8c1e3SAxel Lin 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
5760e8c1e3SAxel Lin 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
5875dbf0a0SAxel Lin 	.enable = regulator_enable_regmap,
5960e8c1e3SAxel Lin 	.disable = regulator_disable_regmap,
6075dbf0a0SAxel Lin 	.is_enabled = regulator_is_enabled_regmap,
6111469e0bSBalaji T K };
6211469e0bSBalaji T K 
6311469e0bSBalaji T K static const struct pbias_reg_info pbias_mmc_omap2430 = {
6411469e0bSBalaji T K 	.enable = BIT(1),
6511469e0bSBalaji T K 	.enable_mask = BIT(1),
6611469e0bSBalaji T K 	.vmode = BIT(0),
67c329061bSKishon Vijay Abraham I 	.disable_val = 0,
6811469e0bSBalaji T K 	.enable_time = 100,
6927eae9d4SRavikumar Kattekola 	.pbias_volt_table = pbias_volt_table_3_0V,
7027eae9d4SRavikumar Kattekola 	.n_voltages = 2,
7111469e0bSBalaji T K 	.name = "pbias_mmc_omap2430"
7211469e0bSBalaji T K };
7311469e0bSBalaji T K 
7411469e0bSBalaji T K static const struct pbias_reg_info pbias_sim_omap3 = {
7511469e0bSBalaji T K 	.enable = BIT(9),
7611469e0bSBalaji T K 	.enable_mask = BIT(9),
7711469e0bSBalaji T K 	.vmode = BIT(8),
7811469e0bSBalaji T K 	.enable_time = 100,
7927eae9d4SRavikumar Kattekola 	.pbias_volt_table = pbias_volt_table_3_0V,
8027eae9d4SRavikumar Kattekola 	.n_voltages = 2,
8111469e0bSBalaji T K 	.name = "pbias_sim_omap3"
8211469e0bSBalaji T K };
8311469e0bSBalaji T K 
8411469e0bSBalaji T K static const struct pbias_reg_info pbias_mmc_omap4 = {
8511469e0bSBalaji T K 	.enable = BIT(26) | BIT(22),
8611469e0bSBalaji T K 	.enable_mask = BIT(26) | BIT(25) | BIT(22),
87c329061bSKishon Vijay Abraham I 	.disable_val = BIT(25),
8811469e0bSBalaji T K 	.vmode = BIT(21),
8911469e0bSBalaji T K 	.enable_time = 100,
9027eae9d4SRavikumar Kattekola 	.pbias_volt_table = pbias_volt_table_3_0V,
9127eae9d4SRavikumar Kattekola 	.n_voltages = 2,
9211469e0bSBalaji T K 	.name = "pbias_mmc_omap4"
9311469e0bSBalaji T K };
9411469e0bSBalaji T K 
9511469e0bSBalaji T K static const struct pbias_reg_info pbias_mmc_omap5 = {
9611469e0bSBalaji T K 	.enable = BIT(27) | BIT(26),
9711469e0bSBalaji T K 	.enable_mask = BIT(27) | BIT(25) | BIT(26),
98c329061bSKishon Vijay Abraham I 	.disable_val = BIT(25),
9911469e0bSBalaji T K 	.vmode = BIT(21),
10011469e0bSBalaji T K 	.enable_time = 100,
10127eae9d4SRavikumar Kattekola 	.pbias_volt_table = pbias_volt_table_3_3V,
10227eae9d4SRavikumar Kattekola 	.n_voltages = 2,
10311469e0bSBalaji T K 	.name = "pbias_mmc_omap5"
10411469e0bSBalaji T K };
10511469e0bSBalaji T K 
10611469e0bSBalaji T K static struct of_regulator_match pbias_matches[] = {
10711469e0bSBalaji T K 	{ .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430},
10811469e0bSBalaji T K 	{ .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3},
10911469e0bSBalaji T K 	{ .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4},
11011469e0bSBalaji T K 	{ .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5},
11111469e0bSBalaji T K };
11211469e0bSBalaji T K #define PBIAS_NUM_REGS	ARRAY_SIZE(pbias_matches)
11311469e0bSBalaji T K 
114b9c93646SKishon Vijay Abraham I /* Offset from SCM general area (and syscon) base */
115b9c93646SKishon Vijay Abraham I 
116b9c93646SKishon Vijay Abraham I static const struct pbias_of_data pbias_of_data_omap2 = {
117b9c93646SKishon Vijay Abraham I 	.offset = 0x230,
118b9c93646SKishon Vijay Abraham I };
119b9c93646SKishon Vijay Abraham I 
120b9c93646SKishon Vijay Abraham I static const struct pbias_of_data pbias_of_data_omap3 = {
121b9c93646SKishon Vijay Abraham I 	.offset = 0x2b0,
122b9c93646SKishon Vijay Abraham I };
123b9c93646SKishon Vijay Abraham I 
124b9c93646SKishon Vijay Abraham I static const struct pbias_of_data pbias_of_data_omap4 = {
125b9c93646SKishon Vijay Abraham I 	.offset = 0x60,
126b9c93646SKishon Vijay Abraham I };
127b9c93646SKishon Vijay Abraham I 
128b9c93646SKishon Vijay Abraham I static const struct pbias_of_data pbias_of_data_omap5 = {
129b9c93646SKishon Vijay Abraham I 	.offset = 0x60,
130b9c93646SKishon Vijay Abraham I };
131b9c93646SKishon Vijay Abraham I 
132b9c93646SKishon Vijay Abraham I static const struct pbias_of_data pbias_of_data_dra7 = {
133b9c93646SKishon Vijay Abraham I 	.offset = 0xe00,
134b9c93646SKishon Vijay Abraham I };
135b9c93646SKishon Vijay Abraham I 
13611469e0bSBalaji T K static const struct of_device_id pbias_of_match[] = {
13711469e0bSBalaji T K 	{ .compatible = "ti,pbias-omap", },
138b9c93646SKishon Vijay Abraham I 	{ .compatible = "ti,pbias-omap2", .data = &pbias_of_data_omap2, },
139b9c93646SKishon Vijay Abraham I 	{ .compatible = "ti,pbias-omap3", .data = &pbias_of_data_omap3, },
140b9c93646SKishon Vijay Abraham I 	{ .compatible = "ti,pbias-omap4", .data = &pbias_of_data_omap4, },
141b9c93646SKishon Vijay Abraham I 	{ .compatible = "ti,pbias-omap5", .data = &pbias_of_data_omap5, },
142b9c93646SKishon Vijay Abraham I 	{ .compatible = "ti,pbias-dra7", .data = &pbias_of_data_dra7, },
14311469e0bSBalaji T K 	{},
14411469e0bSBalaji T K };
14511469e0bSBalaji T K MODULE_DEVICE_TABLE(of, pbias_of_match);
14611469e0bSBalaji T K 
pbias_regulator_probe(struct platform_device * pdev)14711469e0bSBalaji T K static int pbias_regulator_probe(struct platform_device *pdev)
14811469e0bSBalaji T K {
14911469e0bSBalaji T K 	struct device_node *np = pdev->dev.of_node;
15011469e0bSBalaji T K 	struct resource *res;
15111469e0bSBalaji T K 	struct regulator_config cfg = { };
152df8c542eSAxel Lin 	struct regulator_desc *desc;
153df8c542eSAxel Lin 	struct regulator_dev *rdev;
15411469e0bSBalaji T K 	struct regmap *syscon;
15511469e0bSBalaji T K 	const struct pbias_reg_info *info;
156df8c542eSAxel Lin 	int ret, count, idx;
157b9c93646SKishon Vijay Abraham I 	const struct pbias_of_data *data;
158b9c93646SKishon Vijay Abraham I 	unsigned int offset;
15911469e0bSBalaji T K 
16011469e0bSBalaji T K 	count = of_regulator_match(&pdev->dev, np, pbias_matches,
16111469e0bSBalaji T K 						PBIAS_NUM_REGS);
16211469e0bSBalaji T K 	if (count < 0)
16311469e0bSBalaji T K 		return count;
16411469e0bSBalaji T K 
165df8c542eSAxel Lin 	desc = devm_kcalloc(&pdev->dev, count, sizeof(*desc), GFP_KERNEL);
166df8c542eSAxel Lin 	if (!desc)
16711469e0bSBalaji T K 		return -ENOMEM;
16811469e0bSBalaji T K 
16911469e0bSBalaji T K 	syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
17011469e0bSBalaji T K 	if (IS_ERR(syscon))
17111469e0bSBalaji T K 		return PTR_ERR(syscon);
17211469e0bSBalaji T K 
17341145b98SAxel Lin 	data = of_device_get_match_data(&pdev->dev);
17441145b98SAxel Lin 	if (data) {
175b9c93646SKishon Vijay Abraham I 		offset = data->offset;
176b9c93646SKishon Vijay Abraham I 	} else {
177b9c93646SKishon Vijay Abraham I 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
178b9c93646SKishon Vijay Abraham I 		if (!res)
179b9c93646SKishon Vijay Abraham I 			return -EINVAL;
180b9c93646SKishon Vijay Abraham I 
181b9c93646SKishon Vijay Abraham I 		offset = res->start;
182b9c93646SKishon Vijay Abraham I 		dev_WARN(&pdev->dev,
183b9c93646SKishon Vijay Abraham I 			 "using legacy dt data for pbias offset\n");
184b9c93646SKishon Vijay Abraham I 	}
185b9c93646SKishon Vijay Abraham I 
18660e8c1e3SAxel Lin 	cfg.regmap = syscon;
18711469e0bSBalaji T K 	cfg.dev = &pdev->dev;
18811469e0bSBalaji T K 
189df8c542eSAxel Lin 	for (idx = 0; idx < PBIAS_NUM_REGS && count; idx++) {
19011469e0bSBalaji T K 		if (!pbias_matches[idx].init_data ||
19111469e0bSBalaji T K 			!pbias_matches[idx].of_node)
19211469e0bSBalaji T K 			continue;
19311469e0bSBalaji T K 
19411469e0bSBalaji T K 		info = pbias_matches[idx].driver_data;
19511469e0bSBalaji T K 		if (!info)
19611469e0bSBalaji T K 			return -ENODEV;
19711469e0bSBalaji T K 
198df8c542eSAxel Lin 		desc->name = info->name;
199df8c542eSAxel Lin 		desc->owner = THIS_MODULE;
200df8c542eSAxel Lin 		desc->type = REGULATOR_VOLTAGE;
201df8c542eSAxel Lin 		desc->ops = &pbias_regulator_voltage_ops;
202df8c542eSAxel Lin 		desc->volt_table = info->pbias_volt_table;
203df8c542eSAxel Lin 		desc->n_voltages = info->n_voltages;
204df8c542eSAxel Lin 		desc->enable_time = info->enable_time;
205df8c542eSAxel Lin 		desc->vsel_reg = offset;
206df8c542eSAxel Lin 		desc->vsel_mask = info->vmode;
207df8c542eSAxel Lin 		desc->enable_reg = offset;
208df8c542eSAxel Lin 		desc->enable_mask = info->enable_mask;
209df8c542eSAxel Lin 		desc->enable_val = info->enable;
210df8c542eSAxel Lin 		desc->disable_val = info->disable_val;
21111469e0bSBalaji T K 
21211469e0bSBalaji T K 		cfg.init_data = pbias_matches[idx].init_data;
21311469e0bSBalaji T K 		cfg.of_node = pbias_matches[idx].of_node;
21411469e0bSBalaji T K 
215df8c542eSAxel Lin 		rdev = devm_regulator_register(&pdev->dev, desc, &cfg);
216df8c542eSAxel Lin 		if (IS_ERR(rdev)) {
217df8c542eSAxel Lin 			ret = PTR_ERR(rdev);
21811469e0bSBalaji T K 			dev_err(&pdev->dev,
21911469e0bSBalaji T K 				"Failed to register regulator: %d\n", ret);
22011469e0bSBalaji T K 			return ret;
22111469e0bSBalaji T K 		}
222df8c542eSAxel Lin 		desc++;
223df8c542eSAxel Lin 		count--;
224df8c542eSAxel Lin 	}
225df8c542eSAxel Lin 
226df8c542eSAxel Lin 	return 0;
227df8c542eSAxel Lin }
22811469e0bSBalaji T K 
22911469e0bSBalaji T K static struct platform_driver pbias_regulator_driver = {
23011469e0bSBalaji T K 	.probe		= pbias_regulator_probe,
23111469e0bSBalaji T K 	.driver		= {
23211469e0bSBalaji T K 		.name		= "pbias-regulator",
233*259b93b2SDouglas Anderson 		.probe_type	= PROBE_PREFER_ASYNCHRONOUS,
23411469e0bSBalaji T K 		.of_match_table = of_match_ptr(pbias_of_match),
23511469e0bSBalaji T K 	},
23611469e0bSBalaji T K };
23711469e0bSBalaji T K 
23811469e0bSBalaji T K module_platform_driver(pbias_regulator_driver);
23911469e0bSBalaji T K 
24011469e0bSBalaji T K MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
24111469e0bSBalaji T K MODULE_DESCRIPTION("pbias voltage regulator");
24211469e0bSBalaji T K MODULE_LICENSE("GPL");
24311469e0bSBalaji T K MODULE_ALIAS("platform:pbias-regulator");
244