blob: 48016a050d5fb9b9c19981075755bd3831be0652 [file] [log] [blame]
Sonic Zhang8b385d92010-06-04 11:46:04 +08001/*
2 * Voltage and current regulation for AD5398 and AD5821
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Enter bugs at http://blackfin.uclinux.org/
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
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>
18
19#define AD5398_CURRENT_EN_MASK 0x8000
20
21struct ad5398_chip_info {
22 struct i2c_client *client;
23 int min_uA;
24 int max_uA;
25 unsigned int current_level;
26 unsigned int current_mask;
27 unsigned int current_offset;
Axel Lin58d463e2010-09-01 10:29:18 +080028 struct regulator_dev *rdev;
Sonic Zhang8b385d92010-06-04 11:46:04 +080029};
30
31static int ad5398_calc_current(struct ad5398_chip_info *chip,
32 unsigned selector)
33{
34 unsigned range_uA = chip->max_uA - chip->min_uA;
35
36 return chip->min_uA + (selector * range_uA / chip->current_level);
37}
38
39static int ad5398_read_reg(struct i2c_client *client, unsigned short *data)
40{
41 unsigned short val;
42 int ret;
43
44 ret = i2c_master_recv(client, (char *)&val, 2);
45 if (ret < 0) {
46 dev_err(&client->dev, "I2C read error\n");
47 return ret;
48 }
49 *data = be16_to_cpu(val);
50
51 return ret;
52}
53
54static int ad5398_write_reg(struct i2c_client *client, const unsigned short data)
55{
56 unsigned short val;
57 int ret;
58
59 val = cpu_to_be16(data);
60 ret = i2c_master_send(client, (char *)&val, 2);
61 if (ret < 0)
62 dev_err(&client->dev, "I2C write error\n");
63
64 return ret;
65}
66
67static int ad5398_get_current_limit(struct regulator_dev *rdev)
68{
69 struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
70 struct i2c_client *client = chip->client;
71 unsigned short data;
72 int ret;
73
74 ret = ad5398_read_reg(client, &data);
75 if (ret < 0)
76 return ret;
77
78 ret = (data & chip->current_mask) >> chip->current_offset;
79
80 return ad5398_calc_current(chip, ret);
81}
82
83static int ad5398_set_current_limit(struct regulator_dev *rdev, int min_uA, int max_uA)
84{
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;
88 unsigned selector;
89 unsigned short data;
90 int ret;
91
Axel Lin9c6a74c2012-07-04 11:55:07 +080092 if (min_uA < chip->min_uA)
93 min_uA = chip->min_uA;
94 if (max_uA > chip->max_uA)
95 max_uA = chip->max_uA;
96
97 if (min_uA > chip->max_uA || max_uA < chip->min_uA)
Sonic Zhang8b385d92010-06-04 11:46:04 +080098 return -EINVAL;
99
Axel Lin8148ed62012-03-02 16:20:54 +0800100 selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level,
101 range_uA);
Sonic Zhang8b385d92010-06-04 11:46:04 +0800102 if (ad5398_calc_current(chip, selector) > max_uA)
103 return -EINVAL;
104
Axel Lin935c14a2012-03-27 10:08:25 +0800105 dev_dbg(&client->dev, "changing current %duA\n",
106 ad5398_calc_current(chip, selector));
Sonic Zhang8b385d92010-06-04 11:46:04 +0800107
108 /* read chip enable bit */
109 ret = ad5398_read_reg(client, &data);
110 if (ret < 0)
111 return ret;
112
113 /* prepare register data */
114 selector = (selector << chip->current_offset) & chip->current_mask;
115 data = (unsigned short)selector | (data & AD5398_CURRENT_EN_MASK);
116
117 /* write the new current value back as well as enable bit */
118 ret = ad5398_write_reg(client, data);
119
120 return ret;
121}
122
123static int ad5398_is_enabled(struct regulator_dev *rdev)
124{
125 struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
126 struct i2c_client *client = chip->client;
127 unsigned short data;
128 int ret;
129
130 ret = ad5398_read_reg(client, &data);
131 if (ret < 0)
132 return ret;
133
134 if (data & AD5398_CURRENT_EN_MASK)
135 return 1;
136 else
137 return 0;
138}
139
140static int ad5398_enable(struct regulator_dev *rdev)
141{
142 struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
143 struct i2c_client *client = chip->client;
144 unsigned short data;
145 int ret;
146
147 ret = ad5398_read_reg(client, &data);
148 if (ret < 0)
149 return ret;
150
151 if (data & AD5398_CURRENT_EN_MASK)
152 return 0;
153
154 data |= AD5398_CURRENT_EN_MASK;
155
156 ret = ad5398_write_reg(client, data);
157
158 return ret;
159}
160
161static int ad5398_disable(struct regulator_dev *rdev)
162{
163 struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
164 struct i2c_client *client = chip->client;
165 unsigned short data;
166 int ret;
167
168 ret = ad5398_read_reg(client, &data);
169 if (ret < 0)
170 return ret;
171
172 if (!(data & AD5398_CURRENT_EN_MASK))
173 return 0;
174
175 data &= ~AD5398_CURRENT_EN_MASK;
176
177 ret = ad5398_write_reg(client, data);
178
179 return ret;
180}
181
182static struct regulator_ops ad5398_ops = {
183 .get_current_limit = ad5398_get_current_limit,
184 .set_current_limit = ad5398_set_current_limit,
185 .enable = ad5398_enable,
186 .disable = ad5398_disable,
187 .is_enabled = ad5398_is_enabled,
188};
189
Axel Lin487f71e2012-04-05 11:56:56 +0800190static const struct regulator_desc ad5398_reg = {
Sonic Zhang8b385d92010-06-04 11:46:04 +0800191 .name = "isink",
192 .id = 0,
193 .ops = &ad5398_ops,
194 .type = REGULATOR_CURRENT,
195 .owner = THIS_MODULE,
196};
197
198struct ad5398_current_data_format {
199 int current_bits;
200 int current_offset;
201 int min_uA;
202 int max_uA;
203};
204
205static const struct ad5398_current_data_format df_10_4_120 = {10, 4, 0, 120000};
206
207static const struct i2c_device_id ad5398_id[] = {
208 { "ad5398", (kernel_ulong_t)&df_10_4_120 },
209 { "ad5821", (kernel_ulong_t)&df_10_4_120 },
210 { }
211};
212MODULE_DEVICE_TABLE(i2c, ad5398_id);
213
Bill Pembertona5023572012-11-19 13:22:22 -0500214static int ad5398_probe(struct i2c_client *client,
Sonic Zhang8b385d92010-06-04 11:46:04 +0800215 const struct i2c_device_id *id)
216{
Jingoo Handff91d02013-07-30 17:20:47 +0900217 struct regulator_init_data *init_data = dev_get_platdata(&client->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100218 struct regulator_config config = { };
Sonic Zhang8b385d92010-06-04 11:46:04 +0800219 struct ad5398_chip_info *chip;
220 const struct ad5398_current_data_format *df =
221 (struct ad5398_current_data_format *)id->driver_data;
Sonic Zhang8b385d92010-06-04 11:46:04 +0800222
223 if (!init_data)
224 return -EINVAL;
225
Axel Lin0df8c962012-03-27 10:09:42 +0800226 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
Sonic Zhang8b385d92010-06-04 11:46:04 +0800227 if (!chip)
228 return -ENOMEM;
229
Mark Brownc1727082012-04-04 00:50:22 +0100230 config.dev = &client->dev;
231 config.init_data = init_data;
232 config.driver_data = chip;
233
Sonic Zhang8b385d92010-06-04 11:46:04 +0800234 chip->client = client;
235
236 chip->min_uA = df->min_uA;
237 chip->max_uA = df->max_uA;
238 chip->current_level = 1 << df->current_bits;
239 chip->current_offset = df->current_offset;
240 chip->current_mask = (chip->current_level - 1) << chip->current_offset;
241
Axel Lin9b2cdac2013-09-03 14:20:25 +0800242 chip->rdev = devm_regulator_register(&client->dev, &ad5398_reg,
243 &config);
Axel Lin58d463e2010-09-01 10:29:18 +0800244 if (IS_ERR(chip->rdev)) {
Sonic Zhang8b385d92010-06-04 11:46:04 +0800245 dev_err(&client->dev, "failed to register %s %s\n",
246 id->name, ad5398_reg.name);
Axel Lin9b2cdac2013-09-03 14:20:25 +0800247 return PTR_ERR(chip->rdev);
Sonic Zhang8b385d92010-06-04 11:46:04 +0800248 }
249
250 i2c_set_clientdata(client, chip);
251 dev_dbg(&client->dev, "%s regulator driver is registered.\n", id->name);
252 return 0;
Sonic Zhang8b385d92010-06-04 11:46:04 +0800253}
254
255static struct i2c_driver ad5398_driver = {
256 .probe = ad5398_probe,
Sonic Zhang8b385d92010-06-04 11:46:04 +0800257 .driver = {
258 .name = "ad5398",
259 },
260 .id_table = ad5398_id,
261};
262
263static int __init ad5398_init(void)
264{
265 return i2c_add_driver(&ad5398_driver);
266}
Sonic Zhang839b8362010-06-10 16:50:20 +0800267subsys_initcall(ad5398_init);
Sonic Zhang8b385d92010-06-04 11:46:04 +0800268
269static void __exit ad5398_exit(void)
270{
271 i2c_del_driver(&ad5398_driver);
272}
273module_exit(ad5398_exit);
274
275MODULE_DESCRIPTION("AD5398 and AD5821 current regulator driver");
276MODULE_AUTHOR("Sonic Zhang");
277MODULE_LICENSE("GPL");
278MODULE_ALIAS("i2c:ad5398-regulator");