1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020, The Linux Foundation. All rights reserved.
3 
4 #include <linux/module.h>
5 #include <linux/of_irq.h>
6 #include <linux/of.h>
7 #include <linux/of_device.h>
8 #include <linux/platform_device.h>
9 #include <linux/regmap.h>
10 #include <linux/regulator/driver.h>
11 #include <linux/regulator/of_regulator.h>
12 
13 #define REG_PERPH_TYPE                  0x04
14 
15 #define QCOM_LAB_TYPE			0x24
16 #define QCOM_IBB_TYPE			0x20
17 
18 #define PMI8998_LAB_REG_BASE		0xde00
19 #define PMI8998_IBB_REG_BASE		0xdc00
20 
21 #define REG_LABIBB_STATUS1		0x08
22 #define REG_LABIBB_ENABLE_CTL		0x46
23 #define LABIBB_STATUS1_VREG_OK_BIT	BIT(7)
24 #define LABIBB_CONTROL_ENABLE		BIT(7)
25 
26 #define LAB_ENABLE_CTL_MASK		BIT(7)
27 #define IBB_ENABLE_CTL_MASK		(BIT(7) | BIT(6))
28 
29 #define LABIBB_OFF_ON_DELAY		1000
30 #define LAB_ENABLE_TIME			(LABIBB_OFF_ON_DELAY * 2)
31 #define IBB_ENABLE_TIME			(LABIBB_OFF_ON_DELAY * 10)
32 #define LABIBB_POLL_ENABLED_TIME	1000
33 
34 struct labibb_regulator {
35 	struct regulator_desc		desc;
36 	struct device			*dev;
37 	struct regmap			*regmap;
38 	struct regulator_dev		*rdev;
39 	u16				base;
40 	u8				type;
41 };
42 
43 struct labibb_regulator_data {
44 	const char			*name;
45 	u8				type;
46 	u16				base;
47 	const struct regulator_desc	*desc;
48 };
49 
50 static const struct regulator_ops qcom_labibb_ops = {
51 	.enable			= regulator_enable_regmap,
52 	.disable		= regulator_disable_regmap,
53 	.is_enabled		= regulator_is_enabled_regmap,
54 };
55 
56 static const struct regulator_desc pmi8998_lab_desc = {
57 	.enable_mask		= LAB_ENABLE_CTL_MASK,
58 	.enable_reg		= (PMI8998_LAB_REG_BASE + REG_LABIBB_ENABLE_CTL),
59 	.enable_val		= LABIBB_CONTROL_ENABLE,
60 	.enable_time		= LAB_ENABLE_TIME,
61 	.poll_enabled_time	= LABIBB_POLL_ENABLED_TIME,
62 	.off_on_delay		= LABIBB_OFF_ON_DELAY,
63 	.owner			= THIS_MODULE,
64 	.type			= REGULATOR_VOLTAGE,
65 	.ops			= &qcom_labibb_ops,
66 };
67 
68 static const struct regulator_desc pmi8998_ibb_desc = {
69 	.enable_mask		= IBB_ENABLE_CTL_MASK,
70 	.enable_reg		= (PMI8998_IBB_REG_BASE + REG_LABIBB_ENABLE_CTL),
71 	.enable_val		= LABIBB_CONTROL_ENABLE,
72 	.enable_time		= IBB_ENABLE_TIME,
73 	.poll_enabled_time	= LABIBB_POLL_ENABLED_TIME,
74 	.off_on_delay		= LABIBB_OFF_ON_DELAY,
75 	.owner			= THIS_MODULE,
76 	.type			= REGULATOR_VOLTAGE,
77 	.ops			= &qcom_labibb_ops,
78 };
79 
80 static const struct labibb_regulator_data pmi8998_labibb_data[] = {
81 	{"lab", QCOM_LAB_TYPE, PMI8998_LAB_REG_BASE, &pmi8998_lab_desc},
82 	{"ibb", QCOM_IBB_TYPE, PMI8998_IBB_REG_BASE, &pmi8998_ibb_desc},
83 	{ },
84 };
85 
86 static const struct of_device_id qcom_labibb_match[] = {
87 	{ .compatible = "qcom,pmi8998-lab-ibb", .data = &pmi8998_labibb_data},
88 	{ },
89 };
90 MODULE_DEVICE_TABLE(of, qcom_labibb_match);
91 
92 static int qcom_labibb_regulator_probe(struct platform_device *pdev)
93 {
94 	struct labibb_regulator *vreg;
95 	struct device *dev = &pdev->dev;
96 	struct regulator_config cfg = {};
97 
98 	const struct of_device_id *match;
99 	const struct labibb_regulator_data *reg_data;
100 	struct regmap *reg_regmap;
101 	unsigned int type;
102 	int ret;
103 
104 	reg_regmap = dev_get_regmap(pdev->dev.parent, NULL);
105 	if (!reg_regmap) {
106 		dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
107 		return -ENODEV;
108 	}
109 
110 	match = of_match_device(qcom_labibb_match, &pdev->dev);
111 	if (!match)
112 		return -ENODEV;
113 
114 	for (reg_data = match->data; reg_data->name; reg_data++) {
115 
116 		/* Validate if the type of regulator is indeed
117 		 * what's mentioned in DT.
118 		 */
119 		ret = regmap_read(reg_regmap, reg_data->base + REG_PERPH_TYPE,
120 				  &type);
121 		if (ret < 0) {
122 			dev_err(dev,
123 				"Peripheral type read failed ret=%d\n",
124 				ret);
125 			return -EINVAL;
126 		}
127 
128 		if (WARN_ON((type != QCOM_LAB_TYPE) && (type != QCOM_IBB_TYPE)) ||
129 		    WARN_ON(type != reg_data->type))
130 			return -EINVAL;
131 
132 		vreg  = devm_kzalloc(&pdev->dev, sizeof(*vreg),
133 					   GFP_KERNEL);
134 		if (!vreg)
135 			return -ENOMEM;
136 
137 		vreg->regmap = reg_regmap;
138 		vreg->dev = dev;
139 		vreg->base = reg_data->base;
140 		vreg->type = reg_data->type;
141 
142 		memcpy(&vreg->desc, reg_data->desc, sizeof(vreg->desc));
143 		vreg->desc.of_match = reg_data->name;
144 		vreg->desc.name = reg_data->name;
145 
146 		cfg.dev = vreg->dev;
147 		cfg.driver_data = vreg;
148 		cfg.regmap = vreg->regmap;
149 
150 		vreg->rdev = devm_regulator_register(vreg->dev, &vreg->desc,
151 							&cfg);
152 
153 		if (IS_ERR(vreg->rdev)) {
154 			dev_err(dev, "qcom_labibb: error registering %s : %d\n",
155 					reg_data->name, ret);
156 			return PTR_ERR(vreg->rdev);
157 		}
158 	}
159 
160 	return 0;
161 }
162 
163 static struct platform_driver qcom_labibb_regulator_driver = {
164 	.driver	= {
165 		.name = "qcom-lab-ibb-regulator",
166 		.of_match_table	= qcom_labibb_match,
167 	},
168 	.probe = qcom_labibb_regulator_probe,
169 };
170 module_platform_driver(qcom_labibb_regulator_driver);
171 
172 MODULE_DESCRIPTION("Qualcomm labibb driver");
173 MODULE_AUTHOR("Nisha Kumari <nishakumari@codeaurora.org>");
174 MODULE_AUTHOR("Sumit Semwal <sumit.semwal@linaro.org>");
175 MODULE_LICENSE("GPL v2");
176