g762.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /*
  2. * g762 - Driver for the Global Mixed-mode Technology Inc. fan speed
  3. * PWM controller chips from G762 family, i.e. G762 and G763
  4. *
  5. * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
  6. *
  7. * This work is based on a basic version for 2.6.31 kernel developed
  8. * by Olivier Mouchet for LaCie. Updates and correction have been
  9. * performed to run on recent kernels. Additional features, like the
  10. * ability to configure various characteristics via .dts file or
  11. * board init file have been added. Detailed datasheet on which this
  12. * development is based is available here:
  13. *
  14. * http://natisbad.org/NAS/refs/GMT_EDS-762_763-080710-0.2.pdf
  15. *
  16. * Headers from previous developments have been kept below:
  17. *
  18. * Copyright (c) 2009 LaCie
  19. *
  20. * Author: Olivier Mouchet <olivier.mouchet@gmail.com>
  21. *
  22. * based on g760a code written by Herbert Valerio Riedel <hvr@gnu.org>
  23. * Copyright (C) 2007 Herbert Valerio Riedel <hvr@gnu.org>
  24. *
  25. * g762: minimal datasheet available at:
  26. * http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf
  27. *
  28. * This program is free software; you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License as published by
  30. * the Free Software Foundation; either version 2 of the License, or
  31. * (at your option) any later version.
  32. *
  33. * This program is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. * GNU General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation.
  41. */
  42. #include <linux/device.h>
  43. #include <linux/module.h>
  44. #include <linux/init.h>
  45. #include <linux/jiffies.h>
  46. #include <linux/i2c.h>
  47. #include <linux/hwmon.h>
  48. #include <linux/hwmon-sysfs.h>
  49. #include <linux/err.h>
  50. #include <linux/mutex.h>
  51. #include <linux/kernel.h>
  52. #include <linux/clk.h>
  53. #include <linux/of.h>
  54. #include <linux/of_device.h>
  55. #include <linux/platform_data/g762.h>
  56. #define DRVNAME "g762"
  57. static const struct i2c_device_id g762_id[] = {
  58. { "g762", 0 },
  59. { "g763", 0 },
  60. { }
  61. };
  62. MODULE_DEVICE_TABLE(i2c, g762_id);
  63. enum g762_regs {
  64. G762_REG_SET_CNT = 0x00,
  65. G762_REG_ACT_CNT = 0x01,
  66. G762_REG_FAN_STA = 0x02,
  67. G762_REG_SET_OUT = 0x03,
  68. G762_REG_FAN_CMD1 = 0x04,
  69. G762_REG_FAN_CMD2 = 0x05,
  70. };
  71. /* Config register bits */
  72. #define G762_REG_FAN_CMD1_DET_FAN_FAIL 0x80 /* enable fan_fail signal */
  73. #define G762_REG_FAN_CMD1_DET_FAN_OOC 0x40 /* enable fan_out_of_control */
  74. #define G762_REG_FAN_CMD1_OUT_MODE 0x20 /* out mode: PWM or DC */
  75. #define G762_REG_FAN_CMD1_FAN_MODE 0x10 /* fan mode: closed/open-loop */
  76. #define G762_REG_FAN_CMD1_CLK_DIV_ID1 0x08 /* clock divisor value */
  77. #define G762_REG_FAN_CMD1_CLK_DIV_ID0 0x04
  78. #define G762_REG_FAN_CMD1_PWM_POLARITY 0x02 /* PWM polarity */
  79. #define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */
  80. #define G762_REG_FAN_CMD2_GEAR_MODE_1 0x08 /* fan gear mode */
  81. #define G762_REG_FAN_CMD2_GEAR_MODE_0 0x04
  82. #define G762_REG_FAN_CMD2_FAN_STARTV_1 0x02 /* fan startup voltage */
  83. #define G762_REG_FAN_CMD2_FAN_STARTV_0 0x01
  84. #define G762_REG_FAN_STA_FAIL 0x02 /* fan fail */
  85. #define G762_REG_FAN_STA_OOC 0x01 /* fan out of control */
  86. /* Config register values */
  87. #define G762_OUT_MODE_PWM 1
  88. #define G762_OUT_MODE_DC 0
  89. #define G762_FAN_MODE_CLOSED_LOOP 2
  90. #define G762_FAN_MODE_OPEN_LOOP 1
  91. #define G762_PWM_POLARITY_NEGATIVE 1
  92. #define G762_PWM_POLARITY_POSITIVE 0
  93. /* Register data is read (and cached) at most once per second. */
  94. #define G762_UPDATE_INTERVAL HZ
  95. /*
  96. * Extract pulse count per fan revolution value (2 or 4) from given
  97. * FAN_CMD1 register value.
  98. */
  99. #define G762_PULSE_FROM_REG(reg) \
  100. ((((reg) & G762_REG_FAN_CMD1_PULSE_PER_REV) + 1) << 1)
  101. /*
  102. * Extract fan clock divisor (1, 2, 4 or 8) from given FAN_CMD1
  103. * register value.
  104. */
  105. #define G762_CLKDIV_FROM_REG(reg) \
  106. (1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 | \
  107. G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2))
  108. /*
  109. * Extract fan gear mode multiplier value (0, 2 or 4) from given
  110. * FAN_CMD2 register value.
  111. */
  112. #define G762_GEARMULT_FROM_REG(reg) \
  113. (1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 | \
  114. G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
  115. struct g762_data {
  116. struct i2c_client *client;
  117. struct clk *clk;
  118. /* update mutex */
  119. struct mutex update_lock;
  120. /* board specific parameters. */
  121. u32 clk_freq;
  122. /* g762 register cache */
  123. bool valid;
  124. unsigned long last_updated; /* in jiffies */
  125. u8 set_cnt; /* controls fan rotation speed in closed-loop mode */
  126. u8 act_cnt; /* provides access to current fan RPM value */
  127. u8 fan_sta; /* bit 0: set when actual fan speed is more than
  128. * 25% outside requested fan speed
  129. * bit 1: set when no transition occurs on fan
  130. * pin for 0.7s
  131. */
  132. u8 set_out; /* controls fan rotation speed in open-loop mode */
  133. u8 fan_cmd1; /* 0: FG_PLS_ID0 FG pulses count per revolution
  134. * 0: 2 counts per revolution
  135. * 1: 4 counts per revolution
  136. * 1: PWM_POLARITY 1: negative_duty
  137. * 0: positive_duty
  138. * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1]
  139. * 00: Divide fan clock by 1
  140. * 01: Divide fan clock by 2
  141. * 10: Divide fan clock by 4
  142. * 11: Divide fan clock by 8
  143. * 4: FAN_MODE 1:closed-loop, 0:open-loop
  144. * 5: OUT_MODE 1:PWM, 0:DC
  145. * 6: DET_FAN_OOC enable "fan ooc" status
  146. * 7: DET_FAN_FAIL enable "fan fail" status
  147. */
  148. u8 fan_cmd2; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code
  149. * 2,3: FG_GEAR_MODE
  150. * 00: multiplier = 1
  151. * 01: multiplier = 2
  152. * 10: multiplier = 4
  153. * 4: Mask ALERT# (g763 only)
  154. */
  155. };
  156. /*
  157. * Convert count value from fan controller register (FAN_SET_CNT) into fan
  158. * speed RPM value. Note that the datasheet documents a basic formula;
  159. * influence of additional parameters (fan clock divisor, fan gear mode)
  160. * have been infered from examples in the datasheet and tests.
  161. */
  162. static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p,
  163. u8 clk_div, u8 gear_mult)
  164. {
  165. if (cnt == 0xff) /* setting cnt to 255 stops the fan */
  166. return 0;
  167. return (clk_freq * 30 * gear_mult) / ((cnt ? cnt : 1) * p * clk_div);
  168. }
  169. /*
  170. * Convert fan RPM value from sysfs into count value for fan controller
  171. * register (FAN_SET_CNT).
  172. */
  173. static inline unsigned char cnt_from_rpm(unsigned long rpm, u32 clk_freq, u16 p,
  174. u8 clk_div, u8 gear_mult)
  175. {
  176. unsigned long f1 = clk_freq * 30 * gear_mult;
  177. unsigned long f2 = p * clk_div;
  178. if (!rpm) /* to stop the fan, set cnt to 255 */
  179. return 0xff;
  180. rpm = clamp_val(rpm, f1 / (255 * f2), ULONG_MAX / f2);
  181. return DIV_ROUND_CLOSEST(f1, rpm * f2);
  182. }
  183. /* helper to grab and cache data, at most one time per second */
  184. static struct g762_data *g762_update_client(struct device *dev)
  185. {
  186. struct g762_data *data = dev_get_drvdata(dev);
  187. struct i2c_client *client = data->client;
  188. int ret = 0;
  189. mutex_lock(&data->update_lock);
  190. if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) &&
  191. likely(data->valid))
  192. goto out;
  193. ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT);
  194. if (ret < 0)
  195. goto out;
  196. data->set_cnt = ret;
  197. ret = i2c_smbus_read_byte_data(client, G762_REG_ACT_CNT);
  198. if (ret < 0)
  199. goto out;
  200. data->act_cnt = ret;
  201. ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_STA);
  202. if (ret < 0)
  203. goto out;
  204. data->fan_sta = ret;
  205. ret = i2c_smbus_read_byte_data(client, G762_REG_SET_OUT);
  206. if (ret < 0)
  207. goto out;
  208. data->set_out = ret;
  209. ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD1);
  210. if (ret < 0)
  211. goto out;
  212. data->fan_cmd1 = ret;
  213. ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD2);
  214. if (ret < 0)
  215. goto out;
  216. data->fan_cmd2 = ret;
  217. data->last_updated = jiffies;
  218. data->valid = true;
  219. out:
  220. mutex_unlock(&data->update_lock);
  221. if (ret < 0) /* upon error, encode it in return value */
  222. data = ERR_PTR(ret);
  223. return data;
  224. }
  225. /* helpers for writing hardware parameters */
  226. /*
  227. * Set input clock frequency received on CLK pin of the chip. Accepted values
  228. * are between 0 and 0xffffff. If zero is given, then default frequency
  229. * (32,768Hz) is used. Note that clock frequency is a characteristic of the
  230. * system but an internal parameter, i.e. value is not passed to the device.
  231. */
  232. static int do_set_clk_freq(struct device *dev, unsigned long val)
  233. {
  234. struct g762_data *data = dev_get_drvdata(dev);
  235. if (val > 0xffffff)
  236. return -EINVAL;
  237. if (!val)
  238. val = 32768;
  239. data->clk_freq = val;
  240. return 0;
  241. }
  242. /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
  243. static int do_set_pwm_mode(struct device *dev, unsigned long val)
  244. {
  245. struct g762_data *data = g762_update_client(dev);
  246. int ret;
  247. if (IS_ERR(data))
  248. return PTR_ERR(data);
  249. mutex_lock(&data->update_lock);
  250. switch (val) {
  251. case G762_OUT_MODE_PWM:
  252. data->fan_cmd1 |= G762_REG_FAN_CMD1_OUT_MODE;
  253. break;
  254. case G762_OUT_MODE_DC:
  255. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE;
  256. break;
  257. default:
  258. ret = -EINVAL;
  259. goto out;
  260. }
  261. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  262. data->fan_cmd1);
  263. data->valid = false;
  264. out:
  265. mutex_unlock(&data->update_lock);
  266. return ret;
  267. }
  268. /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
  269. static int do_set_fan_div(struct device *dev, unsigned long val)
  270. {
  271. struct g762_data *data = g762_update_client(dev);
  272. int ret;
  273. if (IS_ERR(data))
  274. return PTR_ERR(data);
  275. mutex_lock(&data->update_lock);
  276. switch (val) {
  277. case 1:
  278. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
  279. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
  280. break;
  281. case 2:
  282. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
  283. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
  284. break;
  285. case 4:
  286. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
  287. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
  288. break;
  289. case 8:
  290. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
  291. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
  292. break;
  293. default:
  294. ret = -EINVAL;
  295. goto out;
  296. }
  297. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  298. data->fan_cmd1);
  299. data->valid = false;
  300. out:
  301. mutex_unlock(&data->update_lock);
  302. return ret;
  303. }
  304. /* Set fan gear mode. Accepts either 0, 1 or 2. */
  305. static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
  306. {
  307. struct g762_data *data = g762_update_client(dev);
  308. int ret;
  309. if (IS_ERR(data))
  310. return PTR_ERR(data);
  311. mutex_lock(&data->update_lock);
  312. switch (val) {
  313. case 0:
  314. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
  315. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
  316. break;
  317. case 1:
  318. data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_0;
  319. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
  320. break;
  321. case 2:
  322. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
  323. data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_1;
  324. break;
  325. default:
  326. ret = -EINVAL;
  327. goto out;
  328. }
  329. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
  330. data->fan_cmd2);
  331. data->valid = false;
  332. out:
  333. mutex_unlock(&data->update_lock);
  334. return ret;
  335. }
  336. /* Set number of fan pulses per revolution. Accepts either 2 or 4. */
  337. static int do_set_fan_pulses(struct device *dev, unsigned long val)
  338. {
  339. struct g762_data *data = g762_update_client(dev);
  340. int ret;
  341. if (IS_ERR(data))
  342. return PTR_ERR(data);
  343. mutex_lock(&data->update_lock);
  344. switch (val) {
  345. case 2:
  346. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV;
  347. break;
  348. case 4:
  349. data->fan_cmd1 |= G762_REG_FAN_CMD1_PULSE_PER_REV;
  350. break;
  351. default:
  352. ret = -EINVAL;
  353. goto out;
  354. }
  355. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  356. data->fan_cmd1);
  357. data->valid = false;
  358. out:
  359. mutex_unlock(&data->update_lock);
  360. return ret;
  361. }
  362. /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
  363. static int do_set_pwm_enable(struct device *dev, unsigned long val)
  364. {
  365. struct g762_data *data = g762_update_client(dev);
  366. int ret;
  367. if (IS_ERR(data))
  368. return PTR_ERR(data);
  369. mutex_lock(&data->update_lock);
  370. switch (val) {
  371. case G762_FAN_MODE_CLOSED_LOOP:
  372. data->fan_cmd1 |= G762_REG_FAN_CMD1_FAN_MODE;
  373. break;
  374. case G762_FAN_MODE_OPEN_LOOP:
  375. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE;
  376. /*
  377. * BUG FIX: if SET_CNT register value is 255 then, for some
  378. * unknown reason, fan will not rotate as expected, no matter
  379. * the value of SET_OUT (to be specific, this seems to happen
  380. * only in PWM mode). To workaround this bug, we give SET_CNT
  381. * value of 254 if it is 255 when switching to open-loop.
  382. */
  383. if (data->set_cnt == 0xff)
  384. i2c_smbus_write_byte_data(data->client,
  385. G762_REG_SET_CNT, 254);
  386. break;
  387. default:
  388. ret = -EINVAL;
  389. goto out;
  390. }
  391. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  392. data->fan_cmd1);
  393. data->valid = false;
  394. out:
  395. mutex_unlock(&data->update_lock);
  396. return ret;
  397. }
  398. /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
  399. static int do_set_pwm_polarity(struct device *dev, unsigned long val)
  400. {
  401. struct g762_data *data = g762_update_client(dev);
  402. int ret;
  403. if (IS_ERR(data))
  404. return PTR_ERR(data);
  405. mutex_lock(&data->update_lock);
  406. switch (val) {
  407. case G762_PWM_POLARITY_POSITIVE:
  408. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY;
  409. break;
  410. case G762_PWM_POLARITY_NEGATIVE:
  411. data->fan_cmd1 |= G762_REG_FAN_CMD1_PWM_POLARITY;
  412. break;
  413. default:
  414. ret = -EINVAL;
  415. goto out;
  416. }
  417. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  418. data->fan_cmd1);
  419. data->valid = false;
  420. out:
  421. mutex_unlock(&data->update_lock);
  422. return ret;
  423. }
  424. /*
  425. * Set pwm value. Accepts values between 0 (stops the fan) and
  426. * 255 (full speed). This only makes sense in open-loop mode.
  427. */
  428. static int do_set_pwm(struct device *dev, unsigned long val)
  429. {
  430. struct g762_data *data = dev_get_drvdata(dev);
  431. struct i2c_client *client = data->client;
  432. int ret;
  433. if (val > 255)
  434. return -EINVAL;
  435. mutex_lock(&data->update_lock);
  436. ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val);
  437. data->valid = false;
  438. mutex_unlock(&data->update_lock);
  439. return ret;
  440. }
  441. /*
  442. * Set fan RPM value. Can be called both in closed and open-loop mode
  443. * but effect will only be seen after closed-loop mode is configured.
  444. */
  445. static int do_set_fan_target(struct device *dev, unsigned long val)
  446. {
  447. struct g762_data *data = g762_update_client(dev);
  448. int ret;
  449. if (IS_ERR(data))
  450. return PTR_ERR(data);
  451. mutex_lock(&data->update_lock);
  452. data->set_cnt = cnt_from_rpm(val, data->clk_freq,
  453. G762_PULSE_FROM_REG(data->fan_cmd1),
  454. G762_CLKDIV_FROM_REG(data->fan_cmd1),
  455. G762_GEARMULT_FROM_REG(data->fan_cmd2));
  456. ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT,
  457. data->set_cnt);
  458. data->valid = false;
  459. mutex_unlock(&data->update_lock);
  460. return ret;
  461. }
  462. /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
  463. static int do_set_fan_startv(struct device *dev, unsigned long val)
  464. {
  465. struct g762_data *data = g762_update_client(dev);
  466. int ret;
  467. if (IS_ERR(data))
  468. return PTR_ERR(data);
  469. mutex_lock(&data->update_lock);
  470. switch (val) {
  471. case 0:
  472. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
  473. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
  474. break;
  475. case 1:
  476. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
  477. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
  478. break;
  479. case 2:
  480. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
  481. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
  482. break;
  483. case 3:
  484. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
  485. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
  486. break;
  487. default:
  488. ret = -EINVAL;
  489. goto out;
  490. }
  491. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
  492. data->fan_cmd2);
  493. data->valid = false;
  494. out:
  495. mutex_unlock(&data->update_lock);
  496. return ret;
  497. }
  498. /*
  499. * Helper to import hardware characteristics from .dts file and push
  500. * those to the chip.
  501. */
  502. #ifdef CONFIG_OF
  503. static const struct of_device_id g762_dt_match[] = {
  504. { .compatible = "gmt,g762" },
  505. { .compatible = "gmt,g763" },
  506. { },
  507. };
  508. MODULE_DEVICE_TABLE(of, g762_dt_match);
  509. /*
  510. * Grab clock (a required property), enable it, get (fixed) clock frequency
  511. * and store it. Note: upon success, clock has been prepared and enabled; it
  512. * must later be unprepared and disabled (e.g. during module unloading) by a
  513. * call to g762_of_clock_disable(). Note that a reference to clock is kept
  514. * in our private data structure to be used in this function.
  515. */
  516. static void g762_of_clock_disable(void *data)
  517. {
  518. struct g762_data *g762 = data;
  519. clk_disable_unprepare(g762->clk);
  520. clk_put(g762->clk);
  521. }
  522. static int g762_of_clock_enable(struct i2c_client *client)
  523. {
  524. struct g762_data *data;
  525. unsigned long clk_freq;
  526. struct clk *clk;
  527. int ret;
  528. if (!client->dev.of_node)
  529. return 0;
  530. clk = of_clk_get(client->dev.of_node, 0);
  531. if (IS_ERR(clk)) {
  532. dev_err(&client->dev, "failed to get clock\n");
  533. return PTR_ERR(clk);
  534. }
  535. ret = clk_prepare_enable(clk);
  536. if (ret) {
  537. dev_err(&client->dev, "failed to enable clock\n");
  538. goto clk_put;
  539. }
  540. clk_freq = clk_get_rate(clk);
  541. ret = do_set_clk_freq(&client->dev, clk_freq);
  542. if (ret) {
  543. dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq);
  544. goto clk_unprep;
  545. }
  546. data = i2c_get_clientdata(client);
  547. data->clk = clk;
  548. devm_add_action(&client->dev, g762_of_clock_disable, data);
  549. return 0;
  550. clk_unprep:
  551. clk_disable_unprepare(clk);
  552. clk_put:
  553. clk_put(clk);
  554. return ret;
  555. }
  556. static int g762_of_prop_import_one(struct i2c_client *client,
  557. const char *pname,
  558. int (*psetter)(struct device *dev,
  559. unsigned long val))
  560. {
  561. int ret;
  562. u32 pval;
  563. if (of_property_read_u32(client->dev.of_node, pname, &pval))
  564. return 0;
  565. dev_dbg(&client->dev, "found %s (%d)\n", pname, pval);
  566. ret = (*psetter)(&client->dev, pval);
  567. if (ret)
  568. dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval);
  569. return ret;
  570. }
  571. static int g762_of_prop_import(struct i2c_client *client)
  572. {
  573. int ret;
  574. if (!client->dev.of_node)
  575. return 0;
  576. ret = g762_of_prop_import_one(client, "fan_gear_mode",
  577. do_set_fan_gear_mode);
  578. if (ret)
  579. return ret;
  580. ret = g762_of_prop_import_one(client, "pwm_polarity",
  581. do_set_pwm_polarity);
  582. if (ret)
  583. return ret;
  584. return g762_of_prop_import_one(client, "fan_startv",
  585. do_set_fan_startv);
  586. }
  587. #else
  588. static int g762_of_prop_import(struct i2c_client *client)
  589. {
  590. return 0;
  591. }
  592. static int g762_of_clock_enable(struct i2c_client *client)
  593. {
  594. return 0;
  595. }
  596. #endif
  597. /*
  598. * Helper to import hardware characteristics from .dts file and push
  599. * those to the chip.
  600. */
  601. static int g762_pdata_prop_import(struct i2c_client *client)
  602. {
  603. struct g762_platform_data *pdata = dev_get_platdata(&client->dev);
  604. int ret;
  605. if (!pdata)
  606. return 0;
  607. ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode);
  608. if (ret)
  609. return ret;
  610. ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity);
  611. if (ret)
  612. return ret;
  613. ret = do_set_fan_startv(&client->dev, pdata->fan_startv);
  614. if (ret)
  615. return ret;
  616. return do_set_clk_freq(&client->dev, pdata->clk_freq);
  617. }
  618. /*
  619. * sysfs attributes
  620. */
  621. /*
  622. * Read function for fan1_input sysfs file. Return current fan RPM value, or
  623. * 0 if fan is out of control.
  624. */
  625. static ssize_t fan1_input_show(struct device *dev,
  626. struct device_attribute *da, char *buf)
  627. {
  628. struct g762_data *data = g762_update_client(dev);
  629. unsigned int rpm = 0;
  630. if (IS_ERR(data))
  631. return PTR_ERR(data);
  632. mutex_lock(&data->update_lock);
  633. /* reverse logic: fan out of control reporting is enabled low */
  634. if (data->fan_sta & G762_REG_FAN_STA_OOC) {
  635. rpm = rpm_from_cnt(data->act_cnt, data->clk_freq,
  636. G762_PULSE_FROM_REG(data->fan_cmd1),
  637. G762_CLKDIV_FROM_REG(data->fan_cmd1),
  638. G762_GEARMULT_FROM_REG(data->fan_cmd2));
  639. }
  640. mutex_unlock(&data->update_lock);
  641. return sprintf(buf, "%u\n", rpm);
  642. }
  643. /*
  644. * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
  645. * control mode i.e. PWM (1) or DC (0).
  646. */
  647. static ssize_t pwm1_mode_show(struct device *dev, struct device_attribute *da,
  648. char *buf)
  649. {
  650. struct g762_data *data = g762_update_client(dev);
  651. if (IS_ERR(data))
  652. return PTR_ERR(data);
  653. return sprintf(buf, "%d\n",
  654. !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE));
  655. }
  656. static ssize_t pwm1_mode_store(struct device *dev,
  657. struct device_attribute *da, const char *buf,
  658. size_t count)
  659. {
  660. unsigned long val;
  661. int ret;
  662. if (kstrtoul(buf, 10, &val))
  663. return -EINVAL;
  664. ret = do_set_pwm_mode(dev, val);
  665. if (ret < 0)
  666. return ret;
  667. return count;
  668. }
  669. /*
  670. * Read and write functions for fan1_div sysfs file. Get and set fan
  671. * controller prescaler value
  672. */
  673. static ssize_t fan1_div_show(struct device *dev, struct device_attribute *da,
  674. char *buf)
  675. {
  676. struct g762_data *data = g762_update_client(dev);
  677. if (IS_ERR(data))
  678. return PTR_ERR(data);
  679. return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1));
  680. }
  681. static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
  682. const char *buf, size_t count)
  683. {
  684. unsigned long val;
  685. int ret;
  686. if (kstrtoul(buf, 10, &val))
  687. return -EINVAL;
  688. ret = do_set_fan_div(dev, val);
  689. if (ret < 0)
  690. return ret;
  691. return count;
  692. }
  693. /*
  694. * Read and write functions for fan1_pulses sysfs file. Get and set number
  695. * of tachometer pulses per fan revolution.
  696. */
  697. static ssize_t fan1_pulses_show(struct device *dev,
  698. struct device_attribute *da, char *buf)
  699. {
  700. struct g762_data *data = g762_update_client(dev);
  701. if (IS_ERR(data))
  702. return PTR_ERR(data);
  703. return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
  704. }
  705. static ssize_t fan1_pulses_store(struct device *dev,
  706. struct device_attribute *da, const char *buf,
  707. size_t count)
  708. {
  709. unsigned long val;
  710. int ret;
  711. if (kstrtoul(buf, 10, &val))
  712. return -EINVAL;
  713. ret = do_set_fan_pulses(dev, val);
  714. if (ret < 0)
  715. return ret;
  716. return count;
  717. }
  718. /*
  719. * Read and write functions for pwm1_enable. Get and set fan speed control mode
  720. * (i.e. closed or open-loop).
  721. *
  722. * Following documentation about hwmon's sysfs interface, a pwm1_enable node
  723. * should accept the following:
  724. *
  725. * 0 : no fan speed control (i.e. fan at full speed)
  726. * 1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
  727. * 2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
  728. *
  729. * but we do not accept 0 as this mode is not natively supported by the chip
  730. * and it is not emulated by g762 driver. -EINVAL is returned in this case.
  731. */
  732. static ssize_t pwm1_enable_show(struct device *dev,
  733. struct device_attribute *da, char *buf)
  734. {
  735. struct g762_data *data = g762_update_client(dev);
  736. if (IS_ERR(data))
  737. return PTR_ERR(data);
  738. return sprintf(buf, "%d\n",
  739. (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1);
  740. }
  741. static ssize_t pwm1_enable_store(struct device *dev,
  742. struct device_attribute *da, const char *buf,
  743. size_t count)
  744. {
  745. unsigned long val;
  746. int ret;
  747. if (kstrtoul(buf, 10, &val))
  748. return -EINVAL;
  749. ret = do_set_pwm_enable(dev, val);
  750. if (ret < 0)
  751. return ret;
  752. return count;
  753. }
  754. /*
  755. * Read and write functions for pwm1 sysfs file. Get and set pwm value
  756. * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
  757. * makes it run at full speed.
  758. */
  759. static ssize_t pwm1_show(struct device *dev, struct device_attribute *da,
  760. char *buf)
  761. {
  762. struct g762_data *data = g762_update_client(dev);
  763. if (IS_ERR(data))
  764. return PTR_ERR(data);
  765. return sprintf(buf, "%d\n", data->set_out);
  766. }
  767. static ssize_t pwm1_store(struct device *dev, struct device_attribute *da,
  768. const char *buf, size_t count)
  769. {
  770. unsigned long val;
  771. int ret;
  772. if (kstrtoul(buf, 10, &val))
  773. return -EINVAL;
  774. ret = do_set_pwm(dev, val);
  775. if (ret < 0)
  776. return ret;
  777. return count;
  778. }
  779. /*
  780. * Read and write function for fan1_target sysfs file. Get/set the fan speed in
  781. * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
  782. * the fan speed using pulses from fan tachometer.
  783. *
  784. * Refer to rpm_from_cnt() implementation above to get info about count number
  785. * calculation.
  786. *
  787. * Also note that due to rounding errors it is possible that you don't read
  788. * back exactly the value you have set.
  789. */
  790. static ssize_t fan1_target_show(struct device *dev,
  791. struct device_attribute *da, char *buf)
  792. {
  793. struct g762_data *data = g762_update_client(dev);
  794. unsigned int rpm;
  795. if (IS_ERR(data))
  796. return PTR_ERR(data);
  797. mutex_lock(&data->update_lock);
  798. rpm = rpm_from_cnt(data->set_cnt, data->clk_freq,
  799. G762_PULSE_FROM_REG(data->fan_cmd1),
  800. G762_CLKDIV_FROM_REG(data->fan_cmd1),
  801. G762_GEARMULT_FROM_REG(data->fan_cmd2));
  802. mutex_unlock(&data->update_lock);
  803. return sprintf(buf, "%u\n", rpm);
  804. }
  805. static ssize_t fan1_target_store(struct device *dev,
  806. struct device_attribute *da, const char *buf,
  807. size_t count)
  808. {
  809. unsigned long val;
  810. int ret;
  811. if (kstrtoul(buf, 10, &val))
  812. return -EINVAL;
  813. ret = do_set_fan_target(dev, val);
  814. if (ret < 0)
  815. return ret;
  816. return count;
  817. }
  818. /* read function for fan1_fault sysfs file. */
  819. static ssize_t fan1_fault_show(struct device *dev, struct device_attribute *da,
  820. char *buf)
  821. {
  822. struct g762_data *data = g762_update_client(dev);
  823. if (IS_ERR(data))
  824. return PTR_ERR(data);
  825. return sprintf(buf, "%u\n", !!(data->fan_sta & G762_REG_FAN_STA_FAIL));
  826. }
  827. /*
  828. * read function for fan1_alarm sysfs file. Note that OOC condition is
  829. * enabled low
  830. */
  831. static ssize_t fan1_alarm_show(struct device *dev,
  832. struct device_attribute *da, char *buf)
  833. {
  834. struct g762_data *data = g762_update_client(dev);
  835. if (IS_ERR(data))
  836. return PTR_ERR(data);
  837. return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC));
  838. }
  839. static DEVICE_ATTR_RW(pwm1);
  840. static DEVICE_ATTR_RW(pwm1_mode);
  841. static DEVICE_ATTR_RW(pwm1_enable);
  842. static DEVICE_ATTR_RO(fan1_input);
  843. static DEVICE_ATTR_RO(fan1_alarm);
  844. static DEVICE_ATTR_RO(fan1_fault);
  845. static DEVICE_ATTR_RW(fan1_target);
  846. static DEVICE_ATTR_RW(fan1_div);
  847. static DEVICE_ATTR_RW(fan1_pulses);
  848. /* Driver data */
  849. static struct attribute *g762_attrs[] = {
  850. &dev_attr_fan1_input.attr,
  851. &dev_attr_fan1_alarm.attr,
  852. &dev_attr_fan1_fault.attr,
  853. &dev_attr_fan1_target.attr,
  854. &dev_attr_fan1_div.attr,
  855. &dev_attr_fan1_pulses.attr,
  856. &dev_attr_pwm1.attr,
  857. &dev_attr_pwm1_mode.attr,
  858. &dev_attr_pwm1_enable.attr,
  859. NULL
  860. };
  861. ATTRIBUTE_GROUPS(g762);
  862. /*
  863. * Enable both fan failure detection and fan out of control protection. The
  864. * function does not protect change/access to data structure; it must thus
  865. * only be called during initialization.
  866. */
  867. static inline int g762_fan_init(struct device *dev)
  868. {
  869. struct g762_data *data = g762_update_client(dev);
  870. if (IS_ERR(data))
  871. return PTR_ERR(data);
  872. data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL;
  873. data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
  874. data->valid = false;
  875. return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  876. data->fan_cmd1);
  877. }
  878. static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
  879. {
  880. struct device *dev = &client->dev;
  881. struct device *hwmon_dev;
  882. struct g762_data *data;
  883. int ret;
  884. if (!i2c_check_functionality(client->adapter,
  885. I2C_FUNC_SMBUS_BYTE_DATA))
  886. return -ENODEV;
  887. data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
  888. if (!data)
  889. return -ENOMEM;
  890. i2c_set_clientdata(client, data);
  891. data->client = client;
  892. mutex_init(&data->update_lock);
  893. /* Enable fan failure detection and fan out of control protection */
  894. ret = g762_fan_init(dev);
  895. if (ret)
  896. return ret;
  897. /* Get configuration via DT ... */
  898. ret = g762_of_clock_enable(client);
  899. if (ret)
  900. return ret;
  901. ret = g762_of_prop_import(client);
  902. if (ret)
  903. return ret;
  904. /* ... or platform_data */
  905. ret = g762_pdata_prop_import(client);
  906. if (ret)
  907. return ret;
  908. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  909. data, g762_groups);
  910. return PTR_ERR_OR_ZERO(hwmon_dev);
  911. }
  912. static struct i2c_driver g762_driver = {
  913. .driver = {
  914. .name = DRVNAME,
  915. .of_match_table = of_match_ptr(g762_dt_match),
  916. },
  917. .probe = g762_probe,
  918. .id_table = g762_id,
  919. };
  920. module_i2c_driver(g762_driver);
  921. MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
  922. MODULE_DESCRIPTION("GMT G762/G763 driver");
  923. MODULE_LICENSE("GPL");