sx9310.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. #include <linux/interrupt.h>
  2. #include <linux/i2c.h>
  3. #include <linux/slab.h>
  4. #include <linux/irq.h>
  5. #include <linux/gpio.h>
  6. #include <linux/miscdevice.h>
  7. #include <asm/uaccess.h>
  8. #include <linux/delay.h>
  9. #include <linux/input.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/kobject.h>
  12. #include <linux/platform_device.h>
  13. #include <asm/atomic.h>
  14. #include <asm/io.h>
  15. #include <asm-generic/gpio.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include "sx9310.h"
  21. /*----------------------------------------------------------------------------*/
  22. #define SX9310_DEV_NAME "SX9310"
  23. #define SAR_DEBUG_ENABLE
  24. /*----------------------------------------------------------------------------*/
  25. #define SAR_TAG "[SAR]"
  26. #ifdef SAR_DEBUG_ENABLE
  27. #define SAR_FUN(f) printk(KERN_ERR SAR_TAG"%s\n", __FUNCTION__)
  28. #define SAR_ERR(fmt, args...) printk(KERN_ERR SAR_TAG"%s %d : "fmt, __FUNCTION__, __LINE__, ##args)
  29. #define SAR_ERR_ST(f) printk(KERN_ERR SAR_TAG"%s %d : ", __FUNCTION__, __LINE__)
  30. #define SAR_LOG(fmt, args...) printk(KERN_ERR SAR_TAG fmt, ##args)
  31. #define SAR_DBG(fmt, args...) printk(KERN_ERR SAR_TAG fmt, ##args)
  32. #else
  33. #define SAR_FUN(f)
  34. #define SAR_ERR(fmt, args...) printk(KERN_ERR SAR_TAG"%s %d : "fmt, __FUNCTION__, __LINE__, ##args)
  35. #define SAR_ERR_ST(f)
  36. #define SAR_LOG(fmt, args...) printk(KERN_ERR SAR_TAG fmt, ##args)
  37. #define SAR_DBG(fmt, args...)
  38. #endif
  39. /*----------------------------------------------------------------------------*/
  40. //ioctl
  41. #define SAR 0X84
  42. #define GET_SAR_MSB_DATA _IOR(SAR, 0x09, int)
  43. #define GET_SAR_LSB_DATA _IOR(SAR, 0x10, int)
  44. /*----------------------------------------------------------------------------*/
  45. struct platform_device * sar_pltfm_dev;
  46. static const struct i2c_device_id sx9310_i2c_id[] = {{SX9310_DEV_NAME,0},{}};
  47. static struct i2c_client * sx9310_i2c_client = NULL;
  48. static psx93XX_t sx9310_obj = NULL;
  49. static int SAR_EINT_PIN;
  50. /*----------------------------------------------------------------------------*/
  51. static int sx9310_probe(struct platform_device *pdev);
  52. static int sx9310_remove(struct platform_device *pdev);
  53. static int sx9310_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
  54. static int sx9310_i2c_remove(struct i2c_client *client);
  55. static int sx9310_open(struct inode *inode, struct file *file);
  56. static long sx9310_unlocked_ioctl(struct file *file, unsigned int cmd,unsigned long arg);
  57. /*----------------------------------------------------------------------------*/
  58. typedef struct sx9310
  59. {
  60. pbuttonInformation_t pbuttonInformation;
  61. psx9310_platform_data_t hw; /* specific platform data settings */
  62. } sx9310_t, *psx9310_t;
  63. /*----------------------------------------------------------------------------*/
  64. static struct _totalButtonInformation smtcButtonInformation = {
  65. .buttons = psmtcButtons,
  66. .buttonSize = ARRAY_SIZE(psmtcButtons),
  67. };
  68. /*----------------------------------------------------------------------------*/
  69. static const struct of_device_id sar_of_pltmatch[] = {
  70. {.compatible = "mediatek,sarsensor",},
  71. {},
  72. };
  73. /*----------------------------------------------------------------------------*/
  74. static const struct of_device_id sar_of_i2cmatch[] = {
  75. {.compatible = "mediatek,SAR",},
  76. {},
  77. };
  78. /*----------------------------------------------------------------------------*/
  79. static struct platform_driver sx9310_driver = {
  80. .probe = sx9310_probe,
  81. .remove = sx9310_remove,
  82. .driver = {
  83. .name = "sar",
  84. .of_match_table = sar_of_pltmatch,
  85. }
  86. };
  87. /*----------------------------------------------------------------------------*/
  88. static struct i2c_driver sx9310_i2c_driver = {
  89. .probe = sx9310_i2c_probe,
  90. .remove = sx9310_i2c_remove,
  91. .id_table = sx9310_i2c_id,
  92. .driver = {
  93. .name = SX9310_DEV_NAME,
  94. .of_match_table = sar_of_i2cmatch,
  95. },
  96. };
  97. /*----------------------------------------------------------------------------*/
  98. static struct file_operations sx9310_fops = {
  99. .open = sx9310_open,
  100. .unlocked_ioctl = sx9310_unlocked_ioctl,
  101. };
  102. /*----------------------------------------------------------------------------*/
  103. static struct miscdevice sx9310_device = {
  104. .minor = MISC_DYNAMIC_MINOR,
  105. .name = "sar",
  106. .fops = &sx9310_fops,
  107. };
  108. /*----------------------------------------------------------------------------*/
  109. static int sx9310_get_nirq_state(void)
  110. {
  111. return !gpio_get_value(SAR_EINT_PIN);
  112. }
  113. /*----------------------------------------------------------------------------*/
  114. static sx9310_platform_data_t sx9310_config = {
  115. /* Function pointer to get the NIRQ state (1->NIRQ-low, 0->NIRQ-high) */
  116. .get_is_nirq_low = sx9310_get_nirq_state,
  117. /* pointer to an initializer function. Here in case needed in the future */
  118. .init_platform_hw = NULL,
  119. /* pointer to an exit function. Here in case needed in the future */
  120. .exit_platform_hw = NULL,
  121. .pi2c_reg = sx9310_i2c_reg_setup,
  122. .i2c_reg_num = ARRAY_SIZE(sx9310_i2c_reg_setup),
  123. .pbuttonInformation = &smtcButtonInformation,
  124. };
  125. /*----------------------------------------------------------------------------*/
  126. static void ForcetoTouched(psx93XX_t this)
  127. {
  128. psx9310_t pDevice = NULL;
  129. struct input_dev *input = NULL;
  130. struct _buttonInfo *pCurrentButton = NULL;
  131. if (this && (pDevice = this->pDevice))
  132. {
  133. SAR_FUN();
  134. pCurrentButton = pDevice->pbuttonInformation->buttons;
  135. input = pDevice->pbuttonInformation->input;
  136. //modify xmysx20161027
  137. //input_report_key(input, pCurrentButton->keycode, 1);
  138. input_report_key(input, KEY_SAR_ACTIVE, 1);
  139. input_sync(input);
  140. input_report_key(input, KEY_SAR_ACTIVE, 0);
  141. pCurrentButton->state = ACTIVE;
  142. input_sync(input);
  143. }
  144. }
  145. /*----------------------------------------------------------------------------*/
  146. static int write_register(psx93XX_t this, u8 address, u8 value)
  147. {
  148. struct i2c_client * client;
  149. char buffer[2];
  150. int returnValue = 0;
  151. buffer[0] = address;
  152. buffer[1] = value;
  153. returnValue = -ENOMEM;
  154. if (this && this->bus) {
  155. client = this->bus;
  156. returnValue = i2c_master_send(client,buffer,2);
  157. SAR_LOG("write_register Address: 0x%x Value: 0x%x Return: %d\n",
  158. address,value,returnValue);
  159. }
  160. if(returnValue < 0){
  161. ForcetoTouched(this);
  162. SAR_LOG("Write_register-ForcetoTouched()\n");
  163. }
  164. return returnValue;
  165. }
  166. /*----------------------------------------------------------------------------*/
  167. static int read_register(psx93XX_t this, u8 address, u8 *value)
  168. {
  169. struct i2c_client * client = 0;
  170. s32 returnValue = 0;
  171. if (this && value && this->bus) {
  172. client = this->bus;
  173. returnValue = i2c_smbus_read_byte_data(client,address);
  174. SAR_LOG("read_register Address: 0x%x Return: 0x%x\n",address,returnValue);
  175. if (returnValue >= 0) {
  176. *value = returnValue;
  177. return 0;
  178. } else {
  179. return returnValue;
  180. }
  181. }
  182. ForcetoTouched(this);
  183. SAR_LOG("read_register-ForcetoTouched()\n");
  184. return -ENOMEM;
  185. }
  186. /*----------------------------------------------------------------------------*/
  187. static int read_regStat(psx93XX_t this)
  188. {
  189. u8 data = 0;
  190. if (this) {
  191. if (read_register(this,SX9310_IRQSTAT_REG,&data) == 0)
  192. return (data & 0x00FF);
  193. }
  194. return 0;
  195. }
  196. /*----------------------------------------------------------------------------*/
  197. static int manual_offset_calibration(psx93XX_t this)
  198. {
  199. s32 returnValue = 0;
  200. returnValue = write_register(this,SX9310_IRQSTAT_REG,0xFF);
  201. return returnValue;
  202. }
  203. /*----------------------------------------------------------------------------*/
  204. static int sx9310_open(struct inode *inode, struct file *file)
  205. {
  206. file->private_data = sx9310_i2c_client;
  207. if (!file->private_data)
  208. {
  209. SAR_ERR("null pointer!!\n");
  210. return -EINVAL;
  211. }
  212. return nonseekable_open(inode, file);
  213. }
  214. /*----------------------------------------------------------------------------*/
  215. static long sx9310_unlocked_ioctl(struct file *file, unsigned int cmd,unsigned long arg)
  216. {
  217. psx93XX_t this = i2c_get_clientdata(sx9310_i2c_client);
  218. int err = 0;
  219. void __user *ptr = (void __user*) arg;
  220. u8 dat;
  221. switch (cmd)
  222. {
  223. case GET_SAR_MSB_DATA:
  224. read_register(this,SX9310_DIFFMSB,&dat);
  225. if(copy_to_user(ptr, &dat, sizeof(dat)))
  226. {
  227. err = -EFAULT;
  228. goto err_out;
  229. }
  230. break;
  231. case GET_SAR_LSB_DATA:
  232. read_register(this,SX9310_DIFFLSB,&dat);
  233. if(copy_to_user(ptr, &dat, sizeof(dat)))
  234. {
  235. err = -EFAULT;
  236. goto err_out;
  237. }
  238. break;
  239. default:
  240. SAR_ERR("%s not supported = 0x%04x", __FUNCTION__, cmd);
  241. err = -ENOIOCTLCMD;
  242. break;
  243. }
  244. err_out:
  245. return err;
  246. }
  247. /*----------------------------------------------------------------------------*/
  248. static ssize_t sx9310_show_sarreg(struct device_driver *ddri, char *buf)
  249. {
  250. u8 Value1 = -1;
  251. u8 Value2 = -1;
  252. u8 Value3 = -1;
  253. u8 Value4 = -1;
  254. read_register(sx9310_obj,SX9310_STAT0_REG,&Value1);
  255. read_register(sx9310_obj,SX9310_STAT1_REG,&Value2);
  256. read_register(sx9310_obj,SX9310_DIFFMSB,&Value3);
  257. read_register(sx9310_obj,SX9310_DIFFLSB,&Value4);
  258. return sprintf(buf,"STAT0:0x%02X STAT1:0x%02X DIFFMSB:0x%02X DIFFLSB:0x%02X\n"
  259. ,Value1,Value2,Value3,Value4);
  260. }
  261. /*----------------------------------------------------------------------------*/
  262. static ssize_t sx9310_show_nirq(struct device_driver *ddri, char *buf)
  263. {
  264. u8 value;
  265. value = sx9310_obj->refreshStatus(sx9310_obj);
  266. return sprintf(buf,"reset NIRQ %s, val:%d\n",(value != 0)?"success":"fail", value);
  267. }
  268. /*----------------------------------------------------------------------------*/
  269. static ssize_t sx9310_show_reset(struct device_driver *ddri, char *buf)
  270. {
  271. u8 value;
  272. value = sx9310_obj->init(sx9310_obj);
  273. return sprintf(buf,"reset chip %s\n",(value == 0)?"success":"fail");
  274. }
  275. /*----------------------------------------------------------------------------*/
  276. static DRIVER_ATTR(sarreg,S_IWUSR | S_IRUGO,sx9310_show_sarreg,NULL);
  277. static DRIVER_ATTR(nirq,S_IWUSR | S_IRUGO,sx9310_show_nirq,NULL);
  278. static DRIVER_ATTR(rst,S_IWUSR | S_IRUGO,sx9310_show_reset,NULL);
  279. /*----------------------------------------------------------------------------*/
  280. static struct driver_attribute *sx9310_attr_list[] = {
  281. &driver_attr_sarreg,
  282. &driver_attr_nirq,
  283. &driver_attr_rst,
  284. };
  285. /*----------------------------------------------------------------------------*/
  286. static int sx9310_create_attr(struct device_driver *driver)
  287. {
  288. int idx, err = 0;
  289. int num = (int)(sizeof(sx9310_attr_list)/sizeof(sx9310_attr_list[0]));
  290. if (driver == NULL)
  291. {
  292. return -EINVAL;
  293. }
  294. for(idx = 0; idx < num; idx++)
  295. {
  296. err = driver_create_file(driver, sx9310_attr_list[idx]);
  297. if(err)
  298. {
  299. SAR_ERR("driver_create_file (%s) = %d\n", sx9310_attr_list[idx]->attr.name, err);
  300. break;
  301. }
  302. }
  303. return err;
  304. }
  305. /*----------------------------------------------------------------------------*/
  306. static int sx9310_delete_attr(struct device_driver *driver)
  307. {
  308. int idx;
  309. int num = (int)(sizeof(sx9310_attr_list)/sizeof(sx9310_attr_list[0]));
  310. if (!driver)
  311. return -EINVAL;
  312. for (idx = 0; idx < num; idx++)
  313. {
  314. driver_remove_file(driver, sx9310_attr_list[idx]);
  315. }
  316. return 0;
  317. }
  318. /*----------------------------------------------------------------------------*/
  319. static int hw_init(psx93XX_t this)
  320. {
  321. psx9310_t pDevice = 0;
  322. psx9310_platform_data_t pdata = 0;
  323. int i = 0;
  324. int err;
  325. /* configure device */
  326. SAR_LOG("Going to Setup I2C Registers\n");
  327. if (this && (pDevice = this->pDevice) && (pdata = pDevice->hw))
  328. {
  329. while ( i < pdata->i2c_reg_num) {
  330. /* Write all registers/values contained in i2c_reg */
  331. SAR_LOG("Going to Write Reg: 0x%x Value: 0x%x\n",
  332. pdata->pi2c_reg[i].reg,pdata->pi2c_reg[i].val);
  333. err = write_register(this, pdata->pi2c_reg[i].reg,pdata->pi2c_reg[i].val);
  334. if(err < 0)
  335. return -1;
  336. i++;
  337. }
  338. } else {
  339. SAR_ERR("ERROR! platform data 0x%p\n",pDevice->hw);
  340. //Force to touched if error
  341. ForcetoTouched(this);
  342. SAR_ERR("Hardware_init-ForcetoTouched()\n");
  343. }
  344. return 0;
  345. }
  346. /*----------------------------------------------------------------------------*/
  347. static int initialize(psx93XX_t this)
  348. {
  349. int err;
  350. /* perform a reset */
  351. err = write_register(this,SX9310_SOFTRESET_REG,SX9310_SOFTRESET);
  352. if(err < 0)
  353. goto init_fail;
  354. /* wait until the reset has finished by monitoring NIRQ */
  355. SAR_LOG("Sent Software Reset. Waiting until device is back from reset to continue.\n");
  356. /* just sleep for awhile instead of using a loop with reading irq status */
  357. msleep(300);
  358. err = hw_init(this);
  359. if(err < 0)
  360. goto init_fail;
  361. msleep(100); /* make sure everything is running */
  362. err = manual_offset_calibration(this);
  363. if(err < 0)
  364. goto init_fail;
  365. read_regStat(this); //need to release NIRQ
  366. return 0;
  367. init_fail:
  368. return -1;
  369. }
  370. /*----------------------------------------------------------------------------*/
  371. static void touchProcess(psx93XX_t this)
  372. {
  373. int counter = 0;
  374. u8 i = 0;
  375. int numberOfButtons = 0;
  376. psx9310_t pDevice = this->pDevice;
  377. struct _buttonInfo *buttons = NULL;
  378. struct input_dev *input = NULL;
  379. struct _buttonInfo *pCurrentButton = NULL;
  380. SAR_FUN();
  381. read_register(this, SX9310_STAT0_REG, &i);
  382. buttons = pDevice->pbuttonInformation->buttons;
  383. input = pDevice->pbuttonInformation->input;
  384. numberOfButtons = pDevice->pbuttonInformation->buttonSize;
  385. if (unlikely( (buttons==NULL) || (input==NULL) )) {
  386. SAR_ERR("button or input device is NULL\n");
  387. return;
  388. }
  389. for (counter = 0; counter < numberOfButtons; counter++) {
  390. pCurrentButton = &buttons[counter];
  391. if (pCurrentButton==NULL) {
  392. SAR_ERR("current button at index: %d is NULL\n", counter);
  393. return;
  394. }
  395. switch (pCurrentButton->state) {
  396. case IDLE: /* Button is not being touched! */
  397. if (((i & pCurrentButton->mask) == pCurrentButton->mask)) {
  398. /* User pressed button */
  399. SAR_LOG("cap button %d touched\n",counter);
  400. input_report_key(input, KEY_SAR_ACTIVE, 1);
  401. input_sync(input);
  402. input_report_key(input, KEY_SAR_ACTIVE, 0);
  403. input_sync(input);
  404. pCurrentButton->state = ACTIVE;
  405. } else {
  406. SAR_LOG("Button %d already released\n",counter);
  407. }
  408. break;
  409. case ACTIVE: /* Button is being touched! */
  410. if (((i & pCurrentButton->mask) != pCurrentButton->mask)) {
  411. /* User released button */
  412. SAR_LOG("cap button %d released\n",counter);
  413. input_report_key(input, KEY_SAR_IDLE, 1);
  414. input_sync(input);
  415. input_report_key(input, KEY_SAR_IDLE, 0);
  416. input_sync(input);
  417. pCurrentButton->state = IDLE;
  418. } else {
  419. SAR_LOG("Button %d still touched\n",counter);
  420. }
  421. break;
  422. default: /* Shouldn't be here, device only allowed ACTIVE or IDLE */
  423. break;
  424. };
  425. }
  426. }
  427. /*----------------------------------------------------------------------------*/
  428. static void sx9310_eint_work(struct work_struct *work)
  429. {
  430. psx93XX_t this = 0;
  431. int status = 0;
  432. int counter = 0;
  433. this = container_of(work,sx93XX_t,dworker.work);
  434. status = this->refreshStatus(this);
  435. counter = -1;
  436. SAR_LOG("Worker - Refresh Status %d\n",status);
  437. while((++counter) < MAX_NUM_STATUS_BITS) { /* counter start from MSB */
  438. if (((status>>counter) & 0x01) && (this->statusFunc[counter])) {
  439. SAR_LOG("Function Pointer Found. Calling\n");
  440. this->statusFunc[counter](this);
  441. }
  442. }
  443. enable_irq(this->irq);
  444. }
  445. /*----------------------------------------------------------------------------*/
  446. static irqreturn_t sx9310_eint_handler(int irq, void *desc)
  447. {
  448. disable_irq_nosync(sx9310_obj->irq);
  449. SAR_FUN();
  450. schedule_delayed_work(&sx9310_obj->dworker,0);
  451. return IRQ_HANDLED;
  452. }
  453. /*----------------------------------------------------------------------------*/
  454. static int sx9310_setup_eint(struct i2c_client *client)
  455. {
  456. int ret;
  457. struct pinctrl * pinctrl;
  458. struct pinctrl_state * pins_default;
  459. struct pinctrl_state * pins_cfg;
  460. u32 ints[2] = {0,0};
  461. SAR_FUN();
  462. /* gpio setting */
  463. pinctrl = devm_pinctrl_get(&sar_pltfm_dev->dev);
  464. if (IS_ERR(pinctrl)) {
  465. ret = PTR_ERR(pinctrl);
  466. SAR_ERR("Cannot find alsps pinctrl!\n");
  467. return ret;
  468. }
  469. pins_default = pinctrl_lookup_state(pinctrl, "pin_default");
  470. if (IS_ERR(pins_default)) {
  471. ret = PTR_ERR(pins_default);
  472. SAR_ERR("Cannot find alsps pinctrl default!\n");
  473. }
  474. pins_cfg = pinctrl_lookup_state(pinctrl, "pin_cfg");
  475. if (IS_ERR(pins_cfg)) {
  476. ret = PTR_ERR(pins_cfg);
  477. SAR_ERR("Cannot find alsps pinctrl pin_cfg!\n");
  478. return ret;
  479. }
  480. pinctrl_select_state(pinctrl, pins_cfg);
  481. /* eint request */
  482. sx9310_obj->irq_node = of_find_matching_node(sx9310_obj->irq_node, sar_of_pltmatch);
  483. if (sx9310_obj->irq_node) {
  484. of_property_read_u32_array(sx9310_obj->irq_node, "debounce", ints, ARRAY_SIZE(ints));
  485. //gpio_request(ints[0], "sarsensor");
  486. SAR_EINT_PIN = ints[0];
  487. gpio_set_debounce(ints[0], ints[1]);
  488. SAR_LOG("ints[0] = %d, ints[1] = %d!!\n", ints[0], ints[1]);
  489. sx9310_obj->irq = irq_of_parse_and_map(sx9310_obj->irq_node, 0);
  490. SAR_LOG("sx9310_obj->irq = %d\n", sx9310_obj->irq);
  491. if (!sx9310_obj->irq) {
  492. SAR_ERR("irq_of_parse_and_map fail!!\n");
  493. return -EINVAL;
  494. }
  495. if (request_irq(sx9310_obj->irq, sx9310_eint_handler, IRQF_TRIGGER_NONE, "SAR-eint", NULL)) {
  496. SAR_ERR("IRQ LINE NOT AVAILABLE!!\n");
  497. return -EINVAL;
  498. }
  499. } else {
  500. SAR_ERR("null irq node!!\n");
  501. return -EINVAL;
  502. }
  503. return 0;
  504. }
  505. /*----------------------------------------------------------------------------*/
  506. static int sx9310_init_client(psx93XX_t this)
  507. {
  508. int res = 0;
  509. SAR_FUN();
  510. this->init(this);
  511. if(res != 0)
  512. {
  513. SAR_ERR("SX9310 init failed\n");
  514. return res;
  515. }
  516. res = sx9310_setup_eint(sx9310_i2c_client);
  517. if(res != 0)
  518. {
  519. SAR_ERR("setup eint: %d\n", res);
  520. return res;
  521. }
  522. return res;
  523. }
  524. /*----------------------------------------------------------------------------*/
  525. static int sx9310_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  526. {
  527. int err = 0;
  528. psx93XX_t this;
  529. psx9310_t pDevice;
  530. psx9310_platform_data_t pplatData;
  531. struct input_dev * input = NULL;
  532. int i;
  533. SAR_FUN();
  534. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)){
  535. SAR_ERR("i2c check error\n");
  536. return -EIO;
  537. }
  538. this = kzalloc(sizeof(sx93XX_t), GFP_KERNEL); /* create memory for main struct */
  539. if(this == NULL){
  540. err = -ENOMEM;
  541. goto exit;
  542. }
  543. pplatData = &sx9310_config;
  544. /* In case we need to reinitialize data
  545. * (e.q. if suspend reset device) */
  546. this->init = initialize;
  547. /* shortcut to read status of interrupt */
  548. this->refreshStatus = read_regStat;
  549. /* pointer to function from platform data to get pendown
  550. * (1->NIRQ=0, 0->NIRQ=1) */
  551. this->get_nirq_low = pplatData->get_is_nirq_low;
  552. /* do we need to create an irq timer after interrupt ? */
  553. this->useIrqTimer = 0;
  554. /* Setup function to call on corresponding reg irq source bit */
  555. if (MAX_NUM_STATUS_BITS>= 8)
  556. {
  557. this->statusFunc[0] = 0; /* TXEN_STAT */
  558. this->statusFunc[1] = 0; /* UNUSED */
  559. this->statusFunc[2] = 0; /* UNUSED */
  560. this->statusFunc[3] = 0; /* CONV_STAT */
  561. this->statusFunc[4] = 0; /* COMP_STAT */
  562. this->statusFunc[5] = touchProcess; /* RELEASE_STAT */
  563. this->statusFunc[6] = touchProcess; /* TOUCH_STAT */
  564. this->statusFunc[7] = 0; /* RESET_STAT */
  565. }
  566. /* create memory for device specific struct */
  567. this->pDevice = pDevice = kzalloc(sizeof(sx9310_t), GFP_KERNEL);
  568. if(this->pDevice == NULL){
  569. err = -ENOMEM;
  570. goto exit;
  571. }
  572. /* Check if we hava a platform initialization function to call*/
  573. if (pplatData->init_platform_hw)
  574. pplatData->init_platform_hw();
  575. /* Add Pointer to main platform data struct */
  576. pDevice->hw = pplatData;
  577. /* Initialize the button information initialized with keycodes */
  578. pDevice->pbuttonInformation = pplatData->pbuttonInformation;
  579. /* Create the input device */
  580. input = input_allocate_device();
  581. if (!input) {
  582. SAR_ERR("allocate input device failed\n");
  583. err = -ENOMEM;
  584. goto exit;
  585. }
  586. /* Set all the keycodes */
  587. __set_bit(EV_KEY, input->evbit);
  588. for (i = 0; i < pDevice->pbuttonInformation->buttonSize; i++) {
  589. __set_bit(pDevice->pbuttonInformation->buttons[i].keycode,
  590. input->keybit);
  591. pDevice->pbuttonInformation->buttons[i].state = IDLE;
  592. }
  593. __set_bit(KEY_SAR_ACTIVE, input->keybit);
  594. __set_bit(KEY_SAR_IDLE, input->keybit);
  595. /* save the input pointer and finish initialization */
  596. pDevice->pbuttonInformation->input = input;
  597. input->name = "SX9310 Cap Touch";
  598. input->id.bustype = BUS_I2C;
  599. if(input_register_device(input)){
  600. SAR_ERR("register input device failed\n");
  601. err = -ENOMEM;
  602. goto exit;
  603. }
  604. /* initialize worker function */
  605. INIT_DELAYED_WORK(&this->dworker, sx9310_eint_work);
  606. this->bus = client;
  607. sx9310_i2c_client = client;
  608. i2c_set_clientdata(client, this);
  609. sx9310_obj = this;
  610. err = sx9310_init_client(this);
  611. if(err)
  612. {
  613. goto exit;
  614. }
  615. err = sx9310_create_attr(&sx9310_driver.driver);
  616. if(err)
  617. {
  618. goto exit;
  619. }
  620. err = misc_register(&sx9310_device);
  621. if(err)
  622. {
  623. SAR_ERR("sx9310_device register failed\n");
  624. goto exit;
  625. }
  626. SAR_LOG("%s: OK\n", __func__);
  627. return 0;
  628. exit:
  629. sx9310_i2c_client = NULL;
  630. SAR_ERR("%s: err = %d\n", __func__, err);
  631. return err;
  632. }
  633. /*----------------------------------------------------------------------------*/
  634. static int sx9310_i2c_remove(struct i2c_client *client)
  635. {
  636. int err = 0;
  637. err = sx9310_delete_attr(&sx9310_driver.driver);
  638. if(err)
  639. {
  640. SAR_ERR("sx9310_delete_attr fail: %d\n", err);
  641. }
  642. sx9310_i2c_client = NULL;
  643. i2c_unregister_device(client);
  644. kfree(i2c_get_clientdata(client));
  645. return 0;
  646. }
  647. /*----------------------------------------------------------------------------*/
  648. static int sx9310_probe(struct platform_device *pdev)
  649. {
  650. SAR_FUN();
  651. sar_pltfm_dev = pdev;
  652. i2c_add_driver(&sx9310_i2c_driver);
  653. return 0;
  654. }
  655. /*----------------------------------------------------------------------------*/
  656. static int sx9310_remove(struct platform_device *pdev)
  657. {
  658. SAR_FUN();
  659. return 0;
  660. }
  661. /*----------------------------------------------------------------------------*/
  662. static int __init sx9310_init(void)
  663. {
  664. SAR_FUN();
  665. platform_driver_register(&sx9310_driver);
  666. return 0;
  667. }
  668. /*----------------------------------------------------------------------------*/
  669. static void __exit sx9310_exit(void)
  670. {
  671. SAR_FUN();
  672. }
  673. /*----------------------------------------------------------------------------*/
  674. module_init(sx9310_init);
  675. module_exit(sx9310_exit);
  676. /*----------------------------------------------------------------------------*/
  677. MODULE_DESCRIPTION("SX9310 SAR SENSOR Driver");
  678. MODULE_LICENSE("GPL");