mpl3115.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. 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. 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. .driver_module = THIS_MODULE,
  198. };
  199. static int mpl3115_probe(struct i2c_client *client,
  200. const struct i2c_device_id *id)
  201. {
  202. struct mpl3115_data *data;
  203. struct iio_dev *indio_dev;
  204. int ret;
  205. ret = i2c_smbus_read_byte_data(client, MPL3115_WHO_AM_I);
  206. if (ret < 0)
  207. return ret;
  208. if (ret != MPL3115_DEVICE_ID)
  209. return -ENODEV;
  210. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  211. if (!indio_dev)
  212. return -ENOMEM;
  213. data = iio_priv(indio_dev);
  214. data->client = client;
  215. mutex_init(&data->lock);
  216. i2c_set_clientdata(client, indio_dev);
  217. indio_dev->info = &mpl3115_info;
  218. indio_dev->name = id->name;
  219. indio_dev->dev.parent = &client->dev;
  220. indio_dev->modes = INDIO_DIRECT_MODE;
  221. indio_dev->channels = mpl3115_channels;
  222. indio_dev->num_channels = ARRAY_SIZE(mpl3115_channels);
  223. /* software reset, I2C transfer is aborted (fails) */
  224. i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
  225. MPL3115_CTRL_RESET);
  226. msleep(50);
  227. data->ctrl_reg1 = MPL3115_CTRL_OS_258MS;
  228. ret = i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
  229. data->ctrl_reg1);
  230. if (ret < 0)
  231. return ret;
  232. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  233. mpl3115_trigger_handler, NULL);
  234. if (ret < 0)
  235. return ret;
  236. ret = iio_device_register(indio_dev);
  237. if (ret < 0)
  238. goto buffer_cleanup;
  239. return 0;
  240. buffer_cleanup:
  241. iio_triggered_buffer_cleanup(indio_dev);
  242. return ret;
  243. }
  244. static int mpl3115_standby(struct mpl3115_data *data)
  245. {
  246. return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
  247. data->ctrl_reg1 & ~MPL3115_CTRL_ACTIVE);
  248. }
  249. static int mpl3115_remove(struct i2c_client *client)
  250. {
  251. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  252. iio_device_unregister(indio_dev);
  253. iio_triggered_buffer_cleanup(indio_dev);
  254. mpl3115_standby(iio_priv(indio_dev));
  255. return 0;
  256. }
  257. #ifdef CONFIG_PM_SLEEP
  258. static int mpl3115_suspend(struct device *dev)
  259. {
  260. return mpl3115_standby(iio_priv(i2c_get_clientdata(
  261. to_i2c_client(dev))));
  262. }
  263. static int mpl3115_resume(struct device *dev)
  264. {
  265. struct mpl3115_data *data = iio_priv(i2c_get_clientdata(
  266. to_i2c_client(dev)));
  267. return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
  268. data->ctrl_reg1);
  269. }
  270. static SIMPLE_DEV_PM_OPS(mpl3115_pm_ops, mpl3115_suspend, mpl3115_resume);
  271. #define MPL3115_PM_OPS (&mpl3115_pm_ops)
  272. #else
  273. #define MPL3115_PM_OPS NULL
  274. #endif
  275. static const struct i2c_device_id mpl3115_id[] = {
  276. { "mpl3115", 0 },
  277. { }
  278. };
  279. MODULE_DEVICE_TABLE(i2c, mpl3115_id);
  280. static struct i2c_driver mpl3115_driver = {
  281. .driver = {
  282. .name = "mpl3115",
  283. .pm = MPL3115_PM_OPS,
  284. },
  285. .probe = mpl3115_probe,
  286. .remove = mpl3115_remove,
  287. .id_table = mpl3115_id,
  288. };
  289. module_i2c_driver(mpl3115_driver);
  290. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  291. MODULE_DESCRIPTION("Freescale MPL3115 pressure/temperature driver");
  292. MODULE_LICENSE("GPL");