g762.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 device *hwmon_dev;
  117. struct i2c_client *client;
  118. struct clk *clk;
  119. /* update mutex */
  120. struct mutex update_lock;
  121. /* board specific parameters. */
  122. u32 clk_freq;
  123. /* g762 register cache */
  124. bool valid;
  125. unsigned long last_updated; /* in jiffies */
  126. u8 set_cnt; /* controls fan rotation speed in closed-loop mode */
  127. u8 act_cnt; /* provides access to current fan RPM value */
  128. u8 fan_sta; /* bit 0: set when actual fan speed is more than
  129. * 25% outside requested fan speed
  130. * bit 1: set when no transition occurs on fan
  131. * pin for 0.7s
  132. */
  133. u8 set_out; /* controls fan rotation speed in open-loop mode */
  134. u8 fan_cmd1; /* 0: FG_PLS_ID0 FG pulses count per revolution
  135. * 0: 2 counts per revolution
  136. * 1: 4 counts per revolution
  137. * 1: PWM_POLARITY 1: negative_duty
  138. * 0: positive_duty
  139. * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1]
  140. * 00: Divide fan clock by 1
  141. * 01: Divide fan clock by 2
  142. * 10: Divide fan clock by 4
  143. * 11: Divide fan clock by 8
  144. * 4: FAN_MODE 1:closed-loop, 0:open-loop
  145. * 5: OUT_MODE 1:PWM, 0:DC
  146. * 6: DET_FAN_OOC enable "fan ooc" status
  147. * 7: DET_FAN_FAIL enable "fan fail" status
  148. */
  149. u8 fan_cmd2; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code
  150. * 2,3: FG_GEAR_MODE
  151. * 00: multiplier = 1
  152. * 01: multiplier = 2
  153. * 10: multiplier = 4
  154. * 4: Mask ALERT# (g763 only)
  155. */
  156. };
  157. /*
  158. * Convert count value from fan controller register (FAN_SET_CNT) into fan
  159. * speed RPM value. Note that the datasheet documents a basic formula;
  160. * influence of additional parameters (fan clock divisor, fan gear mode)
  161. * have been infered from examples in the datasheet and tests.
  162. */
  163. static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p,
  164. u8 clk_div, u8 gear_mult)
  165. {
  166. if (cnt == 0xff) /* setting cnt to 255 stops the fan */
  167. return 0;
  168. return (clk_freq * 30 * gear_mult) / ((cnt ? cnt : 1) * p * clk_div);
  169. }
  170. /*
  171. * Convert fan RPM value from sysfs into count value for fan controller
  172. * register (FAN_SET_CNT).
  173. */
  174. static inline unsigned char cnt_from_rpm(u32 rpm, u32 clk_freq, u16 p,
  175. u8 clk_div, u8 gear_mult)
  176. {
  177. if (!rpm) /* to stop the fan, set cnt to 255 */
  178. return 0xff;
  179. return clamp_val(((clk_freq * 30 * gear_mult) / (rpm * p * clk_div)),
  180. 0, 255);
  181. }
  182. /* helper to grab and cache data, at most one time per second */
  183. static struct g762_data *g762_update_client(struct device *dev)
  184. {
  185. struct g762_data *data = dev_get_drvdata(dev);
  186. struct i2c_client *client = data->client;
  187. int ret = 0;
  188. mutex_lock(&data->update_lock);
  189. if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) &&
  190. likely(data->valid))
  191. goto out;
  192. ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT);
  193. if (ret < 0)
  194. goto out;
  195. data->set_cnt = ret;
  196. ret = i2c_smbus_read_byte_data(client, G762_REG_ACT_CNT);
  197. if (ret < 0)
  198. goto out;
  199. data->act_cnt = ret;
  200. ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_STA);
  201. if (ret < 0)
  202. goto out;
  203. data->fan_sta = ret;
  204. ret = i2c_smbus_read_byte_data(client, G762_REG_SET_OUT);
  205. if (ret < 0)
  206. goto out;
  207. data->set_out = ret;
  208. ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD1);
  209. if (ret < 0)
  210. goto out;
  211. data->fan_cmd1 = ret;
  212. ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD2);
  213. if (ret < 0)
  214. goto out;
  215. data->fan_cmd2 = ret;
  216. data->last_updated = jiffies;
  217. data->valid = true;
  218. out:
  219. mutex_unlock(&data->update_lock);
  220. if (ret < 0) /* upon error, encode it in return value */
  221. data = ERR_PTR(ret);
  222. return data;
  223. }
  224. /* helpers for writing hardware parameters */
  225. /*
  226. * Set input clock frequency received on CLK pin of the chip. Accepted values
  227. * are between 0 and 0xffffff. If zero is given, then default frequency
  228. * (32,768Hz) is used. Note that clock frequency is a characteristic of the
  229. * system but an internal parameter, i.e. value is not passed to the device.
  230. */
  231. static int do_set_clk_freq(struct device *dev, unsigned long val)
  232. {
  233. struct g762_data *data = dev_get_drvdata(dev);
  234. if (val > 0xffffff)
  235. return -EINVAL;
  236. if (!val)
  237. val = 32768;
  238. data->clk_freq = val;
  239. return 0;
  240. }
  241. /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
  242. static int do_set_pwm_mode(struct device *dev, unsigned long val)
  243. {
  244. struct g762_data *data = g762_update_client(dev);
  245. int ret;
  246. if (IS_ERR(data))
  247. return PTR_ERR(data);
  248. mutex_lock(&data->update_lock);
  249. switch (val) {
  250. case G762_OUT_MODE_PWM:
  251. data->fan_cmd1 |= G762_REG_FAN_CMD1_OUT_MODE;
  252. break;
  253. case G762_OUT_MODE_DC:
  254. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE;
  255. break;
  256. default:
  257. ret = -EINVAL;
  258. goto out;
  259. }
  260. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  261. data->fan_cmd1);
  262. data->valid = false;
  263. out:
  264. mutex_unlock(&data->update_lock);
  265. return ret;
  266. }
  267. /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
  268. static int do_set_fan_div(struct device *dev, unsigned long val)
  269. {
  270. struct g762_data *data = g762_update_client(dev);
  271. int ret;
  272. if (IS_ERR(data))
  273. return PTR_ERR(data);
  274. mutex_lock(&data->update_lock);
  275. switch (val) {
  276. case 1:
  277. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
  278. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
  279. break;
  280. case 2:
  281. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
  282. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
  283. break;
  284. case 4:
  285. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
  286. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
  287. break;
  288. case 8:
  289. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
  290. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
  291. break;
  292. default:
  293. ret = -EINVAL;
  294. goto out;
  295. }
  296. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  297. data->fan_cmd1);
  298. data->valid = false;
  299. out:
  300. mutex_unlock(&data->update_lock);
  301. return ret;
  302. }
  303. /* Set fan gear mode. Accepts either 0, 1 or 2. */
  304. static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
  305. {
  306. struct g762_data *data = g762_update_client(dev);
  307. int ret;
  308. if (IS_ERR(data))
  309. return PTR_ERR(data);
  310. mutex_lock(&data->update_lock);
  311. switch (val) {
  312. case 0:
  313. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
  314. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
  315. break;
  316. case 1:
  317. data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_0;
  318. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
  319. break;
  320. case 2:
  321. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
  322. data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_1;
  323. break;
  324. default:
  325. ret = -EINVAL;
  326. goto out;
  327. }
  328. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
  329. data->fan_cmd2);
  330. data->valid = false;
  331. out:
  332. mutex_unlock(&data->update_lock);
  333. return ret;
  334. }
  335. /* Set number of fan pulses per revolution. Accepts either 2 or 4. */
  336. static int do_set_fan_pulses(struct device *dev, unsigned long val)
  337. {
  338. struct g762_data *data = g762_update_client(dev);
  339. int ret;
  340. if (IS_ERR(data))
  341. return PTR_ERR(data);
  342. mutex_lock(&data->update_lock);
  343. switch (val) {
  344. case 2:
  345. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV;
  346. break;
  347. case 4:
  348. data->fan_cmd1 |= G762_REG_FAN_CMD1_PULSE_PER_REV;
  349. break;
  350. default:
  351. ret = -EINVAL;
  352. goto out;
  353. }
  354. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  355. data->fan_cmd1);
  356. data->valid = false;
  357. out:
  358. mutex_unlock(&data->update_lock);
  359. return ret;
  360. }
  361. /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
  362. static int do_set_pwm_enable(struct device *dev, unsigned long val)
  363. {
  364. struct g762_data *data = g762_update_client(dev);
  365. int ret;
  366. if (IS_ERR(data))
  367. return PTR_ERR(data);
  368. mutex_lock(&data->update_lock);
  369. switch (val) {
  370. case G762_FAN_MODE_CLOSED_LOOP:
  371. data->fan_cmd1 |= G762_REG_FAN_CMD1_FAN_MODE;
  372. break;
  373. case G762_FAN_MODE_OPEN_LOOP:
  374. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE;
  375. /*
  376. * BUG FIX: if SET_CNT register value is 255 then, for some
  377. * unknown reason, fan will not rotate as expected, no matter
  378. * the value of SET_OUT (to be specific, this seems to happen
  379. * only in PWM mode). To workaround this bug, we give SET_CNT
  380. * value of 254 if it is 255 when switching to open-loop.
  381. */
  382. if (data->set_cnt == 0xff)
  383. i2c_smbus_write_byte_data(data->client,
  384. G762_REG_SET_CNT, 254);
  385. break;
  386. default:
  387. ret = -EINVAL;
  388. goto out;
  389. }
  390. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  391. data->fan_cmd1);
  392. data->valid = false;
  393. out:
  394. mutex_unlock(&data->update_lock);
  395. return ret;
  396. }
  397. /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
  398. static int do_set_pwm_polarity(struct device *dev, unsigned long val)
  399. {
  400. struct g762_data *data = g762_update_client(dev);
  401. int ret;
  402. if (IS_ERR(data))
  403. return PTR_ERR(data);
  404. mutex_lock(&data->update_lock);
  405. switch (val) {
  406. case G762_PWM_POLARITY_POSITIVE:
  407. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY;
  408. break;
  409. case G762_PWM_POLARITY_NEGATIVE:
  410. data->fan_cmd1 |= G762_REG_FAN_CMD1_PWM_POLARITY;
  411. break;
  412. default:
  413. ret = -EINVAL;
  414. goto out;
  415. }
  416. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  417. data->fan_cmd1);
  418. data->valid = false;
  419. out:
  420. mutex_unlock(&data->update_lock);
  421. return ret;
  422. }
  423. /*
  424. * Set pwm value. Accepts values between 0 (stops the fan) and
  425. * 255 (full speed). This only makes sense in open-loop mode.
  426. */
  427. static int do_set_pwm(struct device *dev, unsigned long val)
  428. {
  429. struct g762_data *data = dev_get_drvdata(dev);
  430. struct i2c_client *client = data->client;
  431. int ret;
  432. if (val > 255)
  433. return -EINVAL;
  434. mutex_lock(&data->update_lock);
  435. ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val);
  436. data->valid = false;
  437. mutex_unlock(&data->update_lock);
  438. return ret;
  439. }
  440. /*
  441. * Set fan RPM value. Can be called both in closed and open-loop mode
  442. * but effect will only be seen after closed-loop mode is configured.
  443. */
  444. static int do_set_fan_target(struct device *dev, unsigned long val)
  445. {
  446. struct g762_data *data = g762_update_client(dev);
  447. int ret;
  448. if (IS_ERR(data))
  449. return PTR_ERR(data);
  450. mutex_lock(&data->update_lock);
  451. data->set_cnt = cnt_from_rpm(val, data->clk_freq,
  452. G762_PULSE_FROM_REG(data->fan_cmd1),
  453. G762_CLKDIV_FROM_REG(data->fan_cmd1),
  454. G762_GEARMULT_FROM_REG(data->fan_cmd2));
  455. ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT,
  456. data->set_cnt);
  457. data->valid = false;
  458. mutex_unlock(&data->update_lock);
  459. return ret;
  460. }
  461. /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
  462. static int do_set_fan_startv(struct device *dev, unsigned long val)
  463. {
  464. struct g762_data *data = g762_update_client(dev);
  465. int ret;
  466. if (IS_ERR(data))
  467. return PTR_ERR(data);
  468. mutex_lock(&data->update_lock);
  469. switch (val) {
  470. case 0:
  471. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
  472. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
  473. break;
  474. case 1:
  475. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
  476. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
  477. break;
  478. case 2:
  479. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
  480. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
  481. break;
  482. case 3:
  483. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
  484. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
  485. break;
  486. default:
  487. ret = -EINVAL;
  488. goto out;
  489. }
  490. ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
  491. data->fan_cmd2);
  492. data->valid = false;
  493. out:
  494. mutex_unlock(&data->update_lock);
  495. return ret;
  496. }
  497. /*
  498. * Helper to import hardware characteristics from .dts file and push
  499. * those to the chip.
  500. */
  501. #ifdef CONFIG_OF
  502. static const struct of_device_id g762_dt_match[] = {
  503. { .compatible = "gmt,g762" },
  504. { .compatible = "gmt,g763" },
  505. { },
  506. };
  507. MODULE_DEVICE_TABLE(of, g762_dt_match);
  508. /*
  509. * Grab clock (a required property), enable it, get (fixed) clock frequency
  510. * and store it. Note: upon success, clock has been prepared and enabled; it
  511. * must later be unprepared and disabled (e.g. during module unloading) by a
  512. * call to g762_of_clock_disable(). Note that a reference to clock is kept
  513. * in our private data structure to be used in this function.
  514. */
  515. static int g762_of_clock_enable(struct i2c_client *client)
  516. {
  517. struct g762_data *data;
  518. unsigned long clk_freq;
  519. struct clk *clk;
  520. int ret;
  521. if (!client->dev.of_node)
  522. return 0;
  523. clk = of_clk_get(client->dev.of_node, 0);
  524. if (IS_ERR(clk)) {
  525. dev_err(&client->dev, "failed to get clock\n");
  526. return PTR_ERR(clk);
  527. }
  528. ret = clk_prepare_enable(clk);
  529. if (ret) {
  530. dev_err(&client->dev, "failed to enable clock\n");
  531. goto clk_put;
  532. }
  533. clk_freq = clk_get_rate(clk);
  534. ret = do_set_clk_freq(&client->dev, clk_freq);
  535. if (ret) {
  536. dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq);
  537. goto clk_unprep;
  538. }
  539. data = i2c_get_clientdata(client);
  540. data->clk = clk;
  541. return 0;
  542. clk_unprep:
  543. clk_disable_unprepare(clk);
  544. clk_put:
  545. clk_put(clk);
  546. return ret;
  547. }
  548. static void g762_of_clock_disable(struct i2c_client *client)
  549. {
  550. struct g762_data *data = i2c_get_clientdata(client);
  551. if (!data->clk)
  552. return;
  553. clk_disable_unprepare(data->clk);
  554. clk_put(data->clk);
  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. static void g762_of_clock_disable(struct i2c_client *client) { }
  597. #endif
  598. /*
  599. * Helper to import hardware characteristics from .dts file and push
  600. * those to the chip.
  601. */
  602. static int g762_pdata_prop_import(struct i2c_client *client)
  603. {
  604. struct g762_platform_data *pdata = dev_get_platdata(&client->dev);
  605. int ret;
  606. if (!pdata)
  607. return 0;
  608. ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode);
  609. if (ret)
  610. return ret;
  611. ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity);
  612. if (ret)
  613. return ret;
  614. ret = do_set_fan_startv(&client->dev, pdata->fan_startv);
  615. if (ret)
  616. return ret;
  617. return do_set_clk_freq(&client->dev, pdata->clk_freq);
  618. }
  619. /*
  620. * sysfs attributes
  621. */
  622. /*
  623. * Read function for fan1_input sysfs file. Return current fan RPM value, or
  624. * 0 if fan is out of control.
  625. */
  626. static ssize_t get_fan_rpm(struct device *dev, struct device_attribute *da,
  627. char *buf)
  628. {
  629. struct g762_data *data = g762_update_client(dev);
  630. unsigned int rpm = 0;
  631. if (IS_ERR(data))
  632. return PTR_ERR(data);
  633. mutex_lock(&data->update_lock);
  634. /* reverse logic: fan out of control reporting is enabled low */
  635. if (data->fan_sta & G762_REG_FAN_STA_OOC) {
  636. rpm = rpm_from_cnt(data->act_cnt, data->clk_freq,
  637. G762_PULSE_FROM_REG(data->fan_cmd1),
  638. G762_CLKDIV_FROM_REG(data->fan_cmd1),
  639. G762_GEARMULT_FROM_REG(data->fan_cmd2));
  640. }
  641. mutex_unlock(&data->update_lock);
  642. return sprintf(buf, "%u\n", rpm);
  643. }
  644. /*
  645. * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
  646. * control mode i.e. PWM (1) or DC (0).
  647. */
  648. static ssize_t get_pwm_mode(struct device *dev, struct device_attribute *da,
  649. char *buf)
  650. {
  651. struct g762_data *data = g762_update_client(dev);
  652. if (IS_ERR(data))
  653. return PTR_ERR(data);
  654. return sprintf(buf, "%d\n",
  655. !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE));
  656. }
  657. static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *da,
  658. const char *buf, 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 get_fan_div(struct device *dev,
  674. struct device_attribute *da, 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 set_fan_div(struct device *dev,
  682. struct device_attribute *da,
  683. const char *buf, size_t count)
  684. {
  685. unsigned long val;
  686. int ret;
  687. if (kstrtoul(buf, 10, &val))
  688. return -EINVAL;
  689. ret = do_set_fan_div(dev, val);
  690. if (ret < 0)
  691. return ret;
  692. return count;
  693. }
  694. /*
  695. * Read and write functions for fan1_pulses sysfs file. Get and set number
  696. * of tachometer pulses per fan revolution.
  697. */
  698. static ssize_t get_fan_pulses(struct device *dev,
  699. struct device_attribute *da, char *buf)
  700. {
  701. struct g762_data *data = g762_update_client(dev);
  702. if (IS_ERR(data))
  703. return PTR_ERR(data);
  704. return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
  705. }
  706. static ssize_t set_fan_pulses(struct device *dev,
  707. struct device_attribute *da,
  708. const char *buf, size_t count)
  709. {
  710. unsigned long val;
  711. int ret;
  712. if (kstrtoul(buf, 10, &val))
  713. return -EINVAL;
  714. ret = do_set_fan_pulses(dev, val);
  715. if (ret < 0)
  716. return ret;
  717. return count;
  718. }
  719. /*
  720. * Read and write functions for pwm1_enable. Get and set fan speed control mode
  721. * (i.e. closed or open-loop).
  722. *
  723. * Following documentation about hwmon's sysfs interface, a pwm1_enable node
  724. * should accept followings:
  725. *
  726. * 0 : no fan speed control (i.e. fan at full speed)
  727. * 1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
  728. * 2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
  729. *
  730. * but we do not accept 0 as this mode is not natively supported by the chip
  731. * and it is not emulated by g762 driver. -EINVAL is returned in this case.
  732. */
  733. static ssize_t get_pwm_enable(struct device *dev,
  734. struct device_attribute *da, char *buf)
  735. {
  736. struct g762_data *data = g762_update_client(dev);
  737. if (IS_ERR(data))
  738. return PTR_ERR(data);
  739. return sprintf(buf, "%d\n",
  740. (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1);
  741. }
  742. static ssize_t set_pwm_enable(struct device *dev,
  743. struct device_attribute *da,
  744. const char *buf, size_t count)
  745. {
  746. unsigned long val;
  747. int ret;
  748. if (kstrtoul(buf, 10, &val))
  749. return -EINVAL;
  750. ret = do_set_pwm_enable(dev, val);
  751. if (ret < 0)
  752. return ret;
  753. return count;
  754. }
  755. /*
  756. * Read and write functions for pwm1 sysfs file. Get and set pwm value
  757. * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
  758. * makes it run at full speed.
  759. */
  760. static ssize_t get_pwm(struct device *dev, struct device_attribute *da,
  761. char *buf)
  762. {
  763. struct g762_data *data = g762_update_client(dev);
  764. if (IS_ERR(data))
  765. return PTR_ERR(data);
  766. return sprintf(buf, "%d\n", data->set_out);
  767. }
  768. static ssize_t set_pwm(struct device *dev, struct device_attribute *da,
  769. const char *buf, size_t count)
  770. {
  771. unsigned long val;
  772. int ret;
  773. if (kstrtoul(buf, 10, &val))
  774. return -EINVAL;
  775. ret = do_set_pwm(dev, val);
  776. if (ret < 0)
  777. return ret;
  778. return count;
  779. }
  780. /*
  781. * Read and write function for fan1_target sysfs file. Get/set the fan speed in
  782. * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
  783. * the fan speed using pulses from fan tachometer.
  784. *
  785. * Refer to rpm_from_cnt() implementation above to get info about count number
  786. * calculation.
  787. *
  788. * Also note that due to rounding errors it is possible that you don't read
  789. * back exactly the value you have set.
  790. */
  791. static ssize_t get_fan_target(struct device *dev, struct device_attribute *da,
  792. char *buf)
  793. {
  794. struct g762_data *data = g762_update_client(dev);
  795. unsigned int rpm;
  796. if (IS_ERR(data))
  797. return PTR_ERR(data);
  798. mutex_lock(&data->update_lock);
  799. rpm = rpm_from_cnt(data->set_cnt, data->clk_freq,
  800. G762_PULSE_FROM_REG(data->fan_cmd1),
  801. G762_CLKDIV_FROM_REG(data->fan_cmd1),
  802. G762_GEARMULT_FROM_REG(data->fan_cmd2));
  803. mutex_unlock(&data->update_lock);
  804. return sprintf(buf, "%u\n", rpm);
  805. }
  806. static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
  807. const char *buf, 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 get_fan_failure(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 get_fan_ooc(struct device *dev, struct device_attribute *da,
  832. 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(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
  840. static DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, get_pwm_mode, set_pwm_mode);
  841. static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  842. get_pwm_enable, set_pwm_enable);
  843. static DEVICE_ATTR(fan1_input, S_IRUGO, get_fan_rpm, NULL);
  844. static DEVICE_ATTR(fan1_alarm, S_IRUGO, get_fan_ooc, NULL);
  845. static DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_failure, NULL);
  846. static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO,
  847. get_fan_target, set_fan_target);
  848. static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_fan_div, set_fan_div);
  849. static DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO,
  850. get_fan_pulses, set_fan_pulses);
  851. /* Driver data */
  852. static struct attribute *g762_attrs[] = {
  853. &dev_attr_fan1_input.attr,
  854. &dev_attr_fan1_alarm.attr,
  855. &dev_attr_fan1_fault.attr,
  856. &dev_attr_fan1_target.attr,
  857. &dev_attr_fan1_div.attr,
  858. &dev_attr_fan1_pulses.attr,
  859. &dev_attr_pwm1.attr,
  860. &dev_attr_pwm1_mode.attr,
  861. &dev_attr_pwm1_enable.attr,
  862. NULL
  863. };
  864. ATTRIBUTE_GROUPS(g762);
  865. /*
  866. * Enable both fan failure detection and fan out of control protection. The
  867. * function does not protect change/access to data structure; it must thus
  868. * only be called during initialization.
  869. */
  870. static inline int g762_fan_init(struct device *dev)
  871. {
  872. struct g762_data *data = g762_update_client(dev);
  873. if (IS_ERR(data))
  874. return PTR_ERR(data);
  875. data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL;
  876. data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
  877. data->valid = false;
  878. return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  879. data->fan_cmd1);
  880. }
  881. static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
  882. {
  883. struct device *dev = &client->dev;
  884. struct g762_data *data;
  885. int ret;
  886. if (!i2c_check_functionality(client->adapter,
  887. I2C_FUNC_SMBUS_BYTE_DATA))
  888. return -ENODEV;
  889. data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
  890. if (!data)
  891. return -ENOMEM;
  892. i2c_set_clientdata(client, data);
  893. data->client = client;
  894. mutex_init(&data->update_lock);
  895. /* Enable fan failure detection and fan out of control protection */
  896. ret = g762_fan_init(dev);
  897. if (ret)
  898. return ret;
  899. /* Get configuration via DT ... */
  900. ret = g762_of_clock_enable(client);
  901. if (ret)
  902. return ret;
  903. ret = g762_of_prop_import(client);
  904. if (ret)
  905. goto clock_dis;
  906. /* ... or platform_data */
  907. ret = g762_pdata_prop_import(client);
  908. if (ret)
  909. goto clock_dis;
  910. data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
  911. data, g762_groups);
  912. if (IS_ERR(data->hwmon_dev)) {
  913. ret = PTR_ERR(data->hwmon_dev);
  914. goto clock_dis;
  915. }
  916. return 0;
  917. clock_dis:
  918. g762_of_clock_disable(client);
  919. return ret;
  920. }
  921. static int g762_remove(struct i2c_client *client)
  922. {
  923. struct g762_data *data = i2c_get_clientdata(client);
  924. hwmon_device_unregister(data->hwmon_dev);
  925. g762_of_clock_disable(client);
  926. return 0;
  927. }
  928. static struct i2c_driver g762_driver = {
  929. .driver = {
  930. .name = DRVNAME,
  931. .of_match_table = of_match_ptr(g762_dt_match),
  932. },
  933. .probe = g762_probe,
  934. .remove = g762_remove,
  935. .id_table = g762_id,
  936. };
  937. module_i2c_driver(g762_driver);
  938. MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
  939. MODULE_DESCRIPTION("GMT G762/G763 driver");
  940. MODULE_LICENSE("GPL");