mpl3115.c 8.1 KB

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