blob: 4a6e7186334e31ce4eccea8b0937312c20b5e29f [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>
13#include <linux/platform_device.h>
14#include <linux/i2c.h>
Haojian Zhuang53dbab72010-01-08 06:01:24 -050015#include <linux/mfd/88pm860x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050017
Haojian Zhuang53dbab72010-01-08 06:01:24 -050018static inline int pm860x_read_device(struct i2c_client *i2c,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050019 int reg, int bytes, void *dest)
20{
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050021 unsigned char data;
22 int ret;
23
24 data = (unsigned char)reg;
25 ret = i2c_master_send(i2c, &data, 1);
26 if (ret < 0)
27 return ret;
28
29 ret = i2c_master_recv(i2c, dest, bytes);
30 if (ret < 0)
31 return ret;
32 return 0;
33}
34
Haojian Zhuang53dbab72010-01-08 06:01:24 -050035static inline int pm860x_write_device(struct i2c_client *i2c,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050036 int reg, int bytes, void *src)
37{
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050038 unsigned char buf[bytes + 1];
39 int ret;
40
41 buf[0] = (unsigned char)reg;
42 memcpy(&buf[1], src, bytes);
43
44 ret = i2c_master_send(i2c, buf, bytes + 1);
45 if (ret < 0)
46 return ret;
47 return 0;
48}
49
Haojian Zhuang53dbab72010-01-08 06:01:24 -050050int pm860x_reg_read(struct i2c_client *i2c, int reg)
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050051{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050052 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050053 unsigned char data;
54 int ret;
55
56 mutex_lock(&chip->io_lock);
Haojian Zhuang53dbab72010-01-08 06:01:24 -050057 ret = pm860x_read_device(i2c, reg, 1, &data);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050058 mutex_unlock(&chip->io_lock);
59
60 if (ret < 0)
61 return ret;
62 else
63 return (int)data;
64}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050065EXPORT_SYMBOL(pm860x_reg_read);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050066
Haojian Zhuang53dbab72010-01-08 06:01:24 -050067int pm860x_reg_write(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050068 unsigned char data)
69{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050070 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050071 int ret;
72
73 mutex_lock(&chip->io_lock);
Haojian Zhuang53dbab72010-01-08 06:01:24 -050074 ret = pm860x_write_device(i2c, reg, 1, &data);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050075 mutex_unlock(&chip->io_lock);
76
77 return ret;
78}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050079EXPORT_SYMBOL(pm860x_reg_write);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050080
Haojian Zhuang53dbab72010-01-08 06:01:24 -050081int pm860x_bulk_read(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050082 int count, unsigned char *buf)
83{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050084 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050085 int ret;
86
87 mutex_lock(&chip->io_lock);
Haojian Zhuang53dbab72010-01-08 06:01:24 -050088 ret = pm860x_read_device(i2c, reg, count, buf);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050089 mutex_unlock(&chip->io_lock);
90
91 return ret;
92}
Haojian Zhuang53dbab72010-01-08 06:01:24 -050093EXPORT_SYMBOL(pm860x_bulk_read);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050094
Haojian Zhuang53dbab72010-01-08 06:01:24 -050095int pm860x_bulk_write(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050096 int count, unsigned char *buf)
97{
Haojian Zhuang53dbab72010-01-08 06:01:24 -050098 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -050099 int ret;
100
101 mutex_lock(&chip->io_lock);
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500102 ret = pm860x_write_device(i2c, reg, count, buf);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500103 mutex_unlock(&chip->io_lock);
104
105 return ret;
106}
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500107EXPORT_SYMBOL(pm860x_bulk_write);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500108
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500109int pm860x_set_bits(struct i2c_client *i2c, int reg,
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500110 unsigned char mask, unsigned char data)
111{
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500112 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500113 unsigned char value;
114 int ret;
115
116 mutex_lock(&chip->io_lock);
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500117 ret = pm860x_read_device(i2c, reg, 1, &value);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500118 if (ret < 0)
119 goto out;
120 value &= ~mask;
121 value |= data;
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500122 ret = pm860x_write_device(i2c, reg, 1, &value);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500123out:
124 mutex_unlock(&chip->io_lock);
125 return ret;
126}
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500127EXPORT_SYMBOL(pm860x_set_bits);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500128
129
130static const struct i2c_device_id pm860x_id_table[] = {
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500131 { "88PM860x", 0 },
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500132 {}
133};
134MODULE_DEVICE_TABLE(i2c, pm860x_id_table);
135
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500136static int verify_addr(struct i2c_client *i2c)
137{
138 unsigned short addr_8607[] = {0x30, 0x34};
139 unsigned short addr_8606[] = {0x10, 0x11};
140 int size, i;
141
142 if (i2c == NULL)
143 return 0;
144 size = ARRAY_SIZE(addr_8606);
145 for (i = 0; i < size; i++) {
146 if (i2c->addr == *(addr_8606 + i))
147 return CHIP_PM8606;
148 }
149 size = ARRAY_SIZE(addr_8607);
150 for (i = 0; i < size; i++) {
151 if (i2c->addr == *(addr_8607 + i))
152 return CHIP_PM8607;
153 }
154 return 0;
155}
156
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500157static int __devinit pm860x_probe(struct i2c_client *client,
158 const struct i2c_device_id *id)
159{
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500160 struct pm860x_platform_data *pdata = client->dev.platform_data;
Haojian Zhuange8343dd2010-02-03 15:38:12 -0500161 struct pm860x_chip *chip;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500162
Haojian Zhuange8343dd2010-02-03 15:38:12 -0500163 if (!pdata) {
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500164 pr_info("No platform data in %s!\n", __func__);
165 return -EINVAL;
166 }
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500167
Haojian Zhuange8343dd2010-02-03 15:38:12 -0500168 chip = kzalloc(sizeof(struct pm860x_chip), GFP_KERNEL);
169 if (chip == NULL)
170 return -ENOMEM;
171
172 chip->id = verify_addr(client);
173 chip->client = client;
174 i2c_set_clientdata(client, chip);
175 chip->dev = &client->dev;
176 mutex_init(&chip->io_lock);
177 dev_set_drvdata(chip->dev, chip);
178
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500179 /*
180 * Both client and companion client shares same platform driver.
181 * Driver distinguishes them by pdata->companion_addr.
182 * pdata->companion_addr is only assigned if companion chip exists.
183 * At the same time, the companion_addr shouldn't equal to client
184 * address.
185 */
Haojian Zhuange8343dd2010-02-03 15:38:12 -0500186 if (pdata->companion_addr && (pdata->companion_addr != client->addr)) {
187 chip->companion_addr = pdata->companion_addr;
188 chip->companion = i2c_new_dummy(chip->client->adapter,
189 chip->companion_addr);
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500190 i2c_set_clientdata(chip->companion, chip);
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500191 }
Haojian Zhuange8343dd2010-02-03 15:38:12 -0500192
193 pm860x_device_init(chip, pdata);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500194 return 0;
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500195}
196
197static int __devexit pm860x_remove(struct i2c_client *client)
198{
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500199 struct pm860x_chip *chip = i2c_get_clientdata(client);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500200
Haojian Zhuang53dbab72010-01-08 06:01:24 -0500201 pm860x_device_exit(chip);
202 i2c_unregister_device(chip->companion);
203 i2c_set_clientdata(chip->companion, NULL);
204 i2c_set_clientdata(chip->client, NULL);
Haojian Zhuangbbd51b12010-01-06 17:04:18 -0500205 kfree(chip);
206 return 0;
207}
208
209static struct i2c_driver pm860x_driver = {
210 .driver = {
211 .name = "88PM860x",
212 .owner = THIS_MODULE,
213 },
214 .probe = pm860x_probe,
215 .remove = __devexit_p(pm860x_remove),
216 .id_table = pm860x_id_table,
217};
218
219static int __init pm860x_i2c_init(void)
220{
221 int ret;
222 ret = i2c_add_driver(&pm860x_driver);
223 if (ret != 0)
224 pr_err("Failed to register 88PM860x I2C driver: %d\n", ret);
225 return ret;
226}
227subsys_initcall(pm860x_i2c_init);
228
229static void __exit pm860x_i2c_exit(void)
230{
231 i2c_del_driver(&pm860x_driver);
232}
233module_exit(pm860x_i2c_exit);
234
235MODULE_DESCRIPTION("I2C Driver for Marvell 88PM860x");
236MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
237MODULE_LICENSE("GPL");