blob: 84e313107233ee8b1e86f0610be093eaa5e1637d [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.
Lee Jones0363be82014-05-08 12:55:35 +01005 *
6 * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05007 *
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#include <linux/kernel.h>
13#include <linux/module.h>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050014#include <linux/i2c.h>
Jett.Zhoub46a36c2011-11-11 15:38:27 +080015#include <linux/regmap.h>
Haojian Zhuang53dbab72010-01-08 06:01:24 -050016#include <linux/mfd/88pm860x.h>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050017
Haojian Zhuang53dbab72010-01-08 06:01:24 -050018int pm860x_reg_read(struct i2c_client *i2c, int reg)
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050019{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050020 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080021 struct regmap *map = (i2c == chip->client) ? chip->regmap
22 : chip->regmap_companion;
23 unsigned int data;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050024 int ret;
25
Jett.Zhoub46a36c2011-11-11 15:38:27 +080026 ret = regmap_read(map, reg, &data);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050027 if (ret < 0)
28 return ret;
29 else
30 return (int)data;
31}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050032EXPORT_SYMBOL(pm860x_reg_read);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050033
Haojian Zhuang53dbab72010-01-08 06:01:24 -050034int pm860x_reg_write(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050035 unsigned char data)
36{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050037 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080038 struct regmap *map = (i2c == chip->client) ? chip->regmap
39 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050040 int ret;
41
Jett.Zhoub46a36c2011-11-11 15:38:27 +080042 ret = regmap_write(map, reg, data);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050043 return ret;
44}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050045EXPORT_SYMBOL(pm860x_reg_write);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050046
Haojian Zhuang53dbab72010-01-08 06:01:24 -050047int pm860x_bulk_read(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050048 int count, unsigned char *buf)
49{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050050 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080051 struct regmap *map = (i2c == chip->client) ? chip->regmap
52 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050053 int ret;
54
Jett.Zhoub46a36c2011-11-11 15:38:27 +080055 ret = regmap_raw_read(map, reg, buf, count);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050056 return ret;
57}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050058EXPORT_SYMBOL(pm860x_bulk_read);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050059
Haojian Zhuang53dbab72010-01-08 06:01:24 -050060int pm860x_bulk_write(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050061 int count, unsigned char *buf)
62{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050063 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080064 struct regmap *map = (i2c == chip->client) ? chip->regmap
65 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050066 int ret;
67
Jett.Zhoub46a36c2011-11-11 15:38:27 +080068 ret = regmap_raw_write(map, reg, buf, count);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050069 return ret;
70}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050071EXPORT_SYMBOL(pm860x_bulk_write);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050072
Haojian Zhuang53dbab72010-01-08 06:01:24 -050073int pm860x_set_bits(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050074 unsigned char mask, unsigned char data)
75{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050076 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080077 struct regmap *map = (i2c == chip->client) ? chip->regmap
78 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050079 int ret;
80
Jett.Zhoub46a36c2011-11-11 15:38:27 +080081 ret = regmap_update_bits(map, reg, mask, data);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050082 return ret;
83}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050084EXPORT_SYMBOL(pm860x_set_bits);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050085
Jett.Zhou5bdf7412011-11-11 15:38:26 +080086static int read_device(struct i2c_client *i2c, int reg,
87 int bytes, void *dest)
88{
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;
Shubhrajyoti D48942222012-09-17 21:14:58 +053092 struct i2c_msg msg[2] = {
93 {
94 .addr = i2c->addr,
95 .flags = 0,
96 .len = 1,
97 .buf = msgbuf0
98 },
99 { .addr = i2c->addr,
100 .flags = I2C_M_RD,
101 .len = 0,
102 .buf = msgbuf1
103 },
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800104 };
105 int num = 1, ret = 0;
106
107 if (dest == NULL)
108 return -EINVAL;
109 msgbuf0[0] = (unsigned char)reg; /* command */
110 msg[1].len = bytes;
111
112 /* if data needs to read back, num should be 2 */
113 if (bytes > 0)
114 num = 2;
115 ret = adap->algo->master_xfer(adap, msg, num);
116 memcpy(dest, msgbuf1, bytes);
117 if (ret < 0)
118 return ret;
119 return 0;
120}
121
122static int write_device(struct i2c_client *i2c, int reg,
123 int bytes, void *src)
124{
Lee Jones7a36ceb2014-08-28 10:51:26 +0100125 unsigned char buf[2];
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800126 struct i2c_adapter *adap = i2c->adapter;
127 struct i2c_msg msg;
128 int ret;
129
130 buf[0] = (unsigned char)reg;
131 memcpy(&buf[1], src, bytes);
132 msg.addr = i2c->addr;
133 msg.flags = 0;
134 msg.len = bytes + 1;
135 msg.buf = buf;
136
137 ret = adap->algo->master_xfer(adap, &msg, 1);
138 if (ret < 0)
139 return ret;
140 return 0;
141}
142
Haojian Zhuang09b03412011-03-07 23:43:16 +0800143int pm860x_page_reg_write(struct i2c_client *i2c, int reg,
144 unsigned char data)
145{
Haojian Zhuang09b03412011-03-07 23:43:16 +0800146 unsigned char zero;
147 int ret;
148
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800149 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 = write_device(i2c, reg, 1, &data);
154 read_device(i2c, 0xFE, 0, &zero);
155 read_device(i2c, 0xFC, 0, &zero);
156 i2c_unlock_adapter(i2c->adapter);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800157 return ret;
158}
159EXPORT_SYMBOL(pm860x_page_reg_write);
160
161int pm860x_page_bulk_read(struct i2c_client *i2c, int reg,
162 int count, unsigned char *buf)
163{
Haojian Zhuang09b03412011-03-07 23:43:16 +0800164 unsigned char zero = 0;
165 int ret;
166
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800167 i2c_lock_adapter(i2c->adapter);
168 read_device(i2c, 0xfa, 0, &zero);
169 read_device(i2c, 0xfb, 0, &zero);
170 read_device(i2c, 0xff, 0, &zero);
171 ret = read_device(i2c, reg, count, buf);
172 read_device(i2c, 0xFE, 0, &zero);
173 read_device(i2c, 0xFC, 0, &zero);
174 i2c_unlock_adapter(i2c->adapter);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800175 return ret;
176}
177EXPORT_SYMBOL(pm860x_page_bulk_read);