qcom-pbs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. guard(mutex)(&pbs->lock);
  74. ret = regmap_read(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH2, &val);
  75. if (ret < 0)
  76. return ret;
  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. return ret;
  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. break;
  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. break;
  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. break;
  101. ret = qcom_pbs_wait_for_ack(pbs, bit_pos);
  102. if (ret < 0)
  103. break;
  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. /* Clear all the requested bitmap */
  109. return regmap_update_bits(pbs->regmap, pbs->base + PBS_CLIENT_SCRATCH1, bitmap, 0);
  110. }
  111. EXPORT_SYMBOL_GPL(qcom_pbs_trigger_event);
  112. /**
  113. * get_pbs_client_device() - Get the PBS device used by client
  114. * @dev: Client device
  115. *
  116. * This function is used to get the PBS device that is being
  117. * used by the client.
  118. *
  119. * Return: pbs_dev on success, ERR_PTR on failure
  120. */
  121. struct pbs_dev *get_pbs_client_device(struct device *dev)
  122. {
  123. struct platform_device *pdev;
  124. struct pbs_dev *pbs;
  125. struct device_node *pbs_dev_node __free(device_node) = of_parse_phandle(dev->of_node,
  126. "qcom,pbs", 0);
  127. if (!pbs_dev_node) {
  128. dev_err(dev, "Missing qcom,pbs property\n");
  129. return ERR_PTR(-ENODEV);
  130. }
  131. pdev = of_find_device_by_node(pbs_dev_node);
  132. if (!pdev) {
  133. dev_err(dev, "Unable to find PBS dev_node\n");
  134. return ERR_PTR(-EPROBE_DEFER);
  135. }
  136. pbs = platform_get_drvdata(pdev);
  137. if (!pbs) {
  138. dev_err(dev, "Cannot get pbs instance from %s\n", dev_name(&pdev->dev));
  139. platform_device_put(pdev);
  140. return ERR_PTR(-EPROBE_DEFER);
  141. }
  142. pbs->link = device_link_add(dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER);
  143. if (!pbs->link) {
  144. dev_err(&pdev->dev, "Failed to create device link to consumer %s\n", dev_name(dev));
  145. platform_device_put(pdev);
  146. return ERR_PTR(-EINVAL);
  147. }
  148. platform_device_put(pdev);
  149. return pbs;
  150. }
  151. EXPORT_SYMBOL_GPL(get_pbs_client_device);
  152. static int qcom_pbs_probe(struct platform_device *pdev)
  153. {
  154. struct pbs_dev *pbs;
  155. u32 val;
  156. int ret;
  157. pbs = devm_kzalloc(&pdev->dev, sizeof(*pbs), GFP_KERNEL);
  158. if (!pbs)
  159. return -ENOMEM;
  160. pbs->dev = &pdev->dev;
  161. pbs->regmap = dev_get_regmap(pbs->dev->parent, NULL);
  162. if (!pbs->regmap) {
  163. dev_err(pbs->dev, "Couldn't get parent's regmap\n");
  164. return -EINVAL;
  165. }
  166. ret = device_property_read_u32(pbs->dev, "reg", &val);
  167. if (ret < 0) {
  168. dev_err(pbs->dev, "Couldn't find reg, ret = %d\n", ret);
  169. return ret;
  170. }
  171. pbs->base = val;
  172. mutex_init(&pbs->lock);
  173. platform_set_drvdata(pdev, pbs);
  174. return 0;
  175. }
  176. static const struct of_device_id qcom_pbs_match_table[] = {
  177. { .compatible = "qcom,pbs" },
  178. {}
  179. };
  180. MODULE_DEVICE_TABLE(of, qcom_pbs_match_table);
  181. static struct platform_driver qcom_pbs_driver = {
  182. .driver = {
  183. .name = "qcom-pbs",
  184. .of_match_table = qcom_pbs_match_table,
  185. },
  186. .probe = qcom_pbs_probe,
  187. };
  188. module_platform_driver(qcom_pbs_driver)
  189. MODULE_DESCRIPTION("QCOM PBS DRIVER");
  190. MODULE_LICENSE("GPL");