mpl3115.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. if (iio_buffer_enabled(indio_dev))
  73. return -EBUSY;
  74. switch (chan->type) {
  75. case IIO_PRESSURE: /* in 0.25 pascal / LSB */
  76. mutex_lock(&data->lock);
  77. ret = mpl3115_request(data);
  78. if (ret < 0) {
  79. mutex_unlock(&data->lock);
  80. return ret;
  81. }
  82. ret = i2c_smbus_read_i2c_block_data(data->client,
  83. MPL3115_OUT_PRESS, 3, (u8 *) &tmp);
  84. mutex_unlock(&data->lock);
  85. if (ret < 0)
  86. return ret;
  87. *val = be32_to_cpu(tmp) >> 12;
  88. return IIO_VAL_INT;
  89. case IIO_TEMP: /* in 0.0625 celsius / LSB */
  90. mutex_lock(&data->lock);
  91. ret = mpl3115_request(data);
  92. if (ret < 0) {
  93. mutex_unlock(&data->lock);
  94. return ret;
  95. }
  96. ret = i2c_smbus_read_i2c_block_data(data->client,
  97. MPL3115_OUT_TEMP, 2, (u8 *) &tmp);
  98. mutex_unlock(&data->lock);
  99. if (ret < 0)
  100. return ret;
  101. *val = sign_extend32(be32_to_cpu(tmp) >> 20, 11);
  102. return IIO_VAL_INT;
  103. default:
  104. return -EINVAL;
  105. }
  106. case IIO_CHAN_INFO_SCALE:
  107. switch (chan->type) {
  108. case IIO_PRESSURE:
  109. *val = 0;
  110. *val2 = 250; /* want kilopascal */
  111. return IIO_VAL_INT_PLUS_MICRO;
  112. case IIO_TEMP:
  113. *val = 0;
  114. *val2 = 62500;
  115. return IIO_VAL_INT_PLUS_MICRO;
  116. default:
  117. return -EINVAL;
  118. }
  119. }
  120. return -EINVAL;
  121. }
  122. static irqreturn_t mpl3115_trigger_handler(int irq, void *p)
  123. {
  124. struct iio_poll_func *pf = p;
  125. struct iio_dev *indio_dev = pf->indio_dev;
  126. struct mpl3115_data *data = iio_priv(indio_dev);
  127. u8 buffer[16]; /* 32-bit channel + 16-bit channel + padding + ts */
  128. int ret, pos = 0;
  129. mutex_lock(&data->lock);
  130. ret = mpl3115_request(data);
  131. if (ret < 0) {
  132. mutex_unlock(&data->lock);
  133. goto done;
  134. }
  135. memset(buffer, 0, sizeof(buffer));
  136. if (test_bit(0, indio_dev->active_scan_mask)) {
  137. ret = i2c_smbus_read_i2c_block_data(data->client,
  138. MPL3115_OUT_PRESS, 3, &buffer[pos]);
  139. if (ret < 0) {
  140. mutex_unlock(&data->lock);
  141. goto done;
  142. }
  143. pos += 4;
  144. }
  145. if (test_bit(1, indio_dev->active_scan_mask)) {
  146. ret = i2c_smbus_read_i2c_block_data(data->client,
  147. MPL3115_OUT_TEMP, 2, &buffer[pos]);
  148. if (ret < 0) {
  149. mutex_unlock(&data->lock);
  150. goto done;
  151. }
  152. }
  153. mutex_unlock(&data->lock);
  154. iio_push_to_buffers_with_timestamp(indio_dev, buffer,
  155. iio_get_time_ns());
  156. done:
  157. iio_trigger_notify_done(indio_dev->trig);
  158. return IRQ_HANDLED;
  159. }
  160. static const struct iio_chan_spec mpl3115_channels[] = {
  161. {
  162. .type = IIO_PRESSURE,
  163. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  164. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  165. .scan_index = 0,
  166. .scan_type = {
  167. .sign = 'u',
  168. .realbits = 20,
  169. .storagebits = 32,
  170. .shift = 12,
  171. .endianness = IIO_BE,
  172. }
  173. },
  174. {
  175. .type = IIO_TEMP,
  176. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  177. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  178. .scan_index = 1,
  179. .scan_type = {
  180. .sign = 's',
  181. .realbits = 12,
  182. .storagebits = 16,
  183. .shift = 4,
  184. .endianness = IIO_BE,
  185. }
  186. },
  187. IIO_CHAN_SOFT_TIMESTAMP(2),
  188. };
  189. static const struct iio_info mpl3115_info = {
  190. .read_raw = &mpl3115_read_raw,
  191. .driver_module = THIS_MODULE,
  192. };
  193. static int mpl3115_probe(struct i2c_client *client,
  194. const struct i2c_device_id *id)
  195. {
  196. struct mpl3115_data *data;
  197. struct iio_dev *indio_dev;
  198. int ret;
  199. ret = i2c_smbus_read_byte_data(client, MPL3115_WHO_AM_I);
  200. if (ret < 0)
  201. return ret;
  202. if (ret != MPL3115_DEVICE_ID)
  203. return -ENODEV;
  204. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  205. if (!indio_dev)
  206. return -ENOMEM;
  207. data = iio_priv(indio_dev);
  208. data->client = client;
  209. mutex_init(&data->lock);
  210. i2c_set_clientdata(client, indio_dev);
  211. indio_dev->info = &mpl3115_info;
  212. indio_dev->name = id->name;
  213. indio_dev->dev.parent = &client->dev;
  214. indio_dev->modes = INDIO_DIRECT_MODE;
  215. indio_dev->channels = mpl3115_channels;
  216. indio_dev->num_channels = ARRAY_SIZE(mpl3115_channels);
  217. /* software reset, I2C transfer is aborted (fails) */
  218. i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
  219. MPL3115_CTRL_RESET);
  220. msleep(50);
  221. data->ctrl_reg1 = MPL3115_CTRL_OS_258MS;
  222. ret = i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
  223. data->ctrl_reg1);
  224. if (ret < 0)
  225. return ret;
  226. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  227. mpl3115_trigger_handler, NULL);
  228. if (ret < 0)
  229. return ret;
  230. ret = iio_device_register(indio_dev);
  231. if (ret < 0)
  232. goto buffer_cleanup;
  233. return 0;
  234. buffer_cleanup:
  235. iio_triggered_buffer_cleanup(indio_dev);
  236. return ret;
  237. }
  238. static int mpl3115_standby(struct mpl3115_data *data)
  239. {
  240. return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
  241. data->ctrl_reg1 & ~MPL3115_CTRL_ACTIVE);
  242. }
  243. static int mpl3115_remove(struct i2c_client *client)
  244. {
  245. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  246. iio_device_unregister(indio_dev);
  247. iio_triggered_buffer_cleanup(indio_dev);
  248. mpl3115_standby(iio_priv(indio_dev));
  249. return 0;
  250. }
  251. #ifdef CONFIG_PM_SLEEP
  252. static int mpl3115_suspend(struct device *dev)
  253. {
  254. return mpl3115_standby(iio_priv(i2c_get_clientdata(
  255. to_i2c_client(dev))));
  256. }
  257. static int mpl3115_resume(struct device *dev)
  258. {
  259. struct mpl3115_data *data = iio_priv(i2c_get_clientdata(
  260. to_i2c_client(dev)));
  261. return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
  262. data->ctrl_reg1);
  263. }
  264. static SIMPLE_DEV_PM_OPS(mpl3115_pm_ops, mpl3115_suspend, mpl3115_resume);
  265. #define MPL3115_PM_OPS (&mpl3115_pm_ops)
  266. #else
  267. #define MPL3115_PM_OPS NULL
  268. #endif
  269. static const struct i2c_device_id mpl3115_id[] = {
  270. { "mpl3115", 0 },
  271. { }
  272. };
  273. MODULE_DEVICE_TABLE(i2c, mpl3115_id);
  274. static struct i2c_driver mpl3115_driver = {
  275. .driver = {
  276. .name = "mpl3115",
  277. .pm = MPL3115_PM_OPS,
  278. },
  279. .probe = mpl3115_probe,
  280. .remove = mpl3115_remove,
  281. .id_table = mpl3115_id,
  282. };
  283. module_i2c_driver(mpl3115_driver);
  284. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  285. MODULE_DESCRIPTION("Freescale MPL3115 pressure/temperature driver");
  286. MODULE_LICENSE("GPL");