mpl3115.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * mpl3115.c - Support for Freescale MPL3115A2 pressure/temperature sensor
  3. *
  4. * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * (7-bit I2C slave address 0x60)
  11. *
  12. * TODO: FIFO buffer, altimeter mode, oversampling, continuous mode,
  13. * interrupts, user offset correction, raw mode
  14. */
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/sysfs.h>
  19. #include <linux/iio/trigger_consumer.h>
  20. #include <linux/iio/buffer.h>
  21. #include <linux/iio/triggered_buffer.h>
  22. #include <linux/delay.h>
  23. #define MPL3115_STATUS 0x00
  24. #define MPL3115_OUT_PRESS 0x01 /* MSB first, 20 bit */
  25. #define MPL3115_OUT_TEMP 0x04 /* MSB first, 12 bit */
  26. #define MPL3115_WHO_AM_I 0x0c
  27. #define MPL3115_CTRL_REG1 0x26
  28. #define MPL3115_DEVICE_ID 0xc4
  29. #define MPL3115_STATUS_PRESS_RDY BIT(2)
  30. #define MPL3115_STATUS_TEMP_RDY BIT(1)
  31. #define MPL3115_CTRL_RESET BIT(2) /* software reset */
  32. #define MPL3115_CTRL_OST BIT(1) /* initiate measurement */
  33. #define MPL3115_CTRL_ACTIVE BIT(0) /* continuous measurement */
  34. #define MPL3115_CTRL_OS_258MS (BIT(5) | BIT(4)) /* 64x oversampling */
  35. struct mpl3115_data {
  36. struct i2c_client *client;
  37. struct mutex lock;
  38. u8 ctrl_reg1;
  39. };
  40. static int mpl3115_request(struct mpl3115_data *data)
  41. {
  42. int ret, tries = 15;
  43. /* trigger measurement */
  44. ret = i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
  45. data->ctrl_reg1 | MPL3115_CTRL_OST);
  46. if (ret < 0)
  47. return ret;
  48. while (tries-- > 0) {
  49. ret = i2c_smbus_read_byte_data(data->client, MPL3115_CTRL_REG1);
  50. if (ret < 0)
  51. return ret;
  52. /* wait for data ready, i.e. OST cleared */
  53. if (!(ret & MPL3115_CTRL_OST))
  54. break;
  55. msleep(20);
  56. }
  57. if (tries < 0) {
  58. dev_err(&data->client->dev, "data not ready\n");
  59. return -EIO;
  60. }
  61. return 0;
  62. }
  63. static int mpl3115_read_raw(struct iio_dev *indio_dev,
  64. struct iio_chan_spec const *chan,
  65. int *val, int *val2, long mask)
  66. {
  67. struct mpl3115_data *data = iio_priv(indio_dev);
  68. __be32 tmp = 0;
  69. int ret;
  70. switch (mask) {
  71. case IIO_CHAN_INFO_RAW:
  72. ret = iio_device_claim_direct_mode(indio_dev);
  73. if (ret)
  74. return ret;
  75. switch (chan->type) {
  76. case IIO_PRESSURE: /* in 0.25 pascal / LSB */
  77. mutex_lock(&data->lock);
  78. ret = mpl3115_request(data);
  79. if (ret < 0) {
  80. mutex_unlock(&data->lock);
  81. break;
  82. }
  83. ret = i2c_smbus_read_i2c_block_data(data->client,
  84. MPL3115_OUT_PRESS, 3, (u8 *) &tmp);
  85. mutex_unlock(&data->lock);
  86. if (ret < 0)
  87. break;
  88. *val = be32_to_cpu(tmp) >> 12;
  89. ret = IIO_VAL_INT;
  90. break;
  91. case IIO_TEMP: /* in 0.0625 celsius / LSB */
  92. mutex_lock(&data->lock);
  93. ret = mpl3115_request(data);
  94. if (ret < 0) {
  95. mutex_unlock(&data->lock);
  96. break;
  97. }
  98. ret = i2c_smbus_read_i2c_block_data(data->client,
  99. MPL3115_OUT_TEMP, 2, (u8 *) &tmp);
  100. mutex_unlock(&data->lock);
  101. if (ret < 0)
  102. break;
  103. *val = sign_extend32(be32_to_cpu(tmp) >> 20, 11);
  104. ret = IIO_VAL_INT;
  105. break;
  106. default:
  107. ret = -EINVAL;
  108. break;
  109. }
  110. iio_device_release_direct_mode(indio_dev);
  111. return ret;
  112. case IIO_CHAN_INFO_SCALE:
  113. switch (chan->type) {
  114. case IIO_PRESSURE:
  115. *val = 0;
  116. *val2 = 250; /* want kilopascal */
  117. return IIO_VAL_INT_PLUS_MICRO;
  118. case IIO_TEMP:
  119. *val = 0;
  120. *val2 = 62500;
  121. return IIO_VAL_INT_PLUS_MICRO;
  122. default:
  123. return -EINVAL;
  124. }
  125. }
  126. return -EINVAL;
  127. }
  128. static irqreturn_t mpl3115_trigger_handler(int irq, void *p)
  129. {
  130. struct iio_poll_func *pf = p;
  131. struct iio_dev *indio_dev = pf->indio_dev;
  132. struct mpl3115_data *data = iio_priv(indio_dev);
  133. u8 buffer[16]; /* 32-bit channel + 16-bit channel + padding + ts */
  134. int ret, pos = 0;
  135. mutex_lock(&data->lock);
  136. ret = mpl3115_request(data);
  137. if (ret < 0) {
  138. mutex_unlock(&data->lock);
  139. goto done;
  140. }
  141. memset(buffer, 0, sizeof(buffer));
  142. if (test_bit(0, indio_dev->active_scan_mask)) {
  143. ret = i2c_smbus_read_i2c_block_data(data->client,
  144. MPL3115_OUT_PRESS, 3, &buffer[pos]);
  145. if (ret < 0) {
  146. mutex_unlock(&data->lock);
  147. goto done;
  148. }
  149. pos += 4;
  150. }
  151. if (test_bit(1, indio_dev->active_scan_mask)) {
  152. ret = i2c_smbus_read_i2c_block_data(data->client,
  153. MPL3115_OUT_TEMP, 2, &buffer[pos]);
  154. if (ret < 0) {
  155. mutex_unlock(&data->lock);
  156. goto done;
  157. }
  158. }
  159. mutex_unlock(&data->lock);
  160. iio_push_to_buffers_with_timestamp(indio_dev, buffer,
  161. iio_get_time_ns(indio_dev));
  162. done:
  163. iio_trigger_notify_done(indio_dev->trig);
  164. return IRQ_HANDLED;
  165. }
  166. static const struct iio_chan_spec mpl3115_channels[] = {
  167. {
  168. .type = IIO_PRESSURE,
  169. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  170. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  171. .scan_index = 0,
  172. .scan_type = {
  173. .sign = 'u',
  174. .realbits = 20,
  175. .storagebits = 32,
  176. .shift = 12,
  177. .endianness = IIO_BE,
  178. }
  179. },
  180. {
  181. .type = IIO_TEMP,
  182. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  183. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  184. .scan_index = 1,
  185. .scan_type = {
  186. .sign = 's',
  187. .realbits = 12,
  188. .storagebits = 16,
  189. .shift = 4,
  190. .endianness = IIO_BE,
  191. }
  192. },
  193. IIO_CHAN_SOFT_TIMESTAMP(2),
  194. };
  195. static const struct iio_info mpl3115_info = {
  196. .read_raw = &mpl3115_read_raw,
  197. };
  198. static int mpl3115_probe(struct i2c_client *client,
  199. const struct i2c_device_id *id)
  200. {
  201. struct mpl3115_data *data;
  202. struct iio_dev *indio_dev;
  203. int ret;
  204. ret = i2c_smbus_read_byte_data(client, MPL3115_WHO_AM_I);
  205. if (ret < 0)
  206. return ret;
  207. if (ret != MPL3115_DEVICE_ID)
  208. return -ENODEV;
  209. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  210. if (!indio_dev)
  211. return -ENOMEM;
  212. data = iio_priv(indio_dev);
  213. data->client = client;
  214. mutex_init(&data->lock);
  215. i2c_set_clientdata(client, indio_dev);
  216. indio_dev->info = &mpl3115_info;
  217. indio_dev->name = id->name;
  218. indio_dev->dev.parent = &client->dev;
  219. indio_dev->modes = INDIO_DIRECT_MODE;
  220. indio_dev->channels = mpl3115_channels;
  221. indio_dev->num_channels = ARRAY_SIZE(mpl3115_channels);
  222. /* software reset, I2C transfer is aborted (fails) */
  223. i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
  224. MPL3115_CTRL_RESET);
  225. msleep(50);
  226. data->ctrl_reg1 = MPL3115_CTRL_OS_258MS;
  227. ret = i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
  228. data->ctrl_reg1);
  229. if (ret < 0)
  230. return ret;
  231. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  232. mpl3115_trigger_handler, NULL);
  233. if (ret < 0)
  234. return ret;
  235. ret = iio_device_register(indio_dev);
  236. if (ret < 0)
  237. goto buffer_cleanup;
  238. return 0;
  239. buffer_cleanup:
  240. iio_triggered_buffer_cleanup(indio_dev);
  241. return ret;
  242. }
  243. static int mpl3115_standby(struct mpl3115_data *data)
  244. {
  245. return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
  246. data->ctrl_reg1 & ~MPL3115_CTRL_ACTIVE);
  247. }
  248. static int mpl3115_remove(struct i2c_client *client)
  249. {
  250. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  251. iio_device_unregister(indio_dev);
  252. iio_triggered_buffer_cleanup(indio_dev);
  253. mpl3115_standby(iio_priv(indio_dev));
  254. return 0;
  255. }
  256. #ifdef CONFIG_PM_SLEEP
  257. static int mpl3115_suspend(struct device *dev)
  258. {
  259. return mpl3115_standby(iio_priv(i2c_get_clientdata(
  260. to_i2c_client(dev))));
  261. }
  262. static int mpl3115_resume(struct device *dev)
  263. {
  264. struct mpl3115_data *data = iio_priv(i2c_get_clientdata(
  265. to_i2c_client(dev)));
  266. return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
  267. data->ctrl_reg1);
  268. }
  269. static SIMPLE_DEV_PM_OPS(mpl3115_pm_ops, mpl3115_suspend, mpl3115_resume);
  270. #define MPL3115_PM_OPS (&mpl3115_pm_ops)
  271. #else
  272. #define MPL3115_PM_OPS NULL
  273. #endif
  274. static const struct i2c_device_id mpl3115_id[] = {
  275. { "mpl3115", 0 },
  276. { }
  277. };
  278. MODULE_DEVICE_TABLE(i2c, mpl3115_id);
  279. static const struct of_device_id mpl3115_of_match[] = {
  280. { .compatible = "fsl,mpl3115" },
  281. { }
  282. };
  283. MODULE_DEVICE_TABLE(of, mpl3115_of_match);
  284. static struct i2c_driver mpl3115_driver = {
  285. .driver = {
  286. .name = "mpl3115",
  287. .of_match_table = mpl3115_of_match,
  288. .pm = MPL3115_PM_OPS,
  289. },
  290. .probe = mpl3115_probe,
  291. .remove = mpl3115_remove,
  292. .id_table = mpl3115_id,
  293. };
  294. module_i2c_driver(mpl3115_driver);
  295. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  296. MODULE_DESCRIPTION("Freescale MPL3115 pressure/temperature driver");
  297. MODULE_LICENSE("GPL");