adm1021.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
  5. Philip Edelbrock <phil@netroedge.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/i2c.h>
  23. #include <linux/hwmon.h>
  24. #include <linux/hwmon-sysfs.h>
  25. #include <linux/err.h>
  26. #include <linux/mutex.h>
  27. /* Addresses to scan */
  28. static const unsigned short normal_i2c[] = {
  29. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  30. /* Insmod parameters */
  31. I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm,
  32. mc1066);
  33. /* adm1021 constants specified below */
  34. /* The adm1021 registers */
  35. /* Read-only */
  36. /* For nr in 0-1 */
  37. #define ADM1021_REG_TEMP(nr) (nr)
  38. #define ADM1021_REG_STATUS 0x02
  39. /* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
  40. #define ADM1021_REG_MAN_ID 0xFE
  41. /* ADM1021 = 0x0X, ADM1023 = 0x3X */
  42. #define ADM1021_REG_DEV_ID 0xFF
  43. /* These use different addresses for reading/writing */
  44. #define ADM1021_REG_CONFIG_R 0x03
  45. #define ADM1021_REG_CONFIG_W 0x09
  46. #define ADM1021_REG_CONV_RATE_R 0x04
  47. #define ADM1021_REG_CONV_RATE_W 0x0A
  48. /* These are for the ADM1023's additional precision on the remote temp sensor */
  49. #define ADM1023_REG_REM_TEMP_PREC 0x10
  50. #define ADM1023_REG_REM_OFFSET 0x11
  51. #define ADM1023_REG_REM_OFFSET_PREC 0x12
  52. #define ADM1023_REG_REM_TOS_PREC 0x13
  53. #define ADM1023_REG_REM_THYST_PREC 0x14
  54. /* limits */
  55. /* For nr in 0-1 */
  56. #define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr))
  57. #define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr))
  58. #define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr))
  59. #define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr))
  60. /* write-only */
  61. #define ADM1021_REG_ONESHOT 0x0F
  62. /* Initial values */
  63. /* Note: Even though I left the low and high limits named os and hyst,
  64. they don't quite work like a thermostat the way the LM75 does. I.e.,
  65. a lower temp than THYST actually triggers an alarm instead of
  66. clearing it. Weird, ey? --Phil */
  67. /* Each client has this additional data */
  68. struct adm1021_data {
  69. struct device *hwmon_dev;
  70. enum chips type;
  71. struct mutex update_lock;
  72. char valid; /* !=0 if following fields are valid */
  73. unsigned long last_updated; /* In jiffies */
  74. s8 temp_max[2]; /* Register values */
  75. s8 temp_min[2];
  76. s8 temp[2];
  77. u8 alarms;
  78. /* Special values for ADM1023 only */
  79. u8 remote_temp_prec;
  80. u8 remote_temp_os_prec;
  81. u8 remote_temp_hyst_prec;
  82. u8 remote_temp_offset;
  83. u8 remote_temp_offset_prec;
  84. };
  85. static int adm1021_probe(struct i2c_client *client,
  86. const struct i2c_device_id *id);
  87. static int adm1021_detect(struct i2c_client *client, int kind,
  88. struct i2c_board_info *info);
  89. static void adm1021_init_client(struct i2c_client *client);
  90. static int adm1021_remove(struct i2c_client *client);
  91. static struct adm1021_data *adm1021_update_device(struct device *dev);
  92. /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
  93. static int read_only;
  94. static const struct i2c_device_id adm1021_id[] = {
  95. { "adm1021", adm1021 },
  96. { "adm1023", adm1023 },
  97. { "max1617", max1617 },
  98. { "max1617a", max1617a },
  99. { "thmc10", thmc10 },
  100. { "lm84", lm84 },
  101. { "gl523sm", gl523sm },
  102. { "mc1066", mc1066 },
  103. { }
  104. };
  105. MODULE_DEVICE_TABLE(i2c, adm1021_id);
  106. /* This is the driver that will be inserted */
  107. static struct i2c_driver adm1021_driver = {
  108. .class = I2C_CLASS_HWMON,
  109. .driver = {
  110. .name = "adm1021",
  111. },
  112. .probe = adm1021_probe,
  113. .remove = adm1021_remove,
  114. .id_table = adm1021_id,
  115. .detect = adm1021_detect,
  116. .address_data = &addr_data,
  117. };
  118. static ssize_t show_temp(struct device *dev,
  119. struct device_attribute *devattr, char *buf)
  120. {
  121. int index = to_sensor_dev_attr(devattr)->index;
  122. struct adm1021_data *data = adm1021_update_device(dev);
  123. return sprintf(buf, "%d\n", 1000 * data->temp[index]);
  124. }
  125. static ssize_t show_temp_max(struct device *dev,
  126. struct device_attribute *devattr, char *buf)
  127. {
  128. int index = to_sensor_dev_attr(devattr)->index;
  129. struct adm1021_data *data = adm1021_update_device(dev);
  130. return sprintf(buf, "%d\n", 1000 * data->temp_max[index]);
  131. }
  132. static ssize_t show_temp_min(struct device *dev,
  133. struct device_attribute *devattr, char *buf)
  134. {
  135. int index = to_sensor_dev_attr(devattr)->index;
  136. struct adm1021_data *data = adm1021_update_device(dev);
  137. return sprintf(buf, "%d\n", 1000 * data->temp_min[index]);
  138. }
  139. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  140. char *buf)
  141. {
  142. int index = to_sensor_dev_attr(attr)->index;
  143. struct adm1021_data *data = adm1021_update_device(dev);
  144. return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
  145. }
  146. static ssize_t show_alarms(struct device *dev,
  147. struct device_attribute *attr,
  148. char *buf)
  149. {
  150. struct adm1021_data *data = adm1021_update_device(dev);
  151. return sprintf(buf, "%u\n", data->alarms);
  152. }
  153. static ssize_t set_temp_max(struct device *dev,
  154. struct device_attribute *devattr,
  155. const char *buf, size_t count)
  156. {
  157. int index = to_sensor_dev_attr(devattr)->index;
  158. struct i2c_client *client = to_i2c_client(dev);
  159. struct adm1021_data *data = i2c_get_clientdata(client);
  160. long temp = simple_strtol(buf, NULL, 10) / 1000;
  161. mutex_lock(&data->update_lock);
  162. data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
  163. if (!read_only)
  164. i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
  165. data->temp_max[index]);
  166. mutex_unlock(&data->update_lock);
  167. return count;
  168. }
  169. static ssize_t set_temp_min(struct device *dev,
  170. struct device_attribute *devattr,
  171. const char *buf, size_t count)
  172. {
  173. int index = to_sensor_dev_attr(devattr)->index;
  174. struct i2c_client *client = to_i2c_client(dev);
  175. struct adm1021_data *data = i2c_get_clientdata(client);
  176. long temp = simple_strtol(buf, NULL, 10) / 1000;
  177. mutex_lock(&data->update_lock);
  178. data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
  179. if (!read_only)
  180. i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
  181. data->temp_min[index]);
  182. mutex_unlock(&data->update_lock);
  183. return count;
  184. }
  185. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  186. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  187. set_temp_max, 0);
  188. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
  189. set_temp_min, 0);
  190. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  191. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  192. set_temp_max, 1);
  193. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
  194. set_temp_min, 1);
  195. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  196. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  197. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  198. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  199. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  200. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  201. static struct attribute *adm1021_attributes[] = {
  202. &sensor_dev_attr_temp1_max.dev_attr.attr,
  203. &sensor_dev_attr_temp1_min.dev_attr.attr,
  204. &sensor_dev_attr_temp1_input.dev_attr.attr,
  205. &sensor_dev_attr_temp2_max.dev_attr.attr,
  206. &sensor_dev_attr_temp2_min.dev_attr.attr,
  207. &sensor_dev_attr_temp2_input.dev_attr.attr,
  208. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  209. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  210. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  211. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  212. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  213. &dev_attr_alarms.attr,
  214. NULL
  215. };
  216. static const struct attribute_group adm1021_group = {
  217. .attrs = adm1021_attributes,
  218. };
  219. /* Return 0 if detection is successful, -ENODEV otherwise */
  220. static int adm1021_detect(struct i2c_client *client, int kind,
  221. struct i2c_board_info *info)
  222. {
  223. struct i2c_adapter *adapter = client->adapter;
  224. int i;
  225. const char *type_name = "";
  226. int conv_rate, status, config;
  227. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  228. pr_debug("adm1021: detect failed, "
  229. "smbus byte data not supported!\n");
  230. return -ENODEV;
  231. }
  232. status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
  233. conv_rate = i2c_smbus_read_byte_data(client,
  234. ADM1021_REG_CONV_RATE_R);
  235. config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
  236. /* Now, we do the remaining detection. */
  237. if (kind < 0) {
  238. if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
  239. || (conv_rate & 0xF8) != 0x00) {
  240. pr_debug("adm1021: detect failed, "
  241. "chip not detected!\n");
  242. return -ENODEV;
  243. }
  244. }
  245. /* Determine the chip type. */
  246. if (kind <= 0) {
  247. i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
  248. if (i == 0x41)
  249. if ((i2c_smbus_read_byte_data(client,
  250. ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
  251. kind = adm1023;
  252. else
  253. kind = adm1021;
  254. else if (i == 0x49)
  255. kind = thmc10;
  256. else if (i == 0x23)
  257. kind = gl523sm;
  258. else if ((i == 0x4d) &&
  259. (i2c_smbus_read_byte_data(client,
  260. ADM1021_REG_DEV_ID) == 0x01))
  261. kind = max1617a;
  262. else if (i == 0x54)
  263. kind = mc1066;
  264. /* LM84 Mfr ID in a different place, and it has more unused bits */
  265. else if (conv_rate == 0x00
  266. && (kind == 0 /* skip extra detection */
  267. || ((config & 0x7F) == 0x00
  268. && (status & 0xAB) == 0x00)))
  269. kind = lm84;
  270. else
  271. kind = max1617;
  272. }
  273. if (kind == max1617) {
  274. type_name = "max1617";
  275. } else if (kind == max1617a) {
  276. type_name = "max1617a";
  277. } else if (kind == adm1021) {
  278. type_name = "adm1021";
  279. } else if (kind == adm1023) {
  280. type_name = "adm1023";
  281. } else if (kind == thmc10) {
  282. type_name = "thmc10";
  283. } else if (kind == lm84) {
  284. type_name = "lm84";
  285. } else if (kind == gl523sm) {
  286. type_name = "gl523sm";
  287. } else if (kind == mc1066) {
  288. type_name = "mc1066";
  289. }
  290. pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
  291. type_name, i2c_adapter_id(adapter), client->addr);
  292. strlcpy(info->type, type_name, I2C_NAME_SIZE);
  293. return 0;
  294. }
  295. static int adm1021_probe(struct i2c_client *client,
  296. const struct i2c_device_id *id)
  297. {
  298. struct adm1021_data *data;
  299. int err;
  300. data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL);
  301. if (!data) {
  302. pr_debug("adm1021: detect failed, kzalloc failed!\n");
  303. err = -ENOMEM;
  304. goto error0;
  305. }
  306. i2c_set_clientdata(client, data);
  307. data->type = id->driver_data;
  308. mutex_init(&data->update_lock);
  309. /* Initialize the ADM1021 chip */
  310. if (data->type != lm84 && !read_only)
  311. adm1021_init_client(client);
  312. /* Register sysfs hooks */
  313. if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
  314. goto error1;
  315. data->hwmon_dev = hwmon_device_register(&client->dev);
  316. if (IS_ERR(data->hwmon_dev)) {
  317. err = PTR_ERR(data->hwmon_dev);
  318. goto error3;
  319. }
  320. return 0;
  321. error3:
  322. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  323. error1:
  324. kfree(data);
  325. error0:
  326. return err;
  327. }
  328. static void adm1021_init_client(struct i2c_client *client)
  329. {
  330. /* Enable ADC and disable suspend mode */
  331. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  332. i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
  333. /* Set Conversion rate to 1/sec (this can be tinkered with) */
  334. i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
  335. }
  336. static int adm1021_remove(struct i2c_client *client)
  337. {
  338. struct adm1021_data *data = i2c_get_clientdata(client);
  339. hwmon_device_unregister(data->hwmon_dev);
  340. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  341. kfree(data);
  342. return 0;
  343. }
  344. static struct adm1021_data *adm1021_update_device(struct device *dev)
  345. {
  346. struct i2c_client *client = to_i2c_client(dev);
  347. struct adm1021_data *data = i2c_get_clientdata(client);
  348. mutex_lock(&data->update_lock);
  349. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  350. || !data->valid) {
  351. int i;
  352. dev_dbg(&client->dev, "Starting adm1021 update\n");
  353. for (i = 0; i < 2; i++) {
  354. data->temp[i] = i2c_smbus_read_byte_data(client,
  355. ADM1021_REG_TEMP(i));
  356. data->temp_max[i] = i2c_smbus_read_byte_data(client,
  357. ADM1021_REG_TOS_R(i));
  358. data->temp_min[i] = i2c_smbus_read_byte_data(client,
  359. ADM1021_REG_THYST_R(i));
  360. }
  361. data->alarms = i2c_smbus_read_byte_data(client,
  362. ADM1021_REG_STATUS) & 0x7c;
  363. if (data->type == adm1023) {
  364. data->remote_temp_prec =
  365. i2c_smbus_read_byte_data(client,
  366. ADM1023_REG_REM_TEMP_PREC);
  367. data->remote_temp_os_prec =
  368. i2c_smbus_read_byte_data(client,
  369. ADM1023_REG_REM_TOS_PREC);
  370. data->remote_temp_hyst_prec =
  371. i2c_smbus_read_byte_data(client,
  372. ADM1023_REG_REM_THYST_PREC);
  373. data->remote_temp_offset =
  374. i2c_smbus_read_byte_data(client,
  375. ADM1023_REG_REM_OFFSET);
  376. data->remote_temp_offset_prec =
  377. i2c_smbus_read_byte_data(client,
  378. ADM1023_REG_REM_OFFSET_PREC);
  379. }
  380. data->last_updated = jiffies;
  381. data->valid = 1;
  382. }
  383. mutex_unlock(&data->update_lock);
  384. return data;
  385. }
  386. static int __init sensors_adm1021_init(void)
  387. {
  388. return i2c_add_driver(&adm1021_driver);
  389. }
  390. static void __exit sensors_adm1021_exit(void)
  391. {
  392. i2c_del_driver(&adm1021_driver);
  393. }
  394. MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
  395. "Philip Edelbrock <phil@netroedge.com>");
  396. MODULE_DESCRIPTION("adm1021 driver");
  397. MODULE_LICENSE("GPL");
  398. module_param(read_only, bool, 0);
  399. MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
  400. module_init(sensors_adm1021_init)
  401. module_exit(sensors_adm1021_exit)