regulator: mp8865: add regulator driver for MP8865
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / mp8865-regulator.c
1 /*
2  * Regulator driver for mp8865
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for  more details.
12  */
13
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/regulator/of_regulator.h>
20
21 #define MP8865_MAX_VSEL                 0x7f
22 #define VOL_MIN_IDX                     0x00
23 #define VOL_MAX_IDX                     0x7f
24
25 /* Register definitions */
26 #define MP8865_VOUT_REG                 0
27 #define MP8865_SYSCNTL_REG              1
28 #define MP8865_ID_REG                   2
29 #define MP8865_STATUS_REG               3
30
31 #define MP8865_VOUT_MASK                0x7f
32 #define MP8865_VBOOT_MASK               0x80
33
34 #define MP8865_ENABLE                   0x80
35 #define MP8865_GO_BIT                   0x40
36 #define MP8865_SLEW_RATE_MASK           0x1c
37 #define MP8865_SWITCH_FRE_MASK          0x06
38 #define MP8865_PFM_MODE                 0X01
39
40 /* mp8865 chip information */
41 struct mp8865_chip {
42         struct device *dev;
43         struct regulator_desc desc;
44         struct regulator_dev  *rdev;
45         struct regmap *regmap;
46 };
47
48 struct mp8865_platform_data {
49         struct regulator_init_data *mp8865_init_data;
50         struct device_node *of_node;
51 };
52
53 static const struct regulator_linear_range mp8865_voltage_ranges[] = {
54         REGULATOR_LINEAR_RANGE(600000, VOL_MIN_IDX, VOL_MAX_IDX, 10000),
55 };
56
57 static int mp8865_set_voltage(struct regulator_dev *rdev, unsigned sel)
58 {
59         int ret;
60
61         ret = regmap_update_bits(rdev->regmap, MP8865_SYSCNTL_REG,
62                                  MP8865_GO_BIT, MP8865_GO_BIT);
63         if (ret)
64                 return ret;
65
66         sel <<= ffs(rdev->desc->vsel_mask) - 1;
67         return regmap_write(rdev->regmap, MP8865_VOUT_REG, sel);
68 }
69
70 static const struct regmap_config mp8865_regmap_config = {
71         .reg_bits = 8,
72         .val_bits = 8,
73 };
74
75 static struct regulator_ops mp8865_dcdc_ops = {
76         .list_voltage = regulator_list_voltage_linear_range,
77         .map_voltage = regulator_map_voltage_linear_range,
78         .get_voltage_sel = regulator_get_voltage_sel_regmap,
79         .set_voltage_sel = mp8865_set_voltage,
80         .enable = regulator_enable_regmap,
81         .disable = regulator_disable_regmap,
82         .is_enabled = regulator_is_enabled_regmap,
83 };
84
85 static const struct of_device_id mp8865_of_match[] = {
86         {.compatible =  "mps,mp8865", .data = (void *)0},
87         {},
88 };
89 MODULE_DEVICE_TABLE(of, mp8865_of_match);
90
91 static struct of_regulator_match mp8865_matches = {
92         .name = "mp8865_dcdc1",
93         .driver_data = (void *)0,
94 };
95
96 static struct mp8865_platform_data *
97         mp8865_parse_dt(struct i2c_client *client,
98                         struct of_regulator_match **mp8865_reg_matches)
99 {
100         struct mp8865_platform_data *pdata;
101         struct device_node *np, *regulators;
102         int ret;
103
104         pdata = devm_kzalloc(&client->dev, sizeof(struct mp8865_platform_data),
105                              GFP_KERNEL);
106         if (!pdata)
107                 return NULL;
108
109         np = of_node_get(client->dev.of_node);
110         regulators = of_find_node_by_name(np, "regulators");
111         if (!regulators) {
112                 dev_err(&client->dev, "regulator node not found\n");
113                 return NULL;
114         }
115
116         ret = of_regulator_match(&client->dev, regulators, &mp8865_matches, 1);
117         if (ret < 0) {
118                 dev_err(&client->dev, "Error parsing regulators init data\n");
119                 return NULL;
120         }
121         *mp8865_reg_matches = &mp8865_matches;
122         of_node_put(regulators);
123
124         pdata->mp8865_init_data = mp8865_matches.init_data;
125         pdata->of_node = mp8865_matches.of_node;
126
127         return pdata;
128 }
129
130 static int mp8865_probe(struct i2c_client *client,
131                         const struct i2c_device_id *id)
132 {
133         struct mp8865_chip *mp8865;
134         struct mp8865_platform_data *pdata;
135         struct of_regulator_match *mp8865_reg_matches = NULL;
136         struct regulator_dev *sy_rdev;
137         struct regulator_config config;
138         struct regulator_desc *rdesc;
139
140         mp8865 = devm_kzalloc(&client->dev, sizeof(struct mp8865_chip),
141                               GFP_KERNEL);
142         if (!mp8865)
143                 return -ENOMEM;
144         pdata = dev_get_platdata(&client->dev);
145         if (!pdata)
146                 pdata = mp8865_parse_dt(client, &mp8865_reg_matches);
147
148         if (!pdata || !pdata->mp8865_init_data) {
149                 dev_err(&client->dev, "Platform data not found!\n");
150                 return -ENODEV;
151         }
152
153         mp8865->dev = &client->dev;
154         mp8865->regmap = devm_regmap_init_i2c(client, &mp8865_regmap_config);
155         if (IS_ERR(mp8865->regmap)) {
156                 dev_err(&client->dev, "Failed to allocate regmap!\n");
157                 return PTR_ERR(mp8865->regmap);
158         }
159
160         i2c_set_clientdata(client, mp8865);
161
162         config.dev = mp8865->dev;
163         config.driver_data = mp8865;
164         config.init_data = pdata->mp8865_init_data;
165         config.of_node = pdata->of_node;
166         config.regmap = mp8865->regmap;
167
168         rdesc = &mp8865->desc;
169         rdesc->name = "mp8865_dcdc1",
170         rdesc->id = 0,
171         rdesc->ops = &mp8865_dcdc_ops,
172         rdesc->n_voltages = MP8865_MAX_VSEL + 1,
173         rdesc->linear_ranges = mp8865_voltage_ranges,
174         rdesc->n_linear_ranges = ARRAY_SIZE(mp8865_voltage_ranges),
175         rdesc->vsel_reg = MP8865_VOUT_REG;
176         rdesc->vsel_mask = MP8865_VOUT_MASK;
177         rdesc->enable_reg = MP8865_SYSCNTL_REG;
178         rdesc->enable_mask = MP8865_ENABLE;
179         rdesc->type = REGULATOR_VOLTAGE,
180         rdesc->owner = THIS_MODULE;
181
182         sy_rdev = devm_regulator_register(mp8865->dev, &mp8865->desc,
183                                           &config);
184         if (IS_ERR(sy_rdev)) {
185                 dev_err(mp8865->dev, "failed to register regulator\n");
186                 return PTR_ERR(sy_rdev);
187         }
188
189         dev_info(mp8865->dev, "mp8865 register successful\n");
190
191         return 0;
192 }
193
194 static const struct i2c_device_id mp8865_id[] = {
195         { .name = "mps,mp8865", },
196         {  },
197 };
198 MODULE_DEVICE_TABLE(i2c, mp8865_id);
199
200 static struct i2c_driver mp8865_driver = {
201         .driver = {
202                 .name = "mp8865",
203                 .of_match_table = of_match_ptr(mp8865_of_match),
204                 .owner = THIS_MODULE,
205         },
206         .probe    = mp8865_probe,
207         .id_table = mp8865_id,
208 };
209 module_i2c_driver(mp8865_driver);
210
211 MODULE_AUTHOR("Zain Wang <zain.wang@rock-chips.com>");
212 MODULE_DESCRIPTION("mp8865 voltage regulator driver");
213 MODULE_LICENSE("GPL v2");