tmp421.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 const unsigned short normal_i2c[] = { 0x2a, 0x4c, 0x4d, 0x4e, 0x4f,
  37. I2C_CLIENT_END };
  38. enum chips { tmp421, tmp422, tmp423 };
  39. /* The TMP421 registers */
  40. #define TMP421_CONFIG_REG_1 0x09
  41. #define TMP421_CONVERSION_RATE_REG 0x0B
  42. #define TMP421_MANUFACTURER_ID_REG 0xFE
  43. #define TMP421_DEVICE_ID_REG 0xFF
  44. static const u8 TMP421_TEMP_MSB[4] = { 0x00, 0x01, 0x02, 0x03 };
  45. static const u8 TMP421_TEMP_LSB[4] = { 0x10, 0x11, 0x12, 0x13 };
  46. /* Flags */
  47. #define TMP421_CONFIG_SHUTDOWN 0x40
  48. #define TMP421_CONFIG_RANGE 0x04
  49. /* Manufacturer / Device ID's */
  50. #define TMP421_MANUFACTURER_ID 0x55
  51. #define TMP421_DEVICE_ID 0x21
  52. #define TMP422_DEVICE_ID 0x22
  53. #define TMP423_DEVICE_ID 0x23
  54. static const struct i2c_device_id tmp421_id[] = {
  55. { "tmp421", 2 },
  56. { "tmp422", 3 },
  57. { "tmp423", 4 },
  58. { }
  59. };
  60. MODULE_DEVICE_TABLE(i2c, tmp421_id);
  61. struct tmp421_data {
  62. struct device *hwmon_dev;
  63. struct mutex update_lock;
  64. char valid;
  65. unsigned long last_updated;
  66. int channels;
  67. u8 config;
  68. s16 temp[4];
  69. };
  70. static int temp_from_s16(s16 reg)
  71. {
  72. /* Mask out status bits */
  73. int temp = reg & ~0xf;
  74. return (temp * 1000 + 128) / 256;
  75. }
  76. static int temp_from_u16(u16 reg)
  77. {
  78. /* Mask out status bits */
  79. int temp = reg & ~0xf;
  80. /* Add offset for extended temperature range. */
  81. temp -= 64 * 256;
  82. return (temp * 1000 + 128) / 256;
  83. }
  84. static struct tmp421_data *tmp421_update_device(struct device *dev)
  85. {
  86. struct i2c_client *client = to_i2c_client(dev);
  87. struct tmp421_data *data = i2c_get_clientdata(client);
  88. int i;
  89. mutex_lock(&data->update_lock);
  90. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  91. data->config = i2c_smbus_read_byte_data(client,
  92. TMP421_CONFIG_REG_1);
  93. for (i = 0; i < data->channels; i++) {
  94. data->temp[i] = i2c_smbus_read_byte_data(client,
  95. TMP421_TEMP_MSB[i]) << 8;
  96. data->temp[i] |= i2c_smbus_read_byte_data(client,
  97. TMP421_TEMP_LSB[i]);
  98. }
  99. data->last_updated = jiffies;
  100. data->valid = 1;
  101. }
  102. mutex_unlock(&data->update_lock);
  103. return data;
  104. }
  105. static ssize_t show_temp_value(struct device *dev,
  106. struct device_attribute *devattr, char *buf)
  107. {
  108. int index = to_sensor_dev_attr(devattr)->index;
  109. struct tmp421_data *data = tmp421_update_device(dev);
  110. int temp;
  111. mutex_lock(&data->update_lock);
  112. if (data->config & TMP421_CONFIG_RANGE)
  113. temp = temp_from_u16(data->temp[index]);
  114. else
  115. temp = temp_from_s16(data->temp[index]);
  116. mutex_unlock(&data->update_lock);
  117. return sprintf(buf, "%d\n", temp);
  118. }
  119. static ssize_t show_fault(struct device *dev,
  120. struct device_attribute *devattr, char *buf)
  121. {
  122. int index = to_sensor_dev_attr(devattr)->index;
  123. struct tmp421_data *data = tmp421_update_device(dev);
  124. /*
  125. * The OPEN bit signals a fault. This is bit 0 of the temperature
  126. * register (low byte).
  127. */
  128. if (data->temp[index] & 0x01)
  129. return sprintf(buf, "1\n");
  130. else
  131. return sprintf(buf, "0\n");
  132. }
  133. static umode_t tmp421_is_visible(struct kobject *kobj, struct attribute *a,
  134. int n)
  135. {
  136. struct device *dev = container_of(kobj, struct device, kobj);
  137. struct tmp421_data *data = dev_get_drvdata(dev);
  138. struct device_attribute *devattr;
  139. unsigned int index;
  140. devattr = container_of(a, struct device_attribute, attr);
  141. index = to_sensor_dev_attr(devattr)->index;
  142. if (index < data->channels)
  143. return a->mode;
  144. return 0;
  145. }
  146. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0);
  147. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1);
  148. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1);
  149. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_value, NULL, 2);
  150. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2);
  151. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_value, NULL, 3);
  152. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3);
  153. static struct attribute *tmp421_attr[] = {
  154. &sensor_dev_attr_temp1_input.dev_attr.attr,
  155. &sensor_dev_attr_temp2_input.dev_attr.attr,
  156. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  157. &sensor_dev_attr_temp3_input.dev_attr.attr,
  158. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  159. &sensor_dev_attr_temp4_input.dev_attr.attr,
  160. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  161. NULL
  162. };
  163. static const struct attribute_group tmp421_group = {
  164. .attrs = tmp421_attr,
  165. .is_visible = tmp421_is_visible,
  166. };
  167. static int tmp421_init_client(struct i2c_client *client)
  168. {
  169. int config, config_orig;
  170. /* Set the conversion rate to 2 Hz */
  171. i2c_smbus_write_byte_data(client, TMP421_CONVERSION_RATE_REG, 0x05);
  172. /* Start conversions (disable shutdown if necessary) */
  173. config = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1);
  174. if (config < 0) {
  175. dev_err(&client->dev, "Could not read configuration"
  176. " register (%d)\n", config);
  177. return -ENODEV;
  178. }
  179. config_orig = config;
  180. config &= ~TMP421_CONFIG_SHUTDOWN;
  181. if (config != config_orig) {
  182. dev_info(&client->dev, "Enable monitoring chip\n");
  183. i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_1, config);
  184. }
  185. return 0;
  186. }
  187. static int tmp421_detect(struct i2c_client *client,
  188. struct i2c_board_info *info)
  189. {
  190. enum chips kind;
  191. struct i2c_adapter *adapter = client->adapter;
  192. const char *names[] = { "TMP421", "TMP422", "TMP423" };
  193. u8 reg;
  194. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  195. return -ENODEV;
  196. reg = i2c_smbus_read_byte_data(client, TMP421_MANUFACTURER_ID_REG);
  197. if (reg != TMP421_MANUFACTURER_ID)
  198. return -ENODEV;
  199. reg = i2c_smbus_read_byte_data(client, TMP421_DEVICE_ID_REG);
  200. switch (reg) {
  201. case TMP421_DEVICE_ID:
  202. kind = tmp421;
  203. break;
  204. case TMP422_DEVICE_ID:
  205. kind = tmp422;
  206. break;
  207. case TMP423_DEVICE_ID:
  208. kind = tmp423;
  209. break;
  210. default:
  211. return -ENODEV;
  212. }
  213. strlcpy(info->type, tmp421_id[kind].name, I2C_NAME_SIZE);
  214. dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x\n",
  215. names[kind], client->addr);
  216. return 0;
  217. }
  218. static int tmp421_probe(struct i2c_client *client,
  219. const struct i2c_device_id *id)
  220. {
  221. struct tmp421_data *data;
  222. int err;
  223. data = kzalloc(sizeof(struct tmp421_data), GFP_KERNEL);
  224. if (!data)
  225. return -ENOMEM;
  226. i2c_set_clientdata(client, data);
  227. mutex_init(&data->update_lock);
  228. data->channels = id->driver_data;
  229. err = tmp421_init_client(client);
  230. if (err)
  231. goto exit_free;
  232. err = sysfs_create_group(&client->dev.kobj, &tmp421_group);
  233. if (err)
  234. goto exit_free;
  235. data->hwmon_dev = hwmon_device_register(&client->dev);
  236. if (IS_ERR(data->hwmon_dev)) {
  237. err = PTR_ERR(data->hwmon_dev);
  238. data->hwmon_dev = NULL;
  239. goto exit_remove;
  240. }
  241. return 0;
  242. exit_remove:
  243. sysfs_remove_group(&client->dev.kobj, &tmp421_group);
  244. exit_free:
  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. kfree(data);
  254. return 0;
  255. }
  256. static struct i2c_driver tmp421_driver = {
  257. .class = I2C_CLASS_HWMON,
  258. .driver = {
  259. .name = "tmp421",
  260. },
  261. .probe = tmp421_probe,
  262. .remove = tmp421_remove,
  263. .id_table = tmp421_id,
  264. .detect = tmp421_detect,
  265. .address_list = normal_i2c,
  266. };
  267. module_i2c_driver(tmp421_driver);
  268. MODULE_AUTHOR("Andre Prendel <andre.prendel@gmx.de>");
  269. MODULE_DESCRIPTION("Texas Instruments TMP421/422/423 temperature sensor"
  270. " driver");
  271. MODULE_LICENSE("GPL");