max6650.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * max6650.c - Part of lm_sensors, Linux kernel modules for hardware
  4. * monitoring.
  5. *
  6. * (C) 2007 by Hans J. Koch <[email protected]>
  7. *
  8. * based on code written by John Morris <[email protected]>
  9. * Copyright (c) 2003 Spirent Communications
  10. * and Claus Gindhart <[email protected]>
  11. *
  12. * This module has only been tested with the MAX6650 chip. It should
  13. * also work with the MAX6651. It does not distinguish max6650 and max6651
  14. * chips.
  15. *
  16. * The datasheet was last seen at:
  17. *
  18. * http://pdfserv.maxim-ic.com/en/ds/MAX6650-MAX6651.pdf
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/i2c.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/hwmon-sysfs.h>
  27. #include <linux/err.h>
  28. #include <linux/of_device.h>
  29. #include <linux/thermal.h>
  30. /*
  31. * Insmod parameters
  32. */
  33. /* fan_voltage: 5=5V fan, 12=12V fan, 0=don't change */
  34. static int fan_voltage;
  35. /* prescaler: Possible values are 1, 2, 4, 8, 16 or 0 for don't change */
  36. static int prescaler;
  37. /* clock: The clock frequency of the chip (max6651 can be clocked externally) */
  38. static int clock = 254000;
  39. module_param(fan_voltage, int, 0444);
  40. module_param(prescaler, int, 0444);
  41. module_param(clock, int, 0444);
  42. /*
  43. * MAX 6650/6651 registers
  44. */
  45. #define MAX6650_REG_SPEED 0x00
  46. #define MAX6650_REG_CONFIG 0x02
  47. #define MAX6650_REG_GPIO_DEF 0x04
  48. #define MAX6650_REG_DAC 0x06
  49. #define MAX6650_REG_ALARM_EN 0x08
  50. #define MAX6650_REG_ALARM 0x0A
  51. #define MAX6650_REG_TACH0 0x0C
  52. #define MAX6650_REG_TACH1 0x0E
  53. #define MAX6650_REG_TACH2 0x10
  54. #define MAX6650_REG_TACH3 0x12
  55. #define MAX6650_REG_GPIO_STAT 0x14
  56. #define MAX6650_REG_COUNT 0x16
  57. /*
  58. * Config register bits
  59. */
  60. #define MAX6650_CFG_V12 0x08
  61. #define MAX6650_CFG_PRESCALER_MASK 0x07
  62. #define MAX6650_CFG_PRESCALER_2 0x01
  63. #define MAX6650_CFG_PRESCALER_4 0x02
  64. #define MAX6650_CFG_PRESCALER_8 0x03
  65. #define MAX6650_CFG_PRESCALER_16 0x04
  66. #define MAX6650_CFG_MODE_MASK 0x30
  67. #define MAX6650_CFG_MODE_ON 0x00
  68. #define MAX6650_CFG_MODE_OFF 0x10
  69. #define MAX6650_CFG_MODE_CLOSED_LOOP 0x20
  70. #define MAX6650_CFG_MODE_OPEN_LOOP 0x30
  71. #define MAX6650_COUNT_MASK 0x03
  72. /*
  73. * Alarm status register bits
  74. */
  75. #define MAX6650_ALRM_MAX 0x01
  76. #define MAX6650_ALRM_MIN 0x02
  77. #define MAX6650_ALRM_TACH 0x04
  78. #define MAX6650_ALRM_GPIO1 0x08
  79. #define MAX6650_ALRM_GPIO2 0x10
  80. /* Minimum and maximum values of the FAN-RPM */
  81. #define FAN_RPM_MIN 240
  82. #define FAN_RPM_MAX 30000
  83. #define DIV_FROM_REG(reg) (1 << ((reg) & 7))
  84. #define DAC_LIMIT(v12) ((v12) ? 180 : 76)
  85. /*
  86. * Client data (each client gets its own)
  87. */
  88. struct max6650_data {
  89. struct i2c_client *client;
  90. struct mutex update_lock; /* protect alarm register updates */
  91. int nr_fans;
  92. bool valid; /* false until following fields are valid */
  93. unsigned long last_updated; /* in jiffies */
  94. /* register values */
  95. u8 speed;
  96. u8 config;
  97. u8 tach[4];
  98. u8 count;
  99. u8 dac;
  100. u8 alarm;
  101. u8 alarm_en;
  102. unsigned long cooling_dev_state;
  103. };
  104. static const u8 tach_reg[] = {
  105. MAX6650_REG_TACH0,
  106. MAX6650_REG_TACH1,
  107. MAX6650_REG_TACH2,
  108. MAX6650_REG_TACH3,
  109. };
  110. static const struct of_device_id __maybe_unused max6650_dt_match[] = {
  111. {
  112. .compatible = "maxim,max6650",
  113. .data = (void *)1
  114. },
  115. {
  116. .compatible = "maxim,max6651",
  117. .data = (void *)4
  118. },
  119. { },
  120. };
  121. MODULE_DEVICE_TABLE(of, max6650_dt_match);
  122. static int dac_to_pwm(int dac, bool v12)
  123. {
  124. /*
  125. * Useful range for dac is 0-180 for 12V fans and 0-76 for 5V fans.
  126. * Lower DAC values mean higher speeds.
  127. */
  128. return clamp_val(255 - (255 * dac) / DAC_LIMIT(v12), 0, 255);
  129. }
  130. static u8 pwm_to_dac(unsigned int pwm, bool v12)
  131. {
  132. int limit = DAC_LIMIT(v12);
  133. return limit - (limit * pwm) / 255;
  134. }
  135. static struct max6650_data *max6650_update_device(struct device *dev)
  136. {
  137. struct max6650_data *data = dev_get_drvdata(dev);
  138. struct i2c_client *client = data->client;
  139. int reg, err = 0;
  140. int i;
  141. mutex_lock(&data->update_lock);
  142. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  143. for (i = 0; i < data->nr_fans; i++) {
  144. reg = i2c_smbus_read_byte_data(client, tach_reg[i]);
  145. if (reg < 0) {
  146. err = reg;
  147. goto error;
  148. }
  149. data->tach[i] = reg;
  150. }
  151. /*
  152. * Alarms are cleared on read in case the condition that
  153. * caused the alarm is removed. Keep the value latched here
  154. * for providing the register through different alarm files.
  155. */
  156. reg = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM);
  157. if (reg < 0) {
  158. err = reg;
  159. goto error;
  160. }
  161. data->alarm |= reg;
  162. data->last_updated = jiffies;
  163. data->valid = true;
  164. }
  165. error:
  166. mutex_unlock(&data->update_lock);
  167. if (err)
  168. data = ERR_PTR(err);
  169. return data;
  170. }
  171. /*
  172. * Change the operating mode of the chip (if needed).
  173. * mode is one of the MAX6650_CFG_MODE_* values.
  174. */
  175. static int max6650_set_operating_mode(struct max6650_data *data, u8 mode)
  176. {
  177. int result;
  178. u8 config = data->config;
  179. if (mode == (config & MAX6650_CFG_MODE_MASK))
  180. return 0;
  181. config = (config & ~MAX6650_CFG_MODE_MASK) | mode;
  182. result = i2c_smbus_write_byte_data(data->client, MAX6650_REG_CONFIG,
  183. config);
  184. if (result < 0)
  185. return result;
  186. data->config = config;
  187. return 0;
  188. }
  189. /*
  190. * Set the fan speed to the specified RPM (or read back the RPM setting).
  191. * This works in closed loop mode only. Use pwm1 for open loop speed setting.
  192. *
  193. * The MAX6650/1 will automatically control fan speed when in closed loop
  194. * mode.
  195. *
  196. * Assumptions:
  197. *
  198. * 1) The MAX6650/1 internal 254kHz clock frequency is set correctly. Use
  199. * the clock module parameter if you need to fine tune this.
  200. *
  201. * 2) The prescaler (low three bits of the config register) has already
  202. * been set to an appropriate value. Use the prescaler module parameter
  203. * if your BIOS doesn't initialize the chip properly.
  204. *
  205. * The relevant equations are given on pages 21 and 22 of the datasheet.
  206. *
  207. * From the datasheet, the relevant equation when in regulation is:
  208. *
  209. * [fCLK / (128 x (KTACH + 1))] = 2 x FanSpeed / KSCALE
  210. *
  211. * where:
  212. *
  213. * fCLK is the oscillator frequency (either the 254kHz internal
  214. * oscillator or the externally applied clock)
  215. *
  216. * KTACH is the value in the speed register
  217. *
  218. * FanSpeed is the speed of the fan in rps
  219. *
  220. * KSCALE is the prescaler value (1, 2, 4, 8, or 16)
  221. *
  222. * When reading, we need to solve for FanSpeed. When writing, we need to
  223. * solve for KTACH.
  224. *
  225. * Note: this tachometer is completely separate from the tachometers
  226. * used to measure the fan speeds. Only one fan's speed (fan1) is
  227. * controlled.
  228. */
  229. static int max6650_set_target(struct max6650_data *data, unsigned long rpm)
  230. {
  231. int kscale, ktach;
  232. if (rpm == 0)
  233. return max6650_set_operating_mode(data, MAX6650_CFG_MODE_OFF);
  234. rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
  235. /*
  236. * Divide the required speed by 60 to get from rpm to rps, then
  237. * use the datasheet equation:
  238. *
  239. * KTACH = [(fCLK x KSCALE) / (256 x FanSpeed)] - 1
  240. */
  241. kscale = DIV_FROM_REG(data->config);
  242. ktach = ((clock * kscale) / (256 * rpm / 60)) - 1;
  243. if (ktach < 0)
  244. ktach = 0;
  245. if (ktach > 255)
  246. ktach = 255;
  247. data->speed = ktach;
  248. return i2c_smbus_write_byte_data(data->client, MAX6650_REG_SPEED,
  249. data->speed);
  250. }
  251. /*
  252. * Get gpio alarm status:
  253. * Possible values:
  254. * 0 = no alarm
  255. * 1 = alarm
  256. */
  257. static ssize_t alarm_show(struct device *dev,
  258. struct device_attribute *devattr, char *buf)
  259. {
  260. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  261. struct max6650_data *data = max6650_update_device(dev);
  262. bool alarm;
  263. if (IS_ERR(data))
  264. return PTR_ERR(data);
  265. alarm = data->alarm & attr->index;
  266. if (alarm) {
  267. mutex_lock(&data->update_lock);
  268. data->alarm &= ~attr->index;
  269. data->valid = false;
  270. mutex_unlock(&data->update_lock);
  271. }
  272. return sprintf(buf, "%d\n", alarm);
  273. }
  274. static SENSOR_DEVICE_ATTR_RO(gpio1_alarm, alarm, MAX6650_ALRM_GPIO1);
  275. static SENSOR_DEVICE_ATTR_RO(gpio2_alarm, alarm, MAX6650_ALRM_GPIO2);
  276. static umode_t max6650_attrs_visible(struct kobject *kobj, struct attribute *a,
  277. int n)
  278. {
  279. struct device *dev = kobj_to_dev(kobj);
  280. struct max6650_data *data = dev_get_drvdata(dev);
  281. struct device_attribute *devattr;
  282. /*
  283. * Hide the alarms that have not been enabled by the firmware
  284. */
  285. devattr = container_of(a, struct device_attribute, attr);
  286. if (devattr == &sensor_dev_attr_gpio1_alarm.dev_attr ||
  287. devattr == &sensor_dev_attr_gpio2_alarm.dev_attr) {
  288. if (!(data->alarm_en & to_sensor_dev_attr(devattr)->index))
  289. return 0;
  290. }
  291. return a->mode;
  292. }
  293. static struct attribute *max6650_attrs[] = {
  294. &sensor_dev_attr_gpio1_alarm.dev_attr.attr,
  295. &sensor_dev_attr_gpio2_alarm.dev_attr.attr,
  296. NULL
  297. };
  298. static const struct attribute_group max6650_group = {
  299. .attrs = max6650_attrs,
  300. .is_visible = max6650_attrs_visible,
  301. };
  302. static const struct attribute_group *max6650_groups[] = {
  303. &max6650_group,
  304. NULL
  305. };
  306. static int max6650_init_client(struct max6650_data *data,
  307. struct i2c_client *client)
  308. {
  309. struct device *dev = &client->dev;
  310. int reg;
  311. int err;
  312. u32 voltage;
  313. u32 prescale;
  314. u32 target_rpm;
  315. if (of_property_read_u32(dev->of_node, "maxim,fan-microvolt",
  316. &voltage))
  317. voltage = fan_voltage;
  318. else
  319. voltage /= 1000000; /* Microvolts to volts */
  320. if (of_property_read_u32(dev->of_node, "maxim,fan-prescale",
  321. &prescale))
  322. prescale = prescaler;
  323. reg = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG);
  324. if (reg < 0) {
  325. dev_err(dev, "Error reading config register, aborting.\n");
  326. return reg;
  327. }
  328. switch (voltage) {
  329. case 0:
  330. break;
  331. case 5:
  332. reg &= ~MAX6650_CFG_V12;
  333. break;
  334. case 12:
  335. reg |= MAX6650_CFG_V12;
  336. break;
  337. default:
  338. dev_err(dev, "illegal value for fan_voltage (%d)\n", voltage);
  339. }
  340. switch (prescale) {
  341. case 0:
  342. break;
  343. case 1:
  344. reg &= ~MAX6650_CFG_PRESCALER_MASK;
  345. break;
  346. case 2:
  347. reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
  348. | MAX6650_CFG_PRESCALER_2;
  349. break;
  350. case 4:
  351. reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
  352. | MAX6650_CFG_PRESCALER_4;
  353. break;
  354. case 8:
  355. reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
  356. | MAX6650_CFG_PRESCALER_8;
  357. break;
  358. case 16:
  359. reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
  360. | MAX6650_CFG_PRESCALER_16;
  361. break;
  362. default:
  363. dev_err(dev, "illegal value for prescaler (%d)\n", prescale);
  364. }
  365. dev_info(dev, "Fan voltage: %dV, prescaler: %d.\n",
  366. (reg & MAX6650_CFG_V12) ? 12 : 5,
  367. 1 << (reg & MAX6650_CFG_PRESCALER_MASK));
  368. err = i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, reg);
  369. if (err) {
  370. dev_err(dev, "Config write error, aborting.\n");
  371. return err;
  372. }
  373. data->config = reg;
  374. reg = i2c_smbus_read_byte_data(client, MAX6650_REG_SPEED);
  375. if (reg < 0) {
  376. dev_err(dev, "Failed to read speed register, aborting.\n");
  377. return reg;
  378. }
  379. data->speed = reg;
  380. reg = i2c_smbus_read_byte_data(client, MAX6650_REG_DAC);
  381. if (reg < 0) {
  382. dev_err(dev, "Failed to read DAC register, aborting.\n");
  383. return reg;
  384. }
  385. data->dac = reg;
  386. reg = i2c_smbus_read_byte_data(client, MAX6650_REG_COUNT);
  387. if (reg < 0) {
  388. dev_err(dev, "Failed to read count register, aborting.\n");
  389. return reg;
  390. }
  391. data->count = reg;
  392. reg = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM_EN);
  393. if (reg < 0) {
  394. dev_err(dev, "Failed to read alarm configuration, aborting.\n");
  395. return reg;
  396. }
  397. data->alarm_en = reg;
  398. if (!of_property_read_u32(client->dev.of_node, "maxim,fan-target-rpm",
  399. &target_rpm)) {
  400. max6650_set_target(data, target_rpm);
  401. max6650_set_operating_mode(data, MAX6650_CFG_MODE_CLOSED_LOOP);
  402. }
  403. return 0;
  404. }
  405. static int max6650_get_max_state(struct thermal_cooling_device *cdev,
  406. unsigned long *state)
  407. {
  408. *state = 255;
  409. return 0;
  410. }
  411. static int max6650_get_cur_state(struct thermal_cooling_device *cdev,
  412. unsigned long *state)
  413. {
  414. struct max6650_data *data = cdev->devdata;
  415. *state = data->cooling_dev_state;
  416. return 0;
  417. }
  418. static int max6650_set_cur_state(struct thermal_cooling_device *cdev,
  419. unsigned long state)
  420. {
  421. struct max6650_data *data = cdev->devdata;
  422. struct i2c_client *client = data->client;
  423. int err;
  424. state = clamp_val(state, 0, 255);
  425. mutex_lock(&data->update_lock);
  426. data->dac = pwm_to_dac(state, data->config & MAX6650_CFG_V12);
  427. err = i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, data->dac);
  428. if (!err) {
  429. max6650_set_operating_mode(data, state ?
  430. MAX6650_CFG_MODE_OPEN_LOOP :
  431. MAX6650_CFG_MODE_OFF);
  432. data->cooling_dev_state = state;
  433. }
  434. mutex_unlock(&data->update_lock);
  435. return err;
  436. }
  437. static const struct thermal_cooling_device_ops max6650_cooling_ops = {
  438. .get_max_state = max6650_get_max_state,
  439. .get_cur_state = max6650_get_cur_state,
  440. .set_cur_state = max6650_set_cur_state,
  441. };
  442. static int max6650_read(struct device *dev, enum hwmon_sensor_types type,
  443. u32 attr, int channel, long *val)
  444. {
  445. struct max6650_data *data = max6650_update_device(dev);
  446. int mode;
  447. if (IS_ERR(data))
  448. return PTR_ERR(data);
  449. switch (type) {
  450. case hwmon_pwm:
  451. switch (attr) {
  452. case hwmon_pwm_input:
  453. *val = dac_to_pwm(data->dac,
  454. data->config & MAX6650_CFG_V12);
  455. break;
  456. case hwmon_pwm_enable:
  457. /*
  458. * Possible values:
  459. * 0 = Fan always on
  460. * 1 = Open loop, Voltage is set according to speed,
  461. * not regulated.
  462. * 2 = Closed loop, RPM for all fans regulated by fan1
  463. * tachometer
  464. * 3 = Fan off
  465. */
  466. mode = (data->config & MAX6650_CFG_MODE_MASK) >> 4;
  467. *val = (4 - mode) & 3; /* {0 1 2 3} -> {0 3 2 1} */
  468. break;
  469. default:
  470. return -EOPNOTSUPP;
  471. }
  472. break;
  473. case hwmon_fan:
  474. switch (attr) {
  475. case hwmon_fan_input:
  476. /*
  477. * Calculation details:
  478. *
  479. * Each tachometer counts over an interval given by the
  480. * "count" register (0.25, 0.5, 1 or 2 seconds).
  481. * The driver assumes that the fans produce two pulses
  482. * per revolution (this seems to be the most common).
  483. */
  484. *val = DIV_ROUND_CLOSEST(data->tach[channel] * 120,
  485. DIV_FROM_REG(data->count));
  486. break;
  487. case hwmon_fan_div:
  488. *val = DIV_FROM_REG(data->count);
  489. break;
  490. case hwmon_fan_target:
  491. /*
  492. * Use the datasheet equation:
  493. * FanSpeed = KSCALE x fCLK / [256 x (KTACH + 1)]
  494. * then multiply by 60 to give rpm.
  495. */
  496. *val = 60 * DIV_FROM_REG(data->config) * clock /
  497. (256 * (data->speed + 1));
  498. break;
  499. case hwmon_fan_min_alarm:
  500. *val = !!(data->alarm & MAX6650_ALRM_MIN);
  501. data->alarm &= ~MAX6650_ALRM_MIN;
  502. data->valid = false;
  503. break;
  504. case hwmon_fan_max_alarm:
  505. *val = !!(data->alarm & MAX6650_ALRM_MAX);
  506. data->alarm &= ~MAX6650_ALRM_MAX;
  507. data->valid = false;
  508. break;
  509. case hwmon_fan_fault:
  510. *val = !!(data->alarm & MAX6650_ALRM_TACH);
  511. data->alarm &= ~MAX6650_ALRM_TACH;
  512. data->valid = false;
  513. break;
  514. default:
  515. return -EOPNOTSUPP;
  516. }
  517. break;
  518. default:
  519. return -EOPNOTSUPP;
  520. }
  521. return 0;
  522. }
  523. static const u8 max6650_pwm_modes[] = {
  524. MAX6650_CFG_MODE_ON,
  525. MAX6650_CFG_MODE_OPEN_LOOP,
  526. MAX6650_CFG_MODE_CLOSED_LOOP,
  527. MAX6650_CFG_MODE_OFF,
  528. };
  529. static int max6650_write(struct device *dev, enum hwmon_sensor_types type,
  530. u32 attr, int channel, long val)
  531. {
  532. struct max6650_data *data = dev_get_drvdata(dev);
  533. int ret = 0;
  534. u8 reg;
  535. mutex_lock(&data->update_lock);
  536. switch (type) {
  537. case hwmon_pwm:
  538. switch (attr) {
  539. case hwmon_pwm_input:
  540. reg = pwm_to_dac(clamp_val(val, 0, 255),
  541. data->config & MAX6650_CFG_V12);
  542. ret = i2c_smbus_write_byte_data(data->client,
  543. MAX6650_REG_DAC, reg);
  544. if (ret)
  545. break;
  546. data->dac = reg;
  547. break;
  548. case hwmon_pwm_enable:
  549. if (val < 0 || val >= ARRAY_SIZE(max6650_pwm_modes)) {
  550. ret = -EINVAL;
  551. break;
  552. }
  553. ret = max6650_set_operating_mode(data,
  554. max6650_pwm_modes[val]);
  555. break;
  556. default:
  557. ret = -EOPNOTSUPP;
  558. break;
  559. }
  560. break;
  561. case hwmon_fan:
  562. switch (attr) {
  563. case hwmon_fan_div:
  564. switch (val) {
  565. case 1:
  566. reg = 0;
  567. break;
  568. case 2:
  569. reg = 1;
  570. break;
  571. case 4:
  572. reg = 2;
  573. break;
  574. case 8:
  575. reg = 3;
  576. break;
  577. default:
  578. ret = -EINVAL;
  579. goto error;
  580. }
  581. ret = i2c_smbus_write_byte_data(data->client,
  582. MAX6650_REG_COUNT, reg);
  583. if (ret)
  584. break;
  585. data->count = reg;
  586. break;
  587. case hwmon_fan_target:
  588. if (val < 0) {
  589. ret = -EINVAL;
  590. break;
  591. }
  592. ret = max6650_set_target(data, val);
  593. break;
  594. default:
  595. ret = -EOPNOTSUPP;
  596. break;
  597. }
  598. break;
  599. default:
  600. ret = -EOPNOTSUPP;
  601. break;
  602. }
  603. error:
  604. mutex_unlock(&data->update_lock);
  605. return ret;
  606. }
  607. static umode_t max6650_is_visible(const void *_data,
  608. enum hwmon_sensor_types type, u32 attr,
  609. int channel)
  610. {
  611. const struct max6650_data *data = _data;
  612. if (channel && (channel >= data->nr_fans || type != hwmon_fan))
  613. return 0;
  614. switch (type) {
  615. case hwmon_fan:
  616. switch (attr) {
  617. case hwmon_fan_input:
  618. return 0444;
  619. case hwmon_fan_target:
  620. case hwmon_fan_div:
  621. return 0644;
  622. case hwmon_fan_min_alarm:
  623. if (data->alarm_en & MAX6650_ALRM_MIN)
  624. return 0444;
  625. break;
  626. case hwmon_fan_max_alarm:
  627. if (data->alarm_en & MAX6650_ALRM_MAX)
  628. return 0444;
  629. break;
  630. case hwmon_fan_fault:
  631. if (data->alarm_en & MAX6650_ALRM_TACH)
  632. return 0444;
  633. break;
  634. default:
  635. break;
  636. }
  637. break;
  638. case hwmon_pwm:
  639. switch (attr) {
  640. case hwmon_pwm_input:
  641. case hwmon_pwm_enable:
  642. return 0644;
  643. default:
  644. break;
  645. }
  646. break;
  647. default:
  648. break;
  649. }
  650. return 0;
  651. }
  652. static const struct hwmon_channel_info *max6650_info[] = {
  653. HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_DIV |
  654. HWMON_F_MIN_ALARM | HWMON_F_MAX_ALARM |
  655. HWMON_F_FAULT,
  656. HWMON_F_INPUT, HWMON_F_INPUT, HWMON_F_INPUT),
  657. HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
  658. NULL
  659. };
  660. static const struct hwmon_ops max6650_hwmon_ops = {
  661. .read = max6650_read,
  662. .write = max6650_write,
  663. .is_visible = max6650_is_visible,
  664. };
  665. static const struct hwmon_chip_info max6650_chip_info = {
  666. .ops = &max6650_hwmon_ops,
  667. .info = max6650_info,
  668. };
  669. static const struct i2c_device_id max6650_id[];
  670. static int max6650_probe(struct i2c_client *client)
  671. {
  672. struct thermal_cooling_device *cooling_dev;
  673. struct device *dev = &client->dev;
  674. const struct of_device_id *of_id =
  675. of_match_device(of_match_ptr(max6650_dt_match), dev);
  676. struct max6650_data *data;
  677. struct device *hwmon_dev;
  678. int err;
  679. data = devm_kzalloc(dev, sizeof(struct max6650_data), GFP_KERNEL);
  680. if (!data)
  681. return -ENOMEM;
  682. data->client = client;
  683. i2c_set_clientdata(client, data);
  684. mutex_init(&data->update_lock);
  685. data->nr_fans = of_id ? (int)(uintptr_t)of_id->data :
  686. i2c_match_id(max6650_id, client)->driver_data;
  687. /*
  688. * Initialize the max6650 chip
  689. */
  690. err = max6650_init_client(data, client);
  691. if (err)
  692. return err;
  693. hwmon_dev = devm_hwmon_device_register_with_info(dev,
  694. client->name, data,
  695. &max6650_chip_info,
  696. max6650_groups);
  697. err = PTR_ERR_OR_ZERO(hwmon_dev);
  698. if (err)
  699. return err;
  700. if (IS_ENABLED(CONFIG_THERMAL)) {
  701. cooling_dev = devm_thermal_of_cooling_device_register(dev,
  702. dev->of_node, client->name,
  703. data, &max6650_cooling_ops);
  704. if (IS_ERR(cooling_dev)) {
  705. dev_warn(dev, "thermal cooling device register failed: %ld\n",
  706. PTR_ERR(cooling_dev));
  707. }
  708. }
  709. return 0;
  710. }
  711. static const struct i2c_device_id max6650_id[] = {
  712. { "max6650", 1 },
  713. { "max6651", 4 },
  714. { }
  715. };
  716. MODULE_DEVICE_TABLE(i2c, max6650_id);
  717. static struct i2c_driver max6650_driver = {
  718. .driver = {
  719. .name = "max6650",
  720. .of_match_table = of_match_ptr(max6650_dt_match),
  721. },
  722. .probe_new = max6650_probe,
  723. .id_table = max6650_id,
  724. };
  725. module_i2c_driver(max6650_driver);
  726. MODULE_AUTHOR("Hans J. Koch");
  727. MODULE_DESCRIPTION("MAX6650 sensor driver");
  728. MODULE_LICENSE("GPL");