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