emc1403.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/i2c.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/hwmon-sysfs.h>
  31. #include <linux/err.h>
  32. #include <linux/sysfs.h>
  33. #include <linux/mutex.h>
  34. #include <linux/jiffies.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 i2c_client *client;
  40. const struct attribute_group *groups[3];
  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 sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  53. struct thermal_data *data = dev_get_drvdata(dev);
  54. int retval;
  55. retval = i2c_smbus_read_byte_data(data->client, sda->index);
  56. if (retval < 0)
  57. return retval;
  58. return sprintf(buf, "%d000\n", retval);
  59. }
  60. static ssize_t show_bit(struct device *dev,
  61. struct device_attribute *attr, char *buf)
  62. {
  63. struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
  64. struct thermal_data *data = dev_get_drvdata(dev);
  65. int retval;
  66. retval = i2c_smbus_read_byte_data(data->client, sda->nr);
  67. if (retval < 0)
  68. return retval;
  69. return sprintf(buf, "%d\n", !!(retval & sda->index));
  70. }
  71. static ssize_t store_temp(struct device *dev,
  72. struct device_attribute *attr, const char *buf, size_t count)
  73. {
  74. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  75. struct thermal_data *data = dev_get_drvdata(dev);
  76. unsigned long val;
  77. int retval;
  78. if (kstrtoul(buf, 10, &val))
  79. return -EINVAL;
  80. retval = i2c_smbus_write_byte_data(data->client, sda->index,
  81. DIV_ROUND_CLOSEST(val, 1000));
  82. if (retval < 0)
  83. return retval;
  84. return count;
  85. }
  86. static ssize_t store_bit(struct device *dev,
  87. struct device_attribute *attr, const char *buf, size_t count)
  88. {
  89. struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
  90. struct thermal_data *data = dev_get_drvdata(dev);
  91. struct i2c_client *client = data->client;
  92. unsigned long val;
  93. int retval;
  94. if (kstrtoul(buf, 10, &val))
  95. return -EINVAL;
  96. mutex_lock(&data->mutex);
  97. retval = i2c_smbus_read_byte_data(client, sda->nr);
  98. if (retval < 0)
  99. goto fail;
  100. retval &= ~sda->index;
  101. if (val)
  102. retval |= sda->index;
  103. retval = i2c_smbus_write_byte_data(client, sda->index, retval);
  104. if (retval == 0)
  105. retval = count;
  106. fail:
  107. mutex_unlock(&data->mutex);
  108. return retval;
  109. }
  110. static ssize_t show_hyst(struct device *dev,
  111. struct device_attribute *attr, char *buf)
  112. {
  113. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  114. struct thermal_data *data = dev_get_drvdata(dev);
  115. struct i2c_client *client = data->client;
  116. int retval;
  117. int hyst;
  118. retval = i2c_smbus_read_byte_data(client, sda->index);
  119. if (retval < 0)
  120. return retval;
  121. if (time_after(jiffies, data->hyst_valid)) {
  122. hyst = i2c_smbus_read_byte_data(client, 0x21);
  123. if (hyst < 0)
  124. return retval;
  125. data->cached_hyst = hyst;
  126. data->hyst_valid = jiffies + HZ;
  127. }
  128. return sprintf(buf, "%d000\n", retval - data->cached_hyst);
  129. }
  130. static ssize_t store_hyst(struct device *dev,
  131. struct device_attribute *attr, const char *buf, size_t count)
  132. {
  133. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  134. struct thermal_data *data = dev_get_drvdata(dev);
  135. struct i2c_client *client = data->client;
  136. int retval;
  137. int hyst;
  138. unsigned long val;
  139. if (kstrtoul(buf, 10, &val))
  140. return -EINVAL;
  141. mutex_lock(&data->mutex);
  142. retval = i2c_smbus_read_byte_data(client, sda->index);
  143. if (retval < 0)
  144. goto fail;
  145. hyst = val - retval * 1000;
  146. hyst = DIV_ROUND_CLOSEST(hyst, 1000);
  147. if (hyst < 0 || hyst > 255) {
  148. retval = -ERANGE;
  149. goto fail;
  150. }
  151. retval = i2c_smbus_write_byte_data(client, 0x21, hyst);
  152. if (retval == 0) {
  153. retval = count;
  154. data->cached_hyst = hyst;
  155. data->hyst_valid = jiffies + HZ;
  156. }
  157. fail:
  158. mutex_unlock(&data->mutex);
  159. return retval;
  160. }
  161. /*
  162. * Sensors. We pass the actual i2c register to the methods.
  163. */
  164. static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR,
  165. show_temp, store_temp, 0x06);
  166. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  167. show_temp, store_temp, 0x05);
  168. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  169. show_temp, store_temp, 0x20);
  170. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0x00);
  171. static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO,
  172. show_bit, NULL, 0x36, 0x01);
  173. static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO,
  174. show_bit, NULL, 0x35, 0x01);
  175. static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO,
  176. show_bit, NULL, 0x37, 0x01);
  177. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR,
  178. show_hyst, store_hyst, 0x20);
  179. static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR,
  180. show_temp, store_temp, 0x08);
  181. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
  182. show_temp, store_temp, 0x07);
  183. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
  184. show_temp, store_temp, 0x19);
  185. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0x01);
  186. static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO,
  187. show_bit, NULL, 0x36, 0x02);
  188. static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO,
  189. show_bit, NULL, 0x35, 0x02);
  190. static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO,
  191. show_bit, NULL, 0x37, 0x02);
  192. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO | S_IWUSR,
  193. show_hyst, store_hyst, 0x19);
  194. static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR,
  195. show_temp, store_temp, 0x16);
  196. static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
  197. show_temp, store_temp, 0x15);
  198. static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
  199. show_temp, store_temp, 0x1A);
  200. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 0x23);
  201. static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO,
  202. show_bit, NULL, 0x36, 0x04);
  203. static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO,
  204. show_bit, NULL, 0x35, 0x04);
  205. static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO,
  206. show_bit, NULL, 0x37, 0x04);
  207. static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO | S_IWUSR,
  208. show_hyst, store_hyst, 0x1A);
  209. static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO | S_IWUSR,
  210. show_temp, store_temp, 0x2D);
  211. static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO | S_IWUSR,
  212. show_temp, store_temp, 0x2C);
  213. static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
  214. show_temp, store_temp, 0x30);
  215. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 0x2A);
  216. static SENSOR_DEVICE_ATTR_2(temp4_min_alarm, S_IRUGO,
  217. show_bit, NULL, 0x36, 0x08);
  218. static SENSOR_DEVICE_ATTR_2(temp4_max_alarm, S_IRUGO,
  219. show_bit, NULL, 0x35, 0x08);
  220. static SENSOR_DEVICE_ATTR_2(temp4_crit_alarm, S_IRUGO,
  221. show_bit, NULL, 0x37, 0x08);
  222. static SENSOR_DEVICE_ATTR(temp4_crit_hyst, S_IRUGO | S_IWUSR,
  223. show_hyst, store_hyst, 0x30);
  224. static SENSOR_DEVICE_ATTR_2(power_state, S_IRUGO | S_IWUSR,
  225. show_bit, store_bit, 0x03, 0x40);
  226. static struct attribute *emc1403_attrs[] = {
  227. &sensor_dev_attr_temp1_min.dev_attr.attr,
  228. &sensor_dev_attr_temp1_max.dev_attr.attr,
  229. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  230. &sensor_dev_attr_temp1_input.dev_attr.attr,
  231. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  232. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  233. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  234. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  235. &sensor_dev_attr_temp2_min.dev_attr.attr,
  236. &sensor_dev_attr_temp2_max.dev_attr.attr,
  237. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  238. &sensor_dev_attr_temp2_input.dev_attr.attr,
  239. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  240. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  241. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  242. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  243. &sensor_dev_attr_temp3_min.dev_attr.attr,
  244. &sensor_dev_attr_temp3_max.dev_attr.attr,
  245. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  246. &sensor_dev_attr_temp3_input.dev_attr.attr,
  247. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  248. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  249. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  250. &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
  251. &sensor_dev_attr_power_state.dev_attr.attr,
  252. NULL
  253. };
  254. static const struct attribute_group emc1403_group = {
  255. .attrs = emc1403_attrs,
  256. };
  257. static struct attribute *emc1404_attrs[] = {
  258. &sensor_dev_attr_temp4_min.dev_attr.attr,
  259. &sensor_dev_attr_temp4_max.dev_attr.attr,
  260. &sensor_dev_attr_temp4_crit.dev_attr.attr,
  261. &sensor_dev_attr_temp4_input.dev_attr.attr,
  262. &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
  263. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  264. &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
  265. &sensor_dev_attr_temp4_crit_hyst.dev_attr.attr,
  266. NULL
  267. };
  268. static const struct attribute_group emc1404_group = {
  269. .attrs = emc1404_attrs,
  270. };
  271. static int emc1403_detect(struct i2c_client *client,
  272. struct i2c_board_info *info)
  273. {
  274. int id;
  275. /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
  276. id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
  277. if (id != 0x5d)
  278. return -ENODEV;
  279. id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
  280. switch (id) {
  281. case 0x21:
  282. strlcpy(info->type, "emc1403", I2C_NAME_SIZE);
  283. break;
  284. case 0x23:
  285. strlcpy(info->type, "emc1423", I2C_NAME_SIZE);
  286. break;
  287. case 0x25:
  288. strlcpy(info->type, "emc1404", I2C_NAME_SIZE);
  289. break;
  290. case 0x27:
  291. strlcpy(info->type, "emc1424", I2C_NAME_SIZE);
  292. break;
  293. default:
  294. return -ENODEV;
  295. }
  296. id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
  297. if (id != 0x01)
  298. return -ENODEV;
  299. return 0;
  300. }
  301. static int emc1403_probe(struct i2c_client *client,
  302. const struct i2c_device_id *id)
  303. {
  304. struct thermal_data *data;
  305. struct device *hwmon_dev;
  306. data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
  307. GFP_KERNEL);
  308. if (data == NULL)
  309. return -ENOMEM;
  310. data->client = client;
  311. mutex_init(&data->mutex);
  312. data->hyst_valid = jiffies - 1; /* Expired */
  313. data->groups[0] = &emc1403_group;
  314. if (id->driver_data)
  315. data->groups[1] = &emc1404_group;
  316. hwmon_dev = hwmon_device_register_with_groups(&client->dev,
  317. client->name, data,
  318. data->groups);
  319. if (IS_ERR(hwmon_dev))
  320. return PTR_ERR(hwmon_dev);
  321. dev_info(&client->dev, "%s Thermal chip found\n", id->name);
  322. return 0;
  323. }
  324. static const unsigned short emc1403_address_list[] = {
  325. 0x18, 0x29, 0x4c, 0x4d, I2C_CLIENT_END
  326. };
  327. static const struct i2c_device_id emc1403_idtable[] = {
  328. { "emc1403", 0 },
  329. { "emc1404", 1 },
  330. { "emc1423", 0 },
  331. { "emc1424", 1 },
  332. { }
  333. };
  334. MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
  335. static struct i2c_driver sensor_emc1403 = {
  336. .class = I2C_CLASS_HWMON,
  337. .driver = {
  338. .name = "emc1403",
  339. },
  340. .detect = emc1403_detect,
  341. .probe = emc1403_probe,
  342. .id_table = emc1403_idtable,
  343. .address_list = emc1403_address_list,
  344. };
  345. module_i2c_driver(sensor_emc1403);
  346. MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
  347. MODULE_DESCRIPTION("emc1403 Thermal Driver");
  348. MODULE_LICENSE("GPL v2");