abp060mg.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (C) 2016 - Marcin Malagowski <mrc@bourne.st>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/i2c.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/iio/iio.h>
  22. #define ABP060MG_ERROR_MASK 0xC000
  23. #define ABP060MG_RESP_TIME_MS 40
  24. #define ABP060MG_MIN_COUNTS 1638 /* = 0x0666 (10% of u14) */
  25. #define ABP060MG_MAX_COUNTS 14745 /* = 0x3999 (90% of u14) */
  26. #define ABP060MG_NUM_COUNTS (ABP060MG_MAX_COUNTS - ABP060MG_MIN_COUNTS)
  27. enum abp_variant {
  28. /* gage [kPa] */
  29. ABP006KG, ABP010KG, ABP016KG, ABP025KG, ABP040KG, ABP060KG, ABP100KG,
  30. ABP160KG, ABP250KG, ABP400KG, ABP600KG, ABP001GG,
  31. /* differential [kPa] */
  32. ABP006KD, ABP010KD, ABP016KD, ABP025KD, ABP040KD, ABP060KD, ABP100KD,
  33. ABP160KD, ABP250KD, ABP400KD,
  34. /* gage [psi] */
  35. ABP001PG, ABP005PG, ABP015PG, ABP030PG, ABP060PG, ABP100PG, ABP150PG,
  36. /* differential [psi] */
  37. ABP001PD, ABP005PD, ABP015PD, ABP030PD, ABP060PD,
  38. };
  39. struct abp_config {
  40. int min;
  41. int max;
  42. };
  43. static struct abp_config abp_config[] = {
  44. /* mbar & kPa variants */
  45. [ABP006KG] = { .min = 0, .max = 6000 },
  46. [ABP010KG] = { .min = 0, .max = 10000 },
  47. [ABP016KG] = { .min = 0, .max = 16000 },
  48. [ABP025KG] = { .min = 0, .max = 25000 },
  49. [ABP040KG] = { .min = 0, .max = 40000 },
  50. [ABP060KG] = { .min = 0, .max = 60000 },
  51. [ABP100KG] = { .min = 0, .max = 100000 },
  52. [ABP160KG] = { .min = 0, .max = 160000 },
  53. [ABP250KG] = { .min = 0, .max = 250000 },
  54. [ABP400KG] = { .min = 0, .max = 400000 },
  55. [ABP600KG] = { .min = 0, .max = 600000 },
  56. [ABP001GG] = { .min = 0, .max = 1000000 },
  57. [ABP006KD] = { .min = -6000, .max = 6000 },
  58. [ABP010KD] = { .min = -10000, .max = 10000 },
  59. [ABP016KD] = { .min = -16000, .max = 16000 },
  60. [ABP025KD] = { .min = -25000, .max = 25000 },
  61. [ABP040KD] = { .min = -40000, .max = 40000 },
  62. [ABP060KD] = { .min = -60000, .max = 60000 },
  63. [ABP100KD] = { .min = -100000, .max = 100000 },
  64. [ABP160KD] = { .min = -160000, .max = 160000 },
  65. [ABP250KD] = { .min = -250000, .max = 250000 },
  66. [ABP400KD] = { .min = -400000, .max = 400000 },
  67. /* psi variants (1 psi ~ 6895 Pa) */
  68. [ABP001PG] = { .min = 0, .max = 6985 },
  69. [ABP005PG] = { .min = 0, .max = 34474 },
  70. [ABP015PG] = { .min = 0, .max = 103421 },
  71. [ABP030PG] = { .min = 0, .max = 206843 },
  72. [ABP060PG] = { .min = 0, .max = 413686 },
  73. [ABP100PG] = { .min = 0, .max = 689476 },
  74. [ABP150PG] = { .min = 0, .max = 1034214 },
  75. [ABP001PD] = { .min = -6895, .max = 6895 },
  76. [ABP005PD] = { .min = -34474, .max = 34474 },
  77. [ABP015PD] = { .min = -103421, .max = 103421 },
  78. [ABP030PD] = { .min = -206843, .max = 206843 },
  79. [ABP060PD] = { .min = -413686, .max = 413686 },
  80. };
  81. struct abp_state {
  82. struct i2c_client *client;
  83. struct mutex lock;
  84. /*
  85. * bus-dependent MEASURE_REQUEST length.
  86. * If no SMBUS_QUICK support, need to send dummy byte
  87. */
  88. int mreq_len;
  89. /* model-dependent values (calculated on probe) */
  90. int scale;
  91. int offset;
  92. };
  93. static const struct iio_chan_spec abp060mg_channels[] = {
  94. {
  95. .type = IIO_PRESSURE,
  96. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  97. BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE),
  98. },
  99. };
  100. static int abp060mg_get_measurement(struct abp_state *state, int *val)
  101. {
  102. struct i2c_client *client = state->client;
  103. __be16 buf[2];
  104. u16 pressure;
  105. int ret;
  106. buf[0] = 0;
  107. ret = i2c_master_send(client, (u8 *)&buf, state->mreq_len);
  108. if (ret < 0)
  109. return ret;
  110. msleep_interruptible(ABP060MG_RESP_TIME_MS);
  111. ret = i2c_master_recv(client, (u8 *)&buf, sizeof(buf));
  112. if (ret < 0)
  113. return ret;
  114. pressure = be16_to_cpu(buf[0]);
  115. if (pressure & ABP060MG_ERROR_MASK)
  116. return -EIO;
  117. if (pressure < ABP060MG_MIN_COUNTS || pressure > ABP060MG_MAX_COUNTS)
  118. return -EIO;
  119. *val = pressure;
  120. return IIO_VAL_INT;
  121. }
  122. static int abp060mg_read_raw(struct iio_dev *indio_dev,
  123. struct iio_chan_spec const *chan, int *val,
  124. int *val2, long mask)
  125. {
  126. struct abp_state *state = iio_priv(indio_dev);
  127. int ret;
  128. mutex_lock(&state->lock);
  129. switch (mask) {
  130. case IIO_CHAN_INFO_RAW:
  131. ret = abp060mg_get_measurement(state, val);
  132. break;
  133. case IIO_CHAN_INFO_OFFSET:
  134. *val = state->offset;
  135. ret = IIO_VAL_INT;
  136. break;
  137. case IIO_CHAN_INFO_SCALE:
  138. *val = state->scale;
  139. *val2 = ABP060MG_NUM_COUNTS * 1000; /* to kPa */
  140. ret = IIO_VAL_FRACTIONAL;
  141. break;
  142. default:
  143. ret = -EINVAL;
  144. break;
  145. }
  146. mutex_unlock(&state->lock);
  147. return ret;
  148. }
  149. static const struct iio_info abp060mg_info = {
  150. .read_raw = abp060mg_read_raw,
  151. };
  152. static void abp060mg_init_device(struct iio_dev *indio_dev, unsigned long id)
  153. {
  154. struct abp_state *state = iio_priv(indio_dev);
  155. struct abp_config *cfg = &abp_config[id];
  156. state->scale = cfg->max - cfg->min;
  157. state->offset = -ABP060MG_MIN_COUNTS;
  158. if (cfg->min < 0) /* differential */
  159. state->offset -= ABP060MG_NUM_COUNTS >> 1;
  160. }
  161. static int abp060mg_probe(struct i2c_client *client,
  162. const struct i2c_device_id *id)
  163. {
  164. struct iio_dev *indio_dev;
  165. struct abp_state *state;
  166. unsigned long cfg_id = id->driver_data;
  167. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
  168. if (!indio_dev)
  169. return -ENOMEM;
  170. state = iio_priv(indio_dev);
  171. i2c_set_clientdata(client, state);
  172. state->client = client;
  173. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
  174. state->mreq_len = 1;
  175. abp060mg_init_device(indio_dev, cfg_id);
  176. indio_dev->dev.parent = &client->dev;
  177. indio_dev->name = dev_name(&client->dev);
  178. indio_dev->modes = INDIO_DIRECT_MODE;
  179. indio_dev->info = &abp060mg_info;
  180. indio_dev->channels = abp060mg_channels;
  181. indio_dev->num_channels = ARRAY_SIZE(abp060mg_channels);
  182. mutex_init(&state->lock);
  183. return devm_iio_device_register(&client->dev, indio_dev);
  184. }
  185. static const struct i2c_device_id abp060mg_id_table[] = {
  186. /* mbar & kPa variants (abp060m [60 mbar] == abp006k [6 kPa]) */
  187. /* gage: */
  188. { "abp060mg", ABP006KG }, { "abp006kg", ABP006KG },
  189. { "abp100mg", ABP010KG }, { "abp010kg", ABP010KG },
  190. { "abp160mg", ABP016KG }, { "abp016kg", ABP016KG },
  191. { "abp250mg", ABP025KG }, { "abp025kg", ABP025KG },
  192. { "abp400mg", ABP040KG }, { "abp040kg", ABP040KG },
  193. { "abp600mg", ABP060KG }, { "abp060kg", ABP060KG },
  194. { "abp001bg", ABP100KG }, { "abp100kg", ABP100KG },
  195. { "abp1_6bg", ABP160KG }, { "abp160kg", ABP160KG },
  196. { "abp2_5bg", ABP250KG }, { "abp250kg", ABP250KG },
  197. { "abp004bg", ABP400KG }, { "abp400kg", ABP400KG },
  198. { "abp006bg", ABP600KG }, { "abp600kg", ABP600KG },
  199. { "abp010bg", ABP001GG }, { "abp001gg", ABP001GG },
  200. /* differential: */
  201. { "abp060md", ABP006KD }, { "abp006kd", ABP006KD },
  202. { "abp100md", ABP010KD }, { "abp010kd", ABP010KD },
  203. { "abp160md", ABP016KD }, { "abp016kd", ABP016KD },
  204. { "abp250md", ABP025KD }, { "abp025kd", ABP025KD },
  205. { "abp400md", ABP040KD }, { "abp040kd", ABP040KD },
  206. { "abp600md", ABP060KD }, { "abp060kd", ABP060KD },
  207. { "abp001bd", ABP100KD }, { "abp100kd", ABP100KD },
  208. { "abp1_6bd", ABP160KD }, { "abp160kd", ABP160KD },
  209. { "abp2_5bd", ABP250KD }, { "abp250kd", ABP250KD },
  210. { "abp004bd", ABP400KD }, { "abp400kd", ABP400KD },
  211. /* psi variants */
  212. /* gage: */
  213. { "abp001pg", ABP001PG },
  214. { "abp005pg", ABP005PG },
  215. { "abp015pg", ABP015PG },
  216. { "abp030pg", ABP030PG },
  217. { "abp060pg", ABP060PG },
  218. { "abp100pg", ABP100PG },
  219. { "abp150pg", ABP150PG },
  220. /* differential: */
  221. { "abp001pd", ABP001PD },
  222. { "abp005pd", ABP005PD },
  223. { "abp015pd", ABP015PD },
  224. { "abp030pd", ABP030PD },
  225. { "abp060pd", ABP060PD },
  226. { /* empty */ },
  227. };
  228. MODULE_DEVICE_TABLE(i2c, abp060mg_id_table);
  229. static struct i2c_driver abp060mg_driver = {
  230. .driver = {
  231. .name = "abp060mg",
  232. },
  233. .probe = abp060mg_probe,
  234. .id_table = abp060mg_id_table,
  235. };
  236. module_i2c_driver(abp060mg_driver);
  237. MODULE_AUTHOR("Marcin Malagowski <mrc@bourne.st>");
  238. MODULE_DESCRIPTION("Honeywell ABP pressure sensor driver");
  239. MODULE_LICENSE("GPL");