]>
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 | ||
72b39f6f MB |
202 | static void dev_get_regmap_release(struct device *dev, void *res) |
203 | { | |
204 | /* | |
205 | * We don't actually have anything to do here; the goal here | |
206 | * is not to manage the regmap but to provide a simple way to | |
207 | * get the regmap back given a struct device. | |
208 | */ | |
209 | } | |
210 | ||
b83a313b MB |
211 | /** |
212 | * regmap_init(): Initialise register map | |
213 | * | |
214 | * @dev: Device that will be interacted with | |
215 | * @bus: Bus-specific callbacks to use with device | |
0135bbcc | 216 | * @bus_context: Data passed to bus-specific callbacks |
b83a313b MB |
217 | * @config: Configuration for register map |
218 | * | |
219 | * The return value will be an ERR_PTR() on error or a valid pointer to | |
220 | * a struct regmap. This function should generally not be called | |
221 | * directly, it should be called by bus-specific init functions. | |
222 | */ | |
223 | struct regmap *regmap_init(struct device *dev, | |
224 | const struct regmap_bus *bus, | |
0135bbcc | 225 | void *bus_context, |
b83a313b MB |
226 | const struct regmap_config *config) |
227 | { | |
72b39f6f | 228 | struct regmap *map, **m; |
b83a313b MB |
229 | int ret = -EINVAL; |
230 | ||
231 | if (!bus || !config) | |
abbb18fb | 232 | goto err; |
b83a313b MB |
233 | |
234 | map = kzalloc(sizeof(*map), GFP_KERNEL); | |
235 | if (map == NULL) { | |
236 | ret = -ENOMEM; | |
237 | goto err; | |
238 | } | |
239 | ||
bacdbe07 SW |
240 | if (bus->fast_io) { |
241 | spin_lock_init(&map->spinlock); | |
242 | map->lock = regmap_lock_spinlock; | |
243 | map->unlock = regmap_unlock_spinlock; | |
244 | } else { | |
245 | mutex_init(&map->mutex); | |
246 | map->lock = regmap_lock_mutex; | |
247 | map->unlock = regmap_unlock_mutex; | |
248 | } | |
b83a313b | 249 | map->format.buf_size = (config->reg_bits + config->val_bits) / 8; |
c212accc | 250 | map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8); |
82159ba8 | 251 | map->format.pad_bytes = config->pad_bits / 8; |
c212accc | 252 | map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8); |
82159ba8 | 253 | map->format.buf_size += map->format.pad_bytes; |
d939fb9a | 254 | map->reg_shift = config->pad_bits % 8; |
f01ee60f SW |
255 | if (config->reg_stride) |
256 | map->reg_stride = config->reg_stride; | |
257 | else | |
258 | map->reg_stride = 1; | |
2e33caf1 | 259 | map->use_single_rw = config->use_single_rw; |
b83a313b MB |
260 | map->dev = dev; |
261 | map->bus = bus; | |
0135bbcc | 262 | map->bus_context = bus_context; |
2e2ae66d MB |
263 | map->max_register = config->max_register; |
264 | map->writeable_reg = config->writeable_reg; | |
265 | map->readable_reg = config->readable_reg; | |
266 | map->volatile_reg = config->volatile_reg; | |
2efe1642 | 267 | map->precious_reg = config->precious_reg; |
5d1729e7 | 268 | map->cache_type = config->cache_type; |
72b39f6f | 269 | map->name = config->name; |
b83a313b | 270 | |
6f306441 LPC |
271 | if (config->read_flag_mask || config->write_flag_mask) { |
272 | map->read_flag_mask = config->read_flag_mask; | |
273 | map->write_flag_mask = config->write_flag_mask; | |
274 | } else { | |
275 | map->read_flag_mask = bus->read_flag_mask; | |
276 | } | |
277 | ||
d939fb9a | 278 | switch (config->reg_bits + map->reg_shift) { |
9aa50750 WS |
279 | case 2: |
280 | switch (config->val_bits) { | |
281 | case 6: | |
282 | map->format.format_write = regmap_format_2_6_write; | |
283 | break; | |
284 | default: | |
285 | goto err_map; | |
286 | } | |
287 | break; | |
288 | ||
b83a313b MB |
289 | case 4: |
290 | switch (config->val_bits) { | |
291 | case 12: | |
292 | map->format.format_write = regmap_format_4_12_write; | |
293 | break; | |
294 | default: | |
295 | goto err_map; | |
296 | } | |
297 | break; | |
298 | ||
299 | case 7: | |
300 | switch (config->val_bits) { | |
301 | case 9: | |
302 | map->format.format_write = regmap_format_7_9_write; | |
303 | break; | |
304 | default: | |
305 | goto err_map; | |
306 | } | |
307 | break; | |
308 | ||
7e5ec63e LPC |
309 | case 10: |
310 | switch (config->val_bits) { | |
311 | case 14: | |
312 | map->format.format_write = regmap_format_10_14_write; | |
313 | break; | |
314 | default: | |
315 | goto err_map; | |
316 | } | |
317 | break; | |
318 | ||
b83a313b MB |
319 | case 8: |
320 | map->format.format_reg = regmap_format_8; | |
321 | break; | |
322 | ||
323 | case 16: | |
324 | map->format.format_reg = regmap_format_16; | |
325 | break; | |
326 | ||
7d5e525b MB |
327 | case 32: |
328 | map->format.format_reg = regmap_format_32; | |
329 | break; | |
330 | ||
b83a313b MB |
331 | default: |
332 | goto err_map; | |
333 | } | |
334 | ||
335 | switch (config->val_bits) { | |
336 | case 8: | |
337 | map->format.format_val = regmap_format_8; | |
338 | map->format.parse_val = regmap_parse_8; | |
339 | break; | |
340 | case 16: | |
341 | map->format.format_val = regmap_format_16; | |
342 | map->format.parse_val = regmap_parse_16; | |
343 | break; | |
ea279fc5 MR |
344 | case 24: |
345 | map->format.format_val = regmap_format_24; | |
346 | map->format.parse_val = regmap_parse_24; | |
347 | break; | |
7d5e525b MB |
348 | case 32: |
349 | map->format.format_val = regmap_format_32; | |
350 | map->format.parse_val = regmap_parse_32; | |
351 | break; | |
b83a313b MB |
352 | } |
353 | ||
7a647614 MB |
354 | if (map->format.format_write) |
355 | map->use_single_rw = true; | |
356 | ||
b83a313b MB |
357 | if (!map->format.format_write && |
358 | !(map->format.format_reg && map->format.format_val)) | |
359 | goto err_map; | |
360 | ||
82159ba8 | 361 | map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL); |
b83a313b MB |
362 | if (map->work_buf == NULL) { |
363 | ret = -ENOMEM; | |
5204f5e3 | 364 | goto err_map; |
b83a313b MB |
365 | } |
366 | ||
d3c242e1 | 367 | regmap_debugfs_init(map, config->name); |
052d2cd1 | 368 | |
e5e3b8ab | 369 | ret = regcache_init(map, config); |
5d1729e7 | 370 | if (ret < 0) |
bfaa25f3 | 371 | goto err_debugfs; |
5d1729e7 | 372 | |
72b39f6f MB |
373 | /* Add a devres resource for dev_get_regmap() */ |
374 | m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL); | |
375 | if (!m) { | |
376 | ret = -ENOMEM; | |
377 | goto err_cache; | |
378 | } | |
379 | *m = map; | |
380 | devres_add(dev, m); | |
381 | ||
b83a313b MB |
382 | return map; |
383 | ||
72b39f6f MB |
384 | err_cache: |
385 | regcache_exit(map); | |
bfaa25f3 SW |
386 | err_debugfs: |
387 | regmap_debugfs_exit(map); | |
58072cbf | 388 | kfree(map->work_buf); |
b83a313b MB |
389 | err_map: |
390 | kfree(map); | |
391 | err: | |
392 | return ERR_PTR(ret); | |
393 | } | |
394 | EXPORT_SYMBOL_GPL(regmap_init); | |
395 | ||
c0eb4676 MB |
396 | static void devm_regmap_release(struct device *dev, void *res) |
397 | { | |
398 | regmap_exit(*(struct regmap **)res); | |
399 | } | |
400 | ||
401 | /** | |
402 | * devm_regmap_init(): Initialise managed register map | |
403 | * | |
404 | * @dev: Device that will be interacted with | |
405 | * @bus: Bus-specific callbacks to use with device | |
0135bbcc | 406 | * @bus_context: Data passed to bus-specific callbacks |
c0eb4676 MB |
407 | * @config: Configuration for register map |
408 | * | |
409 | * The return value will be an ERR_PTR() on error or a valid pointer | |
410 | * to a struct regmap. This function should generally not be called | |
411 | * directly, it should be called by bus-specific init functions. The | |
412 | * map will be automatically freed by the device management code. | |
413 | */ | |
414 | struct regmap *devm_regmap_init(struct device *dev, | |
415 | const struct regmap_bus *bus, | |
0135bbcc | 416 | void *bus_context, |
c0eb4676 MB |
417 | const struct regmap_config *config) |
418 | { | |
419 | struct regmap **ptr, *regmap; | |
420 | ||
421 | ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL); | |
422 | if (!ptr) | |
423 | return ERR_PTR(-ENOMEM); | |
424 | ||
0135bbcc | 425 | regmap = regmap_init(dev, bus, bus_context, config); |
c0eb4676 MB |
426 | if (!IS_ERR(regmap)) { |
427 | *ptr = regmap; | |
428 | devres_add(dev, ptr); | |
429 | } else { | |
430 | devres_free(ptr); | |
431 | } | |
432 | ||
433 | return regmap; | |
434 | } | |
435 | EXPORT_SYMBOL_GPL(devm_regmap_init); | |
436 | ||
bf315173 MB |
437 | /** |
438 | * regmap_reinit_cache(): Reinitialise the current register cache | |
439 | * | |
440 | * @map: Register map to operate on. | |
441 | * @config: New configuration. Only the cache data will be used. | |
442 | * | |
443 | * Discard any existing register cache for the map and initialize a | |
444 | * new cache. This can be used to restore the cache to defaults or to | |
445 | * update the cache configuration to reflect runtime discovery of the | |
446 | * hardware. | |
447 | */ | |
448 | int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config) | |
449 | { | |
450 | int ret; | |
451 | ||
bacdbe07 | 452 | map->lock(map); |
bf315173 MB |
453 | |
454 | regcache_exit(map); | |
a24f64a6 | 455 | regmap_debugfs_exit(map); |
bf315173 MB |
456 | |
457 | map->max_register = config->max_register; | |
458 | map->writeable_reg = config->writeable_reg; | |
459 | map->readable_reg = config->readable_reg; | |
460 | map->volatile_reg = config->volatile_reg; | |
461 | map->precious_reg = config->precious_reg; | |
462 | map->cache_type = config->cache_type; | |
463 | ||
d3c242e1 | 464 | regmap_debugfs_init(map, config->name); |
a24f64a6 | 465 | |
421e8d2d MB |
466 | map->cache_bypass = false; |
467 | map->cache_only = false; | |
468 | ||
bf315173 MB |
469 | ret = regcache_init(map, config); |
470 | ||
bacdbe07 | 471 | map->unlock(map); |
bf315173 MB |
472 | |
473 | return ret; | |
474 | } | |
475 | ||
b83a313b MB |
476 | /** |
477 | * regmap_exit(): Free a previously allocated register map | |
478 | */ | |
479 | void regmap_exit(struct regmap *map) | |
480 | { | |
5d1729e7 | 481 | regcache_exit(map); |
31244e39 | 482 | regmap_debugfs_exit(map); |
0135bbcc SW |
483 | if (map->bus->free_context) |
484 | map->bus->free_context(map->bus_context); | |
b83a313b | 485 | kfree(map->work_buf); |
b83a313b MB |
486 | kfree(map); |
487 | } | |
488 | EXPORT_SYMBOL_GPL(regmap_exit); | |
489 | ||
72b39f6f MB |
490 | static int dev_get_regmap_match(struct device *dev, void *res, void *data) |
491 | { | |
492 | struct regmap **r = res; | |
493 | if (!r || !*r) { | |
494 | WARN_ON(!r || !*r); | |
495 | return 0; | |
496 | } | |
497 | ||
498 | /* If the user didn't specify a name match any */ | |
499 | if (data) | |
500 | return (*r)->name == data; | |
501 | else | |
502 | return 1; | |
503 | } | |
504 | ||
505 | /** | |
506 | * dev_get_regmap(): Obtain the regmap (if any) for a device | |
507 | * | |
508 | * @dev: Device to retrieve the map for | |
509 | * @name: Optional name for the register map, usually NULL. | |
510 | * | |
511 | * Returns the regmap for the device if one is present, or NULL. If | |
512 | * name is specified then it must match the name specified when | |
513 | * registering the device, if it is NULL then the first regmap found | |
514 | * will be used. Devices with multiple register maps are very rare, | |
515 | * generic code should normally not need to specify a name. | |
516 | */ | |
517 | struct regmap *dev_get_regmap(struct device *dev, const char *name) | |
518 | { | |
519 | struct regmap **r = devres_find(dev, dev_get_regmap_release, | |
520 | dev_get_regmap_match, (void *)name); | |
521 | ||
522 | if (!r) | |
523 | return NULL; | |
524 | return *r; | |
525 | } | |
526 | EXPORT_SYMBOL_GPL(dev_get_regmap); | |
527 | ||
b83a313b MB |
528 | static int _regmap_raw_write(struct regmap *map, unsigned int reg, |
529 | const void *val, size_t val_len) | |
530 | { | |
6f306441 | 531 | u8 *u8 = map->work_buf; |
b83a313b MB |
532 | void *buf; |
533 | int ret = -ENOTSUPP; | |
534 | size_t len; | |
73304781 MB |
535 | int i; |
536 | ||
537 | /* Check for unwritable registers before we start */ | |
538 | if (map->writeable_reg) | |
539 | for (i = 0; i < val_len / map->format.val_bytes; i++) | |
f01ee60f SW |
540 | if (!map->writeable_reg(map->dev, |
541 | reg + (i * map->reg_stride))) | |
73304781 | 542 | return -EINVAL; |
b83a313b | 543 | |
c9157198 LD |
544 | if (!map->cache_bypass && map->format.parse_val) { |
545 | unsigned int ival; | |
546 | int val_bytes = map->format.val_bytes; | |
547 | for (i = 0; i < val_len / val_bytes; i++) { | |
548 | memcpy(map->work_buf, val + (i * val_bytes), val_bytes); | |
549 | ival = map->format.parse_val(map->work_buf); | |
f01ee60f SW |
550 | ret = regcache_write(map, reg + (i * map->reg_stride), |
551 | ival); | |
c9157198 LD |
552 | if (ret) { |
553 | dev_err(map->dev, | |
554 | "Error in caching of register: %u ret: %d\n", | |
555 | reg + i, ret); | |
556 | return ret; | |
557 | } | |
558 | } | |
559 | if (map->cache_only) { | |
560 | map->cache_dirty = true; | |
561 | return 0; | |
562 | } | |
563 | } | |
564 | ||
d939fb9a | 565 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
b83a313b | 566 | |
6f306441 LPC |
567 | u8[0] |= map->write_flag_mask; |
568 | ||
fb2736bb MB |
569 | trace_regmap_hw_write_start(map->dev, reg, |
570 | val_len / map->format.val_bytes); | |
571 | ||
2547e201 MB |
572 | /* If we're doing a single register write we can probably just |
573 | * send the work_buf directly, otherwise try to do a gather | |
574 | * write. | |
575 | */ | |
82159ba8 MB |
576 | if (val == (map->work_buf + map->format.pad_bytes + |
577 | map->format.reg_bytes)) | |
0135bbcc | 578 | ret = map->bus->write(map->bus_context, map->work_buf, |
82159ba8 MB |
579 | map->format.reg_bytes + |
580 | map->format.pad_bytes + | |
581 | val_len); | |
2547e201 | 582 | else if (map->bus->gather_write) |
0135bbcc | 583 | ret = map->bus->gather_write(map->bus_context, map->work_buf, |
82159ba8 MB |
584 | map->format.reg_bytes + |
585 | map->format.pad_bytes, | |
b83a313b MB |
586 | val, val_len); |
587 | ||
2547e201 | 588 | /* If that didn't work fall back on linearising by hand. */ |
b83a313b | 589 | if (ret == -ENOTSUPP) { |
82159ba8 MB |
590 | len = map->format.reg_bytes + map->format.pad_bytes + val_len; |
591 | buf = kzalloc(len, GFP_KERNEL); | |
b83a313b MB |
592 | if (!buf) |
593 | return -ENOMEM; | |
594 | ||
595 | memcpy(buf, map->work_buf, map->format.reg_bytes); | |
82159ba8 MB |
596 | memcpy(buf + map->format.reg_bytes + map->format.pad_bytes, |
597 | val, val_len); | |
0135bbcc | 598 | ret = map->bus->write(map->bus_context, buf, len); |
b83a313b MB |
599 | |
600 | kfree(buf); | |
601 | } | |
602 | ||
fb2736bb MB |
603 | trace_regmap_hw_write_done(map->dev, reg, |
604 | val_len / map->format.val_bytes); | |
605 | ||
b83a313b MB |
606 | return ret; |
607 | } | |
608 | ||
4d2dc095 DP |
609 | int _regmap_write(struct regmap *map, unsigned int reg, |
610 | unsigned int val) | |
b83a313b | 611 | { |
fb2736bb | 612 | int ret; |
b83a313b MB |
613 | BUG_ON(!map->format.format_write && !map->format.format_val); |
614 | ||
c9157198 | 615 | if (!map->cache_bypass && map->format.format_write) { |
5d1729e7 DP |
616 | ret = regcache_write(map, reg, val); |
617 | if (ret != 0) | |
618 | return ret; | |
8ae0d7e8 MB |
619 | if (map->cache_only) { |
620 | map->cache_dirty = true; | |
5d1729e7 | 621 | return 0; |
8ae0d7e8 | 622 | } |
5d1729e7 DP |
623 | } |
624 | ||
fb2736bb MB |
625 | trace_regmap_reg_write(map->dev, reg, val); |
626 | ||
b83a313b MB |
627 | if (map->format.format_write) { |
628 | map->format.format_write(map, reg, val); | |
629 | ||
fb2736bb MB |
630 | trace_regmap_hw_write_start(map->dev, reg, 1); |
631 | ||
0135bbcc | 632 | ret = map->bus->write(map->bus_context, map->work_buf, |
fb2736bb MB |
633 | map->format.buf_size); |
634 | ||
635 | trace_regmap_hw_write_done(map->dev, reg, 1); | |
636 | ||
637 | return ret; | |
b83a313b | 638 | } else { |
82159ba8 | 639 | map->format.format_val(map->work_buf + map->format.reg_bytes |
d939fb9a | 640 | + map->format.pad_bytes, val, 0); |
b83a313b | 641 | return _regmap_raw_write(map, reg, |
82159ba8 MB |
642 | map->work_buf + |
643 | map->format.reg_bytes + | |
644 | map->format.pad_bytes, | |
b83a313b MB |
645 | map->format.val_bytes); |
646 | } | |
647 | } | |
648 | ||
649 | /** | |
650 | * regmap_write(): Write a value to a single register | |
651 | * | |
652 | * @map: Register map to write to | |
653 | * @reg: Register to write to | |
654 | * @val: Value to be written | |
655 | * | |
656 | * A value of zero will be returned on success, a negative errno will | |
657 | * be returned in error cases. | |
658 | */ | |
659 | int regmap_write(struct regmap *map, unsigned int reg, unsigned int val) | |
660 | { | |
661 | int ret; | |
662 | ||
f01ee60f SW |
663 | if (reg % map->reg_stride) |
664 | return -EINVAL; | |
665 | ||
bacdbe07 | 666 | map->lock(map); |
b83a313b MB |
667 | |
668 | ret = _regmap_write(map, reg, val); | |
669 | ||
bacdbe07 | 670 | map->unlock(map); |
b83a313b MB |
671 | |
672 | return ret; | |
673 | } | |
674 | EXPORT_SYMBOL_GPL(regmap_write); | |
675 | ||
676 | /** | |
677 | * regmap_raw_write(): Write raw values to one or more registers | |
678 | * | |
679 | * @map: Register map to write to | |
680 | * @reg: Initial register to write to | |
681 | * @val: Block of data to be written, laid out for direct transmission to the | |
682 | * device | |
683 | * @val_len: Length of data pointed to by val. | |
684 | * | |
685 | * This function is intended to be used for things like firmware | |
686 | * download where a large block of data needs to be transferred to the | |
687 | * device. No formatting will be done on the data provided. | |
688 | * | |
689 | * A value of zero will be returned on success, a negative errno will | |
690 | * be returned in error cases. | |
691 | */ | |
692 | int regmap_raw_write(struct regmap *map, unsigned int reg, | |
693 | const void *val, size_t val_len) | |
694 | { | |
695 | int ret; | |
696 | ||
851960ba SW |
697 | if (val_len % map->format.val_bytes) |
698 | return -EINVAL; | |
f01ee60f SW |
699 | if (reg % map->reg_stride) |
700 | return -EINVAL; | |
851960ba | 701 | |
bacdbe07 | 702 | map->lock(map); |
b83a313b MB |
703 | |
704 | ret = _regmap_raw_write(map, reg, val, val_len); | |
705 | ||
bacdbe07 | 706 | map->unlock(map); |
b83a313b MB |
707 | |
708 | return ret; | |
709 | } | |
710 | EXPORT_SYMBOL_GPL(regmap_raw_write); | |
711 | ||
8eaeb219 LD |
712 | /* |
713 | * regmap_bulk_write(): Write multiple registers to the device | |
714 | * | |
715 | * @map: Register map to write to | |
716 | * @reg: First register to be write from | |
717 | * @val: Block of data to be written, in native register size for device | |
718 | * @val_count: Number of registers to write | |
719 | * | |
720 | * This function is intended to be used for writing a large block of | |
721 | * data to be device either in single transfer or multiple transfer. | |
722 | * | |
723 | * A value of zero will be returned on success, a negative errno will | |
724 | * be returned in error cases. | |
725 | */ | |
726 | int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, | |
727 | size_t val_count) | |
728 | { | |
729 | int ret = 0, i; | |
730 | size_t val_bytes = map->format.val_bytes; | |
731 | void *wval; | |
732 | ||
733 | if (!map->format.parse_val) | |
734 | return -EINVAL; | |
f01ee60f SW |
735 | if (reg % map->reg_stride) |
736 | return -EINVAL; | |
8eaeb219 | 737 | |
bacdbe07 | 738 | map->lock(map); |
8eaeb219 LD |
739 | |
740 | /* No formatting is require if val_byte is 1 */ | |
741 | if (val_bytes == 1) { | |
742 | wval = (void *)val; | |
743 | } else { | |
744 | wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL); | |
745 | if (!wval) { | |
746 | ret = -ENOMEM; | |
747 | dev_err(map->dev, "Error in memory allocation\n"); | |
748 | goto out; | |
749 | } | |
750 | for (i = 0; i < val_count * val_bytes; i += val_bytes) | |
751 | map->format.parse_val(wval + i); | |
752 | } | |
2e33caf1 AJ |
753 | /* |
754 | * Some devices does not support bulk write, for | |
755 | * them we have a series of single write operations. | |
756 | */ | |
757 | if (map->use_single_rw) { | |
758 | for (i = 0; i < val_count; i++) { | |
759 | ret = regmap_raw_write(map, | |
760 | reg + (i * map->reg_stride), | |
761 | val + (i * val_bytes), | |
762 | val_bytes); | |
763 | if (ret != 0) | |
764 | return ret; | |
765 | } | |
766 | } else { | |
767 | ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count); | |
768 | } | |
8eaeb219 LD |
769 | |
770 | if (val_bytes != 1) | |
771 | kfree(wval); | |
772 | ||
773 | out: | |
bacdbe07 | 774 | map->unlock(map); |
8eaeb219 LD |
775 | return ret; |
776 | } | |
777 | EXPORT_SYMBOL_GPL(regmap_bulk_write); | |
778 | ||
b83a313b MB |
779 | static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val, |
780 | unsigned int val_len) | |
781 | { | |
782 | u8 *u8 = map->work_buf; | |
783 | int ret; | |
784 | ||
d939fb9a | 785 | map->format.format_reg(map->work_buf, reg, map->reg_shift); |
b83a313b MB |
786 | |
787 | /* | |
6f306441 | 788 | * Some buses or devices flag reads by setting the high bits in the |
b83a313b MB |
789 | * register addresss; since it's always the high bits for all |
790 | * current formats we can do this here rather than in | |
791 | * formatting. This may break if we get interesting formats. | |
792 | */ | |
6f306441 | 793 | u8[0] |= map->read_flag_mask; |
b83a313b | 794 | |
fb2736bb MB |
795 | trace_regmap_hw_read_start(map->dev, reg, |
796 | val_len / map->format.val_bytes); | |
797 | ||
0135bbcc | 798 | ret = map->bus->read(map->bus_context, map->work_buf, |
82159ba8 | 799 | map->format.reg_bytes + map->format.pad_bytes, |
40c5cc26 | 800 | val, val_len); |
b83a313b | 801 | |
fb2736bb MB |
802 | trace_regmap_hw_read_done(map->dev, reg, |
803 | val_len / map->format.val_bytes); | |
804 | ||
805 | return ret; | |
b83a313b MB |
806 | } |
807 | ||
808 | static int _regmap_read(struct regmap *map, unsigned int reg, | |
809 | unsigned int *val) | |
810 | { | |
811 | int ret; | |
812 | ||
5d1729e7 DP |
813 | if (!map->cache_bypass) { |
814 | ret = regcache_read(map, reg, val); | |
815 | if (ret == 0) | |
816 | return 0; | |
817 | } | |
818 | ||
19254411 LPC |
819 | if (!map->format.parse_val) |
820 | return -EINVAL; | |
821 | ||
5d1729e7 DP |
822 | if (map->cache_only) |
823 | return -EBUSY; | |
824 | ||
b83a313b | 825 | ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes); |
fb2736bb | 826 | if (ret == 0) { |
b83a313b | 827 | *val = map->format.parse_val(map->work_buf); |
fb2736bb MB |
828 | trace_regmap_reg_read(map->dev, reg, *val); |
829 | } | |
b83a313b | 830 | |
f2985367 MB |
831 | if (ret == 0 && !map->cache_bypass) |
832 | regcache_write(map, reg, *val); | |
833 | ||
b83a313b MB |
834 | return ret; |
835 | } | |
836 | ||
837 | /** | |
838 | * regmap_read(): Read a value from a single register | |
839 | * | |
840 | * @map: Register map to write to | |
841 | * @reg: Register to be read from | |
842 | * @val: Pointer to store read value | |
843 | * | |
844 | * A value of zero will be returned on success, a negative errno will | |
845 | * be returned in error cases. | |
846 | */ | |
847 | int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) | |
848 | { | |
849 | int ret; | |
850 | ||
f01ee60f SW |
851 | if (reg % map->reg_stride) |
852 | return -EINVAL; | |
853 | ||
bacdbe07 | 854 | map->lock(map); |
b83a313b MB |
855 | |
856 | ret = _regmap_read(map, reg, val); | |
857 | ||
bacdbe07 | 858 | map->unlock(map); |
b83a313b MB |
859 | |
860 | return ret; | |
861 | } | |
862 | EXPORT_SYMBOL_GPL(regmap_read); | |
863 | ||
864 | /** | |
865 | * regmap_raw_read(): Read raw data from the device | |
866 | * | |
867 | * @map: Register map to write to | |
868 | * @reg: First register to be read from | |
869 | * @val: Pointer to store read value | |
870 | * @val_len: Size of data to read | |
871 | * | |
872 | * A value of zero will be returned on success, a negative errno will | |
873 | * be returned in error cases. | |
874 | */ | |
875 | int regmap_raw_read(struct regmap *map, unsigned int reg, void *val, | |
876 | size_t val_len) | |
877 | { | |
b8fb5ab1 MB |
878 | size_t val_bytes = map->format.val_bytes; |
879 | size_t val_count = val_len / val_bytes; | |
880 | unsigned int v; | |
881 | int ret, i; | |
04e016ad | 882 | |
851960ba SW |
883 | if (val_len % map->format.val_bytes) |
884 | return -EINVAL; | |
f01ee60f SW |
885 | if (reg % map->reg_stride) |
886 | return -EINVAL; | |
851960ba | 887 | |
bacdbe07 | 888 | map->lock(map); |
b83a313b | 889 | |
b8fb5ab1 MB |
890 | if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || |
891 | map->cache_type == REGCACHE_NONE) { | |
892 | /* Physical block read if there's no cache involved */ | |
893 | ret = _regmap_raw_read(map, reg, val, val_len); | |
894 | ||
895 | } else { | |
896 | /* Otherwise go word by word for the cache; should be low | |
897 | * cost as we expect to hit the cache. | |
898 | */ | |
899 | for (i = 0; i < val_count; i++) { | |
f01ee60f SW |
900 | ret = _regmap_read(map, reg + (i * map->reg_stride), |
901 | &v); | |
b8fb5ab1 MB |
902 | if (ret != 0) |
903 | goto out; | |
904 | ||
d939fb9a | 905 | map->format.format_val(val + (i * val_bytes), v, 0); |
b8fb5ab1 MB |
906 | } |
907 | } | |
b83a313b | 908 | |
b8fb5ab1 | 909 | out: |
bacdbe07 | 910 | map->unlock(map); |
b83a313b MB |
911 | |
912 | return ret; | |
913 | } | |
914 | EXPORT_SYMBOL_GPL(regmap_raw_read); | |
915 | ||
916 | /** | |
917 | * regmap_bulk_read(): Read multiple registers from the device | |
918 | * | |
919 | * @map: Register map to write to | |
920 | * @reg: First register to be read from | |
921 | * @val: Pointer to store read value, in native register size for device | |
922 | * @val_count: Number of registers to read | |
923 | * | |
924 | * A value of zero will be returned on success, a negative errno will | |
925 | * be returned in error cases. | |
926 | */ | |
927 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, | |
928 | size_t val_count) | |
929 | { | |
930 | int ret, i; | |
931 | size_t val_bytes = map->format.val_bytes; | |
82cd9965 | 932 | bool vol = regmap_volatile_range(map, reg, val_count); |
5d1729e7 | 933 | |
b83a313b MB |
934 | if (!map->format.parse_val) |
935 | return -EINVAL; | |
f01ee60f SW |
936 | if (reg % map->reg_stride) |
937 | return -EINVAL; | |
b83a313b | 938 | |
de2d808f | 939 | if (vol || map->cache_type == REGCACHE_NONE) { |
2e33caf1 AJ |
940 | /* |
941 | * Some devices does not support bulk read, for | |
942 | * them we have a series of single read operations. | |
943 | */ | |
944 | if (map->use_single_rw) { | |
945 | for (i = 0; i < val_count; i++) { | |
946 | ret = regmap_raw_read(map, | |
947 | reg + (i * map->reg_stride), | |
948 | val + (i * val_bytes), | |
949 | val_bytes); | |
950 | if (ret != 0) | |
951 | return ret; | |
952 | } | |
953 | } else { | |
954 | ret = regmap_raw_read(map, reg, val, | |
955 | val_bytes * val_count); | |
956 | if (ret != 0) | |
957 | return ret; | |
958 | } | |
de2d808f MB |
959 | |
960 | for (i = 0; i < val_count * val_bytes; i += val_bytes) | |
961 | map->format.parse_val(val + i); | |
962 | } else { | |
963 | for (i = 0; i < val_count; i++) { | |
6560ffd1 | 964 | unsigned int ival; |
f01ee60f | 965 | ret = regmap_read(map, reg + (i * map->reg_stride), |
25061d28 | 966 | &ival); |
de2d808f MB |
967 | if (ret != 0) |
968 | return ret; | |
6560ffd1 | 969 | memcpy(val + (i * val_bytes), &ival, val_bytes); |
de2d808f MB |
970 | } |
971 | } | |
b83a313b MB |
972 | |
973 | return 0; | |
974 | } | |
975 | EXPORT_SYMBOL_GPL(regmap_bulk_read); | |
976 | ||
018690d3 MB |
977 | static int _regmap_update_bits(struct regmap *map, unsigned int reg, |
978 | unsigned int mask, unsigned int val, | |
979 | bool *change) | |
b83a313b MB |
980 | { |
981 | int ret; | |
d91e8db2 | 982 | unsigned int tmp, orig; |
b83a313b | 983 | |
bacdbe07 | 984 | map->lock(map); |
b83a313b | 985 | |
d91e8db2 | 986 | ret = _regmap_read(map, reg, &orig); |
b83a313b MB |
987 | if (ret != 0) |
988 | goto out; | |
989 | ||
d91e8db2 | 990 | tmp = orig & ~mask; |
b83a313b MB |
991 | tmp |= val & mask; |
992 | ||
018690d3 | 993 | if (tmp != orig) { |
d91e8db2 | 994 | ret = _regmap_write(map, reg, tmp); |
018690d3 MB |
995 | *change = true; |
996 | } else { | |
997 | *change = false; | |
998 | } | |
b83a313b MB |
999 | |
1000 | out: | |
bacdbe07 | 1001 | map->unlock(map); |
b83a313b MB |
1002 | |
1003 | return ret; | |
1004 | } | |
018690d3 MB |
1005 | |
1006 | /** | |
1007 | * regmap_update_bits: Perform a read/modify/write cycle on the register map | |
1008 | * | |
1009 | * @map: Register map to update | |
1010 | * @reg: Register to update | |
1011 | * @mask: Bitmask to change | |
1012 | * @val: New value for bitmask | |
1013 | * | |
1014 | * Returns zero for success, a negative number on error. | |
1015 | */ | |
1016 | int regmap_update_bits(struct regmap *map, unsigned int reg, | |
1017 | unsigned int mask, unsigned int val) | |
1018 | { | |
1019 | bool change; | |
1020 | return _regmap_update_bits(map, reg, mask, val, &change); | |
1021 | } | |
b83a313b | 1022 | EXPORT_SYMBOL_GPL(regmap_update_bits); |
31244e39 | 1023 | |
018690d3 MB |
1024 | /** |
1025 | * regmap_update_bits_check: Perform a read/modify/write cycle on the | |
1026 | * register map and report if updated | |
1027 | * | |
1028 | * @map: Register map to update | |
1029 | * @reg: Register to update | |
1030 | * @mask: Bitmask to change | |
1031 | * @val: New value for bitmask | |
1032 | * @change: Boolean indicating if a write was done | |
1033 | * | |
1034 | * Returns zero for success, a negative number on error. | |
1035 | */ | |
1036 | int regmap_update_bits_check(struct regmap *map, unsigned int reg, | |
1037 | unsigned int mask, unsigned int val, | |
1038 | bool *change) | |
1039 | { | |
1040 | return _regmap_update_bits(map, reg, mask, val, change); | |
1041 | } | |
1042 | EXPORT_SYMBOL_GPL(regmap_update_bits_check); | |
1043 | ||
22f0d90a MB |
1044 | /** |
1045 | * regmap_register_patch: Register and apply register updates to be applied | |
1046 | * on device initialistion | |
1047 | * | |
1048 | * @map: Register map to apply updates to. | |
1049 | * @regs: Values to update. | |
1050 | * @num_regs: Number of entries in regs. | |
1051 | * | |
1052 | * Register a set of register updates to be applied to the device | |
1053 | * whenever the device registers are synchronised with the cache and | |
1054 | * apply them immediately. Typically this is used to apply | |
1055 | * corrections to be applied to the device defaults on startup, such | |
1056 | * as the updates some vendors provide to undocumented registers. | |
1057 | */ | |
1058 | int regmap_register_patch(struct regmap *map, const struct reg_default *regs, | |
1059 | int num_regs) | |
1060 | { | |
1061 | int i, ret; | |
1062 | bool bypass; | |
1063 | ||
1064 | /* If needed the implementation can be extended to support this */ | |
1065 | if (map->patch) | |
1066 | return -EBUSY; | |
1067 | ||
bacdbe07 | 1068 | map->lock(map); |
22f0d90a MB |
1069 | |
1070 | bypass = map->cache_bypass; | |
1071 | ||
1072 | map->cache_bypass = true; | |
1073 | ||
1074 | /* Write out first; it's useful to apply even if we fail later. */ | |
1075 | for (i = 0; i < num_regs; i++) { | |
1076 | ret = _regmap_write(map, regs[i].reg, regs[i].def); | |
1077 | if (ret != 0) { | |
1078 | dev_err(map->dev, "Failed to write %x = %x: %d\n", | |
1079 | regs[i].reg, regs[i].def, ret); | |
1080 | goto out; | |
1081 | } | |
1082 | } | |
1083 | ||
2a14d7d9 | 1084 | map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL); |
22f0d90a MB |
1085 | if (map->patch != NULL) { |
1086 | memcpy(map->patch, regs, | |
1087 | num_regs * sizeof(struct reg_default)); | |
1088 | map->patch_regs = num_regs; | |
1089 | } else { | |
1090 | ret = -ENOMEM; | |
1091 | } | |
1092 | ||
1093 | out: | |
1094 | map->cache_bypass = bypass; | |
1095 | ||
bacdbe07 | 1096 | map->unlock(map); |
22f0d90a MB |
1097 | |
1098 | return ret; | |
1099 | } | |
1100 | EXPORT_SYMBOL_GPL(regmap_register_patch); | |
1101 | ||
eae4b51b | 1102 | /* |
a6539c32 MB |
1103 | * regmap_get_val_bytes(): Report the size of a register value |
1104 | * | |
1105 | * Report the size of a register value, mainly intended to for use by | |
1106 | * generic infrastructure built on top of regmap. | |
1107 | */ | |
1108 | int regmap_get_val_bytes(struct regmap *map) | |
1109 | { | |
1110 | if (map->format.format_write) | |
1111 | return -EINVAL; | |
1112 | ||
1113 | return map->format.val_bytes; | |
1114 | } | |
1115 | EXPORT_SYMBOL_GPL(regmap_get_val_bytes); | |
1116 | ||
31244e39 MB |
1117 | static int __init regmap_initcall(void) |
1118 | { | |
1119 | regmap_debugfs_initcall(); | |
1120 | ||
1121 | return 0; | |
1122 | } | |
1123 | postcore_initcall(regmap_initcall); |