f75375s.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. * f75375s.c - driver for the Fintek F75375/SP, F75373 and
  3. * F75387SG/RG hardware monitoring features
  4. * Copyright (C) 2006-2007 Riku Voipio
  5. *
  6. * Datasheets available at:
  7. *
  8. * f75375:
  9. * http://www.fintek.com.tw/files/productfiles/F75375_V026P.pdf
  10. *
  11. * f75373:
  12. * http://www.fintek.com.tw/files/productfiles/F75373_V025P.pdf
  13. *
  14. * f75387:
  15. * http://www.fintek.com.tw/files/productfiles/F75387_V027P.pdf
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30. *
  31. */
  32. #include <linux/module.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/hwmon.h>
  35. #include <linux/hwmon-sysfs.h>
  36. #include <linux/i2c.h>
  37. #include <linux/err.h>
  38. #include <linux/mutex.h>
  39. #include <linux/f75375s.h>
  40. #include <linux/slab.h>
  41. /* Addresses to scan */
  42. static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
  43. enum chips { f75373, f75375, f75387 };
  44. /* Fintek F75375 registers */
  45. #define F75375_REG_CONFIG0 0x0
  46. #define F75375_REG_CONFIG1 0x1
  47. #define F75375_REG_CONFIG2 0x2
  48. #define F75375_REG_CONFIG3 0x3
  49. #define F75375_REG_ADDR 0x4
  50. #define F75375_REG_INTR 0x31
  51. #define F75375_CHIP_ID 0x5A
  52. #define F75375_REG_VERSION 0x5C
  53. #define F75375_REG_VENDOR 0x5D
  54. #define F75375_REG_FAN_TIMER 0x60
  55. #define F75375_REG_VOLT(nr) (0x10 + (nr))
  56. #define F75375_REG_VOLT_HIGH(nr) (0x20 + (nr) * 2)
  57. #define F75375_REG_VOLT_LOW(nr) (0x21 + (nr) * 2)
  58. #define F75375_REG_TEMP(nr) (0x14 + (nr))
  59. #define F75387_REG_TEMP11_LSB(nr) (0x1a + (nr))
  60. #define F75375_REG_TEMP_HIGH(nr) (0x28 + (nr) * 2)
  61. #define F75375_REG_TEMP_HYST(nr) (0x29 + (nr) * 2)
  62. #define F75375_REG_FAN(nr) (0x16 + (nr) * 2)
  63. #define F75375_REG_FAN_MIN(nr) (0x2C + (nr) * 2)
  64. #define F75375_REG_FAN_FULL(nr) (0x70 + (nr) * 0x10)
  65. #define F75375_REG_FAN_PWM_DUTY(nr) (0x76 + (nr) * 0x10)
  66. #define F75375_REG_FAN_PWM_CLOCK(nr) (0x7D + (nr) * 0x10)
  67. #define F75375_REG_FAN_EXP(nr) (0x74 + (nr) * 0x10)
  68. #define F75375_REG_FAN_B_TEMP(nr, step) ((0xA0 + (nr) * 0x10) + (step))
  69. #define F75375_REG_FAN_B_SPEED(nr, step) \
  70. ((0xA5 + (nr) * 0x10) + (step) * 2)
  71. #define F75375_REG_PWM1_RAISE_DUTY 0x69
  72. #define F75375_REG_PWM2_RAISE_DUTY 0x6A
  73. #define F75375_REG_PWM1_DROP_DUTY 0x6B
  74. #define F75375_REG_PWM2_DROP_DUTY 0x6C
  75. #define F75375_FAN_CTRL_LINEAR(nr) (4 + nr)
  76. #define F75387_FAN_CTRL_LINEAR(nr) (1 + ((nr) * 4))
  77. #define FAN_CTRL_MODE(nr) (4 + ((nr) * 2))
  78. #define F75387_FAN_DUTY_MODE(nr) (2 + ((nr) * 4))
  79. #define F75387_FAN_MANU_MODE(nr) ((nr) * 4)
  80. /*
  81. * Data structures and manipulation thereof
  82. */
  83. struct f75375_data {
  84. unsigned short addr;
  85. struct device *hwmon_dev;
  86. const char *name;
  87. int kind;
  88. struct mutex update_lock; /* protect register access */
  89. char valid;
  90. unsigned long last_updated; /* In jiffies */
  91. unsigned long last_limits; /* In jiffies */
  92. /* Register values */
  93. u8 in[4];
  94. u8 in_max[4];
  95. u8 in_min[4];
  96. u16 fan[2];
  97. u16 fan_min[2];
  98. u16 fan_max[2];
  99. u16 fan_target[2];
  100. u8 fan_timer;
  101. u8 pwm[2];
  102. u8 pwm_mode[2];
  103. u8 pwm_enable[2];
  104. /*
  105. * f75387: For remote temperature reading, it uses signed 11-bit
  106. * values with LSB = 0.125 degree Celsius, left-justified in 16-bit
  107. * registers. For original 8-bit temp readings, the LSB just is 0.
  108. */
  109. s16 temp11[2];
  110. s8 temp_high[2];
  111. s8 temp_max_hyst[2];
  112. };
  113. static int f75375_detect(struct i2c_client *client,
  114. struct i2c_board_info *info);
  115. static int f75375_probe(struct i2c_client *client,
  116. const struct i2c_device_id *id);
  117. static int f75375_remove(struct i2c_client *client);
  118. static const struct i2c_device_id f75375_id[] = {
  119. { "f75373", f75373 },
  120. { "f75375", f75375 },
  121. { "f75387", f75387 },
  122. { }
  123. };
  124. MODULE_DEVICE_TABLE(i2c, f75375_id);
  125. static struct i2c_driver f75375_driver = {
  126. .class = I2C_CLASS_HWMON,
  127. .driver = {
  128. .name = "f75375",
  129. },
  130. .probe = f75375_probe,
  131. .remove = f75375_remove,
  132. .id_table = f75375_id,
  133. .detect = f75375_detect,
  134. .address_list = normal_i2c,
  135. };
  136. static inline int f75375_read8(struct i2c_client *client, u8 reg)
  137. {
  138. return i2c_smbus_read_byte_data(client, reg);
  139. }
  140. /* in most cases, should be called while holding update_lock */
  141. static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
  142. {
  143. return (i2c_smbus_read_byte_data(client, reg) << 8)
  144. | i2c_smbus_read_byte_data(client, reg + 1);
  145. }
  146. static inline void f75375_write8(struct i2c_client *client, u8 reg,
  147. u8 value)
  148. {
  149. i2c_smbus_write_byte_data(client, reg, value);
  150. }
  151. static inline void f75375_write16(struct i2c_client *client, u8 reg,
  152. u16 value)
  153. {
  154. int err = i2c_smbus_write_byte_data(client, reg, (value >> 8));
  155. if (err)
  156. return;
  157. i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
  158. }
  159. static void f75375_write_pwm(struct i2c_client *client, int nr)
  160. {
  161. struct f75375_data *data = i2c_get_clientdata(client);
  162. if (data->kind == f75387)
  163. f75375_write16(client, F75375_REG_FAN_EXP(nr), data->pwm[nr]);
  164. else
  165. f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
  166. data->pwm[nr]);
  167. }
  168. static struct f75375_data *f75375_update_device(struct device *dev)
  169. {
  170. struct i2c_client *client = to_i2c_client(dev);
  171. struct f75375_data *data = i2c_get_clientdata(client);
  172. int nr;
  173. mutex_lock(&data->update_lock);
  174. /* Limit registers cache is refreshed after 60 seconds */
  175. if (time_after(jiffies, data->last_limits + 60 * HZ)
  176. || !data->valid) {
  177. for (nr = 0; nr < 2; nr++) {
  178. data->temp_high[nr] =
  179. f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
  180. data->temp_max_hyst[nr] =
  181. f75375_read8(client, F75375_REG_TEMP_HYST(nr));
  182. data->fan_max[nr] =
  183. f75375_read16(client, F75375_REG_FAN_FULL(nr));
  184. data->fan_min[nr] =
  185. f75375_read16(client, F75375_REG_FAN_MIN(nr));
  186. data->fan_target[nr] =
  187. f75375_read16(client, F75375_REG_FAN_EXP(nr));
  188. }
  189. for (nr = 0; nr < 4; nr++) {
  190. data->in_max[nr] =
  191. f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
  192. data->in_min[nr] =
  193. f75375_read8(client, F75375_REG_VOLT_LOW(nr));
  194. }
  195. data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
  196. data->last_limits = jiffies;
  197. }
  198. /* Measurement registers cache is refreshed after 2 second */
  199. if (time_after(jiffies, data->last_updated + 2 * HZ)
  200. || !data->valid) {
  201. for (nr = 0; nr < 2; nr++) {
  202. data->pwm[nr] = f75375_read8(client,
  203. F75375_REG_FAN_PWM_DUTY(nr));
  204. /* assign MSB, therefore shift it by 8 bits */
  205. data->temp11[nr] =
  206. f75375_read8(client, F75375_REG_TEMP(nr)) << 8;
  207. if (data->kind == f75387)
  208. /* merge F75387's temperature LSB (11-bit) */
  209. data->temp11[nr] |=
  210. f75375_read8(client,
  211. F75387_REG_TEMP11_LSB(nr));
  212. data->fan[nr] =
  213. f75375_read16(client, F75375_REG_FAN(nr));
  214. }
  215. for (nr = 0; nr < 4; nr++)
  216. data->in[nr] =
  217. f75375_read8(client, F75375_REG_VOLT(nr));
  218. data->last_updated = jiffies;
  219. data->valid = 1;
  220. }
  221. mutex_unlock(&data->update_lock);
  222. return data;
  223. }
  224. static inline u16 rpm_from_reg(u16 reg)
  225. {
  226. if (reg == 0 || reg == 0xffff)
  227. return 0;
  228. return 1500000 / reg;
  229. }
  230. static inline u16 rpm_to_reg(int rpm)
  231. {
  232. if (rpm < 367 || rpm > 0xffff)
  233. return 0xffff;
  234. return 1500000 / rpm;
  235. }
  236. static bool duty_mode_enabled(u8 pwm_enable)
  237. {
  238. switch (pwm_enable) {
  239. case 0: /* Manual, duty mode (full speed) */
  240. case 1: /* Manual, duty mode */
  241. case 4: /* Auto, duty mode */
  242. return true;
  243. case 2: /* Auto, speed mode */
  244. case 3: /* Manual, speed mode */
  245. return false;
  246. default:
  247. WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
  248. return true;
  249. }
  250. }
  251. static bool auto_mode_enabled(u8 pwm_enable)
  252. {
  253. switch (pwm_enable) {
  254. case 0: /* Manual, duty mode (full speed) */
  255. case 1: /* Manual, duty mode */
  256. case 3: /* Manual, speed mode */
  257. return false;
  258. case 2: /* Auto, speed mode */
  259. case 4: /* Auto, duty mode */
  260. return true;
  261. default:
  262. WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
  263. return false;
  264. }
  265. }
  266. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  267. const char *buf, size_t count)
  268. {
  269. int nr = to_sensor_dev_attr(attr)->index;
  270. struct i2c_client *client = to_i2c_client(dev);
  271. struct f75375_data *data = i2c_get_clientdata(client);
  272. unsigned long val;
  273. int err;
  274. err = kstrtoul(buf, 10, &val);
  275. if (err < 0)
  276. return err;
  277. mutex_lock(&data->update_lock);
  278. data->fan_min[nr] = rpm_to_reg(val);
  279. f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
  280. mutex_unlock(&data->update_lock);
  281. return count;
  282. }
  283. static ssize_t set_fan_target(struct device *dev, struct device_attribute *attr,
  284. const char *buf, size_t count)
  285. {
  286. int nr = to_sensor_dev_attr(attr)->index;
  287. struct i2c_client *client = to_i2c_client(dev);
  288. struct f75375_data *data = i2c_get_clientdata(client);
  289. unsigned long val;
  290. int err;
  291. err = kstrtoul(buf, 10, &val);
  292. if (err < 0)
  293. return err;
  294. if (auto_mode_enabled(data->pwm_enable[nr]))
  295. return -EINVAL;
  296. if (data->kind == f75387 && duty_mode_enabled(data->pwm_enable[nr]))
  297. return -EINVAL;
  298. mutex_lock(&data->update_lock);
  299. data->fan_target[nr] = rpm_to_reg(val);
  300. f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_target[nr]);
  301. mutex_unlock(&data->update_lock);
  302. return count;
  303. }
  304. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  305. const char *buf, size_t count)
  306. {
  307. int nr = to_sensor_dev_attr(attr)->index;
  308. struct i2c_client *client = to_i2c_client(dev);
  309. struct f75375_data *data = i2c_get_clientdata(client);
  310. unsigned long val;
  311. int err;
  312. err = kstrtoul(buf, 10, &val);
  313. if (err < 0)
  314. return err;
  315. if (auto_mode_enabled(data->pwm_enable[nr]) ||
  316. !duty_mode_enabled(data->pwm_enable[nr]))
  317. return -EINVAL;
  318. mutex_lock(&data->update_lock);
  319. data->pwm[nr] = clamp_val(val, 0, 255);
  320. f75375_write_pwm(client, nr);
  321. mutex_unlock(&data->update_lock);
  322. return count;
  323. }
  324. static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
  325. *attr, char *buf)
  326. {
  327. int nr = to_sensor_dev_attr(attr)->index;
  328. struct f75375_data *data = f75375_update_device(dev);
  329. return sprintf(buf, "%d\n", data->pwm_enable[nr]);
  330. }
  331. static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
  332. {
  333. struct f75375_data *data = i2c_get_clientdata(client);
  334. u8 fanmode;
  335. if (val < 0 || val > 4)
  336. return -EINVAL;
  337. fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
  338. if (data->kind == f75387) {
  339. /* For now, deny dangerous toggling of duty mode */
  340. if (duty_mode_enabled(data->pwm_enable[nr]) !=
  341. duty_mode_enabled(val))
  342. return -EOPNOTSUPP;
  343. /* clear each fanX_mode bit before setting them properly */
  344. fanmode &= ~(1 << F75387_FAN_DUTY_MODE(nr));
  345. fanmode &= ~(1 << F75387_FAN_MANU_MODE(nr));
  346. switch (val) {
  347. case 0: /* full speed */
  348. fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
  349. fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
  350. data->pwm[nr] = 255;
  351. break;
  352. case 1: /* PWM */
  353. fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
  354. fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
  355. break;
  356. case 2: /* Automatic, speed mode */
  357. break;
  358. case 3: /* fan speed */
  359. fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
  360. break;
  361. case 4: /* Automatic, pwm */
  362. fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
  363. break;
  364. }
  365. } else {
  366. /* clear each fanX_mode bit before setting them properly */
  367. fanmode &= ~(3 << FAN_CTRL_MODE(nr));
  368. switch (val) {
  369. case 0: /* full speed */
  370. fanmode |= (3 << FAN_CTRL_MODE(nr));
  371. data->pwm[nr] = 255;
  372. break;
  373. case 1: /* PWM */
  374. fanmode |= (3 << FAN_CTRL_MODE(nr));
  375. break;
  376. case 2: /* AUTOMATIC*/
  377. fanmode |= (1 << FAN_CTRL_MODE(nr));
  378. break;
  379. case 3: /* fan speed */
  380. break;
  381. case 4: /* Automatic pwm */
  382. return -EINVAL;
  383. }
  384. }
  385. f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
  386. data->pwm_enable[nr] = val;
  387. if (val == 0)
  388. f75375_write_pwm(client, nr);
  389. return 0;
  390. }
  391. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
  392. const char *buf, size_t count)
  393. {
  394. int nr = to_sensor_dev_attr(attr)->index;
  395. struct i2c_client *client = to_i2c_client(dev);
  396. struct f75375_data *data = i2c_get_clientdata(client);
  397. unsigned long val;
  398. int err;
  399. err = kstrtoul(buf, 10, &val);
  400. if (err < 0)
  401. return err;
  402. mutex_lock(&data->update_lock);
  403. err = set_pwm_enable_direct(client, nr, val);
  404. mutex_unlock(&data->update_lock);
  405. return err ? err : count;
  406. }
  407. static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
  408. const char *buf, size_t count)
  409. {
  410. int nr = to_sensor_dev_attr(attr)->index;
  411. struct i2c_client *client = to_i2c_client(dev);
  412. struct f75375_data *data = i2c_get_clientdata(client);
  413. unsigned long val;
  414. int err;
  415. u8 conf;
  416. char reg, ctrl;
  417. err = kstrtoul(buf, 10, &val);
  418. if (err < 0)
  419. return err;
  420. if (!(val == 0 || val == 1))
  421. return -EINVAL;
  422. /* F75373 does not support DC (linear voltage) fan control mode */
  423. if (data->kind == f75373 && val == 0)
  424. return -EINVAL;
  425. /* take care for different registers */
  426. if (data->kind == f75387) {
  427. reg = F75375_REG_FAN_TIMER;
  428. ctrl = F75387_FAN_CTRL_LINEAR(nr);
  429. } else {
  430. reg = F75375_REG_CONFIG1;
  431. ctrl = F75375_FAN_CTRL_LINEAR(nr);
  432. }
  433. mutex_lock(&data->update_lock);
  434. conf = f75375_read8(client, reg);
  435. conf &= ~(1 << ctrl);
  436. if (val == 0)
  437. conf |= (1 << ctrl);
  438. f75375_write8(client, reg, conf);
  439. data->pwm_mode[nr] = val;
  440. mutex_unlock(&data->update_lock);
  441. return count;
  442. }
  443. static ssize_t show_pwm(struct device *dev, struct device_attribute
  444. *attr, char *buf)
  445. {
  446. int nr = to_sensor_dev_attr(attr)->index;
  447. struct f75375_data *data = f75375_update_device(dev);
  448. return sprintf(buf, "%d\n", data->pwm[nr]);
  449. }
  450. static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
  451. *attr, char *buf)
  452. {
  453. int nr = to_sensor_dev_attr(attr)->index;
  454. struct f75375_data *data = f75375_update_device(dev);
  455. return sprintf(buf, "%d\n", data->pwm_mode[nr]);
  456. }
  457. #define VOLT_FROM_REG(val) ((val) * 8)
  458. #define VOLT_TO_REG(val) ((val) / 8)
  459. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  460. char *buf)
  461. {
  462. int nr = to_sensor_dev_attr(attr)->index;
  463. struct f75375_data *data = f75375_update_device(dev);
  464. return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
  465. }
  466. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  467. char *buf)
  468. {
  469. int nr = to_sensor_dev_attr(attr)->index;
  470. struct f75375_data *data = f75375_update_device(dev);
  471. return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
  472. }
  473. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  474. char *buf)
  475. {
  476. int nr = to_sensor_dev_attr(attr)->index;
  477. struct f75375_data *data = f75375_update_device(dev);
  478. return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
  479. }
  480. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  481. const char *buf, size_t count)
  482. {
  483. int nr = to_sensor_dev_attr(attr)->index;
  484. struct i2c_client *client = to_i2c_client(dev);
  485. struct f75375_data *data = i2c_get_clientdata(client);
  486. unsigned long val;
  487. int err;
  488. err = kstrtoul(buf, 10, &val);
  489. if (err < 0)
  490. return err;
  491. val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
  492. mutex_lock(&data->update_lock);
  493. data->in_max[nr] = val;
  494. f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
  495. mutex_unlock(&data->update_lock);
  496. return count;
  497. }
  498. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  499. const char *buf, size_t count)
  500. {
  501. int nr = to_sensor_dev_attr(attr)->index;
  502. struct i2c_client *client = to_i2c_client(dev);
  503. struct f75375_data *data = i2c_get_clientdata(client);
  504. unsigned long val;
  505. int err;
  506. err = kstrtoul(buf, 10, &val);
  507. if (err < 0)
  508. return err;
  509. val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
  510. mutex_lock(&data->update_lock);
  511. data->in_min[nr] = val;
  512. f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
  513. mutex_unlock(&data->update_lock);
  514. return count;
  515. }
  516. #define TEMP_FROM_REG(val) ((val) * 1000)
  517. #define TEMP_TO_REG(val) ((val) / 1000)
  518. #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
  519. static ssize_t show_temp11(struct device *dev, struct device_attribute *attr,
  520. char *buf)
  521. {
  522. int nr = to_sensor_dev_attr(attr)->index;
  523. struct f75375_data *data = f75375_update_device(dev);
  524. return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[nr]));
  525. }
  526. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  527. char *buf)
  528. {
  529. int nr = to_sensor_dev_attr(attr)->index;
  530. struct f75375_data *data = f75375_update_device(dev);
  531. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
  532. }
  533. static ssize_t show_temp_max_hyst(struct device *dev,
  534. struct device_attribute *attr, char *buf)
  535. {
  536. int nr = to_sensor_dev_attr(attr)->index;
  537. struct f75375_data *data = f75375_update_device(dev);
  538. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
  539. }
  540. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  541. const char *buf, size_t count)
  542. {
  543. int nr = to_sensor_dev_attr(attr)->index;
  544. struct i2c_client *client = to_i2c_client(dev);
  545. struct f75375_data *data = i2c_get_clientdata(client);
  546. unsigned long val;
  547. int err;
  548. err = kstrtoul(buf, 10, &val);
  549. if (err < 0)
  550. return err;
  551. val = clamp_val(TEMP_TO_REG(val), 0, 127);
  552. mutex_lock(&data->update_lock);
  553. data->temp_high[nr] = val;
  554. f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
  555. mutex_unlock(&data->update_lock);
  556. return count;
  557. }
  558. static ssize_t set_temp_max_hyst(struct device *dev,
  559. struct device_attribute *attr, const char *buf, size_t count)
  560. {
  561. int nr = to_sensor_dev_attr(attr)->index;
  562. struct i2c_client *client = to_i2c_client(dev);
  563. struct f75375_data *data = i2c_get_clientdata(client);
  564. unsigned long val;
  565. int err;
  566. err = kstrtoul(buf, 10, &val);
  567. if (err < 0)
  568. return err;
  569. val = clamp_val(TEMP_TO_REG(val), 0, 127);
  570. mutex_lock(&data->update_lock);
  571. data->temp_max_hyst[nr] = val;
  572. f75375_write8(client, F75375_REG_TEMP_HYST(nr),
  573. data->temp_max_hyst[nr]);
  574. mutex_unlock(&data->update_lock);
  575. return count;
  576. }
  577. #define show_fan(thing) \
  578. static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
  579. char *buf)\
  580. {\
  581. int nr = to_sensor_dev_attr(attr)->index;\
  582. struct f75375_data *data = f75375_update_device(dev); \
  583. return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
  584. }
  585. show_fan(fan);
  586. show_fan(fan_min);
  587. show_fan(fan_max);
  588. show_fan(fan_target);
  589. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
  590. static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
  591. show_in_max, set_in_max, 0);
  592. static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
  593. show_in_min, set_in_min, 0);
  594. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
  595. static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
  596. show_in_max, set_in_max, 1);
  597. static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
  598. show_in_min, set_in_min, 1);
  599. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
  600. static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
  601. show_in_max, set_in_max, 2);
  602. static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
  603. show_in_min, set_in_min, 2);
  604. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
  605. static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
  606. show_in_max, set_in_max, 3);
  607. static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
  608. show_in_min, set_in_min, 3);
  609. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 0);
  610. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
  611. show_temp_max_hyst, set_temp_max_hyst, 0);
  612. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
  613. show_temp_max, set_temp_max, 0);
  614. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 1);
  615. static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
  616. show_temp_max_hyst, set_temp_max_hyst, 1);
  617. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
  618. show_temp_max, set_temp_max, 1);
  619. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  620. static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, 0);
  621. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
  622. show_fan_min, set_fan_min, 0);
  623. static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO|S_IWUSR,
  624. show_fan_target, set_fan_target, 0);
  625. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  626. static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max, NULL, 1);
  627. static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
  628. show_fan_min, set_fan_min, 1);
  629. static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO|S_IWUSR,
  630. show_fan_target, set_fan_target, 1);
  631. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
  632. show_pwm, set_pwm, 0);
  633. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
  634. show_pwm_enable, set_pwm_enable, 0);
  635. static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
  636. show_pwm_mode, set_pwm_mode, 0);
  637. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
  638. show_pwm, set_pwm, 1);
  639. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
  640. show_pwm_enable, set_pwm_enable, 1);
  641. static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
  642. show_pwm_mode, set_pwm_mode, 1);
  643. static struct attribute *f75375_attributes[] = {
  644. &sensor_dev_attr_temp1_input.dev_attr.attr,
  645. &sensor_dev_attr_temp1_max.dev_attr.attr,
  646. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  647. &sensor_dev_attr_temp2_input.dev_attr.attr,
  648. &sensor_dev_attr_temp2_max.dev_attr.attr,
  649. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  650. &sensor_dev_attr_fan1_input.dev_attr.attr,
  651. &sensor_dev_attr_fan1_max.dev_attr.attr,
  652. &sensor_dev_attr_fan1_min.dev_attr.attr,
  653. &sensor_dev_attr_fan1_target.dev_attr.attr,
  654. &sensor_dev_attr_fan2_input.dev_attr.attr,
  655. &sensor_dev_attr_fan2_max.dev_attr.attr,
  656. &sensor_dev_attr_fan2_min.dev_attr.attr,
  657. &sensor_dev_attr_fan2_target.dev_attr.attr,
  658. &sensor_dev_attr_pwm1.dev_attr.attr,
  659. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  660. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  661. &sensor_dev_attr_pwm2.dev_attr.attr,
  662. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  663. &sensor_dev_attr_pwm2_mode.dev_attr.attr,
  664. &sensor_dev_attr_in0_input.dev_attr.attr,
  665. &sensor_dev_attr_in0_max.dev_attr.attr,
  666. &sensor_dev_attr_in0_min.dev_attr.attr,
  667. &sensor_dev_attr_in1_input.dev_attr.attr,
  668. &sensor_dev_attr_in1_max.dev_attr.attr,
  669. &sensor_dev_attr_in1_min.dev_attr.attr,
  670. &sensor_dev_attr_in2_input.dev_attr.attr,
  671. &sensor_dev_attr_in2_max.dev_attr.attr,
  672. &sensor_dev_attr_in2_min.dev_attr.attr,
  673. &sensor_dev_attr_in3_input.dev_attr.attr,
  674. &sensor_dev_attr_in3_max.dev_attr.attr,
  675. &sensor_dev_attr_in3_min.dev_attr.attr,
  676. NULL
  677. };
  678. static const struct attribute_group f75375_group = {
  679. .attrs = f75375_attributes,
  680. };
  681. static void f75375_init(struct i2c_client *client, struct f75375_data *data,
  682. struct f75375s_platform_data *f75375s_pdata)
  683. {
  684. int nr;
  685. if (!f75375s_pdata) {
  686. u8 conf, mode;
  687. int nr;
  688. conf = f75375_read8(client, F75375_REG_CONFIG1);
  689. mode = f75375_read8(client, F75375_REG_FAN_TIMER);
  690. for (nr = 0; nr < 2; nr++) {
  691. if (data->kind == f75387) {
  692. bool manu, duty;
  693. if (!(mode & (1 << F75387_FAN_CTRL_LINEAR(nr))))
  694. data->pwm_mode[nr] = 1;
  695. manu = ((mode >> F75387_FAN_MANU_MODE(nr)) & 1);
  696. duty = ((mode >> F75387_FAN_DUTY_MODE(nr)) & 1);
  697. if (!manu && duty)
  698. /* auto, pwm */
  699. data->pwm_enable[nr] = 4;
  700. else if (manu && !duty)
  701. /* manual, speed */
  702. data->pwm_enable[nr] = 3;
  703. else if (!manu && !duty)
  704. /* automatic, speed */
  705. data->pwm_enable[nr] = 2;
  706. else
  707. /* manual, pwm */
  708. data->pwm_enable[nr] = 1;
  709. } else {
  710. if (!(conf & (1 << F75375_FAN_CTRL_LINEAR(nr))))
  711. data->pwm_mode[nr] = 1;
  712. switch ((mode >> FAN_CTRL_MODE(nr)) & 3) {
  713. case 0: /* speed */
  714. data->pwm_enable[nr] = 3;
  715. break;
  716. case 1: /* automatic */
  717. data->pwm_enable[nr] = 2;
  718. break;
  719. default: /* manual */
  720. data->pwm_enable[nr] = 1;
  721. break;
  722. }
  723. }
  724. }
  725. return;
  726. }
  727. set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
  728. set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
  729. for (nr = 0; nr < 2; nr++) {
  730. if (auto_mode_enabled(f75375s_pdata->pwm_enable[nr]) ||
  731. !duty_mode_enabled(f75375s_pdata->pwm_enable[nr]))
  732. continue;
  733. data->pwm[nr] = clamp_val(f75375s_pdata->pwm[nr], 0, 255);
  734. f75375_write_pwm(client, nr);
  735. }
  736. }
  737. static int f75375_probe(struct i2c_client *client,
  738. const struct i2c_device_id *id)
  739. {
  740. struct f75375_data *data;
  741. struct f75375s_platform_data *f75375s_pdata =
  742. dev_get_platdata(&client->dev);
  743. int err;
  744. if (!i2c_check_functionality(client->adapter,
  745. I2C_FUNC_SMBUS_BYTE_DATA))
  746. return -EIO;
  747. data = devm_kzalloc(&client->dev, sizeof(struct f75375_data),
  748. GFP_KERNEL);
  749. if (!data)
  750. return -ENOMEM;
  751. i2c_set_clientdata(client, data);
  752. mutex_init(&data->update_lock);
  753. data->kind = id->driver_data;
  754. err = sysfs_create_group(&client->dev.kobj, &f75375_group);
  755. if (err)
  756. return err;
  757. if (data->kind != f75373) {
  758. err = sysfs_chmod_file(&client->dev.kobj,
  759. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  760. S_IRUGO | S_IWUSR);
  761. if (err)
  762. goto exit_remove;
  763. err = sysfs_chmod_file(&client->dev.kobj,
  764. &sensor_dev_attr_pwm2_mode.dev_attr.attr,
  765. S_IRUGO | S_IWUSR);
  766. if (err)
  767. goto exit_remove;
  768. }
  769. data->hwmon_dev = hwmon_device_register(&client->dev);
  770. if (IS_ERR(data->hwmon_dev)) {
  771. err = PTR_ERR(data->hwmon_dev);
  772. goto exit_remove;
  773. }
  774. f75375_init(client, data, f75375s_pdata);
  775. return 0;
  776. exit_remove:
  777. sysfs_remove_group(&client->dev.kobj, &f75375_group);
  778. return err;
  779. }
  780. static int f75375_remove(struct i2c_client *client)
  781. {
  782. struct f75375_data *data = i2c_get_clientdata(client);
  783. hwmon_device_unregister(data->hwmon_dev);
  784. sysfs_remove_group(&client->dev.kobj, &f75375_group);
  785. return 0;
  786. }
  787. /* Return 0 if detection is successful, -ENODEV otherwise */
  788. static int f75375_detect(struct i2c_client *client,
  789. struct i2c_board_info *info)
  790. {
  791. struct i2c_adapter *adapter = client->adapter;
  792. u16 vendid, chipid;
  793. u8 version;
  794. const char *name;
  795. vendid = f75375_read16(client, F75375_REG_VENDOR);
  796. chipid = f75375_read16(client, F75375_CHIP_ID);
  797. if (vendid != 0x1934)
  798. return -ENODEV;
  799. if (chipid == 0x0306)
  800. name = "f75375";
  801. else if (chipid == 0x0204)
  802. name = "f75373";
  803. else if (chipid == 0x0410)
  804. name = "f75387";
  805. else
  806. return -ENODEV;
  807. version = f75375_read8(client, F75375_REG_VERSION);
  808. dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
  809. strlcpy(info->type, name, I2C_NAME_SIZE);
  810. return 0;
  811. }
  812. module_i2c_driver(f75375_driver);
  813. MODULE_AUTHOR("Riku Voipio");
  814. MODULE_LICENSE("GPL");
  815. MODULE_DESCRIPTION("F75373/F75375/F75387 hardware monitoring driver");