blob: b1dad1f9c47df25ffc1e36e81ce0d313f6c22e8b [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>
18
Mark Brownfb2736b2011-07-24 21:30:55 +010019#define CREATE_TRACE_POINTS
20#include <trace/events/regmap.h>
21
Mark Brown93de9122011-07-20 22:35:37 +010022#include "internal.h"
Mark Brownb83a3132011-05-11 19:59:58 +020023
Mark Brown8de2f082011-08-10 17:14:41 +090024bool regmap_writeable(struct regmap *map, unsigned int reg)
25{
26 if (map->max_register && reg > map->max_register)
27 return false;
28
29 if (map->writeable_reg)
30 return map->writeable_reg(map->dev, reg);
31
32 return true;
33}
34
35bool regmap_readable(struct regmap *map, unsigned int reg)
36{
37 if (map->max_register && reg > map->max_register)
38 return false;
39
Wolfram Sang4191f192012-01-30 15:08:16 +010040 if (map->format.format_write)
41 return false;
42
Mark Brown8de2f082011-08-10 17:14:41 +090043 if (map->readable_reg)
44 return map->readable_reg(map->dev, reg);
45
46 return true;
47}
48
49bool regmap_volatile(struct regmap *map, unsigned int reg)
50{
Wolfram Sang4191f192012-01-30 15:08:16 +010051 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +090052 return false;
53
54 if (map->volatile_reg)
55 return map->volatile_reg(map->dev, reg);
56
57 return true;
58}
59
60bool regmap_precious(struct regmap *map, unsigned int reg)
61{
Wolfram Sang4191f192012-01-30 15:08:16 +010062 if (!regmap_readable(map, reg))
Mark Brown8de2f082011-08-10 17:14:41 +090063 return false;
64
65 if (map->precious_reg)
66 return map->precious_reg(map->dev, reg);
67
68 return false;
69}
70
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +010071static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
72 unsigned int num)
73{
74 unsigned int i;
75
76 for (i = 0; i < num; i++)
77 if (!regmap_volatile(map, reg + i))
78 return false;
79
80 return true;
81}
82
Wolfram Sang9aa50752012-01-27 16:10:22 +010083static void regmap_format_2_6_write(struct regmap *map,
84 unsigned int reg, unsigned int val)
85{
86 u8 *out = map->work_buf;
87
88 *out = (reg << 6) | val;
89}
90
Mark Brownb83a3132011-05-11 19:59:58 +020091static void regmap_format_4_12_write(struct regmap *map,
92 unsigned int reg, unsigned int val)
93{
94 __be16 *out = map->work_buf;
95 *out = cpu_to_be16((reg << 12) | val);
96}
97
98static void regmap_format_7_9_write(struct regmap *map,
99 unsigned int reg, unsigned int val)
100{
101 __be16 *out = map->work_buf;
102 *out = cpu_to_be16((reg << 9) | val);
103}
104
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100105static void regmap_format_10_14_write(struct regmap *map,
106 unsigned int reg, unsigned int val)
107{
108 u8 *out = map->work_buf;
109
110 out[2] = val;
111 out[1] = (val >> 8) | (reg << 6);
112 out[0] = reg >> 2;
113}
114
Mark Brownb83a3132011-05-11 19:59:58 +0200115static void regmap_format_8(void *buf, unsigned int val)
116{
117 u8 *b = buf;
118
119 b[0] = val;
120}
121
122static void regmap_format_16(void *buf, unsigned int val)
123{
124 __be16 *b = buf;
125
126 b[0] = cpu_to_be16(val);
127}
128
Mark Brown7d5e5252012-02-17 15:58:25 -0800129static void regmap_format_32(void *buf, unsigned int val)
130{
131 __be32 *b = buf;
132
133 b[0] = cpu_to_be32(val);
134}
135
Mark Brownb83a3132011-05-11 19:59:58 +0200136static unsigned int regmap_parse_8(void *buf)
137{
138 u8 *b = buf;
139
140 return b[0];
141}
142
143static unsigned int regmap_parse_16(void *buf)
144{
145 __be16 *b = buf;
146
147 b[0] = be16_to_cpu(b[0]);
148
149 return b[0];
150}
151
Mark Brown7d5e5252012-02-17 15:58:25 -0800152static unsigned int regmap_parse_32(void *buf)
153{
154 __be32 *b = buf;
155
156 b[0] = be32_to_cpu(b[0]);
157
158 return b[0];
159}
160
Mark Brownb83a3132011-05-11 19:59:58 +0200161/**
162 * regmap_init(): Initialise register map
163 *
164 * @dev: Device that will be interacted with
165 * @bus: Bus-specific callbacks to use with device
166 * @config: Configuration for register map
167 *
168 * The return value will be an ERR_PTR() on error or a valid pointer to
169 * a struct regmap. This function should generally not be called
170 * directly, it should be called by bus-specific init functions.
171 */
172struct regmap *regmap_init(struct device *dev,
173 const struct regmap_bus *bus,
174 const struct regmap_config *config)
175{
176 struct regmap *map;
177 int ret = -EINVAL;
178
179 if (!bus || !config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100180 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200181
182 map = kzalloc(sizeof(*map), GFP_KERNEL);
183 if (map == NULL) {
184 ret = -ENOMEM;
185 goto err;
186 }
187
188 mutex_init(&map->lock);
189 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100190 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000191 map->format.pad_bytes = config->pad_bits / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100192 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000193 map->format.buf_size += map->format.pad_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +0200194 map->dev = dev;
195 map->bus = bus;
Mark Brown2e2ae662011-07-20 22:33:39 +0100196 map->max_register = config->max_register;
197 map->writeable_reg = config->writeable_reg;
198 map->readable_reg = config->readable_reg;
199 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900200 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100201 map->cache_type = config->cache_type;
Mark Brownb83a3132011-05-11 19:59:58 +0200202
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200203 if (config->read_flag_mask || config->write_flag_mask) {
204 map->read_flag_mask = config->read_flag_mask;
205 map->write_flag_mask = config->write_flag_mask;
206 } else {
207 map->read_flag_mask = bus->read_flag_mask;
208 }
209
Mark Brownb83a3132011-05-11 19:59:58 +0200210 switch (config->reg_bits) {
Wolfram Sang9aa50752012-01-27 16:10:22 +0100211 case 2:
212 switch (config->val_bits) {
213 case 6:
214 map->format.format_write = regmap_format_2_6_write;
215 break;
216 default:
217 goto err_map;
218 }
219 break;
220
Mark Brownb83a3132011-05-11 19:59:58 +0200221 case 4:
222 switch (config->val_bits) {
223 case 12:
224 map->format.format_write = regmap_format_4_12_write;
225 break;
226 default:
227 goto err_map;
228 }
229 break;
230
231 case 7:
232 switch (config->val_bits) {
233 case 9:
234 map->format.format_write = regmap_format_7_9_write;
235 break;
236 default:
237 goto err_map;
238 }
239 break;
240
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100241 case 10:
242 switch (config->val_bits) {
243 case 14:
244 map->format.format_write = regmap_format_10_14_write;
245 break;
246 default:
247 goto err_map;
248 }
249 break;
250
Mark Brownb83a3132011-05-11 19:59:58 +0200251 case 8:
252 map->format.format_reg = regmap_format_8;
253 break;
254
255 case 16:
256 map->format.format_reg = regmap_format_16;
257 break;
258
Mark Brown7d5e5252012-02-17 15:58:25 -0800259 case 32:
260 map->format.format_reg = regmap_format_32;
261 break;
262
Mark Brownb83a3132011-05-11 19:59:58 +0200263 default:
264 goto err_map;
265 }
266
267 switch (config->val_bits) {
268 case 8:
269 map->format.format_val = regmap_format_8;
270 map->format.parse_val = regmap_parse_8;
271 break;
272 case 16:
273 map->format.format_val = regmap_format_16;
274 map->format.parse_val = regmap_parse_16;
275 break;
Mark Brown7d5e5252012-02-17 15:58:25 -0800276 case 32:
277 map->format.format_val = regmap_format_32;
278 map->format.parse_val = regmap_parse_32;
279 break;
Mark Brownb83a3132011-05-11 19:59:58 +0200280 }
281
282 if (!map->format.format_write &&
283 !(map->format.format_reg && map->format.format_val))
284 goto err_map;
285
Mark Brown82159ba2012-01-18 10:52:25 +0000286 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200287 if (map->work_buf == NULL) {
288 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700289 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200290 }
291
Stephen Warrend3c242e2012-04-04 15:48:29 -0600292 regmap_debugfs_init(map, config->name);
Mark Brown052d2cd2011-11-21 19:05:13 +0000293
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100294 ret = regcache_init(map, config);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100295 if (ret < 0)
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100296 goto err_free_workbuf;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100297
Mark Brownb83a3132011-05-11 19:59:58 +0200298 return map;
299
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100300err_free_workbuf:
301 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200302err_map:
303 kfree(map);
304err:
305 return ERR_PTR(ret);
306}
307EXPORT_SYMBOL_GPL(regmap_init);
308
Mark Brownc0eb4672012-01-30 19:56:52 +0000309static void devm_regmap_release(struct device *dev, void *res)
310{
311 regmap_exit(*(struct regmap **)res);
312}
313
314/**
315 * devm_regmap_init(): Initialise managed register map
316 *
317 * @dev: Device that will be interacted with
318 * @bus: Bus-specific callbacks to use with device
319 * @config: Configuration for register map
320 *
321 * The return value will be an ERR_PTR() on error or a valid pointer
322 * to a struct regmap. This function should generally not be called
323 * directly, it should be called by bus-specific init functions. The
324 * map will be automatically freed by the device management code.
325 */
326struct regmap *devm_regmap_init(struct device *dev,
327 const struct regmap_bus *bus,
328 const struct regmap_config *config)
329{
330 struct regmap **ptr, *regmap;
331
332 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
333 if (!ptr)
334 return ERR_PTR(-ENOMEM);
335
336 regmap = regmap_init(dev, bus, config);
337 if (!IS_ERR(regmap)) {
338 *ptr = regmap;
339 devres_add(dev, ptr);
340 } else {
341 devres_free(ptr);
342 }
343
344 return regmap;
345}
346EXPORT_SYMBOL_GPL(devm_regmap_init);
347
Mark Brownb83a3132011-05-11 19:59:58 +0200348/**
Mark Brownbf315172011-12-03 17:06:20 +0000349 * regmap_reinit_cache(): Reinitialise the current register cache
350 *
351 * @map: Register map to operate on.
352 * @config: New configuration. Only the cache data will be used.
353 *
354 * Discard any existing register cache for the map and initialize a
355 * new cache. This can be used to restore the cache to defaults or to
356 * update the cache configuration to reflect runtime discovery of the
357 * hardware.
358 */
359int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
360{
361 int ret;
362
363 mutex_lock(&map->lock);
364
365 regcache_exit(map);
Mark Browna24f64a2012-01-26 18:30:16 +0000366 regmap_debugfs_exit(map);
Mark Brownbf315172011-12-03 17:06:20 +0000367
368 map->max_register = config->max_register;
369 map->writeable_reg = config->writeable_reg;
370 map->readable_reg = config->readable_reg;
371 map->volatile_reg = config->volatile_reg;
372 map->precious_reg = config->precious_reg;
373 map->cache_type = config->cache_type;
374
Stephen Warrend3c242e2012-04-04 15:48:29 -0600375 regmap_debugfs_init(map, config->name);
Mark Browna24f64a2012-01-26 18:30:16 +0000376
Mark Brown421e8d22012-01-20 13:39:37 +0000377 map->cache_bypass = false;
378 map->cache_only = false;
379
Mark Brownbf315172011-12-03 17:06:20 +0000380 ret = regcache_init(map, config);
381
382 mutex_unlock(&map->lock);
383
384 return ret;
385}
386
387/**
Mark Brownb83a3132011-05-11 19:59:58 +0200388 * regmap_exit(): Free a previously allocated register map
389 */
390void regmap_exit(struct regmap *map)
391{
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100392 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100393 regmap_debugfs_exit(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200394 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200395 kfree(map);
396}
397EXPORT_SYMBOL_GPL(regmap_exit);
398
399static int _regmap_raw_write(struct regmap *map, unsigned int reg,
400 const void *val, size_t val_len)
401{
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200402 u8 *u8 = map->work_buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200403 void *buf;
404 int ret = -ENOTSUPP;
405 size_t len;
Mark Brown73304782011-07-24 11:46:20 +0100406 int i;
407
408 /* Check for unwritable registers before we start */
409 if (map->writeable_reg)
410 for (i = 0; i < val_len / map->format.val_bytes; i++)
411 if (!map->writeable_reg(map->dev, reg + i))
412 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +0200413
Laxman Dewanganc9157192012-02-10 21:30:27 +0530414 if (!map->cache_bypass && map->format.parse_val) {
415 unsigned int ival;
416 int val_bytes = map->format.val_bytes;
417 for (i = 0; i < val_len / val_bytes; i++) {
418 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
419 ival = map->format.parse_val(map->work_buf);
420 ret = regcache_write(map, reg + i, ival);
421 if (ret) {
422 dev_err(map->dev,
423 "Error in caching of register: %u ret: %d\n",
424 reg + i, ret);
425 return ret;
426 }
427 }
428 if (map->cache_only) {
429 map->cache_dirty = true;
430 return 0;
431 }
432 }
433
Mark Brownb83a3132011-05-11 19:59:58 +0200434 map->format.format_reg(map->work_buf, reg);
435
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200436 u8[0] |= map->write_flag_mask;
437
Mark Brownfb2736b2011-07-24 21:30:55 +0100438 trace_regmap_hw_write_start(map->dev, reg,
439 val_len / map->format.val_bytes);
440
Mark Brown2547e202011-07-20 21:47:22 +0100441 /* If we're doing a single register write we can probably just
442 * send the work_buf directly, otherwise try to do a gather
443 * write.
444 */
Mark Brown82159ba2012-01-18 10:52:25 +0000445 if (val == (map->work_buf + map->format.pad_bytes +
446 map->format.reg_bytes))
Mark Brown2547e202011-07-20 21:47:22 +0100447 ret = map->bus->write(map->dev, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000448 map->format.reg_bytes +
449 map->format.pad_bytes +
450 val_len);
Mark Brown2547e202011-07-20 21:47:22 +0100451 else if (map->bus->gather_write)
Mark Brownb83a3132011-05-11 19:59:58 +0200452 ret = map->bus->gather_write(map->dev, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000453 map->format.reg_bytes +
454 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +0200455 val, val_len);
456
Mark Brown2547e202011-07-20 21:47:22 +0100457 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +0200458 if (ret == -ENOTSUPP) {
Mark Brown82159ba2012-01-18 10:52:25 +0000459 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
460 buf = kzalloc(len, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200461 if (!buf)
462 return -ENOMEM;
463
464 memcpy(buf, map->work_buf, map->format.reg_bytes);
Mark Brown82159ba2012-01-18 10:52:25 +0000465 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
466 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200467 ret = map->bus->write(map->dev, buf, len);
468
469 kfree(buf);
470 }
471
Mark Brownfb2736b2011-07-24 21:30:55 +0100472 trace_regmap_hw_write_done(map->dev, reg,
473 val_len / map->format.val_bytes);
474
Mark Brownb83a3132011-05-11 19:59:58 +0200475 return ret;
476}
477
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +0100478int _regmap_write(struct regmap *map, unsigned int reg,
479 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +0200480{
Mark Brownfb2736b2011-07-24 21:30:55 +0100481 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200482 BUG_ON(!map->format.format_write && !map->format.format_val);
483
Laxman Dewanganc9157192012-02-10 21:30:27 +0530484 if (!map->cache_bypass && map->format.format_write) {
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100485 ret = regcache_write(map, reg, val);
486 if (ret != 0)
487 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200488 if (map->cache_only) {
489 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100490 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200491 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100492 }
493
Mark Brownfb2736b2011-07-24 21:30:55 +0100494 trace_regmap_reg_write(map->dev, reg, val);
495
Mark Brownb83a3132011-05-11 19:59:58 +0200496 if (map->format.format_write) {
497 map->format.format_write(map, reg, val);
498
Mark Brownfb2736b2011-07-24 21:30:55 +0100499 trace_regmap_hw_write_start(map->dev, reg, 1);
500
501 ret = map->bus->write(map->dev, map->work_buf,
502 map->format.buf_size);
503
504 trace_regmap_hw_write_done(map->dev, reg, 1);
505
506 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200507 } else {
Mark Brown82159ba2012-01-18 10:52:25 +0000508 map->format.format_val(map->work_buf + map->format.reg_bytes
509 + map->format.pad_bytes, val);
Mark Brownb83a3132011-05-11 19:59:58 +0200510 return _regmap_raw_write(map, reg,
Mark Brown82159ba2012-01-18 10:52:25 +0000511 map->work_buf +
512 map->format.reg_bytes +
513 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +0200514 map->format.val_bytes);
515 }
516}
517
518/**
519 * regmap_write(): Write a value to a single register
520 *
521 * @map: Register map to write to
522 * @reg: Register to write to
523 * @val: Value to be written
524 *
525 * A value of zero will be returned on success, a negative errno will
526 * be returned in error cases.
527 */
528int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
529{
530 int ret;
531
532 mutex_lock(&map->lock);
533
534 ret = _regmap_write(map, reg, val);
535
536 mutex_unlock(&map->lock);
537
538 return ret;
539}
540EXPORT_SYMBOL_GPL(regmap_write);
541
542/**
543 * regmap_raw_write(): Write raw values to one or more registers
544 *
545 * @map: Register map to write to
546 * @reg: Initial register to write to
547 * @val: Block of data to be written, laid out for direct transmission to the
548 * device
549 * @val_len: Length of data pointed to by val.
550 *
551 * This function is intended to be used for things like firmware
552 * download where a large block of data needs to be transferred to the
553 * device. No formatting will be done on the data provided.
554 *
555 * A value of zero will be returned on success, a negative errno will
556 * be returned in error cases.
557 */
558int regmap_raw_write(struct regmap *map, unsigned int reg,
559 const void *val, size_t val_len)
560{
561 int ret;
562
563 mutex_lock(&map->lock);
564
565 ret = _regmap_raw_write(map, reg, val, val_len);
566
567 mutex_unlock(&map->lock);
568
569 return ret;
570}
571EXPORT_SYMBOL_GPL(regmap_raw_write);
572
Laxman Dewangan8eaeb212012-02-12 19:49:43 +0530573/*
574 * regmap_bulk_write(): Write multiple registers to the device
575 *
576 * @map: Register map to write to
577 * @reg: First register to be write from
578 * @val: Block of data to be written, in native register size for device
579 * @val_count: Number of registers to write
580 *
581 * This function is intended to be used for writing a large block of
582 * data to be device either in single transfer or multiple transfer.
583 *
584 * A value of zero will be returned on success, a negative errno will
585 * be returned in error cases.
586 */
587int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
588 size_t val_count)
589{
590 int ret = 0, i;
591 size_t val_bytes = map->format.val_bytes;
592 void *wval;
593
594 if (!map->format.parse_val)
595 return -EINVAL;
596
597 mutex_lock(&map->lock);
598
599 /* No formatting is require if val_byte is 1 */
600 if (val_bytes == 1) {
601 wval = (void *)val;
602 } else {
603 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
604 if (!wval) {
605 ret = -ENOMEM;
606 dev_err(map->dev, "Error in memory allocation\n");
607 goto out;
608 }
609 for (i = 0; i < val_count * val_bytes; i += val_bytes)
610 map->format.parse_val(wval + i);
611 }
612 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
613
614 if (val_bytes != 1)
615 kfree(wval);
616
617out:
618 mutex_unlock(&map->lock);
619 return ret;
620}
621EXPORT_SYMBOL_GPL(regmap_bulk_write);
622
Mark Brownb83a3132011-05-11 19:59:58 +0200623static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
624 unsigned int val_len)
625{
626 u8 *u8 = map->work_buf;
627 int ret;
628
629 map->format.format_reg(map->work_buf, reg);
630
631 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200632 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +0200633 * register addresss; since it's always the high bits for all
634 * current formats we can do this here rather than in
635 * formatting. This may break if we get interesting formats.
636 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200637 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200638
Mark Brownfb2736b2011-07-24 21:30:55 +0100639 trace_regmap_hw_read_start(map->dev, reg,
640 val_len / map->format.val_bytes);
641
Mark Brown82159ba2012-01-18 10:52:25 +0000642 ret = map->bus->read(map->dev, map->work_buf,
643 map->format.reg_bytes + map->format.pad_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +0100644 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200645
Mark Brownfb2736b2011-07-24 21:30:55 +0100646 trace_regmap_hw_read_done(map->dev, reg,
647 val_len / map->format.val_bytes);
648
649 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200650}
651
652static int _regmap_read(struct regmap *map, unsigned int reg,
653 unsigned int *val)
654{
655 int ret;
656
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100657 if (!map->cache_bypass) {
658 ret = regcache_read(map, reg, val);
659 if (ret == 0)
660 return 0;
661 }
662
Lars-Peter Clausen19254412011-11-16 16:28:19 +0100663 if (!map->format.parse_val)
664 return -EINVAL;
665
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100666 if (map->cache_only)
667 return -EBUSY;
668
Mark Brownb83a3132011-05-11 19:59:58 +0200669 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +0100670 if (ret == 0) {
Mark Brownb83a3132011-05-11 19:59:58 +0200671 *val = map->format.parse_val(map->work_buf);
Mark Brownfb2736b2011-07-24 21:30:55 +0100672 trace_regmap_reg_read(map->dev, reg, *val);
673 }
Mark Brownb83a3132011-05-11 19:59:58 +0200674
675 return ret;
676}
677
678/**
679 * regmap_read(): Read a value from a single register
680 *
681 * @map: Register map to write to
682 * @reg: Register to be read from
683 * @val: Pointer to store read value
684 *
685 * A value of zero will be returned on success, a negative errno will
686 * be returned in error cases.
687 */
688int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
689{
690 int ret;
691
692 mutex_lock(&map->lock);
693
694 ret = _regmap_read(map, reg, val);
695
696 mutex_unlock(&map->lock);
697
698 return ret;
699}
700EXPORT_SYMBOL_GPL(regmap_read);
701
702/**
703 * regmap_raw_read(): Read raw data from the device
704 *
705 * @map: Register map to write to
706 * @reg: First register to be read from
707 * @val: Pointer to store read value
708 * @val_len: Size of data to read
709 *
710 * A value of zero will be returned on success, a negative errno will
711 * be returned in error cases.
712 */
713int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
714 size_t val_len)
715{
Mark Brownb8fb5ab2012-02-21 19:12:47 +0000716 size_t val_bytes = map->format.val_bytes;
717 size_t val_count = val_len / val_bytes;
718 unsigned int v;
719 int ret, i;
Mark Brown04e016a2011-10-09 13:35:43 +0100720
Mark Brownb83a3132011-05-11 19:59:58 +0200721 mutex_lock(&map->lock);
722
Mark Brownb8fb5ab2012-02-21 19:12:47 +0000723 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
724 map->cache_type == REGCACHE_NONE) {
725 /* Physical block read if there's no cache involved */
726 ret = _regmap_raw_read(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200727
Mark Brownb8fb5ab2012-02-21 19:12:47 +0000728 } else {
729 /* Otherwise go word by word for the cache; should be low
730 * cost as we expect to hit the cache.
731 */
732 for (i = 0; i < val_count; i++) {
733 ret = _regmap_read(map, reg + i, &v);
734 if (ret != 0)
735 goto out;
736
737 map->format.format_val(val + (i * val_bytes), v);
738 }
739 }
740
741 out:
Mark Brownb83a3132011-05-11 19:59:58 +0200742 mutex_unlock(&map->lock);
743
744 return ret;
745}
746EXPORT_SYMBOL_GPL(regmap_raw_read);
747
748/**
749 * regmap_bulk_read(): Read multiple registers from the device
750 *
751 * @map: Register map to write to
752 * @reg: First register to be read from
753 * @val: Pointer to store read value, in native register size for device
754 * @val_count: Number of registers to read
755 *
756 * A value of zero will be returned on success, a negative errno will
757 * be returned in error cases.
758 */
759int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
760 size_t val_count)
761{
762 int ret, i;
763 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100764 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100765
Mark Brownb83a3132011-05-11 19:59:58 +0200766 if (!map->format.parse_val)
767 return -EINVAL;
768
Mark Brownde2d8082011-10-10 13:24:52 +0100769 if (vol || map->cache_type == REGCACHE_NONE) {
770 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
771 if (ret != 0)
772 return ret;
773
774 for (i = 0; i < val_count * val_bytes; i += val_bytes)
775 map->format.parse_val(val + i);
776 } else {
777 for (i = 0; i < val_count; i++) {
778 ret = regmap_read(map, reg + i, val + (i * val_bytes));
779 if (ret != 0)
780 return ret;
781 }
782 }
Mark Brownb83a3132011-05-11 19:59:58 +0200783
784 return 0;
785}
786EXPORT_SYMBOL_GPL(regmap_bulk_read);
787
Mark Brown018690d2011-11-29 20:10:36 +0000788static int _regmap_update_bits(struct regmap *map, unsigned int reg,
789 unsigned int mask, unsigned int val,
790 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +0200791{
792 int ret;
Mark Brownd91e8db22011-11-18 16:03:50 +0000793 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +0200794
795 mutex_lock(&map->lock);
796
Mark Brownd91e8db22011-11-18 16:03:50 +0000797 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +0200798 if (ret != 0)
799 goto out;
800
Mark Brownd91e8db22011-11-18 16:03:50 +0000801 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200802 tmp |= val & mask;
803
Mark Brown018690d2011-11-29 20:10:36 +0000804 if (tmp != orig) {
Mark Brownd91e8db22011-11-18 16:03:50 +0000805 ret = _regmap_write(map, reg, tmp);
Mark Brown018690d2011-11-29 20:10:36 +0000806 *change = true;
807 } else {
808 *change = false;
809 }
Mark Brownb83a3132011-05-11 19:59:58 +0200810
811out:
812 mutex_unlock(&map->lock);
813
814 return ret;
815}
Mark Brown018690d2011-11-29 20:10:36 +0000816
817/**
818 * regmap_update_bits: Perform a read/modify/write cycle on the register map
819 *
820 * @map: Register map to update
821 * @reg: Register to update
822 * @mask: Bitmask to change
823 * @val: New value for bitmask
824 *
825 * Returns zero for success, a negative number on error.
826 */
827int regmap_update_bits(struct regmap *map, unsigned int reg,
828 unsigned int mask, unsigned int val)
829{
830 bool change;
831 return _regmap_update_bits(map, reg, mask, val, &change);
832}
Mark Brownb83a3132011-05-11 19:59:58 +0200833EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +0100834
Mark Brown018690d2011-11-29 20:10:36 +0000835/**
836 * regmap_update_bits_check: Perform a read/modify/write cycle on the
837 * register map and report if updated
838 *
839 * @map: Register map to update
840 * @reg: Register to update
841 * @mask: Bitmask to change
842 * @val: New value for bitmask
843 * @change: Boolean indicating if a write was done
844 *
845 * Returns zero for success, a negative number on error.
846 */
847int regmap_update_bits_check(struct regmap *map, unsigned int reg,
848 unsigned int mask, unsigned int val,
849 bool *change)
850{
851 return _regmap_update_bits(map, reg, mask, val, change);
852}
853EXPORT_SYMBOL_GPL(regmap_update_bits_check);
854
Mark Brown22f0d902012-01-21 12:01:14 +0000855/**
856 * regmap_register_patch: Register and apply register updates to be applied
857 * on device initialistion
858 *
859 * @map: Register map to apply updates to.
860 * @regs: Values to update.
861 * @num_regs: Number of entries in regs.
862 *
863 * Register a set of register updates to be applied to the device
864 * whenever the device registers are synchronised with the cache and
865 * apply them immediately. Typically this is used to apply
866 * corrections to be applied to the device defaults on startup, such
867 * as the updates some vendors provide to undocumented registers.
868 */
869int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
870 int num_regs)
871{
872 int i, ret;
873 bool bypass;
874
875 /* If needed the implementation can be extended to support this */
876 if (map->patch)
877 return -EBUSY;
878
879 mutex_lock(&map->lock);
880
881 bypass = map->cache_bypass;
882
883 map->cache_bypass = true;
884
885 /* Write out first; it's useful to apply even if we fail later. */
886 for (i = 0; i < num_regs; i++) {
887 ret = _regmap_write(map, regs[i].reg, regs[i].def);
888 if (ret != 0) {
889 dev_err(map->dev, "Failed to write %x = %x: %d\n",
890 regs[i].reg, regs[i].def, ret);
891 goto out;
892 }
893 }
894
Axel Lin2a14d7d92012-02-10 19:29:55 +0800895 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
Mark Brown22f0d902012-01-21 12:01:14 +0000896 if (map->patch != NULL) {
897 memcpy(map->patch, regs,
898 num_regs * sizeof(struct reg_default));
899 map->patch_regs = num_regs;
900 } else {
901 ret = -ENOMEM;
902 }
903
904out:
905 map->cache_bypass = bypass;
906
907 mutex_unlock(&map->lock);
908
909 return ret;
910}
911EXPORT_SYMBOL_GPL(regmap_register_patch);
912
Mark Browneae4b512012-03-14 13:15:03 +0000913/*
Mark Browna6539c32012-02-17 14:20:14 -0800914 * regmap_get_val_bytes(): Report the size of a register value
915 *
916 * Report the size of a register value, mainly intended to for use by
917 * generic infrastructure built on top of regmap.
918 */
919int regmap_get_val_bytes(struct regmap *map)
920{
921 if (map->format.format_write)
922 return -EINVAL;
923
924 return map->format.val_bytes;
925}
926EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
927
Mark Brown31244e32011-07-20 22:56:53 +0100928static int __init regmap_initcall(void)
929{
930 regmap_debugfs_initcall();
931
932 return 0;
933}
934postcore_initcall(regmap_initcall);