tmp102.c 8.1 KB

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