fschmd.c 39 KB

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