1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
6 #include <linux/cleanup.h>
7 #include <linux/delay.h>
9 #include <linux/module.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>
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
28 struct regmap
*regmap
;
30 struct device_link
*link
;
35 static int qcom_pbs_wait_for_ack(struct pbs_dev
*pbs
, u8 bit_pos
)
40 ret
= regmap_read_poll_timeout(pbs
->regmap
, pbs
->base
+ PBS_CLIENT_SCRATCH2
,
41 val
, val
& BIT(bit_pos
), DELAY
, DELAY
* RETRIES
);
44 dev_err(pbs
->dev
, "Timeout for PBS ACK/NACK for bit %u\n", bit_pos
);
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
);
54 dev_dbg(pbs
->dev
, "PBS sequence for bit %u executed!\n", bit_pos
);
59 * qcom_pbs_trigger_event() - Trigger the PBS RAM sequence
60 * @pbs: Pointer to PBS device
63 * This function is used to trigger the PBS RAM sequence to be
64 * executed by the client driver.
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
73 * Return: 0 on success, < 0 on failure
75 int qcom_pbs_trigger_event(struct pbs_dev
*pbs
, u8 bitmap
)
84 if (IS_ERR_OR_NULL(pbs
))
87 guard(mutex
)(&pbs
->lock
);
88 ret
= regmap_read(pbs
->regmap
, pbs
->base
+ PBS_CLIENT_SCRATCH2
, &val
);
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);
99 for (bit_pos
= 0; bit_pos
< 8; bit_pos
++) {
100 if (!(bitmap
& BIT(bit_pos
)))
103 /* Clear the PBS sequence bit position */
104 ret
= regmap_update_bits(pbs
->regmap
, pbs
->base
+ PBS_CLIENT_SCRATCH2
,
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
));
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
);
121 ret
= qcom_pbs_wait_for_ack(pbs
, bit_pos
);
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);
130 /* Clear all the requested bitmap */
131 return regmap_update_bits(pbs
->regmap
, pbs
->base
+ PBS_CLIENT_SCRATCH1
, bitmap
, 0);
133 EXPORT_SYMBOL_GPL(qcom_pbs_trigger_event
);
136 * get_pbs_client_device() - Get the PBS device used by client
137 * @dev: Client device
139 * This function is used to get the PBS device that is being
140 * used by the client.
142 * Return: pbs_dev on success, ERR_PTR on failure
144 struct pbs_dev
*get_pbs_client_device(struct device
*dev
)
146 struct platform_device
*pdev
;
149 struct device_node
*pbs_dev_node
__free(device_node
) = of_parse_phandle(dev
->of_node
,
152 dev_err(dev
, "Missing qcom,pbs property\n");
153 return ERR_PTR(-ENODEV
);
156 pdev
= of_find_device_by_node(pbs_dev_node
);
158 dev_err(dev
, "Unable to find PBS dev_node\n");
159 return ERR_PTR(-EPROBE_DEFER
);
162 pbs
= platform_get_drvdata(pdev
);
164 dev_err(dev
, "Cannot get pbs instance from %s\n", dev_name(&pdev
->dev
));
165 platform_device_put(pdev
);
166 return ERR_PTR(-EPROBE_DEFER
);
169 pbs
->link
= device_link_add(dev
, &pdev
->dev
, DL_FLAG_AUTOREMOVE_SUPPLIER
);
171 dev_err(&pdev
->dev
, "Failed to create device link to consumer %s\n", dev_name(dev
));
172 platform_device_put(pdev
);
173 return ERR_PTR(-EINVAL
);
178 EXPORT_SYMBOL_GPL(get_pbs_client_device
);
180 static int qcom_pbs_probe(struct platform_device
*pdev
)
186 pbs
= devm_kzalloc(&pdev
->dev
, sizeof(*pbs
), GFP_KERNEL
);
190 pbs
->dev
= &pdev
->dev
;
191 pbs
->regmap
= dev_get_regmap(pbs
->dev
->parent
, NULL
);
193 dev_err(pbs
->dev
, "Couldn't get parent's regmap\n");
197 ret
= device_property_read_u32(pbs
->dev
, "reg", &val
);
199 dev_err(pbs
->dev
, "Couldn't find reg, ret = %d\n", ret
);
203 mutex_init(&pbs
->lock
);
205 platform_set_drvdata(pdev
, pbs
);
210 static const struct of_device_id qcom_pbs_match_table
[] = {
211 { .compatible
= "qcom,pbs" },
214 MODULE_DEVICE_TABLE(of
, qcom_pbs_match_table
);
216 static struct platform_driver qcom_pbs_driver
= {
219 .of_match_table
= qcom_pbs_match_table
,
221 .probe
= qcom_pbs_probe
,
223 module_platform_driver(qcom_pbs_driver
)
225 MODULE_DESCRIPTION("QCOM PBS DRIVER");
226 MODULE_LICENSE("GPL");