max6642.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for +/-1 degree C, SMBus-Compatible Remote/Local Temperature Sensor
  4. * with Overtemperature Alarm
  5. *
  6. * Copyright (C) 2011 AppearTV AS
  7. *
  8. * Derived from:
  9. *
  10. * Based on the max1619 driver.
  11. * Copyright (C) 2003-2004 Oleksij Rempel <[email protected]>
  12. * Jean Delvare <[email protected]>
  13. *
  14. * The MAX6642 is a sensor chip made by Maxim.
  15. * It reports up to two temperatures (its own plus up to
  16. * one external one). Complete datasheet can be
  17. * obtained from Maxim's website at:
  18. * http://datasheets.maxim-ic.com/en/ds/MAX6642.pdf
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/i2c.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/hwmon-sysfs.h>
  27. #include <linux/err.h>
  28. #include <linux/mutex.h>
  29. #include <linux/sysfs.h>
  30. static const unsigned short normal_i2c[] = {
  31. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  32. /*
  33. * The MAX6642 registers
  34. */
  35. #define MAX6642_REG_R_MAN_ID 0xFE
  36. #define MAX6642_REG_R_CONFIG 0x03
  37. #define MAX6642_REG_W_CONFIG 0x09
  38. #define MAX6642_REG_R_STATUS 0x02
  39. #define MAX6642_REG_R_LOCAL_TEMP 0x00
  40. #define MAX6642_REG_R_LOCAL_TEMPL 0x11
  41. #define MAX6642_REG_R_LOCAL_HIGH 0x05
  42. #define MAX6642_REG_W_LOCAL_HIGH 0x0B
  43. #define MAX6642_REG_R_REMOTE_TEMP 0x01
  44. #define MAX6642_REG_R_REMOTE_TEMPL 0x10
  45. #define MAX6642_REG_R_REMOTE_HIGH 0x07
  46. #define MAX6642_REG_W_REMOTE_HIGH 0x0D
  47. /*
  48. * Conversions
  49. */
  50. static int temp_from_reg10(int val)
  51. {
  52. return val * 250;
  53. }
  54. static int temp_from_reg(int val)
  55. {
  56. return val * 1000;
  57. }
  58. static int temp_to_reg(int val)
  59. {
  60. return val / 1000;
  61. }
  62. /*
  63. * Client data (each client gets its own)
  64. */
  65. struct max6642_data {
  66. struct i2c_client *client;
  67. struct mutex update_lock;
  68. bool valid; /* zero until following fields are valid */
  69. unsigned long last_updated; /* in jiffies */
  70. /* registers values */
  71. u16 temp_input[2]; /* local/remote */
  72. u16 temp_high[2]; /* local/remote */
  73. u8 alarms;
  74. };
  75. /*
  76. * Real code
  77. */
  78. static void max6642_init_client(struct max6642_data *data,
  79. struct i2c_client *client)
  80. {
  81. u8 config;
  82. /*
  83. * Start the conversions.
  84. */
  85. config = i2c_smbus_read_byte_data(client, MAX6642_REG_R_CONFIG);
  86. if (config & 0x40)
  87. i2c_smbus_write_byte_data(client, MAX6642_REG_W_CONFIG,
  88. config & 0xBF); /* run */
  89. data->temp_high[0] = i2c_smbus_read_byte_data(client,
  90. MAX6642_REG_R_LOCAL_HIGH);
  91. data->temp_high[1] = i2c_smbus_read_byte_data(client,
  92. MAX6642_REG_R_REMOTE_HIGH);
  93. }
  94. /* Return 0 if detection is successful, -ENODEV otherwise */
  95. static int max6642_detect(struct i2c_client *client,
  96. struct i2c_board_info *info)
  97. {
  98. struct i2c_adapter *adapter = client->adapter;
  99. u8 reg_config, reg_status, man_id;
  100. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  101. return -ENODEV;
  102. /* identification */
  103. man_id = i2c_smbus_read_byte_data(client, MAX6642_REG_R_MAN_ID);
  104. if (man_id != 0x4D)
  105. return -ENODEV;
  106. /* sanity check */
  107. if (i2c_smbus_read_byte_data(client, 0x04) != 0x4D
  108. || i2c_smbus_read_byte_data(client, 0x06) != 0x4D
  109. || i2c_smbus_read_byte_data(client, 0xff) != 0x4D)
  110. return -ENODEV;
  111. /*
  112. * We read the config and status register, the 4 lower bits in the
  113. * config register should be zero and bit 5, 3, 1 and 0 should be
  114. * zero in the status register.
  115. */
  116. reg_config = i2c_smbus_read_byte_data(client, MAX6642_REG_R_CONFIG);
  117. if ((reg_config & 0x0f) != 0x00)
  118. return -ENODEV;
  119. /* in between, another round of sanity checks */
  120. if (i2c_smbus_read_byte_data(client, 0x04) != reg_config
  121. || i2c_smbus_read_byte_data(client, 0x06) != reg_config
  122. || i2c_smbus_read_byte_data(client, 0xff) != reg_config)
  123. return -ENODEV;
  124. reg_status = i2c_smbus_read_byte_data(client, MAX6642_REG_R_STATUS);
  125. if ((reg_status & 0x2b) != 0x00)
  126. return -ENODEV;
  127. strscpy(info->type, "max6642", I2C_NAME_SIZE);
  128. return 0;
  129. }
  130. static struct max6642_data *max6642_update_device(struct device *dev)
  131. {
  132. struct max6642_data *data = dev_get_drvdata(dev);
  133. struct i2c_client *client = data->client;
  134. u16 val, tmp;
  135. mutex_lock(&data->update_lock);
  136. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  137. dev_dbg(dev, "Updating max6642 data.\n");
  138. val = i2c_smbus_read_byte_data(client,
  139. MAX6642_REG_R_LOCAL_TEMPL);
  140. tmp = (val >> 6) & 3;
  141. val = i2c_smbus_read_byte_data(client,
  142. MAX6642_REG_R_LOCAL_TEMP);
  143. val = (val << 2) | tmp;
  144. data->temp_input[0] = val;
  145. val = i2c_smbus_read_byte_data(client,
  146. MAX6642_REG_R_REMOTE_TEMPL);
  147. tmp = (val >> 6) & 3;
  148. val = i2c_smbus_read_byte_data(client,
  149. MAX6642_REG_R_REMOTE_TEMP);
  150. val = (val << 2) | tmp;
  151. data->temp_input[1] = val;
  152. data->alarms = i2c_smbus_read_byte_data(client,
  153. MAX6642_REG_R_STATUS);
  154. data->last_updated = jiffies;
  155. data->valid = true;
  156. }
  157. mutex_unlock(&data->update_lock);
  158. return data;
  159. }
  160. /*
  161. * Sysfs stuff
  162. */
  163. static ssize_t temp_max10_show(struct device *dev,
  164. struct device_attribute *dev_attr, char *buf)
  165. {
  166. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  167. struct max6642_data *data = max6642_update_device(dev);
  168. return sprintf(buf, "%d\n",
  169. temp_from_reg10(data->temp_input[attr->index]));
  170. }
  171. static ssize_t temp_max_show(struct device *dev,
  172. struct device_attribute *attr, char *buf)
  173. {
  174. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
  175. struct max6642_data *data = max6642_update_device(dev);
  176. return sprintf(buf, "%d\n", temp_from_reg(data->temp_high[attr2->nr]));
  177. }
  178. static ssize_t temp_max_store(struct device *dev,
  179. struct device_attribute *attr, const char *buf,
  180. size_t count)
  181. {
  182. struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
  183. struct max6642_data *data = dev_get_drvdata(dev);
  184. unsigned long val;
  185. int err;
  186. err = kstrtoul(buf, 10, &val);
  187. if (err < 0)
  188. return err;
  189. mutex_lock(&data->update_lock);
  190. data->temp_high[attr2->nr] = clamp_val(temp_to_reg(val), 0, 255);
  191. i2c_smbus_write_byte_data(data->client, attr2->index,
  192. data->temp_high[attr2->nr]);
  193. mutex_unlock(&data->update_lock);
  194. return count;
  195. }
  196. static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
  197. char *buf)
  198. {
  199. int bitnr = to_sensor_dev_attr(attr)->index;
  200. struct max6642_data *data = max6642_update_device(dev);
  201. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  202. }
  203. static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_max10, 0);
  204. static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_max10, 1);
  205. static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp_max, 0,
  206. MAX6642_REG_W_LOCAL_HIGH);
  207. static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp_max, 1,
  208. MAX6642_REG_W_REMOTE_HIGH);
  209. static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 2);
  210. static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 6);
  211. static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 4);
  212. static struct attribute *max6642_attrs[] = {
  213. &sensor_dev_attr_temp1_input.dev_attr.attr,
  214. &sensor_dev_attr_temp2_input.dev_attr.attr,
  215. &sensor_dev_attr_temp1_max.dev_attr.attr,
  216. &sensor_dev_attr_temp2_max.dev_attr.attr,
  217. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  218. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  219. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  220. NULL
  221. };
  222. ATTRIBUTE_GROUPS(max6642);
  223. static int max6642_probe(struct i2c_client *client)
  224. {
  225. struct device *dev = &client->dev;
  226. struct max6642_data *data;
  227. struct device *hwmon_dev;
  228. data = devm_kzalloc(dev, sizeof(struct max6642_data), GFP_KERNEL);
  229. if (!data)
  230. return -ENOMEM;
  231. data->client = client;
  232. mutex_init(&data->update_lock);
  233. /* Initialize the MAX6642 chip */
  234. max6642_init_client(data, client);
  235. hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
  236. client->name, data,
  237. max6642_groups);
  238. return PTR_ERR_OR_ZERO(hwmon_dev);
  239. }
  240. /*
  241. * Driver data (common to all clients)
  242. */
  243. static const struct i2c_device_id max6642_id[] = {
  244. { "max6642", 0 },
  245. { }
  246. };
  247. MODULE_DEVICE_TABLE(i2c, max6642_id);
  248. static struct i2c_driver max6642_driver = {
  249. .class = I2C_CLASS_HWMON,
  250. .driver = {
  251. .name = "max6642",
  252. },
  253. .probe_new = max6642_probe,
  254. .id_table = max6642_id,
  255. .detect = max6642_detect,
  256. .address_list = normal_i2c,
  257. };
  258. module_i2c_driver(max6642_driver);
  259. MODULE_AUTHOR("Per Dalen <[email protected]>");
  260. MODULE_DESCRIPTION("MAX6642 sensor driver");
  261. MODULE_LICENSE("GPL");