adm1021.c 13 KB

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