1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
6 #include <linux/delay.h>
8 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/of_platform.h>
12 #include <linux/regmap.h>
13 #include <linux/spmi.h>
14 #include <linux/soc/qcom/qcom-pbs.h>
16 #define PBS_CLIENT_TRIG_CTL 0x42
17 #define PBS_CLIENT_SW_TRIG_BIT BIT(7)
18 #define PBS_CLIENT_SCRATCH1 0x50
19 #define PBS_CLIENT_SCRATCH2 0x51
20 #define PBS_CLIENT_SCRATCH2_ERROR 0xFF
27 struct regmap *regmap;
29 struct device_link *link;
34 static int qcom_pbs_wait_for_ack(struct pbs_dev *pbs, u8 bit_pos)
39 ret = regmap_read_poll_timeout(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2,
40 val, val & BIT(bit_pos), DELAY, DELAY * RETRIES);
43 dev_err(pbs->dev, "Timeout for PBS ACK/NACK for bit %u\n", bit_pos);
47 if (val == PBS_CLIENT_SCRATCH2_ERROR) {
48 ret = regmap_write(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, 0);
49 dev_err(pbs->dev, "NACK from PBS for bit %u\n", bit_pos);
53 dev_dbg(pbs->dev, "PBS sequence for bit %u executed!\n", bit_pos);
58 * qcom_pbs_trigger_event() - Trigger the PBS RAM sequence
59 * @pbs: Pointer to PBS device
62 * This function is used to trigger the PBS RAM sequence to be
63 * executed by the client driver.
65 * The PBS trigger sequence involves
66 * 1. setting the PBS sequence bit in PBS_CLIENT_SCRATCH1
67 * 2. Initiating the SW PBS trigger
68 * 3. Checking the equivalent bit in PBS_CLIENT_SCRATCH2 for the
69 * completion of the sequence.
70 * 4. If PBS_CLIENT_SCRATCH2 == 0xFF, the PBS sequence failed to execute
72 * Return: 0 on success, < 0 on failure
74 int qcom_pbs_trigger_event(struct pbs_dev *pbs, u8 bitmap)
83 if (IS_ERR_OR_NULL(pbs))
86 mutex_lock(&pbs->lock);
87 ret = regmap_read(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, &val);
91 if (val == PBS_CLIENT_SCRATCH2_ERROR) {
92 /* PBS error - clear SCRATCH2 register */
93 ret = regmap_write(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, 0);
98 for (bit_pos = 0; bit_pos < 8; bit_pos++) {
99 if (!(bitmap & BIT(bit_pos)))
102 /* Clear the PBS sequence bit position */
103 ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2,
106 goto out_clear_scratch1;
108 /* Set the PBS sequence bit position */
109 ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH1,
110 BIT(bit_pos), BIT(bit_pos));
112 goto out_clear_scratch1;
114 /* Initiate the SW trigger */
115 ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_TRIG_CTL,
116 PBS_CLIENT_SW_TRIG_BIT, PBS_CLIENT_SW_TRIG_BIT);
118 goto out_clear_scratch1;
120 ret = qcom_pbs_wait_for_ack(pbs, bit_pos);
122 goto out_clear_scratch1;
124 /* Clear the PBS sequence bit position */
125 regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH1, BIT(bit_pos), 0);
126 regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, BIT(bit_pos), 0);
130 /* Clear all the requested bitmap */
131 ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH1, bitmap, 0);
134 mutex_unlock(&pbs->lock);
138 EXPORT_SYMBOL_GPL(qcom_pbs_trigger_event);
141 * get_pbs_client_device() - Get the PBS device used by client
142 * @dev: Client device
144 * This function is used to get the PBS device that is being
145 * used by the client.
147 * Return: pbs_dev on success, ERR_PTR on failure
149 struct pbs_dev *get_pbs_client_device(struct device *dev)
151 struct device_node *pbs_dev_node;
152 struct platform_device *pdev;
155 pbs_dev_node = of_parse_phandle(dev->of_node, "qcom,pbs", 0);
157 dev_err(dev, "Missing qcom,pbs property\n");
158 return ERR_PTR(-ENODEV);
161 pdev = of_find_device_by_node(pbs_dev_node);
163 dev_err(dev, "Unable to find PBS dev_node\n");
164 pbs = ERR_PTR(-EPROBE_DEFER);
168 pbs = platform_get_drvdata(pdev);
170 dev_err(dev, "Cannot get pbs instance from %s\n", dev_name(&pdev->dev));
171 platform_device_put(pdev);
172 pbs = ERR_PTR(-EPROBE_DEFER);
176 pbs->link = device_link_add(dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER);
178 dev_err(&pdev->dev, "Failed to create device link to consumer %s\n", dev_name(dev));
179 platform_device_put(pdev);
180 pbs = ERR_PTR(-EINVAL);
185 of_node_put(pbs_dev_node);
188 EXPORT_SYMBOL_GPL(get_pbs_client_device);
190 static int qcom_pbs_probe(struct platform_device *pdev)
196 pbs = devm_kzalloc(&pdev->dev, sizeof(*pbs), GFP_KERNEL);
200 pbs->dev = &pdev->dev;
201 pbs->regmap = dev_get_regmap(pbs->dev->parent, NULL);
203 dev_err(pbs->dev, "Couldn't get parent's regmap\n");
207 ret = device_property_read_u32(pbs->dev, "reg", &val);
209 dev_err(pbs->dev, "Couldn't find reg, ret = %d\n", ret);
213 mutex_init(&pbs->lock);
215 platform_set_drvdata(pdev, pbs);
220 static const struct of_device_id qcom_pbs_match_table[] = {
221 { .compatible = "qcom,pbs" },
224 MODULE_DEVICE_TABLE(of, qcom_pbs_match_table);
226 static struct platform_driver qcom_pbs_driver = {
229 .of_match_table = qcom_pbs_match_table,
231 .probe = qcom_pbs_probe,
233 module_platform_driver(qcom_pbs_driver)
235 MODULE_DESCRIPTION("QCOM PBS DRIVER");
236 MODULE_LICENSE("GPL");