1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C driver for Marvell 88PM860x
5 * Copyright (C) 2009 Marvell International Ltd.
7 * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/regmap.h>
13 #include <linux/mfd/88pm860x.h>
15 int pm860x_reg_read(struct i2c_client *i2c, int reg)
17 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
18 struct regmap *map = (i2c == chip->client) ? chip->regmap
19 : chip->regmap_companion;
23 ret = regmap_read(map, reg, &data);
29 EXPORT_SYMBOL(pm860x_reg_read);
31 int pm860x_reg_write(struct i2c_client *i2c, int reg,
34 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
35 struct regmap *map = (i2c == chip->client) ? chip->regmap
36 : chip->regmap_companion;
39 ret = regmap_write(map, reg, data);
42 EXPORT_SYMBOL(pm860x_reg_write);
44 int pm860x_bulk_read(struct i2c_client *i2c, int reg,
45 int count, unsigned char *buf)
47 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
48 struct regmap *map = (i2c == chip->client) ? chip->regmap
49 : chip->regmap_companion;
52 ret = regmap_raw_read(map, reg, buf, count);
55 EXPORT_SYMBOL(pm860x_bulk_read);
57 int pm860x_bulk_write(struct i2c_client *i2c, int reg,
58 int count, unsigned char *buf)
60 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
61 struct regmap *map = (i2c == chip->client) ? chip->regmap
62 : chip->regmap_companion;
65 ret = regmap_raw_write(map, reg, buf, count);
68 EXPORT_SYMBOL(pm860x_bulk_write);
70 int pm860x_set_bits(struct i2c_client *i2c, int reg,
71 unsigned char mask, unsigned char data)
73 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
74 struct regmap *map = (i2c == chip->client) ? chip->regmap
75 : chip->regmap_companion;
78 ret = regmap_update_bits(map, reg, mask, data);
81 EXPORT_SYMBOL(pm860x_set_bits);
83 static int read_device(struct i2c_client *i2c, int reg,
84 int bytes, void *dest)
86 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX + 3];
87 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX + 2];
88 struct i2c_adapter *adap = i2c->adapter;
89 struct i2c_msg msg[2] = {
102 int num = 1, ret = 0;
106 msgbuf0[0] = (unsigned char)reg; /* command */
109 /* if data needs to read back, num should be 2 */
112 ret = adap->algo->master_xfer(adap, msg, num);
113 memcpy(dest, msgbuf1, bytes);
119 static int write_device(struct i2c_client *i2c, int reg,
120 int bytes, void *src)
122 unsigned char buf[2];
123 struct i2c_adapter *adap = i2c->adapter;
127 buf[0] = (unsigned char)reg;
128 memcpy(&buf[1], src, bytes);
129 msg.addr = i2c->addr;
134 ret = adap->algo->master_xfer(adap, &msg, 1);
140 int pm860x_page_reg_write(struct i2c_client *i2c, int reg,
146 i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
147 read_device(i2c, 0xFA, 0, &zero);
148 read_device(i2c, 0xFB, 0, &zero);
149 read_device(i2c, 0xFF, 0, &zero);
150 ret = write_device(i2c, reg, 1, &data);
151 read_device(i2c, 0xFE, 0, &zero);
152 read_device(i2c, 0xFC, 0, &zero);
153 i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
156 EXPORT_SYMBOL(pm860x_page_reg_write);
158 int pm860x_page_bulk_read(struct i2c_client *i2c, int reg,
159 int count, unsigned char *buf)
161 unsigned char zero = 0;
164 i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
165 read_device(i2c, 0xfa, 0, &zero);
166 read_device(i2c, 0xfb, 0, &zero);
167 read_device(i2c, 0xff, 0, &zero);
168 ret = read_device(i2c, reg, count, buf);
169 read_device(i2c, 0xFE, 0, &zero);
170 read_device(i2c, 0xFC, 0, &zero);
171 i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
174 EXPORT_SYMBOL(pm860x_page_bulk_read);