connector_state.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * connection_state.c
  3. *
  4. * Copyright (C) 2022 Samsung Electronics
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/sti/sec_abc_detect_conn.h>
  18. #if defined(CONFIG_SEC_KUNIT)
  19. #define __visible_for_testing
  20. #else
  21. #define __visible_for_testing static
  22. #endif
  23. /*
  24. * Get gpio value.
  25. */
  26. int get_gpio_value(int gpio)
  27. {
  28. #if defined(CONFIG_SEC_KUNIT)
  29. return 1;
  30. #else
  31. return gpio_get_value(gpio);
  32. #endif
  33. }
  34. /*
  35. * Return connector total count.
  36. */
  37. __visible_for_testing ssize_t connector_count_show(struct device *dev,
  38. struct device_attribute *attr,
  39. char *buf)
  40. {
  41. struct sec_det_conn_p_data *pdata;
  42. if (gpinfo == 0)
  43. return 0;
  44. pdata = gpinfo->pdata;
  45. SEC_CONN_PRINT("read connector_count = %d\n", pdata->gpio_total_cnt);
  46. return snprintf(buf, 12, "%d\n", pdata->gpio_total_cnt);
  47. }
  48. static DEVICE_ATTR_RO(connector_count);
  49. /*
  50. * Check all connector gpio state and return the "name":"value" pair.
  51. * value 1 means disconnected, and 0 means connected.
  52. * "SUB_CONNECTOR":"1","UPPER_C2C_DET":"0"
  53. */
  54. __visible_for_testing ssize_t connector_state_show(struct device *dev,
  55. struct device_attribute *attr,
  56. char *buf)
  57. {
  58. struct sec_det_conn_p_data *pdata;
  59. char connector_state[1024] = {0, };
  60. char gpio_value_str[12] = {0, };
  61. int i;
  62. if (gpinfo == 0)
  63. return 0;
  64. pdata = gpinfo->pdata;
  65. for (i = 0; i < pdata->gpio_total_cnt; i++) {
  66. if (i > 0)
  67. strlcat(connector_state, ",", 1024);
  68. strlcat(connector_state, "\"", 1024);
  69. strlcat(connector_state, pdata->name[i], 1024);
  70. strlcat(connector_state, "\":\"", 1024);
  71. SEC_CONN_PRINT("get value : %s[%d]\n", pdata->name[i], pdata->irq_gpio[i]);
  72. sprintf(gpio_value_str, "%d", get_gpio_value(pdata->irq_gpio[i]));
  73. strlcat(connector_state, gpio_value_str, 1024);
  74. strlcat(connector_state, "\"", 1024);
  75. }
  76. connector_state[strlen(connector_state)] = '\0';
  77. SEC_CONN_PRINT("connector_state : %s\n", connector_state);
  78. return snprintf(buf, 1024, "%s\n", connector_state);
  79. }
  80. static DEVICE_ATTR_RO(connector_state);
  81. /*
  82. * Create sys node for connector connection state and connector count.
  83. */
  84. void create_current_connection_state_sysnode_files(struct sec_det_conn_info *pinfo)
  85. {
  86. int ret = 0;
  87. /* Create sys node /sys/class/sec/sec_detect_conn/connector_count */
  88. ret = device_create_file(pinfo->dev, &dev_attr_connector_count);
  89. if (ret)
  90. pr_err("%s: Failed to create connector_count.\n", __func__);
  91. /* Create sys node /sys/class/sec/sec_detect_conn/connector_state */
  92. ret = device_create_file(pinfo->dev, &dev_attr_connector_state);
  93. if (ret)
  94. pr_err("%s: Failed to create connector_state.\n", __func__);
  95. }