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
|
// SPDX-License-Identifier: GPL-2.0
//
// Register map access API - FSI support
//
// Copyright 2022 IBM Corp
//
// Author: Eddie James <eajames@linux.ibm.com>
#include <linux/fsi.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include "internal.h"
static int regmap_fsi32_reg_read(void *context, unsigned int reg, unsigned int *val)
{
u32 v;
int ret;
ret = fsi_slave_read(context, reg, &v, sizeof(v));
if (ret)
return ret;
*val = v;
return 0;
}
static int regmap_fsi32_reg_write(void *context, unsigned int reg, unsigned int val)
{
u32 v = val;
return fsi_slave_write(context, reg, &v, sizeof(v));
}
static const struct regmap_bus regmap_fsi32 = {
.reg_write = regmap_fsi32_reg_write,
.reg_read = regmap_fsi32_reg_read,
};
static int regmap_fsi32le_reg_read(void *context, unsigned int reg, unsigned int *val)
{
__be32 v;
int ret;
ret = fsi_slave_read(context, reg, &v, sizeof(v));
if (ret)
return ret;
*val = be32_to_cpu(v);
return 0;
}
static int regmap_fsi32le_reg_write(void *context, unsigned int reg, unsigned int val)
{
__be32 v = cpu_to_be32(val);
return fsi_slave_write(context, reg, &v, sizeof(v));
}
static const struct regmap_bus regmap_fsi32le = {
.reg_write = regmap_fsi32le_reg_write,
.reg_read = regmap_fsi32le_reg_read,
};
static int regmap_fsi16_reg_read(void *context, unsigned int reg, unsigned int *val)
{
u16 v;
int ret;
ret = fsi_slave_read(context, reg, &v, sizeof(v));
if (ret)
return ret;
*val = v;
return 0;
}
static int regmap_fsi16_reg_write(void *context, unsigned int reg, unsigned int val)
{
u16 v;
if (val > 0xffff)
return -EINVAL;
v = val;
return fsi_slave_write(context, reg, &v, sizeof(v));
}
static const struct regmap_bus regmap_fsi16 = {
.reg_write = regmap_fsi16_reg_write,
.reg_read = regmap_fsi16_reg_read,
};
static int regmap_fsi16le_reg_read(void *context, unsigned int reg, unsigned int *val)
{
__be16 v;
int ret;
ret = fsi_slave_read(context, reg, &v, sizeof(v));
if (ret)
return ret;
*val = be16_to_cpu(v);
return 0;
}
static int regmap_fsi16le_reg_write(void *context, unsigned int reg, unsigned int val)
{
__be16 v;
if (val > 0xffff)
return -EINVAL;
v = cpu_to_be16(val);
return fsi_slave_write(context, reg, &v, sizeof(v));
}
static const struct regmap_bus regmap_fsi16le = {
.reg_write = regmap_fsi16le_reg_write,
.reg_read = regmap_fsi16le_reg_read,
};
static int regmap_fsi8_reg_read(void *context, unsigned int reg, unsigned int *val)
{
u8 v;
int ret;
ret = fsi_slave_read(context, reg, &v, sizeof(v));
if (ret)
return ret;
*val = v;
return 0;
}
static int regmap_fsi8_reg_write(void *context, unsigned int reg, unsigned int val)
{
u8 v;
if (val > 0xff)
return -EINVAL;
v = val;
return fsi_slave_write(context, reg, &v, sizeof(v));
}
static const struct regmap_bus regmap_fsi8 = {
.reg_write = regmap_fsi8_reg_write,
.reg_read = regmap_fsi8_reg_read,
};
static const struct regmap_bus *regmap_get_fsi_bus(struct fsi_device *fsi_dev,
const struct regmap_config *config)
{
const struct regmap_bus *bus = NULL;
if (config->reg_bits == 8 || config->reg_bits == 16 || config->reg_bits == 32) {
switch (config->val_bits) {
case 8:
bus = ®map_fsi8;
break;
case 16:
switch (regmap_get_val_endian(&fsi_dev->dev, NULL, config)) {
case REGMAP_ENDIAN_LITTLE:
#ifdef __LITTLE_ENDIAN
case REGMAP_ENDIAN_NATIVE:
#endif
bus = ®map_fsi16le;
break;
case REGMAP_ENDIAN_DEFAULT:
case REGMAP_ENDIAN_BIG:
#ifdef __BIG_ENDIAN
case REGMAP_ENDIAN_NATIVE:
#endif
bus = ®map_fsi16;
break;
default:
break;
}
break;
case 32:
switch (regmap_get_val_endian(&fsi_dev->dev, NULL, config)) {
case REGMAP_ENDIAN_LITTLE:
#ifdef __LITTLE_ENDIAN
case REGMAP_ENDIAN_NATIVE:
#endif
bus = ®map_fsi32le;
break;
case REGMAP_ENDIAN_DEFAULT:
case REGMAP_ENDIAN_BIG:
#ifdef __BIG_ENDIAN
case REGMAP_ENDIAN_NATIVE:
#endif
bus = ®map_fsi32;
break;
default:
break;
}
break;
}
}
return bus ?: ERR_PTR(-EOPNOTSUPP);
}
struct regmap *__regmap_init_fsi(struct fsi_device *fsi_dev, const struct regmap_config *config,
struct lock_class_key *lock_key, const char *lock_name)
{
const struct regmap_bus *bus = regmap_get_fsi_bus(fsi_dev, config);
if (IS_ERR(bus))
return ERR_CAST(bus);
return __regmap_init(&fsi_dev->dev, bus, fsi_dev->slave, config, lock_key, lock_name);
}
EXPORT_SYMBOL_GPL(__regmap_init_fsi);
struct regmap *__devm_regmap_init_fsi(struct fsi_device *fsi_dev,
const struct regmap_config *config,
struct lock_class_key *lock_key, const char *lock_name)
{
const struct regmap_bus *bus = regmap_get_fsi_bus(fsi_dev, config);
if (IS_ERR(bus))
return ERR_CAST(bus);
return __devm_regmap_init(&fsi_dev->dev, bus, fsi_dev->slave, config, lock_key, lock_name);
}
EXPORT_SYMBOL_GPL(__devm_regmap_init_fsi);
MODULE_LICENSE("GPL");
|