adm1021.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 i2c_client *client;
  74. enum chips type;
  75. const struct attribute_group *groups[3];
  76. struct mutex update_lock;
  77. char valid; /* !=0 if following fields are valid */
  78. char low_power; /* !=0 if device in low power mode */
  79. unsigned long last_updated; /* In jiffies */
  80. int temp_max[2]; /* Register values */
  81. int temp_min[2];
  82. int temp[2];
  83. u8 alarms;
  84. /* Special values for ADM1023 only */
  85. u8 remote_temp_offset;
  86. u8 remote_temp_offset_prec;
  87. };
  88. /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
  89. static bool read_only;
  90. static struct adm1021_data *adm1021_update_device(struct device *dev)
  91. {
  92. struct adm1021_data *data = dev_get_drvdata(dev);
  93. struct i2c_client *client = data->client;
  94. mutex_lock(&data->update_lock);
  95. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  96. || !data->valid) {
  97. int i;
  98. dev_dbg(dev, "Starting adm1021 update\n");
  99. for (i = 0; i < 2; i++) {
  100. data->temp[i] = 1000 *
  101. (s8) i2c_smbus_read_byte_data(
  102. client, ADM1021_REG_TEMP(i));
  103. data->temp_max[i] = 1000 *
  104. (s8) i2c_smbus_read_byte_data(
  105. client, ADM1021_REG_TOS_R(i));
  106. if (data->type != lm84) {
  107. data->temp_min[i] = 1000 *
  108. (s8) i2c_smbus_read_byte_data(client,
  109. ADM1021_REG_THYST_R(i));
  110. }
  111. }
  112. data->alarms = i2c_smbus_read_byte_data(client,
  113. ADM1021_REG_STATUS) & 0x7c;
  114. if (data->type == adm1023) {
  115. /*
  116. * The ADM1023 provides 3 extra bits of precision for
  117. * the remote sensor in extra registers.
  118. */
  119. data->temp[1] += 125 * (i2c_smbus_read_byte_data(
  120. client, ADM1023_REG_REM_TEMP_PREC) >> 5);
  121. data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
  122. client, ADM1023_REG_REM_TOS_PREC) >> 5);
  123. data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
  124. client, ADM1023_REG_REM_THYST_PREC) >> 5);
  125. data->remote_temp_offset =
  126. i2c_smbus_read_byte_data(client,
  127. ADM1023_REG_REM_OFFSET);
  128. data->remote_temp_offset_prec =
  129. i2c_smbus_read_byte_data(client,
  130. ADM1023_REG_REM_OFFSET_PREC);
  131. }
  132. data->last_updated = jiffies;
  133. data->valid = 1;
  134. }
  135. mutex_unlock(&data->update_lock);
  136. return data;
  137. }
  138. static ssize_t show_temp(struct device *dev,
  139. struct device_attribute *devattr, char *buf)
  140. {
  141. int index = to_sensor_dev_attr(devattr)->index;
  142. struct adm1021_data *data = adm1021_update_device(dev);
  143. return sprintf(buf, "%d\n", data->temp[index]);
  144. }
  145. static ssize_t show_temp_max(struct device *dev,
  146. struct device_attribute *devattr, char *buf)
  147. {
  148. int index = to_sensor_dev_attr(devattr)->index;
  149. struct adm1021_data *data = adm1021_update_device(dev);
  150. return sprintf(buf, "%d\n", data->temp_max[index]);
  151. }
  152. static ssize_t show_temp_min(struct device *dev,
  153. struct device_attribute *devattr, char *buf)
  154. {
  155. int index = to_sensor_dev_attr(devattr)->index;
  156. struct adm1021_data *data = adm1021_update_device(dev);
  157. return sprintf(buf, "%d\n", data->temp_min[index]);
  158. }
  159. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  160. char *buf)
  161. {
  162. int index = to_sensor_dev_attr(attr)->index;
  163. struct adm1021_data *data = adm1021_update_device(dev);
  164. return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
  165. }
  166. static ssize_t show_alarms(struct device *dev,
  167. struct device_attribute *attr,
  168. char *buf)
  169. {
  170. struct adm1021_data *data = adm1021_update_device(dev);
  171. return sprintf(buf, "%u\n", data->alarms);
  172. }
  173. static ssize_t set_temp_max(struct device *dev,
  174. struct device_attribute *devattr,
  175. const char *buf, size_t count)
  176. {
  177. int index = to_sensor_dev_attr(devattr)->index;
  178. struct adm1021_data *data = dev_get_drvdata(dev);
  179. struct i2c_client *client = data->client;
  180. long temp;
  181. int reg_val, err;
  182. err = kstrtol(buf, 10, &temp);
  183. if (err)
  184. return err;
  185. temp /= 1000;
  186. mutex_lock(&data->update_lock);
  187. reg_val = clamp_val(temp, -128, 127);
  188. data->temp_max[index] = reg_val * 1000;
  189. if (!read_only)
  190. i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
  191. reg_val);
  192. mutex_unlock(&data->update_lock);
  193. return count;
  194. }
  195. static ssize_t set_temp_min(struct device *dev,
  196. struct device_attribute *devattr,
  197. const char *buf, size_t count)
  198. {
  199. int index = to_sensor_dev_attr(devattr)->index;
  200. struct adm1021_data *data = dev_get_drvdata(dev);
  201. struct i2c_client *client = data->client;
  202. long temp;
  203. int reg_val, err;
  204. err = kstrtol(buf, 10, &temp);
  205. if (err)
  206. return err;
  207. temp /= 1000;
  208. mutex_lock(&data->update_lock);
  209. reg_val = clamp_val(temp, -128, 127);
  210. data->temp_min[index] = reg_val * 1000;
  211. if (!read_only)
  212. i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
  213. reg_val);
  214. mutex_unlock(&data->update_lock);
  215. return count;
  216. }
  217. static ssize_t show_low_power(struct device *dev,
  218. struct device_attribute *devattr, char *buf)
  219. {
  220. struct adm1021_data *data = adm1021_update_device(dev);
  221. return sprintf(buf, "%d\n", data->low_power);
  222. }
  223. static ssize_t set_low_power(struct device *dev,
  224. struct device_attribute *devattr,
  225. const char *buf, size_t count)
  226. {
  227. struct adm1021_data *data = dev_get_drvdata(dev);
  228. struct i2c_client *client = data->client;
  229. char low_power;
  230. unsigned long val;
  231. int err;
  232. err = kstrtoul(buf, 10, &val);
  233. if (err)
  234. return err;
  235. low_power = val != 0;
  236. mutex_lock(&data->update_lock);
  237. if (low_power != data->low_power) {
  238. int config = i2c_smbus_read_byte_data(
  239. client, ADM1021_REG_CONFIG_R);
  240. data->low_power = low_power;
  241. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  242. (config & 0xBF) | (low_power << 6));
  243. }
  244. mutex_unlock(&data->update_lock);
  245. return count;
  246. }
  247. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  248. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  249. set_temp_max, 0);
  250. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
  251. set_temp_min, 0);
  252. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  253. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  254. set_temp_max, 1);
  255. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
  256. set_temp_min, 1);
  257. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  258. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  259. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  260. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  261. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  262. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  263. static DEVICE_ATTR(low_power, S_IWUSR | S_IRUGO, show_low_power, set_low_power);
  264. static struct attribute *adm1021_attributes[] = {
  265. &sensor_dev_attr_temp1_max.dev_attr.attr,
  266. &sensor_dev_attr_temp1_input.dev_attr.attr,
  267. &sensor_dev_attr_temp2_max.dev_attr.attr,
  268. &sensor_dev_attr_temp2_input.dev_attr.attr,
  269. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  270. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  271. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  272. &dev_attr_alarms.attr,
  273. &dev_attr_low_power.attr,
  274. NULL
  275. };
  276. static const struct attribute_group adm1021_group = {
  277. .attrs = adm1021_attributes,
  278. };
  279. static struct attribute *adm1021_min_attributes[] = {
  280. &sensor_dev_attr_temp1_min.dev_attr.attr,
  281. &sensor_dev_attr_temp2_min.dev_attr.attr,
  282. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  283. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  284. NULL
  285. };
  286. static const struct attribute_group adm1021_min_group = {
  287. .attrs = adm1021_min_attributes,
  288. };
  289. /* Return 0 if detection is successful, -ENODEV otherwise */
  290. static int adm1021_detect(struct i2c_client *client,
  291. struct i2c_board_info *info)
  292. {
  293. struct i2c_adapter *adapter = client->adapter;
  294. const char *type_name;
  295. int conv_rate, status, config, man_id, dev_id;
  296. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  297. pr_debug("detect failed, smbus byte data not supported!\n");
  298. return -ENODEV;
  299. }
  300. status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
  301. conv_rate = i2c_smbus_read_byte_data(client,
  302. ADM1021_REG_CONV_RATE_R);
  303. config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
  304. /* Check unused bits */
  305. if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
  306. pr_debug("detect failed, chip not detected!\n");
  307. return -ENODEV;
  308. }
  309. /* Determine the chip type. */
  310. man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
  311. dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
  312. if (man_id < 0 || dev_id < 0)
  313. return -ENODEV;
  314. if (man_id == 0x4d && dev_id == 0x01)
  315. type_name = "max1617a";
  316. else if (man_id == 0x41) {
  317. if ((dev_id & 0xF0) == 0x30)
  318. type_name = "adm1023";
  319. else if ((dev_id & 0xF0) == 0x00)
  320. type_name = "adm1021";
  321. else
  322. return -ENODEV;
  323. } else if (man_id == 0x49)
  324. type_name = "thmc10";
  325. else if (man_id == 0x23)
  326. type_name = "gl523sm";
  327. else if (man_id == 0x54)
  328. type_name = "mc1066";
  329. else {
  330. int lte, rte, lhi, rhi, llo, rlo;
  331. /* extra checks for LM84 and MAX1617 to avoid misdetections */
  332. llo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(0));
  333. rlo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(1));
  334. /* fail if any of the additional register reads failed */
  335. if (llo < 0 || rlo < 0)
  336. return -ENODEV;
  337. lte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(0));
  338. rte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(1));
  339. lhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(0));
  340. rhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(1));
  341. /*
  342. * Fail for negative temperatures and negative high limits.
  343. * This check also catches read errors on the tested registers.
  344. */
  345. if ((s8)lte < 0 || (s8)rte < 0 || (s8)lhi < 0 || (s8)rhi < 0)
  346. return -ENODEV;
  347. /* fail if all registers hold the same value */
  348. if (lte == rte && lte == lhi && lte == rhi && lte == llo
  349. && lte == rlo)
  350. return -ENODEV;
  351. /*
  352. * LM84 Mfr ID is in a different place,
  353. * and it has more unused bits.
  354. */
  355. if (conv_rate == 0x00
  356. && (config & 0x7F) == 0x00
  357. && (status & 0xAB) == 0x00) {
  358. type_name = "lm84";
  359. } else {
  360. /* fail if low limits are larger than high limits */
  361. if ((s8)llo > lhi || (s8)rlo > rhi)
  362. return -ENODEV;
  363. type_name = "max1617";
  364. }
  365. }
  366. pr_debug("Detected chip %s at adapter %d, address 0x%02x.\n",
  367. type_name, i2c_adapter_id(adapter), client->addr);
  368. strlcpy(info->type, type_name, I2C_NAME_SIZE);
  369. return 0;
  370. }
  371. static void adm1021_init_client(struct i2c_client *client)
  372. {
  373. /* Enable ADC and disable suspend mode */
  374. i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
  375. i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
  376. /* Set Conversion rate to 1/sec (this can be tinkered with) */
  377. i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
  378. }
  379. static int adm1021_probe(struct i2c_client *client,
  380. const struct i2c_device_id *id)
  381. {
  382. struct device *dev = &client->dev;
  383. struct adm1021_data *data;
  384. struct device *hwmon_dev;
  385. data = devm_kzalloc(dev, sizeof(struct adm1021_data), GFP_KERNEL);
  386. if (!data)
  387. return -ENOMEM;
  388. data->client = client;
  389. data->type = id->driver_data;
  390. mutex_init(&data->update_lock);
  391. /* Initialize the ADM1021 chip */
  392. if (data->type != lm84 && !read_only)
  393. adm1021_init_client(client);
  394. data->groups[0] = &adm1021_group;
  395. if (data->type != lm84)
  396. data->groups[1] = &adm1021_min_group;
  397. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  398. data, data->groups);
  399. return PTR_ERR_OR_ZERO(hwmon_dev);
  400. }
  401. static const struct i2c_device_id adm1021_id[] = {
  402. { "adm1021", adm1021 },
  403. { "adm1023", adm1023 },
  404. { "max1617", max1617 },
  405. { "max1617a", max1617a },
  406. { "thmc10", thmc10 },
  407. { "lm84", lm84 },
  408. { "gl523sm", gl523sm },
  409. { "mc1066", mc1066 },
  410. { }
  411. };
  412. MODULE_DEVICE_TABLE(i2c, adm1021_id);
  413. static struct i2c_driver adm1021_driver = {
  414. .class = I2C_CLASS_HWMON,
  415. .driver = {
  416. .name = "adm1021",
  417. },
  418. .probe = adm1021_probe,
  419. .id_table = adm1021_id,
  420. .detect = adm1021_detect,
  421. .address_list = normal_i2c,
  422. };
  423. module_i2c_driver(adm1021_driver);
  424. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  425. "Philip Edelbrock <phil@netroedge.com>");
  426. MODULE_DESCRIPTION("adm1021 driver");
  427. MODULE_LICENSE("GPL");
  428. module_param(read_only, bool, 0);
  429. MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");