max6639.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * max6639.c - Support for Maxim MAX6639
  3. *
  4. * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller
  5. *
  6. * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
  7. *
  8. * based on the initial MAX6639 support from semptian.net
  9. * by He Changqing <hechangqing@semptian.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/i2c.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/err.h>
  33. #include <linux/mutex.h>
  34. #include <linux/i2c/max6639.h>
  35. /* Addresses to scan */
  36. static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END };
  37. /* The MAX6639 registers, valid channel numbers: 0, 1 */
  38. #define MAX6639_REG_TEMP(ch) (0x00 + (ch))
  39. #define MAX6639_REG_STATUS 0x02
  40. #define MAX6639_REG_OUTPUT_MASK 0x03
  41. #define MAX6639_REG_GCONFIG 0x04
  42. #define MAX6639_REG_TEMP_EXT(ch) (0x05 + (ch))
  43. #define MAX6639_REG_ALERT_LIMIT(ch) (0x08 + (ch))
  44. #define MAX6639_REG_OT_LIMIT(ch) (0x0A + (ch))
  45. #define MAX6639_REG_THERM_LIMIT(ch) (0x0C + (ch))
  46. #define MAX6639_REG_FAN_CONFIG1(ch) (0x10 + (ch) * 4)
  47. #define MAX6639_REG_FAN_CONFIG2a(ch) (0x11 + (ch) * 4)
  48. #define MAX6639_REG_FAN_CONFIG2b(ch) (0x12 + (ch) * 4)
  49. #define MAX6639_REG_FAN_CONFIG3(ch) (0x13 + (ch) * 4)
  50. #define MAX6639_REG_FAN_CNT(ch) (0x20 + (ch))
  51. #define MAX6639_REG_TARGET_CNT(ch) (0x22 + (ch))
  52. #define MAX6639_REG_FAN_PPR(ch) (0x24 + (ch))
  53. #define MAX6639_REG_TARGTDUTY(ch) (0x26 + (ch))
  54. #define MAX6639_REG_FAN_START_TEMP(ch) (0x28 + (ch))
  55. #define MAX6639_REG_DEVID 0x3D
  56. #define MAX6639_REG_MANUID 0x3E
  57. #define MAX6639_REG_DEVREV 0x3F
  58. /* Register bits */
  59. #define MAX6639_GCONFIG_STANDBY 0x80
  60. #define MAX6639_GCONFIG_POR 0x40
  61. #define MAX6639_GCONFIG_DISABLE_TIMEOUT 0x20
  62. #define MAX6639_GCONFIG_CH2_LOCAL 0x10
  63. #define MAX6639_GCONFIG_PWM_FREQ_HI 0x08
  64. #define MAX6639_FAN_CONFIG1_PWM 0x80
  65. #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED 0x40
  66. static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 };
  67. #define FAN_FROM_REG(val, rpm_range) ((val) == 0 || (val) == 255 ? \
  68. 0 : (rpm_ranges[rpm_range] * 30) / (val))
  69. #define TEMP_LIMIT_TO_REG(val) clamp_val((val) / 1000, 0, 255)
  70. /*
  71. * Client data (each client gets its own)
  72. */
  73. struct max6639_data {
  74. struct i2c_client *client;
  75. struct mutex update_lock;
  76. char valid; /* !=0 if following fields are valid */
  77. unsigned long last_updated; /* In jiffies */
  78. /* Register values sampled regularly */
  79. u16 temp[2]; /* Temperature, in 1/8 C, 0..255 C */
  80. bool temp_fault[2]; /* Detected temperature diode failure */
  81. u8 fan[2]; /* Register value: TACH count for fans >=30 */
  82. u8 status; /* Detected channel alarms and fan failures */
  83. /* Register values only written to */
  84. u8 pwm[2]; /* Register value: Duty cycle 0..120 */
  85. u8 temp_therm[2]; /* THERM Temperature, 0..255 C (->_max) */
  86. u8 temp_alert[2]; /* ALERT Temperature, 0..255 C (->_crit) */
  87. u8 temp_ot[2]; /* OT Temperature, 0..255 C (->_emergency) */
  88. /* Register values initialized only once */
  89. u8 ppr; /* Pulses per rotation 0..3 for 1..4 ppr */
  90. u8 rpm_range; /* Index in above rpm_ranges table */
  91. };
  92. static struct max6639_data *max6639_update_device(struct device *dev)
  93. {
  94. struct max6639_data *data = dev_get_drvdata(dev);
  95. struct i2c_client *client = data->client;
  96. struct max6639_data *ret = data;
  97. int i;
  98. int status_reg;
  99. mutex_lock(&data->update_lock);
  100. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  101. int res;
  102. dev_dbg(&client->dev, "Starting max6639 update\n");
  103. status_reg = i2c_smbus_read_byte_data(client,
  104. MAX6639_REG_STATUS);
  105. if (status_reg < 0) {
  106. ret = ERR_PTR(status_reg);
  107. goto abort;
  108. }
  109. data->status = status_reg;
  110. for (i = 0; i < 2; i++) {
  111. res = i2c_smbus_read_byte_data(client,
  112. MAX6639_REG_FAN_CNT(i));
  113. if (res < 0) {
  114. ret = ERR_PTR(res);
  115. goto abort;
  116. }
  117. data->fan[i] = res;
  118. res = i2c_smbus_read_byte_data(client,
  119. MAX6639_REG_TEMP_EXT(i));
  120. if (res < 0) {
  121. ret = ERR_PTR(res);
  122. goto abort;
  123. }
  124. data->temp[i] = res >> 5;
  125. data->temp_fault[i] = res & 0x01;
  126. res = i2c_smbus_read_byte_data(client,
  127. MAX6639_REG_TEMP(i));
  128. if (res < 0) {
  129. ret = ERR_PTR(res);
  130. goto abort;
  131. }
  132. data->temp[i] |= res << 3;
  133. }
  134. data->last_updated = jiffies;
  135. data->valid = 1;
  136. }
  137. abort:
  138. mutex_unlock(&data->update_lock);
  139. return ret;
  140. }
  141. static ssize_t show_temp_input(struct device *dev,
  142. struct device_attribute *dev_attr, char *buf)
  143. {
  144. long temp;
  145. struct max6639_data *data = max6639_update_device(dev);
  146. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  147. if (IS_ERR(data))
  148. return PTR_ERR(data);
  149. temp = data->temp[attr->index] * 125;
  150. return sprintf(buf, "%ld\n", temp);
  151. }
  152. static ssize_t show_temp_fault(struct device *dev,
  153. struct device_attribute *dev_attr, char *buf)
  154. {
  155. struct max6639_data *data = max6639_update_device(dev);
  156. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  157. if (IS_ERR(data))
  158. return PTR_ERR(data);
  159. return sprintf(buf, "%d\n", data->temp_fault[attr->index]);
  160. }
  161. static ssize_t show_temp_max(struct device *dev,
  162. struct device_attribute *dev_attr, char *buf)
  163. {
  164. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  165. struct max6639_data *data = dev_get_drvdata(dev);
  166. return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000));
  167. }
  168. static ssize_t set_temp_max(struct device *dev,
  169. struct device_attribute *dev_attr,
  170. const char *buf, size_t count)
  171. {
  172. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  173. struct max6639_data *data = dev_get_drvdata(dev);
  174. struct i2c_client *client = data->client;
  175. unsigned long val;
  176. int res;
  177. res = kstrtoul(buf, 10, &val);
  178. if (res)
  179. return res;
  180. mutex_lock(&data->update_lock);
  181. data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val);
  182. i2c_smbus_write_byte_data(client,
  183. MAX6639_REG_THERM_LIMIT(attr->index),
  184. data->temp_therm[attr->index]);
  185. mutex_unlock(&data->update_lock);
  186. return count;
  187. }
  188. static ssize_t show_temp_crit(struct device *dev,
  189. struct device_attribute *dev_attr, char *buf)
  190. {
  191. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  192. struct max6639_data *data = dev_get_drvdata(dev);
  193. return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000));
  194. }
  195. static ssize_t set_temp_crit(struct device *dev,
  196. struct device_attribute *dev_attr,
  197. const char *buf, size_t count)
  198. {
  199. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  200. struct max6639_data *data = dev_get_drvdata(dev);
  201. struct i2c_client *client = data->client;
  202. unsigned long val;
  203. int res;
  204. res = kstrtoul(buf, 10, &val);
  205. if (res)
  206. return res;
  207. mutex_lock(&data->update_lock);
  208. data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val);
  209. i2c_smbus_write_byte_data(client,
  210. MAX6639_REG_ALERT_LIMIT(attr->index),
  211. data->temp_alert[attr->index]);
  212. mutex_unlock(&data->update_lock);
  213. return count;
  214. }
  215. static ssize_t show_temp_emergency(struct device *dev,
  216. struct device_attribute *dev_attr,
  217. char *buf)
  218. {
  219. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  220. struct max6639_data *data = dev_get_drvdata(dev);
  221. return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000));
  222. }
  223. static ssize_t set_temp_emergency(struct device *dev,
  224. struct device_attribute *dev_attr,
  225. const char *buf, size_t count)
  226. {
  227. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  228. struct max6639_data *data = dev_get_drvdata(dev);
  229. struct i2c_client *client = data->client;
  230. unsigned long val;
  231. int res;
  232. res = kstrtoul(buf, 10, &val);
  233. if (res)
  234. return res;
  235. mutex_lock(&data->update_lock);
  236. data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val);
  237. i2c_smbus_write_byte_data(client,
  238. MAX6639_REG_OT_LIMIT(attr->index),
  239. data->temp_ot[attr->index]);
  240. mutex_unlock(&data->update_lock);
  241. return count;
  242. }
  243. static ssize_t show_pwm(struct device *dev,
  244. struct device_attribute *dev_attr, char *buf)
  245. {
  246. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  247. struct max6639_data *data = dev_get_drvdata(dev);
  248. return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120);
  249. }
  250. static ssize_t set_pwm(struct device *dev,
  251. struct device_attribute *dev_attr,
  252. const char *buf, size_t count)
  253. {
  254. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  255. struct max6639_data *data = dev_get_drvdata(dev);
  256. struct i2c_client *client = data->client;
  257. unsigned long val;
  258. int res;
  259. res = kstrtoul(buf, 10, &val);
  260. if (res)
  261. return res;
  262. val = clamp_val(val, 0, 255);
  263. mutex_lock(&data->update_lock);
  264. data->pwm[attr->index] = (u8)(val * 120 / 255);
  265. i2c_smbus_write_byte_data(client,
  266. MAX6639_REG_TARGTDUTY(attr->index),
  267. data->pwm[attr->index]);
  268. mutex_unlock(&data->update_lock);
  269. return count;
  270. }
  271. static ssize_t show_fan_input(struct device *dev,
  272. struct device_attribute *dev_attr, char *buf)
  273. {
  274. struct max6639_data *data = max6639_update_device(dev);
  275. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  276. if (IS_ERR(data))
  277. return PTR_ERR(data);
  278. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
  279. data->rpm_range));
  280. }
  281. static ssize_t show_alarm(struct device *dev,
  282. struct device_attribute *dev_attr, char *buf)
  283. {
  284. struct max6639_data *data = max6639_update_device(dev);
  285. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  286. if (IS_ERR(data))
  287. return PTR_ERR(data);
  288. return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index)));
  289. }
  290. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0);
  291. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1);
  292. static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
  293. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
  294. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
  295. set_temp_max, 0);
  296. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
  297. set_temp_max, 1);
  298. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit,
  299. set_temp_crit, 0);
  300. static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit,
  301. set_temp_crit, 1);
  302. static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO,
  303. show_temp_emergency, set_temp_emergency, 0);
  304. static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO,
  305. show_temp_emergency, set_temp_emergency, 1);
  306. static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
  307. static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
  308. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
  309. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
  310. static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
  311. static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 0);
  312. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 3);
  313. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
  314. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 7);
  315. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 6);
  316. static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 5);
  317. static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 4);
  318. static struct attribute *max6639_attrs[] = {
  319. &sensor_dev_attr_temp1_input.dev_attr.attr,
  320. &sensor_dev_attr_temp2_input.dev_attr.attr,
  321. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  322. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  323. &sensor_dev_attr_temp1_max.dev_attr.attr,
  324. &sensor_dev_attr_temp2_max.dev_attr.attr,
  325. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  326. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  327. &sensor_dev_attr_temp1_emergency.dev_attr.attr,
  328. &sensor_dev_attr_temp2_emergency.dev_attr.attr,
  329. &sensor_dev_attr_pwm1.dev_attr.attr,
  330. &sensor_dev_attr_pwm2.dev_attr.attr,
  331. &sensor_dev_attr_fan1_input.dev_attr.attr,
  332. &sensor_dev_attr_fan2_input.dev_attr.attr,
  333. &sensor_dev_attr_fan1_fault.dev_attr.attr,
  334. &sensor_dev_attr_fan2_fault.dev_attr.attr,
  335. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  336. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  337. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  338. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  339. &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
  340. &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
  341. NULL
  342. };
  343. ATTRIBUTE_GROUPS(max6639);
  344. /*
  345. * returns respective index in rpm_ranges table
  346. * 1 by default on invalid range
  347. */
  348. static int rpm_range_to_reg(int range)
  349. {
  350. int i;
  351. for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) {
  352. if (rpm_ranges[i] == range)
  353. return i;
  354. }
  355. return 1; /* default: 4000 RPM */
  356. }
  357. static int max6639_init_client(struct i2c_client *client,
  358. struct max6639_data *data)
  359. {
  360. struct max6639_platform_data *max6639_info =
  361. dev_get_platdata(&client->dev);
  362. int i;
  363. int rpm_range = 1; /* default: 4000 RPM */
  364. int err;
  365. /* Reset chip to default values, see below for GCONFIG setup */
  366. err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
  367. MAX6639_GCONFIG_POR);
  368. if (err)
  369. goto exit;
  370. /* Fans pulse per revolution is 2 by default */
  371. if (max6639_info && max6639_info->ppr > 0 &&
  372. max6639_info->ppr < 5)
  373. data->ppr = max6639_info->ppr;
  374. else
  375. data->ppr = 2;
  376. data->ppr -= 1;
  377. if (max6639_info)
  378. rpm_range = rpm_range_to_reg(max6639_info->rpm_range);
  379. data->rpm_range = rpm_range;
  380. for (i = 0; i < 2; i++) {
  381. /* Set Fan pulse per revolution */
  382. err = i2c_smbus_write_byte_data(client,
  383. MAX6639_REG_FAN_PPR(i),
  384. data->ppr << 6);
  385. if (err)
  386. goto exit;
  387. /* Fans config PWM, RPM */
  388. err = i2c_smbus_write_byte_data(client,
  389. MAX6639_REG_FAN_CONFIG1(i),
  390. MAX6639_FAN_CONFIG1_PWM | rpm_range);
  391. if (err)
  392. goto exit;
  393. /* Fans PWM polarity high by default */
  394. if (max6639_info && max6639_info->pwm_polarity == 0)
  395. err = i2c_smbus_write_byte_data(client,
  396. MAX6639_REG_FAN_CONFIG2a(i), 0x00);
  397. else
  398. err = i2c_smbus_write_byte_data(client,
  399. MAX6639_REG_FAN_CONFIG2a(i), 0x02);
  400. if (err)
  401. goto exit;
  402. /*
  403. * /THERM full speed enable,
  404. * PWM frequency 25kHz, see also GCONFIG below
  405. */
  406. err = i2c_smbus_write_byte_data(client,
  407. MAX6639_REG_FAN_CONFIG3(i),
  408. MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03);
  409. if (err)
  410. goto exit;
  411. /* Max. temp. 80C/90C/100C */
  412. data->temp_therm[i] = 80;
  413. data->temp_alert[i] = 90;
  414. data->temp_ot[i] = 100;
  415. err = i2c_smbus_write_byte_data(client,
  416. MAX6639_REG_THERM_LIMIT(i),
  417. data->temp_therm[i]);
  418. if (err)
  419. goto exit;
  420. err = i2c_smbus_write_byte_data(client,
  421. MAX6639_REG_ALERT_LIMIT(i),
  422. data->temp_alert[i]);
  423. if (err)
  424. goto exit;
  425. err = i2c_smbus_write_byte_data(client,
  426. MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]);
  427. if (err)
  428. goto exit;
  429. /* PWM 120/120 (i.e. 100%) */
  430. data->pwm[i] = 120;
  431. err = i2c_smbus_write_byte_data(client,
  432. MAX6639_REG_TARGTDUTY(i), data->pwm[i]);
  433. if (err)
  434. goto exit;
  435. }
  436. /* Start monitoring */
  437. err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
  438. MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL |
  439. MAX6639_GCONFIG_PWM_FREQ_HI);
  440. exit:
  441. return err;
  442. }
  443. /* Return 0 if detection is successful, -ENODEV otherwise */
  444. static int max6639_detect(struct i2c_client *client,
  445. struct i2c_board_info *info)
  446. {
  447. struct i2c_adapter *adapter = client->adapter;
  448. int dev_id, manu_id;
  449. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  450. return -ENODEV;
  451. /* Actual detection via device and manufacturer ID */
  452. dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID);
  453. manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID);
  454. if (dev_id != 0x58 || manu_id != 0x4D)
  455. return -ENODEV;
  456. strlcpy(info->type, "max6639", I2C_NAME_SIZE);
  457. return 0;
  458. }
  459. static int max6639_probe(struct i2c_client *client,
  460. const struct i2c_device_id *id)
  461. {
  462. struct device *dev = &client->dev;
  463. struct max6639_data *data;
  464. struct device *hwmon_dev;
  465. int err;
  466. data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL);
  467. if (!data)
  468. return -ENOMEM;
  469. data->client = client;
  470. mutex_init(&data->update_lock);
  471. /* Initialize the max6639 chip */
  472. err = max6639_init_client(client, data);
  473. if (err < 0)
  474. return err;
  475. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  476. data,
  477. max6639_groups);
  478. return PTR_ERR_OR_ZERO(hwmon_dev);
  479. }
  480. #ifdef CONFIG_PM_SLEEP
  481. static int max6639_suspend(struct device *dev)
  482. {
  483. struct i2c_client *client = to_i2c_client(dev);
  484. int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
  485. if (data < 0)
  486. return data;
  487. return i2c_smbus_write_byte_data(client,
  488. MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY);
  489. }
  490. static int max6639_resume(struct device *dev)
  491. {
  492. struct i2c_client *client = to_i2c_client(dev);
  493. int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
  494. if (data < 0)
  495. return data;
  496. return i2c_smbus_write_byte_data(client,
  497. MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY);
  498. }
  499. #endif /* CONFIG_PM_SLEEP */
  500. static const struct i2c_device_id max6639_id[] = {
  501. {"max6639", 0},
  502. { }
  503. };
  504. MODULE_DEVICE_TABLE(i2c, max6639_id);
  505. static SIMPLE_DEV_PM_OPS(max6639_pm_ops, max6639_suspend, max6639_resume);
  506. static struct i2c_driver max6639_driver = {
  507. .class = I2C_CLASS_HWMON,
  508. .driver = {
  509. .name = "max6639",
  510. .pm = &max6639_pm_ops,
  511. },
  512. .probe = max6639_probe,
  513. .id_table = max6639_id,
  514. .detect = max6639_detect,
  515. .address_list = normal_i2c,
  516. };
  517. module_i2c_driver(max6639_driver);
  518. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  519. MODULE_DESCRIPTION("max6639 driver");
  520. MODULE_LICENSE("GPL");