-
Notifications
You must be signed in to change notification settings - Fork 11
/
fpc1020_regulator.c
223 lines (177 loc) · 5.43 KB
/
fpc1020_regulator.c
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
/* FPC1020 Touch sensor driver
*
* Copyright (c) 2013,2014 Fingerprint Cards AB <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License Version 2
* as published by the Free Software Foundation.
*/
#define DEBUG
#include <linux/compiler.h> /* "__must_check", used by regulator API*/
#include <linux/types.h> /* "bool", used by regulator API */
#include <linux/regulator/consumer.h>
#ifndef CONFIG_OF
#include <linux/spi/fpc1020.h>
#include <linux/spi/fpc1020_common.h>
#include <linux/spi/fpc1020_regulator.h>
#else
#include <linux/of.h>
#include "fpc1020.h"
#include "fpc1020_common.h"
#include "fpc1020_regulator.h"
#endif
/* -------------------------------------------------------------------- */
int fpc1020_regulator_configure(fpc1020_data_t *fpc1020)
{
int error = 0;
dev_dbg(&fpc1020->spi->dev, "%s\n", __func__);
fpc1020->vcc_spi = regulator_get(&fpc1020->spi->dev, "vcc_spi");
if (IS_ERR(fpc1020->vcc_spi)) {
error = PTR_ERR(fpc1020->vcc_spi);
dev_err(&fpc1020->spi->dev,
"Regulator get failed, vcc_spi, error=%d\n", error);
goto supply_err;
}
if (regulator_count_voltages(fpc1020->vcc_spi) > 0) {
error = regulator_set_voltage(fpc1020->vcc_spi,
SUPPLY_SPI_MIN, SUPPLY_SPI_MAX);
if (error) {
dev_err(&fpc1020->spi->dev,
"regulator set(spi) failed, error=%d\n", error);
goto supply_err;
}
}
fpc1020->vdd_io = regulator_get(&fpc1020->spi->dev, "vdd_io");
if (IS_ERR(fpc1020->vdd_io)) {
error = PTR_ERR(fpc1020->vdd_io);
dev_err(&fpc1020->spi->dev,
"Regulator get failed, vdd_io, error=%d\n", error);
goto supply_err;
}
if (regulator_count_voltages(fpc1020->vdd_io) > 0) {
error = regulator_set_voltage(fpc1020->vdd_io,
SUPPLY_IO_MIN, SUPPLY_IO_MAX);
if (error) {
dev_err(&fpc1020->spi->dev,
"regulator set(io) failed, error=%d\n", error);
goto supply_err;
}
}
fpc1020->vdd_ana = regulator_get(&fpc1020->spi->dev, "vdd_ana");
if (IS_ERR(fpc1020->vdd_ana)) {
error = PTR_ERR(fpc1020->vdd_ana);
dev_err(&fpc1020->spi->dev,
"Regulator get failed, vdd_ana error=%d\n", error);
goto supply_err;
}
if (regulator_count_voltages(fpc1020->vdd_ana) > 0) {
error = regulator_set_voltage(fpc1020->vdd_ana,
SUPPLY_ANA_MIN, SUPPLY_ANA_MAX);
if (error) {
dev_err(&fpc1020->spi->dev,
"regulator set(ana) failed, error=%d\n", error);
goto supply_err;
}
}
return 0;
supply_err:
fpc1020_regulator_release(fpc1020);
return error;
}
/* -------------------------------------------------------------------- */
int fpc1020_regulator_release(fpc1020_data_t *fpc1020)
{
if (fpc1020->vcc_spi != NULL) {
regulator_put(fpc1020->vcc_spi);
fpc1020->vcc_spi = NULL;
}
if (fpc1020->vdd_io != NULL) {
regulator_put(fpc1020->vdd_io);
fpc1020->vdd_io = NULL;
}
if (fpc1020->vdd_ana != NULL) {
regulator_put(fpc1020->vdd_ana);
fpc1020->vdd_ana = NULL;
}
fpc1020->power_enabled = false;
return 0;
}
/* -------------------------------------------------------------------- */
int fpc1020_regulator_set(fpc1020_data_t *fpc1020, bool enable)
{
int error = 0;
if ((fpc1020->vcc_spi == NULL) ||
(fpc1020->vdd_io == NULL) ||
(fpc1020->vdd_ana == NULL)) {
dev_err(&fpc1020->spi->dev,
"Regulators not set\n");
return -EINVAL;
}
if (enable) {
dev_dbg(&fpc1020->spi->dev, "%s on\n", __func__);
regulator_set_optimum_mode(fpc1020->vcc_spi,
SUPPLY_SPI_REQ_CURRENT);
error = (regulator_is_enabled(fpc1020->vcc_spi) == 0) ?
regulator_enable(fpc1020->vcc_spi) : 0;
if (error) {
dev_err(&fpc1020->spi->dev,
"Regulator vcc_spi enable failed, error=%d\n",
error);
goto out_err;
}
regulator_set_optimum_mode(fpc1020->vdd_io,
SUPPLY_IO_REQ_CURRENT);
error = (regulator_is_enabled(fpc1020->vdd_io) == 0) ?
regulator_enable(fpc1020->vdd_io) : 0;
if (error) {
dev_err(&fpc1020->spi->dev,
"Regulator vdd_io enable failed, error=%d\n",
error);
goto out_err;
}
regulator_set_optimum_mode(fpc1020->vdd_ana,
SUPPLY_ANA_REQ_CURRENT);
error = (regulator_is_enabled(fpc1020->vdd_ana) == 0) ?
regulator_enable(fpc1020->vdd_ana) : 0;
if (error) {
dev_err(&fpc1020->spi->dev,
"Regulator vdd_ana enable failed, error=%d\n",
error);
goto out_err;
}
} else {
dev_dbg(&fpc1020->spi->dev, "%s off\n", __func__);
error = (fpc1020->power_enabled &&
regulator_is_enabled(fpc1020->vcc_spi) > 0) ?
regulator_disable(fpc1020->vcc_spi) : 0;
if (error) {
dev_err(&fpc1020->spi->dev,
"Regulator vcc_spi disable failed, error=%d\n",
error);
goto out_err;
}
error = (fpc1020->power_enabled &&
regulator_is_enabled(fpc1020->vdd_io) > 0) ?
regulator_disable(fpc1020->vdd_io) : 0;
if (error) {
dev_err(&fpc1020->spi->dev,
"Regulator vdd_io disable failed, error=%d\n",
error);
goto out_err;
}
error = (fpc1020->power_enabled &&
regulator_is_enabled(fpc1020->vdd_ana) > 0) ?
regulator_disable(fpc1020->vdd_ana) : 0;
if (error) {
dev_err(&fpc1020->spi->dev,
"Regulator vdd_ana disable failed error=%d\n",
error);
goto out_err;
}
}
fpc1020->power_enabled = enable;
return 0;
out_err:
fpc1020_regulator_release(fpc1020);
return error;
}