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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2026 Roman Vivchar <rva333@protonmail.com>
*
* Based on drivers/iio/adc/mt6359-auxadc.c
*/
#include <linux/array_size.h>
#include <linux/bitfield.h>
#include <linux/bits.h>
#include <linux/cleanup.h>
#include <linux/delay.h>
#include <linux/iio/iio.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/stringify.h>
#include <linux/time.h>
#include <linux/types.h>
#include <linux/mfd/mt6323/registers.h>
#include <dt-bindings/iio/adc/mediatek,mt6323-auxadc.h>
#define AUXADC_STRUP_CON10_RSTB_SEL BIT(7)
#define AUXADC_STRUP_CON10_RSTB_SW BIT(5)
#define AUXADC_TOP_CKPDN2_CTL_CK BIT(5)
#define AUXADC_TRIM_CH2_MASK GENMASK(11, 10)
#define AUXADC_TRIM_CH4_MASK GENMASK(9, 8)
#define AUXADC_TRIM_CH5_MASK GENMASK(5, 4)
#define AUXADC_TRIM_CH6_MASK GENMASK(3, 2)
#define AUXADC_CON27_VREF18_ENB_MD BIT(15)
#define AUXADC_CON27_MD_STATUS BIT(0)
#define AUXADC_CON19_GPS_STATUS BIT(1)
#define AUXADC_CON26_VREF18_SELB BIT(1)
#define AUXADC_CON26_DECI_GDLY_SEL BIT(0)
#define AUXADC_CON11_VBUF_EN BIT(4)
#define AUXADC_CON19_DECI_GDLY_MASK GENMASK(15, 14)
#define AUXADC_ADC19_BUSY_MASK GENMASK(15, 1)
#define AUXADC_READY_MASK BIT(15)
#define AUXADC_DATA_MASK GENMASK(14, 0)
#define AUXADC_CON9_OSR_MASK GENMASK(12, 10)
#define AUXADC_DEFAULT_OSR 3
#define MTK_PMIC_IIO_CHAN(_name, _chan, _addr) \
{ \
.type = IIO_VOLTAGE, \
.indexed = 1, \
.channel = _chan, \
.address = _addr, \
.datasheet_name = __stringify(_name), \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
BIT(IIO_CHAN_INFO_SCALE), \
}
/*
* AUXADC reports everything in mV, including temperature and
* current channels. Channel macros are mapped such that their
* ID matches their respective hardware bit position in CON22.
*/
static const struct iio_chan_spec mt6323_auxadc_channels[] = {
MTK_PMIC_IIO_CHAN(baton2, MT6323_AUXADC_BATON2, MT6323_AUXADC_ADC6),
MTK_PMIC_IIO_CHAN(ch6, MT6323_AUXADC_CH6, MT6323_AUXADC_ADC11),
MTK_PMIC_IIO_CHAN(bat_temp, MT6323_AUXADC_BAT_TEMP, MT6323_AUXADC_ADC5),
MTK_PMIC_IIO_CHAN(chip_temp, MT6323_AUXADC_CHIP_TEMP, MT6323_AUXADC_ADC4),
MTK_PMIC_IIO_CHAN(vcdt, MT6323_AUXADC_VCDT, MT6323_AUXADC_ADC2),
MTK_PMIC_IIO_CHAN(baton1, MT6323_AUXADC_BATON1, MT6323_AUXADC_ADC3),
MTK_PMIC_IIO_CHAN(isense, MT6323_AUXADC_ISENSE, MT6323_AUXADC_ADC1),
MTK_PMIC_IIO_CHAN(batsns, MT6323_AUXADC_BATSNS, MT6323_AUXADC_ADC0),
MTK_PMIC_IIO_CHAN(accdet, MT6323_AUXADC_ACCDET, MT6323_AUXADC_ADC7),
};
/*
* The MediaTek MT6323 (as well as a lot of other PMICs) has the following hierarchy:
* PMIC AUXADC <- PMIC MFD <- SoC PWRAP (wrapper for PWRAP FSM)
*
* Therefore, PWRAP regmap should be obtained using dev->parent->parent.
*/
struct mt6323_auxadc {
struct regmap *regmap;
/* AUXADC doesn't support reading multiple channels simultaneously. */
struct mutex lock;
};
static int mt6323_auxadc_prepare_channel(struct mt6323_auxadc *auxadc)
{
struct regmap *map = auxadc->regmap;
u32 val;
int ret;
ret = regmap_read(map, MT6323_AUXADC_CON19, &val);
if (ret)
return ret;
/* The ADC is idle. */
if (!(val & AUXADC_CON19_DECI_GDLY_MASK))
return 0;
ret = regmap_read_poll_timeout(map, MT6323_AUXADC_ADC19,
val, !(val & AUXADC_ADC19_BUSY_MASK),
10, 500);
if (ret)
return ret;
return regmap_clear_bits(map, MT6323_AUXADC_CON19,
AUXADC_CON19_DECI_GDLY_MASK);
}
static int mt6323_auxadc_request(struct mt6323_auxadc *auxadc,
unsigned long channel)
{
struct regmap *map = auxadc->regmap;
int ret;
ret = regmap_set_bits(map, MT6323_AUXADC_CON11, AUXADC_CON11_VBUF_EN);
if (ret)
return ret;
return regmap_set_bits(map, MT6323_AUXADC_CON22, BIT(channel));
}
static int mt6323_auxadc_release(struct mt6323_auxadc *auxadc,
unsigned long channel)
{
struct regmap *map = auxadc->regmap;
int ret;
ret = regmap_clear_bits(map, MT6323_AUXADC_CON22, BIT(channel));
if (ret)
return ret;
return regmap_clear_bits(map, MT6323_AUXADC_CON11, AUXADC_CON11_VBUF_EN);
}
static int mt6323_auxadc_read(struct mt6323_auxadc *auxadc,
const struct iio_chan_spec *chan, int *out)
{
struct regmap *map = auxadc->regmap;
u32 val;
int ret;
ret = regmap_read_poll_timeout(map, chan->address,
val, (val & AUXADC_READY_MASK),
1 * USEC_PER_MSEC, 100 * USEC_PER_MSEC);
if (ret)
return ret;
*out = FIELD_GET(AUXADC_DATA_MASK, val);
return 0;
}
static int mt6323_auxadc_read_raw(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan,
int *val, int *val2, long mask)
{
struct mt6323_auxadc *auxadc = iio_priv(indio_dev);
int ret, mult;
switch (mask) {
case IIO_CHAN_INFO_SCALE:
if (chan->channel == MT6323_AUXADC_ISENSE ||
chan->channel == MT6323_AUXADC_BATSNS)
mult = 4;
else
mult = 1;
/* 1800mV full range with 15-bit resolution. */
*val = mult * 1800;
*val2 = 15;
return IIO_VAL_FRACTIONAL_LOG2;
case IIO_CHAN_INFO_RAW: {
guard(mutex)(&auxadc->lock);
ret = mt6323_auxadc_prepare_channel(auxadc);
if (ret)
return ret;
ret = mt6323_auxadc_request(auxadc, chan->channel);
if (ret)
return ret;
/* Hardware limitation: the AUXADC needs a delay to become ready. */
fsleep(300);
ret = mt6323_auxadc_read(auxadc, chan, val);
if (mt6323_auxadc_release(auxadc, chan->channel))
dev_err(&indio_dev->dev,
"failed to release channel %d\n", chan->channel);
if (ret)
return ret;
return IIO_VAL_INT;
}
default:
return -EINVAL;
}
}
static int mt6323_auxadc_init(struct mt6323_auxadc *auxadc)
{
struct regmap *map = auxadc->regmap;
int ret;
ret = regmap_set_bits(map, MT6323_STRUP_CON10,
AUXADC_STRUP_CON10_RSTB_SW |
AUXADC_STRUP_CON10_RSTB_SEL);
if (ret)
return ret;
ret = regmap_set_bits(map, MT6323_TOP_CKPDN2, AUXADC_TOP_CKPDN2_CTL_CK);
if (ret)
return ret;
ret = regmap_update_bits(map, MT6323_AUXADC_CON10,
AUXADC_TRIM_CH2_MASK | AUXADC_TRIM_CH4_MASK |
AUXADC_TRIM_CH5_MASK | AUXADC_TRIM_CH6_MASK,
FIELD_PREP(AUXADC_TRIM_CH2_MASK, 1) |
FIELD_PREP(AUXADC_TRIM_CH4_MASK, 1) |
FIELD_PREP(AUXADC_TRIM_CH5_MASK, 1) |
FIELD_PREP(AUXADC_TRIM_CH6_MASK, 1));
if (ret)
return ret;
ret = regmap_set_bits(map, MT6323_AUXADC_CON27,
AUXADC_CON27_VREF18_ENB_MD |
AUXADC_CON27_MD_STATUS);
if (ret)
return ret;
ret = regmap_set_bits(map, MT6323_AUXADC_CON19, AUXADC_CON19_GPS_STATUS);
if (ret)
return ret;
ret = regmap_set_bits(map, MT6323_AUXADC_CON26,
AUXADC_CON26_VREF18_SELB |
AUXADC_CON26_DECI_GDLY_SEL);
if (ret)
return ret;
return regmap_update_bits(map, MT6323_AUXADC_CON9, AUXADC_CON9_OSR_MASK,
FIELD_PREP(AUXADC_CON9_OSR_MASK, AUXADC_DEFAULT_OSR));
}
static const struct iio_info mt6323_auxadc_iio_info = {
.read_raw = mt6323_auxadc_read_raw,
};
static int mt6323_auxadc_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct mt6323_auxadc *auxadc;
struct regmap *regmap;
struct iio_dev *iio;
int ret;
regmap = dev_get_regmap(dev->parent->parent, NULL);
if (!regmap)
return dev_err_probe(dev, -ENODEV, "failed to get regmap\n");
iio = devm_iio_device_alloc(dev, sizeof(*auxadc));
if (!iio)
return -ENOMEM;
auxadc = iio_priv(iio);
auxadc->regmap = regmap;
ret = devm_mutex_init(dev, &auxadc->lock);
if (ret)
return ret;
ret = mt6323_auxadc_init(auxadc);
if (ret)
return dev_err_probe(dev, ret, "failed to initialize auxadc\n");
iio->name = "mt6323-auxadc";
iio->info = &mt6323_auxadc_iio_info;
iio->modes = INDIO_DIRECT_MODE;
iio->channels = mt6323_auxadc_channels;
iio->num_channels = ARRAY_SIZE(mt6323_auxadc_channels);
return devm_iio_device_register(dev, iio);
}
static const struct of_device_id mt6323_auxadc_of_match[] = {
{ .compatible = "mediatek,mt6323-auxadc" },
{ }
};
MODULE_DEVICE_TABLE(of, mt6323_auxadc_of_match);
static struct platform_driver mt6323_auxadc_driver = {
.driver = {
.name = "mt6323-auxadc",
.of_match_table = mt6323_auxadc_of_match,
},
.probe = mt6323_auxadc_probe,
};
module_platform_driver(mt6323_auxadc_driver);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("MediaTek MT6323 PMIC AUXADC Driver");
|