adm1021.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
  29. 0x29, 0x2a, 0x2b,
  30. 0x4c, 0x4d, 0x4e,
  31. I2C_CLIENT_END };
  32. /* Insmod parameters */
  33. I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm,
  34. mc1066);
  35. /* adm1021 constants specified below */
  36. /* The adm1021 registers */
  37. /* Read-only */
  38. /* For nr in 0-1 */
  39. #define ADM1021_REG_TEMP(nr) (nr)
  40. #define ADM1021_REG_STATUS 0x02
  41. /* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
  42. #define ADM1021_REG_MAN_ID 0xFE
  43. /* ADM1021 = 0x0X, ADM1023 = 0x3X */
  44. #define ADM1021_REG_DEV_ID 0xFF
  45. /* These use different addresses for reading/writing */
  46. #define ADM1021_REG_CONFIG_R 0x03
  47. #define ADM1021_REG_CONFIG_W 0x09
  48. #define ADM1021_REG_CONV_RATE_R 0x04
  49. #define ADM1021_REG_CONV_RATE_W 0x0A
  50. /* These are for the ADM1023's additional precision on the remote temp sensor */
  51. #define ADM1023_REG_REM_TEMP_PREC 0x10
  52. #define ADM1023_REG_REM_OFFSET 0x11
  53. #define ADM1023_REG_REM_OFFSET_PREC 0x12
  54. #define ADM1023_REG_REM_TOS_PREC 0x13
  55. #define ADM1023_REG_REM_THYST_PREC 0x14
  56. /* limits */
  57. /* For nr in 0-1 */
  58. #define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr))
  59. #define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr))
  60. #define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr))
  61. #define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr))
  62. /* write-only */
  63. #define ADM1021_REG_ONESHOT 0x0F
  64. /* Initial values */
  65. /* Note: Even though I left the low and high limits named os and hyst,
  66. they don't quite work like a thermostat the way the LM75 does. I.e.,
  67. a lower temp than THYST actually triggers an alarm instead of
  68. clearing it. Weird, ey? --Phil */
  69. /* Each client has this additional data */
  70. struct adm1021_data {
  71. struct i2c_client client;
  72. struct device *hwmon_dev;
  73. enum chips type;
  74. struct mutex update_lock;
  75. char valid; /* !=0 if following fields are valid */
  76. unsigned long last_updated; /* In jiffies */
  77. s8 temp_max[2]; /* Register values */
  78. s8 temp_min[2];
  79. s8 temp[2];
  80. u8 alarms;
  81. /* Special values for ADM1023 only */
  82. u8 remote_temp_prec;
  83. u8 remote_temp_os_prec;
  84. u8 remote_temp_hyst_prec;
  85. u8 remote_temp_offset;
  86. u8 remote_temp_offset_prec;
  87. };
  88. static int adm1021_attach_adapter(struct i2c_adapter *adapter);
  89. static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind);
  90. static void adm1021_init_client(struct i2c_client *client);
  91. static int adm1021_detach_client(struct i2c_client *client);
  92. static struct adm1021_data *adm1021_update_device(struct device *dev);
  93. /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
  94. static int read_only;
  95. /* This is the driver that will be inserted */
  96. static struct i2c_driver adm1021_driver = {
  97. .driver = {
  98. .name = "adm1021",
  99. },
  100. .attach_adapter = adm1021_attach_adapter,
  101. .detach_client = adm1021_detach_client,
  102. };
  103. static ssize_t show_temp(struct device *dev,
  104. struct device_attribute *devattr, char *buf)
  105. {
  106. int index = to_sensor_dev_attr(devattr)->index;
  107. struct adm1021_data *data = adm1021_update_device(dev);
  108. return sprintf(buf, "%d\n", 1000 * data->temp[index]);
  109. }
  110. static ssize_t show_temp_max(struct device *dev,
  111. struct device_attribute *devattr, char *buf)
  112. {
  113. int index = to_sensor_dev_attr(devattr)->index;
  114. struct adm1021_data *data = adm1021_update_device(dev);
  115. return sprintf(buf, "%d\n", 1000 * data->temp_max[index]);
  116. }
  117. static ssize_t show_temp_min(struct device *dev,
  118. struct device_attribute *devattr, char *buf)
  119. {
  120. int index = to_sensor_dev_attr(devattr)->index;
  121. struct adm1021_data *data = adm1021_update_device(dev);
  122. return sprintf(buf, "%d\n", 1000 * data->temp_min[index]);
  123. }
  124. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  125. char *buf)
  126. {
  127. int index = to_sensor_dev_attr(attr)->index;
  128. struct adm1021_data *data = adm1021_update_device(dev);
  129. return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
  130. }
  131. static ssize_t show_alarms(struct device *dev,
  132. struct device_attribute *attr,
  133. char *buf)
  134. {
  135. struct adm1021_data *data = adm1021_update_device(dev);
  136. return sprintf(buf, "%u\n", data->alarms);
  137. }
  138. static ssize_t set_temp_max(struct device *dev,
  139. struct device_attribute *devattr,
  140. const char *buf, size_t count)
  141. {
  142. int index = to_sensor_dev_attr(devattr)->index;
  143. struct i2c_client *client = to_i2c_client(dev);
  144. struct adm1021_data *data = i2c_get_clientdata(client);
  145. long temp = simple_strtol(buf, NULL, 10) / 1000;
  146. mutex_lock(&data->update_lock);
  147. data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
  148. if (!read_only)
  149. i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
  150. data->temp_max[index]);
  151. mutex_unlock(&data->update_lock);
  152. return count;
  153. }
  154. static ssize_t set_temp_min(struct device *dev,
  155. struct device_attribute *devattr,
  156. const char *buf, size_t count)
  157. {
  158. int index = to_sensor_dev_attr(devattr)->index;
  159. struct i2c_client *client = to_i2c_client(dev);
  160. struct adm1021_data *data = i2c_get_clientdata(client);
  161. long temp = simple_strtol(buf, NULL, 10) / 1000;
  162. mutex_lock(&data->update_lock);
  163. data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
  164. if (!read_only)
  165. i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
  166. data->temp_min[index]);
  167. mutex_unlock(&data->update_lock);
  168. return count;
  169. }
  170. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  171. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  172. set_temp_max, 0);
  173. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
  174. set_temp_min, 0);
  175. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  176. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  177. set_temp_max, 1);
  178. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
  179. set_temp_min, 1);
  180. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  181. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  182. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  183. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  184. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  185. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  186. static int adm1021_attach_adapter(struct i2c_adapter *adapter)
  187. {
  188. if (!(adapter->class & I2C_CLASS_HWMON))
  189. return 0;
  190. return i2c_probe(adapter, &addr_data, adm1021_detect);
  191. }
  192. static struct attribute *adm1021_attributes[] = {
  193. &sensor_dev_attr_temp1_max.dev_attr.attr,
  194. &sensor_dev_attr_temp1_min.dev_attr.attr,
  195. &sensor_dev_attr_temp1_input.dev_attr.attr,
  196. &sensor_dev_attr_temp2_max.dev_attr.attr,
  197. &sensor_dev_attr_temp2_min.dev_attr.attr,
  198. &sensor_dev_attr_temp2_input.dev_attr.attr,
  199. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  200. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  201. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  202. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  203. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  204. &dev_attr_alarms.attr,
  205. NULL
  206. };
  207. static const struct attribute_group adm1021_group = {
  208. .attrs = adm1021_attributes,
  209. };
  210. static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
  211. {
  212. int i;
  213. struct i2c_client *client;
  214. struct adm1021_data *data;
  215. int err = 0;
  216. const char *type_name = "";
  217. int conv_rate, status, config;
  218. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  219. pr_debug("adm1021: detect failed, "
  220. "smbus byte data not supported!\n");
  221. goto error0;
  222. }
  223. /* OK. For now, we presume we have a valid client. We now create the
  224. client structure, even though we cannot fill it completely yet.
  225. But it allows us to access adm1021 register values. */
  226. if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
  227. pr_debug("adm1021: detect failed, kzalloc failed!\n");
  228. err = -ENOMEM;
  229. goto error0;
  230. }
  231. client = &data->client;
  232. i2c_set_clientdata(client, data);
  233. client->addr = address;
  234. client->adapter = adapter;
  235. client->driver = &adm1021_driver;
  236. status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
  237. conv_rate = i2c_smbus_read_byte_data(client,
  238. ADM1021_REG_CONV_RATE_R);
  239. config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
  240. /* Now, we do the remaining detection. */
  241. if (kind < 0) {
  242. if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
  243. || (conv_rate & 0xF8) != 0x00) {
  244. pr_debug("adm1021: detect failed, "
  245. "chip not detected!\n");
  246. err = -ENODEV;
  247. goto error1;
  248. }
  249. }
  250. /* Determine the chip type. */
  251. if (kind <= 0) {
  252. i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
  253. if (i == 0x41)
  254. if ((i2c_smbus_read_byte_data(client,
  255. ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
  256. kind = adm1023;
  257. else
  258. kind = adm1021;
  259. else if (i == 0x49)
  260. kind = thmc10;
  261. else if (i == 0x23)
  262. kind = gl523sm;
  263. else if ((i == 0x4d) &&
  264. (i2c_smbus_read_byte_data(client,
  265. ADM1021_REG_DEV_ID) == 0x01))
  266. kind = max1617a;
  267. else if (i == 0x54)
  268. kind = mc1066;
  269. /* LM84 Mfr ID in a different place, and it has more unused bits */
  270. else if (conv_rate == 0x00
  271. && (kind == 0 /* skip extra detection */
  272. || ((config & 0x7F) == 0x00
  273. && (status & 0xAB) == 0x00)))
  274. kind = lm84;
  275. else
  276. kind = max1617;
  277. }
  278. if (kind == max1617) {
  279. type_name = "max1617";
  280. } else if (kind == max1617a) {
  281. type_name = "max1617a";
  282. } else if (kind == adm1021) {
  283. type_name = "adm1021";
  284. } else if (kind == adm1023) {
  285. type_name = "adm1023";
  286. } else if (kind == thmc10) {
  287. type_name = "thmc10";
  288. } else if (kind == lm84) {
  289. type_name = "lm84";
  290. } else if (kind == gl523sm) {
  291. type_name = "gl523sm";
  292. } else if (kind == mc1066) {
  293. type_name = "mc1066";
  294. }
  295. pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
  296. type_name, i2c_adapter_id(adapter), address);
  297. /* Fill in the remaining client fields */
  298. strlcpy(client->name, type_name, I2C_NAME_SIZE);
  299. data->type = kind;
  300. mutex_init(&data->update_lock);
  301. /* Tell the I2C layer a new client has arrived */
  302. if ((err = i2c_attach_client(client)))
  303. goto error1;
  304. /* Initialize the ADM1021 chip */
  305. if (kind != lm84 && !read_only)
  306. adm1021_init_client(client);
  307. /* Register sysfs hooks */
  308. if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
  309. goto error2;
  310. data->hwmon_dev = hwmon_device_register(&client->dev);
  311. if (IS_ERR(data->hwmon_dev)) {
  312. err = PTR_ERR(data->hwmon_dev);
  313. goto error3;
  314. }
  315. return 0;
  316. error3:
  317. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  318. error2:
  319. i2c_detach_client(client);
  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_detach_client(struct i2c_client *client)
  334. {
  335. struct adm1021_data *data = i2c_get_clientdata(client);
  336. int err;
  337. hwmon_device_unregister(data->hwmon_dev);
  338. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  339. if ((err = i2c_detach_client(client)))
  340. return err;
  341. kfree(data);
  342. return 0;
  343. }
  344. static struct adm1021_data *adm1021_update_device(struct device *dev)
  345. {
  346. struct i2c_client *client = to_i2c_client(dev);
  347. struct adm1021_data *data = i2c_get_clientdata(client);
  348. mutex_lock(&data->update_lock);
  349. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  350. || !data->valid) {
  351. int i;
  352. dev_dbg(&client->dev, "Starting adm1021 update\n");
  353. for (i = 0; i < 2; i++) {
  354. data->temp[i] = i2c_smbus_read_byte_data(client,
  355. ADM1021_REG_TEMP(i));
  356. data->temp_max[i] = i2c_smbus_read_byte_data(client,
  357. ADM1021_REG_TOS_R(i));
  358. data->temp_min[i] = i2c_smbus_read_byte_data(client,
  359. 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. data->remote_temp_prec =
  365. i2c_smbus_read_byte_data(client,
  366. ADM1023_REG_REM_TEMP_PREC);
  367. data->remote_temp_os_prec =
  368. i2c_smbus_read_byte_data(client,
  369. ADM1023_REG_REM_TOS_PREC);
  370. data->remote_temp_hyst_prec =
  371. i2c_smbus_read_byte_data(client,
  372. ADM1023_REG_REM_THYST_PREC);
  373. data->remote_temp_offset =
  374. i2c_smbus_read_byte_data(client,
  375. ADM1023_REG_REM_OFFSET);
  376. data->remote_temp_offset_prec =
  377. i2c_smbus_read_byte_data(client,
  378. ADM1023_REG_REM_OFFSET_PREC);
  379. }
  380. data->last_updated = jiffies;
  381. data->valid = 1;
  382. }
  383. mutex_unlock(&data->update_lock);
  384. return data;
  385. }
  386. static int __init sensors_adm1021_init(void)
  387. {
  388. return i2c_add_driver(&adm1021_driver);
  389. }
  390. static void __exit sensors_adm1021_exit(void)
  391. {
  392. i2c_del_driver(&adm1021_driver);
  393. }
  394. MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
  395. "Philip Edelbrock <phil@netroedge.com>");
  396. MODULE_DESCRIPTION("adm1021 driver");
  397. MODULE_LICENSE("GPL");
  398. module_param(read_only, bool, 0);
  399. MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
  400. module_init(sensors_adm1021_init)
  401. module_exit(sensors_adm1021_exit)