2 * I2C driver for Marvell 88PM860x
4 * Copyright (C) 2009 Marvell International Ltd.
6 * Author: Haojian Zhuang <haojian.zhuang@marvell.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.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/regmap.h>
16 #include <linux/mfd/88pm860x.h>
18 int pm860x_reg_read(struct i2c_client *i2c, int reg)
20 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
21 struct regmap *map = (i2c == chip->client) ? chip->regmap
22 : chip->regmap_companion;
26 ret = regmap_read(map, reg, &data);
32 EXPORT_SYMBOL(pm860x_reg_read);
34 int pm860x_reg_write(struct i2c_client *i2c, int reg,
37 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
38 struct regmap *map = (i2c == chip->client) ? chip->regmap
39 : chip->regmap_companion;
42 ret = regmap_write(map, reg, data);
45 EXPORT_SYMBOL(pm860x_reg_write);
47 int pm860x_bulk_read(struct i2c_client *i2c, int reg,
48 int count, unsigned char *buf)
50 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
51 struct regmap *map = (i2c == chip->client) ? chip->regmap
52 : chip->regmap_companion;
55 ret = regmap_raw_read(map, reg, buf, count);
58 EXPORT_SYMBOL(pm860x_bulk_read);
60 int pm860x_bulk_write(struct i2c_client *i2c, int reg,
61 int count, unsigned char *buf)
63 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
64 struct regmap *map = (i2c == chip->client) ? chip->regmap
65 : chip->regmap_companion;
68 ret = regmap_raw_write(map, reg, buf, count);
71 EXPORT_SYMBOL(pm860x_bulk_write);
73 int pm860x_set_bits(struct i2c_client *i2c, int reg,
74 unsigned char mask, unsigned char data)
76 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
77 struct regmap *map = (i2c == chip->client) ? chip->regmap
78 : chip->regmap_companion;
81 ret = regmap_update_bits(map, reg, mask, data);
84 EXPORT_SYMBOL(pm860x_set_bits);
86 static int read_device(struct i2c_client *i2c, int reg,
87 int bytes, void *dest)
89 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX + 3];
90 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX + 2];
91 struct i2c_adapter *adap = i2c->adapter;
92 struct i2c_msg msg[2] = {
105 int num = 1, ret = 0;
109 msgbuf0[0] = (unsigned char)reg; /* command */
112 /* if data needs to read back, num should be 2 */
115 ret = adap->algo->master_xfer(adap, msg, num);
116 memcpy(dest, msgbuf1, bytes);
122 static int write_device(struct i2c_client *i2c, int reg,
123 int bytes, void *src)
125 unsigned char buf[bytes + 1];
126 struct i2c_adapter *adap = i2c->adapter;
130 buf[0] = (unsigned char)reg;
131 memcpy(&buf[1], src, bytes);
132 msg.addr = i2c->addr;
137 ret = adap->algo->master_xfer(adap, &msg, 1);
143 int pm860x_page_reg_read(struct i2c_client *i2c, int reg)
145 unsigned char zero = 0;
149 i2c_lock_adapter(i2c->adapter);
150 read_device(i2c, 0xFA, 0, &zero);
151 read_device(i2c, 0xFB, 0, &zero);
152 read_device(i2c, 0xFF, 0, &zero);
153 ret = read_device(i2c, reg, 1, &data);
156 read_device(i2c, 0xFE, 0, &zero);
157 read_device(i2c, 0xFC, 0, &zero);
158 i2c_unlock_adapter(i2c->adapter);
161 EXPORT_SYMBOL(pm860x_page_reg_read);
163 int pm860x_page_reg_write(struct i2c_client *i2c, int reg,
169 i2c_lock_adapter(i2c->adapter);
170 read_device(i2c, 0xFA, 0, &zero);
171 read_device(i2c, 0xFB, 0, &zero);
172 read_device(i2c, 0xFF, 0, &zero);
173 ret = write_device(i2c, reg, 1, &data);
174 read_device(i2c, 0xFE, 0, &zero);
175 read_device(i2c, 0xFC, 0, &zero);
176 i2c_unlock_adapter(i2c->adapter);
179 EXPORT_SYMBOL(pm860x_page_reg_write);
181 int pm860x_page_bulk_read(struct i2c_client *i2c, int reg,
182 int count, unsigned char *buf)
184 unsigned char zero = 0;
187 i2c_lock_adapter(i2c->adapter);
188 read_device(i2c, 0xfa, 0, &zero);
189 read_device(i2c, 0xfb, 0, &zero);
190 read_device(i2c, 0xff, 0, &zero);
191 ret = read_device(i2c, reg, count, buf);
192 read_device(i2c, 0xFE, 0, &zero);
193 read_device(i2c, 0xFC, 0, &zero);
194 i2c_unlock_adapter(i2c->adapter);
197 EXPORT_SYMBOL(pm860x_page_bulk_read);
199 int pm860x_page_bulk_write(struct i2c_client *i2c, int reg,
200 int count, unsigned char *buf)
202 unsigned char zero = 0;
205 i2c_lock_adapter(i2c->adapter);
206 read_device(i2c, 0xFA, 0, &zero);
207 read_device(i2c, 0xFB, 0, &zero);
208 read_device(i2c, 0xFF, 0, &zero);
209 ret = write_device(i2c, reg, count, buf);
210 read_device(i2c, 0xFE, 0, &zero);
211 read_device(i2c, 0xFC, 0, &zero);
212 i2c_unlock_adapter(i2c->adapter);
213 i2c_unlock_adapter(i2c->adapter);
216 EXPORT_SYMBOL(pm860x_page_bulk_write);
218 int pm860x_page_set_bits(struct i2c_client *i2c, int reg,
219 unsigned char mask, unsigned char data)
225 i2c_lock_adapter(i2c->adapter);
226 read_device(i2c, 0xFA, 0, &zero);
227 read_device(i2c, 0xFB, 0, &zero);
228 read_device(i2c, 0xFF, 0, &zero);
229 ret = read_device(i2c, reg, 1, &value);
234 ret = write_device(i2c, reg, 1, &value);
236 read_device(i2c, 0xFE, 0, &zero);
237 read_device(i2c, 0xFC, 0, &zero);
238 i2c_unlock_adapter(i2c->adapter);
241 EXPORT_SYMBOL(pm860x_page_set_bits);