2 * I2C driver for Marvell 88PM860x
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/regmap.h>
15 #include <linux/mfd/88pm860x.h>
17 int pm860x_reg_read(struct i2c_client
*i2c
, int reg
)
19 struct pm860x_chip
*chip
= i2c_get_clientdata(i2c
);
20 struct regmap
*map
= (i2c
== chip
->client
) ? chip
->regmap
21 : chip
->regmap_companion
;
25 ret
= regmap_read(map
, reg
, &data
);
31 EXPORT_SYMBOL(pm860x_reg_read
);
33 int pm860x_reg_write(struct i2c_client
*i2c
, int reg
,
36 struct pm860x_chip
*chip
= i2c_get_clientdata(i2c
);
37 struct regmap
*map
= (i2c
== chip
->client
) ? chip
->regmap
38 : chip
->regmap_companion
;
41 ret
= regmap_write(map
, reg
, data
);
44 EXPORT_SYMBOL(pm860x_reg_write
);
46 int pm860x_bulk_read(struct i2c_client
*i2c
, int reg
,
47 int count
, unsigned char *buf
)
49 struct pm860x_chip
*chip
= i2c_get_clientdata(i2c
);
50 struct regmap
*map
= (i2c
== chip
->client
) ? chip
->regmap
51 : chip
->regmap_companion
;
54 ret
= regmap_raw_read(map
, reg
, buf
, count
);
57 EXPORT_SYMBOL(pm860x_bulk_read
);
59 int pm860x_bulk_write(struct i2c_client
*i2c
, int reg
,
60 int count
, unsigned char *buf
)
62 struct pm860x_chip
*chip
= i2c_get_clientdata(i2c
);
63 struct regmap
*map
= (i2c
== chip
->client
) ? chip
->regmap
64 : chip
->regmap_companion
;
67 ret
= regmap_raw_write(map
, reg
, buf
, count
);
70 EXPORT_SYMBOL(pm860x_bulk_write
);
72 int pm860x_set_bits(struct i2c_client
*i2c
, int reg
,
73 unsigned char mask
, unsigned char data
)
75 struct pm860x_chip
*chip
= i2c_get_clientdata(i2c
);
76 struct regmap
*map
= (i2c
== chip
->client
) ? chip
->regmap
77 : chip
->regmap_companion
;
80 ret
= regmap_update_bits(map
, reg
, mask
, data
);
83 EXPORT_SYMBOL(pm860x_set_bits
);
85 static int read_device(struct i2c_client
*i2c
, int reg
,
86 int bytes
, void *dest
)
88 unsigned char msgbuf0
[I2C_SMBUS_BLOCK_MAX
+ 3];
89 unsigned char msgbuf1
[I2C_SMBUS_BLOCK_MAX
+ 2];
90 struct i2c_adapter
*adap
= i2c
->adapter
;
91 struct i2c_msg msg
[2] = {
104 int num
= 1, ret
= 0;
108 msgbuf0
[0] = (unsigned char)reg
; /* command */
111 /* if data needs to read back, num should be 2 */
114 ret
= adap
->algo
->master_xfer(adap
, msg
, num
);
115 memcpy(dest
, msgbuf1
, bytes
);
121 static int write_device(struct i2c_client
*i2c
, int reg
,
122 int bytes
, void *src
)
124 unsigned char buf
[bytes
+ 1];
125 struct i2c_adapter
*adap
= i2c
->adapter
;
129 buf
[0] = (unsigned char)reg
;
130 memcpy(&buf
[1], src
, bytes
);
131 msg
.addr
= i2c
->addr
;
136 ret
= adap
->algo
->master_xfer(adap
, &msg
, 1);
142 int pm860x_page_reg_read(struct i2c_client
*i2c
, int reg
)
144 unsigned char zero
= 0;
148 i2c_lock_adapter(i2c
->adapter
);
149 read_device(i2c
, 0xFA, 0, &zero
);
150 read_device(i2c
, 0xFB, 0, &zero
);
151 read_device(i2c
, 0xFF, 0, &zero
);
152 ret
= read_device(i2c
, reg
, 1, &data
);
155 read_device(i2c
, 0xFE, 0, &zero
);
156 read_device(i2c
, 0xFC, 0, &zero
);
157 i2c_unlock_adapter(i2c
->adapter
);
160 EXPORT_SYMBOL(pm860x_page_reg_read
);
162 int pm860x_page_reg_write(struct i2c_client
*i2c
, int reg
,
168 i2c_lock_adapter(i2c
->adapter
);
169 read_device(i2c
, 0xFA, 0, &zero
);
170 read_device(i2c
, 0xFB, 0, &zero
);
171 read_device(i2c
, 0xFF, 0, &zero
);
172 ret
= write_device(i2c
, reg
, 1, &data
);
173 read_device(i2c
, 0xFE, 0, &zero
);
174 read_device(i2c
, 0xFC, 0, &zero
);
175 i2c_unlock_adapter(i2c
->adapter
);
178 EXPORT_SYMBOL(pm860x_page_reg_write
);
180 int pm860x_page_bulk_read(struct i2c_client
*i2c
, int reg
,
181 int count
, unsigned char *buf
)
183 unsigned char zero
= 0;
186 i2c_lock_adapter(i2c
->adapter
);
187 read_device(i2c
, 0xfa, 0, &zero
);
188 read_device(i2c
, 0xfb, 0, &zero
);
189 read_device(i2c
, 0xff, 0, &zero
);
190 ret
= read_device(i2c
, reg
, count
, buf
);
191 read_device(i2c
, 0xFE, 0, &zero
);
192 read_device(i2c
, 0xFC, 0, &zero
);
193 i2c_unlock_adapter(i2c
->adapter
);
196 EXPORT_SYMBOL(pm860x_page_bulk_read
);
198 int pm860x_page_bulk_write(struct i2c_client
*i2c
, int reg
,
199 int count
, unsigned char *buf
)
201 unsigned char zero
= 0;
204 i2c_lock_adapter(i2c
->adapter
);
205 read_device(i2c
, 0xFA, 0, &zero
);
206 read_device(i2c
, 0xFB, 0, &zero
);
207 read_device(i2c
, 0xFF, 0, &zero
);
208 ret
= write_device(i2c
, reg
, count
, buf
);
209 read_device(i2c
, 0xFE, 0, &zero
);
210 read_device(i2c
, 0xFC, 0, &zero
);
211 i2c_unlock_adapter(i2c
->adapter
);
212 i2c_unlock_adapter(i2c
->adapter
);
215 EXPORT_SYMBOL(pm860x_page_bulk_write
);
217 int pm860x_page_set_bits(struct i2c_client
*i2c
, int reg
,
218 unsigned char mask
, unsigned char data
)
224 i2c_lock_adapter(i2c
->adapter
);
225 read_device(i2c
, 0xFA, 0, &zero
);
226 read_device(i2c
, 0xFB, 0, &zero
);
227 read_device(i2c
, 0xFF, 0, &zero
);
228 ret
= read_device(i2c
, reg
, 1, &value
);
233 ret
= write_device(i2c
, reg
, 1, &value
);
235 read_device(i2c
, 0xFE, 0, &zero
);
236 read_device(i2c
, 0xFC, 0, &zero
);
237 i2c_unlock_adapter(i2c
->adapter
);
240 EXPORT_SYMBOL(pm860x_page_set_bits
);