blob: 9d8029449292ec73e3d4b2758162a72498e620a9 [file] [log] [blame]
Mark Brownb83a3132011-05-11 19:59:58 +02001#ifndef __LINUX_REGMAP_H
2#define __LINUX_REGMAP_H
3
4/*
5 * Register map access API
6 *
7 * Copyright 2011 Wolfson Microelectronics plc
8 *
9 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#include <linux/device.h>
17#include <linux/list.h>
18#include <linux/module.h>
19
Mark Brown9943fa32011-06-20 19:02:29 +010020struct i2c_client;
Mark Browna676f082011-05-12 11:42:10 +020021struct spi_device;
Mark Brown9943fa32011-06-20 19:02:29 +010022
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010023/* An enum of all the supported cache types */
24enum regcache_type {
25 REGCACHE_NONE,
26};
27
Mark Browndd898b22011-07-20 22:28:58 +010028/**
Mark Brownbd20eb52011-08-19 18:09:38 +090029 * Default value for a register. We use an array of structs rather
30 * than a simple array as many modern devices have very sparse
31 * register maps.
32 *
33 * @reg: Register address.
34 * @def: Register default value.
35 */
36struct reg_default {
37 unsigned int reg;
38 unsigned int def;
39};
40
41/**
Mark Browndd898b22011-07-20 22:28:58 +010042 * Configuration for the register map of a device.
43 *
44 * @reg_bits: Number of bits in a register address, mandatory.
45 * @val_bits: Number of bits in a register value, mandatory.
Mark Brown2e2ae662011-07-20 22:33:39 +010046 *
Mark Brown3566cc92011-08-09 10:23:22 +090047 * @writeable_reg: Optional callback returning true if the register
48 * can be written to.
49 * @readable_reg: Optional callback returning true if the register
50 * can be read from.
51 * @volatile_reg: Optional callback returning true if the register
52 * value can't be cached.
53 * @precious_reg: Optional callback returning true if the rgister
54 * should not be read outside of a call from the driver
55 * (eg, a clear on read interrupt status register).
Mark Brownbd20eb52011-08-19 18:09:38 +090056 *
57 * @max_register: Optional, specifies the maximum valid register index.
58 * @reg_defaults: Power on reset values for registers (for use with
59 * register cache support).
60 * @num_reg_defaults: Number of elements in reg_defaults.
Lars-Peter Clausen6f306442011-09-05 20:46:32 +020061 *
62 * @read_flag_mask: Mask to be set in the top byte of the register when doing
63 * a read.
64 * @write_flag_mask: Mask to be set in the top byte of the register when doing
65 * a write. If both read_flag_mask and write_flag_mask are
66 * empty the regmap_bus default masks are used.
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010067 *
68 * @cache_type: The actual cache type.
69 * @reg_defaults_raw: Power on reset values for registers (for use with
70 * register cache support).
71 * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
Mark Browndd898b22011-07-20 22:28:58 +010072 */
Mark Brownb83a3132011-05-11 19:59:58 +020073struct regmap_config {
74 int reg_bits;
75 int val_bits;
Mark Brown2e2ae662011-07-20 22:33:39 +010076
Mark Brown2e2ae662011-07-20 22:33:39 +010077 bool (*writeable_reg)(struct device *dev, unsigned int reg);
78 bool (*readable_reg)(struct device *dev, unsigned int reg);
79 bool (*volatile_reg)(struct device *dev, unsigned int reg);
Mark Brown18694882011-08-08 15:40:22 +090080 bool (*precious_reg)(struct device *dev, unsigned int reg);
Mark Brownbd20eb52011-08-19 18:09:38 +090081
82 unsigned int max_register;
83 struct reg_default *reg_defaults;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010084 unsigned int num_reg_defaults;
85 enum regcache_type cache_type;
86 const void *reg_defaults_raw;
87 unsigned int num_reg_defaults_raw;
Lars-Peter Clausen6f306442011-09-05 20:46:32 +020088
89 u8 read_flag_mask;
90 u8 write_flag_mask;
Mark Brownb83a3132011-05-11 19:59:58 +020091};
92
93typedef int (*regmap_hw_write)(struct device *dev, const void *data,
94 size_t count);
95typedef int (*regmap_hw_gather_write)(struct device *dev,
96 const void *reg, size_t reg_len,
97 const void *val, size_t val_len);
98typedef int (*regmap_hw_read)(struct device *dev,
99 const void *reg_buf, size_t reg_size,
100 void *val_buf, size_t val_size);
101
102/**
103 * Description of a hardware bus for the register map infrastructure.
104 *
Mark Brownb83a3132011-05-11 19:59:58 +0200105 * @write: Write operation.
106 * @gather_write: Write operation with split register/value, return -ENOTSUPP
107 * if not implemented on a given device.
108 * @read: Read operation. Data is returned in the buffer used to transmit
109 * data.
Mark Brownb83a3132011-05-11 19:59:58 +0200110 * @read_flag_mask: Mask to be set in the top byte of the register when doing
111 * a read.
112 */
113struct regmap_bus {
Mark Brownb83a3132011-05-11 19:59:58 +0200114 regmap_hw_write write;
115 regmap_hw_gather_write gather_write;
116 regmap_hw_read read;
Mark Brownb83a3132011-05-11 19:59:58 +0200117 u8 read_flag_mask;
118};
119
120struct regmap *regmap_init(struct device *dev,
121 const struct regmap_bus *bus,
122 const struct regmap_config *config);
Mark Brown9943fa32011-06-20 19:02:29 +0100123struct regmap *regmap_init_i2c(struct i2c_client *i2c,
124 const struct regmap_config *config);
Mark Browna676f082011-05-12 11:42:10 +0200125struct regmap *regmap_init_spi(struct spi_device *dev,
126 const struct regmap_config *config);
127
Mark Brownb83a3132011-05-11 19:59:58 +0200128void regmap_exit(struct regmap *map);
129int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
130int regmap_raw_write(struct regmap *map, unsigned int reg,
131 const void *val, size_t val_len);
132int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
133int regmap_raw_read(struct regmap *map, unsigned int reg,
134 void *val, size_t val_len);
135int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
136 size_t val_count);
137int regmap_update_bits(struct regmap *map, unsigned int reg,
138 unsigned int mask, unsigned int val);
139
140#endif