Blame view

drivers/regulator/ad5398.c 6.26 KB
80503b23b   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
8b385d9b9   Sonic Zhang   regulator: new dr...
2
3
4
5
6
7
  /*
   * Voltage and current regulation for AD5398 and AD5821
   *
   * Copyright 2010 Analog Devices Inc.
   *
   * Enter bugs at http://blackfin.uclinux.org/
8b385d9b9   Sonic Zhang   regulator: new dr...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
   */
  
  #include <linux/module.h>
  #include <linux/err.h>
  #include <linux/i2c.h>
  #include <linux/slab.h>
  #include <linux/platform_device.h>
  #include <linux/regulator/driver.h>
  #include <linux/regulator/machine.h>
  
  #define AD5398_CURRENT_EN_MASK	0x8000
  
  struct ad5398_chip_info {
  	struct i2c_client *client;
  	int min_uA;
  	int max_uA;
  	unsigned int current_level;
  	unsigned int current_mask;
  	unsigned int current_offset;
58d463eec   Axel Lin   regulator: ad5398...
27
  	struct regulator_dev *rdev;
8b385d9b9   Sonic Zhang   regulator: new dr...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  };
  
  static int ad5398_calc_current(struct ad5398_chip_info *chip,
  	unsigned selector)
  {
  	unsigned range_uA = chip->max_uA - chip->min_uA;
  
  	return chip->min_uA + (selector * range_uA / chip->current_level);
  }
  
  static int ad5398_read_reg(struct i2c_client *client, unsigned short *data)
  {
  	unsigned short val;
  	int ret;
  
  	ret = i2c_master_recv(client, (char *)&val, 2);
  	if (ret < 0) {
  		dev_err(&client->dev, "I2C read error
  ");
  		return ret;
  	}
  	*data = be16_to_cpu(val);
  
  	return ret;
  }
  
  static int ad5398_write_reg(struct i2c_client *client, const unsigned short data)
  {
  	unsigned short val;
  	int ret;
  
  	val = cpu_to_be16(data);
  	ret = i2c_master_send(client, (char *)&val, 2);
4434cee9b   Axel Lin   regulator: ad5398...
61
  	if (ret != 2) {
8b385d9b9   Sonic Zhang   regulator: new dr...
62
63
  		dev_err(&client->dev, "I2C write error
  ");
4434cee9b   Axel Lin   regulator: ad5398...
64
65
  		return ret < 0 ? ret : -EIO;
  	}
8b385d9b9   Sonic Zhang   regulator: new dr...
66

4434cee9b   Axel Lin   regulator: ad5398...
67
  	return 0;
8b385d9b9   Sonic Zhang   regulator: new dr...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  }
  
  static int ad5398_get_current_limit(struct regulator_dev *rdev)
  {
  	struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  	struct i2c_client *client = chip->client;
  	unsigned short data;
  	int ret;
  
  	ret = ad5398_read_reg(client, &data);
  	if (ret < 0)
  		return ret;
  
  	ret = (data & chip->current_mask) >> chip->current_offset;
  
  	return ad5398_calc_current(chip, ret);
  }
  
  static int ad5398_set_current_limit(struct regulator_dev *rdev, int min_uA, int max_uA)
  {
  	struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  	struct i2c_client *client = chip->client;
  	unsigned range_uA = chip->max_uA - chip->min_uA;
  	unsigned selector;
  	unsigned short data;
  	int ret;
9c6a74c5e   Axel Lin   regulator: ad5398...
94
95
96
97
98
99
  	if (min_uA < chip->min_uA)
  		min_uA = chip->min_uA;
  	if (max_uA > chip->max_uA)
  		max_uA = chip->max_uA;
  
  	if (min_uA > chip->max_uA || max_uA < chip->min_uA)
8b385d9b9   Sonic Zhang   regulator: new dr...
100
  		return -EINVAL;
8148ed6e6   Axel Lin   regulator: ad5398...
101
102
  	selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level,
  				range_uA);
8b385d9b9   Sonic Zhang   regulator: new dr...
103
104
  	if (ad5398_calc_current(chip, selector) > max_uA)
  		return -EINVAL;
935c14a21   Axel Lin   regulator: ad5398...
105
106
107
  	dev_dbg(&client->dev, "changing current %duA
  ",
  		ad5398_calc_current(chip, selector));
8b385d9b9   Sonic Zhang   regulator: new dr...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  
  	/* read chip enable bit */
  	ret = ad5398_read_reg(client, &data);
  	if (ret < 0)
  		return ret;
  
  	/* prepare register data */
  	selector = (selector << chip->current_offset) & chip->current_mask;
  	data = (unsigned short)selector | (data & AD5398_CURRENT_EN_MASK);
  
  	/* write the new current value back as well as enable bit */
  	ret = ad5398_write_reg(client, data);
  
  	return ret;
  }
  
  static int ad5398_is_enabled(struct regulator_dev *rdev)
  {
  	struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  	struct i2c_client *client = chip->client;
  	unsigned short data;
  	int ret;
  
  	ret = ad5398_read_reg(client, &data);
  	if (ret < 0)
  		return ret;
  
  	if (data & AD5398_CURRENT_EN_MASK)
  		return 1;
  	else
  		return 0;
  }
  
  static int ad5398_enable(struct regulator_dev *rdev)
  {
  	struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  	struct i2c_client *client = chip->client;
  	unsigned short data;
  	int ret;
  
  	ret = ad5398_read_reg(client, &data);
  	if (ret < 0)
  		return ret;
  
  	if (data & AD5398_CURRENT_EN_MASK)
  		return 0;
  
  	data |= AD5398_CURRENT_EN_MASK;
  
  	ret = ad5398_write_reg(client, data);
  
  	return ret;
  }
  
  static int ad5398_disable(struct regulator_dev *rdev)
  {
  	struct ad5398_chip_info *chip = rdev_get_drvdata(rdev);
  	struct i2c_client *client = chip->client;
  	unsigned short data;
  	int ret;
  
  	ret = ad5398_read_reg(client, &data);
  	if (ret < 0)
  		return ret;
  
  	if (!(data & AD5398_CURRENT_EN_MASK))
  		return 0;
  
  	data &= ~AD5398_CURRENT_EN_MASK;
  
  	ret = ad5398_write_reg(client, data);
  
  	return ret;
  }
bb24b9df7   Bhumika Goyal   regulator: ad5398...
182
  static const struct regulator_ops ad5398_ops = {
8b385d9b9   Sonic Zhang   regulator: new dr...
183
184
185
186
187
188
  	.get_current_limit = ad5398_get_current_limit,
  	.set_current_limit = ad5398_set_current_limit,
  	.enable = ad5398_enable,
  	.disable = ad5398_disable,
  	.is_enabled = ad5398_is_enabled,
  };
487f71e6f   Axel Lin   regulator: ad5398...
189
  static const struct regulator_desc ad5398_reg = {
8b385d9b9   Sonic Zhang   regulator: new dr...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  	.name = "isink",
  	.id = 0,
  	.ops = &ad5398_ops,
  	.type = REGULATOR_CURRENT,
  	.owner = THIS_MODULE,
  };
  
  struct ad5398_current_data_format {
  	int current_bits;
  	int current_offset;
  	int min_uA;
  	int max_uA;
  };
  
  static const struct ad5398_current_data_format df_10_4_120 = {10, 4, 0, 120000};
  
  static const struct i2c_device_id ad5398_id[] = {
  	{ "ad5398", (kernel_ulong_t)&df_10_4_120 },
  	{ "ad5821", (kernel_ulong_t)&df_10_4_120 },
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, ad5398_id);
a5023574d   Bill Pemberton   regulator: remove...
212
  static int ad5398_probe(struct i2c_client *client,
8b385d9b9   Sonic Zhang   regulator: new dr...
213
214
  				const struct i2c_device_id *id)
  {
dff91d0b7   Jingoo Han   regulator: use de...
215
  	struct regulator_init_data *init_data = dev_get_platdata(&client->dev);
c172708d3   Mark Brown   regulator: core: ...
216
  	struct regulator_config config = { };
8b385d9b9   Sonic Zhang   regulator: new dr...
217
218
219
  	struct ad5398_chip_info *chip;
  	const struct ad5398_current_data_format *df =
  			(struct ad5398_current_data_format *)id->driver_data;
8b385d9b9   Sonic Zhang   regulator: new dr...
220
221
222
  
  	if (!init_data)
  		return -EINVAL;
0df8c96fa   Axel Lin   regulator: ad5398...
223
  	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
8b385d9b9   Sonic Zhang   regulator: new dr...
224
225
  	if (!chip)
  		return -ENOMEM;
c172708d3   Mark Brown   regulator: core: ...
226
227
228
  	config.dev = &client->dev;
  	config.init_data = init_data;
  	config.driver_data = chip;
8b385d9b9   Sonic Zhang   regulator: new dr...
229
230
231
232
233
234
235
  	chip->client = client;
  
  	chip->min_uA = df->min_uA;
  	chip->max_uA = df->max_uA;
  	chip->current_level = 1 << df->current_bits;
  	chip->current_offset = df->current_offset;
  	chip->current_mask = (chip->current_level - 1) << chip->current_offset;
9b2cdac71   Axel Lin   regulator: ad5398...
236
237
  	chip->rdev = devm_regulator_register(&client->dev, &ad5398_reg,
  					     &config);
58d463eec   Axel Lin   regulator: ad5398...
238
  	if (IS_ERR(chip->rdev)) {
8b385d9b9   Sonic Zhang   regulator: new dr...
239
240
241
  		dev_err(&client->dev, "failed to register %s %s
  ",
  			id->name, ad5398_reg.name);
9b2cdac71   Axel Lin   regulator: ad5398...
242
  		return PTR_ERR(chip->rdev);
8b385d9b9   Sonic Zhang   regulator: new dr...
243
244
245
246
247
248
  	}
  
  	i2c_set_clientdata(client, chip);
  	dev_dbg(&client->dev, "%s regulator driver is registered.
  ", id->name);
  	return 0;
8b385d9b9   Sonic Zhang   regulator: new dr...
249
250
251
252
  }
  
  static struct i2c_driver ad5398_driver = {
  	.probe = ad5398_probe,
8b385d9b9   Sonic Zhang   regulator: new dr...
253
254
255
256
257
258
259
260
261
262
  	.driver		= {
  		.name	= "ad5398",
  	},
  	.id_table	= ad5398_id,
  };
  
  static int __init ad5398_init(void)
  {
  	return i2c_add_driver(&ad5398_driver);
  }
839b8362a   Sonic Zhang   regulator: make s...
263
  subsys_initcall(ad5398_init);
8b385d9b9   Sonic Zhang   regulator: new dr...
264
265
266
267
268
269
270
271
272
273
  
  static void __exit ad5398_exit(void)
  {
  	i2c_del_driver(&ad5398_driver);
  }
  module_exit(ad5398_exit);
  
  MODULE_DESCRIPTION("AD5398 and AD5821 current regulator driver");
  MODULE_AUTHOR("Sonic Zhang");
  MODULE_LICENSE("GPL");