blob: 3cf4785c3afece000e3109219c787927d8d2b874 [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
13#include <linux/slab.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/err.h>
17
Mark Brownfb2736b2011-07-24 21:30:55 +010018#define CREATE_TRACE_POINTS
19#include <trace/events/regmap.h>
20
Mark Brown93de9122011-07-20 22:35:37 +010021#include "internal.h"
Mark Brownb83a3132011-05-11 19:59:58 +020022
Mark Brown8de2f082011-08-10 17:14:41 +090023bool regmap_writeable(struct regmap *map, unsigned int reg)
24{
25 if (map->max_register && reg > map->max_register)
26 return false;
27
28 if (map->writeable_reg)
29 return map->writeable_reg(map->dev, reg);
30
31 return true;
32}
33
34bool regmap_readable(struct regmap *map, unsigned int reg)
35{
36 if (map->max_register && reg > map->max_register)
37 return false;
38
39 if (map->readable_reg)
40 return map->readable_reg(map->dev, reg);
41
42 return true;
43}
44
45bool regmap_volatile(struct regmap *map, unsigned int reg)
46{
47 if (map->max_register && reg > map->max_register)
48 return false;
49
50 if (map->volatile_reg)
51 return map->volatile_reg(map->dev, reg);
52
53 return true;
54}
55
56bool regmap_precious(struct regmap *map, unsigned int reg)
57{
58 if (map->max_register && reg > map->max_register)
59 return false;
60
61 if (map->precious_reg)
62 return map->precious_reg(map->dev, reg);
63
64 return false;
65}
66
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +010067static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
68 unsigned int num)
69{
70 unsigned int i;
71
72 for (i = 0; i < num; i++)
73 if (!regmap_volatile(map, reg + i))
74 return false;
75
76 return true;
77}
78
Mark Brownb83a3132011-05-11 19:59:58 +020079static void regmap_format_4_12_write(struct regmap *map,
80 unsigned int reg, unsigned int val)
81{
82 __be16 *out = map->work_buf;
83 *out = cpu_to_be16((reg << 12) | val);
84}
85
86static void regmap_format_7_9_write(struct regmap *map,
87 unsigned int reg, unsigned int val)
88{
89 __be16 *out = map->work_buf;
90 *out = cpu_to_be16((reg << 9) | val);
91}
92
93static void regmap_format_8(void *buf, unsigned int val)
94{
95 u8 *b = buf;
96
97 b[0] = val;
98}
99
100static void regmap_format_16(void *buf, unsigned int val)
101{
102 __be16 *b = buf;
103
104 b[0] = cpu_to_be16(val);
105}
106
107static unsigned int regmap_parse_8(void *buf)
108{
109 u8 *b = buf;
110
111 return b[0];
112}
113
114static unsigned int regmap_parse_16(void *buf)
115{
116 __be16 *b = buf;
117
118 b[0] = be16_to_cpu(b[0]);
119
120 return b[0];
121}
122
123/**
124 * regmap_init(): Initialise register map
125 *
126 * @dev: Device that will be interacted with
127 * @bus: Bus-specific callbacks to use with device
128 * @config: Configuration for register map
129 *
130 * The return value will be an ERR_PTR() on error or a valid pointer to
131 * a struct regmap. This function should generally not be called
132 * directly, it should be called by bus-specific init functions.
133 */
134struct regmap *regmap_init(struct device *dev,
135 const struct regmap_bus *bus,
136 const struct regmap_config *config)
137{
138 struct regmap *map;
139 int ret = -EINVAL;
140
141 if (!bus || !config)
Lars-Peter Clausenabbb18f2011-11-14 10:40:15 +0100142 goto err;
Mark Brownb83a3132011-05-11 19:59:58 +0200143
144 map = kzalloc(sizeof(*map), GFP_KERNEL);
145 if (map == NULL) {
146 ret = -ENOMEM;
147 goto err;
148 }
149
150 mutex_init(&map->lock);
151 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
152 map->format.reg_bytes = config->reg_bits / 8;
153 map->format.val_bytes = config->val_bits / 8;
154 map->dev = dev;
155 map->bus = bus;
Mark Brown2e2ae662011-07-20 22:33:39 +0100156 map->max_register = config->max_register;
157 map->writeable_reg = config->writeable_reg;
158 map->readable_reg = config->readable_reg;
159 map->volatile_reg = config->volatile_reg;
Mark Brown2efe1642011-08-08 15:41:46 +0900160 map->precious_reg = config->precious_reg;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100161 map->cache_type = config->cache_type;
Mark Brownb83a3132011-05-11 19:59:58 +0200162
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200163 if (config->read_flag_mask || config->write_flag_mask) {
164 map->read_flag_mask = config->read_flag_mask;
165 map->write_flag_mask = config->write_flag_mask;
166 } else {
167 map->read_flag_mask = bus->read_flag_mask;
168 }
169
Mark Brownb83a3132011-05-11 19:59:58 +0200170 switch (config->reg_bits) {
171 case 4:
172 switch (config->val_bits) {
173 case 12:
174 map->format.format_write = regmap_format_4_12_write;
175 break;
176 default:
177 goto err_map;
178 }
179 break;
180
181 case 7:
182 switch (config->val_bits) {
183 case 9:
184 map->format.format_write = regmap_format_7_9_write;
185 break;
186 default:
187 goto err_map;
188 }
189 break;
190
191 case 8:
192 map->format.format_reg = regmap_format_8;
193 break;
194
195 case 16:
196 map->format.format_reg = regmap_format_16;
197 break;
198
199 default:
200 goto err_map;
201 }
202
203 switch (config->val_bits) {
204 case 8:
205 map->format.format_val = regmap_format_8;
206 map->format.parse_val = regmap_parse_8;
207 break;
208 case 16:
209 map->format.format_val = regmap_format_16;
210 map->format.parse_val = regmap_parse_16;
211 break;
212 }
213
214 if (!map->format.format_write &&
215 !(map->format.format_reg && map->format.format_val))
216 goto err_map;
217
218 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
219 if (map->work_buf == NULL) {
220 ret = -ENOMEM;
Mark Brown5204f5e2011-09-05 08:07:47 -0700221 goto err_map;
Mark Brownb83a3132011-05-11 19:59:58 +0200222 }
223
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100224 ret = regcache_init(map, config);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100225 if (ret < 0)
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100226 goto err_free_workbuf;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100227
Mark Brown31244e32011-07-20 22:56:53 +0100228 regmap_debugfs_init(map);
229
Mark Brownb83a3132011-05-11 19:59:58 +0200230 return map;
231
Lars-Peter Clausen58072cb2011-11-10 18:15:15 +0100232err_free_workbuf:
233 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200234err_map:
235 kfree(map);
236err:
237 return ERR_PTR(ret);
238}
239EXPORT_SYMBOL_GPL(regmap_init);
240
241/**
242 * regmap_exit(): Free a previously allocated register map
243 */
244void regmap_exit(struct regmap *map)
245{
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100246 regcache_exit(map);
Mark Brown31244e32011-07-20 22:56:53 +0100247 regmap_debugfs_exit(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200248 kfree(map->work_buf);
Mark Brownb83a3132011-05-11 19:59:58 +0200249 kfree(map);
250}
251EXPORT_SYMBOL_GPL(regmap_exit);
252
253static int _regmap_raw_write(struct regmap *map, unsigned int reg,
254 const void *val, size_t val_len)
255{
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200256 u8 *u8 = map->work_buf;
Mark Brownb83a3132011-05-11 19:59:58 +0200257 void *buf;
258 int ret = -ENOTSUPP;
259 size_t len;
Mark Brown73304782011-07-24 11:46:20 +0100260 int i;
261
262 /* Check for unwritable registers before we start */
263 if (map->writeable_reg)
264 for (i = 0; i < val_len / map->format.val_bytes; i++)
265 if (!map->writeable_reg(map->dev, reg + i))
266 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +0200267
268 map->format.format_reg(map->work_buf, reg);
269
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200270 u8[0] |= map->write_flag_mask;
271
Mark Brownfb2736b2011-07-24 21:30:55 +0100272 trace_regmap_hw_write_start(map->dev, reg,
273 val_len / map->format.val_bytes);
274
Mark Brown2547e202011-07-20 21:47:22 +0100275 /* If we're doing a single register write we can probably just
276 * send the work_buf directly, otherwise try to do a gather
277 * write.
278 */
279 if (val == map->work_buf + map->format.reg_bytes)
280 ret = map->bus->write(map->dev, map->work_buf,
281 map->format.reg_bytes + val_len);
282 else if (map->bus->gather_write)
Mark Brownb83a3132011-05-11 19:59:58 +0200283 ret = map->bus->gather_write(map->dev, map->work_buf,
284 map->format.reg_bytes,
285 val, val_len);
286
Mark Brown2547e202011-07-20 21:47:22 +0100287 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +0200288 if (ret == -ENOTSUPP) {
289 len = map->format.reg_bytes + val_len;
290 buf = kmalloc(len, GFP_KERNEL);
291 if (!buf)
292 return -ENOMEM;
293
294 memcpy(buf, map->work_buf, map->format.reg_bytes);
295 memcpy(buf + map->format.reg_bytes, val, val_len);
296 ret = map->bus->write(map->dev, buf, len);
297
298 kfree(buf);
299 }
300
Mark Brownfb2736b2011-07-24 21:30:55 +0100301 trace_regmap_hw_write_done(map->dev, reg,
302 val_len / map->format.val_bytes);
303
Mark Brownb83a3132011-05-11 19:59:58 +0200304 return ret;
305}
306
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +0100307int _regmap_write(struct regmap *map, unsigned int reg,
308 unsigned int val)
Mark Brownb83a3132011-05-11 19:59:58 +0200309{
Mark Brownfb2736b2011-07-24 21:30:55 +0100310 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200311 BUG_ON(!map->format.format_write && !map->format.format_val);
312
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100313 if (!map->cache_bypass) {
314 ret = regcache_write(map, reg, val);
315 if (ret != 0)
316 return ret;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200317 if (map->cache_only) {
318 map->cache_dirty = true;
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100319 return 0;
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200320 }
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100321 }
322
Mark Brownfb2736b2011-07-24 21:30:55 +0100323 trace_regmap_reg_write(map->dev, reg, val);
324
Mark Brownb83a3132011-05-11 19:59:58 +0200325 if (map->format.format_write) {
326 map->format.format_write(map, reg, val);
327
Mark Brownfb2736b2011-07-24 21:30:55 +0100328 trace_regmap_hw_write_start(map->dev, reg, 1);
329
330 ret = map->bus->write(map->dev, map->work_buf,
331 map->format.buf_size);
332
333 trace_regmap_hw_write_done(map->dev, reg, 1);
334
335 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200336 } else {
337 map->format.format_val(map->work_buf + map->format.reg_bytes,
338 val);
339 return _regmap_raw_write(map, reg,
340 map->work_buf + map->format.reg_bytes,
341 map->format.val_bytes);
342 }
343}
344
345/**
346 * regmap_write(): Write a value to a single register
347 *
348 * @map: Register map to write to
349 * @reg: Register to write to
350 * @val: Value to be written
351 *
352 * A value of zero will be returned on success, a negative errno will
353 * be returned in error cases.
354 */
355int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
356{
357 int ret;
358
359 mutex_lock(&map->lock);
360
361 ret = _regmap_write(map, reg, val);
362
363 mutex_unlock(&map->lock);
364
365 return ret;
366}
367EXPORT_SYMBOL_GPL(regmap_write);
368
369/**
370 * regmap_raw_write(): Write raw values to one or more registers
371 *
372 * @map: Register map to write to
373 * @reg: Initial register to write to
374 * @val: Block of data to be written, laid out for direct transmission to the
375 * device
376 * @val_len: Length of data pointed to by val.
377 *
378 * This function is intended to be used for things like firmware
379 * download where a large block of data needs to be transferred to the
380 * device. No formatting will be done on the data provided.
381 *
382 * A value of zero will be returned on success, a negative errno will
383 * be returned in error cases.
384 */
385int regmap_raw_write(struct regmap *map, unsigned int reg,
386 const void *val, size_t val_len)
387{
Lars-Peter Clausenc48a9d72011-11-08 18:37:26 +0100388 size_t val_count = val_len / map->format.val_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +0200389 int ret;
390
Lars-Peter Clausenc48a9d72011-11-08 18:37:26 +0100391 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
392 map->cache_type != REGCACHE_NONE);
Mark Brown04e016a2011-10-09 13:35:43 +0100393
Mark Brownb83a3132011-05-11 19:59:58 +0200394 mutex_lock(&map->lock);
395
396 ret = _regmap_raw_write(map, reg, val, val_len);
397
398 mutex_unlock(&map->lock);
399
400 return ret;
401}
402EXPORT_SYMBOL_GPL(regmap_raw_write);
403
404static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
405 unsigned int val_len)
406{
407 u8 *u8 = map->work_buf;
408 int ret;
409
410 map->format.format_reg(map->work_buf, reg);
411
412 /*
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200413 * Some buses or devices flag reads by setting the high bits in the
Mark Brownb83a3132011-05-11 19:59:58 +0200414 * register addresss; since it's always the high bits for all
415 * current formats we can do this here rather than in
416 * formatting. This may break if we get interesting formats.
417 */
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200418 u8[0] |= map->read_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +0200419
Mark Brownfb2736b2011-07-24 21:30:55 +0100420 trace_regmap_hw_read_start(map->dev, reg,
421 val_len / map->format.val_bytes);
422
Mark Brownb83a3132011-05-11 19:59:58 +0200423 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +0100424 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200425
Mark Brownfb2736b2011-07-24 21:30:55 +0100426 trace_regmap_hw_read_done(map->dev, reg,
427 val_len / map->format.val_bytes);
428
429 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200430}
431
432static int _regmap_read(struct regmap *map, unsigned int reg,
433 unsigned int *val)
434{
435 int ret;
436
437 if (!map->format.parse_val)
438 return -EINVAL;
439
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100440 if (!map->cache_bypass) {
441 ret = regcache_read(map, reg, val);
442 if (ret == 0)
443 return 0;
444 }
445
446 if (map->cache_only)
447 return -EBUSY;
448
Mark Brownb83a3132011-05-11 19:59:58 +0200449 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +0100450 if (ret == 0) {
Mark Brownb83a3132011-05-11 19:59:58 +0200451 *val = map->format.parse_val(map->work_buf);
Mark Brownfb2736b2011-07-24 21:30:55 +0100452 trace_regmap_reg_read(map->dev, reg, *val);
453 }
Mark Brownb83a3132011-05-11 19:59:58 +0200454
455 return ret;
456}
457
458/**
459 * regmap_read(): Read a value from a single register
460 *
461 * @map: Register map to write to
462 * @reg: Register to be read from
463 * @val: Pointer to store read value
464 *
465 * A value of zero will be returned on success, a negative errno will
466 * be returned in error cases.
467 */
468int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
469{
470 int ret;
471
472 mutex_lock(&map->lock);
473
474 ret = _regmap_read(map, reg, val);
475
476 mutex_unlock(&map->lock);
477
478 return ret;
479}
480EXPORT_SYMBOL_GPL(regmap_read);
481
482/**
483 * regmap_raw_read(): Read raw data from the device
484 *
485 * @map: Register map to write to
486 * @reg: First register to be read from
487 * @val: Pointer to store read value
488 * @val_len: Size of data to read
489 *
490 * A value of zero will be returned on success, a negative errno will
491 * be returned in error cases.
492 */
493int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
494 size_t val_len)
495{
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100496 size_t val_count = val_len / map->format.val_bytes;
Mark Brownb83a3132011-05-11 19:59:58 +0200497 int ret;
498
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100499 WARN_ON(!regmap_volatile_range(map, reg, val_count) &&
500 map->cache_type != REGCACHE_NONE);
Mark Brown04e016a2011-10-09 13:35:43 +0100501
Mark Brownb83a3132011-05-11 19:59:58 +0200502 mutex_lock(&map->lock);
503
504 ret = _regmap_raw_read(map, reg, val, val_len);
505
506 mutex_unlock(&map->lock);
507
508 return ret;
509}
510EXPORT_SYMBOL_GPL(regmap_raw_read);
511
512/**
513 * regmap_bulk_read(): Read multiple registers from the device
514 *
515 * @map: Register map to write to
516 * @reg: First register to be read from
517 * @val: Pointer to store read value, in native register size for device
518 * @val_count: Number of registers to read
519 *
520 * A value of zero will be returned on success, a negative errno will
521 * be returned in error cases.
522 */
523int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
524 size_t val_count)
525{
526 int ret, i;
527 size_t val_bytes = map->format.val_bytes;
Lars-Peter Clausen82cd9962011-11-08 18:37:25 +0100528 bool vol = regmap_volatile_range(map, reg, val_count);
Dimitris Papastamos5d1729e2011-09-19 14:34:05 +0100529
Mark Brownb83a3132011-05-11 19:59:58 +0200530 if (!map->format.parse_val)
531 return -EINVAL;
532
Mark Brownde2d8082011-10-10 13:24:52 +0100533 if (vol || map->cache_type == REGCACHE_NONE) {
534 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
535 if (ret != 0)
536 return ret;
537
538 for (i = 0; i < val_count * val_bytes; i += val_bytes)
539 map->format.parse_val(val + i);
540 } else {
541 for (i = 0; i < val_count; i++) {
542 ret = regmap_read(map, reg + i, val + (i * val_bytes));
543 if (ret != 0)
544 return ret;
545 }
546 }
Mark Brownb83a3132011-05-11 19:59:58 +0200547
548 return 0;
549}
550EXPORT_SYMBOL_GPL(regmap_bulk_read);
551
552/**
Mark Brownb973aa32011-10-28 23:46:18 +0200553 * regmap_update_bits: Perform a read/modify/write cycle on the register map
Mark Brownb83a3132011-05-11 19:59:58 +0200554 *
555 * @map: Register map to update
556 * @reg: Register to update
557 * @mask: Bitmask to change
558 * @val: New value for bitmask
559 *
560 * Returns zero for success, a negative number on error.
561 */
562int regmap_update_bits(struct regmap *map, unsigned int reg,
563 unsigned int mask, unsigned int val)
564{
565 int ret;
566 unsigned int tmp;
567
568 mutex_lock(&map->lock);
569
570 ret = _regmap_read(map, reg, &tmp);
571 if (ret != 0)
572 goto out;
573
574 tmp &= ~mask;
575 tmp |= val & mask;
576
577 ret = _regmap_write(map, reg, tmp);
578
579out:
580 mutex_unlock(&map->lock);
581
582 return ret;
583}
584EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +0100585
586static int __init regmap_initcall(void)
587{
588 regmap_debugfs_initcall();
589
590 return 0;
591}
592postcore_initcall(regmap_initcall);