thmc50.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #define THMC50_REG_CONF_nFANOFF 0x20
  49. /* Each client has this additional data */
  50. struct thmc50_data {
  51. struct device *hwmon_dev;
  52. struct mutex update_lock;
  53. enum chips type;
  54. unsigned long last_updated; /* In jiffies */
  55. char has_temp3; /* !=0 if it is ADM1022 in temp3 mode */
  56. char valid; /* !=0 if following fields are valid */
  57. /* Register values */
  58. s8 temp_input[3];
  59. s8 temp_max[3];
  60. s8 temp_min[3];
  61. u8 analog_out;
  62. u8 alarms;
  63. };
  64. static int thmc50_detect(struct i2c_client *client, int kind,
  65. struct i2c_board_info *info);
  66. static int thmc50_probe(struct i2c_client *client,
  67. const struct i2c_device_id *id);
  68. static int thmc50_remove(struct i2c_client *client);
  69. static void thmc50_init_client(struct i2c_client *client);
  70. static struct thmc50_data *thmc50_update_device(struct device *dev);
  71. static const struct i2c_device_id thmc50_id[] = {
  72. { "adm1022", adm1022 },
  73. { "thmc50", thmc50 },
  74. { }
  75. };
  76. MODULE_DEVICE_TABLE(i2c, thmc50_id);
  77. static struct i2c_driver thmc50_driver = {
  78. .class = I2C_CLASS_HWMON,
  79. .driver = {
  80. .name = "thmc50",
  81. },
  82. .probe = thmc50_probe,
  83. .remove = thmc50_remove,
  84. .id_table = thmc50_id,
  85. .detect = thmc50_detect,
  86. .address_data = &addr_data,
  87. };
  88. static ssize_t show_analog_out(struct device *dev,
  89. struct device_attribute *attr, char *buf)
  90. {
  91. struct thmc50_data *data = thmc50_update_device(dev);
  92. return sprintf(buf, "%d\n", data->analog_out);
  93. }
  94. static ssize_t set_analog_out(struct device *dev,
  95. struct device_attribute *attr,
  96. const char *buf, size_t count)
  97. {
  98. struct i2c_client *client = to_i2c_client(dev);
  99. struct thmc50_data *data = i2c_get_clientdata(client);
  100. int tmp = simple_strtoul(buf, NULL, 10);
  101. int config;
  102. mutex_lock(&data->update_lock);
  103. data->analog_out = SENSORS_LIMIT(tmp, 0, 255);
  104. i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
  105. data->analog_out);
  106. config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
  107. if (data->analog_out == 0)
  108. config &= ~THMC50_REG_CONF_nFANOFF;
  109. else
  110. config |= THMC50_REG_CONF_nFANOFF;
  111. i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
  112. mutex_unlock(&data->update_lock);
  113. return count;
  114. }
  115. /* There is only one PWM mode = DC */
  116. static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr,
  117. char *buf)
  118. {
  119. return sprintf(buf, "0\n");
  120. }
  121. /* Temperatures */
  122. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  123. char *buf)
  124. {
  125. int nr = to_sensor_dev_attr(attr)->index;
  126. struct thmc50_data *data = thmc50_update_device(dev);
  127. return sprintf(buf, "%d\n", data->temp_input[nr] * 1000);
  128. }
  129. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  130. char *buf)
  131. {
  132. int nr = to_sensor_dev_attr(attr)->index;
  133. struct thmc50_data *data = thmc50_update_device(dev);
  134. return sprintf(buf, "%d\n", data->temp_min[nr] * 1000);
  135. }
  136. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  137. const char *buf, size_t count)
  138. {
  139. int nr = to_sensor_dev_attr(attr)->index;
  140. struct i2c_client *client = to_i2c_client(dev);
  141. struct thmc50_data *data = i2c_get_clientdata(client);
  142. int val = simple_strtol(buf, NULL, 10);
  143. mutex_lock(&data->update_lock);
  144. data->temp_min[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
  145. i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr],
  146. data->temp_min[nr]);
  147. mutex_unlock(&data->update_lock);
  148. return count;
  149. }
  150. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  151. char *buf)
  152. {
  153. int nr = to_sensor_dev_attr(attr)->index;
  154. struct thmc50_data *data = thmc50_update_device(dev);
  155. return sprintf(buf, "%d\n", data->temp_max[nr] * 1000);
  156. }
  157. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  158. const char *buf, size_t count)
  159. {
  160. int nr = to_sensor_dev_attr(attr)->index;
  161. struct i2c_client *client = to_i2c_client(dev);
  162. struct thmc50_data *data = i2c_get_clientdata(client);
  163. int val = simple_strtol(buf, NULL, 10);
  164. mutex_lock(&data->update_lock);
  165. data->temp_max[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
  166. i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr],
  167. data->temp_max[nr]);
  168. mutex_unlock(&data->update_lock);
  169. return count;
  170. }
  171. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  172. char *buf)
  173. {
  174. int index = to_sensor_dev_attr(attr)->index;
  175. struct thmc50_data *data = thmc50_update_device(dev);
  176. return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
  177. }
  178. #define temp_reg(offset) \
  179. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
  180. NULL, offset - 1); \
  181. static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  182. show_temp_min, set_temp_min, offset - 1); \
  183. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  184. show_temp_max, set_temp_max, offset - 1);
  185. temp_reg(1);
  186. temp_reg(2);
  187. temp_reg(3);
  188. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0);
  189. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
  190. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
  191. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 7);
  192. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
  193. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_analog_out,
  194. set_analog_out, 0);
  195. static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
  196. static struct attribute *thmc50_attributes[] = {
  197. &sensor_dev_attr_temp1_max.dev_attr.attr,
  198. &sensor_dev_attr_temp1_min.dev_attr.attr,
  199. &sensor_dev_attr_temp1_input.dev_attr.attr,
  200. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  201. &sensor_dev_attr_temp2_max.dev_attr.attr,
  202. &sensor_dev_attr_temp2_min.dev_attr.attr,
  203. &sensor_dev_attr_temp2_input.dev_attr.attr,
  204. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  205. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  206. &sensor_dev_attr_pwm1.dev_attr.attr,
  207. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  208. NULL
  209. };
  210. static const struct attribute_group thmc50_group = {
  211. .attrs = thmc50_attributes,
  212. };
  213. /* for ADM1022 3rd temperature mode */
  214. static struct attribute *temp3_attributes[] = {
  215. &sensor_dev_attr_temp3_max.dev_attr.attr,
  216. &sensor_dev_attr_temp3_min.dev_attr.attr,
  217. &sensor_dev_attr_temp3_input.dev_attr.attr,
  218. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  219. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  220. NULL
  221. };
  222. static const struct attribute_group temp3_group = {
  223. .attrs = temp3_attributes,
  224. };
  225. /* Return 0 if detection is successful, -ENODEV otherwise */
  226. static int thmc50_detect(struct i2c_client *client, int kind,
  227. struct i2c_board_info *info)
  228. {
  229. unsigned company;
  230. unsigned revision;
  231. unsigned config;
  232. struct i2c_adapter *adapter = client->adapter;
  233. int err = 0;
  234. const char *type_name;
  235. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  236. pr_debug("thmc50: detect failed, "
  237. "smbus byte data not supported!\n");
  238. return -ENODEV;
  239. }
  240. pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
  241. client->addr, i2c_adapter_id(client->adapter));
  242. /* Now, we do the remaining detection. */
  243. company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID);
  244. revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE);
  245. config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
  246. if (kind == 0)
  247. kind = thmc50;
  248. else if (kind < 0) {
  249. err = -ENODEV;
  250. if (revision >= 0xc0 && ((config & 0x10) == 0)) {
  251. if (company == 0x49) {
  252. kind = thmc50;
  253. err = 0;
  254. } else if (company == 0x41) {
  255. kind = adm1022;
  256. err = 0;
  257. }
  258. }
  259. }
  260. if (err == -ENODEV) {
  261. pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
  262. return err;
  263. }
  264. if (kind == adm1022) {
  265. int id = i2c_adapter_id(client->adapter);
  266. int i;
  267. type_name = "adm1022";
  268. for (i = 0; i + 1 < adm1022_temp3_num; i += 2)
  269. if (adm1022_temp3[i] == id &&
  270. adm1022_temp3[i + 1] == client->addr) {
  271. /* enable 2nd remote temp */
  272. config |= (1 << 7);
  273. i2c_smbus_write_byte_data(client,
  274. THMC50_REG_CONF,
  275. config);
  276. break;
  277. }
  278. } else {
  279. type_name = "thmc50";
  280. }
  281. pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
  282. type_name, (revision >> 4) - 0xc, revision & 0xf);
  283. strlcpy(info->type, type_name, I2C_NAME_SIZE);
  284. return 0;
  285. }
  286. static int thmc50_probe(struct i2c_client *client,
  287. const struct i2c_device_id *id)
  288. {
  289. struct thmc50_data *data;
  290. int err;
  291. data = kzalloc(sizeof(struct thmc50_data), GFP_KERNEL);
  292. if (!data) {
  293. pr_debug("thmc50: detect failed, kzalloc failed!\n");
  294. err = -ENOMEM;
  295. goto exit;
  296. }
  297. i2c_set_clientdata(client, data);
  298. data->type = id->driver_data;
  299. mutex_init(&data->update_lock);
  300. thmc50_init_client(client);
  301. /* Register sysfs hooks */
  302. if ((err = sysfs_create_group(&client->dev.kobj, &thmc50_group)))
  303. goto exit_free;
  304. /* Register ADM1022 sysfs hooks */
  305. if (data->has_temp3)
  306. if ((err = sysfs_create_group(&client->dev.kobj,
  307. &temp3_group)))
  308. goto exit_remove_sysfs_thmc50;
  309. /* Register a new directory entry with module sensors */
  310. data->hwmon_dev = hwmon_device_register(&client->dev);
  311. if (IS_ERR(data->hwmon_dev)) {
  312. err = PTR_ERR(data->hwmon_dev);
  313. goto exit_remove_sysfs;
  314. }
  315. return 0;
  316. exit_remove_sysfs:
  317. if (data->has_temp3)
  318. sysfs_remove_group(&client->dev.kobj, &temp3_group);
  319. exit_remove_sysfs_thmc50:
  320. sysfs_remove_group(&client->dev.kobj, &thmc50_group);
  321. exit_free:
  322. kfree(data);
  323. exit:
  324. return err;
  325. }
  326. static int thmc50_remove(struct i2c_client *client)
  327. {
  328. struct thmc50_data *data = i2c_get_clientdata(client);
  329. hwmon_device_unregister(data->hwmon_dev);
  330. sysfs_remove_group(&client->dev.kobj, &thmc50_group);
  331. if (data->has_temp3)
  332. sysfs_remove_group(&client->dev.kobj, &temp3_group);
  333. kfree(data);
  334. return 0;
  335. }
  336. static void thmc50_init_client(struct i2c_client *client)
  337. {
  338. struct thmc50_data *data = i2c_get_clientdata(client);
  339. int config;
  340. data->analog_out = i2c_smbus_read_byte_data(client,
  341. THMC50_REG_ANALOG_OUT);
  342. /* set up to at least 1 */
  343. if (data->analog_out == 0) {
  344. data->analog_out = 1;
  345. i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
  346. data->analog_out);
  347. }
  348. config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
  349. config |= 0x1; /* start the chip if it is in standby mode */
  350. if (data->type == adm1022 && (config & (1 << 7)))
  351. data->has_temp3 = 1;
  352. i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
  353. }
  354. static struct thmc50_data *thmc50_update_device(struct device *dev)
  355. {
  356. struct i2c_client *client = to_i2c_client(dev);
  357. struct thmc50_data *data = i2c_get_clientdata(client);
  358. int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0);
  359. mutex_lock(&data->update_lock);
  360. if (time_after(jiffies, data->last_updated + timeout)
  361. || !data->valid) {
  362. int temps = data->has_temp3 ? 3 : 2;
  363. int i;
  364. for (i = 0; i < temps; i++) {
  365. data->temp_input[i] = i2c_smbus_read_byte_data(client,
  366. THMC50_REG_TEMP[i]);
  367. data->temp_max[i] = i2c_smbus_read_byte_data(client,
  368. THMC50_REG_TEMP_MAX[i]);
  369. data->temp_min[i] = i2c_smbus_read_byte_data(client,
  370. THMC50_REG_TEMP_MIN[i]);
  371. }
  372. data->analog_out =
  373. i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT);
  374. data->alarms =
  375. i2c_smbus_read_byte_data(client, THMC50_REG_INTR);
  376. data->last_updated = jiffies;
  377. data->valid = 1;
  378. }
  379. mutex_unlock(&data->update_lock);
  380. return data;
  381. }
  382. static int __init sm_thmc50_init(void)
  383. {
  384. return i2c_add_driver(&thmc50_driver);
  385. }
  386. static void __exit sm_thmc50_exit(void)
  387. {
  388. i2c_del_driver(&thmc50_driver);
  389. }
  390. MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
  391. MODULE_DESCRIPTION("THMC50 driver");
  392. module_init(sm_thmc50_init);
  393. module_exit(sm_thmc50_exit);