thmc50.c 13 KB

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