max6650.c 19 KB

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