summaryrefslogtreecommitdiffstats
path: root/drivers/regulator/pfuze.c
blob: dc41e8f55b84a60262e59c0c0a4f3e8d86599ff1 (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
/*
 * Copyright (C) 2017 Sascha Hauer, Pengutronix
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *
 */

#include <common.h>
#include <init.h>
#include <driver.h>
#include <xfuncs.h>
#include <errno.h>
#include <malloc.h>
#include <of.h>
#include <regmap.h>
#include <mfd/pfuze.h>

#include <i2c/i2c.h>

#include <poweroff.h>
#include <mach/imx6.h>

#define DRIVERNAME		"pfuze"

#define MC13XXX_NUMREGS		0x3f

#define PFUZE100_SW1ABMODE	0x23
#define PFUZE100_SW1CMODE	0x31
#define PFUZE100_SW2MODE	0x38
#define PFUZE100_SW3AMODE	0x3f
#define PFUZE100_SW3BMODE	0x46
#define PFUZE100_SW4MODE	0x4d
#define PFUZE100_VGEN1VOL	0x6c
#define PFUZE100_VGEN2VOL	0x6d
#define PFUZE100_VGEN3VOL	0x6e
#define PFUZE100_VGEN4VOL	0x6f
#define PFUZE100_VGEN5VOL	0x70
#define PFUZE100_VGEN6VOL	0x71

#define PFUZE100_SWxMODE_MASK	0xf
#define PFUZE100_SWxMODE_APS_APS	0x8
#define PFUZE100_SWxMODE_APS_OFF	0x4

#define PFUZE100_VGENxLPWR	BIT(6)
#define PFUZE100_VGENxSTBY	BIT(5)

struct pfuze {
	struct device_d			*dev;
	struct regmap			*map;
	struct i2c_client		*client;
	int				revision;
};

struct pfuze_devtype {
	int	(*revision)(struct pfuze*);
};

#define to_pfuze(a)		container_of(a, struct pfuze, cdev)

static struct pfuze *pfuze_dev;

static void(*pfuze_init_callback)(struct regmap *map);

int pfuze_register_init_callback(void(*callback)(struct regmap *map))
{
	if (pfuze_init_callback)
		return -EBUSY;

	pfuze_init_callback = callback;

	if (pfuze_dev)
		pfuze_init_callback(pfuze_dev->map);

	return 0;
}

static int pfuze_i2c_reg_read(void *ctx, unsigned int reg, unsigned int *val)
{
	struct pfuze *pfuze = ctx;
	u8 buf[1];
	int ret;

	ret = i2c_read_reg(pfuze->client, reg, buf, 1);
	*val = buf[0];

	return ret == 1 ? 0 : ret;
}

static int pfuze_i2c_reg_write(void *ctx, unsigned int reg, unsigned int val)
{
	struct pfuze *pfuze = ctx;
	u8 buf[] = {
		val & 0xff,
	};
	int ret;

	ret = i2c_write_reg(pfuze->client, reg, buf, 1);

	return ret == 1 ? 0 : ret;
}

static void pfuze_power_off_prepare(struct poweroff_handler *handler)
{
	dev_info(pfuze_dev->dev, "Configure standy mode for power off");

	/* Switch from default mode: APS/APS to APS/Off */
	regmap_write_bits(pfuze_dev->map, PFUZE100_SW1ABMODE,
			  PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
	regmap_write_bits(pfuze_dev->map, PFUZE100_SW1CMODE,
			  PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
	regmap_write_bits(pfuze_dev->map, PFUZE100_SW2MODE,
			  PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
	regmap_write_bits(pfuze_dev->map, PFUZE100_SW3AMODE,
			  PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
	regmap_write_bits(pfuze_dev->map, PFUZE100_SW3BMODE,
			  PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);
	regmap_write_bits(pfuze_dev->map, PFUZE100_SW4MODE,
			  PFUZE100_SWxMODE_MASK, PFUZE100_SWxMODE_APS_OFF);

	regmap_write_bits(pfuze_dev->map, PFUZE100_VGEN1VOL,
			  PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
			  PFUZE100_VGENxSTBY);
	regmap_write_bits(pfuze_dev->map, PFUZE100_VGEN2VOL,
			  PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
			  PFUZE100_VGENxSTBY);
	regmap_write_bits(pfuze_dev->map, PFUZE100_VGEN3VOL,
			  PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
			  PFUZE100_VGENxSTBY);
	regmap_write_bits(pfuze_dev->map, PFUZE100_VGEN4VOL,
			  PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
			  PFUZE100_VGENxSTBY);
	regmap_write_bits(pfuze_dev->map, PFUZE100_VGEN5VOL,
			  PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
			  PFUZE100_VGENxSTBY);
	regmap_write_bits(pfuze_dev->map, PFUZE100_VGEN6VOL,
			  PFUZE100_VGENxLPWR | PFUZE100_VGENxSTBY,
			  PFUZE100_VGENxSTBY);

	imx6_pm_stby_poweroff();
}

static struct regmap_bus regmap_pfuze_i2c_bus = {
	.reg_write = pfuze_i2c_reg_write,
	.reg_read = pfuze_i2c_reg_read,
};

static const struct regmap_config pfuze_regmap_i2c_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.max_register = 127,
};

static int __init pfuze_probe(struct device_d *dev)
{
	struct pfuze_devtype *devtype;
	int ret;

	if (pfuze_dev)
		return -EBUSY;

	ret = dev_get_drvdata(dev, (const void **)&devtype);
	if (ret)
		return ret;

	pfuze_dev = xzalloc(sizeof(*pfuze_dev));
	pfuze_dev->dev = dev;

	pfuze_dev->client = to_i2c_client(dev);
	pfuze_dev->map = regmap_init(dev, &regmap_pfuze_i2c_bus,
				     pfuze_dev, &pfuze_regmap_i2c_config);

	ret = regmap_register_cdev(pfuze_dev->map, NULL);
	if (ret)
		return ret;

	if (pfuze_init_callback)
		pfuze_init_callback(pfuze_dev->map);

	if (of_property_read_bool(dev->device_node,
				  "fsl,pmic-stby-poweroff"))
		return poweroff_handler_register_fn(pfuze_power_off_prepare);

	return 0;
}

static struct pfuze_devtype pfuze100_devtype = {
};

static struct pfuze_devtype pfuze200_devtype = {
};

static struct pfuze_devtype pfuze3000_devtype = {
};

static struct platform_device_id pfuze_ids[] = {
	{ .name = "pfuze100", .driver_data = (ulong)&pfuze100_devtype, },
	{ .name = "pfuze200", .driver_data = (ulong)&pfuze200_devtype, },
	{ .name = "pfuze3000", .driver_data = (ulong)&pfuze3000_devtype, },
	{ }
};

static __maybe_unused struct of_device_id pfuze_dt_ids[] = {
	{ .compatible = "fsl,pfuze100", .data = &pfuze100_devtype, },
	{ .compatible = "fsl,pfuze200", .data = &pfuze200_devtype, },
	{ .compatible = "fsl,pfuze3000", .data = &pfuze3000_devtype, },
	{ }
};

static struct driver_d pfuze_i2c_driver = {
	.name		= "pfuze-i2c",
	.probe		= pfuze_probe,
	.id_table	= pfuze_ids,
	.of_compatible	= DRV_OF_COMPAT(pfuze_dt_ids),
};

static int __init pfuze_init(void)
{
	int ret;

	ret = i2c_driver_register(&pfuze_i2c_driver);
	if (ret)
		return ret;

	return 0;

}
late_initcall(pfuze_init);