18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Voltage and current regulation for AD5398 and AD5821 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2010 Analog Devices Inc. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Enter bugs at http://blackfin.uclinux.org/ 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/err.h> 128c2ecf20Sopenharmony_ci#include <linux/i2c.h> 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 158c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h> 168c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#define AD5398_CURRENT_EN_MASK 0x8000 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistruct ad5398_chip_info { 218c2ecf20Sopenharmony_ci struct i2c_client *client; 228c2ecf20Sopenharmony_ci int min_uA; 238c2ecf20Sopenharmony_ci int max_uA; 248c2ecf20Sopenharmony_ci unsigned int current_level; 258c2ecf20Sopenharmony_ci unsigned int current_mask; 268c2ecf20Sopenharmony_ci unsigned int current_offset; 278c2ecf20Sopenharmony_ci struct regulator_dev *rdev; 288c2ecf20Sopenharmony_ci}; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic int ad5398_calc_current(struct ad5398_chip_info *chip, 318c2ecf20Sopenharmony_ci unsigned selector) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci unsigned range_uA = chip->max_uA - chip->min_uA; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci return chip->min_uA + (selector * range_uA / chip->current_level); 368c2ecf20Sopenharmony_ci} 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic int ad5398_read_reg(struct i2c_client *client, unsigned short *data) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci unsigned short val; 418c2ecf20Sopenharmony_ci int ret; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci ret = i2c_master_recv(client, (char *)&val, 2); 448c2ecf20Sopenharmony_ci if (ret < 0) { 458c2ecf20Sopenharmony_ci dev_err(&client->dev, "I2C read error\n"); 468c2ecf20Sopenharmony_ci return ret; 478c2ecf20Sopenharmony_ci } 488c2ecf20Sopenharmony_ci *data = be16_to_cpu(val); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci return ret; 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic int ad5398_write_reg(struct i2c_client *client, const unsigned short data) 548c2ecf20Sopenharmony_ci{ 558c2ecf20Sopenharmony_ci unsigned short val; 568c2ecf20Sopenharmony_ci int ret; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci val = cpu_to_be16(data); 598c2ecf20Sopenharmony_ci ret = i2c_master_send(client, (char *)&val, 2); 608c2ecf20Sopenharmony_ci if (ret != 2) { 618c2ecf20Sopenharmony_ci dev_err(&client->dev, "I2C write error\n"); 628c2ecf20Sopenharmony_ci return ret < 0 ? ret : -EIO; 638c2ecf20Sopenharmony_ci } 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci return 0; 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic int ad5398_get_current_limit(struct regulator_dev *rdev) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci struct ad5398_chip_info *chip = rdev_get_drvdata(rdev); 718c2ecf20Sopenharmony_ci struct i2c_client *client = chip->client; 728c2ecf20Sopenharmony_ci unsigned short data; 738c2ecf20Sopenharmony_ci int ret; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci ret = ad5398_read_reg(client, &data); 768c2ecf20Sopenharmony_ci if (ret < 0) 778c2ecf20Sopenharmony_ci return ret; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci ret = (data & chip->current_mask) >> chip->current_offset; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci return ad5398_calc_current(chip, ret); 828c2ecf20Sopenharmony_ci} 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistatic int ad5398_set_current_limit(struct regulator_dev *rdev, int min_uA, int max_uA) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci struct ad5398_chip_info *chip = rdev_get_drvdata(rdev); 878c2ecf20Sopenharmony_ci struct i2c_client *client = chip->client; 888c2ecf20Sopenharmony_ci unsigned range_uA = chip->max_uA - chip->min_uA; 898c2ecf20Sopenharmony_ci unsigned selector; 908c2ecf20Sopenharmony_ci unsigned short data; 918c2ecf20Sopenharmony_ci int ret; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci if (min_uA < chip->min_uA) 948c2ecf20Sopenharmony_ci min_uA = chip->min_uA; 958c2ecf20Sopenharmony_ci if (max_uA > chip->max_uA) 968c2ecf20Sopenharmony_ci max_uA = chip->max_uA; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci if (min_uA > chip->max_uA || max_uA < chip->min_uA) 998c2ecf20Sopenharmony_ci return -EINVAL; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci selector = DIV_ROUND_UP((min_uA - chip->min_uA) * chip->current_level, 1028c2ecf20Sopenharmony_ci range_uA); 1038c2ecf20Sopenharmony_ci if (ad5398_calc_current(chip, selector) > max_uA) 1048c2ecf20Sopenharmony_ci return -EINVAL; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "changing current %duA\n", 1078c2ecf20Sopenharmony_ci ad5398_calc_current(chip, selector)); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci /* read chip enable bit */ 1108c2ecf20Sopenharmony_ci ret = ad5398_read_reg(client, &data); 1118c2ecf20Sopenharmony_ci if (ret < 0) 1128c2ecf20Sopenharmony_ci return ret; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci /* prepare register data */ 1158c2ecf20Sopenharmony_ci selector = (selector << chip->current_offset) & chip->current_mask; 1168c2ecf20Sopenharmony_ci data = (unsigned short)selector | (data & AD5398_CURRENT_EN_MASK); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci /* write the new current value back as well as enable bit */ 1198c2ecf20Sopenharmony_ci ret = ad5398_write_reg(client, data); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci return ret; 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic int ad5398_is_enabled(struct regulator_dev *rdev) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci struct ad5398_chip_info *chip = rdev_get_drvdata(rdev); 1278c2ecf20Sopenharmony_ci struct i2c_client *client = chip->client; 1288c2ecf20Sopenharmony_ci unsigned short data; 1298c2ecf20Sopenharmony_ci int ret; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci ret = ad5398_read_reg(client, &data); 1328c2ecf20Sopenharmony_ci if (ret < 0) 1338c2ecf20Sopenharmony_ci return ret; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci if (data & AD5398_CURRENT_EN_MASK) 1368c2ecf20Sopenharmony_ci return 1; 1378c2ecf20Sopenharmony_ci else 1388c2ecf20Sopenharmony_ci return 0; 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistatic int ad5398_enable(struct regulator_dev *rdev) 1428c2ecf20Sopenharmony_ci{ 1438c2ecf20Sopenharmony_ci struct ad5398_chip_info *chip = rdev_get_drvdata(rdev); 1448c2ecf20Sopenharmony_ci struct i2c_client *client = chip->client; 1458c2ecf20Sopenharmony_ci unsigned short data; 1468c2ecf20Sopenharmony_ci int ret; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci ret = ad5398_read_reg(client, &data); 1498c2ecf20Sopenharmony_ci if (ret < 0) 1508c2ecf20Sopenharmony_ci return ret; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci if (data & AD5398_CURRENT_EN_MASK) 1538c2ecf20Sopenharmony_ci return 0; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci data |= AD5398_CURRENT_EN_MASK; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci ret = ad5398_write_reg(client, data); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci return ret; 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_cistatic int ad5398_disable(struct regulator_dev *rdev) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci struct ad5398_chip_info *chip = rdev_get_drvdata(rdev); 1658c2ecf20Sopenharmony_ci struct i2c_client *client = chip->client; 1668c2ecf20Sopenharmony_ci unsigned short data; 1678c2ecf20Sopenharmony_ci int ret; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci ret = ad5398_read_reg(client, &data); 1708c2ecf20Sopenharmony_ci if (ret < 0) 1718c2ecf20Sopenharmony_ci return ret; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci if (!(data & AD5398_CURRENT_EN_MASK)) 1748c2ecf20Sopenharmony_ci return 0; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci data &= ~AD5398_CURRENT_EN_MASK; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci ret = ad5398_write_reg(client, data); 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci return ret; 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_cistatic const struct regulator_ops ad5398_ops = { 1848c2ecf20Sopenharmony_ci .get_current_limit = ad5398_get_current_limit, 1858c2ecf20Sopenharmony_ci .set_current_limit = ad5398_set_current_limit, 1868c2ecf20Sopenharmony_ci .enable = ad5398_enable, 1878c2ecf20Sopenharmony_ci .disable = ad5398_disable, 1888c2ecf20Sopenharmony_ci .is_enabled = ad5398_is_enabled, 1898c2ecf20Sopenharmony_ci}; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_cistatic const struct regulator_desc ad5398_reg = { 1928c2ecf20Sopenharmony_ci .name = "isink", 1938c2ecf20Sopenharmony_ci .id = 0, 1948c2ecf20Sopenharmony_ci .ops = &ad5398_ops, 1958c2ecf20Sopenharmony_ci .type = REGULATOR_CURRENT, 1968c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 1978c2ecf20Sopenharmony_ci}; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_cistruct ad5398_current_data_format { 2008c2ecf20Sopenharmony_ci int current_bits; 2018c2ecf20Sopenharmony_ci int current_offset; 2028c2ecf20Sopenharmony_ci int min_uA; 2038c2ecf20Sopenharmony_ci int max_uA; 2048c2ecf20Sopenharmony_ci}; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_cistatic const struct ad5398_current_data_format df_10_4_120 = {10, 4, 0, 120000}; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistatic const struct i2c_device_id ad5398_id[] = { 2098c2ecf20Sopenharmony_ci { "ad5398", (kernel_ulong_t)&df_10_4_120 }, 2108c2ecf20Sopenharmony_ci { "ad5821", (kernel_ulong_t)&df_10_4_120 }, 2118c2ecf20Sopenharmony_ci { } 2128c2ecf20Sopenharmony_ci}; 2138c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, ad5398_id); 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_cistatic int ad5398_probe(struct i2c_client *client, 2168c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 2178c2ecf20Sopenharmony_ci{ 2188c2ecf20Sopenharmony_ci struct regulator_init_data *init_data = dev_get_platdata(&client->dev); 2198c2ecf20Sopenharmony_ci struct regulator_config config = { }; 2208c2ecf20Sopenharmony_ci struct ad5398_chip_info *chip; 2218c2ecf20Sopenharmony_ci const struct ad5398_current_data_format *df = 2228c2ecf20Sopenharmony_ci (struct ad5398_current_data_format *)id->driver_data; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci if (!init_data) 2258c2ecf20Sopenharmony_ci return -EINVAL; 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); 2288c2ecf20Sopenharmony_ci if (!chip) 2298c2ecf20Sopenharmony_ci return -ENOMEM; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci config.dev = &client->dev; 2328c2ecf20Sopenharmony_ci config.init_data = init_data; 2338c2ecf20Sopenharmony_ci config.driver_data = chip; 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci chip->client = client; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci chip->min_uA = df->min_uA; 2388c2ecf20Sopenharmony_ci chip->max_uA = df->max_uA; 2398c2ecf20Sopenharmony_ci chip->current_level = 1 << df->current_bits; 2408c2ecf20Sopenharmony_ci chip->current_offset = df->current_offset; 2418c2ecf20Sopenharmony_ci chip->current_mask = (chip->current_level - 1) << chip->current_offset; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci chip->rdev = devm_regulator_register(&client->dev, &ad5398_reg, 2448c2ecf20Sopenharmony_ci &config); 2458c2ecf20Sopenharmony_ci if (IS_ERR(chip->rdev)) { 2468c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed to register %s %s\n", 2478c2ecf20Sopenharmony_ci id->name, ad5398_reg.name); 2488c2ecf20Sopenharmony_ci return PTR_ERR(chip->rdev); 2498c2ecf20Sopenharmony_ci } 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci i2c_set_clientdata(client, chip); 2528c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "%s regulator driver is registered.\n", id->name); 2538c2ecf20Sopenharmony_ci return 0; 2548c2ecf20Sopenharmony_ci} 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_cistatic struct i2c_driver ad5398_driver = { 2578c2ecf20Sopenharmony_ci .probe = ad5398_probe, 2588c2ecf20Sopenharmony_ci .driver = { 2598c2ecf20Sopenharmony_ci .name = "ad5398", 2608c2ecf20Sopenharmony_ci }, 2618c2ecf20Sopenharmony_ci .id_table = ad5398_id, 2628c2ecf20Sopenharmony_ci}; 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_cistatic int __init ad5398_init(void) 2658c2ecf20Sopenharmony_ci{ 2668c2ecf20Sopenharmony_ci return i2c_add_driver(&ad5398_driver); 2678c2ecf20Sopenharmony_ci} 2688c2ecf20Sopenharmony_cisubsys_initcall(ad5398_init); 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_cistatic void __exit ad5398_exit(void) 2718c2ecf20Sopenharmony_ci{ 2728c2ecf20Sopenharmony_ci i2c_del_driver(&ad5398_driver); 2738c2ecf20Sopenharmony_ci} 2748c2ecf20Sopenharmony_cimodule_exit(ad5398_exit); 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("AD5398 and AD5821 current regulator driver"); 2778c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sonic Zhang"); 2788c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 279