blob: dac1fb584fa35decbab9334f47a673cc5ecbb084 [file] [log] [blame]
Axel Lin6a1beee2019-04-19 08:58:24 +08001// SPDX-License-Identifier: GPL-2.0
2//
3// FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver.
4//
5// Supported Part Numbers:
6// FAN53555UC00X/01X/03X/04X/05X
7//
8// Copyright (c) 2012 Marvell Technology Ltd.
9// Yunfan Zhang <yfzhang@marvell.com>
10
Yunfan Zhang49d8c592012-09-08 03:52:18 -070011#include <linux/module.h>
12#include <linux/param.h>
13#include <linux/err.h>
14#include <linux/platform_device.h>
15#include <linux/regulator/driver.h>
16#include <linux/regulator/machine.h>
Heiko Stuebner91f23d82014-09-16 21:22:53 +020017#include <linux/regulator/of_regulator.h>
18#include <linux/of_device.h>
Yunfan Zhang49d8c592012-09-08 03:52:18 -070019#include <linux/i2c.h>
20#include <linux/slab.h>
21#include <linux/regmap.h>
22#include <linux/regulator/fan53555.h>
23
24/* Voltage setting */
25#define FAN53555_VSEL0 0x00
26#define FAN53555_VSEL1 0x01
Joseph Chen914df8f2021-04-21 18:03:38 -030027
28#define TCS4525_VSEL0 0x11
29#define TCS4525_VSEL1 0x10
30#define TCS4525_TIME 0x13
31#define TCS4525_COMMAND 0x14
32
Yunfan Zhang49d8c592012-09-08 03:52:18 -070033/* Control register */
34#define FAN53555_CONTROL 0x02
35/* IC Type */
36#define FAN53555_ID1 0x03
37/* IC mask version */
38#define FAN53555_ID2 0x04
39/* Monitor register */
40#define FAN53555_MONITOR 0x05
41
42/* VSEL bit definitions */
43#define VSEL_BUCK_EN (1 << 7)
44#define VSEL_MODE (1 << 6)
Yunfan Zhang49d8c592012-09-08 03:52:18 -070045/* Chip ID and Verison */
46#define DIE_ID 0x0F /* ID1 */
47#define DIE_REV 0x0F /* ID2 */
48/* Control bit definitions */
49#define CTL_OUTPUT_DISCHG (1 << 7)
50#define CTL_SLEW_MASK (0x7 << 4)
51#define CTL_SLEW_SHIFT 4
52#define CTL_RESET (1 << 2)
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -080053#define CTL_MODE_VSEL0_MODE BIT(0)
54#define CTL_MODE_VSEL1_MODE BIT(1)
Yunfan Zhang49d8c592012-09-08 03:52:18 -070055
56#define FAN53555_NVOLTAGES 64 /* Numbers of voltages */
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -080057#define FAN53526_NVOLTAGES 128
Joseph Chen914df8f2021-04-21 18:03:38 -030058
Joseph Chen914df8f2021-04-21 18:03:38 -030059#define TCS_VSEL0_MODE (1 << 7)
60#define TCS_VSEL1_MODE (1 << 6)
61
62#define TCS_SLEW_SHIFT 3
63#define TCS_SLEW_MASK (0x3 < 3)
Yunfan Zhang49d8c592012-09-08 03:52:18 -070064
Heiko Stuebneree309282014-09-16 21:22:54 +020065enum fan53555_vendor {
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -080066 FAN53526_VENDOR_FAIRCHILD = 0,
67 FAN53555_VENDOR_FAIRCHILD,
Heiko Stuebneree309282014-09-16 21:22:54 +020068 FAN53555_VENDOR_SILERGY,
Peter Geisb3cc8ec2021-05-11 17:13:35 -040069 FAN53526_VENDOR_TCS,
Heiko Stuebneree309282014-09-16 21:22:54 +020070};
71
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -080072enum {
73 FAN53526_CHIP_ID_01 = 1,
74};
75
76enum {
77 FAN53526_CHIP_REV_08 = 8,
78};
79
Yunfan Zhang49d8c592012-09-08 03:52:18 -070080/* IC Type */
81enum {
82 FAN53555_CHIP_ID_00 = 0,
83 FAN53555_CHIP_ID_01,
84 FAN53555_CHIP_ID_02,
85 FAN53555_CHIP_ID_03,
86 FAN53555_CHIP_ID_04,
87 FAN53555_CHIP_ID_05,
Wadim Egorov5e39cf42016-04-20 10:01:07 +020088 FAN53555_CHIP_ID_08 = 8,
Yunfan Zhang49d8c592012-09-08 03:52:18 -070089};
90
Peter Geisf9028dc2021-05-11 17:13:34 -040091enum {
92 TCS4525_CHIP_ID_12 = 12,
93};
94
Rudi Heitbaum5eee5ec2021-06-02 11:29:47 +000095enum {
96 TCS4526_CHIP_ID_00 = 0,
97};
98
Wadim Egorove57cbb72016-04-20 10:01:08 +020099/* IC mask revision */
100enum {
101 FAN53555_CHIP_REV_00 = 0x3,
102 FAN53555_CHIP_REV_13 = 0xf,
103};
104
Heiko Stuebneree309282014-09-16 21:22:54 +0200105enum {
106 SILERGY_SYR82X = 8,
Vasily Khoruzhick5365e3d2019-11-06 08:12:11 -0800107 SILERGY_SYR83X = 9,
Heiko Stuebneree309282014-09-16 21:22:54 +0200108};
109
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700110struct fan53555_device_info {
Heiko Stuebneree309282014-09-16 21:22:54 +0200111 enum fan53555_vendor vendor;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700112 struct device *dev;
113 struct regulator_desc desc;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700114 struct regulator_init_data *regulator;
115 /* IC Type and Rev */
116 int chip_id;
117 int chip_rev;
118 /* Voltage setting register */
119 unsigned int vol_reg;
120 unsigned int sleep_reg;
121 /* Voltage range and step(linear) */
122 unsigned int vsel_min;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700123 unsigned int vsel_step;
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800124 unsigned int vsel_count;
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800125 /* Mode */
126 unsigned int mode_reg;
127 unsigned int mode_mask;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700128 /* Sleep voltage cache */
129 unsigned int sleep_vol_cache;
Joseph Chen914df8f2021-04-21 18:03:38 -0300130 /* Slew rate */
131 unsigned int slew_reg;
132 unsigned int slew_mask;
Axel Linb61ac762021-05-25 20:40:17 +0800133 const unsigned int *ramp_delay_table;
134 unsigned int n_ramp_values;
Joseph Chen914df8f2021-04-21 18:03:38 -0300135 unsigned int slew_rate;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700136};
137
138static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV)
139{
140 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
141 int ret;
142
143 if (di->sleep_vol_cache == uV)
144 return 0;
145 ret = regulator_map_voltage_linear(rdev, uV, uV);
146 if (ret < 0)
Sachin Kamat145fe1e2014-02-18 16:11:04 +0530147 return ret;
Axel Lina69929c2019-04-19 08:58:23 +0800148 ret = regmap_update_bits(rdev->regmap, di->sleep_reg,
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800149 di->desc.vsel_mask, ret);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700150 if (ret < 0)
Sachin Kamat145fe1e2014-02-18 16:11:04 +0530151 return ret;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700152 /* Cache the sleep voltage setting.
153 * Might not be the real voltage which is rounded */
154 di->sleep_vol_cache = uV;
155
156 return 0;
157}
158
zhangqingab7cad32016-01-12 03:05:56 -0800159static int fan53555_set_suspend_enable(struct regulator_dev *rdev)
160{
161 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
162
Axel Lina69929c2019-04-19 08:58:23 +0800163 return regmap_update_bits(rdev->regmap, di->sleep_reg,
zhangqingab7cad32016-01-12 03:05:56 -0800164 VSEL_BUCK_EN, VSEL_BUCK_EN);
165}
166
167static int fan53555_set_suspend_disable(struct regulator_dev *rdev)
168{
169 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
170
Axel Lina69929c2019-04-19 08:58:23 +0800171 return regmap_update_bits(rdev->regmap, di->sleep_reg,
zhangqingab7cad32016-01-12 03:05:56 -0800172 VSEL_BUCK_EN, 0);
173}
174
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700175static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode)
176{
177 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
178
179 switch (mode) {
180 case REGULATOR_MODE_FAST:
Axel Lina69929c2019-04-19 08:58:23 +0800181 regmap_update_bits(rdev->regmap, di->mode_reg,
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800182 di->mode_mask, di->mode_mask);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700183 break;
184 case REGULATOR_MODE_NORMAL:
Axel Lina69929c2019-04-19 08:58:23 +0800185 regmap_update_bits(rdev->regmap, di->vol_reg, di->mode_mask, 0);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700186 break;
187 default:
188 return -EINVAL;
189 }
190 return 0;
191}
192
193static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
194{
195 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
196 unsigned int val;
197 int ret = 0;
198
Axel Lina69929c2019-04-19 08:58:23 +0800199 ret = regmap_read(rdev->regmap, di->mode_reg, &val);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700200 if (ret < 0)
201 return ret;
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800202 if (val & di->mode_mask)
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700203 return REGULATOR_MODE_FAST;
204 else
205 return REGULATOR_MODE_NORMAL;
206}
207
Axel Linb61ac762021-05-25 20:40:17 +0800208static const unsigned int slew_rates[] = {
Heiko Stuebnerdd7e71f2014-09-16 17:54:01 +0200209 64000,
210 32000,
211 16000,
212 8000,
213 4000,
214 2000,
215 1000,
216 500,
217};
218
Axel Linb61ac762021-05-25 20:40:17 +0800219static const unsigned int tcs_slew_rates[] = {
Joseph Chen914df8f2021-04-21 18:03:38 -0300220 18700,
221 9300,
222 4600,
223 2300,
224};
225
Bhumika Goyal71880ab2017-01-28 19:32:05 +0530226static const struct regulator_ops fan53555_regulator_ops = {
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700227 .set_voltage_sel = regulator_set_voltage_sel_regmap,
228 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Heiko Stuebnerfda87a42015-05-26 18:19:45 +0200229 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700230 .map_voltage = regulator_map_voltage_linear,
231 .list_voltage = regulator_list_voltage_linear,
232 .set_suspend_voltage = fan53555_set_suspend_voltage,
233 .enable = regulator_enable_regmap,
234 .disable = regulator_disable_regmap,
235 .is_enabled = regulator_is_enabled_regmap,
236 .set_mode = fan53555_set_mode,
237 .get_mode = fan53555_get_mode,
Axel Linb61ac762021-05-25 20:40:17 +0800238 .set_ramp_delay = regulator_set_ramp_delay_regmap,
zhangqingab7cad32016-01-12 03:05:56 -0800239 .set_suspend_enable = fan53555_set_suspend_enable,
240 .set_suspend_disable = fan53555_set_suspend_disable,
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700241};
242
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800243static int fan53526_voltages_setup_fairchild(struct fan53555_device_info *di)
244{
245 /* Init voltage range and step */
246 switch (di->chip_id) {
247 case FAN53526_CHIP_ID_01:
248 switch (di->chip_rev) {
249 case FAN53526_CHIP_REV_08:
250 di->vsel_min = 600000;
251 di->vsel_step = 6250;
252 break;
253 default:
254 dev_err(di->dev,
255 "Chip ID %d with rev %d not supported!\n",
256 di->chip_id, di->chip_rev);
257 return -EINVAL;
258 }
259 break;
260 default:
261 dev_err(di->dev,
262 "Chip ID %d not supported!\n", di->chip_id);
263 return -EINVAL;
264 }
265
Axel Lin30b38b82021-05-25 20:40:16 +0800266 di->slew_reg = FAN53555_CONTROL;
267 di->slew_mask = CTL_SLEW_MASK;
Axel Linb61ac762021-05-25 20:40:17 +0800268 di->ramp_delay_table = slew_rates;
269 di->n_ramp_values = ARRAY_SIZE(slew_rates);
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800270 di->vsel_count = FAN53526_NVOLTAGES;
271
272 return 0;
273}
274
Heiko Stuebneree309282014-09-16 21:22:54 +0200275static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700276{
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700277 /* Init voltage range and step */
278 switch (di->chip_id) {
279 case FAN53555_CHIP_ID_00:
Wadim Egorove57cbb72016-04-20 10:01:08 +0200280 switch (di->chip_rev) {
281 case FAN53555_CHIP_REV_00:
282 di->vsel_min = 600000;
283 di->vsel_step = 10000;
284 break;
285 case FAN53555_CHIP_REV_13:
286 di->vsel_min = 800000;
287 di->vsel_step = 10000;
288 break;
289 default:
290 dev_err(di->dev,
291 "Chip ID %d with rev %d not supported!\n",
292 di->chip_id, di->chip_rev);
293 return -EINVAL;
294 }
295 break;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700296 case FAN53555_CHIP_ID_01:
297 case FAN53555_CHIP_ID_03:
298 case FAN53555_CHIP_ID_05:
Wadim Egorov5e39cf42016-04-20 10:01:07 +0200299 case FAN53555_CHIP_ID_08:
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700300 di->vsel_min = 600000;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700301 di->vsel_step = 10000;
302 break;
303 case FAN53555_CHIP_ID_04:
304 di->vsel_min = 603000;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700305 di->vsel_step = 12826;
306 break;
307 default:
308 dev_err(di->dev,
Heiko Stuebneree309282014-09-16 21:22:54 +0200309 "Chip ID %d not supported!\n", di->chip_id);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700310 return -EINVAL;
311 }
Joseph Chen914df8f2021-04-21 18:03:38 -0300312 di->slew_reg = FAN53555_CONTROL;
313 di->slew_mask = CTL_SLEW_MASK;
Axel Linb61ac762021-05-25 20:40:17 +0800314 di->ramp_delay_table = slew_rates;
315 di->n_ramp_values = ARRAY_SIZE(slew_rates);
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800316 di->vsel_count = FAN53555_NVOLTAGES;
317
Heiko Stuebnerdd7e71f2014-09-16 17:54:01 +0200318 return 0;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700319}
320
Heiko Stuebneree309282014-09-16 21:22:54 +0200321static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
322{
323 /* Init voltage range and step */
324 switch (di->chip_id) {
325 case SILERGY_SYR82X:
Vasily Khoruzhick5365e3d2019-11-06 08:12:11 -0800326 case SILERGY_SYR83X:
Heiko Stuebneree309282014-09-16 21:22:54 +0200327 di->vsel_min = 712500;
328 di->vsel_step = 12500;
329 break;
330 default:
331 dev_err(di->dev,
332 "Chip ID %d not supported!\n", di->chip_id);
333 return -EINVAL;
334 }
Joseph Chen914df8f2021-04-21 18:03:38 -0300335 di->slew_reg = FAN53555_CONTROL;
Joseph Chen914df8f2021-04-21 18:03:38 -0300336 di->slew_mask = CTL_SLEW_MASK;
Axel Linb61ac762021-05-25 20:40:17 +0800337 di->ramp_delay_table = slew_rates;
338 di->n_ramp_values = ARRAY_SIZE(slew_rates);
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800339 di->vsel_count = FAN53555_NVOLTAGES;
340
Heiko Stuebneree309282014-09-16 21:22:54 +0200341 return 0;
342}
343
Peter Geisb3cc8ec2021-05-11 17:13:35 -0400344static int fan53526_voltages_setup_tcs(struct fan53555_device_info *di)
Joseph Chen914df8f2021-04-21 18:03:38 -0300345{
Peter Geisf9028dc2021-05-11 17:13:34 -0400346 switch (di->chip_id) {
347 case TCS4525_CHIP_ID_12:
Rudi Heitbaum5eee5ec2021-06-02 11:29:47 +0000348 case TCS4526_CHIP_ID_00:
Peter Geisf9028dc2021-05-11 17:13:34 -0400349 di->slew_reg = TCS4525_TIME;
350 di->slew_mask = TCS_SLEW_MASK;
Axel Linb61ac762021-05-25 20:40:17 +0800351 di->ramp_delay_table = tcs_slew_rates;
352 di->n_ramp_values = ARRAY_SIZE(tcs_slew_rates);
Joseph Chen914df8f2021-04-21 18:03:38 -0300353
Peter Geisf9028dc2021-05-11 17:13:34 -0400354 /* Init voltage range and step */
355 di->vsel_min = 600000;
356 di->vsel_step = 6250;
357 di->vsel_count = FAN53526_NVOLTAGES;
358 break;
359 default:
360 dev_err(di->dev, "Chip ID %d not supported!\n", di->chip_id);
361 return -EINVAL;
362 }
Joseph Chen914df8f2021-04-21 18:03:38 -0300363
364 return 0;
365}
366
Heiko Stuebneree309282014-09-16 21:22:54 +0200367/* For 00,01,03,05 options:
368 * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
369 * For 04 option:
370 * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
371 * */
372static int fan53555_device_setup(struct fan53555_device_info *di,
373 struct fan53555_platform_data *pdata)
374{
375 int ret = 0;
376
377 /* Setup voltage control register */
Joseph Chen914df8f2021-04-21 18:03:38 -0300378 switch (di->vendor) {
379 case FAN53526_VENDOR_FAIRCHILD:
380 case FAN53555_VENDOR_FAIRCHILD:
381 case FAN53555_VENDOR_SILERGY:
382 switch (pdata->sleep_vsel_id) {
383 case FAN53555_VSEL_ID_0:
384 di->sleep_reg = FAN53555_VSEL0;
385 di->vol_reg = FAN53555_VSEL1;
386 break;
387 case FAN53555_VSEL_ID_1:
388 di->sleep_reg = FAN53555_VSEL1;
389 di->vol_reg = FAN53555_VSEL0;
390 break;
391 default:
392 dev_err(di->dev, "Invalid VSEL ID!\n");
393 return -EINVAL;
394 }
Heiko Stuebneree309282014-09-16 21:22:54 +0200395 break;
Peter Geisb3cc8ec2021-05-11 17:13:35 -0400396 case FAN53526_VENDOR_TCS:
Joseph Chen914df8f2021-04-21 18:03:38 -0300397 switch (pdata->sleep_vsel_id) {
398 case FAN53555_VSEL_ID_0:
399 di->sleep_reg = TCS4525_VSEL0;
400 di->vol_reg = TCS4525_VSEL1;
401 break;
402 case FAN53555_VSEL_ID_1:
403 di->sleep_reg = TCS4525_VSEL1;
404 di->vol_reg = TCS4525_VSEL0;
405 break;
406 default:
407 dev_err(di->dev, "Invalid VSEL ID!\n");
408 return -EINVAL;
409 }
Heiko Stuebneree309282014-09-16 21:22:54 +0200410 break;
411 default:
Joseph Chen914df8f2021-04-21 18:03:38 -0300412 dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
Heiko Stuebneree309282014-09-16 21:22:54 +0200413 return -EINVAL;
414 }
415
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800416 /* Setup mode control register */
Heiko Stuebneree309282014-09-16 21:22:54 +0200417 switch (di->vendor) {
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800418 case FAN53526_VENDOR_FAIRCHILD:
419 di->mode_reg = FAN53555_CONTROL;
420
421 switch (pdata->sleep_vsel_id) {
422 case FAN53555_VSEL_ID_0:
423 di->mode_mask = CTL_MODE_VSEL1_MODE;
424 break;
425 case FAN53555_VSEL_ID_1:
426 di->mode_mask = CTL_MODE_VSEL0_MODE;
427 break;
428 }
429 break;
430 case FAN53555_VENDOR_FAIRCHILD:
431 case FAN53555_VENDOR_SILERGY:
432 di->mode_reg = di->vol_reg;
433 di->mode_mask = VSEL_MODE;
434 break;
Peter Geisb3cc8ec2021-05-11 17:13:35 -0400435 case FAN53526_VENDOR_TCS:
Joseph Chen914df8f2021-04-21 18:03:38 -0300436 di->mode_reg = TCS4525_COMMAND;
437
438 switch (pdata->sleep_vsel_id) {
439 case FAN53555_VSEL_ID_0:
440 di->mode_mask = TCS_VSEL1_MODE;
441 break;
442 case FAN53555_VSEL_ID_1:
443 di->mode_mask = TCS_VSEL0_MODE;
444 break;
445 }
446 break;
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800447 default:
448 dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
449 return -EINVAL;
450 }
451
452 /* Setup voltage range */
453 switch (di->vendor) {
454 case FAN53526_VENDOR_FAIRCHILD:
455 ret = fan53526_voltages_setup_fairchild(di);
456 break;
Heiko Stuebneree309282014-09-16 21:22:54 +0200457 case FAN53555_VENDOR_FAIRCHILD:
458 ret = fan53555_voltages_setup_fairchild(di);
459 break;
460 case FAN53555_VENDOR_SILERGY:
461 ret = fan53555_voltages_setup_silergy(di);
462 break;
Peter Geisb3cc8ec2021-05-11 17:13:35 -0400463 case FAN53526_VENDOR_TCS:
464 ret = fan53526_voltages_setup_tcs(di);
Joseph Chen914df8f2021-04-21 18:03:38 -0300465 break;
Heiko Stuebneree309282014-09-16 21:22:54 +0200466 default:
Axel Linfe230532014-09-18 21:48:48 +0800467 dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
Heiko Stuebneree309282014-09-16 21:22:54 +0200468 return -EINVAL;
469 }
470
471 return ret;
472}
473
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700474static int fan53555_regulator_register(struct fan53555_device_info *di,
475 struct regulator_config *config)
476{
477 struct regulator_desc *rdesc = &di->desc;
Axel Lina69929c2019-04-19 08:58:23 +0800478 struct regulator_dev *rdev;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700479
480 rdesc->name = "fan53555-reg";
Heiko Stuebner3415d602014-09-14 21:23:01 +0200481 rdesc->supply_name = "vin";
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700482 rdesc->ops = &fan53555_regulator_ops;
483 rdesc->type = REGULATOR_VOLTAGE;
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800484 rdesc->n_voltages = di->vsel_count;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700485 rdesc->enable_reg = di->vol_reg;
486 rdesc->enable_mask = VSEL_BUCK_EN;
487 rdesc->min_uV = di->vsel_min;
488 rdesc->uV_step = di->vsel_step;
489 rdesc->vsel_reg = di->vol_reg;
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800490 rdesc->vsel_mask = di->vsel_count - 1;
Axel Linb61ac762021-05-25 20:40:17 +0800491 rdesc->ramp_reg = di->slew_reg;
492 rdesc->ramp_mask = di->slew_mask;
493 rdesc->ramp_delay_table = di->ramp_delay_table;
494 rdesc->n_ramp_values = di->n_ramp_values;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700495 rdesc->owner = THIS_MODULE;
496
Axel Lina69929c2019-04-19 08:58:23 +0800497 rdev = devm_regulator_register(di->dev, &di->desc, config);
498 return PTR_ERR_OR_ZERO(rdev);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700499}
500
Krzysztof Kozlowski121b5672015-01-05 10:04:19 +0100501static const struct regmap_config fan53555_regmap_config = {
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700502 .reg_bits = 8,
503 .val_bits = 8,
504};
505
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200506static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100507 struct device_node *np,
508 const struct regulator_desc *desc)
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200509{
510 struct fan53555_platform_data *pdata;
511 int ret;
512 u32 tmp;
513
514 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
515 if (!pdata)
516 return NULL;
517
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100518 pdata->regulator = of_get_regulator_init_data(dev, np, desc);
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200519
520 ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
521 &tmp);
522 if (!ret)
523 pdata->sleep_vsel_id = tmp;
524
525 return pdata;
526}
527
Jisheng Zhang5e97d7e2020-08-21 11:13:24 +0800528static const struct of_device_id __maybe_unused fan53555_dt_ids[] = {
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200529 {
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800530 .compatible = "fcs,fan53526",
531 .data = (void *)FAN53526_VENDOR_FAIRCHILD,
532 }, {
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200533 .compatible = "fcs,fan53555",
Heiko Stuebneree309282014-09-16 21:22:54 +0200534 .data = (void *)FAN53555_VENDOR_FAIRCHILD
535 }, {
536 .compatible = "silergy,syr827",
537 .data = (void *)FAN53555_VENDOR_SILERGY,
538 }, {
539 .compatible = "silergy,syr828",
540 .data = (void *)FAN53555_VENDOR_SILERGY,
Joseph Chen914df8f2021-04-21 18:03:38 -0300541 }, {
542 .compatible = "tcs,tcs4525",
Peter Geisb3cc8ec2021-05-11 17:13:35 -0400543 .data = (void *)FAN53526_VENDOR_TCS
Rudi Heitbaum5eee5ec2021-06-02 11:29:47 +0000544 }, {
545 .compatible = "tcs,tcs4526",
546 .data = (void *)FAN53526_VENDOR_TCS
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200547 },
548 { }
549};
550MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
551
Bill Pembertona5023572012-11-19 13:22:22 -0500552static int fan53555_regulator_probe(struct i2c_client *client,
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700553 const struct i2c_device_id *id)
554{
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200555 struct device_node *np = client->dev.of_node;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700556 struct fan53555_device_info *di;
557 struct fan53555_platform_data *pdata;
558 struct regulator_config config = { };
Axel Lina69929c2019-04-19 08:58:23 +0800559 struct regmap *regmap;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700560 unsigned int val;
561 int ret;
562
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100563 di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info),
564 GFP_KERNEL);
565 if (!di)
566 return -ENOMEM;
567
Jingoo Handff91d02013-07-30 17:20:47 +0900568 pdata = dev_get_platdata(&client->dev);
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200569 if (!pdata)
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100570 pdata = fan53555_parse_dt(&client->dev, np, &di->desc);
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200571
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700572 if (!pdata || !pdata->regulator) {
573 dev_err(&client->dev, "Platform data not found!\n");
574 return -ENODEV;
575 }
576
Axel Line13426b2014-09-18 21:49:38 +0800577 di->regulator = pdata->regulator;
Heiko Stuebneree309282014-09-16 21:22:54 +0200578 if (client->dev.of_node) {
Jisheng Zhangd110e3e2017-07-20 16:42:17 +0800579 di->vendor =
580 (unsigned long)of_device_get_match_data(&client->dev);
Heiko Stuebneree309282014-09-16 21:22:54 +0200581 } else {
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200582 /* if no ramp constraint set, get the pdata ramp_delay */
583 if (!di->regulator->constraints.ramp_delay) {
Axel Lin87919e02019-02-24 11:21:46 +0800584 if (pdata->slew_rate >= ARRAY_SIZE(slew_rates)) {
585 dev_err(&client->dev, "Invalid slew_rate\n");
586 return -EINVAL;
587 }
Heiko Stuebnerdd7e71f2014-09-16 17:54:01 +0200588
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200589 di->regulator->constraints.ramp_delay
Axel Lin87919e02019-02-24 11:21:46 +0800590 = slew_rates[pdata->slew_rate];
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200591 }
Heiko Stuebneree309282014-09-16 21:22:54 +0200592
593 di->vendor = id->driver_data;
Heiko Stuebnerdd7e71f2014-09-16 17:54:01 +0200594 }
595
Axel Lina69929c2019-04-19 08:58:23 +0800596 regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
597 if (IS_ERR(regmap)) {
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700598 dev_err(&client->dev, "Failed to allocate regmap!\n");
Axel Lina69929c2019-04-19 08:58:23 +0800599 return PTR_ERR(regmap);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700600 }
601 di->dev = &client->dev;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700602 i2c_set_clientdata(client, di);
603 /* Get chip ID */
Axel Lina69929c2019-04-19 08:58:23 +0800604 ret = regmap_read(regmap, FAN53555_ID1, &val);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700605 if (ret < 0) {
606 dev_err(&client->dev, "Failed to get chip ID!\n");
Sachin Kamat145fe1e2014-02-18 16:11:04 +0530607 return ret;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700608 }
609 di->chip_id = val & DIE_ID;
610 /* Get chip revision */
Axel Lina69929c2019-04-19 08:58:23 +0800611 ret = regmap_read(regmap, FAN53555_ID2, &val);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700612 if (ret < 0) {
613 dev_err(&client->dev, "Failed to get chip Rev!\n");
Sachin Kamat145fe1e2014-02-18 16:11:04 +0530614 return ret;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700615 }
616 di->chip_rev = val & DIE_REV;
617 dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n",
618 di->chip_id, di->chip_rev);
619 /* Device init */
620 ret = fan53555_device_setup(di, pdata);
621 if (ret < 0) {
622 dev_err(&client->dev, "Failed to setup device!\n");
623 return ret;
624 }
625 /* Register regulator */
626 config.dev = di->dev;
627 config.init_data = di->regulator;
Axel Lina69929c2019-04-19 08:58:23 +0800628 config.regmap = regmap;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700629 config.driver_data = di;
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200630 config.of_node = np;
631
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700632 ret = fan53555_regulator_register(di, &config);
633 if (ret < 0)
634 dev_err(&client->dev, "Failed to register regulator!\n");
635 return ret;
636
637}
638
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700639static const struct i2c_device_id fan53555_id[] = {
Heiko Stuebneree309282014-09-16 21:22:54 +0200640 {
Bjorn Anderssonf2a9eb92019-02-20 16:43:03 -0800641 .name = "fan53526",
642 .driver_data = FAN53526_VENDOR_FAIRCHILD
643 }, {
Heiko Stuebneree309282014-09-16 21:22:54 +0200644 .name = "fan53555",
645 .driver_data = FAN53555_VENDOR_FAIRCHILD
646 }, {
Guillaume Tuckerfc1111b2017-08-21 13:47:43 +0100647 .name = "syr827",
648 .driver_data = FAN53555_VENDOR_SILERGY
649 }, {
650 .name = "syr828",
Heiko Stuebneree309282014-09-16 21:22:54 +0200651 .driver_data = FAN53555_VENDOR_SILERGY
Joseph Chen914df8f2021-04-21 18:03:38 -0300652 }, {
653 .name = "tcs4525",
Peter Geisb3cc8ec2021-05-11 17:13:35 -0400654 .driver_data = FAN53526_VENDOR_TCS
Rudi Heitbaum5eee5ec2021-06-02 11:29:47 +0000655 }, {
656 .name = "tcs4526",
657 .driver_data = FAN53526_VENDOR_TCS
Heiko Stuebneree309282014-09-16 21:22:54 +0200658 },
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700659 { },
660};
Javier Martinez Canillase80c47b2015-07-30 18:18:40 +0200661MODULE_DEVICE_TABLE(i2c, fan53555_id);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700662
663static struct i2c_driver fan53555_regulator_driver = {
664 .driver = {
665 .name = "fan53555-regulator",
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200666 .of_match_table = of_match_ptr(fan53555_dt_ids),
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700667 },
668 .probe = fan53555_regulator_probe,
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700669 .id_table = fan53555_id,
670};
671
672module_i2c_driver(fan53555_regulator_driver);
673
674MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
675MODULE_DESCRIPTION("FAN53555 regulator driver");
676MODULE_LICENSE("GPL v2");