]>
Commit | Line | Data |
---|---|---|
b83a313b MB |
1 | /* |
2 | * Register map access API | |
3 | * | |
4 | * Copyright 2011 Wolfson Microelectronics plc | |
5 | * | |
6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
f5d6eba7 | 13 | #include <linux/device.h> |
b83a313b | 14 | #include <linux/slab.h> |
19694b5e | 15 | #include <linux/export.h> |
b83a313b MB |
16 | #include <linux/mutex.h> |
17 | #include <linux/err.h> | |
18 | ||
fb2736bb MB |
19 | #define CREATE_TRACE_POINTS |
20 | #include <trace/events/regmap.h> | |
21 | ||
93de9124 | 22 | #include "internal.h" |
b83a313b | 23 | |
8de2f081 MB |
24 | bool regmap_writeable(struct regmap *map, unsigned int reg) |
25 | { | |
26 | if (map->max_register && reg > map->max_register) | |
27 | return false; | |
28 | ||
29 | if (map->writeable_reg) | |
30 | return map->writeable_reg(map->dev, reg); | |
31 | ||
32 | return true; | |
33 | } | |
34 | ||
35 | bool regmap_readable(struct regmap *map, unsigned int reg) | |
36 | { | |
37 | if (map->max_register && reg > map->max_register) | |
38 | return false; | |
39 | ||
4191f197 WS |
40 | if (map->format.format_write) |
41 | return false; | |
42 | ||
8de2f081 MB |
43 | if (map->readable_reg) |
44 | return map->readable_reg(map->dev, reg); | |
45 | ||
46 | return true; | |
47 | } | |
48 | ||
49 | bool regmap_volatile(struct regmap *map, unsigned int reg) | |
50 | { | |
4191f197 | 51 | if (!regmap_readable(map, reg)) |
8de2f081 MB |
52 | return false; |
53 | ||
54 | if (map->volatile_reg) | |
55 | return map->volatile_reg(map->dev, reg); | |
56 | ||
57 | return true; | |
58 | } | |
59 | ||
60 | bool regmap_precious(struct regmap *map, unsigned int reg) | |
61 | { | |
4191f197 | 62 | if (!regmap_readable(map, reg)) |
8de2f081 MB |
63 | return false; |
64 | ||
65 | if (map->precious_reg) | |
66 | return map->precious_reg(map->dev, reg); | |
67 | ||
68 | return false; | |
69 | } | |
70 | ||
82cd9965 LPC |
71 | static bool regmap_volatile_range(struct regmap *map, unsigned int reg, |
72 | unsigned int num) | |
73 | { | |
74 | unsigned int i; | |
75 | ||
76 | for (i = 0; i < num; i++) | |
77 | if (!regmap_volatile(map, reg + i)) | |
78 | return false; | |
79 | ||
80 | return true; | |
81 | } | |
82 | ||
9aa50750 WS |
83 | static void regmap_format_2_6_write(struct regmap *map, |
84 | unsigned int reg, unsigned int val) | |
85 | { | |
86 | u8 *out = map->work_buf; | |
87 | ||
88 | *out = (reg << 6) | val; | |
89 | } | |
90 | ||
b83a313b MB |
91 | static void regmap_format_4_12_write(struct regmap *map, |
92 | unsigned int reg, unsigned int val) | |
93 | { | |
94 | __be16 *out = map->work_buf; | |
95 | *out = cpu_to_be16((reg << 12) | val); | |
96 | } | |
97 | ||
98 | static void regmap_format_7_9_write(struct regmap *map, | |
99 | unsigned int reg, unsigned int val) | |
100 | { | |
101 | __be16 *out = map->work_buf; | |
102 | *out = cpu_to_be16((reg << 9) | val); | |
103 | } | |
104 | ||
7e5ec63e LPC |
105 | static void regmap_format_10_14_write(struct regmap *map, |
106 | unsigned int reg, unsigned int val) | |
107 | { | |
108 | u8 *out = map->work_buf; | |
109 | ||
110 | out[2] = val; | |
111 | out[1] = (val >> 8) | (reg << 6); | |
112 | out[0] = reg >> 2; | |
113 | } | |
114 | ||
d939fb9a | 115 | static void regmap_format_8(void *buf, unsigned int val, unsigned int shift) |
b83a313b MB |
116 | { |
117 | u8 *b = buf; | |
118 | ||
d939fb9a | 119 | b[0] = val << shift; |
b83a313b MB |
120 | } |
121 | ||
d939fb9a | 122 | static void regmap_format_16(void *buf, unsigned int val, unsigned int shift) |
b83a313b MB |
123 | { |
124 | __be16 *b = buf; | |
125 | ||
d939fb9a | 126 | b[0] = cpu_to_be16(val << shift); |
b83a313b MB |
127 | } |
128 | ||
d939fb9a | 129 | static void regmap_format_24(void *buf, unsigned int val, unsigned int shift) |
ea279fc5 MR |
130 | { |
131 | u8 *b = buf; | |
132 | ||
d939fb9a MR |
133 | val <<= shift; |
134 | ||
ea279fc5 MR |
135 | b[0] = val >> 16; |
136 | b[1] = val >> 8; | |
137 | b[2] = val; | |
138 | } | |
139 | ||
d939fb9a | 140 | static void regmap_format_32(void *buf, unsigned int val, unsigned int shift) |
7d5e525b MB |
141 | { |
142 | __be32 *b = buf; | |
143 | ||
d939fb9a | 144 | b[0] = cpu_to_be32(val << shift); |
7d5e525b MB |
145 | } |
146 | ||
b83a313b MB |
147 | static unsigned int regmap_parse_8(void *buf) |
148 | { | |
149 | u8 *b = buf; | |
150 | ||
151 | return b[0]; | |
152 | } | |
153 | ||
154 | static unsigned int regmap_parse_16(void *buf) | |
155 | { | |
156 | __be16 *b = buf; | |
157 | ||
158 | b[0] = be16_to_cpu(b[0]); | |
159 | ||
160 | return b[0]; | |
161 | } | |
162 | ||
ea279fc5 MR |
163 | static unsigned int regmap_parse_24(void *buf) |
164 | { | |
165 | u8 *b = buf; | |
166 | unsigned int ret = b[2]; | |
167 | ret |= ((unsigned int)b[1]) << 8; | |
168 | ret |= ((unsigned int)b[0]) << 16; | |
169 | ||
170 | return ret; | |
171 | } | |
172 | ||
7d5e525b MB |
173 | static unsigned int regmap_parse_32(void *buf) |
174 | { | |
175 | __be32 *b = buf; | |
176 | ||
177 | b[0] = be32_to_cpu(b[0]); | |
178 | ||
179 | return b[0]; | |
180 | } | |
181 | ||
bacdbe07 SW |
182 | static void regmap_lock_mutex(struct regmap *map) |
183 | { | |
184 | mutex_lock(&map->mutex); | |
185 | } | |
186 | ||
187 | static void regmap_unlock_mutex(struct regmap *map) | |
188 | { | |
189 | mutex_unlock(&map->mutex); | |
190 | } | |
191 | ||
192 | static void regmap_lock_spinlock(struct regmap *map) | |
193 | { | |
194 | spin_lock(&map->spinlock); | |
195 | } | |
196 | ||
197 | static void regmap_unlock_spinlock(struct regmap *map) | |
198 | { | |
199 | spin_unlock(&map->spinlock); | |
200 | } | |
201 | ||
b83a313b MB |
202 | /** |
203 | * regmap_init(): Initialise register map | |
204 | * | |
205 | * @dev: Device that will be interacted with | |
206 | * @bus: Bus-specific callbacks to use with device | |
0135bbcc | 207 | * @bus_context: Data passed to bus-specific callbacks |
b83a313b MB |
208 | * @config: Configuration for register map |
209 | * | |
210 | * The return value will be an ERR_PTR() on error or a valid pointer to | |
211 | * a struct regmap. This function should generally not be called | |
212 | * directly, it should be called by bus-specific init functions. | |
213 | */ | |
214 | struct regmap *regmap_init(struct device *dev, | |
215 | const struct regmap_bus *bus, | |
0135bbcc | 216 | void *bus_context, |
b83a313b MB |
217 | const struct regmap_config *config) |
218 | { | |
219 | struct regmap *map; | |
220 | int ret = -EINVAL; | |
221 | ||
222 | if (!bus || !config) | |
abbb18fb | 223 | goto err; |
b83a313b MB |
224 | |
225 | map = kzalloc(sizeof(*map), GFP_KERNEL); | |
226 | if (map == NULL) { | |
227 | ret = -ENOMEM; | |
228 | goto err; | |
229 | } | |
230 | ||
bacdbe07 SW |
231 | if (bus->fast_io) { |
232 | spin_lock_init(&map->spinlock); | |
233 | map->lock = regmap_lock_spinlock; | |
234 | map->unlock = regmap_unlock_spinlock; | |
235 | } else { | |
236 | mutex_init(&map->mutex); | |
237 | map->lock = regmap_lock_mutex; | |
238 | map->unlock = regmap_unlock_mutex; | |
239 | } | |
b83a313b | 240 | map->format.buf_size = (config->reg_bits + config->val_bits) / 8; |
c212accc | 241 | map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8); |
82159ba8 | 242 | map->format.pad_bytes = config->pad_bits / 8; |
c212accc | 243 | map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8); |
82159ba8 | 244 | map->format.buf_size += map->format.pad_bytes; |
d939fb9a | 245 | map->reg_shift = config->pad_bits % 8; |
b83a313b MB |
246 | map->dev = dev; |
247 | map->bus = bus; | |
0135bbcc | 248 | map->bus_context = bus_context; |
2e2ae66d MB |
249 | map->max_register = config->max_register; |
250 | map->writeable_reg = config->writeable_reg; | |
251 | map->readable_reg = config->readable_reg; | |
252 | map->volatile_reg = config->volatile_reg; | |
2efe1642 | 253 | map->precious_reg = config->precious_reg; |
5d1729e7 | 254 | map->cache_type = config->cache_type; |
b83a313b | 255 | |
6f306441 LPC |
256 | if (config->read_flag_mask || config->write_flag_mask) { |
257 | map->read_flag_mask = config->read_flag_mask; | |
258 | map->write_flag_mask = config->write_flag_mask; | |
259 | } else { | |
260 | map->read_flag_mask = bus->read_flag_mask; | |
261 | } | |
262 | ||
d939fb9a | 263 | switch (config->reg_bits + map->reg_shift) { |
9aa50750 WS |
264 | case 2: |
265 | switch (config->val_bits) { | |
266 | case 6: | |
267 | map->format.format_write = regmap_format_2_6_write; | |
268 | break; | |
269 | default: | |
270 | goto err_map; | |
271 | } | |
272 | break; | |
273 | ||
b83a313b MB |
274 | case 4: |
275 | switch (config->val_bits) { | |
276 | case 12: | |
277 | map->format.format_write = regmap_format_4_12_write; | |
278 | break; | |
279 | default: | |
280 | goto err_map; | |
281 | } | |
282 | break; | |
283 | ||
284 | case 7: | |
285 | switch (config->val_bits) { | |
286 | case 9: | |
287 | map->format.format_write = regmap_format_7_9_write; | |
288 | break; | |
289 | default: | |
290 | goto err_map; | |
291 | } | |
292 | break; | |
293 | ||
7e5ec63e LPC |
294 | case 10: |
295 | switch (config->val_bits) { | |
296 | case 14: | |
297 | map->format.format_write = regmap_format_10_14_write; | |
298 | break; | |
299 | default: | |
300 | goto err_map; | |
301 | } | |
302 | break; | |
303 | ||
b83a313b MB |
304 | case 8: |
305 | map->format.format_reg = regmap_format_8; | |
306 | break; | |
307 | ||
308 | case 16: | |
309 | map->format.format_reg = regmap_format_16; | |
310 | break; | |
311 | ||
7d5e525b MB |
312 | case 32: |
313 | map->format.format_reg = regmap_format_32; | |
314 | break; | |
315 | ||
b83a313b MB |
316 | default: |
317 | goto err_map; | |
318 | } | |
319 | ||
320 | switch (config->val_bits) { | |
321 | case 8: | |
322 | map->format.format_val = regmap_format_8; | |
323 | map->format.parse_val = regmap_parse_8; | |
324 | break; | |
325 | case 16: | |
326 | map->format.format_val = regmap_format_16; | |
327 | map->format.parse_val = regmap_parse_16; | |
328 | break; | |
ea279fc5 MR |
329 | case 24: |
330 | map->format.format_val = regmap_format_24; | |
331 | map->format.parse_val = regmap_parse_24; | |
332 | break; | |
7d5e525b MB |
333 | case 32: |
334 | map->format.format_val = regmap_format_32; | |
335 | map->format.parse_val = regmap_parse_32; | |
336 | break; | |
b83a313b MB |
337 | } |
338 | ||
339 | if (!map->format.format_write && | |
340 | !(map->format.format_reg && map->format.format_val)) | |
341 | goto err_map; | |
342 | ||
82159ba8 | 343 | map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL); |
b83a313b MB |
344 | if (map->work_buf == NULL) { |
345 | ret = -ENOMEM; | |
5204f5e3 | 346 | goto err_map; |
b83a313b MB |
347 | } |
348 | ||
d3c242e1 | 349 | regmap_debugfs_init(map, config->name); |
052d2cd1 | 350 | |
e5e3b8ab | 351 | ret = regcache_init(map, config); |
5d1729e7 | 352 | if (ret < 0) |
58072cbf | 353 | goto err_free_workbuf; |
5d1729e7 | 354 | |
b83a313b MB |
355 | return map; |
356 | ||
58072cbf LPC |
357 | err_free_workbuf: |
358 | kfree(map->work_buf); | |
b83a313b MB |
359 | err_map: |
360 | kfree(map); | |
361 | err: | |
362 | return ERR_PTR(ret); | |
363 | } | |
364 | EXPORT_SYMBOL_GPL(regmap_init); | |
365 | ||
c0eb4676 MB |
366 | static void devm_regmap_release(struct device *dev, void *res) |
367 | { | |
368 | regmap_exit(*(struct regmap **)res); | |
369 | } | |
370 | ||
371 | /** | |
372 | * devm_regmap_init(): Initialise managed register map | |
373 | * | |
374 | * @dev: Device that will be interacted with | |
375 | * @bus: Bus-specific callbacks to use with device | |
0135bbcc | 376 | * @bus_context: Data passed to bus-specific callbacks |
c0eb4676 MB |
377 | * @config: Configuration for register map |
378 | * | |
379 | * The return value will be an ERR_PTR() on error or a valid pointer | |
380 | * to a struct regmap. This function should generally not be called | |
381 | * directly, it should be called by bus-specific init functions. The | |
382 | * map will be automatically freed by the device management code. | |
383 | */ | |
384 | struct regmap *devm_regmap_init(struct device *dev, | |
385 | const struct regmap_bus *bus, | |
0135bbcc | 386 | void *bus_context, |
c0eb4676 MB |
387 | const struct regmap_config *config) |
388 | { | |
389 | struct regmap **ptr, *regmap; | |
390 | ||
391 | ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL); | |
392 | if (!ptr) | |
393 | return ERR_PTR(-ENOMEM); | |
394 | ||
0135bbcc | 395 | regmap = regmap_init(dev, bus, bus_context, config); |
c0eb4676 MB |
396 | if (!IS_ERR(regmap)) { |
397 | *ptr = regmap; | |
398 | devres_add(dev, ptr); | |
399 | } else { | |
400 | devres_free(ptr); | |
401 | } | |
402 | ||
403 | return regmap; | |
404 | } | |
405 | EXPORT_SYMBOL_GPL(devm_regmap_init); | |
406 | ||
bf315173 MB |
407 | /** |
408 | * regmap_reinit_cache(): Reinitialise the current register cache | |
409 | * | |
410 | * @map: Register map to operate on. | |
411 | * @config: New configuration. Only the cache data will be used. | |
412 | * | |
413 | * Discard any existing register cache for the map and initialize a | |
414 | * new cache. This can be used to restore the cache to defaults or to | |
415 | * update the cache configuration to reflect runtime discovery of the | |
416 | * hardware. | |
417 | */ | |
418 | int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) | |
419 | { | |
420 | int ret; | |
421 | ||
bacdbe07 | 422 | map->lock(map); |
bf315173 MB |
423 | |
424 | regcache_exit(map); | |
a24f64a6 | 425 | regmap_debugfs_exit(map); |
bf315173 MB |
426 | |
427 | map->max_register = config->max_register; | |
428 | map->writeable_reg = config->writeable_reg; | |
429 | map->readable_reg = config->readable_reg; | |
430 | map->volatile_reg = config->volatile_reg; | |
431 | map->precious_reg = config->precious_reg; | |
432 | map->cache_type = config->cache_type; | |
433 | ||
d3c242e1 | 434 | regmap_debugfs_init(map, config->name); |
a24f64a6 | 435 | |
421e8d2d MB |
436 | map->cache_bypass = false; |
437 | map->cache_only = false; | |
438 | ||
bf315173 MB |
439 | ret = regcache_init(map, config); |
440 | ||
bacdbe07 | 441 | map->unlock(map); |
bf315173 MB |
442 | |
443 | return ret; | |
444 | } | |
445 | ||
b83a313b MB |
446 | /** |
447 | * regmap_exit(): Free a previously allocated register map | |
448 | */ | |
449 | void regmap_exit(struct regmap *map) | |
450 | { | |
5d1729e7 | 451 | regcache_exit(map); |
31244e39 | 452 | regmap_debugfs_exit(map); |
0135bbcc SW |
453 | if (map->bus->free_context) |
454 | map->bus->free_context(map->bus_context); | |
b83a313b | 455 | kfree(map->work_buf); |
b83a313b MB |
456 | kfree(map); |
457 | } | |
458 | EXPORT_SYMBOL_GPL(regmap_exit); | |
459 | ||
460 | static int _regmap_raw_write(struct regmap *map, unsigned int reg, | |
461 | const void *val, size_t val_len) | |
462 | { | |
6f306441 | 463 | u8 *u8 = map->work_buf; |
b83a313b MB |
464 | void *buf; |
465 | int ret = -ENOTSUPP; | |
466 | size_t len; | |
73304781 MB |
467 | int i; |
468 | ||
469 | /* Check for unwritable registers before we start */ | |
470 | if (map->writeable_reg) | |
471 | for (i = 0; i < val_len / map->format.val_bytes; i++) | |
472 | if (!map->writeable_reg(map->dev, reg + i)) | |
473 | return -EINVAL; | |
b83a313b | 474 | |
c9157198 LD |
475 | if (!map->cache_bypass && map->format.parse_val) { |
476 | unsigned int ival; | |
477 | int val_bytes = map->format.val_bytes; | |
478 | for (i = 0; i < val_len / val_bytes; i++) { | |
479 | memcpy(map->work_buf, val + (i * val_bytes), val_bytes); | |
480 | ival = map->format.parse_val(map->work_buf); | |
481 | ret = regcache_write(map, reg + i, ival); | |
482 | if (ret) { | |
483 | dev_err(map->dev, | |
484 | "Error in caching of register: %u ret: %d\n", | |
485 | reg + i, ret); | |
486 | return ret; | |
487 | } | |
488 | } | |
489 | if (map->cache_only) { | |
490 | map->cache_dirty = true; | |
491 | return 0; | |
492 | } | |
493 | } | |
494 | ||
d939fb9a | 495 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
b83a313b | 496 | |
6f306441 LPC |
497 | u8[0] |= map->write_flag_mask; |
498 | ||
fb2736bb MB |
499 | trace_regmap_hw_write_start(map->dev, reg, |
500 | val_len / map->format.val_bytes); | |
501 | ||
2547e201 MB |
502 | /* If we're doing a single register write we can probably just |
503 | * send the work_buf directly, otherwise try to do a gather | |
504 | * write. | |
505 | */ | |
82159ba8 MB |
506 | if (val == (map->work_buf + map->format.pad_bytes + |
507 | map->format.reg_bytes)) | |
0135bbcc | 508 | ret = map->bus->write(map->bus_context, map->work_buf, |
82159ba8 MB |
509 | map->format.reg_bytes + |
510 | map->format.pad_bytes + | |
511 | val_len); | |
2547e201 | 512 | else if (map->bus->gather_write) |
0135bbcc | 513 | ret = map->bus->gather_write(map->bus_context, map->work_buf, |
82159ba8 MB |
514 | map->format.reg_bytes + |
515 | map->format.pad_bytes, | |
b83a313b MB |
516 | val, val_len); |
517 | ||
2547e201 | 518 | /* If that didn't work fall back on linearising by hand. */ |
b83a313b | 519 | if (ret == -ENOTSUPP) { |
82159ba8 MB |
520 | len = map->format.reg_bytes + map->format.pad_bytes + val_len; |
521 | buf = kzalloc(len, GFP_KERNEL); | |
b83a313b MB |
522 | if (!buf) |
523 | return -ENOMEM; | |
524 | ||
525 | memcpy(buf, map->work_buf, map->format.reg_bytes); | |
82159ba8 MB |
526 | memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, |
527 | val, val_len); | |
0135bbcc | 528 | ret = map->bus->write(map->bus_context, buf, len); |
b83a313b MB |
529 | |
530 | kfree(buf); | |
531 | } | |
532 | ||
fb2736bb MB |
533 | trace_regmap_hw_write_done(map->dev, reg, |
534 | val_len / map->format.val_bytes); | |
535 | ||
b83a313b MB |
536 | return ret; |
537 | } | |
538 | ||
4d2dc095 DP |
539 | int _regmap_write(struct regmap *map, unsigned int reg, |
540 | unsigned int val) | |
b83a313b | 541 | { |
fb2736bb | 542 | int ret; |
b83a313b MB |
543 | BUG_ON(!map->format.format_write && !map->format.format_val); |
544 | ||
c9157198 | 545 | if (!map->cache_bypass && map->format.format_write) { |
5d1729e7 DP |
546 | ret = regcache_write(map, reg, val); |
547 | if (ret != 0) | |
548 | return ret; | |
8ae0d7e8 MB |
549 | if (map->cache_only) { |
550 | map->cache_dirty = true; | |
5d1729e7 | 551 | return 0; |
8ae0d7e8 | 552 | } |
5d1729e7 DP |
553 | } |
554 | ||
fb2736bb MB |
555 | trace_regmap_reg_write(map->dev, reg, val); |
556 | ||
b83a313b MB |
557 | if (map->format.format_write) { |
558 | map->format.format_write(map, reg, val); | |
559 | ||
fb2736bb MB |
560 | trace_regmap_hw_write_start(map->dev, reg, 1); |
561 | ||
0135bbcc | 562 | ret = map->bus->write(map->bus_context, map->work_buf, |
fb2736bb MB |
563 | map->format.buf_size); |
564 | ||
565 | trace_regmap_hw_write_done(map->dev, reg, 1); | |
566 | ||
567 | return ret; | |
b83a313b | 568 | } else { |
82159ba8 | 569 | map->format.format_val(map->work_buf + map->format.reg_bytes |
d939fb9a | 570 | + map->format.pad_bytes, val, 0); |
b83a313b | 571 | return _regmap_raw_write(map, reg, |
82159ba8 MB |
572 | map->work_buf + |
573 | map->format.reg_bytes + | |
574 | map->format.pad_bytes, | |
b83a313b MB |
575 | map->format.val_bytes); |
576 | } | |
577 | } | |
578 | ||
579 | /** | |
580 | * regmap_write(): Write a value to a single register | |
581 | * | |
582 | * @map: Register map to write to | |
583 | * @reg: Register to write to | |
584 | * @val: Value to be written | |
585 | * | |
586 | * A value of zero will be returned on success, a negative errno will | |
587 | * be returned in error cases. | |
588 | */ | |
589 | int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) | |
590 | { | |
591 | int ret; | |
592 | ||
bacdbe07 | 593 | map->lock(map); |
b83a313b MB |
594 | |
595 | ret = _regmap_write(map, reg, val); | |
596 | ||
bacdbe07 | 597 | map->unlock(map); |
b83a313b MB |
598 | |
599 | return ret; | |
600 | } | |
601 | EXPORT_SYMBOL_GPL(regmap_write); | |
602 | ||
603 | /** | |
604 | * regmap_raw_write(): Write raw values to one or more registers | |
605 | * | |
606 | * @map: Register map to write to | |
607 | * @reg: Initial register to write to | |
608 | * @val: Block of data to be written, laid out for direct transmission to the | |
609 | * device | |
610 | * @val_len: Length of data pointed to by val. | |
611 | * | |
612 | * This function is intended to be used for things like firmware | |
613 | * download where a large block of data needs to be transferred to the | |
614 | * device. No formatting will be done on the data provided. | |
615 | * | |
616 | * A value of zero will be returned on success, a negative errno will | |
617 | * be returned in error cases. | |
618 | */ | |
619 | int regmap_raw_write(struct regmap *map, unsigned int reg, | |
620 | const void *val, size_t val_len) | |
621 | { | |
622 | int ret; | |
623 | ||
851960ba SW |
624 | if (val_len % map->format.val_bytes) |
625 | return -EINVAL; | |
626 | ||
bacdbe07 | 627 | map->lock(map); |
b83a313b MB |
628 | |
629 | ret = _regmap_raw_write(map, reg, val, val_len); | |
630 | ||
bacdbe07 | 631 | map->unlock(map); |
b83a313b MB |
632 | |
633 | return ret; | |
634 | } | |
635 | EXPORT_SYMBOL_GPL(regmap_raw_write); | |
636 | ||
8eaeb219 LD |
637 | /* |
638 | * regmap_bulk_write(): Write multiple registers to the device | |
639 | * | |
640 | * @map: Register map to write to | |
641 | * @reg: First register to be write from | |
642 | * @val: Block of data to be written, in native register size for device | |
643 | * @val_count: Number of registers to write | |
644 | * | |
645 | * This function is intended to be used for writing a large block of | |
646 | * data to be device either in single transfer or multiple transfer. | |
647 | * | |
648 | * A value of zero will be returned on success, a negative errno will | |
649 | * be returned in error cases. | |
650 | */ | |
651 | int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, | |
652 | size_t val_count) | |
653 | { | |
654 | int ret = 0, i; | |
655 | size_t val_bytes = map->format.val_bytes; | |
656 | void *wval; | |
657 | ||
658 | if (!map->format.parse_val) | |
659 | return -EINVAL; | |
660 | ||
bacdbe07 | 661 | map->lock(map); |
8eaeb219 LD |
662 | |
663 | /* No formatting is require if val_byte is 1 */ | |
664 | if (val_bytes == 1) { | |
665 | wval = (void *)val; | |
666 | } else { | |
667 | wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL); | |
668 | if (!wval) { | |
669 | ret = -ENOMEM; | |
670 | dev_err(map->dev, "Error in memory allocation\n"); | |
671 | goto out; | |
672 | } | |
673 | for (i = 0; i < val_count * val_bytes; i += val_bytes) | |
674 | map->format.parse_val(wval + i); | |
675 | } | |
676 | ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count); | |
677 | ||
678 | if (val_bytes != 1) | |
679 | kfree(wval); | |
680 | ||
681 | out: | |
bacdbe07 | 682 | map->unlock(map); |
8eaeb219 LD |
683 | return ret; |
684 | } | |
685 | EXPORT_SYMBOL_GPL(regmap_bulk_write); | |
686 | ||
b83a313b MB |
687 | static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
688 | unsigned int val_len) | |
689 | { | |
690 | u8 *u8 = map->work_buf; | |
691 | int ret; | |
692 | ||
d939fb9a | 693 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
b83a313b MB |
694 | |
695 | /* | |
6f306441 | 696 | * Some buses or devices flag reads by setting the high bits in the |
b83a313b MB |
697 | * register addresss; since it's always the high bits for all |
698 | * current formats we can do this here rather than in | |
699 | * formatting. This may break if we get interesting formats. | |
700 | */ | |
6f306441 | 701 | u8[0] |= map->read_flag_mask; |
b83a313b | 702 | |
fb2736bb MB |
703 | trace_regmap_hw_read_start(map->dev, reg, |
704 | val_len / map->format.val_bytes); | |
705 | ||
0135bbcc | 706 | ret = map->bus->read(map->bus_context, map->work_buf, |
82159ba8 | 707 | map->format.reg_bytes + map->format.pad_bytes, |
40c5cc26 | 708 | val, val_len); |
b83a313b | 709 | |
fb2736bb MB |
710 | trace_regmap_hw_read_done(map->dev, reg, |
711 | val_len / map->format.val_bytes); | |
712 | ||
713 | return ret; | |
b83a313b MB |
714 | } |
715 | ||
716 | static int _regmap_read(struct regmap *map, unsigned int reg, | |
717 | unsigned int *val) | |
718 | { | |
719 | int ret; | |
720 | ||
5d1729e7 DP |
721 | if (!map->cache_bypass) { |
722 | ret = regcache_read(map, reg, val); | |
723 | if (ret == 0) | |
724 | return 0; | |
725 | } | |
726 | ||
19254411 LPC |
727 | if (!map->format.parse_val) |
728 | return -EINVAL; | |
729 | ||
5d1729e7 DP |
730 | if (map->cache_only) |
731 | return -EBUSY; | |
732 | ||
b83a313b | 733 | ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes); |
fb2736bb | 734 | if (ret == 0) { |
b83a313b | 735 | *val = map->format.parse_val(map->work_buf); |
fb2736bb MB |
736 | trace_regmap_reg_read(map->dev, reg, *val); |
737 | } | |
b83a313b MB |
738 | |
739 | return ret; | |
740 | } | |
741 | ||
742 | /** | |
743 | * regmap_read(): Read a value from a single register | |
744 | * | |
745 | * @map: Register map to write to | |
746 | * @reg: Register to be read from | |
747 | * @val: Pointer to store read value | |
748 | * | |
749 | * A value of zero will be returned on success, a negative errno will | |
750 | * be returned in error cases. | |
751 | */ | |
752 | int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) | |
753 | { | |
754 | int ret; | |
755 | ||
bacdbe07 | 756 | map->lock(map); |
b83a313b MB |
757 | |
758 | ret = _regmap_read(map, reg, val); | |
759 | ||
bacdbe07 | 760 | map->unlock(map); |
b83a313b MB |
761 | |
762 | return ret; | |
763 | } | |
764 | EXPORT_SYMBOL_GPL(regmap_read); | |
765 | ||
766 | /** | |
767 | * regmap_raw_read(): Read raw data from the device | |
768 | * | |
769 | * @map: Register map to write to | |
770 | * @reg: First register to be read from | |
771 | * @val: Pointer to store read value | |
772 | * @val_len: Size of data to read | |
773 | * | |
774 | * A value of zero will be returned on success, a negative errno will | |
775 | * be returned in error cases. | |
776 | */ | |
777 | int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, | |
778 | size_t val_len) | |
779 | { | |
b8fb5ab1 MB |
780 | size_t val_bytes = map->format.val_bytes; |
781 | size_t val_count = val_len / val_bytes; | |
782 | unsigned int v; | |
783 | int ret, i; | |
04e016ad | 784 | |
851960ba SW |
785 | if (val_len % map->format.val_bytes) |
786 | return -EINVAL; | |
787 | ||
bacdbe07 | 788 | map->lock(map); |
b83a313b | 789 | |
b8fb5ab1 MB |
790 | if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || |
791 | map->cache_type == REGCACHE_NONE) { | |
792 | /* Physical block read if there's no cache involved */ | |
793 | ret = _regmap_raw_read(map, reg, val, val_len); | |
794 | ||
795 | } else { | |
796 | /* Otherwise go word by word for the cache; should be low | |
797 | * cost as we expect to hit the cache. | |
798 | */ | |
799 | for (i = 0; i < val_count; i++) { | |
800 | ret = _regmap_read(map, reg + i, &v); | |
801 | if (ret != 0) | |
802 | goto out; | |
803 | ||
d939fb9a | 804 | map->format.format_val(val + (i * val_bytes), v, 0); |
b8fb5ab1 MB |
805 | } |
806 | } | |
b83a313b | 807 | |
b8fb5ab1 | 808 | out: |
bacdbe07 | 809 | map->unlock(map); |
b83a313b MB |
810 | |
811 | return ret; | |
812 | } | |
813 | EXPORT_SYMBOL_GPL(regmap_raw_read); | |
814 | ||
815 | /** | |
816 | * regmap_bulk_read(): Read multiple registers from the device | |
817 | * | |
818 | * @map: Register map to write to | |
819 | * @reg: First register to be read from | |
820 | * @val: Pointer to store read value, in native register size for device | |
821 | * @val_count: Number of registers to read | |
822 | * | |
823 | * A value of zero will be returned on success, a negative errno will | |
824 | * be returned in error cases. | |
825 | */ | |
826 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, | |
827 | size_t val_count) | |
828 | { | |
829 | int ret, i; | |
830 | size_t val_bytes = map->format.val_bytes; | |
82cd9965 | 831 | bool vol = regmap_volatile_range(map, reg, val_count); |
5d1729e7 | 832 | |
b83a313b MB |
833 | if (!map->format.parse_val) |
834 | return -EINVAL; | |
835 | ||
de2d808f MB |
836 | if (vol || map->cache_type == REGCACHE_NONE) { |
837 | ret = regmap_raw_read(map, reg, val, val_bytes * val_count); | |
838 | if (ret != 0) | |
839 | return ret; | |
840 | ||
841 | for (i = 0; i < val_count * val_bytes; i += val_bytes) | |
842 | map->format.parse_val(val + i); | |
843 | } else { | |
844 | for (i = 0; i < val_count; i++) { | |
845 | ret = regmap_read(map, reg + i, val + (i * val_bytes)); | |
846 | if (ret != 0) | |
847 | return ret; | |
848 | } | |
849 | } | |
b83a313b MB |
850 | |
851 | return 0; | |
852 | } | |
853 | EXPORT_SYMBOL_GPL(regmap_bulk_read); | |
854 | ||
018690d3 MB |
855 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
856 | unsigned int mask, unsigned int val, | |
857 | bool *change) | |
b83a313b MB |
858 | { |
859 | int ret; | |
d91e8db2 | 860 | unsigned int tmp, orig; |
b83a313b | 861 | |
bacdbe07 | 862 | map->lock(map); |
b83a313b | 863 | |
d91e8db2 | 864 | ret = _regmap_read(map, reg, &orig); |
b83a313b MB |
865 | if (ret != 0) |
866 | goto out; | |
867 | ||
d91e8db2 | 868 | tmp = orig & ~mask; |
b83a313b MB |
869 | tmp |= val & mask; |
870 | ||
018690d3 | 871 | if (tmp != orig) { |
d91e8db2 | 872 | ret = _regmap_write(map, reg, tmp); |
018690d3 MB |
873 | *change = true; |
874 | } else { | |
875 | *change = false; | |
876 | } | |
b83a313b MB |
877 | |
878 | out: | |
bacdbe07 | 879 | map->unlock(map); |
b83a313b MB |
880 | |
881 | return ret; | |
882 | } | |
018690d3 MB |
883 | |
884 | /** | |
885 | * regmap_update_bits: Perform a read/modify/write cycle on the register map | |
886 | * | |
887 | * @map: Register map to update | |
888 | * @reg: Register to update | |
889 | * @mask: Bitmask to change | |
890 | * @val: New value for bitmask | |
891 | * | |
892 | * Returns zero for success, a negative number on error. | |
893 | */ | |
894 | int regmap_update_bits(struct regmap *map, unsigned int reg, | |
895 | unsigned int mask, unsigned int val) | |
896 | { | |
897 | bool change; | |
898 | return _regmap_update_bits(map, reg, mask, val, &change); | |
899 | } | |
b83a313b | 900 | EXPORT_SYMBOL_GPL(regmap_update_bits); |
31244e39 | 901 | |
018690d3 MB |
902 | /** |
903 | * regmap_update_bits_check: Perform a read/modify/write cycle on the | |
904 | * register map and report if updated | |
905 | * | |
906 | * @map: Register map to update | |
907 | * @reg: Register to update | |
908 | * @mask: Bitmask to change | |
909 | * @val: New value for bitmask | |
910 | * @change: Boolean indicating if a write was done | |
911 | * | |
912 | * Returns zero for success, a negative number on error. | |
913 | */ | |
914 | int regmap_update_bits_check(struct regmap *map, unsigned int reg, | |
915 | unsigned int mask, unsigned int val, | |
916 | bool *change) | |
917 | { | |
918 | return _regmap_update_bits(map, reg, mask, val, change); | |
919 | } | |
920 | EXPORT_SYMBOL_GPL(regmap_update_bits_check); | |
921 | ||
22f0d90a MB |
922 | /** |
923 | * regmap_register_patch: Register and apply register updates to be applied | |
924 | * on device initialistion | |
925 | * | |
926 | * @map: Register map to apply updates to. | |
927 | * @regs: Values to update. | |
928 | * @num_regs: Number of entries in regs. | |
929 | * | |
930 | * Register a set of register updates to be applied to the device | |
931 | * whenever the device registers are synchronised with the cache and | |
932 | * apply them immediately. Typically this is used to apply | |
933 | * corrections to be applied to the device defaults on startup, such | |
934 | * as the updates some vendors provide to undocumented registers. | |
935 | */ | |
936 | int regmap_register_patch(struct regmap *map, const struct reg_default *regs, | |
937 | int num_regs) | |
938 | { | |
939 | int i, ret; | |
940 | bool bypass; | |
941 | ||
942 | /* If needed the implementation can be extended to support this */ | |
943 | if (map->patch) | |
944 | return -EBUSY; | |
945 | ||
bacdbe07 | 946 | map->lock(map); |
22f0d90a MB |
947 | |
948 | bypass = map->cache_bypass; | |
949 | ||
950 | map->cache_bypass = true; | |
951 | ||
952 | /* Write out first; it's useful to apply even if we fail later. */ | |
953 | for (i = 0; i < num_regs; i++) { | |
954 | ret = _regmap_write(map, regs[i].reg, regs[i].def); | |
955 | if (ret != 0) { | |
956 | dev_err(map->dev, "Failed to write %x = %x: %d\n", | |
957 | regs[i].reg, regs[i].def, ret); | |
958 | goto out; | |
959 | } | |
960 | } | |
961 | ||
2a14d7d9 | 962 | map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL); |
22f0d90a MB |
963 | if (map->patch != NULL) { |
964 | memcpy(map->patch, regs, | |
965 | num_regs * sizeof(struct reg_default)); | |
966 | map->patch_regs = num_regs; | |
967 | } else { | |
968 | ret = -ENOMEM; | |
969 | } | |
970 | ||
971 | out: | |
972 | map->cache_bypass = bypass; | |
973 | ||
bacdbe07 | 974 | map->unlock(map); |
22f0d90a MB |
975 | |
976 | return ret; | |
977 | } | |
978 | EXPORT_SYMBOL_GPL(regmap_register_patch); | |
979 | ||
eae4b51b | 980 | /* |
a6539c32 MB |
981 | * regmap_get_val_bytes(): Report the size of a register value |
982 | * | |
983 | * Report the size of a register value, mainly intended to for use by | |
984 | * generic infrastructure built on top of regmap. | |
985 | */ | |
986 | int regmap_get_val_bytes(struct regmap *map) | |
987 | { | |
988 | if (map->format.format_write) | |
989 | return -EINVAL; | |
990 | ||
991 | return map->format.val_bytes; | |
992 | } | |
993 | EXPORT_SYMBOL_GPL(regmap_get_val_bytes); | |
994 | ||
31244e39 MB |
995 | static int __init regmap_initcall(void) |
996 | { | |
997 | regmap_debugfs_initcall(); | |
998 | ||
999 | return 0; | |
1000 | } | |
1001 | postcore_initcall(regmap_initcall); |