Kiran Gunda | 04967c8 | 2017-02-14 14:22:18 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "PBS: %s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/spmi.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/regmap.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/qpnp/qpnp-pbs.h> |
| 24 | |
| 25 | #define QPNP_PBS_DEV_NAME "qcom,qpnp-pbs" |
| 26 | |
| 27 | #define PBS_CLIENT_TRIG_CTL 0x42 |
| 28 | #define PBS_CLIENT_SW_TRIG_BIT BIT(7) |
| 29 | #define PBS_CLIENT_SCRATCH1 0x50 |
| 30 | #define PBS_CLIENT_SCRATCH2 0x51 |
| 31 | |
| 32 | static LIST_HEAD(pbs_dev_list); |
| 33 | static DEFINE_MUTEX(pbs_list_lock); |
| 34 | |
| 35 | struct qpnp_pbs { |
| 36 | struct platform_device *pdev; |
| 37 | struct device *dev; |
| 38 | struct device_node *dev_node; |
| 39 | struct regmap *regmap; |
| 40 | struct mutex pbs_lock; |
| 41 | struct list_head link; |
| 42 | |
| 43 | u32 base; |
| 44 | }; |
| 45 | |
| 46 | static int qpnp_pbs_read(struct qpnp_pbs *pbs, u32 address, |
| 47 | u8 *val, int count) |
| 48 | { |
| 49 | int rc = 0; |
| 50 | struct platform_device *pdev = pbs->pdev; |
| 51 | |
| 52 | rc = regmap_bulk_read(pbs->regmap, address, val, count); |
| 53 | if (rc) |
| 54 | pr_err("Failed to read address=0x%02x sid=0x%02x rc=%d\n", |
| 55 | address, to_spmi_device(pdev->dev.parent)->usid, rc); |
| 56 | |
| 57 | return rc; |
| 58 | } |
| 59 | |
| 60 | static int qpnp_pbs_write(struct qpnp_pbs *pbs, u16 address, |
| 61 | u8 *val, int count) |
| 62 | { |
| 63 | int rc = 0; |
| 64 | struct platform_device *pdev = pbs->pdev; |
| 65 | |
| 66 | rc = regmap_bulk_write(pbs->regmap, address, val, count); |
| 67 | if (rc < 0) |
| 68 | pr_err("Failed to write address =0x%02x sid=0x%02x rc=%d\n", |
| 69 | address, to_spmi_device(pdev->dev.parent)->usid, rc); |
| 70 | else |
| 71 | pr_debug("Wrote 0x%02X to addr 0x%04x\n", *val, address); |
| 72 | |
| 73 | return rc; |
| 74 | } |
| 75 | |
| 76 | static int qpnp_pbs_masked_write(struct qpnp_pbs *pbs, u16 address, |
| 77 | u8 mask, u8 val) |
| 78 | { |
| 79 | int rc; |
| 80 | |
| 81 | rc = regmap_update_bits(pbs->regmap, address, mask, val); |
| 82 | if (rc < 0) |
| 83 | pr_err("Failed to write address 0x%04X, rc = %d\n", |
| 84 | address, rc); |
| 85 | else |
| 86 | pr_debug("Wrote 0x%02X to addr 0x%04X\n", |
| 87 | val, address); |
| 88 | |
| 89 | return rc; |
| 90 | } |
| 91 | |
| 92 | static struct qpnp_pbs *get_pbs_client_node(struct device_node *dev_node) |
| 93 | { |
| 94 | struct qpnp_pbs *pbs; |
| 95 | |
| 96 | mutex_lock(&pbs_list_lock); |
| 97 | list_for_each_entry(pbs, &pbs_dev_list, link) { |
| 98 | if (dev_node == pbs->dev_node) { |
| 99 | mutex_unlock(&pbs_list_lock); |
| 100 | return pbs; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | mutex_unlock(&pbs_list_lock); |
| 105 | return ERR_PTR(-EINVAL); |
| 106 | } |
| 107 | |
| 108 | static int qpnp_pbs_wait_for_ack(struct qpnp_pbs *pbs, u8 bit_pos) |
| 109 | { |
| 110 | int rc = 0; |
| 111 | u16 retries = 2000, dly = 1000; |
| 112 | u8 val; |
| 113 | |
| 114 | while (retries--) { |
| 115 | rc = qpnp_pbs_read(pbs, pbs->base + |
| 116 | PBS_CLIENT_SCRATCH2, &val, 1); |
| 117 | if (rc < 0) { |
| 118 | pr_err("Failed to read register %x rc = %d\n", |
| 119 | PBS_CLIENT_SCRATCH2, rc); |
| 120 | return rc; |
| 121 | } |
| 122 | |
| 123 | if (val == 0xFF) { |
| 124 | /* PBS error - clear SCRATCH2 register */ |
| 125 | rc = qpnp_pbs_write(pbs, pbs->base + |
| 126 | PBS_CLIENT_SCRATCH2, 0, 1); |
| 127 | if (rc < 0) { |
| 128 | pr_err("Failed to clear register %x rc=%d\n", |
| 129 | PBS_CLIENT_SCRATCH2, rc); |
| 130 | return rc; |
| 131 | } |
| 132 | |
| 133 | pr_err("NACK from PBS for bit %d\n", bit_pos); |
| 134 | return -EINVAL; |
| 135 | } |
| 136 | |
| 137 | if (val & BIT(bit_pos)) { |
| 138 | pr_debug("PBS sequence for bit %d executed!\n", |
| 139 | bit_pos); |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | usleep_range(dly, dly + 100); |
| 144 | } |
| 145 | |
| 146 | if (!retries) { |
| 147 | pr_err("Timeout for PBS ACK/NACK for bit %d\n", bit_pos); |
| 148 | return -ETIMEDOUT; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * qpnp_pbs_trigger_event - Trigger the PBS RAM sequence |
| 156 | * |
| 157 | * Returns = 0 If the PBS RAM sequence executed successfully. |
| 158 | * |
| 159 | * Returns < 0 for errors. |
| 160 | * |
| 161 | * This function is used to trigger the PBS RAM sequence to be |
| 162 | * executed by the client driver. |
| 163 | * |
| 164 | * The PBS trigger sequence involves |
| 165 | * 1. setting the PBS sequence bit in PBS_CLIENT_SCRATCH1 |
| 166 | * 2. Initiating the SW PBS trigger |
| 167 | * 3. Checking the equivalent bit in PBS_CLIENT_SCRATCH2 for the |
| 168 | * completion of the sequence. |
| 169 | * 4. If PBS_CLIENT_SCRATCH2 == 0xFF, the PBS sequence failed to execute |
| 170 | */ |
| 171 | int qpnp_pbs_trigger_event(struct device_node *dev_node, u8 bitmap) |
| 172 | { |
| 173 | struct qpnp_pbs *pbs; |
| 174 | int rc = 0; |
| 175 | u16 bit_pos = 0; |
| 176 | u8 val, mask = 0; |
| 177 | |
| 178 | if (!dev_node) |
| 179 | return -EINVAL; |
| 180 | |
| 181 | if (!bitmap) { |
| 182 | pr_err("Invalid bitmap passed by client\n"); |
| 183 | return -EINVAL; |
| 184 | } |
| 185 | |
| 186 | pbs = get_pbs_client_node(dev_node); |
| 187 | if (IS_ERR_OR_NULL(pbs)) { |
| 188 | pr_err("Unable to find the PBS dev_node\n"); |
| 189 | return -EINVAL; |
| 190 | } |
| 191 | |
| 192 | mutex_lock(&pbs->pbs_lock); |
| 193 | rc = qpnp_pbs_read(pbs, pbs->base + PBS_CLIENT_SCRATCH2, &val, 1); |
| 194 | if (rc < 0) { |
| 195 | pr_err("read register %x failed rc = %d\n", |
| 196 | PBS_CLIENT_SCRATCH2, rc); |
| 197 | goto out; |
| 198 | } |
| 199 | |
| 200 | if (val == 0xFF) { |
| 201 | /* PBS error - clear SCRATCH2 register */ |
| 202 | rc = qpnp_pbs_write(pbs, pbs->base + PBS_CLIENT_SCRATCH2, 0, 1); |
| 203 | if (rc < 0) { |
| 204 | pr_err("Failed to clear register %x rc=%d\n", |
| 205 | PBS_CLIENT_SCRATCH2, rc); |
| 206 | goto out; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | for (bit_pos = 0; bit_pos < 8; bit_pos++) { |
| 211 | if (bitmap & BIT(bit_pos)) { |
| 212 | /* |
| 213 | * Clear the PBS sequence bit position in |
| 214 | * PBS_CLIENT_SCRATCH2 mask register. |
| 215 | */ |
| 216 | rc = qpnp_pbs_masked_write(pbs, pbs->base + |
| 217 | PBS_CLIENT_SCRATCH2, BIT(bit_pos), 0); |
| 218 | if (rc < 0) { |
| 219 | pr_err("Failed to clear %x reg bit rc=%d\n", |
| 220 | PBS_CLIENT_SCRATCH2, rc); |
| 221 | goto error; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Set the PBS sequence bit position in |
| 226 | * PBS_CLIENT_SCRATCH1 register. |
| 227 | */ |
| 228 | val = mask = BIT(bit_pos); |
| 229 | rc = qpnp_pbs_masked_write(pbs, pbs->base + |
| 230 | PBS_CLIENT_SCRATCH1, mask, val); |
| 231 | if (rc < 0) { |
| 232 | pr_err("Failed to set %x reg bit rc=%d\n", |
| 233 | PBS_CLIENT_SCRATCH1, rc); |
| 234 | goto error; |
| 235 | } |
| 236 | |
| 237 | /* Initiate the SW trigger */ |
| 238 | val = mask = PBS_CLIENT_SW_TRIG_BIT; |
| 239 | rc = qpnp_pbs_masked_write(pbs, pbs->base + |
| 240 | PBS_CLIENT_TRIG_CTL, mask, val); |
| 241 | if (rc < 0) { |
| 242 | pr_err("Failed to write register %x rc=%d\n", |
| 243 | PBS_CLIENT_TRIG_CTL, rc); |
| 244 | goto error; |
| 245 | } |
| 246 | |
| 247 | rc = qpnp_pbs_wait_for_ack(pbs, bit_pos); |
| 248 | if (rc < 0) { |
| 249 | pr_err("Error during wait_for_ack\n"); |
| 250 | goto error; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Clear the PBS sequence bit position in |
| 255 | * PBS_CLIENT_SCRATCH1 register. |
| 256 | */ |
| 257 | rc = qpnp_pbs_masked_write(pbs, pbs->base + |
| 258 | PBS_CLIENT_SCRATCH1, BIT(bit_pos), 0); |
| 259 | if (rc < 0) { |
| 260 | pr_err("Failed to clear %x reg bit rc=%d\n", |
| 261 | PBS_CLIENT_SCRATCH1, rc); |
| 262 | goto error; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Clear the PBS sequence bit position in |
| 267 | * PBS_CLIENT_SCRATCH2 mask register. |
| 268 | */ |
| 269 | rc = qpnp_pbs_masked_write(pbs, pbs->base + |
| 270 | PBS_CLIENT_SCRATCH2, BIT(bit_pos), 0); |
| 271 | if (rc < 0) { |
| 272 | pr_err("Failed to clear %x reg bit rc=%d\n", |
| 273 | PBS_CLIENT_SCRATCH2, rc); |
| 274 | goto error; |
| 275 | } |
| 276 | |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | error: |
| 281 | /* Clear all the requested bitmap */ |
| 282 | rc = qpnp_pbs_masked_write(pbs, pbs->base + PBS_CLIENT_SCRATCH1, |
| 283 | bitmap, 0); |
| 284 | if (rc < 0) |
| 285 | pr_err("Failed to clear %x reg bit rc=%d\n", |
| 286 | PBS_CLIENT_SCRATCH1, rc); |
| 287 | out: |
| 288 | mutex_unlock(&pbs->pbs_lock); |
| 289 | |
| 290 | return rc; |
| 291 | } |
| 292 | EXPORT_SYMBOL(qpnp_pbs_trigger_event); |
| 293 | |
| 294 | static int qpnp_pbs_probe(struct platform_device *pdev) |
| 295 | { |
| 296 | int rc = 0; |
| 297 | u32 val = 0; |
| 298 | struct qpnp_pbs *pbs; |
| 299 | |
| 300 | pbs = devm_kzalloc(&pdev->dev, sizeof(*pbs), GFP_KERNEL); |
| 301 | if (!pbs) |
| 302 | return -ENOMEM; |
| 303 | |
| 304 | pbs->pdev = pdev; |
| 305 | pbs->dev = &pdev->dev; |
| 306 | pbs->dev_node = pdev->dev.of_node; |
| 307 | pbs->regmap = dev_get_regmap(pdev->dev.parent, NULL); |
| 308 | if (!pbs->regmap) { |
| 309 | dev_err(&pdev->dev, "Couldn't get parent's regmap\n"); |
| 310 | return -EINVAL; |
| 311 | } |
| 312 | |
| 313 | rc = of_property_read_u32(pdev->dev.of_node, "reg", &val); |
| 314 | if (rc < 0) { |
| 315 | dev_err(&pdev->dev, |
| 316 | "Couldn't find reg in node = %s rc = %d\n", |
| 317 | pdev->dev.of_node->full_name, rc); |
| 318 | return rc; |
| 319 | } |
| 320 | |
| 321 | pbs->base = val; |
| 322 | mutex_init(&pbs->pbs_lock); |
| 323 | |
| 324 | dev_set_drvdata(&pdev->dev, pbs); |
| 325 | |
| 326 | mutex_lock(&pbs_list_lock); |
| 327 | list_add(&pbs->link, &pbs_dev_list); |
| 328 | mutex_unlock(&pbs_list_lock); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static const struct of_device_id qpnp_pbs_match_table[] = { |
| 334 | { .compatible = QPNP_PBS_DEV_NAME }, |
| 335 | {} |
| 336 | }; |
| 337 | |
| 338 | static struct platform_driver qpnp_pbs_driver = { |
| 339 | .driver = { |
| 340 | .name = QPNP_PBS_DEV_NAME, |
| 341 | .owner = THIS_MODULE, |
| 342 | .of_match_table = qpnp_pbs_match_table, |
| 343 | }, |
| 344 | .probe = qpnp_pbs_probe, |
| 345 | }; |
| 346 | |
| 347 | static int __init qpnp_pbs_init(void) |
| 348 | { |
| 349 | return platform_driver_register(&qpnp_pbs_driver); |
| 350 | } |
| 351 | arch_initcall(qpnp_pbs_init); |
| 352 | |
| 353 | static void __exit qpnp_pbs_exit(void) |
| 354 | { |
| 355 | return platform_driver_unregister(&qpnp_pbs_driver); |
| 356 | } |
| 357 | module_exit(qpnp_pbs_exit); |
| 358 | |
| 359 | MODULE_DESCRIPTION("QPNP PBS DRIVER"); |
| 360 | MODULE_LICENSE("GPL v2"); |
| 361 | MODULE_ALIAS("platform:" QPNP_PBS_DEV_NAME); |