f75375s.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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/2005111152950.pdf
  10. *
  11. * f75373:
  12. * http://www.fintek.com.tw/files/productfiles/2005111153128.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. /* Addresses to scan */
  38. static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
  39. /* Insmod parameters */
  40. I2C_CLIENT_INSMOD_2(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) (5 + ((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, int kind,
  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_data = &addr_data,
  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. f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
  267. data->pwm[nr]);
  268. break;
  269. case 1: /* PWM */
  270. fanmode |= (3 << FAN_CTRL_MODE(nr));
  271. break;
  272. case 2: /* AUTOMATIC*/
  273. fanmode |= (2 << FAN_CTRL_MODE(nr));
  274. break;
  275. case 3: /* fan speed */
  276. break;
  277. }
  278. f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
  279. data->pwm_enable[nr] = val;
  280. return 0;
  281. }
  282. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
  283. const char *buf, size_t count)
  284. {
  285. int nr = to_sensor_dev_attr(attr)->index;
  286. struct i2c_client *client = to_i2c_client(dev);
  287. struct f75375_data *data = i2c_get_clientdata(client);
  288. int val = simple_strtoul(buf, NULL, 10);
  289. int err = 0;
  290. mutex_lock(&data->update_lock);
  291. err = set_pwm_enable_direct(client, nr, val);
  292. mutex_unlock(&data->update_lock);
  293. return err ? err : count;
  294. }
  295. static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
  296. const char *buf, size_t count)
  297. {
  298. int nr = to_sensor_dev_attr(attr)->index;
  299. struct i2c_client *client = to_i2c_client(dev);
  300. struct f75375_data *data = i2c_get_clientdata(client);
  301. int val = simple_strtoul(buf, NULL, 10);
  302. u8 conf = 0;
  303. if (!(val == 0 || val == 1))
  304. return -EINVAL;
  305. mutex_lock(&data->update_lock);
  306. conf = f75375_read8(client, F75375_REG_CONFIG1);
  307. conf = ~(1 << FAN_CTRL_LINEAR(nr));
  308. if (val == 0)
  309. conf |= (1 << FAN_CTRL_LINEAR(nr)) ;
  310. f75375_write8(client, F75375_REG_CONFIG1, conf);
  311. data->pwm_mode[nr] = val;
  312. mutex_unlock(&data->update_lock);
  313. return count;
  314. }
  315. static ssize_t show_pwm(struct device *dev, struct device_attribute
  316. *attr, char *buf)
  317. {
  318. int nr = to_sensor_dev_attr(attr)->index;
  319. struct f75375_data *data = f75375_update_device(dev);
  320. return sprintf(buf, "%d\n", data->pwm[nr]);
  321. }
  322. static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
  323. *attr, char *buf)
  324. {
  325. int nr = to_sensor_dev_attr(attr)->index;
  326. struct f75375_data *data = f75375_update_device(dev);
  327. return sprintf(buf, "%d\n", data->pwm_mode[nr]);
  328. }
  329. #define VOLT_FROM_REG(val) ((val) * 8)
  330. #define VOLT_TO_REG(val) ((val) / 8)
  331. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  332. char *buf)
  333. {
  334. int nr = to_sensor_dev_attr(attr)->index;
  335. struct f75375_data *data = f75375_update_device(dev);
  336. return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
  337. }
  338. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  339. char *buf)
  340. {
  341. int nr = to_sensor_dev_attr(attr)->index;
  342. struct f75375_data *data = f75375_update_device(dev);
  343. return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
  344. }
  345. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  346. char *buf)
  347. {
  348. int nr = to_sensor_dev_attr(attr)->index;
  349. struct f75375_data *data = f75375_update_device(dev);
  350. return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
  351. }
  352. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  353. const char *buf, size_t count)
  354. {
  355. int nr = to_sensor_dev_attr(attr)->index;
  356. struct i2c_client *client = to_i2c_client(dev);
  357. struct f75375_data *data = i2c_get_clientdata(client);
  358. int val = simple_strtoul(buf, NULL, 10);
  359. val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
  360. mutex_lock(&data->update_lock);
  361. data->in_max[nr] = val;
  362. f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
  363. mutex_unlock(&data->update_lock);
  364. return count;
  365. }
  366. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  367. const char *buf, size_t count)
  368. {
  369. int nr = to_sensor_dev_attr(attr)->index;
  370. struct i2c_client *client = to_i2c_client(dev);
  371. struct f75375_data *data = i2c_get_clientdata(client);
  372. int val = simple_strtoul(buf, NULL, 10);
  373. val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
  374. mutex_lock(&data->update_lock);
  375. data->in_min[nr] = val;
  376. f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
  377. mutex_unlock(&data->update_lock);
  378. return count;
  379. }
  380. #define TEMP_FROM_REG(val) ((val) * 1000)
  381. #define TEMP_TO_REG(val) ((val) / 1000)
  382. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  383. char *buf)
  384. {
  385. int nr = to_sensor_dev_attr(attr)->index;
  386. struct f75375_data *data = f75375_update_device(dev);
  387. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
  388. }
  389. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  390. char *buf)
  391. {
  392. int nr = to_sensor_dev_attr(attr)->index;
  393. struct f75375_data *data = f75375_update_device(dev);
  394. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
  395. }
  396. static ssize_t show_temp_max_hyst(struct device *dev,
  397. struct device_attribute *attr, char *buf)
  398. {
  399. int nr = to_sensor_dev_attr(attr)->index;
  400. struct f75375_data *data = f75375_update_device(dev);
  401. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
  402. }
  403. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  404. const char *buf, size_t count)
  405. {
  406. int nr = to_sensor_dev_attr(attr)->index;
  407. struct i2c_client *client = to_i2c_client(dev);
  408. struct f75375_data *data = i2c_get_clientdata(client);
  409. int val = simple_strtol(buf, NULL, 10);
  410. val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
  411. mutex_lock(&data->update_lock);
  412. data->temp_high[nr] = val;
  413. f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
  414. mutex_unlock(&data->update_lock);
  415. return count;
  416. }
  417. static ssize_t set_temp_max_hyst(struct device *dev,
  418. struct device_attribute *attr, const char *buf, size_t count)
  419. {
  420. int nr = to_sensor_dev_attr(attr)->index;
  421. struct i2c_client *client = to_i2c_client(dev);
  422. struct f75375_data *data = i2c_get_clientdata(client);
  423. int val = simple_strtol(buf, NULL, 10);
  424. val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
  425. mutex_lock(&data->update_lock);
  426. data->temp_max_hyst[nr] = val;
  427. f75375_write8(client, F75375_REG_TEMP_HYST(nr),
  428. data->temp_max_hyst[nr]);
  429. mutex_unlock(&data->update_lock);
  430. return count;
  431. }
  432. #define show_fan(thing) \
  433. static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
  434. char *buf)\
  435. {\
  436. int nr = to_sensor_dev_attr(attr)->index;\
  437. struct f75375_data *data = f75375_update_device(dev); \
  438. return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
  439. }
  440. show_fan(fan);
  441. show_fan(fan_min);
  442. show_fan(fan_full);
  443. show_fan(fan_exp);
  444. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
  445. static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
  446. show_in_max, set_in_max, 0);
  447. static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
  448. show_in_min, set_in_min, 0);
  449. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
  450. static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
  451. show_in_max, set_in_max, 1);
  452. static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
  453. show_in_min, set_in_min, 1);
  454. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
  455. static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
  456. show_in_max, set_in_max, 2);
  457. static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
  458. show_in_min, set_in_min, 2);
  459. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
  460. static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
  461. show_in_max, set_in_max, 3);
  462. static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
  463. show_in_min, set_in_min, 3);
  464. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  465. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
  466. show_temp_max_hyst, set_temp_max_hyst, 0);
  467. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
  468. show_temp_max, set_temp_max, 0);
  469. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  470. static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
  471. show_temp_max_hyst, set_temp_max_hyst, 1);
  472. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
  473. show_temp_max, set_temp_max, 1);
  474. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  475. static SENSOR_DEVICE_ATTR(fan1_full, S_IRUGO, show_fan_full, NULL, 0);
  476. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
  477. show_fan_min, set_fan_min, 0);
  478. static SENSOR_DEVICE_ATTR(fan1_exp, S_IRUGO|S_IWUSR,
  479. show_fan_exp, set_fan_exp, 0);
  480. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  481. static SENSOR_DEVICE_ATTR(fan2_full, S_IRUGO, show_fan_full, NULL, 1);
  482. static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
  483. show_fan_min, set_fan_min, 1);
  484. static SENSOR_DEVICE_ATTR(fan2_exp, S_IRUGO|S_IWUSR,
  485. show_fan_exp, set_fan_exp, 1);
  486. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
  487. show_pwm, set_pwm, 0);
  488. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
  489. show_pwm_enable, set_pwm_enable, 0);
  490. static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
  491. show_pwm_mode, set_pwm_mode, 0);
  492. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
  493. show_pwm, set_pwm, 1);
  494. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
  495. show_pwm_enable, set_pwm_enable, 1);
  496. static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
  497. show_pwm_mode, set_pwm_mode, 1);
  498. static struct attribute *f75375_attributes[] = {
  499. &sensor_dev_attr_temp1_input.dev_attr.attr,
  500. &sensor_dev_attr_temp1_max.dev_attr.attr,
  501. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  502. &sensor_dev_attr_temp2_input.dev_attr.attr,
  503. &sensor_dev_attr_temp2_max.dev_attr.attr,
  504. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  505. &sensor_dev_attr_fan1_input.dev_attr.attr,
  506. &sensor_dev_attr_fan1_full.dev_attr.attr,
  507. &sensor_dev_attr_fan1_min.dev_attr.attr,
  508. &sensor_dev_attr_fan1_exp.dev_attr.attr,
  509. &sensor_dev_attr_fan2_input.dev_attr.attr,
  510. &sensor_dev_attr_fan2_full.dev_attr.attr,
  511. &sensor_dev_attr_fan2_min.dev_attr.attr,
  512. &sensor_dev_attr_fan2_exp.dev_attr.attr,
  513. &sensor_dev_attr_pwm1.dev_attr.attr,
  514. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  515. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  516. &sensor_dev_attr_pwm2.dev_attr.attr,
  517. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  518. &sensor_dev_attr_pwm2_mode.dev_attr.attr,
  519. &sensor_dev_attr_in0_input.dev_attr.attr,
  520. &sensor_dev_attr_in0_max.dev_attr.attr,
  521. &sensor_dev_attr_in0_min.dev_attr.attr,
  522. &sensor_dev_attr_in1_input.dev_attr.attr,
  523. &sensor_dev_attr_in1_max.dev_attr.attr,
  524. &sensor_dev_attr_in1_min.dev_attr.attr,
  525. &sensor_dev_attr_in2_input.dev_attr.attr,
  526. &sensor_dev_attr_in2_max.dev_attr.attr,
  527. &sensor_dev_attr_in2_min.dev_attr.attr,
  528. &sensor_dev_attr_in3_input.dev_attr.attr,
  529. &sensor_dev_attr_in3_max.dev_attr.attr,
  530. &sensor_dev_attr_in3_min.dev_attr.attr,
  531. NULL
  532. };
  533. static const struct attribute_group f75375_group = {
  534. .attrs = f75375_attributes,
  535. };
  536. static void f75375_init(struct i2c_client *client, struct f75375_data *data,
  537. struct f75375s_platform_data *f75375s_pdata)
  538. {
  539. int nr;
  540. set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
  541. set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
  542. for (nr = 0; nr < 2; nr++) {
  543. data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255);
  544. f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
  545. data->pwm[nr]);
  546. }
  547. }
  548. static int f75375_probe(struct i2c_client *client,
  549. const struct i2c_device_id *id)
  550. {
  551. struct f75375_data *data;
  552. struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data;
  553. int err;
  554. if (!i2c_check_functionality(client->adapter,
  555. I2C_FUNC_SMBUS_BYTE_DATA))
  556. return -EIO;
  557. if (!(data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL)))
  558. return -ENOMEM;
  559. i2c_set_clientdata(client, data);
  560. mutex_init(&data->update_lock);
  561. data->kind = id->driver_data;
  562. if ((err = sysfs_create_group(&client->dev.kobj, &f75375_group)))
  563. goto exit_free;
  564. if (data->kind == f75375) {
  565. err = sysfs_chmod_file(&client->dev.kobj,
  566. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  567. S_IRUGO | S_IWUSR);
  568. if (err)
  569. goto exit_remove;
  570. err = sysfs_chmod_file(&client->dev.kobj,
  571. &sensor_dev_attr_pwm2_mode.dev_attr.attr,
  572. S_IRUGO | S_IWUSR);
  573. if (err)
  574. goto exit_remove;
  575. }
  576. data->hwmon_dev = hwmon_device_register(&client->dev);
  577. if (IS_ERR(data->hwmon_dev)) {
  578. err = PTR_ERR(data->hwmon_dev);
  579. goto exit_remove;
  580. }
  581. if (f75375s_pdata != NULL)
  582. f75375_init(client, data, f75375s_pdata);
  583. return 0;
  584. exit_remove:
  585. sysfs_remove_group(&client->dev.kobj, &f75375_group);
  586. exit_free:
  587. kfree(data);
  588. i2c_set_clientdata(client, NULL);
  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. i2c_set_clientdata(client, NULL);
  598. return 0;
  599. }
  600. /* Return 0 if detection is successful, -ENODEV otherwise */
  601. static int f75375_detect(struct i2c_client *client, int kind,
  602. struct i2c_board_info *info)
  603. {
  604. struct i2c_adapter *adapter = client->adapter;
  605. u8 version = 0;
  606. const char *name = "";
  607. if (kind < 0) {
  608. u16 vendid = f75375_read16(client, F75375_REG_VENDOR);
  609. u16 chipid = f75375_read16(client, F75375_CHIP_ID);
  610. version = f75375_read8(client, F75375_REG_VERSION);
  611. if (chipid == 0x0306 && vendid == 0x1934) {
  612. kind = f75375;
  613. } else if (chipid == 0x0204 && vendid == 0x1934) {
  614. kind = f75373;
  615. } else {
  616. dev_err(&adapter->dev,
  617. "failed,%02X,%02X,%02X\n",
  618. chipid, version, vendid);
  619. return -ENODEV;
  620. }
  621. }
  622. if (kind == f75375) {
  623. name = "f75375";
  624. } else if (kind == f75373) {
  625. name = "f75373";
  626. }
  627. dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
  628. strlcpy(info->type, name, I2C_NAME_SIZE);
  629. return 0;
  630. }
  631. static int __init sensors_f75375_init(void)
  632. {
  633. return i2c_add_driver(&f75375_driver);
  634. }
  635. static void __exit sensors_f75375_exit(void)
  636. {
  637. i2c_del_driver(&f75375_driver);
  638. }
  639. MODULE_AUTHOR("Riku Voipio");
  640. MODULE_LICENSE("GPL");
  641. MODULE_DESCRIPTION("F75373/F75375 hardware monitoring driver");
  642. module_init(sensors_f75375_init);
  643. module_exit(sensors_f75375_exit);