1 /*
2  * Regulator driver for tps65090 power management chip.
3  *
4  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
5 
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9 
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>
17  */
18 
19 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/err.h>
24 #include <linux/platform_device.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/machine.h>
27 #include <linux/mfd/tps65090.h>
28 #include <linux/regulator/tps65090-regulator.h>
29 
30 struct tps65090_regulator {
31 	int		id;
32 	/* Regulator register address.*/
33 	u8		reg_en_reg;
34 	u8		en_bit;
35 
36 	/* used by regulator core */
37 	struct regulator_desc	desc;
38 
39 	/* Device */
40 	struct device		*dev;
41 };
42 
43 static inline struct device *to_tps65090_dev(struct regulator_dev *rdev)
44 {
45 	return rdev_get_dev(rdev)->parent->parent;
46 }
47 
48 static int tps65090_reg_is_enabled(struct regulator_dev *rdev)
49 {
50 	struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
51 	struct device *parent = to_tps65090_dev(rdev);
52 	uint8_t control;
53 	int ret;
54 
55 	ret = tps65090_read(parent, ri->reg_en_reg, &control);
56 	if (ret < 0) {
57 		dev_err(&rdev->dev, "Error in reading reg 0x%x\n",
58 			ri->reg_en_reg);
59 		return ret;
60 	}
61 	return (((control >> ri->en_bit) & 1) == 1);
62 }
63 
64 static int tps65090_reg_enable(struct regulator_dev *rdev)
65 {
66 	struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
67 	struct device *parent = to_tps65090_dev(rdev);
68 	int ret;
69 
70 	ret = tps65090_set_bits(parent, ri->reg_en_reg, ri->en_bit);
71 	if (ret < 0)
72 		dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
73 			ri->reg_en_reg);
74 	return ret;
75 }
76 
77 static int tps65090_reg_disable(struct regulator_dev *rdev)
78 {
79 	struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
80 	struct device *parent = to_tps65090_dev(rdev);
81 	int ret;
82 
83 	ret = tps65090_clr_bits(parent, ri->reg_en_reg, ri->en_bit);
84 	if (ret < 0)
85 		dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
86 			ri->reg_en_reg);
87 
88 	return ret;
89 }
90 
91 static struct regulator_ops tps65090_ops = {
92 	.enable		= tps65090_reg_enable,
93 	.disable	= tps65090_reg_disable,
94 	.is_enabled	= tps65090_reg_is_enabled,
95 };
96 
97 #define tps65090_REG(_id)				\
98 {							\
99 	.reg_en_reg	= (TPS65090_ID_##_id) + 12,	\
100 	.en_bit		= 0,				\
101 	.id		= TPS65090_ID_##_id,		\
102 	.desc = {					\
103 		.name = tps65090_rails(_id),		\
104 		.id = TPS65090_ID_##_id,		\
105 		.ops = &tps65090_ops,			\
106 		.type = REGULATOR_VOLTAGE,		\
107 		.owner = THIS_MODULE,			\
108 	},						\
109 }
110 
111 static struct tps65090_regulator TPS65090_regulator[] = {
112 	tps65090_REG(DCDC1),
113 	tps65090_REG(DCDC2),
114 	tps65090_REG(DCDC3),
115 	tps65090_REG(FET1),
116 	tps65090_REG(FET2),
117 	tps65090_REG(FET3),
118 	tps65090_REG(FET4),
119 	tps65090_REG(FET5),
120 	tps65090_REG(FET6),
121 	tps65090_REG(FET7),
122 };
123 
124 static inline struct tps65090_regulator *find_regulator_info(int id)
125 {
126 	struct tps65090_regulator *ri;
127 	int i;
128 
129 	for (i = 0; i < ARRAY_SIZE(TPS65090_regulator); i++) {
130 		ri = &TPS65090_regulator[i];
131 		if (ri->desc.id == id)
132 			return ri;
133 	}
134 	return NULL;
135 }
136 
137 static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
138 {
139 	struct tps65090_regulator *ri = NULL;
140 	struct regulator_config config = { };
141 	struct regulator_dev *rdev;
142 	struct tps65090_regulator_platform_data *tps_pdata;
143 	int id = pdev->id;
144 
145 	dev_dbg(&pdev->dev, "Probing regulator %d\n", id);
146 
147 	ri = find_regulator_info(id);
148 	if (ri == NULL) {
149 		dev_err(&pdev->dev, "invalid regulator ID specified\n");
150 		return -EINVAL;
151 	}
152 	tps_pdata = pdev->dev.platform_data;
153 	ri->dev = &pdev->dev;
154 
155 	config.dev = &pdev->dev;
156 	config.init_data = &tps_pdata->regulator;
157 	config.driver_data = ri;
158 
159 	rdev = regulator_register(&ri->desc, &config);
160 	if (IS_ERR(rdev)) {
161 		dev_err(&pdev->dev, "failed to register regulator %s\n",
162 				ri->desc.name);
163 		return PTR_ERR(rdev);
164 	}
165 
166 	platform_set_drvdata(pdev, rdev);
167 	return 0;
168 }
169 
170 static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
171 {
172 	struct regulator_dev *rdev = platform_get_drvdata(pdev);
173 
174 	regulator_unregister(rdev);
175 	return 0;
176 }
177 
178 static struct platform_driver tps65090_regulator_driver = {
179 	.driver	= {
180 		.name	= "tps65090-regulator",
181 		.owner	= THIS_MODULE,
182 	},
183 	.probe		= tps65090_regulator_probe,
184 	.remove		= __devexit_p(tps65090_regulator_remove),
185 };
186 
187 static int __init tps65090_regulator_init(void)
188 {
189 	return platform_driver_register(&tps65090_regulator_driver);
190 }
191 subsys_initcall(tps65090_regulator_init);
192 
193 static void __exit tps65090_regulator_exit(void)
194 {
195 	platform_driver_unregister(&tps65090_regulator_driver);
196 }
197 module_exit(tps65090_regulator_exit);
198 
199 MODULE_DESCRIPTION("tps65090 regulator driver");
200 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
201 MODULE_LICENSE("GPL v2");
202