ms5611_i2c.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * MS5611 pressure and temperature sensor driver (I2C bus)
  3. *
  4. * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * 7-bit I2C slave addresses:
  11. *
  12. * 0x77 (CSB pin low)
  13. * 0x76 (CSB pin high)
  14. *
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/i2c.h>
  18. #include <linux/module.h>
  19. #include <linux/of_device.h>
  20. #include "ms5611.h"
  21. static int ms5611_i2c_reset(struct device *dev)
  22. {
  23. struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
  24. return i2c_smbus_write_byte(st->client, MS5611_RESET);
  25. }
  26. static int ms5611_i2c_read_prom_word(struct device *dev, int index, u16 *word)
  27. {
  28. int ret;
  29. struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
  30. ret = i2c_smbus_read_word_swapped(st->client,
  31. MS5611_READ_PROM_WORD + (index << 1));
  32. if (ret < 0)
  33. return ret;
  34. *word = ret;
  35. return 0;
  36. }
  37. static int ms5611_i2c_read_adc(struct ms5611_state *st, s32 *val)
  38. {
  39. int ret;
  40. u8 buf[3];
  41. ret = i2c_smbus_read_i2c_block_data(st->client, MS5611_READ_ADC,
  42. 3, buf);
  43. if (ret < 0)
  44. return ret;
  45. *val = (buf[0] << 16) | (buf[1] << 8) | buf[2];
  46. return 0;
  47. }
  48. static int ms5611_i2c_read_adc_temp_and_pressure(struct device *dev,
  49. s32 *temp, s32 *pressure)
  50. {
  51. int ret;
  52. struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
  53. const struct ms5611_osr *osr = st->temp_osr;
  54. ret = i2c_smbus_write_byte(st->client, osr->cmd);
  55. if (ret < 0)
  56. return ret;
  57. usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
  58. ret = ms5611_i2c_read_adc(st, temp);
  59. if (ret < 0)
  60. return ret;
  61. osr = st->pressure_osr;
  62. ret = i2c_smbus_write_byte(st->client, osr->cmd);
  63. if (ret < 0)
  64. return ret;
  65. usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
  66. return ms5611_i2c_read_adc(st, pressure);
  67. }
  68. static int ms5611_i2c_probe(struct i2c_client *client,
  69. const struct i2c_device_id *id)
  70. {
  71. struct ms5611_state *st;
  72. struct iio_dev *indio_dev;
  73. if (!i2c_check_functionality(client->adapter,
  74. I2C_FUNC_SMBUS_WRITE_BYTE |
  75. I2C_FUNC_SMBUS_READ_WORD_DATA |
  76. I2C_FUNC_SMBUS_READ_I2C_BLOCK))
  77. return -EOPNOTSUPP;
  78. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
  79. if (!indio_dev)
  80. return -ENOMEM;
  81. st = iio_priv(indio_dev);
  82. i2c_set_clientdata(client, indio_dev);
  83. st->reset = ms5611_i2c_reset;
  84. st->read_prom_word = ms5611_i2c_read_prom_word;
  85. st->read_adc_temp_and_pressure = ms5611_i2c_read_adc_temp_and_pressure;
  86. st->client = client;
  87. return ms5611_probe(indio_dev, &client->dev, id->name, id->driver_data);
  88. }
  89. static int ms5611_i2c_remove(struct i2c_client *client)
  90. {
  91. return ms5611_remove(i2c_get_clientdata(client));
  92. }
  93. #if defined(CONFIG_OF)
  94. static const struct of_device_id ms5611_i2c_matches[] = {
  95. { .compatible = "meas,ms5611" },
  96. { .compatible = "ms5611" },
  97. { .compatible = "meas,ms5607" },
  98. { .compatible = "ms5607" },
  99. { }
  100. };
  101. MODULE_DEVICE_TABLE(of, ms5611_i2c_matches);
  102. #endif
  103. static const struct i2c_device_id ms5611_id[] = {
  104. { "ms5611", MS5611 },
  105. { "ms5607", MS5607 },
  106. { }
  107. };
  108. MODULE_DEVICE_TABLE(i2c, ms5611_id);
  109. static struct i2c_driver ms5611_driver = {
  110. .driver = {
  111. .name = "ms5611",
  112. .of_match_table = of_match_ptr(ms5611_i2c_matches)
  113. },
  114. .id_table = ms5611_id,
  115. .probe = ms5611_i2c_probe,
  116. .remove = ms5611_i2c_remove,
  117. };
  118. module_i2c_driver(ms5611_driver);
  119. MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
  120. MODULE_DESCRIPTION("MS5611 i2c driver");
  121. MODULE_LICENSE("GPL v2");