bma150.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. * Copyright (c) 2011 Bosch Sensortec GmbH
  3. * Copyright (c) 2011 Unixphere
  4. *
  5. * This driver adds support for Bosch Sensortec's digital acceleration
  6. * sensors BMA150 and SMB380.
  7. * The SMB380 is fully compatible with BMA150 and only differs in packaging.
  8. *
  9. * The datasheet for the BMA150 chip can be found here:
  10. * http://www.bosch-sensortec.com/content/language1/downloads/BST-BMA150-DS000-07.pdf
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/i2c.h>
  29. #include <linux/input.h>
  30. #include <linux/input-polldev.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/delay.h>
  33. #include <linux/slab.h>
  34. #include <linux/pm.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/bma150.h>
  37. #define ABSMAX_ACC_VAL 0x01FF
  38. #define ABSMIN_ACC_VAL -(ABSMAX_ACC_VAL)
  39. /* Each axis is represented by a 2-byte data word */
  40. #define BMA150_XYZ_DATA_SIZE 6
  41. /* Input poll interval in milliseconds */
  42. #define BMA150_POLL_INTERVAL 10
  43. #define BMA150_POLL_MAX 200
  44. #define BMA150_POLL_MIN 0
  45. #define BMA150_BW_25HZ 0
  46. #define BMA150_BW_50HZ 1
  47. #define BMA150_BW_100HZ 2
  48. #define BMA150_BW_190HZ 3
  49. #define BMA150_BW_375HZ 4
  50. #define BMA150_BW_750HZ 5
  51. #define BMA150_BW_1500HZ 6
  52. #define BMA150_RANGE_2G 0
  53. #define BMA150_RANGE_4G 1
  54. #define BMA150_RANGE_8G 2
  55. #define BMA150_MODE_NORMAL 0
  56. #define BMA150_MODE_SLEEP 2
  57. #define BMA150_MODE_WAKE_UP 3
  58. /* Data register addresses */
  59. #define BMA150_DATA_0_REG 0x00
  60. #define BMA150_DATA_1_REG 0x01
  61. #define BMA150_DATA_2_REG 0x02
  62. /* Control register addresses */
  63. #define BMA150_CTRL_0_REG 0x0A
  64. #define BMA150_CTRL_1_REG 0x0B
  65. #define BMA150_CTRL_2_REG 0x14
  66. #define BMA150_CTRL_3_REG 0x15
  67. /* Configuration/Setting register addresses */
  68. #define BMA150_CFG_0_REG 0x0C
  69. #define BMA150_CFG_1_REG 0x0D
  70. #define BMA150_CFG_2_REG 0x0E
  71. #define BMA150_CFG_3_REG 0x0F
  72. #define BMA150_CFG_4_REG 0x10
  73. #define BMA150_CFG_5_REG 0x11
  74. #define BMA150_CHIP_ID 2
  75. #define BMA150_CHIP_ID_REG BMA150_DATA_0_REG
  76. #define BMA150_ACC_X_LSB_REG BMA150_DATA_2_REG
  77. #define BMA150_SLEEP_POS 0
  78. #define BMA150_SLEEP_MSK 0x01
  79. #define BMA150_SLEEP_REG BMA150_CTRL_0_REG
  80. #define BMA150_BANDWIDTH_POS 0
  81. #define BMA150_BANDWIDTH_MSK 0x07
  82. #define BMA150_BANDWIDTH_REG BMA150_CTRL_2_REG
  83. #define BMA150_RANGE_POS 3
  84. #define BMA150_RANGE_MSK 0x18
  85. #define BMA150_RANGE_REG BMA150_CTRL_2_REG
  86. #define BMA150_WAKE_UP_POS 0
  87. #define BMA150_WAKE_UP_MSK 0x01
  88. #define BMA150_WAKE_UP_REG BMA150_CTRL_3_REG
  89. #define BMA150_SW_RES_POS 1
  90. #define BMA150_SW_RES_MSK 0x02
  91. #define BMA150_SW_RES_REG BMA150_CTRL_0_REG
  92. /* Any-motion interrupt register fields */
  93. #define BMA150_ANY_MOTION_EN_POS 6
  94. #define BMA150_ANY_MOTION_EN_MSK 0x40
  95. #define BMA150_ANY_MOTION_EN_REG BMA150_CTRL_1_REG
  96. #define BMA150_ANY_MOTION_DUR_POS 6
  97. #define BMA150_ANY_MOTION_DUR_MSK 0xC0
  98. #define BMA150_ANY_MOTION_DUR_REG BMA150_CFG_5_REG
  99. #define BMA150_ANY_MOTION_THRES_REG BMA150_CFG_4_REG
  100. /* Advanced interrupt register fields */
  101. #define BMA150_ADV_INT_EN_POS 6
  102. #define BMA150_ADV_INT_EN_MSK 0x40
  103. #define BMA150_ADV_INT_EN_REG BMA150_CTRL_3_REG
  104. /* High-G interrupt register fields */
  105. #define BMA150_HIGH_G_EN_POS 1
  106. #define BMA150_HIGH_G_EN_MSK 0x02
  107. #define BMA150_HIGH_G_EN_REG BMA150_CTRL_1_REG
  108. #define BMA150_HIGH_G_HYST_POS 3
  109. #define BMA150_HIGH_G_HYST_MSK 0x38
  110. #define BMA150_HIGH_G_HYST_REG BMA150_CFG_5_REG
  111. #define BMA150_HIGH_G_DUR_REG BMA150_CFG_3_REG
  112. #define BMA150_HIGH_G_THRES_REG BMA150_CFG_2_REG
  113. /* Low-G interrupt register fields */
  114. #define BMA150_LOW_G_EN_POS 0
  115. #define BMA150_LOW_G_EN_MSK 0x01
  116. #define BMA150_LOW_G_EN_REG BMA150_CTRL_1_REG
  117. #define BMA150_LOW_G_HYST_POS 0
  118. #define BMA150_LOW_G_HYST_MSK 0x07
  119. #define BMA150_LOW_G_HYST_REG BMA150_CFG_5_REG
  120. #define BMA150_LOW_G_DUR_REG BMA150_CFG_1_REG
  121. #define BMA150_LOW_G_THRES_REG BMA150_CFG_0_REG
  122. struct bma150_data {
  123. struct i2c_client *client;
  124. struct input_polled_dev *input_polled;
  125. struct input_dev *input;
  126. u8 mode;
  127. };
  128. /*
  129. * The settings for the given range, bandwidth and interrupt features
  130. * are stated and verified by Bosch Sensortec where they are configured
  131. * to provide a generic sensitivity performance.
  132. */
  133. static struct bma150_cfg default_cfg __devinitdata = {
  134. .any_motion_int = 1,
  135. .hg_int = 1,
  136. .lg_int = 1,
  137. .any_motion_dur = 0,
  138. .any_motion_thres = 0,
  139. .hg_hyst = 0,
  140. .hg_dur = 150,
  141. .hg_thres = 160,
  142. .lg_hyst = 0,
  143. .lg_dur = 150,
  144. .lg_thres = 20,
  145. .range = BMA150_RANGE_2G,
  146. .bandwidth = BMA150_BW_50HZ
  147. };
  148. static int bma150_write_byte(struct i2c_client *client, u8 reg, u8 val)
  149. {
  150. s32 ret;
  151. /* As per specification, disable irq in between register writes */
  152. if (client->irq)
  153. disable_irq_nosync(client->irq);
  154. ret = i2c_smbus_write_byte_data(client, reg, val);
  155. if (client->irq)
  156. enable_irq(client->irq);
  157. return ret;
  158. }
  159. static int bma150_set_reg_bits(struct i2c_client *client,
  160. int val, int shift, u8 mask, u8 reg)
  161. {
  162. int data;
  163. data = i2c_smbus_read_byte_data(client, reg);
  164. if (data < 0)
  165. return data;
  166. data = (data & ~mask) | ((val << shift) & mask);
  167. return bma150_write_byte(client, reg, data);
  168. }
  169. static int bma150_set_mode(struct bma150_data *bma150, u8 mode)
  170. {
  171. int error;
  172. error = bma150_set_reg_bits(bma150->client, mode, BMA150_WAKE_UP_POS,
  173. BMA150_WAKE_UP_MSK, BMA150_WAKE_UP_REG);
  174. if (error)
  175. return error;
  176. error = bma150_set_reg_bits(bma150->client, mode, BMA150_SLEEP_POS,
  177. BMA150_SLEEP_MSK, BMA150_SLEEP_REG);
  178. if (error)
  179. return error;
  180. if (mode == BMA150_MODE_NORMAL)
  181. msleep(2);
  182. bma150->mode = mode;
  183. return 0;
  184. }
  185. static int __devinit bma150_soft_reset(struct bma150_data *bma150)
  186. {
  187. int error;
  188. error = bma150_set_reg_bits(bma150->client, 1, BMA150_SW_RES_POS,
  189. BMA150_SW_RES_MSK, BMA150_SW_RES_REG);
  190. if (error)
  191. return error;
  192. msleep(2);
  193. return 0;
  194. }
  195. static int __devinit bma150_set_range(struct bma150_data *bma150, u8 range)
  196. {
  197. return bma150_set_reg_bits(bma150->client, range, BMA150_RANGE_POS,
  198. BMA150_RANGE_MSK, BMA150_RANGE_REG);
  199. }
  200. static int __devinit bma150_set_bandwidth(struct bma150_data *bma150, u8 bw)
  201. {
  202. return bma150_set_reg_bits(bma150->client, bw, BMA150_BANDWIDTH_POS,
  203. BMA150_BANDWIDTH_MSK, BMA150_BANDWIDTH_REG);
  204. }
  205. static int __devinit bma150_set_low_g_interrupt(struct bma150_data *bma150,
  206. u8 enable, u8 hyst, u8 dur, u8 thres)
  207. {
  208. int error;
  209. error = bma150_set_reg_bits(bma150->client, hyst,
  210. BMA150_LOW_G_HYST_POS, BMA150_LOW_G_HYST_MSK,
  211. BMA150_LOW_G_HYST_REG);
  212. if (error)
  213. return error;
  214. error = bma150_write_byte(bma150->client, BMA150_LOW_G_DUR_REG, dur);
  215. if (error)
  216. return error;
  217. error = bma150_write_byte(bma150->client, BMA150_LOW_G_THRES_REG, thres);
  218. if (error)
  219. return error;
  220. return bma150_set_reg_bits(bma150->client, !!enable,
  221. BMA150_LOW_G_EN_POS, BMA150_LOW_G_EN_MSK,
  222. BMA150_LOW_G_EN_REG);
  223. }
  224. static int __devinit bma150_set_high_g_interrupt(struct bma150_data *bma150,
  225. u8 enable, u8 hyst, u8 dur, u8 thres)
  226. {
  227. int error;
  228. error = bma150_set_reg_bits(bma150->client, hyst,
  229. BMA150_HIGH_G_HYST_POS, BMA150_HIGH_G_HYST_MSK,
  230. BMA150_HIGH_G_HYST_REG);
  231. if (error)
  232. return error;
  233. error = bma150_write_byte(bma150->client,
  234. BMA150_HIGH_G_DUR_REG, dur);
  235. if (error)
  236. return error;
  237. error = bma150_write_byte(bma150->client,
  238. BMA150_HIGH_G_THRES_REG, thres);
  239. if (error)
  240. return error;
  241. return bma150_set_reg_bits(bma150->client, !!enable,
  242. BMA150_HIGH_G_EN_POS, BMA150_HIGH_G_EN_MSK,
  243. BMA150_HIGH_G_EN_REG);
  244. }
  245. static int __devinit bma150_set_any_motion_interrupt(struct bma150_data *bma150,
  246. u8 enable, u8 dur, u8 thres)
  247. {
  248. int error;
  249. error = bma150_set_reg_bits(bma150->client, dur,
  250. BMA150_ANY_MOTION_DUR_POS,
  251. BMA150_ANY_MOTION_DUR_MSK,
  252. BMA150_ANY_MOTION_DUR_REG);
  253. if (error)
  254. return error;
  255. error = bma150_write_byte(bma150->client,
  256. BMA150_ANY_MOTION_THRES_REG, thres);
  257. if (error)
  258. return error;
  259. error = bma150_set_reg_bits(bma150->client, !!enable,
  260. BMA150_ADV_INT_EN_POS, BMA150_ADV_INT_EN_MSK,
  261. BMA150_ADV_INT_EN_REG);
  262. if (error)
  263. return error;
  264. return bma150_set_reg_bits(bma150->client, !!enable,
  265. BMA150_ANY_MOTION_EN_POS,
  266. BMA150_ANY_MOTION_EN_MSK,
  267. BMA150_ANY_MOTION_EN_REG);
  268. }
  269. static void bma150_report_xyz(struct bma150_data *bma150)
  270. {
  271. u8 data[BMA150_XYZ_DATA_SIZE];
  272. s16 x, y, z;
  273. s32 ret;
  274. ret = i2c_smbus_read_i2c_block_data(bma150->client,
  275. BMA150_ACC_X_LSB_REG, BMA150_XYZ_DATA_SIZE, data);
  276. if (ret != BMA150_XYZ_DATA_SIZE)
  277. return;
  278. x = ((0xc0 & data[0]) >> 6) | (data[1] << 2);
  279. y = ((0xc0 & data[2]) >> 6) | (data[3] << 2);
  280. z = ((0xc0 & data[4]) >> 6) | (data[5] << 2);
  281. /* sign extension */
  282. x = (s16) (x << 6) >> 6;
  283. y = (s16) (y << 6) >> 6;
  284. z = (s16) (z << 6) >> 6;
  285. input_report_abs(bma150->input, ABS_X, x);
  286. input_report_abs(bma150->input, ABS_Y, y);
  287. input_report_abs(bma150->input, ABS_Z, z);
  288. input_sync(bma150->input);
  289. }
  290. static irqreturn_t bma150_irq_thread(int irq, void *dev)
  291. {
  292. bma150_report_xyz(dev);
  293. return IRQ_HANDLED;
  294. }
  295. static void bma150_poll(struct input_polled_dev *dev)
  296. {
  297. bma150_report_xyz(dev->private);
  298. }
  299. static int bma150_open(struct bma150_data *bma150)
  300. {
  301. int error;
  302. error = pm_runtime_get_sync(&bma150->client->dev);
  303. if (error && error != -ENOSYS)
  304. return error;
  305. /*
  306. * See if runtime PM woke up the device. If runtime PM
  307. * is disabled we need to do it ourselves.
  308. */
  309. if (bma150->mode != BMA150_MODE_NORMAL) {
  310. error = bma150_set_mode(bma150, BMA150_MODE_NORMAL);
  311. if (error)
  312. return error;
  313. }
  314. return 0;
  315. }
  316. static void bma150_close(struct bma150_data *bma150)
  317. {
  318. pm_runtime_put_sync(&bma150->client->dev);
  319. if (bma150->mode != BMA150_MODE_SLEEP)
  320. bma150_set_mode(bma150, BMA150_MODE_SLEEP);
  321. }
  322. static int bma150_irq_open(struct input_dev *input)
  323. {
  324. struct bma150_data *bma150 = input_get_drvdata(input);
  325. return bma150_open(bma150);
  326. }
  327. static void bma150_irq_close(struct input_dev *input)
  328. {
  329. struct bma150_data *bma150 = input_get_drvdata(input);
  330. bma150_close(bma150);
  331. }
  332. static void bma150_poll_open(struct input_polled_dev *ipoll_dev)
  333. {
  334. struct bma150_data *bma150 = ipoll_dev->private;
  335. bma150_open(bma150);
  336. }
  337. static void bma150_poll_close(struct input_polled_dev *ipoll_dev)
  338. {
  339. struct bma150_data *bma150 = ipoll_dev->private;
  340. bma150_close(bma150);
  341. }
  342. static int __devinit bma150_initialize(struct bma150_data *bma150,
  343. const struct bma150_cfg *cfg)
  344. {
  345. int error;
  346. error = bma150_soft_reset(bma150);
  347. if (error)
  348. return error;
  349. error = bma150_set_bandwidth(bma150, cfg->bandwidth);
  350. if (error)
  351. return error;
  352. error = bma150_set_range(bma150, cfg->range);
  353. if (error)
  354. return error;
  355. if (bma150->client->irq) {
  356. error = bma150_set_any_motion_interrupt(bma150,
  357. cfg->any_motion_int,
  358. cfg->any_motion_dur,
  359. cfg->any_motion_thres);
  360. if (error)
  361. return error;
  362. error = bma150_set_high_g_interrupt(bma150,
  363. cfg->hg_int, cfg->hg_hyst,
  364. cfg->hg_dur, cfg->hg_thres);
  365. if (error)
  366. return error;
  367. error = bma150_set_low_g_interrupt(bma150,
  368. cfg->lg_int, cfg->lg_hyst,
  369. cfg->lg_dur, cfg->lg_thres);
  370. if (error)
  371. return error;
  372. }
  373. return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
  374. }
  375. static void __devinit bma150_init_input_device(struct bma150_data *bma150,
  376. struct input_dev *idev)
  377. {
  378. idev->name = BMA150_DRIVER;
  379. idev->phys = BMA150_DRIVER "/input0";
  380. idev->id.bustype = BUS_I2C;
  381. idev->dev.parent = &bma150->client->dev;
  382. idev->evbit[0] = BIT_MASK(EV_ABS);
  383. input_set_abs_params(idev, ABS_X, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
  384. input_set_abs_params(idev, ABS_Y, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
  385. input_set_abs_params(idev, ABS_Z, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
  386. }
  387. static int __devinit bma150_register_input_device(struct bma150_data *bma150)
  388. {
  389. struct input_dev *idev;
  390. int error;
  391. idev = input_allocate_device();
  392. if (!idev)
  393. return -ENOMEM;
  394. bma150_init_input_device(bma150, idev);
  395. idev->open = bma150_irq_open;
  396. idev->close = bma150_irq_close;
  397. input_set_drvdata(idev, bma150);
  398. error = input_register_device(idev);
  399. if (error) {
  400. input_free_device(idev);
  401. return error;
  402. }
  403. bma150->input = idev;
  404. return 0;
  405. }
  406. static int __devinit bma150_register_polled_device(struct bma150_data *bma150)
  407. {
  408. struct input_polled_dev *ipoll_dev;
  409. int error;
  410. ipoll_dev = input_allocate_polled_device();
  411. if (!ipoll_dev)
  412. return -ENOMEM;
  413. ipoll_dev->private = bma150;
  414. ipoll_dev->open = bma150_poll_open;
  415. ipoll_dev->close = bma150_poll_close;
  416. ipoll_dev->poll = bma150_poll;
  417. ipoll_dev->poll_interval = BMA150_POLL_INTERVAL;
  418. ipoll_dev->poll_interval_min = BMA150_POLL_MIN;
  419. ipoll_dev->poll_interval_max = BMA150_POLL_MAX;
  420. bma150_init_input_device(bma150, ipoll_dev->input);
  421. error = input_register_polled_device(ipoll_dev);
  422. if (error) {
  423. input_free_polled_device(ipoll_dev);
  424. return error;
  425. }
  426. bma150->input_polled = ipoll_dev;
  427. bma150->input = ipoll_dev->input;
  428. return 0;
  429. }
  430. static int __devinit bma150_probe(struct i2c_client *client,
  431. const struct i2c_device_id *id)
  432. {
  433. const struct bma150_platform_data *pdata = client->dev.platform_data;
  434. const struct bma150_cfg *cfg;
  435. struct bma150_data *bma150;
  436. int chip_id;
  437. int error;
  438. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  439. dev_err(&client->dev, "i2c_check_functionality error\n");
  440. return -EIO;
  441. }
  442. chip_id = i2c_smbus_read_byte_data(client, BMA150_CHIP_ID_REG);
  443. if (chip_id != BMA150_CHIP_ID) {
  444. dev_err(&client->dev, "BMA150 chip id error: %d\n", chip_id);
  445. return -EINVAL;
  446. }
  447. bma150 = kzalloc(sizeof(struct bma150_data), GFP_KERNEL);
  448. if (!bma150)
  449. return -ENOMEM;
  450. bma150->client = client;
  451. if (pdata) {
  452. if (pdata->irq_gpio_cfg) {
  453. error = pdata->irq_gpio_cfg();
  454. if (error) {
  455. dev_err(&client->dev,
  456. "IRQ GPIO conf. error %d, error %d\n",
  457. client->irq, error);
  458. goto err_free_mem;
  459. }
  460. }
  461. cfg = &pdata->cfg;
  462. } else {
  463. cfg = &default_cfg;
  464. }
  465. error = bma150_initialize(bma150, cfg);
  466. if (error)
  467. goto err_free_mem;
  468. if (client->irq > 0) {
  469. error = bma150_register_input_device(bma150);
  470. if (error)
  471. goto err_free_mem;
  472. error = request_threaded_irq(client->irq,
  473. NULL, bma150_irq_thread,
  474. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  475. BMA150_DRIVER, bma150);
  476. if (error) {
  477. dev_err(&client->dev,
  478. "irq request failed %d, error %d\n",
  479. client->irq, error);
  480. input_unregister_device(bma150->input);
  481. goto err_free_mem;
  482. }
  483. } else {
  484. error = bma150_register_polled_device(bma150);
  485. if (error)
  486. goto err_free_mem;
  487. }
  488. i2c_set_clientdata(client, bma150);
  489. pm_runtime_enable(&client->dev);
  490. return 0;
  491. err_free_mem:
  492. kfree(bma150);
  493. return error;
  494. }
  495. static int __devexit bma150_remove(struct i2c_client *client)
  496. {
  497. struct bma150_data *bma150 = i2c_get_clientdata(client);
  498. pm_runtime_disable(&client->dev);
  499. if (client->irq > 0) {
  500. free_irq(client->irq, bma150);
  501. input_unregister_device(bma150->input);
  502. } else {
  503. input_unregister_polled_device(bma150->input_polled);
  504. input_free_polled_device(bma150->input_polled);
  505. }
  506. kfree(bma150);
  507. return 0;
  508. }
  509. #ifdef CONFIG_PM
  510. static int bma150_suspend(struct device *dev)
  511. {
  512. struct i2c_client *client = to_i2c_client(dev);
  513. struct bma150_data *bma150 = i2c_get_clientdata(client);
  514. return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
  515. }
  516. static int bma150_resume(struct device *dev)
  517. {
  518. struct i2c_client *client = to_i2c_client(dev);
  519. struct bma150_data *bma150 = i2c_get_clientdata(client);
  520. return bma150_set_mode(bma150, BMA150_MODE_NORMAL);
  521. }
  522. #endif
  523. static UNIVERSAL_DEV_PM_OPS(bma150_pm, bma150_suspend, bma150_resume, NULL);
  524. static const struct i2c_device_id bma150_id[] = {
  525. { "bma150", 0 },
  526. { "smb380", 0 },
  527. { "bma023", 0 },
  528. { }
  529. };
  530. MODULE_DEVICE_TABLE(i2c, bma150_id);
  531. static struct i2c_driver bma150_driver = {
  532. .driver = {
  533. .owner = THIS_MODULE,
  534. .name = BMA150_DRIVER,
  535. .pm = &bma150_pm,
  536. },
  537. .class = I2C_CLASS_HWMON,
  538. .id_table = bma150_id,
  539. .probe = bma150_probe,
  540. .remove = __devexit_p(bma150_remove),
  541. };
  542. static int __init BMA150_init(void)
  543. {
  544. return i2c_add_driver(&bma150_driver);
  545. }
  546. static void __exit BMA150_exit(void)
  547. {
  548. i2c_del_driver(&bma150_driver);
  549. }
  550. MODULE_AUTHOR("Albert Zhang <xu.zhang@bosch-sensortec.com>");
  551. MODULE_DESCRIPTION("BMA150 driver");
  552. MODULE_LICENSE("GPL");
  553. module_init(BMA150_init);
  554. module_exit(BMA150_exit);