bma180.c 21 KB

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