PageRenderTime 76ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/input/misc/bma150_driver.c

https://gitlab.com/rychly/nst-linux-sources
C | 1058 lines | 896 code | 119 blank | 43 comment | 88 complexity | b139c78d63f120159952a3403ceb4821 MD5 | raw file
  1. /*
  2. * BMA150 linux driver
  3. *
  4. * Usage: BMA150 driver by i2c for linux
  5. *
  6. *
  7. * This software program is licensed subject to the GNU General Public License
  8. * (GPL).Version 2,June 1991, available at http://www.fsf.org/copyleft/gpl.html
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/list.h>
  17. #include <linux/i2c.h>
  18. #include <linux/i2c-dev.h>
  19. #include <asm/uaccess.h>
  20. #include <linux/unistd.h>
  21. #include <linux/delay.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/input.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/irq.h>
  27. #include <mach/gpio.h>
  28. #include <linux/timer.h>
  29. #include <linux/jiffies.h>
  30. #include "smb380.h"
  31. #include "smb380calib.h"
  32. #ifdef BMA150_MODULES
  33. #include "smb380.c"
  34. #include "smb380calib.c"
  35. #endif
  36. #define BMA150_ENABLE_IRQ
  37. #define EVENT_TIPATP ABS_X
  38. #define EVENT_SHAKE ABS_Y
  39. #define EVENT_FFALL ABS_Z
  40. #define BMA150_DEBUG
  41. //#define BMA150_SMBUS
  42. /* i2c operation for bma150 API */
  43. static char bma150_i2c_write(unsigned char reg_addr, unsigned char *data, unsigned char len);
  44. static char bma150_i2c_read(unsigned char reg_addr, unsigned char *data, unsigned char len);
  45. static void bma150_i2c_delay(unsigned int msec);
  46. /* globe variant */
  47. static struct i2c_client *bma150_client = NULL;
  48. struct bma150_data {
  49. smb380_t smb380;
  50. int IRQ;
  51. struct fasync_struct *async_queue;
  52. };
  53. /*definition for GPIO*/
  54. #ifdef BMA150_ENABLE_IRQ
  55. static int bma150_interrupt_config(void);
  56. #define BMA150_IRQ_PIN 133
  57. //#define OMAP_CTRL_READADDR 0x48002000
  58. //#define OMAP_MMC2_DATA1 0x15C+2
  59. #define BMA150_IRQ OMAP_GPIO_IRQ(BMA150_IRQ_PIN)
  60. /* config interval of tap-tip */
  61. #define TAPTIP_INTERVAL 60
  62. #define SHAKE_INTERVAL 40
  63. #define LOW_G_THRES 20
  64. #define LOW_G_DUR 150
  65. #define HIGH_G_THRES 160
  66. #define HIGH_G_DUR 60
  67. #define ANY_MOTION_THRES 30
  68. #define ANY_MOTION_CT 2
  69. #endif
  70. #ifdef BMA150_ENABLE_IRQ
  71. static int bma150_interrupt_config()
  72. {
  73. unsigned char temp;
  74. #ifdef BMA150_DEBUG
  75. printk(KERN_INFO "%s\n",__FUNCTION__);
  76. #endif
  77. /* config interrupt pin for ZOOM_1 omap3430*/
  78. if(gpio_request(BMA150_IRQ_PIN, "bma150_irq") < 0)
  79. {
  80. printk(KERN_ERR "Failed to request GPIO%d for bma150 IRQ\n",
  81. BMA150_IRQ_PIN);
  82. return -1;
  83. }
  84. gpio_direction_input(BMA150_IRQ_PIN);
  85. /* low-g interrupt config: 0.156g, 150ms */
  86. temp = LOW_G_THRES;
  87. smb380_set_low_g_threshold(temp);
  88. temp = LOW_G_DUR;
  89. smb380_set_low_g_duration(temp);
  90. /* high-g interrupt config: 1.50g, 150ms */
  91. temp = HIGH_G_THRES;
  92. smb380_set_high_g_threshold(temp);
  93. temp = HIGH_G_DUR;
  94. smb380_set_high_g_duration(temp);
  95. /* any motion interrupt config: 0.75g, 3 */
  96. temp = ANY_MOTION_THRES;
  97. smb380_set_any_motion_threshold(temp);
  98. temp = ANY_MOTION_CT;
  99. smb380_set_any_motion_count(temp);
  100. return 0;
  101. }
  102. #endif
  103. static irqreturn_t bma150_irq_handler(int irq, void *_id)
  104. {
  105. struct bma150_data *data;
  106. unsigned long flags;
  107. if(((smb380_t*)_id)->chip_id != 0x02)
  108. {
  109. #ifdef BMA150_DEBUG
  110. printk(KERN_INFO "%s error\n",__FUNCTION__);
  111. #endif
  112. return IRQ_HANDLED;
  113. }
  114. if(bma150_client == NULL)
  115. return IRQ_HANDLED;
  116. printk("bma150 irq handler\n");
  117. data = i2c_get_clientdata(bma150_client);
  118. if(data == NULL)
  119. return IRQ_HANDLED;
  120. local_irq_save(flags);
  121. if(data->async_queue)
  122. kill_fasync(&data->async_queue,SIGIO, POLL_IN);
  123. local_irq_restore(flags);
  124. return IRQ_HANDLED;
  125. }
  126. /* i2c delay routine for eeprom */
  127. static inline void bma150_i2c_delay(unsigned int msec)
  128. {
  129. mdelay(msec);
  130. }
  131. /* i2c write routine for bma150 */
  132. static inline char bma150_i2c_write(unsigned char reg_addr, unsigned char *data, unsigned char len)
  133. {
  134. s32 dummy;
  135. #ifndef BMA150_SMBUS
  136. unsigned char buffer[2];
  137. #endif
  138. if( bma150_client == NULL ) /* No global client pointer? */
  139. return -1;
  140. while(len--)
  141. {
  142. #ifdef BMA150_SMBUS
  143. dummy = i2c_smbus_write_byte_data(bma150_client, reg_addr, *data);
  144. #else
  145. buffer[0] = reg_addr;
  146. buffer[1] = *data;
  147. dummy = i2c_master_send(bma150_client, (char*)buffer, 2);
  148. #endif
  149. reg_addr++;
  150. data++;
  151. if(dummy < 0)
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. /* i2c read routine for bma150 */
  157. static inline char bma150_i2c_read(unsigned char reg_addr, unsigned char *data, unsigned char len)
  158. {
  159. s32 dummy;
  160. if( bma150_client == NULL ) /* No global client pointer? */
  161. return -1;
  162. while(len--)
  163. {
  164. #ifdef BMA150_SMBUS
  165. dummy = i2c_smbus_read_byte_data(bma150_client, reg_addr);
  166. if(dummy < 0)
  167. return -1;
  168. *data = dummy & 0x000000ff;
  169. #else
  170. dummy = i2c_master_send(bma150_client, (char*)&reg_addr, 1);
  171. if(dummy < 0)
  172. return -1;
  173. dummy = i2c_master_recv(bma150_client, (char*)data, 1);
  174. if(dummy < 0)
  175. return -1;
  176. #endif
  177. reg_addr++;
  178. data++;
  179. }
  180. return 0;
  181. }
  182. /* read command for BMA150 device file */
  183. static ssize_t bma150_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  184. {
  185. smb380acc_t acc;
  186. int ret;
  187. if( bma150_client == NULL )
  188. {
  189. #ifdef BMA150_DEBUG
  190. printk(KERN_INFO "I2C driver not install\n");
  191. #endif
  192. return -1;
  193. }
  194. smb380_read_accel_xyz(&acc);
  195. #ifdef BMA150_DEBUG
  196. printk(KERN_INFO "BMA150: X/Y/Z axis: %-8d %-8d %-8d\n" ,
  197. (int)acc.x, (int)acc.y, (int)acc.z);
  198. #endif
  199. #if 0
  200. if( count != sizeof(acc) )
  201. {
  202. return -1;
  203. }
  204. #endif
  205. ret = copy_to_user(buf,&acc, sizeof(acc));
  206. if( ret != 0 )
  207. {
  208. #ifdef BMA150_DEBUG
  209. printk(KERN_INFO "BMA150: copy_to_user result: %d\n", ret);
  210. #endif
  211. }
  212. return sizeof(acc);
  213. }
  214. /* write command for BMA150 device file */
  215. static ssize_t bma150_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
  216. {
  217. if( bma150_client == NULL )
  218. return -1;
  219. #ifdef BMA150_DEBUG
  220. printk(KERN_INFO "BMA150 should be accessed with ioctl command\n");
  221. #endif
  222. return 0;
  223. }
  224. /* open command for BMA150 device file */
  225. static int bma150_open(struct inode *inode, struct file *file)
  226. {
  227. #ifdef BMA150_DEBUG
  228. printk(KERN_INFO "%s\n",__FUNCTION__);
  229. #endif
  230. if( bma150_client == NULL)
  231. {
  232. #ifdef BMA150_DEBUG
  233. printk(KERN_INFO "I2C driver not install\n");
  234. #endif
  235. return -1;
  236. }
  237. #ifdef BMA150_DEBUG
  238. printk(KERN_INFO "BMA150 has been opened\n");
  239. #endif
  240. return 0;
  241. }
  242. /* release command for BMA150 device file */
  243. static int bma150_close(struct inode *inode, struct file *file)
  244. {
  245. #ifdef BMA150_DEBUG
  246. printk(KERN_INFO "%s\n",__FUNCTION__);
  247. #endif
  248. return 0;
  249. }
  250. /* ioctl command for BMA150 device file */
  251. static int bma150_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  252. {
  253. int err = 0;
  254. unsigned char data[6];
  255. int temp;
  256. struct bma150_data* pdata;
  257. pdata = i2c_get_clientdata(bma150_client);
  258. #ifdef BMA150_DEBUG
  259. printk(KERN_INFO "%s\n",__FUNCTION__);
  260. #endif
  261. /* check cmd */
  262. if(_IOC_TYPE(cmd) != BMA150_IOC_MAGIC)
  263. {
  264. #ifdef BMA150_DEBUG
  265. printk(KERN_INFO "cmd magic type error\n");
  266. #endif
  267. return -ENOTTY;
  268. }
  269. if(_IOC_NR(cmd) > BMA150_IOC_MAXNR)
  270. {
  271. #ifdef BMA150_DEBUG
  272. printk(KERN_INFO "cmd number error\n");
  273. #endif
  274. return -ENOTTY;
  275. }
  276. if(_IOC_DIR(cmd) & _IOC_READ)
  277. err = !access_ok(VERIFY_WRITE,(void __user*)arg, _IOC_SIZE(cmd));
  278. else if(_IOC_DIR(cmd) & _IOC_WRITE)
  279. err = !access_ok(VERIFY_READ, (void __user*)arg, _IOC_SIZE(cmd));
  280. if(err)
  281. {
  282. #ifdef BMA150_DEBUG
  283. printk(KERN_INFO "cmd access_ok error\n");
  284. #endif
  285. return -EFAULT;
  286. }
  287. /* check bam150_client */
  288. if( bma150_client == NULL)
  289. {
  290. #ifdef BMA150_DEBUG
  291. printk(KERN_INFO "I2C driver not install\n");
  292. #endif
  293. return -EFAULT;
  294. }
  295. /* cmd mapping */
  296. switch(cmd)
  297. {
  298. case BMA150_SOFT_RESET:
  299. err = smb380_soft_reset();
  300. return err;
  301. case BMA150_SET_RANGE:
  302. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  303. {
  304. #ifdef BMA150_DEBUG
  305. printk(KERN_INFO "copy_from_user error\n");
  306. #endif
  307. return -EFAULT;
  308. }
  309. err = smb380_set_range(*data);
  310. return err;
  311. case BMA150_GET_RANGE:
  312. err = smb380_get_range(data);
  313. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  314. {
  315. #ifdef BMA150_DEBUG
  316. printk(KERN_INFO "copy_to_user error\n");
  317. #endif
  318. return -EFAULT;
  319. }
  320. return err;
  321. case BMA150_SET_MODE:
  322. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  323. {
  324. #ifdef BMA150_DEBUG
  325. printk(KERN_INFO "copy_from_user error\n");
  326. #endif
  327. return -EFAULT;
  328. }
  329. err = smb380_set_mode(*data);
  330. return err;
  331. case BMA150_GET_MODE:
  332. err = smb380_get_mode(data);
  333. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  334. {
  335. #ifdef BMA150_DEBUG
  336. printk(KERN_INFO "copy_to_user error\n");
  337. #endif
  338. return -EFAULT;
  339. }
  340. return err;
  341. case BMA150_SET_BANDWIDTH:
  342. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  343. {
  344. #ifdef BMA150_DEBUG
  345. printk(KERN_INFO "copy_from_user error\n");
  346. #endif
  347. return -EFAULT;
  348. }
  349. err = smb380_set_bandwidth(*data);
  350. return err;
  351. case BMA150_GET_BANDWIDTH:
  352. err = smb380_get_bandwidth(data);
  353. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  354. {
  355. #ifdef BMA150_DEBUG
  356. printk(KERN_INFO "copy_to_user error\n");
  357. #endif
  358. return -EFAULT;
  359. }
  360. return err;
  361. case BMA150_SET_WAKE_UP_PAUSE:
  362. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  363. {
  364. #ifdef BMA150_DEBUG
  365. printk(KERN_INFO "copy_from_user error\n");
  366. #endif
  367. return -EFAULT;
  368. }
  369. err = smb380_set_wake_up_pause(*data);
  370. return err;
  371. case BMA150_GET_WAKE_UP_PAUSE:
  372. err = smb380_get_wake_up_pause(data);
  373. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  374. {
  375. #ifdef BMA150_DEBUG
  376. printk(KERN_INFO "copy_to_user error\n");
  377. #endif
  378. return -EFAULT;
  379. }
  380. return err;
  381. case BMA150_SET_LOW_G_THRESHOLD:
  382. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  383. {
  384. #ifdef BMA150_DEBUG
  385. printk(KERN_INFO "copy_from_user error\n");
  386. #endif
  387. return -EFAULT;
  388. }
  389. err = smb380_set_low_g_threshold(*data);
  390. return err;
  391. case BMA150_GET_LOW_G_THRESHOLD:
  392. err = smb380_get_low_g_threshold(data);
  393. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  394. {
  395. #ifdef BMA150_DEBUG
  396. printk(KERN_INFO "copy_to_user error\n");
  397. #endif
  398. return -EFAULT;
  399. }
  400. return err;
  401. case BMA150_SET_LOW_G_COUNTDOWN:
  402. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  403. {
  404. #ifdef BMA150_DEBUG
  405. printk(KERN_INFO "copy_from_user error\n");
  406. #endif
  407. return -EFAULT;
  408. }
  409. err = smb380_set_low_g_countdown(*data);
  410. return err;
  411. case BMA150_GET_LOW_G_COUNTDOWN:
  412. err = smb380_get_low_g_countdown(data);
  413. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  414. {
  415. #ifdef BMA150_DEBUG
  416. printk(KERN_INFO "copy_to_user error\n");
  417. #endif
  418. return -EFAULT;
  419. }
  420. return err;
  421. case BMA150_SET_HIGH_G_COUNTDOWN:
  422. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  423. {
  424. #ifdef BMA150_DEBUG
  425. printk(KERN_INFO "copy_from_user error\n");
  426. #endif
  427. return -EFAULT;
  428. }
  429. err = smb380_set_high_g_countdown(*data);
  430. return err;
  431. case BMA150_GET_HIGH_G_COUNTDOWN:
  432. err = smb380_get_high_g_countdown(data);
  433. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  434. {
  435. #ifdef BMA150_DEBUG
  436. printk(KERN_INFO "copy_to_user error\n");
  437. #endif
  438. return -EFAULT;
  439. }
  440. return err;
  441. case BMA150_SET_LOW_G_DURATION:
  442. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  443. {
  444. #ifdef BMA150_DEBUG
  445. printk(KERN_INFO "copy_from_user error\n");
  446. #endif
  447. return -EFAULT;
  448. }
  449. err = smb380_set_low_g_duration(*data);
  450. return err;
  451. case BMA150_GET_LOW_G_DURATION:
  452. err = smb380_get_low_g_duration(data);
  453. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  454. {
  455. #ifdef BMA150_DEBUG
  456. printk(KERN_INFO "copy_to_user error\n");
  457. #endif
  458. return -EFAULT;
  459. }
  460. return err;
  461. case BMA150_SET_HIGH_G_THRESHOLD:
  462. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  463. {
  464. #ifdef BMA150_DEBUG
  465. printk(KERN_INFO "copy_from_user error\n");
  466. #endif
  467. return -EFAULT;
  468. }
  469. err = smb380_set_high_g_threshold(*data);
  470. return err;
  471. case BMA150_GET_HIGH_G_THRESHOLD:
  472. err = smb380_get_high_g_threshold(data);
  473. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  474. {
  475. #ifdef BMA150_DEBUG
  476. printk(KERN_INFO "copy_to_user error\n");
  477. #endif
  478. return -EFAULT;
  479. }
  480. return err;
  481. case BMA150_SET_HIGH_G_DURATION:
  482. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  483. {
  484. #ifdef BMA150_DEBUG
  485. printk(KERN_INFO "copy_from_user error\n");
  486. #endif
  487. return -EFAULT;
  488. }
  489. err = smb380_set_high_g_duration(*data);
  490. return err;
  491. case BMA150_GET_HIGH_G_DURATION:
  492. err = smb380_get_high_g_duration(data);
  493. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  494. {
  495. #ifdef BMA150_DEBUG
  496. printk(KERN_INFO "copy_to_user error\n");
  497. #endif
  498. return -EFAULT;
  499. }
  500. return err;
  501. case BMA150_SET_ANY_MOTION_THRESHOLD:
  502. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  503. {
  504. #ifdef BMA150_DEBUG
  505. printk(KERN_INFO "copy_from_user error\n");
  506. #endif
  507. return -EFAULT;
  508. }
  509. err = smb380_set_any_motion_threshold(*data);
  510. return err;
  511. case BMA150_GET_ANY_MOTION_THRESHOLD:
  512. err = smb380_get_any_motion_threshold(data);
  513. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  514. {
  515. #ifdef BMA150_DEBUG
  516. printk(KERN_INFO "copy_to_user error\n");
  517. #endif
  518. return -EFAULT;
  519. }
  520. return err;
  521. case BMA150_SET_ANY_MOTION_COUNT:
  522. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  523. {
  524. #ifdef BMA150_DEBUG
  525. printk(KERN_INFO "copy_from_user error\n");
  526. #endif
  527. return -EFAULT;
  528. }
  529. err = smb380_set_any_motion_count(*data);
  530. return err;
  531. case BMA150_GET_ANY_MOTION_COUNT:
  532. err = smb380_get_any_motion_count(data);
  533. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  534. {
  535. #ifdef BMA150_DEBUG
  536. printk(KERN_INFO "copy_to_user error\n");
  537. #endif
  538. return -EFAULT;
  539. }
  540. return err;
  541. case BMA150_SET_INTERRUPT_MASK:
  542. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  543. {
  544. #ifdef BMA150_DEBUG
  545. printk(KERN_INFO "copy_from_user error\n");
  546. #endif
  547. return -EFAULT;
  548. }
  549. err = smb380_set_interrupt_mask(*data);
  550. return err;
  551. case BMA150_GET_INTERRUPT_MASK:
  552. err = smb380_get_interrupt_mask(data);
  553. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  554. {
  555. #ifdef BMA150_DEBUG
  556. printk(KERN_INFO "copy_to_user error\n");
  557. #endif
  558. return -EFAULT;
  559. }
  560. return err;
  561. case BMA150_RESET_INTERRUPT:
  562. err = smb380_reset_interrupt();
  563. return err;
  564. case BMA150_READ_ACCEL_X:
  565. err = smb380_read_accel_x((short*)data);
  566. if(copy_to_user((short*)arg,(short*)data,2)!=0)
  567. {
  568. #ifdef BMA150_DEBUG
  569. printk(KERN_INFO "copy_to_user error\n");
  570. #endif
  571. return -EFAULT;
  572. }
  573. return err;
  574. case BMA150_READ_ACCEL_Y:
  575. err = smb380_read_accel_y((short*)data);
  576. if(copy_to_user((short*)arg,(short*)data,2)!=0)
  577. {
  578. #ifdef BMA150_DEBUG
  579. printk(KERN_INFO "copy_to_user error\n");
  580. #endif
  581. return -EFAULT;
  582. }
  583. return err;
  584. case BMA150_READ_ACCEL_Z:
  585. err = smb380_read_accel_z((short*)data);
  586. if(copy_to_user((short*)arg,(short*)data,2)!=0)
  587. {
  588. #ifdef BMA150_DEBUG
  589. printk(KERN_INFO "copy_to_user error\n");
  590. #endif
  591. return -EFAULT;
  592. }
  593. return err;
  594. case BMA150_GET_INTERRUPT_STATUS:
  595. err = smb380_get_interrupt_status(data);
  596. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  597. {
  598. #ifdef BMA150_DEBUG
  599. printk(KERN_INFO "copy_to_user error\n");
  600. #endif
  601. return -EFAULT;
  602. }
  603. return err;
  604. case BMA150_SET_LOW_G_INT:
  605. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  606. {
  607. #ifdef BMA150_DEBUG
  608. printk(KERN_INFO "copy_from_user error\n");
  609. #endif
  610. return -EFAULT;
  611. }
  612. err = smb380_set_low_g_int(*data);
  613. return err;
  614. case BMA150_SET_HIGH_G_INT:
  615. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  616. {
  617. #ifdef BMA150_DEBUG
  618. printk(KERN_INFO "copy_from_user error\n");
  619. #endif
  620. return -EFAULT;
  621. }
  622. err = smb380_set_high_g_int(*data);
  623. return err;
  624. case BMA150_SET_ANY_MOTION_INT:
  625. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  626. {
  627. #ifdef BMA150_DEBUG
  628. printk(KERN_INFO "copy_from_user error\n");
  629. #endif
  630. return -EFAULT;
  631. }
  632. err = smb380_set_any_motion_int(*data);
  633. return err;
  634. case BMA150_SET_ALERT_INT:
  635. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  636. {
  637. #ifdef BMA150_DEBUG
  638. printk(KERN_INFO "copy_from_user error\n");
  639. #endif
  640. return -EFAULT;
  641. }
  642. err = smb380_set_alert_int(*data);
  643. return err;
  644. case BMA150_SET_ADVANCED_INT:
  645. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  646. {
  647. #ifdef BMA150_DEBUG
  648. printk(KERN_INFO "copy_from_user error\n");
  649. #endif
  650. return -EFAULT;
  651. }
  652. err = smb380_set_advanced_int(*data);
  653. return err;
  654. case BMA150_LATCH_INT:
  655. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  656. {
  657. #ifdef BMA150_DEBUG
  658. printk(KERN_INFO "copy_from_user error\n");
  659. #endif
  660. return -EFAULT;
  661. }
  662. err = smb380_latch_int(*data);
  663. return err;
  664. case BMA150_SET_NEW_DATA_INT:
  665. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  666. {
  667. #ifdef BMA150_DEBUG
  668. printk(KERN_INFO "copy_from_user error\n");
  669. #endif
  670. return -EFAULT;
  671. }
  672. err = smb380_set_new_data_int(*data);
  673. return err;
  674. case BMA150_GET_LOW_G_HYST:
  675. err = smb380_get_low_g_hysteresis(data);
  676. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  677. {
  678. #ifdef BMA150_DEBUG
  679. printk(KERN_INFO "copy_to_user error\n");
  680. #endif
  681. return -EFAULT;
  682. }
  683. return err;
  684. case BMA150_SET_LOW_G_HYST:
  685. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  686. {
  687. #ifdef BMA150_DEBUG
  688. printk(KERN_INFO "copy_from_user error\n");
  689. #endif
  690. return -EFAULT;
  691. }
  692. err = smb380_set_low_g_hysteresis(*data);
  693. return err;
  694. case BMA150_GET_HIGH_G_HYST:
  695. err = smb380_get_high_g_hysteresis(data);
  696. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  697. {
  698. #ifdef BMA150_DEBUG
  699. printk(KERN_INFO "copy_to_user error\n");
  700. #endif
  701. return -EFAULT;
  702. }
  703. return err;
  704. case BMA150_SET_HIGH_G_HYST:
  705. if(copy_from_user(data,(unsigned char*)arg,1)!=0)
  706. {
  707. #ifdef BMA150_DEBUG
  708. printk(KERN_INFO "copy_from_user error\n");
  709. #endif
  710. return -EFAULT;
  711. }
  712. err = smb380_set_high_g_hysteresis(*data);
  713. return err;
  714. case BMA150_READ_ACCEL_XYZ:
  715. err = smb380_read_accel_xyz((smb380acc_t*)data);
  716. if(copy_to_user((smb380acc_t*)arg,(smb380acc_t*)data,6)!=0)
  717. {
  718. #ifdef BMA150_DEBUG
  719. printk(KERN_INFO "copy_to error\n");
  720. #endif
  721. return -EFAULT;
  722. }
  723. return err;
  724. case BMA150_READ_TEMPERATURE:
  725. err = smb380_read_temperature(data);
  726. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  727. {
  728. #ifdef BMA150_DEBUG
  729. printk(KERN_INFO "copy_to_user error\n");
  730. #endif
  731. return -EFAULT;
  732. }
  733. return err;
  734. /* offset calibration routine */
  735. case BMA150_CALIBRATION:
  736. if(copy_from_user((smb380acc_t*)data,(smb380acc_t*)arg,6)!=0)
  737. {
  738. #ifdef BMA150_DEBUG
  739. printk(KERN_INFO "copy_from_user error\n");
  740. #endif
  741. return -EFAULT;
  742. }
  743. /* iteration time 20 */
  744. temp = 20;
  745. err = smb380_calibrate(*(smb380acc_t*)data, &temp);
  746. return err;
  747. case BMA150_READ_INT:
  748. //data[0] = omap_get_gpio_datain(BMA150_IRQ_PIN);
  749. smb380_read_reg(0x0B,data,1);
  750. smb380_read_reg(0x15,data+1,1);
  751. data[0] = data[0] & 0x43;
  752. data[1] = data[1] & 0x40;
  753. data[1] <<= 1;
  754. data[0] |= data[1];
  755. if(copy_to_user((unsigned char*)arg,data,1)!=0)
  756. {
  757. #ifdef BMA150_DEBUG
  758. printk(KERN_INFO "copy_to_user error\n");
  759. #endif
  760. return -EFAULT;
  761. }
  762. return err;
  763. default:
  764. return 0;
  765. }
  766. }
  767. static int bma150_fasync(int fd, struct file *file, int mode)
  768. {
  769. struct bma150_data* data;
  770. #ifdef BMA150_DEBUG
  771. printk(KERN_INFO "%s\n",__FUNCTION__);
  772. #endif
  773. data=i2c_get_clientdata(bma150_client);
  774. return fasync_helper(fd,file,mode,&data->async_queue);
  775. }
  776. static const struct file_operations bma150_fops = {
  777. .owner = THIS_MODULE,
  778. .read = bma150_read,
  779. .write = bma150_write,
  780. .open = bma150_open,
  781. .release = bma150_close,
  782. .ioctl = bma150_ioctl,
  783. .fasync = bma150_fasync,
  784. };
  785. /* May 4th 2009 modified*
  786. * add miscdevices for bma
  787. */
  788. static struct miscdevice bma_device = {
  789. .minor = MISC_DYNAMIC_MINOR,
  790. .name = "bma150",
  791. .fops = &bma150_fops,
  792. };
  793. static int bma150_detect(struct i2c_client *client, int kind,
  794. struct i2c_board_info *info)
  795. {
  796. struct i2c_adapter *adapter = client->adapter;
  797. #ifdef BMA150_DEBUG
  798. printk(KERN_INFO "%s\n", __FUNCTION__);
  799. #endif
  800. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  801. return -ENODEV;
  802. strlcpy(info->type, "bma150", I2C_NAME_SIZE);
  803. return 0;
  804. }
  805. static int bma150_probe(struct i2c_client *client,
  806. const struct i2c_device_id *id)
  807. {
  808. int err = 0;
  809. int tempvalue;
  810. struct bma150_data *data;
  811. #ifdef BMA150_DEBUG
  812. printk(KERN_INFO "%s\n",__FUNCTION__);
  813. #endif
  814. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  815. {
  816. printk(KERN_INFO "i2c_check_functionality error\n");
  817. goto exit;
  818. }
  819. data = kzalloc(sizeof(struct bma150_data), GFP_KERNEL);
  820. if (!data) {
  821. err = -ENOMEM;
  822. goto exit;
  823. }
  824. /* read chip id */
  825. tempvalue = 0;
  826. #ifdef BMA150_SMBUS
  827. tempvalue = i2c_smbus_read_word_data(client, 0x00);
  828. #else
  829. i2c_master_send(client, (char*)&tempvalue, 1);
  830. i2c_master_recv(client, (char*)&tempvalue, 1);
  831. #endif
  832. if((tempvalue & 0x00FF) == 0x0002)
  833. {
  834. printk(KERN_INFO "Bosch Sensortec Device detected!\nBMA150/SMB380 registered I2C driver!\n");
  835. bma150_client = client;
  836. }
  837. else
  838. {
  839. printk(KERN_INFO "Bosch Sensortec Device not found, i2c error %d \n", tempvalue);
  840. i2c_detach_client(client); /*modified on Apr 27 2009*/
  841. bma150_client = NULL;
  842. err = -1;
  843. goto kfree_exit;
  844. }
  845. i2c_set_clientdata(bma150_client, data);
  846. /* May 4th modified for device create*/
  847. err = misc_register(&bma_device);
  848. if (err) {
  849. printk(KERN_ERR "bma150 device register failed\n");
  850. goto kfree_exit;
  851. }
  852. printk(KERN_INFO "bma150 device create ok\n");
  853. /* bma150 sensor initial */
  854. data->smb380.bus_write = bma150_i2c_write;
  855. data->smb380.bus_read = bma150_i2c_read;
  856. data->smb380.delay_msec = bma150_i2c_delay;
  857. smb380_init(&data->smb380);
  858. smb380_set_bandwidth(4); //bandwidth 375Hz
  859. smb380_set_range(0); //range +/-2G
  860. //close all interrupt
  861. smb380_set_low_g_int(0);
  862. smb380_set_high_g_int(0);
  863. smb380_set_any_motion_int(0);
  864. smb380_set_alert_int(0);
  865. smb380_set_advanced_int(1);
  866. smb380_reset_interrupt();
  867. /* register interrupt */
  868. #ifdef BMA150_ENABLE_IRQ
  869. err = bma150_interrupt_config();
  870. if (err < 0)
  871. goto exit_dereg;
  872. data->IRQ = BMA150_IRQ;
  873. err = request_irq(data->IRQ, bma150_irq_handler, IRQF_TRIGGER_RISING, "bma150", &data->smb380);
  874. if (err)
  875. {
  876. printk(KERN_ERR "could not request irq\n");
  877. goto exit_dereg;
  878. }
  879. #endif
  880. return 0;
  881. #ifdef BMA150_ENABLE_IRQ
  882. exit_dereg:
  883. misc_deregister(&bma_device);
  884. #endif
  885. kfree_exit:
  886. kfree(data);
  887. exit:
  888. return err;
  889. }
  890. static int bma150_remove(struct i2c_client *client)
  891. {
  892. struct bma150_data *data = i2c_get_clientdata(client);
  893. #ifdef BMA150_DEBUG
  894. printk(KERN_INFO "%s\n",__FUNCTION__);
  895. #endif
  896. misc_deregister(&bma_device);
  897. #ifdef BMA150_ENABLE_IRQ
  898. free_irq(data->IRQ, &data->smb380);
  899. #endif
  900. i2c_detach_client(client);
  901. kfree(data);
  902. bma150_client = NULL;
  903. return 0;
  904. }
  905. static unsigned short normal_i2c[] = { 0x38, I2C_CLIENT_END};
  906. I2C_CLIENT_INSMOD_1(bma150);
  907. static const struct i2c_device_id bma150_id[] = {
  908. { "bma150", 0 },
  909. { }
  910. };
  911. MODULE_DEVICE_TABLE(i2c, bma150_id);
  912. static struct i2c_driver bma150_driver = {
  913. .driver = {
  914. .owner = THIS_MODULE,
  915. .name = "bma150",
  916. },
  917. .class = I2C_CLASS_HWMON,
  918. .id_table = bma150_id,
  919. .address_data = &addr_data,
  920. .probe = bma150_probe,
  921. .remove = bma150_remove,
  922. .detect = bma150_detect,
  923. };
  924. static int __init BMA150_init(void)
  925. {
  926. #ifdef BMA150_DEBUG
  927. printk(KERN_INFO "%s\n",__FUNCTION__);
  928. #endif
  929. return i2c_add_driver(&bma150_driver);
  930. }
  931. static void __exit BMA150_exit(void)
  932. {
  933. i2c_del_driver(&bma150_driver);
  934. printk(KERN_ERR "BMA150 exit\n");
  935. }
  936. MODULE_DESCRIPTION("BMA150 driver");
  937. MODULE_LICENSE("GPL");
  938. module_init(BMA150_init);
  939. module_exit(BMA150_exit);