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. #include <linux/mutex.h>
  34. /*
  35. * Addresses to scan
  36. */
  37. static unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
  38. /*
  39. * Insmod parameters
  40. */
  41. I2C_CLIENT_INSMOD_1(fscpos);
  42. /*
  43. * The FSCPOS registers
  44. */
  45. /* chip identification */
  46. #define FSCPOS_REG_IDENT_0 0x00
  47. #define FSCPOS_REG_IDENT_1 0x01
  48. #define FSCPOS_REG_IDENT_2 0x02
  49. #define FSCPOS_REG_REVISION 0x03
  50. /* global control and status */
  51. #define FSCPOS_REG_EVENT_STATE 0x04
  52. #define FSCPOS_REG_CONTROL 0x05
  53. /* watchdog */
  54. #define FSCPOS_REG_WDOG_PRESET 0x28
  55. #define FSCPOS_REG_WDOG_STATE 0x23
  56. #define FSCPOS_REG_WDOG_CONTROL 0x21
  57. /* voltages */
  58. #define FSCPOS_REG_VOLT_12 0x45
  59. #define FSCPOS_REG_VOLT_5 0x42
  60. #define FSCPOS_REG_VOLT_BATT 0x48
  61. /* fans - the chip does not support minimum speed for fan2 */
  62. static u8 FSCPOS_REG_PWM[] = { 0x55, 0x65 };
  63. static u8 FSCPOS_REG_FAN_ACT[] = { 0x0e, 0x6b, 0xab };
  64. static u8 FSCPOS_REG_FAN_STATE[] = { 0x0d, 0x62, 0xa2 };
  65. static u8 FSCPOS_REG_FAN_RIPPLE[] = { 0x0f, 0x6f, 0xaf };
  66. /* temperatures */
  67. static u8 FSCPOS_REG_TEMP_ACT[] = { 0x64, 0x32, 0x35 };
  68. static u8 FSCPOS_REG_TEMP_STATE[] = { 0x71, 0x81, 0x91 };
  69. /*
  70. * Functions declaration
  71. */
  72. static int fscpos_attach_adapter(struct i2c_adapter *adapter);
  73. static int fscpos_detect(struct i2c_adapter *adapter, int address, int kind);
  74. static int fscpos_detach_client(struct i2c_client *client);
  75. static int fscpos_read_value(struct i2c_client *client, u8 reg);
  76. static int fscpos_write_value(struct i2c_client *client, u8 reg, u8 value);
  77. static struct fscpos_data *fscpos_update_device(struct device *dev);
  78. static void fscpos_init_client(struct i2c_client *client);
  79. static void reset_fan_alarm(struct i2c_client *client, int nr);
  80. /*
  81. * Driver data (common to all clients)
  82. */
  83. static struct i2c_driver fscpos_driver = {
  84. .driver = {
  85. .name = "fscpos",
  86. },
  87. .id = I2C_DRIVERID_FSCPOS,
  88. .attach_adapter = fscpos_attach_adapter,
  89. .detach_client = fscpos_detach_client,
  90. };
  91. /*
  92. * Client data (each client gets its own)
  93. */
  94. struct fscpos_data {
  95. struct i2c_client client;
  96. struct class_device *class_dev;
  97. struct mutex update_lock;
  98. char valid; /* 0 until following fields are valid */
  99. unsigned long last_updated; /* In jiffies */
  100. /* register values */
  101. u8 revision; /* revision of chip */
  102. u8 global_event; /* global event status */
  103. u8 global_control; /* global control register */
  104. u8 wdog_control; /* watchdog control */
  105. u8 wdog_state; /* watchdog status */
  106. u8 wdog_preset; /* watchdog preset */
  107. u8 volt[3]; /* 12, 5, battery current */
  108. u8 temp_act[3]; /* temperature */
  109. u8 temp_status[3]; /* status of sensor */
  110. u8 fan_act[3]; /* fans revolutions per second */
  111. u8 fan_status[3]; /* fan status */
  112. u8 pwm[2]; /* fan min value for rps */
  113. u8 fan_ripple[3]; /* divider for rps */
  114. };
  115. /* Temperature */
  116. #define TEMP_FROM_REG(val) (((val) - 128) * 1000)
  117. static ssize_t show_temp_input(struct fscpos_data *data, char *buf, int nr)
  118. {
  119. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[nr - 1]));
  120. }
  121. static ssize_t show_temp_status(struct fscpos_data *data, char *buf, int nr)
  122. {
  123. /* bits 2..7 reserved => mask with 0x03 */
  124. return sprintf(buf, "%u\n", data->temp_status[nr - 1] & 0x03);
  125. }
  126. static ssize_t show_temp_reset(struct fscpos_data *data, char *buf, int nr)
  127. {
  128. return sprintf(buf, "1\n");
  129. }
  130. static ssize_t set_temp_reset(struct i2c_client *client, struct fscpos_data
  131. *data, const char *buf, size_t count, int nr, int reg)
  132. {
  133. unsigned long v = simple_strtoul(buf, NULL, 10);
  134. if (v != 1) {
  135. dev_err(&client->dev, "temp_reset value %ld not supported. "
  136. "Use 1 to reset the alarm!\n", v);
  137. return -EINVAL;
  138. }
  139. dev_info(&client->dev, "You used the temp_reset feature which has not "
  140. "been proplerly tested. Please report your "
  141. "experience to the module author.\n");
  142. /* Supported value: 2 (clears the status) */
  143. fscpos_write_value(client, FSCPOS_REG_TEMP_STATE[nr - 1], 2);
  144. return count;
  145. }
  146. /* Fans */
  147. #define RPM_FROM_REG(val) ((val) * 60)
  148. static ssize_t show_fan_status(struct fscpos_data *data, char *buf, int nr)
  149. {
  150. /* bits 0..1, 3..7 reserved => mask with 0x04 */
  151. return sprintf(buf, "%u\n", data->fan_status[nr - 1] & 0x04);
  152. }
  153. static ssize_t show_fan_input(struct fscpos_data *data, char *buf, int nr)
  154. {
  155. return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[nr - 1]));
  156. }
  157. static ssize_t show_fan_ripple(struct fscpos_data *data, char *buf, int nr)
  158. {
  159. /* bits 2..7 reserved => mask with 0x03 */
  160. return sprintf(buf, "%u\n", data->fan_ripple[nr - 1] & 0x03);
  161. }
  162. static ssize_t set_fan_ripple(struct i2c_client *client, struct fscpos_data
  163. *data, const char *buf, size_t count, int nr, int reg)
  164. {
  165. /* supported values: 2, 4, 8 */
  166. unsigned long v = simple_strtoul(buf, NULL, 10);
  167. switch (v) {
  168. case 2: v = 1; break;
  169. case 4: v = 2; break;
  170. case 8: v = 3; break;
  171. default:
  172. dev_err(&client->dev, "fan_ripple value %ld not supported. "
  173. "Must be one of 2, 4 or 8!\n", v);
  174. return -EINVAL;
  175. }
  176. mutex_lock(&data->update_lock);
  177. /* bits 2..7 reserved => mask with 0x03 */
  178. data->fan_ripple[nr - 1] &= ~0x03;
  179. data->fan_ripple[nr - 1] |= v;
  180. fscpos_write_value(client, reg, data->fan_ripple[nr - 1]);
  181. mutex_unlock(&data->update_lock);
  182. return count;
  183. }
  184. static ssize_t show_pwm(struct fscpos_data *data, char *buf, int nr)
  185. {
  186. return sprintf(buf, "%u\n", data->pwm[nr - 1]);
  187. }
  188. static ssize_t set_pwm(struct i2c_client *client, struct fscpos_data *data,
  189. const char *buf, size_t count, int nr, int reg)
  190. {
  191. unsigned long v = simple_strtoul(buf, NULL, 10);
  192. /* Range: 0..255 */
  193. if (v < 0) v = 0;
  194. if (v > 255) v = 255;
  195. mutex_lock(&data->update_lock);
  196. data->pwm[nr - 1] = v;
  197. fscpos_write_value(client, reg, data->pwm[nr - 1]);
  198. mutex_unlock(&data->update_lock);
  199. return count;
  200. }
  201. static void reset_fan_alarm(struct i2c_client *client, int nr)
  202. {
  203. fscpos_write_value(client, FSCPOS_REG_FAN_STATE[nr], 4);
  204. }
  205. /* Volts */
  206. #define VOLT_FROM_REG(val, mult) ((val) * (mult) / 255)
  207. static ssize_t show_volt_12(struct device *dev, struct device_attribute *attr, char *buf)
  208. {
  209. struct fscpos_data *data = fscpos_update_device(dev);
  210. return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[0], 14200));
  211. }
  212. static ssize_t show_volt_5(struct device *dev, struct device_attribute *attr, char *buf)
  213. {
  214. struct fscpos_data *data = fscpos_update_device(dev);
  215. return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[1], 6600));
  216. }
  217. static ssize_t show_volt_batt(struct device *dev, struct device_attribute *attr, char *buf)
  218. {
  219. struct fscpos_data *data = fscpos_update_device(dev);
  220. return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[2], 3300));
  221. }
  222. /* Watchdog */
  223. static ssize_t show_wdog_control(struct fscpos_data *data, char *buf)
  224. {
  225. /* bits 0..3 reserved, bit 6 write only => mask with 0xb0 */
  226. return sprintf(buf, "%u\n", data->wdog_control & 0xb0);
  227. }
  228. static ssize_t set_wdog_control(struct i2c_client *client, struct fscpos_data
  229. *data, const char *buf, size_t count, int reg)
  230. {
  231. /* bits 0..3 reserved => mask with 0xf0 */
  232. unsigned long v = simple_strtoul(buf, NULL, 10) & 0xf0;
  233. mutex_lock(&data->update_lock);
  234. data->wdog_control &= ~0xf0;
  235. data->wdog_control |= v;
  236. fscpos_write_value(client, reg, data->wdog_control);
  237. mutex_unlock(&data->update_lock);
  238. return count;
  239. }
  240. static ssize_t show_wdog_state(struct fscpos_data *data, char *buf)
  241. {
  242. /* bits 0, 2..7 reserved => mask with 0x02 */
  243. return sprintf(buf, "%u\n", data->wdog_state & 0x02);
  244. }
  245. static ssize_t set_wdog_state(struct i2c_client *client, struct fscpos_data
  246. *data, const char *buf, size_t count, int reg)
  247. {
  248. unsigned long v = simple_strtoul(buf, NULL, 10) & 0x02;
  249. /* Valid values: 2 (clear) */
  250. if (v != 2) {
  251. dev_err(&client->dev, "wdog_state value %ld not supported. "
  252. "Must be 2 to clear the state!\n", v);
  253. return -EINVAL;
  254. }
  255. mutex_lock(&data->update_lock);
  256. data->wdog_state &= ~v;
  257. fscpos_write_value(client, reg, v);
  258. mutex_unlock(&data->update_lock);
  259. return count;
  260. }
  261. static ssize_t show_wdog_preset(struct fscpos_data *data, char *buf)
  262. {
  263. return sprintf(buf, "%u\n", data->wdog_preset);
  264. }
  265. static ssize_t set_wdog_preset(struct i2c_client *client, struct fscpos_data
  266. *data, const char *buf, size_t count, int reg)
  267. {
  268. unsigned long v = simple_strtoul(buf, NULL, 10) & 0xff;
  269. mutex_lock(&data->update_lock);
  270. data->wdog_preset = v;
  271. fscpos_write_value(client, reg, data->wdog_preset);
  272. mutex_unlock(&data->update_lock);
  273. return count;
  274. }
  275. /* Event */
  276. static ssize_t show_event(struct device *dev, struct device_attribute *attr, char *buf)
  277. {
  278. /* bits 5..7 reserved => mask with 0x1f */
  279. struct fscpos_data *data = fscpos_update_device(dev);
  280. return sprintf(buf, "%u\n", data->global_event & 0x9b);
  281. }
  282. /*
  283. * Sysfs stuff
  284. */
  285. #define create_getter(kind, sub) \
  286. static ssize_t sysfs_show_##kind##sub(struct device *dev, struct device_attribute *attr, char *buf) \
  287. { \
  288. struct fscpos_data *data = fscpos_update_device(dev); \
  289. return show_##kind##sub(data, buf); \
  290. }
  291. #define create_getter_n(kind, offset, sub) \
  292. static ssize_t sysfs_show_##kind##offset##sub(struct device *dev, struct device_attribute *attr, char\
  293. *buf) \
  294. { \
  295. struct fscpos_data *data = fscpos_update_device(dev); \
  296. return show_##kind##sub(data, buf, offset); \
  297. }
  298. #define create_setter(kind, sub, reg) \
  299. static ssize_t sysfs_set_##kind##sub (struct device *dev, struct device_attribute *attr, const char \
  300. *buf, size_t count) \
  301. { \
  302. struct i2c_client *client = to_i2c_client(dev); \
  303. struct fscpos_data *data = i2c_get_clientdata(client); \
  304. return set_##kind##sub(client, data, buf, count, reg); \
  305. }
  306. #define create_setter_n(kind, offset, sub, reg) \
  307. static ssize_t sysfs_set_##kind##offset##sub (struct device *dev, struct device_attribute *attr, \
  308. const char *buf, size_t count) \
  309. { \
  310. struct i2c_client *client = to_i2c_client(dev); \
  311. struct fscpos_data *data = i2c_get_clientdata(client); \
  312. return set_##kind##sub(client, data, buf, count, offset, reg);\
  313. }
  314. #define create_sysfs_device_ro(kind, sub, offset) \
  315. static DEVICE_ATTR(kind##offset##sub, S_IRUGO, \
  316. sysfs_show_##kind##offset##sub, NULL);
  317. #define create_sysfs_device_rw(kind, sub, offset) \
  318. static DEVICE_ATTR(kind##offset##sub, S_IRUGO | S_IWUSR, \
  319. sysfs_show_##kind##offset##sub, sysfs_set_##kind##offset##sub);
  320. #define sysfs_ro_n(kind, sub, offset) \
  321. create_getter_n(kind, offset, sub); \
  322. create_sysfs_device_ro(kind, sub, offset);
  323. #define sysfs_rw_n(kind, sub, offset, reg) \
  324. create_getter_n(kind, offset, sub); \
  325. create_setter_n(kind, offset, sub, reg); \
  326. create_sysfs_device_rw(kind, sub, offset);
  327. #define sysfs_rw(kind, sub, reg) \
  328. create_getter(kind, sub); \
  329. create_setter(kind, sub, reg); \
  330. create_sysfs_device_rw(kind, sub,);
  331. #define sysfs_fan_with_min(offset, reg_status, reg_ripple, reg_min) \
  332. sysfs_fan(offset, reg_status, reg_ripple); \
  333. sysfs_rw_n(pwm,, offset, reg_min);
  334. #define sysfs_fan(offset, reg_status, reg_ripple) \
  335. sysfs_ro_n(fan, _input, offset); \
  336. sysfs_ro_n(fan, _status, offset); \
  337. sysfs_rw_n(fan, _ripple, offset, reg_ripple);
  338. #define sysfs_temp(offset, reg_status) \
  339. sysfs_ro_n(temp, _input, offset); \
  340. sysfs_ro_n(temp, _status, offset); \
  341. sysfs_rw_n(temp, _reset, offset, reg_status);
  342. #define sysfs_watchdog(reg_wdog_preset, reg_wdog_state, reg_wdog_control) \
  343. sysfs_rw(wdog, _control, reg_wdog_control); \
  344. sysfs_rw(wdog, _preset, reg_wdog_preset); \
  345. sysfs_rw(wdog, _state, reg_wdog_state);
  346. sysfs_fan_with_min(1, FSCPOS_REG_FAN_STATE[0], FSCPOS_REG_FAN_RIPPLE[0],
  347. FSCPOS_REG_PWM[0]);
  348. sysfs_fan_with_min(2, FSCPOS_REG_FAN_STATE[1], FSCPOS_REG_FAN_RIPPLE[1],
  349. FSCPOS_REG_PWM[1]);
  350. sysfs_fan(3, FSCPOS_REG_FAN_STATE[2], FSCPOS_REG_FAN_RIPPLE[2]);
  351. sysfs_temp(1, FSCPOS_REG_TEMP_STATE[0]);
  352. sysfs_temp(2, FSCPOS_REG_TEMP_STATE[1]);
  353. sysfs_temp(3, FSCPOS_REG_TEMP_STATE[2]);
  354. sysfs_watchdog(FSCPOS_REG_WDOG_PRESET, FSCPOS_REG_WDOG_STATE,
  355. FSCPOS_REG_WDOG_CONTROL);
  356. static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
  357. static DEVICE_ATTR(in0_input, S_IRUGO, show_volt_12, NULL);
  358. static DEVICE_ATTR(in1_input, S_IRUGO, show_volt_5, NULL);
  359. static DEVICE_ATTR(in2_input, S_IRUGO, show_volt_batt, NULL);
  360. static int fscpos_attach_adapter(struct i2c_adapter *adapter)
  361. {
  362. if (!(adapter->class & I2C_CLASS_HWMON))
  363. return 0;
  364. return i2c_probe(adapter, &addr_data, fscpos_detect);
  365. }
  366. static int fscpos_detect(struct i2c_adapter *adapter, int address, int kind)
  367. {
  368. struct i2c_client *new_client;
  369. struct fscpos_data *data;
  370. int err = 0;
  371. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  372. goto exit;
  373. /*
  374. * OK. For now, we presume we have a valid client. We now create the
  375. * client structure, even though we cannot fill it completely yet.
  376. * But it allows us to access fscpos_{read,write}_value.
  377. */
  378. if (!(data = kzalloc(sizeof(struct fscpos_data), GFP_KERNEL))) {
  379. err = -ENOMEM;
  380. goto exit;
  381. }
  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. mutex_init(&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. mutex_lock(&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. mutex_unlock(&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);