thmc50.c 14 KB

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