thmc50.c 13 KB

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