1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Voltage and current regulation for AD5398 and AD5821
5 * Copyright 2010 Analog Devices Inc.
7 * Enter bugs at http://blackfin.uclinux.org/
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/machine.h>
18 #define AD5398_CURRENT_EN_MASK 0x8000
20 struct ad5398_chip_info
{
21 struct i2c_client
*client
;
24 unsigned int current_level
;
25 unsigned int current_mask
;
26 unsigned int current_offset
;
27 struct regulator_dev
*rdev
;
30 static int ad5398_calc_current(struct ad5398_chip_info
*chip
,
33 unsigned range_uA
= chip
->max_uA
- chip
->min_uA
;
35 return chip
->min_uA
+ (selector
* range_uA
/ chip
->current_level
);
38 static int ad5398_read_reg(struct i2c_client
*client
, unsigned short *data
)
43 ret
= i2c_master_recv(client
, (char *)&val
, 2);
45 dev_err(&client
->dev
, "I2C read error\n");
48 *data
= be16_to_cpu(val
);
53 static int ad5398_write_reg(struct i2c_client
*client
, const unsigned short data
)
58 val
= cpu_to_be16(data
);
59 ret
= i2c_master_send(client
, (char *)&val
, 2);
61 dev_err(&client
->dev
, "I2C write error\n");
62 return ret
< 0 ? ret
: -EIO
;
68 static int ad5398_get_current_limit(struct regulator_dev
*rdev
)
70 struct ad5398_chip_info
*chip
= rdev_get_drvdata(rdev
);
71 struct i2c_client
*client
= chip
->client
;
75 ret
= ad5398_read_reg(client
, &data
);
79 ret
= (data
& chip
->current_mask
) >> chip
->current_offset
;
81 return ad5398_calc_current(chip
, ret
);
84 static int ad5398_set_current_limit(struct regulator_dev
*rdev
, int min_uA
, int max_uA
)
86 struct ad5398_chip_info
*chip
= rdev_get_drvdata(rdev
);
87 struct i2c_client
*client
= chip
->client
;
88 unsigned range_uA
= chip
->max_uA
- chip
->min_uA
;
93 if (min_uA
< chip
->min_uA
)
94 min_uA
= chip
->min_uA
;
95 if (max_uA
> chip
->max_uA
)
96 max_uA
= chip
->max_uA
;
98 if (min_uA
> chip
->max_uA
|| max_uA
< chip
->min_uA
)
101 selector
= DIV_ROUND_UP((min_uA
- chip
->min_uA
) * chip
->current_level
,
103 if (ad5398_calc_current(chip
, selector
) > max_uA
)
106 dev_dbg(&client
->dev
, "changing current %duA\n",
107 ad5398_calc_current(chip
, selector
));
109 /* read chip enable bit */
110 ret
= ad5398_read_reg(client
, &data
);
114 /* prepare register data */
115 selector
= (selector
<< chip
->current_offset
) & chip
->current_mask
;
116 data
= (unsigned short)selector
| (data
& AD5398_CURRENT_EN_MASK
);
118 /* write the new current value back as well as enable bit */
119 ret
= ad5398_write_reg(client
, data
);
124 static int ad5398_is_enabled(struct regulator_dev
*rdev
)
126 struct ad5398_chip_info
*chip
= rdev_get_drvdata(rdev
);
127 struct i2c_client
*client
= chip
->client
;
131 ret
= ad5398_read_reg(client
, &data
);
135 if (data
& AD5398_CURRENT_EN_MASK
)
141 static int ad5398_enable(struct regulator_dev
*rdev
)
143 struct ad5398_chip_info
*chip
= rdev_get_drvdata(rdev
);
144 struct i2c_client
*client
= chip
->client
;
148 ret
= ad5398_read_reg(client
, &data
);
152 if (data
& AD5398_CURRENT_EN_MASK
)
155 data
|= AD5398_CURRENT_EN_MASK
;
157 ret
= ad5398_write_reg(client
, data
);
162 static int ad5398_disable(struct regulator_dev
*rdev
)
164 struct ad5398_chip_info
*chip
= rdev_get_drvdata(rdev
);
165 struct i2c_client
*client
= chip
->client
;
169 ret
= ad5398_read_reg(client
, &data
);
173 if (!(data
& AD5398_CURRENT_EN_MASK
))
176 data
&= ~AD5398_CURRENT_EN_MASK
;
178 ret
= ad5398_write_reg(client
, data
);
183 static const struct regulator_ops ad5398_ops
= {
184 .get_current_limit
= ad5398_get_current_limit
,
185 .set_current_limit
= ad5398_set_current_limit
,
186 .enable
= ad5398_enable
,
187 .disable
= ad5398_disable
,
188 .is_enabled
= ad5398_is_enabled
,
191 static const struct regulator_desc ad5398_reg
= {
195 .type
= REGULATOR_CURRENT
,
196 .owner
= THIS_MODULE
,
199 struct ad5398_current_data_format
{
206 static const struct ad5398_current_data_format df_10_4_120
= {10, 4, 0, 120000};
208 static const struct i2c_device_id ad5398_id
[] = {
209 { "ad5398", (kernel_ulong_t
)&df_10_4_120
},
210 { "ad5821", (kernel_ulong_t
)&df_10_4_120
},
213 MODULE_DEVICE_TABLE(i2c
, ad5398_id
);
215 static int ad5398_probe(struct i2c_client
*client
,
216 const struct i2c_device_id
*id
)
218 struct regulator_init_data
*init_data
= dev_get_platdata(&client
->dev
);
219 struct regulator_config config
= { };
220 struct ad5398_chip_info
*chip
;
221 const struct ad5398_current_data_format
*df
=
222 (struct ad5398_current_data_format
*)id
->driver_data
;
227 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
231 config
.dev
= &client
->dev
;
232 config
.init_data
= init_data
;
233 config
.driver_data
= chip
;
235 chip
->client
= client
;
237 chip
->min_uA
= df
->min_uA
;
238 chip
->max_uA
= df
->max_uA
;
239 chip
->current_level
= 1 << df
->current_bits
;
240 chip
->current_offset
= df
->current_offset
;
241 chip
->current_mask
= (chip
->current_level
- 1) << chip
->current_offset
;
243 chip
->rdev
= devm_regulator_register(&client
->dev
, &ad5398_reg
,
245 if (IS_ERR(chip
->rdev
)) {
246 dev_err(&client
->dev
, "failed to register %s %s\n",
247 id
->name
, ad5398_reg
.name
);
248 return PTR_ERR(chip
->rdev
);
251 i2c_set_clientdata(client
, chip
);
252 dev_dbg(&client
->dev
, "%s regulator driver is registered.\n", id
->name
);
256 static struct i2c_driver ad5398_driver
= {
257 .probe
= ad5398_probe
,
261 .id_table
= ad5398_id
,
264 static int __init
ad5398_init(void)
266 return i2c_add_driver(&ad5398_driver
);
268 subsys_initcall(ad5398_init
);
270 static void __exit
ad5398_exit(void)
272 i2c_del_driver(&ad5398_driver
);
274 module_exit(ad5398_exit
);
276 MODULE_DESCRIPTION("AD5398 and AD5821 current regulator driver");
277 MODULE_AUTHOR("Sonic Zhang");
278 MODULE_LICENSE("GPL");