diff options
Diffstat (limited to 'drivers/mfd/88pm860x-i2c.c')
-rw-r--r-- | drivers/mfd/88pm860x-i2c.c | 172 |
1 files changed, 119 insertions, 53 deletions
diff --git a/drivers/mfd/88pm860x-i2c.c b/drivers/mfd/88pm860x-i2c.c index dda23cbfe415..6d7dba2bce8a 100644 --- a/drivers/mfd/88pm860x-i2c.c +++ b/drivers/mfd/88pm860x-i2c.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * I2C driver for Marvell 88PM8607 | 2 | * I2C driver for Marvell 88PM860x |
3 | * | 3 | * |
4 | * Copyright (C) 2009 Marvell International Ltd. | 4 | * Copyright (C) 2009 Marvell International Ltd. |
5 | * Haojian Zhuang <haojian.zhuang@marvell.com> | 5 | * Haojian Zhuang <haojian.zhuang@marvell.com> |
@@ -12,12 +12,11 @@ | |||
12 | #include <linux/module.h> | 12 | #include <linux/module.h> |
13 | #include <linux/platform_device.h> | 13 | #include <linux/platform_device.h> |
14 | #include <linux/i2c.h> | 14 | #include <linux/i2c.h> |
15 | #include <linux/mfd/88pm8607.h> | 15 | #include <linux/mfd/88pm860x.h> |
16 | 16 | ||
17 | static inline int pm8607_read_device(struct pm8607_chip *chip, | 17 | static inline int pm860x_read_device(struct i2c_client *i2c, |
18 | int reg, int bytes, void *dest) | 18 | int reg, int bytes, void *dest) |
19 | { | 19 | { |
20 | struct i2c_client *i2c = chip->client; | ||
21 | unsigned char data; | 20 | unsigned char data; |
22 | int ret; | 21 | int ret; |
23 | 22 | ||
@@ -32,10 +31,9 @@ static inline int pm8607_read_device(struct pm8607_chip *chip, | |||
32 | return 0; | 31 | return 0; |
33 | } | 32 | } |
34 | 33 | ||
35 | static inline int pm8607_write_device(struct pm8607_chip *chip, | 34 | static inline int pm860x_write_device(struct i2c_client *i2c, |
36 | int reg, int bytes, void *src) | 35 | int reg, int bytes, void *src) |
37 | { | 36 | { |
38 | struct i2c_client *i2c = chip->client; | ||
39 | unsigned char buf[bytes + 1]; | 37 | unsigned char buf[bytes + 1]; |
40 | int ret; | 38 | int ret; |
41 | 39 | ||
@@ -48,13 +46,14 @@ static inline int pm8607_write_device(struct pm8607_chip *chip, | |||
48 | return 0; | 46 | return 0; |
49 | } | 47 | } |
50 | 48 | ||
51 | int pm8607_reg_read(struct pm8607_chip *chip, int reg) | 49 | int pm860x_reg_read(struct i2c_client *i2c, int reg) |
52 | { | 50 | { |
51 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); | ||
53 | unsigned char data; | 52 | unsigned char data; |
54 | int ret; | 53 | int ret; |
55 | 54 | ||
56 | mutex_lock(&chip->io_lock); | 55 | mutex_lock(&chip->io_lock); |
57 | ret = chip->read(chip, reg, 1, &data); | 56 | ret = pm860x_read_device(i2c, reg, 1, &data); |
58 | mutex_unlock(&chip->io_lock); | 57 | mutex_unlock(&chip->io_lock); |
59 | 58 | ||
60 | if (ret < 0) | 59 | if (ret < 0) |
@@ -62,111 +61,178 @@ int pm8607_reg_read(struct pm8607_chip *chip, int reg) | |||
62 | else | 61 | else |
63 | return (int)data; | 62 | return (int)data; |
64 | } | 63 | } |
65 | EXPORT_SYMBOL(pm8607_reg_read); | 64 | EXPORT_SYMBOL(pm860x_reg_read); |
66 | 65 | ||
67 | int pm8607_reg_write(struct pm8607_chip *chip, int reg, | 66 | int pm860x_reg_write(struct i2c_client *i2c, int reg, |
68 | unsigned char data) | 67 | unsigned char data) |
69 | { | 68 | { |
69 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); | ||
70 | int ret; | 70 | int ret; |
71 | 71 | ||
72 | mutex_lock(&chip->io_lock); | 72 | mutex_lock(&chip->io_lock); |
73 | ret = chip->write(chip, reg, 1, &data); | 73 | ret = pm860x_write_device(i2c, reg, 1, &data); |
74 | mutex_unlock(&chip->io_lock); | 74 | mutex_unlock(&chip->io_lock); |
75 | 75 | ||
76 | return ret; | 76 | return ret; |
77 | } | 77 | } |
78 | EXPORT_SYMBOL(pm8607_reg_write); | 78 | EXPORT_SYMBOL(pm860x_reg_write); |
79 | 79 | ||
80 | int pm8607_bulk_read(struct pm8607_chip *chip, int reg, | 80 | int pm860x_bulk_read(struct i2c_client *i2c, int reg, |
81 | int count, unsigned char *buf) | 81 | int count, unsigned char *buf) |
82 | { | 82 | { |
83 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); | ||
83 | int ret; | 84 | int ret; |
84 | 85 | ||
85 | mutex_lock(&chip->io_lock); | 86 | mutex_lock(&chip->io_lock); |
86 | ret = chip->read(chip, reg, count, buf); | 87 | ret = pm860x_read_device(i2c, reg, count, buf); |
87 | mutex_unlock(&chip->io_lock); | 88 | mutex_unlock(&chip->io_lock); |
88 | 89 | ||
89 | return ret; | 90 | return ret; |
90 | } | 91 | } |
91 | EXPORT_SYMBOL(pm8607_bulk_read); | 92 | EXPORT_SYMBOL(pm860x_bulk_read); |
92 | 93 | ||
93 | int pm8607_bulk_write(struct pm8607_chip *chip, int reg, | 94 | int pm860x_bulk_write(struct i2c_client *i2c, int reg, |
94 | int count, unsigned char *buf) | 95 | int count, unsigned char *buf) |
95 | { | 96 | { |
97 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); | ||
96 | int ret; | 98 | int ret; |
97 | 99 | ||
98 | mutex_lock(&chip->io_lock); | 100 | mutex_lock(&chip->io_lock); |
99 | ret = chip->write(chip, reg, count, buf); | 101 | ret = pm860x_write_device(i2c, reg, count, buf); |
100 | mutex_unlock(&chip->io_lock); | 102 | mutex_unlock(&chip->io_lock); |
101 | 103 | ||
102 | return ret; | 104 | return ret; |
103 | } | 105 | } |
104 | EXPORT_SYMBOL(pm8607_bulk_write); | 106 | EXPORT_SYMBOL(pm860x_bulk_write); |
105 | 107 | ||
106 | int pm8607_set_bits(struct pm8607_chip *chip, int reg, | 108 | int pm860x_set_bits(struct i2c_client *i2c, int reg, |
107 | unsigned char mask, unsigned char data) | 109 | unsigned char mask, unsigned char data) |
108 | { | 110 | { |
111 | struct pm860x_chip *chip = i2c_get_clientdata(i2c); | ||
109 | unsigned char value; | 112 | unsigned char value; |
110 | int ret; | 113 | int ret; |
111 | 114 | ||
112 | mutex_lock(&chip->io_lock); | 115 | mutex_lock(&chip->io_lock); |
113 | ret = chip->read(chip, reg, 1, &value); | 116 | ret = pm860x_read_device(i2c, reg, 1, &value); |
114 | if (ret < 0) | 117 | if (ret < 0) |
115 | goto out; | 118 | goto out; |
116 | value &= ~mask; | 119 | value &= ~mask; |
117 | value |= data; | 120 | value |= data; |
118 | ret = chip->write(chip, reg, 1, &value); | 121 | ret = pm860x_write_device(i2c, reg, 1, &value); |
119 | out: | 122 | out: |
120 | mutex_unlock(&chip->io_lock); | 123 | mutex_unlock(&chip->io_lock); |
121 | return ret; | 124 | return ret; |
122 | } | 125 | } |
123 | EXPORT_SYMBOL(pm8607_set_bits); | 126 | EXPORT_SYMBOL(pm860x_set_bits); |
124 | 127 | ||
125 | 128 | ||
126 | static const struct i2c_device_id pm860x_id_table[] = { | 129 | static const struct i2c_device_id pm860x_id_table[] = { |
127 | { "88PM8607", 0 }, | 130 | { "88PM860x", 0 }, |
128 | {} | 131 | {} |
129 | }; | 132 | }; |
130 | MODULE_DEVICE_TABLE(i2c, pm860x_id_table); | 133 | MODULE_DEVICE_TABLE(i2c, pm860x_id_table); |
131 | 134 | ||
135 | static int verify_addr(struct i2c_client *i2c) | ||
136 | { | ||
137 | unsigned short addr_8607[] = {0x30, 0x34}; | ||
138 | unsigned short addr_8606[] = {0x10, 0x11}; | ||
139 | int size, i; | ||
140 | |||
141 | if (i2c == NULL) | ||
142 | return 0; | ||
143 | size = ARRAY_SIZE(addr_8606); | ||
144 | for (i = 0; i < size; i++) { | ||
145 | if (i2c->addr == *(addr_8606 + i)) | ||
146 | return CHIP_PM8606; | ||
147 | } | ||
148 | size = ARRAY_SIZE(addr_8607); | ||
149 | for (i = 0; i < size; i++) { | ||
150 | if (i2c->addr == *(addr_8607 + i)) | ||
151 | return CHIP_PM8607; | ||
152 | } | ||
153 | return 0; | ||
154 | } | ||
155 | |||
132 | static int __devinit pm860x_probe(struct i2c_client *client, | 156 | static int __devinit pm860x_probe(struct i2c_client *client, |
133 | const struct i2c_device_id *id) | 157 | const struct i2c_device_id *id) |
134 | { | 158 | { |
135 | struct pm8607_platform_data *pdata = client->dev.platform_data; | 159 | struct pm860x_platform_data *pdata = client->dev.platform_data; |
136 | struct pm8607_chip *chip; | 160 | static struct pm860x_chip *chip; |
137 | int ret; | 161 | struct i2c_board_info i2c_info = { |
138 | 162 | .type = "88PM860x", | |
139 | chip = kzalloc(sizeof(struct pm8607_chip), GFP_KERNEL); | 163 | .platform_data = client->dev.platform_data, |
140 | if (chip == NULL) | 164 | }; |
141 | return -ENOMEM; | 165 | int addr_c, found_companion = 0; |
142 | 166 | ||
143 | chip->client = client; | 167 | if (pdata == NULL) { |
144 | chip->dev = &client->dev; | 168 | pr_info("No platform data in %s!\n", __func__); |
145 | chip->read = pm8607_read_device; | 169 | return -EINVAL; |
146 | chip->write = pm8607_write_device; | 170 | } |
147 | memcpy(&chip->id, id, sizeof(struct i2c_device_id)); | 171 | |
148 | i2c_set_clientdata(client, chip); | 172 | /* |
149 | 173 | * Both client and companion client shares same platform driver. | |
150 | mutex_init(&chip->io_lock); | 174 | * Driver distinguishes them by pdata->companion_addr. |
151 | dev_set_drvdata(chip->dev, chip); | 175 | * pdata->companion_addr is only assigned if companion chip exists. |
152 | 176 | * At the same time, the companion_addr shouldn't equal to client | |
153 | ret = pm860x_device_init(chip, pdata); | 177 | * address. |
154 | if (ret < 0) | 178 | */ |
155 | goto out; | 179 | addr_c = pdata->companion_addr; |
156 | 180 | if (addr_c && (addr_c != client->addr)) { | |
157 | 181 | i2c_info.addr = addr_c; | |
182 | found_companion = 1; | ||
183 | } | ||
184 | |||
185 | if (found_companion || (addr_c == 0)) { | ||
186 | chip = kzalloc(sizeof(struct pm860x_chip), GFP_KERNEL); | ||
187 | if (chip == NULL) | ||
188 | return -ENOMEM; | ||
189 | |||
190 | chip->id = verify_addr(client); | ||
191 | chip->companion_addr = addr_c; | ||
192 | chip->client = client; | ||
193 | i2c_set_clientdata(client, chip); | ||
194 | chip->dev = &client->dev; | ||
195 | mutex_init(&chip->io_lock); | ||
196 | dev_set_drvdata(chip->dev, chip); | ||
197 | |||
198 | if (found_companion) { | ||
199 | /* | ||
200 | * If this driver is built in, probe function is | ||
201 | * recursive. | ||
202 | * If this driver is built as module, the next probe | ||
203 | * function is called after the first one finished. | ||
204 | */ | ||
205 | chip->companion = i2c_new_device(client->adapter, | ||
206 | &i2c_info); | ||
207 | } | ||
208 | } | ||
209 | |||
210 | /* | ||
211 | * If companion chip existes, it's called by companion probe. | ||
212 | * If there's no companion chip, it's called by client probe. | ||
213 | */ | ||
214 | if ((addr_c == 0) || (addr_c == client->addr)) { | ||
215 | chip->companion = client; | ||
216 | i2c_set_clientdata(chip->companion, chip); | ||
217 | pm860x_device_init(chip, pdata); | ||
218 | } | ||
158 | return 0; | 219 | return 0; |
159 | |||
160 | out: | ||
161 | i2c_set_clientdata(client, NULL); | ||
162 | kfree(chip); | ||
163 | return ret; | ||
164 | } | 220 | } |
165 | 221 | ||
166 | static int __devexit pm860x_remove(struct i2c_client *client) | 222 | static int __devexit pm860x_remove(struct i2c_client *client) |
167 | { | 223 | { |
168 | struct pm8607_chip *chip = i2c_get_clientdata(client); | 224 | struct pm860x_chip *chip = i2c_get_clientdata(client); |
169 | 225 | ||
226 | /* | ||
227 | * If companion existes, companion client is removed first. | ||
228 | * Because companion client is registered last and removed first. | ||
229 | */ | ||
230 | if (chip->companion_addr == client->addr) | ||
231 | return 0; | ||
232 | pm860x_device_exit(chip); | ||
233 | i2c_unregister_device(chip->companion); | ||
234 | i2c_set_clientdata(chip->companion, NULL); | ||
235 | i2c_set_clientdata(chip->client, NULL); | ||
170 | kfree(chip); | 236 | kfree(chip); |
171 | return 0; | 237 | return 0; |
172 | } | 238 | } |