tmp103.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Texas Instruments TMP103 SMBus temperature sensor driver
  3. * Copyright (C) 2014 Heiko Schocher <hs@denx.de>
  4. *
  5. * Based on:
  6. * Texas Instruments TMP102 SMBus temperature sensor driver
  7. *
  8. * Copyright (C) 2010 Steven King <sfking@fdwdc.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/i2c.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/hwmon-sysfs.h>
  27. #include <linux/err.h>
  28. #include <linux/mutex.h>
  29. #include <linux/device.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/regmap.h>
  32. #define TMP103_TEMP_REG 0x00
  33. #define TMP103_CONF_REG 0x01
  34. #define TMP103_TLOW_REG 0x02
  35. #define TMP103_THIGH_REG 0x03
  36. #define TMP103_CONF_M0 0x01
  37. #define TMP103_CONF_M1 0x02
  38. #define TMP103_CONF_LC 0x04
  39. #define TMP103_CONF_FL 0x08
  40. #define TMP103_CONF_FH 0x10
  41. #define TMP103_CONF_CR0 0x20
  42. #define TMP103_CONF_CR1 0x40
  43. #define TMP103_CONF_ID 0x80
  44. #define TMP103_CONF_SD (TMP103_CONF_M1)
  45. #define TMP103_CONF_SD_MASK (TMP103_CONF_M0 | TMP103_CONF_M1)
  46. #define TMP103_CONFIG (TMP103_CONF_CR1 | TMP103_CONF_M1)
  47. #define TMP103_CONFIG_MASK (TMP103_CONF_CR0 | TMP103_CONF_CR1 | \
  48. TMP103_CONF_M0 | TMP103_CONF_M1)
  49. static inline int tmp103_reg_to_mc(s8 val)
  50. {
  51. return val * 1000;
  52. }
  53. static inline u8 tmp103_mc_to_reg(int val)
  54. {
  55. return DIV_ROUND_CLOSEST(val, 1000);
  56. }
  57. static ssize_t tmp103_show_temp(struct device *dev,
  58. struct device_attribute *attr,
  59. char *buf)
  60. {
  61. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  62. struct regmap *regmap = dev_get_drvdata(dev);
  63. unsigned int regval;
  64. int ret;
  65. ret = regmap_read(regmap, sda->index, &regval);
  66. if (ret < 0)
  67. return ret;
  68. return sprintf(buf, "%d\n", tmp103_reg_to_mc(regval));
  69. }
  70. static ssize_t tmp103_set_temp(struct device *dev,
  71. struct device_attribute *attr,
  72. const char *buf, size_t count)
  73. {
  74. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  75. struct regmap *regmap = dev_get_drvdata(dev);
  76. long val;
  77. int ret;
  78. if (kstrtol(buf, 10, &val) < 0)
  79. return -EINVAL;
  80. val = clamp_val(val, -55000, 127000);
  81. ret = regmap_write(regmap, sda->index, tmp103_mc_to_reg(val));
  82. return ret ? ret : count;
  83. }
  84. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp103_show_temp, NULL ,
  85. TMP103_TEMP_REG);
  86. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, tmp103_show_temp,
  87. tmp103_set_temp, TMP103_TLOW_REG);
  88. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp103_show_temp,
  89. tmp103_set_temp, TMP103_THIGH_REG);
  90. static struct attribute *tmp103_attrs[] = {
  91. &sensor_dev_attr_temp1_input.dev_attr.attr,
  92. &sensor_dev_attr_temp1_min.dev_attr.attr,
  93. &sensor_dev_attr_temp1_max.dev_attr.attr,
  94. NULL
  95. };
  96. ATTRIBUTE_GROUPS(tmp103);
  97. static bool tmp103_regmap_is_volatile(struct device *dev, unsigned int reg)
  98. {
  99. return reg == TMP103_TEMP_REG;
  100. }
  101. static const struct regmap_config tmp103_regmap_config = {
  102. .reg_bits = 8,
  103. .val_bits = 8,
  104. .max_register = TMP103_THIGH_REG,
  105. .volatile_reg = tmp103_regmap_is_volatile,
  106. };
  107. static int tmp103_probe(struct i2c_client *client,
  108. const struct i2c_device_id *id)
  109. {
  110. struct device *dev = &client->dev;
  111. struct device *hwmon_dev;
  112. struct regmap *regmap;
  113. int ret;
  114. regmap = devm_regmap_init_i2c(client, &tmp103_regmap_config);
  115. if (IS_ERR(regmap)) {
  116. dev_err(dev, "failed to allocate register map\n");
  117. return PTR_ERR(regmap);
  118. }
  119. ret = regmap_update_bits(regmap, TMP103_CONF_REG, TMP103_CONFIG_MASK,
  120. TMP103_CONFIG);
  121. if (ret < 0) {
  122. dev_err(&client->dev, "error writing config register\n");
  123. return ret;
  124. }
  125. i2c_set_clientdata(client, regmap);
  126. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  127. regmap, tmp103_groups);
  128. return PTR_ERR_OR_ZERO(hwmon_dev);
  129. }
  130. #ifdef CONFIG_PM
  131. static int tmp103_suspend(struct device *dev)
  132. {
  133. struct regmap *regmap = dev_get_drvdata(dev);
  134. return regmap_update_bits(regmap, TMP103_CONF_REG,
  135. TMP103_CONF_SD_MASK, 0);
  136. }
  137. static int tmp103_resume(struct device *dev)
  138. {
  139. struct regmap *regmap = dev_get_drvdata(dev);
  140. return regmap_update_bits(regmap, TMP103_CONF_REG,
  141. TMP103_CONF_SD_MASK, TMP103_CONF_SD);
  142. }
  143. static const struct dev_pm_ops tmp103_dev_pm_ops = {
  144. .suspend = tmp103_suspend,
  145. .resume = tmp103_resume,
  146. };
  147. #define TMP103_DEV_PM_OPS (&tmp103_dev_pm_ops)
  148. #else
  149. #define TMP103_DEV_PM_OPS NULL
  150. #endif /* CONFIG_PM */
  151. static const struct i2c_device_id tmp103_id[] = {
  152. { "tmp103", 0 },
  153. { }
  154. };
  155. MODULE_DEVICE_TABLE(i2c, tmp103_id);
  156. static struct i2c_driver tmp103_driver = {
  157. .driver = {
  158. .name = "tmp103",
  159. .pm = TMP103_DEV_PM_OPS,
  160. },
  161. .probe = tmp103_probe,
  162. .id_table = tmp103_id,
  163. };
  164. module_i2c_driver(tmp103_driver);
  165. MODULE_AUTHOR("Heiko Schocher <hs@denx.de>");
  166. MODULE_DESCRIPTION("Texas Instruments TMP103 temperature sensor driver");
  167. MODULE_LICENSE("GPL");