fschmd.c 41 KB

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