emc1403.c 11 KB

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