fscpos.c 20 KB

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