max6642.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 Oleksij Rempel <bug-track@fisher-privat.net>
  11. * Jean Delvare <jdelvare@suse.de>
  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 i2c_client *client;
  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 max6642_data *data,
  92. struct i2c_client *client)
  93. {
  94. u8 config;
  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 max6642_data *data = dev_get_drvdata(dev);
  146. struct i2c_client *client = data->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(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 sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  180. struct max6642_data *data = max6642_update_device(dev);
  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 sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
  188. struct max6642_data *data = max6642_update_device(dev);
  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. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
  195. struct max6642_data *data = dev_get_drvdata(dev);
  196. unsigned long val;
  197. int err;
  198. err = kstrtoul(buf, 10, &val);
  199. if (err < 0)
  200. return err;
  201. mutex_lock(&data->update_lock);
  202. data->temp_high[attr2->nr] = clamp_val(temp_to_reg(val), 0, 255);
  203. i2c_smbus_write_byte_data(data->client, attr2->index,
  204. data->temp_high[attr2->nr]);
  205. mutex_unlock(&data->update_lock);
  206. return count;
  207. }
  208. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  209. char *buf)
  210. {
  211. int bitnr = to_sensor_dev_attr(attr)->index;
  212. struct max6642_data *data = max6642_update_device(dev);
  213. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  214. }
  215. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_max10, NULL, 0);
  216. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_max10, NULL, 1);
  217. static SENSOR_DEVICE_ATTR_2(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  218. set_temp_max, 0, MAX6642_REG_W_LOCAL_HIGH);
  219. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  220. set_temp_max, 1, MAX6642_REG_W_REMOTE_HIGH);
  221. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  222. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  223. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  224. static struct attribute *max6642_attrs[] = {
  225. &sensor_dev_attr_temp1_input.dev_attr.attr,
  226. &sensor_dev_attr_temp2_input.dev_attr.attr,
  227. &sensor_dev_attr_temp1_max.dev_attr.attr,
  228. &sensor_dev_attr_temp2_max.dev_attr.attr,
  229. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  230. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  231. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  232. NULL
  233. };
  234. ATTRIBUTE_GROUPS(max6642);
  235. static int max6642_probe(struct i2c_client *client,
  236. const struct i2c_device_id *id)
  237. {
  238. struct device *dev = &client->dev;
  239. struct max6642_data *data;
  240. struct device *hwmon_dev;
  241. data = devm_kzalloc(dev, sizeof(struct max6642_data), GFP_KERNEL);
  242. if (!data)
  243. return -ENOMEM;
  244. data->client = client;
  245. mutex_init(&data->update_lock);
  246. /* Initialize the MAX6642 chip */
  247. max6642_init_client(data, client);
  248. hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
  249. client->name, data,
  250. max6642_groups);
  251. return PTR_ERR_OR_ZERO(hwmon_dev);
  252. }
  253. /*
  254. * Driver data (common to all clients)
  255. */
  256. static const struct i2c_device_id max6642_id[] = {
  257. { "max6642", 0 },
  258. { }
  259. };
  260. MODULE_DEVICE_TABLE(i2c, max6642_id);
  261. static struct i2c_driver max6642_driver = {
  262. .class = I2C_CLASS_HWMON,
  263. .driver = {
  264. .name = "max6642",
  265. },
  266. .probe = max6642_probe,
  267. .id_table = max6642_id,
  268. .detect = max6642_detect,
  269. .address_list = normal_i2c,
  270. };
  271. module_i2c_driver(max6642_driver);
  272. MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
  273. MODULE_DESCRIPTION("MAX6642 sensor driver");
  274. MODULE_LICENSE("GPL");