Merge tag 'for-linus' of git://linux-c6x.org/git/projects/linux-c6x-upstreaming
[profile/ivi/kernel-x86-ivi.git] / drivers / regulator / da9210-regulator.c
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/regulator/of_regulator.h>
29 #include <linux/regmap.h>
30
31 #include "da9210-regulator.h"
32
33 struct da9210 {
34         struct regulator_dev *rdev;
35         struct regmap *regmap;
36 };
37
38 static const struct regmap_config da9210_regmap_config = {
39         .reg_bits = 8,
40         .val_bits = 8,
41 };
42
43 static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
44                                     int max_uA);
45 static int da9210_get_current_limit(struct regulator_dev *rdev);
46
47 static struct regulator_ops da9210_buck_ops = {
48         .enable = regulator_enable_regmap,
49         .disable = regulator_disable_regmap,
50         .is_enabled = regulator_is_enabled_regmap,
51         .set_voltage_sel = regulator_set_voltage_sel_regmap,
52         .get_voltage_sel = regulator_get_voltage_sel_regmap,
53         .list_voltage = regulator_list_voltage_linear,
54         .set_current_limit = da9210_set_current_limit,
55         .get_current_limit = da9210_get_current_limit,
56 };
57
58 /* Default limits measured in millivolts and milliamps */
59 #define DA9210_MIN_MV           300
60 #define DA9210_MAX_MV           1570
61 #define DA9210_STEP_MV          10
62
63 /* Current limits for buck (uA) indices corresponds with register values */
64 static const int da9210_buck_limits[] = {
65         1600000, 1800000, 2000000, 2200000, 2400000, 2600000, 2800000, 3000000,
66         3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 4600000
67 };
68
69 static const struct regulator_desc da9210_reg = {
70         .name = "DA9210",
71         .id = 0,
72         .ops = &da9210_buck_ops,
73         .type = REGULATOR_VOLTAGE,
74         .n_voltages = ((DA9210_MAX_MV - DA9210_MIN_MV) / DA9210_STEP_MV) + 1,
75         .min_uV = (DA9210_MIN_MV * 1000),
76         .uV_step = (DA9210_STEP_MV * 1000),
77         .vsel_reg = DA9210_REG_VBUCK_A,
78         .vsel_mask = DA9210_VBUCK_MASK,
79         .enable_reg = DA9210_REG_BUCK_CONT,
80         .enable_mask = DA9210_BUCK_EN,
81         .owner = THIS_MODULE,
82 };
83
84 static int da9210_set_current_limit(struct regulator_dev *rdev, int min_uA,
85                                     int max_uA)
86 {
87         struct da9210 *chip = rdev_get_drvdata(rdev);
88         unsigned int sel;
89         int i;
90
91         /* search for closest to maximum */
92         for (i = ARRAY_SIZE(da9210_buck_limits)-1; i >= 0; i--) {
93                 if (min_uA <= da9210_buck_limits[i] &&
94                     max_uA >= da9210_buck_limits[i]) {
95                         sel = i;
96                         sel = sel << DA9210_BUCK_ILIM_SHIFT;
97                         return regmap_update_bits(chip->regmap,
98                                                   DA9210_REG_BUCK_ILIM,
99                                                   DA9210_BUCK_ILIM_MASK, sel);
100                 }
101         }
102
103         return -EINVAL;
104 }
105
106 static int da9210_get_current_limit(struct regulator_dev *rdev)
107 {
108         struct da9210 *chip = rdev_get_drvdata(rdev);
109         unsigned int data;
110         unsigned int sel;
111         int ret;
112
113         ret = regmap_read(chip->regmap, DA9210_REG_BUCK_ILIM, &data);
114         if (ret < 0)
115                 return ret;
116
117         /* select one of 16 values: 0000 (1600mA) to 1111 (4600mA) */
118         sel = (data & DA9210_BUCK_ILIM_MASK) >> DA9210_BUCK_ILIM_SHIFT;
119
120         return da9210_buck_limits[sel];
121 }
122
123 /*
124  * I2C driver interface functions
125  */
126 static int da9210_i2c_probe(struct i2c_client *i2c,
127                             const struct i2c_device_id *id)
128 {
129         struct da9210 *chip;
130         struct device *dev = &i2c->dev;
131         struct da9210_pdata *pdata = dev_get_platdata(dev);
132         struct regulator_dev *rdev = NULL;
133         struct regulator_config config = { };
134         int error;
135
136         chip = devm_kzalloc(&i2c->dev, sizeof(struct da9210), GFP_KERNEL);
137         if (NULL == chip) {
138                 dev_err(&i2c->dev,
139                         "Cannot kzalloc memory for regulator structure\n");
140                 return -ENOMEM;
141         }
142
143         chip->regmap = devm_regmap_init_i2c(i2c, &da9210_regmap_config);
144         if (IS_ERR(chip->regmap)) {
145                 error = PTR_ERR(chip->regmap);
146                 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
147                         error);
148                 return error;
149         }
150
151         config.dev = &i2c->dev;
152         config.init_data = pdata ? &pdata->da9210_constraints :
153                 of_get_regulator_init_data(dev, dev->of_node);
154         config.driver_data = chip;
155         config.regmap = chip->regmap;
156         config.of_node = dev->of_node;
157
158         rdev = devm_regulator_register(&i2c->dev, &da9210_reg, &config);
159         if (IS_ERR(rdev)) {
160                 dev_err(&i2c->dev, "Failed to register DA9210 regulator\n");
161                 return PTR_ERR(rdev);
162         }
163
164         chip->rdev = rdev;
165
166         i2c_set_clientdata(i2c, chip);
167
168         return 0;
169 }
170
171 static const struct i2c_device_id da9210_i2c_id[] = {
172         {"da9210", 0},
173         {},
174 };
175
176 MODULE_DEVICE_TABLE(i2c, da9210_i2c_id);
177
178 static struct i2c_driver da9210_regulator_driver = {
179         .driver = {
180                 .name = "da9210",
181                 .owner = THIS_MODULE,
182         },
183         .probe = da9210_i2c_probe,
184         .id_table = da9210_i2c_id,
185 };
186
187 module_i2c_driver(da9210_regulator_driver);
188
189 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
190 MODULE_DESCRIPTION("Regulator device driver for Dialog DA9210");
191 MODULE_LICENSE("GPL v2");