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
|
// SPDX-License-Identifier: GPL-2.0
/*
* MIPI Camera Control Interface (CCI) register access helpers.
*
* Copyright (C) 2023 Hans de Goede <hansg@kernel.org>
*/
#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/dev_printk.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/types.h>
#include <asm/unaligned.h>
#include <media/v4l2-cci.h>
int cci_read(struct regmap *map, u32 reg, u64 *val, int *err)
{
bool little_endian;
unsigned int len;
u8 buf[8];
int ret;
if (err && *err)
return *err;
little_endian = reg & CCI_REG_LE;
len = CCI_REG_WIDTH_BYTES(reg);
reg = CCI_REG_ADDR(reg);
ret = regmap_bulk_read(map, reg, buf, len);
if (ret) {
dev_err(regmap_get_device(map), "Error reading reg 0x%4x: %d\n",
reg, ret);
goto out;
}
switch (len) {
case 1:
*val = buf[0];
break;
case 2:
if (little_endian)
*val = get_unaligned_le16(buf);
else
*val = get_unaligned_be16(buf);
break;
case 3:
if (little_endian)
*val = get_unaligned_le24(buf);
else
*val = get_unaligned_be24(buf);
break;
case 4:
if (little_endian)
*val = get_unaligned_le32(buf);
else
*val = get_unaligned_be32(buf);
break;
case 8:
if (little_endian)
*val = get_unaligned_le64(buf);
else
*val = get_unaligned_be64(buf);
break;
default:
dev_err(regmap_get_device(map), "Error invalid reg-width %u for reg 0x%04x\n",
len, reg);
ret = -EINVAL;
break;
}
out:
if (ret && err)
*err = ret;
return ret;
}
EXPORT_SYMBOL_GPL(cci_read);
int cci_write(struct regmap *map, u32 reg, u64 val, int *err)
{
bool little_endian;
unsigned int len;
u8 buf[8];
int ret;
if (err && *err)
return *err;
little_endian = reg & CCI_REG_LE;
len = CCI_REG_WIDTH_BYTES(reg);
reg = CCI_REG_ADDR(reg);
switch (len) {
case 1:
buf[0] = val;
break;
case 2:
if (little_endian)
put_unaligned_le16(val, buf);
else
put_unaligned_be16(val, buf);
break;
case 3:
if (little_endian)
put_unaligned_le24(val, buf);
else
put_unaligned_be24(val, buf);
break;
case 4:
if (little_endian)
put_unaligned_le32(val, buf);
else
put_unaligned_be32(val, buf);
break;
case 8:
if (little_endian)
put_unaligned_le64(val, buf);
else
put_unaligned_be64(val, buf);
break;
default:
dev_err(regmap_get_device(map), "Error invalid reg-width %u for reg 0x%04x\n",
len, reg);
ret = -EINVAL;
goto out;
}
ret = regmap_bulk_write(map, reg, buf, len);
if (ret)
dev_err(regmap_get_device(map), "Error writing reg 0x%4x: %d\n",
reg, ret);
out:
if (ret && err)
*err = ret;
return ret;
}
EXPORT_SYMBOL_GPL(cci_write);
int cci_update_bits(struct regmap *map, u32 reg, u64 mask, u64 val, int *err)
{
u64 readval;
int ret;
ret = cci_read(map, reg, &readval, err);
if (ret)
return ret;
val = (readval & ~mask) | (val & mask);
return cci_write(map, reg, val, err);
}
EXPORT_SYMBOL_GPL(cci_update_bits);
int cci_multi_reg_write(struct regmap *map, const struct cci_reg_sequence *regs,
unsigned int num_regs, int *err)
{
unsigned int i;
int ret;
for (i = 0; i < num_regs; i++) {
ret = cci_write(map, regs[i].reg, regs[i].val, err);
if (ret)
return ret;
}
return 0;
}
EXPORT_SYMBOL_GPL(cci_multi_reg_write);
#if IS_ENABLED(CONFIG_V4L2_CCI_I2C)
struct regmap *devm_cci_regmap_init_i2c(struct i2c_client *client,
int reg_addr_bits)
{
struct regmap_config config = {
.reg_bits = reg_addr_bits,
.val_bits = 8,
.reg_format_endian = REGMAP_ENDIAN_BIG,
.disable_locking = true,
};
return devm_regmap_init_i2c(client, &config);
}
EXPORT_SYMBOL_GPL(devm_cci_regmap_init_i2c);
#endif
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>");
MODULE_DESCRIPTION("MIPI Camera Control Interface (CCI) support");
|