emc1403.c 11 KB

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