g762.c 27 KB

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