PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/hwmon/emc1403.c

https://github.com/zossso/cm-kernel
C | 344 lines | 269 code | 42 blank | 33 comment | 23 complexity | a174abd480727f36bdbe64094aab3648 MD5 | raw file
  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 show_hyst(struct device *dev,
  83. struct device_attribute *attr, char *buf)
  84. {
  85. struct i2c_client *client = to_i2c_client(dev);
  86. struct thermal_data *data = i2c_get_clientdata(client);
  87. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  88. int retval;
  89. int hyst;
  90. retval = i2c_smbus_read_byte_data(client, sda->index);
  91. if (retval < 0)
  92. return retval;
  93. if (time_after(jiffies, data->hyst_valid)) {
  94. hyst = i2c_smbus_read_byte_data(client, 0x21);
  95. if (hyst < 0)
  96. return retval;
  97. data->cached_hyst = hyst;
  98. data->hyst_valid = jiffies + HZ;
  99. }
  100. return sprintf(buf, "%d000\n", retval - data->cached_hyst);
  101. }
  102. static ssize_t store_hyst(struct device *dev,
  103. struct device_attribute *attr, const char *buf, size_t count)
  104. {
  105. struct i2c_client *client = to_i2c_client(dev);
  106. struct thermal_data *data = i2c_get_clientdata(client);
  107. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  108. int retval;
  109. int hyst;
  110. unsigned long val;
  111. if (strict_strtoul(buf, 10, &val))
  112. return -EINVAL;
  113. mutex_lock(&data->mutex);
  114. retval = i2c_smbus_read_byte_data(client, sda->index);
  115. if (retval < 0)
  116. goto fail;
  117. hyst = val - retval * 1000;
  118. hyst = DIV_ROUND_CLOSEST(hyst, 1000);
  119. if (hyst < 0 || hyst > 255) {
  120. retval = -ERANGE;
  121. goto fail;
  122. }
  123. retval = i2c_smbus_write_byte_data(client, 0x21, hyst);
  124. if (retval == 0) {
  125. retval = count;
  126. data->cached_hyst = hyst;
  127. data->hyst_valid = jiffies + HZ;
  128. }
  129. fail:
  130. mutex_unlock(&data->mutex);
  131. return retval;
  132. }
  133. /*
  134. * Sensors. We pass the actual i2c register to the methods.
  135. */
  136. static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR,
  137. show_temp, store_temp, 0x06);
  138. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  139. show_temp, store_temp, 0x05);
  140. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  141. show_temp, store_temp, 0x20);
  142. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0x00);
  143. static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO,
  144. show_bit, NULL, 0x36, 0x01);
  145. static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO,
  146. show_bit, NULL, 0x35, 0x01);
  147. static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO,
  148. show_bit, NULL, 0x37, 0x01);
  149. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR,
  150. show_hyst, store_hyst, 0x20);
  151. static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR,
  152. show_temp, store_temp, 0x08);
  153. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
  154. show_temp, store_temp, 0x07);
  155. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
  156. show_temp, store_temp, 0x19);
  157. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0x01);
  158. static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO,
  159. show_bit, NULL, 0x36, 0x02);
  160. static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO,
  161. show_bit, NULL, 0x35, 0x02);
  162. static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO,
  163. show_bit, NULL, 0x37, 0x02);
  164. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO | S_IWUSR,
  165. show_hyst, store_hyst, 0x19);
  166. static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR,
  167. show_temp, store_temp, 0x16);
  168. static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
  169. show_temp, store_temp, 0x15);
  170. static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
  171. show_temp, store_temp, 0x1A);
  172. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 0x23);
  173. static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO,
  174. show_bit, NULL, 0x36, 0x04);
  175. static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO,
  176. show_bit, NULL, 0x35, 0x04);
  177. static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO,
  178. show_bit, NULL, 0x37, 0x04);
  179. static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO | S_IWUSR,
  180. show_hyst, store_hyst, 0x1A);
  181. static struct attribute *mid_att_thermal[] = {
  182. &sensor_dev_attr_temp1_min.dev_attr.attr,
  183. &sensor_dev_attr_temp1_max.dev_attr.attr,
  184. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  185. &sensor_dev_attr_temp1_input.dev_attr.attr,
  186. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  187. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  188. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  189. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  190. &sensor_dev_attr_temp2_min.dev_attr.attr,
  191. &sensor_dev_attr_temp2_max.dev_attr.attr,
  192. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  193. &sensor_dev_attr_temp2_input.dev_attr.attr,
  194. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  195. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  196. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  197. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  198. &sensor_dev_attr_temp3_min.dev_attr.attr,
  199. &sensor_dev_attr_temp3_max.dev_attr.attr,
  200. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  201. &sensor_dev_attr_temp3_input.dev_attr.attr,
  202. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  203. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  204. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  205. &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
  206. NULL
  207. };
  208. static const struct attribute_group m_thermal_gr = {
  209. .attrs = mid_att_thermal
  210. };
  211. static int emc1403_detect(struct i2c_client *client,
  212. struct i2c_board_info *info)
  213. {
  214. int id;
  215. /* Check if thermal chip is SMSC and EMC1403 */
  216. id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
  217. if (id != 0x5d)
  218. return -ENODEV;
  219. /* Note: 0x25 is the 1404 which is very similar and this
  220. driver could be extended */
  221. id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
  222. if (id != 0x21)
  223. return -ENODEV;
  224. id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
  225. if (id != 0x01)
  226. return -ENODEV;
  227. strlcpy(info->type, "emc1403", I2C_NAME_SIZE);
  228. return 0;
  229. }
  230. static int emc1403_probe(struct i2c_client *client,
  231. const struct i2c_device_id *id)
  232. {
  233. int res;
  234. struct thermal_data *data;
  235. data = kzalloc(sizeof(struct thermal_data), GFP_KERNEL);
  236. if (data == NULL) {
  237. dev_warn(&client->dev, "out of memory");
  238. return -ENOMEM;
  239. }
  240. i2c_set_clientdata(client, data);
  241. mutex_init(&data->mutex);
  242. data->hyst_valid = jiffies - 1; /* Expired */
  243. res = sysfs_create_group(&client->dev.kobj, &m_thermal_gr);
  244. if (res) {
  245. dev_warn(&client->dev, "create group failed\n");
  246. hwmon_device_unregister(data->hwmon_dev);
  247. goto thermal_error1;
  248. }
  249. data->hwmon_dev = hwmon_device_register(&client->dev);
  250. if (IS_ERR(data->hwmon_dev)) {
  251. res = PTR_ERR(data->hwmon_dev);
  252. dev_warn(&client->dev, "register hwmon dev failed\n");
  253. goto thermal_error2;
  254. }
  255. dev_info(&client->dev, "EMC1403 Thermal chip found\n");
  256. return res;
  257. thermal_error2:
  258. sysfs_remove_group(&client->dev.kobj, &m_thermal_gr);
  259. thermal_error1:
  260. kfree(data);
  261. return res;
  262. }
  263. static int emc1403_remove(struct i2c_client *client)
  264. {
  265. struct thermal_data *data = i2c_get_clientdata(client);
  266. hwmon_device_unregister(data->hwmon_dev);
  267. sysfs_remove_group(&client->dev.kobj, &m_thermal_gr);
  268. kfree(data);
  269. return 0;
  270. }
  271. static const unsigned short emc1403_address_list[] = {
  272. 0x18, 0x2a, 0x4c, 0x4d, I2C_CLIENT_END
  273. };
  274. static const struct i2c_device_id emc1403_idtable[] = {
  275. { "emc1403", 0 },
  276. { }
  277. };
  278. MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
  279. static struct i2c_driver sensor_emc1403 = {
  280. .class = I2C_CLASS_HWMON,
  281. .driver = {
  282. .name = "emc1403",
  283. },
  284. .detect = emc1403_detect,
  285. .probe = emc1403_probe,
  286. .remove = emc1403_remove,
  287. .id_table = emc1403_idtable,
  288. .address_list = emc1403_address_list,
  289. };
  290. static int __init sensor_emc1403_init(void)
  291. {
  292. return i2c_add_driver(&sensor_emc1403);
  293. }
  294. static void __exit sensor_emc1403_exit(void)
  295. {
  296. i2c_del_driver(&sensor_emc1403);
  297. }
  298. module_init(sensor_emc1403_init);
  299. module_exit(sensor_emc1403_exit);
  300. MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
  301. MODULE_DESCRIPTION("emc1403 Thermal Driver");
  302. MODULE_LICENSE("GPL v2");