adm1029.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 <[email protected]>
  6. *
  7. * Based on LM83 Driver by Jean Delvare <[email protected]>
  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. if (data->fan[attr->index] == 0 ||
  143. (data->fan_div[attr->index] & 0xC0) == 0 ||
  144. data->fan[attr->index] == 255) {
  145. return sprintf(buf, "0\n");
  146. }
  147. val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
  148. / data->fan[attr->index];
  149. return sprintf(buf, "%d\n", val);
  150. }
  151. static ssize_t
  152. fan_div_show(struct device *dev, struct device_attribute *devattr, char *buf)
  153. {
  154. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  155. struct adm1029_data *data = adm1029_update_device(dev);
  156. if ((data->fan_div[attr->index] & 0xC0) == 0)
  157. return sprintf(buf, "0\n");
  158. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
  159. }
  160. static ssize_t fan_div_store(struct device *dev,
  161. struct device_attribute *devattr,
  162. const char *buf, size_t count)
  163. {
  164. struct adm1029_data *data = dev_get_drvdata(dev);
  165. struct i2c_client *client = data->client;
  166. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  167. u8 reg;
  168. long val;
  169. int ret = kstrtol(buf, 10, &val);
  170. if (ret < 0)
  171. return ret;
  172. mutex_lock(&data->update_lock);
  173. /*Read actual config */
  174. reg = i2c_smbus_read_byte_data(client,
  175. ADM1029_REG_FAN_DIV[attr->index]);
  176. switch (val) {
  177. case 1:
  178. val = 1;
  179. break;
  180. case 2:
  181. val = 2;
  182. break;
  183. case 4:
  184. val = 3;
  185. break;
  186. default:
  187. mutex_unlock(&data->update_lock);
  188. dev_err(&client->dev,
  189. "fan_div value %ld not supported. Choose one of 1, 2 or 4!\n",
  190. val);
  191. return -EINVAL;
  192. }
  193. /* Update the value */
  194. reg = (reg & 0x3F) | (val << 6);
  195. /* Update the cache */
  196. data->fan_div[attr->index] = reg;
  197. /* Write value */
  198. i2c_smbus_write_byte_data(client,
  199. ADM1029_REG_FAN_DIV[attr->index], reg);
  200. mutex_unlock(&data->update_lock);
  201. return count;
  202. }
  203. /* Access rights on sysfs. */
  204. static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
  205. static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
  206. static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
  207. static SENSOR_DEVICE_ATTR_RO(temp1_max, temp, 3);
  208. static SENSOR_DEVICE_ATTR_RO(temp2_max, temp, 4);
  209. static SENSOR_DEVICE_ATTR_RO(temp3_max, temp, 5);
  210. static SENSOR_DEVICE_ATTR_RO(temp1_min, temp, 6);
  211. static SENSOR_DEVICE_ATTR_RO(temp2_min, temp, 7);
  212. static SENSOR_DEVICE_ATTR_RO(temp3_min, temp, 8);
  213. static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
  214. static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
  215. static SENSOR_DEVICE_ATTR_RO(fan1_min, fan, 2);
  216. static SENSOR_DEVICE_ATTR_RO(fan2_min, fan, 3);
  217. static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
  218. static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
  219. static struct attribute *adm1029_attrs[] = {
  220. &sensor_dev_attr_temp1_input.dev_attr.attr,
  221. &sensor_dev_attr_temp1_min.dev_attr.attr,
  222. &sensor_dev_attr_temp1_max.dev_attr.attr,
  223. &sensor_dev_attr_temp2_input.dev_attr.attr,
  224. &sensor_dev_attr_temp2_min.dev_attr.attr,
  225. &sensor_dev_attr_temp2_max.dev_attr.attr,
  226. &sensor_dev_attr_temp3_input.dev_attr.attr,
  227. &sensor_dev_attr_temp3_min.dev_attr.attr,
  228. &sensor_dev_attr_temp3_max.dev_attr.attr,
  229. &sensor_dev_attr_fan1_input.dev_attr.attr,
  230. &sensor_dev_attr_fan2_input.dev_attr.attr,
  231. &sensor_dev_attr_fan1_min.dev_attr.attr,
  232. &sensor_dev_attr_fan2_min.dev_attr.attr,
  233. &sensor_dev_attr_fan1_div.dev_attr.attr,
  234. &sensor_dev_attr_fan2_div.dev_attr.attr,
  235. NULL
  236. };
  237. ATTRIBUTE_GROUPS(adm1029);
  238. /*
  239. * Real code
  240. */
  241. /* Return 0 if detection is successful, -ENODEV otherwise */
  242. static int adm1029_detect(struct i2c_client *client,
  243. struct i2c_board_info *info)
  244. {
  245. struct i2c_adapter *adapter = client->adapter;
  246. u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
  247. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  248. return -ENODEV;
  249. /*
  250. * ADM1029 doesn't have CHIP ID, check just MAN ID
  251. * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
  252. * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
  253. * documented
  254. */
  255. man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
  256. chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
  257. temp_devices_installed = i2c_smbus_read_byte_data(client,
  258. ADM1029_REG_TEMP_DEVICES_INSTALLED);
  259. nb_fan_support = i2c_smbus_read_byte_data(client,
  260. ADM1029_REG_NB_FAN_SUPPORT);
  261. /* 0x41 is Analog Devices */
  262. if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01 ||
  263. nb_fan_support != 0x03)
  264. return -ENODEV;
  265. if ((chip_id & 0xF0) != 0x00) {
  266. /*
  267. * There are no "official" CHIP ID, so actually
  268. * we use Major/Minor revision for that
  269. */
  270. pr_info("Unknown major revision %x, please let us know\n",
  271. chip_id);
  272. return -ENODEV;
  273. }
  274. strscpy(info->type, "adm1029", I2C_NAME_SIZE);
  275. return 0;
  276. }
  277. static int adm1029_init_client(struct i2c_client *client)
  278. {
  279. u8 config;
  280. config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
  281. if ((config & 0x10) == 0) {
  282. i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
  283. config | 0x10);
  284. }
  285. /* recheck config */
  286. config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
  287. if ((config & 0x10) == 0) {
  288. dev_err(&client->dev, "Initialization failed!\n");
  289. return 0;
  290. }
  291. return 1;
  292. }
  293. static int adm1029_probe(struct i2c_client *client)
  294. {
  295. struct device *dev = &client->dev;
  296. struct adm1029_data *data;
  297. struct device *hwmon_dev;
  298. data = devm_kzalloc(dev, sizeof(struct adm1029_data), GFP_KERNEL);
  299. if (!data)
  300. return -ENOMEM;
  301. data->client = client;
  302. mutex_init(&data->update_lock);
  303. /*
  304. * Initialize the ADM1029 chip
  305. * Check config register
  306. */
  307. if (adm1029_init_client(client) == 0)
  308. return -ENODEV;
  309. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  310. data,
  311. adm1029_groups);
  312. return PTR_ERR_OR_ZERO(hwmon_dev);
  313. }
  314. static const struct i2c_device_id adm1029_id[] = {
  315. { "adm1029", 0 },
  316. { }
  317. };
  318. MODULE_DEVICE_TABLE(i2c, adm1029_id);
  319. static struct i2c_driver adm1029_driver = {
  320. .class = I2C_CLASS_HWMON,
  321. .driver = {
  322. .name = "adm1029",
  323. },
  324. .probe_new = adm1029_probe,
  325. .id_table = adm1029_id,
  326. .detect = adm1029_detect,
  327. .address_list = normal_i2c,
  328. };
  329. module_i2c_driver(adm1029_driver);
  330. MODULE_AUTHOR("Corentin LABBE <[email protected]>");
  331. MODULE_DESCRIPTION("adm1029 driver");
  332. MODULE_LICENSE("GPL v2");