tmp421.c 8.1 KB

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