adm1021.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. enum chips {
  31. adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066 };
  32. /* adm1021 constants specified below */
  33. /* The adm1021 registers */
  34. /* Read-only */
  35. /* For nr in 0-1 */
  36. #define ADM1021_REG_TEMP(nr) (nr)
  37. #define ADM1021_REG_STATUS 0x02
  38. /* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
  39. #define ADM1021_REG_MAN_ID 0xFE
  40. /* ADM1021 = 0x0X, ADM1023 = 0x3X */
  41. #define ADM1021_REG_DEV_ID 0xFF
  42. /* These use different addresses for reading/writing */
  43. #define ADM1021_REG_CONFIG_R 0x03
  44. #define ADM1021_REG_CONFIG_W 0x09
  45. #define ADM1021_REG_CONV_RATE_R 0x04
  46. #define ADM1021_REG_CONV_RATE_W 0x0A
  47. /* These are for the ADM1023's additional precision on the remote temp sensor */
  48. #define ADM1023_REG_REM_TEMP_PREC 0x10
  49. #define ADM1023_REG_REM_OFFSET 0x11
  50. #define ADM1023_REG_REM_OFFSET_PREC 0x12
  51. #define ADM1023_REG_REM_TOS_PREC 0x13
  52. #define ADM1023_REG_REM_THYST_PREC 0x14
  53. /* limits */
  54. /* For nr in 0-1 */
  55. #define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr))
  56. #define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr))
  57. #define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr))
  58. #define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr))
  59. /* write-only */
  60. #define ADM1021_REG_ONESHOT 0x0F
  61. /* Initial values */
  62. /* Note: Even though I left the low and high limits named os and hyst,
  63. they don't quite work like a thermostat the way the LM75 does. I.e.,
  64. a lower temp than THYST actually triggers an alarm instead of
  65. clearing it. Weird, ey? --Phil */
  66. /* Each client has this additional data */
  67. struct adm1021_data {
  68. struct device *hwmon_dev;
  69. enum chips type;
  70. struct mutex update_lock;
  71. char valid; /* !=0 if following fields are valid */
  72. char low_power; /* !=0 if device in low power mode */
  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,
  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_list = normal_i2c,
  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 ssize_t show_low_power(struct device *dev,
  183. struct device_attribute *devattr, char *buf)
  184. {
  185. struct adm1021_data *data = adm1021_update_device(dev);
  186. return sprintf(buf, "%d\n", data->low_power);
  187. }
  188. static ssize_t set_low_power(struct device *dev,
  189. struct device_attribute *devattr,
  190. const char *buf, size_t count)
  191. {
  192. struct i2c_client *client = to_i2c_client(dev);
  193. struct adm1021_data *data = i2c_get_clientdata(client);
  194. int low_power = simple_strtol(buf, NULL, 10) != 0;
  195. mutex_lock(&data->update_lock);
  196. if (low_power != data->low_power) {
  197. int config = i2c_smbus_read_byte_data(
  198. client, ADM1021_REG_CONFIG_R);
  199. data->low_power = low_power;
  200. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  201. (config & 0xBF) | (low_power << 6));
  202. }
  203. mutex_unlock(&data->update_lock);
  204. return count;
  205. }
  206. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  207. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  208. set_temp_max, 0);
  209. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
  210. set_temp_min, 0);
  211. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  212. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  213. set_temp_max, 1);
  214. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
  215. set_temp_min, 1);
  216. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  217. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  218. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  219. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  220. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  221. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  222. static DEVICE_ATTR(low_power, S_IWUSR | S_IRUGO, show_low_power, set_low_power);
  223. static struct attribute *adm1021_attributes[] = {
  224. &sensor_dev_attr_temp1_max.dev_attr.attr,
  225. &sensor_dev_attr_temp1_min.dev_attr.attr,
  226. &sensor_dev_attr_temp1_input.dev_attr.attr,
  227. &sensor_dev_attr_temp2_max.dev_attr.attr,
  228. &sensor_dev_attr_temp2_min.dev_attr.attr,
  229. &sensor_dev_attr_temp2_input.dev_attr.attr,
  230. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  231. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  232. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  233. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  234. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  235. &dev_attr_alarms.attr,
  236. &dev_attr_low_power.attr,
  237. NULL
  238. };
  239. static const struct attribute_group adm1021_group = {
  240. .attrs = adm1021_attributes,
  241. };
  242. /* Return 0 if detection is successful, -ENODEV otherwise */
  243. static int adm1021_detect(struct i2c_client *client,
  244. struct i2c_board_info *info)
  245. {
  246. struct i2c_adapter *adapter = client->adapter;
  247. const char *type_name;
  248. int conv_rate, status, config, man_id, dev_id;
  249. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  250. pr_debug("adm1021: detect failed, "
  251. "smbus byte data not supported!\n");
  252. return -ENODEV;
  253. }
  254. status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
  255. conv_rate = i2c_smbus_read_byte_data(client,
  256. ADM1021_REG_CONV_RATE_R);
  257. config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
  258. /* Check unused bits */
  259. if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
  260. pr_debug("adm1021: detect failed, chip not detected!\n");
  261. return -ENODEV;
  262. }
  263. /* Determine the chip type. */
  264. man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
  265. dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
  266. if (man_id == 0x4d && dev_id == 0x01)
  267. type_name = "max1617a";
  268. else if (man_id == 0x41) {
  269. if ((dev_id & 0xF0) == 0x30)
  270. type_name = "adm1023";
  271. else
  272. type_name = "adm1021";
  273. } else if (man_id == 0x49)
  274. type_name = "thmc10";
  275. else if (man_id == 0x23)
  276. type_name = "gl523sm";
  277. else if (man_id == 0x54)
  278. type_name = "mc1066";
  279. /* LM84 Mfr ID in a different place, and it has more unused bits */
  280. else if (conv_rate == 0x00
  281. && (config & 0x7F) == 0x00
  282. && (status & 0xAB) == 0x00)
  283. type_name = "lm84";
  284. else
  285. type_name = "max1617";
  286. pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
  287. type_name, i2c_adapter_id(adapter), client->addr);
  288. strlcpy(info->type, type_name, I2C_NAME_SIZE);
  289. return 0;
  290. }
  291. static int adm1021_probe(struct i2c_client *client,
  292. const struct i2c_device_id *id)
  293. {
  294. struct adm1021_data *data;
  295. int err;
  296. data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL);
  297. if (!data) {
  298. pr_debug("adm1021: detect failed, kzalloc failed!\n");
  299. err = -ENOMEM;
  300. goto error0;
  301. }
  302. i2c_set_clientdata(client, data);
  303. data->type = id->driver_data;
  304. mutex_init(&data->update_lock);
  305. /* Initialize the ADM1021 chip */
  306. if (data->type != lm84 && !read_only)
  307. adm1021_init_client(client);
  308. /* Register sysfs hooks */
  309. if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
  310. goto error1;
  311. data->hwmon_dev = hwmon_device_register(&client->dev);
  312. if (IS_ERR(data->hwmon_dev)) {
  313. err = PTR_ERR(data->hwmon_dev);
  314. goto error3;
  315. }
  316. return 0;
  317. error3:
  318. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  319. error1:
  320. kfree(data);
  321. error0:
  322. return err;
  323. }
  324. static void adm1021_init_client(struct i2c_client *client)
  325. {
  326. /* Enable ADC and disable suspend mode */
  327. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  328. i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
  329. /* Set Conversion rate to 1/sec (this can be tinkered with) */
  330. i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
  331. }
  332. static int adm1021_remove(struct i2c_client *client)
  333. {
  334. struct adm1021_data *data = i2c_get_clientdata(client);
  335. hwmon_device_unregister(data->hwmon_dev);
  336. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  337. kfree(data);
  338. return 0;
  339. }
  340. static struct adm1021_data *adm1021_update_device(struct device *dev)
  341. {
  342. struct i2c_client *client = to_i2c_client(dev);
  343. struct adm1021_data *data = i2c_get_clientdata(client);
  344. mutex_lock(&data->update_lock);
  345. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  346. || !data->valid) {
  347. int i;
  348. dev_dbg(&client->dev, "Starting adm1021 update\n");
  349. for (i = 0; i < 2; i++) {
  350. data->temp[i] = 1000 *
  351. (s8) i2c_smbus_read_byte_data(
  352. client, ADM1021_REG_TEMP(i));
  353. data->temp_max[i] = 1000 *
  354. (s8) i2c_smbus_read_byte_data(
  355. client, ADM1021_REG_TOS_R(i));
  356. data->temp_min[i] = 1000 *
  357. (s8) i2c_smbus_read_byte_data(
  358. client, ADM1021_REG_THYST_R(i));
  359. }
  360. data->alarms = i2c_smbus_read_byte_data(client,
  361. ADM1021_REG_STATUS) & 0x7c;
  362. if (data->type == adm1023) {
  363. /* The ADM1023 provides 3 extra bits of precision for
  364. * the remote sensor in extra registers. */
  365. data->temp[1] += 125 * (i2c_smbus_read_byte_data(
  366. client, ADM1023_REG_REM_TEMP_PREC) >> 5);
  367. data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
  368. client, ADM1023_REG_REM_TOS_PREC) >> 5);
  369. data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
  370. client, ADM1023_REG_REM_THYST_PREC) >> 5);
  371. data->remote_temp_offset =
  372. i2c_smbus_read_byte_data(client,
  373. ADM1023_REG_REM_OFFSET);
  374. data->remote_temp_offset_prec =
  375. i2c_smbus_read_byte_data(client,
  376. ADM1023_REG_REM_OFFSET_PREC);
  377. }
  378. data->last_updated = jiffies;
  379. data->valid = 1;
  380. }
  381. mutex_unlock(&data->update_lock);
  382. return data;
  383. }
  384. static int __init sensors_adm1021_init(void)
  385. {
  386. return i2c_add_driver(&adm1021_driver);
  387. }
  388. static void __exit sensors_adm1021_exit(void)
  389. {
  390. i2c_del_driver(&adm1021_driver);
  391. }
  392. MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
  393. "Philip Edelbrock <phil@netroedge.com>");
  394. MODULE_DESCRIPTION("adm1021 driver");
  395. MODULE_LICENSE("GPL");
  396. module_param(read_only, bool, 0);
  397. MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
  398. module_init(sensors_adm1021_init)
  399. module_exit(sensors_adm1021_exit)