blob: a3eaef6552ce9d3eeec6861934365d7e52454b21 [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
23static void regmap_format_4_12_write(struct regmap *map,
24 unsigned int reg, unsigned int val)
25{
26 __be16 *out = map->work_buf;
27 *out = cpu_to_be16((reg << 12) | val);
28}
29
30static void regmap_format_7_9_write(struct regmap *map,
31 unsigned int reg, unsigned int val)
32{
33 __be16 *out = map->work_buf;
34 *out = cpu_to_be16((reg << 9) | val);
35}
36
37static void regmap_format_8(void *buf, unsigned int val)
38{
39 u8 *b = buf;
40
41 b[0] = val;
42}
43
44static void regmap_format_16(void *buf, unsigned int val)
45{
46 __be16 *b = buf;
47
48 b[0] = cpu_to_be16(val);
49}
50
51static unsigned int regmap_parse_8(void *buf)
52{
53 u8 *b = buf;
54
55 return b[0];
56}
57
58static unsigned int regmap_parse_16(void *buf)
59{
60 __be16 *b = buf;
61
62 b[0] = be16_to_cpu(b[0]);
63
64 return b[0];
65}
66
67/**
68 * regmap_init(): Initialise register map
69 *
70 * @dev: Device that will be interacted with
71 * @bus: Bus-specific callbacks to use with device
72 * @config: Configuration for register map
73 *
74 * The return value will be an ERR_PTR() on error or a valid pointer to
75 * a struct regmap. This function should generally not be called
76 * directly, it should be called by bus-specific init functions.
77 */
78struct regmap *regmap_init(struct device *dev,
79 const struct regmap_bus *bus,
80 const struct regmap_config *config)
81{
82 struct regmap *map;
83 int ret = -EINVAL;
84
85 if (!bus || !config)
86 return NULL;
87
88 map = kzalloc(sizeof(*map), GFP_KERNEL);
89 if (map == NULL) {
90 ret = -ENOMEM;
91 goto err;
92 }
93
94 mutex_init(&map->lock);
95 map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
96 map->format.reg_bytes = config->reg_bits / 8;
97 map->format.val_bytes = config->val_bits / 8;
98 map->dev = dev;
99 map->bus = bus;
Mark Brown2e2ae662011-07-20 22:33:39 +0100100 map->max_register = config->max_register;
101 map->writeable_reg = config->writeable_reg;
102 map->readable_reg = config->readable_reg;
103 map->volatile_reg = config->volatile_reg;
Mark Brownb83a3132011-05-11 19:59:58 +0200104
105 switch (config->reg_bits) {
106 case 4:
107 switch (config->val_bits) {
108 case 12:
109 map->format.format_write = regmap_format_4_12_write;
110 break;
111 default:
112 goto err_map;
113 }
114 break;
115
116 case 7:
117 switch (config->val_bits) {
118 case 9:
119 map->format.format_write = regmap_format_7_9_write;
120 break;
121 default:
122 goto err_map;
123 }
124 break;
125
126 case 8:
127 map->format.format_reg = regmap_format_8;
128 break;
129
130 case 16:
131 map->format.format_reg = regmap_format_16;
132 break;
133
134 default:
135 goto err_map;
136 }
137
138 switch (config->val_bits) {
139 case 8:
140 map->format.format_val = regmap_format_8;
141 map->format.parse_val = regmap_parse_8;
142 break;
143 case 16:
144 map->format.format_val = regmap_format_16;
145 map->format.parse_val = regmap_parse_16;
146 break;
147 }
148
149 if (!map->format.format_write &&
150 !(map->format.format_reg && map->format.format_val))
151 goto err_map;
152
153 map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
154 if (map->work_buf == NULL) {
155 ret = -ENOMEM;
156 goto err_bus;
157 }
158
Mark Brown31244e32011-07-20 22:56:53 +0100159 regmap_debugfs_init(map);
160
Mark Brownb83a3132011-05-11 19:59:58 +0200161 return map;
162
163err_bus:
164 module_put(map->bus->owner);
165err_map:
166 kfree(map);
167err:
168 return ERR_PTR(ret);
169}
170EXPORT_SYMBOL_GPL(regmap_init);
171
172/**
173 * regmap_exit(): Free a previously allocated register map
174 */
175void regmap_exit(struct regmap *map)
176{
Mark Brown31244e32011-07-20 22:56:53 +0100177 regmap_debugfs_exit(map);
Mark Brownb83a3132011-05-11 19:59:58 +0200178 kfree(map->work_buf);
179 module_put(map->bus->owner);
180 kfree(map);
181}
182EXPORT_SYMBOL_GPL(regmap_exit);
183
184static int _regmap_raw_write(struct regmap *map, unsigned int reg,
185 const void *val, size_t val_len)
186{
187 void *buf;
188 int ret = -ENOTSUPP;
189 size_t len;
Mark Brown73304782011-07-24 11:46:20 +0100190 int i;
191
192 /* Check for unwritable registers before we start */
193 if (map->writeable_reg)
194 for (i = 0; i < val_len / map->format.val_bytes; i++)
195 if (!map->writeable_reg(map->dev, reg + i))
196 return -EINVAL;
Mark Brownb83a3132011-05-11 19:59:58 +0200197
198 map->format.format_reg(map->work_buf, reg);
199
Mark Brownfb2736b2011-07-24 21:30:55 +0100200 trace_regmap_hw_write_start(map->dev, reg,
201 val_len / map->format.val_bytes);
202
Mark Brown2547e202011-07-20 21:47:22 +0100203 /* If we're doing a single register write we can probably just
204 * send the work_buf directly, otherwise try to do a gather
205 * write.
206 */
207 if (val == map->work_buf + map->format.reg_bytes)
208 ret = map->bus->write(map->dev, map->work_buf,
209 map->format.reg_bytes + val_len);
210 else if (map->bus->gather_write)
Mark Brownb83a3132011-05-11 19:59:58 +0200211 ret = map->bus->gather_write(map->dev, map->work_buf,
212 map->format.reg_bytes,
213 val, val_len);
214
Mark Brown2547e202011-07-20 21:47:22 +0100215 /* If that didn't work fall back on linearising by hand. */
Mark Brownb83a3132011-05-11 19:59:58 +0200216 if (ret == -ENOTSUPP) {
217 len = map->format.reg_bytes + val_len;
218 buf = kmalloc(len, GFP_KERNEL);
219 if (!buf)
220 return -ENOMEM;
221
222 memcpy(buf, map->work_buf, map->format.reg_bytes);
223 memcpy(buf + map->format.reg_bytes, val, val_len);
224 ret = map->bus->write(map->dev, buf, len);
225
226 kfree(buf);
227 }
228
Mark Brownfb2736b2011-07-24 21:30:55 +0100229 trace_regmap_hw_write_done(map->dev, reg,
230 val_len / map->format.val_bytes);
231
Mark Brownb83a3132011-05-11 19:59:58 +0200232 return ret;
233}
234
235static int _regmap_write(struct regmap *map, unsigned int reg,
236 unsigned int val)
237{
Mark Brownfb2736b2011-07-24 21:30:55 +0100238 int ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200239 BUG_ON(!map->format.format_write && !map->format.format_val);
240
Mark Brownfb2736b2011-07-24 21:30:55 +0100241 trace_regmap_reg_write(map->dev, reg, val);
242
Mark Brownb83a3132011-05-11 19:59:58 +0200243 if (map->format.format_write) {
244 map->format.format_write(map, reg, val);
245
Mark Brownfb2736b2011-07-24 21:30:55 +0100246 trace_regmap_hw_write_start(map->dev, reg, 1);
247
248 ret = map->bus->write(map->dev, map->work_buf,
249 map->format.buf_size);
250
251 trace_regmap_hw_write_done(map->dev, reg, 1);
252
253 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200254 } else {
255 map->format.format_val(map->work_buf + map->format.reg_bytes,
256 val);
257 return _regmap_raw_write(map, reg,
258 map->work_buf + map->format.reg_bytes,
259 map->format.val_bytes);
260 }
261}
262
263/**
264 * regmap_write(): Write a value to a single register
265 *
266 * @map: Register map to write to
267 * @reg: Register to write to
268 * @val: Value to be written
269 *
270 * A value of zero will be returned on success, a negative errno will
271 * be returned in error cases.
272 */
273int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
274{
275 int ret;
276
277 mutex_lock(&map->lock);
278
279 ret = _regmap_write(map, reg, val);
280
281 mutex_unlock(&map->lock);
282
283 return ret;
284}
285EXPORT_SYMBOL_GPL(regmap_write);
286
287/**
288 * regmap_raw_write(): Write raw values to one or more registers
289 *
290 * @map: Register map to write to
291 * @reg: Initial register to write to
292 * @val: Block of data to be written, laid out for direct transmission to the
293 * device
294 * @val_len: Length of data pointed to by val.
295 *
296 * This function is intended to be used for things like firmware
297 * download where a large block of data needs to be transferred to the
298 * device. No formatting will be done on the data provided.
299 *
300 * A value of zero will be returned on success, a negative errno will
301 * be returned in error cases.
302 */
303int regmap_raw_write(struct regmap *map, unsigned int reg,
304 const void *val, size_t val_len)
305{
306 int ret;
307
308 mutex_lock(&map->lock);
309
310 ret = _regmap_raw_write(map, reg, val, val_len);
311
312 mutex_unlock(&map->lock);
313
314 return ret;
315}
316EXPORT_SYMBOL_GPL(regmap_raw_write);
317
318static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
319 unsigned int val_len)
320{
321 u8 *u8 = map->work_buf;
322 int ret;
323
324 map->format.format_reg(map->work_buf, reg);
325
326 /*
327 * Some buses flag reads by setting the high bits in the
328 * register addresss; since it's always the high bits for all
329 * current formats we can do this here rather than in
330 * formatting. This may break if we get interesting formats.
331 */
332 if (map->bus->read_flag_mask)
333 u8[0] |= map->bus->read_flag_mask;
334
Mark Brownfb2736b2011-07-24 21:30:55 +0100335 trace_regmap_hw_read_start(map->dev, reg,
336 val_len / map->format.val_bytes);
337
Mark Brownb83a3132011-05-11 19:59:58 +0200338 ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
Mark Brown40c5cc22011-07-24 22:39:12 +0100339 val, val_len);
Mark Brownb83a3132011-05-11 19:59:58 +0200340
Mark Brownfb2736b2011-07-24 21:30:55 +0100341 trace_regmap_hw_read_done(map->dev, reg,
342 val_len / map->format.val_bytes);
343
344 return ret;
Mark Brownb83a3132011-05-11 19:59:58 +0200345}
346
347static int _regmap_read(struct regmap *map, unsigned int reg,
348 unsigned int *val)
349{
350 int ret;
351
352 if (!map->format.parse_val)
353 return -EINVAL;
354
355 ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
Mark Brownfb2736b2011-07-24 21:30:55 +0100356 if (ret == 0) {
Mark Brownb83a3132011-05-11 19:59:58 +0200357 *val = map->format.parse_val(map->work_buf);
Mark Brownfb2736b2011-07-24 21:30:55 +0100358 trace_regmap_reg_read(map->dev, reg, *val);
359 }
Mark Brownb83a3132011-05-11 19:59:58 +0200360
361 return ret;
362}
363
364/**
365 * regmap_read(): Read a value from a single register
366 *
367 * @map: Register map to write to
368 * @reg: Register to be read from
369 * @val: Pointer to store read value
370 *
371 * A value of zero will be returned on success, a negative errno will
372 * be returned in error cases.
373 */
374int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
375{
376 int ret;
377
378 mutex_lock(&map->lock);
379
380 ret = _regmap_read(map, reg, val);
381
382 mutex_unlock(&map->lock);
383
384 return ret;
385}
386EXPORT_SYMBOL_GPL(regmap_read);
387
388/**
389 * regmap_raw_read(): Read raw data from the device
390 *
391 * @map: Register map to write to
392 * @reg: First register to be read from
393 * @val: Pointer to store read value
394 * @val_len: Size of data to read
395 *
396 * A value of zero will be returned on success, a negative errno will
397 * be returned in error cases.
398 */
399int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
400 size_t val_len)
401{
402 int ret;
403
404 mutex_lock(&map->lock);
405
406 ret = _regmap_raw_read(map, reg, val, val_len);
407
408 mutex_unlock(&map->lock);
409
410 return ret;
411}
412EXPORT_SYMBOL_GPL(regmap_raw_read);
413
414/**
415 * regmap_bulk_read(): Read multiple registers from the device
416 *
417 * @map: Register map to write to
418 * @reg: First register to be read from
419 * @val: Pointer to store read value, in native register size for device
420 * @val_count: Number of registers to read
421 *
422 * A value of zero will be returned on success, a negative errno will
423 * be returned in error cases.
424 */
425int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
426 size_t val_count)
427{
428 int ret, i;
429 size_t val_bytes = map->format.val_bytes;
430
431 if (!map->format.parse_val)
432 return -EINVAL;
433
434 ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
435 if (ret != 0)
436 return ret;
437
438 for (i = 0; i < val_count * val_bytes; i += val_bytes)
439 map->format.parse_val(val + i);
440
441 return 0;
442}
443EXPORT_SYMBOL_GPL(regmap_bulk_read);
444
445/**
446 * remap_update_bits: Perform a read/modify/write cycle on the register map
447 *
448 * @map: Register map to update
449 * @reg: Register to update
450 * @mask: Bitmask to change
451 * @val: New value for bitmask
452 *
453 * Returns zero for success, a negative number on error.
454 */
455int regmap_update_bits(struct regmap *map, unsigned int reg,
456 unsigned int mask, unsigned int val)
457{
458 int ret;
459 unsigned int tmp;
460
461 mutex_lock(&map->lock);
462
463 ret = _regmap_read(map, reg, &tmp);
464 if (ret != 0)
465 goto out;
466
467 tmp &= ~mask;
468 tmp |= val & mask;
469
470 ret = _regmap_write(map, reg, tmp);
471
472out:
473 mutex_unlock(&map->lock);
474
475 return ret;
476}
477EXPORT_SYMBOL_GPL(regmap_update_bits);
Mark Brown31244e32011-07-20 22:56:53 +0100478
479static int __init regmap_initcall(void)
480{
481 regmap_debugfs_initcall();
482
483 return 0;
484}
485postcore_initcall(regmap_initcall);