max6650.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * max6650.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring.
  4. *
  5. * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de>
  6. *
  7. * based on code written by John Morris <john.morris@spirentcom.com>
  8. * Copyright (c) 2003 Spirent Communications
  9. * and Claus Gindhart <claus.gindhart@kontron.com>
  10. *
  11. * This module has only been tested with the MAX6650 chip. It should
  12. * also work with the MAX6651. It does not distinguish max6650 and max6651
  13. * chips.
  14. *
  15. * The datasheet was last seen at:
  16. *
  17. * http://pdfserv.maxim-ic.com/en/ds/MAX6650-MAX6651.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. /*
  42. * Insmod parameters
  43. */
  44. /* fan_voltage: 5=5V fan, 12=12V fan, 0=don't change */
  45. static int fan_voltage;
  46. /* prescaler: Possible values are 1, 2, 4, 8, 16 or 0 for don't change */
  47. static int prescaler;
  48. /* clock: The clock frequency of the chip the driver should assume */
  49. static int clock = 254000;
  50. module_param(fan_voltage, int, S_IRUGO);
  51. module_param(prescaler, int, S_IRUGO);
  52. module_param(clock, int, S_IRUGO);
  53. /*
  54. * MAX 6650/6651 registers
  55. */
  56. #define MAX6650_REG_SPEED 0x00
  57. #define MAX6650_REG_CONFIG 0x02
  58. #define MAX6650_REG_GPIO_DEF 0x04
  59. #define MAX6650_REG_DAC 0x06
  60. #define MAX6650_REG_ALARM_EN 0x08
  61. #define MAX6650_REG_ALARM 0x0A
  62. #define MAX6650_REG_TACH0 0x0C
  63. #define MAX6650_REG_TACH1 0x0E
  64. #define MAX6650_REG_TACH2 0x10
  65. #define MAX6650_REG_TACH3 0x12
  66. #define MAX6650_REG_GPIO_STAT 0x14
  67. #define MAX6650_REG_COUNT 0x16
  68. /*
  69. * Config register bits
  70. */
  71. #define MAX6650_CFG_V12 0x08
  72. #define MAX6650_CFG_PRESCALER_MASK 0x07
  73. #define MAX6650_CFG_PRESCALER_2 0x01
  74. #define MAX6650_CFG_PRESCALER_4 0x02
  75. #define MAX6650_CFG_PRESCALER_8 0x03
  76. #define MAX6650_CFG_PRESCALER_16 0x04
  77. #define MAX6650_CFG_MODE_MASK 0x30
  78. #define MAX6650_CFG_MODE_ON 0x00
  79. #define MAX6650_CFG_MODE_OFF 0x10
  80. #define MAX6650_CFG_MODE_CLOSED_LOOP 0x20
  81. #define MAX6650_CFG_MODE_OPEN_LOOP 0x30
  82. #define MAX6650_COUNT_MASK 0x03
  83. /*
  84. * Alarm status register bits
  85. */
  86. #define MAX6650_ALRM_MAX 0x01
  87. #define MAX6650_ALRM_MIN 0x02
  88. #define MAX6650_ALRM_TACH 0x04
  89. #define MAX6650_ALRM_GPIO1 0x08
  90. #define MAX6650_ALRM_GPIO2 0x10
  91. /* Minimum and maximum values of the FAN-RPM */
  92. #define FAN_RPM_MIN 240
  93. #define FAN_RPM_MAX 30000
  94. #define DIV_FROM_REG(reg) (1 << (reg & 7))
  95. static int max6650_probe(struct i2c_client *client,
  96. const struct i2c_device_id *id);
  97. static int max6650_init_client(struct i2c_client *client);
  98. static int max6650_remove(struct i2c_client *client);
  99. static struct max6650_data *max6650_update_device(struct device *dev);
  100. /*
  101. * Driver data (common to all clients)
  102. */
  103. static const struct i2c_device_id max6650_id[] = {
  104. { "max6650", 1 },
  105. { "max6651", 4 },
  106. { }
  107. };
  108. MODULE_DEVICE_TABLE(i2c, max6650_id);
  109. static struct i2c_driver max6650_driver = {
  110. .driver = {
  111. .name = "max6650",
  112. },
  113. .probe = max6650_probe,
  114. .remove = max6650_remove,
  115. .id_table = max6650_id,
  116. };
  117. /*
  118. * Client data (each client gets its own)
  119. */
  120. struct max6650_data {
  121. struct device *hwmon_dev;
  122. struct mutex update_lock;
  123. int nr_fans;
  124. char valid; /* zero until following fields are valid */
  125. unsigned long last_updated; /* in jiffies */
  126. /* register values */
  127. u8 speed;
  128. u8 config;
  129. u8 tach[4];
  130. u8 count;
  131. u8 dac;
  132. u8 alarm;
  133. };
  134. static ssize_t get_fan(struct device *dev, struct device_attribute *devattr,
  135. char *buf)
  136. {
  137. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  138. struct max6650_data *data = max6650_update_device(dev);
  139. int rpm;
  140. /*
  141. * Calculation details:
  142. *
  143. * Each tachometer counts over an interval given by the "count"
  144. * register (0.25, 0.5, 1 or 2 seconds). This module assumes
  145. * that the fans produce two pulses per revolution (this seems
  146. * to be the most common).
  147. */
  148. rpm = ((data->tach[attr->index] * 120) / DIV_FROM_REG(data->count));
  149. return sprintf(buf, "%d\n", rpm);
  150. }
  151. /*
  152. * Set the fan speed to the specified RPM (or read back the RPM setting).
  153. * This works in closed loop mode only. Use pwm1 for open loop speed setting.
  154. *
  155. * The MAX6650/1 will automatically control fan speed when in closed loop
  156. * mode.
  157. *
  158. * Assumptions:
  159. *
  160. * 1) The MAX6650/1 internal 254kHz clock frequency is set correctly. Use
  161. * the clock module parameter if you need to fine tune this.
  162. *
  163. * 2) The prescaler (low three bits of the config register) has already
  164. * been set to an appropriate value. Use the prescaler module parameter
  165. * if your BIOS doesn't initialize the chip properly.
  166. *
  167. * The relevant equations are given on pages 21 and 22 of the datasheet.
  168. *
  169. * From the datasheet, the relevant equation when in regulation is:
  170. *
  171. * [fCLK / (128 x (KTACH + 1))] = 2 x FanSpeed / KSCALE
  172. *
  173. * where:
  174. *
  175. * fCLK is the oscillator frequency (either the 254kHz internal
  176. * oscillator or the externally applied clock)
  177. *
  178. * KTACH is the value in the speed register
  179. *
  180. * FanSpeed is the speed of the fan in rps
  181. *
  182. * KSCALE is the prescaler value (1, 2, 4, 8, or 16)
  183. *
  184. * When reading, we need to solve for FanSpeed. When writing, we need to
  185. * solve for KTACH.
  186. *
  187. * Note: this tachometer is completely separate from the tachometers
  188. * used to measure the fan speeds. Only one fan's speed (fan1) is
  189. * controlled.
  190. */
  191. static ssize_t get_target(struct device *dev, struct device_attribute *devattr,
  192. char *buf)
  193. {
  194. struct max6650_data *data = max6650_update_device(dev);
  195. int kscale, ktach, rpm;
  196. /*
  197. * Use the datasheet equation:
  198. *
  199. * FanSpeed = KSCALE x fCLK / [256 x (KTACH + 1)]
  200. *
  201. * then multiply by 60 to give rpm.
  202. */
  203. kscale = DIV_FROM_REG(data->config);
  204. ktach = data->speed;
  205. rpm = 60 * kscale * clock / (256 * (ktach + 1));
  206. return sprintf(buf, "%d\n", rpm);
  207. }
  208. static ssize_t set_target(struct device *dev, struct device_attribute *devattr,
  209. const char *buf, size_t count)
  210. {
  211. struct i2c_client *client = to_i2c_client(dev);
  212. struct max6650_data *data = i2c_get_clientdata(client);
  213. int kscale, ktach;
  214. unsigned long rpm;
  215. int err;
  216. err = kstrtoul(buf, 10, &rpm);
  217. if (err)
  218. return err;
  219. rpm = SENSORS_LIMIT(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
  220. /*
  221. * Divide the required speed by 60 to get from rpm to rps, then
  222. * use the datasheet equation:
  223. *
  224. * KTACH = [(fCLK x KSCALE) / (256 x FanSpeed)] - 1
  225. */
  226. mutex_lock(&data->update_lock);
  227. kscale = DIV_FROM_REG(data->config);
  228. ktach = ((clock * kscale) / (256 * rpm / 60)) - 1;
  229. if (ktach < 0)
  230. ktach = 0;
  231. if (ktach > 255)
  232. ktach = 255;
  233. data->speed = ktach;
  234. i2c_smbus_write_byte_data(client, MAX6650_REG_SPEED, data->speed);
  235. mutex_unlock(&data->update_lock);
  236. return count;
  237. }
  238. /*
  239. * Get/set the fan speed in open loop mode using pwm1 sysfs file.
  240. * Speed is given as a relative value from 0 to 255, where 255 is maximum
  241. * speed. Note that this is done by writing directly to the chip's DAC,
  242. * it won't change the closed loop speed set by fan1_target.
  243. * Also note that due to rounding errors it is possible that you don't read
  244. * back exactly the value you have set.
  245. */
  246. static ssize_t get_pwm(struct device *dev, struct device_attribute *devattr,
  247. char *buf)
  248. {
  249. int pwm;
  250. struct max6650_data *data = max6650_update_device(dev);
  251. /*
  252. * Useful range for dac is 0-180 for 12V fans and 0-76 for 5V fans.
  253. * Lower DAC values mean higher speeds.
  254. */
  255. if (data->config & MAX6650_CFG_V12)
  256. pwm = 255 - (255 * (int)data->dac)/180;
  257. else
  258. pwm = 255 - (255 * (int)data->dac)/76;
  259. if (pwm < 0)
  260. pwm = 0;
  261. return sprintf(buf, "%d\n", pwm);
  262. }
  263. static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
  264. const char *buf, size_t count)
  265. {
  266. struct i2c_client *client = to_i2c_client(dev);
  267. struct max6650_data *data = i2c_get_clientdata(client);
  268. unsigned long pwm;
  269. int err;
  270. err = kstrtoul(buf, 10, &pwm);
  271. if (err)
  272. return err;
  273. pwm = SENSORS_LIMIT(pwm, 0, 255);
  274. mutex_lock(&data->update_lock);
  275. if (data->config & MAX6650_CFG_V12)
  276. data->dac = 180 - (180 * pwm)/255;
  277. else
  278. data->dac = 76 - (76 * pwm)/255;
  279. i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, data->dac);
  280. mutex_unlock(&data->update_lock);
  281. return count;
  282. }
  283. /*
  284. * Get/Set controller mode:
  285. * Possible values:
  286. * 0 = Fan always on
  287. * 1 = Open loop, Voltage is set according to speed, not regulated.
  288. * 2 = Closed loop, RPM for all fans regulated by fan1 tachometer
  289. */
  290. static ssize_t get_enable(struct device *dev, struct device_attribute *devattr,
  291. char *buf)
  292. {
  293. struct max6650_data *data = max6650_update_device(dev);
  294. int mode = (data->config & MAX6650_CFG_MODE_MASK) >> 4;
  295. int sysfs_modes[4] = {0, 1, 2, 1};
  296. return sprintf(buf, "%d\n", sysfs_modes[mode]);
  297. }
  298. static ssize_t set_enable(struct device *dev, struct device_attribute *devattr,
  299. const char *buf, size_t count)
  300. {
  301. struct i2c_client *client = to_i2c_client(dev);
  302. struct max6650_data *data = i2c_get_clientdata(client);
  303. int max6650_modes[3] = {0, 3, 2};
  304. unsigned long mode;
  305. int err;
  306. err = kstrtoul(buf, 10, &mode);
  307. if (err)
  308. return err;
  309. if (mode > 2)
  310. return -EINVAL;
  311. mutex_lock(&data->update_lock);
  312. data->config = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG);
  313. data->config = (data->config & ~MAX6650_CFG_MODE_MASK)
  314. | (max6650_modes[mode] << 4);
  315. i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, data->config);
  316. mutex_unlock(&data->update_lock);
  317. return count;
  318. }
  319. /*
  320. * Read/write functions for fan1_div sysfs file. The MAX6650 has no such
  321. * divider. We handle this by converting between divider and counttime:
  322. *
  323. * (counttime == k) <==> (divider == 2^k), k = 0, 1, 2, or 3
  324. *
  325. * Lower values of k allow to connect a faster fan without the risk of
  326. * counter overflow. The price is lower resolution. You can also set counttime
  327. * using the module parameter. Note that the module parameter "prescaler" also
  328. * influences the behaviour. Unfortunately, there's no sysfs attribute
  329. * defined for that. See the data sheet for details.
  330. */
  331. static ssize_t get_div(struct device *dev, struct device_attribute *devattr,
  332. char *buf)
  333. {
  334. struct max6650_data *data = max6650_update_device(dev);
  335. return sprintf(buf, "%d\n", DIV_FROM_REG(data->count));
  336. }
  337. static ssize_t set_div(struct device *dev, struct device_attribute *devattr,
  338. const char *buf, size_t count)
  339. {
  340. struct i2c_client *client = to_i2c_client(dev);
  341. struct max6650_data *data = i2c_get_clientdata(client);
  342. unsigned long div;
  343. int err;
  344. err = kstrtoul(buf, 10, &div);
  345. if (err)
  346. return err;
  347. mutex_lock(&data->update_lock);
  348. switch (div) {
  349. case 1:
  350. data->count = 0;
  351. break;
  352. case 2:
  353. data->count = 1;
  354. break;
  355. case 4:
  356. data->count = 2;
  357. break;
  358. case 8:
  359. data->count = 3;
  360. break;
  361. default:
  362. mutex_unlock(&data->update_lock);
  363. return -EINVAL;
  364. }
  365. i2c_smbus_write_byte_data(client, MAX6650_REG_COUNT, data->count);
  366. mutex_unlock(&data->update_lock);
  367. return count;
  368. }
  369. /*
  370. * Get alarm stati:
  371. * Possible values:
  372. * 0 = no alarm
  373. * 1 = alarm
  374. */
  375. static ssize_t get_alarm(struct device *dev, struct device_attribute *devattr,
  376. char *buf)
  377. {
  378. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  379. struct max6650_data *data = max6650_update_device(dev);
  380. struct i2c_client *client = to_i2c_client(dev);
  381. int alarm = 0;
  382. if (data->alarm & attr->index) {
  383. mutex_lock(&data->update_lock);
  384. alarm = 1;
  385. data->alarm &= ~attr->index;
  386. data->alarm |= i2c_smbus_read_byte_data(client,
  387. MAX6650_REG_ALARM);
  388. mutex_unlock(&data->update_lock);
  389. }
  390. return sprintf(buf, "%d\n", alarm);
  391. }
  392. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, 0);
  393. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan, NULL, 1);
  394. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, get_fan, NULL, 2);
  395. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, get_fan, NULL, 3);
  396. static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO, get_target, set_target);
  397. static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_div, set_div);
  398. static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, get_enable, set_enable);
  399. static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
  400. static SENSOR_DEVICE_ATTR(fan1_max_alarm, S_IRUGO, get_alarm, NULL,
  401. MAX6650_ALRM_MAX);
  402. static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, get_alarm, NULL,
  403. MAX6650_ALRM_MIN);
  404. static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_alarm, NULL,
  405. MAX6650_ALRM_TACH);
  406. static SENSOR_DEVICE_ATTR(gpio1_alarm, S_IRUGO, get_alarm, NULL,
  407. MAX6650_ALRM_GPIO1);
  408. static SENSOR_DEVICE_ATTR(gpio2_alarm, S_IRUGO, get_alarm, NULL,
  409. MAX6650_ALRM_GPIO2);
  410. static umode_t max6650_attrs_visible(struct kobject *kobj, struct attribute *a,
  411. int n)
  412. {
  413. struct device *dev = container_of(kobj, struct device, kobj);
  414. struct i2c_client *client = to_i2c_client(dev);
  415. u8 alarm_en = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM_EN);
  416. struct device_attribute *devattr;
  417. /*
  418. * Hide the alarms that have not been enabled by the firmware
  419. */
  420. devattr = container_of(a, struct device_attribute, attr);
  421. if (devattr == &sensor_dev_attr_fan1_max_alarm.dev_attr
  422. || devattr == &sensor_dev_attr_fan1_min_alarm.dev_attr
  423. || devattr == &sensor_dev_attr_fan1_fault.dev_attr
  424. || devattr == &sensor_dev_attr_gpio1_alarm.dev_attr
  425. || devattr == &sensor_dev_attr_gpio2_alarm.dev_attr) {
  426. if (!(alarm_en & to_sensor_dev_attr(devattr)->index))
  427. return 0;
  428. }
  429. return a->mode;
  430. }
  431. static struct attribute *max6650_attrs[] = {
  432. &sensor_dev_attr_fan1_input.dev_attr.attr,
  433. &dev_attr_fan1_target.attr,
  434. &dev_attr_fan1_div.attr,
  435. &dev_attr_pwm1_enable.attr,
  436. &dev_attr_pwm1.attr,
  437. &sensor_dev_attr_fan1_max_alarm.dev_attr.attr,
  438. &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
  439. &sensor_dev_attr_fan1_fault.dev_attr.attr,
  440. &sensor_dev_attr_gpio1_alarm.dev_attr.attr,
  441. &sensor_dev_attr_gpio2_alarm.dev_attr.attr,
  442. NULL
  443. };
  444. static struct attribute_group max6650_attr_grp = {
  445. .attrs = max6650_attrs,
  446. .is_visible = max6650_attrs_visible,
  447. };
  448. static struct attribute *max6651_attrs[] = {
  449. &sensor_dev_attr_fan2_input.dev_attr.attr,
  450. &sensor_dev_attr_fan3_input.dev_attr.attr,
  451. &sensor_dev_attr_fan4_input.dev_attr.attr,
  452. NULL
  453. };
  454. static const struct attribute_group max6651_attr_grp = {
  455. .attrs = max6651_attrs,
  456. };
  457. /*
  458. * Real code
  459. */
  460. static int max6650_probe(struct i2c_client *client,
  461. const struct i2c_device_id *id)
  462. {
  463. struct max6650_data *data;
  464. int err;
  465. data = kzalloc(sizeof(struct max6650_data), GFP_KERNEL);
  466. if (!data) {
  467. dev_err(&client->dev, "out of memory.\n");
  468. return -ENOMEM;
  469. }
  470. i2c_set_clientdata(client, data);
  471. mutex_init(&data->update_lock);
  472. data->nr_fans = id->driver_data;
  473. /*
  474. * Initialize the max6650 chip
  475. */
  476. err = max6650_init_client(client);
  477. if (err)
  478. goto err_free;
  479. err = sysfs_create_group(&client->dev.kobj, &max6650_attr_grp);
  480. if (err)
  481. goto err_free;
  482. /* 3 additional fan inputs for the MAX6651 */
  483. if (data->nr_fans == 4) {
  484. err = sysfs_create_group(&client->dev.kobj, &max6651_attr_grp);
  485. if (err)
  486. goto err_remove;
  487. }
  488. data->hwmon_dev = hwmon_device_register(&client->dev);
  489. if (!IS_ERR(data->hwmon_dev))
  490. return 0;
  491. err = PTR_ERR(data->hwmon_dev);
  492. dev_err(&client->dev, "error registering hwmon device.\n");
  493. if (data->nr_fans == 4)
  494. sysfs_remove_group(&client->dev.kobj, &max6651_attr_grp);
  495. err_remove:
  496. sysfs_remove_group(&client->dev.kobj, &max6650_attr_grp);
  497. err_free:
  498. kfree(data);
  499. return err;
  500. }
  501. static int max6650_remove(struct i2c_client *client)
  502. {
  503. struct max6650_data *data = i2c_get_clientdata(client);
  504. hwmon_device_unregister(data->hwmon_dev);
  505. if (data->nr_fans == 4)
  506. sysfs_remove_group(&client->dev.kobj, &max6651_attr_grp);
  507. sysfs_remove_group(&client->dev.kobj, &max6650_attr_grp);
  508. kfree(data);
  509. return 0;
  510. }
  511. static int max6650_init_client(struct i2c_client *client)
  512. {
  513. struct max6650_data *data = i2c_get_clientdata(client);
  514. int config;
  515. int err = -EIO;
  516. config = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG);
  517. if (config < 0) {
  518. dev_err(&client->dev, "Error reading config, aborting.\n");
  519. return err;
  520. }
  521. switch (fan_voltage) {
  522. case 0:
  523. break;
  524. case 5:
  525. config &= ~MAX6650_CFG_V12;
  526. break;
  527. case 12:
  528. config |= MAX6650_CFG_V12;
  529. break;
  530. default:
  531. dev_err(&client->dev, "illegal value for fan_voltage (%d)\n",
  532. fan_voltage);
  533. }
  534. dev_info(&client->dev, "Fan voltage is set to %dV.\n",
  535. (config & MAX6650_CFG_V12) ? 12 : 5);
  536. switch (prescaler) {
  537. case 0:
  538. break;
  539. case 1:
  540. config &= ~MAX6650_CFG_PRESCALER_MASK;
  541. break;
  542. case 2:
  543. config = (config & ~MAX6650_CFG_PRESCALER_MASK)
  544. | MAX6650_CFG_PRESCALER_2;
  545. break;
  546. case 4:
  547. config = (config & ~MAX6650_CFG_PRESCALER_MASK)
  548. | MAX6650_CFG_PRESCALER_4;
  549. break;
  550. case 8:
  551. config = (config & ~MAX6650_CFG_PRESCALER_MASK)
  552. | MAX6650_CFG_PRESCALER_8;
  553. break;
  554. case 16:
  555. config = (config & ~MAX6650_CFG_PRESCALER_MASK)
  556. | MAX6650_CFG_PRESCALER_16;
  557. break;
  558. default:
  559. dev_err(&client->dev, "illegal value for prescaler (%d)\n",
  560. prescaler);
  561. }
  562. dev_info(&client->dev, "Prescaler is set to %d.\n",
  563. 1 << (config & MAX6650_CFG_PRESCALER_MASK));
  564. /*
  565. * If mode is set to "full off", we change it to "open loop" and
  566. * set DAC to 255, which has the same effect. We do this because
  567. * there's no "full off" mode defined in hwmon specifcations.
  568. */
  569. if ((config & MAX6650_CFG_MODE_MASK) == MAX6650_CFG_MODE_OFF) {
  570. dev_dbg(&client->dev, "Change mode to open loop, full off.\n");
  571. config = (config & ~MAX6650_CFG_MODE_MASK)
  572. | MAX6650_CFG_MODE_OPEN_LOOP;
  573. if (i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, 255)) {
  574. dev_err(&client->dev, "DAC write error, aborting.\n");
  575. return err;
  576. }
  577. }
  578. if (i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, config)) {
  579. dev_err(&client->dev, "Config write error, aborting.\n");
  580. return err;
  581. }
  582. data->config = config;
  583. data->count = i2c_smbus_read_byte_data(client, MAX6650_REG_COUNT);
  584. return 0;
  585. }
  586. static const u8 tach_reg[] = {
  587. MAX6650_REG_TACH0,
  588. MAX6650_REG_TACH1,
  589. MAX6650_REG_TACH2,
  590. MAX6650_REG_TACH3,
  591. };
  592. static struct max6650_data *max6650_update_device(struct device *dev)
  593. {
  594. int i;
  595. struct i2c_client *client = to_i2c_client(dev);
  596. struct max6650_data *data = i2c_get_clientdata(client);
  597. mutex_lock(&data->update_lock);
  598. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  599. data->speed = i2c_smbus_read_byte_data(client,
  600. MAX6650_REG_SPEED);
  601. data->config = i2c_smbus_read_byte_data(client,
  602. MAX6650_REG_CONFIG);
  603. for (i = 0; i < data->nr_fans; i++) {
  604. data->tach[i] = i2c_smbus_read_byte_data(client,
  605. tach_reg[i]);
  606. }
  607. data->count = i2c_smbus_read_byte_data(client,
  608. MAX6650_REG_COUNT);
  609. data->dac = i2c_smbus_read_byte_data(client, MAX6650_REG_DAC);
  610. /*
  611. * Alarms are cleared on read in case the condition that
  612. * caused the alarm is removed. Keep the value latched here
  613. * for providing the register through different alarm files.
  614. */
  615. data->alarm |= i2c_smbus_read_byte_data(client,
  616. MAX6650_REG_ALARM);
  617. data->last_updated = jiffies;
  618. data->valid = 1;
  619. }
  620. mutex_unlock(&data->update_lock);
  621. return data;
  622. }
  623. module_i2c_driver(max6650_driver);
  624. MODULE_AUTHOR("Hans J. Koch");
  625. MODULE_DESCRIPTION("MAX6650 sensor driver");
  626. MODULE_LICENSE("GPL");