rmi_dev.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. * Synaptics DSX touchscreen driver
  3. *
  4. * Copyright (C) 2012 Synaptics Incorporated
  5. *
  6. * Copyright (C) 2012 Alexandra Chin <alexandra.chin@tw.synaptics.com>
  7. * Copyright (C) 2012 Scott Lin <scott.lin@tw.synaptics.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/input.h>
  26. #include <linux/gpio.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/cdev.h>
  29. #ifdef CONFIG_HAS_EARLYSUSPEND
  30. #include <linux/earlysuspend.h>
  31. #endif
  32. /* #include <linux/input/synaptics_dsx.h> */
  33. #include "include/synaptics_dsx_rmi4_i2c.h"
  34. #include "tpd.h"
  35. #define SYN_I2C_RETRY_TIMES 10
  36. #define CHAR_DEVICE_NAME "rmi"
  37. #define DEVICE_CLASS_NAME "rmidev"
  38. #define DEV_NUMBER 1
  39. #define REG_ADDR_LIMIT 0xFFFF
  40. struct kobject *properties_kobj_rmidev;
  41. static ssize_t rmidev_sysfs_open_store(struct device *dev,
  42. struct device_attribute *attr, const char *buf,
  43. size_t count);
  44. static ssize_t rmidev_sysfs_release_store(struct device *dev,
  45. struct device_attribute *attr, const char *buf,
  46. size_t count);
  47. static ssize_t rmidev_sysfs_address_store(struct device *dev,
  48. struct device_attribute *attr, const char *buf,
  49. size_t count);
  50. static ssize_t rmidev_sysfs_length_store(struct device *dev,
  51. struct device_attribute *attr, const char *buf,
  52. size_t count);
  53. static ssize_t rmidev_sysfs_data_show(struct device *dev, struct device_attribute *attr, char *buf);
  54. static ssize_t rmidev_sysfs_data_store(struct device *dev,
  55. struct device_attribute *attr, const char *buf,
  56. size_t count);
  57. static ssize_t rmidev_sysfs_attn_show(struct device *dev, struct device_attribute *attr, char *buf);
  58. static ssize_t rmidev_sysfs_attn_store(struct device *dev,
  59. struct device_attribute *attr, const char *buf,
  60. size_t count);
  61. struct rmidev_handle {
  62. dev_t dev_no;
  63. unsigned short address;
  64. unsigned int length;
  65. struct device dev;
  66. struct synaptics_rmi4_data *rmi4_data;
  67. struct synaptics_rmi4_exp_fn_ptr *fn_ptr;
  68. struct kobject *sysfs_dir;
  69. void *data;
  70. };
  71. struct rmidev_data {
  72. int ref_count;
  73. struct cdev main_dev;
  74. struct class *device_class;
  75. struct mutex file_mutex;
  76. struct rmidev_handle *rmi_dev;
  77. };
  78. static struct device_attribute attrs[] = {
  79. __ATTR(open, (S_IRUGO | S_IWUSR),
  80. synaptics_rmi4_show_error,
  81. rmidev_sysfs_open_store),
  82. __ATTR(release, (S_IRUGO | S_IWUSR),
  83. synaptics_rmi4_show_error,
  84. rmidev_sysfs_release_store),
  85. __ATTR(address, (S_IRUGO | S_IWUSR),
  86. synaptics_rmi4_show_error,
  87. rmidev_sysfs_address_store),
  88. __ATTR(length, (S_IRUGO | S_IWUSR),
  89. synaptics_rmi4_show_error,
  90. rmidev_sysfs_length_store),
  91. __ATTR(data, (S_IRUGO | S_IWUSR),
  92. rmidev_sysfs_data_show,
  93. rmidev_sysfs_data_store),
  94. __ATTR(attn, (S_IRUGO | S_IWUSR),
  95. rmidev_sysfs_attn_show,
  96. rmidev_sysfs_attn_store),
  97. };
  98. static int rmidev_major_num;
  99. static struct class *rmidev_device_class;
  100. static struct rmidev_handle *rmidev;
  101. static struct completion remove_complete;
  102. static ssize_t rmidev_sysfs_open_store(struct device *dev,
  103. struct device_attribute *attr, const char *buf, size_t count)
  104. {
  105. unsigned int input;
  106. if (kstrtoint(buf, 10, &input))
  107. return -EINVAL;
  108. if (input != 1)
  109. return -EINVAL;
  110. rmidev->fn_ptr->enable(rmidev->rmi4_data, false);
  111. dev_dbg(&rmidev->rmi4_data->i2c_client->dev,
  112. "%s: Attention interrupt disabled\n", __func__);
  113. return count;
  114. }
  115. static ssize_t rmidev_sysfs_release_store(struct device *dev,
  116. struct device_attribute *attr, const char *buf,
  117. size_t count)
  118. {
  119. unsigned int input;
  120. if (kstrtoint(buf, 10, &input))
  121. return -EINVAL;
  122. if (input != 1)
  123. return -EINVAL;
  124. rmidev->fn_ptr->enable(rmidev->rmi4_data, true);
  125. dev_dbg(&rmidev->rmi4_data->i2c_client->dev, "%s: Attention interrupt enabled\n", __func__);
  126. return count;
  127. }
  128. static ssize_t rmidev_sysfs_address_store(struct device *dev,
  129. struct device_attribute *attr, const char *buf,
  130. size_t count)
  131. {
  132. unsigned int input;
  133. if (kstrtoint(buf, 10, &input))
  134. return -EINVAL;
  135. if (input > REG_ADDR_LIMIT)
  136. return -EINVAL;
  137. rmidev->address = (unsigned short)input;
  138. return count;
  139. }
  140. static ssize_t rmidev_sysfs_length_store(struct device *dev,
  141. struct device_attribute *attr, const char *buf,
  142. size_t count)
  143. {
  144. unsigned int input;
  145. if (kstrtoint(buf, 10, &input))
  146. return -EINVAL;
  147. if (input > REG_ADDR_LIMIT)
  148. return -EINVAL;
  149. rmidev->length = input;
  150. return count;
  151. }
  152. static ssize_t rmidev_sysfs_data_show(struct device *dev, struct device_attribute *attr, char *buf)
  153. {
  154. int retval;
  155. unsigned int data_length = rmidev->length;
  156. if (data_length > (REG_ADDR_LIMIT - rmidev->address))
  157. data_length = REG_ADDR_LIMIT - rmidev->address;
  158. if (data_length) {
  159. retval = rmidev->fn_ptr->read(rmidev->rmi4_data,
  160. rmidev->address, (unsigned char *)buf, data_length);
  161. if (retval < 0) {
  162. dev_err(&rmidev->rmi4_data->i2c_client->dev,
  163. "%s: Failed to read data\n", __func__);
  164. return retval;
  165. }
  166. } else {
  167. return -EINVAL;
  168. }
  169. return data_length;
  170. }
  171. static ssize_t rmidev_sysfs_data_store(struct device *dev,
  172. struct device_attribute *attr, const char *buf, size_t count)
  173. {
  174. int retval;
  175. unsigned int data_length = rmidev->length;
  176. if (data_length > (REG_ADDR_LIMIT - rmidev->address))
  177. data_length = REG_ADDR_LIMIT - rmidev->address;
  178. if (data_length) {
  179. retval = rmidev->fn_ptr->write(rmidev->rmi4_data,
  180. rmidev->address, (unsigned char *)buf, data_length);
  181. if (retval < 0) {
  182. dev_err(&rmidev->rmi4_data->i2c_client->dev,
  183. "%s: Failed to write data\n", __func__);
  184. return retval;
  185. }
  186. } else {
  187. return -EINVAL;
  188. }
  189. return data_length;
  190. }
  191. static ssize_t rmidev_sysfs_attn_show(struct device *dev, struct device_attribute *attr, char *buf)
  192. {
  193. int attn = 0/*gpio_get_value(tpd_int_gpio_number)*/;
  194. return sprintf(buf, "%d %d\n", IRQF_TRIGGER_FALLING, attn);
  195. }
  196. static ssize_t rmidev_sysfs_attn_store(struct device *dev,
  197. struct device_attribute *attr, const char *buf, size_t count)
  198. {
  199. return 0;
  200. }
  201. /*
  202. * rmidev_llseek - used to set up register address
  203. *
  204. * @filp: file structure for seek
  205. * @off: offset
  206. * if whence == SEEK_SET,
  207. * high 16 bits: page address
  208. * low 16 bits: register address
  209. * if whence == SEEK_CUR,
  210. * offset from current position
  211. * if whence == SEEK_END,
  212. * offset from end position (0xFFFF)
  213. * @whence: SEEK_SET, SEEK_CUR, or SEEK_END
  214. */
  215. static loff_t rmidev_llseek(struct file *filp, loff_t off, int whence)
  216. {
  217. loff_t newpos;
  218. struct rmidev_data *dev_data = filp->private_data;
  219. if (IS_ERR(dev_data)) {
  220. pr_err("%s: Pointer of char device data is invalid", __func__);
  221. return -EBADF;
  222. }
  223. mutex_lock(&(dev_data->file_mutex));
  224. switch (whence) {
  225. case SEEK_SET:
  226. newpos = off;
  227. break;
  228. case SEEK_CUR:
  229. newpos = filp->f_pos + off;
  230. break;
  231. case SEEK_END:
  232. newpos = REG_ADDR_LIMIT + off;
  233. break;
  234. default:
  235. newpos = -EINVAL;
  236. goto clean_up;
  237. }
  238. if (newpos < 0 || newpos > REG_ADDR_LIMIT) {
  239. dev_err(&rmidev->rmi4_data->i2c_client->dev,
  240. "%s: New position 0x%04x is invalid\n", __func__, (unsigned int)newpos);
  241. newpos = -EINVAL;
  242. goto clean_up;
  243. }
  244. filp->f_pos = newpos;
  245. clean_up:
  246. mutex_unlock(&(dev_data->file_mutex));
  247. return newpos;
  248. }
  249. /*
  250. * rmidev_read: - use to read data from rmi device
  251. *
  252. * @filp: file structure for read
  253. * @buf: user space buffer pointer
  254. * @count: number of bytes to read
  255. * @f_pos: offset (starting register address)
  256. */
  257. static ssize_t rmidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  258. {
  259. ssize_t retval;
  260. unsigned char tmpbuf[count + 1];
  261. struct rmidev_data *dev_data = filp->private_data;
  262. if (IS_ERR(dev_data)) {
  263. pr_err("%s: Pointer of char device data is invalid", __func__);
  264. return -EBADF;
  265. }
  266. if (count == 0)
  267. return 0;
  268. if (count > (REG_ADDR_LIMIT - *f_pos))
  269. count = REG_ADDR_LIMIT - *f_pos;
  270. mutex_lock(&(dev_data->file_mutex));
  271. retval = rmidev->fn_ptr->read(rmidev->rmi4_data, *f_pos, tmpbuf, count);
  272. if (retval < 0)
  273. goto clean_up;
  274. if (copy_to_user(buf, tmpbuf, count))
  275. retval = -EFAULT;
  276. else
  277. *f_pos += retval;
  278. clean_up:
  279. mutex_unlock(&(dev_data->file_mutex));
  280. return retval;
  281. }
  282. /*
  283. * rmidev_write: - used to write data to rmi device
  284. *
  285. * @filep: file structure for write
  286. * @buf: user space buffer pointer
  287. * @count: number of bytes to write
  288. * @f_pos: offset (starting register address)
  289. */
  290. static ssize_t rmidev_write(struct file *filp, const char __user *buf,
  291. size_t count, loff_t *f_pos)
  292. {
  293. ssize_t retval;
  294. unsigned char tmpbuf[count + 1];
  295. struct rmidev_data *dev_data = filp->private_data;
  296. if (IS_ERR(dev_data)) {
  297. pr_err("%s: Pointer of char device data is invalid", __func__);
  298. return -EBADF;
  299. }
  300. if (count == 0)
  301. return 0;
  302. if (count > (REG_ADDR_LIMIT - *f_pos))
  303. count = REG_ADDR_LIMIT - *f_pos;
  304. if (copy_from_user(tmpbuf, buf, count))
  305. return -EFAULT;
  306. mutex_lock(&(dev_data->file_mutex));
  307. retval = rmidev->fn_ptr->write(rmidev->rmi4_data, *f_pos, tmpbuf, count);
  308. if (retval >= 0)
  309. *f_pos += retval;
  310. mutex_unlock(&(dev_data->file_mutex));
  311. return retval;
  312. }
  313. /*
  314. * rmidev_open: enable access to rmi device
  315. * @inp: inode struture
  316. * @filp: file structure
  317. */
  318. static int rmidev_open(struct inode *inp, struct file *filp)
  319. {
  320. int retval = 0;
  321. struct rmidev_data *dev_data = container_of(inp->i_cdev, struct rmidev_data, main_dev);
  322. if (!dev_data)
  323. return -EACCES;
  324. filp->private_data = dev_data;
  325. mutex_lock(&(dev_data->file_mutex));
  326. rmidev->fn_ptr->enable(rmidev->rmi4_data, false);
  327. dev_dbg(&rmidev->rmi4_data->i2c_client->dev,
  328. "%s: Attention interrupt disabled\n", __func__);
  329. if (dev_data->ref_count < 1)
  330. dev_data->ref_count++;
  331. else
  332. retval = -EACCES;
  333. mutex_unlock(&(dev_data->file_mutex));
  334. return retval;
  335. }
  336. /*
  337. * rmidev_release: - release access to rmi device
  338. * @inp: inode structure
  339. * @filp: file structure
  340. */
  341. static int rmidev_release(struct inode *inp, struct file *filp)
  342. {
  343. struct rmidev_data *dev_data = container_of(inp->i_cdev, struct rmidev_data, main_dev);
  344. if (!dev_data)
  345. return -EACCES;
  346. mutex_lock(&(dev_data->file_mutex));
  347. dev_data->ref_count--;
  348. if (dev_data->ref_count < 0)
  349. dev_data->ref_count = 0;
  350. rmidev->fn_ptr->enable(rmidev->rmi4_data, true);
  351. dev_dbg(&rmidev->rmi4_data->i2c_client->dev, "%s: Attention interrupt enabled\n", __func__);
  352. mutex_unlock(&(dev_data->file_mutex));
  353. return 0;
  354. }
  355. static const struct file_operations rmidev_fops = {
  356. .owner = THIS_MODULE,
  357. .llseek = rmidev_llseek,
  358. .read = rmidev_read,
  359. .write = rmidev_write,
  360. .open = rmidev_open,
  361. .release = rmidev_release,
  362. };
  363. static void rmidev_device_cleanup(struct rmidev_data *dev_data)
  364. {
  365. dev_t devno;
  366. if (dev_data) {
  367. devno = dev_data->main_dev.dev;
  368. if (dev_data->device_class)
  369. device_destroy(dev_data->device_class, devno);
  370. cdev_del(&dev_data->main_dev);
  371. unregister_chrdev_region(devno, 1);
  372. dev_dbg(&rmidev->rmi4_data->i2c_client->dev,
  373. "%s: rmidev device removed\n", __func__);
  374. }
  375. }
  376. static char *rmi_char_devnode(struct device *dev, umode_t *mode)
  377. {
  378. if (!mode)
  379. return NULL;
  380. *mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  381. return kasprintf(GFP_KERNEL, "rmi/%s", dev_name(dev));
  382. }
  383. static int rmidev_create_device_class(void)
  384. {
  385. rmidev_device_class = class_create(THIS_MODULE, DEVICE_CLASS_NAME);
  386. if (IS_ERR(rmidev_device_class)) {
  387. pr_err("%s: Failed to create /dev/%s\n", __func__, CHAR_DEVICE_NAME);
  388. return -ENODEV;
  389. }
  390. rmidev_device_class->devnode = rmi_char_devnode;
  391. return 0;
  392. }
  393. /**
  394. * synaptics_rmi4_set_page()
  395. *
  396. * Called by synaptics_rmi4_i2c_read() and synaptics_rmi4_i2c_write().
  397. *
  398. * This function writes to the page select register to switch to the
  399. * assigned page.
  400. */
  401. #if 0
  402. static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *rmi4_data, unsigned int address)
  403. {
  404. int retval = 0;
  405. unsigned char retry;
  406. unsigned char buf[PAGE_SELECT_LEN];
  407. unsigned char page;
  408. struct i2c_client *i2c = rmi4_data->i2c_client;
  409. page = ((address >> 8) & MASK_8BIT);
  410. if (page != rmi4_data->current_page) {
  411. buf[0] = MASK_8BIT;
  412. buf[1] = page;
  413. for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
  414. retval = i2c_master_send(i2c, buf, PAGE_SELECT_LEN);
  415. if (retval != PAGE_SELECT_LEN) {
  416. dev_err(&i2c->dev, "%s: I2C retry %d\n", __func__, retry + 1);
  417. msleep(20);
  418. } else {
  419. rmi4_data->current_page = page;
  420. break;
  421. }
  422. }
  423. } else {
  424. retval = PAGE_SELECT_LEN;
  425. }
  426. return retval;
  427. }
  428. #endif
  429. /**
  430. * synaptics_rmi4_i2c_read()
  431. *
  432. * Called by various functions in this driver, and also exported to
  433. * other expansion Function modules such as rmi_dev.
  434. *
  435. * This function reads data of an arbitrary length from the sensor,
  436. * starting from an assigned register address of the sensor, via I2C
  437. * with a retry mechanism.
  438. */
  439. static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data,
  440. unsigned short addr, unsigned char *data, unsigned short length)
  441. {
  442. return tpd_i2c_read_data(rmi4_data->i2c_client, addr, data, length);
  443. /* int retval;
  444. unsigned char retry;
  445. unsigned char buf;
  446. struct i2c_msg msg[] = {
  447. {
  448. .addr = rmi4_data->i2c_client->addr,
  449. .flags = 0,
  450. .len = 1,
  451. .buf = &buf,
  452. },
  453. {
  454. .addr = rmi4_data->i2c_client->addr,
  455. .flags = I2C_M_RD,
  456. .len = length,
  457. .buf = data,
  458. },
  459. };
  460. buf = addr & MASK_8BIT;
  461. mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
  462. retval = synaptics_rmi4_set_page(rmi4_data, addr);
  463. if (retval != PAGE_SELECT_LEN)
  464. goto exit;
  465. for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
  466. if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 2) == 2) {
  467. retval = length;
  468. break;
  469. }
  470. dev_err(&rmi4_data->i2c_client->dev,
  471. "%s: I2C retry %d\n",
  472. __func__, retry + 1);
  473. msleep(20);
  474. }
  475. if (retry == SYN_I2C_RETRY_TIMES) {
  476. dev_err(&rmi4_data->i2c_client->dev,
  477. "%s: I2C read over retry limit\n",
  478. __func__);
  479. retval = -EIO;
  480. }
  481. exit:
  482. mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
  483. return retval;*/
  484. }
  485. /**
  486. * synaptics_rmi4_i2c_write()
  487. *
  488. * Called by various functions in this driver, and also exported to
  489. * other expansion Function modules such as rmi_dev.
  490. *
  491. * This function writes data of an arbitrary length to the sensor,
  492. * starting from an assigned register address of the sensor, via I2C with
  493. * a retry mechanism.
  494. */
  495. static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data,
  496. unsigned short addr, unsigned char *data, unsigned short length)
  497. {
  498. return tpd_i2c_write_data(rmi4_data->i2c_client, addr, data, length);
  499. /* int retval;
  500. unsigned char retry;
  501. unsigned char buf[length + 1];
  502. struct i2c_msg msg[] = {
  503. {
  504. .addr = rmi4_data->i2c_client->addr,
  505. .flags = 0,
  506. .len = length + 1,
  507. .buf = buf,
  508. }
  509. };
  510. mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
  511. retval = synaptics_rmi4_set_page(rmi4_data, addr);
  512. if (retval != PAGE_SELECT_LEN)
  513. goto exit;
  514. buf[0] = addr & MASK_8BIT;
  515. memcpy(&buf[1], &data[0], length);
  516. for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
  517. if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 1) == 1) {
  518. retval = length;
  519. break;
  520. }
  521. dev_err(&rmi4_data->i2c_client->dev,
  522. "%s: I2C retry %d\n",
  523. __func__, retry + 1);
  524. msleep(20);
  525. }
  526. if (retry == SYN_I2C_RETRY_TIMES) {
  527. dev_err(&rmi4_data->i2c_client->dev,
  528. "%s: I2C write over retry limit\n",
  529. __func__);
  530. retval = -EIO;
  531. }
  532. exit:
  533. mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
  534. return retval;*/
  535. }
  536. /**
  537. * synaptics_rmi4_irq_enable()
  538. *
  539. * Called by synaptics_rmi4_probe() and the power management functions
  540. * in this driver and also exported to other expansion Function modules
  541. * such as rmi_dev.
  542. *
  543. * This function handles the enabling and disabling of the attention
  544. * irq including the setting up of the ISR thread.
  545. */
  546. static int synaptics_rmi4_irq_enable(struct synaptics_rmi4_data *rmi4_data, bool enable)
  547. {
  548. int retval = 0;
  549. unsigned char intr_status;
  550. /* const struct synaptics_dsx_platform_data *platform_data = rmi4_data->i2c_client->dev.platform_data; */
  551. if (enable) {
  552. if (rmi4_data->irq_enabled)
  553. return retval;
  554. /* Clear interrupts first */
  555. retval = synaptics_rmi4_i2c_read(rmi4_data,
  556. rmi4_data->f01_data_base_addr + 1,
  557. &intr_status, rmi4_data->num_of_intr_regs);
  558. if (retval < 0)
  559. return retval;
  560. /*retval = request_threaded_irq(rmi4_data->irq, NULL,
  561. synaptics_rmi4_irq, platform_data->irq_type,
  562. DRIVER_NAME, rmi4_data);
  563. if (retval < 0) {
  564. dev_err(&rmi4_data->i2c_client->dev,
  565. "%s: Failed to create irq thread\n",
  566. __func__);
  567. return retval;
  568. } */
  569. rmi4_data->irq_enabled = true;
  570. } else {
  571. if (rmi4_data->irq_enabled) {
  572. disable_irq(rmi4_data->irq);
  573. free_irq(rmi4_data->irq, rmi4_data);
  574. rmi4_data->irq_enabled = false;
  575. }
  576. }
  577. return retval;
  578. }
  579. static int rmidev_init_device(struct i2c_client *client)
  580. {
  581. int retval;
  582. dev_t dev_no;
  583. unsigned char attr_count;
  584. struct rmidev_data *dev_data;
  585. struct device *device_ptr;
  586. TPD_DMESG("%s:enter\n", __func__);
  587. rmidev = kzalloc(sizeof(*rmidev), GFP_KERNEL);
  588. if (!rmidev) {
  589. /*dev_err(&client->dev, "%s: Failed to alloc mem for rmidev\n", __func__);*/
  590. retval = -ENOMEM;
  591. goto err_rmidev;
  592. }
  593. rmidev->rmi4_data = kzalloc(sizeof(struct synaptics_rmi4_data), GFP_KERNEL);
  594. if (!rmidev->rmi4_data) {
  595. /*dev_err(&client->dev, "%s: Failed to alloc mem for rmi4_data\n", __func__);*/
  596. retval = -ENOMEM;
  597. goto err_fn_ptr;
  598. }
  599. rmidev->fn_ptr = kzalloc(sizeof(*(rmidev->fn_ptr)), GFP_KERNEL);
  600. if (!rmidev) {
  601. /*dev_err(&client->dev, "%s: Failed to alloc mem for fn_ptr\n", __func__);*/
  602. retval = -ENOMEM;
  603. goto exit_free_rmi4;
  604. }
  605. rmidev->rmi4_data->input_dev = tpd->dev;
  606. rmidev->rmi4_data->i2c_client = client;
  607. rmidev->fn_ptr->read = synaptics_rmi4_i2c_read;
  608. rmidev->fn_ptr->write = synaptics_rmi4_i2c_write;
  609. rmidev->fn_ptr->enable = synaptics_rmi4_irq_enable;
  610. mutex_init(&(rmidev->rmi4_data->rmi4_io_ctrl_mutex));
  611. retval = rmidev_create_device_class();
  612. if (retval < 0) {
  613. dev_err(&client->dev, "%s: Failed to create device class\n", __func__);
  614. goto err_device_class;
  615. }
  616. TPD_DMESG("%s:after create device class\n", __func__);
  617. if (rmidev_major_num) {
  618. dev_no = MKDEV(rmidev_major_num, DEV_NUMBER);
  619. retval = register_chrdev_region(dev_no, 1, CHAR_DEVICE_NAME);
  620. } else {
  621. retval = alloc_chrdev_region(&dev_no, 0, 1, CHAR_DEVICE_NAME);
  622. if (retval < 0) {
  623. dev_err(&client->dev,
  624. "%s: Failed to allocate char device region\n", __func__);
  625. goto err_device_region;
  626. }
  627. rmidev_major_num = MAJOR(dev_no);
  628. dev_dbg(&client->dev,
  629. "%s: Major number of rmidev = %d\n", __func__, rmidev_major_num);
  630. }
  631. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  632. if (!dev_data) {
  633. /*dev_err(&client->dev, "%s: Failed to alloc mem for dev_data\n", __func__);*/
  634. retval = -ENOMEM;
  635. goto err_dev_data;
  636. }
  637. mutex_init(&dev_data->file_mutex);
  638. dev_data->rmi_dev = rmidev;
  639. rmidev->data = dev_data;
  640. cdev_init(&dev_data->main_dev, &rmidev_fops);
  641. retval = cdev_add(&dev_data->main_dev, dev_no, 1);
  642. if (retval < 0) {
  643. dev_err(&client->dev, "%s: Failed to add rmi char device\n", __func__);
  644. goto err_char_device;
  645. }
  646. dev_set_name(&rmidev->dev, "rmidev%d", MINOR(dev_no));
  647. dev_data->device_class = rmidev_device_class;
  648. device_ptr = device_create(dev_data->device_class, NULL, dev_no,
  649. NULL, CHAR_DEVICE_NAME "%d", MINOR(dev_no));
  650. TPD_DMESG("%s:after devicecreate\n", __func__);
  651. if (IS_ERR(device_ptr)) {
  652. dev_err(&client->dev, "%s: Failed to create rmi char device\n", __func__);
  653. retval = -ENODEV;
  654. goto err_char_device;
  655. }
  656. /* retval = gpio_export(rmidev->rmi4_data->board->gpio, false);
  657. if (retval < 0) {
  658. dev_err(&client->dev,
  659. "%s: Failed to export gpio (%d)\n",
  660. __func__, rmidev->rmi4_data->board->gpio);
  661. } else {
  662. retval = gpio_export_link(&(rmidev->rmi4_data->input_dev->dev),
  663. "attn", rmidev->rmi4_data->board->gpio);
  664. if (retval < 0) {
  665. dev_err(&rmidev->rmi4_data->input_dev->dev,
  666. "%s Failed to create gpio symlink\n",
  667. __func__);
  668. } else {
  669. dev_dbg(&rmidev->rmi4_data->input_dev->dev,
  670. "%s: Exported gpio %d\n",
  671. __func__, rmi4_data->board->gpio);
  672. }
  673. }*/
  674. /* rmidev->sysfs_dir = kobject_create_and_add("rmidev",
  675. &rmidev->rmi4_data->input_dev->dev.kobj);
  676. TPD_DMESG("%s:rmidev after kobject_create_and_add\n",__func__);
  677. if (!rmidev->sysfs_dir) {
  678. dev_err(&rmidev->rmi4_data->i2c_client->dev,
  679. "%s: Failed to create sysfs directory\n",
  680. __func__);
  681. goto err_sysfs_dir;
  682. }*/
  683. properties_kobj_rmidev = kobject_create_and_add("rmidev", properties_kobj_synap);
  684. if (!properties_kobj_rmidev) {
  685. dev_err(&rmidev->rmi4_data->i2c_client->dev,
  686. "%s: Failed to create sysfs directory\n", __func__);
  687. goto err_sysfs_dir;
  688. }
  689. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
  690. /* retval = sysfs_create_file(rmidev->sysfs_dir, &attrs[attr_count].attr); */
  691. retval = sysfs_create_file(properties_kobj_rmidev, &attrs[attr_count].attr);
  692. if (retval < 0) {
  693. dev_err(&rmidev->rmi4_data->input_dev->dev,
  694. "%s: Failed to create sysfs attributes\n", __func__);
  695. retval = -ENODEV;
  696. goto err_sysfs_attrs;
  697. }
  698. }
  699. return 0;
  700. err_sysfs_attrs:
  701. for (attr_count--; attr_count >= 0; attr_count--)
  702. sysfs_remove_file(properties_kobj_rmidev, &attrs[attr_count].attr);
  703. kobject_put(properties_kobj_rmidev);
  704. err_sysfs_dir:
  705. err_char_device:
  706. rmidev_device_cleanup(dev_data);
  707. kfree(dev_data);
  708. err_dev_data:
  709. unregister_chrdev_region(dev_no, 1);
  710. err_device_region:
  711. class_destroy(rmidev_device_class);
  712. err_device_class:
  713. kfree(rmidev->rmi4_data);
  714. exit_free_rmi4:
  715. kfree(rmidev);
  716. err_fn_ptr:
  717. kfree(rmidev);
  718. err_rmidev:
  719. return retval;
  720. }
  721. static void rmidev_remove_device(struct i2c_client *client)
  722. {
  723. unsigned char attr_count;
  724. struct rmidev_data *dev_data;
  725. if (!rmidev)
  726. return;
  727. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++)
  728. sysfs_remove_file(rmidev->sysfs_dir, &attrs[attr_count].attr);
  729. kobject_put(rmidev->sysfs_dir);
  730. dev_data = rmidev->data;
  731. if (dev_data) {
  732. rmidev_device_cleanup(dev_data);
  733. kfree(dev_data);
  734. }
  735. unregister_chrdev_region(rmidev->dev_no, 1);
  736. class_destroy(rmidev_device_class);
  737. kfree(rmidev->fn_ptr);
  738. kfree(rmidev);
  739. complete(&remove_complete);
  740. }
  741. static int __init rmidev_module_init(void)
  742. {
  743. synaptics_rmi4_new_function(RMI_DEV, true, rmidev_init_device, rmidev_remove_device, NULL);
  744. return 0;
  745. }
  746. static void __exit rmidev_module_exit(void)
  747. {
  748. init_completion(&remove_complete);
  749. synaptics_rmi4_new_function(RMI_DEV, false, rmidev_init_device, rmidev_remove_device, NULL);
  750. wait_for_completion(&remove_complete);
  751. }
  752. module_init(rmidev_module_init);
  753. module_exit(rmidev_module_exit);
  754. MODULE_AUTHOR("Synaptics, Inc.");
  755. MODULE_DESCRIPTION("RMI4 RMI_Dev Module");
  756. MODULE_LICENSE("GPL");
  757. MODULE_VERSION(SYNAPTICS_RMI4_DRIVER_VERSION);