blob: ff8f803ce8334d6e0bd27565601ef767b51007f0 [file] [log] [blame]
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05001/*
Haojian Zhuang53dbab72010-01-08 06:01:24 -05002 * I2C driver for Marvell 88PM860x
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05003 *
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
6 *
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.
10 */
11#include <linux/kernel.h>
12#include <linux/module.h>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050013#include <linux/i2c.h>
Jett.Zhoub46a36c2011-11-11 15:38:27 +080014#include <linux/regmap.h>
Haojian Zhuang53dbab72010-01-08 06:01:24 -050015#include <linux/mfd/88pm860x.h>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050016
Haojian Zhuang53dbab72010-01-08 06:01:24 -050017int pm860x_reg_read(struct i2c_client *i2c, int reg)
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050018{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050019 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080020 struct regmap *map = (i2c == chip->client) ? chip->regmap
21 : chip->regmap_companion;
22 unsigned int data;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050023 int ret;
24
Jett.Zhoub46a36c2011-11-11 15:38:27 +080025 ret = regmap_read(map, reg, &data);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050026 if (ret < 0)
27 return ret;
28 else
29 return (int)data;
30}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050031EXPORT_SYMBOL(pm860x_reg_read);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050032
Haojian Zhuang53dbab72010-01-08 06:01:24 -050033int pm860x_reg_write(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050034 unsigned char data)
35{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050036 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080037 struct regmap *map = (i2c == chip->client) ? chip->regmap
38 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050039 int ret;
40
Jett.Zhoub46a36c2011-11-11 15:38:27 +080041 ret = regmap_write(map, reg, data);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050042 return ret;
43}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050044EXPORT_SYMBOL(pm860x_reg_write);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050045
Haojian Zhuang53dbab72010-01-08 06:01:24 -050046int pm860x_bulk_read(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050047 int count, unsigned char *buf)
48{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050049 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080050 struct regmap *map = (i2c == chip->client) ? chip->regmap
51 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050052 int ret;
53
Jett.Zhoub46a36c2011-11-11 15:38:27 +080054 ret = regmap_raw_read(map, reg, buf, count);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050055 return ret;
56}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050057EXPORT_SYMBOL(pm860x_bulk_read);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050058
Haojian Zhuang53dbab72010-01-08 06:01:24 -050059int pm860x_bulk_write(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050060 int count, unsigned char *buf)
61{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050062 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080063 struct regmap *map = (i2c == chip->client) ? chip->regmap
64 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050065 int ret;
66
Jett.Zhoub46a36c2011-11-11 15:38:27 +080067 ret = regmap_raw_write(map, reg, buf, count);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050068 return ret;
69}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050070EXPORT_SYMBOL(pm860x_bulk_write);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050071
Haojian Zhuang53dbab72010-01-08 06:01:24 -050072int pm860x_set_bits(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050073 unsigned char mask, unsigned char data)
74{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050075 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080076 struct regmap *map = (i2c == chip->client) ? chip->regmap
77 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050078 int ret;
79
Jett.Zhoub46a36c2011-11-11 15:38:27 +080080 ret = regmap_update_bits(map, reg, mask, data);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050081 return ret;
82}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050083EXPORT_SYMBOL(pm860x_set_bits);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050084
Jett.Zhou5bdf7412011-11-11 15:38:26 +080085static int read_device(struct i2c_client *i2c, int reg,
86 int bytes, void *dest)
87{
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;
Shubhrajyoti D48942222012-09-17 21:14:58 +053091 struct i2c_msg msg[2] = {
92 {
93 .addr = i2c->addr,
94 .flags = 0,
95 .len = 1,
96 .buf = msgbuf0
97 },
98 { .addr = i2c->addr,
99 .flags = I2C_M_RD,
100 .len = 0,
101 .buf = msgbuf1
102 },
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800103 };
104 int num = 1, ret = 0;
105
106 if (dest == NULL)
107 return -EINVAL;
108 msgbuf0[0] = (unsigned char)reg; /* command */
109 msg[1].len = bytes;
110
111 /* if data needs to read back, num should be 2 */
112 if (bytes > 0)
113 num = 2;
114 ret = adap->algo->master_xfer(adap, msg, num);
115 memcpy(dest, msgbuf1, bytes);
116 if (ret < 0)
117 return ret;
118 return 0;
119}
120
121static int write_device(struct i2c_client *i2c, int reg,
122 int bytes, void *src)
123{
124 unsigned char buf[bytes + 1];
125 struct i2c_adapter *adap = i2c->adapter;
126 struct i2c_msg msg;
127 int ret;
128
129 buf[0] = (unsigned char)reg;
130 memcpy(&buf[1], src, bytes);
131 msg.addr = i2c->addr;
132 msg.flags = 0;
133 msg.len = bytes + 1;
134 msg.buf = buf;
135
136 ret = adap->algo->master_xfer(adap, &msg, 1);
137 if (ret < 0)
138 return ret;
139 return 0;
140}
141
Haojian Zhuang09b03412011-03-07 23:43:16 +0800142int pm860x_page_reg_read(struct i2c_client *i2c, int reg)
143{
Haojian Zhuang09b03412011-03-07 23:43:16 +0800144 unsigned char zero = 0;
145 unsigned char data;
146 int ret;
147
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800148 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);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800153 if (ret >= 0)
154 ret = (int)data;
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800155 read_device(i2c, 0xFE, 0, &zero);
156 read_device(i2c, 0xFC, 0, &zero);
157 i2c_unlock_adapter(i2c->adapter);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800158 return ret;
159}
160EXPORT_SYMBOL(pm860x_page_reg_read);
161
162int pm860x_page_reg_write(struct i2c_client *i2c, int reg,
163 unsigned char data)
164{
Haojian Zhuang09b03412011-03-07 23:43:16 +0800165 unsigned char zero;
166 int ret;
167
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800168 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);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800176 return ret;
177}
178EXPORT_SYMBOL(pm860x_page_reg_write);
179
180int pm860x_page_bulk_read(struct i2c_client *i2c, int reg,
181 int count, unsigned char *buf)
182{
Haojian Zhuang09b03412011-03-07 23:43:16 +0800183 unsigned char zero = 0;
184 int ret;
185
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800186 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);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800194 return ret;
195}
196EXPORT_SYMBOL(pm860x_page_bulk_read);
197
198int pm860x_page_bulk_write(struct i2c_client *i2c, int reg,
199 int count, unsigned char *buf)
200{
Haojian Zhuang09b03412011-03-07 23:43:16 +0800201 unsigned char zero = 0;
202 int ret;
203
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800204 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);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800213 return ret;
214}
215EXPORT_SYMBOL(pm860x_page_bulk_write);
216
217int pm860x_page_set_bits(struct i2c_client *i2c, int reg,
218 unsigned char mask, unsigned char data)
219{
Haojian Zhuang09b03412011-03-07 23:43:16 +0800220 unsigned char zero;
221 unsigned char value;
222 int ret;
223
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800224 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);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800229 if (ret < 0)
230 goto out;
231 value &= ~mask;
232 value |= data;
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800233 ret = write_device(i2c, reg, 1, &value);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800234out:
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800235 read_device(i2c, 0xFE, 0, &zero);
236 read_device(i2c, 0xFC, 0, &zero);
237 i2c_unlock_adapter(i2c->adapter);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800238 return ret;
239}
240EXPORT_SYMBOL(pm860x_page_set_bits);