max6650.c 19 KB

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