max6650.c 20 KB

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