synaptics_dsx_video.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Synaptics DSX touchscreen driver
  3. *
  4. * Copyright (C) 2012-2016 Synaptics Incorporated. All rights reserved.
  5. *
  6. * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
  7. * Copyright (C) 2012 Alexandra Chin <[email protected]>
  8. * Copyright (C) 2012 Scott Lin <[email protected]>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * INFORMATION CONTAINED IN THIS DOCUMENT IS PROVIDED "AS-IS," AND SYNAPTICS
  21. * EXPRESSLY DISCLAIMS ALL EXPRESS AND IMPLIED WARRANTIES, INCLUDING ANY
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
  23. * AND ANY WARRANTIES OF NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS.
  24. * IN NO EVENT SHALL SYNAPTICS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, PUNITIVE, OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR IN CONNECTION
  26. * WITH THE USE OF THE INFORMATION CONTAINED IN THIS DOCUMENT, HOWEVER CAUSED
  27. * AND BASED ON ANY THEORY OF LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  28. * NEGLIGENCE OR OTHER TORTIOUS ACTION, AND EVEN IF SYNAPTICS WAS ADVISED OF
  29. * THE POSSIBILITY OF SUCH DAMAGE. IF A TRIBUNAL OF COMPETENT JURISDICTION DOES
  30. * NOT PERMIT THE DISCLAIMER OF DIRECT DAMAGES OR ANY OTHER DAMAGES, SYNAPTICS'
  31. * TOTAL CUMULATIVE LIABILITY TO ANY PARTY SHALL NOT EXCEED ONE HUNDRED U.S.
  32. * DOLLARS.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/slab.h>
  37. #include <linux/delay.h>
  38. #include <linux/input.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/input/synaptics_dsx.h>
  41. #include "synaptics_dsx_core.h"
  42. #define SYSFS_FOLDER_NAME "video"
  43. static ssize_t video_sysfs_dcs_write_store(struct device *dev,
  44. struct device_attribute *attr, const char *buf, size_t count);
  45. static ssize_t video_sysfs_param_store(struct device *dev,
  46. struct device_attribute *attr, const char *buf, size_t count);
  47. static int video_send_dcs_command(unsigned char command_opcode);
  48. struct f38_command {
  49. union {
  50. struct {
  51. unsigned char command_opcode;
  52. unsigned char register_access:1;
  53. unsigned char gamma_page:1;
  54. unsigned char f38_control1_b2__7:6;
  55. unsigned char parameter_field_1;
  56. unsigned char parameter_field_2;
  57. unsigned char parameter_field_3;
  58. unsigned char parameter_field_4;
  59. unsigned char send_to_dcs:1;
  60. unsigned char f38_command6_b1__7:7;
  61. } __packed;
  62. unsigned char data[7];
  63. };
  64. };
  65. struct synaptics_rmi4_video_handle {
  66. unsigned char param;
  67. unsigned short query_base_addr;
  68. unsigned short control_base_addr;
  69. unsigned short data_base_addr;
  70. unsigned short command_base_addr;
  71. struct synaptics_rmi4_data *rmi4_data;
  72. struct kobject *sysfs_dir;
  73. };
  74. #ifdef RMI_DCS_SUSPEND_RESUME
  75. struct dcs_command {
  76. unsigned char command;
  77. unsigned int wait_time;
  78. };
  79. static struct dcs_command suspend_sequence[] = {
  80. {
  81. .command = 0x28,
  82. .wait_time = 200,
  83. },
  84. {
  85. .command = 0x10,
  86. .wait_time = 200,
  87. },
  88. };
  89. static struct dcs_command resume_sequence[] = {
  90. {
  91. .command = 0x11,
  92. .wait_time = 200,
  93. },
  94. {
  95. .command = 0x29,
  96. .wait_time = 200,
  97. },
  98. };
  99. #endif
  100. static struct device_attribute attrs[] = {
  101. __ATTR(dcs_write, 0220,
  102. synaptics_rmi4_show_error,
  103. video_sysfs_dcs_write_store),
  104. __ATTR(param, 0220,
  105. synaptics_rmi4_show_error,
  106. video_sysfs_param_store),
  107. };
  108. static struct synaptics_rmi4_video_handle *video;
  109. DECLARE_COMPLETION(video_remove_complete);
  110. static ssize_t video_sysfs_dcs_write_store(struct device *dev,
  111. struct device_attribute *attr, const char *buf, size_t count)
  112. {
  113. int retval;
  114. unsigned int input;
  115. if (kstrtouint(buf, 16, &input) != 1)
  116. return -EINVAL;
  117. retval = video_send_dcs_command((unsigned char)input);
  118. if (retval < 0)
  119. return retval;
  120. return count;
  121. }
  122. static ssize_t video_sysfs_param_store(struct device *dev,
  123. struct device_attribute *attr, const char *buf, size_t count)
  124. {
  125. unsigned int input;
  126. if (kstrtouint(buf, 16, &input) != 1)
  127. return -EINVAL;
  128. video->param = (unsigned char)input;
  129. return count;
  130. }
  131. static int video_send_dcs_command(unsigned char command_opcode)
  132. {
  133. int retval;
  134. struct f38_command command;
  135. struct synaptics_rmi4_data *rmi4_data = video->rmi4_data;
  136. memset(&command, 0x00, sizeof(command));
  137. command.command_opcode = command_opcode;
  138. command.parameter_field_1 = video->param;
  139. command.send_to_dcs = 1;
  140. video->param = 0;
  141. retval = synaptics_rmi4_reg_write(rmi4_data,
  142. video->command_base_addr,
  143. command.data,
  144. sizeof(command.data));
  145. if (retval < 0) {
  146. dev_err(rmi4_data->pdev->dev.parent,
  147. "%s: Failed to send DCS command\n",
  148. __func__);
  149. return retval;
  150. }
  151. return 0;
  152. }
  153. static int video_scan_pdt(void)
  154. {
  155. int retval;
  156. unsigned char page;
  157. unsigned short addr;
  158. bool f38_found = false;
  159. struct synaptics_rmi4_fn_desc rmi_fd;
  160. struct synaptics_rmi4_data *rmi4_data = video->rmi4_data;
  161. for (page = 0; page < PAGES_TO_SERVICE; page++) {
  162. for (addr = PDT_START; addr > PDT_END; addr -= PDT_ENTRY_SIZE) {
  163. addr |= (page << 8);
  164. retval = synaptics_rmi4_reg_read(rmi4_data,
  165. addr,
  166. (unsigned char *)&rmi_fd,
  167. sizeof(rmi_fd));
  168. if (retval < 0)
  169. return retval;
  170. addr &= ~(MASK_8BIT << 8);
  171. if (!rmi_fd.fn_number)
  172. break;
  173. if (rmi_fd.fn_number == SYNAPTICS_RMI4_F38) {
  174. f38_found = true;
  175. goto f38_found;
  176. }
  177. }
  178. }
  179. if (!f38_found) {
  180. dev_err(rmi4_data->pdev->dev.parent,
  181. "%s: Failed to find F38\n",
  182. __func__);
  183. return -EINVAL;
  184. }
  185. f38_found:
  186. video->query_base_addr = rmi_fd.query_base_addr | (page << 8);
  187. video->control_base_addr = rmi_fd.ctrl_base_addr | (page << 8);
  188. video->data_base_addr = rmi_fd.data_base_addr | (page << 8);
  189. video->command_base_addr = rmi_fd.cmd_base_addr | (page << 8);
  190. return 0;
  191. }
  192. static int synaptics_rmi4_video_init(struct synaptics_rmi4_data *rmi4_data)
  193. {
  194. int retval;
  195. unsigned char attr_count;
  196. if (video) {
  197. dev_dbg(rmi4_data->pdev->dev.parent,
  198. "%s: Handle already exists\n",
  199. __func__);
  200. return 0;
  201. }
  202. video = kzalloc(sizeof(*video), GFP_KERNEL);
  203. if (!video) {
  204. dev_err(rmi4_data->pdev->dev.parent,
  205. "%s: Failed to alloc mem for video\n",
  206. __func__);
  207. retval = -ENOMEM;
  208. goto exit;
  209. }
  210. video->rmi4_data = rmi4_data;
  211. retval = video_scan_pdt();
  212. if (retval < 0) {
  213. retval = 0;
  214. goto exit_scan_pdt;
  215. }
  216. video->sysfs_dir = kobject_create_and_add(SYSFS_FOLDER_NAME,
  217. &rmi4_data->input_dev->dev.kobj);
  218. if (!video->sysfs_dir) {
  219. dev_err(rmi4_data->pdev->dev.parent,
  220. "%s: Failed to create sysfs directory\n",
  221. __func__);
  222. retval = -ENODEV;
  223. goto exit_sysfs_dir;
  224. }
  225. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
  226. retval = sysfs_create_file(video->sysfs_dir,
  227. &attrs[attr_count].attr);
  228. if (retval < 0) {
  229. dev_err(rmi4_data->pdev->dev.parent,
  230. "%s: Failed to create sysfs attributes\n",
  231. __func__);
  232. retval = -ENODEV;
  233. goto exit_sysfs_attrs;
  234. }
  235. }
  236. return 0;
  237. exit_sysfs_attrs:
  238. for (attr_count--; attr_count >= 0; attr_count--)
  239. sysfs_remove_file(video->sysfs_dir, &attrs[attr_count].attr);
  240. kobject_put(video->sysfs_dir);
  241. exit_sysfs_dir:
  242. exit_scan_pdt:
  243. kfree(video);
  244. video = NULL;
  245. exit:
  246. return retval;
  247. }
  248. static void synaptics_rmi4_video_remove(struct synaptics_rmi4_data *rmi4_data)
  249. {
  250. unsigned char attr_count;
  251. if (!video)
  252. goto exit;
  253. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++)
  254. sysfs_remove_file(video->sysfs_dir, &attrs[attr_count].attr);
  255. kobject_put(video->sysfs_dir);
  256. kfree(video);
  257. video = NULL;
  258. exit:
  259. complete(&video_remove_complete);
  260. }
  261. static void synaptics_rmi4_video_reset(struct synaptics_rmi4_data *rmi4_data)
  262. {
  263. if (!video)
  264. synaptics_rmi4_video_init(rmi4_data);
  265. }
  266. #ifdef RMI_DCS_SUSPEND_RESUME
  267. static void synaptics_rmi4_video_suspend(struct synaptics_rmi4_data *rmi4_data)
  268. {
  269. int retval;
  270. unsigned char ii;
  271. unsigned char command;
  272. unsigned char num_of_cmds;
  273. if (!video)
  274. return;
  275. num_of_cmds = ARRAY_SIZE(suspend_sequence);
  276. for (ii = 0; ii < num_of_cmds; ii++) {
  277. command = suspend_sequence[ii].command;
  278. retval = video_send_dcs_command(command);
  279. if (retval < 0)
  280. return;
  281. msleep(suspend_sequence[ii].wait_time);
  282. }
  283. }
  284. static void synaptics_rmi4_video_resume(struct synaptics_rmi4_data *rmi4_data)
  285. {
  286. int retval;
  287. unsigned char ii;
  288. unsigned char command;
  289. unsigned char num_of_cmds;
  290. if (!video)
  291. return;
  292. num_of_cmds = ARRAY_SIZE(resume_sequence);
  293. for (ii = 0; ii < num_of_cmds; ii++) {
  294. command = resume_sequence[ii].command;
  295. retval = video_send_dcs_command(command);
  296. if (retval < 0)
  297. return;
  298. msleep(resume_sequence[ii].wait_time);
  299. }
  300. }
  301. #endif
  302. static struct synaptics_rmi4_exp_fn video_module = {
  303. .fn_type = RMI_VIDEO,
  304. .init = synaptics_rmi4_video_init,
  305. .remove = synaptics_rmi4_video_remove,
  306. .reset = synaptics_rmi4_video_reset,
  307. .reinit = NULL,
  308. .early_suspend = NULL,
  309. #ifdef RMI_DCS_SUSPEND_RESUME
  310. .suspend = synaptics_rmi4_video_suspend,
  311. .resume = synaptics_rmi4_video_resume,
  312. #else
  313. .suspend = NULL,
  314. .resume = NULL,
  315. #endif
  316. .late_resume = NULL,
  317. .attn = NULL,
  318. };
  319. static int __init rmi4_video_module_init(void)
  320. {
  321. synaptics_rmi4_new_function(&video_module, true);
  322. return 0;
  323. }
  324. static void __exit rmi4_video_module_exit(void)
  325. {
  326. synaptics_rmi4_new_function(&video_module, false);
  327. wait_for_completion(&video_remove_complete);
  328. }
  329. module_init(rmi4_video_module_init);
  330. module_exit(rmi4_video_module_exit);
  331. MODULE_AUTHOR("Synaptics, Inc.");
  332. MODULE_DESCRIPTION("Synaptics DSX Video Module");
  333. MODULE_LICENSE("GPL v2");