adm1021.c 13 KB

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