tmp102.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* Texas Instruments TMP102 SMBus temperature sensor driver
  2. *
  3. * Copyright (C) 2010 Steven King <sfking@fdwdc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/i2c.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/err.h>
  23. #include <linux/mutex.h>
  24. #include <linux/device.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/regmap.h>
  27. #include <linux/thermal.h>
  28. #include <linux/of.h>
  29. #define DRIVER_NAME "tmp102"
  30. #define TMP102_TEMP_REG 0x00
  31. #define TMP102_CONF_REG 0x01
  32. /* note: these bit definitions are byte swapped */
  33. #define TMP102_CONF_SD 0x0100
  34. #define TMP102_CONF_TM 0x0200
  35. #define TMP102_CONF_POL 0x0400
  36. #define TMP102_CONF_F0 0x0800
  37. #define TMP102_CONF_F1 0x1000
  38. #define TMP102_CONF_R0 0x2000
  39. #define TMP102_CONF_R1 0x4000
  40. #define TMP102_CONF_OS 0x8000
  41. #define TMP102_CONF_EM 0x0010
  42. #define TMP102_CONF_AL 0x0020
  43. #define TMP102_CONF_CR0 0x0040
  44. #define TMP102_CONF_CR1 0x0080
  45. #define TMP102_TLOW_REG 0x02
  46. #define TMP102_THIGH_REG 0x03
  47. #define TMP102_CONFREG_MASK (TMP102_CONF_SD | TMP102_CONF_TM | \
  48. TMP102_CONF_POL | TMP102_CONF_F0 | \
  49. TMP102_CONF_F1 | TMP102_CONF_OS | \
  50. TMP102_CONF_EM | TMP102_CONF_AL | \
  51. TMP102_CONF_CR0 | TMP102_CONF_CR1)
  52. #define TMP102_CONFIG_CLEAR (TMP102_CONF_SD | TMP102_CONF_OS | \
  53. TMP102_CONF_CR0)
  54. #define TMP102_CONFIG_SET (TMP102_CONF_TM | TMP102_CONF_EM | \
  55. TMP102_CONF_CR1)
  56. #define CONVERSION_TIME_MS 35 /* in milli-seconds */
  57. struct tmp102 {
  58. struct regmap *regmap;
  59. u16 config_orig;
  60. unsigned long ready_time;
  61. };
  62. /* convert left adjusted 13-bit TMP102 register value to milliCelsius */
  63. static inline int tmp102_reg_to_mC(s16 val)
  64. {
  65. return ((val & ~0x01) * 1000) / 128;
  66. }
  67. /* convert milliCelsius to left adjusted 13-bit TMP102 register value */
  68. static inline u16 tmp102_mC_to_reg(int val)
  69. {
  70. return (val * 128) / 1000;
  71. }
  72. static int tmp102_read_temp(void *dev, int *temp)
  73. {
  74. struct tmp102 *tmp102 = dev_get_drvdata(dev);
  75. unsigned int reg;
  76. int ret;
  77. if (time_before(jiffies, tmp102->ready_time)) {
  78. dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__);
  79. return -EAGAIN;
  80. }
  81. ret = regmap_read(tmp102->regmap, TMP102_TEMP_REG, &reg);
  82. if (ret < 0)
  83. return ret;
  84. *temp = tmp102_reg_to_mC(reg);
  85. return 0;
  86. }
  87. static ssize_t tmp102_show_temp(struct device *dev,
  88. struct device_attribute *attr,
  89. char *buf)
  90. {
  91. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  92. struct tmp102 *tmp102 = dev_get_drvdata(dev);
  93. int regaddr = sda->index;
  94. unsigned int reg;
  95. int err;
  96. if (regaddr == TMP102_TEMP_REG &&
  97. time_before(jiffies, tmp102->ready_time))
  98. return -EAGAIN;
  99. err = regmap_read(tmp102->regmap, regaddr, &reg);
  100. if (err < 0)
  101. return err;
  102. return sprintf(buf, "%d\n", tmp102_reg_to_mC(reg));
  103. }
  104. static ssize_t tmp102_set_temp(struct device *dev,
  105. struct device_attribute *attr,
  106. const char *buf, size_t count)
  107. {
  108. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  109. struct tmp102 *tmp102 = dev_get_drvdata(dev);
  110. int reg = sda->index;
  111. long val;
  112. int err;
  113. if (kstrtol(buf, 10, &val) < 0)
  114. return -EINVAL;
  115. val = clamp_val(val, -256000, 255000);
  116. err = regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(val));
  117. return err ? : count;
  118. }
  119. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL,
  120. TMP102_TEMP_REG);
  121. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp,
  122. tmp102_set_temp, TMP102_TLOW_REG);
  123. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp,
  124. tmp102_set_temp, TMP102_THIGH_REG);
  125. static struct attribute *tmp102_attrs[] = {
  126. &sensor_dev_attr_temp1_input.dev_attr.attr,
  127. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  128. &sensor_dev_attr_temp1_max.dev_attr.attr,
  129. NULL
  130. };
  131. ATTRIBUTE_GROUPS(tmp102);
  132. static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops = {
  133. .get_temp = tmp102_read_temp,
  134. };
  135. static void tmp102_restore_config(void *data)
  136. {
  137. struct tmp102 *tmp102 = data;
  138. regmap_write(tmp102->regmap, TMP102_CONF_REG, tmp102->config_orig);
  139. }
  140. static bool tmp102_is_writeable_reg(struct device *dev, unsigned int reg)
  141. {
  142. return reg != TMP102_TEMP_REG;
  143. }
  144. static bool tmp102_is_volatile_reg(struct device *dev, unsigned int reg)
  145. {
  146. return reg == TMP102_TEMP_REG;
  147. }
  148. static const struct regmap_config tmp102_regmap_config = {
  149. .reg_bits = 8,
  150. .val_bits = 16,
  151. .max_register = TMP102_THIGH_REG,
  152. .writeable_reg = tmp102_is_writeable_reg,
  153. .volatile_reg = tmp102_is_volatile_reg,
  154. .val_format_endian = REGMAP_ENDIAN_BIG,
  155. .cache_type = REGCACHE_RBTREE,
  156. .use_single_rw = true,
  157. };
  158. static int tmp102_probe(struct i2c_client *client,
  159. const struct i2c_device_id *id)
  160. {
  161. struct device *dev = &client->dev;
  162. struct device *hwmon_dev;
  163. struct tmp102 *tmp102;
  164. unsigned int regval;
  165. int err;
  166. if (!i2c_check_functionality(client->adapter,
  167. I2C_FUNC_SMBUS_WORD_DATA)) {
  168. dev_err(dev,
  169. "adapter doesn't support SMBus word transactions\n");
  170. return -ENODEV;
  171. }
  172. tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL);
  173. if (!tmp102)
  174. return -ENOMEM;
  175. i2c_set_clientdata(client, tmp102);
  176. tmp102->regmap = devm_regmap_init_i2c(client, &tmp102_regmap_config);
  177. if (IS_ERR(tmp102->regmap))
  178. return PTR_ERR(tmp102->regmap);
  179. err = regmap_read(tmp102->regmap, TMP102_CONF_REG, &regval);
  180. if (err < 0) {
  181. dev_err(dev, "error reading config register\n");
  182. return err;
  183. }
  184. if ((regval & ~TMP102_CONFREG_MASK) !=
  185. (TMP102_CONF_R0 | TMP102_CONF_R1)) {
  186. dev_err(dev, "unexpected config register value\n");
  187. return -ENODEV;
  188. }
  189. tmp102->config_orig = regval;
  190. devm_add_action(dev, tmp102_restore_config, tmp102);
  191. regval &= ~TMP102_CONFIG_CLEAR;
  192. regval |= TMP102_CONFIG_SET;
  193. err = regmap_write(tmp102->regmap, TMP102_CONF_REG, regval);
  194. if (err < 0) {
  195. dev_err(dev, "error writing config register\n");
  196. return err;
  197. }
  198. tmp102->ready_time = jiffies;
  199. if (tmp102->config_orig & TMP102_CONF_SD) {
  200. /*
  201. * Mark that we are not ready with data until the first
  202. * conversion is complete
  203. */
  204. tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS);
  205. }
  206. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  207. tmp102,
  208. tmp102_groups);
  209. if (IS_ERR(hwmon_dev)) {
  210. dev_dbg(dev, "unable to register hwmon device\n");
  211. return PTR_ERR(hwmon_dev);
  212. }
  213. devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev,
  214. &tmp102_of_thermal_ops);
  215. dev_info(dev, "initialized\n");
  216. return 0;
  217. }
  218. #ifdef CONFIG_PM_SLEEP
  219. static int tmp102_suspend(struct device *dev)
  220. {
  221. struct i2c_client *client = to_i2c_client(dev);
  222. struct tmp102 *tmp102 = i2c_get_clientdata(client);
  223. return regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
  224. TMP102_CONF_SD, TMP102_CONF_SD);
  225. }
  226. static int tmp102_resume(struct device *dev)
  227. {
  228. struct i2c_client *client = to_i2c_client(dev);
  229. struct tmp102 *tmp102 = i2c_get_clientdata(client);
  230. int err;
  231. err = regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
  232. TMP102_CONF_SD, 0);
  233. tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
  234. return err;
  235. }
  236. #endif /* CONFIG_PM */
  237. static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume);
  238. static const struct i2c_device_id tmp102_id[] = {
  239. { "tmp102", 0 },
  240. { }
  241. };
  242. MODULE_DEVICE_TABLE(i2c, tmp102_id);
  243. static struct i2c_driver tmp102_driver = {
  244. .driver.name = DRIVER_NAME,
  245. .driver.pm = &tmp102_dev_pm_ops,
  246. .probe = tmp102_probe,
  247. .id_table = tmp102_id,
  248. };
  249. module_i2c_driver(tmp102_driver);
  250. MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
  251. MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
  252. MODULE_LICENSE("GPL");