qpnp-pbs.c 9.0 KB

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