qti-ocp-notifier.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved. */
  3. #include <linux/err.h>
  4. #include <linux/idr.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/ipc_logging.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/nvmem-consumer.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/string.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/machine.h>
  17. #include "internal.h"
  18. #define OCP_LOG_ENTRY_SIZE 2
  19. #define IPC_LOG_PAGES 3
  20. struct ocp_log_entry {
  21. u16 ppid;
  22. u8 mode_at_ocp;
  23. };
  24. struct nvmem_log_info {
  25. struct nvmem_cell *nvmem_cell;
  26. int entry_count;
  27. void *ipc_log;
  28. };
  29. struct ocp_notifier_dev {
  30. struct device *dev;
  31. struct idr regulators;
  32. struct nvmem_log_info ocp;
  33. struct nvmem_log_info alarm;
  34. };
  35. static const char *rdev_name(struct regulator_dev *rdev)
  36. {
  37. if (rdev->constraints && rdev->constraints->name)
  38. return rdev->constraints->name;
  39. else if (rdev->desc->name)
  40. return rdev->desc->name;
  41. else
  42. return "";
  43. }
  44. #define BUF_SIZE 16
  45. static int ocp_notifier_get_regulator_name(struct ocp_notifier_dev *ocp_dev,
  46. int ppid, const char **name)
  47. {
  48. struct regulator_dev *rdev;
  49. struct regulator *reg;
  50. char buf[BUF_SIZE];
  51. int ret;
  52. rdev = idr_find(&ocp_dev->regulators, ppid);
  53. if (!rdev) {
  54. buf[0] = '\0';
  55. scnprintf(buf, BUF_SIZE, "periph-%03x", ppid);
  56. reg = regulator_get_optional(ocp_dev->dev, buf);
  57. if (IS_ERR(reg)) {
  58. ret = PTR_ERR(reg);
  59. if (ret == -EPROBE_DEFER)
  60. return ret;
  61. /* Ignore case of unspecified supply mapping */
  62. dev_dbg(ocp_dev->dev, "failed to get %s-supply, ret=%d\n",
  63. buf, ret);
  64. } else {
  65. rdev = reg->rdev;
  66. regulator_put(reg);
  67. ret = idr_alloc(&ocp_dev->regulators, rdev, ppid,
  68. ppid + 1, GFP_KERNEL);
  69. if (ret < 0)
  70. return ret;
  71. else if (ret != ppid)
  72. return -EINVAL;
  73. }
  74. }
  75. if (rdev)
  76. *name = rdev_name(rdev);
  77. return 0;
  78. }
  79. static int ocp_notifier_log_event(struct ocp_notifier_dev *ocp_dev,
  80. struct nvmem_log_info *log_info,
  81. struct ocp_log_entry *entry,
  82. const char *label)
  83. {
  84. const char *name = NULL;
  85. int ret;
  86. if (entry->ppid == 0)
  87. return 0;
  88. ret = ocp_notifier_get_regulator_name(ocp_dev, entry->ppid, &name);
  89. if (ret)
  90. return ret;
  91. if (name) {
  92. pr_err_ratelimited("%s name=%s, ppid=0x%03X, mode=%u\n",
  93. label, name, entry->ppid, entry->mode_at_ocp);
  94. ipc_log_string(log_info->ipc_log, "%s name=%s, ppid=0x%03X, mode=%u\n",
  95. label, name, entry->ppid, entry->mode_at_ocp);
  96. } else {
  97. pr_err_ratelimited("%s ppid=0x%03X, mode=%u\n",
  98. label, entry->ppid, entry->mode_at_ocp);
  99. ipc_log_string(log_info->ipc_log, "%s ppid=0x%03X, mode=%u\n",
  100. label, entry->ppid, entry->mode_at_ocp);
  101. }
  102. return 0;
  103. }
  104. static int ocp_notifier_read_entry(struct ocp_notifier_dev *ocp_dev,
  105. struct nvmem_log_info *log_info,
  106. u32 index,
  107. struct ocp_log_entry *entry)
  108. {
  109. size_t len = 0;
  110. u8 *buf;
  111. int ret, i;
  112. if (index >= log_info->entry_count)
  113. return -EINVAL;
  114. buf = nvmem_cell_read(log_info->nvmem_cell, &len);
  115. if (IS_ERR(buf)) {
  116. ret = PTR_ERR(buf);
  117. dev_err(ocp_dev->dev, "failed to read nvmem cell, ret=%d\n", ret);
  118. return ret;
  119. }
  120. i = index * OCP_LOG_ENTRY_SIZE;
  121. if (i + 1 >= len) {
  122. dev_err(ocp_dev->dev, "invalid OCP log index=%i\n", i);
  123. kfree(buf);
  124. return -EINVAL;
  125. }
  126. /*
  127. * OCP log entry layout:
  128. * Byte 0: [7:4] - SID
  129. * [2:0] - mode at OCP
  130. * Byte 1: [7:0] - PID
  131. */
  132. entry->ppid = (((u16)buf[i] << 4) & 0xF00) | buf[i + 1];
  133. entry->mode_at_ocp = buf[i] & 0x7;
  134. kfree(buf);
  135. return 0;
  136. }
  137. static irqreturn_t ocp_notifier_handler(int irq, void *data)
  138. {
  139. struct ocp_notifier_dev *ocp_dev = data;
  140. struct ocp_log_entry entry = {0};
  141. struct regulator_dev *rdev;
  142. int ret;
  143. ret = ocp_notifier_read_entry(ocp_dev, &ocp_dev->ocp, 0, &entry);
  144. if (ret)
  145. goto done;
  146. ret = ocp_notifier_log_event(ocp_dev, &ocp_dev->ocp, &entry,
  147. "Regulator OCP during runtime:");
  148. if (ret)
  149. goto done;
  150. rdev = idr_find(&ocp_dev->regulators, entry.ppid);
  151. if (!rdev)
  152. goto done;
  153. regulator_notifier_call_chain(rdev, REGULATOR_EVENT_OVER_CURRENT, NULL);
  154. done:
  155. return IRQ_HANDLED;
  156. }
  157. static irqreturn_t alarm_notifier_handler(int irq, void *data)
  158. {
  159. struct ocp_notifier_dev *ocp_dev = data;
  160. struct ocp_log_entry entry = {0};
  161. struct regulator_dev *rdev;
  162. int ret;
  163. ret = ocp_notifier_read_entry(ocp_dev, &ocp_dev->alarm, 0, &entry);
  164. if (ret)
  165. goto done;
  166. ret = ocp_notifier_log_event(ocp_dev, &ocp_dev->alarm, &entry,
  167. "Regulator alarm during runtime:");
  168. if (ret)
  169. goto done;
  170. rdev = idr_find(&ocp_dev->regulators, entry.ppid);
  171. if (!rdev)
  172. goto done;
  173. regulator_notifier_call_chain(rdev, REGULATOR_EVENT_UNDER_VOLTAGE,
  174. NULL);
  175. done:
  176. return IRQ_HANDLED;
  177. }
  178. static int alarm_notifier_init(struct platform_device *pdev,
  179. struct ocp_notifier_dev *ocp_dev)
  180. {
  181. struct ocp_log_entry entry = {0};
  182. size_t len = 0;
  183. int ret, i, irq;
  184. u8 *buf;
  185. ocp_dev->alarm.nvmem_cell = devm_nvmem_cell_get(&pdev->dev,
  186. "alarm_log");
  187. if (IS_ERR(ocp_dev->alarm.nvmem_cell)) {
  188. ret = PTR_ERR(ocp_dev->alarm.nvmem_cell);
  189. if (ret == -EPROBE_DEFER)
  190. return ret;
  191. /* Alarm event details are optional */
  192. ocp_dev->alarm.nvmem_cell = NULL;
  193. return 0;
  194. }
  195. irq = platform_get_irq(pdev, 1);
  196. if (irq < 0) {
  197. dev_err(&pdev->dev, "failed to get alarm irq, ret=%d\n", irq);
  198. return irq;
  199. }
  200. ocp_dev->alarm.ipc_log = ipc_log_context_create(IPC_LOG_PAGES,
  201. "regulator_alarm", 0);
  202. buf = nvmem_cell_read(ocp_dev->alarm.nvmem_cell, &len);
  203. if (IS_ERR(buf)) {
  204. ret = PTR_ERR(buf);
  205. dev_err(&pdev->dev, "failed to read alarm nvmem cell, ret=%d\n", ret);
  206. goto free_log;
  207. }
  208. ocp_dev->alarm.entry_count = len / OCP_LOG_ENTRY_SIZE;
  209. kfree(buf);
  210. for (i = 0; i < ocp_dev->alarm.entry_count; i++) {
  211. ret = ocp_notifier_read_entry(ocp_dev, &ocp_dev->alarm, i,
  212. &entry);
  213. if (ret)
  214. goto free_log;
  215. ret = ocp_notifier_log_event(ocp_dev, &ocp_dev->alarm, &entry,
  216. "Regulator alarm event before kernel boot:");
  217. if (ret) {
  218. if (ret != -EPROBE_DEFER)
  219. dev_err(&pdev->dev, "failed to log alarm entry %d, ret=%d\n",
  220. i, ret);
  221. goto free_log;
  222. }
  223. }
  224. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  225. alarm_notifier_handler, IRQF_ONESHOT,
  226. "regulator-alarm", ocp_dev);
  227. if (ret)
  228. goto free_log;
  229. return 0;
  230. free_log:
  231. ipc_log_context_destroy(ocp_dev->alarm.ipc_log);
  232. return ret;
  233. }
  234. static int ocp_notifier_probe(struct platform_device *pdev)
  235. {
  236. struct ocp_notifier_dev *ocp_dev;
  237. struct ocp_log_entry entry = {0};
  238. size_t len = 0;
  239. int ret, i, irq;
  240. u8 *buf;
  241. ocp_dev = devm_kzalloc(&pdev->dev, sizeof(*ocp_dev), GFP_KERNEL);
  242. if (!ocp_dev)
  243. return -ENOMEM;
  244. ocp_dev->dev = &pdev->dev;
  245. ocp_dev->ocp.nvmem_cell = devm_nvmem_cell_get(&pdev->dev, "ocp_log");
  246. if (IS_ERR(ocp_dev->ocp.nvmem_cell)) {
  247. ret = PTR_ERR(ocp_dev->ocp.nvmem_cell);
  248. if (ret != -EPROBE_DEFER)
  249. dev_err(&pdev->dev, "failed to get nvmem cell, ret=%d\n",
  250. ret);
  251. return ret;
  252. }
  253. irq = platform_get_irq(pdev, 0);
  254. if (irq < 0) {
  255. dev_err(&pdev->dev, "failed to get OCP irq, ret=%d\n", irq);
  256. return irq;
  257. }
  258. ocp_dev->ocp.ipc_log = ipc_log_context_create(IPC_LOG_PAGES,
  259. "regulator_ocp", 0);
  260. platform_set_drvdata(pdev, ocp_dev);
  261. buf = nvmem_cell_read(ocp_dev->ocp.nvmem_cell, &len);
  262. if (IS_ERR(buf)) {
  263. ret = PTR_ERR(buf);
  264. dev_err(&pdev->dev, "failed to read OCP nvmem cell, ret=%d\n", ret);
  265. goto free_log;
  266. }
  267. ocp_dev->ocp.entry_count = len / OCP_LOG_ENTRY_SIZE;
  268. kfree(buf);
  269. idr_init(&ocp_dev->regulators);
  270. for (i = 0; i < ocp_dev->ocp.entry_count; i++) {
  271. ret = ocp_notifier_read_entry(ocp_dev, &ocp_dev->ocp, i,
  272. &entry);
  273. if (ret)
  274. goto free_idr;
  275. ret = ocp_notifier_log_event(ocp_dev, &ocp_dev->ocp, &entry,
  276. "Regulator OCP event before kernel boot:");
  277. if (ret) {
  278. if (ret != -EPROBE_DEFER)
  279. dev_err(&pdev->dev, "failed to log OCP entry %d, ret=%d\n",
  280. i, ret);
  281. goto free_idr;
  282. }
  283. }
  284. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  285. ocp_notifier_handler, IRQF_ONESHOT,
  286. "regulator-ocp", ocp_dev);
  287. if (ret)
  288. goto free_idr;
  289. ret = alarm_notifier_init(pdev, ocp_dev);
  290. if (ret)
  291. goto free_idr;
  292. return 0;
  293. free_idr:
  294. idr_destroy(&ocp_dev->regulators);
  295. free_log:
  296. ipc_log_context_destroy(ocp_dev->ocp.ipc_log);
  297. return ret;
  298. }
  299. static int ocp_notifier_remove(struct platform_device *pdev)
  300. {
  301. struct ocp_notifier_dev *ocp_dev = platform_get_drvdata(pdev);
  302. ipc_log_context_destroy(ocp_dev->ocp.ipc_log);
  303. ipc_log_context_destroy(ocp_dev->alarm.ipc_log);
  304. idr_destroy(&ocp_dev->regulators);
  305. return 0;
  306. }
  307. static const struct of_device_id ocp_notifier_of_match[] = {
  308. { .compatible = "qcom,regulator-ocp-notifier" },
  309. {}
  310. };
  311. MODULE_DEVICE_TABLE(of, ocp_notifier_of_match);
  312. static struct platform_driver ocp_notifier_driver = {
  313. .driver = {
  314. .name = "qti-ocp-notifier",
  315. .of_match_table = of_match_ptr(ocp_notifier_of_match),
  316. },
  317. .probe = ocp_notifier_probe,
  318. .remove = ocp_notifier_remove,
  319. };
  320. module_platform_driver(ocp_notifier_driver);
  321. MODULE_DESCRIPTION("QTI Regulator OCP Notifier Driver");
  322. MODULE_LICENSE("GPL v2");