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 = 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");