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