blob: 8ffce9bdb4819fc2576e2073dd186e510bd3839a [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
Marc Reilly89a89b52012-03-16 12:11:42 +1100129static void regmap_format_24(void *buf, unsigned int val)
130{
131 u8 *b = buf;
132
133 b[0] = val >> 16;
134 b[1] = val >> 8;
135 b[2] = val;
136}
137
Mark Brown7d5e5252012-02-17 15:58:25 -0800138static void regmap_format_32(void *buf, unsigned int val)
139{
140 __be32 *b = buf;
141
142 b[0] = cpu_to_be32(val);
143}
144
Mark Brownb83a3132011-05-11 19:59:58 +0200145static unsigned int regmap_parse_8(void *buf)
146{
147 u8 *b = buf;
148
149 return b[0];
150}
151
152static unsigned int regmap_parse_16(void *buf)
153{
154 __be16 *b = buf;
155
156 b[0] = be16_to_cpu(b[0]);
157
158 return b[0];
159}
160
Marc Reilly89a89b52012-03-16 12:11:42 +1100161static unsigned int regmap_parse_24(void *buf)
162{
163 u8 *b = buf;
164 unsigned int ret = b[2];
165 ret |= ((unsigned int)b[1]) << 8;
166 ret |= ((unsigned int)b[0]) << 16;
167
168 return ret;
169}
170
Mark Brown7d5e5252012-02-17 15:58:25 -0800171static unsigned int regmap_parse_32(void *buf)
172{
173 __be32 *b = buf;
174
175 b[0] = be32_to_cpu(b[0]);
176
177 return b[0];
178}
179
Mark Brownb83a3132011-05-11 19:59:58 +0200180/**
181 * regmap_init(): Initialise register map
182 *
183 * @dev: Device that will be interacted with
184 * @bus: Bus-specific callbacks to use with device
185 * @config: Configuration for register map
186 *
187 * The return value will be an ERR_PTR() on error or a valid pointer to
188 * a struct regmap. This function should generally not be called
189 * directly, it should be called by bus-specific init functions.
190 */
191struct regmap *regmap_init(struct device *dev,
192 const struct regmap_bus *bus,
193 const struct regmap_config *config)
194{
195 struct regmap *map;
196 int ret = -EINVAL;
197
198 if (!bus || !config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100199 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200200
201 map = kzalloc(sizeof(*map), GFP_KERNEL);
202 if (map == NULL) {
203 ret = -ENOMEM;
204 goto err;
205 }
206
207 mutex_init(&map->lock);
208 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100209 map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000210 map->format.pad_bytes = config->pad_bits / 8;
Wolfram Sangc212acc2012-01-28 02:16:41 +0100211 map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
Mark Brown82159ba2012-01-18 10:52:25 +0000212 map->format.buf_size += map->format.pad_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +0200213 map->dev = dev;
214 map->bus = bus;
Mark Brown2e2ae662011-07-20 22:33:39 +0100215 map->max_register = config->max_register;
216 map->writeable_reg = config->writeable_reg;
217 map->readable_reg = config->readable_reg;
218 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900219 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100220 map->cache_type = config->cache_type;
Mark Brownb83a3132011-05-11 19:59:58 +0200221
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200222 if (config->read_flag_mask || config->write_flag_mask) {
223 map->read_flag_mask = config->read_flag_mask;
224 map->write_flag_mask = config->write_flag_mask;
225 } else {
226 map->read_flag_mask = bus->read_flag_mask;
227 }
228
Mark Brownb83a3132011-05-11 19:59:58 +0200229 switch (config->reg_bits) {
Wolfram Sang9aa50752012-01-27 16:10:22 +0100230 case 2:
231 switch (config->val_bits) {
232 case 6:
233 map->format.format_write = regmap_format_2_6_write;
234 break;
235 default:
236 goto err_map;
237 }
238 break;
239
Mark Brownb83a3132011-05-11 19:59:58 +0200240 case 4:
241 switch (config->val_bits) {
242 case 12:
243 map->format.format_write = regmap_format_4_12_write;
244 break;
245 default:
246 goto err_map;
247 }
248 break;
249
250 case 7:
251 switch (config->val_bits) {
252 case 9:
253 map->format.format_write = regmap_format_7_9_write;
254 break;
255 default:
256 goto err_map;
257 }
258 break;
259
Lars-Peter Clausen7e5ec632011-11-16 16:28:21 +0100260 case 10:
261 switch (config->val_bits) {
262 case 14:
263 map->format.format_write = regmap_format_10_14_write;
264 break;
265 default:
266 goto err_map;
267 }
268 break;
269
Mark Brownb83a3132011-05-11 19:59:58 +0200270 case 8:
271 map->format.format_reg = regmap_format_8;
272 break;
273
274 case 16:
275 map->format.format_reg = regmap_format_16;
276 break;
277
Mark Brown7d5e5252012-02-17 15:58:25 -0800278 case 32:
279 map->format.format_reg = regmap_format_32;
280 break;
281
Mark Brownb83a3132011-05-11 19:59:58 +0200282 default:
283 goto err_map;
284 }
285
286 switch (config->val_bits) {
287 case 8:
288 map->format.format_val = regmap_format_8;
289 map->format.parse_val = regmap_parse_8;
290 break;
291 case 16:
292 map->format.format_val = regmap_format_16;
293 map->format.parse_val = regmap_parse_16;
294 break;
Marc Reilly89a89b52012-03-16 12:11:42 +1100295 case 24:
296 map->format.format_val = regmap_format_24;
297 map->format.parse_val = regmap_parse_24;
298 break;
Mark Brown7d5e5252012-02-17 15:58:25 -0800299 case 32:
300 map->format.format_val = regmap_format_32;
301 map->format.parse_val = regmap_parse_32;
302 break;
Mark Brownb83a3132011-05-11 19:59:58 +0200303 }
304
305 if (!map->format.format_write &&
306 !(map->format.format_reg && map->format.format_val))
307 goto err_map;
308
Mark Brown82159ba2012-01-18 10:52:25 +0000309 map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200310 if (map->work_buf == NULL) {
311 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700312 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200313 }
314
Mark Brown052d2cd2011-11-21 19:05:13 +0000315 regmap_debugfs_init(map);
316
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100317 ret = regcache_init(map, config);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100318 if (ret < 0)
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100319 goto err_free_workbuf;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100320
Mark Brownb83a3132011-05-11 19:59:58 +0200321 return map;
322
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100323err_free_workbuf:
324 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200325err_map:
326 kfree(map);
327err:
328 return ERR_PTR(ret);
329}
330EXPORT_SYMBOL_GPL(regmap_init);
331
Mark Brownc0eb4672012-01-30 19:56:52 +0000332static void devm_regmap_release(struct device *dev, void *res)
333{
334 regmap_exit(*(struct regmap **)res);
335}
336
337/**
338 * devm_regmap_init(): Initialise managed register map
339 *
340 * @dev: Device that will be interacted with
341 * @bus: Bus-specific callbacks to use with device
342 * @config: Configuration for register map
343 *
344 * The return value will be an ERR_PTR() on error or a valid pointer
345 * to a struct regmap. This function should generally not be called
346 * directly, it should be called by bus-specific init functions. The
347 * map will be automatically freed by the device management code.
348 */
349struct regmap *devm_regmap_init(struct device *dev,
350 const struct regmap_bus *bus,
351 const struct regmap_config *config)
352{
353 struct regmap **ptr, *regmap;
354
355 ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
356 if (!ptr)
357 return ERR_PTR(-ENOMEM);
358
359 regmap = regmap_init(dev, bus, config);
360 if (!IS_ERR(regmap)) {
361 *ptr = regmap;
362 devres_add(dev, ptr);
363 } else {
364 devres_free(ptr);
365 }
366
367 return regmap;
368}
369EXPORT_SYMBOL_GPL(devm_regmap_init);
370
Mark Brownb83a3132011-05-11 19:59:58 +0200371/**
Mark Brownbf315172011-12-03 17:06:20 +0000372 * regmap_reinit_cache(): Reinitialise the current register cache
373 *
374 * @map: Register map to operate on.
375 * @config: New configuration. Only the cache data will be used.
376 *
377 * Discard any existing register cache for the map and initialize a
378 * new cache. This can be used to restore the cache to defaults or to
379 * update the cache configuration to reflect runtime discovery of the
380 * hardware.
381 */
382int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
383{
384 int ret;
385
386 mutex_lock(&map->lock);
387
388 regcache_exit(map);
Mark Browna24f64a2012-01-26 18:30:16 +0000389 regmap_debugfs_exit(map);
Mark Brownbf315172011-12-03 17:06:20 +0000390
391 map->max_register = config->max_register;
392 map->writeable_reg = config->writeable_reg;
393 map->readable_reg = config->readable_reg;
394 map->volatile_reg = config->volatile_reg;
395 map->precious_reg = config->precious_reg;
396 map->cache_type = config->cache_type;
397
Mark Browna24f64a2012-01-26 18:30:16 +0000398 regmap_debugfs_init(map);
399
Mark Brown421e8d22012-01-20 13:39:37 +0000400 map->cache_bypass = false;
401 map->cache_only = false;
402
Mark Brownbf315172011-12-03 17:06:20 +0000403 ret = regcache_init(map, config);
404
405 mutex_unlock(&map->lock);
406
407 return ret;
408}
409
410/**
Mark Brownb83a3132011-05-11 19:59:58 +0200411 * regmap_exit(): Free a previously allocated register map
412 */
413void regmap_exit(struct regmap *map)
414{
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100415 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100416 regmap_debugfs_exit(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200417 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200418 kfree(map);
419}
420EXPORT_SYMBOL_GPL(regmap_exit);
421
422static int _regmap_raw_write(struct regmap *map, unsigned int reg,
423 const void *val, size_t val_len)
424{
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200425 u8 *u8 = map->work_buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200426 void *buf;
427 int ret = -ENOTSUPP;
428 size_t len;
Mark Brown73304782011-07-24 11:46:20 +0100429 int i;
430
431 /* Check for unwritable registers before we start */
432 if (map->writeable_reg)
433 for (i = 0; i < val_len / map->format.val_bytes; i++)
434 if (!map->writeable_reg(map->dev, reg + i))
435 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +0200436
Laxman Dewanganc9157192012-02-10 21:30:27 +0530437 if (!map->cache_bypass && map->format.parse_val) {
438 unsigned int ival;
439 int val_bytes = map->format.val_bytes;
440 for (i = 0; i < val_len / val_bytes; i++) {
441 memcpy(map->work_buf, val + (i * val_bytes), val_bytes);
442 ival = map->format.parse_val(map->work_buf);
443 ret = regcache_write(map, reg + i, ival);
444 if (ret) {
445 dev_err(map->dev,
446 "Error in caching of register: %u ret: %d\n",
447 reg + i, ret);
448 return ret;
449 }
450 }
451 if (map->cache_only) {
452 map->cache_dirty = true;
453 return 0;
454 }
455 }
456
Mark Brownb83a3132011-05-11 19:59:58 +0200457 map->format.format_reg(map->work_buf, reg);
458
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200459 u8[0] |= map->write_flag_mask;
460
Mark Brownfb2736b2011-07-24 21:30:55 +0100461 trace_regmap_hw_write_start(map->dev, reg,
462 val_len / map->format.val_bytes);
463
Mark Brown2547e202011-07-20 21:47:22 +0100464 /* If we're doing a single register write we can probably just
465 * send the work_buf directly, otherwise try to do a gather
466 * write.
467 */
Mark Brown82159ba2012-01-18 10:52:25 +0000468 if (val == (map->work_buf + map->format.pad_bytes +
469 map->format.reg_bytes))
Mark Brown2547e202011-07-20 21:47:22 +0100470 ret = map->bus->write(map->dev, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000471 map->format.reg_bytes +
472 map->format.pad_bytes +
473 val_len);
Mark Brown2547e202011-07-20 21:47:22 +0100474 else if (map->bus->gather_write)
Mark Brownb83a3132011-05-11 19:59:58 +0200475 ret = map->bus->gather_write(map->dev, map->work_buf,
Mark Brown82159ba2012-01-18 10:52:25 +0000476 map->format.reg_bytes +
477 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +0200478 val, val_len);
479
Mark Brown2547e202011-07-20 21:47:22 +0100480 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +0200481 if (ret == -ENOTSUPP) {
Mark Brown82159ba2012-01-18 10:52:25 +0000482 len = map->format.reg_bytes + map->format.pad_bytes + val_len;
483 buf = kzalloc(len, GFP_KERNEL);
Mark Brownb83a3132011-05-11 19:59:58 +0200484 if (!buf)
485 return -ENOMEM;
486
487 memcpy(buf, map->work_buf, map->format.reg_bytes);
Mark Brown82159ba2012-01-18 10:52:25 +0000488 memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
489 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200490 ret = map->bus->write(map->dev, buf, len);
491
492 kfree(buf);
493 }
494
Mark Brownfb2736b2011-07-24 21:30:55 +0100495 trace_regmap_hw_write_done(map->dev, reg,
496 val_len / map->format.val_bytes);
497
Mark Brownb83a3132011-05-11 19:59:58 +0200498 return ret;
499}
500
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +0100501int _regmap_write(struct regmap *map, unsigned int reg,
502 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +0200503{
Mark Brownfb2736b2011-07-24 21:30:55 +0100504 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200505 BUG_ON(!map->format.format_write && !map->format.format_val);
506
Laxman Dewanganc9157192012-02-10 21:30:27 +0530507 if (!map->cache_bypass && map->format.format_write) {
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100508 ret = regcache_write(map, reg, val);
509 if (ret != 0)
510 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200511 if (map->cache_only) {
512 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100513 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200514 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100515 }
516
Mark Brownfb2736b2011-07-24 21:30:55 +0100517 trace_regmap_reg_write(map->dev, reg, val);
518
Mark Brownb83a3132011-05-11 19:59:58 +0200519 if (map->format.format_write) {
520 map->format.format_write(map, reg, val);
521
Mark Brownfb2736b2011-07-24 21:30:55 +0100522 trace_regmap_hw_write_start(map->dev, reg, 1);
523
524 ret = map->bus->write(map->dev, map->work_buf,
525 map->format.buf_size);
526
527 trace_regmap_hw_write_done(map->dev, reg, 1);
528
529 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200530 } else {
Mark Brown82159ba2012-01-18 10:52:25 +0000531 map->format.format_val(map->work_buf + map->format.reg_bytes
532 + map->format.pad_bytes, val);
Mark Brownb83a3132011-05-11 19:59:58 +0200533 return _regmap_raw_write(map, reg,
Mark Brown82159ba2012-01-18 10:52:25 +0000534 map->work_buf +
535 map->format.reg_bytes +
536 map->format.pad_bytes,
Mark Brownb83a3132011-05-11 19:59:58 +0200537 map->format.val_bytes);
538 }
539}
540
541/**
542 * regmap_write(): Write a value to a single register
543 *
544 * @map: Register map to write to
545 * @reg: Register to write to
546 * @val: Value to be written
547 *
548 * A value of zero will be returned on success, a negative errno will
549 * be returned in error cases.
550 */
551int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
552{
553 int ret;
554
555 mutex_lock(&map->lock);
556
557 ret = _regmap_write(map, reg, val);
558
559 mutex_unlock(&map->lock);
560
561 return ret;
562}
563EXPORT_SYMBOL_GPL(regmap_write);
564
565/**
566 * regmap_raw_write(): Write raw values to one or more registers
567 *
568 * @map: Register map to write to
569 * @reg: Initial register to write to
570 * @val: Block of data to be written, laid out for direct transmission to the
571 * device
572 * @val_len: Length of data pointed to by val.
573 *
574 * This function is intended to be used for things like firmware
575 * download where a large block of data needs to be transferred to the
576 * device. No formatting will be done on the data provided.
577 *
578 * A value of zero will be returned on success, a negative errno will
579 * be returned in error cases.
580 */
581int regmap_raw_write(struct regmap *map, unsigned int reg,
582 const void *val, size_t val_len)
583{
584 int ret;
585
586 mutex_lock(&map->lock);
587
588 ret = _regmap_raw_write(map, reg, val, val_len);
589
590 mutex_unlock(&map->lock);
591
592 return ret;
593}
594EXPORT_SYMBOL_GPL(regmap_raw_write);
595
Laxman Dewangan8eaeb212012-02-12 19:49:43 +0530596/*
597 * regmap_bulk_write(): Write multiple registers to the device
598 *
599 * @map: Register map to write to
600 * @reg: First register to be write from
601 * @val: Block of data to be written, in native register size for device
602 * @val_count: Number of registers to write
603 *
604 * This function is intended to be used for writing a large block of
605 * data to be device either in single transfer or multiple transfer.
606 *
607 * A value of zero will be returned on success, a negative errno will
608 * be returned in error cases.
609 */
610int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
611 size_t val_count)
612{
613 int ret = 0, i;
614 size_t val_bytes = map->format.val_bytes;
615 void *wval;
616
617 if (!map->format.parse_val)
618 return -EINVAL;
619
620 mutex_lock(&map->lock);
621
622 /* No formatting is require if val_byte is 1 */
623 if (val_bytes == 1) {
624 wval = (void *)val;
625 } else {
626 wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
627 if (!wval) {
628 ret = -ENOMEM;
629 dev_err(map->dev, "Error in memory allocation\n");
630 goto out;
631 }
632 for (i = 0; i < val_count * val_bytes; i += val_bytes)
633 map->format.parse_val(wval + i);
634 }
635 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
636
637 if (val_bytes != 1)
638 kfree(wval);
639
640out:
641 mutex_unlock(&map->lock);
642 return ret;
643}
644EXPORT_SYMBOL_GPL(regmap_bulk_write);
645
Mark Brownb83a3132011-05-11 19:59:58 +0200646static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
647 unsigned int val_len)
648{
649 u8 *u8 = map->work_buf;
650 int ret;
651
652 map->format.format_reg(map->work_buf, reg);
653
654 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200655 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +0200656 * register addresss; since it's always the high bits for all
657 * current formats we can do this here rather than in
658 * formatting. This may break if we get interesting formats.
659 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200660 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200661
Mark Brownfb2736b2011-07-24 21:30:55 +0100662 trace_regmap_hw_read_start(map->dev, reg,
663 val_len / map->format.val_bytes);
664
Mark Brown82159ba2012-01-18 10:52:25 +0000665 ret = map->bus->read(map->dev, map->work_buf,
666 map->format.reg_bytes + map->format.pad_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +0100667 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200668
Mark Brownfb2736b2011-07-24 21:30:55 +0100669 trace_regmap_hw_read_done(map->dev, reg,
670 val_len / map->format.val_bytes);
671
672 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200673}
674
675static int _regmap_read(struct regmap *map, unsigned int reg,
676 unsigned int *val)
677{
678 int ret;
679
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100680 if (!map->cache_bypass) {
681 ret = regcache_read(map, reg, val);
682 if (ret == 0)
683 return 0;
684 }
685
Lars-Peter Clausen19254412011-11-16 16:28:19 +0100686 if (!map->format.parse_val)
687 return -EINVAL;
688
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100689 if (map->cache_only)
690 return -EBUSY;
691
Mark Brownb83a3132011-05-11 19:59:58 +0200692 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +0100693 if (ret == 0) {
Mark Brownb83a3132011-05-11 19:59:58 +0200694 *val = map->format.parse_val(map->work_buf);
Mark Brownfb2736b2011-07-24 21:30:55 +0100695 trace_regmap_reg_read(map->dev, reg, *val);
696 }
Mark Brownb83a3132011-05-11 19:59:58 +0200697
698 return ret;
699}
700
701/**
702 * regmap_read(): Read a value from a single register
703 *
704 * @map: Register map to write to
705 * @reg: Register to be read from
706 * @val: Pointer to store read value
707 *
708 * A value of zero will be returned on success, a negative errno will
709 * be returned in error cases.
710 */
711int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
712{
713 int ret;
714
715 mutex_lock(&map->lock);
716
717 ret = _regmap_read(map, reg, val);
718
719 mutex_unlock(&map->lock);
720
721 return ret;
722}
723EXPORT_SYMBOL_GPL(regmap_read);
724
725/**
726 * regmap_raw_read(): Read raw data from the device
727 *
728 * @map: Register map to write to
729 * @reg: First register to be read from
730 * @val: Pointer to store read value
731 * @val_len: Size of data to read
732 *
733 * A value of zero will be returned on success, a negative errno will
734 * be returned in error cases.
735 */
736int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
737 size_t val_len)
738{
Mark Brownb8fb5ab2012-02-21 19:12:47 +0000739 size_t val_bytes = map->format.val_bytes;
740 size_t val_count = val_len / val_bytes;
741 unsigned int v;
742 int ret, i;
Mark Brown04e016a2011-10-09 13:35:43 +0100743
Mark Brownb83a3132011-05-11 19:59:58 +0200744 mutex_lock(&map->lock);
745
Mark Brownb8fb5ab2012-02-21 19:12:47 +0000746 if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
747 map->cache_type == REGCACHE_NONE) {
748 /* Physical block read if there's no cache involved */
749 ret = _regmap_raw_read(map, reg, val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200750
Mark Brownb8fb5ab2012-02-21 19:12:47 +0000751 } else {
752 /* Otherwise go word by word for the cache; should be low
753 * cost as we expect to hit the cache.
754 */
755 for (i = 0; i < val_count; i++) {
756 ret = _regmap_read(map, reg + i, &v);
757 if (ret != 0)
758 goto out;
759
760 map->format.format_val(val + (i * val_bytes), v);
761 }
762 }
763
764 out:
Mark Brownb83a3132011-05-11 19:59:58 +0200765 mutex_unlock(&map->lock);
766
767 return ret;
768}
769EXPORT_SYMBOL_GPL(regmap_raw_read);
770
771/**
772 * regmap_bulk_read(): Read multiple registers from the device
773 *
774 * @map: Register map to write to
775 * @reg: First register to be read from
776 * @val: Pointer to store read value, in native register size for device
777 * @val_count: Number of registers to read
778 *
779 * A value of zero will be returned on success, a negative errno will
780 * be returned in error cases.
781 */
782int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
783 size_t val_count)
784{
785 int ret, i;
786 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100787 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100788
Mark Brownb83a3132011-05-11 19:59:58 +0200789 if (!map->format.parse_val)
790 return -EINVAL;
791
Mark Brownde2d8082011-10-10 13:24:52 +0100792 if (vol || map->cache_type == REGCACHE_NONE) {
793 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
794 if (ret != 0)
795 return ret;
796
797 for (i = 0; i < val_count * val_bytes; i += val_bytes)
798 map->format.parse_val(val + i);
799 } else {
800 for (i = 0; i < val_count; i++) {
801 ret = regmap_read(map, reg + i, val + (i * val_bytes));
802 if (ret != 0)
803 return ret;
804 }
805 }
Mark Brownb83a3132011-05-11 19:59:58 +0200806
807 return 0;
808}
809EXPORT_SYMBOL_GPL(regmap_bulk_read);
810
Mark Brown018690d2011-11-29 20:10:36 +0000811static int _regmap_update_bits(struct regmap *map, unsigned int reg,
812 unsigned int mask, unsigned int val,
813 bool *change)
Mark Brownb83a3132011-05-11 19:59:58 +0200814{
815 int ret;
Mark Brownd91e8db22011-11-18 16:03:50 +0000816 unsigned int tmp, orig;
Mark Brownb83a3132011-05-11 19:59:58 +0200817
818 mutex_lock(&map->lock);
819
Mark Brownd91e8db22011-11-18 16:03:50 +0000820 ret = _regmap_read(map, reg, &orig);
Mark Brownb83a3132011-05-11 19:59:58 +0200821 if (ret != 0)
822 goto out;
823
Mark Brownd91e8db22011-11-18 16:03:50 +0000824 tmp = orig & ~mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200825 tmp |= val & mask;
826
Mark Brown018690d2011-11-29 20:10:36 +0000827 if (tmp != orig) {
Mark Brownd91e8db22011-11-18 16:03:50 +0000828 ret = _regmap_write(map, reg, tmp);
Mark Brown018690d2011-11-29 20:10:36 +0000829 *change = true;
830 } else {
831 *change = false;
832 }
Mark Brownb83a3132011-05-11 19:59:58 +0200833
834out:
835 mutex_unlock(&map->lock);
836
837 return ret;
838}
Mark Brown018690d2011-11-29 20:10:36 +0000839
840/**
841 * regmap_update_bits: Perform a read/modify/write cycle on the register map
842 *
843 * @map: Register map to update
844 * @reg: Register to update
845 * @mask: Bitmask to change
846 * @val: New value for bitmask
847 *
848 * Returns zero for success, a negative number on error.
849 */
850int regmap_update_bits(struct regmap *map, unsigned int reg,
851 unsigned int mask, unsigned int val)
852{
853 bool change;
854 return _regmap_update_bits(map, reg, mask, val, &change);
855}
Mark Brownb83a3132011-05-11 19:59:58 +0200856EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +0100857
Mark Brown018690d2011-11-29 20:10:36 +0000858/**
859 * regmap_update_bits_check: Perform a read/modify/write cycle on the
860 * register map and report if updated
861 *
862 * @map: Register map to update
863 * @reg: Register to update
864 * @mask: Bitmask to change
865 * @val: New value for bitmask
866 * @change: Boolean indicating if a write was done
867 *
868 * Returns zero for success, a negative number on error.
869 */
870int regmap_update_bits_check(struct regmap *map, unsigned int reg,
871 unsigned int mask, unsigned int val,
872 bool *change)
873{
874 return _regmap_update_bits(map, reg, mask, val, change);
875}
876EXPORT_SYMBOL_GPL(regmap_update_bits_check);
877
Mark Brown22f0d902012-01-21 12:01:14 +0000878/**
879 * regmap_register_patch: Register and apply register updates to be applied
880 * on device initialistion
881 *
882 * @map: Register map to apply updates to.
883 * @regs: Values to update.
884 * @num_regs: Number of entries in regs.
885 *
886 * Register a set of register updates to be applied to the device
887 * whenever the device registers are synchronised with the cache and
888 * apply them immediately. Typically this is used to apply
889 * corrections to be applied to the device defaults on startup, such
890 * as the updates some vendors provide to undocumented registers.
891 */
892int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
893 int num_regs)
894{
895 int i, ret;
896 bool bypass;
897
898 /* If needed the implementation can be extended to support this */
899 if (map->patch)
900 return -EBUSY;
901
902 mutex_lock(&map->lock);
903
904 bypass = map->cache_bypass;
905
906 map->cache_bypass = true;
907
908 /* Write out first; it's useful to apply even if we fail later. */
909 for (i = 0; i < num_regs; i++) {
910 ret = _regmap_write(map, regs[i].reg, regs[i].def);
911 if (ret != 0) {
912 dev_err(map->dev, "Failed to write %x = %x: %d\n",
913 regs[i].reg, regs[i].def, ret);
914 goto out;
915 }
916 }
917
Axel Lin2a14d7d92012-02-10 19:29:55 +0800918 map->patch = kcalloc(num_regs, sizeof(struct reg_default), GFP_KERNEL);
Mark Brown22f0d902012-01-21 12:01:14 +0000919 if (map->patch != NULL) {
920 memcpy(map->patch, regs,
921 num_regs * sizeof(struct reg_default));
922 map->patch_regs = num_regs;
923 } else {
924 ret = -ENOMEM;
925 }
926
927out:
928 map->cache_bypass = bypass;
929
930 mutex_unlock(&map->lock);
931
932 return ret;
933}
934EXPORT_SYMBOL_GPL(regmap_register_patch);
935
Mark Browneae4b512012-03-14 13:15:03 +0000936/*
Mark Browna6539c32012-02-17 14:20:14 -0800937 * regmap_get_val_bytes(): Report the size of a register value
938 *
939 * Report the size of a register value, mainly intended to for use by
940 * generic infrastructure built on top of regmap.
941 */
942int regmap_get_val_bytes(struct regmap *map)
943{
944 if (map->format.format_write)
945 return -EINVAL;
946
947 return map->format.val_bytes;
948}
949EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
950
Mark Brown31244e32011-07-20 22:56:53 +0100951static int __init regmap_initcall(void)
952{
953 regmap_debugfs_initcall();
954
955 return 0;
956}
957postcore_initcall(regmap_initcall);