Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | struct 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 Lin | 58d463e | 2010-09-01 10:29:18 +0800 | [diff] [blame] | 28 | struct regulator_dev *rdev; |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static 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 | |
| 39 | static 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 | |
| 54 | static 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); |
Axel Lin | 4434cee | 2016-02-11 18:17:03 +0800 | [diff] [blame] | 61 | if (ret != 2) { |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 62 | dev_err(&client->dev, "I2C write error\n"); |
Axel Lin | 4434cee | 2016-02-11 18:17:03 +0800 | [diff] [blame] | 63 | return ret < 0 ? ret : -EIO; |
| 64 | } |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 65 | |
Axel Lin | 4434cee | 2016-02-11 18:17:03 +0800 | [diff] [blame] | 66 | return 0; |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static int ad5398_get_current_limit(struct regulator_dev *rdev) |
| 70 | { |
| 71 | struct ad5398_chip_info *chip = rdev_get_drvdata(rdev); |
| 72 | struct i2c_client *client = chip->client; |
| 73 | unsigned short data; |
| 74 | int ret; |
| 75 | |
| 76 | ret = ad5398_read_reg(client, &data); |
| 77 | if (ret < 0) |
| 78 | return ret; |
| 79 | |
| 80 | ret = (data & chip->current_mask) >> chip->current_offset; |
| 81 | |
| 82 | return ad5398_calc_current(chip, ret); |
| 83 | } |
| 84 | |
| 85 | static int ad5398_set_current_limit(struct regulator_dev *rdev, int min_uA, int max_uA) |
| 86 | { |
| 87 | struct ad5398_chip_info *chip = rdev_get_drvdata(rdev); |
| 88 | struct i2c_client *client = chip->client; |
| 89 | unsigned range_uA = chip->max_uA - chip->min_uA; |
| 90 | unsigned selector; |
| 91 | unsigned short data; |
| 92 | int ret; |
| 93 | |
Axel Lin | 9c6a74c | 2012-07-04 11:55:07 +0800 | [diff] [blame] | 94 | if (min_uA < chip->min_uA) |
| 95 | min_uA = chip->min_uA; |
| 96 | if (max_uA > chip->max_uA) |
| 97 | max_uA = chip->max_uA; |
| 98 | |
| 99 | if (min_uA > chip->max_uA || max_uA < chip->min_uA) |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 100 | return -EINVAL; |
| 101 | |
Axel Lin | 8148ed6 | 2012-03-02 16:20:54 +0800 | [diff] [blame] | 102 | selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level, |
| 103 | range_uA); |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 104 | if (ad5398_calc_current(chip, selector) > max_uA) |
| 105 | return -EINVAL; |
| 106 | |
Axel Lin | 935c14a | 2012-03-27 10:08:25 +0800 | [diff] [blame] | 107 | dev_dbg(&client->dev, "changing current %duA\n", |
| 108 | ad5398_calc_current(chip, selector)); |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 109 | |
| 110 | /* read chip enable bit */ |
| 111 | ret = ad5398_read_reg(client, &data); |
| 112 | if (ret < 0) |
| 113 | return ret; |
| 114 | |
| 115 | /* prepare register data */ |
| 116 | selector = (selector << chip->current_offset) & chip->current_mask; |
| 117 | data = (unsigned short)selector | (data & AD5398_CURRENT_EN_MASK); |
| 118 | |
| 119 | /* write the new current value back as well as enable bit */ |
| 120 | ret = ad5398_write_reg(client, data); |
| 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | static int ad5398_is_enabled(struct regulator_dev *rdev) |
| 126 | { |
| 127 | struct ad5398_chip_info *chip = rdev_get_drvdata(rdev); |
| 128 | struct i2c_client *client = chip->client; |
| 129 | unsigned short data; |
| 130 | int ret; |
| 131 | |
| 132 | ret = ad5398_read_reg(client, &data); |
| 133 | if (ret < 0) |
| 134 | return ret; |
| 135 | |
| 136 | if (data & AD5398_CURRENT_EN_MASK) |
| 137 | return 1; |
| 138 | else |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int ad5398_enable(struct regulator_dev *rdev) |
| 143 | { |
| 144 | struct ad5398_chip_info *chip = rdev_get_drvdata(rdev); |
| 145 | struct i2c_client *client = chip->client; |
| 146 | unsigned short data; |
| 147 | int ret; |
| 148 | |
| 149 | ret = ad5398_read_reg(client, &data); |
| 150 | if (ret < 0) |
| 151 | return ret; |
| 152 | |
| 153 | if (data & AD5398_CURRENT_EN_MASK) |
| 154 | return 0; |
| 155 | |
| 156 | data |= AD5398_CURRENT_EN_MASK; |
| 157 | |
| 158 | ret = ad5398_write_reg(client, data); |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | static int ad5398_disable(struct regulator_dev *rdev) |
| 164 | { |
| 165 | struct ad5398_chip_info *chip = rdev_get_drvdata(rdev); |
| 166 | struct i2c_client *client = chip->client; |
| 167 | unsigned short data; |
| 168 | int ret; |
| 169 | |
| 170 | ret = ad5398_read_reg(client, &data); |
| 171 | if (ret < 0) |
| 172 | return ret; |
| 173 | |
| 174 | if (!(data & AD5398_CURRENT_EN_MASK)) |
| 175 | return 0; |
| 176 | |
| 177 | data &= ~AD5398_CURRENT_EN_MASK; |
| 178 | |
| 179 | ret = ad5398_write_reg(client, data); |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
Bhumika Goyal | bb24b9d | 2017-01-28 19:05:22 +0530 | [diff] [blame] | 184 | static const struct regulator_ops ad5398_ops = { |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 185 | .get_current_limit = ad5398_get_current_limit, |
| 186 | .set_current_limit = ad5398_set_current_limit, |
| 187 | .enable = ad5398_enable, |
| 188 | .disable = ad5398_disable, |
| 189 | .is_enabled = ad5398_is_enabled, |
| 190 | }; |
| 191 | |
Axel Lin | 487f71e | 2012-04-05 11:56:56 +0800 | [diff] [blame] | 192 | static const struct regulator_desc ad5398_reg = { |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 193 | .name = "isink", |
| 194 | .id = 0, |
| 195 | .ops = &ad5398_ops, |
| 196 | .type = REGULATOR_CURRENT, |
| 197 | .owner = THIS_MODULE, |
| 198 | }; |
| 199 | |
| 200 | struct ad5398_current_data_format { |
| 201 | int current_bits; |
| 202 | int current_offset; |
| 203 | int min_uA; |
| 204 | int max_uA; |
| 205 | }; |
| 206 | |
| 207 | static const struct ad5398_current_data_format df_10_4_120 = {10, 4, 0, 120000}; |
| 208 | |
| 209 | static const struct i2c_device_id ad5398_id[] = { |
| 210 | { "ad5398", (kernel_ulong_t)&df_10_4_120 }, |
| 211 | { "ad5821", (kernel_ulong_t)&df_10_4_120 }, |
| 212 | { } |
| 213 | }; |
| 214 | MODULE_DEVICE_TABLE(i2c, ad5398_id); |
| 215 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 216 | static int ad5398_probe(struct i2c_client *client, |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 217 | const struct i2c_device_id *id) |
| 218 | { |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 219 | struct regulator_init_data *init_data = dev_get_platdata(&client->dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 220 | struct regulator_config config = { }; |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 221 | struct ad5398_chip_info *chip; |
| 222 | const struct ad5398_current_data_format *df = |
| 223 | (struct ad5398_current_data_format *)id->driver_data; |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 224 | |
| 225 | if (!init_data) |
| 226 | return -EINVAL; |
| 227 | |
Axel Lin | 0df8c96 | 2012-03-27 10:09:42 +0800 | [diff] [blame] | 228 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 229 | if (!chip) |
| 230 | return -ENOMEM; |
| 231 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 232 | config.dev = &client->dev; |
| 233 | config.init_data = init_data; |
| 234 | config.driver_data = chip; |
| 235 | |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 236 | chip->client = client; |
| 237 | |
| 238 | chip->min_uA = df->min_uA; |
| 239 | chip->max_uA = df->max_uA; |
| 240 | chip->current_level = 1 << df->current_bits; |
| 241 | chip->current_offset = df->current_offset; |
| 242 | chip->current_mask = (chip->current_level - 1) << chip->current_offset; |
| 243 | |
Axel Lin | 9b2cdac | 2013-09-03 14:20:25 +0800 | [diff] [blame] | 244 | chip->rdev = devm_regulator_register(&client->dev, &ad5398_reg, |
| 245 | &config); |
Axel Lin | 58d463e | 2010-09-01 10:29:18 +0800 | [diff] [blame] | 246 | if (IS_ERR(chip->rdev)) { |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 247 | dev_err(&client->dev, "failed to register %s %s\n", |
| 248 | id->name, ad5398_reg.name); |
Axel Lin | 9b2cdac | 2013-09-03 14:20:25 +0800 | [diff] [blame] | 249 | return PTR_ERR(chip->rdev); |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | i2c_set_clientdata(client, chip); |
| 253 | dev_dbg(&client->dev, "%s regulator driver is registered.\n", id->name); |
| 254 | return 0; |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static struct i2c_driver ad5398_driver = { |
| 258 | .probe = ad5398_probe, |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 259 | .driver = { |
| 260 | .name = "ad5398", |
| 261 | }, |
| 262 | .id_table = ad5398_id, |
| 263 | }; |
| 264 | |
| 265 | static int __init ad5398_init(void) |
| 266 | { |
| 267 | return i2c_add_driver(&ad5398_driver); |
| 268 | } |
Sonic Zhang | 839b836 | 2010-06-10 16:50:20 +0800 | [diff] [blame] | 269 | subsys_initcall(ad5398_init); |
Sonic Zhang | 8b385d9 | 2010-06-04 11:46:04 +0800 | [diff] [blame] | 270 | |
| 271 | static void __exit ad5398_exit(void) |
| 272 | { |
| 273 | i2c_del_driver(&ad5398_driver); |
| 274 | } |
| 275 | module_exit(ad5398_exit); |
| 276 | |
| 277 | MODULE_DESCRIPTION("AD5398 and AD5821 current regulator driver"); |
| 278 | MODULE_AUTHOR("Sonic Zhang"); |
| 279 | MODULE_LICENSE("GPL"); |