tmp421.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. u32 temp_config[5];
  66. struct hwmon_channel_info temp_info;
  67. const struct hwmon_channel_info *info[2];
  68. struct hwmon_chip_info chip;
  69. char valid;
  70. unsigned long last_updated;
  71. int channels;
  72. u8 config;
  73. s16 temp[4];
  74. };
  75. static int temp_from_s16(s16 reg)
  76. {
  77. /* Mask out status bits */
  78. int temp = reg & ~0xf;
  79. return (temp * 1000 + 128) / 256;
  80. }
  81. static int temp_from_u16(u16 reg)
  82. {
  83. /* Mask out status bits */
  84. int temp = reg & ~0xf;
  85. /* Add offset for extended temperature range. */
  86. temp -= 64 * 256;
  87. return (temp * 1000 + 128) / 256;
  88. }
  89. static struct tmp421_data *tmp421_update_device(struct device *dev)
  90. {
  91. struct tmp421_data *data = dev_get_drvdata(dev);
  92. struct i2c_client *client = data->client;
  93. int i;
  94. mutex_lock(&data->update_lock);
  95. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  96. data->config = i2c_smbus_read_byte_data(client,
  97. TMP421_CONFIG_REG_1);
  98. for (i = 0; i < data->channels; i++) {
  99. data->temp[i] = i2c_smbus_read_byte_data(client,
  100. TMP421_TEMP_MSB[i]) << 8;
  101. data->temp[i] |= i2c_smbus_read_byte_data(client,
  102. TMP421_TEMP_LSB[i]);
  103. }
  104. data->last_updated = jiffies;
  105. data->valid = 1;
  106. }
  107. mutex_unlock(&data->update_lock);
  108. return data;
  109. }
  110. static int tmp421_read(struct device *dev, enum hwmon_sensor_types type,
  111. u32 attr, int channel, long *val)
  112. {
  113. struct tmp421_data *tmp421 = tmp421_update_device(dev);
  114. switch (attr) {
  115. case hwmon_temp_input:
  116. if (tmp421->config & TMP421_CONFIG_RANGE)
  117. *val = temp_from_u16(tmp421->temp[channel]);
  118. else
  119. *val = temp_from_s16(tmp421->temp[channel]);
  120. return 0;
  121. case hwmon_temp_fault:
  122. /*
  123. * The OPEN bit signals a fault. This is bit 0 of the temperature
  124. * register (low byte).
  125. */
  126. *val = tmp421->temp[channel] & 0x01;
  127. return 0;
  128. default:
  129. return -EOPNOTSUPP;
  130. }
  131. }
  132. static umode_t tmp421_is_visible(const void *data, enum hwmon_sensor_types type,
  133. u32 attr, int channel)
  134. {
  135. switch (attr) {
  136. case hwmon_temp_fault:
  137. if (channel == 0)
  138. return 0;
  139. return S_IRUGO;
  140. case hwmon_temp_input:
  141. return S_IRUGO;
  142. default:
  143. return 0;
  144. }
  145. }
  146. static int tmp421_init_client(struct i2c_client *client)
  147. {
  148. int config, config_orig;
  149. /* Set the conversion rate to 2 Hz */
  150. i2c_smbus_write_byte_data(client, TMP421_CONVERSION_RATE_REG, 0x05);
  151. /* Start conversions (disable shutdown if necessary) */
  152. config = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1);
  153. if (config < 0) {
  154. dev_err(&client->dev,
  155. "Could not read configuration register (%d)\n", config);
  156. return config;
  157. }
  158. config_orig = config;
  159. config &= ~TMP421_CONFIG_SHUTDOWN;
  160. if (config != config_orig) {
  161. dev_info(&client->dev, "Enable monitoring chip\n");
  162. i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_1, config);
  163. }
  164. return 0;
  165. }
  166. static int tmp421_detect(struct i2c_client *client,
  167. struct i2c_board_info *info)
  168. {
  169. enum chips kind;
  170. struct i2c_adapter *adapter = client->adapter;
  171. const char * const names[] = { "TMP421", "TMP422", "TMP423",
  172. "TMP441", "TMP442" };
  173. int addr = client->addr;
  174. u8 reg;
  175. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  176. return -ENODEV;
  177. reg = i2c_smbus_read_byte_data(client, TMP421_MANUFACTURER_ID_REG);
  178. if (reg != TMP421_MANUFACTURER_ID)
  179. return -ENODEV;
  180. reg = i2c_smbus_read_byte_data(client, TMP421_CONVERSION_RATE_REG);
  181. if (reg & 0xf8)
  182. return -ENODEV;
  183. reg = i2c_smbus_read_byte_data(client, TMP421_STATUS_REG);
  184. if (reg & 0x7f)
  185. return -ENODEV;
  186. reg = i2c_smbus_read_byte_data(client, TMP421_DEVICE_ID_REG);
  187. switch (reg) {
  188. case TMP421_DEVICE_ID:
  189. kind = tmp421;
  190. break;
  191. case TMP422_DEVICE_ID:
  192. if (addr == 0x2a)
  193. return -ENODEV;
  194. kind = tmp422;
  195. break;
  196. case TMP423_DEVICE_ID:
  197. if (addr != 0x4c && addr != 0x4d)
  198. return -ENODEV;
  199. kind = tmp423;
  200. break;
  201. case TMP441_DEVICE_ID:
  202. kind = tmp441;
  203. break;
  204. case TMP442_DEVICE_ID:
  205. if (addr != 0x4c && addr != 0x4d)
  206. return -ENODEV;
  207. kind = tmp442;
  208. break;
  209. default:
  210. return -ENODEV;
  211. }
  212. strlcpy(info->type, tmp421_id[kind].name, I2C_NAME_SIZE);
  213. dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x\n",
  214. names[kind], client->addr);
  215. return 0;
  216. }
  217. static const struct hwmon_ops tmp421_ops = {
  218. .is_visible = tmp421_is_visible,
  219. .read = tmp421_read,
  220. };
  221. static int tmp421_probe(struct i2c_client *client,
  222. const struct i2c_device_id *id)
  223. {
  224. struct device *dev = &client->dev;
  225. struct device *hwmon_dev;
  226. struct tmp421_data *data;
  227. int i, err;
  228. data = devm_kzalloc(dev, sizeof(struct tmp421_data), GFP_KERNEL);
  229. if (!data)
  230. return -ENOMEM;
  231. mutex_init(&data->update_lock);
  232. data->channels = id->driver_data;
  233. data->client = client;
  234. err = tmp421_init_client(client);
  235. if (err)
  236. return err;
  237. for (i = 0; i < data->channels; i++)
  238. data->temp_config[i] = HWMON_T_INPUT | HWMON_T_FAULT;
  239. data->chip.ops = &tmp421_ops;
  240. data->chip.info = data->info;
  241. data->info[0] = &data->temp_info;
  242. data->temp_info.type = hwmon_temp;
  243. data->temp_info.config = data->temp_config;
  244. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  245. data,
  246. &data->chip,
  247. NULL);
  248. return PTR_ERR_OR_ZERO(hwmon_dev);
  249. }
  250. static struct i2c_driver tmp421_driver = {
  251. .class = I2C_CLASS_HWMON,
  252. .driver = {
  253. .name = "tmp421",
  254. },
  255. .probe = tmp421_probe,
  256. .id_table = tmp421_id,
  257. .detect = tmp421_detect,
  258. .address_list = normal_i2c,
  259. };
  260. module_i2c_driver(tmp421_driver);
  261. MODULE_AUTHOR("Andre Prendel <andre.prendel@gmx.de>");
  262. MODULE_DESCRIPTION("Texas Instruments TMP421/422/423/441/442 temperature sensor driver");
  263. MODULE_LICENSE("GPL");