qcom-pbs.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/cleanup.h>
  6. #include <linux/delay.h>
  7. #include <linux/err.h>
  8. #include <linux/module.h>
  9. #include <linux/of.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>
  15. #define PBS_CLIENT_TRIG_CTL 0x42
  16. #define PBS_CLIENT_SW_TRIG_BIT BIT(7)
  17. #define PBS_CLIENT_SCRATCH1 0x50
  18. #define PBS_CLIENT_SCRATCH2 0x51
  19. #define PBS_CLIENT_SCRATCH2_ERROR 0xFF
  20. #define RETRIES 2000
  21. #define DELAY 1100
  22. struct pbs_dev {
  23. struct device *dev;
  24. struct regmap *regmap;
  25. struct mutex lock;
  26. struct device_link *link;
  27. u32 base;
  28. };
  29. static int qcom_pbs_wait_for_ack(struct pbs_dev *pbs, u8 bit_pos)
  30. {
  31. unsigned int val;
  32. int ret;
  33. ret = regmap_read_poll_timeout(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2,
  34. val, val & BIT(bit_pos), DELAY, DELAY * RETRIES);
  35. if (ret < 0) {
  36. dev_err(pbs->dev, "Timeout for PBS ACK/NACK for bit %u\n", bit_pos);
  37. return -ETIMEDOUT;
  38. }
  39. if (val == PBS_CLIENT_SCRATCH2_ERROR) {
  40. ret = regmap_write(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, 0);
  41. dev_err(pbs->dev, "NACK from PBS for bit %u\n", bit_pos);
  42. return -EINVAL;
  43. }
  44. dev_dbg(pbs->dev, "PBS sequence for bit %u executed!\n", bit_pos);
  45. return 0;
  46. }
  47. /**
  48. * qcom_pbs_trigger_event() - Trigger the PBS RAM sequence
  49. * @pbs: Pointer to PBS device
  50. * @bitmap: bitmap
  51. *
  52. * This function is used to trigger the PBS RAM sequence to be
  53. * executed by the client driver.
  54. *
  55. * The PBS trigger sequence involves
  56. * 1. setting the PBS sequence bit in PBS_CLIENT_SCRATCH1
  57. * 2. Initiating the SW PBS trigger
  58. * 3. Checking the equivalent bit in PBS_CLIENT_SCRATCH2 for the
  59. * completion of the sequence.
  60. * 4. If PBS_CLIENT_SCRATCH2 == 0xFF, the PBS sequence failed to execute
  61. *
  62. * Return: 0 on success, < 0 on failure
  63. */
  64. int qcom_pbs_trigger_event(struct pbs_dev *pbs, u8 bitmap)
  65. {
  66. unsigned int val;
  67. u16 bit_pos;
  68. int ret;
  69. if (WARN_ON(!bitmap))
  70. return -EINVAL;
  71. if (IS_ERR_OR_NULL(pbs))
  72. return -EINVAL;
  73. mutex_lock(&pbs->lock);
  74. ret = regmap_read(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, &val);
  75. if (ret < 0)
  76. goto out;
  77. if (val == PBS_CLIENT_SCRATCH2_ERROR) {
  78. /* PBS error - clear SCRATCH2 register */
  79. ret = regmap_write(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, 0);
  80. if (ret < 0)
  81. goto out;
  82. }
  83. for (bit_pos = 0; bit_pos < 8; bit_pos++) {
  84. if (!(bitmap & BIT(bit_pos)))
  85. continue;
  86. /* Clear the PBS sequence bit position */
  87. ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2,
  88. BIT(bit_pos), 0);
  89. if (ret < 0)
  90. goto out_clear_scratch1;
  91. /* Set the PBS sequence bit position */
  92. ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH1,
  93. BIT(bit_pos), BIT(bit_pos));
  94. if (ret < 0)
  95. goto out_clear_scratch1;
  96. /* Initiate the SW trigger */
  97. ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_TRIG_CTL,
  98. PBS_CLIENT_SW_TRIG_BIT, PBS_CLIENT_SW_TRIG_BIT);
  99. if (ret < 0)
  100. goto out_clear_scratch1;
  101. ret = qcom_pbs_wait_for_ack(pbs, bit_pos);
  102. if (ret < 0)
  103. goto out_clear_scratch1;
  104. /* Clear the PBS sequence bit position */
  105. regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH1, BIT(bit_pos), 0);
  106. regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, BIT(bit_pos), 0);
  107. }
  108. out_clear_scratch1:
  109. /* Clear all the requested bitmap */
  110. ret = regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH1, bitmap, 0);
  111. out:
  112. mutex_unlock(&pbs->lock);
  113. return ret;
  114. }
  115. EXPORT_SYMBOL_GPL(qcom_pbs_trigger_event);
  116. /**
  117. * get_pbs_client_device() - Get the PBS device used by client
  118. * @dev: Client device
  119. *
  120. * This function is used to get the PBS device that is being
  121. * used by the client.
  122. *
  123. * Return: pbs_dev on success, ERR_PTR on failure
  124. */
  125. struct pbs_dev *get_pbs_client_device(struct device *dev)
  126. {
  127. struct platform_device *pdev;
  128. struct pbs_dev *pbs;
  129. struct device_node *pbs_dev_node __free(device_node) = of_parse_phandle(dev->of_node,
  130. "qcom,pbs", 0);
  131. if (!pbs_dev_node) {
  132. dev_err(dev, "Missing qcom,pbs property\n");
  133. return ERR_PTR(-ENODEV);
  134. }
  135. pdev = of_find_device_by_node(pbs_dev_node);
  136. if (!pdev) {
  137. dev_err(dev, "Unable to find PBS dev_node\n");
  138. return ERR_PTR(-EPROBE_DEFER);
  139. }
  140. pbs = platform_get_drvdata(pdev);
  141. if (!pbs) {
  142. dev_err(dev, "Cannot get pbs instance from %s\n", dev_name(&pdev->dev));
  143. platform_device_put(pdev);
  144. return ERR_PTR(-EPROBE_DEFER);
  145. }
  146. pbs->link = device_link_add(dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER);
  147. if (!pbs->link) {
  148. dev_err(&pdev->dev, "Failed to create device link to consumer %s\n", dev_name(dev));
  149. platform_device_put(pdev);
  150. return ERR_PTR(-EINVAL);
  151. }
  152. return pbs;
  153. }
  154. EXPORT_SYMBOL_GPL(get_pbs_client_device);
  155. static int qcom_pbs_probe(struct platform_device *pdev)
  156. {
  157. struct pbs_dev *pbs;
  158. u32 val;
  159. int ret;
  160. pbs = devm_kzalloc(&pdev->dev, sizeof(*pbs), GFP_KERNEL);
  161. if (!pbs)
  162. return -ENOMEM;
  163. pbs->dev = &pdev->dev;
  164. pbs->regmap = dev_get_regmap(pbs->dev->parent, NULL);
  165. if (!pbs->regmap) {
  166. dev_err(pbs->dev, "Couldn't get parent's regmap\n");
  167. return -EINVAL;
  168. }
  169. ret = device_property_read_u32(pbs->dev, "reg", &val);
  170. if (ret < 0) {
  171. dev_err(pbs->dev, "Couldn't find reg, ret = %d\n", ret);
  172. return ret;
  173. }
  174. pbs->base = val;
  175. mutex_init(&pbs->lock);
  176. platform_set_drvdata(pdev, pbs);
  177. return 0;
  178. }
  179. static const struct of_device_id qcom_pbs_match_table[] = {
  180. { .compatible = "qcom,pbs" },
  181. {}
  182. };
  183. MODULE_DEVICE_TABLE(of, qcom_pbs_match_table);
  184. static struct platform_driver qcom_pbs_driver = {
  185. .driver = {
  186. .name = "qcom-pbs",
  187. .of_match_table = qcom_pbs_match_table,
  188. },
  189. .probe = qcom_pbs_probe,
  190. };
  191. module_platform_driver(qcom_pbs_driver)
  192. MODULE_DESCRIPTION("QCOM PBS DRIVER");
  193. MODULE_LICENSE("GPL");