adm1021.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. int i;
  249. const char *type_name = "";
  250. int conv_rate, status, config;
  251. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  252. pr_debug("adm1021: detect failed, "
  253. "smbus byte data not supported!\n");
  254. return -ENODEV;
  255. }
  256. status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
  257. conv_rate = i2c_smbus_read_byte_data(client,
  258. ADM1021_REG_CONV_RATE_R);
  259. config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
  260. /* Now, we do the remaining detection. */
  261. if (kind < 0) {
  262. if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
  263. || (conv_rate & 0xF8) != 0x00) {
  264. pr_debug("adm1021: detect failed, "
  265. "chip not detected!\n");
  266. return -ENODEV;
  267. }
  268. }
  269. /* Determine the chip type. */
  270. if (kind <= 0) {
  271. i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
  272. if (i == 0x41)
  273. if ((i2c_smbus_read_byte_data(client,
  274. ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
  275. kind = adm1023;
  276. else
  277. kind = adm1021;
  278. else if (i == 0x49)
  279. kind = thmc10;
  280. else if (i == 0x23)
  281. kind = gl523sm;
  282. else if ((i == 0x4d) &&
  283. (i2c_smbus_read_byte_data(client,
  284. ADM1021_REG_DEV_ID) == 0x01))
  285. kind = max1617a;
  286. else if (i == 0x54)
  287. kind = mc1066;
  288. /* LM84 Mfr ID in a different place, and it has more unused bits */
  289. else if (conv_rate == 0x00
  290. && (kind == 0 /* skip extra detection */
  291. || ((config & 0x7F) == 0x00
  292. && (status & 0xAB) == 0x00)))
  293. kind = lm84;
  294. else
  295. kind = max1617;
  296. }
  297. if (kind == max1617) {
  298. type_name = "max1617";
  299. } else if (kind == max1617a) {
  300. type_name = "max1617a";
  301. } else if (kind == adm1021) {
  302. type_name = "adm1021";
  303. } else if (kind == adm1023) {
  304. type_name = "adm1023";
  305. } else if (kind == thmc10) {
  306. type_name = "thmc10";
  307. } else if (kind == lm84) {
  308. type_name = "lm84";
  309. } else if (kind == gl523sm) {
  310. type_name = "gl523sm";
  311. } else if (kind == mc1066) {
  312. type_name = "mc1066";
  313. }
  314. pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
  315. type_name, i2c_adapter_id(adapter), client->addr);
  316. strlcpy(info->type, type_name, I2C_NAME_SIZE);
  317. return 0;
  318. }
  319. static int adm1021_probe(struct i2c_client *client,
  320. const struct i2c_device_id *id)
  321. {
  322. struct adm1021_data *data;
  323. int err;
  324. data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL);
  325. if (!data) {
  326. pr_debug("adm1021: detect failed, kzalloc failed!\n");
  327. err = -ENOMEM;
  328. goto error0;
  329. }
  330. i2c_set_clientdata(client, data);
  331. data->type = id->driver_data;
  332. mutex_init(&data->update_lock);
  333. /* Initialize the ADM1021 chip */
  334. if (data->type != lm84 && !read_only)
  335. adm1021_init_client(client);
  336. /* Register sysfs hooks */
  337. if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
  338. goto error1;
  339. data->hwmon_dev = hwmon_device_register(&client->dev);
  340. if (IS_ERR(data->hwmon_dev)) {
  341. err = PTR_ERR(data->hwmon_dev);
  342. goto error3;
  343. }
  344. return 0;
  345. error3:
  346. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  347. error1:
  348. kfree(data);
  349. error0:
  350. return err;
  351. }
  352. static void adm1021_init_client(struct i2c_client *client)
  353. {
  354. /* Enable ADC and disable suspend mode */
  355. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  356. i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
  357. /* Set Conversion rate to 1/sec (this can be tinkered with) */
  358. i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
  359. }
  360. static int adm1021_remove(struct i2c_client *client)
  361. {
  362. struct adm1021_data *data = i2c_get_clientdata(client);
  363. hwmon_device_unregister(data->hwmon_dev);
  364. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  365. kfree(data);
  366. return 0;
  367. }
  368. static struct adm1021_data *adm1021_update_device(struct device *dev)
  369. {
  370. struct i2c_client *client = to_i2c_client(dev);
  371. struct adm1021_data *data = i2c_get_clientdata(client);
  372. mutex_lock(&data->update_lock);
  373. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  374. || !data->valid) {
  375. int i;
  376. dev_dbg(&client->dev, "Starting adm1021 update\n");
  377. for (i = 0; i < 2; i++) {
  378. data->temp[i] = 1000 *
  379. (s8) i2c_smbus_read_byte_data(
  380. client, ADM1021_REG_TEMP(i));
  381. data->temp_max[i] = 1000 *
  382. (s8) i2c_smbus_read_byte_data(
  383. client, ADM1021_REG_TOS_R(i));
  384. data->temp_min[i] = 1000 *
  385. (s8) i2c_smbus_read_byte_data(
  386. client, ADM1021_REG_THYST_R(i));
  387. }
  388. data->alarms = i2c_smbus_read_byte_data(client,
  389. ADM1021_REG_STATUS) & 0x7c;
  390. if (data->type == adm1023) {
  391. /* The ADM1023 provides 3 extra bits of precision for
  392. * the remote sensor in extra registers. */
  393. data->temp[1] += 125 * (i2c_smbus_read_byte_data(
  394. client, ADM1023_REG_REM_TEMP_PREC) >> 5);
  395. data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
  396. client, ADM1023_REG_REM_TOS_PREC) >> 5);
  397. data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
  398. client, ADM1023_REG_REM_THYST_PREC) >> 5);
  399. data->remote_temp_offset =
  400. i2c_smbus_read_byte_data(client,
  401. ADM1023_REG_REM_OFFSET);
  402. data->remote_temp_offset_prec =
  403. i2c_smbus_read_byte_data(client,
  404. ADM1023_REG_REM_OFFSET_PREC);
  405. }
  406. data->last_updated = jiffies;
  407. data->valid = 1;
  408. }
  409. mutex_unlock(&data->update_lock);
  410. return data;
  411. }
  412. static int __init sensors_adm1021_init(void)
  413. {
  414. return i2c_add_driver(&adm1021_driver);
  415. }
  416. static void __exit sensors_adm1021_exit(void)
  417. {
  418. i2c_del_driver(&adm1021_driver);
  419. }
  420. MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
  421. "Philip Edelbrock <phil@netroedge.com>");
  422. MODULE_DESCRIPTION("adm1021 driver");
  423. MODULE_LICENSE("GPL");
  424. module_param(read_only, bool, 0);
  425. MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
  426. module_init(sensors_adm1021_init)
  427. module_exit(sensors_adm1021_exit)