adm1021.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/i2c.h>
  26. #include <linux/hwmon.h>
  27. #include <linux/hwmon-sysfs.h>
  28. #include <linux/err.h>
  29. #include <linux/mutex.h>
  30. /* Addresses to scan */
  31. static const unsigned short normal_i2c[] = {
  32. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  33. enum chips {
  34. adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, 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. /*
  66. * Note: Even though I left the low and high limits named os and hyst,
  67. * they don't quite work like a thermostat the way the LM75 does. I.e.,
  68. * a lower temp than THYST actually triggers an alarm instead of
  69. * clearing it. Weird, ey? --Phil
  70. */
  71. /* Each client has this additional data */
  72. struct adm1021_data {
  73. struct device *hwmon_dev;
  74. enum chips type;
  75. struct mutex update_lock;
  76. char valid; /* !=0 if following fields are valid */
  77. char low_power; /* !=0 if device in low power mode */
  78. unsigned long last_updated; /* In jiffies */
  79. int temp_max[2]; /* Register values */
  80. int temp_min[2];
  81. int temp[2];
  82. u8 alarms;
  83. /* Special values for ADM1023 only */
  84. u8 remote_temp_offset;
  85. u8 remote_temp_offset_prec;
  86. };
  87. static int adm1021_probe(struct i2c_client *client,
  88. const struct i2c_device_id *id);
  89. static int adm1021_detect(struct i2c_client *client,
  90. struct i2c_board_info *info);
  91. static void adm1021_init_client(struct i2c_client *client);
  92. static int adm1021_remove(struct i2c_client *client);
  93. static struct adm1021_data *adm1021_update_device(struct device *dev);
  94. /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
  95. static bool read_only;
  96. static const struct i2c_device_id adm1021_id[] = {
  97. { "adm1021", adm1021 },
  98. { "adm1023", adm1023 },
  99. { "max1617", max1617 },
  100. { "max1617a", max1617a },
  101. { "thmc10", thmc10 },
  102. { "lm84", lm84 },
  103. { "gl523sm", gl523sm },
  104. { "mc1066", mc1066 },
  105. { }
  106. };
  107. MODULE_DEVICE_TABLE(i2c, adm1021_id);
  108. /* This is the driver that will be inserted */
  109. static struct i2c_driver adm1021_driver = {
  110. .class = I2C_CLASS_HWMON,
  111. .driver = {
  112. .name = "adm1021",
  113. },
  114. .probe = adm1021_probe,
  115. .remove = adm1021_remove,
  116. .id_table = adm1021_id,
  117. .detect = adm1021_detect,
  118. .address_list = normal_i2c,
  119. };
  120. static ssize_t show_temp(struct device *dev,
  121. struct device_attribute *devattr, char *buf)
  122. {
  123. int index = to_sensor_dev_attr(devattr)->index;
  124. struct adm1021_data *data = adm1021_update_device(dev);
  125. return sprintf(buf, "%d\n", data->temp[index]);
  126. }
  127. static ssize_t show_temp_max(struct device *dev,
  128. struct device_attribute *devattr, char *buf)
  129. {
  130. int index = to_sensor_dev_attr(devattr)->index;
  131. struct adm1021_data *data = adm1021_update_device(dev);
  132. return sprintf(buf, "%d\n", data->temp_max[index]);
  133. }
  134. static ssize_t show_temp_min(struct device *dev,
  135. struct device_attribute *devattr, char *buf)
  136. {
  137. int index = to_sensor_dev_attr(devattr)->index;
  138. struct adm1021_data *data = adm1021_update_device(dev);
  139. return sprintf(buf, "%d\n", data->temp_min[index]);
  140. }
  141. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  142. char *buf)
  143. {
  144. int index = to_sensor_dev_attr(attr)->index;
  145. struct adm1021_data *data = adm1021_update_device(dev);
  146. return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
  147. }
  148. static ssize_t show_alarms(struct device *dev,
  149. struct device_attribute *attr,
  150. char *buf)
  151. {
  152. struct adm1021_data *data = adm1021_update_device(dev);
  153. return sprintf(buf, "%u\n", data->alarms);
  154. }
  155. static ssize_t set_temp_max(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;
  163. int err;
  164. err = kstrtol(buf, 10, &temp);
  165. if (err)
  166. return err;
  167. temp /= 1000;
  168. mutex_lock(&data->update_lock);
  169. data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
  170. if (!read_only)
  171. i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
  172. data->temp_max[index]);
  173. mutex_unlock(&data->update_lock);
  174. return count;
  175. }
  176. static ssize_t set_temp_min(struct device *dev,
  177. struct device_attribute *devattr,
  178. const char *buf, size_t count)
  179. {
  180. int index = to_sensor_dev_attr(devattr)->index;
  181. struct i2c_client *client = to_i2c_client(dev);
  182. struct adm1021_data *data = i2c_get_clientdata(client);
  183. long temp;
  184. int err;
  185. err = kstrtol(buf, 10, &temp);
  186. if (err)
  187. return err;
  188. temp /= 1000;
  189. mutex_lock(&data->update_lock);
  190. data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
  191. if (!read_only)
  192. i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
  193. data->temp_min[index]);
  194. mutex_unlock(&data->update_lock);
  195. return count;
  196. }
  197. static ssize_t show_low_power(struct device *dev,
  198. struct device_attribute *devattr, char *buf)
  199. {
  200. struct adm1021_data *data = adm1021_update_device(dev);
  201. return sprintf(buf, "%d\n", data->low_power);
  202. }
  203. static ssize_t set_low_power(struct device *dev,
  204. struct device_attribute *devattr,
  205. const char *buf, size_t count)
  206. {
  207. struct i2c_client *client = to_i2c_client(dev);
  208. struct adm1021_data *data = i2c_get_clientdata(client);
  209. char low_power;
  210. unsigned long val;
  211. int err;
  212. err = kstrtoul(buf, 10, &val);
  213. if (err)
  214. return err;
  215. low_power = val != 0;
  216. mutex_lock(&data->update_lock);
  217. if (low_power != data->low_power) {
  218. int config = i2c_smbus_read_byte_data(
  219. client, ADM1021_REG_CONFIG_R);
  220. data->low_power = low_power;
  221. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  222. (config & 0xBF) | (low_power << 6));
  223. }
  224. mutex_unlock(&data->update_lock);
  225. return count;
  226. }
  227. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  228. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  229. set_temp_max, 0);
  230. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
  231. set_temp_min, 0);
  232. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  233. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  234. set_temp_max, 1);
  235. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
  236. set_temp_min, 1);
  237. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  238. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  239. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  240. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  241. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  242. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  243. static DEVICE_ATTR(low_power, S_IWUSR | S_IRUGO, show_low_power, set_low_power);
  244. static struct attribute *adm1021_attributes[] = {
  245. &sensor_dev_attr_temp1_max.dev_attr.attr,
  246. &sensor_dev_attr_temp1_min.dev_attr.attr,
  247. &sensor_dev_attr_temp1_input.dev_attr.attr,
  248. &sensor_dev_attr_temp2_max.dev_attr.attr,
  249. &sensor_dev_attr_temp2_min.dev_attr.attr,
  250. &sensor_dev_attr_temp2_input.dev_attr.attr,
  251. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  252. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  253. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  254. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  255. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  256. &dev_attr_alarms.attr,
  257. &dev_attr_low_power.attr,
  258. NULL
  259. };
  260. static const struct attribute_group adm1021_group = {
  261. .attrs = adm1021_attributes,
  262. };
  263. /* Return 0 if detection is successful, -ENODEV otherwise */
  264. static int adm1021_detect(struct i2c_client *client,
  265. struct i2c_board_info *info)
  266. {
  267. struct i2c_adapter *adapter = client->adapter;
  268. const char *type_name;
  269. int conv_rate, status, config, man_id, dev_id;
  270. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  271. pr_debug("adm1021: detect failed, "
  272. "smbus byte data not supported!\n");
  273. return -ENODEV;
  274. }
  275. status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
  276. conv_rate = i2c_smbus_read_byte_data(client,
  277. ADM1021_REG_CONV_RATE_R);
  278. config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
  279. /* Check unused bits */
  280. if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
  281. pr_debug("adm1021: detect failed, chip not detected!\n");
  282. return -ENODEV;
  283. }
  284. /* Determine the chip type. */
  285. man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
  286. dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
  287. if (man_id < 0 || dev_id < 0)
  288. return -ENODEV;
  289. if (man_id == 0x4d && dev_id == 0x01)
  290. type_name = "max1617a";
  291. else if (man_id == 0x41) {
  292. if ((dev_id & 0xF0) == 0x30)
  293. type_name = "adm1023";
  294. else if ((dev_id & 0xF0) == 0x00)
  295. type_name = "adm1021";
  296. else
  297. return -ENODEV;
  298. } else if (man_id == 0x49)
  299. type_name = "thmc10";
  300. else if (man_id == 0x23)
  301. type_name = "gl523sm";
  302. else if (man_id == 0x54)
  303. type_name = "mc1066";
  304. else {
  305. int lte, rte, lhi, rhi, llo, rlo;
  306. /* extra checks for LM84 and MAX1617 to avoid misdetections */
  307. llo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(0));
  308. rlo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(1));
  309. /* fail if any of the additional register reads failed */
  310. if (llo < 0 || rlo < 0)
  311. return -ENODEV;
  312. lte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(0));
  313. rte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(1));
  314. lhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(0));
  315. rhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(1));
  316. /*
  317. * Fail for negative temperatures and negative high limits.
  318. * This check also catches read errors on the tested registers.
  319. */
  320. if ((s8)lte < 0 || (s8)rte < 0 || (s8)lhi < 0 || (s8)rhi < 0)
  321. return -ENODEV;
  322. /* fail if all registers hold the same value */
  323. if (lte == rte && lte == lhi && lte == rhi && lte == llo
  324. && lte == rlo)
  325. return -ENODEV;
  326. /*
  327. * LM84 Mfr ID is in a different place,
  328. * and it has more unused bits.
  329. */
  330. if (conv_rate == 0x00
  331. && (config & 0x7F) == 0x00
  332. && (status & 0xAB) == 0x00) {
  333. type_name = "lm84";
  334. } else {
  335. /* fail if low limits are larger than high limits */
  336. if ((s8)llo > lhi || (s8)rlo > rhi)
  337. return -ENODEV;
  338. type_name = "max1617";
  339. }
  340. }
  341. pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
  342. type_name, i2c_adapter_id(adapter), client->addr);
  343. strlcpy(info->type, type_name, I2C_NAME_SIZE);
  344. return 0;
  345. }
  346. static int adm1021_probe(struct i2c_client *client,
  347. const struct i2c_device_id *id)
  348. {
  349. struct adm1021_data *data;
  350. int err;
  351. data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL);
  352. if (!data) {
  353. pr_debug("adm1021: detect failed, kzalloc failed!\n");
  354. err = -ENOMEM;
  355. goto error0;
  356. }
  357. i2c_set_clientdata(client, data);
  358. data->type = id->driver_data;
  359. mutex_init(&data->update_lock);
  360. /* Initialize the ADM1021 chip */
  361. if (data->type != lm84 && !read_only)
  362. adm1021_init_client(client);
  363. /* Register sysfs hooks */
  364. err = sysfs_create_group(&client->dev.kobj, &adm1021_group);
  365. if (err)
  366. goto error1;
  367. data->hwmon_dev = hwmon_device_register(&client->dev);
  368. if (IS_ERR(data->hwmon_dev)) {
  369. err = PTR_ERR(data->hwmon_dev);
  370. goto error3;
  371. }
  372. return 0;
  373. error3:
  374. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  375. error1:
  376. kfree(data);
  377. error0:
  378. return err;
  379. }
  380. static void adm1021_init_client(struct i2c_client *client)
  381. {
  382. /* Enable ADC and disable suspend mode */
  383. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  384. i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
  385. /* Set Conversion rate to 1/sec (this can be tinkered with) */
  386. i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
  387. }
  388. static int adm1021_remove(struct i2c_client *client)
  389. {
  390. struct adm1021_data *data = i2c_get_clientdata(client);
  391. hwmon_device_unregister(data->hwmon_dev);
  392. sysfs_remove_group(&client->dev.kobj, &adm1021_group);
  393. kfree(data);
  394. return 0;
  395. }
  396. static struct adm1021_data *adm1021_update_device(struct device *dev)
  397. {
  398. struct i2c_client *client = to_i2c_client(dev);
  399. struct adm1021_data *data = i2c_get_clientdata(client);
  400. mutex_lock(&data->update_lock);
  401. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  402. || !data->valid) {
  403. int i;
  404. dev_dbg(&client->dev, "Starting adm1021 update\n");
  405. for (i = 0; i < 2; i++) {
  406. data->temp[i] = 1000 *
  407. (s8) i2c_smbus_read_byte_data(
  408. client, ADM1021_REG_TEMP(i));
  409. data->temp_max[i] = 1000 *
  410. (s8) i2c_smbus_read_byte_data(
  411. client, ADM1021_REG_TOS_R(i));
  412. data->temp_min[i] = 1000 *
  413. (s8) i2c_smbus_read_byte_data(
  414. client, ADM1021_REG_THYST_R(i));
  415. }
  416. data->alarms = i2c_smbus_read_byte_data(client,
  417. ADM1021_REG_STATUS) & 0x7c;
  418. if (data->type == adm1023) {
  419. /*
  420. * The ADM1023 provides 3 extra bits of precision for
  421. * the remote sensor in extra registers.
  422. */
  423. data->temp[1] += 125 * (i2c_smbus_read_byte_data(
  424. client, ADM1023_REG_REM_TEMP_PREC) >> 5);
  425. data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
  426. client, ADM1023_REG_REM_TOS_PREC) >> 5);
  427. data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
  428. client, ADM1023_REG_REM_THYST_PREC) >> 5);
  429. data->remote_temp_offset =
  430. i2c_smbus_read_byte_data(client,
  431. ADM1023_REG_REM_OFFSET);
  432. data->remote_temp_offset_prec =
  433. i2c_smbus_read_byte_data(client,
  434. ADM1023_REG_REM_OFFSET_PREC);
  435. }
  436. data->last_updated = jiffies;
  437. data->valid = 1;
  438. }
  439. mutex_unlock(&data->update_lock);
  440. return data;
  441. }
  442. module_i2c_driver(adm1021_driver);
  443. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  444. "Philip Edelbrock <phil@netroedge.com>");
  445. MODULE_DESCRIPTION("adm1021 driver");
  446. MODULE_LICENSE("GPL");
  447. module_param(read_only, bool, 0);
  448. MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");