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