sec_ap_pmic.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/sec_class.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/gpio.h>
  22. #include <linux/device.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/sec_debug.h>
  25. #include <linux/seq_file.h>
  26. #if IS_ENABLED(CONFIG_SEC_CRASHKEY_LONG)
  27. #include <linux/samsung/debug/sec_crashkey_long.h>
  28. #endif
  29. #include <linux/mfd/sec_ap_pmic.h>
  30. #include <trace/events/power.h>
  31. static struct device *sec_ap_pmic_dev;
  32. static struct sec_ap_pmic_info *sec_ap_pmic_data;
  33. static ssize_t manual_reset_show(struct device *in_dev,
  34. struct device_attribute *attr, char *buf)
  35. {
  36. int ret = 0;
  37. ret = sec_get_s2_reset(SEC_PON_KPDPWR_RESIN);
  38. pr_info("%s: ret=%d\n", __func__, ret);
  39. return sprintf(buf, "%d\n", !ret);
  40. }
  41. static ssize_t manual_reset_store(struct device *in_dev,
  42. struct device_attribute *attr, const char *buf, size_t len)
  43. {
  44. int onoff = 0;
  45. if (kstrtoint(buf, 10, &onoff))
  46. return -EINVAL;
  47. pr_info("%s: onoff=%d\n", __func__, onoff);
  48. #if IS_ENABLED(CONFIG_SEC_CRASHKEY_LONG)
  49. if (onoff)
  50. sec_crashkey_long_connect_to_input_evnet();
  51. else
  52. sec_crashkey_long_disconnect_from_input_event();
  53. #endif
  54. return len;
  55. }
  56. static DEVICE_ATTR_RW(manual_reset);
  57. static ssize_t wake_enabled_show(struct device *in_dev,
  58. struct device_attribute *attr, char *buf)
  59. {
  60. return sprintf(buf, "%d\n", (sec_get_pm_key_wk_init(SEC_PON_KPDPWR) && sec_get_pm_key_wk_init(SEC_PON_RESIN)) ? 1 : 0);
  61. }
  62. static ssize_t wake_enabled_store(struct device *in_dev,
  63. struct device_attribute *attr, const char *buf, size_t len)
  64. {
  65. int onoff;
  66. int ret;
  67. if (kstrtoint(buf, 10, &onoff) < 0)
  68. return -EINVAL;
  69. pr_info("%s: onoff=%d\n", __func__, onoff);
  70. ret = sec_set_pm_key_wk_init(SEC_PON_KPDPWR, onoff);
  71. pr_info("%s: PWR ret=%d\n", __func__, ret);
  72. ret = sec_set_pm_key_wk_init(SEC_PON_RESIN, onoff);
  73. pr_info("%s: RESIN ret=%d\n", __func__, ret);
  74. return len;
  75. }
  76. static DEVICE_ATTR_RW(wake_enabled);
  77. #if IS_ENABLED(CONFIG_SEC_GPIO_DUMP)
  78. static ssize_t gpio_dump_show(struct device *in_dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. return sprintf(buf, "%d\n", (gpio_dump_enabled) ? 1 : 0);
  82. }
  83. static ssize_t gpio_dump_store(struct device *in_dev,
  84. struct device_attribute *attr, const char *buf, size_t len)
  85. {
  86. int onoff;
  87. if (kstrtoint(buf, 10, &onoff) < 0)
  88. return -EINVAL;
  89. pr_info("%s: onoff=%d\n", __func__, onoff);
  90. gpio_dump_enabled = (onoff) ? true : false;
  91. return len;
  92. }
  93. static DEVICE_ATTR_RW(gpio_dump);
  94. #endif
  95. /* VDD/IDDQ info */
  96. #define PARAM0_IVALID 1
  97. #define PARAM0_LESS_THAN_0 2
  98. #define DEFAULT_LEN_STR 1023
  99. #define default_scnprintf(buf, offset, fmt, ...) \
  100. do { \
  101. offset += scnprintf(&(buf)[offset], DEFAULT_LEN_STR - (size_t)offset, \
  102. fmt, ##__VA_ARGS__); \
  103. } while (0)
  104. static void check_format(char *buf, ssize_t *size, int max_len_str)
  105. {
  106. int i = 0, cnt = 0, pos = 0;
  107. if (!buf || *size <= 0)
  108. return;
  109. if (*size >= max_len_str)
  110. *size = max_len_str - 1;
  111. while (i < *size && buf[i]) {
  112. if (buf[i] == '"') {
  113. cnt++;
  114. pos = i;
  115. }
  116. if ((buf[i] < 0x20) || (buf[i] == 0x5C) || (buf[i] > 0x7E))
  117. buf[i] = ' ';
  118. i++;
  119. }
  120. if (cnt % 2) {
  121. if (pos == *size - 1) {
  122. buf[*size - 1] = '\0';
  123. } else {
  124. buf[*size - 1] = '"';
  125. buf[*size] = '\0';
  126. }
  127. }
  128. }
  129. static int get_param0(const char *name)
  130. {
  131. struct device_node *np = of_find_node_by_path("/soc/sec_ap_param");
  132. u32 val;
  133. int ret;
  134. if (!np) {
  135. pr_err("No sec_avi_data found\n");
  136. return -PARAM0_IVALID;
  137. }
  138. ret = of_property_read_u32(np, name, &val);
  139. if (ret) {
  140. pr_err("failed to get %s from node\n", name);
  141. return -PARAM0_LESS_THAN_0;
  142. }
  143. return val;
  144. }
  145. #define GET_V(A) ((A) / 1000)
  146. #define GET_MV(A) ((A) % 1000)
  147. static ssize_t show_ap_info(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. ssize_t info_size = 0;
  151. /* currently, support only for GC_OPV */
  152. default_scnprintf(buf, info_size, "\"GC_OPV_3\":\"%d.%03d\"", GET_V(get_param0("go")), GET_MV(get_param0("go")));
  153. default_scnprintf(buf, info_size, ",\"GC_PRM\":\"%d\"", get_param0("gi"));
  154. default_scnprintf(buf, info_size, ",\"DOUR\":\"%d\"", get_param0("dour"));
  155. default_scnprintf(buf, info_size, ",\"DOUB\":\"%d\"", get_param0("doub"));
  156. check_format(buf, &info_size, DEFAULT_LEN_STR);
  157. return info_size;
  158. }
  159. static DEVICE_ATTR(ap_info, 0440, show_ap_info, NULL);
  160. static struct attribute *sec_ap_pmic_attributes[] = {
  161. #if IS_ENABLED(CONFIG_SEC_GPIO_DUMP)
  162. &dev_attr_gpio_dump.attr,
  163. #endif
  164. &dev_attr_manual_reset.attr,
  165. &dev_attr_wake_enabled.attr,
  166. &dev_attr_ap_info.attr,
  167. NULL,
  168. };
  169. static struct attribute_group sec_ap_pmic_attr_group = {
  170. .attrs = sec_ap_pmic_attributes,
  171. };
  172. #if IS_ENABLED(CONFIG_SEC_GPIO_DUMP)
  173. static void gpio_state_debug_suspend_trace_probe(void *unused,
  174. const char *action, int val, bool start)
  175. {
  176. /* SUSPEND: start(1), val(1), action(machine_suspend) */
  177. if (gpio_dump_enabled && start && val > 0 && !strcmp("machine_suspend", action)) {
  178. sec_ap_gpio_debug_print();
  179. sec_pmic_gpio_debug_print();
  180. }
  181. }
  182. #endif
  183. static int sec_ap_pmic_probe(struct platform_device *pdev)
  184. {
  185. struct device_node *node = pdev->dev.of_node;
  186. struct sec_ap_pmic_info *info;
  187. int err;
  188. if (!node) {
  189. dev_err(&pdev->dev, "device-tree data is missing\n");
  190. return -ENXIO;
  191. }
  192. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  193. if (!info) {
  194. dev_err(&pdev->dev, "%s: Fail to alloc info\n", __func__);
  195. return -ENOMEM;
  196. }
  197. platform_set_drvdata(pdev, info);
  198. info->dev = &pdev->dev;
  199. sec_ap_pmic_data = info;
  200. #if IS_ENABLED(CONFIG_SEC_CLASS)
  201. sec_ap_pmic_dev = sec_device_create(NULL, "ap_pmic");
  202. if (unlikely(IS_ERR(sec_ap_pmic_dev))) {
  203. pr_err("%s: Failed to create ap_pmic device\n", __func__);
  204. err = PTR_ERR(sec_ap_pmic_dev);
  205. goto err_device_create;
  206. }
  207. err = sysfs_create_group(&sec_ap_pmic_dev->kobj,
  208. &sec_ap_pmic_attr_group);
  209. if (err < 0) {
  210. pr_err("%s: Failed to create sysfs group\n", __func__);
  211. goto err_device_create;
  212. }
  213. #endif
  214. #if IS_ENABLED(CONFIG_SEC_GPIO_DUMP)
  215. /* Register callback for cheking subsystem stats */
  216. err = register_trace_suspend_resume(
  217. gpio_state_debug_suspend_trace_probe, NULL);
  218. if (err) {
  219. pr_err("%s: Failed to register suspend trace callback, ret=%d\n",
  220. __func__, err);
  221. }
  222. #endif
  223. pr_info("%s: ap_pmic successfully inited.\n", __func__);
  224. return 0;
  225. #if IS_ENABLED(CONFIG_SEC_CLASS)
  226. err_device_create:
  227. sec_device_destroy(sec_ap_pmic_dev->devt);
  228. return err;
  229. #endif
  230. }
  231. static int sec_ap_pmic_remove(struct platform_device *pdev)
  232. {
  233. #if IS_ENABLED(CONFIG_SEC_GPIO_DUMP)
  234. int ret;
  235. ret = unregister_trace_suspend_resume(
  236. gpio_state_debug_suspend_trace_probe, NULL);
  237. #endif
  238. #if IS_ENABLED(CONFIG_SEC_CLASS)
  239. if (sec_ap_pmic_dev) {
  240. sec_device_destroy(sec_ap_pmic_dev->devt);
  241. }
  242. #endif
  243. return 0;
  244. }
  245. static const struct of_device_id sec_ap_pmic_match_table[] = {
  246. { .compatible = "samsung,sec-ap-pmic" },
  247. {}
  248. };
  249. static struct platform_driver sec_ap_pmic_driver = {
  250. .driver = {
  251. .name = "samsung,sec-ap-pmic",
  252. .of_match_table = sec_ap_pmic_match_table,
  253. },
  254. .probe = sec_ap_pmic_probe,
  255. .remove = sec_ap_pmic_remove,
  256. };
  257. module_platform_driver(sec_ap_pmic_driver);
  258. MODULE_DESCRIPTION("sec_ap_pmic driver");
  259. MODULE_SOFTDEP("pre: sec_class");
  260. MODULE_LICENSE("GPL");
  261. MODULE_SOFTDEP("pre: sec_crashkey_long");
  262. MODULE_SOFTDEP("pre: pm8941-pwrkey");
  263. MODULE_AUTHOR("Jiman Cho <[email protected]");
  264. MODULE_LICENSE("GPL");