blob: 662d87ae61cb5254420e8673a8ec53f2a2fb14d7 [file] [log] [blame]
Mauro Carvalho Chehabb660bf62020-08-17 09:10:45 +02001// SPDX-License-Identifier: GPL-2.0
Mauro Carvalho Chehabfac4da42021-01-21 08:18:06 +01002//
3// Device driver for regulators in Hisi IC
4//
5// Copyright (c) 2013 Linaro Ltd.
Hao Fang8d6ee302021-05-22 18:25:51 +08006// Copyright (c) 2011 HiSilicon Ltd.
Mauro Carvalho Chehabe8608f82021-07-02 09:37:17 +02007// Copyright (c) 2020-2021 Huawei Technologies Co., Ltd.
Mauro Carvalho Chehabfac4da42021-01-21 08:18:06 +01008//
9// Guodong Xu <guodong.xu@linaro.org>
Mayulong42f24d92020-08-17 09:10:42 +020010
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +010011#include <linux/delay.h>
Mauro Carvalho Chehab2c4fb702020-08-17 09:10:59 +020012#include <linux/mfd/hi6421-spmi-pmic.h>
13#include <linux/module.h>
Mauro Carvalho Chehab2c4fb702020-08-17 09:10:59 +020014#include <linux/platform_device.h>
Mauro Carvalho Chehabfb02e3e2021-01-29 20:51:47 +010015#include <linux/regmap.h>
Mayulong42f24d92020-08-17 09:10:42 +020016#include <linux/regulator/driver.h>
Mayulong42f24d92020-08-17 09:10:42 +020017#include <linux/spmi.h>
18
Axel Lin9bc146a2021-06-22 12:33:29 +080019struct hi6421_spmi_reg_priv {
Mauro Carvalho Chehab6436a1252021-01-21 08:18:09 +010020 /* Serialize regulator enable logic */
21 struct mutex enable_mutex;
22};
Mayulong42f24d92020-08-17 09:10:42 +020023
Mayulong42f24d92020-08-17 09:10:42 +020024struct hi6421_spmi_reg_info {
25 struct regulator_desc desc;
Mayulong42f24d92020-08-17 09:10:42 +020026 u8 eco_mode_mask;
27 u32 eco_uA;
Mayulong42f24d92020-08-17 09:10:42 +020028};
29
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +020030static const unsigned int range_1v5_to_2v0[] = {
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +010031 1500000, 1550000, 1600000, 1650000,
32 1700000, 1725000, 1750000, 1775000,
33 1800000, 1825000, 1850000, 1875000,
34 1900000, 1925000, 1950000, 2000000
35};
36
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +020037static const unsigned int range_1v725_to_1v9[] = {
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +010038 1725000, 1750000, 1775000, 1800000,
39 1825000, 1850000, 1875000, 1900000
40};
41
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +020042static const unsigned int range_1v75_to_3v3[] = {
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +010043 1750000, 1800000, 1825000, 2800000,
44 2850000, 2950000, 3000000, 3300000
45};
46
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +020047static const unsigned int range_1v8_to_3v0[] = {
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +010048 1800000, 1850000, 2400000, 2600000,
49 2700000, 2850000, 2950000, 3000000
50};
51
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +020052static const unsigned int range_2v5_to_3v3[] = {
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +010053 2500000, 2600000, 2700000, 2800000,
54 3000000, 3100000, 3200000, 3300000
55};
56
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +020057static const unsigned int range_2v6_to_3v3[] = {
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +010058 2600000, 2700000, 2800000, 2900000,
59 3000000, 3100000, 3200000, 3300000
60};
61
Mauro Carvalho Chehab54f11552021-01-21 08:18:13 +010062/**
63 * HI6421V600_LDO() - specify a LDO power line
64 * @_id: LDO id name string
65 * @vtable: voltage table
66 * @ereg: enable register
67 * @emask: enable mask
68 * @vreg: voltage select register
69 * @odelay: off/on delay time in uS
70 * @etime: enable time in uS
71 * @ecomask: eco mode mask
72 * @ecoamp: eco mode load uppler limit in uA
Mayulong42f24d92020-08-17 09:10:42 +020073 */
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +010074#define HI6421V600_LDO(_id, vtable, ereg, emask, vreg, \
75 odelay, etime, ecomask, ecoamp) \
Mauro Carvalho Chehabccb2a742021-07-24 12:22:41 +020076 [hi6421v600_##_id] = { \
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +010077 .desc = { \
78 .name = #_id, \
79 .of_match = of_match_ptr(#_id), \
80 .regulators_node = of_match_ptr("regulators"), \
81 .ops = &hi6421_spmi_ldo_rops, \
82 .type = REGULATOR_VOLTAGE, \
Mauro Carvalho Chehabccb2a742021-07-24 12:22:41 +020083 .id = hi6421v600_##_id, \
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +010084 .owner = THIS_MODULE, \
85 .volt_table = vtable, \
86 .n_voltages = ARRAY_SIZE(vtable), \
Axel Lin50bec7f2021-05-29 09:32:36 +080087 .vsel_mask = ARRAY_SIZE(vtable) - 1, \
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +010088 .vsel_reg = vreg, \
89 .enable_reg = ereg, \
90 .enable_mask = emask, \
91 .enable_time = etime, \
92 .ramp_delay = etime, \
93 .off_on_delay = odelay, \
94 }, \
95 .eco_mode_mask = ecomask, \
96 .eco_uA = ecoamp, \
97 }
Mayulong42f24d92020-08-17 09:10:42 +020098
Mauro Carvalho Chehabc22aeb92020-08-17 09:10:51 +020099static int hi6421_spmi_regulator_enable(struct regulator_dev *rdev)
Mayulong42f24d92020-08-17 09:10:42 +0200100{
Axel Lin5db5dd52021-06-30 15:42:46 +0800101 struct hi6421_spmi_reg_priv *priv = rdev_get_drvdata(rdev);
Mauro Carvalho Chehab6a5e7aa2021-01-21 08:18:10 +0100102 int ret;
Mayulong42f24d92020-08-17 09:10:42 +0200103
Mayulong42f24d92020-08-17 09:10:42 +0200104 /* cannot enable more than one regulator at one time */
Axel Lin9bc146a2021-06-22 12:33:29 +0800105 mutex_lock(&priv->enable_mutex);
Mauro Carvalho Chehab12ca3b22020-08-17 09:10:44 +0200106
Axel Lin4446e6f2021-04-30 16:55:55 +0800107 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
Mauro Carvalho Chehabfb02e3e2021-01-29 20:51:47 +0100108 rdev->desc->enable_mask,
Atul Gopinathan643fd472021-02-09 13:49:34 +0530109 rdev->desc->enable_mask);
Mayulong42f24d92020-08-17 09:10:42 +0200110
Mauro Carvalho Chehabfd765da2021-01-21 08:18:12 +0100111 /* Avoid powering up multiple devices at the same time */
112 usleep_range(rdev->desc->off_on_delay, rdev->desc->off_on_delay + 60);
113
Axel Lin9bc146a2021-06-22 12:33:29 +0800114 mutex_unlock(&priv->enable_mutex);
Mayulong42f24d92020-08-17 09:10:42 +0200115
Mauro Carvalho Chehab6a5e7aa2021-01-21 08:18:10 +0100116 return ret;
Mayulong42f24d92020-08-17 09:10:42 +0200117}
118
Mauro Carvalho Chehabc22aeb92020-08-17 09:10:51 +0200119static unsigned int hi6421_spmi_regulator_get_mode(struct regulator_dev *rdev)
Mayulong42f24d92020-08-17 09:10:42 +0200120{
Axel Lin5db5dd52021-06-30 15:42:46 +0800121 struct hi6421_spmi_reg_info *sreg;
Axel Lin57c045b2021-06-19 20:34:23 +0800122 unsigned int reg_val;
Mayulong42f24d92020-08-17 09:10:42 +0200123
Axel Lin5db5dd52021-06-30 15:42:46 +0800124 sreg = container_of(rdev->desc, struct hi6421_spmi_reg_info, desc);
Axel Lin4446e6f2021-04-30 16:55:55 +0800125 regmap_read(rdev->regmap, rdev->desc->enable_reg, &reg_val);
Mayulong42f24d92020-08-17 09:10:42 +0200126
Mauro Carvalho Chehab32f538042020-08-17 09:10:47 +0200127 if (reg_val & sreg->eco_mode_mask)
Mauro Carvalho Chehab0b5a5622021-01-21 08:18:08 +0100128 return REGULATOR_MODE_IDLE;
Mauro Carvalho Chehab9a183292020-08-17 09:10:48 +0200129
Mauro Carvalho Chehab0b5a5622021-01-21 08:18:08 +0100130 return REGULATOR_MODE_NORMAL;
Mayulong42f24d92020-08-17 09:10:42 +0200131}
132
Mauro Carvalho Chehabc22aeb92020-08-17 09:10:51 +0200133static int hi6421_spmi_regulator_set_mode(struct regulator_dev *rdev,
Mauro Carvalho Chehab1275f3c2020-08-17 09:10:57 +0200134 unsigned int mode)
Mayulong42f24d92020-08-17 09:10:42 +0200135{
Axel Lin5db5dd52021-06-30 15:42:46 +0800136 struct hi6421_spmi_reg_info *sreg;
Axel Lin57c045b2021-06-19 20:34:23 +0800137 unsigned int val;
Mayulong42f24d92020-08-17 09:10:42 +0200138
Axel Lin5db5dd52021-06-30 15:42:46 +0800139 sreg = container_of(rdev->desc, struct hi6421_spmi_reg_info, desc);
Mayulong42f24d92020-08-17 09:10:42 +0200140 switch (mode) {
141 case REGULATOR_MODE_NORMAL:
Mauro Carvalho Chehabdb27f822020-08-17 09:10:49 +0200142 val = 0;
Mayulong42f24d92020-08-17 09:10:42 +0200143 break;
144 case REGULATOR_MODE_IDLE:
Axel Lin57c045b2021-06-19 20:34:23 +0800145 if (!sreg->eco_mode_mask)
146 return -EINVAL;
147
148 val = sreg->eco_mode_mask;
Mayulong42f24d92020-08-17 09:10:42 +0200149 break;
150 default:
151 return -EINVAL;
152 }
153
Axel Lin4446e6f2021-04-30 16:55:55 +0800154 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
Mauro Carvalho Chehabfb02e3e2021-01-29 20:51:47 +0100155 sreg->eco_mode_mask, val);
Mayulong42f24d92020-08-17 09:10:42 +0200156}
157
Mauro Carvalho Chehab1275f3c2020-08-17 09:10:57 +0200158static unsigned int
159hi6421_spmi_regulator_get_optimum_mode(struct regulator_dev *rdev,
160 int input_uV, int output_uV,
161 int load_uA)
Mayulong42f24d92020-08-17 09:10:42 +0200162{
Axel Lin5db5dd52021-06-30 15:42:46 +0800163 struct hi6421_spmi_reg_info *sreg;
164
165 sreg = container_of(rdev->desc, struct hi6421_spmi_reg_info, desc);
Mayulong42f24d92020-08-17 09:10:42 +0200166
Mauro Carvalho Chehab75d39eb2021-01-21 08:18:14 +0100167 if (!sreg->eco_uA || ((unsigned int)load_uA > sreg->eco_uA))
Mayulong42f24d92020-08-17 09:10:42 +0200168 return REGULATOR_MODE_NORMAL;
Mauro Carvalho Chehab2c4fb702020-08-17 09:10:59 +0200169
170 return REGULATOR_MODE_IDLE;
Mayulong42f24d92020-08-17 09:10:42 +0200171}
172
Mauro Carvalho Chehab2c4fb702020-08-17 09:10:59 +0200173static const struct regulator_ops hi6421_spmi_ldo_rops = {
Mauro Carvalho Chehab2ba53d02021-01-29 20:51:48 +0100174 .is_enabled = regulator_is_enabled_regmap,
Mauro Carvalho Chehabc22aeb92020-08-17 09:10:51 +0200175 .enable = hi6421_spmi_regulator_enable,
Axel Lin4446e6f2021-04-30 16:55:55 +0800176 .disable = regulator_disable_regmap,
Mayulong42f24d92020-08-17 09:10:42 +0200177 .list_voltage = regulator_list_voltage_table,
Axel Lin00430f72021-06-10 21:41:28 +0800178 .map_voltage = regulator_map_voltage_ascend,
Mauro Carvalho Chehab2ba53d02021-01-29 20:51:48 +0100179 .get_voltage_sel = regulator_get_voltage_sel_regmap,
180 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mauro Carvalho Chehabc22aeb92020-08-17 09:10:51 +0200181 .get_mode = hi6421_spmi_regulator_get_mode,
182 .set_mode = hi6421_spmi_regulator_set_mode,
183 .get_optimum_mode = hi6421_spmi_regulator_get_optimum_mode,
Mayulong42f24d92020-08-17 09:10:42 +0200184};
185
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100186/* HI6421v600 regulators with known registers */
187enum hi6421_spmi_regulator_id {
Mauro Carvalho Chehabccb2a742021-07-24 12:22:41 +0200188 hi6421v600_ldo3,
189 hi6421v600_ldo4,
190 hi6421v600_ldo9,
191 hi6421v600_ldo15,
192 hi6421v600_ldo16,
193 hi6421v600_ldo17,
194 hi6421v600_ldo33,
195 hi6421v600_ldo34,
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100196};
Mayulong42f24d92020-08-17 09:10:42 +0200197
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100198static struct hi6421_spmi_reg_info regulator_info[] = {
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +0200199 HI6421V600_LDO(ldo3, range_1v5_to_2v0,
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100200 0x16, 0x01, 0x51,
201 20000, 120,
202 0, 0),
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +0200203 HI6421V600_LDO(ldo4, range_1v725_to_1v9,
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100204 0x17, 0x01, 0x52,
205 20000, 120,
206 0x10, 10000),
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +0200207 HI6421V600_LDO(ldo9, range_1v75_to_3v3,
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100208 0x1c, 0x01, 0x57,
209 20000, 360,
210 0x10, 10000),
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +0200211 HI6421V600_LDO(ldo15, range_1v8_to_3v0,
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100212 0x21, 0x01, 0x5c,
213 20000, 360,
214 0x10, 10000),
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +0200215 HI6421V600_LDO(ldo16, range_1v8_to_3v0,
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100216 0x22, 0x01, 0x5d,
217 20000, 360,
218 0x10, 10000),
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +0200219 HI6421V600_LDO(ldo17, range_2v5_to_3v3,
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100220 0x23, 0x01, 0x5e,
221 20000, 120,
222 0x10, 10000),
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +0200223 HI6421V600_LDO(ldo33, range_2v5_to_3v3,
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100224 0x32, 0x01, 0x6d,
225 20000, 120,
226 0, 0),
Mauro Carvalho Chehab5e361292021-07-24 12:22:42 +0200227 HI6421V600_LDO(ldo34, range_2v6_to_3v3,
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100228 0x33, 0x01, 0x6e,
229 20000, 120,
230 0, 0),
231};
Mayulong42f24d92020-08-17 09:10:42 +0200232
Mauro Carvalho Chehabc22aeb92020-08-17 09:10:51 +0200233static int hi6421_spmi_regulator_probe(struct platform_device *pdev)
Mauro Carvalho Chehab75937f82020-08-17 09:10:46 +0200234{
235 struct device *pmic_dev = pdev->dev.parent;
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100236 struct regulator_config config = { };
Axel Lin9bc146a2021-06-22 12:33:29 +0800237 struct hi6421_spmi_reg_priv *priv;
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100238 struct hi6421_spmi_reg_info *info;
239 struct device *dev = &pdev->dev;
Mauro Carvalho Chehabc22aeb92020-08-17 09:10:51 +0200240 struct hi6421_spmi_pmic *pmic;
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100241 struct regulator_dev *rdev;
242 int i;
Mauro Carvalho Chehab75937f82020-08-17 09:10:46 +0200243
Mauro Carvalho Chehab75937f82020-08-17 09:10:46 +0200244 /*
245 * This driver is meant to be called by hi6421-spmi-core,
246 * which should first set drvdata. If this doesn't happen, hit
247 * a warn on and return.
248 */
249 pmic = dev_get_drvdata(pmic_dev);
250 if (WARN_ON(!pmic))
251 return -ENODEV;
252
Axel Lin9bc146a2021-06-22 12:33:29 +0800253 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
254 if (!priv)
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100255 return -ENOMEM;
Mauro Carvalho Chehab75937f82020-08-17 09:10:46 +0200256
Axel Lin9bc146a2021-06-22 12:33:29 +0800257 mutex_init(&priv->enable_mutex);
Mauro Carvalho Chehab75937f82020-08-17 09:10:46 +0200258
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100259 for (i = 0; i < ARRAY_SIZE(regulator_info); i++) {
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100260 info = &regulator_info[i];
Mauro Carvalho Chehab75937f82020-08-17 09:10:46 +0200261
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100262 config.dev = pdev->dev.parent;
Axel Lin5db5dd52021-06-30 15:42:46 +0800263 config.driver_data = priv;
Mauro Carvalho Chehabfcd73242021-01-29 20:51:49 +0100264 config.regmap = pmic->regmap;
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100265
266 rdev = devm_regulator_register(dev, &info->desc, &config);
267 if (IS_ERR(rdev)) {
268 dev_err(dev, "failed to register %s\n",
269 info->desc.name);
270 return PTR_ERR(rdev);
Mauro Carvalho Chehab75937f82020-08-17 09:10:46 +0200271 }
Mauro Carvalho Chehab75937f82020-08-17 09:10:46 +0200272 }
273
Mauro Carvalho Chehab75937f82020-08-17 09:10:46 +0200274 return 0;
275}
276
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100277static const struct platform_device_id hi6421_spmi_regulator_table[] = {
Mauro Carvalho Chehab75937f82020-08-17 09:10:46 +0200278 { .name = "hi6421v600-regulator" },
279 {},
280};
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100281MODULE_DEVICE_TABLE(platform, hi6421_spmi_regulator_table);
Mayulong42f24d92020-08-17 09:10:42 +0200282
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100283static struct platform_driver hi6421_spmi_regulator_driver = {
284 .id_table = hi6421_spmi_regulator_table,
Mayulong42f24d92020-08-17 09:10:42 +0200285 .driver = {
Mauro Carvalho Chehab6a5e7aa2021-01-21 08:18:10 +0100286 .name = "hi6421v600-regulator",
Mayulong42f24d92020-08-17 09:10:42 +0200287 },
Mauro Carvalho Chehabc22aeb92020-08-17 09:10:51 +0200288 .probe = hi6421_spmi_regulator_probe,
Mayulong42f24d92020-08-17 09:10:42 +0200289};
Mauro Carvalho Chehabd2dfd50a2021-01-21 08:18:07 +0100290module_platform_driver(hi6421_spmi_regulator_driver);
Mayulong42f24d92020-08-17 09:10:42 +0200291
Mauro Carvalho Chehab6a5e7aa2021-01-21 08:18:10 +0100292MODULE_DESCRIPTION("Hi6421v600 SPMI regulator driver");
Mayulong42f24d92020-08-17 09:10:42 +0200293MODULE_LICENSE("GPL v2");
294