max6642.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Driver for +/-1 degree C, SMBus-Compatible Remote/Local Temperature Sensor
  3. * with Overtemperature Alarm
  4. *
  5. * Copyright (C) 2011 AppearTV AS
  6. *
  7. * Derived from:
  8. *
  9. * Based on the max1619 driver.
  10. * Copyright (C) 2003-2004 Alexey Fisher <fishor@mail.ru>
  11. * Jean Delvare <khali@linux-fr.org>
  12. *
  13. * The MAX6642 is a sensor chip made by Maxim.
  14. * It reports up to two temperatures (its own plus up to
  15. * one external one). Complete datasheet can be
  16. * obtained from Maxim's website at:
  17. * http://datasheets.maxim-ic.com/en/ds/MAX6642.pdf
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/i2c.h>
  38. #include <linux/hwmon.h>
  39. #include <linux/hwmon-sysfs.h>
  40. #include <linux/err.h>
  41. #include <linux/mutex.h>
  42. #include <linux/sysfs.h>
  43. static const unsigned short normal_i2c[] = {
  44. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  45. /*
  46. * The MAX6642 registers
  47. */
  48. #define MAX6642_REG_R_MAN_ID 0xFE
  49. #define MAX6642_REG_R_CONFIG 0x03
  50. #define MAX6642_REG_W_CONFIG 0x09
  51. #define MAX6642_REG_R_STATUS 0x02
  52. #define MAX6642_REG_R_LOCAL_TEMP 0x00
  53. #define MAX6642_REG_R_LOCAL_TEMPL 0x11
  54. #define MAX6642_REG_R_LOCAL_HIGH 0x05
  55. #define MAX6642_REG_W_LOCAL_HIGH 0x0B
  56. #define MAX6642_REG_R_REMOTE_TEMP 0x01
  57. #define MAX6642_REG_R_REMOTE_TEMPL 0x10
  58. #define MAX6642_REG_R_REMOTE_HIGH 0x07
  59. #define MAX6642_REG_W_REMOTE_HIGH 0x0D
  60. /*
  61. * Conversions
  62. */
  63. static int temp_from_reg10(int val)
  64. {
  65. return val * 250;
  66. }
  67. static int temp_from_reg(int val)
  68. {
  69. return val * 1000;
  70. }
  71. static int temp_to_reg(int val)
  72. {
  73. return val / 1000;
  74. }
  75. /*
  76. * Client data (each client gets its own)
  77. */
  78. struct max6642_data {
  79. struct device *hwmon_dev;
  80. struct mutex update_lock;
  81. bool valid; /* zero until following fields are valid */
  82. unsigned long last_updated; /* in jiffies */
  83. /* registers values */
  84. u16 temp_input[2]; /* local/remote */
  85. u16 temp_high[2]; /* local/remote */
  86. u8 alarms;
  87. };
  88. /*
  89. * Real code
  90. */
  91. static void max6642_init_client(struct i2c_client *client)
  92. {
  93. u8 config;
  94. struct max6642_data *data = i2c_get_clientdata(client);
  95. /*
  96. * Start the conversions.
  97. */
  98. config = i2c_smbus_read_byte_data(client, MAX6642_REG_R_CONFIG);
  99. if (config & 0x40)
  100. i2c_smbus_write_byte_data(client, MAX6642_REG_W_CONFIG,
  101. config & 0xBF); /* run */
  102. data->temp_high[0] = i2c_smbus_read_byte_data(client,
  103. MAX6642_REG_R_LOCAL_HIGH);
  104. data->temp_high[1] = i2c_smbus_read_byte_data(client,
  105. MAX6642_REG_R_REMOTE_HIGH);
  106. }
  107. /* Return 0 if detection is successful, -ENODEV otherwise */
  108. static int max6642_detect(struct i2c_client *client,
  109. struct i2c_board_info *info)
  110. {
  111. struct i2c_adapter *adapter = client->adapter;
  112. u8 reg_config, reg_status, man_id;
  113. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  114. return -ENODEV;
  115. /* identification */
  116. man_id = i2c_smbus_read_byte_data(client, MAX6642_REG_R_MAN_ID);
  117. if (man_id != 0x4D)
  118. return -ENODEV;
  119. /* sanity check */
  120. if (i2c_smbus_read_byte_data(client, 0x04) != 0x4D
  121. || i2c_smbus_read_byte_data(client, 0x06) != 0x4D
  122. || i2c_smbus_read_byte_data(client, 0xff) != 0x4D)
  123. return -ENODEV;
  124. /*
  125. * We read the config and status register, the 4 lower bits in the
  126. * config register should be zero and bit 5, 3, 1 and 0 should be
  127. * zero in the status register.
  128. */
  129. reg_config = i2c_smbus_read_byte_data(client, MAX6642_REG_R_CONFIG);
  130. if ((reg_config & 0x0f) != 0x00)
  131. return -ENODEV;
  132. /* in between, another round of sanity checks */
  133. if (i2c_smbus_read_byte_data(client, 0x04) != reg_config
  134. || i2c_smbus_read_byte_data(client, 0x06) != reg_config
  135. || i2c_smbus_read_byte_data(client, 0xff) != reg_config)
  136. return -ENODEV;
  137. reg_status = i2c_smbus_read_byte_data(client, MAX6642_REG_R_STATUS);
  138. if ((reg_status & 0x2b) != 0x00)
  139. return -ENODEV;
  140. strlcpy(info->type, "max6642", I2C_NAME_SIZE);
  141. return 0;
  142. }
  143. static struct max6642_data *max6642_update_device(struct device *dev)
  144. {
  145. struct i2c_client *client = to_i2c_client(dev);
  146. struct max6642_data *data = i2c_get_clientdata(client);
  147. u16 val, tmp;
  148. mutex_lock(&data->update_lock);
  149. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  150. dev_dbg(&client->dev, "Updating max6642 data.\n");
  151. val = i2c_smbus_read_byte_data(client,
  152. MAX6642_REG_R_LOCAL_TEMPL);
  153. tmp = (val >> 6) & 3;
  154. val = i2c_smbus_read_byte_data(client,
  155. MAX6642_REG_R_LOCAL_TEMP);
  156. val = (val << 2) | tmp;
  157. data->temp_input[0] = val;
  158. val = i2c_smbus_read_byte_data(client,
  159. MAX6642_REG_R_REMOTE_TEMPL);
  160. tmp = (val >> 6) & 3;
  161. val = i2c_smbus_read_byte_data(client,
  162. MAX6642_REG_R_REMOTE_TEMP);
  163. val = (val << 2) | tmp;
  164. data->temp_input[1] = val;
  165. data->alarms = i2c_smbus_read_byte_data(client,
  166. MAX6642_REG_R_STATUS);
  167. data->last_updated = jiffies;
  168. data->valid = 1;
  169. }
  170. mutex_unlock(&data->update_lock);
  171. return data;
  172. }
  173. /*
  174. * Sysfs stuff
  175. */
  176. static ssize_t show_temp_max10(struct device *dev,
  177. struct device_attribute *dev_attr, char *buf)
  178. {
  179. struct max6642_data *data = max6642_update_device(dev);
  180. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  181. return sprintf(buf, "%d\n",
  182. temp_from_reg10(data->temp_input[attr->index]));
  183. }
  184. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  185. char *buf)
  186. {
  187. struct max6642_data *data = max6642_update_device(dev);
  188. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
  189. return sprintf(buf, "%d\n", temp_from_reg(data->temp_high[attr2->nr]));
  190. }
  191. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  192. const char *buf, size_t count)
  193. {
  194. unsigned long val;
  195. int err;
  196. struct i2c_client *client = to_i2c_client(dev);
  197. struct max6642_data *data = i2c_get_clientdata(client);
  198. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
  199. err = strict_strtoul(buf, 10, &val);
  200. if (err < 0)
  201. return err;
  202. mutex_lock(&data->update_lock);
  203. data->temp_high[attr2->nr] = SENSORS_LIMIT(temp_to_reg(val), 0, 255);
  204. i2c_smbus_write_byte_data(client, attr2->index,
  205. data->temp_high[attr2->nr]);
  206. mutex_unlock(&data->update_lock);
  207. return count;
  208. }
  209. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  210. char *buf)
  211. {
  212. int bitnr = to_sensor_dev_attr(attr)->index;
  213. struct max6642_data *data = max6642_update_device(dev);
  214. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  215. }
  216. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_max10, NULL, 0);
  217. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_max10, NULL, 1);
  218. static SENSOR_DEVICE_ATTR_2(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  219. set_temp_max, 0, MAX6642_REG_W_LOCAL_HIGH);
  220. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  221. set_temp_max, 1, MAX6642_REG_W_REMOTE_HIGH);
  222. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  223. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  224. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  225. static struct attribute *max6642_attributes[] = {
  226. &sensor_dev_attr_temp1_input.dev_attr.attr,
  227. &sensor_dev_attr_temp2_input.dev_attr.attr,
  228. &sensor_dev_attr_temp1_max.dev_attr.attr,
  229. &sensor_dev_attr_temp2_max.dev_attr.attr,
  230. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  231. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  232. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  233. NULL
  234. };
  235. static const struct attribute_group max6642_group = {
  236. .attrs = max6642_attributes,
  237. };
  238. static int max6642_probe(struct i2c_client *new_client,
  239. const struct i2c_device_id *id)
  240. {
  241. struct max6642_data *data;
  242. int err;
  243. data = kzalloc(sizeof(struct max6642_data), GFP_KERNEL);
  244. if (!data) {
  245. err = -ENOMEM;
  246. goto exit;
  247. }
  248. i2c_set_clientdata(new_client, data);
  249. mutex_init(&data->update_lock);
  250. /* Initialize the MAX6642 chip */
  251. max6642_init_client(new_client);
  252. /* Register sysfs hooks */
  253. err = sysfs_create_group(&new_client->dev.kobj, &max6642_group);
  254. if (err)
  255. goto exit_free;
  256. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  257. if (IS_ERR(data->hwmon_dev)) {
  258. err = PTR_ERR(data->hwmon_dev);
  259. goto exit_remove_files;
  260. }
  261. return 0;
  262. exit_remove_files:
  263. sysfs_remove_group(&new_client->dev.kobj, &max6642_group);
  264. exit_free:
  265. kfree(data);
  266. exit:
  267. return err;
  268. }
  269. static int max6642_remove(struct i2c_client *client)
  270. {
  271. struct max6642_data *data = i2c_get_clientdata(client);
  272. hwmon_device_unregister(data->hwmon_dev);
  273. sysfs_remove_group(&client->dev.kobj, &max6642_group);
  274. kfree(data);
  275. return 0;
  276. }
  277. /*
  278. * Driver data (common to all clients)
  279. */
  280. static const struct i2c_device_id max6642_id[] = {
  281. { "max6642", 0 },
  282. { }
  283. };
  284. MODULE_DEVICE_TABLE(i2c, max6642_id);
  285. static struct i2c_driver max6642_driver = {
  286. .class = I2C_CLASS_HWMON,
  287. .driver = {
  288. .name = "max6642",
  289. },
  290. .probe = max6642_probe,
  291. .remove = max6642_remove,
  292. .id_table = max6642_id,
  293. .detect = max6642_detect,
  294. .address_list = normal_i2c,
  295. };
  296. static int __init max6642_init(void)
  297. {
  298. return i2c_add_driver(&max6642_driver);
  299. }
  300. static void __exit max6642_exit(void)
  301. {
  302. i2c_del_driver(&max6642_driver);
  303. }
  304. MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
  305. MODULE_DESCRIPTION("MAX6642 sensor driver");
  306. MODULE_LICENSE("GPL");
  307. module_init(max6642_init);
  308. module_exit(max6642_exit);