bma180.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. * bma180.c - IIO driver for Bosch BMA180 triaxial acceleration sensor
  3. *
  4. * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com>
  5. *
  6. * Support for BMA250 (c) Peter Meerwald <pmeerw@pmeerw.net>
  7. *
  8. * This file is subject to the terms and conditions of version 2 of
  9. * the GNU General Public License. See the file COPYING in the main
  10. * directory of this archive for more details.
  11. *
  12. * SPI is not supported by driver
  13. * BMA180: 7-bit I2C slave address 0x40 or 0x41
  14. * BMA250: 7-bit I2C slave address 0x18 or 0x19
  15. */
  16. #include <linux/module.h>
  17. #include <linux/i2c.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/of.h>
  21. #include <linux/bitops.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/sysfs.h>
  26. #include <linux/iio/buffer.h>
  27. #include <linux/iio/trigger.h>
  28. #include <linux/iio/trigger_consumer.h>
  29. #include <linux/iio/triggered_buffer.h>
  30. #define BMA180_DRV_NAME "bma180"
  31. #define BMA180_IRQ_NAME "bma180_event"
  32. enum {
  33. BMA180,
  34. BMA250,
  35. };
  36. struct bma180_data;
  37. struct bma180_part_info {
  38. const struct iio_chan_spec *channels;
  39. unsigned num_channels;
  40. const int *scale_table;
  41. unsigned num_scales;
  42. const int *bw_table;
  43. unsigned num_bw;
  44. u8 int_reset_reg, int_reset_mask;
  45. u8 sleep_reg, sleep_mask;
  46. u8 bw_reg, bw_mask;
  47. u8 scale_reg, scale_mask;
  48. u8 power_reg, power_mask, lowpower_val;
  49. u8 int_enable_reg, int_enable_mask;
  50. u8 softreset_reg;
  51. int (*chip_config)(struct bma180_data *data);
  52. void (*chip_disable)(struct bma180_data *data);
  53. };
  54. /* Register set */
  55. #define BMA180_CHIP_ID 0x00 /* Need to distinguish BMA180 from other */
  56. #define BMA180_ACC_X_LSB 0x02 /* First of 6 registers of accel data */
  57. #define BMA180_TEMP 0x08
  58. #define BMA180_CTRL_REG0 0x0d
  59. #define BMA180_RESET 0x10
  60. #define BMA180_BW_TCS 0x20
  61. #define BMA180_CTRL_REG3 0x21
  62. #define BMA180_TCO_Z 0x30
  63. #define BMA180_OFFSET_LSB1 0x35
  64. /* BMA180_CTRL_REG0 bits */
  65. #define BMA180_DIS_WAKE_UP BIT(0) /* Disable wake up mode */
  66. #define BMA180_SLEEP BIT(1) /* 1 - chip will sleep */
  67. #define BMA180_EE_W BIT(4) /* Unlock writing to addr from 0x20 */
  68. #define BMA180_RESET_INT BIT(6) /* Reset pending interrupts */
  69. /* BMA180_CTRL_REG3 bits */
  70. #define BMA180_NEW_DATA_INT BIT(1) /* Intr every new accel data is ready */
  71. /* BMA180_OFFSET_LSB1 skipping mode bit */
  72. #define BMA180_SMP_SKIP BIT(0)
  73. /* Bit masks for registers bit fields */
  74. #define BMA180_RANGE 0x0e /* Range of measured accel values */
  75. #define BMA180_BW 0xf0 /* Accel bandwidth */
  76. #define BMA180_MODE_CONFIG 0x03 /* Config operation modes */
  77. /* We have to write this value in reset register to do soft reset */
  78. #define BMA180_RESET_VAL 0xb6
  79. #define BMA180_ID_REG_VAL 0x03
  80. /* Chip power modes */
  81. #define BMA180_LOW_POWER 0x03
  82. #define BMA250_RANGE_REG 0x0f
  83. #define BMA250_BW_REG 0x10
  84. #define BMA250_POWER_REG 0x11
  85. #define BMA250_RESET_REG 0x14
  86. #define BMA250_INT_ENABLE_REG 0x17
  87. #define BMA250_INT_MAP_REG 0x1a
  88. #define BMA250_INT_RESET_REG 0x21
  89. #define BMA250_RANGE_MASK GENMASK(3, 0) /* Range of accel values */
  90. #define BMA250_BW_MASK GENMASK(4, 0) /* Accel bandwidth */
  91. #define BMA250_SUSPEND_MASK BIT(7) /* chip will sleep */
  92. #define BMA250_LOWPOWER_MASK BIT(6)
  93. #define BMA250_DATA_INTEN_MASK BIT(4)
  94. #define BMA250_INT1_DATA_MASK BIT(0)
  95. #define BMA250_INT_RESET_MASK BIT(7) /* Reset pending interrupts */
  96. struct bma180_data {
  97. struct i2c_client *client;
  98. struct iio_trigger *trig;
  99. const struct bma180_part_info *part_info;
  100. struct mutex mutex;
  101. bool sleep_state;
  102. int scale;
  103. int bw;
  104. bool pmode;
  105. u8 buff[16]; /* 3x 16-bit + 8-bit + padding + timestamp */
  106. };
  107. enum bma180_chan {
  108. AXIS_X,
  109. AXIS_Y,
  110. AXIS_Z,
  111. TEMP
  112. };
  113. static int bma180_bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */
  114. static int bma180_scale_table[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 };
  115. static int bma250_bw_table[] = { 8, 16, 31, 63, 125, 250 }; /* Hz */
  116. static int bma250_scale_table[] = { 0, 0, 0, 38344, 0, 76590, 0, 0, 153180, 0,
  117. 0, 0, 306458 };
  118. static int bma180_get_data_reg(struct bma180_data *data, enum bma180_chan chan)
  119. {
  120. int ret;
  121. if (data->sleep_state)
  122. return -EBUSY;
  123. switch (chan) {
  124. case TEMP:
  125. ret = i2c_smbus_read_byte_data(data->client, BMA180_TEMP);
  126. if (ret < 0)
  127. dev_err(&data->client->dev, "failed to read temp register\n");
  128. break;
  129. default:
  130. ret = i2c_smbus_read_word_data(data->client,
  131. BMA180_ACC_X_LSB + chan * 2);
  132. if (ret < 0)
  133. dev_err(&data->client->dev,
  134. "failed to read accel_%c register\n",
  135. 'x' + chan);
  136. }
  137. return ret;
  138. }
  139. static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val)
  140. {
  141. int ret = i2c_smbus_read_byte_data(data->client, reg);
  142. u8 reg_val = (ret & ~mask) | (val << (ffs(mask) - 1));
  143. if (ret < 0)
  144. return ret;
  145. return i2c_smbus_write_byte_data(data->client, reg, reg_val);
  146. }
  147. static int bma180_reset_intr(struct bma180_data *data)
  148. {
  149. int ret = bma180_set_bits(data, data->part_info->int_reset_reg,
  150. data->part_info->int_reset_mask, 1);
  151. if (ret)
  152. dev_err(&data->client->dev, "failed to reset interrupt\n");
  153. return ret;
  154. }
  155. static int bma180_set_new_data_intr_state(struct bma180_data *data, bool state)
  156. {
  157. int ret = bma180_set_bits(data, data->part_info->int_enable_reg,
  158. data->part_info->int_enable_mask, state);
  159. if (ret)
  160. goto err;
  161. ret = bma180_reset_intr(data);
  162. if (ret)
  163. goto err;
  164. return 0;
  165. err:
  166. dev_err(&data->client->dev,
  167. "failed to set new data interrupt state %d\n", state);
  168. return ret;
  169. }
  170. static int bma180_set_sleep_state(struct bma180_data *data, bool state)
  171. {
  172. int ret = bma180_set_bits(data, data->part_info->sleep_reg,
  173. data->part_info->sleep_mask, state);
  174. if (ret) {
  175. dev_err(&data->client->dev,
  176. "failed to set sleep state %d\n", state);
  177. return ret;
  178. }
  179. data->sleep_state = state;
  180. return 0;
  181. }
  182. static int bma180_set_ee_writing_state(struct bma180_data *data, bool state)
  183. {
  184. int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_EE_W, state);
  185. if (ret)
  186. dev_err(&data->client->dev,
  187. "failed to set ee writing state %d\n", state);
  188. return ret;
  189. }
  190. static int bma180_set_bw(struct bma180_data *data, int val)
  191. {
  192. int ret, i;
  193. if (data->sleep_state)
  194. return -EBUSY;
  195. for (i = 0; i < data->part_info->num_bw; ++i) {
  196. if (data->part_info->bw_table[i] == val) {
  197. ret = bma180_set_bits(data, data->part_info->bw_reg,
  198. data->part_info->bw_mask, i);
  199. if (ret) {
  200. dev_err(&data->client->dev,
  201. "failed to set bandwidth\n");
  202. return ret;
  203. }
  204. data->bw = val;
  205. return 0;
  206. }
  207. }
  208. return -EINVAL;
  209. }
  210. static int bma180_set_scale(struct bma180_data *data, int val)
  211. {
  212. int ret, i;
  213. if (data->sleep_state)
  214. return -EBUSY;
  215. for (i = 0; i < data->part_info->num_scales; ++i)
  216. if (data->part_info->scale_table[i] == val) {
  217. ret = bma180_set_bits(data, data->part_info->scale_reg,
  218. data->part_info->scale_mask, i);
  219. if (ret) {
  220. dev_err(&data->client->dev,
  221. "failed to set scale\n");
  222. return ret;
  223. }
  224. data->scale = val;
  225. return 0;
  226. }
  227. return -EINVAL;
  228. }
  229. static int bma180_set_pmode(struct bma180_data *data, bool mode)
  230. {
  231. u8 reg_val = mode ? data->part_info->lowpower_val : 0;
  232. int ret = bma180_set_bits(data, data->part_info->power_reg,
  233. data->part_info->power_mask, reg_val);
  234. if (ret) {
  235. dev_err(&data->client->dev, "failed to set power mode\n");
  236. return ret;
  237. }
  238. data->pmode = mode;
  239. return 0;
  240. }
  241. static int bma180_soft_reset(struct bma180_data *data)
  242. {
  243. int ret = i2c_smbus_write_byte_data(data->client,
  244. data->part_info->softreset_reg, BMA180_RESET_VAL);
  245. if (ret)
  246. dev_err(&data->client->dev, "failed to reset the chip\n");
  247. return ret;
  248. }
  249. static int bma180_chip_init(struct bma180_data *data)
  250. {
  251. /* Try to read chip_id register. It must return 0x03. */
  252. int ret = i2c_smbus_read_byte_data(data->client, BMA180_CHIP_ID);
  253. if (ret < 0)
  254. return ret;
  255. if (ret != BMA180_ID_REG_VAL)
  256. return -ENODEV;
  257. ret = bma180_soft_reset(data);
  258. if (ret)
  259. return ret;
  260. /*
  261. * No serial transaction should occur within minimum 10 us
  262. * after soft_reset command
  263. */
  264. msleep(20);
  265. ret = bma180_set_new_data_intr_state(data, false);
  266. if (ret)
  267. return ret;
  268. return bma180_set_pmode(data, false);
  269. }
  270. static int bma180_chip_config(struct bma180_data *data)
  271. {
  272. int ret = bma180_chip_init(data);
  273. if (ret)
  274. goto err;
  275. ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_DIS_WAKE_UP, 1);
  276. if (ret)
  277. goto err;
  278. ret = bma180_set_ee_writing_state(data, true);
  279. if (ret)
  280. goto err;
  281. ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1);
  282. if (ret)
  283. goto err;
  284. ret = bma180_set_bw(data, 20); /* 20 Hz */
  285. if (ret)
  286. goto err;
  287. ret = bma180_set_scale(data, 2452); /* 2 G */
  288. if (ret)
  289. goto err;
  290. return 0;
  291. err:
  292. dev_err(&data->client->dev, "failed to config the chip\n");
  293. return ret;
  294. }
  295. static int bma250_chip_config(struct bma180_data *data)
  296. {
  297. int ret = bma180_chip_init(data);
  298. if (ret)
  299. goto err;
  300. ret = bma180_set_bw(data, 16); /* 16 Hz */
  301. if (ret)
  302. goto err;
  303. ret = bma180_set_scale(data, 38344); /* 2 G */
  304. if (ret)
  305. goto err;
  306. ret = bma180_set_bits(data, BMA250_INT_MAP_REG,
  307. BMA250_INT1_DATA_MASK, 1);
  308. if (ret)
  309. goto err;
  310. return 0;
  311. err:
  312. dev_err(&data->client->dev, "failed to config the chip\n");
  313. return ret;
  314. }
  315. static void bma180_chip_disable(struct bma180_data *data)
  316. {
  317. if (bma180_set_new_data_intr_state(data, false))
  318. goto err;
  319. if (bma180_set_ee_writing_state(data, false))
  320. goto err;
  321. if (bma180_set_sleep_state(data, true))
  322. goto err;
  323. return;
  324. err:
  325. dev_err(&data->client->dev, "failed to disable the chip\n");
  326. }
  327. static void bma250_chip_disable(struct bma180_data *data)
  328. {
  329. if (bma180_set_new_data_intr_state(data, false))
  330. goto err;
  331. if (bma180_set_sleep_state(data, true))
  332. goto err;
  333. return;
  334. err:
  335. dev_err(&data->client->dev, "failed to disable the chip\n");
  336. }
  337. static ssize_t bma180_show_avail(char *buf, const int *vals, unsigned n,
  338. bool micros)
  339. {
  340. size_t len = 0;
  341. int i;
  342. for (i = 0; i < n; i++) {
  343. if (!vals[i])
  344. continue;
  345. len += scnprintf(buf + len, PAGE_SIZE - len,
  346. micros ? "0.%06d " : "%d ", vals[i]);
  347. }
  348. buf[len - 1] = '\n';
  349. return len;
  350. }
  351. static ssize_t bma180_show_filter_freq_avail(struct device *dev,
  352. struct device_attribute *attr, char *buf)
  353. {
  354. struct bma180_data *data = iio_priv(dev_to_iio_dev(dev));
  355. return bma180_show_avail(buf, data->part_info->bw_table,
  356. data->part_info->num_bw, false);
  357. }
  358. static ssize_t bma180_show_scale_avail(struct device *dev,
  359. struct device_attribute *attr, char *buf)
  360. {
  361. struct bma180_data *data = iio_priv(dev_to_iio_dev(dev));
  362. return bma180_show_avail(buf, data->part_info->scale_table,
  363. data->part_info->num_scales, true);
  364. }
  365. static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available,
  366. S_IRUGO, bma180_show_filter_freq_avail, NULL, 0);
  367. static IIO_DEVICE_ATTR(in_accel_scale_available,
  368. S_IRUGO, bma180_show_scale_avail, NULL, 0);
  369. static struct attribute *bma180_attributes[] = {
  370. &iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available.
  371. dev_attr.attr,
  372. &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
  373. NULL,
  374. };
  375. static const struct attribute_group bma180_attrs_group = {
  376. .attrs = bma180_attributes,
  377. };
  378. static int bma180_read_raw(struct iio_dev *indio_dev,
  379. struct iio_chan_spec const *chan, int *val, int *val2,
  380. long mask)
  381. {
  382. struct bma180_data *data = iio_priv(indio_dev);
  383. int ret;
  384. switch (mask) {
  385. case IIO_CHAN_INFO_RAW:
  386. mutex_lock(&data->mutex);
  387. if (iio_buffer_enabled(indio_dev)) {
  388. mutex_unlock(&data->mutex);
  389. return -EBUSY;
  390. }
  391. ret = bma180_get_data_reg(data, chan->scan_index);
  392. mutex_unlock(&data->mutex);
  393. if (ret < 0)
  394. return ret;
  395. *val = sign_extend32(ret >> chan->scan_type.shift,
  396. chan->scan_type.realbits - 1);
  397. return IIO_VAL_INT;
  398. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  399. *val = data->bw;
  400. return IIO_VAL_INT;
  401. case IIO_CHAN_INFO_SCALE:
  402. switch (chan->type) {
  403. case IIO_ACCEL:
  404. *val = 0;
  405. *val2 = data->scale;
  406. return IIO_VAL_INT_PLUS_MICRO;
  407. case IIO_TEMP:
  408. *val = 500;
  409. return IIO_VAL_INT;
  410. default:
  411. return -EINVAL;
  412. }
  413. case IIO_CHAN_INFO_OFFSET:
  414. *val = 48; /* 0 LSB @ 24 degree C */
  415. return IIO_VAL_INT;
  416. default:
  417. return -EINVAL;
  418. }
  419. }
  420. static int bma180_write_raw(struct iio_dev *indio_dev,
  421. struct iio_chan_spec const *chan, int val, int val2, long mask)
  422. {
  423. struct bma180_data *data = iio_priv(indio_dev);
  424. int ret;
  425. switch (mask) {
  426. case IIO_CHAN_INFO_SCALE:
  427. if (val)
  428. return -EINVAL;
  429. mutex_lock(&data->mutex);
  430. ret = bma180_set_scale(data, val2);
  431. mutex_unlock(&data->mutex);
  432. return ret;
  433. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  434. if (val2)
  435. return -EINVAL;
  436. mutex_lock(&data->mutex);
  437. ret = bma180_set_bw(data, val);
  438. mutex_unlock(&data->mutex);
  439. return ret;
  440. default:
  441. return -EINVAL;
  442. }
  443. }
  444. static const struct iio_info bma180_info = {
  445. .attrs = &bma180_attrs_group,
  446. .read_raw = bma180_read_raw,
  447. .write_raw = bma180_write_raw,
  448. .driver_module = THIS_MODULE,
  449. };
  450. static const char * const bma180_power_modes[] = { "low_noise", "low_power" };
  451. static int bma180_get_power_mode(struct iio_dev *indio_dev,
  452. const struct iio_chan_spec *chan)
  453. {
  454. struct bma180_data *data = iio_priv(indio_dev);
  455. return data->pmode;
  456. }
  457. static int bma180_set_power_mode(struct iio_dev *indio_dev,
  458. const struct iio_chan_spec *chan, unsigned int mode)
  459. {
  460. struct bma180_data *data = iio_priv(indio_dev);
  461. int ret;
  462. mutex_lock(&data->mutex);
  463. ret = bma180_set_pmode(data, mode);
  464. mutex_unlock(&data->mutex);
  465. return ret;
  466. }
  467. static const struct iio_enum bma180_power_mode_enum = {
  468. .items = bma180_power_modes,
  469. .num_items = ARRAY_SIZE(bma180_power_modes),
  470. .get = bma180_get_power_mode,
  471. .set = bma180_set_power_mode,
  472. };
  473. static const struct iio_chan_spec_ext_info bma180_ext_info[] = {
  474. IIO_ENUM("power_mode", true, &bma180_power_mode_enum),
  475. IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum),
  476. { },
  477. };
  478. #define BMA180_ACC_CHANNEL(_axis, _bits) { \
  479. .type = IIO_ACCEL, \
  480. .modified = 1, \
  481. .channel2 = IIO_MOD_##_axis, \
  482. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  483. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  484. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
  485. .scan_index = AXIS_##_axis, \
  486. .scan_type = { \
  487. .sign = 's', \
  488. .realbits = _bits, \
  489. .storagebits = 16, \
  490. .shift = 16 - _bits, \
  491. }, \
  492. .ext_info = bma180_ext_info, \
  493. }
  494. #define BMA180_TEMP_CHANNEL { \
  495. .type = IIO_TEMP, \
  496. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  497. BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), \
  498. .scan_index = TEMP, \
  499. .scan_type = { \
  500. .sign = 's', \
  501. .realbits = 8, \
  502. .storagebits = 16, \
  503. }, \
  504. }
  505. static const struct iio_chan_spec bma180_channels[] = {
  506. BMA180_ACC_CHANNEL(X, 14),
  507. BMA180_ACC_CHANNEL(Y, 14),
  508. BMA180_ACC_CHANNEL(Z, 14),
  509. BMA180_TEMP_CHANNEL,
  510. IIO_CHAN_SOFT_TIMESTAMP(4),
  511. };
  512. static const struct iio_chan_spec bma250_channels[] = {
  513. BMA180_ACC_CHANNEL(X, 10),
  514. BMA180_ACC_CHANNEL(Y, 10),
  515. BMA180_ACC_CHANNEL(Z, 10),
  516. BMA180_TEMP_CHANNEL,
  517. IIO_CHAN_SOFT_TIMESTAMP(4),
  518. };
  519. static const struct bma180_part_info bma180_part_info[] = {
  520. [BMA180] = {
  521. bma180_channels, ARRAY_SIZE(bma180_channels),
  522. bma180_scale_table, ARRAY_SIZE(bma180_scale_table),
  523. bma180_bw_table, ARRAY_SIZE(bma180_bw_table),
  524. BMA180_CTRL_REG0, BMA180_RESET_INT,
  525. BMA180_CTRL_REG0, BMA180_SLEEP,
  526. BMA180_BW_TCS, BMA180_BW,
  527. BMA180_OFFSET_LSB1, BMA180_RANGE,
  528. BMA180_TCO_Z, BMA180_MODE_CONFIG, BMA180_LOW_POWER,
  529. BMA180_CTRL_REG3, BMA180_NEW_DATA_INT,
  530. BMA180_RESET,
  531. bma180_chip_config,
  532. bma180_chip_disable,
  533. },
  534. [BMA250] = {
  535. bma250_channels, ARRAY_SIZE(bma250_channels),
  536. bma250_scale_table, ARRAY_SIZE(bma250_scale_table),
  537. bma250_bw_table, ARRAY_SIZE(bma250_bw_table),
  538. BMA250_INT_RESET_REG, BMA250_INT_RESET_MASK,
  539. BMA250_POWER_REG, BMA250_SUSPEND_MASK,
  540. BMA250_BW_REG, BMA250_BW_MASK,
  541. BMA250_RANGE_REG, BMA250_RANGE_MASK,
  542. BMA250_POWER_REG, BMA250_LOWPOWER_MASK, 1,
  543. BMA250_INT_ENABLE_REG, BMA250_DATA_INTEN_MASK,
  544. BMA250_RESET_REG,
  545. bma250_chip_config,
  546. bma250_chip_disable,
  547. },
  548. };
  549. static irqreturn_t bma180_trigger_handler(int irq, void *p)
  550. {
  551. struct iio_poll_func *pf = p;
  552. struct iio_dev *indio_dev = pf->indio_dev;
  553. struct bma180_data *data = iio_priv(indio_dev);
  554. int64_t time_ns = iio_get_time_ns();
  555. int bit, ret, i = 0;
  556. mutex_lock(&data->mutex);
  557. for_each_set_bit(bit, indio_dev->active_scan_mask,
  558. indio_dev->masklength) {
  559. ret = bma180_get_data_reg(data, bit);
  560. if (ret < 0) {
  561. mutex_unlock(&data->mutex);
  562. goto err;
  563. }
  564. ((s16 *)data->buff)[i++] = ret;
  565. }
  566. mutex_unlock(&data->mutex);
  567. iio_push_to_buffers_with_timestamp(indio_dev, data->buff, time_ns);
  568. err:
  569. iio_trigger_notify_done(indio_dev->trig);
  570. return IRQ_HANDLED;
  571. }
  572. static int bma180_data_rdy_trigger_set_state(struct iio_trigger *trig,
  573. bool state)
  574. {
  575. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  576. struct bma180_data *data = iio_priv(indio_dev);
  577. return bma180_set_new_data_intr_state(data, state);
  578. }
  579. static int bma180_trig_try_reen(struct iio_trigger *trig)
  580. {
  581. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  582. struct bma180_data *data = iio_priv(indio_dev);
  583. return bma180_reset_intr(data);
  584. }
  585. static const struct iio_trigger_ops bma180_trigger_ops = {
  586. .set_trigger_state = bma180_data_rdy_trigger_set_state,
  587. .try_reenable = bma180_trig_try_reen,
  588. .owner = THIS_MODULE,
  589. };
  590. static int bma180_probe(struct i2c_client *client,
  591. const struct i2c_device_id *id)
  592. {
  593. struct bma180_data *data;
  594. struct iio_dev *indio_dev;
  595. int ret;
  596. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  597. if (!indio_dev)
  598. return -ENOMEM;
  599. data = iio_priv(indio_dev);
  600. i2c_set_clientdata(client, indio_dev);
  601. data->client = client;
  602. data->part_info = &bma180_part_info[id->driver_data];
  603. ret = data->part_info->chip_config(data);
  604. if (ret < 0)
  605. goto err_chip_disable;
  606. mutex_init(&data->mutex);
  607. indio_dev->dev.parent = &client->dev;
  608. indio_dev->channels = data->part_info->channels;
  609. indio_dev->num_channels = data->part_info->num_channels;
  610. indio_dev->name = id->name;
  611. indio_dev->modes = INDIO_DIRECT_MODE;
  612. indio_dev->info = &bma180_info;
  613. if (client->irq > 0) {
  614. data->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
  615. indio_dev->id);
  616. if (!data->trig) {
  617. ret = -ENOMEM;
  618. goto err_chip_disable;
  619. }
  620. ret = devm_request_irq(&client->dev, client->irq,
  621. iio_trigger_generic_data_rdy_poll, IRQF_TRIGGER_RISING,
  622. "bma180_event", data->trig);
  623. if (ret) {
  624. dev_err(&client->dev, "unable to request IRQ\n");
  625. goto err_trigger_free;
  626. }
  627. data->trig->dev.parent = &client->dev;
  628. data->trig->ops = &bma180_trigger_ops;
  629. iio_trigger_set_drvdata(data->trig, indio_dev);
  630. indio_dev->trig = iio_trigger_get(data->trig);
  631. ret = iio_trigger_register(data->trig);
  632. if (ret)
  633. goto err_trigger_free;
  634. }
  635. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  636. bma180_trigger_handler, NULL);
  637. if (ret < 0) {
  638. dev_err(&client->dev, "unable to setup iio triggered buffer\n");
  639. goto err_trigger_unregister;
  640. }
  641. ret = iio_device_register(indio_dev);
  642. if (ret < 0) {
  643. dev_err(&client->dev, "unable to register iio device\n");
  644. goto err_buffer_cleanup;
  645. }
  646. return 0;
  647. err_buffer_cleanup:
  648. iio_triggered_buffer_cleanup(indio_dev);
  649. err_trigger_unregister:
  650. if (data->trig)
  651. iio_trigger_unregister(data->trig);
  652. err_trigger_free:
  653. iio_trigger_free(data->trig);
  654. err_chip_disable:
  655. data->part_info->chip_disable(data);
  656. return ret;
  657. }
  658. static int bma180_remove(struct i2c_client *client)
  659. {
  660. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  661. struct bma180_data *data = iio_priv(indio_dev);
  662. iio_device_unregister(indio_dev);
  663. iio_triggered_buffer_cleanup(indio_dev);
  664. if (data->trig) {
  665. iio_trigger_unregister(data->trig);
  666. iio_trigger_free(data->trig);
  667. }
  668. mutex_lock(&data->mutex);
  669. data->part_info->chip_disable(data);
  670. mutex_unlock(&data->mutex);
  671. return 0;
  672. }
  673. #ifdef CONFIG_PM_SLEEP
  674. static int bma180_suspend(struct device *dev)
  675. {
  676. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  677. struct bma180_data *data = iio_priv(indio_dev);
  678. int ret;
  679. mutex_lock(&data->mutex);
  680. ret = bma180_set_sleep_state(data, true);
  681. mutex_unlock(&data->mutex);
  682. return ret;
  683. }
  684. static int bma180_resume(struct device *dev)
  685. {
  686. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  687. struct bma180_data *data = iio_priv(indio_dev);
  688. int ret;
  689. mutex_lock(&data->mutex);
  690. ret = bma180_set_sleep_state(data, false);
  691. mutex_unlock(&data->mutex);
  692. return ret;
  693. }
  694. static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume);
  695. #define BMA180_PM_OPS (&bma180_pm_ops)
  696. #else
  697. #define BMA180_PM_OPS NULL
  698. #endif
  699. static struct i2c_device_id bma180_ids[] = {
  700. { "bma180", BMA180 },
  701. { "bma250", BMA250 },
  702. { }
  703. };
  704. MODULE_DEVICE_TABLE(i2c, bma180_ids);
  705. static struct i2c_driver bma180_driver = {
  706. .driver = {
  707. .name = "bma180",
  708. .pm = BMA180_PM_OPS,
  709. },
  710. .probe = bma180_probe,
  711. .remove = bma180_remove,
  712. .id_table = bma180_ids,
  713. };
  714. module_i2c_driver(bma180_driver);
  715. MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>");
  716. MODULE_AUTHOR("Texas Instruments, Inc.");
  717. MODULE_DESCRIPTION("Bosch BMA180/BMA250 triaxial acceleration sensor");
  718. MODULE_LICENSE("GPL");