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 i2c_client client;
  70. struct device *hwmon_dev;
  71. enum chips type;
  72. struct mutex update_lock;
  73. char valid; /* !=0 if following fields are valid */
  74. unsigned long last_updated; /* In jiffies */
  75. s8 temp_max[2]; /* Register values */
  76. s8 temp_min[2];
  77. s8 temp[2];
  78. u8 alarms;
  79. /* Special values for ADM1023 only */
  80. u8 remote_temp_prec;
  81. u8 remote_temp_os_prec;
  82. u8 remote_temp_hyst_prec;
  83. u8 remote_temp_offset;
  84. u8 remote_temp_offset_prec;
  85. };
  86. static int adm1021_attach_adapter(struct i2c_adapter *adapter);
  87. static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind);
  88. static void adm1021_init_client(struct i2c_client *client);
  89. static int adm1021_detach_client(struct i2c_client *client);
  90. static struct adm1021_data *adm1021_update_device(struct device *dev);
  91. /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
  92. static int read_only;
  93. /* This is the driver that will be inserted */
  94. static struct i2c_driver adm1021_driver = {
  95. .driver = {
  96. .name = "adm1021",
  97. },
  98. .attach_adapter = adm1021_attach_adapter,
  99. .detach_client = adm1021_detach_client,
  100. };
  101. static ssize_t show_temp(struct device *dev,
  102. struct device_attribute *devattr, char *buf)
  103. {
  104. int index = to_sensor_dev_attr(devattr)->index;
  105. struct adm1021_data *data = adm1021_update_device(dev);
  106. return sprintf(buf, "%d\n", 1000 * data->temp[index]);
  107. }
  108. static ssize_t show_temp_max(struct device *dev,
  109. struct device_attribute *devattr, char *buf)
  110. {
  111. int index = to_sensor_dev_attr(devattr)->index;
  112. struct adm1021_data *data = adm1021_update_device(dev);
  113. return sprintf(buf, "%d\n", 1000 * data->temp_max[index]);
  114. }
  115. static ssize_t show_temp_min(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", 1000 * data->temp_min[index]);
  121. }
  122. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  123. char *buf)
  124. {
  125. int index = to_sensor_dev_attr(attr)->index;
  126. struct adm1021_data *data = adm1021_update_device(dev);
  127. return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
  128. }
  129. static ssize_t show_alarms(struct device *dev,
  130. struct device_attribute *attr,
  131. char *buf)
  132. {
  133. struct adm1021_data *data = adm1021_update_device(dev);
  134. return sprintf(buf, "%u\n", data->alarms);
  135. }
  136. static ssize_t set_temp_max(struct device *dev,
  137. struct device_attribute *devattr,
  138. const char *buf, size_t count)
  139. {
  140. int index = to_sensor_dev_attr(devattr)->index;
  141. struct i2c_client *client = to_i2c_client(dev);
  142. struct adm1021_data *data = i2c_get_clientdata(client);
  143. long temp = simple_strtol(buf, NULL, 10) / 1000;
  144. mutex_lock(&data->update_lock);
  145. data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
  146. if (!read_only)
  147. i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
  148. data->temp_max[index]);
  149. mutex_unlock(&data->update_lock);
  150. return count;
  151. }
  152. static ssize_t set_temp_min(struct device *dev,
  153. struct device_attribute *devattr,
  154. const char *buf, size_t count)
  155. {
  156. int index = to_sensor_dev_attr(devattr)->index;
  157. struct i2c_client *client = to_i2c_client(dev);
  158. struct adm1021_data *data = i2c_get_clientdata(client);
  159. long temp = simple_strtol(buf, NULL, 10) / 1000;
  160. mutex_lock(&data->update_lock);
  161. data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
  162. if (!read_only)
  163. i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
  164. data->temp_min[index]);
  165. mutex_unlock(&data->update_lock);
  166. return count;
  167. }
  168. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  169. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  170. set_temp_max, 0);
  171. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
  172. set_temp_min, 0);
  173. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  174. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  175. set_temp_max, 1);
  176. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
  177. set_temp_min, 1);
  178. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  179. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  180. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  181. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  182. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  183. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  184. static int adm1021_attach_adapter(struct i2c_adapter *adapter)
  185. {
  186. if (!(adapter->class & I2C_CLASS_HWMON))
  187. return 0;
  188. return i2c_probe(adapter, &addr_data, adm1021_detect);
  189. }
  190. static struct attribute *adm1021_attributes[] = {
  191. &sensor_dev_attr_temp1_max.dev_attr.attr,
  192. &sensor_dev_attr_temp1_min.dev_attr.attr,
  193. &sensor_dev_attr_temp1_input.dev_attr.attr,
  194. &sensor_dev_attr_temp2_max.dev_attr.attr,
  195. &sensor_dev_attr_temp2_min.dev_attr.attr,
  196. &sensor_dev_attr_temp2_input.dev_attr.attr,
  197. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  198. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  199. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  200. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  201. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  202. &dev_attr_alarms.attr,
  203. NULL
  204. };
  205. static const struct attribute_group adm1021_group = {
  206. .attrs = adm1021_attributes,
  207. };
  208. static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
  209. {
  210. int i;
  211. struct i2c_client *client;
  212. struct adm1021_data *data;
  213. int err = 0;
  214. const char *type_name = "";
  215. int conv_rate, status, config;
  216. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  217. pr_debug("adm1021: detect failed, "
  218. "smbus byte data not supported!\n");
  219. goto error0;
  220. }
  221. /* OK. For now, we presume we have a valid client. We now create the
  222. client structure, even though we cannot fill it completely yet.
  223. But it allows us to access adm1021 register values. */
  224. if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
  225. pr_debug("adm1021: detect failed, kzalloc failed!\n");
  226. err = -ENOMEM;
  227. goto error0;
  228. }
  229. client = &data->client;
  230. i2c_set_clientdata(client, data);
  231. client->addr = address;
  232. client->adapter = adapter;
  233. client->driver = &adm1021_driver;
  234. status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
  235. conv_rate = i2c_smbus_read_byte_data(client,
  236. ADM1021_REG_CONV_RATE_R);
  237. config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
  238. /* Now, we do the remaining detection. */
  239. if (kind < 0) {
  240. if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
  241. || (conv_rate & 0xF8) != 0x00) {
  242. pr_debug("adm1021: detect failed, "
  243. "chip not detected!\n");
  244. err = -ENODEV;
  245. goto error1;
  246. }
  247. }
  248. /* Determine the chip type. */
  249. if (kind <= 0) {
  250. i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
  251. if (i == 0x41)
  252. if ((i2c_smbus_read_byte_data(client,
  253. ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
  254. kind = adm1023;
  255. else
  256. kind = adm1021;
  257. else if (i == 0x49)
  258. kind = thmc10;
  259. else if (i == 0x23)
  260. kind = gl523sm;
  261. else if ((i == 0x4d) &&
  262. (i2c_smbus_read_byte_data(client,
  263. ADM1021_REG_DEV_ID) == 0x01))
  264. kind = max1617a;
  265. else if (i == 0x54)
  266. kind = mc1066;
  267. /* LM84 Mfr ID in a different place, and it has more unused bits */
  268. else if (conv_rate == 0x00
  269. && (kind == 0 /* skip extra detection */
  270. || ((config & 0x7F) == 0x00
  271. && (status & 0xAB) == 0x00)))
  272. kind = lm84;
  273. else
  274. kind = max1617;
  275. }
  276. if (kind == max1617) {
  277. type_name = "max1617";
  278. } else if (kind == max1617a) {
  279. type_name = "max1617a";
  280. } else if (kind == adm1021) {
  281. type_name = "adm1021";
  282. } else if (kind == adm1023) {
  283. type_name = "adm1023";
  284. } else if (kind == thmc10) {
  285. type_name = "thmc10";
  286. } else if (kind == lm84) {
  287. type_name = "lm84";
  288. } else if (kind == gl523sm) {
  289. type_name = "gl523sm";
  290. } else if (kind == mc1066) {
  291. type_name = "mc1066";
  292. }
  293. pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
  294. type_name, i2c_adapter_id(adapter), address);
  295. /* Fill in the remaining client fields */
  296. strlcpy(client->name, type_name, I2C_NAME_SIZE);
  297. data->type = kind;
  298. mutex_init(&data->update_lock);
  299. /* Tell the I2C layer a new client has arrived */
  300. if ((err = i2c_attach_client(client)))
  301. goto error1;
  302. /* Initialize the ADM1021 chip */
  303. if (kind != lm84 && !read_only)
  304. adm1021_init_client(client);
  305. /* Register sysfs hooks */
  306. if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
  307. goto error2;
  308. data->hwmon_dev = hwmon_device_register(&client->dev);
  309. if (IS_ERR(data->hwmon_dev)) {
  310. err = PTR_ERR(data->hwmon_dev);
  311. goto error3;
  312. }
  313. return 0;
  314. error3:
  315. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  316. error2:
  317. i2c_detach_client(client);
  318. error1:
  319. kfree(data);
  320. error0:
  321. return err;
  322. }
  323. static void adm1021_init_client(struct i2c_client *client)
  324. {
  325. /* Enable ADC and disable suspend mode */
  326. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  327. i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
  328. /* Set Conversion rate to 1/sec (this can be tinkered with) */
  329. i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
  330. }
  331. static int adm1021_detach_client(struct i2c_client *client)
  332. {
  333. struct adm1021_data *data = i2c_get_clientdata(client);
  334. int err;
  335. hwmon_device_unregister(data->hwmon_dev);
  336. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  337. if ((err = i2c_detach_client(client)))
  338. return err;
  339. kfree(data);
  340. return 0;
  341. }
  342. static struct adm1021_data *adm1021_update_device(struct device *dev)
  343. {
  344. struct i2c_client *client = to_i2c_client(dev);
  345. struct adm1021_data *data = i2c_get_clientdata(client);
  346. mutex_lock(&data->update_lock);
  347. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  348. || !data->valid) {
  349. int i;
  350. dev_dbg(&client->dev, "Starting adm1021 update\n");
  351. for (i = 0; i < 2; i++) {
  352. data->temp[i] = i2c_smbus_read_byte_data(client,
  353. ADM1021_REG_TEMP(i));
  354. data->temp_max[i] = i2c_smbus_read_byte_data(client,
  355. ADM1021_REG_TOS_R(i));
  356. data->temp_min[i] = i2c_smbus_read_byte_data(client,
  357. ADM1021_REG_THYST_R(i));
  358. }
  359. data->alarms = i2c_smbus_read_byte_data(client,
  360. ADM1021_REG_STATUS) & 0x7c;
  361. if (data->type == adm1023) {
  362. data->remote_temp_prec =
  363. i2c_smbus_read_byte_data(client,
  364. ADM1023_REG_REM_TEMP_PREC);
  365. data->remote_temp_os_prec =
  366. i2c_smbus_read_byte_data(client,
  367. ADM1023_REG_REM_TOS_PREC);
  368. data->remote_temp_hyst_prec =
  369. i2c_smbus_read_byte_data(client,
  370. ADM1023_REG_REM_THYST_PREC);
  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)