1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Regulator driver for RICOH RC5T583 power management chip.
4  *
5  * Copyright (c) 2011-2012, NVIDIA CORPORATION.  All rights reserved.
6  * Author: Laxman dewangan <ldewangan@nvidia.com>
7  *
8  * based on code
9  *      Copyright (C) 2011 RICOH COMPANY,LTD
10  */
11 
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/mfd/rc5t583.h>
20 
21 struct rc5t583_regulator_info {
22 	int			deepsleep_id;
23 
24 	/* Regulator register address.*/
25 	uint8_t			reg_disc_reg;
26 	uint8_t			disc_bit;
27 	uint8_t			deepsleep_reg;
28 
29 	/* Regulator specific turn-on delay  and voltage settling time*/
30 	int			enable_uv_per_us;
31 
32 	/* Used by regulator core */
33 	struct regulator_desc	desc;
34 };
35 
rc5t583_regulator_enable_time(struct regulator_dev * rdev)36 static int rc5t583_regulator_enable_time(struct regulator_dev *rdev)
37 {
38 	struct rc5t583_regulator_info *reg_info = rdev_get_drvdata(rdev);
39 	int vsel = regulator_get_voltage_sel_regmap(rdev);
40 	int curr_uV = regulator_list_voltage_linear(rdev, vsel);
41 
42 	return DIV_ROUND_UP(curr_uV, reg_info->enable_uv_per_us);
43 }
44 
45 static const struct regulator_ops rc5t583_ops = {
46 	.is_enabled		= regulator_is_enabled_regmap,
47 	.enable			= regulator_enable_regmap,
48 	.disable		= regulator_disable_regmap,
49 	.enable_time		= rc5t583_regulator_enable_time,
50 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
51 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
52 	.list_voltage		= regulator_list_voltage_linear,
53 	.map_voltage		= regulator_map_voltage_linear,
54 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
55 };
56 
57 #define RC5T583_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, \
58 		_vout_mask, _min_mv, _max_mv, _step_uV, _enable_mv) \
59 {								\
60 	.reg_disc_reg	= RC5T583_REG_##_disc_reg,		\
61 	.disc_bit	= _disc_bit,				\
62 	.deepsleep_reg	= RC5T583_REG_##_id##DAC_DS,		\
63 	.enable_uv_per_us = _enable_mv * 1000,			\
64 	.deepsleep_id	= RC5T583_DS_##_id,			\
65 	.desc = {						\
66 		.name = "rc5t583-regulator-"#_id,		\
67 		.id = RC5T583_REGULATOR_##_id,			\
68 		.n_voltages = (_max_mv - _min_mv) * 1000 / _step_uV + 1, \
69 		.ops = &rc5t583_ops,				\
70 		.type = REGULATOR_VOLTAGE,			\
71 		.owner = THIS_MODULE,				\
72 		.vsel_reg = RC5T583_REG_##_id##DAC,		\
73 		.vsel_mask = _vout_mask,			\
74 		.enable_reg = RC5T583_REG_##_en_reg,		\
75 		.enable_mask = BIT(_en_bit),			\
76 		.min_uV	= _min_mv * 1000,			\
77 		.uV_step = _step_uV,				\
78 		.ramp_delay = 40 * 1000,			\
79 	},							\
80 }
81 
82 static struct rc5t583_regulator_info rc5t583_reg_info[RC5T583_REGULATOR_MAX] = {
83 	RC5T583_REG(DC0, DC0CTL, 0, DC0CTL, 1, 0x7F, 700, 1500, 12500, 4),
84 	RC5T583_REG(DC1, DC1CTL, 0, DC1CTL, 1, 0x7F, 700, 1500, 12500, 14),
85 	RC5T583_REG(DC2, DC2CTL, 0, DC2CTL, 1, 0x7F, 900, 2400, 12500, 14),
86 	RC5T583_REG(DC3, DC3CTL, 0, DC3CTL, 1, 0x7F, 900, 2400, 12500, 14),
87 	RC5T583_REG(LDO0, LDOEN2, 0, LDODIS2, 0, 0x7F, 900, 3400, 25000, 160),
88 	RC5T583_REG(LDO1, LDOEN2, 1, LDODIS2, 1, 0x7F, 900, 3400, 25000, 160),
89 	RC5T583_REG(LDO2, LDOEN2, 2, LDODIS2, 2, 0x7F, 900, 3400, 25000, 160),
90 	RC5T583_REG(LDO3, LDOEN2, 3, LDODIS2, 3, 0x7F, 900, 3400, 25000, 160),
91 	RC5T583_REG(LDO4, LDOEN2, 4, LDODIS2, 4, 0x3F, 750, 1500, 12500, 133),
92 	RC5T583_REG(LDO5, LDOEN2, 5, LDODIS2, 5, 0x7F, 900, 3400, 25000, 267),
93 	RC5T583_REG(LDO6, LDOEN2, 6, LDODIS2, 6, 0x7F, 900, 3400, 25000, 133),
94 	RC5T583_REG(LDO7, LDOEN2, 7, LDODIS2, 7, 0x7F, 900, 3400, 25000, 233),
95 	RC5T583_REG(LDO8, LDOEN1, 0, LDODIS1, 0, 0x7F, 900, 3400, 25000, 233),
96 	RC5T583_REG(LDO9, LDOEN1, 1, LDODIS1, 1, 0x7F, 900, 3400, 25000, 133),
97 };
98 
rc5t583_regulator_probe(struct platform_device * pdev)99 static int rc5t583_regulator_probe(struct platform_device *pdev)
100 {
101 	struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
102 	struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
103 	struct regulator_config config = { };
104 	struct regulator_dev *rdev;
105 	struct rc5t583_regulator_info *ri;
106 	int ret;
107 	int id;
108 
109 	if (!pdata) {
110 		dev_err(&pdev->dev, "No platform data, exiting...\n");
111 		return -ENODEV;
112 	}
113 
114 	for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) {
115 		ri = &rc5t583_reg_info[id];
116 
117 		if (ri->deepsleep_id == RC5T583_DS_NONE)
118 			goto skip_ext_pwr_config;
119 
120 		ret = rc5t583_ext_power_req_config(rc5t583->dev,
121 				ri->deepsleep_id,
122 				pdata->regulator_ext_pwr_control[id],
123 				pdata->regulator_deepsleep_slot[id]);
124 		/*
125 		 * Configuring external control is not a major issue,
126 		 * just give warning.
127 		 */
128 		if (ret < 0)
129 			dev_warn(&pdev->dev,
130 				"Failed to configure ext control %d\n", id);
131 
132 skip_ext_pwr_config:
133 		config.dev = &pdev->dev;
134 		config.init_data = pdata->reg_init_data[id];
135 		config.driver_data = ri;
136 		config.regmap = rc5t583->regmap;
137 
138 		rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
139 		if (IS_ERR(rdev)) {
140 			dev_err(&pdev->dev, "Failed to register regulator %s\n",
141 						ri->desc.name);
142 			return PTR_ERR(rdev);
143 		}
144 	}
145 	return 0;
146 }
147 
148 static struct platform_driver rc5t583_regulator_driver = {
149 	.driver	= {
150 		.name	= "rc5t583-regulator",
151 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
152 	},
153 	.probe		= rc5t583_regulator_probe,
154 };
155 
rc5t583_regulator_init(void)156 static int __init rc5t583_regulator_init(void)
157 {
158 	return platform_driver_register(&rc5t583_regulator_driver);
159 }
160 subsys_initcall(rc5t583_regulator_init);
161 
rc5t583_regulator_exit(void)162 static void __exit rc5t583_regulator_exit(void)
163 {
164 	platform_driver_unregister(&rc5t583_regulator_driver);
165 }
166 module_exit(rc5t583_regulator_exit);
167 
168 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
169 MODULE_DESCRIPTION("RC5T583 regulator driver");
170 MODULE_ALIAS("platform:rc5t583-regulator");
171 MODULE_LICENSE("GPL v2");
172