adm1029.c 10 KB

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