tmp117.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Digital temperature sensor with integrated Non-volatile memory
  4. * Copyright (c) 2021 Puranjay Mohan <puranjay12@gmail.com>
  5. *
  6. * Driver for the Texas Instruments TMP117 Temperature Sensor
  7. * (7-bit I2C slave address (0x48 - 0x4B), changeable via ADD pins)
  8. *
  9. * Note: This driver assumes that the sensor has been calibrated beforehand.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/bitops.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/limits.h>
  19. #include <linux/property.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/iio/iio.h>
  22. #define TMP117_REG_TEMP 0x0
  23. #define TMP117_REG_CFGR 0x1
  24. #define TMP117_REG_HIGH_LIM 0x2
  25. #define TMP117_REG_LOW_LIM 0x3
  26. #define TMP117_REG_EEPROM_UL 0x4
  27. #define TMP117_REG_EEPROM1 0x5
  28. #define TMP117_REG_EEPROM2 0x6
  29. #define TMP117_REG_TEMP_OFFSET 0x7
  30. #define TMP117_REG_EEPROM3 0x8
  31. #define TMP117_REG_DEVICE_ID 0xF
  32. #define TMP117_RESOLUTION_10UC 78125
  33. #define MICRODEGREE_PER_10MILLIDEGREE 10000
  34. #define TMP116_DEVICE_ID 0x1116
  35. #define TMP117_DEVICE_ID 0x0117
  36. struct tmp117_data {
  37. struct i2c_client *client;
  38. s16 calibbias;
  39. };
  40. struct tmp11x_info {
  41. const char *name;
  42. struct iio_chan_spec const *channels;
  43. int num_channels;
  44. };
  45. static int tmp117_read_raw(struct iio_dev *indio_dev,
  46. struct iio_chan_spec const *channel, int *val,
  47. int *val2, long mask)
  48. {
  49. struct tmp117_data *data = iio_priv(indio_dev);
  50. s32 ret;
  51. switch (mask) {
  52. case IIO_CHAN_INFO_RAW:
  53. ret = i2c_smbus_read_word_swapped(data->client,
  54. TMP117_REG_TEMP);
  55. if (ret < 0)
  56. return ret;
  57. *val = sign_extend32(ret, 15);
  58. return IIO_VAL_INT;
  59. case IIO_CHAN_INFO_CALIBBIAS:
  60. ret = i2c_smbus_read_word_swapped(data->client,
  61. TMP117_REG_TEMP_OFFSET);
  62. if (ret < 0)
  63. return ret;
  64. *val = sign_extend32(ret, 15);
  65. return IIO_VAL_INT;
  66. case IIO_CHAN_INFO_SCALE:
  67. /*
  68. * Conversion from 10s of uC to mC
  69. * as IIO reports temperature in mC
  70. */
  71. *val = TMP117_RESOLUTION_10UC / MICRODEGREE_PER_10MILLIDEGREE;
  72. *val2 = (TMP117_RESOLUTION_10UC %
  73. MICRODEGREE_PER_10MILLIDEGREE) * 100;
  74. return IIO_VAL_INT_PLUS_MICRO;
  75. default:
  76. return -EINVAL;
  77. }
  78. }
  79. static int tmp117_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec
  80. const *channel, int val, int val2, long mask)
  81. {
  82. struct tmp117_data *data = iio_priv(indio_dev);
  83. s16 off;
  84. switch (mask) {
  85. case IIO_CHAN_INFO_CALIBBIAS:
  86. off = clamp_t(int, val, S16_MIN, S16_MAX);
  87. if (off == data->calibbias)
  88. return 0;
  89. data->calibbias = off;
  90. return i2c_smbus_write_word_swapped(data->client,
  91. TMP117_REG_TEMP_OFFSET, off);
  92. default:
  93. return -EINVAL;
  94. }
  95. }
  96. static const struct iio_chan_spec tmp117_channels[] = {
  97. {
  98. .type = IIO_TEMP,
  99. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  100. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  101. BIT(IIO_CHAN_INFO_SCALE),
  102. },
  103. };
  104. static const struct iio_chan_spec tmp116_channels[] = {
  105. {
  106. .type = IIO_TEMP,
  107. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  108. BIT(IIO_CHAN_INFO_SCALE),
  109. },
  110. };
  111. static const struct tmp11x_info tmp116_channels_info = {
  112. .name = "tmp116",
  113. .channels = tmp116_channels,
  114. .num_channels = ARRAY_SIZE(tmp116_channels)
  115. };
  116. static const struct tmp11x_info tmp117_channels_info = {
  117. .name = "tmp117",
  118. .channels = tmp117_channels,
  119. .num_channels = ARRAY_SIZE(tmp117_channels)
  120. };
  121. static const struct iio_info tmp117_info = {
  122. .read_raw = tmp117_read_raw,
  123. .write_raw = tmp117_write_raw,
  124. };
  125. static int tmp117_probe(struct i2c_client *client)
  126. {
  127. const struct tmp11x_info *match_data;
  128. struct tmp117_data *data;
  129. struct iio_dev *indio_dev;
  130. int dev_id;
  131. int ret;
  132. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
  133. return -EOPNOTSUPP;
  134. ret = devm_regulator_get_enable(&client->dev, "vcc");
  135. if (ret)
  136. return ret;
  137. fsleep(1500);
  138. dev_id = i2c_smbus_read_word_swapped(client, TMP117_REG_DEVICE_ID);
  139. if (dev_id < 0)
  140. return dev_id;
  141. switch (dev_id) {
  142. case TMP116_DEVICE_ID:
  143. match_data = &tmp116_channels_info;
  144. break;
  145. case TMP117_DEVICE_ID:
  146. match_data = &tmp117_channels_info;
  147. break;
  148. default:
  149. dev_info(&client->dev,
  150. "Unknown device id (0x%x), use fallback compatible\n",
  151. dev_id);
  152. match_data = i2c_get_match_data(client);
  153. }
  154. if (!match_data)
  155. return dev_err_probe(&client->dev, -ENODEV,
  156. "Failed to identify unsupported device\n");
  157. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  158. if (!indio_dev)
  159. return -ENOMEM;
  160. data = iio_priv(indio_dev);
  161. data->client = client;
  162. data->calibbias = 0;
  163. indio_dev->modes = INDIO_DIRECT_MODE;
  164. indio_dev->info = &tmp117_info;
  165. indio_dev->channels = match_data->channels;
  166. indio_dev->num_channels = match_data->num_channels;
  167. indio_dev->name = match_data->name;
  168. return devm_iio_device_register(&client->dev, indio_dev);
  169. }
  170. static const struct of_device_id tmp117_of_match[] = {
  171. { .compatible = "ti,tmp116", .data = &tmp116_channels_info },
  172. { .compatible = "ti,tmp117", .data = &tmp117_channels_info },
  173. { }
  174. };
  175. MODULE_DEVICE_TABLE(of, tmp117_of_match);
  176. static const struct i2c_device_id tmp117_id[] = {
  177. { "tmp116", (kernel_ulong_t)&tmp116_channels_info },
  178. { "tmp117", (kernel_ulong_t)&tmp117_channels_info },
  179. { }
  180. };
  181. MODULE_DEVICE_TABLE(i2c, tmp117_id);
  182. static struct i2c_driver tmp117_driver = {
  183. .driver = {
  184. .name = "tmp117",
  185. .of_match_table = tmp117_of_match,
  186. },
  187. .probe = tmp117_probe,
  188. .id_table = tmp117_id,
  189. };
  190. module_i2c_driver(tmp117_driver);
  191. MODULE_AUTHOR("Puranjay Mohan <puranjay12@gmail.com>");
  192. MODULE_DESCRIPTION("TI TMP117 Temperature sensor driver");
  193. MODULE_LICENSE("GPL");