regmap: Just send the buffer directly for single register writes
[pandora-kernel.git] / drivers / base / regmap / regmap.c
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
20 struct regmap;
21
22 struct 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
33 struct 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
42 static 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
49 static 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
56 static void regmap_format_8(void *buf, unsigned int val)
57 {
58         u8 *b = buf;
59
60         b[0] = val;
61 }
62
63 static void regmap_format_16(void *buf, unsigned int val)
64 {
65         __be16 *b = buf;
66
67         b[0] = cpu_to_be16(val);
68 }
69
70 static unsigned int regmap_parse_8(void *buf)
71 {
72         u8 *b = buf;
73
74         return b[0];
75 }
76
77 static 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  */
97 struct 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_bus;
172         }
173
174         return map;
175
176 err_bus:
177         module_put(map->bus->owner);
178 err_map:
179         kfree(map);
180 err:
181         return ERR_PTR(ret);
182 }
183 EXPORT_SYMBOL_GPL(regmap_init);
184
185 /**
186  * regmap_exit(): Free a previously allocated register map
187  */
188 void regmap_exit(struct regmap *map)
189 {
190         kfree(map->work_buf);
191         module_put(map->bus->owner);
192         kfree(map);
193 }
194 EXPORT_SYMBOL_GPL(regmap_exit);
195
196 static int _regmap_raw_write(struct regmap *map, unsigned int reg,
197                              const void *val, size_t val_len)
198 {
199         void *buf;
200         int ret = -ENOTSUPP;
201         size_t len;
202
203         map->format.format_reg(map->work_buf, reg);
204
205         /* If we're doing a single register write we can probably just
206          * send the work_buf directly, otherwise try to do a gather
207          * write.
208          */
209         if (val == map->work_buf + map->format.reg_bytes)
210                 ret = map->bus->write(map->dev, map->work_buf,
211                                       map->format.reg_bytes + val_len);
212         else if (map->bus->gather_write)
213                 ret = map->bus->gather_write(map->dev, map->work_buf,
214                                              map->format.reg_bytes,
215                                              val, val_len);
216
217         /* If that didn't work fall back on linearising by hand. */
218         if (ret == -ENOTSUPP) {
219                 len = map->format.reg_bytes + val_len;
220                 buf = kmalloc(len, GFP_KERNEL);
221                 if (!buf)
222                         return -ENOMEM;
223
224                 memcpy(buf, map->work_buf, map->format.reg_bytes);
225                 memcpy(buf + map->format.reg_bytes, val, val_len);
226                 ret = map->bus->write(map->dev, buf, len);
227
228                 kfree(buf);
229         }
230
231         return ret;
232 }
233
234 static int _regmap_write(struct regmap *map, unsigned int reg,
235                          unsigned int val)
236 {
237         BUG_ON(!map->format.format_write && !map->format.format_val);
238
239         if (map->format.format_write) {
240                 map->format.format_write(map, reg, val);
241
242                 return map->bus->write(map->dev, map->work_buf,
243                                        map->format.buf_size);
244         } else {
245                 map->format.format_val(map->work_buf + map->format.reg_bytes,
246                                        val);
247                 return _regmap_raw_write(map, reg,
248                                          map->work_buf + map->format.reg_bytes,
249                                          map->format.val_bytes);
250         }
251 }
252
253 /**
254  * regmap_write(): Write a value to a single register
255  *
256  * @map: Register map to write to
257  * @reg: Register to write to
258  * @val: Value to be written
259  *
260  * A value of zero will be returned on success, a negative errno will
261  * be returned in error cases.
262  */
263 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
264 {
265         int ret;
266
267         mutex_lock(&map->lock);
268
269         ret = _regmap_write(map, reg, val);
270
271         mutex_unlock(&map->lock);
272
273         return ret;
274 }
275 EXPORT_SYMBOL_GPL(regmap_write);
276
277 /**
278  * regmap_raw_write(): Write raw values to one or more registers
279  *
280  * @map: Register map to write to
281  * @reg: Initial register to write to
282  * @val: Block of data to be written, laid out for direct transmission to the
283  *       device
284  * @val_len: Length of data pointed to by val.
285  *
286  * This function is intended to be used for things like firmware
287  * download where a large block of data needs to be transferred to the
288  * device.  No formatting will be done on the data provided.
289  *
290  * A value of zero will be returned on success, a negative errno will
291  * be returned in error cases.
292  */
293 int regmap_raw_write(struct regmap *map, unsigned int reg,
294                      const void *val, size_t val_len)
295 {
296         int ret;
297
298         mutex_lock(&map->lock);
299
300         ret = _regmap_raw_write(map, reg, val, val_len);
301
302         mutex_unlock(&map->lock);
303
304         return ret;
305 }
306 EXPORT_SYMBOL_GPL(regmap_raw_write);
307
308 static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
309                             unsigned int val_len)
310 {
311         u8 *u8 = map->work_buf;
312         int ret;
313
314         map->format.format_reg(map->work_buf, reg);
315
316         /*
317          * Some buses flag reads by setting the high bits in the
318          * register addresss; since it's always the high bits for all
319          * current formats we can do this here rather than in
320          * formatting.  This may break if we get interesting formats.
321          */
322         if (map->bus->read_flag_mask)
323                 u8[0] |= map->bus->read_flag_mask;
324
325         ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
326                              val, map->format.val_bytes);
327         if (ret != 0)
328                 return ret;
329
330         return 0;
331 }
332
333 static int _regmap_read(struct regmap *map, unsigned int reg,
334                         unsigned int *val)
335 {
336         int ret;
337
338         if (!map->format.parse_val)
339                 return -EINVAL;
340
341         ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
342         if (ret == 0)
343                 *val = map->format.parse_val(map->work_buf);
344
345         return ret;
346 }
347
348 /**
349  * regmap_read(): Read a value from a single register
350  *
351  * @map: Register map to write to
352  * @reg: Register to be read from
353  * @val: Pointer to store read value
354  *
355  * A value of zero will be returned on success, a negative errno will
356  * be returned in error cases.
357  */
358 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
359 {
360         int ret;
361
362         mutex_lock(&map->lock);
363
364         ret = _regmap_read(map, reg, val);
365
366         mutex_unlock(&map->lock);
367
368         return ret;
369 }
370 EXPORT_SYMBOL_GPL(regmap_read);
371
372 /**
373  * regmap_raw_read(): Read raw data from the device
374  *
375  * @map: Register map to write to
376  * @reg: First register to be read from
377  * @val: Pointer to store read value
378  * @val_len: Size of data to read
379  *
380  * A value of zero will be returned on success, a negative errno will
381  * be returned in error cases.
382  */
383 int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
384                     size_t val_len)
385 {
386         int ret;
387
388         mutex_lock(&map->lock);
389
390         ret = _regmap_raw_read(map, reg, val, val_len);
391
392         mutex_unlock(&map->lock);
393
394         return ret;
395 }
396 EXPORT_SYMBOL_GPL(regmap_raw_read);
397
398 /**
399  * regmap_bulk_read(): Read multiple registers from the device
400  *
401  * @map: Register map to write to
402  * @reg: First register to be read from
403  * @val: Pointer to store read value, in native register size for device
404  * @val_count: Number of registers to read
405  *
406  * A value of zero will be returned on success, a negative errno will
407  * be returned in error cases.
408  */
409 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
410                      size_t val_count)
411 {
412         int ret, i;
413         size_t val_bytes = map->format.val_bytes;
414
415         if (!map->format.parse_val)
416                 return -EINVAL;
417
418         ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
419         if (ret != 0)
420                 return ret;
421
422         for (i = 0; i < val_count * val_bytes; i += val_bytes)
423                 map->format.parse_val(val + i);
424
425         return 0;
426 }
427 EXPORT_SYMBOL_GPL(regmap_bulk_read);
428
429 /**
430  * remap_update_bits: Perform a read/modify/write cycle on the register map
431  *
432  * @map: Register map to update
433  * @reg: Register to update
434  * @mask: Bitmask to change
435  * @val: New value for bitmask
436  *
437  * Returns zero for success, a negative number on error.
438  */
439 int regmap_update_bits(struct regmap *map, unsigned int reg,
440                        unsigned int mask, unsigned int val)
441 {
442         int ret;
443         unsigned int tmp;
444
445         mutex_lock(&map->lock);
446
447         ret = _regmap_read(map, reg, &tmp);
448         if (ret != 0)
449                 goto out;
450
451         tmp &= ~mask;
452         tmp |= val & mask;
453
454         ret = _regmap_write(map, reg, tmp);
455
456 out:
457         mutex_unlock(&map->lock);
458
459         return ret;
460 }
461 EXPORT_SYMBOL_GPL(regmap_update_bits);