blob: 29d526e0fc0f38a3a1d75292797158ade217e1ff [file] [log] [blame]
Mark Brownb83a3132011-05-11 19:59:58 +02001/*
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
Stephen Warrenf5d6eba2012-03-09 13:17:28 -070013#include <linux/device.h>
Mark Brownb83a3132011-05-11 19:59:58 +020014#include <linux/slab.h>
Paul Gortmaker19694b52012-02-28 19:28:02 -050015#include <linux/export.h>
Mark Brownb83a3132011-05-11 19:59:58 +020016#include <linux/mutex.h>
17#include <linux/err.h>
Xiubo Lid647c192014-07-15 12:23:02 +080018#include <linux/of.h>
Krystian Garbaciak6863ca62012-06-15 11:23:56 +010019#include <linux/rbtree.h>
Stephen Warren30b2a552013-02-02 22:50:14 -070020#include <linux/sched.h>
Nariman Poushin2de9d602015-07-16 16:36:22 +010021#include <linux/delay.h>
Xiubo Lica747be2016-01-04 18:00:33 +080022#include <linux/log2.h>
Mark Brownb83a3132011-05-11 19:59:58 +020023
Mark Brownfb2736b2011-07-24 21:30:55 +010024#define CREATE_TRACE_POINTS
Steven Rostedtf58078d2015-03-19 17:50:47 -040025#include "trace.h"
Mark Brownfb2736b2011-07-24 21:30:55 +010026
Mark Brown93de9122011-07-20 22:35:37 +010027#include "internal.h"
Mark Brownb83a3132011-05-11 19:59:58 +020028
Mark Brown1044c182012-07-06 14:10:23 +010029/*
30 * Sometimes for failures during very early init the trace
31 * infrastructure isn't available early enough to be used. For this
32 * sort of problem defining LOG_DEVICE will add printks for basic
33 * register I/O on a specific device.
34 */
35#undef LOG_DEVICE
36
37static int _regmap_update_bits(struct regmap *map, unsigned int reg,
38 unsigned int mask, unsigned int val,
Kuninori Morimoto7ff05892015-06-16 08:52:22 +000039 bool *change, bool force_write);
Mark Brown1044c182012-07-06 14:10:23 +010040
Boris BREZILLON3ac17032014-04-17 11:40:11 +020041static int _regmap_bus_reg_read(void *context, unsigned int reg,
42 unsigned int *val);
Andrey Smirnovad278402013-01-12 12:54:12 -080043static int _regmap_bus_read(void *context, unsigned int reg,
44 unsigned int *val);
Andrey Smirnov07c320d2013-01-12 12:54:13 -080045static int _regmap_bus_formatted_write(void *context, unsigned int reg,
46 unsigned int val);
Boris BREZILLON3ac17032014-04-17 11:40:11 +020047static int _regmap_bus_reg_write(void *context, unsigned int reg,
48 unsigned int val);
Andrey Smirnov07c320d2013-01-12 12:54:13 -080049static int _regmap_bus_raw_write(void *context, unsigned int reg,
50 unsigned int val);
Andrey Smirnovad278402013-01-12 12:54:12 -080051
Davide Ciminaghi76aad392012-11-20 15:20:30 +010052bool regmap_reg_in_ranges(unsigned int reg,
53 const struct regmap_range *ranges,
54 unsigned int nranges)
55{
56 const struct regmap_range *r;
57 int i;
58
59 for (i = 0, r = ranges; i < nranges; i++, r++)
60 if (regmap_reg_in_range(reg, r))
61 return true;
62 return false;
63}
64EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
65
Mark Brown154881e2013-05-08 13:55:23 +010066bool regmap_check_range_table(struct regmap *map, unsigned int reg,
67 const struct regmap_access_table *table)
Davide Ciminaghi76aad392012-11-20 15:20:30 +010068{
69 /* Check "no ranges" first */
70 if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
71 return false;
72
73 /* In case zero "yes ranges" are supplied, any reg is OK */
74 if (!table->n_yes_ranges)
75 return true;
76
77 return regmap_reg_in_ranges(reg, table->yes_ranges,
78 table->n_yes_ranges);
79}
Mark Brown154881e2013-05-08 13:55:23 +010080EXPORT_SYMBOL_GPL(regmap_check_range_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +010081
Mark Brown8de2f082011-08-10 17:14:41 +090082bool regmap_writeable(struct regmap *map, unsigned int reg)
83{
84 if (map->max_register && reg > map->max_register)
85 return false;
86
87 if (map->writeable_reg)
88 return map->writeable_reg(map->dev, reg);
89
Davide Ciminaghi76aad392012-11-20 15:20:30 +010090 if (map->wr_table)
Mark Brown154881e2013-05-08 13:55:23 +010091 return regmap_check_range_table(map, reg, map->wr_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +010092
Mark Brown8de2f082011-08-10 17:14:41 +090093 return true;
94}
95
96bool regmap_readable(struct regmap *map, unsigned int reg)
97{
Lars-Peter Clausen04dc91c2015-07-13 12:26:44 +020098 if (!map->reg_read)
99 return false;
100
Mark Brown8de2f082011-08-10 17:14:41 +0900101 if (map->max_register && reg > map->max_register)
102 return false;
103
Wolfram Sang4191f192012-01-30 15:08:16 +0100104 if (map->format.format_write)
105 return false;
106
Mark Brown8de2f082011-08-10 17:14:41 +0900107 if (map->readable_reg)
108 return map->readable_reg(map->dev, reg);
109
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100110 if (map->rd_table)
Mark Brown154881e2013-05-08 13:55:23 +0100111 return regmap_check_range_table(map, reg, map->rd_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100112
Mark Brown8de2f082011-08-10 17:14:41 +0900113 return true;
114}
115
116bool regmap_volatile(struct regmap *map, unsigned int reg)
117{
Mark Brown5844a8b2014-08-26 12:12:17 +0100118 if (!map->format.format_write && !regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +0900119 return false;
120
121 if (map->volatile_reg)
122 return map->volatile_reg(map->dev, reg);
123
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100124 if (map->volatile_table)
Mark Brown154881e2013-05-08 13:55:23 +0100125 return regmap_check_range_table(map, reg, map->volatile_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100126
Mark Brownb92be6f2013-06-03 17:24:08 +0100127 if (map->cache_ops)
128 return false;
129 else
130 return true;
Mark Brown8de2f082011-08-10 17:14:41 +0900131}
132
133bool regmap_precious(struct regmap *map, unsigned int reg)
134{
Wolfram Sang4191f192012-01-30 15:08:16 +0100135 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +0900136 return false;
137
138 if (map->precious_reg)
139 return map->precious_reg(map->dev, reg);
140
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100141 if (map->precious_table)
Mark Brown154881e2013-05-08 13:55:23 +0100142 return regmap_check_range_table(map, reg, map->precious_table);
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100143
Mark Brown8de2f082011-08-10 17:14:41 +0900144 return false;
145}
146
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100147static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
Paul Bollea8f28cf2012-10-08 22:06:30 +0200148 size_t num)
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100149{
150 unsigned int i;
151
152 for (i = 0; i < num; i++)
153 if (!regmap_volatile(map, reg + i))
154 return false;
155
156 return true;
157}
158
Wolfram Sang9aa50752012-01-27 16:10:22 +0100159static void regmap_format_2_6_write(struct regmap *map,
160 unsigned int reg, unsigned int val)
161{
162 u8 *out = map->work_buf;
163
164 *out = (reg << 6) | val;
165}
166
Mark Brownb83a3132011-05-11 19:59:58 +0200167static void regmap_format_4_12_write(struct regmap *map,
168 unsigned int reg, unsigned int val)
169{
170 __be16 *out = map->work_buf;
171 *out = cpu_to_be16((reg << 12) | val);
172}
173
174static void regmap_format_7_9_write(struct regmap *map,
175 unsigned int reg, unsigned int val)
176{
177 __be16 *out = map->work_buf;
178 *out = cpu_to_be16((reg << 9) | val);
179}
180
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100181static void regmap_format_10_14_write(struct regmap *map,
182 unsigned int reg, unsigned int val)
183{
184 u8 *out = map->work_buf;
185
186 out[2] = val;
187 out[1] = (val >> 8) | (reg << 6);
188 out[0] = reg >> 2;
189}
190
Marc Reillyd939fb92012-03-16 12:11:43 +1100191static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200192{
193 u8 *b = buf;
194
Marc Reillyd939fb92012-03-16 12:11:43 +1100195 b[0] = val << shift;
Mark Brownb83a3132011-05-11 19:59:58 +0200196}
197
Stephen Warren141eba22012-05-24 10:47:26 -0600198static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
Mark Brownb83a3132011-05-11 19:59:58 +0200199{
200 __be16 *b = buf;
201
Marc Reillyd939fb92012-03-16 12:11:43 +1100202 b[0] = cpu_to_be16(val << shift);
Mark Brownb83a3132011-05-11 19:59:58 +0200203}
204
Xiubo Li4aa8c062014-04-02 18:09:07 +0800205static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
206{
207 __le16 *b = buf;
208
209 b[0] = cpu_to_le16(val << shift);
210}
211
Stephen Warren141eba22012-05-24 10:47:26 -0600212static void regmap_format_16_native(void *buf, unsigned int val,
213 unsigned int shift)
214{
215 *(u16 *)buf = val << shift;
216}
217
Marc Reillyd939fb92012-03-16 12:11:43 +1100218static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
Marc Reillyea279fc2012-03-16 12:11:42 +1100219{
220 u8 *b = buf;
221
Marc Reillyd939fb92012-03-16 12:11:43 +1100222 val <<= shift;
223
Marc Reillyea279fc2012-03-16 12:11:42 +1100224 b[0] = val >> 16;
225 b[1] = val >> 8;
226 b[2] = val;
227}
228
Stephen Warren141eba22012-05-24 10:47:26 -0600229static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
Mark Brown7d5e5252012-02-17 15:58:25 -0800230{
231 __be32 *b = buf;
232
Marc Reillyd939fb92012-03-16 12:11:43 +1100233 b[0] = cpu_to_be32(val << shift);
Mark Brown7d5e5252012-02-17 15:58:25 -0800234}
235
Xiubo Li4aa8c062014-04-02 18:09:07 +0800236static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
237{
238 __le32 *b = buf;
239
240 b[0] = cpu_to_le32(val << shift);
241}
242
Stephen Warren141eba22012-05-24 10:47:26 -0600243static void regmap_format_32_native(void *buf, unsigned int val,
244 unsigned int shift)
245{
246 *(u32 *)buf = val << shift;
247}
248
Xiubo Liafcc00b2015-12-03 17:31:52 +0800249#ifdef CONFIG_64BIT
250static void regmap_format_64_be(void *buf, unsigned int val, unsigned int shift)
251{
252 __be64 *b = buf;
253
Dan Carpenter01c377b2015-12-12 15:59:14 +0300254 b[0] = cpu_to_be64((u64)val << shift);
Xiubo Liafcc00b2015-12-03 17:31:52 +0800255}
256
257static void regmap_format_64_le(void *buf, unsigned int val, unsigned int shift)
258{
259 __le64 *b = buf;
260
Dan Carpenter01c377b2015-12-12 15:59:14 +0300261 b[0] = cpu_to_le64((u64)val << shift);
Xiubo Liafcc00b2015-12-03 17:31:52 +0800262}
263
264static void regmap_format_64_native(void *buf, unsigned int val,
265 unsigned int shift)
266{
Dan Carpenter01c377b2015-12-12 15:59:14 +0300267 *(u64 *)buf = (u64)val << shift;
Xiubo Liafcc00b2015-12-03 17:31:52 +0800268}
269#endif
270
Mark Brown8a819ff2013-03-04 09:04:51 +0800271static void regmap_parse_inplace_noop(void *buf)
Mark Brownb83a3132011-05-11 19:59:58 +0200272{
Mark Brown8a819ff2013-03-04 09:04:51 +0800273}
274
275static unsigned int regmap_parse_8(const void *buf)
276{
277 const u8 *b = buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200278
279 return b[0];
280}
281
Mark Brown8a819ff2013-03-04 09:04:51 +0800282static unsigned int regmap_parse_16_be(const void *buf)
283{
284 const __be16 *b = buf;
285
286 return be16_to_cpu(b[0]);
287}
288
Xiubo Li4aa8c062014-04-02 18:09:07 +0800289static unsigned int regmap_parse_16_le(const void *buf)
290{
291 const __le16 *b = buf;
292
293 return le16_to_cpu(b[0]);
294}
295
Mark Brown8a819ff2013-03-04 09:04:51 +0800296static void regmap_parse_16_be_inplace(void *buf)
Mark Brownb83a3132011-05-11 19:59:58 +0200297{
298 __be16 *b = buf;
299
300 b[0] = be16_to_cpu(b[0]);
Mark Brownb83a3132011-05-11 19:59:58 +0200301}
302
Xiubo Li4aa8c062014-04-02 18:09:07 +0800303static void regmap_parse_16_le_inplace(void *buf)
304{
305 __le16 *b = buf;
306
307 b[0] = le16_to_cpu(b[0]);
308}
309
Mark Brown8a819ff2013-03-04 09:04:51 +0800310static unsigned int regmap_parse_16_native(const void *buf)
Stephen Warren141eba22012-05-24 10:47:26 -0600311{
312 return *(u16 *)buf;
313}
314
Mark Brown8a819ff2013-03-04 09:04:51 +0800315static unsigned int regmap_parse_24(const void *buf)
Marc Reillyea279fc2012-03-16 12:11:42 +1100316{
Mark Brown8a819ff2013-03-04 09:04:51 +0800317 const u8 *b = buf;
Marc Reillyea279fc2012-03-16 12:11:42 +1100318 unsigned int ret = b[2];
319 ret |= ((unsigned int)b[1]) << 8;
320 ret |= ((unsigned int)b[0]) << 16;
321
322 return ret;
323}
324
Mark Brown8a819ff2013-03-04 09:04:51 +0800325static unsigned int regmap_parse_32_be(const void *buf)
326{
327 const __be32 *b = buf;
328
329 return be32_to_cpu(b[0]);
330}
331
Xiubo Li4aa8c062014-04-02 18:09:07 +0800332static unsigned int regmap_parse_32_le(const void *buf)
333{
334 const __le32 *b = buf;
335
336 return le32_to_cpu(b[0]);
337}
338
Mark Brown8a819ff2013-03-04 09:04:51 +0800339static void regmap_parse_32_be_inplace(void *buf)
Mark Brown7d5e5252012-02-17 15:58:25 -0800340{
341 __be32 *b = buf;
342
343 b[0] = be32_to_cpu(b[0]);
Mark Brown7d5e5252012-02-17 15:58:25 -0800344}
345
Xiubo Li4aa8c062014-04-02 18:09:07 +0800346static void regmap_parse_32_le_inplace(void *buf)
347{
348 __le32 *b = buf;
349
350 b[0] = le32_to_cpu(b[0]);
351}
352
Mark Brown8a819ff2013-03-04 09:04:51 +0800353static unsigned int regmap_parse_32_native(const void *buf)
Stephen Warren141eba22012-05-24 10:47:26 -0600354{
355 return *(u32 *)buf;
356}
357
Xiubo Liafcc00b2015-12-03 17:31:52 +0800358#ifdef CONFIG_64BIT
359static unsigned int regmap_parse_64_be(const void *buf)
360{
361 const __be64 *b = buf;
362
363 return be64_to_cpu(b[0]);
364}
365
366static unsigned int regmap_parse_64_le(const void *buf)
367{
368 const __le64 *b = buf;
369
370 return le64_to_cpu(b[0]);
371}
372
373static void regmap_parse_64_be_inplace(void *buf)
374{
375 __be64 *b = buf;
376
377 b[0] = be64_to_cpu(b[0]);
378}
379
380static void regmap_parse_64_le_inplace(void *buf)
381{
382 __le64 *b = buf;
383
384 b[0] = le64_to_cpu(b[0]);
385}
386
387static unsigned int regmap_parse_64_native(const void *buf)
388{
389 return *(u64 *)buf;
390}
391#endif
392
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200393static void regmap_lock_mutex(void *__map)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600394{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200395 struct regmap *map = __map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600396 mutex_lock(&map->mutex);
397}
398
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200399static void regmap_unlock_mutex(void *__map)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600400{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200401 struct regmap *map = __map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600402 mutex_unlock(&map->mutex);
403}
404
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200405static void regmap_lock_spinlock(void *__map)
Fabio Estevamb4519c72013-07-16 02:10:11 -0300406__acquires(&map->spinlock)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600407{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200408 struct regmap *map = __map;
Lars-Peter Clausen92ab1aa2013-05-24 10:29:22 +0200409 unsigned long flags;
410
411 spin_lock_irqsave(&map->spinlock, flags);
412 map->spinlock_flags = flags;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600413}
414
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200415static void regmap_unlock_spinlock(void *__map)
Fabio Estevamb4519c72013-07-16 02:10:11 -0300416__releases(&map->spinlock)
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600417{
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200418 struct regmap *map = __map;
Lars-Peter Clausen92ab1aa2013-05-24 10:29:22 +0200419 spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600420}
421
Mark Brown72b39f62012-05-08 17:44:40 +0100422static void dev_get_regmap_release(struct device *dev, void *res)
423{
424 /*
425 * We don't actually have anything to do here; the goal here
426 * is not to manage the regmap but to provide a simple way to
427 * get the regmap back given a struct device.
428 */
429}
430
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100431static bool _regmap_range_add(struct regmap *map,
432 struct regmap_range_node *data)
433{
434 struct rb_root *root = &map->range_tree;
435 struct rb_node **new = &(root->rb_node), *parent = NULL;
436
437 while (*new) {
438 struct regmap_range_node *this =
439 container_of(*new, struct regmap_range_node, node);
440
441 parent = *new;
442 if (data->range_max < this->range_min)
443 new = &((*new)->rb_left);
444 else if (data->range_min > this->range_max)
445 new = &((*new)->rb_right);
446 else
447 return false;
448 }
449
450 rb_link_node(&data->node, parent, new);
451 rb_insert_color(&data->node, root);
452
453 return true;
454}
455
456static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
457 unsigned int reg)
458{
459 struct rb_node *node = map->range_tree.rb_node;
460
461 while (node) {
462 struct regmap_range_node *this =
463 container_of(node, struct regmap_range_node, node);
464
465 if (reg < this->range_min)
466 node = node->rb_left;
467 else if (reg > this->range_max)
468 node = node->rb_right;
469 else
470 return this;
471 }
472
473 return NULL;
474}
475
476static void regmap_range_exit(struct regmap *map)
477{
478 struct rb_node *next;
479 struct regmap_range_node *range_node;
480
481 next = rb_first(&map->range_tree);
482 while (next) {
483 range_node = rb_entry(next, struct regmap_range_node, node);
484 next = rb_next(&range_node->node);
485 rb_erase(&range_node->node, &map->range_tree);
486 kfree(range_node);
487 }
488
489 kfree(map->selector_work_buf);
490}
491
Michal Simek6cfec042014-02-10 16:22:33 +0100492int regmap_attach_dev(struct device *dev, struct regmap *map,
493 const struct regmap_config *config)
494{
495 struct regmap **m;
496
497 map->dev = dev;
498
499 regmap_debugfs_init(map, config->name);
500
501 /* Add a devres resource for dev_get_regmap() */
502 m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
503 if (!m) {
504 regmap_debugfs_exit(map);
505 return -ENOMEM;
506 }
507 *m = map;
508 devres_add(dev, m);
509
510 return 0;
511}
512EXPORT_SYMBOL_GPL(regmap_attach_dev);
513
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200514static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
515 const struct regmap_config *config)
Xiubo Lid647c192014-07-15 12:23:02 +0800516{
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200517 enum regmap_endian endian;
Xiubo Lid647c192014-07-15 12:23:02 +0800518
Stephen Warren45e1a272014-08-19 10:49:07 -0600519 /* Retrieve the endianness specification from the regmap config */
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200520 endian = config->reg_format_endian;
Xiubo Lid647c192014-07-15 12:23:02 +0800521
Stephen Warren45e1a272014-08-19 10:49:07 -0600522 /* If the regmap config specified a non-default value, use that */
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200523 if (endian != REGMAP_ENDIAN_DEFAULT)
524 return endian;
Stephen Warren45e1a272014-08-19 10:49:07 -0600525
526 /* Retrieve the endianness specification from the bus config */
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200527 if (bus && bus->reg_format_endian_default)
528 endian = bus->reg_format_endian_default;
Xiubo Lid647c192014-07-15 12:23:02 +0800529
Stephen Warren45e1a272014-08-19 10:49:07 -0600530 /* If the bus specified a non-default value, use that */
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200531 if (endian != REGMAP_ENDIAN_DEFAULT)
532 return endian;
Stephen Warren45e1a272014-08-19 10:49:07 -0600533
534 /* Use this if no other value was found */
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200535 return REGMAP_ENDIAN_BIG;
536}
Stephen Warren45e1a272014-08-19 10:49:07 -0600537
Guenter Roeck3c174d22015-02-03 10:01:18 -0800538enum regmap_endian regmap_get_val_endian(struct device *dev,
539 const struct regmap_bus *bus,
540 const struct regmap_config *config)
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200541{
Pankaj Dubey6e64b6c2014-09-18 15:12:20 +0530542 struct device_node *np;
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200543 enum regmap_endian endian;
544
545 /* Retrieve the endianness specification from the regmap config */
546 endian = config->val_format_endian;
547
548 /* If the regmap config specified a non-default value, use that */
549 if (endian != REGMAP_ENDIAN_DEFAULT)
550 return endian;
551
Pankaj Dubey6e64b6c2014-09-18 15:12:20 +0530552 /* If the dev and dev->of_node exist try to get endianness from DT */
553 if (dev && dev->of_node) {
554 np = dev->of_node;
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200555
Pankaj Dubey6e64b6c2014-09-18 15:12:20 +0530556 /* Parse the device's DT node for an endianness specification */
557 if (of_property_read_bool(np, "big-endian"))
558 endian = REGMAP_ENDIAN_BIG;
559 else if (of_property_read_bool(np, "little-endian"))
560 endian = REGMAP_ENDIAN_LITTLE;
561
562 /* If the endianness was specified in DT, use that */
563 if (endian != REGMAP_ENDIAN_DEFAULT)
564 return endian;
565 }
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200566
567 /* Retrieve the endianness specification from the bus config */
568 if (bus && bus->val_format_endian_default)
569 endian = bus->val_format_endian_default;
570
571 /* If the bus specified a non-default value, use that */
572 if (endian != REGMAP_ENDIAN_DEFAULT)
573 return endian;
574
575 /* Use this if no other value was found */
576 return REGMAP_ENDIAN_BIG;
Xiubo Lid647c192014-07-15 12:23:02 +0800577}
Guenter Roeck3c174d22015-02-03 10:01:18 -0800578EXPORT_SYMBOL_GPL(regmap_get_val_endian);
Xiubo Lid647c192014-07-15 12:23:02 +0800579
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800580struct regmap *__regmap_init(struct device *dev,
581 const struct regmap_bus *bus,
582 void *bus_context,
583 const struct regmap_config *config,
584 struct lock_class_key *lock_key,
585 const char *lock_name)
Mark Brownb83a3132011-05-11 19:59:58 +0200586{
Michal Simek6cfec042014-02-10 16:22:33 +0100587 struct regmap *map;
Mark Brownb83a3132011-05-11 19:59:58 +0200588 int ret = -EINVAL;
Stephen Warren141eba22012-05-24 10:47:26 -0600589 enum regmap_endian reg_endian, val_endian;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100590 int i, j;
Mark Brownb83a3132011-05-11 19:59:58 +0200591
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800592 if (!config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100593 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200594
595 map = kzalloc(sizeof(*map), GFP_KERNEL);
596 if (map == NULL) {
597 ret = -ENOMEM;
598 goto err;
599 }
600
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200601 if (config->lock && config->unlock) {
602 map->lock = config->lock;
603 map->unlock = config->unlock;
604 map->lock_arg = config->lock_arg;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600605 } else {
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800606 if ((bus && bus->fast_io) ||
607 config->fast_io) {
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200608 spin_lock_init(&map->spinlock);
609 map->lock = regmap_lock_spinlock;
610 map->unlock = regmap_unlock_spinlock;
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800611 lockdep_set_class_and_name(&map->spinlock,
612 lock_key, lock_name);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200613 } else {
614 mutex_init(&map->mutex);
615 map->lock = regmap_lock_mutex;
616 map->unlock = regmap_unlock_mutex;
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +0800617 lockdep_set_class_and_name(&map->mutex,
618 lock_key, lock_name);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +0200619 }
620 map->lock_arg = map;
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600621 }
Stephen Boydb4a21fc2015-09-11 16:37:05 -0700622
623 /*
624 * When we write in fast-paths with regmap_bulk_write() don't allocate
625 * scratch buffers with sleeping allocations.
626 */
627 if ((bus && bus->fast_io) || config->fast_io)
628 map->alloc_flags = GFP_ATOMIC;
629 else
630 map->alloc_flags = GFP_KERNEL;
631
Wolfram Sangc212acc2012-01-28 02:16:41 +0100632 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000633 map->format.pad_bytes = config->pad_bits / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100634 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
Fabio Estevam5494a982012-05-31 21:10:30 -0300635 map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
636 config->val_bits + config->pad_bits, 8);
Marc Reillyd939fb92012-03-16 12:11:43 +1100637 map->reg_shift = config->pad_bits % 8;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600638 if (config->reg_stride)
639 map->reg_stride = config->reg_stride;
640 else
641 map->reg_stride = 1;
Xiubo Lica747be2016-01-04 18:00:33 +0800642 if (is_power_of_2(map->reg_stride))
643 map->reg_stride_order = ilog2(map->reg_stride);
644 else
645 map->reg_stride_order = -1;
Markus Pargmann67921a12015-08-21 10:26:42 +0200646 map->use_single_read = config->use_single_rw || !bus || !bus->read;
647 map->use_single_write = config->use_single_rw || !bus || !bus->write;
Markus Pargmann9c9f7f62015-08-21 10:26:43 +0200648 map->can_multi_write = config->can_multi_write && bus && bus->write;
Sergey SENOZHATSKY17649c92015-08-31 18:54:58 +0900649 if (bus) {
650 map->max_raw_read = bus->max_raw_read;
651 map->max_raw_write = bus->max_raw_write;
652 }
Mark Brownb83a3132011-05-11 19:59:58 +0200653 map->dev = dev;
654 map->bus = bus;
Stephen Warren0135bbc2012-04-04 15:48:30 -0600655 map->bus_context = bus_context;
Mark Brown2e2ae662011-07-20 22:33:39 +0100656 map->max_register = config->max_register;
Davide Ciminaghi76aad392012-11-20 15:20:30 +0100657 map->wr_table = config->wr_table;
658 map->rd_table = config->rd_table;
659 map->volatile_table = config->volatile_table;
660 map->precious_table = config->precious_table;
Mark Brown2e2ae662011-07-20 22:33:39 +0100661 map->writeable_reg = config->writeable_reg;
662 map->readable_reg = config->readable_reg;
663 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900664 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100665 map->cache_type = config->cache_type;
Mark Brown72b39f62012-05-08 17:44:40 +0100666 map->name = config->name;
Mark Brownb83a3132011-05-11 19:59:58 +0200667
Mark Brown0d509f22013-01-27 22:07:38 +0800668 spin_lock_init(&map->async_lock);
669 INIT_LIST_HEAD(&map->async_list);
Mark Brown7e09a972013-10-07 23:00:24 +0100670 INIT_LIST_HEAD(&map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +0800671 init_waitqueue_head(&map->async_waitq);
672
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200673 if (config->read_flag_mask || config->write_flag_mask) {
674 map->read_flag_mask = config->read_flag_mask;
675 map->write_flag_mask = config->write_flag_mask;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800676 } else if (bus) {
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200677 map->read_flag_mask = bus->read_flag_mask;
678 }
679
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800680 if (!bus) {
681 map->reg_read = config->reg_read;
682 map->reg_write = config->reg_write;
683
684 map->defer_caching = false;
685 goto skip_format_initialization;
Boris BREZILLON3ac17032014-04-17 11:40:11 +0200686 } else if (!bus->read || !bus->write) {
687 map->reg_read = _regmap_bus_reg_read;
688 map->reg_write = _regmap_bus_reg_write;
689
690 map->defer_caching = false;
691 goto skip_format_initialization;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800692 } else {
693 map->reg_read = _regmap_bus_read;
Jon Ringle77792b12015-10-01 12:38:07 -0400694 map->reg_update_bits = bus->reg_update_bits;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800695 }
Andrey Smirnovad278402013-01-12 12:54:12 -0800696
Geert Uytterhoevencf673fb2014-08-27 16:36:03 +0200697 reg_endian = regmap_get_reg_endian(bus, config);
698 val_endian = regmap_get_val_endian(dev, bus, config);
Stephen Warren141eba22012-05-24 10:47:26 -0600699
Marc Reillyd939fb92012-03-16 12:11:43 +1100700 switch (config->reg_bits + map->reg_shift) {
Wolfram Sang9aa50752012-01-27 16:10:22 +0100701 case 2:
702 switch (config->val_bits) {
703 case 6:
704 map->format.format_write = regmap_format_2_6_write;
705 break;
706 default:
707 goto err_map;
708 }
709 break;
710
Mark Brownb83a3132011-05-11 19:59:58 +0200711 case 4:
712 switch (config->val_bits) {
713 case 12:
714 map->format.format_write = regmap_format_4_12_write;
715 break;
716 default:
717 goto err_map;
718 }
719 break;
720
721 case 7:
722 switch (config->val_bits) {
723 case 9:
724 map->format.format_write = regmap_format_7_9_write;
725 break;
726 default:
727 goto err_map;
728 }
729 break;
730
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100731 case 10:
732 switch (config->val_bits) {
733 case 14:
734 map->format.format_write = regmap_format_10_14_write;
735 break;
736 default:
737 goto err_map;
738 }
739 break;
740
Mark Brownb83a3132011-05-11 19:59:58 +0200741 case 8:
742 map->format.format_reg = regmap_format_8;
743 break;
744
745 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600746 switch (reg_endian) {
747 case REGMAP_ENDIAN_BIG:
748 map->format.format_reg = regmap_format_16_be;
749 break;
750 case REGMAP_ENDIAN_NATIVE:
751 map->format.format_reg = regmap_format_16_native;
752 break;
753 default:
754 goto err_map;
755 }
Mark Brownb83a3132011-05-11 19:59:58 +0200756 break;
757
Lars-Peter Clausen237019e2013-01-10 17:06:10 +0100758 case 24:
759 if (reg_endian != REGMAP_ENDIAN_BIG)
760 goto err_map;
761 map->format.format_reg = regmap_format_24;
762 break;
763
Mark Brown7d5e5252012-02-17 15:58:25 -0800764 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600765 switch (reg_endian) {
766 case REGMAP_ENDIAN_BIG:
767 map->format.format_reg = regmap_format_32_be;
768 break;
769 case REGMAP_ENDIAN_NATIVE:
770 map->format.format_reg = regmap_format_32_native;
771 break;
772 default:
773 goto err_map;
774 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800775 break;
776
Xiubo Liafcc00b2015-12-03 17:31:52 +0800777#ifdef CONFIG_64BIT
778 case 64:
779 switch (reg_endian) {
780 case REGMAP_ENDIAN_BIG:
781 map->format.format_reg = regmap_format_64_be;
782 break;
783 case REGMAP_ENDIAN_NATIVE:
784 map->format.format_reg = regmap_format_64_native;
785 break;
786 default:
787 goto err_map;
788 }
789 break;
790#endif
791
Mark Brownb83a3132011-05-11 19:59:58 +0200792 default:
793 goto err_map;
794 }
795
Mark Brown8a819ff2013-03-04 09:04:51 +0800796 if (val_endian == REGMAP_ENDIAN_NATIVE)
797 map->format.parse_inplace = regmap_parse_inplace_noop;
798
Mark Brownb83a3132011-05-11 19:59:58 +0200799 switch (config->val_bits) {
800 case 8:
801 map->format.format_val = regmap_format_8;
802 map->format.parse_val = regmap_parse_8;
Mark Brown8a819ff2013-03-04 09:04:51 +0800803 map->format.parse_inplace = regmap_parse_inplace_noop;
Mark Brownb83a3132011-05-11 19:59:58 +0200804 break;
805 case 16:
Stephen Warren141eba22012-05-24 10:47:26 -0600806 switch (val_endian) {
807 case REGMAP_ENDIAN_BIG:
808 map->format.format_val = regmap_format_16_be;
809 map->format.parse_val = regmap_parse_16_be;
Mark Brown8a819ff2013-03-04 09:04:51 +0800810 map->format.parse_inplace = regmap_parse_16_be_inplace;
Stephen Warren141eba22012-05-24 10:47:26 -0600811 break;
Xiubo Li4aa8c062014-04-02 18:09:07 +0800812 case REGMAP_ENDIAN_LITTLE:
813 map->format.format_val = regmap_format_16_le;
814 map->format.parse_val = regmap_parse_16_le;
815 map->format.parse_inplace = regmap_parse_16_le_inplace;
816 break;
Stephen Warren141eba22012-05-24 10:47:26 -0600817 case REGMAP_ENDIAN_NATIVE:
818 map->format.format_val = regmap_format_16_native;
819 map->format.parse_val = regmap_parse_16_native;
820 break;
821 default:
822 goto err_map;
823 }
Mark Brownb83a3132011-05-11 19:59:58 +0200824 break;
Marc Reillyea279fc2012-03-16 12:11:42 +1100825 case 24:
Stephen Warren141eba22012-05-24 10:47:26 -0600826 if (val_endian != REGMAP_ENDIAN_BIG)
827 goto err_map;
Marc Reillyea279fc2012-03-16 12:11:42 +1100828 map->format.format_val = regmap_format_24;
829 map->format.parse_val = regmap_parse_24;
830 break;
Mark Brown7d5e5252012-02-17 15:58:25 -0800831 case 32:
Stephen Warren141eba22012-05-24 10:47:26 -0600832 switch (val_endian) {
833 case REGMAP_ENDIAN_BIG:
834 map->format.format_val = regmap_format_32_be;
835 map->format.parse_val = regmap_parse_32_be;
Mark Brown8a819ff2013-03-04 09:04:51 +0800836 map->format.parse_inplace = regmap_parse_32_be_inplace;
Stephen Warren141eba22012-05-24 10:47:26 -0600837 break;
Xiubo Li4aa8c062014-04-02 18:09:07 +0800838 case REGMAP_ENDIAN_LITTLE:
839 map->format.format_val = regmap_format_32_le;
840 map->format.parse_val = regmap_parse_32_le;
841 map->format.parse_inplace = regmap_parse_32_le_inplace;
842 break;
Stephen Warren141eba22012-05-24 10:47:26 -0600843 case REGMAP_ENDIAN_NATIVE:
844 map->format.format_val = regmap_format_32_native;
845 map->format.parse_val = regmap_parse_32_native;
846 break;
847 default:
848 goto err_map;
849 }
Mark Brown7d5e5252012-02-17 15:58:25 -0800850 break;
Xiubo Liafcc00b2015-12-03 17:31:52 +0800851#ifdef CONFIG_64BIT
Dan Carpenter782035e2015-12-12 15:59:43 +0300852 case 64:
Xiubo Liafcc00b2015-12-03 17:31:52 +0800853 switch (val_endian) {
854 case REGMAP_ENDIAN_BIG:
855 map->format.format_val = regmap_format_64_be;
856 map->format.parse_val = regmap_parse_64_be;
857 map->format.parse_inplace = regmap_parse_64_be_inplace;
858 break;
859 case REGMAP_ENDIAN_LITTLE:
860 map->format.format_val = regmap_format_64_le;
861 map->format.parse_val = regmap_parse_64_le;
862 map->format.parse_inplace = regmap_parse_64_le_inplace;
863 break;
864 case REGMAP_ENDIAN_NATIVE:
865 map->format.format_val = regmap_format_64_native;
866 map->format.parse_val = regmap_parse_64_native;
867 break;
868 default:
869 goto err_map;
870 }
871 break;
872#endif
Mark Brownb83a3132011-05-11 19:59:58 +0200873 }
874
Stephen Warren141eba22012-05-24 10:47:26 -0600875 if (map->format.format_write) {
876 if ((reg_endian != REGMAP_ENDIAN_BIG) ||
877 (val_endian != REGMAP_ENDIAN_BIG))
878 goto err_map;
Markus Pargmann67921a12015-08-21 10:26:42 +0200879 map->use_single_write = true;
Stephen Warren141eba22012-05-24 10:47:26 -0600880 }
Mark Brown7a647612012-04-30 23:26:32 +0100881
Mark Brownb83a3132011-05-11 19:59:58 +0200882 if (!map->format.format_write &&
883 !(map->format.format_reg && map->format.format_val))
884 goto err_map;
885
Mark Brown82159ba2012-01-18 10:52:25 +0000886 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200887 if (map->work_buf == NULL) {
888 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700889 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200890 }
891
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800892 if (map->format.format_write) {
893 map->defer_caching = false;
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800894 map->reg_write = _regmap_bus_formatted_write;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800895 } else if (map->format.format_val) {
896 map->defer_caching = true;
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800897 map->reg_write = _regmap_bus_raw_write;
Andrey Smirnovd2a58842013-01-27 10:49:05 -0800898 }
899
900skip_format_initialization:
Andrey Smirnov07c320d2013-01-12 12:54:13 -0800901
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100902 map->range_tree = RB_ROOT;
Mark Browne3549cd2012-10-02 20:17:15 +0100903 for (i = 0; i < config->num_ranges; i++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100904 const struct regmap_range_cfg *range_cfg = &config->ranges[i];
905 struct regmap_range_node *new;
906
907 /* Sanity check */
Mark Brown061adc02012-10-03 12:17:51 +0100908 if (range_cfg->range_max < range_cfg->range_min) {
909 dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
910 range_cfg->range_max, range_cfg->range_min);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100911 goto err_range;
Mark Brown061adc02012-10-03 12:17:51 +0100912 }
913
914 if (range_cfg->range_max > map->max_register) {
915 dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
916 range_cfg->range_max, map->max_register);
917 goto err_range;
918 }
919
920 if (range_cfg->selector_reg > map->max_register) {
921 dev_err(map->dev,
922 "Invalid range %d: selector out of map\n", i);
923 goto err_range;
924 }
925
926 if (range_cfg->window_len == 0) {
927 dev_err(map->dev, "Invalid range %d: window_len 0\n",
928 i);
929 goto err_range;
930 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100931
932 /* Make sure, that this register range has no selector
933 or data window within its boundary */
Mark Browne3549cd2012-10-02 20:17:15 +0100934 for (j = 0; j < config->num_ranges; j++) {
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100935 unsigned sel_reg = config->ranges[j].selector_reg;
936 unsigned win_min = config->ranges[j].window_start;
937 unsigned win_max = win_min +
938 config->ranges[j].window_len - 1;
939
Philipp Zabelf161d222013-07-23 12:16:02 +0200940 /* Allow data window inside its own virtual range */
941 if (j == i)
942 continue;
943
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100944 if (range_cfg->range_min <= sel_reg &&
945 sel_reg <= range_cfg->range_max) {
Mark Brown061adc02012-10-03 12:17:51 +0100946 dev_err(map->dev,
947 "Range %d: selector for %d in window\n",
948 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100949 goto err_range;
950 }
951
952 if (!(win_max < range_cfg->range_min ||
953 win_min > range_cfg->range_max)) {
Mark Brown061adc02012-10-03 12:17:51 +0100954 dev_err(map->dev,
955 "Range %d: window for %d in window\n",
956 i, j);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100957 goto err_range;
958 }
959 }
960
961 new = kzalloc(sizeof(*new), GFP_KERNEL);
962 if (new == NULL) {
963 ret = -ENOMEM;
964 goto err_range;
965 }
966
Mark Brown4b020b32012-10-03 13:13:16 +0100967 new->map = map;
Mark Brownd058bb42012-10-03 12:40:47 +0100968 new->name = range_cfg->name;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100969 new->range_min = range_cfg->range_min;
970 new->range_max = range_cfg->range_max;
971 new->selector_reg = range_cfg->selector_reg;
972 new->selector_mask = range_cfg->selector_mask;
973 new->selector_shift = range_cfg->selector_shift;
974 new->window_start = range_cfg->window_start;
975 new->window_len = range_cfg->window_len;
976
Nenghua Cao53e87f82014-02-21 16:05:45 +0800977 if (!_regmap_range_add(map, new)) {
Mark Brown061adc02012-10-03 12:17:51 +0100978 dev_err(map->dev, "Failed to add range %d\n", i);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100979 kfree(new);
980 goto err_range;
981 }
982
983 if (map->selector_work_buf == NULL) {
984 map->selector_work_buf =
985 kzalloc(map->format.buf_size, GFP_KERNEL);
986 if (map->selector_work_buf == NULL) {
987 ret = -ENOMEM;
988 goto err_range;
989 }
990 }
991 }
Mark Brown052d2cd2011-11-21 19:05:13 +0000992
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100993 ret = regcache_init(map, config);
Mark Brown0ff3e622012-10-04 17:39:13 +0100994 if (ret != 0)
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100995 goto err_range;
996
Daeseok Youna7a037c2014-04-01 19:46:43 +0900997 if (dev) {
Michal Simek6cfec042014-02-10 16:22:33 +0100998 ret = regmap_attach_dev(dev, map, config);
999 if (ret != 0)
1000 goto err_regcache;
Daeseok Youna7a037c2014-04-01 19:46:43 +09001001 }
Mark Brown72b39f62012-05-08 17:44:40 +01001002
Mark Brownb83a3132011-05-11 19:59:58 +02001003 return map;
1004
Michal Simek6cfec042014-02-10 16:22:33 +01001005err_regcache:
Mark Brown72b39f62012-05-08 17:44:40 +01001006 regcache_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001007err_range:
1008 regmap_range_exit(map);
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +01001009 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +02001010err_map:
1011 kfree(map);
1012err:
1013 return ERR_PTR(ret);
1014}
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +08001015EXPORT_SYMBOL_GPL(__regmap_init);
Mark Brownb83a3132011-05-11 19:59:58 +02001016
Mark Brownc0eb4672012-01-30 19:56:52 +00001017static void devm_regmap_release(struct device *dev, void *res)
1018{
1019 regmap_exit(*(struct regmap **)res);
1020}
1021
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +08001022struct regmap *__devm_regmap_init(struct device *dev,
1023 const struct regmap_bus *bus,
1024 void *bus_context,
1025 const struct regmap_config *config,
1026 struct lock_class_key *lock_key,
1027 const char *lock_name)
Mark Brownc0eb4672012-01-30 19:56:52 +00001028{
1029 struct regmap **ptr, *regmap;
1030
1031 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
1032 if (!ptr)
1033 return ERR_PTR(-ENOMEM);
1034
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +08001035 regmap = __regmap_init(dev, bus, bus_context, config,
1036 lock_key, lock_name);
Mark Brownc0eb4672012-01-30 19:56:52 +00001037 if (!IS_ERR(regmap)) {
1038 *ptr = regmap;
1039 devres_add(dev, ptr);
1040 } else {
1041 devres_free(ptr);
1042 }
1043
1044 return regmap;
1045}
Nicolas Boichat3cfe7a72015-07-08 14:30:18 +08001046EXPORT_SYMBOL_GPL(__devm_regmap_init);
Mark Brownc0eb4672012-01-30 19:56:52 +00001047
Srinivas Kandagatla67252282013-06-11 13:18:15 +01001048static void regmap_field_init(struct regmap_field *rm_field,
1049 struct regmap *regmap, struct reg_field reg_field)
1050{
Srinivas Kandagatla67252282013-06-11 13:18:15 +01001051 rm_field->regmap = regmap;
1052 rm_field->reg = reg_field.reg;
1053 rm_field->shift = reg_field.lsb;
Maxime Coquelin921cc292015-06-16 13:53:19 +02001054 rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
Kuninori Morimotoa0102372013-09-01 20:30:50 -07001055 rm_field->id_size = reg_field.id_size;
1056 rm_field->id_offset = reg_field.id_offset;
Srinivas Kandagatla67252282013-06-11 13:18:15 +01001057}
1058
1059/**
1060 * devm_regmap_field_alloc(): Allocate and initialise a register field
1061 * in a register map.
1062 *
1063 * @dev: Device that will be interacted with
1064 * @regmap: regmap bank in which this register field is located.
1065 * @reg_field: Register field with in the bank.
1066 *
1067 * The return value will be an ERR_PTR() on error or a valid pointer
1068 * to a struct regmap_field. The regmap_field will be automatically freed
1069 * by the device management code.
1070 */
1071struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1072 struct regmap *regmap, struct reg_field reg_field)
1073{
1074 struct regmap_field *rm_field = devm_kzalloc(dev,
1075 sizeof(*rm_field), GFP_KERNEL);
1076 if (!rm_field)
1077 return ERR_PTR(-ENOMEM);
1078
1079 regmap_field_init(rm_field, regmap, reg_field);
1080
1081 return rm_field;
1082
1083}
1084EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
1085
1086/**
1087 * devm_regmap_field_free(): Free register field allocated using
1088 * devm_regmap_field_alloc. Usally drivers need not call this function,
1089 * as the memory allocated via devm will be freed as per device-driver
1090 * life-cyle.
1091 *
1092 * @dev: Device that will be interacted with
1093 * @field: regmap field which should be freed.
1094 */
1095void devm_regmap_field_free(struct device *dev,
1096 struct regmap_field *field)
1097{
1098 devm_kfree(dev, field);
1099}
1100EXPORT_SYMBOL_GPL(devm_regmap_field_free);
1101
1102/**
1103 * regmap_field_alloc(): Allocate and initialise a register field
1104 * in a register map.
1105 *
1106 * @regmap: regmap bank in which this register field is located.
1107 * @reg_field: Register field with in the bank.
1108 *
1109 * The return value will be an ERR_PTR() on error or a valid pointer
1110 * to a struct regmap_field. The regmap_field should be freed by the
1111 * user once its finished working with it using regmap_field_free().
1112 */
1113struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1114 struct reg_field reg_field)
1115{
1116 struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
1117
1118 if (!rm_field)
1119 return ERR_PTR(-ENOMEM);
1120
1121 regmap_field_init(rm_field, regmap, reg_field);
1122
1123 return rm_field;
1124}
1125EXPORT_SYMBOL_GPL(regmap_field_alloc);
1126
1127/**
1128 * regmap_field_free(): Free register field allocated using regmap_field_alloc
1129 *
1130 * @field: regmap field which should be freed.
1131 */
1132void regmap_field_free(struct regmap_field *field)
1133{
1134 kfree(field);
1135}
1136EXPORT_SYMBOL_GPL(regmap_field_free);
1137
Mark Brownb83a3132011-05-11 19:59:58 +02001138/**
Mark Brownbf315172011-12-03 17:06:20 +00001139 * regmap_reinit_cache(): Reinitialise the current register cache
1140 *
1141 * @map: Register map to operate on.
1142 * @config: New configuration. Only the cache data will be used.
1143 *
1144 * Discard any existing register cache for the map and initialize a
1145 * new cache. This can be used to restore the cache to defaults or to
1146 * update the cache configuration to reflect runtime discovery of the
1147 * hardware.
Dimitris Papastamos4d879512012-07-27 14:54:15 +01001148 *
1149 * No explicit locking is done here, the user needs to ensure that
1150 * this function will not race with other calls to regmap.
Mark Brownbf315172011-12-03 17:06:20 +00001151 */
1152int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1153{
Mark Brownbf315172011-12-03 17:06:20 +00001154 regcache_exit(map);
Mark Browna24f64a2012-01-26 18:30:16 +00001155 regmap_debugfs_exit(map);
Mark Brownbf315172011-12-03 17:06:20 +00001156
1157 map->max_register = config->max_register;
1158 map->writeable_reg = config->writeable_reg;
1159 map->readable_reg = config->readable_reg;
1160 map->volatile_reg = config->volatile_reg;
1161 map->precious_reg = config->precious_reg;
1162 map->cache_type = config->cache_type;
1163
Stephen Warrend3c242e2012-04-04 15:48:29 -06001164 regmap_debugfs_init(map, config->name);
Mark Browna24f64a2012-01-26 18:30:16 +00001165
Mark Brown421e8d22012-01-20 13:39:37 +00001166 map->cache_bypass = false;
1167 map->cache_only = false;
1168
Dimitris Papastamos4d879512012-07-27 14:54:15 +01001169 return regcache_init(map, config);
Mark Brownbf315172011-12-03 17:06:20 +00001170}
Mark Brown752a6a52012-05-14 10:00:12 +01001171EXPORT_SYMBOL_GPL(regmap_reinit_cache);
Mark Brownbf315172011-12-03 17:06:20 +00001172
1173/**
Mark Brownb83a3132011-05-11 19:59:58 +02001174 * regmap_exit(): Free a previously allocated register map
1175 */
1176void regmap_exit(struct regmap *map)
1177{
Mark Brown7e09a972013-10-07 23:00:24 +01001178 struct regmap_async *async;
1179
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001180 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +01001181 regmap_debugfs_exit(map);
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001182 regmap_range_exit(map);
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001183 if (map->bus && map->bus->free_context)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001184 map->bus->free_context(map->bus_context);
Mark Brownb83a3132011-05-11 19:59:58 +02001185 kfree(map->work_buf);
Mark Brown7e09a972013-10-07 23:00:24 +01001186 while (!list_empty(&map->async_free)) {
1187 async = list_first_entry_or_null(&map->async_free,
1188 struct regmap_async,
1189 list);
1190 list_del(&async->list);
1191 kfree(async->work_buf);
1192 kfree(async);
1193 }
Mark Brownb83a3132011-05-11 19:59:58 +02001194 kfree(map);
1195}
1196EXPORT_SYMBOL_GPL(regmap_exit);
1197
Mark Brown72b39f62012-05-08 17:44:40 +01001198static int dev_get_regmap_match(struct device *dev, void *res, void *data)
1199{
1200 struct regmap **r = res;
1201 if (!r || !*r) {
1202 WARN_ON(!r || !*r);
1203 return 0;
1204 }
1205
1206 /* If the user didn't specify a name match any */
1207 if (data)
1208 return (*r)->name == data;
1209 else
1210 return 1;
1211}
1212
1213/**
1214 * dev_get_regmap(): Obtain the regmap (if any) for a device
1215 *
1216 * @dev: Device to retrieve the map for
1217 * @name: Optional name for the register map, usually NULL.
1218 *
1219 * Returns the regmap for the device if one is present, or NULL. If
1220 * name is specified then it must match the name specified when
1221 * registering the device, if it is NULL then the first regmap found
1222 * will be used. Devices with multiple register maps are very rare,
1223 * generic code should normally not need to specify a name.
1224 */
1225struct regmap *dev_get_regmap(struct device *dev, const char *name)
1226{
1227 struct regmap **r = devres_find(dev, dev_get_regmap_release,
1228 dev_get_regmap_match, (void *)name);
1229
1230 if (!r)
1231 return NULL;
1232 return *r;
1233}
1234EXPORT_SYMBOL_GPL(dev_get_regmap);
1235
Tuomas Tynkkynen8d7d3972014-07-21 18:38:47 +03001236/**
1237 * regmap_get_device(): Obtain the device from a regmap
1238 *
1239 * @map: Register map to operate on.
1240 *
1241 * Returns the underlying device that the regmap has been created for.
1242 */
1243struct device *regmap_get_device(struct regmap *map)
1244{
1245 return map->dev;
1246}
Mark Brownfa2fbe42014-07-25 18:30:31 +01001247EXPORT_SYMBOL_GPL(regmap_get_device);
Tuomas Tynkkynen8d7d3972014-07-21 18:38:47 +03001248
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001249static int _regmap_select_page(struct regmap *map, unsigned int *reg,
Mark Brown98bc7df2012-10-04 17:31:11 +01001250 struct regmap_range_node *range,
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001251 unsigned int val_num)
1252{
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001253 void *orig_work_buf;
1254 unsigned int win_offset;
1255 unsigned int win_page;
1256 bool page_chg;
1257 int ret;
1258
Mark Brown98bc7df2012-10-04 17:31:11 +01001259 win_offset = (*reg - range->range_min) % range->window_len;
1260 win_page = (*reg - range->range_min) / range->window_len;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001261
Mark Brown98bc7df2012-10-04 17:31:11 +01001262 if (val_num > 1) {
1263 /* Bulk write shouldn't cross range boundary */
1264 if (*reg + val_num - 1 > range->range_max)
1265 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001266
Mark Brown98bc7df2012-10-04 17:31:11 +01001267 /* ... or single page boundary */
1268 if (val_num > range->window_len - win_offset)
1269 return -EINVAL;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001270 }
1271
Mark Brown98bc7df2012-10-04 17:31:11 +01001272 /* It is possible to have selector register inside data window.
1273 In that case, selector register is located on every page and
1274 it needs no page switching, when accessed alone. */
1275 if (val_num > 1 ||
1276 range->window_start + win_offset != range->selector_reg) {
1277 /* Use separate work_buf during page switching */
1278 orig_work_buf = map->work_buf;
1279 map->work_buf = map->selector_work_buf;
1280
1281 ret = _regmap_update_bits(map, range->selector_reg,
1282 range->selector_mask,
1283 win_page << range->selector_shift,
Kuninori Morimoto7ff05892015-06-16 08:52:22 +00001284 &page_chg, false);
Mark Brown98bc7df2012-10-04 17:31:11 +01001285
1286 map->work_buf = orig_work_buf;
1287
Mark Brown0ff3e622012-10-04 17:39:13 +01001288 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001289 return ret;
1290 }
1291
1292 *reg = range->window_start + win_offset;
1293
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001294 return 0;
1295}
1296
Mark Brown584de322013-03-13 19:19:34 +00001297int _regmap_raw_write(struct regmap *map, unsigned int reg,
Mark Brown0a819802013-10-09 12:28:52 +01001298 const void *val, size_t val_len)
Mark Brownb83a3132011-05-11 19:59:58 +02001299{
Mark Brown98bc7df2012-10-04 17:31:11 +01001300 struct regmap_range_node *range;
Mark Brown0d509f22013-01-27 22:07:38 +08001301 unsigned long flags;
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001302 u8 *u8 = map->work_buf;
Mark Brown0d509f22013-01-27 22:07:38 +08001303 void *work_val = map->work_buf + map->format.reg_bytes +
1304 map->format.pad_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +02001305 void *buf;
1306 int ret = -ENOTSUPP;
1307 size_t len;
Mark Brown73304782011-07-24 11:46:20 +01001308 int i;
1309
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001310 WARN_ON(!map->bus);
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001311
Mark Brown73304782011-07-24 11:46:20 +01001312 /* Check for unwritable registers before we start */
1313 if (map->writeable_reg)
1314 for (i = 0; i < val_len / map->format.val_bytes; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -06001315 if (!map->writeable_reg(map->dev,
Xiubo Lica747be2016-01-04 18:00:33 +08001316 reg + regmap_get_offset(map, i)))
Mark Brown73304782011-07-24 11:46:20 +01001317 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02001318
Laxman Dewanganc9157192012-02-10 21:30:27 +05301319 if (!map->cache_bypass && map->format.parse_val) {
1320 unsigned int ival;
1321 int val_bytes = map->format.val_bytes;
1322 for (i = 0; i < val_len / val_bytes; i++) {
Stephen Warren5a08d152013-03-20 17:02:02 -06001323 ival = map->format.parse_val(val + (i * val_bytes));
Xiubo Lica747be2016-01-04 18:00:33 +08001324 ret = regcache_write(map,
1325 reg + regmap_get_offset(map, i),
Stephen Warrenf01ee602012-04-09 13:40:24 -06001326 ival);
Laxman Dewanganc9157192012-02-10 21:30:27 +05301327 if (ret) {
1328 dev_err(map->dev,
Mark Brown6d04b8a2012-10-26 19:05:32 +01001329 "Error in caching of register: %x ret: %d\n",
Laxman Dewanganc9157192012-02-10 21:30:27 +05301330 reg + i, ret);
1331 return ret;
1332 }
1333 }
1334 if (map->cache_only) {
1335 map->cache_dirty = true;
1336 return 0;
1337 }
1338 }
1339
Mark Brown98bc7df2012-10-04 17:31:11 +01001340 range = _regmap_range_lookup(map, reg);
1341 if (range) {
Mark Brown8a2ceac2012-10-04 18:20:18 +01001342 int val_num = val_len / map->format.val_bytes;
1343 int win_offset = (reg - range->range_min) % range->window_len;
1344 int win_residue = range->window_len - win_offset;
1345
1346 /* If the write goes beyond the end of the window split it */
1347 while (val_num > win_residue) {
Fabio Estevam1a61cfe2012-10-25 14:07:18 -02001348 dev_dbg(map->dev, "Writing window %d/%zu\n",
Mark Brown8a2ceac2012-10-04 18:20:18 +01001349 win_residue, val_len / map->format.val_bytes);
1350 ret = _regmap_raw_write(map, reg, val, win_residue *
Mark Brown0a819802013-10-09 12:28:52 +01001351 map->format.val_bytes);
Mark Brown8a2ceac2012-10-04 18:20:18 +01001352 if (ret != 0)
1353 return ret;
1354
1355 reg += win_residue;
1356 val_num -= win_residue;
1357 val += win_residue * map->format.val_bytes;
1358 val_len -= win_residue * map->format.val_bytes;
1359
1360 win_offset = (reg - range->range_min) %
1361 range->window_len;
1362 win_residue = range->window_len - win_offset;
1363 }
1364
1365 ret = _regmap_select_page(map, &reg, range, val_num);
Mark Brown0ff3e622012-10-04 17:39:13 +01001366 if (ret != 0)
Mark Brown98bc7df2012-10-04 17:31:11 +01001367 return ret;
1368 }
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01001369
Marc Reillyd939fb92012-03-16 12:11:43 +11001370 map->format.format_reg(map->work_buf, reg, map->reg_shift);
Mark Brownb83a3132011-05-11 19:59:58 +02001371
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02001372 u8[0] |= map->write_flag_mask;
1373
Mark Brown651e0132013-10-08 18:37:36 +01001374 /*
1375 * Essentially all I/O mechanisms will be faster with a single
1376 * buffer to write. Since register syncs often generate raw
1377 * writes of single registers optimise that case.
1378 */
1379 if (val != work_val && val_len == map->format.val_bytes) {
1380 memcpy(work_val, val, map->format.val_bytes);
1381 val = work_val;
1382 }
1383
Mark Brown0a819802013-10-09 12:28:52 +01001384 if (map->async && map->bus->async_write) {
Mark Brown7e09a972013-10-07 23:00:24 +01001385 struct regmap_async *async;
Mark Brown0d509f22013-01-27 22:07:38 +08001386
Philipp Zabelc6b570d2015-03-09 12:20:13 +01001387 trace_regmap_async_write_start(map, reg, val_len);
Mark Brownfe7d4cc2013-02-21 19:05:48 +00001388
Mark Brown7e09a972013-10-07 23:00:24 +01001389 spin_lock_irqsave(&map->async_lock, flags);
1390 async = list_first_entry_or_null(&map->async_free,
1391 struct regmap_async,
1392 list);
1393 if (async)
1394 list_del(&async->list);
1395 spin_unlock_irqrestore(&map->async_lock, flags);
1396
1397 if (!async) {
1398 async = map->bus->async_alloc();
1399 if (!async)
1400 return -ENOMEM;
1401
1402 async->work_buf = kzalloc(map->format.buf_size,
1403 GFP_KERNEL | GFP_DMA);
1404 if (!async->work_buf) {
1405 kfree(async);
1406 return -ENOMEM;
1407 }
Mark Brown0d509f22013-01-27 22:07:38 +08001408 }
1409
Mark Brown0d509f22013-01-27 22:07:38 +08001410 async->map = map;
1411
1412 /* If the caller supplied the value we can use it safely. */
1413 memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
1414 map->format.reg_bytes + map->format.val_bytes);
Mark Brown0d509f22013-01-27 22:07:38 +08001415
1416 spin_lock_irqsave(&map->async_lock, flags);
1417 list_add_tail(&async->list, &map->async_list);
1418 spin_unlock_irqrestore(&map->async_lock, flags);
1419
Mark Brown04c50cc2013-10-10 22:38:29 +01001420 if (val != work_val)
1421 ret = map->bus->async_write(map->bus_context,
1422 async->work_buf,
1423 map->format.reg_bytes +
1424 map->format.pad_bytes,
1425 val, val_len, async);
1426 else
1427 ret = map->bus->async_write(map->bus_context,
1428 async->work_buf,
1429 map->format.reg_bytes +
1430 map->format.pad_bytes +
1431 val_len, NULL, 0, async);
Mark Brown0d509f22013-01-27 22:07:38 +08001432
1433 if (ret != 0) {
1434 dev_err(map->dev, "Failed to schedule write: %d\n",
1435 ret);
1436
1437 spin_lock_irqsave(&map->async_lock, flags);
Mark Brown7e09a972013-10-07 23:00:24 +01001438 list_move(&async->list, &map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +08001439 spin_unlock_irqrestore(&map->async_lock, flags);
Mark Brown0d509f22013-01-27 22:07:38 +08001440 }
Mark Brownf951b652013-03-27 13:08:44 +00001441
1442 return ret;
Mark Brown0d509f22013-01-27 22:07:38 +08001443 }
1444
Philipp Zabelc6b570d2015-03-09 12:20:13 +01001445 trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +01001446
Mark Brown2547e202011-07-20 21:47:22 +01001447 /* If we're doing a single register write we can probably just
1448 * send the work_buf directly, otherwise try to do a gather
1449 * write.
1450 */
Mark Brown0d509f22013-01-27 22:07:38 +08001451 if (val == work_val)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001452 ret = map->bus->write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001453 map->format.reg_bytes +
1454 map->format.pad_bytes +
1455 val_len);
Mark Brown2547e202011-07-20 21:47:22 +01001456 else if (map->bus->gather_write)
Stephen Warren0135bbc2012-04-04 15:48:30 -06001457 ret = map->bus->gather_write(map->bus_context, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +00001458 map->format.reg_bytes +
1459 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +02001460 val, val_len);
1461
Mark Brown2547e202011-07-20 21:47:22 +01001462 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +02001463 if (ret == -ENOTSUPP) {
Mark Brown82159ba2012-01-18 10:52:25 +00001464 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
1465 buf = kzalloc(len, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +02001466 if (!buf)
1467 return -ENOMEM;
1468
1469 memcpy(buf, map->work_buf, map->format.reg_bytes);
Mark Brown82159ba2012-01-18 10:52:25 +00001470 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
1471 val, val_len);
Stephen Warren0135bbc2012-04-04 15:48:30 -06001472 ret = map->bus->write(map->bus_context, buf, len);
Mark Brownb83a3132011-05-11 19:59:58 +02001473
1474 kfree(buf);
1475 }
1476
Philipp Zabelc6b570d2015-03-09 12:20:13 +01001477 trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +01001478
Mark Brownb83a3132011-05-11 19:59:58 +02001479 return ret;
1480}
1481
Mark Brown221ad7f2013-03-26 21:24:20 +00001482/**
1483 * regmap_can_raw_write - Test if regmap_raw_write() is supported
1484 *
1485 * @map: Map to check.
1486 */
1487bool regmap_can_raw_write(struct regmap *map)
1488{
Markus Pargmann07ea4002015-08-12 12:12:33 +02001489 return map->bus && map->bus->write && map->format.format_val &&
1490 map->format.format_reg;
Mark Brown221ad7f2013-03-26 21:24:20 +00001491}
1492EXPORT_SYMBOL_GPL(regmap_can_raw_write);
1493
Markus Pargmannf50c9eb2015-08-30 09:33:54 +02001494/**
1495 * regmap_get_raw_read_max - Get the maximum size we can read
1496 *
1497 * @map: Map to check.
1498 */
1499size_t regmap_get_raw_read_max(struct regmap *map)
1500{
1501 return map->max_raw_read;
1502}
1503EXPORT_SYMBOL_GPL(regmap_get_raw_read_max);
1504
1505/**
1506 * regmap_get_raw_write_max - Get the maximum size we can read
1507 *
1508 * @map: Map to check.
1509 */
1510size_t regmap_get_raw_write_max(struct regmap *map)
1511{
1512 return map->max_raw_write;
1513}
1514EXPORT_SYMBOL_GPL(regmap_get_raw_write_max);
1515
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001516static int _regmap_bus_formatted_write(void *context, unsigned int reg,
1517 unsigned int val)
1518{
1519 int ret;
1520 struct regmap_range_node *range;
1521 struct regmap *map = context;
1522
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001523 WARN_ON(!map->bus || !map->format.format_write);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001524
1525 range = _regmap_range_lookup(map, reg);
1526 if (range) {
1527 ret = _regmap_select_page(map, &reg, range, 1);
1528 if (ret != 0)
1529 return ret;
1530 }
1531
1532 map->format.format_write(map, reg, val);
1533
Philipp Zabelc6b570d2015-03-09 12:20:13 +01001534 trace_regmap_hw_write_start(map, reg, 1);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001535
1536 ret = map->bus->write(map->bus_context, map->work_buf,
1537 map->format.buf_size);
1538
Philipp Zabelc6b570d2015-03-09 12:20:13 +01001539 trace_regmap_hw_write_done(map, reg, 1);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001540
1541 return ret;
1542}
1543
Boris BREZILLON3ac17032014-04-17 11:40:11 +02001544static int _regmap_bus_reg_write(void *context, unsigned int reg,
1545 unsigned int val)
1546{
1547 struct regmap *map = context;
1548
1549 return map->bus->reg_write(map->bus_context, reg, val);
1550}
1551
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001552static int _regmap_bus_raw_write(void *context, unsigned int reg,
1553 unsigned int val)
1554{
1555 struct regmap *map = context;
1556
Mark Brownf1b5c5c2013-03-13 19:18:13 +00001557 WARN_ON(!map->bus || !map->format.format_val);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001558
1559 map->format.format_val(map->work_buf + map->format.reg_bytes
1560 + map->format.pad_bytes, val, 0);
1561 return _regmap_raw_write(map, reg,
1562 map->work_buf +
1563 map->format.reg_bytes +
1564 map->format.pad_bytes,
Mark Brown0a819802013-10-09 12:28:52 +01001565 map->format.val_bytes);
Andrey Smirnov07c320d2013-01-12 12:54:13 -08001566}
1567
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001568static inline void *_regmap_map_get_context(struct regmap *map)
1569{
1570 return (map->bus) ? map : map->bus_context;
1571}
1572
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +01001573int _regmap_write(struct regmap *map, unsigned int reg,
1574 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +02001575{
Mark Brownfb2736b2011-07-24 21:30:55 +01001576 int ret;
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001577 void *context = _regmap_map_get_context(map);
Mark Brownb83a3132011-05-11 19:59:58 +02001578
Ionut Nicu515f2262013-08-09 12:09:20 +02001579 if (!regmap_writeable(map, reg))
1580 return -EIO;
1581
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001582 if (!map->cache_bypass && !map->defer_caching) {
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001583 ret = regcache_write(map, reg, val);
1584 if (ret != 0)
1585 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +02001586 if (map->cache_only) {
1587 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001588 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +02001589 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01001590 }
1591
Mark Brown1044c182012-07-06 14:10:23 +01001592#ifdef LOG_DEVICE
Pankaj Dubey5336be82014-09-27 09:47:55 +05301593 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
Mark Brown1044c182012-07-06 14:10:23 +01001594 dev_info(map->dev, "%x <= %x\n", reg, val);
1595#endif
1596
Philipp Zabelc6b570d2015-03-09 12:20:13 +01001597 trace_regmap_reg_write(map, reg, val);
Mark Brownfb2736b2011-07-24 21:30:55 +01001598
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001599 return map->reg_write(context, reg, val);
Mark Brownb83a3132011-05-11 19:59:58 +02001600}
1601
1602/**
1603 * regmap_write(): Write a value to a single register
1604 *
1605 * @map: Register map to write to
1606 * @reg: Register to write to
1607 * @val: Value to be written
1608 *
1609 * A value of zero will be returned on success, a negative errno will
1610 * be returned in error cases.
1611 */
1612int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1613{
1614 int ret;
1615
Xiubo Lifcac0232015-12-16 17:45:32 +08001616 if (!IS_ALIGNED(reg, map->reg_stride))
Stephen Warrenf01ee602012-04-09 13:40:24 -06001617 return -EINVAL;
1618
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001619 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001620
1621 ret = _regmap_write(map, reg, val);
1622
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001623 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001624
1625 return ret;
1626}
1627EXPORT_SYMBOL_GPL(regmap_write);
1628
1629/**
Mark Brown915f4412013-10-09 13:30:10 +01001630 * regmap_write_async(): Write a value to a single register asynchronously
1631 *
1632 * @map: Register map to write to
1633 * @reg: Register to write to
1634 * @val: Value to be written
1635 *
1636 * A value of zero will be returned on success, a negative errno will
1637 * be returned in error cases.
1638 */
1639int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1640{
1641 int ret;
1642
Xiubo Lifcac0232015-12-16 17:45:32 +08001643 if (!IS_ALIGNED(reg, map->reg_stride))
Mark Brown915f4412013-10-09 13:30:10 +01001644 return -EINVAL;
1645
1646 map->lock(map->lock_arg);
1647
1648 map->async = true;
1649
1650 ret = _regmap_write(map, reg, val);
1651
1652 map->async = false;
1653
1654 map->unlock(map->lock_arg);
1655
1656 return ret;
1657}
1658EXPORT_SYMBOL_GPL(regmap_write_async);
1659
1660/**
Mark Brownb83a3132011-05-11 19:59:58 +02001661 * regmap_raw_write(): Write raw values to one or more registers
1662 *
1663 * @map: Register map to write to
1664 * @reg: Initial register to write to
1665 * @val: Block of data to be written, laid out for direct transmission to the
1666 * device
1667 * @val_len: Length of data pointed to by val.
1668 *
1669 * This function is intended to be used for things like firmware
1670 * download where a large block of data needs to be transferred to the
1671 * device. No formatting will be done on the data provided.
1672 *
1673 * A value of zero will be returned on success, a negative errno will
1674 * be returned in error cases.
1675 */
1676int regmap_raw_write(struct regmap *map, unsigned int reg,
1677 const void *val, size_t val_len)
1678{
1679 int ret;
1680
Mark Brown221ad7f2013-03-26 21:24:20 +00001681 if (!regmap_can_raw_write(map))
Andrey Smirnovd2a58842013-01-27 10:49:05 -08001682 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06001683 if (val_len % map->format.val_bytes)
1684 return -EINVAL;
Markus Pargmannc3359312015-08-30 09:33:55 +02001685 if (map->max_raw_write && map->max_raw_write > val_len)
1686 return -E2BIG;
Stephen Warren851960b2012-04-06 15:16:03 -06001687
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001688 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001689
Mark Brown0a819802013-10-09 12:28:52 +01001690 ret = _regmap_raw_write(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02001691
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02001692 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02001693
1694 return ret;
1695}
1696EXPORT_SYMBOL_GPL(regmap_raw_write);
1697
Srinivas Kandagatla67252282013-06-11 13:18:15 +01001698/**
1699 * regmap_field_write(): Write a value to a single register field
1700 *
1701 * @field: Register field to write to
1702 * @val: Value to be written
1703 *
1704 * A value of zero will be returned on success, a negative errno will
1705 * be returned in error cases.
1706 */
1707int regmap_field_write(struct regmap_field *field, unsigned int val)
1708{
1709 return regmap_update_bits(field->regmap, field->reg,
1710 field->mask, val << field->shift);
1711}
1712EXPORT_SYMBOL_GPL(regmap_field_write);
1713
Kuninori Morimotofdf20022013-09-01 20:24:50 -07001714/**
1715 * regmap_field_update_bits(): Perform a read/modify/write cycle
1716 * on the register field
1717 *
1718 * @field: Register field to write to
1719 * @mask: Bitmask to change
1720 * @val: Value to be written
1721 *
1722 * A value of zero will be returned on success, a negative errno will
1723 * be returned in error cases.
1724 */
1725int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1726{
1727 mask = (mask << field->shift) & field->mask;
1728
1729 return regmap_update_bits(field->regmap, field->reg,
1730 mask, val << field->shift);
1731}
1732EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1733
Kuninori Morimotoa0102372013-09-01 20:30:50 -07001734/**
1735 * regmap_fields_write(): Write a value to a single register field with port ID
1736 *
1737 * @field: Register field to write to
1738 * @id: port ID
1739 * @val: Value to be written
1740 *
1741 * A value of zero will be returned on success, a negative errno will
1742 * be returned in error cases.
1743 */
1744int regmap_fields_write(struct regmap_field *field, unsigned int id,
1745 unsigned int val)
1746{
1747 if (id >= field->id_size)
1748 return -EINVAL;
1749
1750 return regmap_update_bits(field->regmap,
1751 field->reg + (field->id_offset * id),
1752 field->mask, val << field->shift);
1753}
1754EXPORT_SYMBOL_GPL(regmap_fields_write);
1755
Kuninori Morimotoe874e6c2015-06-16 08:52:55 +00001756int regmap_fields_force_write(struct regmap_field *field, unsigned int id,
1757 unsigned int val)
1758{
1759 if (id >= field->id_size)
1760 return -EINVAL;
1761
1762 return regmap_write_bits(field->regmap,
1763 field->reg + (field->id_offset * id),
1764 field->mask, val << field->shift);
1765}
1766EXPORT_SYMBOL_GPL(regmap_fields_force_write);
1767
Kuninori Morimotoa0102372013-09-01 20:30:50 -07001768/**
1769 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1770 * on the register field
1771 *
1772 * @field: Register field to write to
1773 * @id: port ID
1774 * @mask: Bitmask to change
1775 * @val: Value to be written
1776 *
1777 * A value of zero will be returned on success, a negative errno will
1778 * be returned in error cases.
1779 */
1780int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1781 unsigned int mask, unsigned int val)
1782{
1783 if (id >= field->id_size)
1784 return -EINVAL;
1785
1786 mask = (mask << field->shift) & field->mask;
1787
1788 return regmap_update_bits(field->regmap,
1789 field->reg + (field->id_offset * id),
1790 mask, val << field->shift);
1791}
1792EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1793
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301794/*
1795 * regmap_bulk_write(): Write multiple registers to the device
1796 *
1797 * @map: Register map to write to
1798 * @reg: First register to be write from
1799 * @val: Block of data to be written, in native register size for device
1800 * @val_count: Number of registers to write
1801 *
1802 * This function is intended to be used for writing a large block of
Nestor Ovroy31b35e9e2013-01-18 16:51:03 +01001803 * data to the device either in single transfer or multiple transfer.
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301804 *
1805 * A value of zero will be returned on success, a negative errno will
1806 * be returned in error cases.
1807 */
1808int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1809 size_t val_count)
1810{
1811 int ret = 0, i;
1812 size_t val_bytes = map->format.val_bytes;
Markus Pargmannadaac452015-08-30 09:33:53 +02001813 size_t total_size = val_bytes * val_count;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301814
Stephen Boydf4298362013-12-26 13:52:04 -08001815 if (map->bus && !map->format.parse_inplace)
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301816 return -EINVAL;
Xiubo Lifcac0232015-12-16 17:45:32 +08001817 if (!IS_ALIGNED(reg, map->reg_stride))
Stephen Warrenf01ee602012-04-09 13:40:24 -06001818 return -EINVAL;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301819
Stephen Boydf4298362013-12-26 13:52:04 -08001820 /*
1821 * Some devices don't support bulk write, for
Markus Pargmannc594b7f2015-08-21 10:26:41 +02001822 * them we have a series of single write operations in the first two if
1823 * blocks.
1824 *
1825 * The first if block is used for memory mapped io. It does not allow
1826 * val_bytes of 3 for example.
1827 * The second one is used for busses which do not have this limitation
1828 * and can write arbitrary value lengths.
Stephen Boydf4298362013-12-26 13:52:04 -08001829 */
Markus Pargmannc594b7f2015-08-21 10:26:41 +02001830 if (!map->bus) {
Takashi Iwai4999e962014-03-18 12:58:33 +01001831 map->lock(map->lock_arg);
Stephen Boydf4298362013-12-26 13:52:04 -08001832 for (i = 0; i < val_count; i++) {
1833 unsigned int ival;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301834
Stephen Boydf4298362013-12-26 13:52:04 -08001835 switch (val_bytes) {
1836 case 1:
1837 ival = *(u8 *)(val + (i * val_bytes));
1838 break;
1839 case 2:
1840 ival = *(u16 *)(val + (i * val_bytes));
1841 break;
1842 case 4:
1843 ival = *(u32 *)(val + (i * val_bytes));
1844 break;
1845#ifdef CONFIG_64BIT
1846 case 8:
1847 ival = *(u64 *)(val + (i * val_bytes));
1848 break;
1849#endif
1850 default:
1851 ret = -EINVAL;
1852 goto out;
1853 }
1854
Xiubo Lica747be2016-01-04 18:00:33 +08001855 ret = _regmap_write(map,
1856 reg + regmap_get_offset(map, i),
1857 ival);
Stephen Boydf4298362013-12-26 13:52:04 -08001858 if (ret != 0)
1859 goto out;
1860 }
Takashi Iwai4999e962014-03-18 12:58:33 +01001861out:
1862 map->unlock(map->lock_arg);
Markus Pargmannadaac452015-08-30 09:33:53 +02001863 } else if (map->use_single_write ||
1864 (map->max_raw_write && map->max_raw_write < total_size)) {
1865 int chunk_stride = map->reg_stride;
1866 size_t chunk_size = val_bytes;
1867 size_t chunk_count = val_count;
1868
1869 if (!map->use_single_write) {
1870 chunk_size = map->max_raw_write;
1871 if (chunk_size % val_bytes)
1872 chunk_size -= chunk_size % val_bytes;
1873 chunk_count = total_size / chunk_size;
1874 chunk_stride *= chunk_size / val_bytes;
1875 }
1876
Markus Pargmannc594b7f2015-08-21 10:26:41 +02001877 map->lock(map->lock_arg);
Markus Pargmannadaac452015-08-30 09:33:53 +02001878 /* Write as many bytes as possible with chunk_size */
1879 for (i = 0; i < chunk_count; i++) {
Markus Pargmannc594b7f2015-08-21 10:26:41 +02001880 ret = _regmap_raw_write(map,
Markus Pargmannadaac452015-08-30 09:33:53 +02001881 reg + (i * chunk_stride),
1882 val + (i * chunk_size),
1883 chunk_size);
Markus Pargmannc594b7f2015-08-21 10:26:41 +02001884 if (ret)
1885 break;
1886 }
Markus Pargmannadaac452015-08-30 09:33:53 +02001887
1888 /* Write remaining bytes */
1889 if (!ret && chunk_size * i < total_size) {
1890 ret = _regmap_raw_write(map, reg + (i * chunk_stride),
1891 val + (i * chunk_size),
1892 total_size - i * chunk_size);
1893 }
Markus Pargmannc594b7f2015-08-21 10:26:41 +02001894 map->unlock(map->lock_arg);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301895 } else {
Stephen Boydf4298362013-12-26 13:52:04 -08001896 void *wval;
1897
Xiubo Lid6b41cb2014-09-28 17:09:54 +08001898 if (!val_count)
1899 return -EINVAL;
1900
Stephen Boydb4a21fc2015-09-11 16:37:05 -07001901 wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301902 if (!wval) {
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301903 dev_err(map->dev, "Error in memory allocation\n");
Takashi Iwai4999e962014-03-18 12:58:33 +01001904 return -ENOMEM;
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301905 }
1906 for (i = 0; i < val_count * val_bytes; i += val_bytes)
Mark Brown8a819ff2013-03-04 09:04:51 +08001907 map->format.parse_inplace(wval + i);
Stephen Boydf4298362013-12-26 13:52:04 -08001908
Takashi Iwai4999e962014-03-18 12:58:33 +01001909 map->lock(map->lock_arg);
Mark Brown0a819802013-10-09 12:28:52 +01001910 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
Takashi Iwai4999e962014-03-18 12:58:33 +01001911 map->unlock(map->lock_arg);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301912
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301913 kfree(wval);
Stephen Boydf4298362013-12-26 13:52:04 -08001914 }
Laxman Dewangan8eaeb212012-02-12 19:49:43 +05301915 return ret;
1916}
1917EXPORT_SYMBOL_GPL(regmap_bulk_write);
1918
Anthony Oleche33fabd2013-10-11 15:31:11 +01001919/*
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001920 * _regmap_raw_multi_reg_write()
1921 *
1922 * the (register,newvalue) pairs in regs have not been formatted, but
1923 * they are all in the same page and have been changed to being page
Xiubo Lib486afb2015-08-12 15:02:19 +08001924 * relative. The page register has been written if that was necessary.
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001925 */
1926static int _regmap_raw_multi_reg_write(struct regmap *map,
Nariman Poushin8019ff62015-07-16 16:36:21 +01001927 const struct reg_sequence *regs,
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001928 size_t num_regs)
Charles Keepax1d5b40b2014-02-25 13:45:50 +00001929{
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001930 int ret;
1931 void *buf;
1932 int i;
1933 u8 *u8;
1934 size_t val_bytes = map->format.val_bytes;
1935 size_t reg_bytes = map->format.reg_bytes;
1936 size_t pad_bytes = map->format.pad_bytes;
1937 size_t pair_size = reg_bytes + pad_bytes + val_bytes;
1938 size_t len = pair_size * num_regs;
1939
Xiubo Lif5727cd2014-04-30 17:31:08 +08001940 if (!len)
1941 return -EINVAL;
1942
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001943 buf = kzalloc(len, GFP_KERNEL);
1944 if (!buf)
1945 return -ENOMEM;
1946
1947 /* We have to linearise by hand. */
1948
1949 u8 = buf;
Charles Keepax1d5b40b2014-02-25 13:45:50 +00001950
1951 for (i = 0; i < num_regs; i++) {
Markus Pargmann2f9b6602015-08-12 12:12:28 +02001952 unsigned int reg = regs[i].reg;
1953 unsigned int val = regs[i].def;
Philipp Zabelc6b570d2015-03-09 12:20:13 +01001954 trace_regmap_hw_write_start(map, reg, 1);
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001955 map->format.format_reg(u8, reg, map->reg_shift);
1956 u8 += reg_bytes + pad_bytes;
1957 map->format.format_val(u8, val, 0);
1958 u8 += val_bytes;
1959 }
1960 u8 = buf;
1961 *u8 |= map->write_flag_mask;
1962
1963 ret = map->bus->write(map->bus_context, buf, len);
1964
1965 kfree(buf);
1966
1967 for (i = 0; i < num_regs; i++) {
1968 int reg = regs[i].reg;
Philipp Zabelc6b570d2015-03-09 12:20:13 +01001969 trace_regmap_hw_write_done(map, reg, 1);
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001970 }
1971 return ret;
1972}
1973
1974static unsigned int _regmap_register_page(struct regmap *map,
1975 unsigned int reg,
1976 struct regmap_range_node *range)
1977{
1978 unsigned int win_page = (reg - range->range_min) / range->window_len;
1979
1980 return win_page;
1981}
1982
1983static int _regmap_range_multi_paged_reg_write(struct regmap *map,
Nariman Poushin8019ff62015-07-16 16:36:21 +01001984 struct reg_sequence *regs,
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001985 size_t num_regs)
1986{
1987 int ret;
1988 int i, n;
Nariman Poushin8019ff62015-07-16 16:36:21 +01001989 struct reg_sequence *base;
Geert Uytterhoevenb48d1392014-04-22 12:47:29 +02001990 unsigned int this_page = 0;
Nariman Poushin2de9d602015-07-16 16:36:22 +01001991 unsigned int page_change = 0;
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001992 /*
1993 * the set of registers are not neccessarily in order, but
1994 * since the order of write must be preserved this algorithm
Nariman Poushin2de9d602015-07-16 16:36:22 +01001995 * chops the set each time the page changes. This also applies
1996 * if there is a delay required at any point in the sequence.
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00001997 */
1998 base = regs;
1999 for (i = 0, n = 0; i < num_regs; i++, n++) {
2000 unsigned int reg = regs[i].reg;
2001 struct regmap_range_node *range;
2002
2003 range = _regmap_range_lookup(map, reg);
2004 if (range) {
2005 unsigned int win_page = _regmap_register_page(map, reg,
2006 range);
2007
2008 if (i == 0)
2009 this_page = win_page;
2010 if (win_page != this_page) {
2011 this_page = win_page;
Nariman Poushin2de9d602015-07-16 16:36:22 +01002012 page_change = 1;
2013 }
2014 }
2015
2016 /* If we have both a page change and a delay make sure to
2017 * write the regs and apply the delay before we change the
2018 * page.
2019 */
2020
2021 if (page_change || regs[i].delay_us) {
2022
2023 /* For situations where the first write requires
2024 * a delay we need to make sure we don't call
2025 * raw_multi_reg_write with n=0
2026 * This can't occur with page breaks as we
2027 * never write on the first iteration
2028 */
2029 if (regs[i].delay_us && i == 0)
2030 n = 1;
2031
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002032 ret = _regmap_raw_multi_reg_write(map, base, n);
2033 if (ret != 0)
2034 return ret;
Nariman Poushin2de9d602015-07-16 16:36:22 +01002035
2036 if (regs[i].delay_us)
2037 udelay(regs[i].delay_us);
2038
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002039 base += n;
2040 n = 0;
Nariman Poushin2de9d602015-07-16 16:36:22 +01002041
2042 if (page_change) {
2043 ret = _regmap_select_page(map,
2044 &base[n].reg,
2045 range, 1);
2046 if (ret != 0)
2047 return ret;
2048
2049 page_change = 0;
2050 }
2051
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002052 }
Nariman Poushin2de9d602015-07-16 16:36:22 +01002053
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002054 }
2055 if (n > 0)
2056 return _regmap_raw_multi_reg_write(map, base, n);
2057 return 0;
2058}
2059
2060static int _regmap_multi_reg_write(struct regmap *map,
Nariman Poushin8019ff62015-07-16 16:36:21 +01002061 const struct reg_sequence *regs,
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002062 size_t num_regs)
2063{
2064 int i;
2065 int ret;
2066
2067 if (!map->can_multi_write) {
2068 for (i = 0; i < num_regs; i++) {
2069 ret = _regmap_write(map, regs[i].reg, regs[i].def);
2070 if (ret != 0)
2071 return ret;
Nariman Poushin2de9d602015-07-16 16:36:22 +01002072
2073 if (regs[i].delay_us)
2074 udelay(regs[i].delay_us);
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002075 }
2076 return 0;
2077 }
2078
2079 if (!map->format.parse_inplace)
2080 return -EINVAL;
2081
2082 if (map->writeable_reg)
2083 for (i = 0; i < num_regs; i++) {
2084 int reg = regs[i].reg;
2085 if (!map->writeable_reg(map->dev, reg))
2086 return -EINVAL;
Xiubo Lifcac0232015-12-16 17:45:32 +08002087 if (!IS_ALIGNED(reg, map->reg_stride))
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002088 return -EINVAL;
2089 }
2090
2091 if (!map->cache_bypass) {
2092 for (i = 0; i < num_regs; i++) {
2093 unsigned int val = regs[i].def;
2094 unsigned int reg = regs[i].reg;
2095 ret = regcache_write(map, reg, val);
2096 if (ret) {
2097 dev_err(map->dev,
2098 "Error in caching of register: %x ret: %d\n",
2099 reg, ret);
2100 return ret;
2101 }
2102 }
2103 if (map->cache_only) {
2104 map->cache_dirty = true;
2105 return 0;
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002106 }
2107 }
2108
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002109 WARN_ON(!map->bus);
2110
2111 for (i = 0; i < num_regs; i++) {
2112 unsigned int reg = regs[i].reg;
2113 struct regmap_range_node *range;
Nariman Poushin2de9d602015-07-16 16:36:22 +01002114
2115 /* Coalesce all the writes between a page break or a delay
2116 * in a sequence
2117 */
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002118 range = _regmap_range_lookup(map, reg);
Nariman Poushin2de9d602015-07-16 16:36:22 +01002119 if (range || regs[i].delay_us) {
Nariman Poushin8019ff62015-07-16 16:36:21 +01002120 size_t len = sizeof(struct reg_sequence)*num_regs;
2121 struct reg_sequence *base = kmemdup(regs, len,
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002122 GFP_KERNEL);
2123 if (!base)
2124 return -ENOMEM;
2125 ret = _regmap_range_multi_paged_reg_write(map, base,
2126 num_regs);
2127 kfree(base);
2128
2129 return ret;
2130 }
2131 }
2132 return _regmap_raw_multi_reg_write(map, regs, num_regs);
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002133}
2134
Anthony Oleche33fabd2013-10-11 15:31:11 +01002135/*
2136 * regmap_multi_reg_write(): Write multiple registers to the device
2137 *
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002138 * where the set of register,value pairs are supplied in any order,
2139 * possibly not all in a single range.
Anthony Oleche33fabd2013-10-11 15:31:11 +01002140 *
2141 * @map: Register map to write to
2142 * @regs: Array of structures containing register,value to be written
2143 * @num_regs: Number of registers to write
2144 *
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002145 * The 'normal' block write mode will send ultimately send data on the
2146 * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
2147 * addressed. However, this alternative block multi write mode will send
2148 * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
2149 * must of course support the mode.
Anthony Oleche33fabd2013-10-11 15:31:11 +01002150 *
Opensource [Anthony Olech]e894c3f2014-03-04 13:54:02 +00002151 * A value of zero will be returned on success, a negative errno will be
2152 * returned in error cases.
Anthony Oleche33fabd2013-10-11 15:31:11 +01002153 */
Nariman Poushin8019ff62015-07-16 16:36:21 +01002154int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
Charles Keepaxf7e2cec2014-02-25 13:45:49 +00002155 int num_regs)
Anthony Oleche33fabd2013-10-11 15:31:11 +01002156{
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002157 int ret;
Anthony Oleche33fabd2013-10-11 15:31:11 +01002158
2159 map->lock(map->lock_arg);
2160
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002161 ret = _regmap_multi_reg_write(map, regs, num_regs);
2162
Anthony Oleche33fabd2013-10-11 15:31:11 +01002163 map->unlock(map->lock_arg);
2164
2165 return ret;
2166}
2167EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
2168
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002169/*
2170 * regmap_multi_reg_write_bypassed(): Write multiple registers to the
2171 * device but not the cache
2172 *
Mark Brown0d509f22013-01-27 22:07:38 +08002173 * where the set of register are supplied in any order
2174 *
2175 * @map: Register map to write to
2176 * @regs: Array of structures containing register,value to be written
2177 * @num_regs: Number of registers to write
2178 *
2179 * This function is intended to be used for writing a large block of data
2180 * atomically to the device in single transfer for those I2C client devices
2181 * that implement this alternative block write mode.
2182 *
2183 * A value of zero will be returned on success, a negative errno will
2184 * be returned in error cases.
2185 */
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002186int regmap_multi_reg_write_bypassed(struct regmap *map,
Nariman Poushin8019ff62015-07-16 16:36:21 +01002187 const struct reg_sequence *regs,
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002188 int num_regs)
Mark Brown0d509f22013-01-27 22:07:38 +08002189{
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002190 int ret;
2191 bool bypass;
Mark Brown0d509f22013-01-27 22:07:38 +08002192
2193 map->lock(map->lock_arg);
2194
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002195 bypass = map->cache_bypass;
2196 map->cache_bypass = true;
2197
2198 ret = _regmap_multi_reg_write(map, regs, num_regs);
2199
2200 map->cache_bypass = bypass;
2201
Mark Brown0a819802013-10-09 12:28:52 +01002202 map->unlock(map->lock_arg);
2203
2204 return ret;
2205}
Charles Keepax1d5b40b2014-02-25 13:45:50 +00002206EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
Mark Brown0d509f22013-01-27 22:07:38 +08002207
2208/**
2209 * regmap_raw_write_async(): Write raw values to one or more registers
2210 * asynchronously
2211 *
2212 * @map: Register map to write to
Mark Brownb83a3132011-05-11 19:59:58 +02002213 * @reg: Initial register to write to
2214 * @val: Block of data to be written, laid out for direct transmission to the
2215 * device. Must be valid until regmap_async_complete() is called.
Mark Brown98bc7df2012-10-04 17:31:11 +01002216 * @val_len: Length of data pointed to by val.
Mark Brownb83a3132011-05-11 19:59:58 +02002217 *
2218 * This function is intended to be used for things like firmware
2219 * download where a large block of data needs to be transferred to the
Mark Brownf1b5c5c2013-03-13 19:18:13 +00002220 * device. No formatting will be done on the data provided.
Andrey Smirnovd2a58842013-01-27 10:49:05 -08002221 *
Mark Brown98bc7df2012-10-04 17:31:11 +01002222 * If supported by the underlying bus the write will be scheduled
2223 * asynchronously, helping maximise I/O speed on higher speed buses
2224 * like SPI. regmap_async_complete() can be called to ensure that all
2225 * asynchrnous writes have been completed.
Mark Brown0ff3e622012-10-04 17:39:13 +01002226 *
Mark Brown98bc7df2012-10-04 17:31:11 +01002227 * A value of zero will be returned on success, a negative errno will
2228 * be returned in error cases.
Krystian Garbaciak6863ca62012-06-15 11:23:56 +01002229 */
Marc Reillyd939fb92012-03-16 12:11:43 +11002230int regmap_raw_write_async(struct regmap *map, unsigned int reg,
Mark Brownb83a3132011-05-11 19:59:58 +02002231 const void *val, size_t val_len)
2232{
Lars-Peter Clausen6f306442011-09-05 20:46:32 +02002233 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +02002234
2235 if (val_len % map->format.val_bytes)
2236 return -EINVAL;
Xiubo Lifcac0232015-12-16 17:45:32 +08002237 if (!IS_ALIGNED(reg, map->reg_stride))
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01002238 return -EINVAL;
2239
2240 map->lock(map->lock_arg);
2241
Lars-Peter Clausen19254412011-11-16 16:28:19 +01002242 map->async = true;
2243
2244 ret = _regmap_raw_write(map, reg, val, val_len);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01002245
2246 map->async = false;
2247
Mark Brownb83a3132011-05-11 19:59:58 +02002248 map->unlock(map->lock_arg);
Mark Brownfb2736b2011-07-24 21:30:55 +01002249
Mark Brownb83a3132011-05-11 19:59:58 +02002250 return ret;
Mark Brown1044c182012-07-06 14:10:23 +01002251}
2252EXPORT_SYMBOL_GPL(regmap_raw_write_async);
2253
2254static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2255 unsigned int val_len)
2256{
Mark Brownfb2736b2011-07-24 21:30:55 +01002257 struct regmap_range_node *range;
2258 u8 *u8 = map->work_buf;
Mark Brownb83a3132011-05-11 19:59:58 +02002259 int ret;
Mark Brownf2985362012-04-30 21:25:05 +01002260
2261 WARN_ON(!map->bus);
2262
Mark Brownb83a3132011-05-11 19:59:58 +02002263 range = _regmap_range_lookup(map, reg);
2264 if (range) {
2265 ret = _regmap_select_page(map, &reg, range,
2266 val_len / map->format.val_bytes);
2267 if (ret != 0)
2268 return ret;
2269 }
2270
2271 map->format.format_reg(map->work_buf, reg, map->reg_shift);
2272
2273 /*
2274 * Some buses or devices flag reads by setting the high bits in the
Xiubo Lib486afb2015-08-12 15:02:19 +08002275 * register address; since it's always the high bits for all
Mark Brownb83a3132011-05-11 19:59:58 +02002276 * current formats we can do this here rather than in
2277 * formatting. This may break if we get interesting formats.
2278 */
2279 u8[0] |= map->read_flag_mask;
2280
Philipp Zabelc6b570d2015-03-09 12:20:13 +01002281 trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
Mark Brownb83a3132011-05-11 19:59:58 +02002282
2283 ret = map->bus->read(map->bus_context, map->work_buf,
2284 map->format.reg_bytes + map->format.pad_bytes,
2285 val, val_len);
2286
Philipp Zabelc6b570d2015-03-09 12:20:13 +01002287 trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
Mark Brownb83a3132011-05-11 19:59:58 +02002288
2289 return ret;
2290}
2291
Boris BREZILLON3ac17032014-04-17 11:40:11 +02002292static int _regmap_bus_reg_read(void *context, unsigned int reg,
2293 unsigned int *val)
2294{
2295 struct regmap *map = context;
2296
2297 return map->bus->reg_read(map->bus_context, reg, val);
2298}
2299
Andrey Smirnovad278402013-01-12 12:54:12 -08002300static int _regmap_bus_read(void *context, unsigned int reg,
2301 unsigned int *val)
2302{
2303 int ret;
2304 struct regmap *map = context;
2305
2306 if (!map->format.parse_val)
2307 return -EINVAL;
2308
2309 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
2310 if (ret == 0)
2311 *val = map->format.parse_val(map->work_buf);
2312
2313 return ret;
2314}
2315
Mark Brownb83a3132011-05-11 19:59:58 +02002316static int _regmap_read(struct regmap *map, unsigned int reg,
2317 unsigned int *val)
2318{
2319 int ret;
Andrey Smirnovd2a58842013-01-27 10:49:05 -08002320 void *context = _regmap_map_get_context(map);
2321
Mark Brownb83a3132011-05-11 19:59:58 +02002322 if (!map->cache_bypass) {
2323 ret = regcache_read(map, reg, val);
2324 if (ret == 0)
2325 return 0;
2326 }
2327
2328 if (map->cache_only)
2329 return -EBUSY;
2330
Michal Simekd4807ad2014-02-10 12:59:46 +01002331 if (!regmap_readable(map, reg))
2332 return -EIO;
2333
Andrey Smirnovd2a58842013-01-27 10:49:05 -08002334 ret = map->reg_read(context, reg, val);
Mark Brownb83a3132011-05-11 19:59:58 +02002335 if (ret == 0) {
Mark Brownb83a3132011-05-11 19:59:58 +02002336#ifdef LOG_DEVICE
Pankaj Dubey5336be82014-09-27 09:47:55 +05302337 if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
Mark Brownb83a3132011-05-11 19:59:58 +02002338 dev_info(map->dev, "%x => %x\n", reg, *val);
2339#endif
2340
Philipp Zabelc6b570d2015-03-09 12:20:13 +01002341 trace_regmap_reg_read(map, reg, *val);
Mark Brownb83a3132011-05-11 19:59:58 +02002342
Andrey Smirnovad278402013-01-12 12:54:12 -08002343 if (!map->cache_bypass)
2344 regcache_write(map, reg, *val);
2345 }
Mark Brownb83a3132011-05-11 19:59:58 +02002346
2347 return ret;
2348}
2349
2350/**
2351 * regmap_read(): Read a value from a single register
2352 *
Gerhard Sittig00933802013-11-11 10:42:36 +01002353 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02002354 * @reg: Register to be read from
2355 * @val: Pointer to store read value
2356 *
2357 * A value of zero will be returned on success, a negative errno will
2358 * be returned in error cases.
2359 */
2360int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
2361{
2362 int ret;
2363
Xiubo Lifcac0232015-12-16 17:45:32 +08002364 if (!IS_ALIGNED(reg, map->reg_stride))
Stephen Warrenf01ee602012-04-09 13:40:24 -06002365 return -EINVAL;
2366
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002367 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02002368
2369 ret = _regmap_read(map, reg, val);
2370
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002371 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02002372
2373 return ret;
2374}
2375EXPORT_SYMBOL_GPL(regmap_read);
2376
2377/**
2378 * regmap_raw_read(): Read raw data from the device
2379 *
Gerhard Sittig00933802013-11-11 10:42:36 +01002380 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02002381 * @reg: First register to be read from
2382 * @val: Pointer to store read value
2383 * @val_len: Size of data to read
2384 *
2385 * A value of zero will be returned on success, a negative errno will
2386 * be returned in error cases.
2387 */
2388int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
2389 size_t val_len)
2390{
Mark Brownb8fb5ab2012-02-21 19:12:47 +00002391 size_t val_bytes = map->format.val_bytes;
2392 size_t val_count = val_len / val_bytes;
2393 unsigned int v;
2394 int ret, i;
Mark Brown04e016a2011-10-09 13:35:43 +01002395
Andrey Smirnovd2a58842013-01-27 10:49:05 -08002396 if (!map->bus)
2397 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06002398 if (val_len % map->format.val_bytes)
2399 return -EINVAL;
Xiubo Lifcac0232015-12-16 17:45:32 +08002400 if (!IS_ALIGNED(reg, map->reg_stride))
Stephen Warrenf01ee602012-04-09 13:40:24 -06002401 return -EINVAL;
Mark Brownfa3eec72015-07-01 23:51:43 +01002402 if (val_count == 0)
2403 return -EINVAL;
Stephen Warren851960b2012-04-06 15:16:03 -06002404
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002405 map->lock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02002406
Mark Brownb8fb5ab2012-02-21 19:12:47 +00002407 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
2408 map->cache_type == REGCACHE_NONE) {
Markus Pargmann9a16ea92015-08-20 11:12:35 +02002409 if (!map->bus->read) {
2410 ret = -ENOTSUPP;
2411 goto out;
2412 }
Markus Pargmannc3359312015-08-30 09:33:55 +02002413 if (map->max_raw_read && map->max_raw_read < val_len) {
2414 ret = -E2BIG;
2415 goto out;
2416 }
Markus Pargmann9a16ea92015-08-20 11:12:35 +02002417
Mark Brownb8fb5ab2012-02-21 19:12:47 +00002418 /* Physical block read if there's no cache involved */
2419 ret = _regmap_raw_read(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +02002420
Mark Brownb8fb5ab2012-02-21 19:12:47 +00002421 } else {
2422 /* Otherwise go word by word for the cache; should be low
2423 * cost as we expect to hit the cache.
2424 */
2425 for (i = 0; i < val_count; i++) {
Xiubo Lica747be2016-01-04 18:00:33 +08002426 ret = _regmap_read(map, reg + regmap_get_offset(map, i),
Stephen Warrenf01ee602012-04-09 13:40:24 -06002427 &v);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00002428 if (ret != 0)
2429 goto out;
2430
Marc Reillyd939fb92012-03-16 12:11:43 +11002431 map->format.format_val(val + (i * val_bytes), v, 0);
Mark Brownb8fb5ab2012-02-21 19:12:47 +00002432 }
2433 }
2434
2435 out:
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002436 map->unlock(map->lock_arg);
Mark Brownb83a3132011-05-11 19:59:58 +02002437
2438 return ret;
2439}
2440EXPORT_SYMBOL_GPL(regmap_raw_read);
2441
2442/**
Srinivas Kandagatla67252282013-06-11 13:18:15 +01002443 * regmap_field_read(): Read a value to a single register field
2444 *
2445 * @field: Register field to read from
2446 * @val: Pointer to store read value
2447 *
2448 * A value of zero will be returned on success, a negative errno will
2449 * be returned in error cases.
2450 */
2451int regmap_field_read(struct regmap_field *field, unsigned int *val)
2452{
2453 int ret;
2454 unsigned int reg_val;
2455 ret = regmap_read(field->regmap, field->reg, &reg_val);
2456 if (ret != 0)
2457 return ret;
2458
2459 reg_val &= field->mask;
2460 reg_val >>= field->shift;
2461 *val = reg_val;
2462
2463 return ret;
2464}
2465EXPORT_SYMBOL_GPL(regmap_field_read);
2466
2467/**
Kuninori Morimotoa0102372013-09-01 20:30:50 -07002468 * regmap_fields_read(): Read a value to a single register field with port ID
2469 *
2470 * @field: Register field to read from
2471 * @id: port ID
2472 * @val: Pointer to store read value
2473 *
2474 * A value of zero will be returned on success, a negative errno will
2475 * be returned in error cases.
2476 */
2477int regmap_fields_read(struct regmap_field *field, unsigned int id,
2478 unsigned int *val)
2479{
2480 int ret;
2481 unsigned int reg_val;
2482
2483 if (id >= field->id_size)
2484 return -EINVAL;
2485
2486 ret = regmap_read(field->regmap,
2487 field->reg + (field->id_offset * id),
2488 &reg_val);
2489 if (ret != 0)
2490 return ret;
2491
2492 reg_val &= field->mask;
2493 reg_val >>= field->shift;
2494 *val = reg_val;
2495
2496 return ret;
2497}
2498EXPORT_SYMBOL_GPL(regmap_fields_read);
2499
2500/**
Mark Brownb83a3132011-05-11 19:59:58 +02002501 * regmap_bulk_read(): Read multiple registers from the device
2502 *
Gerhard Sittig00933802013-11-11 10:42:36 +01002503 * @map: Register map to read from
Mark Brownb83a3132011-05-11 19:59:58 +02002504 * @reg: First register to be read from
2505 * @val: Pointer to store read value, in native register size for device
2506 * @val_count: Number of registers to read
2507 *
2508 * A value of zero will be returned on success, a negative errno will
2509 * be returned in error cases.
2510 */
2511int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
2512 size_t val_count)
2513{
2514 int ret, i;
2515 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +01002516 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +01002517
Xiubo Lifcac0232015-12-16 17:45:32 +08002518 if (!IS_ALIGNED(reg, map->reg_stride))
Stephen Warrenf01ee602012-04-09 13:40:24 -06002519 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +02002520
Stephen Boyd3b58ee12013-12-13 09:14:07 -08002521 if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
Ashish Jangam2e33caf2012-04-30 23:23:40 +01002522 /*
2523 * Some devices does not support bulk read, for
2524 * them we have a series of single read operations.
2525 */
Markus Pargmannadaac452015-08-30 09:33:53 +02002526 size_t total_size = val_bytes * val_count;
2527
2528 if (!map->use_single_read &&
2529 (!map->max_raw_read || map->max_raw_read > total_size)) {
Ashish Jangam2e33caf2012-04-30 23:23:40 +01002530 ret = regmap_raw_read(map, reg, val,
2531 val_bytes * val_count);
2532 if (ret != 0)
2533 return ret;
Markus Pargmannadaac452015-08-30 09:33:53 +02002534 } else {
2535 /*
2536 * Some devices do not support bulk read or do not
2537 * support large bulk reads, for them we have a series
2538 * of read operations.
2539 */
2540 int chunk_stride = map->reg_stride;
2541 size_t chunk_size = val_bytes;
2542 size_t chunk_count = val_count;
2543
2544 if (!map->use_single_read) {
2545 chunk_size = map->max_raw_read;
2546 if (chunk_size % val_bytes)
2547 chunk_size -= chunk_size % val_bytes;
2548 chunk_count = total_size / chunk_size;
2549 chunk_stride *= chunk_size / val_bytes;
2550 }
2551
2552 /* Read bytes that fit into a multiple of chunk_size */
2553 for (i = 0; i < chunk_count; i++) {
2554 ret = regmap_raw_read(map,
2555 reg + (i * chunk_stride),
2556 val + (i * chunk_size),
2557 chunk_size);
2558 if (ret != 0)
2559 return ret;
2560 }
2561
2562 /* Read remaining bytes */
2563 if (chunk_size * i < total_size) {
2564 ret = regmap_raw_read(map,
2565 reg + (i * chunk_stride),
2566 val + (i * chunk_size),
2567 total_size - i * chunk_size);
2568 if (ret != 0)
2569 return ret;
2570 }
Ashish Jangam2e33caf2012-04-30 23:23:40 +01002571 }
Mark Brownde2d8082011-10-10 13:24:52 +01002572
2573 for (i = 0; i < val_count * val_bytes; i += val_bytes)
Mark Brown8a819ff2013-03-04 09:04:51 +08002574 map->format.parse_inplace(val + i);
Mark Brownde2d8082011-10-10 13:24:52 +01002575 } else {
2576 for (i = 0; i < val_count; i++) {
Laxman Dewangan6560ffd2012-05-09 17:43:12 +05302577 unsigned int ival;
Xiubo Lica747be2016-01-04 18:00:33 +08002578 ret = regmap_read(map, reg + regmap_get_offset(map, i),
Mark Brown25061d22012-05-12 13:06:08 +01002579 &ival);
Mark Brownde2d8082011-10-10 13:24:52 +01002580 if (ret != 0)
2581 return ret;
Mark Brownd5b98eb2015-08-28 20:04:53 +01002582
2583 if (map->format.format_val) {
2584 map->format.format_val(val + (i * val_bytes), ival, 0);
2585 } else {
2586 /* Devices providing read and write
2587 * operations can use the bulk I/O
2588 * functions if they define a val_bytes,
2589 * we assume that the values are native
2590 * endian.
2591 */
Xiubo Li19c04782015-12-09 17:11:53 +08002592#ifdef CONFIG_64BIT
Xiubo Liafcc00b2015-12-03 17:31:52 +08002593 u64 *u64 = val;
Xiubo Li19c04782015-12-09 17:11:53 +08002594#endif
Mark Brownd5b98eb2015-08-28 20:04:53 +01002595 u32 *u32 = val;
2596 u16 *u16 = val;
2597 u8 *u8 = val;
2598
2599 switch (map->format.val_bytes) {
Xiubo Liafcc00b2015-12-03 17:31:52 +08002600#ifdef CONFIG_64BIT
2601 case 8:
2602 u64[i] = ival;
2603 break;
2604#endif
Mark Brownd5b98eb2015-08-28 20:04:53 +01002605 case 4:
2606 u32[i] = ival;
2607 break;
2608 case 2:
2609 u16[i] = ival;
2610 break;
2611 case 1:
2612 u8[i] = ival;
2613 break;
2614 default:
2615 return -EINVAL;
2616 }
2617 }
Mark Brownde2d8082011-10-10 13:24:52 +01002618 }
2619 }
Mark Brownb83a3132011-05-11 19:59:58 +02002620
2621 return 0;
2622}
2623EXPORT_SYMBOL_GPL(regmap_bulk_read);
2624
Mark Brown018690d2011-11-29 20:10:36 +00002625static int _regmap_update_bits(struct regmap *map, unsigned int reg,
2626 unsigned int mask, unsigned int val,
Kuninori Morimoto7ff05892015-06-16 08:52:22 +00002627 bool *change, bool force_write)
Mark Brownb83a3132011-05-11 19:59:58 +02002628{
2629 int ret;
Mark Brownd91e8db22011-11-18 16:03:50 +00002630 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +02002631
Jon Ringle77792b12015-10-01 12:38:07 -04002632 if (change)
2633 *change = false;
Mark Brownb83a3132011-05-11 19:59:58 +02002634
Jon Ringle77792b12015-10-01 12:38:07 -04002635 if (regmap_volatile(map, reg) && map->reg_update_bits) {
2636 ret = map->reg_update_bits(map->bus_context, reg, mask, val);
2637 if (ret == 0 && change)
Xiubo Lie2f74dc2014-02-20 08:50:10 +08002638 *change = true;
Mark Brown018690d2011-11-29 20:10:36 +00002639 } else {
Jon Ringle77792b12015-10-01 12:38:07 -04002640 ret = _regmap_read(map, reg, &orig);
2641 if (ret != 0)
2642 return ret;
2643
2644 tmp = orig & ~mask;
2645 tmp |= val & mask;
2646
2647 if (force_write || (tmp != orig)) {
2648 ret = _regmap_write(map, reg, tmp);
2649 if (ret == 0 && change)
2650 *change = true;
2651 }
Mark Brown018690d2011-11-29 20:10:36 +00002652 }
Mark Brownb83a3132011-05-11 19:59:58 +02002653
Mark Brownb83a3132011-05-11 19:59:58 +02002654 return ret;
2655}
Mark Brown018690d2011-11-29 20:10:36 +00002656
2657/**
2658 * regmap_update_bits: Perform a read/modify/write cycle on the register map
2659 *
2660 * @map: Register map to update
2661 * @reg: Register to update
2662 * @mask: Bitmask to change
2663 * @val: New value for bitmask
2664 *
2665 * Returns zero for success, a negative number on error.
2666 */
2667int regmap_update_bits(struct regmap *map, unsigned int reg,
2668 unsigned int mask, unsigned int val)
2669{
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002670 int ret;
2671
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002672 map->lock(map->lock_arg);
Kuninori Morimoto7ff05892015-06-16 08:52:22 +00002673 ret = _regmap_update_bits(map, reg, mask, val, NULL, false);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002674 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002675
2676 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00002677}
Mark Brownb83a3132011-05-11 19:59:58 +02002678EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +01002679
Mark Brown018690d2011-11-29 20:10:36 +00002680/**
Kuninori Morimotofd4b7286c2015-06-16 08:52:39 +00002681 * regmap_write_bits: Perform a read/modify/write cycle on the register map
2682 *
2683 * @map: Register map to update
2684 * @reg: Register to update
2685 * @mask: Bitmask to change
2686 * @val: New value for bitmask
2687 *
2688 * Returns zero for success, a negative number on error.
2689 */
2690int regmap_write_bits(struct regmap *map, unsigned int reg,
2691 unsigned int mask, unsigned int val)
2692{
2693 int ret;
2694
2695 map->lock(map->lock_arg);
2696 ret = _regmap_update_bits(map, reg, mask, val, NULL, true);
2697 map->unlock(map->lock_arg);
2698
2699 return ret;
2700}
2701EXPORT_SYMBOL_GPL(regmap_write_bits);
2702
2703/**
Mark Brown915f4412013-10-09 13:30:10 +01002704 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
2705 * map asynchronously
2706 *
2707 * @map: Register map to update
2708 * @reg: Register to update
2709 * @mask: Bitmask to change
2710 * @val: New value for bitmask
2711 *
2712 * With most buses the read must be done synchronously so this is most
2713 * useful for devices with a cache which do not need to interact with
2714 * the hardware to determine the current register value.
2715 *
2716 * Returns zero for success, a negative number on error.
2717 */
2718int regmap_update_bits_async(struct regmap *map, unsigned int reg,
2719 unsigned int mask, unsigned int val)
2720{
Mark Brown915f4412013-10-09 13:30:10 +01002721 int ret;
2722
2723 map->lock(map->lock_arg);
2724
2725 map->async = true;
2726
Kuninori Morimoto7ff05892015-06-16 08:52:22 +00002727 ret = _regmap_update_bits(map, reg, mask, val, NULL, false);
Mark Brown915f4412013-10-09 13:30:10 +01002728
2729 map->async = false;
2730
2731 map->unlock(map->lock_arg);
2732
2733 return ret;
2734}
2735EXPORT_SYMBOL_GPL(regmap_update_bits_async);
2736
2737/**
Mark Brown018690d2011-11-29 20:10:36 +00002738 * regmap_update_bits_check: Perform a read/modify/write cycle on the
2739 * register map and report if updated
2740 *
2741 * @map: Register map to update
2742 * @reg: Register to update
2743 * @mask: Bitmask to change
2744 * @val: New value for bitmask
2745 * @change: Boolean indicating if a write was done
2746 *
2747 * Returns zero for success, a negative number on error.
2748 */
2749int regmap_update_bits_check(struct regmap *map, unsigned int reg,
2750 unsigned int mask, unsigned int val,
2751 bool *change)
2752{
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002753 int ret;
2754
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002755 map->lock(map->lock_arg);
Kuninori Morimoto7ff05892015-06-16 08:52:22 +00002756 ret = _regmap_update_bits(map, reg, mask, val, change, false);
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002757 map->unlock(map->lock_arg);
Krystian Garbaciakfc3ebd72012-06-15 11:23:56 +01002758 return ret;
Mark Brown018690d2011-11-29 20:10:36 +00002759}
2760EXPORT_SYMBOL_GPL(regmap_update_bits_check);
2761
Mark Brown915f4412013-10-09 13:30:10 +01002762/**
2763 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2764 * register map asynchronously and report if
2765 * updated
2766 *
2767 * @map: Register map to update
2768 * @reg: Register to update
2769 * @mask: Bitmask to change
2770 * @val: New value for bitmask
2771 * @change: Boolean indicating if a write was done
2772 *
2773 * With most buses the read must be done synchronously so this is most
2774 * useful for devices with a cache which do not need to interact with
2775 * the hardware to determine the current register value.
2776 *
2777 * Returns zero for success, a negative number on error.
2778 */
2779int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2780 unsigned int mask, unsigned int val,
2781 bool *change)
2782{
2783 int ret;
2784
2785 map->lock(map->lock_arg);
2786
2787 map->async = true;
2788
Kuninori Morimoto7ff05892015-06-16 08:52:22 +00002789 ret = _regmap_update_bits(map, reg, mask, val, change, false);
Mark Brown915f4412013-10-09 13:30:10 +01002790
2791 map->async = false;
2792
2793 map->unlock(map->lock_arg);
2794
2795 return ret;
2796}
2797EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2798
Mark Brown0d509f22013-01-27 22:07:38 +08002799void regmap_async_complete_cb(struct regmap_async *async, int ret)
2800{
2801 struct regmap *map = async->map;
2802 bool wake;
2803
Philipp Zabelc6b570d2015-03-09 12:20:13 +01002804 trace_regmap_async_io_complete(map);
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002805
Mark Brown0d509f22013-01-27 22:07:38 +08002806 spin_lock(&map->async_lock);
Mark Brown7e09a972013-10-07 23:00:24 +01002807 list_move(&async->list, &map->async_free);
Mark Brown0d509f22013-01-27 22:07:38 +08002808 wake = list_empty(&map->async_list);
2809
2810 if (ret != 0)
2811 map->async_ret = ret;
2812
2813 spin_unlock(&map->async_lock);
2814
Mark Brown0d509f22013-01-27 22:07:38 +08002815 if (wake)
2816 wake_up(&map->async_waitq);
2817}
Axel Linf804fb52013-02-03 00:14:13 +08002818EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
Mark Brown0d509f22013-01-27 22:07:38 +08002819
2820static int regmap_async_is_done(struct regmap *map)
2821{
2822 unsigned long flags;
2823 int ret;
2824
2825 spin_lock_irqsave(&map->async_lock, flags);
2826 ret = list_empty(&map->async_list);
2827 spin_unlock_irqrestore(&map->async_lock, flags);
2828
2829 return ret;
2830}
2831
2832/**
2833 * regmap_async_complete: Ensure all asynchronous I/O has completed.
2834 *
2835 * @map: Map to operate on.
2836 *
2837 * Blocks until any pending asynchronous I/O has completed. Returns
2838 * an error code for any failed I/O operations.
2839 */
2840int regmap_async_complete(struct regmap *map)
2841{
2842 unsigned long flags;
2843 int ret;
2844
2845 /* Nothing to do with no async support */
Daniel Mackf2e055e2013-07-04 13:11:03 +02002846 if (!map->bus || !map->bus->async_write)
Mark Brown0d509f22013-01-27 22:07:38 +08002847 return 0;
2848
Philipp Zabelc6b570d2015-03-09 12:20:13 +01002849 trace_regmap_async_complete_start(map);
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002850
Mark Brown0d509f22013-01-27 22:07:38 +08002851 wait_event(map->async_waitq, regmap_async_is_done(map));
2852
2853 spin_lock_irqsave(&map->async_lock, flags);
2854 ret = map->async_ret;
2855 map->async_ret = 0;
2856 spin_unlock_irqrestore(&map->async_lock, flags);
2857
Philipp Zabelc6b570d2015-03-09 12:20:13 +01002858 trace_regmap_async_complete_done(map);
Mark Brownfe7d4cc2013-02-21 19:05:48 +00002859
Mark Brown0d509f22013-01-27 22:07:38 +08002860 return ret;
2861}
Mark Brownf88948e2013-02-05 13:53:26 +00002862EXPORT_SYMBOL_GPL(regmap_async_complete);
Mark Brown0d509f22013-01-27 22:07:38 +08002863
Mark Brown22f0d902012-01-21 12:01:14 +00002864/**
2865 * regmap_register_patch: Register and apply register updates to be applied
2866 * on device initialistion
2867 *
2868 * @map: Register map to apply updates to.
2869 * @regs: Values to update.
2870 * @num_regs: Number of entries in regs.
2871 *
2872 * Register a set of register updates to be applied to the device
2873 * whenever the device registers are synchronised with the cache and
2874 * apply them immediately. Typically this is used to apply
2875 * corrections to be applied to the device defaults on startup, such
2876 * as the updates some vendors provide to undocumented registers.
Mark Brown56fb1c72014-03-18 10:53:26 +00002877 *
2878 * The caller must ensure that this function cannot be called
2879 * concurrently with either itself or regcache_sync().
Mark Brown22f0d902012-01-21 12:01:14 +00002880 */
Nariman Poushin8019ff62015-07-16 16:36:21 +01002881int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
Mark Brown22f0d902012-01-21 12:01:14 +00002882 int num_regs)
2883{
Nariman Poushin8019ff62015-07-16 16:36:21 +01002884 struct reg_sequence *p;
Charles Keepax6bf13102014-02-25 13:45:51 +00002885 int ret;
Mark Brown22f0d902012-01-21 12:01:14 +00002886 bool bypass;
2887
Cai Zhiyongbd60e382013-11-18 20:21:49 +08002888 if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
2889 num_regs))
2890 return 0;
2891
Mark Brownaab13eb2013-07-11 12:41:44 +01002892 p = krealloc(map->patch,
Nariman Poushin8019ff62015-07-16 16:36:21 +01002893 sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
Mark Brownaab13eb2013-07-11 12:41:44 +01002894 GFP_KERNEL);
2895 if (p) {
2896 memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
2897 map->patch = p;
2898 map->patch_regs += num_regs;
Mark Brown22f0d902012-01-21 12:01:14 +00002899 } else {
Mark Brown56fb1c72014-03-18 10:53:26 +00002900 return -ENOMEM;
Mark Brown22f0d902012-01-21 12:01:14 +00002901 }
2902
Mark Brown22f0d902012-01-21 12:01:14 +00002903 map->lock(map->lock_arg);
2904
2905 bypass = map->cache_bypass;
2906
2907 map->cache_bypass = true;
2908 map->async = true;
2909
Charles Keepax6bf13102014-02-25 13:45:51 +00002910 ret = _regmap_multi_reg_write(map, regs, num_regs);
Mark Brown22f0d902012-01-21 12:01:14 +00002911
Mark Brown1a25f262013-10-10 20:55:03 +01002912 map->async = false;
Mark Brown22f0d902012-01-21 12:01:14 +00002913 map->cache_bypass = bypass;
2914
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +02002915 map->unlock(map->lock_arg);
Mark Brown22f0d902012-01-21 12:01:14 +00002916
Mark Brown1a25f262013-10-10 20:55:03 +01002917 regmap_async_complete(map);
2918
Mark Brown22f0d902012-01-21 12:01:14 +00002919 return ret;
2920}
2921EXPORT_SYMBOL_GPL(regmap_register_patch);
2922
Mark Browneae4b512012-03-14 13:15:03 +00002923/*
Mark Browna6539c32012-02-17 14:20:14 -08002924 * regmap_get_val_bytes(): Report the size of a register value
2925 *
2926 * Report the size of a register value, mainly intended to for use by
2927 * generic infrastructure built on top of regmap.
2928 */
2929int regmap_get_val_bytes(struct regmap *map)
2930{
2931 if (map->format.format_write)
2932 return -EINVAL;
2933
2934 return map->format.val_bytes;
2935}
2936EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
2937
Srinivas Kandagatla668abc72015-05-21 17:42:43 +01002938/**
2939 * regmap_get_max_register(): Report the max register value
2940 *
2941 * Report the max register value, mainly intended to for use by
2942 * generic infrastructure built on top of regmap.
2943 */
2944int regmap_get_max_register(struct regmap *map)
2945{
2946 return map->max_register ? map->max_register : -EINVAL;
2947}
2948EXPORT_SYMBOL_GPL(regmap_get_max_register);
2949
Srinivas Kandagatlaa2f776c2015-05-21 17:42:54 +01002950/**
2951 * regmap_get_reg_stride(): Report the register address stride
2952 *
2953 * Report the register address stride, mainly intended to for use by
2954 * generic infrastructure built on top of regmap.
2955 */
2956int regmap_get_reg_stride(struct regmap *map)
2957{
2958 return map->reg_stride;
2959}
2960EXPORT_SYMBOL_GPL(regmap_get_reg_stride);
2961
Nenghua Cao13ff50c2014-02-19 18:44:13 +08002962int regmap_parse_val(struct regmap *map, const void *buf,
2963 unsigned int *val)
2964{
2965 if (!map->format.parse_val)
2966 return -EINVAL;
2967
2968 *val = map->format.parse_val(buf);
2969
2970 return 0;
2971}
2972EXPORT_SYMBOL_GPL(regmap_parse_val);
2973
Mark Brown31244e32011-07-20 22:56:53 +01002974static int __init regmap_initcall(void)
2975{
2976 regmap_debugfs_initcall();
2977
2978 return 0;
2979}
2980postcore_initcall(regmap_initcall);