mpl3115.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. /*
  134. * 32-bit channel + 16-bit channel + padding + ts
  135. * Note that it is possible for only one of the first 2
  136. * channels to be enabled. If that happens, the first element
  137. * of the buffer may be either 16 or 32-bits. As such we cannot
  138. * use a simple structure definition to express this data layout.
  139. */
  140. u8 buffer[16] __aligned(8);
  141. int ret, pos = 0;
  142. mutex_lock(&data->lock);
  143. ret = mpl3115_request(data);
  144. if (ret < 0) {
  145. mutex_unlock(&data->lock);
  146. goto done;
  147. }
  148. memset(buffer, 0, sizeof(buffer));
  149. if (test_bit(0, indio_dev->active_scan_mask)) {
  150. ret = i2c_smbus_read_i2c_block_data(data->client,
  151. MPL3115_OUT_PRESS, 3, &buffer[pos]);
  152. if (ret < 0) {
  153. mutex_unlock(&data->lock);
  154. goto done;
  155. }
  156. pos += 4;
  157. }
  158. if (test_bit(1, indio_dev->active_scan_mask)) {
  159. ret = i2c_smbus_read_i2c_block_data(data->client,
  160. MPL3115_OUT_TEMP, 2, &buffer[pos]);
  161. if (ret < 0) {
  162. mutex_unlock(&data->lock);
  163. goto done;
  164. }
  165. }
  166. mutex_unlock(&data->lock);
  167. iio_push_to_buffers_with_timestamp(indio_dev, buffer,
  168. iio_get_time_ns(indio_dev));
  169. done:
  170. iio_trigger_notify_done(indio_dev->trig);
  171. return IRQ_HANDLED;
  172. }
  173. static const struct iio_chan_spec mpl3115_channels[] = {
  174. {
  175. .type = IIO_PRESSURE,
  176. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  177. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  178. .scan_index = 0,
  179. .scan_type = {
  180. .sign = 'u',
  181. .realbits = 20,
  182. .storagebits = 32,
  183. .shift = 12,
  184. .endianness = IIO_BE,
  185. }
  186. },
  187. {
  188. .type = IIO_TEMP,
  189. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  190. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  191. .scan_index = 1,
  192. .scan_type = {
  193. .sign = 's',
  194. .realbits = 12,
  195. .storagebits = 16,
  196. .shift = 4,
  197. .endianness = IIO_BE,
  198. }
  199. },
  200. IIO_CHAN_SOFT_TIMESTAMP(2),
  201. };
  202. static const struct iio_info mpl3115_info = {
  203. .read_raw = &mpl3115_read_raw,
  204. };
  205. static int mpl3115_probe(struct i2c_client *client,
  206. const struct i2c_device_id *id)
  207. {
  208. struct mpl3115_data *data;
  209. struct iio_dev *indio_dev;
  210. int ret;
  211. ret = i2c_smbus_read_byte_data(client, MPL3115_WHO_AM_I);
  212. if (ret < 0)
  213. return ret;
  214. if (ret != MPL3115_DEVICE_ID)
  215. return -ENODEV;
  216. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  217. if (!indio_dev)
  218. return -ENOMEM;
  219. data = iio_priv(indio_dev);
  220. data->client = client;
  221. mutex_init(&data->lock);
  222. i2c_set_clientdata(client, indio_dev);
  223. indio_dev->info = &mpl3115_info;
  224. indio_dev->name = id->name;
  225. indio_dev->dev.parent = &client->dev;
  226. indio_dev->modes = INDIO_DIRECT_MODE;
  227. indio_dev->channels = mpl3115_channels;
  228. indio_dev->num_channels = ARRAY_SIZE(mpl3115_channels);
  229. /* software reset, I2C transfer is aborted (fails) */
  230. i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
  231. MPL3115_CTRL_RESET);
  232. msleep(50);
  233. data->ctrl_reg1 = MPL3115_CTRL_OS_258MS;
  234. ret = i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
  235. data->ctrl_reg1);
  236. if (ret < 0)
  237. return ret;
  238. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  239. mpl3115_trigger_handler, NULL);
  240. if (ret < 0)
  241. return ret;
  242. ret = iio_device_register(indio_dev);
  243. if (ret < 0)
  244. goto buffer_cleanup;
  245. return 0;
  246. buffer_cleanup:
  247. iio_triggered_buffer_cleanup(indio_dev);
  248. return ret;
  249. }
  250. static int mpl3115_standby(struct mpl3115_data *data)
  251. {
  252. return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
  253. data->ctrl_reg1 & ~MPL3115_CTRL_ACTIVE);
  254. }
  255. static int mpl3115_remove(struct i2c_client *client)
  256. {
  257. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  258. iio_device_unregister(indio_dev);
  259. iio_triggered_buffer_cleanup(indio_dev);
  260. mpl3115_standby(iio_priv(indio_dev));
  261. return 0;
  262. }
  263. #ifdef CONFIG_PM_SLEEP
  264. static int mpl3115_suspend(struct device *dev)
  265. {
  266. return mpl3115_standby(iio_priv(i2c_get_clientdata(
  267. to_i2c_client(dev))));
  268. }
  269. static int mpl3115_resume(struct device *dev)
  270. {
  271. struct mpl3115_data *data = iio_priv(i2c_get_clientdata(
  272. to_i2c_client(dev)));
  273. return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
  274. data->ctrl_reg1);
  275. }
  276. static SIMPLE_DEV_PM_OPS(mpl3115_pm_ops, mpl3115_suspend, mpl3115_resume);
  277. #define MPL3115_PM_OPS (&mpl3115_pm_ops)
  278. #else
  279. #define MPL3115_PM_OPS NULL
  280. #endif
  281. static const struct i2c_device_id mpl3115_id[] = {
  282. { "mpl3115", 0 },
  283. { }
  284. };
  285. MODULE_DEVICE_TABLE(i2c, mpl3115_id);
  286. static const struct of_device_id mpl3115_of_match[] = {
  287. { .compatible = "fsl,mpl3115" },
  288. { }
  289. };
  290. MODULE_DEVICE_TABLE(of, mpl3115_of_match);
  291. static struct i2c_driver mpl3115_driver = {
  292. .driver = {
  293. .name = "mpl3115",
  294. .of_match_table = mpl3115_of_match,
  295. .pm = MPL3115_PM_OPS,
  296. },
  297. .probe = mpl3115_probe,
  298. .remove = mpl3115_remove,
  299. .id_table = mpl3115_id,
  300. };
  301. module_i2c_driver(mpl3115_driver);
  302. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  303. MODULE_DESCRIPTION("Freescale MPL3115 pressure/temperature driver");
  304. MODULE_LICENSE("GPL");