adm1021.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. char low_power; /* !=0 if device in low power mode */
  74. unsigned long last_updated; /* In jiffies */
  75. int temp_max[2]; /* Register values */
  76. int temp_min[2];
  77. int temp[2];
  78. u8 alarms;
  79. /* Special values for ADM1023 only */
  80. u8 remote_temp_offset;
  81. u8 remote_temp_offset_prec;
  82. };
  83. static int adm1021_probe(struct i2c_client *client,
  84. const struct i2c_device_id *id);
  85. static int adm1021_detect(struct i2c_client *client, int kind,
  86. struct i2c_board_info *info);
  87. static void adm1021_init_client(struct i2c_client *client);
  88. static int adm1021_remove(struct i2c_client *client);
  89. static struct adm1021_data *adm1021_update_device(struct device *dev);
  90. /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
  91. static int read_only;
  92. static const struct i2c_device_id adm1021_id[] = {
  93. { "adm1021", adm1021 },
  94. { "adm1023", adm1023 },
  95. { "max1617", max1617 },
  96. { "max1617a", max1617a },
  97. { "thmc10", thmc10 },
  98. { "lm84", lm84 },
  99. { "gl523sm", gl523sm },
  100. { "mc1066", mc1066 },
  101. { }
  102. };
  103. MODULE_DEVICE_TABLE(i2c, adm1021_id);
  104. /* This is the driver that will be inserted */
  105. static struct i2c_driver adm1021_driver = {
  106. .class = I2C_CLASS_HWMON,
  107. .driver = {
  108. .name = "adm1021",
  109. },
  110. .probe = adm1021_probe,
  111. .remove = adm1021_remove,
  112. .id_table = adm1021_id,
  113. .detect = adm1021_detect,
  114. .address_data = &addr_data,
  115. };
  116. static ssize_t show_temp(struct device *dev,
  117. struct device_attribute *devattr, char *buf)
  118. {
  119. int index = to_sensor_dev_attr(devattr)->index;
  120. struct adm1021_data *data = adm1021_update_device(dev);
  121. return sprintf(buf, "%d\n", data->temp[index]);
  122. }
  123. static ssize_t show_temp_max(struct device *dev,
  124. struct device_attribute *devattr, char *buf)
  125. {
  126. int index = to_sensor_dev_attr(devattr)->index;
  127. struct adm1021_data *data = adm1021_update_device(dev);
  128. return sprintf(buf, "%d\n", data->temp_max[index]);
  129. }
  130. static ssize_t show_temp_min(struct device *dev,
  131. struct device_attribute *devattr, char *buf)
  132. {
  133. int index = to_sensor_dev_attr(devattr)->index;
  134. struct adm1021_data *data = adm1021_update_device(dev);
  135. return sprintf(buf, "%d\n", data->temp_min[index]);
  136. }
  137. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  138. char *buf)
  139. {
  140. int index = to_sensor_dev_attr(attr)->index;
  141. struct adm1021_data *data = adm1021_update_device(dev);
  142. return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
  143. }
  144. static ssize_t show_alarms(struct device *dev,
  145. struct device_attribute *attr,
  146. char *buf)
  147. {
  148. struct adm1021_data *data = adm1021_update_device(dev);
  149. return sprintf(buf, "%u\n", data->alarms);
  150. }
  151. static ssize_t set_temp_max(struct device *dev,
  152. struct device_attribute *devattr,
  153. const char *buf, size_t count)
  154. {
  155. int index = to_sensor_dev_attr(devattr)->index;
  156. struct i2c_client *client = to_i2c_client(dev);
  157. struct adm1021_data *data = i2c_get_clientdata(client);
  158. long temp = simple_strtol(buf, NULL, 10) / 1000;
  159. mutex_lock(&data->update_lock);
  160. data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
  161. if (!read_only)
  162. i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
  163. data->temp_max[index]);
  164. mutex_unlock(&data->update_lock);
  165. return count;
  166. }
  167. static ssize_t set_temp_min(struct device *dev,
  168. struct device_attribute *devattr,
  169. const char *buf, size_t count)
  170. {
  171. int index = to_sensor_dev_attr(devattr)->index;
  172. struct i2c_client *client = to_i2c_client(dev);
  173. struct adm1021_data *data = i2c_get_clientdata(client);
  174. long temp = simple_strtol(buf, NULL, 10) / 1000;
  175. mutex_lock(&data->update_lock);
  176. data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
  177. if (!read_only)
  178. i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
  179. data->temp_min[index]);
  180. mutex_unlock(&data->update_lock);
  181. return count;
  182. }
  183. static ssize_t show_low_power(struct device *dev,
  184. struct device_attribute *devattr, char *buf)
  185. {
  186. struct adm1021_data *data = adm1021_update_device(dev);
  187. return sprintf(buf, "%d\n", data->low_power);
  188. }
  189. static ssize_t set_low_power(struct device *dev,
  190. struct device_attribute *devattr,
  191. const char *buf, size_t count)
  192. {
  193. struct i2c_client *client = to_i2c_client(dev);
  194. struct adm1021_data *data = i2c_get_clientdata(client);
  195. int low_power = simple_strtol(buf, NULL, 10) != 0;
  196. mutex_lock(&data->update_lock);
  197. if (low_power != data->low_power) {
  198. int config = i2c_smbus_read_byte_data(
  199. client, ADM1021_REG_CONFIG_R);
  200. data->low_power = low_power;
  201. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  202. (config & 0xBF) | (low_power << 6));
  203. }
  204. mutex_unlock(&data->update_lock);
  205. return count;
  206. }
  207. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  208. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  209. set_temp_max, 0);
  210. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
  211. set_temp_min, 0);
  212. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  213. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  214. set_temp_max, 1);
  215. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
  216. set_temp_min, 1);
  217. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  218. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  219. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  220. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  221. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  222. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  223. static DEVICE_ATTR(low_power, S_IWUSR | S_IRUGO, show_low_power, set_low_power);
  224. static struct attribute *adm1021_attributes[] = {
  225. &sensor_dev_attr_temp1_max.dev_attr.attr,
  226. &sensor_dev_attr_temp1_min.dev_attr.attr,
  227. &sensor_dev_attr_temp1_input.dev_attr.attr,
  228. &sensor_dev_attr_temp2_max.dev_attr.attr,
  229. &sensor_dev_attr_temp2_min.dev_attr.attr,
  230. &sensor_dev_attr_temp2_input.dev_attr.attr,
  231. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  232. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  233. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  234. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  235. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  236. &dev_attr_alarms.attr,
  237. &dev_attr_low_power.attr,
  238. NULL
  239. };
  240. static const struct attribute_group adm1021_group = {
  241. .attrs = adm1021_attributes,
  242. };
  243. /* Return 0 if detection is successful, -ENODEV otherwise */
  244. static int adm1021_detect(struct i2c_client *client, int kind,
  245. struct i2c_board_info *info)
  246. {
  247. struct i2c_adapter *adapter = client->adapter;
  248. const char *type_name;
  249. int conv_rate, status, config, man_id, dev_id;
  250. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  251. pr_debug("adm1021: detect failed, "
  252. "smbus byte data not supported!\n");
  253. return -ENODEV;
  254. }
  255. status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
  256. conv_rate = i2c_smbus_read_byte_data(client,
  257. ADM1021_REG_CONV_RATE_R);
  258. config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
  259. /* Check unused bits */
  260. if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
  261. pr_debug("adm1021: detect failed, chip not detected!\n");
  262. return -ENODEV;
  263. }
  264. /* Determine the chip type. */
  265. man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
  266. dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
  267. if (man_id == 0x4d && dev_id == 0x01)
  268. type_name = "max1617a";
  269. else if (man_id == 0x41) {
  270. if ((dev_id & 0xF0) == 0x30)
  271. type_name = "adm1023";
  272. else
  273. type_name = "adm1021";
  274. } else if (man_id == 0x49)
  275. type_name = "thmc10";
  276. else if (man_id == 0x23)
  277. type_name = "gl523sm";
  278. else if (man_id == 0x54)
  279. type_name = "mc1066";
  280. /* LM84 Mfr ID in a different place, and it has more unused bits */
  281. else if (conv_rate == 0x00
  282. && (config & 0x7F) == 0x00
  283. && (status & 0xAB) == 0x00)
  284. type_name = "lm84";
  285. else
  286. type_name = "max1617";
  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)