hp03.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (c) 2016 Marek Vasut <marex@denx.de>
  3. *
  4. * Driver for Hope RF HP03 digital temperature and pressure sensor.
  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. #define pr_fmt(fmt) "hp03: " fmt
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/i2c.h>
  15. #include <linux/regmap.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. /*
  19. * The HP03 sensor occupies two fixed I2C addresses:
  20. * 0x50 ... read-only EEPROM with calibration data
  21. * 0x77 ... read-write ADC for pressure and temperature
  22. */
  23. #define HP03_EEPROM_ADDR 0x50
  24. #define HP03_ADC_ADDR 0x77
  25. #define HP03_EEPROM_CX_OFFSET 0x10
  26. #define HP03_EEPROM_AB_OFFSET 0x1e
  27. #define HP03_EEPROM_CD_OFFSET 0x20
  28. #define HP03_ADC_WRITE_REG 0xff
  29. #define HP03_ADC_READ_REG 0xfd
  30. #define HP03_ADC_READ_PRESSURE 0xf0 /* D1 in datasheet */
  31. #define HP03_ADC_READ_TEMP 0xe8 /* D2 in datasheet */
  32. struct hp03_priv {
  33. struct i2c_client *client;
  34. struct mutex lock;
  35. struct gpio_desc *xclr_gpio;
  36. struct i2c_client *eeprom_client;
  37. struct regmap *eeprom_regmap;
  38. s32 pressure; /* kPa */
  39. s32 temp; /* Deg. C */
  40. };
  41. static const struct iio_chan_spec hp03_channels[] = {
  42. {
  43. .type = IIO_PRESSURE,
  44. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  45. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  46. },
  47. {
  48. .type = IIO_TEMP,
  49. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  50. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  51. },
  52. };
  53. static bool hp03_is_writeable_reg(struct device *dev, unsigned int reg)
  54. {
  55. return false;
  56. }
  57. static bool hp03_is_volatile_reg(struct device *dev, unsigned int reg)
  58. {
  59. return false;
  60. }
  61. static const struct regmap_config hp03_regmap_config = {
  62. .reg_bits = 8,
  63. .val_bits = 8,
  64. .max_register = HP03_EEPROM_CD_OFFSET + 1,
  65. .cache_type = REGCACHE_RBTREE,
  66. .writeable_reg = hp03_is_writeable_reg,
  67. .volatile_reg = hp03_is_volatile_reg,
  68. };
  69. static int hp03_get_temp_pressure(struct hp03_priv *priv, const u8 reg)
  70. {
  71. int ret;
  72. ret = i2c_smbus_write_byte_data(priv->client, HP03_ADC_WRITE_REG, reg);
  73. if (ret < 0)
  74. return ret;
  75. msleep(50); /* Wait for conversion to finish */
  76. return i2c_smbus_read_word_data(priv->client, HP03_ADC_READ_REG);
  77. }
  78. static int hp03_update_temp_pressure(struct hp03_priv *priv)
  79. {
  80. struct device *dev = &priv->client->dev;
  81. u8 coefs[18];
  82. u16 cx_val[7];
  83. int ab_val, d1_val, d2_val, diff_val, dut, off, sens, x;
  84. int i, ret;
  85. /* Sample coefficients from EEPROM */
  86. ret = regmap_bulk_read(priv->eeprom_regmap, HP03_EEPROM_CX_OFFSET,
  87. coefs, sizeof(coefs));
  88. if (ret < 0) {
  89. dev_err(dev, "Failed to read EEPROM (reg=%02x)\n",
  90. HP03_EEPROM_CX_OFFSET);
  91. return ret;
  92. }
  93. /* Sample Temperature and Pressure */
  94. gpiod_set_value_cansleep(priv->xclr_gpio, 1);
  95. ret = hp03_get_temp_pressure(priv, HP03_ADC_READ_PRESSURE);
  96. if (ret < 0) {
  97. dev_err(dev, "Failed to read pressure\n");
  98. goto err_adc;
  99. }
  100. d1_val = ret;
  101. ret = hp03_get_temp_pressure(priv, HP03_ADC_READ_TEMP);
  102. if (ret < 0) {
  103. dev_err(dev, "Failed to read temperature\n");
  104. goto err_adc;
  105. }
  106. d2_val = ret;
  107. gpiod_set_value_cansleep(priv->xclr_gpio, 0);
  108. /* The Cx coefficients and Temp/Pressure values are MSB first. */
  109. for (i = 0; i < 7; i++)
  110. cx_val[i] = (coefs[2 * i] << 8) | (coefs[(2 * i) + 1] << 0);
  111. d1_val = ((d1_val >> 8) & 0xff) | ((d1_val & 0xff) << 8);
  112. d2_val = ((d2_val >> 8) & 0xff) | ((d2_val & 0xff) << 8);
  113. /* Coefficient voodoo from the HP03 datasheet. */
  114. if (d2_val >= cx_val[4])
  115. ab_val = coefs[14]; /* A-value */
  116. else
  117. ab_val = coefs[15]; /* B-value */
  118. diff_val = d2_val - cx_val[4];
  119. dut = (ab_val * (diff_val >> 7) * (diff_val >> 7)) >> coefs[16];
  120. dut = diff_val - dut;
  121. off = (cx_val[1] + (((cx_val[3] - 1024) * dut) >> 14)) * 4;
  122. sens = cx_val[0] + ((cx_val[2] * dut) >> 10);
  123. x = ((sens * (d1_val - 7168)) >> 14) - off;
  124. priv->pressure = ((x * 100) >> 5) + (cx_val[6] * 10);
  125. priv->temp = 250 + ((dut * cx_val[5]) >> 16) - (dut >> coefs[17]);
  126. return 0;
  127. err_adc:
  128. gpiod_set_value_cansleep(priv->xclr_gpio, 0);
  129. return ret;
  130. }
  131. static int hp03_read_raw(struct iio_dev *indio_dev,
  132. struct iio_chan_spec const *chan,
  133. int *val, int *val2, long mask)
  134. {
  135. struct hp03_priv *priv = iio_priv(indio_dev);
  136. int ret;
  137. mutex_lock(&priv->lock);
  138. ret = hp03_update_temp_pressure(priv);
  139. mutex_unlock(&priv->lock);
  140. if (ret)
  141. return ret;
  142. switch (mask) {
  143. case IIO_CHAN_INFO_RAW:
  144. switch (chan->type) {
  145. case IIO_PRESSURE:
  146. *val = priv->pressure;
  147. return IIO_VAL_INT;
  148. case IIO_TEMP:
  149. *val = priv->temp;
  150. return IIO_VAL_INT;
  151. default:
  152. return -EINVAL;
  153. }
  154. break;
  155. case IIO_CHAN_INFO_SCALE:
  156. switch (chan->type) {
  157. case IIO_PRESSURE:
  158. *val = 0;
  159. *val2 = 1000;
  160. return IIO_VAL_INT_PLUS_MICRO;
  161. case IIO_TEMP:
  162. *val = 10;
  163. return IIO_VAL_INT;
  164. default:
  165. return -EINVAL;
  166. }
  167. break;
  168. default:
  169. return -EINVAL;
  170. }
  171. return -EINVAL;
  172. }
  173. static const struct iio_info hp03_info = {
  174. .driver_module = THIS_MODULE,
  175. .read_raw = &hp03_read_raw,
  176. };
  177. static int hp03_probe(struct i2c_client *client,
  178. const struct i2c_device_id *id)
  179. {
  180. struct device *dev = &client->dev;
  181. struct iio_dev *indio_dev;
  182. struct hp03_priv *priv;
  183. int ret;
  184. indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
  185. if (!indio_dev)
  186. return -ENOMEM;
  187. priv = iio_priv(indio_dev);
  188. priv->client = client;
  189. mutex_init(&priv->lock);
  190. indio_dev->dev.parent = dev;
  191. indio_dev->name = id->name;
  192. indio_dev->channels = hp03_channels;
  193. indio_dev->num_channels = ARRAY_SIZE(hp03_channels);
  194. indio_dev->info = &hp03_info;
  195. indio_dev->modes = INDIO_DIRECT_MODE;
  196. priv->xclr_gpio = devm_gpiod_get_index(dev, "xclr", 0, GPIOD_OUT_HIGH);
  197. if (IS_ERR(priv->xclr_gpio)) {
  198. dev_err(dev, "Failed to claim XCLR GPIO\n");
  199. ret = PTR_ERR(priv->xclr_gpio);
  200. return ret;
  201. }
  202. /*
  203. * Allocate another device for the on-sensor EEPROM,
  204. * which has it's dedicated I2C address and contains
  205. * the calibration constants for the sensor.
  206. */
  207. priv->eeprom_client = i2c_new_dummy(client->adapter, HP03_EEPROM_ADDR);
  208. if (!priv->eeprom_client) {
  209. dev_err(dev, "New EEPROM I2C device failed\n");
  210. return -ENODEV;
  211. }
  212. priv->eeprom_regmap = regmap_init_i2c(priv->eeprom_client,
  213. &hp03_regmap_config);
  214. if (IS_ERR(priv->eeprom_regmap)) {
  215. dev_err(dev, "Failed to allocate EEPROM regmap\n");
  216. ret = PTR_ERR(priv->eeprom_regmap);
  217. goto err_cleanup_eeprom_client;
  218. }
  219. ret = iio_device_register(indio_dev);
  220. if (ret) {
  221. dev_err(dev, "Failed to register IIO device\n");
  222. goto err_cleanup_eeprom_regmap;
  223. }
  224. i2c_set_clientdata(client, indio_dev);
  225. return 0;
  226. err_cleanup_eeprom_regmap:
  227. regmap_exit(priv->eeprom_regmap);
  228. err_cleanup_eeprom_client:
  229. i2c_unregister_device(priv->eeprom_client);
  230. return ret;
  231. }
  232. static int hp03_remove(struct i2c_client *client)
  233. {
  234. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  235. struct hp03_priv *priv = iio_priv(indio_dev);
  236. iio_device_unregister(indio_dev);
  237. regmap_exit(priv->eeprom_regmap);
  238. i2c_unregister_device(priv->eeprom_client);
  239. return 0;
  240. }
  241. static const struct i2c_device_id hp03_id[] = {
  242. { "hp03", 0 },
  243. { },
  244. };
  245. MODULE_DEVICE_TABLE(i2c, hp03_id);
  246. static struct i2c_driver hp03_driver = {
  247. .driver = {
  248. .name = "hp03",
  249. },
  250. .probe = hp03_probe,
  251. .remove = hp03_remove,
  252. .id_table = hp03_id,
  253. };
  254. module_i2c_driver(hp03_driver);
  255. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  256. MODULE_DESCRIPTION("Driver for Hope RF HP03 pressure and temperature sensor");
  257. MODULE_LICENSE("GPL v2");