g762.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /*
  2. * g762 - Driver for the Global Mixed-mode Technology Inc. fan speed
  3. * PWM controller chips from G762 family, i.e. G762 and G763
  4. *
  5. * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
  6. *
  7. * This work is based on a basic version for 2.6.31 kernel developed
  8. * by Olivier Mouchet for LaCie. Updates and correction have been
  9. * performed to run on recent kernels. Additional features, like the
  10. * ability to configure various characteristics via .dts file or
  11. * board init file have been added. Detailed datasheet on which this
  12. * development is based is available here:
  13. *
  14. * http://natisbad.org/NAS/refs/GMT_EDS-762_763-080710-0.2.pdf
  15. *
  16. * Headers from previous developments have been kept below:
  17. *
  18. * Copyright (c) 2009 LaCie
  19. *
  20. * Author: Olivier Mouchet <olivier.mouchet@gmail.com>
  21. *
  22. * based on g760a code written by Herbert Valerio Riedel <hvr@gnu.org>
  23. * Copyright (C) 2007 Herbert Valerio Riedel <hvr@gnu.org>
  24. *
  25. * g762: minimal datasheet available at:
  26. * http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf
  27. *
  28. * This program is free software; you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License as published by
  30. * the Free Software Foundation; either version 2 of the License, or
  31. * (at your option) any later version.
  32. *
  33. * This program is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. * GNU General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation.
  41. */
  42. #include <linux/device.h>
  43. #include <linux/module.h>
  44. #include <linux/init.h>
  45. #include <linux/jiffies.h>
  46. #include <linux/i2c.h>
  47. #include <linux/hwmon.h>
  48. #include <linux/hwmon-sysfs.h>
  49. #include <linux/err.h>
  50. #include <linux/mutex.h>
  51. #include <linux/kernel.h>
  52. #include <linux/clk.h>
  53. #include <linux/of.h>
  54. #include <linux/of_device.h>
  55. #include <linux/platform_data/g762.h>
  56. #define DRVNAME "g762"
  57. static const struct i2c_device_id g762_id[] = {
  58. { "g762", 0 },
  59. { "g763", 0 },
  60. { }
  61. };
  62. MODULE_DEVICE_TABLE(i2c, g762_id);
  63. enum g762_regs {
  64. G762_REG_SET_CNT = 0x00,
  65. G762_REG_ACT_CNT = 0x01,
  66. G762_REG_FAN_STA = 0x02,
  67. G762_REG_SET_OUT = 0x03,
  68. G762_REG_FAN_CMD1 = 0x04,
  69. G762_REG_FAN_CMD2 = 0x05,
  70. };
  71. /* Config register bits */
  72. #define G762_REG_FAN_CMD1_DET_FAN_FAIL 0x80 /* enable fan_fail signal */
  73. #define G762_REG_FAN_CMD1_DET_FAN_OOC 0x40 /* enable fan_out_of_control */
  74. #define G762_REG_FAN_CMD1_OUT_MODE 0x20 /* out mode: PWM or DC */
  75. #define G762_REG_FAN_CMD1_FAN_MODE 0x10 /* fan mode: closed/open-loop */
  76. #define G762_REG_FAN_CMD1_CLK_DIV_ID1 0x08 /* clock divisor value */
  77. #define G762_REG_FAN_CMD1_CLK_DIV_ID0 0x04
  78. #define G762_REG_FAN_CMD1_PWM_POLARITY 0x02 /* PWM polarity */
  79. #define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */
  80. #define G762_REG_FAN_CMD2_GEAR_MODE_1 0x08 /* fan gear mode */
  81. #define G762_REG_FAN_CMD2_GEAR_MODE_0 0x04
  82. #define G762_REG_FAN_CMD2_FAN_STARTV_1 0x02 /* fan startup voltage */
  83. #define G762_REG_FAN_CMD2_FAN_STARTV_0 0x01
  84. #define G762_REG_FAN_STA_FAIL 0x02 /* fan fail */
  85. #define G762_REG_FAN_STA_OOC 0x01 /* fan out of control */
  86. /* Config register values */
  87. #define G762_OUT_MODE_PWM 1
  88. #define G762_OUT_MODE_DC 0
  89. #define G762_FAN_MODE_CLOSED_LOOP 2
  90. #define G762_FAN_MODE_OPEN_LOOP 1
  91. #define G762_PWM_POLARITY_NEGATIVE 1
  92. #define G762_PWM_POLARITY_POSITIVE 0
  93. /* Register data is read (and cached) at most once per second. */
  94. #define G762_UPDATE_INTERVAL HZ
  95. /*
  96. * Extract pulse count per fan revolution value (2 or 4) from given
  97. * FAN_CMD1 register value.
  98. */
  99. #define G762_PULSE_FROM_REG(reg) \
  100. ((((reg) & G762_REG_FAN_CMD1_PULSE_PER_REV) + 1) << 1)
  101. /*
  102. * Extract fan clock divisor (1, 2, 4 or 8) from given FAN_CMD1
  103. * register value.
  104. */
  105. #define G762_CLKDIV_FROM_REG(reg) \
  106. (1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 | \
  107. G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2))
  108. /*
  109. * Extract fan gear mode multiplier value (0, 2 or 4) from given
  110. * FAN_CMD2 register value.
  111. */
  112. #define G762_GEARMULT_FROM_REG(reg) \
  113. (1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 | \
  114. G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
  115. struct g762_data {
  116. struct i2c_client *client;
  117. struct device *hwmon_dev;
  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 i2c_client *client = to_i2c_client(dev);
  186. struct g762_data *data = i2c_get_clientdata(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 i2c_client *client = to_i2c_client(dev);
  234. struct g762_data *data = i2c_get_clientdata(client);
  235. if (val > 0xffffff)
  236. return -EINVAL;
  237. if (!val)
  238. val = 32768;
  239. data->clk_freq = val;
  240. return 0;
  241. }
  242. /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
  243. static int do_set_pwm_mode(struct device *dev, unsigned long val)
  244. {
  245. struct i2c_client *client = to_i2c_client(dev);
  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(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 i2c_client *client = to_i2c_client(dev);
  273. struct g762_data *data = g762_update_client(dev);
  274. int ret;
  275. if (IS_ERR(data))
  276. return PTR_ERR(data);
  277. mutex_lock(&data->update_lock);
  278. switch (val) {
  279. case 1:
  280. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
  281. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
  282. break;
  283. case 2:
  284. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
  285. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
  286. break;
  287. case 4:
  288. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
  289. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
  290. break;
  291. case 8:
  292. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
  293. data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
  294. break;
  295. default:
  296. ret = -EINVAL;
  297. goto out;
  298. }
  299. ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1,
  300. data->fan_cmd1);
  301. data->valid = false;
  302. out:
  303. mutex_unlock(&data->update_lock);
  304. return ret;
  305. }
  306. /* Set fan gear mode. Accepts either 0, 1 or 2. */
  307. static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
  308. {
  309. struct i2c_client *client = to_i2c_client(dev);
  310. struct g762_data *data = g762_update_client(dev);
  311. int ret;
  312. if (IS_ERR(data))
  313. return PTR_ERR(data);
  314. mutex_lock(&data->update_lock);
  315. switch (val) {
  316. case 0:
  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 1:
  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. case 2:
  325. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
  326. data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_1;
  327. break;
  328. default:
  329. ret = -EINVAL;
  330. goto out;
  331. }
  332. ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD2,
  333. data->fan_cmd2);
  334. data->valid = false;
  335. out:
  336. mutex_unlock(&data->update_lock);
  337. return ret;
  338. }
  339. /* Set number of fan pulses per revolution. Accepts either 2 or 4. */
  340. static int do_set_fan_pulses(struct device *dev, unsigned long val)
  341. {
  342. struct i2c_client *client = to_i2c_client(dev);
  343. struct g762_data *data = g762_update_client(dev);
  344. int ret;
  345. if (IS_ERR(data))
  346. return PTR_ERR(data);
  347. mutex_lock(&data->update_lock);
  348. switch (val) {
  349. case 2:
  350. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV;
  351. break;
  352. case 4:
  353. data->fan_cmd1 |= G762_REG_FAN_CMD1_PULSE_PER_REV;
  354. break;
  355. default:
  356. ret = -EINVAL;
  357. goto out;
  358. }
  359. ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1,
  360. data->fan_cmd1);
  361. data->valid = false;
  362. out:
  363. mutex_unlock(&data->update_lock);
  364. return ret;
  365. }
  366. /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
  367. static int do_set_pwm_enable(struct device *dev, unsigned long val)
  368. {
  369. struct i2c_client *client = to_i2c_client(dev);
  370. struct g762_data *data = g762_update_client(dev);
  371. int ret;
  372. if (IS_ERR(data))
  373. return PTR_ERR(data);
  374. mutex_lock(&data->update_lock);
  375. switch (val) {
  376. case G762_FAN_MODE_CLOSED_LOOP:
  377. data->fan_cmd1 |= G762_REG_FAN_CMD1_FAN_MODE;
  378. break;
  379. case G762_FAN_MODE_OPEN_LOOP:
  380. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE;
  381. /*
  382. * BUG FIX: if SET_CNT register value is 255 then, for some
  383. * unknown reason, fan will not rotate as expected, no matter
  384. * the value of SET_OUT (to be specific, this seems to happen
  385. * only in PWM mode). To workaround this bug, we give SET_CNT
  386. * value of 254 if it is 255 when switching to open-loop.
  387. */
  388. if (data->set_cnt == 0xff)
  389. i2c_smbus_write_byte_data(client, G762_REG_SET_CNT,
  390. 254);
  391. break;
  392. default:
  393. ret = -EINVAL;
  394. goto out;
  395. }
  396. ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1,
  397. data->fan_cmd1);
  398. data->valid = false;
  399. out:
  400. mutex_unlock(&data->update_lock);
  401. return ret;
  402. }
  403. /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
  404. static int do_set_pwm_polarity(struct device *dev, unsigned long val)
  405. {
  406. struct i2c_client *client = to_i2c_client(dev);
  407. struct g762_data *data = g762_update_client(dev);
  408. int ret;
  409. if (IS_ERR(data))
  410. return PTR_ERR(data);
  411. mutex_lock(&data->update_lock);
  412. switch (val) {
  413. case G762_PWM_POLARITY_POSITIVE:
  414. data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY;
  415. break;
  416. case G762_PWM_POLARITY_NEGATIVE:
  417. data->fan_cmd1 |= G762_REG_FAN_CMD1_PWM_POLARITY;
  418. break;
  419. default:
  420. ret = -EINVAL;
  421. goto out;
  422. }
  423. ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1,
  424. data->fan_cmd1);
  425. data->valid = false;
  426. out:
  427. mutex_unlock(&data->update_lock);
  428. return ret;
  429. }
  430. /*
  431. * Set pwm value. Accepts values between 0 (stops the fan) and
  432. * 255 (full speed). This only makes sense in open-loop mode.
  433. */
  434. static int do_set_pwm(struct device *dev, unsigned long val)
  435. {
  436. struct i2c_client *client = to_i2c_client(dev);
  437. struct g762_data *data = i2c_get_clientdata(client);
  438. int ret;
  439. if (val > 255)
  440. return -EINVAL;
  441. mutex_lock(&data->update_lock);
  442. ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val);
  443. data->valid = false;
  444. mutex_unlock(&data->update_lock);
  445. return ret;
  446. }
  447. /*
  448. * Set fan RPM value. Can be called both in closed and open-loop mode
  449. * but effect will only be seen after closed-loop mode is configured.
  450. */
  451. static int do_set_fan_target(struct device *dev, unsigned long val)
  452. {
  453. struct i2c_client *client = to_i2c_client(dev);
  454. struct g762_data *data = g762_update_client(dev);
  455. int ret;
  456. if (IS_ERR(data))
  457. return PTR_ERR(data);
  458. mutex_lock(&data->update_lock);
  459. data->set_cnt = cnt_from_rpm(val, data->clk_freq,
  460. G762_PULSE_FROM_REG(data->fan_cmd1),
  461. G762_CLKDIV_FROM_REG(data->fan_cmd1),
  462. G762_GEARMULT_FROM_REG(data->fan_cmd2));
  463. ret = i2c_smbus_write_byte_data(client, G762_REG_SET_CNT,
  464. data->set_cnt);
  465. data->valid = false;
  466. mutex_unlock(&data->update_lock);
  467. return ret;
  468. }
  469. /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
  470. static int do_set_fan_startv(struct device *dev, unsigned long val)
  471. {
  472. struct i2c_client *client = to_i2c_client(dev);
  473. struct g762_data *data = g762_update_client(dev);
  474. int ret;
  475. if (IS_ERR(data))
  476. return PTR_ERR(data);
  477. mutex_lock(&data->update_lock);
  478. switch (val) {
  479. case 0:
  480. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
  481. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
  482. break;
  483. case 1:
  484. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
  485. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
  486. break;
  487. case 2:
  488. data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
  489. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
  490. break;
  491. case 3:
  492. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
  493. data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
  494. break;
  495. default:
  496. ret = -EINVAL;
  497. goto out;
  498. }
  499. ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD2,
  500. data->fan_cmd2);
  501. data->valid = false;
  502. out:
  503. mutex_unlock(&data->update_lock);
  504. return ret;
  505. }
  506. /*
  507. * Helper to import hardware characteristics from .dts file and push
  508. * those to the chip.
  509. */
  510. #ifdef CONFIG_OF
  511. static struct of_device_id g762_dt_match[] = {
  512. { .compatible = "gmt,g762" },
  513. { .compatible = "gmt,g763" },
  514. { },
  515. };
  516. /*
  517. * Grab clock (a required property), enable it, get (fixed) clock frequency
  518. * and store it. Note: upon success, clock has been prepared and enabled; it
  519. * must later be unprepared and disabled (e.g. during module unloading) by a
  520. * call to g762_of_clock_disable(). Note that a reference to clock is kept
  521. * in our private data structure to be used in this function.
  522. */
  523. static int g762_of_clock_enable(struct i2c_client *client)
  524. {
  525. struct g762_data *data;
  526. unsigned long clk_freq;
  527. struct clk *clk;
  528. int ret;
  529. if (!client->dev.of_node)
  530. return 0;
  531. clk = of_clk_get(client->dev.of_node, 0);
  532. if (IS_ERR(clk)) {
  533. dev_err(&client->dev, "failed to get clock\n");
  534. return PTR_ERR(clk);
  535. }
  536. ret = clk_prepare_enable(clk);
  537. if (ret) {
  538. dev_err(&client->dev, "failed to enable clock\n");
  539. goto clk_put;
  540. }
  541. clk_freq = clk_get_rate(clk);
  542. ret = do_set_clk_freq(&client->dev, clk_freq);
  543. if (ret) {
  544. dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq);
  545. goto clk_unprep;
  546. }
  547. data = i2c_get_clientdata(client);
  548. data->clk = clk;
  549. return 0;
  550. clk_unprep:
  551. clk_disable_unprepare(clk);
  552. clk_put:
  553. clk_put(clk);
  554. return ret;
  555. }
  556. static void g762_of_clock_disable(struct i2c_client *client)
  557. {
  558. struct g762_data *data = i2c_get_clientdata(client);
  559. if (!data->clk)
  560. return;
  561. clk_disable_unprepare(data->clk);
  562. clk_put(data->clk);
  563. }
  564. static int g762_of_prop_import_one(struct i2c_client *client,
  565. const char *pname,
  566. int (*psetter)(struct device *dev,
  567. unsigned long val))
  568. {
  569. const __be32 *prop;
  570. int len, ret;
  571. u32 pval;
  572. prop = of_get_property(client->dev.of_node, pname, &len);
  573. if (!prop || len != sizeof(u32))
  574. return 0;
  575. pval = be32_to_cpu(prop[0]);
  576. dev_dbg(&client->dev, "found %s (%d)\n", pname, pval);
  577. ret = (*psetter)(&client->dev, pval);
  578. if (ret)
  579. dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval);
  580. return ret;
  581. }
  582. static int g762_of_prop_import(struct i2c_client *client)
  583. {
  584. int ret;
  585. if (!client->dev.of_node)
  586. return 0;
  587. ret = g762_of_prop_import_one(client, "fan_gear_mode",
  588. do_set_fan_gear_mode);
  589. if (ret)
  590. return ret;
  591. ret = g762_of_prop_import_one(client, "pwm_polarity",
  592. do_set_pwm_polarity);
  593. if (ret)
  594. return ret;
  595. return g762_of_prop_import_one(client, "fan_startv",
  596. do_set_fan_startv);
  597. }
  598. #else
  599. static int g762_of_prop_import(struct i2c_client *client)
  600. {
  601. return 0;
  602. }
  603. static int g762_of_clock_enable(struct i2c_client *client)
  604. {
  605. return 0;
  606. }
  607. static void g762_of_clock_disable(struct i2c_client *client) { }
  608. #endif
  609. /*
  610. * Helper to import hardware characteristics from .dts file and push
  611. * those to the chip.
  612. */
  613. static int g762_pdata_prop_import(struct i2c_client *client)
  614. {
  615. struct g762_platform_data *pdata = dev_get_platdata(&client->dev);
  616. int ret;
  617. if (!pdata)
  618. return 0;
  619. ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode);
  620. if (ret)
  621. return ret;
  622. ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity);
  623. if (ret)
  624. return ret;
  625. ret = do_set_fan_startv(&client->dev, pdata->fan_startv);
  626. if (ret)
  627. return ret;
  628. return do_set_clk_freq(&client->dev, pdata->clk_freq);
  629. }
  630. /*
  631. * sysfs attributes
  632. */
  633. /*
  634. * Read function for fan1_input sysfs file. Return current fan RPM value, or
  635. * 0 if fan is out of control.
  636. */
  637. static ssize_t get_fan_rpm(struct device *dev, struct device_attribute *da,
  638. char *buf)
  639. {
  640. struct g762_data *data = g762_update_client(dev);
  641. unsigned int rpm = 0;
  642. if (IS_ERR(data))
  643. return PTR_ERR(data);
  644. mutex_lock(&data->update_lock);
  645. /* reverse logic: fan out of control reporting is enabled low */
  646. if (data->fan_sta & G762_REG_FAN_STA_OOC) {
  647. rpm = rpm_from_cnt(data->act_cnt, data->clk_freq,
  648. G762_PULSE_FROM_REG(data->fan_cmd1),
  649. G762_CLKDIV_FROM_REG(data->fan_cmd1),
  650. G762_GEARMULT_FROM_REG(data->fan_cmd2));
  651. }
  652. mutex_unlock(&data->update_lock);
  653. return sprintf(buf, "%u\n", rpm);
  654. }
  655. /*
  656. * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
  657. * control mode i.e. PWM (1) or DC (0).
  658. */
  659. static ssize_t get_pwm_mode(struct device *dev, struct device_attribute *da,
  660. char *buf)
  661. {
  662. struct g762_data *data = g762_update_client(dev);
  663. if (IS_ERR(data))
  664. return PTR_ERR(data);
  665. return sprintf(buf, "%d\n",
  666. !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE));
  667. }
  668. static ssize_t set_pwm_mode(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_pwm_mode(dev, val);
  676. if (ret < 0)
  677. return ret;
  678. return count;
  679. }
  680. /*
  681. * Read and write functions for fan1_div sysfs file. Get and set fan
  682. * controller prescaler value
  683. */
  684. static ssize_t get_fan_div(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_CLKDIV_FROM_REG(data->fan_cmd1));
  691. }
  692. static ssize_t set_fan_div(struct device *dev,
  693. struct device_attribute *da,
  694. const char *buf, 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_div(dev, val);
  701. if (ret < 0)
  702. return ret;
  703. return count;
  704. }
  705. /*
  706. * Read and write functions for fan1_pulses sysfs file. Get and set number
  707. * of tachometer pulses per fan revolution.
  708. */
  709. static ssize_t get_fan_pulses(struct device *dev,
  710. struct device_attribute *da, char *buf)
  711. {
  712. struct g762_data *data = g762_update_client(dev);
  713. if (IS_ERR(data))
  714. return PTR_ERR(data);
  715. return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
  716. }
  717. static ssize_t set_fan_pulses(struct device *dev,
  718. struct device_attribute *da,
  719. const char *buf, size_t count)
  720. {
  721. unsigned long val;
  722. int ret;
  723. if (kstrtoul(buf, 10, &val))
  724. return -EINVAL;
  725. ret = do_set_fan_pulses(dev, val);
  726. if (ret < 0)
  727. return ret;
  728. return count;
  729. }
  730. /*
  731. * Read and write functions for pwm1_enable. Get and set fan speed control mode
  732. * (i.e. closed or open-loop).
  733. *
  734. * Following documentation about hwmon's sysfs interface, a pwm1_enable node
  735. * should accept followings:
  736. *
  737. * 0 : no fan speed control (i.e. fan at full speed)
  738. * 1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
  739. * 2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
  740. *
  741. * but we do not accept 0 as this mode is not natively supported by the chip
  742. * and it is not emulated by g762 driver. -EINVAL is returned in this case.
  743. */
  744. static ssize_t get_pwm_enable(struct device *dev,
  745. struct device_attribute *da, char *buf)
  746. {
  747. struct g762_data *data = g762_update_client(dev);
  748. if (IS_ERR(data))
  749. return PTR_ERR(data);
  750. return sprintf(buf, "%d\n",
  751. (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1);
  752. }
  753. static ssize_t set_pwm_enable(struct device *dev,
  754. 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_enable(dev, val);
  762. if (ret < 0)
  763. return ret;
  764. return count;
  765. }
  766. /*
  767. * Read and write functions for pwm1 sysfs file. Get and set pwm value
  768. * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
  769. * makes it run at full speed.
  770. */
  771. static ssize_t get_pwm(struct device *dev, struct device_attribute *da,
  772. char *buf)
  773. {
  774. struct g762_data *data = g762_update_client(dev);
  775. if (IS_ERR(data))
  776. return PTR_ERR(data);
  777. return sprintf(buf, "%d\n", data->set_out);
  778. }
  779. static ssize_t set_pwm(struct device *dev, struct device_attribute *da,
  780. const char *buf, size_t count)
  781. {
  782. unsigned long val;
  783. int ret;
  784. if (kstrtoul(buf, 10, &val))
  785. return -EINVAL;
  786. ret = do_set_pwm(dev, val);
  787. if (ret < 0)
  788. return ret;
  789. return count;
  790. }
  791. /*
  792. * Read and write function for fan1_target sysfs file. Get/set the fan speed in
  793. * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
  794. * the fan speed using pulses from fan tachometer.
  795. *
  796. * Refer to rpm_from_cnt() implementation above to get info about count number
  797. * calculation.
  798. *
  799. * Also note that due to rounding errors it is possible that you don't read
  800. * back exactly the value you have set.
  801. */
  802. static ssize_t get_fan_target(struct device *dev, struct device_attribute *da,
  803. char *buf)
  804. {
  805. struct g762_data *data = g762_update_client(dev);
  806. unsigned int rpm;
  807. if (IS_ERR(data))
  808. return PTR_ERR(data);
  809. mutex_lock(&data->update_lock);
  810. rpm = rpm_from_cnt(data->set_cnt, data->clk_freq,
  811. G762_PULSE_FROM_REG(data->fan_cmd1),
  812. G762_CLKDIV_FROM_REG(data->fan_cmd1),
  813. G762_GEARMULT_FROM_REG(data->fan_cmd2));
  814. mutex_unlock(&data->update_lock);
  815. return sprintf(buf, "%u\n", rpm);
  816. }
  817. static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
  818. const char *buf, size_t count)
  819. {
  820. unsigned long val;
  821. int ret;
  822. if (kstrtoul(buf, 10, &val))
  823. return -EINVAL;
  824. ret = do_set_fan_target(dev, val);
  825. if (ret < 0)
  826. return ret;
  827. return count;
  828. }
  829. /* read function for fan1_fault sysfs file. */
  830. static ssize_t get_fan_failure(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_FAIL));
  837. }
  838. /*
  839. * read function for fan1_alarm sysfs file. Note that OOC condition is
  840. * enabled low
  841. */
  842. static ssize_t get_fan_ooc(struct device *dev, struct device_attribute *da,
  843. char *buf)
  844. {
  845. struct g762_data *data = g762_update_client(dev);
  846. if (IS_ERR(data))
  847. return PTR_ERR(data);
  848. return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC));
  849. }
  850. static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
  851. static DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, get_pwm_mode, set_pwm_mode);
  852. static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  853. get_pwm_enable, set_pwm_enable);
  854. static DEVICE_ATTR(fan1_input, S_IRUGO, get_fan_rpm, NULL);
  855. static DEVICE_ATTR(fan1_alarm, S_IRUGO, get_fan_ooc, NULL);
  856. static DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_failure, NULL);
  857. static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO,
  858. get_fan_target, set_fan_target);
  859. static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_fan_div, set_fan_div);
  860. static DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO,
  861. get_fan_pulses, set_fan_pulses);
  862. /* Driver data */
  863. static struct attribute *g762_attributes[] = {
  864. &dev_attr_fan1_input.attr,
  865. &dev_attr_fan1_alarm.attr,
  866. &dev_attr_fan1_fault.attr,
  867. &dev_attr_fan1_target.attr,
  868. &dev_attr_fan1_div.attr,
  869. &dev_attr_fan1_pulses.attr,
  870. &dev_attr_pwm1.attr,
  871. &dev_attr_pwm1_mode.attr,
  872. &dev_attr_pwm1_enable.attr,
  873. NULL
  874. };
  875. static const struct attribute_group g762_group = {
  876. .attrs = g762_attributes,
  877. };
  878. /*
  879. * Enable both fan failure detection and fan out of control protection. The
  880. * function does not protect change/access to data structure; it must thus
  881. * only be called during initialization.
  882. */
  883. static inline int g762_fan_init(struct device *dev)
  884. {
  885. struct i2c_client *client = to_i2c_client(dev);
  886. struct g762_data *data = g762_update_client(dev);
  887. if (IS_ERR(data))
  888. return PTR_ERR(data);
  889. data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL;
  890. data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
  891. data->valid = false;
  892. return i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1,
  893. data->fan_cmd1);
  894. }
  895. static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
  896. {
  897. struct g762_data *data;
  898. int ret;
  899. if (!i2c_check_functionality(client->adapter,
  900. I2C_FUNC_SMBUS_BYTE_DATA))
  901. return -ENODEV;
  902. data = devm_kzalloc(&client->dev, sizeof(struct g762_data), GFP_KERNEL);
  903. if (!data)
  904. return -ENOMEM;
  905. i2c_set_clientdata(client, data);
  906. data->client = client;
  907. mutex_init(&data->update_lock);
  908. /* Enable fan failure detection and fan out of control protection */
  909. ret = g762_fan_init(&client->dev);
  910. if (ret)
  911. return ret;
  912. /* Get configuration via DT ... */
  913. ret = g762_of_clock_enable(client);
  914. if (ret)
  915. return ret;
  916. ret = g762_of_prop_import(client);
  917. if (ret)
  918. goto clock_dis;
  919. /* ... or platform_data */
  920. ret = g762_pdata_prop_import(client);
  921. if (ret)
  922. goto clock_dis;
  923. /* Register sysfs hooks */
  924. ret = sysfs_create_group(&client->dev.kobj, &g762_group);
  925. if (ret)
  926. goto clock_dis;
  927. data->hwmon_dev = hwmon_device_register(&client->dev);
  928. if (IS_ERR(data->hwmon_dev)) {
  929. ret = PTR_ERR(data->hwmon_dev);
  930. goto sysfs_rem;
  931. }
  932. return 0;
  933. sysfs_rem:
  934. sysfs_remove_group(&client->dev.kobj, &g762_group);
  935. clock_dis:
  936. g762_of_clock_disable(client);
  937. return ret;
  938. }
  939. static int g762_remove(struct i2c_client *client)
  940. {
  941. struct g762_data *data = i2c_get_clientdata(client);
  942. hwmon_device_unregister(data->hwmon_dev);
  943. sysfs_remove_group(&client->dev.kobj, &g762_group);
  944. g762_of_clock_disable(client);
  945. return 0;
  946. }
  947. static struct i2c_driver g762_driver = {
  948. .driver = {
  949. .name = DRVNAME,
  950. .owner = THIS_MODULE,
  951. .of_match_table = of_match_ptr(g762_dt_match),
  952. },
  953. .probe = g762_probe,
  954. .remove = g762_remove,
  955. .id_table = g762_id,
  956. };
  957. module_i2c_driver(g762_driver);
  958. MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
  959. MODULE_DESCRIPTION("GMT G762/G763 driver");
  960. MODULE_LICENSE("GPL");