adm1029.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
  3. *
  4. * Copyright (C) 2006 Corentin LABBE <corentin.labbe@geomatys.fr>
  5. *
  6. * Based on LM83 Driver by Jean Delvare <khali@linux-fr.org>
  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 unsigned short normal_i2c[] = {
  40. 0x28, 0x29, 0x2a,
  41. 0x2b, 0x2c, 0x2d,
  42. 0x2e, 0x2f, I2C_CLIENT_END
  43. };
  44. /*
  45. * Insmod parameters
  46. */
  47. I2C_CLIENT_INSMOD_1(adm1029);
  48. /*
  49. * The ADM1029 registers
  50. * Manufacturer ID is 0x41 for Analog Devices
  51. */
  52. #define ADM1029_REG_MAN_ID 0x0D
  53. #define ADM1029_REG_CHIP_ID 0x0E
  54. #define ADM1029_REG_CONFIG 0x01
  55. #define ADM1029_REG_NB_FAN_SUPPORT 0x02
  56. #define ADM1029_REG_TEMP_DEVICES_INSTALLED 0x06
  57. #define ADM1029_REG_LOCAL_TEMP 0xA0
  58. #define ADM1029_REG_REMOTE1_TEMP 0xA1
  59. #define ADM1029_REG_REMOTE2_TEMP 0xA2
  60. #define ADM1029_REG_LOCAL_TEMP_HIGH 0x90
  61. #define ADM1029_REG_REMOTE1_TEMP_HIGH 0x91
  62. #define ADM1029_REG_REMOTE2_TEMP_HIGH 0x92
  63. #define ADM1029_REG_LOCAL_TEMP_LOW 0x98
  64. #define ADM1029_REG_REMOTE1_TEMP_LOW 0x99
  65. #define ADM1029_REG_REMOTE2_TEMP_LOW 0x9A
  66. #define ADM1029_REG_FAN1 0x70
  67. #define ADM1029_REG_FAN2 0x71
  68. #define ADM1029_REG_FAN1_MIN 0x78
  69. #define ADM1029_REG_FAN2_MIN 0x79
  70. #define ADM1029_REG_FAN1_CONFIG 0x68
  71. #define ADM1029_REG_FAN2_CONFIG 0x69
  72. #define TEMP_FROM_REG(val) ((val) * 1000)
  73. #define DIV_FROM_REG(val) ( 1 << (((val) >> 6) - 1))
  74. /* Registers to be checked by adm1029_update_device() */
  75. static const u8 ADM1029_REG_TEMP[] = {
  76. ADM1029_REG_LOCAL_TEMP,
  77. ADM1029_REG_REMOTE1_TEMP,
  78. ADM1029_REG_REMOTE2_TEMP,
  79. ADM1029_REG_LOCAL_TEMP_HIGH,
  80. ADM1029_REG_REMOTE1_TEMP_HIGH,
  81. ADM1029_REG_REMOTE2_TEMP_HIGH,
  82. ADM1029_REG_LOCAL_TEMP_LOW,
  83. ADM1029_REG_REMOTE1_TEMP_LOW,
  84. ADM1029_REG_REMOTE2_TEMP_LOW,
  85. };
  86. static const u8 ADM1029_REG_FAN[] = {
  87. ADM1029_REG_FAN1,
  88. ADM1029_REG_FAN2,
  89. ADM1029_REG_FAN1_MIN,
  90. ADM1029_REG_FAN2_MIN,
  91. };
  92. static const u8 ADM1029_REG_FAN_DIV[] = {
  93. ADM1029_REG_FAN1_CONFIG,
  94. ADM1029_REG_FAN2_CONFIG,
  95. };
  96. /*
  97. * Functions declaration
  98. */
  99. static int adm1029_attach_adapter(struct i2c_adapter *adapter);
  100. static int adm1029_detect(struct i2c_adapter *adapter, int address, int kind);
  101. static int adm1029_detach_client(struct i2c_client *client);
  102. static struct adm1029_data *adm1029_update_device(struct device *dev);
  103. static int adm1029_init_client(struct i2c_client *client);
  104. /*
  105. * Driver data (common to all clients)
  106. */
  107. static struct i2c_driver adm1029_driver = {
  108. .driver = {
  109. .name = "adm1029",
  110. },
  111. .attach_adapter = adm1029_attach_adapter,
  112. .detach_client = adm1029_detach_client,
  113. };
  114. /*
  115. * Client data (each client gets its own)
  116. */
  117. struct adm1029_data {
  118. struct i2c_client client;
  119. struct class_device *class_dev;
  120. struct mutex update_lock;
  121. char valid; /* zero until following fields are valid */
  122. unsigned long last_updated; /* in jiffies */
  123. /* registers values, signed for temperature, unsigned for other stuff */
  124. s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)];
  125. u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)];
  126. u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)];
  127. };
  128. /*
  129. * Sysfs stuff
  130. */
  131. static ssize_t
  132. show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
  133. {
  134. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  135. struct adm1029_data *data = adm1029_update_device(dev);
  136. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
  137. }
  138. static ssize_t
  139. show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
  140. {
  141. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  142. struct adm1029_data *data = adm1029_update_device(dev);
  143. u16 val;
  144. if (data->fan[attr->index] == 0 || data->fan_div[attr->index] == 0
  145. || data->fan[attr->index] == 255) {
  146. return sprintf(buf, "0\n");
  147. }
  148. val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
  149. / data->fan[attr->index];
  150. return sprintf(buf, "%d\n", val);
  151. }
  152. static ssize_t
  153. show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
  154. {
  155. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  156. struct adm1029_data *data = adm1029_update_device(dev);
  157. if (data->fan_div[attr->index] == 0)
  158. return sprintf(buf, "0\n");
  159. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
  160. }
  161. static ssize_t set_fan_div(struct device *dev,
  162. struct device_attribute *devattr, const char *buf, size_t count)
  163. {
  164. struct i2c_client *client = to_i2c_client(dev);
  165. struct adm1029_data *data = i2c_get_clientdata(client);
  166. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  167. long val = simple_strtol(buf, NULL, 10);
  168. u8 reg;
  169. mutex_lock(&data->update_lock);
  170. /*Read actual config */
  171. reg = i2c_smbus_read_byte_data(client,
  172. ADM1029_REG_FAN_DIV[attr->index]);
  173. switch (val) {
  174. case 1:
  175. val = 1;
  176. break;
  177. case 2:
  178. val = 2;
  179. break;
  180. case 4:
  181. val = 3;
  182. break;
  183. default:
  184. mutex_unlock(&data->update_lock);
  185. dev_err(&client->dev, "fan_div value %ld not "
  186. "supported. Choose one of 1, 2 or 4!\n", val);
  187. return -EINVAL;
  188. }
  189. /* Update the value */
  190. reg = (reg & 0x3F) | (val << 6);
  191. /* Write value */
  192. i2c_smbus_write_byte_data(client,
  193. ADM1029_REG_FAN_DIV[attr->index], reg);
  194. mutex_unlock(&data->update_lock);
  195. return count;
  196. }
  197. /*
  198. Access rights on sysfs, S_IRUGO stand for Is Readable by User, Group and Others
  199. S_IWUSR stand for Is Writable by User
  200. */
  201. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  202. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  203. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  204. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 3);
  205. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp, NULL, 4);
  206. static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp, NULL, 5);
  207. static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, NULL, 6);
  208. static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp, NULL, 7);
  209. static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp, NULL, 8);
  210. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  211. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  212. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan, NULL, 2);
  213. static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan, NULL, 3);
  214. static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
  215. show_fan_div, set_fan_div, 0);
  216. static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
  217. show_fan_div, set_fan_div, 1);
  218. static struct attribute *adm1029_attributes[] = {
  219. &sensor_dev_attr_temp1_input.dev_attr.attr,
  220. &sensor_dev_attr_temp1_min.dev_attr.attr,
  221. &sensor_dev_attr_temp1_max.dev_attr.attr,
  222. &sensor_dev_attr_temp2_input.dev_attr.attr,
  223. &sensor_dev_attr_temp2_min.dev_attr.attr,
  224. &sensor_dev_attr_temp2_max.dev_attr.attr,
  225. &sensor_dev_attr_temp3_input.dev_attr.attr,
  226. &sensor_dev_attr_temp3_min.dev_attr.attr,
  227. &sensor_dev_attr_temp3_max.dev_attr.attr,
  228. &sensor_dev_attr_fan1_input.dev_attr.attr,
  229. &sensor_dev_attr_fan2_input.dev_attr.attr,
  230. &sensor_dev_attr_fan1_min.dev_attr.attr,
  231. &sensor_dev_attr_fan2_min.dev_attr.attr,
  232. &sensor_dev_attr_fan1_div.dev_attr.attr,
  233. &sensor_dev_attr_fan2_div.dev_attr.attr,
  234. NULL
  235. };
  236. static const struct attribute_group adm1029_group = {
  237. .attrs = adm1029_attributes,
  238. };
  239. /*
  240. * Real code
  241. */
  242. static int adm1029_attach_adapter(struct i2c_adapter *adapter)
  243. {
  244. if (!(adapter->class & I2C_CLASS_HWMON))
  245. return 0;
  246. return i2c_probe(adapter, &addr_data, adm1029_detect);
  247. }
  248. /*
  249. * The following function does more than just detection. If detection
  250. * succeeds, it also registers the new chip.
  251. */
  252. static int adm1029_detect(struct i2c_adapter *adapter, int address, int kind)
  253. {
  254. struct i2c_client *client;
  255. struct adm1029_data *data;
  256. int err = 0;
  257. const char *name = "";
  258. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  259. goto exit;
  260. if (!(data = kzalloc(sizeof(struct adm1029_data), GFP_KERNEL))) {
  261. err = -ENOMEM;
  262. goto exit;
  263. }
  264. client = &data->client;
  265. i2c_set_clientdata(client, data);
  266. client->addr = address;
  267. client->adapter = adapter;
  268. client->driver = &adm1029_driver;
  269. /* Now we do the detection and identification. A negative kind
  270. * means that the driver was loaded with no force parameter
  271. * (default), so we must both detect and identify the chip
  272. * (actually there is only one possible kind of chip for now, adm1029).
  273. * A zero kind means that the driver was loaded with the force
  274. * parameter, the detection step shall be skipped. A positive kind
  275. * means that the driver was loaded with the force parameter and a
  276. * given kind of chip is requested, so both the detection and the
  277. * identification steps are skipped. */
  278. /* Default to an adm1029 if forced */
  279. if (kind == 0)
  280. kind = adm1029;
  281. /* ADM1029 doesn't have CHIP ID, check just MAN ID
  282. * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
  283. * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
  284. * documented
  285. */
  286. if (kind <= 0) { /* identification */
  287. u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
  288. man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
  289. chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
  290. temp_devices_installed = i2c_smbus_read_byte_data(client,
  291. ADM1029_REG_TEMP_DEVICES_INSTALLED);
  292. nb_fan_support = i2c_smbus_read_byte_data(client,
  293. ADM1029_REG_NB_FAN_SUPPORT);
  294. /* 0x41 is Analog Devices */
  295. if (man_id == 0x41 && (temp_devices_installed & 0xf9) == 0x01
  296. && nb_fan_support == 0x03) {
  297. if ((chip_id & 0xF0) == 0x00) {
  298. kind = adm1029;
  299. } else {
  300. /* There are no "official" CHIP ID, so actually
  301. * we use Major/Minor revision for that */
  302. printk(KERN_INFO
  303. "adm1029: Unknown major revision %x, "
  304. "please let us know\n", chip_id);
  305. }
  306. }
  307. if (kind <= 0) { /* identification failed */
  308. pr_debug("adm1029: Unsupported chip (man_id=0x%02X, "
  309. "chip_id=0x%02X)\n", man_id, chip_id);
  310. goto exit_free;
  311. }
  312. }
  313. if (kind == adm1029) {
  314. name = "adm1029";
  315. }
  316. /* We can fill in the remaining client fields */
  317. strlcpy(client->name, name, I2C_NAME_SIZE);
  318. mutex_init(&data->update_lock);
  319. /* Tell the I2C layer a new client has arrived */
  320. if ((err = i2c_attach_client(client)))
  321. goto exit_free;
  322. /*
  323. * Initialize the ADM1029 chip
  324. * Check config register
  325. */
  326. if (adm1029_init_client(client) == 0)
  327. goto exit_detach;
  328. /* Register sysfs hooks */
  329. if ((err = sysfs_create_group(&client->dev.kobj, &adm1029_group)))
  330. goto exit_detach;
  331. data->class_dev = hwmon_device_register(&client->dev);
  332. if (IS_ERR(data->class_dev)) {
  333. err = PTR_ERR(data->class_dev);
  334. goto exit_remove_files;
  335. }
  336. return 0;
  337. exit_remove_files:
  338. sysfs_remove_group(&client->dev.kobj, &adm1029_group);
  339. exit_detach:
  340. i2c_detach_client(client);
  341. exit_free:
  342. kfree(data);
  343. exit:
  344. return err;
  345. }
  346. static int adm1029_init_client(struct i2c_client *client)
  347. {
  348. u8 config;
  349. config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
  350. if ((config & 0x10) == 0) {
  351. i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
  352. config | 0x10);
  353. }
  354. /* recheck config */
  355. config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
  356. if ((config & 0x10) == 0) {
  357. dev_err(&client->dev, "Initialization failed!\n");
  358. return 0;
  359. }
  360. return 1;
  361. }
  362. static int adm1029_detach_client(struct i2c_client *client)
  363. {
  364. struct adm1029_data *data = i2c_get_clientdata(client);
  365. int err;
  366. hwmon_device_unregister(data->class_dev);
  367. sysfs_remove_group(&client->dev.kobj, &adm1029_group);
  368. if ((err = i2c_detach_client(client)))
  369. return err;
  370. kfree(data);
  371. return 0;
  372. }
  373. /*
  374. function that update the status of the chips (temperature for exemple)
  375. */
  376. static struct adm1029_data *adm1029_update_device(struct device *dev)
  377. {
  378. struct i2c_client *client = to_i2c_client(dev);
  379. struct adm1029_data *data = i2c_get_clientdata(client);
  380. mutex_lock(&data->update_lock);
  381. /*
  382. * Use the "cache" Luke, don't recheck values
  383. * if there are already checked not a long time later
  384. */
  385. if (time_after(jiffies, data->last_updated + HZ * 2)
  386. || !data->valid) {
  387. int nr;
  388. dev_dbg(&client->dev, "Updating adm1029 data\n");
  389. for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
  390. data->temp[nr] =
  391. i2c_smbus_read_byte_data(client,
  392. ADM1029_REG_TEMP[nr]);
  393. }
  394. for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
  395. data->fan[nr] =
  396. i2c_smbus_read_byte_data(client,
  397. ADM1029_REG_FAN[nr]);
  398. }
  399. for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
  400. data->fan_div[nr] =
  401. i2c_smbus_read_byte_data(client,
  402. ADM1029_REG_FAN_DIV[nr]);
  403. }
  404. data->last_updated = jiffies;
  405. data->valid = 1;
  406. }
  407. mutex_unlock(&data->update_lock);
  408. return data;
  409. }
  410. /*
  411. Common module stuff
  412. */
  413. static int __init sensors_adm1029_init(void)
  414. {
  415. return i2c_add_driver(&adm1029_driver);
  416. }
  417. static void __exit sensors_adm1029_exit(void)
  418. {
  419. i2c_del_driver(&adm1029_driver);
  420. }
  421. MODULE_AUTHOR("Corentin LABBE <corentin.labbe@geomatys.fr>");
  422. MODULE_DESCRIPTION("adm1029 driver");
  423. MODULE_LICENSE("GPL v2");
  424. module_init(sensors_adm1029_init);
  425. module_exit(sensors_adm1029_exit);