Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 1 | /* |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 2 | * I2C driver for Marvell 88PM860x |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009 Marvell International Ltd. |
Lee Jones | 0363be8 | 2014-05-08 12:55:35 +0100 | [diff] [blame] | 5 | * |
| 6 | * Author: Haojian Zhuang <haojian.zhuang@marvell.com> |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 7 | * |
| 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 Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 14 | #include <linux/i2c.h> |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 15 | #include <linux/regmap.h> |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 16 | #include <linux/mfd/88pm860x.h> |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 17 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 18 | int pm860x_reg_read(struct i2c_client *i2c, int reg) |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 19 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 20 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 21 | struct regmap *map = (i2c == chip->client) ? chip->regmap |
| 22 | : chip->regmap_companion; |
| 23 | unsigned int data; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 24 | int ret; |
| 25 | |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 26 | ret = regmap_read(map, reg, &data); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 27 | if (ret < 0) |
| 28 | return ret; |
| 29 | else |
| 30 | return (int)data; |
| 31 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 32 | EXPORT_SYMBOL(pm860x_reg_read); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 33 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 34 | int pm860x_reg_write(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 35 | unsigned char data) |
| 36 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 37 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 38 | struct regmap *map = (i2c == chip->client) ? chip->regmap |
| 39 | : chip->regmap_companion; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 40 | int ret; |
| 41 | |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 42 | ret = regmap_write(map, reg, data); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 43 | return ret; |
| 44 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 45 | EXPORT_SYMBOL(pm860x_reg_write); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 46 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 47 | int pm860x_bulk_read(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 48 | int count, unsigned char *buf) |
| 49 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 50 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 51 | struct regmap *map = (i2c == chip->client) ? chip->regmap |
| 52 | : chip->regmap_companion; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 53 | int ret; |
| 54 | |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 55 | ret = regmap_raw_read(map, reg, buf, count); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 56 | return ret; |
| 57 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 58 | EXPORT_SYMBOL(pm860x_bulk_read); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 59 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 60 | int pm860x_bulk_write(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 61 | int count, unsigned char *buf) |
| 62 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 63 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 64 | struct regmap *map = (i2c == chip->client) ? chip->regmap |
| 65 | : chip->regmap_companion; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 66 | int ret; |
| 67 | |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 68 | ret = regmap_raw_write(map, reg, buf, count); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 69 | return ret; |
| 70 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 71 | EXPORT_SYMBOL(pm860x_bulk_write); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 72 | |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 73 | int pm860x_set_bits(struct i2c_client *i2c, int reg, |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 74 | unsigned char mask, unsigned char data) |
| 75 | { |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 76 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 77 | struct regmap *map = (i2c == chip->client) ? chip->regmap |
| 78 | : chip->regmap_companion; |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 79 | int ret; |
| 80 | |
Jett.Zhou | b46a36c | 2011-11-11 15:38:27 +0800 | [diff] [blame] | 81 | ret = regmap_update_bits(map, reg, mask, data); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 82 | return ret; |
| 83 | } |
Haojian Zhuang | 53dbab7 | 2010-01-08 06:01:24 -0500 | [diff] [blame] | 84 | EXPORT_SYMBOL(pm860x_set_bits); |
Haojian Zhuang | bbd51b1 | 2010-01-06 17:04:18 -0500 | [diff] [blame] | 85 | |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 86 | static 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 D | 4894222 | 2012-09-17 21:14:58 +0530 | [diff] [blame] | 92 | 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.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 104 | }; |
| 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 | |
| 122 | static int write_device(struct i2c_client *i2c, int reg, |
| 123 | int bytes, void *src) |
| 124 | { |
| 125 | unsigned char buf[bytes + 1]; |
| 126 | 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 Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 143 | int pm860x_page_reg_read(struct i2c_client *i2c, int reg) |
| 144 | { |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 145 | unsigned char zero = 0; |
| 146 | unsigned char data; |
| 147 | int ret; |
| 148 | |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 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); |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 154 | if (ret >= 0) |
| 155 | ret = (int)data; |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 156 | read_device(i2c, 0xFE, 0, &zero); |
| 157 | read_device(i2c, 0xFC, 0, &zero); |
| 158 | i2c_unlock_adapter(i2c->adapter); |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 159 | return ret; |
| 160 | } |
| 161 | EXPORT_SYMBOL(pm860x_page_reg_read); |
| 162 | |
| 163 | int pm860x_page_reg_write(struct i2c_client *i2c, int reg, |
| 164 | unsigned char data) |
| 165 | { |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 166 | unsigned char zero; |
| 167 | int ret; |
| 168 | |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 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); |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 177 | return ret; |
| 178 | } |
| 179 | EXPORT_SYMBOL(pm860x_page_reg_write); |
| 180 | |
| 181 | int pm860x_page_bulk_read(struct i2c_client *i2c, int reg, |
| 182 | int count, unsigned char *buf) |
| 183 | { |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 184 | unsigned char zero = 0; |
| 185 | int ret; |
| 186 | |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 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); |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 195 | return ret; |
| 196 | } |
| 197 | EXPORT_SYMBOL(pm860x_page_bulk_read); |
| 198 | |
| 199 | int pm860x_page_bulk_write(struct i2c_client *i2c, int reg, |
| 200 | int count, unsigned char *buf) |
| 201 | { |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 202 | unsigned char zero = 0; |
| 203 | int ret; |
| 204 | |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 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); |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 214 | return ret; |
| 215 | } |
| 216 | EXPORT_SYMBOL(pm860x_page_bulk_write); |
| 217 | |
| 218 | int pm860x_page_set_bits(struct i2c_client *i2c, int reg, |
| 219 | unsigned char mask, unsigned char data) |
| 220 | { |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 221 | unsigned char zero; |
| 222 | unsigned char value; |
| 223 | int ret; |
| 224 | |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 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); |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 230 | if (ret < 0) |
| 231 | goto out; |
| 232 | value &= ~mask; |
| 233 | value |= data; |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 234 | ret = write_device(i2c, reg, 1, &value); |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 235 | out: |
Jett.Zhou | 5bdf741 | 2011-11-11 15:38:26 +0800 | [diff] [blame] | 236 | read_device(i2c, 0xFE, 0, &zero); |
| 237 | read_device(i2c, 0xFC, 0, &zero); |
| 238 | i2c_unlock_adapter(i2c->adapter); |
Haojian Zhuang | 09b0341 | 2011-03-07 23:43:16 +0800 | [diff] [blame] | 239 | return ret; |
| 240 | } |
| 241 | EXPORT_SYMBOL(pm860x_page_set_bits); |