tmp421.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* tmp421.c
  2. *
  3. * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
  4. * Preliminary support by:
  5. * Melvin Rook, Raymond Ng
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /*
  22. * Driver for the Texas Instruments TMP421 SMBus temperature sensor IC.
  23. * Supported models: TMP421, TMP422, TMP423
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/i2c.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/err.h>
  33. #include <linux/mutex.h>
  34. #include <linux/sysfs.h>
  35. /* Addresses to scan */
  36. static unsigned short normal_i2c[] = { 0x2a, 0x4c, 0x4d, 0x4e, 0x4f,
  37. I2C_CLIENT_END };
  38. /* Insmod parameters */
  39. I2C_CLIENT_INSMOD_3(tmp421, tmp422, tmp423);
  40. /* The TMP421 registers */
  41. #define TMP421_CONFIG_REG_1 0x09
  42. #define TMP421_CONVERSION_RATE_REG 0x0B
  43. #define TMP421_MANUFACTURER_ID_REG 0xFE
  44. #define TMP421_DEVICE_ID_REG 0xFF
  45. static const u8 TMP421_TEMP_MSB[4] = { 0x00, 0x01, 0x02, 0x03 };
  46. static const u8 TMP421_TEMP_LSB[4] = { 0x10, 0x11, 0x12, 0x13 };
  47. /* Flags */
  48. #define TMP421_CONFIG_SHUTDOWN 0x40
  49. #define TMP421_CONFIG_RANGE 0x04
  50. /* Manufacturer / Device ID's */
  51. #define TMP421_MANUFACTURER_ID 0x55
  52. #define TMP421_DEVICE_ID 0x21
  53. #define TMP422_DEVICE_ID 0x22
  54. #define TMP423_DEVICE_ID 0x23
  55. static const struct i2c_device_id tmp421_id[] = {
  56. { "tmp421", tmp421 },
  57. { "tmp422", tmp422 },
  58. { "tmp423", tmp423 },
  59. { }
  60. };
  61. MODULE_DEVICE_TABLE(i2c, tmp421_id);
  62. struct tmp421_data {
  63. struct device *hwmon_dev;
  64. struct mutex update_lock;
  65. char valid;
  66. unsigned long last_updated;
  67. int kind;
  68. u8 config;
  69. s16 temp[4];
  70. };
  71. static int temp_from_s16(s16 reg)
  72. {
  73. int temp = reg;
  74. return (temp * 1000 + 128) / 256;
  75. }
  76. static int temp_from_u16(u16 reg)
  77. {
  78. int temp = reg;
  79. /* Add offset for extended temperature range. */
  80. temp -= 64 * 256;
  81. return (temp * 1000 + 128) / 256;
  82. }
  83. static struct tmp421_data *tmp421_update_device(struct device *dev)
  84. {
  85. struct i2c_client *client = to_i2c_client(dev);
  86. struct tmp421_data *data = i2c_get_clientdata(client);
  87. int i;
  88. mutex_lock(&data->update_lock);
  89. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  90. data->config = i2c_smbus_read_byte_data(client,
  91. TMP421_CONFIG_REG_1);
  92. for (i = 0; i <= data->kind; i++) {
  93. data->temp[i] = i2c_smbus_read_byte_data(client,
  94. TMP421_TEMP_MSB[i]) << 8;
  95. data->temp[i] |= i2c_smbus_read_byte_data(client,
  96. TMP421_TEMP_LSB[i]);
  97. }
  98. data->last_updated = jiffies;
  99. data->valid = 1;
  100. }
  101. mutex_unlock(&data->update_lock);
  102. return data;
  103. }
  104. static ssize_t show_temp_value(struct device *dev,
  105. struct device_attribute *devattr, char *buf)
  106. {
  107. int index = to_sensor_dev_attr(devattr)->index;
  108. struct tmp421_data *data = tmp421_update_device(dev);
  109. int temp;
  110. mutex_lock(&data->update_lock);
  111. if (data->config & TMP421_CONFIG_RANGE)
  112. temp = temp_from_u16(data->temp[index]);
  113. else
  114. temp = temp_from_s16(data->temp[index]);
  115. mutex_unlock(&data->update_lock);
  116. return sprintf(buf, "%d\n", temp);
  117. }
  118. static ssize_t show_fault(struct device *dev,
  119. struct device_attribute *devattr, char *buf)
  120. {
  121. int index = to_sensor_dev_attr(devattr)->index;
  122. struct tmp421_data *data = tmp421_update_device(dev);
  123. /*
  124. * The OPEN bit signals a fault. This is bit 0 of the temperature
  125. * register (low byte).
  126. */
  127. if (data->temp[index] & 0x01)
  128. return sprintf(buf, "1\n");
  129. else
  130. return sprintf(buf, "0\n");
  131. }
  132. static mode_t tmp421_is_visible(struct kobject *kobj, struct attribute *a,
  133. int n)
  134. {
  135. struct device *dev = container_of(kobj, struct device, kobj);
  136. struct tmp421_data *data = dev_get_drvdata(dev);
  137. struct device_attribute *devattr;
  138. unsigned int index;
  139. devattr = container_of(a, struct device_attribute, attr);
  140. index = to_sensor_dev_attr(devattr)->index;
  141. if (data->kind > index)
  142. return a->mode;
  143. return 0;
  144. }
  145. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0);
  146. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1);
  147. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1);
  148. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_value, NULL, 2);
  149. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2);
  150. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_value, NULL, 3);
  151. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3);
  152. static struct attribute *tmp421_attr[] = {
  153. &sensor_dev_attr_temp1_input.dev_attr.attr,
  154. &sensor_dev_attr_temp2_input.dev_attr.attr,
  155. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  156. &sensor_dev_attr_temp3_input.dev_attr.attr,
  157. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  158. &sensor_dev_attr_temp4_input.dev_attr.attr,
  159. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  160. NULL
  161. };
  162. static const struct attribute_group tmp421_group = {
  163. .attrs = tmp421_attr,
  164. .is_visible = tmp421_is_visible,
  165. };
  166. static int tmp421_init_client(struct i2c_client *client)
  167. {
  168. int config, config_orig;
  169. /* Set the conversion rate to 2 Hz */
  170. i2c_smbus_write_byte_data(client, TMP421_CONVERSION_RATE_REG, 0x05);
  171. /* Start conversions (disable shutdown if necessary) */
  172. config = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1);
  173. if (config < 0) {
  174. dev_err(&client->dev, "Could not read configuration"
  175. " register (%d)\n", config);
  176. return -ENODEV;
  177. }
  178. config_orig = config;
  179. config &= ~TMP421_CONFIG_SHUTDOWN;
  180. if (config != config_orig) {
  181. dev_info(&client->dev, "Enable monitoring chip\n");
  182. i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_1, config);
  183. }
  184. return 0;
  185. }
  186. static int tmp421_detect(struct i2c_client *client, int _kind,
  187. struct i2c_board_info *info)
  188. {
  189. enum chips kind;
  190. struct i2c_adapter *adapter = client->adapter;
  191. const char *names[] = { "TMP421", "TMP422", "TMP423" };
  192. u8 reg;
  193. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  194. return -ENODEV;
  195. reg = i2c_smbus_read_byte_data(client, TMP421_MANUFACTURER_ID_REG);
  196. if (reg != TMP421_MANUFACTURER_ID)
  197. return -ENODEV;
  198. reg = i2c_smbus_read_byte_data(client, TMP421_DEVICE_ID_REG);
  199. switch (reg) {
  200. case TMP421_DEVICE_ID:
  201. kind = tmp421;
  202. break;
  203. case TMP422_DEVICE_ID:
  204. kind = tmp422;
  205. break;
  206. case TMP423_DEVICE_ID:
  207. kind = tmp423;
  208. break;
  209. default:
  210. return -ENODEV;
  211. }
  212. strlcpy(info->type, tmp421_id[kind - 1].name, I2C_NAME_SIZE);
  213. dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x\n",
  214. names[kind - 1], client->addr);
  215. return 0;
  216. }
  217. static int tmp421_probe(struct i2c_client *client,
  218. const struct i2c_device_id *id)
  219. {
  220. struct tmp421_data *data;
  221. int err;
  222. data = kzalloc(sizeof(struct tmp421_data), GFP_KERNEL);
  223. if (!data)
  224. return -ENOMEM;
  225. i2c_set_clientdata(client, data);
  226. mutex_init(&data->update_lock);
  227. data->kind = id->driver_data;
  228. err = tmp421_init_client(client);
  229. if (err)
  230. goto exit_free;
  231. err = sysfs_create_group(&client->dev.kobj, &tmp421_group);
  232. if (err)
  233. goto exit_free;
  234. data->hwmon_dev = hwmon_device_register(&client->dev);
  235. if (IS_ERR(data->hwmon_dev)) {
  236. err = PTR_ERR(data->hwmon_dev);
  237. data->hwmon_dev = NULL;
  238. goto exit_remove;
  239. }
  240. return 0;
  241. exit_remove:
  242. sysfs_remove_group(&client->dev.kobj, &tmp421_group);
  243. exit_free:
  244. i2c_set_clientdata(client, NULL);
  245. kfree(data);
  246. return err;
  247. }
  248. static int tmp421_remove(struct i2c_client *client)
  249. {
  250. struct tmp421_data *data = i2c_get_clientdata(client);
  251. hwmon_device_unregister(data->hwmon_dev);
  252. sysfs_remove_group(&client->dev.kobj, &tmp421_group);
  253. i2c_set_clientdata(client, NULL);
  254. kfree(data);
  255. return 0;
  256. }
  257. static struct i2c_driver tmp421_driver = {
  258. .class = I2C_CLASS_HWMON,
  259. .driver = {
  260. .name = "tmp421",
  261. },
  262. .probe = tmp421_probe,
  263. .remove = tmp421_remove,
  264. .id_table = tmp421_id,
  265. .detect = tmp421_detect,
  266. .address_data = &addr_data,
  267. };
  268. static int __init tmp421_init(void)
  269. {
  270. return i2c_add_driver(&tmp421_driver);
  271. }
  272. static void __exit tmp421_exit(void)
  273. {
  274. i2c_del_driver(&tmp421_driver);
  275. }
  276. MODULE_AUTHOR("Andre Prendel <andre.prendel@gmx.de>");
  277. MODULE_DESCRIPTION("Texas Instruments TMP421/422/423 temperature sensor"
  278. " driver");
  279. MODULE_LICENSE("GPL");
  280. module_init(tmp421_init);
  281. module_exit(tmp421_exit);