adm1021.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. .id = I2C_DRIVERID_ADM1021,
  101. .attach_adapter = adm1021_attach_adapter,
  102. .detach_client = adm1021_detach_client,
  103. };
  104. static ssize_t show_temp(struct device *dev,
  105. struct device_attribute *devattr, char *buf)
  106. {
  107. int index = to_sensor_dev_attr(devattr)->index;
  108. struct adm1021_data *data = adm1021_update_device(dev);
  109. return sprintf(buf, "%d\n", 1000 * data->temp[index]);
  110. }
  111. static ssize_t show_temp_max(struct device *dev,
  112. struct device_attribute *devattr, char *buf)
  113. {
  114. int index = to_sensor_dev_attr(devattr)->index;
  115. struct adm1021_data *data = adm1021_update_device(dev);
  116. return sprintf(buf, "%d\n", 1000 * data->temp_max[index]);
  117. }
  118. static ssize_t show_temp_min(struct device *dev,
  119. struct device_attribute *devattr, char *buf)
  120. {
  121. int index = to_sensor_dev_attr(devattr)->index;
  122. struct adm1021_data *data = adm1021_update_device(dev);
  123. return sprintf(buf, "%d\n", 1000 * data->temp_min[index]);
  124. }
  125. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  126. char *buf)
  127. {
  128. int index = to_sensor_dev_attr(attr)->index;
  129. struct adm1021_data *data = adm1021_update_device(dev);
  130. return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
  131. }
  132. static ssize_t show_alarms(struct device *dev,
  133. struct device_attribute *attr,
  134. char *buf)
  135. {
  136. struct adm1021_data *data = adm1021_update_device(dev);
  137. return sprintf(buf, "%u\n", data->alarms);
  138. }
  139. static ssize_t set_temp_max(struct device *dev,
  140. struct device_attribute *devattr,
  141. const char *buf, size_t count)
  142. {
  143. int index = to_sensor_dev_attr(devattr)->index;
  144. struct i2c_client *client = to_i2c_client(dev);
  145. struct adm1021_data *data = i2c_get_clientdata(client);
  146. long temp = simple_strtol(buf, NULL, 10) / 1000;
  147. mutex_lock(&data->update_lock);
  148. data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
  149. if (!read_only)
  150. i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
  151. data->temp_max[index]);
  152. mutex_unlock(&data->update_lock);
  153. return count;
  154. }
  155. static ssize_t set_temp_min(struct device *dev,
  156. struct device_attribute *devattr,
  157. const char *buf, size_t count)
  158. {
  159. int index = to_sensor_dev_attr(devattr)->index;
  160. struct i2c_client *client = to_i2c_client(dev);
  161. struct adm1021_data *data = i2c_get_clientdata(client);
  162. long temp = simple_strtol(buf, NULL, 10) / 1000;
  163. mutex_lock(&data->update_lock);
  164. data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
  165. if (!read_only)
  166. i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
  167. data->temp_min[index]);
  168. mutex_unlock(&data->update_lock);
  169. return count;
  170. }
  171. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  172. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  173. set_temp_max, 0);
  174. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
  175. set_temp_min, 0);
  176. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  177. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  178. set_temp_max, 1);
  179. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
  180. set_temp_min, 1);
  181. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  182. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  183. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  184. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  185. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  186. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  187. static int adm1021_attach_adapter(struct i2c_adapter *adapter)
  188. {
  189. if (!(adapter->class & I2C_CLASS_HWMON))
  190. return 0;
  191. return i2c_probe(adapter, &addr_data, adm1021_detect);
  192. }
  193. static struct attribute *adm1021_attributes[] = {
  194. &sensor_dev_attr_temp1_max.dev_attr.attr,
  195. &sensor_dev_attr_temp1_min.dev_attr.attr,
  196. &sensor_dev_attr_temp1_input.dev_attr.attr,
  197. &sensor_dev_attr_temp2_max.dev_attr.attr,
  198. &sensor_dev_attr_temp2_min.dev_attr.attr,
  199. &sensor_dev_attr_temp2_input.dev_attr.attr,
  200. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  201. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  202. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  203. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  204. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  205. &dev_attr_alarms.attr,
  206. NULL
  207. };
  208. static const struct attribute_group adm1021_group = {
  209. .attrs = adm1021_attributes,
  210. };
  211. static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
  212. {
  213. int i;
  214. struct i2c_client *client;
  215. struct adm1021_data *data;
  216. int err = 0;
  217. const char *type_name = "";
  218. int conv_rate, status, config;
  219. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  220. pr_debug("adm1021: detect failed, "
  221. "smbus byte data not supported!\n");
  222. goto error0;
  223. }
  224. /* OK. For now, we presume we have a valid client. We now create the
  225. client structure, even though we cannot fill it completely yet.
  226. But it allows us to access adm1021 register values. */
  227. if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
  228. pr_debug("adm1021: detect failed, kzalloc failed!\n");
  229. err = -ENOMEM;
  230. goto error0;
  231. }
  232. client = &data->client;
  233. i2c_set_clientdata(client, data);
  234. client->addr = address;
  235. client->adapter = adapter;
  236. client->driver = &adm1021_driver;
  237. status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
  238. conv_rate = i2c_smbus_read_byte_data(client,
  239. ADM1021_REG_CONV_RATE_R);
  240. config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
  241. /* Now, we do the remaining detection. */
  242. if (kind < 0) {
  243. if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
  244. || (conv_rate & 0xF8) != 0x00) {
  245. pr_debug("adm1021: detect failed, "
  246. "chip not detected!\n");
  247. err = -ENODEV;
  248. goto error1;
  249. }
  250. }
  251. /* Determine the chip type. */
  252. if (kind <= 0) {
  253. i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
  254. if (i == 0x41)
  255. if ((i2c_smbus_read_byte_data(client,
  256. ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
  257. kind = adm1023;
  258. else
  259. kind = adm1021;
  260. else if (i == 0x49)
  261. kind = thmc10;
  262. else if (i == 0x23)
  263. kind = gl523sm;
  264. else if ((i == 0x4d) &&
  265. (i2c_smbus_read_byte_data(client,
  266. ADM1021_REG_DEV_ID) == 0x01))
  267. kind = max1617a;
  268. else if (i == 0x54)
  269. kind = mc1066;
  270. /* LM84 Mfr ID in a different place, and it has more unused bits */
  271. else if (conv_rate == 0x00
  272. && (kind == 0 /* skip extra detection */
  273. || ((config & 0x7F) == 0x00
  274. && (status & 0xAB) == 0x00)))
  275. kind = lm84;
  276. else
  277. kind = max1617;
  278. }
  279. if (kind == max1617) {
  280. type_name = "max1617";
  281. } else if (kind == max1617a) {
  282. type_name = "max1617a";
  283. } else if (kind == adm1021) {
  284. type_name = "adm1021";
  285. } else if (kind == adm1023) {
  286. type_name = "adm1023";
  287. } else if (kind == thmc10) {
  288. type_name = "thmc10";
  289. } else if (kind == lm84) {
  290. type_name = "lm84";
  291. } else if (kind == gl523sm) {
  292. type_name = "gl523sm";
  293. } else if (kind == mc1066) {
  294. type_name = "mc1066";
  295. }
  296. pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
  297. type_name, i2c_adapter_id(adapter), address);
  298. /* Fill in the remaining client fields */
  299. strlcpy(client->name, type_name, I2C_NAME_SIZE);
  300. data->type = kind;
  301. mutex_init(&data->update_lock);
  302. /* Tell the I2C layer a new client has arrived */
  303. if ((err = i2c_attach_client(client)))
  304. goto error1;
  305. /* Initialize the ADM1021 chip */
  306. if (kind != 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 error2;
  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. error2:
  320. i2c_detach_client(client);
  321. error1:
  322. kfree(data);
  323. error0:
  324. return err;
  325. }
  326. static void adm1021_init_client(struct i2c_client *client)
  327. {
  328. /* Enable ADC and disable suspend mode */
  329. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  330. i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
  331. /* Set Conversion rate to 1/sec (this can be tinkered with) */
  332. i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
  333. }
  334. static int adm1021_detach_client(struct i2c_client *client)
  335. {
  336. struct adm1021_data *data = i2c_get_clientdata(client);
  337. int err;
  338. hwmon_device_unregister(data->hwmon_dev);
  339. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  340. if ((err = i2c_detach_client(client)))
  341. return err;
  342. kfree(data);
  343. return 0;
  344. }
  345. static struct adm1021_data *adm1021_update_device(struct device *dev)
  346. {
  347. struct i2c_client *client = to_i2c_client(dev);
  348. struct adm1021_data *data = i2c_get_clientdata(client);
  349. mutex_lock(&data->update_lock);
  350. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  351. || !data->valid) {
  352. int i;
  353. dev_dbg(&client->dev, "Starting adm1021 update\n");
  354. for (i = 0; i < 2; i++) {
  355. data->temp[i] = i2c_smbus_read_byte_data(client,
  356. ADM1021_REG_TEMP(i));
  357. data->temp_max[i] = i2c_smbus_read_byte_data(client,
  358. ADM1021_REG_TOS_R(i));
  359. data->temp_min[i] = i2c_smbus_read_byte_data(client,
  360. ADM1021_REG_THYST_R(i));
  361. }
  362. data->alarms = i2c_smbus_read_byte_data(client,
  363. ADM1021_REG_STATUS) & 0x7c;
  364. if (data->type == adm1023) {
  365. data->remote_temp_prec =
  366. i2c_smbus_read_byte_data(client,
  367. ADM1023_REG_REM_TEMP_PREC);
  368. data->remote_temp_os_prec =
  369. i2c_smbus_read_byte_data(client,
  370. ADM1023_REG_REM_TOS_PREC);
  371. data->remote_temp_hyst_prec =
  372. i2c_smbus_read_byte_data(client,
  373. ADM1023_REG_REM_THYST_PREC);
  374. data->remote_temp_offset =
  375. i2c_smbus_read_byte_data(client,
  376. ADM1023_REG_REM_OFFSET);
  377. data->remote_temp_offset_prec =
  378. i2c_smbus_read_byte_data(client,
  379. ADM1023_REG_REM_OFFSET_PREC);
  380. }
  381. data->last_updated = jiffies;
  382. data->valid = 1;
  383. }
  384. mutex_unlock(&data->update_lock);
  385. return data;
  386. }
  387. static int __init sensors_adm1021_init(void)
  388. {
  389. return i2c_add_driver(&adm1021_driver);
  390. }
  391. static void __exit sensors_adm1021_exit(void)
  392. {
  393. i2c_del_driver(&adm1021_driver);
  394. }
  395. MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
  396. "Philip Edelbrock <phil@netroedge.com>");
  397. MODULE_DESCRIPTION("adm1021 driver");
  398. MODULE_LICENSE("GPL");
  399. module_param(read_only, bool, 0);
  400. MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
  401. module_init(sensors_adm1021_init)
  402. module_exit(sensors_adm1021_exit)