111469e0bSBalaji T K /*
211469e0bSBalaji T K  * pbias-regulator.c
311469e0bSBalaji T K  *
411469e0bSBalaji T K  * Copyright (C) 2014 Texas Instruments Incorporated - http://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 #include <linux/of_device.h>
2911469e0bSBalaji T K 
3011469e0bSBalaji T K struct pbias_reg_info {
3111469e0bSBalaji T K 	u32 enable;
3211469e0bSBalaji T K 	u32 enable_mask;
3311469e0bSBalaji T K 	u32 vmode;
3411469e0bSBalaji T K 	unsigned int enable_time;
3511469e0bSBalaji T K 	char *name;
3611469e0bSBalaji T K };
3711469e0bSBalaji T K 
3811469e0bSBalaji T K struct pbias_regulator_data {
3911469e0bSBalaji T K 	struct regulator_desc desc;
4011469e0bSBalaji T K 	void __iomem *pbias_addr;
4111469e0bSBalaji T K 	struct regulator_dev *dev;
4211469e0bSBalaji T K 	struct regmap *syscon;
4311469e0bSBalaji T K 	const struct pbias_reg_info *info;
4411469e0bSBalaji T K 	int voltage;
4511469e0bSBalaji T K };
4611469e0bSBalaji T K 
4760e8c1e3SAxel Lin static const unsigned int pbias_volt_table[] = {
4860e8c1e3SAxel Lin 	1800000,
4960e8c1e3SAxel Lin 	3000000
5060e8c1e3SAxel Lin };
5111469e0bSBalaji T K 
5211469e0bSBalaji T K static struct regulator_ops pbias_regulator_voltage_ops = {
5360e8c1e3SAxel Lin 	.list_voltage = regulator_list_voltage_table,
5460e8c1e3SAxel Lin 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
5560e8c1e3SAxel Lin 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
5675dbf0a0SAxel Lin 	.enable = regulator_enable_regmap,
5760e8c1e3SAxel Lin 	.disable = regulator_disable_regmap,
5875dbf0a0SAxel Lin 	.is_enabled = regulator_is_enabled_regmap,
5911469e0bSBalaji T K };
6011469e0bSBalaji T K 
6111469e0bSBalaji T K static const struct pbias_reg_info pbias_mmc_omap2430 = {
6211469e0bSBalaji T K 	.enable = BIT(1),
6311469e0bSBalaji T K 	.enable_mask = BIT(1),
6411469e0bSBalaji T K 	.vmode = BIT(0),
6511469e0bSBalaji T K 	.enable_time = 100,
6611469e0bSBalaji T K 	.name = "pbias_mmc_omap2430"
6711469e0bSBalaji T K };
6811469e0bSBalaji T K 
6911469e0bSBalaji T K static const struct pbias_reg_info pbias_sim_omap3 = {
7011469e0bSBalaji T K 	.enable = BIT(9),
7111469e0bSBalaji T K 	.enable_mask = BIT(9),
7211469e0bSBalaji T K 	.vmode = BIT(8),
7311469e0bSBalaji T K 	.enable_time = 100,
7411469e0bSBalaji T K 	.name = "pbias_sim_omap3"
7511469e0bSBalaji T K };
7611469e0bSBalaji T K 
7711469e0bSBalaji T K static const struct pbias_reg_info pbias_mmc_omap4 = {
7811469e0bSBalaji T K 	.enable = BIT(26) | BIT(22),
7911469e0bSBalaji T K 	.enable_mask = BIT(26) | BIT(25) | BIT(22),
8011469e0bSBalaji T K 	.vmode = BIT(21),
8111469e0bSBalaji T K 	.enable_time = 100,
8211469e0bSBalaji T K 	.name = "pbias_mmc_omap4"
8311469e0bSBalaji T K };
8411469e0bSBalaji T K 
8511469e0bSBalaji T K static const struct pbias_reg_info pbias_mmc_omap5 = {
8611469e0bSBalaji T K 	.enable = BIT(27) | BIT(26),
8711469e0bSBalaji T K 	.enable_mask = BIT(27) | BIT(25) | BIT(26),
8811469e0bSBalaji T K 	.vmode = BIT(21),
8911469e0bSBalaji T K 	.enable_time = 100,
9011469e0bSBalaji T K 	.name = "pbias_mmc_omap5"
9111469e0bSBalaji T K };
9211469e0bSBalaji T K 
9311469e0bSBalaji T K static struct of_regulator_match pbias_matches[] = {
9411469e0bSBalaji T K 	{ .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430},
9511469e0bSBalaji T K 	{ .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3},
9611469e0bSBalaji T K 	{ .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4},
9711469e0bSBalaji T K 	{ .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5},
9811469e0bSBalaji T K };
9911469e0bSBalaji T K #define PBIAS_NUM_REGS	ARRAY_SIZE(pbias_matches)
10011469e0bSBalaji T K 
10111469e0bSBalaji T K static const struct of_device_id pbias_of_match[] = {
10211469e0bSBalaji T K 	{ .compatible = "ti,pbias-omap", },
10311469e0bSBalaji T K 	{},
10411469e0bSBalaji T K };
10511469e0bSBalaji T K MODULE_DEVICE_TABLE(of, pbias_of_match);
10611469e0bSBalaji T K 
10711469e0bSBalaji T K static int pbias_regulator_probe(struct platform_device *pdev)
10811469e0bSBalaji T K {
10911469e0bSBalaji T K 	struct device_node *np = pdev->dev.of_node;
11011469e0bSBalaji T K 	struct pbias_regulator_data *drvdata;
11111469e0bSBalaji T K 	struct resource *res;
11211469e0bSBalaji T K 	struct regulator_config cfg = { };
11311469e0bSBalaji T K 	struct regmap *syscon;
11411469e0bSBalaji T K 	const struct pbias_reg_info *info;
11511469e0bSBalaji T K 	int ret = 0;
11611469e0bSBalaji T K 	int count, idx, data_idx = 0;
11711469e0bSBalaji T K 
11811469e0bSBalaji T K 	count = of_regulator_match(&pdev->dev, np, pbias_matches,
11911469e0bSBalaji T K 						PBIAS_NUM_REGS);
12011469e0bSBalaji T K 	if (count < 0)
12111469e0bSBalaji T K 		return count;
12211469e0bSBalaji T K 
12311469e0bSBalaji T K 	drvdata = devm_kzalloc(&pdev->dev, sizeof(struct pbias_regulator_data)
12411469e0bSBalaji T K 			       * count, GFP_KERNEL);
12511469e0bSBalaji T K 	if (drvdata == NULL) {
12611469e0bSBalaji T K 		dev_err(&pdev->dev, "Failed to allocate device data\n");
12711469e0bSBalaji T K 		return -ENOMEM;
12811469e0bSBalaji T K 	}
12911469e0bSBalaji T K 
13011469e0bSBalaji T K 	syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
13111469e0bSBalaji T K 	if (IS_ERR(syscon))
13211469e0bSBalaji T K 		return PTR_ERR(syscon);
13311469e0bSBalaji T K 
13460e8c1e3SAxel Lin 	cfg.regmap = syscon;
13511469e0bSBalaji T K 	cfg.dev = &pdev->dev;
13611469e0bSBalaji T K 
13711469e0bSBalaji T K 	for (idx = 0; idx < PBIAS_NUM_REGS && data_idx < count; idx++) {
13811469e0bSBalaji T K 		if (!pbias_matches[idx].init_data ||
13911469e0bSBalaji T K 			!pbias_matches[idx].of_node)
14011469e0bSBalaji T K 			continue;
14111469e0bSBalaji T K 
14211469e0bSBalaji T K 		info = pbias_matches[idx].driver_data;
14311469e0bSBalaji T K 		if (!info)
14411469e0bSBalaji T K 			return -ENODEV;
14511469e0bSBalaji T K 
14611469e0bSBalaji T K 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
14711469e0bSBalaji T K 		if (!res)
14811469e0bSBalaji T K 			return -EINVAL;
14911469e0bSBalaji T K 
15011469e0bSBalaji T K 		drvdata[data_idx].syscon = syscon;
15111469e0bSBalaji T K 		drvdata[data_idx].info = info;
15211469e0bSBalaji T K 		drvdata[data_idx].desc.name = info->name;
15311469e0bSBalaji T K 		drvdata[data_idx].desc.owner = THIS_MODULE;
15411469e0bSBalaji T K 		drvdata[data_idx].desc.type = REGULATOR_VOLTAGE;
15511469e0bSBalaji T K 		drvdata[data_idx].desc.ops = &pbias_regulator_voltage_ops;
15660e8c1e3SAxel Lin 		drvdata[data_idx].desc.volt_table = pbias_volt_table;
15711469e0bSBalaji T K 		drvdata[data_idx].desc.n_voltages = 2;
15811469e0bSBalaji T K 		drvdata[data_idx].desc.enable_time = info->enable_time;
15960e8c1e3SAxel Lin 		drvdata[data_idx].desc.vsel_reg = res->start;
16060e8c1e3SAxel Lin 		drvdata[data_idx].desc.vsel_mask = info->vmode;
16160e8c1e3SAxel Lin 		drvdata[data_idx].desc.enable_reg = res->start;
16260e8c1e3SAxel Lin 		drvdata[data_idx].desc.enable_mask = info->enable_mask;
16375dbf0a0SAxel Lin 		drvdata[data_idx].desc.enable_val = info->enable;
16411469e0bSBalaji T K 
16511469e0bSBalaji T K 		cfg.init_data = pbias_matches[idx].init_data;
16611469e0bSBalaji T K 		cfg.driver_data = &drvdata[data_idx];
16711469e0bSBalaji T K 		cfg.of_node = pbias_matches[idx].of_node;
16811469e0bSBalaji T K 
16911469e0bSBalaji T K 		drvdata[data_idx].dev = devm_regulator_register(&pdev->dev,
17011469e0bSBalaji T K 					&drvdata[data_idx].desc, &cfg);
17111469e0bSBalaji T K 		if (IS_ERR(drvdata[data_idx].dev)) {
17211469e0bSBalaji T K 			ret = PTR_ERR(drvdata[data_idx].dev);
17311469e0bSBalaji T K 			dev_err(&pdev->dev,
17411469e0bSBalaji T K 				"Failed to register regulator: %d\n", ret);
17511469e0bSBalaji T K 			goto err_regulator;
17611469e0bSBalaji T K 		}
17711469e0bSBalaji T K 		data_idx++;
17811469e0bSBalaji T K 	}
17911469e0bSBalaji T K 
18011469e0bSBalaji T K 	platform_set_drvdata(pdev, drvdata);
18111469e0bSBalaji T K 
18211469e0bSBalaji T K err_regulator:
18311469e0bSBalaji T K 	return ret;
18411469e0bSBalaji T K }
18511469e0bSBalaji T K 
18611469e0bSBalaji T K static struct platform_driver pbias_regulator_driver = {
18711469e0bSBalaji T K 	.probe		= pbias_regulator_probe,
18811469e0bSBalaji T K 	.driver		= {
18911469e0bSBalaji T K 		.name		= "pbias-regulator",
19011469e0bSBalaji T K 		.owner		= THIS_MODULE,
19111469e0bSBalaji T K 		.of_match_table = of_match_ptr(pbias_of_match),
19211469e0bSBalaji T K 	},
19311469e0bSBalaji T K };
19411469e0bSBalaji T K 
19511469e0bSBalaji T K module_platform_driver(pbias_regulator_driver);
19611469e0bSBalaji T K 
19711469e0bSBalaji T K MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
19811469e0bSBalaji T K MODULE_DESCRIPTION("pbias voltage regulator");
19911469e0bSBalaji T K MODULE_LICENSE("GPL");
20011469e0bSBalaji T K MODULE_ALIAS("platform:pbias-regulator");
201