blob: a000aed35755d707717e96497013464e4c67f365 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05002/*
Haojian Zhuang53dbab72010-01-08 06:01:24 -05003 * I2C driver for Marvell 88PM860x
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05004 *
5 * Copyright (C) 2009 Marvell International Ltd.
Lee Jones0363be82014-05-08 12:55:35 +01006 *
7 * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -05008 */
9#include <linux/kernel.h>
10#include <linux/module.h>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050011#include <linux/i2c.h>
Jett.Zhoub46a36c2011-11-11 15:38:27 +080012#include <linux/regmap.h>
Haojian Zhuang53dbab72010-01-08 06:01:24 -050013#include <linux/mfd/88pm860x.h>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050014
Haojian Zhuang53dbab72010-01-08 06:01:24 -050015int pm860x_reg_read(struct i2c_client *i2c, int reg)
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050016{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050017 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080018 struct regmap *map = (i2c == chip->client) ? chip->regmap
19 : chip->regmap_companion;
20 unsigned int data;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050021 int ret;
22
Jett.Zhoub46a36c2011-11-11 15:38:27 +080023 ret = regmap_read(map, reg, &data);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050024 if (ret < 0)
25 return ret;
26 else
27 return (int)data;
28}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050029EXPORT_SYMBOL(pm860x_reg_read);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050030
Haojian Zhuang53dbab72010-01-08 06:01:24 -050031int pm860x_reg_write(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050032 unsigned char data)
33{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050034 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080035 struct regmap *map = (i2c == chip->client) ? chip->regmap
36 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050037 int ret;
38
Jett.Zhoub46a36c2011-11-11 15:38:27 +080039 ret = regmap_write(map, reg, data);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050040 return ret;
41}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050042EXPORT_SYMBOL(pm860x_reg_write);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050043
Haojian Zhuang53dbab72010-01-08 06:01:24 -050044int pm860x_bulk_read(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050045 int count, unsigned char *buf)
46{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050047 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080048 struct regmap *map = (i2c == chip->client) ? chip->regmap
49 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050050 int ret;
51
Jett.Zhoub46a36c2011-11-11 15:38:27 +080052 ret = regmap_raw_read(map, reg, buf, count);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050053 return ret;
54}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050055EXPORT_SYMBOL(pm860x_bulk_read);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050056
Haojian Zhuang53dbab72010-01-08 06:01:24 -050057int pm860x_bulk_write(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050058 int count, unsigned char *buf)
59{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050060 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080061 struct regmap *map = (i2c == chip->client) ? chip->regmap
62 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050063 int ret;
64
Jett.Zhoub46a36c2011-11-11 15:38:27 +080065 ret = regmap_raw_write(map, reg, buf, count);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050066 return ret;
67}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050068EXPORT_SYMBOL(pm860x_bulk_write);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050069
Haojian Zhuang53dbab72010-01-08 06:01:24 -050070int pm860x_set_bits(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050071 unsigned char mask, unsigned char data)
72{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050073 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Jett.Zhoub46a36c2011-11-11 15:38:27 +080074 struct regmap *map = (i2c == chip->client) ? chip->regmap
75 : chip->regmap_companion;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050076 int ret;
77
Jett.Zhoub46a36c2011-11-11 15:38:27 +080078 ret = regmap_update_bits(map, reg, mask, data);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050079 return ret;
80}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050081EXPORT_SYMBOL(pm860x_set_bits);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050082
Jett.Zhou5bdf7412011-11-11 15:38:26 +080083static int read_device(struct i2c_client *i2c, int reg,
84 int bytes, void *dest)
85{
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;
Shubhrajyoti D48942222012-09-17 21:14:58 +053089 struct i2c_msg msg[2] = {
90 {
91 .addr = i2c->addr,
92 .flags = 0,
93 .len = 1,
94 .buf = msgbuf0
95 },
96 { .addr = i2c->addr,
97 .flags = I2C_M_RD,
98 .len = 0,
99 .buf = msgbuf1
100 },
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800101 };
102 int num = 1, ret = 0;
103
104 if (dest == NULL)
105 return -EINVAL;
106 msgbuf0[0] = (unsigned char)reg; /* command */
107 msg[1].len = bytes;
108
109 /* if data needs to read back, num should be 2 */
110 if (bytes > 0)
111 num = 2;
112 ret = adap->algo->master_xfer(adap, msg, num);
113 memcpy(dest, msgbuf1, bytes);
114 if (ret < 0)
115 return ret;
116 return 0;
117}
118
119static int write_device(struct i2c_client *i2c, int reg,
120 int bytes, void *src)
121{
Lee Jones7a36ceb2014-08-28 10:51:26 +0100122 unsigned char buf[2];
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800123 struct i2c_adapter *adap = i2c->adapter;
124 struct i2c_msg msg;
125 int ret;
126
127 buf[0] = (unsigned char)reg;
128 memcpy(&buf[1], src, bytes);
129 msg.addr = i2c->addr;
130 msg.flags = 0;
131 msg.len = bytes + 1;
132 msg.buf = buf;
133
134 ret = adap->algo->master_xfer(adap, &msg, 1);
135 if (ret < 0)
136 return ret;
137 return 0;
138}
139
Haojian Zhuang09b03412011-03-07 23:43:16 +0800140int pm860x_page_reg_write(struct i2c_client *i2c, int reg,
141 unsigned char data)
142{
Haojian Zhuang09b03412011-03-07 23:43:16 +0800143 unsigned char zero;
144 int ret;
145
Peter Rosin8c8f74f2018-06-20 07:18:02 +0200146 i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800147 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);
Peter Rosin8c8f74f2018-06-20 07:18:02 +0200153 i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800154 return ret;
155}
156EXPORT_SYMBOL(pm860x_page_reg_write);
157
158int pm860x_page_bulk_read(struct i2c_client *i2c, int reg,
159 int count, unsigned char *buf)
160{
Haojian Zhuang09b03412011-03-07 23:43:16 +0800161 unsigned char zero = 0;
162 int ret;
163
Peter Rosin8c8f74f2018-06-20 07:18:02 +0200164 i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
Jett.Zhou5bdf7412011-11-11 15:38:26 +0800165 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);
Peter Rosin8c8f74f2018-06-20 07:18:02 +0200171 i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
Haojian Zhuang09b03412011-03-07 23:43:16 +0800172 return ret;
173}
174EXPORT_SYMBOL(pm860x_page_bulk_read);