Blame view

drivers/regulator/max77693.c 7.17 KB
80b022e29   Jonghwa Lee   regulator: max776...
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
  /*
   * max77693.c - Regulator driver for the Maxim 77693
   *
   * Copyright (C) 2013 Samsung Electronics
   * Jonghwa Lee <jonghwa3.lee@samsung.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that 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.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   *
   * This driver is based on max77686.c
   */
  
  #include <linux/err.h>
  #include <linux/slab.h>
  #include <linux/platform_device.h>
  #include <linux/module.h>
  #include <linux/export.h>
  #include <linux/regulator/driver.h>
  #include <linux/regulator/machine.h>
  #include <linux/mfd/max77693.h>
  #include <linux/mfd/max77693-private.h>
  #include <linux/regulator/of_regulator.h>
d0540f91c   Robert Baldyga   mfd: max77693: Re...
34
  #include <linux/regmap.h>
80b022e29   Jonghwa Lee   regulator: max776...
35
36
  
  #define CHGIN_ILIM_STEP_20mA			20000
80b022e29   Jonghwa Lee   regulator: max776...
37
38
39
40
41
  /* CHARGER regulator ops */
  /* CHARGER regulator uses two bits for enabling */
  static int max77693_chg_is_enabled(struct regulator_dev *rdev)
  {
  	int ret;
d0540f91c   Robert Baldyga   mfd: max77693: Re...
42
  	unsigned int val;
80b022e29   Jonghwa Lee   regulator: max776...
43

d0540f91c   Robert Baldyga   mfd: max77693: Re...
44
  	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
80b022e29   Jonghwa Lee   regulator: max776...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  	if (ret)
  		return ret;
  
  	return (val & rdev->desc->enable_mask) == rdev->desc->enable_mask;
  }
  
  /*
   * CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
   * 0x00, 0x01, 0x2, 0x03	= 60 mA
   * 0x04 ~ 0x7E			= (60 + (X - 3) * 20) mA
   */
  static int max77693_chg_get_current_limit(struct regulator_dev *rdev)
  {
  	unsigned int chg_min_uA = rdev->constraints->min_uA;
  	unsigned int chg_max_uA = rdev->constraints->max_uA;
d0540f91c   Robert Baldyga   mfd: max77693: Re...
60
  	unsigned int reg, sel;
80b022e29   Jonghwa Lee   regulator: max776...
61
62
  	unsigned int val;
  	int ret;
d0540f91c   Robert Baldyga   mfd: max77693: Re...
63
  	ret = regmap_read(rdev->regmap, MAX77693_CHG_REG_CHG_CNFG_09, &reg);
80b022e29   Jonghwa Lee   regulator: max776...
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
  	if (ret < 0)
  		return ret;
  
  	sel = reg & CHG_CNFG_09_CHGIN_ILIM_MASK;
  
  	/* the first four codes for charger current are all 60mA */
  	if (sel <= 3)
  		sel = 0;
  	else
  		sel -= 3;
  
  	val = chg_min_uA + CHGIN_ILIM_STEP_20mA * sel;
  	if (val > chg_max_uA)
  		return -EINVAL;
  
  	return val;
  }
  
  static int max77693_chg_set_current_limit(struct regulator_dev *rdev,
  						int min_uA, int max_uA)
  {
  	unsigned int chg_min_uA = rdev->constraints->min_uA;
  	int sel = 0;
  
  	while (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel < min_uA)
  		sel++;
f3ecdd7bf   Axel Lin   regulator: max776...
90
  	if (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel > max_uA)
80b022e29   Jonghwa Lee   regulator: max776...
91
92
93
94
  		return -EINVAL;
  
  	/* the first four codes for charger current are all 60mA */
  	sel += 3;
d0540f91c   Robert Baldyga   mfd: max77693: Re...
95
  	return regmap_write(rdev->regmap,
80b022e29   Jonghwa Lee   regulator: max776...
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
  				MAX77693_CHG_REG_CHG_CNFG_09, sel);
  }
  /* end of CHARGER regulator ops */
  
  static const unsigned int max77693_safeout_table[] = {
  	4850000,
  	4900000,
  	4950000,
  	3300000,
  };
  
  static struct regulator_ops max77693_safeout_ops = {
  	.list_voltage		= regulator_list_voltage_table,
  	.is_enabled		= regulator_is_enabled_regmap,
  	.enable			= regulator_enable_regmap,
  	.disable		= regulator_disable_regmap,
  	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
  	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
  };
  
  static struct regulator_ops max77693_charger_ops = {
  	.is_enabled		= max77693_chg_is_enabled,
  	.enable			= regulator_enable_regmap,
  	.disable		= regulator_disable_regmap,
  	.get_current_limit	= max77693_chg_get_current_limit,
  	.set_current_limit	= max77693_chg_set_current_limit,
  };
  
  #define regulator_desc_esafeout(_num)	{			\
  	.name		= "ESAFEOUT"#_num,			\
  	.id		= MAX77693_ESAFEOUT##_num,		\
  	.n_voltages	= 4,					\
  	.ops		= &max77693_safeout_ops,		\
  	.type		= REGULATOR_VOLTAGE,			\
52f48bf33   Axel Lin   regulator: max776...
130
  	.owner		= THIS_MODULE,				\
80b022e29   Jonghwa Lee   regulator: max776...
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
  	.volt_table	= max77693_safeout_table,		\
  	.vsel_reg	= MAX77693_CHG_REG_SAFEOUT_CTRL,	\
  	.vsel_mask	= SAFEOUT_CTRL_SAFEOUT##_num##_MASK,	\
  	.enable_reg	= MAX77693_CHG_REG_SAFEOUT_CTRL,	\
  	.enable_mask	= SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK ,	\
  }
  
  static struct regulator_desc regulators[] = {
  	regulator_desc_esafeout(1),
  	regulator_desc_esafeout(2),
  	{
  		.name = "CHARGER",
  		.id = MAX77693_CHARGER,
  		.ops = &max77693_charger_ops,
  		.type = REGULATOR_CURRENT,
  		.owner = THIS_MODULE,
  		.enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
  		.enable_mask = CHG_CNFG_00_CHG_MASK |
  				CHG_CNFG_00_BUCK_MASK,
  	},
  };
  
  #ifdef CONFIG_OF
  static int max77693_pmic_dt_parse_rdata(struct device *dev,
  					struct max77693_regulator_data **rdata)
  {
  	struct device_node *np;
  	struct of_regulator_match *rmatch;
  	struct max77693_regulator_data *tmp;
  	int i, matched = 0;
4fa43a4a6   Sachin Kamat   regulator: max776...
161
  	np = of_get_child_by_name(dev->parent->of_node, "regulators");
80b022e29   Jonghwa Lee   regulator: max776...
162
163
164
165
166
  	if (!np)
  		return -EINVAL;
  
  	rmatch = devm_kzalloc(dev,
  		 sizeof(*rmatch) * ARRAY_SIZE(regulators), GFP_KERNEL);
d9aa5c563   Sachin Kamat   regulator: max776...
167
168
  	if (!rmatch) {
  		of_node_put(np);
80b022e29   Jonghwa Lee   regulator: max776...
169
  		return -ENOMEM;
d9aa5c563   Sachin Kamat   regulator: max776...
170
  	}
80b022e29   Jonghwa Lee   regulator: max776...
171
172
173
174
175
  
  	for (i = 0; i < ARRAY_SIZE(regulators); i++)
  		rmatch[i].name = regulators[i].name;
  
  	matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(regulators));
d9aa5c563   Sachin Kamat   regulator: max776...
176
  	of_node_put(np);
80b022e29   Jonghwa Lee   regulator: max776...
177
178
179
180
181
182
183
  	if (matched <= 0)
  		return matched;
  	*rdata = devm_kzalloc(dev, sizeof(**rdata) * matched, GFP_KERNEL);
  	if (!(*rdata))
  		return -ENOMEM;
  
  	tmp = *rdata;
8c7e7ddf3   Axel Lin   regulator: max776...
184
  	for (i = 0; i < matched; i++) {
80b022e29   Jonghwa Lee   regulator: max776...
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
  		tmp->initdata = rmatch[i].init_data;
  		tmp->of_node = rmatch[i].of_node;
  		tmp->id = regulators[i].id;
  		tmp++;
  	}
  
  	return matched;
  }
  #else
  static int max77693_pmic_dt_parse_rdata(struct device *dev,
  					struct max77693_regulator_data **rdata)
  {
  	return 0;
  }
  #endif /* CONFIG_OF */
  
  static int max77693_pmic_init_rdata(struct device *dev,
  				    struct max77693_regulator_data **rdata)
  {
  	struct max77693_platform_data *pdata;
  	int num_regulators = 0;
  
  	pdata = dev_get_platdata(dev->parent);
  	if (pdata) {
  		*rdata = pdata->regulators;
  		num_regulators = pdata->num_regulators;
  	}
  
  	if (!(*rdata) && dev->parent->of_node)
  		num_regulators = max77693_pmic_dt_parse_rdata(dev, rdata);
  
  	return num_regulators;
  }
  
  static int max77693_pmic_probe(struct platform_device *pdev)
  {
  	struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
80b022e29   Jonghwa Lee   regulator: max776...
222
  	struct max77693_regulator_data *rdata = NULL;
027a27545   Sachin Kamat   regulator: max776...
223
  	int num_rdata, i;
ca0c37a0b   Krzysztof Kozlowski   regulator: max776...
224
  	struct regulator_config config = { };
80b022e29   Jonghwa Lee   regulator: max776...
225
226
227
228
229
230
231
  
  	num_rdata = max77693_pmic_init_rdata(&pdev->dev, &rdata);
  	if (!rdata || num_rdata <= 0) {
  		dev_err(&pdev->dev, "No init data supplied.
  ");
  		return -ENODEV;
  	}
80b022e29   Jonghwa Lee   regulator: max776...
232
233
  	config.dev = &pdev->dev;
  	config.regmap = iodev->regmap;
80b022e29   Jonghwa Lee   regulator: max776...
234

640c24a77   Krzysztof Kozlowski   regulator: max776...
235
  	for (i = 0; i < num_rdata; i++) {
80b022e29   Jonghwa Lee   regulator: max776...
236
  		int id = rdata[i].id;
640c24a77   Krzysztof Kozlowski   regulator: max776...
237
  		struct regulator_dev *rdev;
80b022e29   Jonghwa Lee   regulator: max776...
238
239
240
  
  		config.init_data = rdata[i].initdata;
  		config.of_node = rdata[i].of_node;
640c24a77   Krzysztof Kozlowski   regulator: max776...
241
  		rdev = devm_regulator_register(&pdev->dev,
027a27545   Sachin Kamat   regulator: max776...
242
  						&regulators[id], &config);
640c24a77   Krzysztof Kozlowski   regulator: max776...
243
244
  		if (IS_ERR(rdev)) {
  			dev_err(&pdev->dev,
80b022e29   Jonghwa Lee   regulator: max776...
245
246
  				"Failed to initialize regulator-%d
  ", id);
640c24a77   Krzysztof Kozlowski   regulator: max776...
247
  			return PTR_ERR(rdev);
80b022e29   Jonghwa Lee   regulator: max776...
248
249
250
251
  		}
  	}
  
  	return 0;
80b022e29   Jonghwa Lee   regulator: max776...
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  }
  
  static const struct platform_device_id max77693_pmic_id[] = {
  	{"max77693-pmic", 0},
  	{},
  };
  
  MODULE_DEVICE_TABLE(platform, max77693_pmic_id);
  
  static struct platform_driver max77693_pmic_driver = {
  	.driver = {
  		   .name = "max77693-pmic",
  		   .owner = THIS_MODULE,
  		   },
  	.probe = max77693_pmic_probe,
80b022e29   Jonghwa Lee   regulator: max776...
267
268
269
270
271
272
273
274
  	.id_table = max77693_pmic_id,
  };
  
  module_platform_driver(max77693_pmic_driver);
  
  MODULE_DESCRIPTION("MAXIM MAX77693 regulator driver");
  MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
  MODULE_LICENSE("GPL");