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