t5403.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * t5403.c - Support for EPCOS T5403 pressure/temperature sensor
  3. *
  4. * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * (7-bit I2C slave address 0x77)
  11. *
  12. * TODO: end-of-conversion irq
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #include <linux/delay.h>
  19. #define T5403_DATA 0xf5 /* data, LSB first, 16 bit */
  20. #define T5403_CALIB_DATA 0x8e /* 10 calibration coeff., LSB first, 16 bit */
  21. #define T5403_SLAVE_ADDR 0x88 /* I2C slave address, 0x77 */
  22. #define T5403_COMMAND 0xf1
  23. /* command bits */
  24. #define T5403_MODE_SHIFT 3 /* conversion time: 2, 8, 16, 66 ms */
  25. #define T5403_PT BIT(1) /* 0 .. pressure, 1 .. temperature measurement */
  26. #define T5403_SCO BIT(0) /* start conversion */
  27. #define T5403_MODE_LOW 0
  28. #define T5403_MODE_STANDARD 1
  29. #define T5403_MODE_HIGH 2
  30. #define T5403_MODE_ULTRA_HIGH 3
  31. #define T5403_I2C_MASK (~BIT(7))
  32. #define T5403_I2C_ADDR 0x77
  33. static const int t5403_pressure_conv_ms[] = {2, 8, 16, 66};
  34. struct t5403_data {
  35. struct i2c_client *client;
  36. struct mutex lock;
  37. int mode;
  38. __le16 c[10];
  39. };
  40. #define T5403_C_U16(i) le16_to_cpu(data->c[(i) - 1])
  41. #define T5403_C(i) sign_extend32(T5403_C_U16(i), 15)
  42. static int t5403_read(struct t5403_data *data, bool pressure)
  43. {
  44. int wait_time = 3; /* wakeup time in ms */
  45. int ret = i2c_smbus_write_byte_data(data->client, T5403_COMMAND,
  46. (pressure ? (data->mode << T5403_MODE_SHIFT) : T5403_PT) |
  47. T5403_SCO);
  48. if (ret < 0)
  49. return ret;
  50. wait_time += pressure ? t5403_pressure_conv_ms[data->mode] : 2;
  51. msleep(wait_time);
  52. return i2c_smbus_read_word_data(data->client, T5403_DATA);
  53. }
  54. static int t5403_comp_pressure(struct t5403_data *data, int *val, int *val2)
  55. {
  56. int ret;
  57. s16 t_r;
  58. u16 p_r;
  59. s32 S, O, X;
  60. mutex_lock(&data->lock);
  61. ret = t5403_read(data, false);
  62. if (ret < 0)
  63. goto done;
  64. t_r = ret;
  65. ret = t5403_read(data, true);
  66. if (ret < 0)
  67. goto done;
  68. p_r = ret;
  69. /* see EPCOS application note */
  70. S = T5403_C_U16(3) + (s32) T5403_C_U16(4) * t_r / 0x20000 +
  71. T5403_C(5) * t_r / 0x8000 * t_r / 0x80000 +
  72. T5403_C(9) * t_r / 0x8000 * t_r / 0x8000 * t_r / 0x10000;
  73. O = T5403_C(6) * 0x4000 + T5403_C(7) * t_r / 8 +
  74. T5403_C(8) * t_r / 0x8000 * t_r / 16 +
  75. T5403_C(9) * t_r / 0x8000 * t_r / 0x10000 * t_r;
  76. X = (S * p_r + O) / 0x4000;
  77. X += ((X - 75000) * (X - 75000) / 0x10000 - 9537) *
  78. T5403_C(10) / 0x10000;
  79. *val = X / 1000;
  80. *val2 = (X % 1000) * 1000;
  81. done:
  82. mutex_unlock(&data->lock);
  83. return ret;
  84. }
  85. static int t5403_comp_temp(struct t5403_data *data, int *val)
  86. {
  87. int ret;
  88. s16 t_r;
  89. mutex_lock(&data->lock);
  90. ret = t5403_read(data, false);
  91. if (ret < 0)
  92. goto done;
  93. t_r = ret;
  94. /* see EPCOS application note */
  95. *val = ((s32) T5403_C_U16(1) * t_r / 0x100 +
  96. (s32) T5403_C_U16(2) * 0x40) * 1000 / 0x10000;
  97. done:
  98. mutex_unlock(&data->lock);
  99. return ret;
  100. }
  101. static int t5403_read_raw(struct iio_dev *indio_dev,
  102. struct iio_chan_spec const *chan,
  103. int *val, int *val2, long mask)
  104. {
  105. struct t5403_data *data = iio_priv(indio_dev);
  106. int ret;
  107. switch (mask) {
  108. case IIO_CHAN_INFO_PROCESSED:
  109. switch (chan->type) {
  110. case IIO_PRESSURE:
  111. ret = t5403_comp_pressure(data, val, val2);
  112. if (ret < 0)
  113. return ret;
  114. return IIO_VAL_INT_PLUS_MICRO;
  115. case IIO_TEMP:
  116. ret = t5403_comp_temp(data, val);
  117. if (ret < 0)
  118. return ret;
  119. return IIO_VAL_INT;
  120. default:
  121. return -EINVAL;
  122. }
  123. case IIO_CHAN_INFO_INT_TIME:
  124. *val = 0;
  125. *val2 = t5403_pressure_conv_ms[data->mode] * 1000;
  126. return IIO_VAL_INT_PLUS_MICRO;
  127. default:
  128. return -EINVAL;
  129. }
  130. }
  131. static int t5403_write_raw(struct iio_dev *indio_dev,
  132. struct iio_chan_spec const *chan,
  133. int val, int val2, long mask)
  134. {
  135. struct t5403_data *data = iio_priv(indio_dev);
  136. int i;
  137. switch (mask) {
  138. case IIO_CHAN_INFO_INT_TIME:
  139. if (val != 0)
  140. return -EINVAL;
  141. for (i = 0; i < ARRAY_SIZE(t5403_pressure_conv_ms); i++)
  142. if (val2 == t5403_pressure_conv_ms[i] * 1000) {
  143. mutex_lock(&data->lock);
  144. data->mode = i;
  145. mutex_unlock(&data->lock);
  146. return 0;
  147. }
  148. return -EINVAL;
  149. default:
  150. return -EINVAL;
  151. }
  152. }
  153. static const struct iio_chan_spec t5403_channels[] = {
  154. {
  155. .type = IIO_PRESSURE,
  156. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  157. BIT(IIO_CHAN_INFO_INT_TIME),
  158. },
  159. {
  160. .type = IIO_TEMP,
  161. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  162. },
  163. };
  164. static IIO_CONST_ATTR_INT_TIME_AVAIL("0.002 0.008 0.016 0.066");
  165. static struct attribute *t5403_attributes[] = {
  166. &iio_const_attr_integration_time_available.dev_attr.attr,
  167. NULL
  168. };
  169. static const struct attribute_group t5403_attribute_group = {
  170. .attrs = t5403_attributes,
  171. };
  172. static const struct iio_info t5403_info = {
  173. .read_raw = &t5403_read_raw,
  174. .write_raw = &t5403_write_raw,
  175. .attrs = &t5403_attribute_group,
  176. .driver_module = THIS_MODULE,
  177. };
  178. static int t5403_probe(struct i2c_client *client,
  179. const struct i2c_device_id *id)
  180. {
  181. struct t5403_data *data;
  182. struct iio_dev *indio_dev;
  183. int ret;
  184. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
  185. I2C_FUNC_SMBUS_I2C_BLOCK))
  186. return -ENODEV;
  187. ret = i2c_smbus_read_byte_data(client, T5403_SLAVE_ADDR);
  188. if (ret < 0)
  189. return ret;
  190. if ((ret & T5403_I2C_MASK) != T5403_I2C_ADDR)
  191. return -ENODEV;
  192. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  193. if (!indio_dev)
  194. return -ENOMEM;
  195. data = iio_priv(indio_dev);
  196. data->client = client;
  197. mutex_init(&data->lock);
  198. i2c_set_clientdata(client, indio_dev);
  199. indio_dev->info = &t5403_info;
  200. indio_dev->name = id->name;
  201. indio_dev->dev.parent = &client->dev;
  202. indio_dev->modes = INDIO_DIRECT_MODE;
  203. indio_dev->channels = t5403_channels;
  204. indio_dev->num_channels = ARRAY_SIZE(t5403_channels);
  205. data->mode = T5403_MODE_STANDARD;
  206. ret = i2c_smbus_read_i2c_block_data(data->client, T5403_CALIB_DATA,
  207. sizeof(data->c), (u8 *) data->c);
  208. if (ret < 0)
  209. return ret;
  210. return devm_iio_device_register(&client->dev, indio_dev);
  211. }
  212. static const struct i2c_device_id t5403_id[] = {
  213. { "t5403", 0 },
  214. { }
  215. };
  216. MODULE_DEVICE_TABLE(i2c, t5403_id);
  217. static struct i2c_driver t5403_driver = {
  218. .driver = {
  219. .name = "t5403",
  220. },
  221. .probe = t5403_probe,
  222. .id_table = t5403_id,
  223. };
  224. module_i2c_driver(t5403_driver);
  225. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  226. MODULE_DESCRIPTION("EPCOS T5403 pressure/temperature sensor driver");
  227. MODULE_LICENSE("GPL");