f75375s.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * f75375s.c - driver for the Fintek F75375/SP and F75373
  3. * 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. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/hwmon.h>
  32. #include <linux/hwmon-sysfs.h>
  33. #include <linux/i2c.h>
  34. #include <linux/err.h>
  35. #include <linux/mutex.h>
  36. #include <linux/f75375s.h>
  37. #include <linux/slab.h>
  38. /* Addresses to scan */
  39. static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
  40. enum chips { f75373, f75375 };
  41. /* Fintek F75375 registers */
  42. #define F75375_REG_CONFIG0 0x0
  43. #define F75375_REG_CONFIG1 0x1
  44. #define F75375_REG_CONFIG2 0x2
  45. #define F75375_REG_CONFIG3 0x3
  46. #define F75375_REG_ADDR 0x4
  47. #define F75375_REG_INTR 0x31
  48. #define F75375_CHIP_ID 0x5A
  49. #define F75375_REG_VERSION 0x5C
  50. #define F75375_REG_VENDOR 0x5D
  51. #define F75375_REG_FAN_TIMER 0x60
  52. #define F75375_REG_VOLT(nr) (0x10 + (nr))
  53. #define F75375_REG_VOLT_HIGH(nr) (0x20 + (nr) * 2)
  54. #define F75375_REG_VOLT_LOW(nr) (0x21 + (nr) * 2)
  55. #define F75375_REG_TEMP(nr) (0x14 + (nr))
  56. #define F75375_REG_TEMP_HIGH(nr) (0x28 + (nr) * 2)
  57. #define F75375_REG_TEMP_HYST(nr) (0x29 + (nr) * 2)
  58. #define F75375_REG_FAN(nr) (0x16 + (nr) * 2)
  59. #define F75375_REG_FAN_MIN(nr) (0x2C + (nr) * 2)
  60. #define F75375_REG_FAN_FULL(nr) (0x70 + (nr) * 0x10)
  61. #define F75375_REG_FAN_PWM_DUTY(nr) (0x76 + (nr) * 0x10)
  62. #define F75375_REG_FAN_PWM_CLOCK(nr) (0x7D + (nr) * 0x10)
  63. #define F75375_REG_FAN_EXP(nr) (0x74 + (nr) * 0x10)
  64. #define F75375_REG_FAN_B_TEMP(nr, step) ((0xA0 + (nr) * 0x10) + (step))
  65. #define F75375_REG_FAN_B_SPEED(nr, step) \
  66. ((0xA5 + (nr) * 0x10) + (step) * 2)
  67. #define F75375_REG_PWM1_RAISE_DUTY 0x69
  68. #define F75375_REG_PWM2_RAISE_DUTY 0x6A
  69. #define F75375_REG_PWM1_DROP_DUTY 0x6B
  70. #define F75375_REG_PWM2_DROP_DUTY 0x6C
  71. #define FAN_CTRL_LINEAR(nr) (4 + nr)
  72. #define FAN_CTRL_MODE(nr) (4 + ((nr) * 2))
  73. /*
  74. * Data structures and manipulation thereof
  75. */
  76. struct f75375_data {
  77. unsigned short addr;
  78. struct device *hwmon_dev;
  79. const char *name;
  80. int kind;
  81. struct mutex update_lock; /* protect register access */
  82. char valid;
  83. unsigned long last_updated; /* In jiffies */
  84. unsigned long last_limits; /* In jiffies */
  85. /* Register values */
  86. u8 in[4];
  87. u8 in_max[4];
  88. u8 in_min[4];
  89. u16 fan[2];
  90. u16 fan_min[2];
  91. u16 fan_full[2];
  92. u16 fan_exp[2];
  93. u8 fan_timer;
  94. u8 pwm[2];
  95. u8 pwm_mode[2];
  96. u8 pwm_enable[2];
  97. s8 temp[2];
  98. s8 temp_high[2];
  99. s8 temp_max_hyst[2];
  100. };
  101. static int f75375_detect(struct i2c_client *client,
  102. struct i2c_board_info *info);
  103. static int f75375_probe(struct i2c_client *client,
  104. const struct i2c_device_id *id);
  105. static int f75375_remove(struct i2c_client *client);
  106. static const struct i2c_device_id f75375_id[] = {
  107. { "f75373", f75373 },
  108. { "f75375", f75375 },
  109. { }
  110. };
  111. MODULE_DEVICE_TABLE(i2c, f75375_id);
  112. static struct i2c_driver f75375_driver = {
  113. .class = I2C_CLASS_HWMON,
  114. .driver = {
  115. .name = "f75375",
  116. },
  117. .probe = f75375_probe,
  118. .remove = f75375_remove,
  119. .id_table = f75375_id,
  120. .detect = f75375_detect,
  121. .address_list = normal_i2c,
  122. };
  123. static inline int f75375_read8(struct i2c_client *client, u8 reg)
  124. {
  125. return i2c_smbus_read_byte_data(client, reg);
  126. }
  127. /* in most cases, should be called while holding update_lock */
  128. static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
  129. {
  130. return ((i2c_smbus_read_byte_data(client, reg) << 8)
  131. | i2c_smbus_read_byte_data(client, reg + 1));
  132. }
  133. static inline void f75375_write8(struct i2c_client *client, u8 reg,
  134. u8 value)
  135. {
  136. i2c_smbus_write_byte_data(client, reg, value);
  137. }
  138. static inline void f75375_write16(struct i2c_client *client, u8 reg,
  139. u16 value)
  140. {
  141. int err = i2c_smbus_write_byte_data(client, reg, (value >> 8));
  142. if (err)
  143. return;
  144. i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
  145. }
  146. static struct f75375_data *f75375_update_device(struct device *dev)
  147. {
  148. struct i2c_client *client = to_i2c_client(dev);
  149. struct f75375_data *data = i2c_get_clientdata(client);
  150. int nr;
  151. mutex_lock(&data->update_lock);
  152. /* Limit registers cache is refreshed after 60 seconds */
  153. if (time_after(jiffies, data->last_limits + 60 * HZ)
  154. || !data->valid) {
  155. for (nr = 0; nr < 2; nr++) {
  156. data->temp_high[nr] =
  157. f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
  158. data->temp_max_hyst[nr] =
  159. f75375_read8(client, F75375_REG_TEMP_HYST(nr));
  160. data->fan_full[nr] =
  161. f75375_read16(client, F75375_REG_FAN_FULL(nr));
  162. data->fan_min[nr] =
  163. f75375_read16(client, F75375_REG_FAN_MIN(nr));
  164. data->fan_exp[nr] =
  165. f75375_read16(client, F75375_REG_FAN_EXP(nr));
  166. data->pwm[nr] = f75375_read8(client,
  167. F75375_REG_FAN_PWM_DUTY(nr));
  168. }
  169. for (nr = 0; nr < 4; nr++) {
  170. data->in_max[nr] =
  171. f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
  172. data->in_min[nr] =
  173. f75375_read8(client, F75375_REG_VOLT_LOW(nr));
  174. }
  175. data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
  176. data->last_limits = jiffies;
  177. }
  178. /* Measurement registers cache is refreshed after 2 second */
  179. if (time_after(jiffies, data->last_updated + 2 * HZ)
  180. || !data->valid) {
  181. for (nr = 0; nr < 2; nr++) {
  182. data->temp[nr] =
  183. f75375_read8(client, F75375_REG_TEMP(nr));
  184. data->fan[nr] =
  185. f75375_read16(client, F75375_REG_FAN(nr));
  186. }
  187. for (nr = 0; nr < 4; nr++)
  188. data->in[nr] =
  189. f75375_read8(client, F75375_REG_VOLT(nr));
  190. data->last_updated = jiffies;
  191. data->valid = 1;
  192. }
  193. mutex_unlock(&data->update_lock);
  194. return data;
  195. }
  196. static inline u16 rpm_from_reg(u16 reg)
  197. {
  198. if (reg == 0 || reg == 0xffff)
  199. return 0;
  200. return (1500000 / reg);
  201. }
  202. static inline u16 rpm_to_reg(int rpm)
  203. {
  204. if (rpm < 367 || rpm > 0xffff)
  205. return 0xffff;
  206. return (1500000 / rpm);
  207. }
  208. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  209. const char *buf, size_t count)
  210. {
  211. int nr = to_sensor_dev_attr(attr)->index;
  212. struct i2c_client *client = to_i2c_client(dev);
  213. struct f75375_data *data = i2c_get_clientdata(client);
  214. int val = simple_strtoul(buf, NULL, 10);
  215. mutex_lock(&data->update_lock);
  216. data->fan_min[nr] = rpm_to_reg(val);
  217. f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
  218. mutex_unlock(&data->update_lock);
  219. return count;
  220. }
  221. static ssize_t set_fan_exp(struct device *dev, struct device_attribute *attr,
  222. const char *buf, size_t count)
  223. {
  224. int nr = to_sensor_dev_attr(attr)->index;
  225. struct i2c_client *client = to_i2c_client(dev);
  226. struct f75375_data *data = i2c_get_clientdata(client);
  227. int val = simple_strtoul(buf, NULL, 10);
  228. mutex_lock(&data->update_lock);
  229. data->fan_exp[nr] = rpm_to_reg(val);
  230. f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_exp[nr]);
  231. mutex_unlock(&data->update_lock);
  232. return count;
  233. }
  234. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  235. const char *buf, size_t count)
  236. {
  237. int nr = to_sensor_dev_attr(attr)->index;
  238. struct i2c_client *client = to_i2c_client(dev);
  239. struct f75375_data *data = i2c_get_clientdata(client);
  240. int val = simple_strtoul(buf, NULL, 10);
  241. mutex_lock(&data->update_lock);
  242. data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
  243. f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), data->pwm[nr]);
  244. mutex_unlock(&data->update_lock);
  245. return count;
  246. }
  247. static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
  248. *attr, char *buf)
  249. {
  250. int nr = to_sensor_dev_attr(attr)->index;
  251. struct f75375_data *data = f75375_update_device(dev);
  252. return sprintf(buf, "%d\n", data->pwm_enable[nr]);
  253. }
  254. static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
  255. {
  256. struct f75375_data *data = i2c_get_clientdata(client);
  257. u8 fanmode;
  258. if (val < 0 || val > 4)
  259. return -EINVAL;
  260. fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
  261. fanmode &= ~(3 << FAN_CTRL_MODE(nr));
  262. switch (val) {
  263. case 0: /* Full speed */
  264. fanmode |= (3 << FAN_CTRL_MODE(nr));
  265. data->pwm[nr] = 255;
  266. break;
  267. case 1: /* PWM */
  268. fanmode |= (3 << FAN_CTRL_MODE(nr));
  269. break;
  270. case 2: /* AUTOMATIC*/
  271. fanmode |= (1 << FAN_CTRL_MODE(nr));
  272. break;
  273. case 3: /* fan speed */
  274. break;
  275. }
  276. f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
  277. data->pwm_enable[nr] = val;
  278. if (val == 0)
  279. f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
  280. data->pwm[nr]);
  281. return 0;
  282. }
  283. static ssize_t set_pwm_enable(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. int val = simple_strtoul(buf, NULL, 10);
  290. int err = 0;
  291. mutex_lock(&data->update_lock);
  292. err = set_pwm_enable_direct(client, nr, val);
  293. mutex_unlock(&data->update_lock);
  294. return err ? err : count;
  295. }
  296. static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
  297. const char *buf, size_t count)
  298. {
  299. int nr = to_sensor_dev_attr(attr)->index;
  300. struct i2c_client *client = to_i2c_client(dev);
  301. struct f75375_data *data = i2c_get_clientdata(client);
  302. int val = simple_strtoul(buf, NULL, 10);
  303. u8 conf = 0;
  304. if (!(val == 0 || val == 1))
  305. return -EINVAL;
  306. mutex_lock(&data->update_lock);
  307. conf = f75375_read8(client, F75375_REG_CONFIG1);
  308. conf &= ~(1 << FAN_CTRL_LINEAR(nr));
  309. if (val == 0)
  310. conf |= (1 << FAN_CTRL_LINEAR(nr)) ;
  311. f75375_write8(client, F75375_REG_CONFIG1, conf);
  312. data->pwm_mode[nr] = val;
  313. mutex_unlock(&data->update_lock);
  314. return count;
  315. }
  316. static ssize_t show_pwm(struct device *dev, struct device_attribute
  317. *attr, char *buf)
  318. {
  319. int nr = to_sensor_dev_attr(attr)->index;
  320. struct f75375_data *data = f75375_update_device(dev);
  321. return sprintf(buf, "%d\n", data->pwm[nr]);
  322. }
  323. static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
  324. *attr, char *buf)
  325. {
  326. int nr = to_sensor_dev_attr(attr)->index;
  327. struct f75375_data *data = f75375_update_device(dev);
  328. return sprintf(buf, "%d\n", data->pwm_mode[nr]);
  329. }
  330. #define VOLT_FROM_REG(val) ((val) * 8)
  331. #define VOLT_TO_REG(val) ((val) / 8)
  332. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  333. char *buf)
  334. {
  335. int nr = to_sensor_dev_attr(attr)->index;
  336. struct f75375_data *data = f75375_update_device(dev);
  337. return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
  338. }
  339. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  340. char *buf)
  341. {
  342. int nr = to_sensor_dev_attr(attr)->index;
  343. struct f75375_data *data = f75375_update_device(dev);
  344. return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
  345. }
  346. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  347. char *buf)
  348. {
  349. int nr = to_sensor_dev_attr(attr)->index;
  350. struct f75375_data *data = f75375_update_device(dev);
  351. return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
  352. }
  353. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  354. const char *buf, size_t count)
  355. {
  356. int nr = to_sensor_dev_attr(attr)->index;
  357. struct i2c_client *client = to_i2c_client(dev);
  358. struct f75375_data *data = i2c_get_clientdata(client);
  359. int val = simple_strtoul(buf, NULL, 10);
  360. val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
  361. mutex_lock(&data->update_lock);
  362. data->in_max[nr] = val;
  363. f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
  364. mutex_unlock(&data->update_lock);
  365. return count;
  366. }
  367. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  368. const char *buf, size_t count)
  369. {
  370. int nr = to_sensor_dev_attr(attr)->index;
  371. struct i2c_client *client = to_i2c_client(dev);
  372. struct f75375_data *data = i2c_get_clientdata(client);
  373. int val = simple_strtoul(buf, NULL, 10);
  374. val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
  375. mutex_lock(&data->update_lock);
  376. data->in_min[nr] = val;
  377. f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
  378. mutex_unlock(&data->update_lock);
  379. return count;
  380. }
  381. #define TEMP_FROM_REG(val) ((val) * 1000)
  382. #define TEMP_TO_REG(val) ((val) / 1000)
  383. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  384. char *buf)
  385. {
  386. int nr = to_sensor_dev_attr(attr)->index;
  387. struct f75375_data *data = f75375_update_device(dev);
  388. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
  389. }
  390. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  391. char *buf)
  392. {
  393. int nr = to_sensor_dev_attr(attr)->index;
  394. struct f75375_data *data = f75375_update_device(dev);
  395. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
  396. }
  397. static ssize_t show_temp_max_hyst(struct device *dev,
  398. struct device_attribute *attr, char *buf)
  399. {
  400. int nr = to_sensor_dev_attr(attr)->index;
  401. struct f75375_data *data = f75375_update_device(dev);
  402. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
  403. }
  404. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  405. const char *buf, size_t count)
  406. {
  407. int nr = to_sensor_dev_attr(attr)->index;
  408. struct i2c_client *client = to_i2c_client(dev);
  409. struct f75375_data *data = i2c_get_clientdata(client);
  410. int val = simple_strtol(buf, NULL, 10);
  411. val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
  412. mutex_lock(&data->update_lock);
  413. data->temp_high[nr] = val;
  414. f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
  415. mutex_unlock(&data->update_lock);
  416. return count;
  417. }
  418. static ssize_t set_temp_max_hyst(struct device *dev,
  419. struct device_attribute *attr, const char *buf, size_t count)
  420. {
  421. int nr = to_sensor_dev_attr(attr)->index;
  422. struct i2c_client *client = to_i2c_client(dev);
  423. struct f75375_data *data = i2c_get_clientdata(client);
  424. int val = simple_strtol(buf, NULL, 10);
  425. val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
  426. mutex_lock(&data->update_lock);
  427. data->temp_max_hyst[nr] = val;
  428. f75375_write8(client, F75375_REG_TEMP_HYST(nr),
  429. data->temp_max_hyst[nr]);
  430. mutex_unlock(&data->update_lock);
  431. return count;
  432. }
  433. #define show_fan(thing) \
  434. static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
  435. char *buf)\
  436. {\
  437. int nr = to_sensor_dev_attr(attr)->index;\
  438. struct f75375_data *data = f75375_update_device(dev); \
  439. return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
  440. }
  441. show_fan(fan);
  442. show_fan(fan_min);
  443. show_fan(fan_full);
  444. show_fan(fan_exp);
  445. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
  446. static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
  447. show_in_max, set_in_max, 0);
  448. static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
  449. show_in_min, set_in_min, 0);
  450. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
  451. static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
  452. show_in_max, set_in_max, 1);
  453. static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
  454. show_in_min, set_in_min, 1);
  455. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
  456. static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
  457. show_in_max, set_in_max, 2);
  458. static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
  459. show_in_min, set_in_min, 2);
  460. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
  461. static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
  462. show_in_max, set_in_max, 3);
  463. static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
  464. show_in_min, set_in_min, 3);
  465. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  466. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
  467. show_temp_max_hyst, set_temp_max_hyst, 0);
  468. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
  469. show_temp_max, set_temp_max, 0);
  470. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  471. static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
  472. show_temp_max_hyst, set_temp_max_hyst, 1);
  473. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
  474. show_temp_max, set_temp_max, 1);
  475. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  476. static SENSOR_DEVICE_ATTR(fan1_full, S_IRUGO, show_fan_full, NULL, 0);
  477. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
  478. show_fan_min, set_fan_min, 0);
  479. static SENSOR_DEVICE_ATTR(fan1_exp, S_IRUGO|S_IWUSR,
  480. show_fan_exp, set_fan_exp, 0);
  481. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  482. static SENSOR_DEVICE_ATTR(fan2_full, S_IRUGO, show_fan_full, NULL, 1);
  483. static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
  484. show_fan_min, set_fan_min, 1);
  485. static SENSOR_DEVICE_ATTR(fan2_exp, S_IRUGO|S_IWUSR,
  486. show_fan_exp, set_fan_exp, 1);
  487. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
  488. show_pwm, set_pwm, 0);
  489. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
  490. show_pwm_enable, set_pwm_enable, 0);
  491. static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
  492. show_pwm_mode, set_pwm_mode, 0);
  493. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
  494. show_pwm, set_pwm, 1);
  495. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
  496. show_pwm_enable, set_pwm_enable, 1);
  497. static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
  498. show_pwm_mode, set_pwm_mode, 1);
  499. static struct attribute *f75375_attributes[] = {
  500. &sensor_dev_attr_temp1_input.dev_attr.attr,
  501. &sensor_dev_attr_temp1_max.dev_attr.attr,
  502. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  503. &sensor_dev_attr_temp2_input.dev_attr.attr,
  504. &sensor_dev_attr_temp2_max.dev_attr.attr,
  505. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  506. &sensor_dev_attr_fan1_input.dev_attr.attr,
  507. &sensor_dev_attr_fan1_full.dev_attr.attr,
  508. &sensor_dev_attr_fan1_min.dev_attr.attr,
  509. &sensor_dev_attr_fan1_exp.dev_attr.attr,
  510. &sensor_dev_attr_fan2_input.dev_attr.attr,
  511. &sensor_dev_attr_fan2_full.dev_attr.attr,
  512. &sensor_dev_attr_fan2_min.dev_attr.attr,
  513. &sensor_dev_attr_fan2_exp.dev_attr.attr,
  514. &sensor_dev_attr_pwm1.dev_attr.attr,
  515. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  516. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  517. &sensor_dev_attr_pwm2.dev_attr.attr,
  518. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  519. &sensor_dev_attr_pwm2_mode.dev_attr.attr,
  520. &sensor_dev_attr_in0_input.dev_attr.attr,
  521. &sensor_dev_attr_in0_max.dev_attr.attr,
  522. &sensor_dev_attr_in0_min.dev_attr.attr,
  523. &sensor_dev_attr_in1_input.dev_attr.attr,
  524. &sensor_dev_attr_in1_max.dev_attr.attr,
  525. &sensor_dev_attr_in1_min.dev_attr.attr,
  526. &sensor_dev_attr_in2_input.dev_attr.attr,
  527. &sensor_dev_attr_in2_max.dev_attr.attr,
  528. &sensor_dev_attr_in2_min.dev_attr.attr,
  529. &sensor_dev_attr_in3_input.dev_attr.attr,
  530. &sensor_dev_attr_in3_max.dev_attr.attr,
  531. &sensor_dev_attr_in3_min.dev_attr.attr,
  532. NULL
  533. };
  534. static const struct attribute_group f75375_group = {
  535. .attrs = f75375_attributes,
  536. };
  537. static void f75375_init(struct i2c_client *client, struct f75375_data *data,
  538. struct f75375s_platform_data *f75375s_pdata)
  539. {
  540. int nr;
  541. set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
  542. set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
  543. for (nr = 0; nr < 2; nr++) {
  544. data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255);
  545. f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
  546. data->pwm[nr]);
  547. }
  548. }
  549. static int f75375_probe(struct i2c_client *client,
  550. const struct i2c_device_id *id)
  551. {
  552. struct f75375_data *data;
  553. struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data;
  554. int err;
  555. if (!i2c_check_functionality(client->adapter,
  556. I2C_FUNC_SMBUS_BYTE_DATA))
  557. return -EIO;
  558. if (!(data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL)))
  559. return -ENOMEM;
  560. i2c_set_clientdata(client, data);
  561. mutex_init(&data->update_lock);
  562. data->kind = id->driver_data;
  563. if ((err = sysfs_create_group(&client->dev.kobj, &f75375_group)))
  564. goto exit_free;
  565. if (data->kind == f75375) {
  566. err = sysfs_chmod_file(&client->dev.kobj,
  567. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  568. S_IRUGO | S_IWUSR);
  569. if (err)
  570. goto exit_remove;
  571. err = sysfs_chmod_file(&client->dev.kobj,
  572. &sensor_dev_attr_pwm2_mode.dev_attr.attr,
  573. S_IRUGO | S_IWUSR);
  574. if (err)
  575. goto exit_remove;
  576. }
  577. data->hwmon_dev = hwmon_device_register(&client->dev);
  578. if (IS_ERR(data->hwmon_dev)) {
  579. err = PTR_ERR(data->hwmon_dev);
  580. goto exit_remove;
  581. }
  582. if (f75375s_pdata != NULL)
  583. f75375_init(client, data, f75375s_pdata);
  584. return 0;
  585. exit_remove:
  586. sysfs_remove_group(&client->dev.kobj, &f75375_group);
  587. exit_free:
  588. kfree(data);
  589. return err;
  590. }
  591. static int f75375_remove(struct i2c_client *client)
  592. {
  593. struct f75375_data *data = i2c_get_clientdata(client);
  594. hwmon_device_unregister(data->hwmon_dev);
  595. sysfs_remove_group(&client->dev.kobj, &f75375_group);
  596. kfree(data);
  597. return 0;
  598. }
  599. /* Return 0 if detection is successful, -ENODEV otherwise */
  600. static int f75375_detect(struct i2c_client *client,
  601. struct i2c_board_info *info)
  602. {
  603. struct i2c_adapter *adapter = client->adapter;
  604. u16 vendid, chipid;
  605. u8 version;
  606. const char *name;
  607. vendid = f75375_read16(client, F75375_REG_VENDOR);
  608. chipid = f75375_read16(client, F75375_CHIP_ID);
  609. if (chipid == 0x0306 && vendid == 0x1934)
  610. name = "f75375";
  611. else if (chipid == 0x0204 && vendid == 0x1934)
  612. name = "f75373";
  613. else
  614. return -ENODEV;
  615. version = f75375_read8(client, F75375_REG_VERSION);
  616. dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
  617. strlcpy(info->type, name, I2C_NAME_SIZE);
  618. return 0;
  619. }
  620. static int __init sensors_f75375_init(void)
  621. {
  622. return i2c_add_driver(&f75375_driver);
  623. }
  624. static void __exit sensors_f75375_exit(void)
  625. {
  626. i2c_del_driver(&f75375_driver);
  627. }
  628. MODULE_AUTHOR("Riku Voipio");
  629. MODULE_LICENSE("GPL");
  630. MODULE_DESCRIPTION("F75373/F75375 hardware monitoring driver");
  631. module_init(sensors_f75375_init);
  632. module_exit(sensors_f75375_exit);