1 /* 2 * da9210-regulator.c - Regulator device driver for DA9210 3 * Copyright (C) 2013 Dialog Semiconductor Ltd. 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public 16 * License along with this library; if not, write to the 17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 */ 20 21 #include <linux/err.h> 22 #include <linux/i2c.h> 23 #include <linux/module.h> 24 #include <linux/init.h> 25 #include <linux/slab.h> 26 #include <linux/regulator/driver.h> 27 #include <linux/regulator/machine.h> 28 #include <linux/regmap.h> 29 30 #include "da9210-regulator.h" 31 32 struct da9210 { 33 struct regulator_dev *rdev; 34 struct regmap *regmap; 35 }; 36 37 static const struct regmap_config da9210_regmap_config = { 38 .reg_bits = 8, 39 .val_bits = 8, 40 }; 41 42 static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA, 43 int max_uA); 44 static int da9210_get_current_limit(struct regulator_dev *rdev); 45 46 static struct regulator_ops da9210_buck_ops = { 47 .enable = regulator_enable_regmap, 48 .disable = regulator_disable_regmap, 49 .is_enabled = regulator_is_enabled_regmap, 50 .set_voltage_sel = regulator_set_voltage_sel_regmap, 51 .get_voltage_sel = regulator_get_voltage_sel_regmap, 52 .list_voltage = regulator_list_voltage_linear, 53 .set_current_limit = da9210_set_current_limit, 54 .get_current_limit = da9210_get_current_limit, 55 }; 56 57 /* Default limits measured in millivolts and milliamps */ 58 #define DA9210_MIN_MV 300 59 #define DA9210_MAX_MV 1570 60 #define DA9210_STEP_MV 10 61 62 /* Current limits for buck (uA) indices corresponds with register values */ 63 static const int da9210_buck_limits[] = { 64 1600000, 1800000, 2000000, 2200000, 2400000, 2600000, 2800000, 3000000, 65 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 4600000 66 }; 67 68 static const struct regulator_desc da9210_reg = { 69 .name = "DA9210", 70 .id = 0, 71 .ops = &da9210_buck_ops, 72 .type = REGULATOR_VOLTAGE, 73 .n_voltages = ((DA9210_MAX_MV - DA9210_MIN_MV) / DA9210_STEP_MV) + 1, 74 .min_uV = (DA9210_MIN_MV * 1000), 75 .uV_step = (DA9210_STEP_MV * 1000), 76 .vsel_reg = DA9210_REG_VBUCK_A, 77 .vsel_mask = DA9210_VBUCK_MASK, 78 .enable_reg = DA9210_REG_BUCK_CONT, 79 .enable_mask = DA9210_BUCK_EN, 80 .owner = THIS_MODULE, 81 }; 82 83 static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA, 84 int max_uA) 85 { 86 struct da9210 *chip = rdev_get_drvdata(rdev); 87 unsigned int sel; 88 int i; 89 90 /* search for closest to maximum */ 91 for (i = ARRAY_SIZE(da9210_buck_limits)-1; i >= 0; i--) { 92 if (min_uA <= da9210_buck_limits[i] && 93 max_uA >= da9210_buck_limits[i]) { 94 sel = i; 95 sel = sel << DA9210_BUCK_ILIM_SHIFT; 96 return regmap_update_bits(chip->regmap, 97 DA9210_REG_BUCK_ILIM, 98 DA9210_BUCK_ILIM_MASK, sel); 99 } 100 } 101 102 return -EINVAL; 103 } 104 105 static int da9210_get_current_limit(struct regulator_dev *rdev) 106 { 107 struct da9210 *chip = rdev_get_drvdata(rdev); 108 unsigned int data; 109 unsigned int sel; 110 int ret; 111 112 ret = regmap_read(chip->regmap, DA9210_REG_BUCK_ILIM, &data); 113 if (ret < 0) 114 return ret; 115 116 /* select one of 16 values: 0000 (1600mA) to 1111 (4600mA) */ 117 sel = (data & DA9210_BUCK_ILIM_MASK) >> DA9210_BUCK_ILIM_SHIFT; 118 119 return da9210_buck_limits[sel]; 120 } 121 122 /* 123 * I2C driver interface functions 124 */ 125 static int da9210_i2c_probe(struct i2c_client *i2c, 126 const struct i2c_device_id *id) 127 { 128 struct da9210 *chip; 129 struct da9210_pdata *pdata = i2c->dev.platform_data; 130 struct regulator_dev *rdev = NULL; 131 struct regulator_config config = { }; 132 int error; 133 134 chip = devm_kzalloc(&i2c->dev, sizeof(struct da9210), GFP_KERNEL); 135 if (NULL == chip) { 136 dev_err(&i2c->dev, 137 "Cannot kzalloc memory for regulator structure\n"); 138 return -ENOMEM; 139 } 140 141 chip->regmap = devm_regmap_init_i2c(i2c, &da9210_regmap_config); 142 if (IS_ERR(chip->regmap)) { 143 error = PTR_ERR(chip->regmap); 144 dev_err(&i2c->dev, "Failed to allocate register map: %d\n", 145 error); 146 return error; 147 } 148 149 config.dev = &i2c->dev; 150 if (pdata) 151 config.init_data = &pdata->da9210_constraints; 152 config.driver_data = chip; 153 config.regmap = chip->regmap; 154 155 rdev = regulator_register(&da9210_reg, &config); 156 if (IS_ERR(rdev)) { 157 dev_err(&i2c->dev, "Failed to register DA9210 regulator\n"); 158 return PTR_ERR(rdev); 159 } 160 161 chip->rdev = rdev; 162 163 i2c_set_clientdata(i2c, chip); 164 165 return 0; 166 } 167 168 static int da9210_i2c_remove(struct i2c_client *i2c) 169 { 170 struct da9210 *chip = i2c_get_clientdata(i2c); 171 regulator_unregister(chip->rdev); 172 return 0; 173 } 174 175 static const struct i2c_device_id da9210_i2c_id[] = { 176 {"da9210", 0}, 177 {}, 178 }; 179 180 MODULE_DEVICE_TABLE(i2c, da9210_i2c_id); 181 182 static struct i2c_driver da9210_regulator_driver = { 183 .driver = { 184 .name = "da9210", 185 .owner = THIS_MODULE, 186 }, 187 .probe = da9210_i2c_probe, 188 .remove = da9210_i2c_remove, 189 .id_table = da9210_i2c_id, 190 }; 191 192 module_i2c_driver(da9210_regulator_driver); 193 194 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>"); 195 MODULE_DESCRIPTION("Regulator device driver for Dialog DA9210"); 196 MODULE_LICENSE("GPL v2"); 197