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