max6697.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * Copyright (c) 2012 Guenter Roeck <linux@roeck-us.net>
  3. *
  4. * based on max1668.c
  5. * Copyright (c) 2011 David George <david.george@ska.ac.za>
  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. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/i2c.h>
  22. #include <linux/hwmon.h>
  23. #include <linux/hwmon-sysfs.h>
  24. #include <linux/err.h>
  25. #include <linux/mutex.h>
  26. #include <linux/of.h>
  27. #include <linux/platform_data/max6697.h>
  28. enum chips { max6581, max6602, max6622, max6636, max6689, max6693, max6694,
  29. max6697, max6698, max6699 };
  30. /* Report local sensor as temp1 */
  31. static const u8 MAX6697_REG_TEMP[] = {
  32. 0x07, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08 };
  33. static const u8 MAX6697_REG_TEMP_EXT[] = {
  34. 0x57, 0x09, 0x52, 0x53, 0x54, 0x55, 0x56, 0 };
  35. static const u8 MAX6697_REG_MAX[] = {
  36. 0x17, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x18 };
  37. static const u8 MAX6697_REG_CRIT[] = {
  38. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 };
  39. /*
  40. * Map device tree / platform data register bit map to chip bit map.
  41. * Applies to alert register and over-temperature register.
  42. */
  43. #define MAX6697_MAP_BITS(reg) ((((reg) & 0x7e) >> 1) | \
  44. (((reg) & 0x01) << 6) | ((reg) & 0x80))
  45. #define MAX6697_REG_STAT(n) (0x44 + (n))
  46. #define MAX6697_REG_CONFIG 0x41
  47. #define MAX6581_CONF_EXTENDED (1 << 1)
  48. #define MAX6693_CONF_BETA (1 << 2)
  49. #define MAX6697_CONF_RESISTANCE (1 << 3)
  50. #define MAX6697_CONF_TIMEOUT (1 << 5)
  51. #define MAX6697_REG_ALERT_MASK 0x42
  52. #define MAX6697_REG_OVERT_MASK 0x43
  53. #define MAX6581_REG_RESISTANCE 0x4a
  54. #define MAX6581_REG_IDEALITY 0x4b
  55. #define MAX6581_REG_IDEALITY_SELECT 0x4c
  56. #define MAX6581_REG_OFFSET 0x4d
  57. #define MAX6581_REG_OFFSET_SELECT 0x4e
  58. #define MAX6697_CONV_TIME 156 /* ms per channel, worst case */
  59. struct max6697_chip_data {
  60. int channels;
  61. u32 have_ext;
  62. u32 have_crit;
  63. u32 have_fault;
  64. u8 valid_conf;
  65. const u8 *alarm_map;
  66. };
  67. struct max6697_data {
  68. struct i2c_client *client;
  69. enum chips type;
  70. const struct max6697_chip_data *chip;
  71. int update_interval; /* in milli-seconds */
  72. int temp_offset; /* in degrees C */
  73. struct mutex update_lock;
  74. unsigned long last_updated; /* In jiffies */
  75. bool valid; /* true if following fields are valid */
  76. /* 1x local and up to 7x remote */
  77. u8 temp[8][4]; /* [nr][0]=temp [1]=ext [2]=max [3]=crit */
  78. #define MAX6697_TEMP_INPUT 0
  79. #define MAX6697_TEMP_EXT 1
  80. #define MAX6697_TEMP_MAX 2
  81. #define MAX6697_TEMP_CRIT 3
  82. u32 alarms;
  83. };
  84. /* Diode fault status bits on MAX6581 are right shifted by one bit */
  85. static const u8 max6581_alarm_map[] = {
  86. 0, 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15,
  87. 16, 17, 18, 19, 20, 21, 22, 23 };
  88. static const struct max6697_chip_data max6697_chip_data[] = {
  89. [max6581] = {
  90. .channels = 8,
  91. .have_crit = 0xff,
  92. .have_ext = 0x7f,
  93. .have_fault = 0xfe,
  94. .valid_conf = MAX6581_CONF_EXTENDED | MAX6697_CONF_TIMEOUT,
  95. .alarm_map = max6581_alarm_map,
  96. },
  97. [max6602] = {
  98. .channels = 5,
  99. .have_crit = 0x12,
  100. .have_ext = 0x02,
  101. .have_fault = 0x1e,
  102. .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
  103. },
  104. [max6622] = {
  105. .channels = 5,
  106. .have_crit = 0x12,
  107. .have_ext = 0x02,
  108. .have_fault = 0x1e,
  109. .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
  110. },
  111. [max6636] = {
  112. .channels = 7,
  113. .have_crit = 0x72,
  114. .have_ext = 0x02,
  115. .have_fault = 0x7e,
  116. .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
  117. },
  118. [max6689] = {
  119. .channels = 7,
  120. .have_crit = 0x72,
  121. .have_ext = 0x02,
  122. .have_fault = 0x7e,
  123. .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
  124. },
  125. [max6693] = {
  126. .channels = 7,
  127. .have_crit = 0x72,
  128. .have_ext = 0x02,
  129. .have_fault = 0x7e,
  130. .valid_conf = MAX6697_CONF_RESISTANCE | MAX6693_CONF_BETA |
  131. MAX6697_CONF_TIMEOUT,
  132. },
  133. [max6694] = {
  134. .channels = 5,
  135. .have_crit = 0x12,
  136. .have_ext = 0x02,
  137. .have_fault = 0x1e,
  138. .valid_conf = MAX6697_CONF_RESISTANCE | MAX6693_CONF_BETA |
  139. MAX6697_CONF_TIMEOUT,
  140. },
  141. [max6697] = {
  142. .channels = 7,
  143. .have_crit = 0x72,
  144. .have_ext = 0x02,
  145. .have_fault = 0x7e,
  146. .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
  147. },
  148. [max6698] = {
  149. .channels = 7,
  150. .have_crit = 0x72,
  151. .have_ext = 0x02,
  152. .have_fault = 0x0e,
  153. .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
  154. },
  155. [max6699] = {
  156. .channels = 5,
  157. .have_crit = 0x12,
  158. .have_ext = 0x02,
  159. .have_fault = 0x1e,
  160. .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
  161. },
  162. };
  163. static struct max6697_data *max6697_update_device(struct device *dev)
  164. {
  165. struct max6697_data *data = dev_get_drvdata(dev);
  166. struct i2c_client *client = data->client;
  167. struct max6697_data *ret = data;
  168. int val;
  169. int i;
  170. u32 alarms;
  171. mutex_lock(&data->update_lock);
  172. if (data->valid &&
  173. !time_after(jiffies, data->last_updated
  174. + msecs_to_jiffies(data->update_interval)))
  175. goto abort;
  176. for (i = 0; i < data->chip->channels; i++) {
  177. if (data->chip->have_ext & (1 << i)) {
  178. val = i2c_smbus_read_byte_data(client,
  179. MAX6697_REG_TEMP_EXT[i]);
  180. if (unlikely(val < 0)) {
  181. ret = ERR_PTR(val);
  182. goto abort;
  183. }
  184. data->temp[i][MAX6697_TEMP_EXT] = val;
  185. }
  186. val = i2c_smbus_read_byte_data(client, MAX6697_REG_TEMP[i]);
  187. if (unlikely(val < 0)) {
  188. ret = ERR_PTR(val);
  189. goto abort;
  190. }
  191. data->temp[i][MAX6697_TEMP_INPUT] = val;
  192. val = i2c_smbus_read_byte_data(client, MAX6697_REG_MAX[i]);
  193. if (unlikely(val < 0)) {
  194. ret = ERR_PTR(val);
  195. goto abort;
  196. }
  197. data->temp[i][MAX6697_TEMP_MAX] = val;
  198. if (data->chip->have_crit & (1 << i)) {
  199. val = i2c_smbus_read_byte_data(client,
  200. MAX6697_REG_CRIT[i]);
  201. if (unlikely(val < 0)) {
  202. ret = ERR_PTR(val);
  203. goto abort;
  204. }
  205. data->temp[i][MAX6697_TEMP_CRIT] = val;
  206. }
  207. }
  208. alarms = 0;
  209. for (i = 0; i < 3; i++) {
  210. val = i2c_smbus_read_byte_data(client, MAX6697_REG_STAT(i));
  211. if (unlikely(val < 0)) {
  212. ret = ERR_PTR(val);
  213. goto abort;
  214. }
  215. alarms = (alarms << 8) | val;
  216. }
  217. data->alarms = alarms;
  218. data->last_updated = jiffies;
  219. data->valid = true;
  220. abort:
  221. mutex_unlock(&data->update_lock);
  222. return ret;
  223. }
  224. static ssize_t show_temp_input(struct device *dev,
  225. struct device_attribute *devattr, char *buf)
  226. {
  227. int index = to_sensor_dev_attr(devattr)->index;
  228. struct max6697_data *data = max6697_update_device(dev);
  229. int temp;
  230. if (IS_ERR(data))
  231. return PTR_ERR(data);
  232. temp = (data->temp[index][MAX6697_TEMP_INPUT] - data->temp_offset) << 3;
  233. temp |= data->temp[index][MAX6697_TEMP_EXT] >> 5;
  234. return sprintf(buf, "%d\n", temp * 125);
  235. }
  236. static ssize_t show_temp(struct device *dev,
  237. struct device_attribute *devattr, char *buf)
  238. {
  239. int nr = to_sensor_dev_attr_2(devattr)->nr;
  240. int index = to_sensor_dev_attr_2(devattr)->index;
  241. struct max6697_data *data = max6697_update_device(dev);
  242. int temp;
  243. if (IS_ERR(data))
  244. return PTR_ERR(data);
  245. temp = data->temp[nr][index];
  246. temp -= data->temp_offset;
  247. return sprintf(buf, "%d\n", temp * 1000);
  248. }
  249. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  250. char *buf)
  251. {
  252. int index = to_sensor_dev_attr(attr)->index;
  253. struct max6697_data *data = max6697_update_device(dev);
  254. if (IS_ERR(data))
  255. return PTR_ERR(data);
  256. if (data->chip->alarm_map)
  257. index = data->chip->alarm_map[index];
  258. return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1);
  259. }
  260. static ssize_t set_temp(struct device *dev,
  261. struct device_attribute *devattr,
  262. const char *buf, size_t count)
  263. {
  264. int nr = to_sensor_dev_attr_2(devattr)->nr;
  265. int index = to_sensor_dev_attr_2(devattr)->index;
  266. struct max6697_data *data = dev_get_drvdata(dev);
  267. long temp;
  268. int ret;
  269. ret = kstrtol(buf, 10, &temp);
  270. if (ret < 0)
  271. return ret;
  272. mutex_lock(&data->update_lock);
  273. temp = DIV_ROUND_CLOSEST(temp, 1000) + data->temp_offset;
  274. temp = clamp_val(temp, 0, data->type == max6581 ? 255 : 127);
  275. data->temp[nr][index] = temp;
  276. ret = i2c_smbus_write_byte_data(data->client,
  277. index == 2 ? MAX6697_REG_MAX[nr]
  278. : MAX6697_REG_CRIT[nr],
  279. temp);
  280. mutex_unlock(&data->update_lock);
  281. return ret < 0 ? ret : count;
  282. }
  283. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0);
  284. static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  285. 0, MAX6697_TEMP_MAX);
  286. static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
  287. 0, MAX6697_TEMP_CRIT);
  288. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1);
  289. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  290. 1, MAX6697_TEMP_MAX);
  291. static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
  292. 1, MAX6697_TEMP_CRIT);
  293. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2);
  294. static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  295. 2, MAX6697_TEMP_MAX);
  296. static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
  297. 2, MAX6697_TEMP_CRIT);
  298. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input, NULL, 3);
  299. static SENSOR_DEVICE_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  300. 3, MAX6697_TEMP_MAX);
  301. static SENSOR_DEVICE_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
  302. 3, MAX6697_TEMP_CRIT);
  303. static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp_input, NULL, 4);
  304. static SENSOR_DEVICE_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  305. 4, MAX6697_TEMP_MAX);
  306. static SENSOR_DEVICE_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
  307. 4, MAX6697_TEMP_CRIT);
  308. static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp_input, NULL, 5);
  309. static SENSOR_DEVICE_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  310. 5, MAX6697_TEMP_MAX);
  311. static SENSOR_DEVICE_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
  312. 5, MAX6697_TEMP_CRIT);
  313. static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp_input, NULL, 6);
  314. static SENSOR_DEVICE_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  315. 6, MAX6697_TEMP_MAX);
  316. static SENSOR_DEVICE_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
  317. 6, MAX6697_TEMP_CRIT);
  318. static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp_input, NULL, 7);
  319. static SENSOR_DEVICE_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  320. 7, MAX6697_TEMP_MAX);
  321. static SENSOR_DEVICE_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
  322. 7, MAX6697_TEMP_CRIT);
  323. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 22);
  324. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 16);
  325. static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 17);
  326. static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 18);
  327. static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 19);
  328. static SENSOR_DEVICE_ATTR(temp6_max_alarm, S_IRUGO, show_alarm, NULL, 20);
  329. static SENSOR_DEVICE_ATTR(temp7_max_alarm, S_IRUGO, show_alarm, NULL, 21);
  330. static SENSOR_DEVICE_ATTR(temp8_max_alarm, S_IRUGO, show_alarm, NULL, 23);
  331. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
  332. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
  333. static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
  334. static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 10);
  335. static SENSOR_DEVICE_ATTR(temp5_crit_alarm, S_IRUGO, show_alarm, NULL, 11);
  336. static SENSOR_DEVICE_ATTR(temp6_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
  337. static SENSOR_DEVICE_ATTR(temp7_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
  338. static SENSOR_DEVICE_ATTR(temp8_crit_alarm, S_IRUGO, show_alarm, NULL, 15);
  339. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 1);
  340. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
  341. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 3);
  342. static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_alarm, NULL, 4);
  343. static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_alarm, NULL, 5);
  344. static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_alarm, NULL, 6);
  345. static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_alarm, NULL, 7);
  346. static DEVICE_ATTR(dummy, 0, NULL, NULL);
  347. static umode_t max6697_is_visible(struct kobject *kobj, struct attribute *attr,
  348. int index)
  349. {
  350. struct device *dev = container_of(kobj, struct device, kobj);
  351. struct max6697_data *data = dev_get_drvdata(dev);
  352. const struct max6697_chip_data *chip = data->chip;
  353. int channel = index / 6; /* channel number */
  354. int nr = index % 6; /* attribute index within channel */
  355. if (channel >= chip->channels)
  356. return 0;
  357. if ((nr == 3 || nr == 4) && !(chip->have_crit & (1 << channel)))
  358. return 0;
  359. if (nr == 5 && !(chip->have_fault & (1 << channel)))
  360. return 0;
  361. return attr->mode;
  362. }
  363. /*
  364. * max6697_is_visible uses the index into the following array to determine
  365. * if attributes should be created or not. Any change in order or content
  366. * must be matched in max6697_is_visible.
  367. */
  368. static struct attribute *max6697_attributes[] = {
  369. &sensor_dev_attr_temp1_input.dev_attr.attr,
  370. &sensor_dev_attr_temp1_max.dev_attr.attr,
  371. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  372. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  373. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  374. &dev_attr_dummy.attr,
  375. &sensor_dev_attr_temp2_input.dev_attr.attr,
  376. &sensor_dev_attr_temp2_max.dev_attr.attr,
  377. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  378. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  379. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  380. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  381. &sensor_dev_attr_temp3_input.dev_attr.attr,
  382. &sensor_dev_attr_temp3_max.dev_attr.attr,
  383. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  384. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  385. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  386. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  387. &sensor_dev_attr_temp4_input.dev_attr.attr,
  388. &sensor_dev_attr_temp4_max.dev_attr.attr,
  389. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  390. &sensor_dev_attr_temp4_crit.dev_attr.attr,
  391. &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
  392. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  393. &sensor_dev_attr_temp5_input.dev_attr.attr,
  394. &sensor_dev_attr_temp5_max.dev_attr.attr,
  395. &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
  396. &sensor_dev_attr_temp5_crit.dev_attr.attr,
  397. &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
  398. &sensor_dev_attr_temp5_fault.dev_attr.attr,
  399. &sensor_dev_attr_temp6_input.dev_attr.attr,
  400. &sensor_dev_attr_temp6_max.dev_attr.attr,
  401. &sensor_dev_attr_temp6_max_alarm.dev_attr.attr,
  402. &sensor_dev_attr_temp6_crit.dev_attr.attr,
  403. &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr,
  404. &sensor_dev_attr_temp6_fault.dev_attr.attr,
  405. &sensor_dev_attr_temp7_input.dev_attr.attr,
  406. &sensor_dev_attr_temp7_max.dev_attr.attr,
  407. &sensor_dev_attr_temp7_max_alarm.dev_attr.attr,
  408. &sensor_dev_attr_temp7_crit.dev_attr.attr,
  409. &sensor_dev_attr_temp7_crit_alarm.dev_attr.attr,
  410. &sensor_dev_attr_temp7_fault.dev_attr.attr,
  411. &sensor_dev_attr_temp8_input.dev_attr.attr,
  412. &sensor_dev_attr_temp8_max.dev_attr.attr,
  413. &sensor_dev_attr_temp8_max_alarm.dev_attr.attr,
  414. &sensor_dev_attr_temp8_crit.dev_attr.attr,
  415. &sensor_dev_attr_temp8_crit_alarm.dev_attr.attr,
  416. &sensor_dev_attr_temp8_fault.dev_attr.attr,
  417. NULL
  418. };
  419. static const struct attribute_group max6697_group = {
  420. .attrs = max6697_attributes, .is_visible = max6697_is_visible,
  421. };
  422. __ATTRIBUTE_GROUPS(max6697);
  423. static void max6697_get_config_of(struct device_node *node,
  424. struct max6697_platform_data *pdata)
  425. {
  426. int len;
  427. const __be32 *prop;
  428. pdata->smbus_timeout_disable =
  429. of_property_read_bool(node, "smbus-timeout-disable");
  430. pdata->extended_range_enable =
  431. of_property_read_bool(node, "extended-range-enable");
  432. pdata->beta_compensation =
  433. of_property_read_bool(node, "beta-compensation-enable");
  434. prop = of_get_property(node, "alert-mask", &len);
  435. if (prop && len == sizeof(u32))
  436. pdata->alert_mask = be32_to_cpu(prop[0]);
  437. prop = of_get_property(node, "over-temperature-mask", &len);
  438. if (prop && len == sizeof(u32))
  439. pdata->over_temperature_mask = be32_to_cpu(prop[0]);
  440. prop = of_get_property(node, "resistance-cancellation", &len);
  441. if (prop) {
  442. if (len == sizeof(u32))
  443. pdata->resistance_cancellation = be32_to_cpu(prop[0]);
  444. else
  445. pdata->resistance_cancellation = 0xfe;
  446. }
  447. prop = of_get_property(node, "transistor-ideality", &len);
  448. if (prop && len == 2 * sizeof(u32)) {
  449. pdata->ideality_mask = be32_to_cpu(prop[0]);
  450. pdata->ideality_value = be32_to_cpu(prop[1]);
  451. }
  452. }
  453. static int max6697_init_chip(struct max6697_data *data,
  454. struct i2c_client *client)
  455. {
  456. struct max6697_platform_data *pdata = dev_get_platdata(&client->dev);
  457. struct max6697_platform_data p;
  458. const struct max6697_chip_data *chip = data->chip;
  459. int factor = chip->channels;
  460. int ret, reg;
  461. /*
  462. * Don't touch configuration if neither platform data nor OF
  463. * configuration was specified. If that is the case, use the
  464. * current chip configuration.
  465. */
  466. if (!pdata && !client->dev.of_node) {
  467. reg = i2c_smbus_read_byte_data(client, MAX6697_REG_CONFIG);
  468. if (reg < 0)
  469. return reg;
  470. if (data->type == max6581) {
  471. if (reg & MAX6581_CONF_EXTENDED)
  472. data->temp_offset = 64;
  473. reg = i2c_smbus_read_byte_data(client,
  474. MAX6581_REG_RESISTANCE);
  475. if (reg < 0)
  476. return reg;
  477. factor += hweight8(reg);
  478. } else {
  479. if (reg & MAX6697_CONF_RESISTANCE)
  480. factor++;
  481. }
  482. goto done;
  483. }
  484. if (client->dev.of_node) {
  485. memset(&p, 0, sizeof(p));
  486. max6697_get_config_of(client->dev.of_node, &p);
  487. pdata = &p;
  488. }
  489. reg = 0;
  490. if (pdata->smbus_timeout_disable &&
  491. (chip->valid_conf & MAX6697_CONF_TIMEOUT)) {
  492. reg |= MAX6697_CONF_TIMEOUT;
  493. }
  494. if (pdata->extended_range_enable &&
  495. (chip->valid_conf & MAX6581_CONF_EXTENDED)) {
  496. reg |= MAX6581_CONF_EXTENDED;
  497. data->temp_offset = 64;
  498. }
  499. if (pdata->resistance_cancellation &&
  500. (chip->valid_conf & MAX6697_CONF_RESISTANCE)) {
  501. reg |= MAX6697_CONF_RESISTANCE;
  502. factor++;
  503. }
  504. if (pdata->beta_compensation &&
  505. (chip->valid_conf & MAX6693_CONF_BETA)) {
  506. reg |= MAX6693_CONF_BETA;
  507. }
  508. ret = i2c_smbus_write_byte_data(client, MAX6697_REG_CONFIG, reg);
  509. if (ret < 0)
  510. return ret;
  511. ret = i2c_smbus_write_byte_data(client, MAX6697_REG_ALERT_MASK,
  512. MAX6697_MAP_BITS(pdata->alert_mask));
  513. if (ret < 0)
  514. return ret;
  515. ret = i2c_smbus_write_byte_data(client, MAX6697_REG_OVERT_MASK,
  516. MAX6697_MAP_BITS(pdata->over_temperature_mask));
  517. if (ret < 0)
  518. return ret;
  519. if (data->type == max6581) {
  520. factor += hweight8(pdata->resistance_cancellation >> 1);
  521. ret = i2c_smbus_write_byte_data(client, MAX6581_REG_RESISTANCE,
  522. pdata->resistance_cancellation >> 1);
  523. if (ret < 0)
  524. return ret;
  525. ret = i2c_smbus_write_byte_data(client, MAX6581_REG_IDEALITY,
  526. pdata->ideality_value);
  527. if (ret < 0)
  528. return ret;
  529. ret = i2c_smbus_write_byte_data(client,
  530. MAX6581_REG_IDEALITY_SELECT,
  531. pdata->ideality_mask >> 1);
  532. if (ret < 0)
  533. return ret;
  534. }
  535. done:
  536. data->update_interval = factor * MAX6697_CONV_TIME;
  537. return 0;
  538. }
  539. static int max6697_probe(struct i2c_client *client,
  540. const struct i2c_device_id *id)
  541. {
  542. struct i2c_adapter *adapter = client->adapter;
  543. struct device *dev = &client->dev;
  544. struct max6697_data *data;
  545. struct device *hwmon_dev;
  546. int err;
  547. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  548. return -ENODEV;
  549. data = devm_kzalloc(dev, sizeof(struct max6697_data), GFP_KERNEL);
  550. if (!data)
  551. return -ENOMEM;
  552. data->type = id->driver_data;
  553. data->chip = &max6697_chip_data[data->type];
  554. data->client = client;
  555. mutex_init(&data->update_lock);
  556. err = max6697_init_chip(data, client);
  557. if (err)
  558. return err;
  559. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  560. data,
  561. max6697_groups);
  562. return PTR_ERR_OR_ZERO(hwmon_dev);
  563. }
  564. static const struct i2c_device_id max6697_id[] = {
  565. { "max6581", max6581 },
  566. { "max6602", max6602 },
  567. { "max6622", max6622 },
  568. { "max6636", max6636 },
  569. { "max6689", max6689 },
  570. { "max6693", max6693 },
  571. { "max6694", max6694 },
  572. { "max6697", max6697 },
  573. { "max6698", max6698 },
  574. { "max6699", max6699 },
  575. { }
  576. };
  577. MODULE_DEVICE_TABLE(i2c, max6697_id);
  578. static struct i2c_driver max6697_driver = {
  579. .class = I2C_CLASS_HWMON,
  580. .driver = {
  581. .name = "max6697",
  582. },
  583. .probe = max6697_probe,
  584. .id_table = max6697_id,
  585. };
  586. module_i2c_driver(max6697_driver);
  587. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  588. MODULE_DESCRIPTION("MAX6697 temperature sensor driver");
  589. MODULE_LICENSE("GPL");