tmp421.c 8.4 KB

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