forked from notro/tinydrm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinydrm-regmap.c
301 lines (246 loc) · 6.91 KB
/
tinydrm-regmap.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
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
/*
* Copyright 2017 Noralf Trønnes
*
* 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.
*/
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/gpio/consumer.h>
#include <linux/regmap.h>
#include <drm/drmP.h>
#include <drm/tinydrm/tinydrm-helpers.h>
#include <drm/tinydrm/tinydrm-regmap.h>
/**
* DOC: overview
*
*/
/**
* tinydrm_regmap_raw_swap_bytes - Does a raw write require swapping bytes?
* @reg: Regmap
*
* If the bus doesn't support the full regwidth, it has to break up the word.
* Additionally if the bus and machine doesn't match endian wise, this requires
* byteswapping the buffer when using regmap_raw_write().
*
* Returns:
* True if byte swapping is needed, otherwise false
*/
bool tinydrm_regmap_raw_swap_bytes(struct regmap *reg)
{
int val_bytes = regmap_get_val_bytes(reg);
unsigned int bus_val;
u16 val16 = 0x00ff;
if (val_bytes == 1)
return false;
if (WARN_ON_ONCE(val_bytes != 2))
return false;
regmap_parse_val(reg, &val16, &bus_val);
return val16 != bus_val;
}
EXPORT_SYMBOL(tinydrm_regmap_raw_swap_bytes);
struct tinydrm_regmap_i80 {
struct device *dev;
struct regmap *reg;
struct gpio_desc *cs;
struct gpio_desc *idx;
struct gpio_desc *wr;
struct gpio_descs *db;
};
static void tinydrm_i80_write_value(struct gpio_desc *wr,
struct gpio_descs *db, u32 value)
{
int i, values[32];
for (i = 0; i < db->ndescs; i++, value >>= 1)
values[i] = value & 1;
gpiod_set_value_cansleep(wr, 0);
gpiod_set_array_value_cansleep(db->ndescs, db->desc, values);
gpiod_set_value_cansleep(wr, 1);
}
static void tinydrm_i80_write_buf(struct gpio_desc *wr, struct gpio_descs *db,
const void *buf, size_t len)
{
unsigned int width = db->ndescs;
size_t i;
if (width == 8) {
const u8 *buf8 = buf;
for (i = 0; i < len; i++)
tinydrm_i80_write_value(wr, db, *buf8++);
} else if (width == 16) {
const u16 *buf16 = buf;
for (i = 0; i < (len / 2); i++)
tinydrm_i80_write_value(wr, db, *buf16++);
} else {
WARN_ON_ONCE(1);
}
}
static int tinydrm_regmap_i80_gather_write(void *context, const void *reg,
size_t reg_len, const void *val,
size_t val_len)
{
struct tinydrm_regmap_i80 *i80 = context;
if (i80->cs)
gpiod_set_value_cansleep(i80->cs, 0);
if (i80->idx)
gpiod_set_value_cansleep(i80->idx, 0);
tinydrm_i80_write_buf(i80->wr, i80->db, reg, reg_len);
if (i80->idx)
gpiod_set_value_cansleep(i80->idx, 1);
tinydrm_i80_write_buf(i80->wr, i80->db, val, val_len);
if (i80->cs)
gpiod_set_value_cansleep(i80->cs, 1);
return 0;
}
static int tinydrm_regmap_i80_write(void *context, const void *data,
size_t count)
{
struct tinydrm_regmap_i80 *i80 = context;
size_t sz = regmap_get_val_bytes(i80->reg);
return tinydrm_regmap_i80_gather_write(context, data, sz,
data + sz, count - sz);
}
static int tinydrm_regmap_i80_read(void *context, const void *reg,
size_t reg_len, void *val, size_t val_len)
{
return -ENOTSUPP;
}
static const struct regmap_bus tinydrm_i80_bus = {
.write = tinydrm_regmap_i80_write,
.gather_write = tinydrm_regmap_i80_gather_write,
.read = tinydrm_regmap_i80_read,
.reg_format_endian_default = REGMAP_ENDIAN_BIG,
.val_format_endian_default = REGMAP_ENDIAN_BIG,
// NATIVE
};
/**
* tinydrm_i80_init - Initialize an I80 bus regmap
* @dev: Device
* @reg_width: Register width in bits (8 or 16).
* @cs: Chip Select gpio (optional).
* @idx: Index gpio, low writing register number and high writing value
* (optional).
* @wr: Write latch gpio.
* @db: Databus gpio array. The bus can be 8-bit wide even if the register is
* 16-bit.
*
* This function creates a ®map to access the register on a I80 type bus
* connected controller.
*
* Returns I80 ®map on success or ERR_PTR on failure.
*/
struct regmap *tinydrm_i80_init(struct device *dev, unsigned int reg_width,
struct gpio_desc *cs, struct gpio_desc *idx,
struct gpio_desc *wr, struct gpio_descs *db)
{
struct tinydrm_regmap_i80 *i80;
struct regmap_config config = {
.reg_bits = reg_width,
.val_bits = reg_width,
.cache_type = REGCACHE_NONE,
};
if ((db->ndescs != 8 && db->ndescs != 16) ||
(reg_width != 8 && reg_width != 16))
return ERR_PTR(-EINVAL);
i80 = devm_kzalloc(dev, sizeof(*i80), GFP_KERNEL);
if (!i80)
return ERR_PTR(-ENOMEM);
i80->dev = dev;
i80->cs = cs;
i80->idx = idx;
i80->wr = wr;
i80->db = db;
i80->reg = devm_regmap_init(dev, &tinydrm_i80_bus, i80, &config);
return i80->reg;
}
EXPORT_SYMBOL(tinydrm_i80_init);
#ifdef CONFIG_DEBUG_FS
static int
tinydrm_kstrtoul_array_from_user(const char __user *s, size_t count,
unsigned int base,
unsigned long *vals, size_t num_vals)
{
char *buf, *pos, *token;
int ret = -EINVAL, i = 0;
buf = memdup_user_nul(s, count);
if (IS_ERR(buf))
return PTR_ERR(buf);
pos = buf;
while (pos) {
if (i == num_vals) {
ret = -E2BIG;
goto err_free;
}
token = strsep(&pos, " ");
if (!token) {
ret = -EINVAL;
goto err_free;
}
ret = kstrtoul(token, base, vals++);
if (ret < 0)
goto err_free;
i++;
}
err_free:
kfree(buf);
return ret ? ret : i;
}
static ssize_t tinydrm_regmap_debugfs_reg_write(struct file *file,
const char __user *user_buf,
size_t count, loff_t *ppos)
{
struct seq_file *m = file->private_data;
struct regmap *reg = m->private;
unsigned long vals[2];
int ret;
ret = tinydrm_kstrtoul_array_from_user(user_buf, count, 16, vals, 2);
if (ret <= 0)
return ret;
if (ret != 2)
return -EINVAL;
ret = regmap_write(reg, vals[0], vals[1]);
return ret < 0 ? ret : count;
}
static int tinydrm_regmap_debugfs_reg_show(struct seq_file *m, void *d)
{
struct regmap *reg = m->private;
int max_reg = regmap_get_max_register(reg);
int val_bytes = regmap_get_val_bytes(reg);
unsigned int val;
int regnr, ret;
for (regnr = 0; regnr < max_reg; regnr++) {
seq_printf(m, "%.*x: ", val_bytes * 2, regnr);
ret = regmap_read(reg, regnr, &val);
if (ret)
seq_puts(m, "XX\n");
else
seq_printf(m, "%.*x\n", val_bytes * 2, val);
}
return 0;
}
static int tinydrm_regmap_debugfs_reg_open(struct inode *inode,
struct file *file)
{
return single_open(file, tinydrm_regmap_debugfs_reg_show,
inode->i_private);
}
static const struct file_operations tinydrm_regmap_debugfs_reg_fops = {
.owner = THIS_MODULE,
.open = tinydrm_regmap_debugfs_reg_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
.write = tinydrm_regmap_debugfs_reg_write,
};
int tinydrm_regmap_debugfs_init(struct regmap *reg, struct dentry *parent)
{
umode_t mode = S_IFREG | S_IWUSR;
if (regmap_get_max_register(reg))
mode |= S_IRUGO;
debugfs_create_file("registers", mode, parent, reg,
&tinydrm_regmap_debugfs_reg_fops);
return 0;
}
EXPORT_SYMBOL(tinydrm_regmap_debugfs_init);
#endif