hp03.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. .read_raw = &hp03_read_raw,
  175. };
  176. static int hp03_probe(struct i2c_client *client,
  177. const struct i2c_device_id *id)
  178. {
  179. struct device *dev = &client->dev;
  180. struct iio_dev *indio_dev;
  181. struct hp03_priv *priv;
  182. int ret;
  183. indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
  184. if (!indio_dev)
  185. return -ENOMEM;
  186. priv = iio_priv(indio_dev);
  187. priv->client = client;
  188. mutex_init(&priv->lock);
  189. indio_dev->dev.parent = dev;
  190. indio_dev->name = id->name;
  191. indio_dev->channels = hp03_channels;
  192. indio_dev->num_channels = ARRAY_SIZE(hp03_channels);
  193. indio_dev->info = &hp03_info;
  194. indio_dev->modes = INDIO_DIRECT_MODE;
  195. priv->xclr_gpio = devm_gpiod_get_index(dev, "xclr", 0, GPIOD_OUT_HIGH);
  196. if (IS_ERR(priv->xclr_gpio)) {
  197. dev_err(dev, "Failed to claim XCLR GPIO\n");
  198. ret = PTR_ERR(priv->xclr_gpio);
  199. return ret;
  200. }
  201. /*
  202. * Allocate another device for the on-sensor EEPROM,
  203. * which has it's dedicated I2C address and contains
  204. * the calibration constants for the sensor.
  205. */
  206. priv->eeprom_client = i2c_new_dummy(client->adapter, HP03_EEPROM_ADDR);
  207. if (!priv->eeprom_client) {
  208. dev_err(dev, "New EEPROM I2C device failed\n");
  209. return -ENODEV;
  210. }
  211. priv->eeprom_regmap = regmap_init_i2c(priv->eeprom_client,
  212. &hp03_regmap_config);
  213. if (IS_ERR(priv->eeprom_regmap)) {
  214. dev_err(dev, "Failed to allocate EEPROM regmap\n");
  215. ret = PTR_ERR(priv->eeprom_regmap);
  216. goto err_cleanup_eeprom_client;
  217. }
  218. ret = iio_device_register(indio_dev);
  219. if (ret) {
  220. dev_err(dev, "Failed to register IIO device\n");
  221. goto err_cleanup_eeprom_regmap;
  222. }
  223. i2c_set_clientdata(client, indio_dev);
  224. return 0;
  225. err_cleanup_eeprom_regmap:
  226. regmap_exit(priv->eeprom_regmap);
  227. err_cleanup_eeprom_client:
  228. i2c_unregister_device(priv->eeprom_client);
  229. return ret;
  230. }
  231. static int hp03_remove(struct i2c_client *client)
  232. {
  233. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  234. struct hp03_priv *priv = iio_priv(indio_dev);
  235. iio_device_unregister(indio_dev);
  236. regmap_exit(priv->eeprom_regmap);
  237. i2c_unregister_device(priv->eeprom_client);
  238. return 0;
  239. }
  240. static const struct i2c_device_id hp03_id[] = {
  241. { "hp03", 0 },
  242. { },
  243. };
  244. MODULE_DEVICE_TABLE(i2c, hp03_id);
  245. static const struct of_device_id hp03_of_match[] = {
  246. { .compatible = "hoperf,hp03" },
  247. { },
  248. };
  249. MODULE_DEVICE_TABLE(of, hp03_of_match);
  250. static struct i2c_driver hp03_driver = {
  251. .driver = {
  252. .name = "hp03",
  253. .of_match_table = hp03_of_match,
  254. },
  255. .probe = hp03_probe,
  256. .remove = hp03_remove,
  257. .id_table = hp03_id,
  258. };
  259. module_i2c_driver(hp03_driver);
  260. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  261. MODULE_DESCRIPTION("Driver for Hope RF HP03 pressure and temperature sensor");
  262. MODULE_LICENSE("GPL v2");