1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
4 */
5
6 #include <linux/cleanup.h>
7 #include <linux/delay.h>
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/platform_device.h>
12 #include <linux/of_platform.h>
13 #include <linux/regmap.h>
14 #include <linux/spmi.h>
15 #include <linux/soc/qcom/qcom-pbs.h>
16
17 #define PBS_CLIENT_TRIG_CTL 0x42
18 #define PBS_CLIENT_SW_TRIG_BIT BIT(7)
19 #define PBS_CLIENT_SCRATCH1 0x50
20 #define PBS_CLIENT_SCRATCH2 0x51
21 #define PBS_CLIENT_SCRATCH2_ERROR 0xFF
22
23 #define RETRIES 2000
24 #define DELAY 1100
25
26 struct pbs_dev {
27 struct device *dev;
28 struct regmap *regmap;
29 struct mutex lock;
30 struct device_link *link;
31
32 u32 base;
33 };
34
qcom_pbs_wait_for_ack(struct pbs_dev * pbs,u8 bit_pos)35 static int qcom_pbs_wait_for_ack(struct pbs_dev *pbs, u8 bit_pos)
36 {
37 unsigned int val;
38 int ret;
39
40 ret = regmap_read_poll_timeout(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2,
41 val, val & BIT(bit_pos), DELAY, DELAY * RETRIES);
42
43 if (ret < 0) {
44 dev_err(pbs->dev, "Timeout for PBS ACK/NACK for bit %u\n", bit_pos);
45 return -ETIMEDOUT;
46 }
47
48 if (val == PBS_CLIENT_SCRATCH2_ERROR) {
49 ret = regmap_write(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, 0);
50 dev_err(pbs->dev, "NACK from PBS for bit %u\n", bit_pos);
51 return -EINVAL;
52 }
53
54 dev_dbg(pbs->dev, "PBS sequence for bit %u executed!\n", bit_pos);
55 return 0;
56 }
57
58 /**
59 * qcom_pbs_trigger_event() - Trigger the PBS RAM sequence
60 * @pbs: Pointer to PBS device
61 * @bitmap: bitmap
62 *
63 * This function is used to trigger the PBS RAM sequence to be
64 * executed by the client driver.
65 *
66 * The PBS trigger sequence involves
67 * 1. setting the PBS sequence bit in PBS_CLIENT_SCRATCH1
68 * 2. Initiating the SW PBS trigger
69 * 3. Checking the equivalent bit in PBS_CLIENT_SCRATCH2 for the
70 * completion of the sequence.
71 * 4. If PBS_CLIENT_SCRATCH2 == 0xFF, the PBS sequence failed to execute
72 *
73 * Return: 0 on success, < 0 on failure
74 */
qcom_pbs_trigger_event(struct pbs_dev * pbs,u8 bitmap)75 int qcom_pbs_trigger_event(struct pbs_dev *pbs, u8 bitmap)
76 {
77 unsigned int val;
78 u16 bit_pos;
79 int ret;
80
81 if (WARN_ON(!bitmap))
82 return -EINVAL;
83
84 if (IS_ERR_OR_NULL(pbs))
85 return -EINVAL;
86
87 mutex_lock(&pbs->lock);
88 ret = regmap_read(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, &val);
89 if (ret < 0)
90 goto out;
91
92 if (val == PBS_CLIENT_SCRATCH2_ERROR) {
93 /* PBS error - clear SCRATCH2 register */
94 ret = regmap_write(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, 0);
95 if (ret < 0)
96 goto out;
97 }
98
99 for (bit_pos = 0; bit_pos < 8; bit_pos++) {
100 if (!(bitmap & BIT(bit_pos)))
101 continue;
102
103 /* Clear the PBS sequence bit position */
104 ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2,
105 BIT(bit_pos), 0);
106 if (ret < 0)
107 goto out_clear_scratch1;
108
109 /* Set the PBS sequence bit position */
110 ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH1,
111 BIT(bit_pos), BIT(bit_pos));
112 if (ret < 0)
113 goto out_clear_scratch1;
114
115 /* Initiate the SW trigger */
116 ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_TRIG_CTL,
117 PBS_CLIENT_SW_TRIG_BIT, PBS_CLIENT_SW_TRIG_BIT);
118 if (ret < 0)
119 goto out_clear_scratch1;
120
121 ret = qcom_pbs_wait_for_ack(pbs, bit_pos);
122 if (ret < 0)
123 goto out_clear_scratch1;
124
125 /* Clear the PBS sequence bit position */
126 regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH1, BIT(bit_pos), 0);
127 regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, BIT(bit_pos), 0);
128 }
129
130 out_clear_scratch1:
131 /* Clear all the requested bitmap */
132 ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH1, bitmap, 0);
133
134 out:
135 mutex_unlock(&pbs->lock);
136
137 return ret;
138 }
139 EXPORT_SYMBOL_GPL(qcom_pbs_trigger_event);
140
141 /**
142 * get_pbs_client_device() - Get the PBS device used by client
143 * @dev: Client device
144 *
145 * This function is used to get the PBS device that is being
146 * used by the client.
147 *
148 * Return: pbs_dev on success, ERR_PTR on failure
149 */
get_pbs_client_device(struct device * dev)150 struct pbs_dev *get_pbs_client_device(struct device *dev)
151 {
152 struct platform_device *pdev;
153 struct pbs_dev *pbs;
154
155 struct device_node *pbs_dev_node __free(device_node) = of_parse_phandle(dev->of_node,
156 "qcom,pbs", 0);
157 if (!pbs_dev_node) {
158 dev_err(dev, "Missing qcom,pbs property\n");
159 return ERR_PTR(-ENODEV);
160 }
161
162 pdev = of_find_device_by_node(pbs_dev_node);
163 if (!pdev) {
164 dev_err(dev, "Unable to find PBS dev_node\n");
165 return ERR_PTR(-EPROBE_DEFER);
166 }
167
168 pbs = platform_get_drvdata(pdev);
169 if (!pbs) {
170 dev_err(dev, "Cannot get pbs instance from %s\n", dev_name(&pdev->dev));
171 platform_device_put(pdev);
172 return ERR_PTR(-EPROBE_DEFER);
173 }
174
175 pbs->link = device_link_add(dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER);
176 if (!pbs->link) {
177 dev_err(&pdev->dev, "Failed to create device link to consumer %s\n", dev_name(dev));
178 platform_device_put(pdev);
179 return ERR_PTR(-EINVAL);
180 }
181
182 return pbs;
183 }
184 EXPORT_SYMBOL_GPL(get_pbs_client_device);
185
qcom_pbs_probe(struct platform_device * pdev)186 static int qcom_pbs_probe(struct platform_device *pdev)
187 {
188 struct pbs_dev *pbs;
189 u32 val;
190 int ret;
191
192 pbs = devm_kzalloc(&pdev->dev, sizeof(*pbs), GFP_KERNEL);
193 if (!pbs)
194 return -ENOMEM;
195
196 pbs->dev = &pdev->dev;
197 pbs->regmap = dev_get_regmap(pbs->dev->parent, NULL);
198 if (!pbs->regmap) {
199 dev_err(pbs->dev, "Couldn't get parent's regmap\n");
200 return -EINVAL;
201 }
202
203 ret = device_property_read_u32(pbs->dev, "reg", &val);
204 if (ret < 0) {
205 dev_err(pbs->dev, "Couldn't find reg, ret = %d\n", ret);
206 return ret;
207 }
208 pbs->base = val;
209 mutex_init(&pbs->lock);
210
211 platform_set_drvdata(pdev, pbs);
212
213 return 0;
214 }
215
216 static const struct of_device_id qcom_pbs_match_table[] = {
217 { .compatible = "qcom,pbs" },
218 {}
219 };
220 MODULE_DEVICE_TABLE(of, qcom_pbs_match_table);
221
222 static struct platform_driver qcom_pbs_driver = {
223 .driver = {
224 .name = "qcom-pbs",
225 .of_match_table = qcom_pbs_match_table,
226 },
227 .probe = qcom_pbs_probe,
228 };
229 module_platform_driver(qcom_pbs_driver)
230
231 MODULE_DESCRIPTION("QCOM PBS DRIVER");
232 MODULE_LICENSE("GPL");
233