| 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 | |
| 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(map: pbs->regmap, reg: pbs->base + PBS_CLIENT_SCRATCH2, val: 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 | */ |
| 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(ptr: pbs)) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | guard(mutex)(T: &pbs->lock); |
| 88 | ret = regmap_read(map: pbs->regmap, reg: pbs->base + PBS_CLIENT_SCRATCH2, val: &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(map: pbs->regmap, reg: pbs->base + PBS_CLIENT_SCRATCH2, val: 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(map: pbs->regmap, reg: pbs->base + PBS_CLIENT_SCRATCH2, |
| 105 | BIT(bit_pos), val: 0); |
| 106 | if (ret < 0) |
| 107 | break; |
| 108 | |
| 109 | /* Set the PBS sequence bit position */ |
| 110 | ret = regmap_update_bits(map: pbs->regmap, reg: 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(map: pbs->regmap, reg: 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(map: pbs->regmap, reg: pbs->base + PBS_CLIENT_SCRATCH1, BIT(bit_pos), val: 0); |
| 127 | regmap_update_bits(map: pbs->regmap, reg: pbs->base + PBS_CLIENT_SCRATCH2, BIT(bit_pos), val: 0); |
| 128 | } |
| 129 | |
| 130 | /* Clear all the requested bitmap */ |
| 131 | return regmap_update_bits(map: pbs->regmap, reg: pbs->base + PBS_CLIENT_SCRATCH1, mask: bitmap, val: 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 | */ |
| 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(np: dev->of_node, |
| 150 | phandle_name: "qcom,pbs" , index: 0); |
| 151 | if (!pbs_dev_node) { |
| 152 | dev_err(dev, "Missing qcom,pbs property\n" ); |
| 153 | return ERR_PTR(error: -ENODEV); |
| 154 | } |
| 155 | |
| 156 | pdev = of_find_device_by_node(np: pbs_dev_node); |
| 157 | if (!pdev) { |
| 158 | dev_err(dev, "Unable to find PBS dev_node\n" ); |
| 159 | return ERR_PTR(error: -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(error: -EPROBE_DEFER); |
| 167 | } |
| 168 | |
| 169 | pbs->link = device_link_add(consumer: dev, supplier: &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(error: -EINVAL); |
| 174 | } |
| 175 | |
| 176 | platform_device_put(pdev); |
| 177 | |
| 178 | return pbs; |
| 179 | } |
| 180 | EXPORT_SYMBOL_GPL(get_pbs_client_device); |
| 181 | |
| 182 | static int qcom_pbs_probe(struct platform_device *pdev) |
| 183 | { |
| 184 | struct pbs_dev *pbs; |
| 185 | u32 val; |
| 186 | int ret; |
| 187 | |
| 188 | pbs = devm_kzalloc(dev: &pdev->dev, size: sizeof(*pbs), GFP_KERNEL); |
| 189 | if (!pbs) |
| 190 | return -ENOMEM; |
| 191 | |
| 192 | pbs->dev = &pdev->dev; |
| 193 | pbs->regmap = dev_get_regmap(dev: pbs->dev->parent, NULL); |
| 194 | if (!pbs->regmap) { |
| 195 | dev_err(pbs->dev, "Couldn't get parent's regmap\n" ); |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | |
| 199 | ret = device_property_read_u32(dev: pbs->dev, propname: "reg" , val: &val); |
| 200 | if (ret < 0) { |
| 201 | dev_err(pbs->dev, "Couldn't find reg, ret = %d\n" , ret); |
| 202 | return ret; |
| 203 | } |
| 204 | pbs->base = val; |
| 205 | mutex_init(&pbs->lock); |
| 206 | |
| 207 | platform_set_drvdata(pdev, data: pbs); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static const struct of_device_id qcom_pbs_match_table[] = { |
| 213 | { .compatible = "qcom,pbs" }, |
| 214 | {} |
| 215 | }; |
| 216 | MODULE_DEVICE_TABLE(of, qcom_pbs_match_table); |
| 217 | |
| 218 | static struct platform_driver qcom_pbs_driver = { |
| 219 | .driver = { |
| 220 | .name = "qcom-pbs" , |
| 221 | .of_match_table = qcom_pbs_match_table, |
| 222 | }, |
| 223 | .probe = qcom_pbs_probe, |
| 224 | }; |
| 225 | module_platform_driver(qcom_pbs_driver) |
| 226 | |
| 227 | MODULE_DESCRIPTION("QCOM PBS DRIVER" ); |
| 228 | MODULE_LICENSE("GPL" ); |
| 229 | |