adm1021.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. int temp_max[2]; /* Register values */
  75. int temp_min[2];
  76. int temp[2];
  77. u8 alarms;
  78. /* Special values for ADM1023 only */
  79. u8 remote_temp_offset;
  80. u8 remote_temp_offset_prec;
  81. };
  82. static int adm1021_probe(struct i2c_client *client,
  83. const struct i2c_device_id *id);
  84. static int adm1021_detect(struct i2c_client *client, int kind,
  85. struct i2c_board_info *info);
  86. static void adm1021_init_client(struct i2c_client *client);
  87. static int adm1021_remove(struct i2c_client *client);
  88. static struct adm1021_data *adm1021_update_device(struct device *dev);
  89. /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
  90. static int read_only;
  91. static const struct i2c_device_id adm1021_id[] = {
  92. { "adm1021", adm1021 },
  93. { "adm1023", adm1023 },
  94. { "max1617", max1617 },
  95. { "max1617a", max1617a },
  96. { "thmc10", thmc10 },
  97. { "lm84", lm84 },
  98. { "gl523sm", gl523sm },
  99. { "mc1066", mc1066 },
  100. { }
  101. };
  102. MODULE_DEVICE_TABLE(i2c, adm1021_id);
  103. /* This is the driver that will be inserted */
  104. static struct i2c_driver adm1021_driver = {
  105. .class = I2C_CLASS_HWMON,
  106. .driver = {
  107. .name = "adm1021",
  108. },
  109. .probe = adm1021_probe,
  110. .remove = adm1021_remove,
  111. .id_table = adm1021_id,
  112. .detect = adm1021_detect,
  113. .address_data = &addr_data,
  114. };
  115. static ssize_t show_temp(struct device *dev,
  116. struct device_attribute *devattr, char *buf)
  117. {
  118. int index = to_sensor_dev_attr(devattr)->index;
  119. struct adm1021_data *data = adm1021_update_device(dev);
  120. return sprintf(buf, "%d\n", data->temp[index]);
  121. }
  122. static ssize_t show_temp_max(struct device *dev,
  123. struct device_attribute *devattr, char *buf)
  124. {
  125. int index = to_sensor_dev_attr(devattr)->index;
  126. struct adm1021_data *data = adm1021_update_device(dev);
  127. return sprintf(buf, "%d\n", data->temp_max[index]);
  128. }
  129. static ssize_t show_temp_min(struct device *dev,
  130. struct device_attribute *devattr, char *buf)
  131. {
  132. int index = to_sensor_dev_attr(devattr)->index;
  133. struct adm1021_data *data = adm1021_update_device(dev);
  134. return sprintf(buf, "%d\n", data->temp_min[index]);
  135. }
  136. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  137. char *buf)
  138. {
  139. int index = to_sensor_dev_attr(attr)->index;
  140. struct adm1021_data *data = adm1021_update_device(dev);
  141. return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
  142. }
  143. static ssize_t show_alarms(struct device *dev,
  144. struct device_attribute *attr,
  145. char *buf)
  146. {
  147. struct adm1021_data *data = adm1021_update_device(dev);
  148. return sprintf(buf, "%u\n", data->alarms);
  149. }
  150. static ssize_t set_temp_max(struct device *dev,
  151. struct device_attribute *devattr,
  152. const char *buf, size_t count)
  153. {
  154. int index = to_sensor_dev_attr(devattr)->index;
  155. struct i2c_client *client = to_i2c_client(dev);
  156. struct adm1021_data *data = i2c_get_clientdata(client);
  157. long temp = simple_strtol(buf, NULL, 10) / 1000;
  158. mutex_lock(&data->update_lock);
  159. data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
  160. if (!read_only)
  161. i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
  162. data->temp_max[index]);
  163. mutex_unlock(&data->update_lock);
  164. return count;
  165. }
  166. static ssize_t set_temp_min(struct device *dev,
  167. struct device_attribute *devattr,
  168. const char *buf, size_t count)
  169. {
  170. int index = to_sensor_dev_attr(devattr)->index;
  171. struct i2c_client *client = to_i2c_client(dev);
  172. struct adm1021_data *data = i2c_get_clientdata(client);
  173. long temp = simple_strtol(buf, NULL, 10) / 1000;
  174. mutex_lock(&data->update_lock);
  175. data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
  176. if (!read_only)
  177. i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
  178. data->temp_min[index]);
  179. mutex_unlock(&data->update_lock);
  180. return count;
  181. }
  182. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  183. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  184. set_temp_max, 0);
  185. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
  186. set_temp_min, 0);
  187. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  188. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  189. set_temp_max, 1);
  190. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
  191. set_temp_min, 1);
  192. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  193. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  194. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  195. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  196. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  197. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  198. static struct attribute *adm1021_attributes[] = {
  199. &sensor_dev_attr_temp1_max.dev_attr.attr,
  200. &sensor_dev_attr_temp1_min.dev_attr.attr,
  201. &sensor_dev_attr_temp1_input.dev_attr.attr,
  202. &sensor_dev_attr_temp2_max.dev_attr.attr,
  203. &sensor_dev_attr_temp2_min.dev_attr.attr,
  204. &sensor_dev_attr_temp2_input.dev_attr.attr,
  205. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  206. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  207. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  208. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  209. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  210. &dev_attr_alarms.attr,
  211. NULL
  212. };
  213. static const struct attribute_group adm1021_group = {
  214. .attrs = adm1021_attributes,
  215. };
  216. /* Return 0 if detection is successful, -ENODEV otherwise */
  217. static int adm1021_detect(struct i2c_client *client, int kind,
  218. struct i2c_board_info *info)
  219. {
  220. struct i2c_adapter *adapter = client->adapter;
  221. int i;
  222. const char *type_name = "";
  223. int conv_rate, status, config;
  224. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  225. pr_debug("adm1021: detect failed, "
  226. "smbus byte data not supported!\n");
  227. return -ENODEV;
  228. }
  229. status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
  230. conv_rate = i2c_smbus_read_byte_data(client,
  231. ADM1021_REG_CONV_RATE_R);
  232. config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
  233. /* Now, we do the remaining detection. */
  234. if (kind < 0) {
  235. if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
  236. || (conv_rate & 0xF8) != 0x00) {
  237. pr_debug("adm1021: detect failed, "
  238. "chip not detected!\n");
  239. return -ENODEV;
  240. }
  241. }
  242. /* Determine the chip type. */
  243. if (kind <= 0) {
  244. i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
  245. if (i == 0x41)
  246. if ((i2c_smbus_read_byte_data(client,
  247. ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
  248. kind = adm1023;
  249. else
  250. kind = adm1021;
  251. else if (i == 0x49)
  252. kind = thmc10;
  253. else if (i == 0x23)
  254. kind = gl523sm;
  255. else if ((i == 0x4d) &&
  256. (i2c_smbus_read_byte_data(client,
  257. ADM1021_REG_DEV_ID) == 0x01))
  258. kind = max1617a;
  259. else if (i == 0x54)
  260. kind = mc1066;
  261. /* LM84 Mfr ID in a different place, and it has more unused bits */
  262. else if (conv_rate == 0x00
  263. && (kind == 0 /* skip extra detection */
  264. || ((config & 0x7F) == 0x00
  265. && (status & 0xAB) == 0x00)))
  266. kind = lm84;
  267. else
  268. kind = max1617;
  269. }
  270. if (kind == max1617) {
  271. type_name = "max1617";
  272. } else if (kind == max1617a) {
  273. type_name = "max1617a";
  274. } else if (kind == adm1021) {
  275. type_name = "adm1021";
  276. } else if (kind == adm1023) {
  277. type_name = "adm1023";
  278. } else if (kind == thmc10) {
  279. type_name = "thmc10";
  280. } else if (kind == lm84) {
  281. type_name = "lm84";
  282. } else if (kind == gl523sm) {
  283. type_name = "gl523sm";
  284. } else if (kind == mc1066) {
  285. type_name = "mc1066";
  286. }
  287. pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
  288. type_name, i2c_adapter_id(adapter), client->addr);
  289. strlcpy(info->type, type_name, I2C_NAME_SIZE);
  290. return 0;
  291. }
  292. static int adm1021_probe(struct i2c_client *client,
  293. const struct i2c_device_id *id)
  294. {
  295. struct adm1021_data *data;
  296. int err;
  297. data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL);
  298. if (!data) {
  299. pr_debug("adm1021: detect failed, kzalloc failed!\n");
  300. err = -ENOMEM;
  301. goto error0;
  302. }
  303. i2c_set_clientdata(client, data);
  304. data->type = id->driver_data;
  305. mutex_init(&data->update_lock);
  306. /* Initialize the ADM1021 chip */
  307. if (data->type != lm84 && !read_only)
  308. adm1021_init_client(client);
  309. /* Register sysfs hooks */
  310. if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
  311. goto error1;
  312. data->hwmon_dev = hwmon_device_register(&client->dev);
  313. if (IS_ERR(data->hwmon_dev)) {
  314. err = PTR_ERR(data->hwmon_dev);
  315. goto error3;
  316. }
  317. return 0;
  318. error3:
  319. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  320. error1:
  321. kfree(data);
  322. error0:
  323. return err;
  324. }
  325. static void adm1021_init_client(struct i2c_client *client)
  326. {
  327. /* Enable ADC and disable suspend mode */
  328. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  329. i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
  330. /* Set Conversion rate to 1/sec (this can be tinkered with) */
  331. i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
  332. }
  333. static int adm1021_remove(struct i2c_client *client)
  334. {
  335. struct adm1021_data *data = i2c_get_clientdata(client);
  336. hwmon_device_unregister(data->hwmon_dev);
  337. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  338. kfree(data);
  339. return 0;
  340. }
  341. static struct adm1021_data *adm1021_update_device(struct device *dev)
  342. {
  343. struct i2c_client *client = to_i2c_client(dev);
  344. struct adm1021_data *data = i2c_get_clientdata(client);
  345. mutex_lock(&data->update_lock);
  346. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  347. || !data->valid) {
  348. int i;
  349. dev_dbg(&client->dev, "Starting adm1021 update\n");
  350. for (i = 0; i < 2; i++) {
  351. data->temp[i] = 1000 *
  352. (s8) i2c_smbus_read_byte_data(
  353. client, ADM1021_REG_TEMP(i));
  354. data->temp_max[i] = 1000 *
  355. (s8) i2c_smbus_read_byte_data(
  356. client, ADM1021_REG_TOS_R(i));
  357. data->temp_min[i] = 1000 *
  358. (s8) i2c_smbus_read_byte_data(
  359. client, 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. /* The ADM1023 provides 3 extra bits of precision for
  365. * the remote sensor in extra registers. */
  366. data->temp[1] += 125 * (i2c_smbus_read_byte_data(
  367. client, ADM1023_REG_REM_TEMP_PREC) >> 5);
  368. data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
  369. client, ADM1023_REG_REM_TOS_PREC) >> 5);
  370. data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
  371. client, ADM1023_REG_REM_THYST_PREC) >> 5);
  372. data->remote_temp_offset =
  373. i2c_smbus_read_byte_data(client,
  374. ADM1023_REG_REM_OFFSET);
  375. data->remote_temp_offset_prec =
  376. i2c_smbus_read_byte_data(client,
  377. ADM1023_REG_REM_OFFSET_PREC);
  378. }
  379. data->last_updated = jiffies;
  380. data->valid = 1;
  381. }
  382. mutex_unlock(&data->update_lock);
  383. return data;
  384. }
  385. static int __init sensors_adm1021_init(void)
  386. {
  387. return i2c_add_driver(&adm1021_driver);
  388. }
  389. static void __exit sensors_adm1021_exit(void)
  390. {
  391. i2c_del_driver(&adm1021_driver);
  392. }
  393. MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
  394. "Philip Edelbrock <phil@netroedge.com>");
  395. MODULE_DESCRIPTION("adm1021 driver");
  396. MODULE_LICENSE("GPL");
  397. module_param(read_only, bool, 0);
  398. MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
  399. module_init(sensors_adm1021_init)
  400. module_exit(sensors_adm1021_exit)