emc1403.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * emc1403.c - SMSC Thermal Driver
  3. *
  4. * Copyright (C) 2008 Intel Corp
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  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; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  20. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. *
  22. * TODO
  23. * - cache alarm and critical limit registers
  24. * - add emc1404 support
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/i2c.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/err.h>
  33. #include <linux/sysfs.h>
  34. #include <linux/mutex.h>
  35. #define THERMAL_PID_REG 0xfd
  36. #define THERMAL_SMSC_ID_REG 0xfe
  37. #define THERMAL_REVISION_REG 0xff
  38. struct thermal_data {
  39. struct device *hwmon_dev;
  40. struct mutex mutex;
  41. /* Cache the hyst value so we don't keep re-reading it. In theory
  42. we could cache it forever as nobody else should be writing it. */
  43. u8 cached_hyst;
  44. unsigned long hyst_valid;
  45. };
  46. static ssize_t show_temp(struct device *dev,
  47. struct device_attribute *attr, char *buf)
  48. {
  49. struct i2c_client *client = to_i2c_client(dev);
  50. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  51. int retval = i2c_smbus_read_byte_data(client, sda->index);
  52. if (retval < 0)
  53. return retval;
  54. return sprintf(buf, "%d000\n", retval);
  55. }
  56. static ssize_t show_bit(struct device *dev,
  57. struct device_attribute *attr, char *buf)
  58. {
  59. struct i2c_client *client = to_i2c_client(dev);
  60. struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
  61. int retval = i2c_smbus_read_byte_data(client, sda->nr);
  62. if (retval < 0)
  63. return retval;
  64. retval &= sda->index;
  65. return sprintf(buf, "%d\n", retval ? 1 : 0);
  66. }
  67. static ssize_t store_temp(struct device *dev,
  68. struct device_attribute *attr, const char *buf, size_t count)
  69. {
  70. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  71. struct i2c_client *client = to_i2c_client(dev);
  72. unsigned long val;
  73. int retval;
  74. if (strict_strtoul(buf, 10, &val))
  75. return -EINVAL;
  76. retval = i2c_smbus_write_byte_data(client, sda->index,
  77. DIV_ROUND_CLOSEST(val, 1000));
  78. if (retval < 0)
  79. return retval;
  80. return count;
  81. }
  82. static ssize_t store_bit(struct device *dev,
  83. struct device_attribute *attr, const char *buf, size_t count)
  84. {
  85. struct i2c_client *client = to_i2c_client(dev);
  86. struct thermal_data *data = i2c_get_clientdata(client);
  87. struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
  88. unsigned long val;
  89. int retval;
  90. if (strict_strtoul(buf, 10, &val))
  91. return -EINVAL;
  92. mutex_lock(&data->mutex);
  93. retval = i2c_smbus_read_byte_data(client, sda->nr);
  94. if (retval < 0)
  95. goto fail;
  96. retval &= ~sda->index;
  97. if (val)
  98. retval |= sda->index;
  99. retval = i2c_smbus_write_byte_data(client, sda->index, retval);
  100. if (retval == 0)
  101. retval = count;
  102. fail:
  103. mutex_unlock(&data->mutex);
  104. return retval;
  105. }
  106. static ssize_t show_hyst(struct device *dev,
  107. struct device_attribute *attr, char *buf)
  108. {
  109. struct i2c_client *client = to_i2c_client(dev);
  110. struct thermal_data *data = i2c_get_clientdata(client);
  111. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  112. int retval;
  113. int hyst;
  114. retval = i2c_smbus_read_byte_data(client, sda->index);
  115. if (retval < 0)
  116. return retval;
  117. if (time_after(jiffies, data->hyst_valid)) {
  118. hyst = i2c_smbus_read_byte_data(client, 0x21);
  119. if (hyst < 0)
  120. return retval;
  121. data->cached_hyst = hyst;
  122. data->hyst_valid = jiffies + HZ;
  123. }
  124. return sprintf(buf, "%d000\n", retval - data->cached_hyst);
  125. }
  126. static ssize_t store_hyst(struct device *dev,
  127. struct device_attribute *attr, const char *buf, size_t count)
  128. {
  129. struct i2c_client *client = to_i2c_client(dev);
  130. struct thermal_data *data = i2c_get_clientdata(client);
  131. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  132. int retval;
  133. int hyst;
  134. unsigned long val;
  135. if (strict_strtoul(buf, 10, &val))
  136. return -EINVAL;
  137. mutex_lock(&data->mutex);
  138. retval = i2c_smbus_read_byte_data(client, sda->index);
  139. if (retval < 0)
  140. goto fail;
  141. hyst = val - retval * 1000;
  142. hyst = DIV_ROUND_CLOSEST(hyst, 1000);
  143. if (hyst < 0 || hyst > 255) {
  144. retval = -ERANGE;
  145. goto fail;
  146. }
  147. retval = i2c_smbus_write_byte_data(client, 0x21, hyst);
  148. if (retval == 0) {
  149. retval = count;
  150. data->cached_hyst = hyst;
  151. data->hyst_valid = jiffies + HZ;
  152. }
  153. fail:
  154. mutex_unlock(&data->mutex);
  155. return retval;
  156. }
  157. /*
  158. * Sensors. We pass the actual i2c register to the methods.
  159. */
  160. static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR,
  161. show_temp, store_temp, 0x06);
  162. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  163. show_temp, store_temp, 0x05);
  164. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  165. show_temp, store_temp, 0x20);
  166. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0x00);
  167. static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO,
  168. show_bit, NULL, 0x36, 0x01);
  169. static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO,
  170. show_bit, NULL, 0x35, 0x01);
  171. static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO,
  172. show_bit, NULL, 0x37, 0x01);
  173. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR,
  174. show_hyst, store_hyst, 0x20);
  175. static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR,
  176. show_temp, store_temp, 0x08);
  177. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
  178. show_temp, store_temp, 0x07);
  179. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
  180. show_temp, store_temp, 0x19);
  181. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0x01);
  182. static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO,
  183. show_bit, NULL, 0x36, 0x02);
  184. static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO,
  185. show_bit, NULL, 0x35, 0x02);
  186. static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO,
  187. show_bit, NULL, 0x37, 0x02);
  188. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO | S_IWUSR,
  189. show_hyst, store_hyst, 0x19);
  190. static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR,
  191. show_temp, store_temp, 0x16);
  192. static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
  193. show_temp, store_temp, 0x15);
  194. static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
  195. show_temp, store_temp, 0x1A);
  196. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 0x23);
  197. static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO,
  198. show_bit, NULL, 0x36, 0x04);
  199. static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO,
  200. show_bit, NULL, 0x35, 0x04);
  201. static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO,
  202. show_bit, NULL, 0x37, 0x04);
  203. static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO | S_IWUSR,
  204. show_hyst, store_hyst, 0x1A);
  205. static SENSOR_DEVICE_ATTR_2(power_state, S_IRUGO | S_IWUSR,
  206. show_bit, store_bit, 0x03, 0x40);
  207. static struct attribute *mid_att_thermal[] = {
  208. &sensor_dev_attr_temp1_min.dev_attr.attr,
  209. &sensor_dev_attr_temp1_max.dev_attr.attr,
  210. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  211. &sensor_dev_attr_temp1_input.dev_attr.attr,
  212. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  213. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  214. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  215. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  216. &sensor_dev_attr_temp2_min.dev_attr.attr,
  217. &sensor_dev_attr_temp2_max.dev_attr.attr,
  218. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  219. &sensor_dev_attr_temp2_input.dev_attr.attr,
  220. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  221. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  222. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  223. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  224. &sensor_dev_attr_temp3_min.dev_attr.attr,
  225. &sensor_dev_attr_temp3_max.dev_attr.attr,
  226. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  227. &sensor_dev_attr_temp3_input.dev_attr.attr,
  228. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  229. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  230. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  231. &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
  232. &sensor_dev_attr_power_state.dev_attr.attr,
  233. NULL
  234. };
  235. static const struct attribute_group m_thermal_gr = {
  236. .attrs = mid_att_thermal
  237. };
  238. static int emc1403_detect(struct i2c_client *client,
  239. struct i2c_board_info *info)
  240. {
  241. int id;
  242. /* Check if thermal chip is SMSC and EMC1403 */
  243. id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
  244. if (id != 0x5d)
  245. return -ENODEV;
  246. /* Note: 0x25 is the 1404 which is very similar and this
  247. driver could be extended */
  248. id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
  249. if (id != 0x21)
  250. return -ENODEV;
  251. id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
  252. if (id != 0x01)
  253. return -ENODEV;
  254. strlcpy(info->type, "emc1403", I2C_NAME_SIZE);
  255. return 0;
  256. }
  257. static int emc1403_probe(struct i2c_client *client,
  258. const struct i2c_device_id *id)
  259. {
  260. int res;
  261. struct thermal_data *data;
  262. data = kzalloc(sizeof(struct thermal_data), GFP_KERNEL);
  263. if (data == NULL) {
  264. dev_warn(&client->dev, "out of memory");
  265. return -ENOMEM;
  266. }
  267. i2c_set_clientdata(client, data);
  268. mutex_init(&data->mutex);
  269. data->hyst_valid = jiffies - 1; /* Expired */
  270. res = sysfs_create_group(&client->dev.kobj, &m_thermal_gr);
  271. if (res) {
  272. dev_warn(&client->dev, "create group failed\n");
  273. goto thermal_error1;
  274. }
  275. data->hwmon_dev = hwmon_device_register(&client->dev);
  276. if (IS_ERR(data->hwmon_dev)) {
  277. res = PTR_ERR(data->hwmon_dev);
  278. dev_warn(&client->dev, "register hwmon dev failed\n");
  279. goto thermal_error2;
  280. }
  281. dev_info(&client->dev, "EMC1403 Thermal chip found\n");
  282. return res;
  283. thermal_error2:
  284. sysfs_remove_group(&client->dev.kobj, &m_thermal_gr);
  285. thermal_error1:
  286. kfree(data);
  287. return res;
  288. }
  289. static int emc1403_remove(struct i2c_client *client)
  290. {
  291. struct thermal_data *data = i2c_get_clientdata(client);
  292. hwmon_device_unregister(data->hwmon_dev);
  293. sysfs_remove_group(&client->dev.kobj, &m_thermal_gr);
  294. kfree(data);
  295. return 0;
  296. }
  297. static const unsigned short emc1403_address_list[] = {
  298. 0x18, 0x2a, 0x4c, 0x4d, I2C_CLIENT_END
  299. };
  300. static const struct i2c_device_id emc1403_idtable[] = {
  301. { "emc1403", 0 },
  302. { }
  303. };
  304. MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
  305. static struct i2c_driver sensor_emc1403 = {
  306. .class = I2C_CLASS_HWMON,
  307. .driver = {
  308. .name = "emc1403",
  309. },
  310. .detect = emc1403_detect,
  311. .probe = emc1403_probe,
  312. .remove = emc1403_remove,
  313. .id_table = emc1403_idtable,
  314. .address_list = emc1403_address_list,
  315. };
  316. static int __init sensor_emc1403_init(void)
  317. {
  318. return i2c_add_driver(&sensor_emc1403);
  319. }
  320. static void __exit sensor_emc1403_exit(void)
  321. {
  322. i2c_del_driver(&sensor_emc1403);
  323. }
  324. module_init(sensor_emc1403_init);
  325. module_exit(sensor_emc1403_exit);
  326. MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
  327. MODULE_DESCRIPTION("emc1403 Thermal Driver");
  328. MODULE_LICENSE("GPL v2");