regulator: max77693: Support different register configurations
[deliverable/linux.git] / drivers / regulator / max77693.c
1 /*
2 * max77693.c - Regulator driver for the Maxim 77693
3 *
4 * Copyright (C) 2013 Samsung Electronics
5 * Jonghwa Lee <jonghwa3.lee@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * This driver is based on max77686.c
22 */
23
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/platform_device.h>
27 #include <linux/module.h>
28 #include <linux/export.h>
29 #include <linux/regulator/driver.h>
30 #include <linux/regulator/machine.h>
31 #include <linux/mfd/max77693.h>
32 #include <linux/mfd/max77693-private.h>
33 #include <linux/regulator/of_regulator.h>
34 #include <linux/regmap.h>
35
36 /* Charger regulator differences between MAX77693 and MAX77843 */
37 struct chg_reg_data {
38 unsigned int linear_reg;
39 unsigned int linear_mask;
40 unsigned int uA_step;
41 unsigned int min_sel;
42 };
43
44 /*
45 * CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
46 * 0x00, 0x01, 0x2, 0x03 = 60 mA
47 * 0x04 ~ 0x7E = (60 + (X - 3) * 20) mA
48 */
49 static int max77693_chg_get_current_limit(struct regulator_dev *rdev)
50 {
51 const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
52 unsigned int chg_min_uA = rdev->constraints->min_uA;
53 unsigned int chg_max_uA = rdev->constraints->max_uA;
54 unsigned int reg, sel;
55 unsigned int val;
56 int ret;
57
58 ret = regmap_read(rdev->regmap, reg_data->linear_reg, &reg);
59 if (ret < 0)
60 return ret;
61
62 sel = reg & reg_data->linear_mask;
63
64 /* the first four codes for charger current are all 60mA */
65 if (sel <= reg_data->min_sel)
66 sel = 0;
67 else
68 sel -= reg_data->min_sel;
69
70 val = chg_min_uA + reg_data->uA_step * sel;
71 if (val > chg_max_uA)
72 return -EINVAL;
73
74 return val;
75 }
76
77 static int max77693_chg_set_current_limit(struct regulator_dev *rdev,
78 int min_uA, int max_uA)
79 {
80 const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
81 unsigned int chg_min_uA = rdev->constraints->min_uA;
82 int sel = 0;
83
84 while (chg_min_uA + reg_data->uA_step * sel < min_uA)
85 sel++;
86
87 if (chg_min_uA + reg_data->uA_step * sel > max_uA)
88 return -EINVAL;
89
90 /* the first four codes for charger current are all 60mA */
91 sel += reg_data->min_sel;
92
93 return regmap_write(rdev->regmap, reg_data->linear_reg, sel);
94 }
95 /* end of CHARGER regulator ops */
96
97 static const unsigned int max77693_safeout_table[] = {
98 4850000,
99 4900000,
100 4950000,
101 3300000,
102 };
103
104 static struct regulator_ops max77693_safeout_ops = {
105 .list_voltage = regulator_list_voltage_table,
106 .is_enabled = regulator_is_enabled_regmap,
107 .enable = regulator_enable_regmap,
108 .disable = regulator_disable_regmap,
109 .get_voltage_sel = regulator_get_voltage_sel_regmap,
110 .set_voltage_sel = regulator_set_voltage_sel_regmap,
111 };
112
113 static struct regulator_ops max77693_charger_ops = {
114 .is_enabled = regulator_is_enabled_regmap,
115 .enable = regulator_enable_regmap,
116 .disable = regulator_disable_regmap,
117 .get_current_limit = max77693_chg_get_current_limit,
118 .set_current_limit = max77693_chg_set_current_limit,
119 };
120
121 #define regulator_desc_esafeout(_num) { \
122 .name = "ESAFEOUT"#_num, \
123 .id = MAX77693_ESAFEOUT##_num, \
124 .of_match = of_match_ptr("ESAFEOUT"#_num), \
125 .regulators_node = of_match_ptr("regulators"), \
126 .n_voltages = 4, \
127 .ops = &max77693_safeout_ops, \
128 .type = REGULATOR_VOLTAGE, \
129 .owner = THIS_MODULE, \
130 .volt_table = max77693_safeout_table, \
131 .vsel_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
132 .vsel_mask = SAFEOUT_CTRL_SAFEOUT##_num##_MASK, \
133 .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
134 .enable_mask = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \
135 }
136
137 static const struct regulator_desc regulators[] = {
138 regulator_desc_esafeout(1),
139 regulator_desc_esafeout(2),
140 {
141 .name = "CHARGER",
142 .id = MAX77693_CHARGER,
143 .of_match = of_match_ptr("CHARGER"),
144 .regulators_node = of_match_ptr("regulators"),
145 .ops = &max77693_charger_ops,
146 .type = REGULATOR_CURRENT,
147 .owner = THIS_MODULE,
148 .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
149 .enable_mask = CHG_CNFG_00_CHG_MASK |
150 CHG_CNFG_00_BUCK_MASK,
151 .enable_val = CHG_CNFG_00_CHG_MASK | CHG_CNFG_00_BUCK_MASK,
152 },
153 };
154
155 static const struct chg_reg_data max77693_chg_reg_data = {
156 .linear_reg = MAX77693_CHG_REG_CHG_CNFG_09,
157 .linear_mask = CHG_CNFG_09_CHGIN_ILIM_MASK,
158 .uA_step = 20000,
159 .min_sel = 3,
160 };
161
162 static int max77693_pmic_probe(struct platform_device *pdev)
163 {
164 struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
165 int i;
166 struct regulator_config config = { };
167
168 config.dev = iodev->dev;
169 config.regmap = iodev->regmap;
170 config.driver_data = (void *)&max77693_chg_reg_data;
171
172 for (i = 0; i < ARRAY_SIZE(regulators); i++) {
173 struct regulator_dev *rdev;
174
175 rdev = devm_regulator_register(&pdev->dev,
176 &regulators[i], &config);
177 if (IS_ERR(rdev)) {
178 dev_err(&pdev->dev,
179 "Failed to initialize regulator-%d\n", i);
180 return PTR_ERR(rdev);
181 }
182 }
183
184 return 0;
185 }
186
187 static const struct platform_device_id max77693_pmic_id[] = {
188 { "max77693-pmic", TYPE_MAX77693 },
189 {},
190 };
191
192 MODULE_DEVICE_TABLE(platform, max77693_pmic_id);
193
194 static struct platform_driver max77693_pmic_driver = {
195 .driver = {
196 .name = "max77693-pmic",
197 },
198 .probe = max77693_pmic_probe,
199 .id_table = max77693_pmic_id,
200 };
201
202 module_platform_driver(max77693_pmic_driver);
203
204 MODULE_DESCRIPTION("MAXIM MAX77693 regulator driver");
205 MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
206 MODULE_LICENSE("GPL");
This page took 0.035431 seconds and 5 git commands to generate.