2 * Register map access API - I2C support
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
13 #include <linux/regmap.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
18 static int regmap_i2c_write(void *context, const void *data, size_t count)
20 struct device *dev = context;
21 struct i2c_client *i2c = to_i2c_client(dev);
24 ret = i2c_master_send(i2c, data, count);
33 static int regmap_i2c_gather_write(void *context,
34 const void *reg, size_t reg_size,
35 const void *val, size_t val_size)
37 struct device *dev = context;
38 struct i2c_client *i2c = to_i2c_client(dev);
39 struct i2c_msg xfer[2];
42 /* If the I2C controller can't do a gather tell the core, it
43 * will substitute in a linear write for us.
45 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_NOSTART))
48 xfer[0].addr = i2c->addr;
50 xfer[0].len = reg_size;
51 xfer[0].buf = (void *)reg;
52 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
53 xfer[0].scl_rate = 100*1000;
56 xfer[1].addr = i2c->addr;
57 xfer[1].flags = I2C_M_NOSTART;
58 xfer[1].len = val_size;
59 xfer[1].buf = (void *)val;
60 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
61 xfer[1].scl_rate = 100*1000;
64 ret = i2c_transfer(i2c->adapter, xfer, 2);
73 static int regmap_i2c_read(void *context,
74 const void *reg, size_t reg_size,
75 void *val, size_t val_size)
77 struct device *dev = context;
78 struct i2c_client *i2c = to_i2c_client(dev);
79 struct i2c_msg xfer[2];
82 xfer[0].addr = i2c->addr;
84 xfer[0].len = reg_size;
85 xfer[0].buf = (void *)reg;
86 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
87 xfer[0].scl_rate = 100*1000;
90 xfer[1].addr = i2c->addr;
91 xfer[1].flags = I2C_M_RD;
92 xfer[1].len = val_size;
94 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
95 xfer[1].scl_rate = 100*1000;
98 ret = i2c_transfer(i2c->adapter, xfer, 2);
107 static struct regmap_bus regmap_i2c = {
108 .write = regmap_i2c_write,
109 .gather_write = regmap_i2c_gather_write,
110 .read = regmap_i2c_read,
114 * regmap_init_i2c(): Initialise register map
116 * @i2c: Device that will be interacted with
117 * @config: Configuration for register map
119 * The return value will be an ERR_PTR() on error or a valid pointer to
122 struct regmap *regmap_init_i2c(struct i2c_client *i2c,
123 const struct regmap_config *config)
125 return regmap_init(&i2c->dev, ®map_i2c, &i2c->dev, config);
127 EXPORT_SYMBOL_GPL(regmap_init_i2c);
130 * devm_regmap_init_i2c(): Initialise managed register map
132 * @i2c: Device that will be interacted with
133 * @config: Configuration for register map
135 * The return value will be an ERR_PTR() on error or a valid pointer
136 * to a struct regmap. The regmap will be automatically freed by the
137 * device management code.
139 struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
140 const struct regmap_config *config)
142 return devm_regmap_init(&i2c->dev, ®map_i2c, &i2c->dev, config);
144 EXPORT_SYMBOL_GPL(devm_regmap_init_i2c);
146 MODULE_LICENSE("GPL");