g762.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. /*
  508. * Grab clock (a required property), enable it, get (fixed) clock frequency
  509. * and store it. Note: upon success, clock has been prepared and enabled; it
  510. * must later be unprepared and disabled (e.g. during module unloading) by a
  511. * call to g762_of_clock_disable(). Note that a reference to clock is kept
  512. * in our private data structure to be used in this function.
  513. */
  514. static int g762_of_clock_enable(struct i2c_client *client)
  515. {
  516. struct g762_data *data;
  517. unsigned long clk_freq;
  518. struct clk *clk;
  519. int ret;
  520. if (!client->dev.of_node)
  521. return 0;
  522. clk = of_clk_get(client->dev.of_node, 0);
  523. if (IS_ERR(clk)) {
  524. dev_err(&client->dev, "failed to get clock\n");
  525. return PTR_ERR(clk);
  526. }
  527. ret = clk_prepare_enable(clk);
  528. if (ret) {
  529. dev_err(&client->dev, "failed to enable clock\n");
  530. goto clk_put;
  531. }
  532. clk_freq = clk_get_rate(clk);
  533. ret = do_set_clk_freq(&client->dev, clk_freq);
  534. if (ret) {
  535. dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq);
  536. goto clk_unprep;
  537. }
  538. data = i2c_get_clientdata(client);
  539. data->clk = clk;
  540. return 0;
  541. clk_unprep:
  542. clk_disable_unprepare(clk);
  543. clk_put:
  544. clk_put(clk);
  545. return ret;
  546. }
  547. static void g762_of_clock_disable(struct i2c_client *client)
  548. {
  549. struct g762_data *data = i2c_get_clientdata(client);
  550. if (!data->clk)
  551. return;
  552. clk_disable_unprepare(data->clk);
  553. clk_put(data->clk);
  554. }
  555. static int g762_of_prop_import_one(struct i2c_client *client,
  556. const char *pname,
  557. int (*psetter)(struct device *dev,
  558. unsigned long val))
  559. {
  560. int ret;
  561. u32 pval;
  562. if (of_property_read_u32(client->dev.of_node, pname, &pval))
  563. return 0;
  564. dev_dbg(&client->dev, "found %s (%d)\n", pname, pval);
  565. ret = (*psetter)(&client->dev, pval);
  566. if (ret)
  567. dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval);
  568. return ret;
  569. }
  570. static int g762_of_prop_import(struct i2c_client *client)
  571. {
  572. int ret;
  573. if (!client->dev.of_node)
  574. return 0;
  575. ret = g762_of_prop_import_one(client, "fan_gear_mode",
  576. do_set_fan_gear_mode);
  577. if (ret)
  578. return ret;
  579. ret = g762_of_prop_import_one(client, "pwm_polarity",
  580. do_set_pwm_polarity);
  581. if (ret)
  582. return ret;
  583. return g762_of_prop_import_one(client, "fan_startv",
  584. do_set_fan_startv);
  585. }
  586. #else
  587. static int g762_of_prop_import(struct i2c_client *client)
  588. {
  589. return 0;
  590. }
  591. static int g762_of_clock_enable(struct i2c_client *client)
  592. {
  593. return 0;
  594. }
  595. static void g762_of_clock_disable(struct i2c_client *client) { }
  596. #endif
  597. /*
  598. * Helper to import hardware characteristics from .dts file and push
  599. * those to the chip.
  600. */
  601. static int g762_pdata_prop_import(struct i2c_client *client)
  602. {
  603. struct g762_platform_data *pdata = dev_get_platdata(&client->dev);
  604. int ret;
  605. if (!pdata)
  606. return 0;
  607. ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode);
  608. if (ret)
  609. return ret;
  610. ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity);
  611. if (ret)
  612. return ret;
  613. ret = do_set_fan_startv(&client->dev, pdata->fan_startv);
  614. if (ret)
  615. return ret;
  616. return do_set_clk_freq(&client->dev, pdata->clk_freq);
  617. }
  618. /*
  619. * sysfs attributes
  620. */
  621. /*
  622. * Read function for fan1_input sysfs file. Return current fan RPM value, or
  623. * 0 if fan is out of control.
  624. */
  625. static ssize_t get_fan_rpm(struct device *dev, struct device_attribute *da,
  626. char *buf)
  627. {
  628. struct g762_data *data = g762_update_client(dev);
  629. unsigned int rpm = 0;
  630. if (IS_ERR(data))
  631. return PTR_ERR(data);
  632. mutex_lock(&data->update_lock);
  633. /* reverse logic: fan out of control reporting is enabled low */
  634. if (data->fan_sta & G762_REG_FAN_STA_OOC) {
  635. rpm = rpm_from_cnt(data->act_cnt, data->clk_freq,
  636. G762_PULSE_FROM_REG(data->fan_cmd1),
  637. G762_CLKDIV_FROM_REG(data->fan_cmd1),
  638. G762_GEARMULT_FROM_REG(data->fan_cmd2));
  639. }
  640. mutex_unlock(&data->update_lock);
  641. return sprintf(buf, "%u\n", rpm);
  642. }
  643. /*
  644. * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
  645. * control mode i.e. PWM (1) or DC (0).
  646. */
  647. static ssize_t get_pwm_mode(struct device *dev, struct device_attribute *da,
  648. char *buf)
  649. {
  650. struct g762_data *data = g762_update_client(dev);
  651. if (IS_ERR(data))
  652. return PTR_ERR(data);
  653. return sprintf(buf, "%d\n",
  654. !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE));
  655. }
  656. static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *da,
  657. const char *buf, size_t count)
  658. {
  659. unsigned long val;
  660. int ret;
  661. if (kstrtoul(buf, 10, &val))
  662. return -EINVAL;
  663. ret = do_set_pwm_mode(dev, val);
  664. if (ret < 0)
  665. return ret;
  666. return count;
  667. }
  668. /*
  669. * Read and write functions for fan1_div sysfs file. Get and set fan
  670. * controller prescaler value
  671. */
  672. static ssize_t get_fan_div(struct device *dev,
  673. struct device_attribute *da, char *buf)
  674. {
  675. struct g762_data *data = g762_update_client(dev);
  676. if (IS_ERR(data))
  677. return PTR_ERR(data);
  678. return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1));
  679. }
  680. static ssize_t set_fan_div(struct device *dev,
  681. struct device_attribute *da,
  682. const char *buf, size_t count)
  683. {
  684. unsigned long val;
  685. int ret;
  686. if (kstrtoul(buf, 10, &val))
  687. return -EINVAL;
  688. ret = do_set_fan_div(dev, val);
  689. if (ret < 0)
  690. return ret;
  691. return count;
  692. }
  693. /*
  694. * Read and write functions for fan1_pulses sysfs file. Get and set number
  695. * of tachometer pulses per fan revolution.
  696. */
  697. static ssize_t get_fan_pulses(struct device *dev,
  698. struct device_attribute *da, char *buf)
  699. {
  700. struct g762_data *data = g762_update_client(dev);
  701. if (IS_ERR(data))
  702. return PTR_ERR(data);
  703. return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
  704. }
  705. static ssize_t set_fan_pulses(struct device *dev,
  706. struct device_attribute *da,
  707. const char *buf, size_t count)
  708. {
  709. unsigned long val;
  710. int ret;
  711. if (kstrtoul(buf, 10, &val))
  712. return -EINVAL;
  713. ret = do_set_fan_pulses(dev, val);
  714. if (ret < 0)
  715. return ret;
  716. return count;
  717. }
  718. /*
  719. * Read and write functions for pwm1_enable. Get and set fan speed control mode
  720. * (i.e. closed or open-loop).
  721. *
  722. * Following documentation about hwmon's sysfs interface, a pwm1_enable node
  723. * should accept followings:
  724. *
  725. * 0 : no fan speed control (i.e. fan at full speed)
  726. * 1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
  727. * 2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
  728. *
  729. * but we do not accept 0 as this mode is not natively supported by the chip
  730. * and it is not emulated by g762 driver. -EINVAL is returned in this case.
  731. */
  732. static ssize_t get_pwm_enable(struct device *dev,
  733. struct device_attribute *da, char *buf)
  734. {
  735. struct g762_data *data = g762_update_client(dev);
  736. if (IS_ERR(data))
  737. return PTR_ERR(data);
  738. return sprintf(buf, "%d\n",
  739. (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1);
  740. }
  741. static ssize_t set_pwm_enable(struct device *dev,
  742. struct device_attribute *da,
  743. const char *buf, size_t count)
  744. {
  745. unsigned long val;
  746. int ret;
  747. if (kstrtoul(buf, 10, &val))
  748. return -EINVAL;
  749. ret = do_set_pwm_enable(dev, val);
  750. if (ret < 0)
  751. return ret;
  752. return count;
  753. }
  754. /*
  755. * Read and write functions for pwm1 sysfs file. Get and set pwm value
  756. * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
  757. * makes it run at full speed.
  758. */
  759. static ssize_t get_pwm(struct device *dev, struct device_attribute *da,
  760. char *buf)
  761. {
  762. struct g762_data *data = g762_update_client(dev);
  763. if (IS_ERR(data))
  764. return PTR_ERR(data);
  765. return sprintf(buf, "%d\n", data->set_out);
  766. }
  767. static ssize_t set_pwm(struct device *dev, struct device_attribute *da,
  768. const char *buf, size_t count)
  769. {
  770. unsigned long val;
  771. int ret;
  772. if (kstrtoul(buf, 10, &val))
  773. return -EINVAL;
  774. ret = do_set_pwm(dev, val);
  775. if (ret < 0)
  776. return ret;
  777. return count;
  778. }
  779. /*
  780. * Read and write function for fan1_target sysfs file. Get/set the fan speed in
  781. * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
  782. * the fan speed using pulses from fan tachometer.
  783. *
  784. * Refer to rpm_from_cnt() implementation above to get info about count number
  785. * calculation.
  786. *
  787. * Also note that due to rounding errors it is possible that you don't read
  788. * back exactly the value you have set.
  789. */
  790. static ssize_t get_fan_target(struct device *dev, struct device_attribute *da,
  791. char *buf)
  792. {
  793. struct g762_data *data = g762_update_client(dev);
  794. unsigned int rpm;
  795. if (IS_ERR(data))
  796. return PTR_ERR(data);
  797. mutex_lock(&data->update_lock);
  798. rpm = rpm_from_cnt(data->set_cnt, data->clk_freq,
  799. G762_PULSE_FROM_REG(data->fan_cmd1),
  800. G762_CLKDIV_FROM_REG(data->fan_cmd1),
  801. G762_GEARMULT_FROM_REG(data->fan_cmd2));
  802. mutex_unlock(&data->update_lock);
  803. return sprintf(buf, "%u\n", rpm);
  804. }
  805. static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
  806. const char *buf, size_t count)
  807. {
  808. unsigned long val;
  809. int ret;
  810. if (kstrtoul(buf, 10, &val))
  811. return -EINVAL;
  812. ret = do_set_fan_target(dev, val);
  813. if (ret < 0)
  814. return ret;
  815. return count;
  816. }
  817. /* read function for fan1_fault sysfs file. */
  818. static ssize_t get_fan_failure(struct device *dev, struct device_attribute *da,
  819. 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_FAIL));
  825. }
  826. /*
  827. * read function for fan1_alarm sysfs file. Note that OOC condition is
  828. * enabled low
  829. */
  830. static ssize_t get_fan_ooc(struct device *dev, struct device_attribute *da,
  831. char *buf)
  832. {
  833. struct g762_data *data = g762_update_client(dev);
  834. if (IS_ERR(data))
  835. return PTR_ERR(data);
  836. return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC));
  837. }
  838. static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
  839. static DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, get_pwm_mode, set_pwm_mode);
  840. static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  841. get_pwm_enable, set_pwm_enable);
  842. static DEVICE_ATTR(fan1_input, S_IRUGO, get_fan_rpm, NULL);
  843. static DEVICE_ATTR(fan1_alarm, S_IRUGO, get_fan_ooc, NULL);
  844. static DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_failure, NULL);
  845. static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO,
  846. get_fan_target, set_fan_target);
  847. static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_fan_div, set_fan_div);
  848. static DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO,
  849. get_fan_pulses, set_fan_pulses);
  850. /* Driver data */
  851. static struct attribute *g762_attrs[] = {
  852. &dev_attr_fan1_input.attr,
  853. &dev_attr_fan1_alarm.attr,
  854. &dev_attr_fan1_fault.attr,
  855. &dev_attr_fan1_target.attr,
  856. &dev_attr_fan1_div.attr,
  857. &dev_attr_fan1_pulses.attr,
  858. &dev_attr_pwm1.attr,
  859. &dev_attr_pwm1_mode.attr,
  860. &dev_attr_pwm1_enable.attr,
  861. NULL
  862. };
  863. ATTRIBUTE_GROUPS(g762);
  864. /*
  865. * Enable both fan failure detection and fan out of control protection. The
  866. * function does not protect change/access to data structure; it must thus
  867. * only be called during initialization.
  868. */
  869. static inline int g762_fan_init(struct device *dev)
  870. {
  871. struct g762_data *data = g762_update_client(dev);
  872. if (IS_ERR(data))
  873. return PTR_ERR(data);
  874. data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL;
  875. data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
  876. data->valid = false;
  877. return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
  878. data->fan_cmd1);
  879. }
  880. static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
  881. {
  882. struct device *dev = &client->dev;
  883. struct g762_data *data;
  884. int ret;
  885. if (!i2c_check_functionality(client->adapter,
  886. I2C_FUNC_SMBUS_BYTE_DATA))
  887. return -ENODEV;
  888. data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
  889. if (!data)
  890. return -ENOMEM;
  891. i2c_set_clientdata(client, data);
  892. data->client = client;
  893. mutex_init(&data->update_lock);
  894. /* Enable fan failure detection and fan out of control protection */
  895. ret = g762_fan_init(dev);
  896. if (ret)
  897. return ret;
  898. /* Get configuration via DT ... */
  899. ret = g762_of_clock_enable(client);
  900. if (ret)
  901. return ret;
  902. ret = g762_of_prop_import(client);
  903. if (ret)
  904. goto clock_dis;
  905. /* ... or platform_data */
  906. ret = g762_pdata_prop_import(client);
  907. if (ret)
  908. goto clock_dis;
  909. data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
  910. data, g762_groups);
  911. if (IS_ERR(data->hwmon_dev)) {
  912. ret = PTR_ERR(data->hwmon_dev);
  913. goto clock_dis;
  914. }
  915. return 0;
  916. clock_dis:
  917. g762_of_clock_disable(client);
  918. return ret;
  919. }
  920. static int g762_remove(struct i2c_client *client)
  921. {
  922. struct g762_data *data = i2c_get_clientdata(client);
  923. hwmon_device_unregister(data->hwmon_dev);
  924. g762_of_clock_disable(client);
  925. return 0;
  926. }
  927. static struct i2c_driver g762_driver = {
  928. .driver = {
  929. .name = DRVNAME,
  930. .owner = THIS_MODULE,
  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");