g762.c 28 KB

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