fschmd.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. /*
  2. * fschmd.c
  3. *
  4. * Copyright (C) 2007 - 2009 Hans de Goede <hdegoede@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /*
  21. * Merged Fujitsu Siemens hwmon driver, supporting the Poseidon, Hermes,
  22. * Scylla, Heracles, Heimdall, Hades and Syleus chips
  23. *
  24. * Based on the original 2.4 fscscy, 2.6 fscpos, 2.6 fscher and 2.6
  25. * (candidate) fschmd drivers:
  26. * Copyright (C) 2006 Thilo Cestonaro
  27. * <thilo.cestonaro.external@fujitsu-siemens.com>
  28. * Copyright (C) 2004, 2005 Stefan Ott <stefan@desire.ch>
  29. * Copyright (C) 2003, 2004 Reinhard Nissl <rnissl@gmx.de>
  30. * Copyright (c) 2001 Martin Knoblauch <mkn@teraport.de, knobi@knobisoft.de>
  31. * Copyright (C) 2000 Hermann Jung <hej@odn.de>
  32. */
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/i2c.h>
  38. #include <linux/hwmon.h>
  39. #include <linux/hwmon-sysfs.h>
  40. #include <linux/err.h>
  41. #include <linux/mutex.h>
  42. #include <linux/sysfs.h>
  43. #include <linux/dmi.h>
  44. #include <linux/fs.h>
  45. #include <linux/watchdog.h>
  46. #include <linux/miscdevice.h>
  47. #include <linux/uaccess.h>
  48. #include <linux/kref.h>
  49. /* Addresses to scan */
  50. static const unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
  51. /* Insmod parameters */
  52. static bool nowayout = WATCHDOG_NOWAYOUT;
  53. module_param(nowayout, bool, 0);
  54. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  55. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  56. enum chips { fscpos, fscher, fscscy, fschrc, fschmd, fschds, fscsyl };
  57. /*
  58. * The FSCHMD registers and other defines
  59. */
  60. /* chip identification */
  61. #define FSCHMD_REG_IDENT_0 0x00
  62. #define FSCHMD_REG_IDENT_1 0x01
  63. #define FSCHMD_REG_IDENT_2 0x02
  64. #define FSCHMD_REG_REVISION 0x03
  65. /* global control and status */
  66. #define FSCHMD_REG_EVENT_STATE 0x04
  67. #define FSCHMD_REG_CONTROL 0x05
  68. #define FSCHMD_CONTROL_ALERT_LED 0x01
  69. /* watchdog */
  70. static const u8 FSCHMD_REG_WDOG_CONTROL[7] = {
  71. 0x21, 0x21, 0x21, 0x21, 0x21, 0x28, 0x28 };
  72. static const u8 FSCHMD_REG_WDOG_STATE[7] = {
  73. 0x23, 0x23, 0x23, 0x23, 0x23, 0x29, 0x29 };
  74. static const u8 FSCHMD_REG_WDOG_PRESET[7] = {
  75. 0x28, 0x28, 0x28, 0x28, 0x28, 0x2a, 0x2a };
  76. #define FSCHMD_WDOG_CONTROL_TRIGGER 0x10
  77. #define FSCHMD_WDOG_CONTROL_STARTED 0x10 /* the same as trigger */
  78. #define FSCHMD_WDOG_CONTROL_STOP 0x20
  79. #define FSCHMD_WDOG_CONTROL_RESOLUTION 0x40
  80. #define FSCHMD_WDOG_STATE_CARDRESET 0x02
  81. /* voltages, weird order is to keep the same order as the old drivers */
  82. static const u8 FSCHMD_REG_VOLT[7][6] = {
  83. { 0x45, 0x42, 0x48 }, /* pos */
  84. { 0x45, 0x42, 0x48 }, /* her */
  85. { 0x45, 0x42, 0x48 }, /* scy */
  86. { 0x45, 0x42, 0x48 }, /* hrc */
  87. { 0x45, 0x42, 0x48 }, /* hmd */
  88. { 0x21, 0x20, 0x22 }, /* hds */
  89. { 0x21, 0x20, 0x22, 0x23, 0x24, 0x25 }, /* syl */
  90. };
  91. static const int FSCHMD_NO_VOLT_SENSORS[7] = { 3, 3, 3, 3, 3, 3, 6 };
  92. /*
  93. * minimum pwm at which the fan is driven (pwm can by increased depending on
  94. * the temp. Notice that for the scy some fans share there minimum speed.
  95. * Also notice that with the scy the sensor order is different than with the
  96. * other chips, this order was in the 2.4 driver and kept for consistency.
  97. */
  98. static const u8 FSCHMD_REG_FAN_MIN[7][7] = {
  99. { 0x55, 0x65 }, /* pos */
  100. { 0x55, 0x65, 0xb5 }, /* her */
  101. { 0x65, 0x65, 0x55, 0xa5, 0x55, 0xa5 }, /* scy */
  102. { 0x55, 0x65, 0xa5, 0xb5 }, /* hrc */
  103. { 0x55, 0x65, 0xa5, 0xb5, 0xc5 }, /* hmd */
  104. { 0x55, 0x65, 0xa5, 0xb5, 0xc5 }, /* hds */
  105. { 0x54, 0x64, 0x74, 0x84, 0x94, 0xa4, 0xb4 }, /* syl */
  106. };
  107. /* actual fan speed */
  108. static const u8 FSCHMD_REG_FAN_ACT[7][7] = {
  109. { 0x0e, 0x6b, 0xab }, /* pos */
  110. { 0x0e, 0x6b, 0xbb }, /* her */
  111. { 0x6b, 0x6c, 0x0e, 0xab, 0x5c, 0xbb }, /* scy */
  112. { 0x0e, 0x6b, 0xab, 0xbb }, /* hrc */
  113. { 0x5b, 0x6b, 0xab, 0xbb, 0xcb }, /* hmd */
  114. { 0x5b, 0x6b, 0xab, 0xbb, 0xcb }, /* hds */
  115. { 0x57, 0x67, 0x77, 0x87, 0x97, 0xa7, 0xb7 }, /* syl */
  116. };
  117. /* fan status registers */
  118. static const u8 FSCHMD_REG_FAN_STATE[7][7] = {
  119. { 0x0d, 0x62, 0xa2 }, /* pos */
  120. { 0x0d, 0x62, 0xb2 }, /* her */
  121. { 0x62, 0x61, 0x0d, 0xa2, 0x52, 0xb2 }, /* scy */
  122. { 0x0d, 0x62, 0xa2, 0xb2 }, /* hrc */
  123. { 0x52, 0x62, 0xa2, 0xb2, 0xc2 }, /* hmd */
  124. { 0x52, 0x62, 0xa2, 0xb2, 0xc2 }, /* hds */
  125. { 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0 }, /* syl */
  126. };
  127. /* fan ripple / divider registers */
  128. static const u8 FSCHMD_REG_FAN_RIPPLE[7][7] = {
  129. { 0x0f, 0x6f, 0xaf }, /* pos */
  130. { 0x0f, 0x6f, 0xbf }, /* her */
  131. { 0x6f, 0x6f, 0x0f, 0xaf, 0x0f, 0xbf }, /* scy */
  132. { 0x0f, 0x6f, 0xaf, 0xbf }, /* hrc */
  133. { 0x5f, 0x6f, 0xaf, 0xbf, 0xcf }, /* hmd */
  134. { 0x5f, 0x6f, 0xaf, 0xbf, 0xcf }, /* hds */
  135. { 0x56, 0x66, 0x76, 0x86, 0x96, 0xa6, 0xb6 }, /* syl */
  136. };
  137. static const int FSCHMD_NO_FAN_SENSORS[7] = { 3, 3, 6, 4, 5, 5, 7 };
  138. /* Fan status register bitmasks */
  139. #define FSCHMD_FAN_ALARM 0x04 /* called fault by FSC! */
  140. #define FSCHMD_FAN_NOT_PRESENT 0x08
  141. #define FSCHMD_FAN_DISABLED 0x80
  142. /* actual temperature registers */
  143. static const u8 FSCHMD_REG_TEMP_ACT[7][11] = {
  144. { 0x64, 0x32, 0x35 }, /* pos */
  145. { 0x64, 0x32, 0x35 }, /* her */
  146. { 0x64, 0xD0, 0x32, 0x35 }, /* scy */
  147. { 0x64, 0x32, 0x35 }, /* hrc */
  148. { 0x70, 0x80, 0x90, 0xd0, 0xe0 }, /* hmd */
  149. { 0x70, 0x80, 0x90, 0xd0, 0xe0 }, /* hds */
  150. { 0x58, 0x68, 0x78, 0x88, 0x98, 0xa8, /* syl */
  151. 0xb8, 0xc8, 0xd8, 0xe8, 0xf8 },
  152. };
  153. /* temperature state registers */
  154. static const u8 FSCHMD_REG_TEMP_STATE[7][11] = {
  155. { 0x71, 0x81, 0x91 }, /* pos */
  156. { 0x71, 0x81, 0x91 }, /* her */
  157. { 0x71, 0xd1, 0x81, 0x91 }, /* scy */
  158. { 0x71, 0x81, 0x91 }, /* hrc */
  159. { 0x71, 0x81, 0x91, 0xd1, 0xe1 }, /* hmd */
  160. { 0x71, 0x81, 0x91, 0xd1, 0xe1 }, /* hds */
  161. { 0x59, 0x69, 0x79, 0x89, 0x99, 0xa9, /* syl */
  162. 0xb9, 0xc9, 0xd9, 0xe9, 0xf9 },
  163. };
  164. /*
  165. * temperature high limit registers, FSC does not document these. Proven to be
  166. * there with field testing on the fscher and fschrc, already supported / used
  167. * in the fscscy 2.4 driver. FSC has confirmed that the fschmd has registers
  168. * at these addresses, but doesn't want to confirm they are the same as with
  169. * the fscher??
  170. */
  171. static const u8 FSCHMD_REG_TEMP_LIMIT[7][11] = {
  172. { 0, 0, 0 }, /* pos */
  173. { 0x76, 0x86, 0x96 }, /* her */
  174. { 0x76, 0xd6, 0x86, 0x96 }, /* scy */
  175. { 0x76, 0x86, 0x96 }, /* hrc */
  176. { 0x76, 0x86, 0x96, 0xd6, 0xe6 }, /* hmd */
  177. { 0x76, 0x86, 0x96, 0xd6, 0xe6 }, /* hds */
  178. { 0x5a, 0x6a, 0x7a, 0x8a, 0x9a, 0xaa, /* syl */
  179. 0xba, 0xca, 0xda, 0xea, 0xfa },
  180. };
  181. /*
  182. * These were found through experimenting with an fscher, currently they are
  183. * not used, but we keep them around for future reference.
  184. * On the fscsyl AUTOP1 lives at 0x#c (so 0x5c for fan1, 0x6c for fan2, etc),
  185. * AUTOP2 lives at 0x#e, and 0x#1 is a bitmask defining which temps influence
  186. * the fan speed.
  187. * static const u8 FSCHER_REG_TEMP_AUTOP1[] = { 0x73, 0x83, 0x93 };
  188. * static const u8 FSCHER_REG_TEMP_AUTOP2[] = { 0x75, 0x85, 0x95 };
  189. */
  190. static const int FSCHMD_NO_TEMP_SENSORS[7] = { 3, 3, 4, 3, 5, 5, 11 };
  191. /* temp status register bitmasks */
  192. #define FSCHMD_TEMP_WORKING 0x01
  193. #define FSCHMD_TEMP_ALERT 0x02
  194. #define FSCHMD_TEMP_DISABLED 0x80
  195. /* there only really is an alarm if the sensor is working and alert == 1 */
  196. #define FSCHMD_TEMP_ALARM_MASK \
  197. (FSCHMD_TEMP_WORKING | FSCHMD_TEMP_ALERT)
  198. /*
  199. * Functions declarations
  200. */
  201. static int fschmd_probe(struct i2c_client *client,
  202. const struct i2c_device_id *id);
  203. static int fschmd_detect(struct i2c_client *client,
  204. struct i2c_board_info *info);
  205. static int fschmd_remove(struct i2c_client *client);
  206. static struct fschmd_data *fschmd_update_device(struct device *dev);
  207. /*
  208. * Driver data (common to all clients)
  209. */
  210. static const struct i2c_device_id fschmd_id[] = {
  211. { "fscpos", fscpos },
  212. { "fscher", fscher },
  213. { "fscscy", fscscy },
  214. { "fschrc", fschrc },
  215. { "fschmd", fschmd },
  216. { "fschds", fschds },
  217. { "fscsyl", fscsyl },
  218. { }
  219. };
  220. MODULE_DEVICE_TABLE(i2c, fschmd_id);
  221. static struct i2c_driver fschmd_driver = {
  222. .class = I2C_CLASS_HWMON,
  223. .driver = {
  224. .name = "fschmd",
  225. },
  226. .probe = fschmd_probe,
  227. .remove = fschmd_remove,
  228. .id_table = fschmd_id,
  229. .detect = fschmd_detect,
  230. .address_list = normal_i2c,
  231. };
  232. /*
  233. * Client data (each client gets its own)
  234. */
  235. struct fschmd_data {
  236. struct i2c_client *client;
  237. struct device *hwmon_dev;
  238. struct mutex update_lock;
  239. struct mutex watchdog_lock;
  240. struct list_head list; /* member of the watchdog_data_list */
  241. struct kref kref;
  242. struct miscdevice watchdog_miscdev;
  243. enum chips kind;
  244. unsigned long watchdog_is_open;
  245. char watchdog_expect_close;
  246. char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
  247. char valid; /* zero until following fields are valid */
  248. unsigned long last_updated; /* in jiffies */
  249. /* register values */
  250. u8 revision; /* chip revision */
  251. u8 global_control; /* global control register */
  252. u8 watchdog_control; /* watchdog control register */
  253. u8 watchdog_state; /* watchdog status register */
  254. u8 watchdog_preset; /* watchdog counter preset on trigger val */
  255. u8 volt[6]; /* voltage */
  256. u8 temp_act[11]; /* temperature */
  257. u8 temp_status[11]; /* status of sensor */
  258. u8 temp_max[11]; /* high temp limit, notice: undocumented! */
  259. u8 fan_act[7]; /* fans revolutions per second */
  260. u8 fan_status[7]; /* fan status */
  261. u8 fan_min[7]; /* fan min value for rps */
  262. u8 fan_ripple[7]; /* divider for rps */
  263. };
  264. /*
  265. * Global variables to hold information read from special DMI tables, which are
  266. * available on FSC machines with an fscher or later chip. There is no need to
  267. * protect these with a lock as they are only modified from our attach function
  268. * which always gets called with the i2c-core lock held and never accessed
  269. * before the attach function is done with them.
  270. */
  271. static int dmi_mult[6] = { 490, 200, 100, 100, 200, 100 };
  272. static int dmi_offset[6] = { 0, 0, 0, 0, 0, 0 };
  273. static int dmi_vref = -1;
  274. /*
  275. * Somewhat ugly :( global data pointer list with all fschmd devices, so that
  276. * we can find our device data as when using misc_register there is no other
  277. * method to get to ones device data from the open fop.
  278. */
  279. static LIST_HEAD(watchdog_data_list);
  280. /* Note this lock not only protect list access, but also data.kref access */
  281. static DEFINE_MUTEX(watchdog_data_mutex);
  282. /*
  283. * Release our data struct when we're detached from the i2c client *and* all
  284. * references to our watchdog device are released
  285. */
  286. static void fschmd_release_resources(struct kref *ref)
  287. {
  288. struct fschmd_data *data = container_of(ref, struct fschmd_data, kref);
  289. kfree(data);
  290. }
  291. /*
  292. * Sysfs attr show / store functions
  293. */
  294. static ssize_t show_in_value(struct device *dev,
  295. struct device_attribute *devattr, char *buf)
  296. {
  297. const int max_reading[3] = { 14200, 6600, 3300 };
  298. int index = to_sensor_dev_attr(devattr)->index;
  299. struct fschmd_data *data = fschmd_update_device(dev);
  300. if (data->kind == fscher || data->kind >= fschrc)
  301. return sprintf(buf, "%d\n", (data->volt[index] * dmi_vref *
  302. dmi_mult[index]) / 255 + dmi_offset[index]);
  303. else
  304. return sprintf(buf, "%d\n", (data->volt[index] *
  305. max_reading[index] + 128) / 255);
  306. }
  307. #define TEMP_FROM_REG(val) (((val) - 128) * 1000)
  308. static ssize_t show_temp_value(struct device *dev,
  309. struct device_attribute *devattr, char *buf)
  310. {
  311. int index = to_sensor_dev_attr(devattr)->index;
  312. struct fschmd_data *data = fschmd_update_device(dev);
  313. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[index]));
  314. }
  315. static ssize_t show_temp_max(struct device *dev,
  316. struct device_attribute *devattr, char *buf)
  317. {
  318. int index = to_sensor_dev_attr(devattr)->index;
  319. struct fschmd_data *data = fschmd_update_device(dev);
  320. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index]));
  321. }
  322. static ssize_t store_temp_max(struct device *dev, struct device_attribute
  323. *devattr, const char *buf, size_t count)
  324. {
  325. int index = to_sensor_dev_attr(devattr)->index;
  326. struct fschmd_data *data = dev_get_drvdata(dev);
  327. long v;
  328. int err;
  329. err = kstrtol(buf, 10, &v);
  330. if (err)
  331. return err;
  332. v = clamp_val(v / 1000, -128, 127) + 128;
  333. mutex_lock(&data->update_lock);
  334. i2c_smbus_write_byte_data(to_i2c_client(dev),
  335. FSCHMD_REG_TEMP_LIMIT[data->kind][index], v);
  336. data->temp_max[index] = v;
  337. mutex_unlock(&data->update_lock);
  338. return count;
  339. }
  340. static ssize_t show_temp_fault(struct device *dev,
  341. struct device_attribute *devattr, char *buf)
  342. {
  343. int index = to_sensor_dev_attr(devattr)->index;
  344. struct fschmd_data *data = fschmd_update_device(dev);
  345. /* bit 0 set means sensor working ok, so no fault! */
  346. if (data->temp_status[index] & FSCHMD_TEMP_WORKING)
  347. return sprintf(buf, "0\n");
  348. else
  349. return sprintf(buf, "1\n");
  350. }
  351. static ssize_t show_temp_alarm(struct device *dev,
  352. struct device_attribute *devattr, char *buf)
  353. {
  354. int index = to_sensor_dev_attr(devattr)->index;
  355. struct fschmd_data *data = fschmd_update_device(dev);
  356. if ((data->temp_status[index] & FSCHMD_TEMP_ALARM_MASK) ==
  357. FSCHMD_TEMP_ALARM_MASK)
  358. return sprintf(buf, "1\n");
  359. else
  360. return sprintf(buf, "0\n");
  361. }
  362. #define RPM_FROM_REG(val) ((val) * 60)
  363. static ssize_t show_fan_value(struct device *dev,
  364. struct device_attribute *devattr, char *buf)
  365. {
  366. int index = to_sensor_dev_attr(devattr)->index;
  367. struct fschmd_data *data = fschmd_update_device(dev);
  368. return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[index]));
  369. }
  370. static ssize_t show_fan_div(struct device *dev,
  371. struct device_attribute *devattr, char *buf)
  372. {
  373. int index = to_sensor_dev_attr(devattr)->index;
  374. struct fschmd_data *data = fschmd_update_device(dev);
  375. /* bits 2..7 reserved => mask with 3 */
  376. return sprintf(buf, "%d\n", 1 << (data->fan_ripple[index] & 3));
  377. }
  378. static ssize_t store_fan_div(struct device *dev, struct device_attribute
  379. *devattr, const char *buf, size_t count)
  380. {
  381. u8 reg;
  382. int index = to_sensor_dev_attr(devattr)->index;
  383. struct fschmd_data *data = dev_get_drvdata(dev);
  384. /* supported values: 2, 4, 8 */
  385. unsigned long v;
  386. int err;
  387. err = kstrtoul(buf, 10, &v);
  388. if (err)
  389. return err;
  390. switch (v) {
  391. case 2:
  392. v = 1;
  393. break;
  394. case 4:
  395. v = 2;
  396. break;
  397. case 8:
  398. v = 3;
  399. break;
  400. default:
  401. dev_err(dev,
  402. "fan_div value %lu not supported. Choose one of 2, 4 or 8!\n",
  403. v);
  404. return -EINVAL;
  405. }
  406. mutex_lock(&data->update_lock);
  407. reg = i2c_smbus_read_byte_data(to_i2c_client(dev),
  408. FSCHMD_REG_FAN_RIPPLE[data->kind][index]);
  409. /* bits 2..7 reserved => mask with 0x03 */
  410. reg &= ~0x03;
  411. reg |= v;
  412. i2c_smbus_write_byte_data(to_i2c_client(dev),
  413. FSCHMD_REG_FAN_RIPPLE[data->kind][index], reg);
  414. data->fan_ripple[index] = reg;
  415. mutex_unlock(&data->update_lock);
  416. return count;
  417. }
  418. static ssize_t show_fan_alarm(struct device *dev,
  419. struct device_attribute *devattr, char *buf)
  420. {
  421. int index = to_sensor_dev_attr(devattr)->index;
  422. struct fschmd_data *data = fschmd_update_device(dev);
  423. if (data->fan_status[index] & FSCHMD_FAN_ALARM)
  424. return sprintf(buf, "1\n");
  425. else
  426. return sprintf(buf, "0\n");
  427. }
  428. static ssize_t show_fan_fault(struct device *dev,
  429. struct device_attribute *devattr, char *buf)
  430. {
  431. int index = to_sensor_dev_attr(devattr)->index;
  432. struct fschmd_data *data = fschmd_update_device(dev);
  433. if (data->fan_status[index] & FSCHMD_FAN_NOT_PRESENT)
  434. return sprintf(buf, "1\n");
  435. else
  436. return sprintf(buf, "0\n");
  437. }
  438. static ssize_t show_pwm_auto_point1_pwm(struct device *dev,
  439. struct device_attribute *devattr, char *buf)
  440. {
  441. int index = to_sensor_dev_attr(devattr)->index;
  442. struct fschmd_data *data = fschmd_update_device(dev);
  443. int val = data->fan_min[index];
  444. /* 0 = allow turning off (except on the syl), 1-255 = 50-100% */
  445. if (val || data->kind == fscsyl)
  446. val = val / 2 + 128;
  447. return sprintf(buf, "%d\n", val);
  448. }
  449. static ssize_t store_pwm_auto_point1_pwm(struct device *dev,
  450. struct device_attribute *devattr, const char *buf, size_t count)
  451. {
  452. int index = to_sensor_dev_attr(devattr)->index;
  453. struct fschmd_data *data = dev_get_drvdata(dev);
  454. unsigned long v;
  455. int err;
  456. err = kstrtoul(buf, 10, &v);
  457. if (err)
  458. return err;
  459. /* reg: 0 = allow turning off (except on the syl), 1-255 = 50-100% */
  460. if (v || data->kind == fscsyl) {
  461. v = clamp_val(v, 128, 255);
  462. v = (v - 128) * 2 + 1;
  463. }
  464. mutex_lock(&data->update_lock);
  465. i2c_smbus_write_byte_data(to_i2c_client(dev),
  466. FSCHMD_REG_FAN_MIN[data->kind][index], v);
  467. data->fan_min[index] = v;
  468. mutex_unlock(&data->update_lock);
  469. return count;
  470. }
  471. /*
  472. * The FSC hwmon family has the ability to force an attached alert led to flash
  473. * from software, we export this as an alert_led sysfs attr
  474. */
  475. static ssize_t show_alert_led(struct device *dev,
  476. struct device_attribute *devattr, char *buf)
  477. {
  478. struct fschmd_data *data = fschmd_update_device(dev);
  479. if (data->global_control & FSCHMD_CONTROL_ALERT_LED)
  480. return sprintf(buf, "1\n");
  481. else
  482. return sprintf(buf, "0\n");
  483. }
  484. static ssize_t store_alert_led(struct device *dev,
  485. struct device_attribute *devattr, const char *buf, size_t count)
  486. {
  487. u8 reg;
  488. struct fschmd_data *data = dev_get_drvdata(dev);
  489. unsigned long v;
  490. int err;
  491. err = kstrtoul(buf, 10, &v);
  492. if (err)
  493. return err;
  494. mutex_lock(&data->update_lock);
  495. reg = i2c_smbus_read_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL);
  496. if (v)
  497. reg |= FSCHMD_CONTROL_ALERT_LED;
  498. else
  499. reg &= ~FSCHMD_CONTROL_ALERT_LED;
  500. i2c_smbus_write_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL, reg);
  501. data->global_control = reg;
  502. mutex_unlock(&data->update_lock);
  503. return count;
  504. }
  505. static DEVICE_ATTR(alert_led, 0644, show_alert_led, store_alert_led);
  506. static struct sensor_device_attribute fschmd_attr[] = {
  507. SENSOR_ATTR(in0_input, 0444, show_in_value, NULL, 0),
  508. SENSOR_ATTR(in1_input, 0444, show_in_value, NULL, 1),
  509. SENSOR_ATTR(in2_input, 0444, show_in_value, NULL, 2),
  510. SENSOR_ATTR(in3_input, 0444, show_in_value, NULL, 3),
  511. SENSOR_ATTR(in4_input, 0444, show_in_value, NULL, 4),
  512. SENSOR_ATTR(in5_input, 0444, show_in_value, NULL, 5),
  513. };
  514. static struct sensor_device_attribute fschmd_temp_attr[] = {
  515. SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0),
  516. SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0),
  517. SENSOR_ATTR(temp1_fault, 0444, show_temp_fault, NULL, 0),
  518. SENSOR_ATTR(temp1_alarm, 0444, show_temp_alarm, NULL, 0),
  519. SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1),
  520. SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1),
  521. SENSOR_ATTR(temp2_fault, 0444, show_temp_fault, NULL, 1),
  522. SENSOR_ATTR(temp2_alarm, 0444, show_temp_alarm, NULL, 1),
  523. SENSOR_ATTR(temp3_input, 0444, show_temp_value, NULL, 2),
  524. SENSOR_ATTR(temp3_max, 0644, show_temp_max, store_temp_max, 2),
  525. SENSOR_ATTR(temp3_fault, 0444, show_temp_fault, NULL, 2),
  526. SENSOR_ATTR(temp3_alarm, 0444, show_temp_alarm, NULL, 2),
  527. SENSOR_ATTR(temp4_input, 0444, show_temp_value, NULL, 3),
  528. SENSOR_ATTR(temp4_max, 0644, show_temp_max, store_temp_max, 3),
  529. SENSOR_ATTR(temp4_fault, 0444, show_temp_fault, NULL, 3),
  530. SENSOR_ATTR(temp4_alarm, 0444, show_temp_alarm, NULL, 3),
  531. SENSOR_ATTR(temp5_input, 0444, show_temp_value, NULL, 4),
  532. SENSOR_ATTR(temp5_max, 0644, show_temp_max, store_temp_max, 4),
  533. SENSOR_ATTR(temp5_fault, 0444, show_temp_fault, NULL, 4),
  534. SENSOR_ATTR(temp5_alarm, 0444, show_temp_alarm, NULL, 4),
  535. SENSOR_ATTR(temp6_input, 0444, show_temp_value, NULL, 5),
  536. SENSOR_ATTR(temp6_max, 0644, show_temp_max, store_temp_max, 5),
  537. SENSOR_ATTR(temp6_fault, 0444, show_temp_fault, NULL, 5),
  538. SENSOR_ATTR(temp6_alarm, 0444, show_temp_alarm, NULL, 5),
  539. SENSOR_ATTR(temp7_input, 0444, show_temp_value, NULL, 6),
  540. SENSOR_ATTR(temp7_max, 0644, show_temp_max, store_temp_max, 6),
  541. SENSOR_ATTR(temp7_fault, 0444, show_temp_fault, NULL, 6),
  542. SENSOR_ATTR(temp7_alarm, 0444, show_temp_alarm, NULL, 6),
  543. SENSOR_ATTR(temp8_input, 0444, show_temp_value, NULL, 7),
  544. SENSOR_ATTR(temp8_max, 0644, show_temp_max, store_temp_max, 7),
  545. SENSOR_ATTR(temp8_fault, 0444, show_temp_fault, NULL, 7),
  546. SENSOR_ATTR(temp8_alarm, 0444, show_temp_alarm, NULL, 7),
  547. SENSOR_ATTR(temp9_input, 0444, show_temp_value, NULL, 8),
  548. SENSOR_ATTR(temp9_max, 0644, show_temp_max, store_temp_max, 8),
  549. SENSOR_ATTR(temp9_fault, 0444, show_temp_fault, NULL, 8),
  550. SENSOR_ATTR(temp9_alarm, 0444, show_temp_alarm, NULL, 8),
  551. SENSOR_ATTR(temp10_input, 0444, show_temp_value, NULL, 9),
  552. SENSOR_ATTR(temp10_max, 0644, show_temp_max, store_temp_max, 9),
  553. SENSOR_ATTR(temp10_fault, 0444, show_temp_fault, NULL, 9),
  554. SENSOR_ATTR(temp10_alarm, 0444, show_temp_alarm, NULL, 9),
  555. SENSOR_ATTR(temp11_input, 0444, show_temp_value, NULL, 10),
  556. SENSOR_ATTR(temp11_max, 0644, show_temp_max, store_temp_max, 10),
  557. SENSOR_ATTR(temp11_fault, 0444, show_temp_fault, NULL, 10),
  558. SENSOR_ATTR(temp11_alarm, 0444, show_temp_alarm, NULL, 10),
  559. };
  560. static struct sensor_device_attribute fschmd_fan_attr[] = {
  561. SENSOR_ATTR(fan1_input, 0444, show_fan_value, NULL, 0),
  562. SENSOR_ATTR(fan1_div, 0644, show_fan_div, store_fan_div, 0),
  563. SENSOR_ATTR(fan1_alarm, 0444, show_fan_alarm, NULL, 0),
  564. SENSOR_ATTR(fan1_fault, 0444, show_fan_fault, NULL, 0),
  565. SENSOR_ATTR(pwm1_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  566. store_pwm_auto_point1_pwm, 0),
  567. SENSOR_ATTR(fan2_input, 0444, show_fan_value, NULL, 1),
  568. SENSOR_ATTR(fan2_div, 0644, show_fan_div, store_fan_div, 1),
  569. SENSOR_ATTR(fan2_alarm, 0444, show_fan_alarm, NULL, 1),
  570. SENSOR_ATTR(fan2_fault, 0444, show_fan_fault, NULL, 1),
  571. SENSOR_ATTR(pwm2_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  572. store_pwm_auto_point1_pwm, 1),
  573. SENSOR_ATTR(fan3_input, 0444, show_fan_value, NULL, 2),
  574. SENSOR_ATTR(fan3_div, 0644, show_fan_div, store_fan_div, 2),
  575. SENSOR_ATTR(fan3_alarm, 0444, show_fan_alarm, NULL, 2),
  576. SENSOR_ATTR(fan3_fault, 0444, show_fan_fault, NULL, 2),
  577. SENSOR_ATTR(pwm3_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  578. store_pwm_auto_point1_pwm, 2),
  579. SENSOR_ATTR(fan4_input, 0444, show_fan_value, NULL, 3),
  580. SENSOR_ATTR(fan4_div, 0644, show_fan_div, store_fan_div, 3),
  581. SENSOR_ATTR(fan4_alarm, 0444, show_fan_alarm, NULL, 3),
  582. SENSOR_ATTR(fan4_fault, 0444, show_fan_fault, NULL, 3),
  583. SENSOR_ATTR(pwm4_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  584. store_pwm_auto_point1_pwm, 3),
  585. SENSOR_ATTR(fan5_input, 0444, show_fan_value, NULL, 4),
  586. SENSOR_ATTR(fan5_div, 0644, show_fan_div, store_fan_div, 4),
  587. SENSOR_ATTR(fan5_alarm, 0444, show_fan_alarm, NULL, 4),
  588. SENSOR_ATTR(fan5_fault, 0444, show_fan_fault, NULL, 4),
  589. SENSOR_ATTR(pwm5_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  590. store_pwm_auto_point1_pwm, 4),
  591. SENSOR_ATTR(fan6_input, 0444, show_fan_value, NULL, 5),
  592. SENSOR_ATTR(fan6_div, 0644, show_fan_div, store_fan_div, 5),
  593. SENSOR_ATTR(fan6_alarm, 0444, show_fan_alarm, NULL, 5),
  594. SENSOR_ATTR(fan6_fault, 0444, show_fan_fault, NULL, 5),
  595. SENSOR_ATTR(pwm6_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  596. store_pwm_auto_point1_pwm, 5),
  597. SENSOR_ATTR(fan7_input, 0444, show_fan_value, NULL, 6),
  598. SENSOR_ATTR(fan7_div, 0644, show_fan_div, store_fan_div, 6),
  599. SENSOR_ATTR(fan7_alarm, 0444, show_fan_alarm, NULL, 6),
  600. SENSOR_ATTR(fan7_fault, 0444, show_fan_fault, NULL, 6),
  601. SENSOR_ATTR(pwm7_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  602. store_pwm_auto_point1_pwm, 6),
  603. };
  604. /*
  605. * Watchdog routines
  606. */
  607. static int watchdog_set_timeout(struct fschmd_data *data, int timeout)
  608. {
  609. int ret, resolution;
  610. int kind = data->kind + 1; /* 0-x array index -> 1-x module param */
  611. /* 2 second or 60 second resolution? */
  612. if (timeout <= 510 || kind == fscpos || kind == fscscy)
  613. resolution = 2;
  614. else
  615. resolution = 60;
  616. if (timeout < resolution || timeout > (resolution * 255))
  617. return -EINVAL;
  618. mutex_lock(&data->watchdog_lock);
  619. if (!data->client) {
  620. ret = -ENODEV;
  621. goto leave;
  622. }
  623. if (resolution == 2)
  624. data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_RESOLUTION;
  625. else
  626. data->watchdog_control |= FSCHMD_WDOG_CONTROL_RESOLUTION;
  627. data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
  628. /* Write new timeout value */
  629. i2c_smbus_write_byte_data(data->client,
  630. FSCHMD_REG_WDOG_PRESET[data->kind], data->watchdog_preset);
  631. /* Write new control register, do not trigger! */
  632. i2c_smbus_write_byte_data(data->client,
  633. FSCHMD_REG_WDOG_CONTROL[data->kind],
  634. data->watchdog_control & ~FSCHMD_WDOG_CONTROL_TRIGGER);
  635. ret = data->watchdog_preset * resolution;
  636. leave:
  637. mutex_unlock(&data->watchdog_lock);
  638. return ret;
  639. }
  640. static int watchdog_get_timeout(struct fschmd_data *data)
  641. {
  642. int timeout;
  643. mutex_lock(&data->watchdog_lock);
  644. if (data->watchdog_control & FSCHMD_WDOG_CONTROL_RESOLUTION)
  645. timeout = data->watchdog_preset * 60;
  646. else
  647. timeout = data->watchdog_preset * 2;
  648. mutex_unlock(&data->watchdog_lock);
  649. return timeout;
  650. }
  651. static int watchdog_trigger(struct fschmd_data *data)
  652. {
  653. int ret = 0;
  654. mutex_lock(&data->watchdog_lock);
  655. if (!data->client) {
  656. ret = -ENODEV;
  657. goto leave;
  658. }
  659. data->watchdog_control |= FSCHMD_WDOG_CONTROL_TRIGGER;
  660. i2c_smbus_write_byte_data(data->client,
  661. FSCHMD_REG_WDOG_CONTROL[data->kind],
  662. data->watchdog_control);
  663. leave:
  664. mutex_unlock(&data->watchdog_lock);
  665. return ret;
  666. }
  667. static int watchdog_stop(struct fschmd_data *data)
  668. {
  669. int ret = 0;
  670. mutex_lock(&data->watchdog_lock);
  671. if (!data->client) {
  672. ret = -ENODEV;
  673. goto leave;
  674. }
  675. data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_STARTED;
  676. /*
  677. * Don't store the stop flag in our watchdog control register copy, as
  678. * its a write only bit (read always returns 0)
  679. */
  680. i2c_smbus_write_byte_data(data->client,
  681. FSCHMD_REG_WDOG_CONTROL[data->kind],
  682. data->watchdog_control | FSCHMD_WDOG_CONTROL_STOP);
  683. leave:
  684. mutex_unlock(&data->watchdog_lock);
  685. return ret;
  686. }
  687. static int watchdog_open(struct inode *inode, struct file *filp)
  688. {
  689. struct fschmd_data *pos, *data = NULL;
  690. int watchdog_is_open;
  691. /*
  692. * We get called from drivers/char/misc.c with misc_mtx hold, and we
  693. * call misc_register() from fschmd_probe() with watchdog_data_mutex
  694. * hold, as misc_register() takes the misc_mtx lock, this is a possible
  695. * deadlock, so we use mutex_trylock here.
  696. */
  697. if (!mutex_trylock(&watchdog_data_mutex))
  698. return -ERESTARTSYS;
  699. list_for_each_entry(pos, &watchdog_data_list, list) {
  700. if (pos->watchdog_miscdev.minor == iminor(inode)) {
  701. data = pos;
  702. break;
  703. }
  704. }
  705. /* Note we can never not have found data, so we don't check for this */
  706. watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
  707. if (!watchdog_is_open)
  708. kref_get(&data->kref);
  709. mutex_unlock(&watchdog_data_mutex);
  710. if (watchdog_is_open)
  711. return -EBUSY;
  712. /* Start the watchdog */
  713. watchdog_trigger(data);
  714. filp->private_data = data;
  715. return nonseekable_open(inode, filp);
  716. }
  717. static int watchdog_release(struct inode *inode, struct file *filp)
  718. {
  719. struct fschmd_data *data = filp->private_data;
  720. if (data->watchdog_expect_close) {
  721. watchdog_stop(data);
  722. data->watchdog_expect_close = 0;
  723. } else {
  724. watchdog_trigger(data);
  725. dev_crit(&data->client->dev,
  726. "unexpected close, not stopping watchdog!\n");
  727. }
  728. clear_bit(0, &data->watchdog_is_open);
  729. mutex_lock(&watchdog_data_mutex);
  730. kref_put(&data->kref, fschmd_release_resources);
  731. mutex_unlock(&watchdog_data_mutex);
  732. return 0;
  733. }
  734. static ssize_t watchdog_write(struct file *filp, const char __user *buf,
  735. size_t count, loff_t *offset)
  736. {
  737. int ret;
  738. struct fschmd_data *data = filp->private_data;
  739. if (count) {
  740. if (!nowayout) {
  741. size_t i;
  742. /* Clear it in case it was set with a previous write */
  743. data->watchdog_expect_close = 0;
  744. for (i = 0; i != count; i++) {
  745. char c;
  746. if (get_user(c, buf + i))
  747. return -EFAULT;
  748. if (c == 'V')
  749. data->watchdog_expect_close = 1;
  750. }
  751. }
  752. ret = watchdog_trigger(data);
  753. if (ret < 0)
  754. return ret;
  755. }
  756. return count;
  757. }
  758. static long watchdog_ioctl(struct file *filp, unsigned int cmd,
  759. unsigned long arg)
  760. {
  761. struct watchdog_info ident = {
  762. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  763. WDIOF_CARDRESET,
  764. .identity = "FSC watchdog"
  765. };
  766. int i, ret = 0;
  767. struct fschmd_data *data = filp->private_data;
  768. switch (cmd) {
  769. case WDIOC_GETSUPPORT:
  770. ident.firmware_version = data->revision;
  771. if (!nowayout)
  772. ident.options |= WDIOF_MAGICCLOSE;
  773. if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
  774. ret = -EFAULT;
  775. break;
  776. case WDIOC_GETSTATUS:
  777. ret = put_user(0, (int __user *)arg);
  778. break;
  779. case WDIOC_GETBOOTSTATUS:
  780. if (data->watchdog_state & FSCHMD_WDOG_STATE_CARDRESET)
  781. ret = put_user(WDIOF_CARDRESET, (int __user *)arg);
  782. else
  783. ret = put_user(0, (int __user *)arg);
  784. break;
  785. case WDIOC_KEEPALIVE:
  786. ret = watchdog_trigger(data);
  787. break;
  788. case WDIOC_GETTIMEOUT:
  789. i = watchdog_get_timeout(data);
  790. ret = put_user(i, (int __user *)arg);
  791. break;
  792. case WDIOC_SETTIMEOUT:
  793. if (get_user(i, (int __user *)arg)) {
  794. ret = -EFAULT;
  795. break;
  796. }
  797. ret = watchdog_set_timeout(data, i);
  798. if (ret > 0)
  799. ret = put_user(ret, (int __user *)arg);
  800. break;
  801. case WDIOC_SETOPTIONS:
  802. if (get_user(i, (int __user *)arg)) {
  803. ret = -EFAULT;
  804. break;
  805. }
  806. if (i & WDIOS_DISABLECARD)
  807. ret = watchdog_stop(data);
  808. else if (i & WDIOS_ENABLECARD)
  809. ret = watchdog_trigger(data);
  810. else
  811. ret = -EINVAL;
  812. break;
  813. default:
  814. ret = -ENOTTY;
  815. }
  816. return ret;
  817. }
  818. static const struct file_operations watchdog_fops = {
  819. .owner = THIS_MODULE,
  820. .llseek = no_llseek,
  821. .open = watchdog_open,
  822. .release = watchdog_release,
  823. .write = watchdog_write,
  824. .unlocked_ioctl = watchdog_ioctl,
  825. };
  826. /*
  827. * Detect, register, unregister and update device functions
  828. */
  829. /*
  830. * DMI decode routine to read voltage scaling factors from special DMI tables,
  831. * which are available on FSC machines with an fscher or later chip.
  832. */
  833. static void fschmd_dmi_decode(const struct dmi_header *header, void *dummy)
  834. {
  835. int i, mult[3] = { 0 }, offset[3] = { 0 }, vref = 0, found = 0;
  836. /*
  837. * dmi code ugliness, we get passed the address of the contents of
  838. * a complete DMI record, but in the form of a dmi_header pointer, in
  839. * reality this address holds header->length bytes of which the header
  840. * are the first 4 bytes
  841. */
  842. u8 *dmi_data = (u8 *)header;
  843. /* We are looking for OEM-specific type 185 */
  844. if (header->type != 185)
  845. return;
  846. /*
  847. * we are looking for what Siemens calls "subtype" 19, the subtype
  848. * is stored in byte 5 of the dmi block
  849. */
  850. if (header->length < 5 || dmi_data[4] != 19)
  851. return;
  852. /*
  853. * After the subtype comes 1 unknown byte and then blocks of 5 bytes,
  854. * consisting of what Siemens calls an "Entity" number, followed by
  855. * 2 16-bit words in LSB first order
  856. */
  857. for (i = 6; (i + 4) < header->length; i += 5) {
  858. /* entity 1 - 3: voltage multiplier and offset */
  859. if (dmi_data[i] >= 1 && dmi_data[i] <= 3) {
  860. /* Our in sensors order and the DMI order differ */
  861. const int shuffle[3] = { 1, 0, 2 };
  862. int in = shuffle[dmi_data[i] - 1];
  863. /* Check for twice the same entity */
  864. if (found & (1 << in))
  865. return;
  866. mult[in] = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
  867. offset[in] = dmi_data[i + 3] | (dmi_data[i + 4] << 8);
  868. found |= 1 << in;
  869. }
  870. /* entity 7: reference voltage */
  871. if (dmi_data[i] == 7) {
  872. /* Check for twice the same entity */
  873. if (found & 0x08)
  874. return;
  875. vref = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
  876. found |= 0x08;
  877. }
  878. }
  879. if (found == 0x0F) {
  880. for (i = 0; i < 3; i++) {
  881. dmi_mult[i] = mult[i] * 10;
  882. dmi_offset[i] = offset[i] * 10;
  883. }
  884. /*
  885. * According to the docs there should be separate dmi entries
  886. * for the mult's and offsets of in3-5 of the syl, but on
  887. * my test machine these are not present
  888. */
  889. dmi_mult[3] = dmi_mult[2];
  890. dmi_mult[4] = dmi_mult[1];
  891. dmi_mult[5] = dmi_mult[2];
  892. dmi_offset[3] = dmi_offset[2];
  893. dmi_offset[4] = dmi_offset[1];
  894. dmi_offset[5] = dmi_offset[2];
  895. dmi_vref = vref;
  896. }
  897. }
  898. static int fschmd_detect(struct i2c_client *client,
  899. struct i2c_board_info *info)
  900. {
  901. enum chips kind;
  902. struct i2c_adapter *adapter = client->adapter;
  903. char id[4];
  904. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  905. return -ENODEV;
  906. /* Detect & Identify the chip */
  907. id[0] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_0);
  908. id[1] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_1);
  909. id[2] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_2);
  910. id[3] = '\0';
  911. if (!strcmp(id, "PEG"))
  912. kind = fscpos;
  913. else if (!strcmp(id, "HER"))
  914. kind = fscher;
  915. else if (!strcmp(id, "SCY"))
  916. kind = fscscy;
  917. else if (!strcmp(id, "HRC"))
  918. kind = fschrc;
  919. else if (!strcmp(id, "HMD"))
  920. kind = fschmd;
  921. else if (!strcmp(id, "HDS"))
  922. kind = fschds;
  923. else if (!strcmp(id, "SYL"))
  924. kind = fscsyl;
  925. else
  926. return -ENODEV;
  927. strlcpy(info->type, fschmd_id[kind].name, I2C_NAME_SIZE);
  928. return 0;
  929. }
  930. static int fschmd_probe(struct i2c_client *client,
  931. const struct i2c_device_id *id)
  932. {
  933. struct fschmd_data *data;
  934. const char * const names[7] = { "Poseidon", "Hermes", "Scylla",
  935. "Heracles", "Heimdall", "Hades", "Syleus" };
  936. const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
  937. int i, err;
  938. enum chips kind = id->driver_data;
  939. data = kzalloc(sizeof(struct fschmd_data), GFP_KERNEL);
  940. if (!data)
  941. return -ENOMEM;
  942. i2c_set_clientdata(client, data);
  943. mutex_init(&data->update_lock);
  944. mutex_init(&data->watchdog_lock);
  945. INIT_LIST_HEAD(&data->list);
  946. kref_init(&data->kref);
  947. /*
  948. * Store client pointer in our data struct for watchdog usage
  949. * (where the client is found through a data ptr instead of the
  950. * otherway around)
  951. */
  952. data->client = client;
  953. data->kind = kind;
  954. if (kind == fscpos) {
  955. /*
  956. * The Poseidon has hardwired temp limits, fill these
  957. * in for the alarm resetting code
  958. */
  959. data->temp_max[0] = 70 + 128;
  960. data->temp_max[1] = 50 + 128;
  961. data->temp_max[2] = 50 + 128;
  962. }
  963. /* Read the special DMI table for fscher and newer chips */
  964. if ((kind == fscher || kind >= fschrc) && dmi_vref == -1) {
  965. dmi_walk(fschmd_dmi_decode, NULL);
  966. if (dmi_vref == -1) {
  967. dev_warn(&client->dev,
  968. "Couldn't get voltage scaling factors from "
  969. "BIOS DMI table, using builtin defaults\n");
  970. dmi_vref = 33;
  971. }
  972. }
  973. /* Read in some never changing registers */
  974. data->revision = i2c_smbus_read_byte_data(client, FSCHMD_REG_REVISION);
  975. data->global_control = i2c_smbus_read_byte_data(client,
  976. FSCHMD_REG_CONTROL);
  977. data->watchdog_control = i2c_smbus_read_byte_data(client,
  978. FSCHMD_REG_WDOG_CONTROL[data->kind]);
  979. data->watchdog_state = i2c_smbus_read_byte_data(client,
  980. FSCHMD_REG_WDOG_STATE[data->kind]);
  981. data->watchdog_preset = i2c_smbus_read_byte_data(client,
  982. FSCHMD_REG_WDOG_PRESET[data->kind]);
  983. err = device_create_file(&client->dev, &dev_attr_alert_led);
  984. if (err)
  985. goto exit_detach;
  986. for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++) {
  987. err = device_create_file(&client->dev,
  988. &fschmd_attr[i].dev_attr);
  989. if (err)
  990. goto exit_detach;
  991. }
  992. for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++) {
  993. /* Poseidon doesn't have TEMP_LIMIT registers */
  994. if (kind == fscpos && fschmd_temp_attr[i].dev_attr.show ==
  995. show_temp_max)
  996. continue;
  997. if (kind == fscsyl) {
  998. if (i % 4 == 0)
  999. data->temp_status[i / 4] =
  1000. i2c_smbus_read_byte_data(client,
  1001. FSCHMD_REG_TEMP_STATE
  1002. [data->kind][i / 4]);
  1003. if (data->temp_status[i / 4] & FSCHMD_TEMP_DISABLED)
  1004. continue;
  1005. }
  1006. err = device_create_file(&client->dev,
  1007. &fschmd_temp_attr[i].dev_attr);
  1008. if (err)
  1009. goto exit_detach;
  1010. }
  1011. for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++) {
  1012. /* Poseidon doesn't have a FAN_MIN register for its 3rd fan */
  1013. if (kind == fscpos &&
  1014. !strcmp(fschmd_fan_attr[i].dev_attr.attr.name,
  1015. "pwm3_auto_point1_pwm"))
  1016. continue;
  1017. if (kind == fscsyl) {
  1018. if (i % 5 == 0)
  1019. data->fan_status[i / 5] =
  1020. i2c_smbus_read_byte_data(client,
  1021. FSCHMD_REG_FAN_STATE
  1022. [data->kind][i / 5]);
  1023. if (data->fan_status[i / 5] & FSCHMD_FAN_DISABLED)
  1024. continue;
  1025. }
  1026. err = device_create_file(&client->dev,
  1027. &fschmd_fan_attr[i].dev_attr);
  1028. if (err)
  1029. goto exit_detach;
  1030. }
  1031. data->hwmon_dev = hwmon_device_register(&client->dev);
  1032. if (IS_ERR(data->hwmon_dev)) {
  1033. err = PTR_ERR(data->hwmon_dev);
  1034. data->hwmon_dev = NULL;
  1035. goto exit_detach;
  1036. }
  1037. /*
  1038. * We take the data_mutex lock early so that watchdog_open() cannot
  1039. * run when misc_register() has completed, but we've not yet added
  1040. * our data to the watchdog_data_list (and set the default timeout)
  1041. */
  1042. mutex_lock(&watchdog_data_mutex);
  1043. for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
  1044. /* Register our watchdog part */
  1045. snprintf(data->watchdog_name, sizeof(data->watchdog_name),
  1046. "watchdog%c", (i == 0) ? '\0' : ('0' + i));
  1047. data->watchdog_miscdev.name = data->watchdog_name;
  1048. data->watchdog_miscdev.fops = &watchdog_fops;
  1049. data->watchdog_miscdev.minor = watchdog_minors[i];
  1050. err = misc_register(&data->watchdog_miscdev);
  1051. if (err == -EBUSY)
  1052. continue;
  1053. if (err) {
  1054. data->watchdog_miscdev.minor = 0;
  1055. dev_err(&client->dev,
  1056. "Registering watchdog chardev: %d\n", err);
  1057. break;
  1058. }
  1059. list_add(&data->list, &watchdog_data_list);
  1060. watchdog_set_timeout(data, 60);
  1061. dev_info(&client->dev,
  1062. "Registered watchdog chardev major 10, minor: %d\n",
  1063. watchdog_minors[i]);
  1064. break;
  1065. }
  1066. if (i == ARRAY_SIZE(watchdog_minors)) {
  1067. data->watchdog_miscdev.minor = 0;
  1068. dev_warn(&client->dev,
  1069. "Couldn't register watchdog chardev (due to no free minor)\n");
  1070. }
  1071. mutex_unlock(&watchdog_data_mutex);
  1072. dev_info(&client->dev, "Detected FSC %s chip, revision: %d\n",
  1073. names[data->kind], (int) data->revision);
  1074. return 0;
  1075. exit_detach:
  1076. fschmd_remove(client); /* will also free data for us */
  1077. return err;
  1078. }
  1079. static int fschmd_remove(struct i2c_client *client)
  1080. {
  1081. struct fschmd_data *data = i2c_get_clientdata(client);
  1082. int i;
  1083. /* Unregister the watchdog (if registered) */
  1084. if (data->watchdog_miscdev.minor) {
  1085. misc_deregister(&data->watchdog_miscdev);
  1086. if (data->watchdog_is_open) {
  1087. dev_warn(&client->dev,
  1088. "i2c client detached with watchdog open! "
  1089. "Stopping watchdog.\n");
  1090. watchdog_stop(data);
  1091. }
  1092. mutex_lock(&watchdog_data_mutex);
  1093. list_del(&data->list);
  1094. mutex_unlock(&watchdog_data_mutex);
  1095. /* Tell the watchdog code the client is gone */
  1096. mutex_lock(&data->watchdog_lock);
  1097. data->client = NULL;
  1098. mutex_unlock(&data->watchdog_lock);
  1099. }
  1100. /*
  1101. * Check if registered in case we're called from fschmd_detect
  1102. * to cleanup after an error
  1103. */
  1104. if (data->hwmon_dev)
  1105. hwmon_device_unregister(data->hwmon_dev);
  1106. device_remove_file(&client->dev, &dev_attr_alert_led);
  1107. for (i = 0; i < (FSCHMD_NO_VOLT_SENSORS[data->kind]); i++)
  1108. device_remove_file(&client->dev, &fschmd_attr[i].dev_attr);
  1109. for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++)
  1110. device_remove_file(&client->dev,
  1111. &fschmd_temp_attr[i].dev_attr);
  1112. for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++)
  1113. device_remove_file(&client->dev,
  1114. &fschmd_fan_attr[i].dev_attr);
  1115. mutex_lock(&watchdog_data_mutex);
  1116. kref_put(&data->kref, fschmd_release_resources);
  1117. mutex_unlock(&watchdog_data_mutex);
  1118. return 0;
  1119. }
  1120. static struct fschmd_data *fschmd_update_device(struct device *dev)
  1121. {
  1122. struct i2c_client *client = to_i2c_client(dev);
  1123. struct fschmd_data *data = i2c_get_clientdata(client);
  1124. int i;
  1125. mutex_lock(&data->update_lock);
  1126. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  1127. for (i = 0; i < FSCHMD_NO_TEMP_SENSORS[data->kind]; i++) {
  1128. data->temp_act[i] = i2c_smbus_read_byte_data(client,
  1129. FSCHMD_REG_TEMP_ACT[data->kind][i]);
  1130. data->temp_status[i] = i2c_smbus_read_byte_data(client,
  1131. FSCHMD_REG_TEMP_STATE[data->kind][i]);
  1132. /* The fscpos doesn't have TEMP_LIMIT registers */
  1133. if (FSCHMD_REG_TEMP_LIMIT[data->kind][i])
  1134. data->temp_max[i] = i2c_smbus_read_byte_data(
  1135. client,
  1136. FSCHMD_REG_TEMP_LIMIT[data->kind][i]);
  1137. /*
  1138. * reset alarm if the alarm condition is gone,
  1139. * the chip doesn't do this itself
  1140. */
  1141. if ((data->temp_status[i] & FSCHMD_TEMP_ALARM_MASK) ==
  1142. FSCHMD_TEMP_ALARM_MASK &&
  1143. data->temp_act[i] < data->temp_max[i])
  1144. i2c_smbus_write_byte_data(client,
  1145. FSCHMD_REG_TEMP_STATE[data->kind][i],
  1146. data->temp_status[i]);
  1147. }
  1148. for (i = 0; i < FSCHMD_NO_FAN_SENSORS[data->kind]; i++) {
  1149. data->fan_act[i] = i2c_smbus_read_byte_data(client,
  1150. FSCHMD_REG_FAN_ACT[data->kind][i]);
  1151. data->fan_status[i] = i2c_smbus_read_byte_data(client,
  1152. FSCHMD_REG_FAN_STATE[data->kind][i]);
  1153. data->fan_ripple[i] = i2c_smbus_read_byte_data(client,
  1154. FSCHMD_REG_FAN_RIPPLE[data->kind][i]);
  1155. /* The fscpos third fan doesn't have a fan_min */
  1156. if (FSCHMD_REG_FAN_MIN[data->kind][i])
  1157. data->fan_min[i] = i2c_smbus_read_byte_data(
  1158. client,
  1159. FSCHMD_REG_FAN_MIN[data->kind][i]);
  1160. /* reset fan status if speed is back to > 0 */
  1161. if ((data->fan_status[i] & FSCHMD_FAN_ALARM) &&
  1162. data->fan_act[i])
  1163. i2c_smbus_write_byte_data(client,
  1164. FSCHMD_REG_FAN_STATE[data->kind][i],
  1165. data->fan_status[i]);
  1166. }
  1167. for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++)
  1168. data->volt[i] = i2c_smbus_read_byte_data(client,
  1169. FSCHMD_REG_VOLT[data->kind][i]);
  1170. data->last_updated = jiffies;
  1171. data->valid = 1;
  1172. }
  1173. mutex_unlock(&data->update_lock);
  1174. return data;
  1175. }
  1176. module_i2c_driver(fschmd_driver);
  1177. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  1178. MODULE_DESCRIPTION("FSC Poseidon, Hermes, Scylla, Heracles, Heimdall, Hades "
  1179. "and Syleus driver");
  1180. MODULE_LICENSE("GPL");