Commit | Line | Data |
---|---|---|
6ffc3270 LD |
1 | /* |
2 | * Regulator driver for RICOH RC5T583 power management chip. | |
3 | * | |
4 | * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved. | |
5 | * Author: Laxman dewangan <ldewangan@nvidia.com> | |
6 | * | |
7 | * based on code | |
8 | * Copyright (C) 2011 RICOH COMPANY,LTD | |
9 | * | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify it | |
12 | * under the terms and conditions of the GNU General Public License, | |
13 | * version 2, as published by the Free Software Foundation. | |
14 | * | |
15 | * This program is distributed in the hope it will be useful, but WITHOUT | |
16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
18 | * more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 | * | |
23 | */ | |
24 | ||
25 | #include <linux/module.h> | |
6ffc3270 LD |
26 | #include <linux/init.h> |
27 | #include <linux/slab.h> | |
28 | #include <linux/err.h> | |
29 | #include <linux/platform_device.h> | |
30 | #include <linux/regulator/driver.h> | |
31 | #include <linux/regulator/machine.h> | |
32 | #include <linux/gpio.h> | |
33 | #include <linux/mfd/rc5t583.h> | |
34 | ||
35 | struct rc5t583_regulator_info { | |
36 | int deepsleep_id; | |
37 | ||
38 | /* Regulator register address.*/ | |
6ffc3270 LD |
39 | uint8_t reg_disc_reg; |
40 | uint8_t disc_bit; | |
6ffc3270 LD |
41 | uint8_t deepsleep_reg; |
42 | ||
6ffc3270 LD |
43 | /* Regulator specific turn-on delay and voltage settling time*/ |
44 | int enable_uv_per_us; | |
6ffc3270 LD |
45 | |
46 | /* Used by regulator core */ | |
47 | struct regulator_desc desc; | |
48 | }; | |
49 | ||
50 | struct rc5t583_regulator { | |
51 | struct rc5t583_regulator_info *reg_info; | |
6ffc3270 LD |
52 | struct regulator_dev *rdev; |
53 | }; | |
54 | ||
6ffc3270 LD |
55 | static int rc5t583_regulator_enable_time(struct regulator_dev *rdev) |
56 | { | |
57 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); | |
15b397d7 | 58 | int vsel = regulator_get_voltage_sel_regmap(rdev); |
3209be17 | 59 | int curr_uV = regulator_list_voltage_linear(rdev, vsel); |
15b397d7 | 60 | |
6ffc3270 LD |
61 | return DIV_ROUND_UP(curr_uV, reg->reg_info->enable_uv_per_us); |
62 | } | |
63 | ||
6ffc3270 | 64 | static struct regulator_ops rc5t583_ops = { |
5bb6936c AL |
65 | .is_enabled = regulator_is_enabled_regmap, |
66 | .enable = regulator_enable_regmap, | |
67 | .disable = regulator_disable_regmap, | |
6ffc3270 | 68 | .enable_time = rc5t583_regulator_enable_time, |
15b397d7 | 69 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
70e5f645 | 70 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
3209be17 | 71 | .list_voltage = regulator_list_voltage_linear, |
70e5f645 | 72 | .map_voltage = regulator_map_voltage_linear, |
2f6ae6ef | 73 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
6ffc3270 LD |
74 | }; |
75 | ||
95e301ba AL |
76 | #define RC5T583_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, \ |
77 | _vout_mask, _min_mv, _max_mv, _step_uV, _enable_mv) \ | |
6ffc3270 | 78 | { \ |
6ffc3270 LD |
79 | .reg_disc_reg = RC5T583_REG_##_disc_reg, \ |
80 | .disc_bit = _disc_bit, \ | |
95e301ba | 81 | .deepsleep_reg = RC5T583_REG_##_id##DAC_DS, \ |
6ffc3270 | 82 | .enable_uv_per_us = _enable_mv * 1000, \ |
6ffc3270 LD |
83 | .deepsleep_id = RC5T583_DS_##_id, \ |
84 | .desc = { \ | |
85 | .name = "rc5t583-regulator-"#_id, \ | |
86 | .id = RC5T583_REGULATOR_##_id, \ | |
e3a7384c | 87 | .n_voltages = (_max_mv - _min_mv) * 1000 / _step_uV + 1, \ |
6ffc3270 LD |
88 | .ops = &rc5t583_ops, \ |
89 | .type = REGULATOR_VOLTAGE, \ | |
90 | .owner = THIS_MODULE, \ | |
15b397d7 AL |
91 | .vsel_reg = RC5T583_REG_##_id##DAC, \ |
92 | .vsel_mask = _vout_mask, \ | |
5bb6936c AL |
93 | .enable_reg = RC5T583_REG_##_en_reg, \ |
94 | .enable_mask = BIT(_en_bit), \ | |
3209be17 AL |
95 | .min_uV = _min_mv * 1000, \ |
96 | .uV_step = _step_uV, \ | |
2f6ae6ef | 97 | .ramp_delay = 40 * 1000, \ |
6ffc3270 LD |
98 | }, \ |
99 | } | |
100 | ||
101 | static struct rc5t583_regulator_info rc5t583_reg_info[RC5T583_REGULATOR_MAX] = { | |
95e301ba AL |
102 | RC5T583_REG(DC0, DC0CTL, 0, DC0CTL, 1, 0x7F, 700, 1500, 12500, 4), |
103 | RC5T583_REG(DC1, DC1CTL, 0, DC1CTL, 1, 0x7F, 700, 1500, 12500, 14), | |
104 | RC5T583_REG(DC2, DC2CTL, 0, DC2CTL, 1, 0x7F, 900, 2400, 12500, 14), | |
105 | RC5T583_REG(DC3, DC3CTL, 0, DC3CTL, 1, 0x7F, 900, 2400, 12500, 14), | |
106 | RC5T583_REG(LDO0, LDOEN2, 0, LDODIS2, 0, 0x7F, 900, 3400, 25000, 160), | |
107 | RC5T583_REG(LDO1, LDOEN2, 1, LDODIS2, 1, 0x7F, 900, 3400, 25000, 160), | |
108 | RC5T583_REG(LDO2, LDOEN2, 2, LDODIS2, 2, 0x7F, 900, 3400, 25000, 160), | |
109 | RC5T583_REG(LDO3, LDOEN2, 3, LDODIS2, 3, 0x7F, 900, 3400, 25000, 160), | |
110 | RC5T583_REG(LDO4, LDOEN2, 4, LDODIS2, 4, 0x3F, 750, 1500, 12500, 133), | |
111 | RC5T583_REG(LDO5, LDOEN2, 5, LDODIS2, 5, 0x7F, 900, 3400, 25000, 267), | |
112 | RC5T583_REG(LDO6, LDOEN2, 6, LDODIS2, 6, 0x7F, 900, 3400, 25000, 133), | |
113 | RC5T583_REG(LDO7, LDOEN2, 7, LDODIS2, 7, 0x7F, 900, 3400, 25000, 233), | |
114 | RC5T583_REG(LDO8, LDOEN1, 0, LDODIS1, 0, 0x7F, 900, 3400, 25000, 233), | |
115 | RC5T583_REG(LDO9, LDOEN1, 1, LDODIS1, 1, 0x7F, 900, 3400, 25000, 133), | |
6ffc3270 LD |
116 | }; |
117 | ||
a5023574 | 118 | static int rc5t583_regulator_probe(struct platform_device *pdev) |
6ffc3270 LD |
119 | { |
120 | struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent); | |
121 | struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev); | |
c172708d | 122 | struct regulator_config config = { }; |
6ffc3270 LD |
123 | struct rc5t583_regulator *reg = NULL; |
124 | struct rc5t583_regulator *regs; | |
125 | struct regulator_dev *rdev; | |
126 | struct rc5t583_regulator_info *ri; | |
127 | int ret; | |
128 | int id; | |
129 | ||
130 | if (!pdata) { | |
131 | dev_err(&pdev->dev, "No platform data, exiting...\n"); | |
132 | return -ENODEV; | |
133 | } | |
134 | ||
135 | regs = devm_kzalloc(&pdev->dev, RC5T583_REGULATOR_MAX * | |
136 | sizeof(struct rc5t583_regulator), GFP_KERNEL); | |
2ea2583f | 137 | if (!regs) |
6ffc3270 | 138 | return -ENOMEM; |
6ffc3270 LD |
139 | |
140 | ||
141 | for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) { | |
6ffc3270 LD |
142 | reg = ®s[id]; |
143 | ri = &rc5t583_reg_info[id]; | |
144 | reg->reg_info = ri; | |
6ffc3270 LD |
145 | |
146 | if (ri->deepsleep_id == RC5T583_DS_NONE) | |
147 | goto skip_ext_pwr_config; | |
148 | ||
149 | ret = rc5t583_ext_power_req_config(rc5t583->dev, | |
150 | ri->deepsleep_id, | |
151 | pdata->regulator_ext_pwr_control[id], | |
152 | pdata->regulator_deepsleep_slot[id]); | |
153 | /* | |
154 | * Configuring external control is not a major issue, | |
155 | * just give warning. | |
156 | */ | |
157 | if (ret < 0) | |
158 | dev_warn(&pdev->dev, | |
159 | "Failed to configure ext control %d\n", id); | |
160 | ||
161 | skip_ext_pwr_config: | |
c172708d | 162 | config.dev = &pdev->dev; |
3f24bf70 | 163 | config.init_data = pdata->reg_init_data[id]; |
c172708d | 164 | config.driver_data = reg; |
5bb6936c | 165 | config.regmap = rc5t583->regmap; |
c172708d | 166 | |
13775309 | 167 | rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config); |
a69df8a1 | 168 | if (IS_ERR(rdev)) { |
6ffc3270 LD |
169 | dev_err(&pdev->dev, "Failed to register regulator %s\n", |
170 | ri->desc.name); | |
13775309 | 171 | return PTR_ERR(rdev); |
6ffc3270 LD |
172 | } |
173 | reg->rdev = rdev; | |
174 | } | |
175 | platform_set_drvdata(pdev, regs); | |
176 | return 0; | |
6ffc3270 LD |
177 | } |
178 | ||
179 | static struct platform_driver rc5t583_regulator_driver = { | |
180 | .driver = { | |
181 | .name = "rc5t583-regulator", | |
182 | .owner = THIS_MODULE, | |
183 | }, | |
184 | .probe = rc5t583_regulator_probe, | |
6ffc3270 LD |
185 | }; |
186 | ||
187 | static int __init rc5t583_regulator_init(void) | |
188 | { | |
189 | return platform_driver_register(&rc5t583_regulator_driver); | |
190 | } | |
191 | subsys_initcall(rc5t583_regulator_init); | |
192 | ||
193 | static void __exit rc5t583_regulator_exit(void) | |
194 | { | |
195 | platform_driver_unregister(&rc5t583_regulator_driver); | |
196 | } | |
197 | module_exit(rc5t583_regulator_exit); | |
198 | ||
199 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); | |
200 | MODULE_DESCRIPTION("RC5T583 regulator driver"); | |
201 | MODULE_ALIAS("platform:rc5t583-regulator"); | |
4eb06453 | 202 | MODULE_LICENSE("GPL v2"); |