2 * Voltage and current regulation for AD5398 and AD5821
4 * Copyright 2010 Analog Devices Inc.
6 * Enter bugs at http://blackfin.uclinux.org/
8 * Licensed under the GPL-2 or later.
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/slab.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/machine.h>
19 #define AD5398_CURRENT_EN_MASK 0x8000
21 struct ad5398_chip_info {
22 struct i2c_client *client;
25 unsigned int current_level;
26 unsigned int current_mask;
27 unsigned int current_offset;
28 struct regulator_dev *rdev;
31 static int ad5398_calc_current(struct ad5398_chip_info *chip,
34 unsigned range_uA = chip->max_uA - chip->min_uA;
36 return chip->min_uA + (selector * range_uA / chip->current_level);
39 static int ad5398_read_reg(struct i2c_client *client, unsigned short *data)
44 ret = i2c_master_recv(client, (char *)&val, 2);
46 dev_err(&client->dev, "I2C read error\n");
49 *data = be16_to_cpu(val);
54 static int ad5398_write_reg(struct i2c_client *client, const unsigned short data)
59 val = cpu_to_be16(data);
60 ret = i2c_master_send(client, (char *)&val, 2);
62 dev_err(&client->dev, "I2C write error\n");
67 static int ad5398_get_current_limit(struct regulator_dev *rdev)
69 struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
70 struct i2c_client *client = chip->client;
74 ret = ad5398_read_reg(client, &data);
78 ret = (data & chip->current_mask) >> chip->current_offset;
80 return ad5398_calc_current(chip, ret);
83 static int ad5398_set_current_limit(struct regulator_dev *rdev, int min_uA, int max_uA)
85 struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
86 struct i2c_client *client = chip->client;
87 unsigned range_uA = chip->max_uA - chip->min_uA;
92 if (min_uA > chip->max_uA || min_uA < chip->min_uA)
94 if (max_uA > chip->max_uA || max_uA < chip->min_uA)
97 selector = ((min_uA - chip->min_uA) * chip->current_level +
98 range_uA - 1) / range_uA;
99 if (ad5398_calc_current(chip, selector) > max_uA)
102 dev_dbg(&client->dev, "changing current %dmA\n",
103 ad5398_calc_current(chip, selector) / 1000);
105 /* read chip enable bit */
106 ret = ad5398_read_reg(client, &data);
110 /* prepare register data */
111 selector = (selector << chip->current_offset) & chip->current_mask;
112 data = (unsigned short)selector | (data & AD5398_CURRENT_EN_MASK);
114 /* write the new current value back as well as enable bit */
115 ret = ad5398_write_reg(client, data);
120 static int ad5398_is_enabled(struct regulator_dev *rdev)
122 struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
123 struct i2c_client *client = chip->client;
127 ret = ad5398_read_reg(client, &data);
131 if (data & AD5398_CURRENT_EN_MASK)
137 static int ad5398_enable(struct regulator_dev *rdev)
139 struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
140 struct i2c_client *client = chip->client;
144 ret = ad5398_read_reg(client, &data);
148 if (data & AD5398_CURRENT_EN_MASK)
151 data |= AD5398_CURRENT_EN_MASK;
153 ret = ad5398_write_reg(client, data);
158 static int ad5398_disable(struct regulator_dev *rdev)
160 struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
161 struct i2c_client *client = chip->client;
165 ret = ad5398_read_reg(client, &data);
169 if (!(data & AD5398_CURRENT_EN_MASK))
172 data &= ~AD5398_CURRENT_EN_MASK;
174 ret = ad5398_write_reg(client, data);
179 static struct regulator_ops ad5398_ops = {
180 .get_current_limit = ad5398_get_current_limit,
181 .set_current_limit = ad5398_set_current_limit,
182 .enable = ad5398_enable,
183 .disable = ad5398_disable,
184 .is_enabled = ad5398_is_enabled,
187 static struct regulator_desc ad5398_reg = {
191 .type = REGULATOR_CURRENT,
192 .owner = THIS_MODULE,
195 struct ad5398_current_data_format {
202 static const struct ad5398_current_data_format df_10_4_120 = {10, 4, 0, 120000};
204 static const struct i2c_device_id ad5398_id[] = {
205 { "ad5398", (kernel_ulong_t)&df_10_4_120 },
206 { "ad5821", (kernel_ulong_t)&df_10_4_120 },
209 MODULE_DEVICE_TABLE(i2c, ad5398_id);
211 static int __devinit ad5398_probe(struct i2c_client *client,
212 const struct i2c_device_id *id)
214 struct regulator_init_data *init_data = client->dev.platform_data;
215 struct ad5398_chip_info *chip;
216 const struct ad5398_current_data_format *df =
217 (struct ad5398_current_data_format *)id->driver_data;
223 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
227 chip->client = client;
229 chip->min_uA = df->min_uA;
230 chip->max_uA = df->max_uA;
231 chip->current_level = 1 << df->current_bits;
232 chip->current_offset = df->current_offset;
233 chip->current_mask = (chip->current_level - 1) << chip->current_offset;
235 chip->rdev = regulator_register(&ad5398_reg, &client->dev,
237 if (IS_ERR(chip->rdev)) {
238 ret = PTR_ERR(chip->rdev);
239 dev_err(&client->dev, "failed to register %s %s\n",
240 id->name, ad5398_reg.name);
244 i2c_set_clientdata(client, chip);
245 dev_dbg(&client->dev, "%s regulator driver is registered.\n", id->name);
253 static int __devexit ad5398_remove(struct i2c_client *client)
255 struct ad5398_chip_info *chip = i2c_get_clientdata(client);
257 regulator_unregister(chip->rdev);
263 static struct i2c_driver ad5398_driver = {
264 .probe = ad5398_probe,
265 .remove = __devexit_p(ad5398_remove),
269 .id_table = ad5398_id,
272 static int __init ad5398_init(void)
274 return i2c_add_driver(&ad5398_driver);
276 subsys_initcall(ad5398_init);
278 static void __exit ad5398_exit(void)
280 i2c_del_driver(&ad5398_driver);
282 module_exit(ad5398_exit);
284 MODULE_DESCRIPTION("AD5398 and AD5821 current regulator driver");
285 MODULE_AUTHOR("Sonic Zhang");
286 MODULE_LICENSE("GPL");
287 MODULE_ALIAS("i2c:ad5398-regulator");