thmc50.c 14 KB

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