fscpos.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. fscpos.c - Kernel module for hardware monitoring with FSC Poseidon chips
  3. Copyright (C) 2004, 2005 Stefan Ott <stefan@desire.ch>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. /*
  17. fujitsu siemens poseidon chip,
  18. module based on the old fscpos module by Hermann Jung <hej@odn.de> and
  19. the fscher module by Reinhard Nissl <rnissl@gmx.de>
  20. original module based on lm80.c
  21. Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
  22. and Philip Edelbrock <phil@netroedge.com>
  23. Thanks to Jean Delvare for reviewing my code and suggesting a lot of
  24. improvements.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/i2c.h>
  30. #include <linux/init.h>
  31. #include <linux/hwmon.h>
  32. #include <linux/err.h>
  33. /*
  34. * Addresses to scan
  35. */
  36. static unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
  37. /*
  38. * Insmod parameters
  39. */
  40. I2C_CLIENT_INSMOD_1(fscpos);
  41. /*
  42. * The FSCPOS registers
  43. */
  44. /* chip identification */
  45. #define FSCPOS_REG_IDENT_0 0x00
  46. #define FSCPOS_REG_IDENT_1 0x01
  47. #define FSCPOS_REG_IDENT_2 0x02
  48. #define FSCPOS_REG_REVISION 0x03
  49. /* global control and status */
  50. #define FSCPOS_REG_EVENT_STATE 0x04
  51. #define FSCPOS_REG_CONTROL 0x05
  52. /* watchdog */
  53. #define FSCPOS_REG_WDOG_PRESET 0x28
  54. #define FSCPOS_REG_WDOG_STATE 0x23
  55. #define FSCPOS_REG_WDOG_CONTROL 0x21
  56. /* voltages */
  57. #define FSCPOS_REG_VOLT_12 0x45
  58. #define FSCPOS_REG_VOLT_5 0x42
  59. #define FSCPOS_REG_VOLT_BATT 0x48
  60. /* fans - the chip does not support minimum speed for fan2 */
  61. static u8 FSCPOS_REG_PWM[] = { 0x55, 0x65 };
  62. static u8 FSCPOS_REG_FAN_ACT[] = { 0x0e, 0x6b, 0xab };
  63. static u8 FSCPOS_REG_FAN_STATE[] = { 0x0d, 0x62, 0xa2 };
  64. static u8 FSCPOS_REG_FAN_RIPPLE[] = { 0x0f, 0x6f, 0xaf };
  65. /* temperatures */
  66. static u8 FSCPOS_REG_TEMP_ACT[] = { 0x64, 0x32, 0x35 };
  67. static u8 FSCPOS_REG_TEMP_STATE[] = { 0x71, 0x81, 0x91 };
  68. /*
  69. * Functions declaration
  70. */
  71. static int fscpos_attach_adapter(struct i2c_adapter *adapter);
  72. static int fscpos_detect(struct i2c_adapter *adapter, int address, int kind);
  73. static int fscpos_detach_client(struct i2c_client *client);
  74. static int fscpos_read_value(struct i2c_client *client, u8 register);
  75. static int fscpos_write_value(struct i2c_client *client, u8 register, u8 value);
  76. static struct fscpos_data *fscpos_update_device(struct device *dev);
  77. static void fscpos_init_client(struct i2c_client *client);
  78. static void reset_fan_alarm(struct i2c_client *client, int nr);
  79. /*
  80. * Driver data (common to all clients)
  81. */
  82. static struct i2c_driver fscpos_driver = {
  83. .owner = THIS_MODULE,
  84. .name = "fscpos",
  85. .id = I2C_DRIVERID_FSCPOS,
  86. .flags = I2C_DF_NOTIFY,
  87. .attach_adapter = fscpos_attach_adapter,
  88. .detach_client = fscpos_detach_client,
  89. };
  90. /*
  91. * Client data (each client gets its own)
  92. */
  93. struct fscpos_data {
  94. struct i2c_client client;
  95. struct class_device *class_dev;
  96. struct semaphore update_lock;
  97. char valid; /* 0 until following fields are valid */
  98. unsigned long last_updated; /* In jiffies */
  99. /* register values */
  100. u8 revision; /* revision of chip */
  101. u8 global_event; /* global event status */
  102. u8 global_control; /* global control register */
  103. u8 wdog_control; /* watchdog control */
  104. u8 wdog_state; /* watchdog status */
  105. u8 wdog_preset; /* watchdog preset */
  106. u8 volt[3]; /* 12, 5, battery current */
  107. u8 temp_act[3]; /* temperature */
  108. u8 temp_status[3]; /* status of sensor */
  109. u8 fan_act[3]; /* fans revolutions per second */
  110. u8 fan_status[3]; /* fan status */
  111. u8 pwm[2]; /* fan min value for rps */
  112. u8 fan_ripple[3]; /* divider for rps */
  113. };
  114. /* Temperature */
  115. #define TEMP_FROM_REG(val) (((val) - 128) * 1000)
  116. static ssize_t show_temp_input(struct fscpos_data *data, char *buf, int nr)
  117. {
  118. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[nr - 1]));
  119. }
  120. static ssize_t show_temp_status(struct fscpos_data *data, char *buf, int nr)
  121. {
  122. /* bits 2..7 reserved => mask with 0x03 */
  123. return sprintf(buf, "%u\n", data->temp_status[nr - 1] & 0x03);
  124. }
  125. static ssize_t show_temp_reset(struct fscpos_data *data, char *buf, int nr)
  126. {
  127. return sprintf(buf, "1\n");
  128. }
  129. static ssize_t set_temp_reset(struct i2c_client *client, struct fscpos_data
  130. *data, const char *buf, size_t count, int nr, int reg)
  131. {
  132. unsigned long v = simple_strtoul(buf, NULL, 10);
  133. if (v != 1) {
  134. dev_err(&client->dev, "temp_reset value %ld not supported. "
  135. "Use 1 to reset the alarm!\n", v);
  136. return -EINVAL;
  137. }
  138. dev_info(&client->dev, "You used the temp_reset feature which has not "
  139. "been proplerly tested. Please report your "
  140. "experience to the module author.\n");
  141. /* Supported value: 2 (clears the status) */
  142. fscpos_write_value(client, FSCPOS_REG_TEMP_STATE[nr - 1], 2);
  143. return count;
  144. }
  145. /* Fans */
  146. #define RPM_FROM_REG(val) ((val) * 60)
  147. static ssize_t show_fan_status(struct fscpos_data *data, char *buf, int nr)
  148. {
  149. /* bits 0..1, 3..7 reserved => mask with 0x04 */
  150. return sprintf(buf, "%u\n", data->fan_status[nr - 1] & 0x04);
  151. }
  152. static ssize_t show_fan_input(struct fscpos_data *data, char *buf, int nr)
  153. {
  154. return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[nr - 1]));
  155. }
  156. static ssize_t show_fan_ripple(struct fscpos_data *data, char *buf, int nr)
  157. {
  158. /* bits 2..7 reserved => mask with 0x03 */
  159. return sprintf(buf, "%u\n", data->fan_ripple[nr - 1] & 0x03);
  160. }
  161. static ssize_t set_fan_ripple(struct i2c_client *client, struct fscpos_data
  162. *data, const char *buf, size_t count, int nr, int reg)
  163. {
  164. /* supported values: 2, 4, 8 */
  165. unsigned long v = simple_strtoul(buf, NULL, 10);
  166. switch (v) {
  167. case 2: v = 1; break;
  168. case 4: v = 2; break;
  169. case 8: v = 3; break;
  170. default:
  171. dev_err(&client->dev, "fan_ripple value %ld not supported. "
  172. "Must be one of 2, 4 or 8!\n", v);
  173. return -EINVAL;
  174. }
  175. down(&data->update_lock);
  176. /* bits 2..7 reserved => mask with 0x03 */
  177. data->fan_ripple[nr - 1] &= ~0x03;
  178. data->fan_ripple[nr - 1] |= v;
  179. fscpos_write_value(client, reg, data->fan_ripple[nr - 1]);
  180. up(&data->update_lock);
  181. return count;
  182. }
  183. static ssize_t show_pwm(struct fscpos_data *data, char *buf, int nr)
  184. {
  185. return sprintf(buf, "%u\n", data->pwm[nr - 1]);
  186. }
  187. static ssize_t set_pwm(struct i2c_client *client, struct fscpos_data *data,
  188. const char *buf, size_t count, int nr, int reg)
  189. {
  190. unsigned long v = simple_strtoul(buf, NULL, 10);
  191. /* Range: 0..255 */
  192. if (v < 0) v = 0;
  193. if (v > 255) v = 255;
  194. down(&data->update_lock);
  195. data->pwm[nr - 1] = v;
  196. fscpos_write_value(client, reg, data->pwm[nr - 1]);
  197. up(&data->update_lock);
  198. return count;
  199. }
  200. static void reset_fan_alarm(struct i2c_client *client, int nr)
  201. {
  202. fscpos_write_value(client, FSCPOS_REG_FAN_STATE[nr], 4);
  203. }
  204. /* Volts */
  205. #define VOLT_FROM_REG(val, mult) ((val) * (mult) / 255)
  206. static ssize_t show_volt_12(struct device *dev, struct device_attribute *attr, char *buf)
  207. {
  208. struct fscpos_data *data = fscpos_update_device(dev);
  209. return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[0], 14200));
  210. }
  211. static ssize_t show_volt_5(struct device *dev, struct device_attribute *attr, char *buf)
  212. {
  213. struct fscpos_data *data = fscpos_update_device(dev);
  214. return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[1], 6600));
  215. }
  216. static ssize_t show_volt_batt(struct device *dev, struct device_attribute *attr, char *buf)
  217. {
  218. struct fscpos_data *data = fscpos_update_device(dev);
  219. return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[2], 3300));
  220. }
  221. /* Watchdog */
  222. static ssize_t show_wdog_control(struct fscpos_data *data, char *buf)
  223. {
  224. /* bits 0..3 reserved, bit 6 write only => mask with 0xb0 */
  225. return sprintf(buf, "%u\n", data->wdog_control & 0xb0);
  226. }
  227. static ssize_t set_wdog_control(struct i2c_client *client, struct fscpos_data
  228. *data, const char *buf, size_t count, int reg)
  229. {
  230. /* bits 0..3 reserved => mask with 0xf0 */
  231. unsigned long v = simple_strtoul(buf, NULL, 10) & 0xf0;
  232. down(&data->update_lock);
  233. data->wdog_control &= ~0xf0;
  234. data->wdog_control |= v;
  235. fscpos_write_value(client, reg, data->wdog_control);
  236. up(&data->update_lock);
  237. return count;
  238. }
  239. static ssize_t show_wdog_state(struct fscpos_data *data, char *buf)
  240. {
  241. /* bits 0, 2..7 reserved => mask with 0x02 */
  242. return sprintf(buf, "%u\n", data->wdog_state & 0x02);
  243. }
  244. static ssize_t set_wdog_state(struct i2c_client *client, struct fscpos_data
  245. *data, const char *buf, size_t count, int reg)
  246. {
  247. unsigned long v = simple_strtoul(buf, NULL, 10) & 0x02;
  248. /* Valid values: 2 (clear) */
  249. if (v != 2) {
  250. dev_err(&client->dev, "wdog_state value %ld not supported. "
  251. "Must be 2 to clear the state!\n", v);
  252. return -EINVAL;
  253. }
  254. down(&data->update_lock);
  255. data->wdog_state &= ~v;
  256. fscpos_write_value(client, reg, v);
  257. up(&data->update_lock);
  258. return count;
  259. }
  260. static ssize_t show_wdog_preset(struct fscpos_data *data, char *buf)
  261. {
  262. return sprintf(buf, "%u\n", data->wdog_preset);
  263. }
  264. static ssize_t set_wdog_preset(struct i2c_client *client, struct fscpos_data
  265. *data, const char *buf, size_t count, int reg)
  266. {
  267. unsigned long v = simple_strtoul(buf, NULL, 10) & 0xff;
  268. down(&data->update_lock);
  269. data->wdog_preset = v;
  270. fscpos_write_value(client, reg, data->wdog_preset);
  271. up(&data->update_lock);
  272. return count;
  273. }
  274. /* Event */
  275. static ssize_t show_event(struct device *dev, struct device_attribute *attr, char *buf)
  276. {
  277. /* bits 5..7 reserved => mask with 0x1f */
  278. struct fscpos_data *data = fscpos_update_device(dev);
  279. return sprintf(buf, "%u\n", data->global_event & 0x9b);
  280. }
  281. /*
  282. * Sysfs stuff
  283. */
  284. #define create_getter(kind, sub) \
  285. static ssize_t sysfs_show_##kind##sub(struct device *dev, struct device_attribute *attr, char *buf) \
  286. { \
  287. struct fscpos_data *data = fscpos_update_device(dev); \
  288. return show_##kind##sub(data, buf); \
  289. }
  290. #define create_getter_n(kind, offset, sub) \
  291. static ssize_t sysfs_show_##kind##offset##sub(struct device *dev, struct device_attribute *attr, char\
  292. *buf) \
  293. { \
  294. struct fscpos_data *data = fscpos_update_device(dev); \
  295. return show_##kind##sub(data, buf, offset); \
  296. }
  297. #define create_setter(kind, sub, reg) \
  298. static ssize_t sysfs_set_##kind##sub (struct device *dev, struct device_attribute *attr, const char \
  299. *buf, size_t count) \
  300. { \
  301. struct i2c_client *client = to_i2c_client(dev); \
  302. struct fscpos_data *data = i2c_get_clientdata(client); \
  303. return set_##kind##sub(client, data, buf, count, reg); \
  304. }
  305. #define create_setter_n(kind, offset, sub, reg) \
  306. static ssize_t sysfs_set_##kind##offset##sub (struct device *dev, struct device_attribute *attr, \
  307. const char *buf, size_t count) \
  308. { \
  309. struct i2c_client *client = to_i2c_client(dev); \
  310. struct fscpos_data *data = i2c_get_clientdata(client); \
  311. return set_##kind##sub(client, data, buf, count, offset, reg);\
  312. }
  313. #define create_sysfs_device_ro(kind, sub, offset) \
  314. static DEVICE_ATTR(kind##offset##sub, S_IRUGO, \
  315. sysfs_show_##kind##offset##sub, NULL);
  316. #define create_sysfs_device_rw(kind, sub, offset) \
  317. static DEVICE_ATTR(kind##offset##sub, S_IRUGO | S_IWUSR, \
  318. sysfs_show_##kind##offset##sub, sysfs_set_##kind##offset##sub);
  319. #define sysfs_ro_n(kind, sub, offset) \
  320. create_getter_n(kind, offset, sub); \
  321. create_sysfs_device_ro(kind, sub, offset);
  322. #define sysfs_rw_n(kind, sub, offset, reg) \
  323. create_getter_n(kind, offset, sub); \
  324. create_setter_n(kind, offset, sub, reg); \
  325. create_sysfs_device_rw(kind, sub, offset);
  326. #define sysfs_rw(kind, sub, reg) \
  327. create_getter(kind, sub); \
  328. create_setter(kind, sub, reg); \
  329. create_sysfs_device_rw(kind, sub,);
  330. #define sysfs_fan_with_min(offset, reg_status, reg_ripple, reg_min) \
  331. sysfs_fan(offset, reg_status, reg_ripple); \
  332. sysfs_rw_n(pwm,, offset, reg_min);
  333. #define sysfs_fan(offset, reg_status, reg_ripple) \
  334. sysfs_ro_n(fan, _input, offset); \
  335. sysfs_ro_n(fan, _status, offset); \
  336. sysfs_rw_n(fan, _ripple, offset, reg_ripple);
  337. #define sysfs_temp(offset, reg_status) \
  338. sysfs_ro_n(temp, _input, offset); \
  339. sysfs_ro_n(temp, _status, offset); \
  340. sysfs_rw_n(temp, _reset, offset, reg_status);
  341. #define sysfs_watchdog(reg_wdog_preset, reg_wdog_state, reg_wdog_control) \
  342. sysfs_rw(wdog, _control, reg_wdog_control); \
  343. sysfs_rw(wdog, _preset, reg_wdog_preset); \
  344. sysfs_rw(wdog, _state, reg_wdog_state);
  345. sysfs_fan_with_min(1, FSCPOS_REG_FAN_STATE[0], FSCPOS_REG_FAN_RIPPLE[0],
  346. FSCPOS_REG_PWM[0]);
  347. sysfs_fan_with_min(2, FSCPOS_REG_FAN_STATE[1], FSCPOS_REG_FAN_RIPPLE[1],
  348. FSCPOS_REG_PWM[1]);
  349. sysfs_fan(3, FSCPOS_REG_FAN_STATE[2], FSCPOS_REG_FAN_RIPPLE[2]);
  350. sysfs_temp(1, FSCPOS_REG_TEMP_STATE[0]);
  351. sysfs_temp(2, FSCPOS_REG_TEMP_STATE[1]);
  352. sysfs_temp(3, FSCPOS_REG_TEMP_STATE[2]);
  353. sysfs_watchdog(FSCPOS_REG_WDOG_PRESET, FSCPOS_REG_WDOG_STATE,
  354. FSCPOS_REG_WDOG_CONTROL);
  355. static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
  356. static DEVICE_ATTR(in0_input, S_IRUGO, show_volt_12, NULL);
  357. static DEVICE_ATTR(in1_input, S_IRUGO, show_volt_5, NULL);
  358. static DEVICE_ATTR(in2_input, S_IRUGO, show_volt_batt, NULL);
  359. static int fscpos_attach_adapter(struct i2c_adapter *adapter)
  360. {
  361. if (!(adapter->class & I2C_CLASS_HWMON))
  362. return 0;
  363. return i2c_probe(adapter, &addr_data, fscpos_detect);
  364. }
  365. int fscpos_detect(struct i2c_adapter *adapter, int address, int kind)
  366. {
  367. struct i2c_client *new_client;
  368. struct fscpos_data *data;
  369. int err = 0;
  370. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  371. goto exit;
  372. /*
  373. * OK. For now, we presume we have a valid client. We now create the
  374. * client structure, even though we cannot fill it completely yet.
  375. * But it allows us to access fscpos_{read,write}_value.
  376. */
  377. if (!(data = kmalloc(sizeof(struct fscpos_data), GFP_KERNEL))) {
  378. err = -ENOMEM;
  379. goto exit;
  380. }
  381. memset(data, 0, sizeof(struct fscpos_data));
  382. new_client = &data->client;
  383. i2c_set_clientdata(new_client, data);
  384. new_client->addr = address;
  385. new_client->adapter = adapter;
  386. new_client->driver = &fscpos_driver;
  387. new_client->flags = 0;
  388. /* Do the remaining detection unless force or force_fscpos parameter */
  389. if (kind < 0) {
  390. if ((fscpos_read_value(new_client, FSCPOS_REG_IDENT_0)
  391. != 0x50) /* 'P' */
  392. || (fscpos_read_value(new_client, FSCPOS_REG_IDENT_1)
  393. != 0x45) /* 'E' */
  394. || (fscpos_read_value(new_client, FSCPOS_REG_IDENT_2)
  395. != 0x47))/* 'G' */
  396. {
  397. dev_dbg(&new_client->dev, "fscpos detection failed\n");
  398. goto exit_free;
  399. }
  400. }
  401. /* Fill in the remaining client fields and put it in the global list */
  402. strlcpy(new_client->name, "fscpos", I2C_NAME_SIZE);
  403. data->valid = 0;
  404. init_MUTEX(&data->update_lock);
  405. /* Tell the I2C layer a new client has arrived */
  406. if ((err = i2c_attach_client(new_client)))
  407. goto exit_free;
  408. /* Inizialize the fscpos chip */
  409. fscpos_init_client(new_client);
  410. /* Announce that the chip was found */
  411. dev_info(&new_client->dev, "Found fscpos chip, rev %u\n", data->revision);
  412. /* Register sysfs hooks */
  413. data->class_dev = hwmon_device_register(&new_client->dev);
  414. if (IS_ERR(data->class_dev)) {
  415. err = PTR_ERR(data->class_dev);
  416. goto exit_detach;
  417. }
  418. device_create_file(&new_client->dev, &dev_attr_event);
  419. device_create_file(&new_client->dev, &dev_attr_in0_input);
  420. device_create_file(&new_client->dev, &dev_attr_in1_input);
  421. device_create_file(&new_client->dev, &dev_attr_in2_input);
  422. device_create_file(&new_client->dev, &dev_attr_wdog_control);
  423. device_create_file(&new_client->dev, &dev_attr_wdog_preset);
  424. device_create_file(&new_client->dev, &dev_attr_wdog_state);
  425. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  426. device_create_file(&new_client->dev, &dev_attr_temp1_status);
  427. device_create_file(&new_client->dev, &dev_attr_temp1_reset);
  428. device_create_file(&new_client->dev, &dev_attr_temp2_input);
  429. device_create_file(&new_client->dev, &dev_attr_temp2_status);
  430. device_create_file(&new_client->dev, &dev_attr_temp2_reset);
  431. device_create_file(&new_client->dev, &dev_attr_temp3_input);
  432. device_create_file(&new_client->dev, &dev_attr_temp3_status);
  433. device_create_file(&new_client->dev, &dev_attr_temp3_reset);
  434. device_create_file(&new_client->dev, &dev_attr_fan1_input);
  435. device_create_file(&new_client->dev, &dev_attr_fan1_status);
  436. device_create_file(&new_client->dev, &dev_attr_fan1_ripple);
  437. device_create_file(&new_client->dev, &dev_attr_pwm1);
  438. device_create_file(&new_client->dev, &dev_attr_fan2_input);
  439. device_create_file(&new_client->dev, &dev_attr_fan2_status);
  440. device_create_file(&new_client->dev, &dev_attr_fan2_ripple);
  441. device_create_file(&new_client->dev, &dev_attr_pwm2);
  442. device_create_file(&new_client->dev, &dev_attr_fan3_input);
  443. device_create_file(&new_client->dev, &dev_attr_fan3_status);
  444. device_create_file(&new_client->dev, &dev_attr_fan3_ripple);
  445. return 0;
  446. exit_detach:
  447. i2c_detach_client(new_client);
  448. exit_free:
  449. kfree(data);
  450. exit:
  451. return err;
  452. }
  453. static int fscpos_detach_client(struct i2c_client *client)
  454. {
  455. struct fscpos_data *data = i2c_get_clientdata(client);
  456. int err;
  457. hwmon_device_unregister(data->class_dev);
  458. if ((err = i2c_detach_client(client)))
  459. return err;
  460. kfree(data);
  461. return 0;
  462. }
  463. static int fscpos_read_value(struct i2c_client *client, u8 reg)
  464. {
  465. dev_dbg(&client->dev, "Read reg 0x%02x\n", reg);
  466. return i2c_smbus_read_byte_data(client, reg);
  467. }
  468. static int fscpos_write_value(struct i2c_client *client, u8 reg, u8 value)
  469. {
  470. dev_dbg(&client->dev, "Write reg 0x%02x, val 0x%02x\n", reg, value);
  471. return i2c_smbus_write_byte_data(client, reg, value);
  472. }
  473. /* Called when we have found a new FSCPOS chip */
  474. static void fscpos_init_client(struct i2c_client *client)
  475. {
  476. struct fscpos_data *data = i2c_get_clientdata(client);
  477. /* read revision from chip */
  478. data->revision = fscpos_read_value(client, FSCPOS_REG_REVISION);
  479. }
  480. static struct fscpos_data *fscpos_update_device(struct device *dev)
  481. {
  482. struct i2c_client *client = to_i2c_client(dev);
  483. struct fscpos_data *data = i2c_get_clientdata(client);
  484. down(&data->update_lock);
  485. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  486. int i;
  487. dev_dbg(&client->dev, "Starting fscpos update\n");
  488. for (i = 0; i < 3; i++) {
  489. data->temp_act[i] = fscpos_read_value(client,
  490. FSCPOS_REG_TEMP_ACT[i]);
  491. data->temp_status[i] = fscpos_read_value(client,
  492. FSCPOS_REG_TEMP_STATE[i]);
  493. data->fan_act[i] = fscpos_read_value(client,
  494. FSCPOS_REG_FAN_ACT[i]);
  495. data->fan_status[i] = fscpos_read_value(client,
  496. FSCPOS_REG_FAN_STATE[i]);
  497. data->fan_ripple[i] = fscpos_read_value(client,
  498. FSCPOS_REG_FAN_RIPPLE[i]);
  499. if (i < 2) {
  500. /* fan2_min is not supported by the chip */
  501. data->pwm[i] = fscpos_read_value(client,
  502. FSCPOS_REG_PWM[i]);
  503. }
  504. /* reset fan status if speed is back to > 0 */
  505. if (data->fan_status[i] != 0 && data->fan_act[i] > 0) {
  506. reset_fan_alarm(client, i);
  507. }
  508. }
  509. data->volt[0] = fscpos_read_value(client, FSCPOS_REG_VOLT_12);
  510. data->volt[1] = fscpos_read_value(client, FSCPOS_REG_VOLT_5);
  511. data->volt[2] = fscpos_read_value(client, FSCPOS_REG_VOLT_BATT);
  512. data->wdog_preset = fscpos_read_value(client,
  513. FSCPOS_REG_WDOG_PRESET);
  514. data->wdog_state = fscpos_read_value(client,
  515. FSCPOS_REG_WDOG_STATE);
  516. data->wdog_control = fscpos_read_value(client,
  517. FSCPOS_REG_WDOG_CONTROL);
  518. data->global_event = fscpos_read_value(client,
  519. FSCPOS_REG_EVENT_STATE);
  520. data->last_updated = jiffies;
  521. data->valid = 1;
  522. }
  523. up(&data->update_lock);
  524. return data;
  525. }
  526. static int __init sm_fscpos_init(void)
  527. {
  528. return i2c_add_driver(&fscpos_driver);
  529. }
  530. static void __exit sm_fscpos_exit(void)
  531. {
  532. i2c_del_driver(&fscpos_driver);
  533. }
  534. MODULE_AUTHOR("Stefan Ott <stefan@desire.ch> based on work from Hermann Jung "
  535. "<hej@odn.de>, Frodo Looijaard <frodol@dds.nl>"
  536. " and Philip Edelbrock <phil@netroedge.com>");
  537. MODULE_DESCRIPTION("fujitsu siemens poseidon chip driver");
  538. MODULE_LICENSE("GPL");
  539. module_init(sm_fscpos_init);
  540. module_exit(sm_fscpos_exit);