tmp102.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Texas Instruments TMP102 SMBus temperature sensor driver
  3. *
  4. * Copyright (C) 2010 Steven King <sfking@fdwdc.com>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/i2c.h>
  11. #include <linux/hwmon.h>
  12. #include <linux/hwmon-sysfs.h>
  13. #include <linux/err.h>
  14. #include <linux/mutex.h>
  15. #include <linux/device.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/regmap.h>
  18. #include <linux/of.h>
  19. #define DRIVER_NAME "tmp102"
  20. #define TMP102_TEMP_REG 0x00
  21. #define TMP102_CONF_REG 0x01
  22. /* note: these bit definitions are byte swapped */
  23. #define TMP102_CONF_SD 0x0100
  24. #define TMP102_CONF_TM 0x0200
  25. #define TMP102_CONF_POL 0x0400
  26. #define TMP102_CONF_F0 0x0800
  27. #define TMP102_CONF_F1 0x1000
  28. #define TMP102_CONF_R0 0x2000
  29. #define TMP102_CONF_R1 0x4000
  30. #define TMP102_CONF_OS 0x8000
  31. #define TMP102_CONF_EM 0x0010
  32. #define TMP102_CONF_AL 0x0020
  33. #define TMP102_CONF_CR0 0x0040
  34. #define TMP102_CONF_CR1 0x0080
  35. #define TMP102_TLOW_REG 0x02
  36. #define TMP102_THIGH_REG 0x03
  37. #define TMP102_CONFREG_MASK (TMP102_CONF_SD | TMP102_CONF_TM | \
  38. TMP102_CONF_POL | TMP102_CONF_F0 | \
  39. TMP102_CONF_F1 | TMP102_CONF_OS | \
  40. TMP102_CONF_EM | TMP102_CONF_AL | \
  41. TMP102_CONF_CR0 | TMP102_CONF_CR1)
  42. #define TMP102_CONFIG_CLEAR (TMP102_CONF_SD | TMP102_CONF_OS | \
  43. TMP102_CONF_CR0)
  44. #define TMP102_CONFIG_SET (TMP102_CONF_TM | TMP102_CONF_EM | \
  45. TMP102_CONF_CR1)
  46. #define CONVERSION_TIME_MS 35 /* in milli-seconds */
  47. struct tmp102 {
  48. struct regmap *regmap;
  49. u16 config_orig;
  50. unsigned long ready_time;
  51. };
  52. /* convert left adjusted 13-bit TMP102 register value to milliCelsius */
  53. static inline int tmp102_reg_to_mC(s16 val)
  54. {
  55. return ((val & ~0x01) * 1000) / 128;
  56. }
  57. /* convert milliCelsius to left adjusted 13-bit TMP102 register value */
  58. static inline u16 tmp102_mC_to_reg(int val)
  59. {
  60. return (val * 128) / 1000;
  61. }
  62. static int tmp102_read(struct device *dev, enum hwmon_sensor_types type,
  63. u32 attr, int channel, long *temp)
  64. {
  65. struct tmp102 *tmp102 = dev_get_drvdata(dev);
  66. unsigned int regval;
  67. int err, reg;
  68. switch (attr) {
  69. case hwmon_temp_input:
  70. /* Is it too early to return a conversion ? */
  71. if (time_before(jiffies, tmp102->ready_time)) {
  72. dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__);
  73. return -EAGAIN;
  74. }
  75. reg = TMP102_TEMP_REG;
  76. break;
  77. case hwmon_temp_max_hyst:
  78. reg = TMP102_TLOW_REG;
  79. break;
  80. case hwmon_temp_max:
  81. reg = TMP102_THIGH_REG;
  82. break;
  83. default:
  84. return -EOPNOTSUPP;
  85. }
  86. err = regmap_read(tmp102->regmap, reg, &regval);
  87. if (err < 0)
  88. return err;
  89. *temp = tmp102_reg_to_mC(regval);
  90. return 0;
  91. }
  92. static int tmp102_write(struct device *dev, enum hwmon_sensor_types type,
  93. u32 attr, int channel, long temp)
  94. {
  95. struct tmp102 *tmp102 = dev_get_drvdata(dev);
  96. int reg;
  97. switch (attr) {
  98. case hwmon_temp_max_hyst:
  99. reg = TMP102_TLOW_REG;
  100. break;
  101. case hwmon_temp_max:
  102. reg = TMP102_THIGH_REG;
  103. break;
  104. default:
  105. return -EOPNOTSUPP;
  106. }
  107. temp = clamp_val(temp, -256000, 255000);
  108. return regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(temp));
  109. }
  110. static umode_t tmp102_is_visible(const void *data, enum hwmon_sensor_types type,
  111. u32 attr, int channel)
  112. {
  113. if (type != hwmon_temp)
  114. return 0;
  115. switch (attr) {
  116. case hwmon_temp_input:
  117. return 0444;
  118. case hwmon_temp_max_hyst:
  119. case hwmon_temp_max:
  120. return 0644;
  121. default:
  122. return 0;
  123. }
  124. }
  125. static const struct hwmon_channel_info *tmp102_info[] = {
  126. HWMON_CHANNEL_INFO(chip,
  127. HWMON_C_REGISTER_TZ),
  128. HWMON_CHANNEL_INFO(temp,
  129. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
  130. NULL
  131. };
  132. static const struct hwmon_ops tmp102_hwmon_ops = {
  133. .is_visible = tmp102_is_visible,
  134. .read = tmp102_read,
  135. .write = tmp102_write,
  136. };
  137. static const struct hwmon_chip_info tmp102_chip_info = {
  138. .ops = &tmp102_hwmon_ops,
  139. .info = tmp102_info,
  140. };
  141. static void tmp102_restore_config(void *data)
  142. {
  143. struct tmp102 *tmp102 = data;
  144. regmap_write(tmp102->regmap, TMP102_CONF_REG, tmp102->config_orig);
  145. }
  146. static bool tmp102_is_writeable_reg(struct device *dev, unsigned int reg)
  147. {
  148. return reg != TMP102_TEMP_REG;
  149. }
  150. static bool tmp102_is_volatile_reg(struct device *dev, unsigned int reg)
  151. {
  152. return reg == TMP102_TEMP_REG;
  153. }
  154. static const struct regmap_config tmp102_regmap_config = {
  155. .reg_bits = 8,
  156. .val_bits = 16,
  157. .max_register = TMP102_THIGH_REG,
  158. .writeable_reg = tmp102_is_writeable_reg,
  159. .volatile_reg = tmp102_is_volatile_reg,
  160. .val_format_endian = REGMAP_ENDIAN_BIG,
  161. .cache_type = REGCACHE_RBTREE,
  162. .use_single_read = true,
  163. .use_single_write = true,
  164. };
  165. static int tmp102_probe(struct i2c_client *client)
  166. {
  167. struct device *dev = &client->dev;
  168. struct device *hwmon_dev;
  169. struct tmp102 *tmp102;
  170. unsigned int regval;
  171. int err;
  172. if (!i2c_check_functionality(client->adapter,
  173. I2C_FUNC_SMBUS_WORD_DATA)) {
  174. dev_err(dev,
  175. "adapter doesn't support SMBus word transactions\n");
  176. return -ENODEV;
  177. }
  178. tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL);
  179. if (!tmp102)
  180. return -ENOMEM;
  181. i2c_set_clientdata(client, tmp102);
  182. tmp102->regmap = devm_regmap_init_i2c(client, &tmp102_regmap_config);
  183. if (IS_ERR(tmp102->regmap))
  184. return PTR_ERR(tmp102->regmap);
  185. err = regmap_read(tmp102->regmap, TMP102_CONF_REG, &regval);
  186. if (err < 0) {
  187. dev_err(dev, "error reading config register\n");
  188. return err;
  189. }
  190. if ((regval & ~TMP102_CONFREG_MASK) !=
  191. (TMP102_CONF_R0 | TMP102_CONF_R1)) {
  192. dev_err(dev, "unexpected config register value\n");
  193. return -ENODEV;
  194. }
  195. tmp102->config_orig = regval;
  196. err = devm_add_action_or_reset(dev, tmp102_restore_config, tmp102);
  197. if (err)
  198. return err;
  199. regval &= ~TMP102_CONFIG_CLEAR;
  200. regval |= TMP102_CONFIG_SET;
  201. err = regmap_write(tmp102->regmap, TMP102_CONF_REG, regval);
  202. if (err < 0) {
  203. dev_err(dev, "error writing config register\n");
  204. return err;
  205. }
  206. /*
  207. * Mark that we are not ready with data until the first
  208. * conversion is complete
  209. */
  210. tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
  211. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  212. tmp102,
  213. &tmp102_chip_info,
  214. NULL);
  215. if (IS_ERR(hwmon_dev)) {
  216. dev_dbg(dev, "unable to register hwmon device\n");
  217. return PTR_ERR(hwmon_dev);
  218. }
  219. dev_info(dev, "initialized\n");
  220. return 0;
  221. }
  222. #ifdef CONFIG_PM_SLEEP
  223. static int tmp102_suspend(struct device *dev)
  224. {
  225. struct i2c_client *client = to_i2c_client(dev);
  226. struct tmp102 *tmp102 = i2c_get_clientdata(client);
  227. return regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
  228. TMP102_CONF_SD, TMP102_CONF_SD);
  229. }
  230. static int tmp102_resume(struct device *dev)
  231. {
  232. struct i2c_client *client = to_i2c_client(dev);
  233. struct tmp102 *tmp102 = i2c_get_clientdata(client);
  234. int err;
  235. err = regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
  236. TMP102_CONF_SD, 0);
  237. tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
  238. return err;
  239. }
  240. #endif /* CONFIG_PM */
  241. static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume);
  242. static const struct i2c_device_id tmp102_id[] = {
  243. { "tmp102", 0 },
  244. { }
  245. };
  246. MODULE_DEVICE_TABLE(i2c, tmp102_id);
  247. static const struct of_device_id __maybe_unused tmp102_of_match[] = {
  248. { .compatible = "ti,tmp102" },
  249. { },
  250. };
  251. MODULE_DEVICE_TABLE(of, tmp102_of_match);
  252. static struct i2c_driver tmp102_driver = {
  253. .driver.name = DRIVER_NAME,
  254. .driver.of_match_table = of_match_ptr(tmp102_of_match),
  255. .driver.pm = &tmp102_dev_pm_ops,
  256. .probe_new = tmp102_probe,
  257. .id_table = tmp102_id,
  258. };
  259. module_i2c_driver(tmp102_driver);
  260. MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
  261. MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
  262. MODULE_LICENSE("GPL");