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 guard(mutex)(&pbs->lock);
88 ret = regmap_read(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, &val);
89 if (ret < 0)
90 return ret;
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 return ret;
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 break;
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 break;
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 break;
120
121 ret = qcom_pbs_wait_for_ack(pbs, bit_pos);
122 if (ret < 0)
123 break;
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 /* Clear all the requested bitmap */
131 return regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH1, bitmap, 0);
132 }
133 EXPORT_SYMBOL_GPL(qcom_pbs_trigger_event);
134
135 /**
136 * get_pbs_client_device() - Get the PBS device used by client
137 * @dev: Client device
138 *
139 * This function is used to get the PBS device that is being
140 * used by the client.
141 *
142 * Return: pbs_dev on success, ERR_PTR on failure
143 */
get_pbs_client_device(struct device * dev)144 struct pbs_dev *get_pbs_client_device(struct device *dev)
145 {
146 struct platform_device *pdev;
147 struct pbs_dev *pbs;
148
149 struct device_node *pbs_dev_node __free(device_node) = of_parse_phandle(dev->of_node,
150 "qcom,pbs", 0);
151 if (!pbs_dev_node) {
152 dev_err(dev, "Missing qcom,pbs property\n");
153 return ERR_PTR(-ENODEV);
154 }
155
156 pdev = of_find_device_by_node(pbs_dev_node);
157 if (!pdev) {
158 dev_err(dev, "Unable to find PBS dev_node\n");
159 return ERR_PTR(-EPROBE_DEFER);
160 }
161
162 pbs = platform_get_drvdata(pdev);
163 if (!pbs) {
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);
167 }
168
169 pbs->link = device_link_add(dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER);
170 if (!pbs->link) {
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);
174 }
175
176 return pbs;
177 }
178 EXPORT_SYMBOL_GPL(get_pbs_client_device);
179
qcom_pbs_probe(struct platform_device * pdev)180 static int qcom_pbs_probe(struct platform_device *pdev)
181 {
182 struct pbs_dev *pbs;
183 u32 val;
184 int ret;
185
186 pbs = devm_kzalloc(&pdev->dev, sizeof(*pbs), GFP_KERNEL);
187 if (!pbs)
188 return -ENOMEM;
189
190 pbs->dev = &pdev->dev;
191 pbs->regmap = dev_get_regmap(pbs->dev->parent, NULL);
192 if (!pbs->regmap) {
193 dev_err(pbs->dev, "Couldn't get parent's regmap\n");
194 return -EINVAL;
195 }
196
197 ret = device_property_read_u32(pbs->dev, "reg", &val);
198 if (ret < 0) {
199 dev_err(pbs->dev, "Couldn't find reg, ret = %d\n", ret);
200 return ret;
201 }
202 pbs->base = val;
203 mutex_init(&pbs->lock);
204
205 platform_set_drvdata(pdev, pbs);
206
207 return 0;
208 }
209
210 static const struct of_device_id qcom_pbs_match_table[] = {
211 { .compatible = "qcom,pbs" },
212 {}
213 };
214 MODULE_DEVICE_TABLE(of, qcom_pbs_match_table);
215
216 static struct platform_driver qcom_pbs_driver = {
217 .driver = {
218 .name = "qcom-pbs",
219 .of_match_table = qcom_pbs_match_table,
220 },
221 .probe = qcom_pbs_probe,
222 };
223 module_platform_driver(qcom_pbs_driver)
224
225 MODULE_DESCRIPTION("QCOM PBS DRIVER");
226 MODULE_LICENSE("GPL");
227