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/i2c-sensor.h>
  24. #include <linux/hwmon.h>
  25. #include <linux/err.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. static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
  32. /* Insmod parameters */
  33. SENSORS_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, 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. #define ADM1021_REG_MAN_ID 0x0FE /* 0x41 = AMD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi*/
  41. #define ADM1021_REG_DEV_ID 0x0FF /* ADM1021 = 0x0X, ADM1023 = 0x3X */
  42. #define ADM1021_REG_DIE_CODE 0x0FF /* MAX1617A */
  43. /* These use different addresses for reading/writing */
  44. #define ADM1021_REG_CONFIG_R 0x03
  45. #define ADM1021_REG_CONFIG_W 0x09
  46. #define ADM1021_REG_CONV_RATE_R 0x04
  47. #define ADM1021_REG_CONV_RATE_W 0x0A
  48. /* These are for the ADM1023's additional precision on the remote temp sensor */
  49. #define ADM1021_REG_REM_TEMP_PREC 0x010
  50. #define ADM1021_REG_REM_OFFSET 0x011
  51. #define ADM1021_REG_REM_OFFSET_PREC 0x012
  52. #define ADM1021_REG_REM_TOS_PREC 0x013
  53. #define ADM1021_REG_REM_THYST_PREC 0x014
  54. /* limits */
  55. #define ADM1021_REG_TOS_R 0x05
  56. #define ADM1021_REG_TOS_W 0x0B
  57. #define ADM1021_REG_REMOTE_TOS_R 0x07
  58. #define ADM1021_REG_REMOTE_TOS_W 0x0D
  59. #define ADM1021_REG_THYST_R 0x06
  60. #define ADM1021_REG_THYST_W 0x0C
  61. #define ADM1021_REG_REMOTE_THYST_R 0x08
  62. #define ADM1021_REG_REMOTE_THYST_W 0x0E
  63. /* write-only */
  64. #define ADM1021_REG_ONESHOT 0x0F
  65. /* Conversions. Rounding and limit checking is only done on the TO_REG
  66. variants. Note that you should be a bit careful with which arguments
  67. these macros are called: arguments may be evaluated more than once.
  68. Fixing this is just not worth it. */
  69. /* Conversions note: 1021 uses normal integer signed-byte format*/
  70. #define TEMP_FROM_REG(val) (val > 127 ? (val-256)*1000 : val*1000)
  71. #define TEMP_TO_REG(val) (SENSORS_LIMIT((val < 0 ? (val/1000)+256 : val/1000),0,255))
  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 semaphore update_lock;
  83. char valid; /* !=0 if following fields are valid */
  84. unsigned long last_updated; /* In jiffies */
  85. u8 temp_max; /* Register values */
  86. u8 temp_hyst;
  87. u8 temp_input;
  88. u8 remote_temp_max;
  89. u8 remote_temp_hyst;
  90. u8 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 int adm1021_read_value(struct i2c_client *client, u8 reg);
  104. static int adm1021_write_value(struct i2c_client *client, u8 reg,
  105. u16 value);
  106. static struct adm1021_data *adm1021_update_device(struct device *dev);
  107. /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
  108. static int read_only = 0;
  109. /* This is the driver that will be inserted */
  110. static struct i2c_driver adm1021_driver = {
  111. .owner = THIS_MODULE,
  112. .name = "adm1021",
  113. .id = I2C_DRIVERID_ADM1021,
  114. .flags = I2C_DF_NOTIFY,
  115. .attach_adapter = adm1021_attach_adapter,
  116. .detach_client = adm1021_detach_client,
  117. };
  118. #define show(value) \
  119. static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
  120. { \
  121. struct adm1021_data *data = adm1021_update_device(dev); \
  122. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
  123. }
  124. show(temp_max);
  125. show(temp_hyst);
  126. show(temp_input);
  127. show(remote_temp_max);
  128. show(remote_temp_hyst);
  129. show(remote_temp_input);
  130. #define show2(value) \
  131. static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
  132. { \
  133. struct adm1021_data *data = adm1021_update_device(dev); \
  134. return sprintf(buf, "%d\n", data->value); \
  135. }
  136. show2(alarms);
  137. #define set(value, reg) \
  138. static ssize_t set_##value(struct device *dev, struct device_attribute *attr, 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. int temp = simple_strtoul(buf, NULL, 10); \
  143. \
  144. down(&data->update_lock); \
  145. data->value = TEMP_TO_REG(temp); \
  146. adm1021_write_value(client, reg, data->value); \
  147. up(&data->update_lock); \
  148. return count; \
  149. }
  150. set(temp_max, ADM1021_REG_TOS_W);
  151. set(temp_hyst, ADM1021_REG_THYST_W);
  152. set(remote_temp_max, ADM1021_REG_REMOTE_TOS_W);
  153. set(remote_temp_hyst, ADM1021_REG_REMOTE_THYST_W);
  154. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
  155. static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
  156. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
  157. static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_remote_temp_max, set_remote_temp_max);
  158. static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_remote_temp_hyst, set_remote_temp_hyst);
  159. static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote_temp_input, NULL);
  160. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  161. static int adm1021_attach_adapter(struct i2c_adapter *adapter)
  162. {
  163. if (!(adapter->class & I2C_CLASS_HWMON))
  164. return 0;
  165. return i2c_detect(adapter, &addr_data, adm1021_detect);
  166. }
  167. static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
  168. {
  169. int i;
  170. struct i2c_client *new_client;
  171. struct adm1021_data *data;
  172. int err = 0;
  173. const char *type_name = "";
  174. /* Make sure we aren't probing the ISA bus!! This is just a safety check
  175. at this moment; i2c_detect really won't call us. */
  176. #ifdef DEBUG
  177. if (i2c_is_isa_adapter(adapter)) {
  178. dev_dbg(&adapter->dev, "adm1021_detect called for an ISA bus adapter?!?\n");
  179. return 0;
  180. }
  181. #endif
  182. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  183. goto error0;
  184. /* OK. For now, we presume we have a valid client. We now create the
  185. client structure, even though we cannot fill it completely yet.
  186. But it allows us to access adm1021_{read,write}_value. */
  187. if (!(data = kmalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
  188. err = -ENOMEM;
  189. goto error0;
  190. }
  191. memset(data, 0, sizeof(struct adm1021_data));
  192. new_client = &data->client;
  193. i2c_set_clientdata(new_client, data);
  194. new_client->addr = address;
  195. new_client->adapter = adapter;
  196. new_client->driver = &adm1021_driver;
  197. new_client->flags = 0;
  198. /* Now, we do the remaining detection. */
  199. if (kind < 0) {
  200. if ((adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0x03) != 0x00
  201. || (adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x3F) != 0x00
  202. || (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) & 0xF8) != 0x00) {
  203. err = -ENODEV;
  204. goto error1;
  205. }
  206. }
  207. /* Determine the chip type. */
  208. if (kind <= 0) {
  209. i = adm1021_read_value(new_client, ADM1021_REG_MAN_ID);
  210. if (i == 0x41)
  211. if ((adm1021_read_value(new_client, ADM1021_REG_DEV_ID) & 0x0F0) == 0x030)
  212. kind = adm1023;
  213. else
  214. kind = adm1021;
  215. else if (i == 0x49)
  216. kind = thmc10;
  217. else if (i == 0x23)
  218. kind = gl523sm;
  219. else if ((i == 0x4d) &&
  220. (adm1021_read_value(new_client, ADM1021_REG_DEV_ID) == 0x01))
  221. kind = max1617a;
  222. else if (i == 0x54)
  223. kind = mc1066;
  224. /* LM84 Mfr ID in a different place, and it has more unused bits */
  225. else if (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) == 0x00
  226. && (kind == 0 /* skip extra detection */
  227. || ((adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x7F) == 0x00
  228. && (adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0xAB) == 0x00)))
  229. kind = lm84;
  230. else
  231. kind = max1617;
  232. }
  233. if (kind == max1617) {
  234. type_name = "max1617";
  235. } else if (kind == max1617a) {
  236. type_name = "max1617a";
  237. } else if (kind == adm1021) {
  238. type_name = "adm1021";
  239. } else if (kind == adm1023) {
  240. type_name = "adm1023";
  241. } else if (kind == thmc10) {
  242. type_name = "thmc10";
  243. } else if (kind == lm84) {
  244. type_name = "lm84";
  245. } else if (kind == gl523sm) {
  246. type_name = "gl523sm";
  247. } else if (kind == mc1066) {
  248. type_name = "mc1066";
  249. }
  250. /* Fill in the remaining client fields and put it into the global list */
  251. strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
  252. data->type = kind;
  253. data->valid = 0;
  254. init_MUTEX(&data->update_lock);
  255. /* Tell the I2C layer a new client has arrived */
  256. if ((err = i2c_attach_client(new_client)))
  257. goto error1;
  258. /* Initialize the ADM1021 chip */
  259. if (kind != lm84)
  260. adm1021_init_client(new_client);
  261. /* Register sysfs hooks */
  262. data->class_dev = hwmon_device_register(&new_client->dev);
  263. if (IS_ERR(data->class_dev)) {
  264. err = PTR_ERR(data->class_dev);
  265. goto error2;
  266. }
  267. device_create_file(&new_client->dev, &dev_attr_temp1_max);
  268. device_create_file(&new_client->dev, &dev_attr_temp1_min);
  269. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  270. device_create_file(&new_client->dev, &dev_attr_temp2_max);
  271. device_create_file(&new_client->dev, &dev_attr_temp2_min);
  272. device_create_file(&new_client->dev, &dev_attr_temp2_input);
  273. device_create_file(&new_client->dev, &dev_attr_alarms);
  274. return 0;
  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. if ((err = i2c_detach_client(client))) {
  296. dev_err(&client->dev, "Client deregistration failed, client not detached.\n");
  297. return err;
  298. }
  299. kfree(data);
  300. return 0;
  301. }
  302. /* All registers are byte-sized */
  303. static int adm1021_read_value(struct i2c_client *client, u8 reg)
  304. {
  305. return i2c_smbus_read_byte_data(client, reg);
  306. }
  307. static int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value)
  308. {
  309. if (!read_only)
  310. return i2c_smbus_write_byte_data(client, reg, value);
  311. return 0;
  312. }
  313. static struct adm1021_data *adm1021_update_device(struct device *dev)
  314. {
  315. struct i2c_client *client = to_i2c_client(dev);
  316. struct adm1021_data *data = i2c_get_clientdata(client);
  317. down(&data->update_lock);
  318. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  319. || !data->valid) {
  320. dev_dbg(&client->dev, "Starting adm1021 update\n");
  321. data->temp_input = adm1021_read_value(client, ADM1021_REG_TEMP);
  322. data->temp_max = adm1021_read_value(client, ADM1021_REG_TOS_R);
  323. data->temp_hyst = adm1021_read_value(client, ADM1021_REG_THYST_R);
  324. data->remote_temp_input = adm1021_read_value(client, ADM1021_REG_REMOTE_TEMP);
  325. data->remote_temp_max = adm1021_read_value(client, ADM1021_REG_REMOTE_TOS_R);
  326. data->remote_temp_hyst = adm1021_read_value(client, ADM1021_REG_REMOTE_THYST_R);
  327. data->alarms = adm1021_read_value(client, ADM1021_REG_STATUS) & 0x7c;
  328. if (data->type == adm1023) {
  329. data->remote_temp_prec = adm1021_read_value(client, ADM1021_REG_REM_TEMP_PREC);
  330. data->remote_temp_os_prec = adm1021_read_value(client, ADM1021_REG_REM_TOS_PREC);
  331. data->remote_temp_hyst_prec = adm1021_read_value(client, ADM1021_REG_REM_THYST_PREC);
  332. data->remote_temp_offset = adm1021_read_value(client, ADM1021_REG_REM_OFFSET);
  333. data->remote_temp_offset_prec = adm1021_read_value(client, ADM1021_REG_REM_OFFSET_PREC);
  334. }
  335. data->last_updated = jiffies;
  336. data->valid = 1;
  337. }
  338. up(&data->update_lock);
  339. return data;
  340. }
  341. static int __init sensors_adm1021_init(void)
  342. {
  343. return i2c_add_driver(&adm1021_driver);
  344. }
  345. static void __exit sensors_adm1021_exit(void)
  346. {
  347. i2c_del_driver(&adm1021_driver);
  348. }
  349. MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
  350. "Philip Edelbrock <phil@netroedge.com>");
  351. MODULE_DESCRIPTION("adm1021 driver");
  352. MODULE_LICENSE("GPL");
  353. module_param(read_only, bool, 0);
  354. MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
  355. module_init(sensors_adm1021_init)
  356. module_exit(sensors_adm1021_exit)