adm1021.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 struct attribute *adm1021_attributes[] = {
  167. &dev_attr_temp1_max.attr,
  168. &dev_attr_temp1_min.attr,
  169. &dev_attr_temp1_input.attr,
  170. &dev_attr_temp2_max.attr,
  171. &dev_attr_temp2_min.attr,
  172. &dev_attr_temp2_input.attr,
  173. &dev_attr_alarms.attr,
  174. NULL
  175. };
  176. static const struct attribute_group adm1021_group = {
  177. .attrs = adm1021_attributes,
  178. };
  179. static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
  180. {
  181. int i;
  182. struct i2c_client *new_client;
  183. struct adm1021_data *data;
  184. int err = 0;
  185. const char *type_name = "";
  186. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  187. goto error0;
  188. /* OK. For now, we presume we have a valid client. We now create the
  189. client structure, even though we cannot fill it completely yet.
  190. But it allows us to access adm1021_{read,write}_value. */
  191. if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
  192. err = -ENOMEM;
  193. goto error0;
  194. }
  195. new_client = &data->client;
  196. i2c_set_clientdata(new_client, data);
  197. new_client->addr = address;
  198. new_client->adapter = adapter;
  199. new_client->driver = &adm1021_driver;
  200. new_client->flags = 0;
  201. /* Now, we do the remaining detection. */
  202. if (kind < 0) {
  203. if ((adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0x03) != 0x00
  204. || (adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x3F) != 0x00
  205. || (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) & 0xF8) != 0x00) {
  206. err = -ENODEV;
  207. goto error1;
  208. }
  209. }
  210. /* Determine the chip type. */
  211. if (kind <= 0) {
  212. i = adm1021_read_value(new_client, ADM1021_REG_MAN_ID);
  213. if (i == 0x41)
  214. if ((adm1021_read_value(new_client, ADM1021_REG_DEV_ID) & 0x0F0) == 0x030)
  215. kind = adm1023;
  216. else
  217. kind = adm1021;
  218. else if (i == 0x49)
  219. kind = thmc10;
  220. else if (i == 0x23)
  221. kind = gl523sm;
  222. else if ((i == 0x4d) &&
  223. (adm1021_read_value(new_client, ADM1021_REG_DEV_ID) == 0x01))
  224. kind = max1617a;
  225. else if (i == 0x54)
  226. kind = mc1066;
  227. /* LM84 Mfr ID in a different place, and it has more unused bits */
  228. else if (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) == 0x00
  229. && (kind == 0 /* skip extra detection */
  230. || ((adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x7F) == 0x00
  231. && (adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0xAB) == 0x00)))
  232. kind = lm84;
  233. else
  234. kind = max1617;
  235. }
  236. if (kind == max1617) {
  237. type_name = "max1617";
  238. } else if (kind == max1617a) {
  239. type_name = "max1617a";
  240. } else if (kind == adm1021) {
  241. type_name = "adm1021";
  242. } else if (kind == adm1023) {
  243. type_name = "adm1023";
  244. } else if (kind == thmc10) {
  245. type_name = "thmc10";
  246. } else if (kind == lm84) {
  247. type_name = "lm84";
  248. } else if (kind == gl523sm) {
  249. type_name = "gl523sm";
  250. } else if (kind == mc1066) {
  251. type_name = "mc1066";
  252. }
  253. /* Fill in the remaining client fields and put it into the global list */
  254. strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
  255. data->type = kind;
  256. data->valid = 0;
  257. mutex_init(&data->update_lock);
  258. /* Tell the I2C layer a new client has arrived */
  259. if ((err = i2c_attach_client(new_client)))
  260. goto error1;
  261. /* Initialize the ADM1021 chip */
  262. if (kind != lm84)
  263. adm1021_init_client(new_client);
  264. /* Register sysfs hooks */
  265. if ((err = sysfs_create_group(&new_client->dev.kobj, &adm1021_group)))
  266. goto error2;
  267. data->class_dev = hwmon_device_register(&new_client->dev);
  268. if (IS_ERR(data->class_dev)) {
  269. err = PTR_ERR(data->class_dev);
  270. goto error3;
  271. }
  272. return 0;
  273. error3:
  274. sysfs_remove_group(&new_client->dev.kobj, &adm1021_group);
  275. error2:
  276. i2c_detach_client(new_client);
  277. error1:
  278. kfree(data);
  279. error0:
  280. return err;
  281. }
  282. static void adm1021_init_client(struct i2c_client *client)
  283. {
  284. /* Enable ADC and disable suspend mode */
  285. adm1021_write_value(client, ADM1021_REG_CONFIG_W,
  286. adm1021_read_value(client, ADM1021_REG_CONFIG_R) & 0xBF);
  287. /* Set Conversion rate to 1/sec (this can be tinkered with) */
  288. adm1021_write_value(client, ADM1021_REG_CONV_RATE_W, 0x04);
  289. }
  290. static int adm1021_detach_client(struct i2c_client *client)
  291. {
  292. struct adm1021_data *data = i2c_get_clientdata(client);
  293. int err;
  294. hwmon_device_unregister(data->class_dev);
  295. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  296. if ((err = i2c_detach_client(client)))
  297. return err;
  298. kfree(data);
  299. return 0;
  300. }
  301. /* All registers are byte-sized */
  302. static int adm1021_read_value(struct i2c_client *client, u8 reg)
  303. {
  304. return i2c_smbus_read_byte_data(client, reg);
  305. }
  306. static int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value)
  307. {
  308. if (!read_only)
  309. return i2c_smbus_write_byte_data(client, reg, value);
  310. return 0;
  311. }
  312. static struct adm1021_data *adm1021_update_device(struct device *dev)
  313. {
  314. struct i2c_client *client = to_i2c_client(dev);
  315. struct adm1021_data *data = i2c_get_clientdata(client);
  316. mutex_lock(&data->update_lock);
  317. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  318. || !data->valid) {
  319. dev_dbg(&client->dev, "Starting adm1021 update\n");
  320. data->temp_input = adm1021_read_value(client, ADM1021_REG_TEMP);
  321. data->temp_max = adm1021_read_value(client, ADM1021_REG_TOS_R);
  322. data->temp_hyst = adm1021_read_value(client, ADM1021_REG_THYST_R);
  323. data->remote_temp_input = adm1021_read_value(client, ADM1021_REG_REMOTE_TEMP);
  324. data->remote_temp_max = adm1021_read_value(client, ADM1021_REG_REMOTE_TOS_R);
  325. data->remote_temp_hyst = adm1021_read_value(client, ADM1021_REG_REMOTE_THYST_R);
  326. data->alarms = adm1021_read_value(client, ADM1021_REG_STATUS) & 0x7c;
  327. if (data->type == adm1023) {
  328. data->remote_temp_prec = adm1021_read_value(client, ADM1021_REG_REM_TEMP_PREC);
  329. data->remote_temp_os_prec = adm1021_read_value(client, ADM1021_REG_REM_TOS_PREC);
  330. data->remote_temp_hyst_prec = adm1021_read_value(client, ADM1021_REG_REM_THYST_PREC);
  331. data->remote_temp_offset = adm1021_read_value(client, ADM1021_REG_REM_OFFSET);
  332. data->remote_temp_offset_prec = adm1021_read_value(client, ADM1021_REG_REM_OFFSET_PREC);
  333. }
  334. data->last_updated = jiffies;
  335. data->valid = 1;
  336. }
  337. mutex_unlock(&data->update_lock);
  338. return data;
  339. }
  340. static int __init sensors_adm1021_init(void)
  341. {
  342. return i2c_add_driver(&adm1021_driver);
  343. }
  344. static void __exit sensors_adm1021_exit(void)
  345. {
  346. i2c_del_driver(&adm1021_driver);
  347. }
  348. MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
  349. "Philip Edelbrock <phil@netroedge.com>");
  350. MODULE_DESCRIPTION("adm1021 driver");
  351. MODULE_LICENSE("GPL");
  352. module_param(read_only, bool, 0);
  353. MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
  354. module_init(sensors_adm1021_init)
  355. module_exit(sensors_adm1021_exit)