max1668.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (c) 2011 David George <david.george@ska.ac.za>
  3. *
  4. * based on adm1021.c
  5. * some credit to Christoph Scheurer, but largely a rewrite
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/i2c.h>
  26. #include <linux/hwmon.h>
  27. #include <linux/hwmon-sysfs.h>
  28. #include <linux/err.h>
  29. #include <linux/mutex.h>
  30. /* Addresses to scan */
  31. static const unsigned short max1668_addr_list[] = {
  32. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  33. /* max1668 registers */
  34. #define MAX1668_REG_TEMP(nr) (nr)
  35. #define MAX1668_REG_STAT1 0x05
  36. #define MAX1668_REG_STAT2 0x06
  37. #define MAX1668_REG_MAN_ID 0xfe
  38. #define MAX1668_REG_DEV_ID 0xff
  39. /* limits */
  40. /* write high limits */
  41. #define MAX1668_REG_LIMH_WR(nr) (0x13 + 2 * (nr))
  42. /* write low limits */
  43. #define MAX1668_REG_LIML_WR(nr) (0x14 + 2 * (nr))
  44. /* read high limits */
  45. #define MAX1668_REG_LIMH_RD(nr) (0x08 + 2 * (nr))
  46. /* read low limits */
  47. #define MAX1668_REG_LIML_RD(nr) (0x09 + 2 * (nr))
  48. /* manufacturer and device ID Constants */
  49. #define MAN_ID_MAXIM 0x4d
  50. #define DEV_ID_MAX1668 0x3
  51. #define DEV_ID_MAX1805 0x5
  52. #define DEV_ID_MAX1989 0xb
  53. /* read only mode module parameter */
  54. static bool read_only;
  55. module_param(read_only, bool, 0);
  56. MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
  57. enum chips { max1668, max1805, max1989 };
  58. struct max1668_data {
  59. struct i2c_client *client;
  60. const struct attribute_group *groups[3];
  61. enum chips type;
  62. struct mutex update_lock;
  63. char valid; /* !=0 if following fields are valid */
  64. unsigned long last_updated; /* In jiffies */
  65. /* 1x local and 4x remote */
  66. s8 temp_max[5];
  67. s8 temp_min[5];
  68. s8 temp[5];
  69. u16 alarms;
  70. };
  71. static struct max1668_data *max1668_update_device(struct device *dev)
  72. {
  73. struct max1668_data *data = dev_get_drvdata(dev);
  74. struct i2c_client *client = data->client;
  75. struct max1668_data *ret = data;
  76. s32 val;
  77. int i;
  78. mutex_lock(&data->update_lock);
  79. if (data->valid && !time_after(jiffies,
  80. data->last_updated + HZ + HZ / 2))
  81. goto abort;
  82. for (i = 0; i < 5; i++) {
  83. val = i2c_smbus_read_byte_data(client, MAX1668_REG_TEMP(i));
  84. if (unlikely(val < 0)) {
  85. ret = ERR_PTR(val);
  86. goto abort;
  87. }
  88. data->temp[i] = (s8) val;
  89. val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIMH_RD(i));
  90. if (unlikely(val < 0)) {
  91. ret = ERR_PTR(val);
  92. goto abort;
  93. }
  94. data->temp_max[i] = (s8) val;
  95. val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIML_RD(i));
  96. if (unlikely(val < 0)) {
  97. ret = ERR_PTR(val);
  98. goto abort;
  99. }
  100. data->temp_min[i] = (s8) val;
  101. }
  102. val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT1);
  103. if (unlikely(val < 0)) {
  104. ret = ERR_PTR(val);
  105. goto abort;
  106. }
  107. data->alarms = val << 8;
  108. val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT2);
  109. if (unlikely(val < 0)) {
  110. ret = ERR_PTR(val);
  111. goto abort;
  112. }
  113. data->alarms |= val;
  114. data->last_updated = jiffies;
  115. data->valid = 1;
  116. abort:
  117. mutex_unlock(&data->update_lock);
  118. return ret;
  119. }
  120. static ssize_t show_temp(struct device *dev,
  121. struct device_attribute *devattr, char *buf)
  122. {
  123. int index = to_sensor_dev_attr(devattr)->index;
  124. struct max1668_data *data = max1668_update_device(dev);
  125. if (IS_ERR(data))
  126. return PTR_ERR(data);
  127. return sprintf(buf, "%d\n", data->temp[index] * 1000);
  128. }
  129. static ssize_t show_temp_max(struct device *dev,
  130. struct device_attribute *devattr, char *buf)
  131. {
  132. int index = to_sensor_dev_attr(devattr)->index;
  133. struct max1668_data *data = max1668_update_device(dev);
  134. if (IS_ERR(data))
  135. return PTR_ERR(data);
  136. return sprintf(buf, "%d\n", data->temp_max[index] * 1000);
  137. }
  138. static ssize_t show_temp_min(struct device *dev,
  139. struct device_attribute *devattr, char *buf)
  140. {
  141. int index = to_sensor_dev_attr(devattr)->index;
  142. struct max1668_data *data = max1668_update_device(dev);
  143. if (IS_ERR(data))
  144. return PTR_ERR(data);
  145. return sprintf(buf, "%d\n", data->temp_min[index] * 1000);
  146. }
  147. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  148. char *buf)
  149. {
  150. int index = to_sensor_dev_attr(attr)->index;
  151. struct max1668_data *data = max1668_update_device(dev);
  152. if (IS_ERR(data))
  153. return PTR_ERR(data);
  154. return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1);
  155. }
  156. static ssize_t show_fault(struct device *dev,
  157. struct device_attribute *devattr, char *buf)
  158. {
  159. int index = to_sensor_dev_attr(devattr)->index;
  160. struct max1668_data *data = max1668_update_device(dev);
  161. if (IS_ERR(data))
  162. return PTR_ERR(data);
  163. return sprintf(buf, "%u\n",
  164. (data->alarms & (1 << 12)) && data->temp[index] == 127);
  165. }
  166. static ssize_t set_temp_max(struct device *dev,
  167. struct device_attribute *devattr,
  168. const char *buf, size_t count)
  169. {
  170. int index = to_sensor_dev_attr(devattr)->index;
  171. struct max1668_data *data = dev_get_drvdata(dev);
  172. struct i2c_client *client = data->client;
  173. long temp;
  174. int ret;
  175. ret = kstrtol(buf, 10, &temp);
  176. if (ret < 0)
  177. return ret;
  178. mutex_lock(&data->update_lock);
  179. data->temp_max[index] = clamp_val(temp/1000, -128, 127);
  180. ret = i2c_smbus_write_byte_data(client,
  181. MAX1668_REG_LIMH_WR(index),
  182. data->temp_max[index]);
  183. if (ret < 0)
  184. count = ret;
  185. mutex_unlock(&data->update_lock);
  186. return count;
  187. }
  188. static ssize_t set_temp_min(struct device *dev,
  189. struct device_attribute *devattr,
  190. const char *buf, size_t count)
  191. {
  192. int index = to_sensor_dev_attr(devattr)->index;
  193. struct max1668_data *data = dev_get_drvdata(dev);
  194. struct i2c_client *client = data->client;
  195. long temp;
  196. int ret;
  197. ret = kstrtol(buf, 10, &temp);
  198. if (ret < 0)
  199. return ret;
  200. mutex_lock(&data->update_lock);
  201. data->temp_min[index] = clamp_val(temp/1000, -128, 127);
  202. ret = i2c_smbus_write_byte_data(client,
  203. MAX1668_REG_LIML_WR(index),
  204. data->temp_min[index]);
  205. if (ret < 0)
  206. count = ret;
  207. mutex_unlock(&data->update_lock);
  208. return count;
  209. }
  210. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  211. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max,
  212. set_temp_max, 0);
  213. static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min,
  214. set_temp_min, 0);
  215. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  216. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max,
  217. set_temp_max, 1);
  218. static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min,
  219. set_temp_min, 1);
  220. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  221. static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max,
  222. set_temp_max, 2);
  223. static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min,
  224. set_temp_min, 2);
  225. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  226. static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max,
  227. set_temp_max, 3);
  228. static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min,
  229. set_temp_min, 3);
  230. static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
  231. static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max,
  232. set_temp_max, 4);
  233. static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min,
  234. set_temp_min, 4);
  235. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14);
  236. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13);
  237. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7);
  238. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  239. static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  240. static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  241. static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  242. static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2);
  243. static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1);
  244. static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0);
  245. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1);
  246. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2);
  247. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3);
  248. static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_fault, NULL, 4);
  249. /* Attributes common to MAX1668, MAX1989 and MAX1805 */
  250. static struct attribute *max1668_attribute_common[] = {
  251. &sensor_dev_attr_temp1_max.dev_attr.attr,
  252. &sensor_dev_attr_temp1_min.dev_attr.attr,
  253. &sensor_dev_attr_temp1_input.dev_attr.attr,
  254. &sensor_dev_attr_temp2_max.dev_attr.attr,
  255. &sensor_dev_attr_temp2_min.dev_attr.attr,
  256. &sensor_dev_attr_temp2_input.dev_attr.attr,
  257. &sensor_dev_attr_temp3_max.dev_attr.attr,
  258. &sensor_dev_attr_temp3_min.dev_attr.attr,
  259. &sensor_dev_attr_temp3_input.dev_attr.attr,
  260. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  261. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  262. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  263. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  264. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  265. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  266. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  267. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  268. NULL
  269. };
  270. /* Attributes not present on MAX1805 */
  271. static struct attribute *max1668_attribute_unique[] = {
  272. &sensor_dev_attr_temp4_max.dev_attr.attr,
  273. &sensor_dev_attr_temp4_min.dev_attr.attr,
  274. &sensor_dev_attr_temp4_input.dev_attr.attr,
  275. &sensor_dev_attr_temp5_max.dev_attr.attr,
  276. &sensor_dev_attr_temp5_min.dev_attr.attr,
  277. &sensor_dev_attr_temp5_input.dev_attr.attr,
  278. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  279. &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
  280. &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
  281. &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
  282. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  283. &sensor_dev_attr_temp5_fault.dev_attr.attr,
  284. NULL
  285. };
  286. static umode_t max1668_attribute_mode(struct kobject *kobj,
  287. struct attribute *attr, int index)
  288. {
  289. umode_t ret = S_IRUGO;
  290. if (read_only)
  291. return ret;
  292. if (attr == &sensor_dev_attr_temp1_max.dev_attr.attr ||
  293. attr == &sensor_dev_attr_temp2_max.dev_attr.attr ||
  294. attr == &sensor_dev_attr_temp3_max.dev_attr.attr ||
  295. attr == &sensor_dev_attr_temp4_max.dev_attr.attr ||
  296. attr == &sensor_dev_attr_temp5_max.dev_attr.attr ||
  297. attr == &sensor_dev_attr_temp1_min.dev_attr.attr ||
  298. attr == &sensor_dev_attr_temp2_min.dev_attr.attr ||
  299. attr == &sensor_dev_attr_temp3_min.dev_attr.attr ||
  300. attr == &sensor_dev_attr_temp4_min.dev_attr.attr ||
  301. attr == &sensor_dev_attr_temp5_min.dev_attr.attr)
  302. ret |= S_IWUSR;
  303. return ret;
  304. }
  305. static const struct attribute_group max1668_group_common = {
  306. .attrs = max1668_attribute_common,
  307. .is_visible = max1668_attribute_mode
  308. };
  309. static const struct attribute_group max1668_group_unique = {
  310. .attrs = max1668_attribute_unique,
  311. .is_visible = max1668_attribute_mode
  312. };
  313. /* Return 0 if detection is successful, -ENODEV otherwise */
  314. static int max1668_detect(struct i2c_client *client,
  315. struct i2c_board_info *info)
  316. {
  317. struct i2c_adapter *adapter = client->adapter;
  318. const char *type_name;
  319. int man_id, dev_id;
  320. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  321. return -ENODEV;
  322. /* Check for unsupported part */
  323. man_id = i2c_smbus_read_byte_data(client, MAX1668_REG_MAN_ID);
  324. if (man_id != MAN_ID_MAXIM)
  325. return -ENODEV;
  326. dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID);
  327. if (dev_id < 0)
  328. return -ENODEV;
  329. type_name = NULL;
  330. if (dev_id == DEV_ID_MAX1668)
  331. type_name = "max1668";
  332. else if (dev_id == DEV_ID_MAX1805)
  333. type_name = "max1805";
  334. else if (dev_id == DEV_ID_MAX1989)
  335. type_name = "max1989";
  336. if (!type_name)
  337. return -ENODEV;
  338. strlcpy(info->type, type_name, I2C_NAME_SIZE);
  339. return 0;
  340. }
  341. static int max1668_probe(struct i2c_client *client,
  342. const struct i2c_device_id *id)
  343. {
  344. struct i2c_adapter *adapter = client->adapter;
  345. struct device *dev = &client->dev;
  346. struct device *hwmon_dev;
  347. struct max1668_data *data;
  348. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  349. return -ENODEV;
  350. data = devm_kzalloc(dev, sizeof(struct max1668_data), GFP_KERNEL);
  351. if (!data)
  352. return -ENOMEM;
  353. data->client = client;
  354. data->type = id->driver_data;
  355. mutex_init(&data->update_lock);
  356. /* sysfs hooks */
  357. data->groups[0] = &max1668_group_common;
  358. if (data->type == max1668 || data->type == max1989)
  359. data->groups[1] = &max1668_group_unique;
  360. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  361. data, data->groups);
  362. return PTR_ERR_OR_ZERO(hwmon_dev);
  363. }
  364. static const struct i2c_device_id max1668_id[] = {
  365. { "max1668", max1668 },
  366. { "max1805", max1805 },
  367. { "max1989", max1989 },
  368. { }
  369. };
  370. MODULE_DEVICE_TABLE(i2c, max1668_id);
  371. /* This is the driver that will be inserted */
  372. static struct i2c_driver max1668_driver = {
  373. .class = I2C_CLASS_HWMON,
  374. .driver = {
  375. .name = "max1668",
  376. },
  377. .probe = max1668_probe,
  378. .id_table = max1668_id,
  379. .detect = max1668_detect,
  380. .address_list = max1668_addr_list,
  381. };
  382. module_i2c_driver(max1668_driver);
  383. MODULE_AUTHOR("David George <david.george@ska.ac.za>");
  384. MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver");
  385. MODULE_LICENSE("GPL");