38ef95eb8c4ea0733776933c526a4cf42f1a5d0e
[firefly-linux-kernel-4.4.55.git] / drivers / base / regmap / regmap-i2c.c
1 /*
2  * Register map access API - I2C support
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/regmap.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17
18 static int regmap_i2c_write(void *context, const void *data, size_t count)
19 {
20         struct device *dev = context;
21         struct i2c_client *i2c = to_i2c_client(dev);
22         int ret;
23
24         ret = i2c_master_send(i2c, data, count);
25         if (ret == count)
26                 return 0;
27         else if (ret < 0)
28                 return ret;
29         else
30                 return -EIO;
31 }
32
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)
36 {
37         struct device *dev = context;
38         struct i2c_client *i2c = to_i2c_client(dev);
39         struct i2c_msg xfer[2];
40         int ret;
41
42         /* If the I2C controller can't do a gather tell the core, it
43          * will substitute in a linear write for us.
44          */
45         if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_NOSTART))
46                 return -ENOTSUPP;
47
48         xfer[0].addr = i2c->addr;
49         xfer[0].flags = 0;
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;
54 #endif
55
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;
62 #endif
63
64         ret = i2c_transfer(i2c->adapter, xfer, 2);
65         if (ret == 2)
66                 return 0;
67         if (ret < 0)
68                 return ret;
69         else
70                 return -EIO;
71 }
72
73 static int regmap_i2c_read(void *context,
74                            const void *reg, size_t reg_size,
75                            void *val, size_t val_size)
76 {
77         struct device *dev = context;
78         struct i2c_client *i2c = to_i2c_client(dev);
79         struct i2c_msg xfer[2];
80         int ret;
81
82         xfer[0].addr = i2c->addr;
83         xfer[0].flags = 0;
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;
88 #endif
89
90         xfer[1].addr = i2c->addr;
91         xfer[1].flags = I2C_M_RD;
92         xfer[1].len = val_size;
93         xfer[1].buf = val;
94 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
95         xfer[1].scl_rate = 100*1000;
96 #endif
97
98         ret = i2c_transfer(i2c->adapter, xfer, 2);
99         if (ret == 2)
100                 return 0;
101         else if (ret < 0)
102                 return ret;
103         else
104                 return -EIO;
105 }
106
107 static struct regmap_bus regmap_i2c = {
108         .write = regmap_i2c_write,
109         .gather_write = regmap_i2c_gather_write,
110         .read = regmap_i2c_read,
111 };
112
113 /**
114  * regmap_init_i2c(): Initialise register map
115  *
116  * @i2c: Device that will be interacted with
117  * @config: Configuration for register map
118  *
119  * The return value will be an ERR_PTR() on error or a valid pointer to
120  * a struct regmap.
121  */
122 struct regmap *regmap_init_i2c(struct i2c_client *i2c,
123                                const struct regmap_config *config)
124 {
125         return regmap_init(&i2c->dev, &regmap_i2c, &i2c->dev, config);
126 }
127 EXPORT_SYMBOL_GPL(regmap_init_i2c);
128
129 /**
130  * devm_regmap_init_i2c(): Initialise managed register map
131  *
132  * @i2c: Device that will be interacted with
133  * @config: Configuration for register map
134  *
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.
138  */
139 struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
140                                     const struct regmap_config *config)
141 {
142         return devm_regmap_init(&i2c->dev, &regmap_i2c, &i2c->dev, config);
143 }
144 EXPORT_SYMBOL_GPL(devm_regmap_init_i2c);
145
146 MODULE_LICENSE("GPL");