focaltech_ex_mode.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. *
  3. * FocalTech ftxxxx TouchScreen driver.
  4. *
  5. * Copyright (c) 2012-2019, Focaltech Ltd. All rights reserved.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  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. /*****************************************************************************
  18. *
  19. * File Name: focaltech_ex_mode.c
  20. *
  21. * Author: Focaltech Driver Team
  22. *
  23. * Created: 2016-08-31
  24. *
  25. * Abstract:
  26. *
  27. * Reference:
  28. *
  29. *****************************************************************************/
  30. /*****************************************************************************
  31. * 1.Included header files
  32. *****************************************************************************/
  33. #include "focaltech_core.h"
  34. /*****************************************************************************
  35. * 2.Private constant and macro definitions using #define
  36. *****************************************************************************/
  37. /*****************************************************************************
  38. * 3.Private enumerations, structures and unions using typedef
  39. *****************************************************************************/
  40. enum _ex_mode {
  41. MODE_GLOVE = 0,
  42. MODE_COVER,
  43. MODE_CHARGER,
  44. REPORT_RATE,
  45. };
  46. /*****************************************************************************
  47. * 4.Static variables
  48. *****************************************************************************/
  49. /*****************************************************************************
  50. * 5.Global variable or extern global variabls/functions
  51. *****************************************************************************/
  52. /*****************************************************************************
  53. * 6.Static function prototypes
  54. *******************************************************************************/
  55. static int fts_ex_mode_switch(enum _ex_mode mode, u8 value)
  56. {
  57. int ret = 0;
  58. switch (mode) {
  59. case MODE_GLOVE:
  60. ret = fts_write_reg(FTS_REG_GLOVE_MODE_EN, value > 0 ? 1 : 0);
  61. if (ret < 0)
  62. FTS_ERROR("MODE_GLOVE switch to %d fail", value);
  63. break;
  64. case MODE_COVER:
  65. ret = fts_write_reg(FTS_REG_COVER_MODE_EN, value > 0 ? 1 : 0);
  66. if (ret < 0)
  67. FTS_ERROR("MODE_COVER switch to %d fail", value);
  68. break;
  69. case MODE_CHARGER:
  70. ret = fts_write_reg(FTS_REG_CHARGER_MODE_EN, value > 0 ? 1 : 0);
  71. if (ret < 0)
  72. FTS_ERROR("MODE_CHARGER switch to %d fail", value);
  73. break;
  74. case REPORT_RATE:
  75. ret = fts_write_reg(FTS_REG_REPORT_RATE, value);
  76. if (ret < 0)
  77. FTS_ERROR("REPORT_RATE switch to %d fail", value);
  78. break;
  79. default:
  80. FTS_ERROR("mode(%d) unsupport", mode);
  81. ret = -EINVAL;
  82. break;
  83. }
  84. return ret;
  85. }
  86. static ssize_t fts_glove_mode_show(
  87. struct device *dev, struct device_attribute *attr, char *buf)
  88. {
  89. int count = 0;
  90. u8 val = 0;
  91. struct fts_ts_data *ts_data = fts_data;
  92. struct input_dev *input_dev = ts_data->input_dev;
  93. mutex_lock(&input_dev->mutex);
  94. fts_read_reg(FTS_REG_GLOVE_MODE_EN, &val);
  95. count = scnprintf(buf + count, PAGE_SIZE, "Glove Mode:%s\n",
  96. ts_data->glove_mode ? "On" : "Off");
  97. count += scnprintf(buf + count, PAGE_SIZE - count,
  98. "Glove Reg(0xC0):%d\n", val);
  99. mutex_unlock(&input_dev->mutex);
  100. return count;
  101. }
  102. static ssize_t fts_glove_mode_store(
  103. struct device *dev,
  104. struct device_attribute *attr, const char *buf, size_t count)
  105. {
  106. int ret = 0;
  107. struct fts_ts_data *ts_data = fts_data;
  108. if (FTS_SYSFS_ECHO_ON(buf)) {
  109. if (!ts_data->glove_mode) {
  110. FTS_DEBUG("enter glove mode");
  111. ret = fts_ex_mode_switch(MODE_GLOVE, ENABLE);
  112. if (ret >= 0) {
  113. ts_data->glove_mode = ENABLE;
  114. }
  115. }
  116. } else if (FTS_SYSFS_ECHO_OFF(buf)) {
  117. if (ts_data->glove_mode) {
  118. FTS_DEBUG("exit glove mode");
  119. ret = fts_ex_mode_switch(MODE_GLOVE, DISABLE);
  120. if (ret >= 0) {
  121. ts_data->glove_mode = DISABLE;
  122. }
  123. }
  124. }
  125. FTS_DEBUG("glove mode:%d", ts_data->glove_mode);
  126. return count;
  127. }
  128. static ssize_t fts_cover_mode_show(
  129. struct device *dev, struct device_attribute *attr, char *buf)
  130. {
  131. int count = 0;
  132. u8 val = 0;
  133. struct fts_ts_data *ts_data = fts_data;
  134. struct input_dev *input_dev = ts_data->input_dev;
  135. mutex_lock(&input_dev->mutex);
  136. fts_read_reg(FTS_REG_COVER_MODE_EN, &val);
  137. count = scnprintf(buf + count, PAGE_SIZE, "Cover Mode:%s\n",
  138. ts_data->cover_mode ? "On" : "Off");
  139. count += scnprintf(buf + count, PAGE_SIZE - count,
  140. "Cover Reg(0xC1):%d\n", val);
  141. mutex_unlock(&input_dev->mutex);
  142. return count;
  143. }
  144. static ssize_t fts_cover_mode_store(
  145. struct device *dev,
  146. struct device_attribute *attr, const char *buf, size_t count)
  147. {
  148. int ret = 0;
  149. struct fts_ts_data *ts_data = fts_data;
  150. if (FTS_SYSFS_ECHO_ON(buf)) {
  151. if (!ts_data->cover_mode) {
  152. FTS_DEBUG("enter cover mode");
  153. ret = fts_ex_mode_switch(MODE_COVER, ENABLE);
  154. if (ret >= 0) {
  155. ts_data->cover_mode = ENABLE;
  156. }
  157. }
  158. } else if (FTS_SYSFS_ECHO_OFF(buf)) {
  159. if (ts_data->cover_mode) {
  160. FTS_DEBUG("exit cover mode");
  161. ret = fts_ex_mode_switch(MODE_COVER, DISABLE);
  162. if (ret >= 0) {
  163. ts_data->cover_mode = DISABLE;
  164. }
  165. }
  166. }
  167. FTS_DEBUG("cover mode:%d", ts_data->cover_mode);
  168. return count;
  169. }
  170. static ssize_t fts_charger_mode_show(
  171. struct device *dev, struct device_attribute *attr, char *buf)
  172. {
  173. int count = 0;
  174. u8 val = 0;
  175. struct fts_ts_data *ts_data = fts_data;
  176. struct input_dev *input_dev = ts_data->input_dev;
  177. mutex_lock(&input_dev->mutex);
  178. fts_read_reg(FTS_REG_CHARGER_MODE_EN, &val);
  179. count = scnprintf(buf + count, PAGE_SIZE, "Charger Mode:%s\n",
  180. ts_data->charger_mode ? "On" : "Off");
  181. count += scnprintf(buf + count, PAGE_SIZE - count,
  182. "Charger Reg(0x8B):%d\n", val);
  183. mutex_unlock(&input_dev->mutex);
  184. return count;
  185. }
  186. static ssize_t fts_charger_mode_store(
  187. struct device *dev,
  188. struct device_attribute *attr, const char *buf, size_t count)
  189. {
  190. int ret = 0;
  191. struct fts_ts_data *ts_data = fts_data;
  192. if (FTS_SYSFS_ECHO_ON(buf)) {
  193. if (!ts_data->charger_mode) {
  194. FTS_DEBUG("enter charger mode");
  195. ret = fts_ex_mode_switch(MODE_CHARGER, ENABLE);
  196. if (ret >= 0) {
  197. ts_data->charger_mode = ENABLE;
  198. }
  199. }
  200. } else if (FTS_SYSFS_ECHO_OFF(buf)) {
  201. if (ts_data->charger_mode) {
  202. FTS_DEBUG("exit charger mode");
  203. ret = fts_ex_mode_switch(MODE_CHARGER, DISABLE);
  204. if (ret >= 0) {
  205. ts_data->charger_mode = DISABLE;
  206. }
  207. }
  208. }
  209. FTS_DEBUG("charger mode:%d", ts_data->glove_mode);
  210. return count;
  211. }
  212. static ssize_t fts_report_rate_show(
  213. struct device *dev, struct device_attribute *attr, char *buf)
  214. {
  215. int count = 0;
  216. u8 val = 0;
  217. struct fts_ts_data *ts_data = fts_data;
  218. struct input_dev *input_dev = ts_data->input_dev;
  219. mutex_lock(&input_dev->mutex);
  220. fts_read_reg(FTS_REG_REPORT_RATE, &val);
  221. count = scnprintf(buf + count, PAGE_SIZE,
  222. "Report Rate:%d\n", ts_data->report_rate);
  223. count += scnprintf(buf + count, PAGE_SIZE - count,
  224. "Report Rate Reg(0x88):%d\n", val);
  225. mutex_unlock(&input_dev->mutex);
  226. return count;
  227. }
  228. static ssize_t fts_report_rate_store(
  229. struct device *dev,
  230. struct device_attribute *attr, const char *buf, size_t count)
  231. {
  232. int ret = 0;
  233. struct fts_ts_data *ts_data = fts_data;
  234. int rate;
  235. ret = kstrtoint(buf, 16, &rate);
  236. if (ret)
  237. return ret;
  238. if (rate != ts_data->report_rate) {
  239. ret = fts_ex_mode_switch(REPORT_RATE, (u8)rate);
  240. if (ret >= 0)
  241. ts_data->report_rate = rate;
  242. }
  243. FTS_DEBUG("report rate:%d", ts_data->report_rate);
  244. return count;
  245. }
  246. /* read and write charger mode
  247. * read example: cat fts_glove_mode ---read glove mode
  248. * write example:echo 1 > fts_glove_mode ---write glove mode to 01
  249. */
  250. static DEVICE_ATTR(fts_glove_mode, S_IRUGO | S_IWUSR,
  251. fts_glove_mode_show, fts_glove_mode_store);
  252. static DEVICE_ATTR(fts_cover_mode, S_IRUGO | S_IWUSR,
  253. fts_cover_mode_show, fts_cover_mode_store);
  254. static DEVICE_ATTR(fts_charger_mode, S_IRUGO | S_IWUSR,
  255. fts_charger_mode_show, fts_charger_mode_store);
  256. static DEVICE_ATTR_RW(fts_report_rate);
  257. static struct attribute *fts_touch_mode_attrs[] = {
  258. &dev_attr_fts_glove_mode.attr,
  259. &dev_attr_fts_cover_mode.attr,
  260. &dev_attr_fts_charger_mode.attr,
  261. &dev_attr_fts_report_rate.attr,
  262. NULL,
  263. };
  264. static struct attribute_group fts_touch_mode_group = {
  265. .attrs = fts_touch_mode_attrs,
  266. };
  267. int fts_ex_mode_recovery(struct fts_ts_data *ts_data)
  268. {
  269. if (ts_data->glove_mode) {
  270. fts_ex_mode_switch(MODE_GLOVE, ENABLE);
  271. }
  272. if (ts_data->cover_mode) {
  273. fts_ex_mode_switch(MODE_COVER, ENABLE);
  274. }
  275. if (ts_data->charger_mode) {
  276. fts_ex_mode_switch(MODE_CHARGER, ENABLE);
  277. }
  278. if (ts_data->report_rate > 0)
  279. fts_ex_mode_switch(REPORT_RATE, ts_data->report_rate);
  280. return 0;
  281. }
  282. int fts_ex_mode_init(struct fts_ts_data *ts_data)
  283. {
  284. int ret = 0;
  285. ts_data->glove_mode = DISABLE;
  286. ts_data->cover_mode = DISABLE;
  287. ts_data->charger_mode = DISABLE;
  288. ts_data->report_rate = 0;
  289. ret = sysfs_create_group(&ts_data->dev->kobj, &fts_touch_mode_group);
  290. if (ret < 0) {
  291. FTS_ERROR("create sysfs(ex_mode) fail");
  292. sysfs_remove_group(&ts_data->dev->kobj, &fts_touch_mode_group);
  293. return ret;
  294. } else {
  295. FTS_DEBUG("create sysfs(ex_mode) succeedfully");
  296. }
  297. return 0;
  298. }
  299. int fts_ex_mode_exit(struct fts_ts_data *ts_data)
  300. {
  301. sysfs_remove_group(&ts_data->dev->kobj, &fts_touch_mode_group);
  302. return 0;
  303. }