summaryrefslogtreecommitdiff
path: root/drivers/regulator/reg-mc34704.c
blob: 7fb3732b9e03f6f5061db498248fdfd525f96cf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/bitops.h>
#include <linux/err.h>
#include <linux/ioctl.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/driver.h>
#include <linux/mfd/mc34704/core.h>
#include <linux/platform_device.h>
#include <linux/pmic_status.h>
#include <linux/pmic_external.h>

#define MC34704_ONOFFA	0x8
#define MC34704_ONOFFC	0x4
#define MC34704_ONOFFD	0x2
#define MC34704_ONOFFE	0x1

/* Private data for MC34704 regulators */

struct reg_mc34704_priv {
	short enable;		/* enable bit, if available */
	short v_default;	/* default regulator voltage in mV */
	int dvs_min;		/* minimum voltage change in units of 2.5% */
	int dvs_max;		/* maximum voltage change in units of 2.5% */
	char i2c_dvs;		/* i2c DVS register number */
	char i2c_stat;		/* i2c status register number */
};
struct reg_mc34704_priv mc34704_reg_priv[] = {
	{
	 .v_default = REG1_V_MV,
	 .dvs_min = REG1_DVS_MIN_PCT / 2.5,
	 .dvs_max = REG1_DVS_MAX_PCT / 2.5,
	 .i2c_dvs = 0x4,
	 .i2c_stat = 0x5,
	 .enable = MC34704_ONOFFA,
	 },
	{
	 .v_default = REG2_V_MV,
	 .dvs_min = REG2_DVS_MIN_PCT / 2.5,
	 .dvs_max = REG2_DVS_MAX_PCT / 2.5,
	 .i2c_dvs = 0x6,
	 .i2c_stat = 0x7,
	 },
	{
	 .v_default = REG3_V_MV,
	 .dvs_min = REG3_DVS_MIN_PCT / 2.5,
	 .dvs_max = REG3_DVS_MAX_PCT / 2.5,
	 .i2c_dvs = 0x8,
	 .i2c_stat = 0x9,
	 },
	{
	 .v_default = REG4_V_MV,
	 .dvs_min = REG4_DVS_MIN_PCT / 2.5,
	 .dvs_max = REG4_DVS_MAX_PCT / 2.5,
	 .i2c_dvs = 0xA,
	 .i2c_stat = 0xB,
	 },
	{
	 .v_default = REG5_V_MV,
	 .dvs_min = REG5_DVS_MIN_PCT / 2.5,
	 .dvs_max = REG5_DVS_MAX_PCT / 2.5,
	 .i2c_dvs = 0xC,
	 .i2c_stat = 0xE,
	 .enable = MC34704_ONOFFE,
	 },
};

static int mc34704_set_voltage(struct regulator_dev *reg, int MiniV, int uV)
{
	struct reg_mc34704_priv *priv = rdev_get_drvdata(reg);
	int mV = uV / 1000;
	int dV = mV - priv->v_default;

	/* compute dynamic voltage scaling value */
	int dvs = 1000 * dV / priv->v_default / 25;

	/* clip to regulator limits */
	if (dvs > priv->dvs_max)
		dvs = priv->dvs_max;
	if (dvs < priv->dvs_min)
		dvs = priv->dvs_min;

	return pmic_write_reg(priv->i2c_dvs, dvs << 1, 0x1E);
}

static int mc34704_get_voltage(struct regulator_dev *reg)
{
	int mV;
	struct reg_mc34704_priv *priv = rdev_get_drvdata(reg);
	int val, dvs;

	CHECK_ERROR(pmic_read_reg(priv->i2c_dvs, &val, 0xF));

	dvs = (val >> 1) & 0xF;

	/* dvs is 4-bit 2's complement; sign-extend it */
	if (dvs & 8)
		dvs |= -1 & ~0xF;

	/* Regulator voltage is adjusted by (dvs * 2.5%) */
	mV = priv->v_default * (1000 + 25 * dvs) / 1000;

	return 1000 * mV;
}

static int mc34704_enable_reg(struct regulator_dev *reg)
{
	struct reg_mc34704_priv *priv = rdev_get_drvdata(reg);

	if (priv->enable)
		return pmic_write_reg(REG_MC34704_GENERAL2, -1, priv->enable);

	return PMIC_ERROR;
}

static int mc34704_disable_reg(struct regulator_dev *reg)
{
	struct reg_mc34704_priv *priv = rdev_get_drvdata(reg);

	if (priv->enable)
		return pmic_write_reg(REG_MC34704_GENERAL2, 0, priv->enable);

	return PMIC_ERROR;
}

static int mc34704_is_reg_enabled(struct regulator_dev *reg)
{
	struct reg_mc34704_priv *priv = rdev_get_drvdata(reg);
	int val;

	if (priv->enable) {
		CHECK_ERROR(pmic_read_reg(REG_MC34704_GENERAL2, &val,
					  priv->enable));
		return val ? 1 : 0;
	} else {
		return PMIC_ERROR;
	}
}

static struct regulator_ops mc34704_full_ops = {
	.set_voltage = mc34704_set_voltage,
	.get_voltage = mc34704_get_voltage,
	.enable = mc34704_enable_reg,
	.disable = mc34704_disable_reg,
	.is_enabled = mc34704_is_reg_enabled,
};

static struct regulator_ops mc34704_partial_ops = {
	.set_voltage = mc34704_set_voltage,
	.get_voltage = mc34704_get_voltage,
};

static struct regulator_desc reg_mc34704[] = {
	{
	 .name = "REG1_BKLT",
	 .id = MC34704_BKLT,
	 .ops = &mc34704_full_ops,
	 .irq = 0,
	 .type = REGULATOR_VOLTAGE,
	 .owner = THIS_MODULE},
	{
	 .name = "REG2_CPU",
	 .id = MC34704_CPU,
	 .ops = &mc34704_partial_ops,
	 .irq = 0,
	 .type = REGULATOR_VOLTAGE,
	 .owner = THIS_MODULE},
	{
	 .name = "REG3_CORE",
	 .id = MC34704_CORE,
	 .ops = &mc34704_partial_ops,
	 .irq = 0,
	 .type = REGULATOR_VOLTAGE,
	 .owner = THIS_MODULE},
	{
	 .name = "REG4_DDR",
	 .id = MC34704_DDR,
	 .ops = &mc34704_partial_ops,
	 .irq = 0,
	 .type = REGULATOR_VOLTAGE,
	 .owner = THIS_MODULE},
	{
	 .name = "REG5_PERS",
	 .id = MC34704_PERS,
	 .ops = &mc34704_full_ops,
	 .irq = 0,
	 .type = REGULATOR_VOLTAGE,
	 .owner = THIS_MODULE},
};

static int mc34704_regulator_probe(struct platform_device *pdev)
{
	struct regulator_dev *rdev;

	/* register regulator */
	rdev = regulator_register(&reg_mc34704[pdev->id], &pdev->dev,
				  pdev->dev.platform_data,
				  (void *)&mc34704_reg_priv[pdev->id]);
	if (IS_ERR(rdev)) {
		dev_err(&pdev->dev, "failed to register %s\n",
			reg_mc34704[pdev->id].name);
		return PTR_ERR(rdev);
	}

	return 0;
}

static int mc34704_regulator_remove(struct platform_device *pdev)
{
	struct regulator_dev *rdev = platform_get_drvdata(pdev);

	regulator_unregister(rdev);

	return 0;
}

int mc34704_register_regulator(struct mc34704 *mc34704, int reg,
			       struct regulator_init_data *initdata)
{
	struct platform_device *pdev;
	int ret;

	if (mc34704->pmic.pdev[reg])
		return -EBUSY;

	pdev = platform_device_alloc("mc34704-regulatr", reg);
	if (!pdev)
		return -ENOMEM;

	mc34704->pmic.pdev[reg] = pdev;

	initdata->driver_data = mc34704;

	pdev->dev.platform_data = initdata;

	pdev->dev.parent = mc34704->dev;
	platform_set_drvdata(pdev, mc34704);
	ret = platform_device_add(pdev);

	if (ret != 0) {
		dev_err(mc34704->dev, "Failed to register regulator %d: %d\n",
			reg, ret);
		platform_device_del(pdev);
		mc34704->pmic.pdev[reg] = NULL;
	}

	return ret;
}
EXPORT_SYMBOL_GPL(mc34704_register_regulator);

static struct platform_driver mc34704_regulator_driver = {
	.probe = mc34704_regulator_probe,
	.remove = mc34704_regulator_remove,
	.driver = {
		   .name = "mc34704-regulatr",
		   },
};

static int __init mc34704_regulator_init(void)
{
	return platform_driver_register(&mc34704_regulator_driver);
}
subsys_initcall(mc34704_regulator_init);

static void __exit mc34704_regulator_exit(void)
{
	platform_driver_unregister(&mc34704_regulator_driver);
}
module_exit(mc34704_regulator_exit);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("MC34704 Regulator Driver");
MODULE_LICENSE("GPL");