thmc50.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. thmc50.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (C) 2007 Krzysztof Helt <krzysztof.h1@wp.pl>
  5. Based on 2.4 driver by Frodo Looijaard <frodol@dds.nl> and
  6. Philip Edelbrock <phil@netroedge.com>
  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. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c.h>
  23. #include <linux/hwmon.h>
  24. #include <linux/hwmon-sysfs.h>
  25. #include <linux/err.h>
  26. #include <linux/mutex.h>
  27. MODULE_LICENSE("GPL");
  28. /* Addresses to scan */
  29. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  30. /* Insmod parameters */
  31. I2C_CLIENT_INSMOD_2(thmc50, adm1022);
  32. I2C_CLIENT_MODULE_PARM(adm1022_temp3, "List of adapter,address pairs "
  33. "to enable 3rd temperature (ADM1022 only)");
  34. /* Many THMC50 constants specified below */
  35. /* The THMC50 registers */
  36. #define THMC50_REG_CONF 0x40
  37. #define THMC50_REG_COMPANY_ID 0x3E
  38. #define THMC50_REG_DIE_CODE 0x3F
  39. #define THMC50_REG_ANALOG_OUT 0x19
  40. /*
  41. * The mirror status register cannot be used as
  42. * reading it does not clear alarms.
  43. */
  44. #define THMC50_REG_INTR 0x41
  45. static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 };
  46. static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C };
  47. static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B };
  48. static const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 };
  49. static const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 };
  50. #define THMC50_REG_CONF_nFANOFF 0x20
  51. #define THMC50_REG_CONF_PROGRAMMED 0x08
  52. /* Each client has this additional data */
  53. struct thmc50_data {
  54. struct device *hwmon_dev;
  55. struct mutex update_lock;
  56. enum chips type;
  57. unsigned long last_updated; /* In jiffies */
  58. char has_temp3; /* !=0 if it is ADM1022 in temp3 mode */
  59. char valid; /* !=0 if following fields are valid */
  60. /* Register values */
  61. s8 temp_input[3];
  62. s8 temp_max[3];
  63. s8 temp_min[3];
  64. s8 temp_critical[3];
  65. u8 analog_out;
  66. u8 alarms;
  67. };
  68. static int thmc50_detect(struct i2c_client *client, int kind,
  69. struct i2c_board_info *info);
  70. static int thmc50_probe(struct i2c_client *client,
  71. const struct i2c_device_id *id);
  72. static int thmc50_remove(struct i2c_client *client);
  73. static void thmc50_init_client(struct i2c_client *client);
  74. static struct thmc50_data *thmc50_update_device(struct device *dev);
  75. static const struct i2c_device_id thmc50_id[] = {
  76. { "adm1022", adm1022 },
  77. { "thmc50", thmc50 },
  78. { }
  79. };
  80. MODULE_DEVICE_TABLE(i2c, thmc50_id);
  81. static struct i2c_driver thmc50_driver = {
  82. .class = I2C_CLASS_HWMON,
  83. .driver = {
  84. .name = "thmc50",
  85. },
  86. .probe = thmc50_probe,
  87. .remove = thmc50_remove,
  88. .id_table = thmc50_id,
  89. .detect = thmc50_detect,
  90. .address_data = &addr_data,
  91. };
  92. static ssize_t show_analog_out(struct device *dev,
  93. struct device_attribute *attr, char *buf)
  94. {
  95. struct thmc50_data *data = thmc50_update_device(dev);
  96. return sprintf(buf, "%d\n", data->analog_out);
  97. }
  98. static ssize_t set_analog_out(struct device *dev,
  99. struct device_attribute *attr,
  100. const char *buf, size_t count)
  101. {
  102. struct i2c_client *client = to_i2c_client(dev);
  103. struct thmc50_data *data = i2c_get_clientdata(client);
  104. int tmp = simple_strtoul(buf, NULL, 10);
  105. int config;
  106. mutex_lock(&data->update_lock);
  107. data->analog_out = SENSORS_LIMIT(tmp, 0, 255);
  108. i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
  109. data->analog_out);
  110. config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
  111. if (data->analog_out == 0)
  112. config &= ~THMC50_REG_CONF_nFANOFF;
  113. else
  114. config |= THMC50_REG_CONF_nFANOFF;
  115. i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
  116. mutex_unlock(&data->update_lock);
  117. return count;
  118. }
  119. /* There is only one PWM mode = DC */
  120. static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr,
  121. char *buf)
  122. {
  123. return sprintf(buf, "0\n");
  124. }
  125. /* Temperatures */
  126. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  127. char *buf)
  128. {
  129. int nr = to_sensor_dev_attr(attr)->index;
  130. struct thmc50_data *data = thmc50_update_device(dev);
  131. return sprintf(buf, "%d\n", data->temp_input[nr] * 1000);
  132. }
  133. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  134. char *buf)
  135. {
  136. int nr = to_sensor_dev_attr(attr)->index;
  137. struct thmc50_data *data = thmc50_update_device(dev);
  138. return sprintf(buf, "%d\n", data->temp_min[nr] * 1000);
  139. }
  140. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  141. const char *buf, size_t count)
  142. {
  143. int nr = to_sensor_dev_attr(attr)->index;
  144. struct i2c_client *client = to_i2c_client(dev);
  145. struct thmc50_data *data = i2c_get_clientdata(client);
  146. int val = simple_strtol(buf, NULL, 10);
  147. mutex_lock(&data->update_lock);
  148. data->temp_min[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
  149. i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr],
  150. data->temp_min[nr]);
  151. mutex_unlock(&data->update_lock);
  152. return count;
  153. }
  154. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  155. char *buf)
  156. {
  157. int nr = to_sensor_dev_attr(attr)->index;
  158. struct thmc50_data *data = thmc50_update_device(dev);
  159. return sprintf(buf, "%d\n", data->temp_max[nr] * 1000);
  160. }
  161. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  162. const char *buf, size_t count)
  163. {
  164. int nr = to_sensor_dev_attr(attr)->index;
  165. struct i2c_client *client = to_i2c_client(dev);
  166. struct thmc50_data *data = i2c_get_clientdata(client);
  167. int val = simple_strtol(buf, NULL, 10);
  168. mutex_lock(&data->update_lock);
  169. data->temp_max[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
  170. i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr],
  171. data->temp_max[nr]);
  172. mutex_unlock(&data->update_lock);
  173. return count;
  174. }
  175. static ssize_t show_temp_critical(struct device *dev,
  176. struct device_attribute *attr,
  177. char *buf)
  178. {
  179. int nr = to_sensor_dev_attr(attr)->index;
  180. struct thmc50_data *data = thmc50_update_device(dev);
  181. return sprintf(buf, "%d\n", data->temp_critical[nr] * 1000);
  182. }
  183. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  184. char *buf)
  185. {
  186. int index = to_sensor_dev_attr(attr)->index;
  187. struct thmc50_data *data = thmc50_update_device(dev);
  188. return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
  189. }
  190. #define temp_reg(offset) \
  191. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
  192. NULL, offset - 1); \
  193. static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  194. show_temp_min, set_temp_min, offset - 1); \
  195. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  196. show_temp_max, set_temp_max, offset - 1); \
  197. static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO, \
  198. show_temp_critical, NULL, offset - 1);
  199. temp_reg(1);
  200. temp_reg(2);
  201. temp_reg(3);
  202. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0);
  203. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
  204. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
  205. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 7);
  206. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
  207. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_analog_out,
  208. set_analog_out, 0);
  209. static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
  210. static struct attribute *thmc50_attributes[] = {
  211. &sensor_dev_attr_temp1_max.dev_attr.attr,
  212. &sensor_dev_attr_temp1_min.dev_attr.attr,
  213. &sensor_dev_attr_temp1_input.dev_attr.attr,
  214. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  215. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  216. &sensor_dev_attr_temp2_max.dev_attr.attr,
  217. &sensor_dev_attr_temp2_min.dev_attr.attr,
  218. &sensor_dev_attr_temp2_input.dev_attr.attr,
  219. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  220. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  221. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  222. &sensor_dev_attr_pwm1.dev_attr.attr,
  223. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  224. NULL
  225. };
  226. static const struct attribute_group thmc50_group = {
  227. .attrs = thmc50_attributes,
  228. };
  229. /* for ADM1022 3rd temperature mode */
  230. static struct attribute *temp3_attributes[] = {
  231. &sensor_dev_attr_temp3_max.dev_attr.attr,
  232. &sensor_dev_attr_temp3_min.dev_attr.attr,
  233. &sensor_dev_attr_temp3_input.dev_attr.attr,
  234. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  235. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  236. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  237. NULL
  238. };
  239. static const struct attribute_group temp3_group = {
  240. .attrs = temp3_attributes,
  241. };
  242. /* Return 0 if detection is successful, -ENODEV otherwise */
  243. static int thmc50_detect(struct i2c_client *client, int kind,
  244. struct i2c_board_info *info)
  245. {
  246. unsigned company;
  247. unsigned revision;
  248. unsigned config;
  249. struct i2c_adapter *adapter = client->adapter;
  250. int err = 0;
  251. const char *type_name;
  252. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  253. pr_debug("thmc50: detect failed, "
  254. "smbus byte data not supported!\n");
  255. return -ENODEV;
  256. }
  257. pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
  258. client->addr, i2c_adapter_id(client->adapter));
  259. /* Now, we do the remaining detection. */
  260. company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID);
  261. revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE);
  262. config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
  263. if (kind == 0)
  264. kind = thmc50;
  265. else if (kind < 0) {
  266. err = -ENODEV;
  267. if (revision >= 0xc0 && ((config & 0x10) == 0)) {
  268. if (company == 0x49) {
  269. kind = thmc50;
  270. err = 0;
  271. } else if (company == 0x41) {
  272. kind = adm1022;
  273. err = 0;
  274. }
  275. }
  276. }
  277. if (err == -ENODEV) {
  278. pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
  279. return err;
  280. }
  281. if (kind == adm1022) {
  282. int id = i2c_adapter_id(client->adapter);
  283. int i;
  284. type_name = "adm1022";
  285. for (i = 0; i + 1 < adm1022_temp3_num; i += 2)
  286. if (adm1022_temp3[i] == id &&
  287. adm1022_temp3[i + 1] == client->addr) {
  288. /* enable 2nd remote temp */
  289. config |= (1 << 7);
  290. i2c_smbus_write_byte_data(client,
  291. THMC50_REG_CONF,
  292. config);
  293. break;
  294. }
  295. } else {
  296. type_name = "thmc50";
  297. }
  298. pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
  299. type_name, (revision >> 4) - 0xc, revision & 0xf);
  300. strlcpy(info->type, type_name, I2C_NAME_SIZE);
  301. return 0;
  302. }
  303. static int thmc50_probe(struct i2c_client *client,
  304. const struct i2c_device_id *id)
  305. {
  306. struct thmc50_data *data;
  307. int err;
  308. data = kzalloc(sizeof(struct thmc50_data), GFP_KERNEL);
  309. if (!data) {
  310. pr_debug("thmc50: detect failed, kzalloc failed!\n");
  311. err = -ENOMEM;
  312. goto exit;
  313. }
  314. i2c_set_clientdata(client, data);
  315. data->type = id->driver_data;
  316. mutex_init(&data->update_lock);
  317. thmc50_init_client(client);
  318. /* Register sysfs hooks */
  319. if ((err = sysfs_create_group(&client->dev.kobj, &thmc50_group)))
  320. goto exit_free;
  321. /* Register ADM1022 sysfs hooks */
  322. if (data->has_temp3)
  323. if ((err = sysfs_create_group(&client->dev.kobj,
  324. &temp3_group)))
  325. goto exit_remove_sysfs_thmc50;
  326. /* Register a new directory entry with module sensors */
  327. data->hwmon_dev = hwmon_device_register(&client->dev);
  328. if (IS_ERR(data->hwmon_dev)) {
  329. err = PTR_ERR(data->hwmon_dev);
  330. goto exit_remove_sysfs;
  331. }
  332. return 0;
  333. exit_remove_sysfs:
  334. if (data->has_temp3)
  335. sysfs_remove_group(&client->dev.kobj, &temp3_group);
  336. exit_remove_sysfs_thmc50:
  337. sysfs_remove_group(&client->dev.kobj, &thmc50_group);
  338. exit_free:
  339. kfree(data);
  340. exit:
  341. return err;
  342. }
  343. static int thmc50_remove(struct i2c_client *client)
  344. {
  345. struct thmc50_data *data = i2c_get_clientdata(client);
  346. hwmon_device_unregister(data->hwmon_dev);
  347. sysfs_remove_group(&client->dev.kobj, &thmc50_group);
  348. if (data->has_temp3)
  349. sysfs_remove_group(&client->dev.kobj, &temp3_group);
  350. kfree(data);
  351. return 0;
  352. }
  353. static void thmc50_init_client(struct i2c_client *client)
  354. {
  355. struct thmc50_data *data = i2c_get_clientdata(client);
  356. int config;
  357. data->analog_out = i2c_smbus_read_byte_data(client,
  358. THMC50_REG_ANALOG_OUT);
  359. /* set up to at least 1 */
  360. if (data->analog_out == 0) {
  361. data->analog_out = 1;
  362. i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
  363. data->analog_out);
  364. }
  365. config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
  366. config |= 0x1; /* start the chip if it is in standby mode */
  367. if (data->type == adm1022 && (config & (1 << 7)))
  368. data->has_temp3 = 1;
  369. i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
  370. }
  371. static struct thmc50_data *thmc50_update_device(struct device *dev)
  372. {
  373. struct i2c_client *client = to_i2c_client(dev);
  374. struct thmc50_data *data = i2c_get_clientdata(client);
  375. int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0);
  376. mutex_lock(&data->update_lock);
  377. if (time_after(jiffies, data->last_updated + timeout)
  378. || !data->valid) {
  379. int temps = data->has_temp3 ? 3 : 2;
  380. int i;
  381. int prog = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
  382. prog &= THMC50_REG_CONF_PROGRAMMED;
  383. for (i = 0; i < temps; i++) {
  384. data->temp_input[i] = i2c_smbus_read_byte_data(client,
  385. THMC50_REG_TEMP[i]);
  386. data->temp_max[i] = i2c_smbus_read_byte_data(client,
  387. THMC50_REG_TEMP_MAX[i]);
  388. data->temp_min[i] = i2c_smbus_read_byte_data(client,
  389. THMC50_REG_TEMP_MIN[i]);
  390. data->temp_critical[i] =
  391. i2c_smbus_read_byte_data(client,
  392. prog ? THMC50_REG_TEMP_CRITICAL[i]
  393. : THMC50_REG_TEMP_DEFAULT[i]);
  394. }
  395. data->analog_out =
  396. i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT);
  397. data->alarms =
  398. i2c_smbus_read_byte_data(client, THMC50_REG_INTR);
  399. data->last_updated = jiffies;
  400. data->valid = 1;
  401. }
  402. mutex_unlock(&data->update_lock);
  403. return data;
  404. }
  405. static int __init sm_thmc50_init(void)
  406. {
  407. return i2c_add_driver(&thmc50_driver);
  408. }
  409. static void __exit sm_thmc50_exit(void)
  410. {
  411. i2c_del_driver(&thmc50_driver);
  412. }
  413. MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
  414. MODULE_DESCRIPTION("THMC50 driver");
  415. module_init(sm_thmc50_init);
  416. module_exit(sm_thmc50_exit);