fschmd.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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 = SENSORS_LIMIT(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, "fan_div value %lu not supported. "
  402. "Choose one of 2, 4 or 8!\n", v);
  403. return -EINVAL;
  404. }
  405. mutex_lock(&data->update_lock);
  406. reg = i2c_smbus_read_byte_data(to_i2c_client(dev),
  407. FSCHMD_REG_FAN_RIPPLE[data->kind][index]);
  408. /* bits 2..7 reserved => mask with 0x03 */
  409. reg &= ~0x03;
  410. reg |= v;
  411. i2c_smbus_write_byte_data(to_i2c_client(dev),
  412. FSCHMD_REG_FAN_RIPPLE[data->kind][index], reg);
  413. data->fan_ripple[index] = reg;
  414. mutex_unlock(&data->update_lock);
  415. return count;
  416. }
  417. static ssize_t show_fan_alarm(struct device *dev,
  418. struct device_attribute *devattr, char *buf)
  419. {
  420. int index = to_sensor_dev_attr(devattr)->index;
  421. struct fschmd_data *data = fschmd_update_device(dev);
  422. if (data->fan_status[index] & FSCHMD_FAN_ALARM)
  423. return sprintf(buf, "1\n");
  424. else
  425. return sprintf(buf, "0\n");
  426. }
  427. static ssize_t show_fan_fault(struct device *dev,
  428. struct device_attribute *devattr, char *buf)
  429. {
  430. int index = to_sensor_dev_attr(devattr)->index;
  431. struct fschmd_data *data = fschmd_update_device(dev);
  432. if (data->fan_status[index] & FSCHMD_FAN_NOT_PRESENT)
  433. return sprintf(buf, "1\n");
  434. else
  435. return sprintf(buf, "0\n");
  436. }
  437. static ssize_t show_pwm_auto_point1_pwm(struct device *dev,
  438. struct device_attribute *devattr, char *buf)
  439. {
  440. int index = to_sensor_dev_attr(devattr)->index;
  441. struct fschmd_data *data = fschmd_update_device(dev);
  442. int val = data->fan_min[index];
  443. /* 0 = allow turning off (except on the syl), 1-255 = 50-100% */
  444. if (val || data->kind == fscsyl)
  445. val = val / 2 + 128;
  446. return sprintf(buf, "%d\n", val);
  447. }
  448. static ssize_t store_pwm_auto_point1_pwm(struct device *dev,
  449. struct device_attribute *devattr, const char *buf, size_t count)
  450. {
  451. int index = to_sensor_dev_attr(devattr)->index;
  452. struct fschmd_data *data = dev_get_drvdata(dev);
  453. unsigned long v;
  454. int err;
  455. err = kstrtoul(buf, 10, &v);
  456. if (err)
  457. return err;
  458. /* reg: 0 = allow turning off (except on the syl), 1-255 = 50-100% */
  459. if (v || data->kind == fscsyl) {
  460. v = SENSORS_LIMIT(v, 128, 255);
  461. v = (v - 128) * 2 + 1;
  462. }
  463. mutex_lock(&data->update_lock);
  464. i2c_smbus_write_byte_data(to_i2c_client(dev),
  465. FSCHMD_REG_FAN_MIN[data->kind][index], v);
  466. data->fan_min[index] = v;
  467. mutex_unlock(&data->update_lock);
  468. return count;
  469. }
  470. /*
  471. * The FSC hwmon family has the ability to force an attached alert led to flash
  472. * from software, we export this as an alert_led sysfs attr
  473. */
  474. static ssize_t show_alert_led(struct device *dev,
  475. struct device_attribute *devattr, char *buf)
  476. {
  477. struct fschmd_data *data = fschmd_update_device(dev);
  478. if (data->global_control & FSCHMD_CONTROL_ALERT_LED)
  479. return sprintf(buf, "1\n");
  480. else
  481. return sprintf(buf, "0\n");
  482. }
  483. static ssize_t store_alert_led(struct device *dev,
  484. struct device_attribute *devattr, const char *buf, size_t count)
  485. {
  486. u8 reg;
  487. struct fschmd_data *data = dev_get_drvdata(dev);
  488. unsigned long v;
  489. int err;
  490. err = kstrtoul(buf, 10, &v);
  491. if (err)
  492. return err;
  493. mutex_lock(&data->update_lock);
  494. reg = i2c_smbus_read_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL);
  495. if (v)
  496. reg |= FSCHMD_CONTROL_ALERT_LED;
  497. else
  498. reg &= ~FSCHMD_CONTROL_ALERT_LED;
  499. i2c_smbus_write_byte_data(to_i2c_client(dev), FSCHMD_REG_CONTROL, reg);
  500. data->global_control = reg;
  501. mutex_unlock(&data->update_lock);
  502. return count;
  503. }
  504. static DEVICE_ATTR(alert_led, 0644, show_alert_led, store_alert_led);
  505. static struct sensor_device_attribute fschmd_attr[] = {
  506. SENSOR_ATTR(in0_input, 0444, show_in_value, NULL, 0),
  507. SENSOR_ATTR(in1_input, 0444, show_in_value, NULL, 1),
  508. SENSOR_ATTR(in2_input, 0444, show_in_value, NULL, 2),
  509. SENSOR_ATTR(in3_input, 0444, show_in_value, NULL, 3),
  510. SENSOR_ATTR(in4_input, 0444, show_in_value, NULL, 4),
  511. SENSOR_ATTR(in5_input, 0444, show_in_value, NULL, 5),
  512. };
  513. static struct sensor_device_attribute fschmd_temp_attr[] = {
  514. SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0),
  515. SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0),
  516. SENSOR_ATTR(temp1_fault, 0444, show_temp_fault, NULL, 0),
  517. SENSOR_ATTR(temp1_alarm, 0444, show_temp_alarm, NULL, 0),
  518. SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1),
  519. SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1),
  520. SENSOR_ATTR(temp2_fault, 0444, show_temp_fault, NULL, 1),
  521. SENSOR_ATTR(temp2_alarm, 0444, show_temp_alarm, NULL, 1),
  522. SENSOR_ATTR(temp3_input, 0444, show_temp_value, NULL, 2),
  523. SENSOR_ATTR(temp3_max, 0644, show_temp_max, store_temp_max, 2),
  524. SENSOR_ATTR(temp3_fault, 0444, show_temp_fault, NULL, 2),
  525. SENSOR_ATTR(temp3_alarm, 0444, show_temp_alarm, NULL, 2),
  526. SENSOR_ATTR(temp4_input, 0444, show_temp_value, NULL, 3),
  527. SENSOR_ATTR(temp4_max, 0644, show_temp_max, store_temp_max, 3),
  528. SENSOR_ATTR(temp4_fault, 0444, show_temp_fault, NULL, 3),
  529. SENSOR_ATTR(temp4_alarm, 0444, show_temp_alarm, NULL, 3),
  530. SENSOR_ATTR(temp5_input, 0444, show_temp_value, NULL, 4),
  531. SENSOR_ATTR(temp5_max, 0644, show_temp_max, store_temp_max, 4),
  532. SENSOR_ATTR(temp5_fault, 0444, show_temp_fault, NULL, 4),
  533. SENSOR_ATTR(temp5_alarm, 0444, show_temp_alarm, NULL, 4),
  534. SENSOR_ATTR(temp6_input, 0444, show_temp_value, NULL, 5),
  535. SENSOR_ATTR(temp6_max, 0644, show_temp_max, store_temp_max, 5),
  536. SENSOR_ATTR(temp6_fault, 0444, show_temp_fault, NULL, 5),
  537. SENSOR_ATTR(temp6_alarm, 0444, show_temp_alarm, NULL, 5),
  538. SENSOR_ATTR(temp7_input, 0444, show_temp_value, NULL, 6),
  539. SENSOR_ATTR(temp7_max, 0644, show_temp_max, store_temp_max, 6),
  540. SENSOR_ATTR(temp7_fault, 0444, show_temp_fault, NULL, 6),
  541. SENSOR_ATTR(temp7_alarm, 0444, show_temp_alarm, NULL, 6),
  542. SENSOR_ATTR(temp8_input, 0444, show_temp_value, NULL, 7),
  543. SENSOR_ATTR(temp8_max, 0644, show_temp_max, store_temp_max, 7),
  544. SENSOR_ATTR(temp8_fault, 0444, show_temp_fault, NULL, 7),
  545. SENSOR_ATTR(temp8_alarm, 0444, show_temp_alarm, NULL, 7),
  546. SENSOR_ATTR(temp9_input, 0444, show_temp_value, NULL, 8),
  547. SENSOR_ATTR(temp9_max, 0644, show_temp_max, store_temp_max, 8),
  548. SENSOR_ATTR(temp9_fault, 0444, show_temp_fault, NULL, 8),
  549. SENSOR_ATTR(temp9_alarm, 0444, show_temp_alarm, NULL, 8),
  550. SENSOR_ATTR(temp10_input, 0444, show_temp_value, NULL, 9),
  551. SENSOR_ATTR(temp10_max, 0644, show_temp_max, store_temp_max, 9),
  552. SENSOR_ATTR(temp10_fault, 0444, show_temp_fault, NULL, 9),
  553. SENSOR_ATTR(temp10_alarm, 0444, show_temp_alarm, NULL, 9),
  554. SENSOR_ATTR(temp11_input, 0444, show_temp_value, NULL, 10),
  555. SENSOR_ATTR(temp11_max, 0644, show_temp_max, store_temp_max, 10),
  556. SENSOR_ATTR(temp11_fault, 0444, show_temp_fault, NULL, 10),
  557. SENSOR_ATTR(temp11_alarm, 0444, show_temp_alarm, NULL, 10),
  558. };
  559. static struct sensor_device_attribute fschmd_fan_attr[] = {
  560. SENSOR_ATTR(fan1_input, 0444, show_fan_value, NULL, 0),
  561. SENSOR_ATTR(fan1_div, 0644, show_fan_div, store_fan_div, 0),
  562. SENSOR_ATTR(fan1_alarm, 0444, show_fan_alarm, NULL, 0),
  563. SENSOR_ATTR(fan1_fault, 0444, show_fan_fault, NULL, 0),
  564. SENSOR_ATTR(pwm1_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  565. store_pwm_auto_point1_pwm, 0),
  566. SENSOR_ATTR(fan2_input, 0444, show_fan_value, NULL, 1),
  567. SENSOR_ATTR(fan2_div, 0644, show_fan_div, store_fan_div, 1),
  568. SENSOR_ATTR(fan2_alarm, 0444, show_fan_alarm, NULL, 1),
  569. SENSOR_ATTR(fan2_fault, 0444, show_fan_fault, NULL, 1),
  570. SENSOR_ATTR(pwm2_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  571. store_pwm_auto_point1_pwm, 1),
  572. SENSOR_ATTR(fan3_input, 0444, show_fan_value, NULL, 2),
  573. SENSOR_ATTR(fan3_div, 0644, show_fan_div, store_fan_div, 2),
  574. SENSOR_ATTR(fan3_alarm, 0444, show_fan_alarm, NULL, 2),
  575. SENSOR_ATTR(fan3_fault, 0444, show_fan_fault, NULL, 2),
  576. SENSOR_ATTR(pwm3_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  577. store_pwm_auto_point1_pwm, 2),
  578. SENSOR_ATTR(fan4_input, 0444, show_fan_value, NULL, 3),
  579. SENSOR_ATTR(fan4_div, 0644, show_fan_div, store_fan_div, 3),
  580. SENSOR_ATTR(fan4_alarm, 0444, show_fan_alarm, NULL, 3),
  581. SENSOR_ATTR(fan4_fault, 0444, show_fan_fault, NULL, 3),
  582. SENSOR_ATTR(pwm4_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  583. store_pwm_auto_point1_pwm, 3),
  584. SENSOR_ATTR(fan5_input, 0444, show_fan_value, NULL, 4),
  585. SENSOR_ATTR(fan5_div, 0644, show_fan_div, store_fan_div, 4),
  586. SENSOR_ATTR(fan5_alarm, 0444, show_fan_alarm, NULL, 4),
  587. SENSOR_ATTR(fan5_fault, 0444, show_fan_fault, NULL, 4),
  588. SENSOR_ATTR(pwm5_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  589. store_pwm_auto_point1_pwm, 4),
  590. SENSOR_ATTR(fan6_input, 0444, show_fan_value, NULL, 5),
  591. SENSOR_ATTR(fan6_div, 0644, show_fan_div, store_fan_div, 5),
  592. SENSOR_ATTR(fan6_alarm, 0444, show_fan_alarm, NULL, 5),
  593. SENSOR_ATTR(fan6_fault, 0444, show_fan_fault, NULL, 5),
  594. SENSOR_ATTR(pwm6_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  595. store_pwm_auto_point1_pwm, 5),
  596. SENSOR_ATTR(fan7_input, 0444, show_fan_value, NULL, 6),
  597. SENSOR_ATTR(fan7_div, 0644, show_fan_div, store_fan_div, 6),
  598. SENSOR_ATTR(fan7_alarm, 0444, show_fan_alarm, NULL, 6),
  599. SENSOR_ATTR(fan7_fault, 0444, show_fan_fault, NULL, 6),
  600. SENSOR_ATTR(pwm7_auto_point1_pwm, 0644, show_pwm_auto_point1_pwm,
  601. store_pwm_auto_point1_pwm, 6),
  602. };
  603. /*
  604. * Watchdog routines
  605. */
  606. static int watchdog_set_timeout(struct fschmd_data *data, int timeout)
  607. {
  608. int ret, resolution;
  609. int kind = data->kind + 1; /* 0-x array index -> 1-x module param */
  610. /* 2 second or 60 second resolution? */
  611. if (timeout <= 510 || kind == fscpos || kind == fscscy)
  612. resolution = 2;
  613. else
  614. resolution = 60;
  615. if (timeout < resolution || timeout > (resolution * 255))
  616. return -EINVAL;
  617. mutex_lock(&data->watchdog_lock);
  618. if (!data->client) {
  619. ret = -ENODEV;
  620. goto leave;
  621. }
  622. if (resolution == 2)
  623. data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_RESOLUTION;
  624. else
  625. data->watchdog_control |= FSCHMD_WDOG_CONTROL_RESOLUTION;
  626. data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
  627. /* Write new timeout value */
  628. i2c_smbus_write_byte_data(data->client,
  629. FSCHMD_REG_WDOG_PRESET[data->kind], data->watchdog_preset);
  630. /* Write new control register, do not trigger! */
  631. i2c_smbus_write_byte_data(data->client,
  632. FSCHMD_REG_WDOG_CONTROL[data->kind],
  633. data->watchdog_control & ~FSCHMD_WDOG_CONTROL_TRIGGER);
  634. ret = data->watchdog_preset * resolution;
  635. leave:
  636. mutex_unlock(&data->watchdog_lock);
  637. return ret;
  638. }
  639. static int watchdog_get_timeout(struct fschmd_data *data)
  640. {
  641. int timeout;
  642. mutex_lock(&data->watchdog_lock);
  643. if (data->watchdog_control & FSCHMD_WDOG_CONTROL_RESOLUTION)
  644. timeout = data->watchdog_preset * 60;
  645. else
  646. timeout = data->watchdog_preset * 2;
  647. mutex_unlock(&data->watchdog_lock);
  648. return timeout;
  649. }
  650. static int watchdog_trigger(struct fschmd_data *data)
  651. {
  652. int ret = 0;
  653. mutex_lock(&data->watchdog_lock);
  654. if (!data->client) {
  655. ret = -ENODEV;
  656. goto leave;
  657. }
  658. data->watchdog_control |= FSCHMD_WDOG_CONTROL_TRIGGER;
  659. i2c_smbus_write_byte_data(data->client,
  660. FSCHMD_REG_WDOG_CONTROL[data->kind],
  661. data->watchdog_control);
  662. leave:
  663. mutex_unlock(&data->watchdog_lock);
  664. return ret;
  665. }
  666. static int watchdog_stop(struct fschmd_data *data)
  667. {
  668. int ret = 0;
  669. mutex_lock(&data->watchdog_lock);
  670. if (!data->client) {
  671. ret = -ENODEV;
  672. goto leave;
  673. }
  674. data->watchdog_control &= ~FSCHMD_WDOG_CONTROL_STARTED;
  675. /*
  676. * Don't store the stop flag in our watchdog control register copy, as
  677. * its a write only bit (read always returns 0)
  678. */
  679. i2c_smbus_write_byte_data(data->client,
  680. FSCHMD_REG_WDOG_CONTROL[data->kind],
  681. data->watchdog_control | FSCHMD_WDOG_CONTROL_STOP);
  682. leave:
  683. mutex_unlock(&data->watchdog_lock);
  684. return ret;
  685. }
  686. static int watchdog_open(struct inode *inode, struct file *filp)
  687. {
  688. struct fschmd_data *pos, *data = NULL;
  689. int watchdog_is_open;
  690. /*
  691. * We get called from drivers/char/misc.c with misc_mtx hold, and we
  692. * call misc_register() from fschmd_probe() with watchdog_data_mutex
  693. * hold, as misc_register() takes the misc_mtx lock, this is a possible
  694. * deadlock, so we use mutex_trylock here.
  695. */
  696. if (!mutex_trylock(&watchdog_data_mutex))
  697. return -ERESTARTSYS;
  698. list_for_each_entry(pos, &watchdog_data_list, list) {
  699. if (pos->watchdog_miscdev.minor == iminor(inode)) {
  700. data = pos;
  701. break;
  702. }
  703. }
  704. /* Note we can never not have found data, so we don't check for this */
  705. watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
  706. if (!watchdog_is_open)
  707. kref_get(&data->kref);
  708. mutex_unlock(&watchdog_data_mutex);
  709. if (watchdog_is_open)
  710. return -EBUSY;
  711. /* Start the watchdog */
  712. watchdog_trigger(data);
  713. filp->private_data = data;
  714. return nonseekable_open(inode, filp);
  715. }
  716. static int watchdog_release(struct inode *inode, struct file *filp)
  717. {
  718. struct fschmd_data *data = filp->private_data;
  719. if (data->watchdog_expect_close) {
  720. watchdog_stop(data);
  721. data->watchdog_expect_close = 0;
  722. } else {
  723. watchdog_trigger(data);
  724. dev_crit(&data->client->dev,
  725. "unexpected close, not stopping watchdog!\n");
  726. }
  727. clear_bit(0, &data->watchdog_is_open);
  728. mutex_lock(&watchdog_data_mutex);
  729. kref_put(&data->kref, fschmd_release_resources);
  730. mutex_unlock(&watchdog_data_mutex);
  731. return 0;
  732. }
  733. static ssize_t watchdog_write(struct file *filp, const char __user *buf,
  734. size_t count, loff_t *offset)
  735. {
  736. int ret;
  737. struct fschmd_data *data = filp->private_data;
  738. if (count) {
  739. if (!nowayout) {
  740. size_t i;
  741. /* Clear it in case it was set with a previous write */
  742. data->watchdog_expect_close = 0;
  743. for (i = 0; i != count; i++) {
  744. char c;
  745. if (get_user(c, buf + i))
  746. return -EFAULT;
  747. if (c == 'V')
  748. data->watchdog_expect_close = 1;
  749. }
  750. }
  751. ret = watchdog_trigger(data);
  752. if (ret < 0)
  753. return ret;
  754. }
  755. return count;
  756. }
  757. static long watchdog_ioctl(struct file *filp, unsigned int cmd,
  758. unsigned long arg)
  759. {
  760. struct watchdog_info ident = {
  761. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  762. WDIOF_CARDRESET,
  763. .identity = "FSC watchdog"
  764. };
  765. int i, ret = 0;
  766. struct fschmd_data *data = filp->private_data;
  767. switch (cmd) {
  768. case WDIOC_GETSUPPORT:
  769. ident.firmware_version = data->revision;
  770. if (!nowayout)
  771. ident.options |= WDIOF_MAGICCLOSE;
  772. if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
  773. ret = -EFAULT;
  774. break;
  775. case WDIOC_GETSTATUS:
  776. ret = put_user(0, (int __user *)arg);
  777. break;
  778. case WDIOC_GETBOOTSTATUS:
  779. if (data->watchdog_state & FSCHMD_WDOG_STATE_CARDRESET)
  780. ret = put_user(WDIOF_CARDRESET, (int __user *)arg);
  781. else
  782. ret = put_user(0, (int __user *)arg);
  783. break;
  784. case WDIOC_KEEPALIVE:
  785. ret = watchdog_trigger(data);
  786. break;
  787. case WDIOC_GETTIMEOUT:
  788. i = watchdog_get_timeout(data);
  789. ret = put_user(i, (int __user *)arg);
  790. break;
  791. case WDIOC_SETTIMEOUT:
  792. if (get_user(i, (int __user *)arg)) {
  793. ret = -EFAULT;
  794. break;
  795. }
  796. ret = watchdog_set_timeout(data, i);
  797. if (ret > 0)
  798. ret = put_user(ret, (int __user *)arg);
  799. break;
  800. case WDIOC_SETOPTIONS:
  801. if (get_user(i, (int __user *)arg)) {
  802. ret = -EFAULT;
  803. break;
  804. }
  805. if (i & WDIOS_DISABLECARD)
  806. ret = watchdog_stop(data);
  807. else if (i & WDIOS_ENABLECARD)
  808. ret = watchdog_trigger(data);
  809. else
  810. ret = -EINVAL;
  811. break;
  812. default:
  813. ret = -ENOTTY;
  814. }
  815. return ret;
  816. }
  817. static const struct file_operations watchdog_fops = {
  818. .owner = THIS_MODULE,
  819. .llseek = no_llseek,
  820. .open = watchdog_open,
  821. .release = watchdog_release,
  822. .write = watchdog_write,
  823. .unlocked_ioctl = watchdog_ioctl,
  824. };
  825. /*
  826. * Detect, register, unregister and update device functions
  827. */
  828. /*
  829. * DMI decode routine to read voltage scaling factors from special DMI tables,
  830. * which are available on FSC machines with an fscher or later chip.
  831. */
  832. static void fschmd_dmi_decode(const struct dmi_header *header, void *dummy)
  833. {
  834. int i, mult[3] = { 0 }, offset[3] = { 0 }, vref = 0, found = 0;
  835. /*
  836. * dmi code ugliness, we get passed the address of the contents of
  837. * a complete DMI record, but in the form of a dmi_header pointer, in
  838. * reality this address holds header->length bytes of which the header
  839. * are the first 4 bytes
  840. */
  841. u8 *dmi_data = (u8 *)header;
  842. /* We are looking for OEM-specific type 185 */
  843. if (header->type != 185)
  844. return;
  845. /*
  846. * we are looking for what Siemens calls "subtype" 19, the subtype
  847. * is stored in byte 5 of the dmi block
  848. */
  849. if (header->length < 5 || dmi_data[4] != 19)
  850. return;
  851. /*
  852. * After the subtype comes 1 unknown byte and then blocks of 5 bytes,
  853. * consisting of what Siemens calls an "Entity" number, followed by
  854. * 2 16-bit words in LSB first order
  855. */
  856. for (i = 6; (i + 4) < header->length; i += 5) {
  857. /* entity 1 - 3: voltage multiplier and offset */
  858. if (dmi_data[i] >= 1 && dmi_data[i] <= 3) {
  859. /* Our in sensors order and the DMI order differ */
  860. const int shuffle[3] = { 1, 0, 2 };
  861. int in = shuffle[dmi_data[i] - 1];
  862. /* Check for twice the same entity */
  863. if (found & (1 << in))
  864. return;
  865. mult[in] = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
  866. offset[in] = dmi_data[i + 3] | (dmi_data[i + 4] << 8);
  867. found |= 1 << in;
  868. }
  869. /* entity 7: reference voltage */
  870. if (dmi_data[i] == 7) {
  871. /* Check for twice the same entity */
  872. if (found & 0x08)
  873. return;
  874. vref = dmi_data[i + 1] | (dmi_data[i + 2] << 8);
  875. found |= 0x08;
  876. }
  877. }
  878. if (found == 0x0F) {
  879. for (i = 0; i < 3; i++) {
  880. dmi_mult[i] = mult[i] * 10;
  881. dmi_offset[i] = offset[i] * 10;
  882. }
  883. /*
  884. * According to the docs there should be separate dmi entries
  885. * for the mult's and offsets of in3-5 of the syl, but on
  886. * my test machine these are not present
  887. */
  888. dmi_mult[3] = dmi_mult[2];
  889. dmi_mult[4] = dmi_mult[1];
  890. dmi_mult[5] = dmi_mult[2];
  891. dmi_offset[3] = dmi_offset[2];
  892. dmi_offset[4] = dmi_offset[1];
  893. dmi_offset[5] = dmi_offset[2];
  894. dmi_vref = vref;
  895. }
  896. }
  897. static int fschmd_detect(struct i2c_client *client,
  898. struct i2c_board_info *info)
  899. {
  900. enum chips kind;
  901. struct i2c_adapter *adapter = client->adapter;
  902. char id[4];
  903. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  904. return -ENODEV;
  905. /* Detect & Identify the chip */
  906. id[0] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_0);
  907. id[1] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_1);
  908. id[2] = i2c_smbus_read_byte_data(client, FSCHMD_REG_IDENT_2);
  909. id[3] = '\0';
  910. if (!strcmp(id, "PEG"))
  911. kind = fscpos;
  912. else if (!strcmp(id, "HER"))
  913. kind = fscher;
  914. else if (!strcmp(id, "SCY"))
  915. kind = fscscy;
  916. else if (!strcmp(id, "HRC"))
  917. kind = fschrc;
  918. else if (!strcmp(id, "HMD"))
  919. kind = fschmd;
  920. else if (!strcmp(id, "HDS"))
  921. kind = fschds;
  922. else if (!strcmp(id, "SYL"))
  923. kind = fscsyl;
  924. else
  925. return -ENODEV;
  926. strlcpy(info->type, fschmd_id[kind].name, I2C_NAME_SIZE);
  927. return 0;
  928. }
  929. static int fschmd_probe(struct i2c_client *client,
  930. const struct i2c_device_id *id)
  931. {
  932. struct fschmd_data *data;
  933. const char * const names[7] = { "Poseidon", "Hermes", "Scylla",
  934. "Heracles", "Heimdall", "Hades", "Syleus" };
  935. const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
  936. int i, err;
  937. enum chips kind = id->driver_data;
  938. data = kzalloc(sizeof(struct fschmd_data), GFP_KERNEL);
  939. if (!data)
  940. return -ENOMEM;
  941. i2c_set_clientdata(client, data);
  942. mutex_init(&data->update_lock);
  943. mutex_init(&data->watchdog_lock);
  944. INIT_LIST_HEAD(&data->list);
  945. kref_init(&data->kref);
  946. /*
  947. * Store client pointer in our data struct for watchdog usage
  948. * (where the client is found through a data ptr instead of the
  949. * otherway around)
  950. */
  951. data->client = client;
  952. data->kind = kind;
  953. if (kind == fscpos) {
  954. /*
  955. * The Poseidon has hardwired temp limits, fill these
  956. * in for the alarm resetting code
  957. */
  958. data->temp_max[0] = 70 + 128;
  959. data->temp_max[1] = 50 + 128;
  960. data->temp_max[2] = 50 + 128;
  961. }
  962. /* Read the special DMI table for fscher and newer chips */
  963. if ((kind == fscher || kind >= fschrc) && dmi_vref == -1) {
  964. dmi_walk(fschmd_dmi_decode, NULL);
  965. if (dmi_vref == -1) {
  966. dev_warn(&client->dev,
  967. "Couldn't get voltage scaling factors from "
  968. "BIOS DMI table, using builtin defaults\n");
  969. dmi_vref = 33;
  970. }
  971. }
  972. /* Read in some never changing registers */
  973. data->revision = i2c_smbus_read_byte_data(client, FSCHMD_REG_REVISION);
  974. data->global_control = i2c_smbus_read_byte_data(client,
  975. FSCHMD_REG_CONTROL);
  976. data->watchdog_control = i2c_smbus_read_byte_data(client,
  977. FSCHMD_REG_WDOG_CONTROL[data->kind]);
  978. data->watchdog_state = i2c_smbus_read_byte_data(client,
  979. FSCHMD_REG_WDOG_STATE[data->kind]);
  980. data->watchdog_preset = i2c_smbus_read_byte_data(client,
  981. FSCHMD_REG_WDOG_PRESET[data->kind]);
  982. err = device_create_file(&client->dev, &dev_attr_alert_led);
  983. if (err)
  984. goto exit_detach;
  985. for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++) {
  986. err = device_create_file(&client->dev,
  987. &fschmd_attr[i].dev_attr);
  988. if (err)
  989. goto exit_detach;
  990. }
  991. for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++) {
  992. /* Poseidon doesn't have TEMP_LIMIT registers */
  993. if (kind == fscpos && fschmd_temp_attr[i].dev_attr.show ==
  994. show_temp_max)
  995. continue;
  996. if (kind == fscsyl) {
  997. if (i % 4 == 0)
  998. data->temp_status[i / 4] =
  999. i2c_smbus_read_byte_data(client,
  1000. FSCHMD_REG_TEMP_STATE
  1001. [data->kind][i / 4]);
  1002. if (data->temp_status[i / 4] & FSCHMD_TEMP_DISABLED)
  1003. continue;
  1004. }
  1005. err = device_create_file(&client->dev,
  1006. &fschmd_temp_attr[i].dev_attr);
  1007. if (err)
  1008. goto exit_detach;
  1009. }
  1010. for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++) {
  1011. /* Poseidon doesn't have a FAN_MIN register for its 3rd fan */
  1012. if (kind == fscpos &&
  1013. !strcmp(fschmd_fan_attr[i].dev_attr.attr.name,
  1014. "pwm3_auto_point1_pwm"))
  1015. continue;
  1016. if (kind == fscsyl) {
  1017. if (i % 5 == 0)
  1018. data->fan_status[i / 5] =
  1019. i2c_smbus_read_byte_data(client,
  1020. FSCHMD_REG_FAN_STATE
  1021. [data->kind][i / 5]);
  1022. if (data->fan_status[i / 5] & FSCHMD_FAN_DISABLED)
  1023. continue;
  1024. }
  1025. err = device_create_file(&client->dev,
  1026. &fschmd_fan_attr[i].dev_attr);
  1027. if (err)
  1028. goto exit_detach;
  1029. }
  1030. data->hwmon_dev = hwmon_device_register(&client->dev);
  1031. if (IS_ERR(data->hwmon_dev)) {
  1032. err = PTR_ERR(data->hwmon_dev);
  1033. data->hwmon_dev = NULL;
  1034. goto exit_detach;
  1035. }
  1036. /*
  1037. * We take the data_mutex lock early so that watchdog_open() cannot
  1038. * run when misc_register() has completed, but we've not yet added
  1039. * our data to the watchdog_data_list (and set the default timeout)
  1040. */
  1041. mutex_lock(&watchdog_data_mutex);
  1042. for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
  1043. /* Register our watchdog part */
  1044. snprintf(data->watchdog_name, sizeof(data->watchdog_name),
  1045. "watchdog%c", (i == 0) ? '\0' : ('0' + i));
  1046. data->watchdog_miscdev.name = data->watchdog_name;
  1047. data->watchdog_miscdev.fops = &watchdog_fops;
  1048. data->watchdog_miscdev.minor = watchdog_minors[i];
  1049. err = misc_register(&data->watchdog_miscdev);
  1050. if (err == -EBUSY)
  1051. continue;
  1052. if (err) {
  1053. data->watchdog_miscdev.minor = 0;
  1054. dev_err(&client->dev,
  1055. "Registering watchdog chardev: %d\n", err);
  1056. break;
  1057. }
  1058. list_add(&data->list, &watchdog_data_list);
  1059. watchdog_set_timeout(data, 60);
  1060. dev_info(&client->dev,
  1061. "Registered watchdog chardev major 10, minor: %d\n",
  1062. watchdog_minors[i]);
  1063. break;
  1064. }
  1065. if (i == ARRAY_SIZE(watchdog_minors)) {
  1066. data->watchdog_miscdev.minor = 0;
  1067. dev_warn(&client->dev, "Couldn't register watchdog chardev "
  1068. "(due to no free minor)\n");
  1069. }
  1070. mutex_unlock(&watchdog_data_mutex);
  1071. dev_info(&client->dev, "Detected FSC %s chip, revision: %d\n",
  1072. names[data->kind], (int) data->revision);
  1073. return 0;
  1074. exit_detach:
  1075. fschmd_remove(client); /* will also free data for us */
  1076. return err;
  1077. }
  1078. static int fschmd_remove(struct i2c_client *client)
  1079. {
  1080. struct fschmd_data *data = i2c_get_clientdata(client);
  1081. int i;
  1082. /* Unregister the watchdog (if registered) */
  1083. if (data->watchdog_miscdev.minor) {
  1084. misc_deregister(&data->watchdog_miscdev);
  1085. if (data->watchdog_is_open) {
  1086. dev_warn(&client->dev,
  1087. "i2c client detached with watchdog open! "
  1088. "Stopping watchdog.\n");
  1089. watchdog_stop(data);
  1090. }
  1091. mutex_lock(&watchdog_data_mutex);
  1092. list_del(&data->list);
  1093. mutex_unlock(&watchdog_data_mutex);
  1094. /* Tell the watchdog code the client is gone */
  1095. mutex_lock(&data->watchdog_lock);
  1096. data->client = NULL;
  1097. mutex_unlock(&data->watchdog_lock);
  1098. }
  1099. /*
  1100. * Check if registered in case we're called from fschmd_detect
  1101. * to cleanup after an error
  1102. */
  1103. if (data->hwmon_dev)
  1104. hwmon_device_unregister(data->hwmon_dev);
  1105. device_remove_file(&client->dev, &dev_attr_alert_led);
  1106. for (i = 0; i < (FSCHMD_NO_VOLT_SENSORS[data->kind]); i++)
  1107. device_remove_file(&client->dev, &fschmd_attr[i].dev_attr);
  1108. for (i = 0; i < (FSCHMD_NO_TEMP_SENSORS[data->kind] * 4); i++)
  1109. device_remove_file(&client->dev,
  1110. &fschmd_temp_attr[i].dev_attr);
  1111. for (i = 0; i < (FSCHMD_NO_FAN_SENSORS[data->kind] * 5); i++)
  1112. device_remove_file(&client->dev,
  1113. &fschmd_fan_attr[i].dev_attr);
  1114. mutex_lock(&watchdog_data_mutex);
  1115. kref_put(&data->kref, fschmd_release_resources);
  1116. mutex_unlock(&watchdog_data_mutex);
  1117. return 0;
  1118. }
  1119. static struct fschmd_data *fschmd_update_device(struct device *dev)
  1120. {
  1121. struct i2c_client *client = to_i2c_client(dev);
  1122. struct fschmd_data *data = i2c_get_clientdata(client);
  1123. int i;
  1124. mutex_lock(&data->update_lock);
  1125. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  1126. for (i = 0; i < FSCHMD_NO_TEMP_SENSORS[data->kind]; i++) {
  1127. data->temp_act[i] = i2c_smbus_read_byte_data(client,
  1128. FSCHMD_REG_TEMP_ACT[data->kind][i]);
  1129. data->temp_status[i] = i2c_smbus_read_byte_data(client,
  1130. FSCHMD_REG_TEMP_STATE[data->kind][i]);
  1131. /* The fscpos doesn't have TEMP_LIMIT registers */
  1132. if (FSCHMD_REG_TEMP_LIMIT[data->kind][i])
  1133. data->temp_max[i] = i2c_smbus_read_byte_data(
  1134. client,
  1135. FSCHMD_REG_TEMP_LIMIT[data->kind][i]);
  1136. /*
  1137. * reset alarm if the alarm condition is gone,
  1138. * the chip doesn't do this itself
  1139. */
  1140. if ((data->temp_status[i] & FSCHMD_TEMP_ALARM_MASK) ==
  1141. FSCHMD_TEMP_ALARM_MASK &&
  1142. data->temp_act[i] < data->temp_max[i])
  1143. i2c_smbus_write_byte_data(client,
  1144. FSCHMD_REG_TEMP_STATE[data->kind][i],
  1145. data->temp_status[i]);
  1146. }
  1147. for (i = 0; i < FSCHMD_NO_FAN_SENSORS[data->kind]; i++) {
  1148. data->fan_act[i] = i2c_smbus_read_byte_data(client,
  1149. FSCHMD_REG_FAN_ACT[data->kind][i]);
  1150. data->fan_status[i] = i2c_smbus_read_byte_data(client,
  1151. FSCHMD_REG_FAN_STATE[data->kind][i]);
  1152. data->fan_ripple[i] = i2c_smbus_read_byte_data(client,
  1153. FSCHMD_REG_FAN_RIPPLE[data->kind][i]);
  1154. /* The fscpos third fan doesn't have a fan_min */
  1155. if (FSCHMD_REG_FAN_MIN[data->kind][i])
  1156. data->fan_min[i] = i2c_smbus_read_byte_data(
  1157. client,
  1158. FSCHMD_REG_FAN_MIN[data->kind][i]);
  1159. /* reset fan status if speed is back to > 0 */
  1160. if ((data->fan_status[i] & FSCHMD_FAN_ALARM) &&
  1161. data->fan_act[i])
  1162. i2c_smbus_write_byte_data(client,
  1163. FSCHMD_REG_FAN_STATE[data->kind][i],
  1164. data->fan_status[i]);
  1165. }
  1166. for (i = 0; i < FSCHMD_NO_VOLT_SENSORS[data->kind]; i++)
  1167. data->volt[i] = i2c_smbus_read_byte_data(client,
  1168. FSCHMD_REG_VOLT[data->kind][i]);
  1169. data->last_updated = jiffies;
  1170. data->valid = 1;
  1171. }
  1172. mutex_unlock(&data->update_lock);
  1173. return data;
  1174. }
  1175. module_i2c_driver(fschmd_driver);
  1176. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  1177. MODULE_DESCRIPTION("FSC Poseidon, Hermes, Scylla, Heracles, Heimdall, Hades "
  1178. "and Syleus driver");
  1179. MODULE_LICENSE("GPL");