adm1029.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
  3. *
  4. * Copyright (C) 2006 Corentin LABBE <clabbe.montjoie@gmail.com>
  5. *
  6. * Based on LM83 Driver by Jean Delvare <jdelvare@suse.de>
  7. *
  8. * Give only processor, motherboard temperatures and fan tachs
  9. * Very rare chip please let me know if you use it
  10. *
  11. * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf
  12. *
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation version 2 of the License
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/i2c.h>
  32. #include <linux/hwmon-sysfs.h>
  33. #include <linux/hwmon.h>
  34. #include <linux/err.h>
  35. #include <linux/mutex.h>
  36. /*
  37. * Addresses to scan
  38. */
  39. static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
  40. 0x2e, 0x2f, I2C_CLIENT_END
  41. };
  42. /*
  43. * The ADM1029 registers
  44. * Manufacturer ID is 0x41 for Analog Devices
  45. */
  46. #define ADM1029_REG_MAN_ID 0x0D
  47. #define ADM1029_REG_CHIP_ID 0x0E
  48. #define ADM1029_REG_CONFIG 0x01
  49. #define ADM1029_REG_NB_FAN_SUPPORT 0x02
  50. #define ADM1029_REG_TEMP_DEVICES_INSTALLED 0x06
  51. #define ADM1029_REG_LOCAL_TEMP 0xA0
  52. #define ADM1029_REG_REMOTE1_TEMP 0xA1
  53. #define ADM1029_REG_REMOTE2_TEMP 0xA2
  54. #define ADM1029_REG_LOCAL_TEMP_HIGH 0x90
  55. #define ADM1029_REG_REMOTE1_TEMP_HIGH 0x91
  56. #define ADM1029_REG_REMOTE2_TEMP_HIGH 0x92
  57. #define ADM1029_REG_LOCAL_TEMP_LOW 0x98
  58. #define ADM1029_REG_REMOTE1_TEMP_LOW 0x99
  59. #define ADM1029_REG_REMOTE2_TEMP_LOW 0x9A
  60. #define ADM1029_REG_FAN1 0x70
  61. #define ADM1029_REG_FAN2 0x71
  62. #define ADM1029_REG_FAN1_MIN 0x78
  63. #define ADM1029_REG_FAN2_MIN 0x79
  64. #define ADM1029_REG_FAN1_CONFIG 0x68
  65. #define ADM1029_REG_FAN2_CONFIG 0x69
  66. #define TEMP_FROM_REG(val) ((val) * 1000)
  67. #define DIV_FROM_REG(val) (1 << (((val) >> 6) - 1))
  68. /* Registers to be checked by adm1029_update_device() */
  69. static const u8 ADM1029_REG_TEMP[] = {
  70. ADM1029_REG_LOCAL_TEMP,
  71. ADM1029_REG_REMOTE1_TEMP,
  72. ADM1029_REG_REMOTE2_TEMP,
  73. ADM1029_REG_LOCAL_TEMP_HIGH,
  74. ADM1029_REG_REMOTE1_TEMP_HIGH,
  75. ADM1029_REG_REMOTE2_TEMP_HIGH,
  76. ADM1029_REG_LOCAL_TEMP_LOW,
  77. ADM1029_REG_REMOTE1_TEMP_LOW,
  78. ADM1029_REG_REMOTE2_TEMP_LOW,
  79. };
  80. static const u8 ADM1029_REG_FAN[] = {
  81. ADM1029_REG_FAN1,
  82. ADM1029_REG_FAN2,
  83. ADM1029_REG_FAN1_MIN,
  84. ADM1029_REG_FAN2_MIN,
  85. };
  86. static const u8 ADM1029_REG_FAN_DIV[] = {
  87. ADM1029_REG_FAN1_CONFIG,
  88. ADM1029_REG_FAN2_CONFIG,
  89. };
  90. /*
  91. * Client data (each client gets its own)
  92. */
  93. struct adm1029_data {
  94. struct i2c_client *client;
  95. struct mutex update_lock;
  96. char valid; /* zero until following fields are valid */
  97. unsigned long last_updated; /* in jiffies */
  98. /* registers values, signed for temperature, unsigned for other stuff */
  99. s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)];
  100. u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)];
  101. u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)];
  102. };
  103. /*
  104. * function that update the status of the chips (temperature for example)
  105. */
  106. static struct adm1029_data *adm1029_update_device(struct device *dev)
  107. {
  108. struct adm1029_data *data = dev_get_drvdata(dev);
  109. struct i2c_client *client = data->client;
  110. mutex_lock(&data->update_lock);
  111. /*
  112. * Use the "cache" Luke, don't recheck values
  113. * if there are already checked not a long time later
  114. */
  115. if (time_after(jiffies, data->last_updated + HZ * 2)
  116. || !data->valid) {
  117. int nr;
  118. dev_dbg(&client->dev, "Updating adm1029 data\n");
  119. for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
  120. data->temp[nr] =
  121. i2c_smbus_read_byte_data(client,
  122. ADM1029_REG_TEMP[nr]);
  123. }
  124. for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
  125. data->fan[nr] =
  126. i2c_smbus_read_byte_data(client,
  127. ADM1029_REG_FAN[nr]);
  128. }
  129. for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
  130. data->fan_div[nr] =
  131. i2c_smbus_read_byte_data(client,
  132. ADM1029_REG_FAN_DIV[nr]);
  133. }
  134. data->last_updated = jiffies;
  135. data->valid = 1;
  136. }
  137. mutex_unlock(&data->update_lock);
  138. return data;
  139. }
  140. /*
  141. * Sysfs stuff
  142. */
  143. static ssize_t
  144. show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
  145. {
  146. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  147. struct adm1029_data *data = adm1029_update_device(dev);
  148. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
  149. }
  150. static ssize_t
  151. show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
  152. {
  153. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  154. struct adm1029_data *data = adm1029_update_device(dev);
  155. u16 val;
  156. if (data->fan[attr->index] == 0
  157. || (data->fan_div[attr->index] & 0xC0) == 0
  158. || data->fan[attr->index] == 255) {
  159. return sprintf(buf, "0\n");
  160. }
  161. val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
  162. / data->fan[attr->index];
  163. return sprintf(buf, "%d\n", val);
  164. }
  165. static ssize_t
  166. show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
  167. {
  168. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  169. struct adm1029_data *data = adm1029_update_device(dev);
  170. if ((data->fan_div[attr->index] & 0xC0) == 0)
  171. return sprintf(buf, "0\n");
  172. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
  173. }
  174. static ssize_t set_fan_div(struct device *dev,
  175. struct device_attribute *devattr, const char *buf, size_t count)
  176. {
  177. struct adm1029_data *data = dev_get_drvdata(dev);
  178. struct i2c_client *client = data->client;
  179. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  180. u8 reg;
  181. long val;
  182. int ret = kstrtol(buf, 10, &val);
  183. if (ret < 0)
  184. return ret;
  185. mutex_lock(&data->update_lock);
  186. /*Read actual config */
  187. reg = i2c_smbus_read_byte_data(client,
  188. ADM1029_REG_FAN_DIV[attr->index]);
  189. switch (val) {
  190. case 1:
  191. val = 1;
  192. break;
  193. case 2:
  194. val = 2;
  195. break;
  196. case 4:
  197. val = 3;
  198. break;
  199. default:
  200. mutex_unlock(&data->update_lock);
  201. dev_err(&client->dev,
  202. "fan_div value %ld not supported. Choose one of 1, 2 or 4!\n",
  203. val);
  204. return -EINVAL;
  205. }
  206. /* Update the value */
  207. reg = (reg & 0x3F) | (val << 6);
  208. /* Update the cache */
  209. data->fan_div[attr->index] = reg;
  210. /* Write value */
  211. i2c_smbus_write_byte_data(client,
  212. ADM1029_REG_FAN_DIV[attr->index], reg);
  213. mutex_unlock(&data->update_lock);
  214. return count;
  215. }
  216. /*
  217. * Access rights on sysfs. S_IRUGO: Is Readable by User, Group and Others
  218. * S_IWUSR: Is Writable by User.
  219. */
  220. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  221. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  222. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  223. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 3);
  224. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp, NULL, 4);
  225. static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp, NULL, 5);
  226. static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, NULL, 6);
  227. static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp, NULL, 7);
  228. static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp, NULL, 8);
  229. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  230. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  231. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan, NULL, 2);
  232. static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan, NULL, 3);
  233. static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
  234. show_fan_div, set_fan_div, 0);
  235. static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
  236. show_fan_div, set_fan_div, 1);
  237. static struct attribute *adm1029_attrs[] = {
  238. &sensor_dev_attr_temp1_input.dev_attr.attr,
  239. &sensor_dev_attr_temp1_min.dev_attr.attr,
  240. &sensor_dev_attr_temp1_max.dev_attr.attr,
  241. &sensor_dev_attr_temp2_input.dev_attr.attr,
  242. &sensor_dev_attr_temp2_min.dev_attr.attr,
  243. &sensor_dev_attr_temp2_max.dev_attr.attr,
  244. &sensor_dev_attr_temp3_input.dev_attr.attr,
  245. &sensor_dev_attr_temp3_min.dev_attr.attr,
  246. &sensor_dev_attr_temp3_max.dev_attr.attr,
  247. &sensor_dev_attr_fan1_input.dev_attr.attr,
  248. &sensor_dev_attr_fan2_input.dev_attr.attr,
  249. &sensor_dev_attr_fan1_min.dev_attr.attr,
  250. &sensor_dev_attr_fan2_min.dev_attr.attr,
  251. &sensor_dev_attr_fan1_div.dev_attr.attr,
  252. &sensor_dev_attr_fan2_div.dev_attr.attr,
  253. NULL
  254. };
  255. ATTRIBUTE_GROUPS(adm1029);
  256. /*
  257. * Real code
  258. */
  259. /* Return 0 if detection is successful, -ENODEV otherwise */
  260. static int adm1029_detect(struct i2c_client *client,
  261. struct i2c_board_info *info)
  262. {
  263. struct i2c_adapter *adapter = client->adapter;
  264. u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
  265. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  266. return -ENODEV;
  267. /*
  268. * ADM1029 doesn't have CHIP ID, check just MAN ID
  269. * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
  270. * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
  271. * documented
  272. */
  273. man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
  274. chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
  275. temp_devices_installed = i2c_smbus_read_byte_data(client,
  276. ADM1029_REG_TEMP_DEVICES_INSTALLED);
  277. nb_fan_support = i2c_smbus_read_byte_data(client,
  278. ADM1029_REG_NB_FAN_SUPPORT);
  279. /* 0x41 is Analog Devices */
  280. if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01
  281. || nb_fan_support != 0x03)
  282. return -ENODEV;
  283. if ((chip_id & 0xF0) != 0x00) {
  284. /*
  285. * There are no "official" CHIP ID, so actually
  286. * we use Major/Minor revision for that
  287. */
  288. pr_info("Unknown major revision %x, please let us know\n",
  289. chip_id);
  290. return -ENODEV;
  291. }
  292. strlcpy(info->type, "adm1029", I2C_NAME_SIZE);
  293. return 0;
  294. }
  295. static int adm1029_init_client(struct i2c_client *client)
  296. {
  297. u8 config;
  298. config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
  299. if ((config & 0x10) == 0) {
  300. i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
  301. config | 0x10);
  302. }
  303. /* recheck config */
  304. config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
  305. if ((config & 0x10) == 0) {
  306. dev_err(&client->dev, "Initialization failed!\n");
  307. return 0;
  308. }
  309. return 1;
  310. }
  311. static int adm1029_probe(struct i2c_client *client,
  312. const struct i2c_device_id *id)
  313. {
  314. struct device *dev = &client->dev;
  315. struct adm1029_data *data;
  316. struct device *hwmon_dev;
  317. data = devm_kzalloc(dev, sizeof(struct adm1029_data), GFP_KERNEL);
  318. if (!data)
  319. return -ENOMEM;
  320. data->client = client;
  321. mutex_init(&data->update_lock);
  322. /*
  323. * Initialize the ADM1029 chip
  324. * Check config register
  325. */
  326. if (adm1029_init_client(client) == 0)
  327. return -ENODEV;
  328. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  329. data,
  330. adm1029_groups);
  331. return PTR_ERR_OR_ZERO(hwmon_dev);
  332. }
  333. static const struct i2c_device_id adm1029_id[] = {
  334. { "adm1029", 0 },
  335. { }
  336. };
  337. MODULE_DEVICE_TABLE(i2c, adm1029_id);
  338. static struct i2c_driver adm1029_driver = {
  339. .class = I2C_CLASS_HWMON,
  340. .driver = {
  341. .name = "adm1029",
  342. },
  343. .probe = adm1029_probe,
  344. .id_table = adm1029_id,
  345. .detect = adm1029_detect,
  346. .address_list = normal_i2c,
  347. };
  348. module_i2c_driver(adm1029_driver);
  349. MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
  350. MODULE_DESCRIPTION("adm1029 driver");
  351. MODULE_LICENSE("GPL v2");