npcm750-pwm-fan.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2014-2018 Nuvoton Technology corporation.
  3. #include <linux/clk.h>
  4. #include <linux/device.h>
  5. #include <linux/hwmon.h>
  6. #include <linux/hwmon-sysfs.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/thermal.h>
  16. /* NPCM7XX PWM registers */
  17. #define NPCM7XX_PWM_REG_BASE(base, n) ((base) + ((n) * 0x1000L))
  18. #define NPCM7XX_PWM_REG_PR(base, n) (NPCM7XX_PWM_REG_BASE(base, n) + 0x00)
  19. #define NPCM7XX_PWM_REG_CSR(base, n) (NPCM7XX_PWM_REG_BASE(base, n) + 0x04)
  20. #define NPCM7XX_PWM_REG_CR(base, n) (NPCM7XX_PWM_REG_BASE(base, n) + 0x08)
  21. #define NPCM7XX_PWM_REG_CNRx(base, n, ch) \
  22. (NPCM7XX_PWM_REG_BASE(base, n) + 0x0C + (12 * (ch)))
  23. #define NPCM7XX_PWM_REG_CMRx(base, n, ch) \
  24. (NPCM7XX_PWM_REG_BASE(base, n) + 0x10 + (12 * (ch)))
  25. #define NPCM7XX_PWM_REG_PDRx(base, n, ch) \
  26. (NPCM7XX_PWM_REG_BASE(base, n) + 0x14 + (12 * (ch)))
  27. #define NPCM7XX_PWM_REG_PIER(base, n) (NPCM7XX_PWM_REG_BASE(base, n) + 0x3C)
  28. #define NPCM7XX_PWM_REG_PIIR(base, n) (NPCM7XX_PWM_REG_BASE(base, n) + 0x40)
  29. #define NPCM7XX_PWM_CTRL_CH0_MODE_BIT BIT(3)
  30. #define NPCM7XX_PWM_CTRL_CH1_MODE_BIT BIT(11)
  31. #define NPCM7XX_PWM_CTRL_CH2_MODE_BIT BIT(15)
  32. #define NPCM7XX_PWM_CTRL_CH3_MODE_BIT BIT(19)
  33. #define NPCM7XX_PWM_CTRL_CH0_INV_BIT BIT(2)
  34. #define NPCM7XX_PWM_CTRL_CH1_INV_BIT BIT(10)
  35. #define NPCM7XX_PWM_CTRL_CH2_INV_BIT BIT(14)
  36. #define NPCM7XX_PWM_CTRL_CH3_INV_BIT BIT(18)
  37. #define NPCM7XX_PWM_CTRL_CH0_EN_BIT BIT(0)
  38. #define NPCM7XX_PWM_CTRL_CH1_EN_BIT BIT(8)
  39. #define NPCM7XX_PWM_CTRL_CH2_EN_BIT BIT(12)
  40. #define NPCM7XX_PWM_CTRL_CH3_EN_BIT BIT(16)
  41. /* Define the maximum PWM channel number */
  42. #define NPCM7XX_PWM_MAX_CHN_NUM 8
  43. #define NPCM7XX_PWM_MAX_CHN_NUM_IN_A_MODULE 4
  44. #define NPCM7XX_PWM_MAX_MODULES 2
  45. /* Define the Counter Register, value = 100 for match 100% */
  46. #define NPCM7XX_PWM_COUNTER_DEFAULT_NUM 255
  47. #define NPCM7XX_PWM_CMR_DEFAULT_NUM 255
  48. #define NPCM7XX_PWM_CMR_MAX 255
  49. /* default all PWM channels PRESCALE2 = 1 */
  50. #define NPCM7XX_PWM_PRESCALE2_DEFAULT_CH0 0x4
  51. #define NPCM7XX_PWM_PRESCALE2_DEFAULT_CH1 0x40
  52. #define NPCM7XX_PWM_PRESCALE2_DEFAULT_CH2 0x400
  53. #define NPCM7XX_PWM_PRESCALE2_DEFAULT_CH3 0x4000
  54. #define PWM_OUTPUT_FREQ_25KHZ 25000
  55. #define PWN_CNT_DEFAULT 256
  56. #define MIN_PRESCALE1 2
  57. #define NPCM7XX_PWM_PRESCALE_SHIFT_CH01 8
  58. #define NPCM7XX_PWM_PRESCALE2_DEFAULT (NPCM7XX_PWM_PRESCALE2_DEFAULT_CH0 | \
  59. NPCM7XX_PWM_PRESCALE2_DEFAULT_CH1 | \
  60. NPCM7XX_PWM_PRESCALE2_DEFAULT_CH2 | \
  61. NPCM7XX_PWM_PRESCALE2_DEFAULT_CH3)
  62. #define NPCM7XX_PWM_CTRL_MODE_DEFAULT (NPCM7XX_PWM_CTRL_CH0_MODE_BIT | \
  63. NPCM7XX_PWM_CTRL_CH1_MODE_BIT | \
  64. NPCM7XX_PWM_CTRL_CH2_MODE_BIT | \
  65. NPCM7XX_PWM_CTRL_CH3_MODE_BIT)
  66. /* NPCM7XX FAN Tacho registers */
  67. #define NPCM7XX_FAN_REG_BASE(base, n) ((base) + ((n) * 0x1000L))
  68. #define NPCM7XX_FAN_REG_TCNT1(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x00)
  69. #define NPCM7XX_FAN_REG_TCRA(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x02)
  70. #define NPCM7XX_FAN_REG_TCRB(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x04)
  71. #define NPCM7XX_FAN_REG_TCNT2(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x06)
  72. #define NPCM7XX_FAN_REG_TPRSC(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x08)
  73. #define NPCM7XX_FAN_REG_TCKC(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x0A)
  74. #define NPCM7XX_FAN_REG_TMCTRL(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x0C)
  75. #define NPCM7XX_FAN_REG_TICTRL(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x0E)
  76. #define NPCM7XX_FAN_REG_TICLR(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x10)
  77. #define NPCM7XX_FAN_REG_TIEN(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x12)
  78. #define NPCM7XX_FAN_REG_TCPA(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x14)
  79. #define NPCM7XX_FAN_REG_TCPB(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x16)
  80. #define NPCM7XX_FAN_REG_TCPCFG(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x18)
  81. #define NPCM7XX_FAN_REG_TINASEL(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x1A)
  82. #define NPCM7XX_FAN_REG_TINBSEL(base, n) (NPCM7XX_FAN_REG_BASE(base, n) + 0x1C)
  83. #define NPCM7XX_FAN_TCKC_CLKX_NONE 0
  84. #define NPCM7XX_FAN_TCKC_CLK1_APB BIT(0)
  85. #define NPCM7XX_FAN_TCKC_CLK2_APB BIT(3)
  86. #define NPCM7XX_FAN_TMCTRL_TBEN BIT(6)
  87. #define NPCM7XX_FAN_TMCTRL_TAEN BIT(5)
  88. #define NPCM7XX_FAN_TMCTRL_TBEDG BIT(4)
  89. #define NPCM7XX_FAN_TMCTRL_TAEDG BIT(3)
  90. #define NPCM7XX_FAN_TMCTRL_MODE_5 BIT(2)
  91. #define NPCM7XX_FAN_TICLR_CLEAR_ALL GENMASK(5, 0)
  92. #define NPCM7XX_FAN_TICLR_TFCLR BIT(5)
  93. #define NPCM7XX_FAN_TICLR_TECLR BIT(4)
  94. #define NPCM7XX_FAN_TICLR_TDCLR BIT(3)
  95. #define NPCM7XX_FAN_TICLR_TCCLR BIT(2)
  96. #define NPCM7XX_FAN_TICLR_TBCLR BIT(1)
  97. #define NPCM7XX_FAN_TICLR_TACLR BIT(0)
  98. #define NPCM7XX_FAN_TIEN_ENABLE_ALL GENMASK(5, 0)
  99. #define NPCM7XX_FAN_TIEN_TFIEN BIT(5)
  100. #define NPCM7XX_FAN_TIEN_TEIEN BIT(4)
  101. #define NPCM7XX_FAN_TIEN_TDIEN BIT(3)
  102. #define NPCM7XX_FAN_TIEN_TCIEN BIT(2)
  103. #define NPCM7XX_FAN_TIEN_TBIEN BIT(1)
  104. #define NPCM7XX_FAN_TIEN_TAIEN BIT(0)
  105. #define NPCM7XX_FAN_TICTRL_TFPND BIT(5)
  106. #define NPCM7XX_FAN_TICTRL_TEPND BIT(4)
  107. #define NPCM7XX_FAN_TICTRL_TDPND BIT(3)
  108. #define NPCM7XX_FAN_TICTRL_TCPND BIT(2)
  109. #define NPCM7XX_FAN_TICTRL_TBPND BIT(1)
  110. #define NPCM7XX_FAN_TICTRL_TAPND BIT(0)
  111. #define NPCM7XX_FAN_TCPCFG_HIBEN BIT(7)
  112. #define NPCM7XX_FAN_TCPCFG_EQBEN BIT(6)
  113. #define NPCM7XX_FAN_TCPCFG_LOBEN BIT(5)
  114. #define NPCM7XX_FAN_TCPCFG_CPBSEL BIT(4)
  115. #define NPCM7XX_FAN_TCPCFG_HIAEN BIT(3)
  116. #define NPCM7XX_FAN_TCPCFG_EQAEN BIT(2)
  117. #define NPCM7XX_FAN_TCPCFG_LOAEN BIT(1)
  118. #define NPCM7XX_FAN_TCPCFG_CPASEL BIT(0)
  119. /* FAN General Definition */
  120. /* Define the maximum FAN channel number */
  121. #define NPCM7XX_FAN_MAX_MODULE 8
  122. #define NPCM7XX_FAN_MAX_CHN_NUM_IN_A_MODULE 2
  123. #define NPCM7XX_FAN_MAX_CHN_NUM 16
  124. /*
  125. * Get Fan Tach Timeout (base on clock 214843.75Hz, 1 cnt = 4.654us)
  126. * Timeout 94ms ~= 0x5000
  127. * (The minimum FAN speed could to support ~640RPM/pulse 1,
  128. * 320RPM/pulse 2, ...-- 10.6Hz)
  129. */
  130. #define NPCM7XX_FAN_TIMEOUT 0x5000
  131. #define NPCM7XX_FAN_TCNT 0xFFFF
  132. #define NPCM7XX_FAN_TCPA (NPCM7XX_FAN_TCNT - NPCM7XX_FAN_TIMEOUT)
  133. #define NPCM7XX_FAN_TCPB (NPCM7XX_FAN_TCNT - NPCM7XX_FAN_TIMEOUT)
  134. #define NPCM7XX_FAN_POLL_TIMER_200MS 200
  135. #define NPCM7XX_FAN_DEFAULT_PULSE_PER_REVOLUTION 2
  136. #define NPCM7XX_FAN_TINASEL_FANIN_DEFAULT 0
  137. #define NPCM7XX_FAN_CLK_PRESCALE 255
  138. #define NPCM7XX_FAN_CMPA 0
  139. #define NPCM7XX_FAN_CMPB 1
  140. /* Obtain the fan number */
  141. #define NPCM7XX_FAN_INPUT(fan, cmp) (((fan) << 1) + (cmp))
  142. /* fan sample status */
  143. #define FAN_DISABLE 0xFF
  144. #define FAN_INIT 0x00
  145. #define FAN_PREPARE_TO_GET_FIRST_CAPTURE 0x01
  146. #define FAN_ENOUGH_SAMPLE 0x02
  147. struct npcm7xx_fan_dev {
  148. u8 fan_st_flg;
  149. u8 fan_pls_per_rev;
  150. u16 fan_cnt;
  151. u32 fan_cnt_tmp;
  152. };
  153. struct npcm7xx_cooling_device {
  154. char name[THERMAL_NAME_LENGTH];
  155. struct npcm7xx_pwm_fan_data *data;
  156. struct thermal_cooling_device *tcdev;
  157. int pwm_port;
  158. u8 *cooling_levels;
  159. u8 max_state;
  160. u8 cur_state;
  161. };
  162. struct npcm7xx_pwm_fan_data {
  163. void __iomem *pwm_base;
  164. void __iomem *fan_base;
  165. unsigned long pwm_clk_freq;
  166. unsigned long fan_clk_freq;
  167. struct clk *pwm_clk;
  168. struct clk *fan_clk;
  169. struct mutex pwm_lock[NPCM7XX_PWM_MAX_MODULES];
  170. spinlock_t fan_lock[NPCM7XX_FAN_MAX_MODULE];
  171. int fan_irq[NPCM7XX_FAN_MAX_MODULE];
  172. bool pwm_present[NPCM7XX_PWM_MAX_CHN_NUM];
  173. bool fan_present[NPCM7XX_FAN_MAX_CHN_NUM];
  174. u32 input_clk_freq;
  175. struct timer_list fan_timer;
  176. struct npcm7xx_fan_dev fan_dev[NPCM7XX_FAN_MAX_CHN_NUM];
  177. struct npcm7xx_cooling_device *cdev[NPCM7XX_PWM_MAX_CHN_NUM];
  178. u8 fan_select;
  179. };
  180. static int npcm7xx_pwm_config_set(struct npcm7xx_pwm_fan_data *data,
  181. int channel, u16 val)
  182. {
  183. u32 pwm_ch = (channel % NPCM7XX_PWM_MAX_CHN_NUM_IN_A_MODULE);
  184. u32 module = (channel / NPCM7XX_PWM_MAX_CHN_NUM_IN_A_MODULE);
  185. u32 tmp_buf, ctrl_en_bit, env_bit;
  186. /*
  187. * Config PWM Comparator register for setting duty cycle
  188. */
  189. mutex_lock(&data->pwm_lock[module]);
  190. /* write new CMR value */
  191. iowrite32(val, NPCM7XX_PWM_REG_CMRx(data->pwm_base, module, pwm_ch));
  192. tmp_buf = ioread32(NPCM7XX_PWM_REG_CR(data->pwm_base, module));
  193. switch (pwm_ch) {
  194. case 0:
  195. ctrl_en_bit = NPCM7XX_PWM_CTRL_CH0_EN_BIT;
  196. env_bit = NPCM7XX_PWM_CTRL_CH0_INV_BIT;
  197. break;
  198. case 1:
  199. ctrl_en_bit = NPCM7XX_PWM_CTRL_CH1_EN_BIT;
  200. env_bit = NPCM7XX_PWM_CTRL_CH1_INV_BIT;
  201. break;
  202. case 2:
  203. ctrl_en_bit = NPCM7XX_PWM_CTRL_CH2_EN_BIT;
  204. env_bit = NPCM7XX_PWM_CTRL_CH2_INV_BIT;
  205. break;
  206. case 3:
  207. ctrl_en_bit = NPCM7XX_PWM_CTRL_CH3_EN_BIT;
  208. env_bit = NPCM7XX_PWM_CTRL_CH3_INV_BIT;
  209. break;
  210. default:
  211. mutex_unlock(&data->pwm_lock[module]);
  212. return -ENODEV;
  213. }
  214. if (val == 0) {
  215. /* Disable PWM */
  216. tmp_buf &= ~ctrl_en_bit;
  217. tmp_buf |= env_bit;
  218. } else {
  219. /* Enable PWM */
  220. tmp_buf |= ctrl_en_bit;
  221. tmp_buf &= ~env_bit;
  222. }
  223. iowrite32(tmp_buf, NPCM7XX_PWM_REG_CR(data->pwm_base, module));
  224. mutex_unlock(&data->pwm_lock[module]);
  225. return 0;
  226. }
  227. static inline void npcm7xx_fan_start_capture(struct npcm7xx_pwm_fan_data *data,
  228. u8 fan, u8 cmp)
  229. {
  230. u8 fan_id;
  231. u8 reg_mode;
  232. u8 reg_int;
  233. unsigned long flags;
  234. fan_id = NPCM7XX_FAN_INPUT(fan, cmp);
  235. /* to check whether any fan tach is enable */
  236. if (data->fan_dev[fan_id].fan_st_flg != FAN_DISABLE) {
  237. /* reset status */
  238. spin_lock_irqsave(&data->fan_lock[fan], flags);
  239. data->fan_dev[fan_id].fan_st_flg = FAN_INIT;
  240. reg_int = ioread8(NPCM7XX_FAN_REG_TIEN(data->fan_base, fan));
  241. /*
  242. * the interrupt enable bits do not need to be cleared before
  243. * it sets, the interrupt enable bits are cleared only on reset.
  244. * the clock unit control register is behaving in the same
  245. * manner that the interrupt enable register behave.
  246. */
  247. if (cmp == NPCM7XX_FAN_CMPA) {
  248. /* enable interrupt */
  249. iowrite8(reg_int | (NPCM7XX_FAN_TIEN_TAIEN |
  250. NPCM7XX_FAN_TIEN_TEIEN),
  251. NPCM7XX_FAN_REG_TIEN(data->fan_base, fan));
  252. reg_mode = NPCM7XX_FAN_TCKC_CLK1_APB
  253. | ioread8(NPCM7XX_FAN_REG_TCKC(data->fan_base,
  254. fan));
  255. /* start to Capture */
  256. iowrite8(reg_mode, NPCM7XX_FAN_REG_TCKC(data->fan_base,
  257. fan));
  258. } else {
  259. /* enable interrupt */
  260. iowrite8(reg_int | (NPCM7XX_FAN_TIEN_TBIEN |
  261. NPCM7XX_FAN_TIEN_TFIEN),
  262. NPCM7XX_FAN_REG_TIEN(data->fan_base, fan));
  263. reg_mode =
  264. NPCM7XX_FAN_TCKC_CLK2_APB
  265. | ioread8(NPCM7XX_FAN_REG_TCKC(data->fan_base,
  266. fan));
  267. /* start to Capture */
  268. iowrite8(reg_mode,
  269. NPCM7XX_FAN_REG_TCKC(data->fan_base, fan));
  270. }
  271. spin_unlock_irqrestore(&data->fan_lock[fan], flags);
  272. }
  273. }
  274. /*
  275. * Enable a background timer to poll fan tach value, (200ms * 4)
  276. * to polling all fan
  277. */
  278. static void npcm7xx_fan_polling(struct timer_list *t)
  279. {
  280. struct npcm7xx_pwm_fan_data *data;
  281. int i;
  282. data = from_timer(data, t, fan_timer);
  283. /*
  284. * Polling two module per one round,
  285. * FAN01 & FAN89 / FAN23 & FAN1011 / FAN45 & FAN1213 / FAN67 & FAN1415
  286. */
  287. for (i = data->fan_select; i < NPCM7XX_FAN_MAX_MODULE;
  288. i = i + 4) {
  289. /* clear the flag and reset the counter (TCNT) */
  290. iowrite8(NPCM7XX_FAN_TICLR_CLEAR_ALL,
  291. NPCM7XX_FAN_REG_TICLR(data->fan_base, i));
  292. if (data->fan_present[i * 2]) {
  293. iowrite16(NPCM7XX_FAN_TCNT,
  294. NPCM7XX_FAN_REG_TCNT1(data->fan_base, i));
  295. npcm7xx_fan_start_capture(data, i, NPCM7XX_FAN_CMPA);
  296. }
  297. if (data->fan_present[(i * 2) + 1]) {
  298. iowrite16(NPCM7XX_FAN_TCNT,
  299. NPCM7XX_FAN_REG_TCNT2(data->fan_base, i));
  300. npcm7xx_fan_start_capture(data, i, NPCM7XX_FAN_CMPB);
  301. }
  302. }
  303. data->fan_select++;
  304. data->fan_select &= 0x3;
  305. /* reset the timer interval */
  306. data->fan_timer.expires = jiffies +
  307. msecs_to_jiffies(NPCM7XX_FAN_POLL_TIMER_200MS);
  308. add_timer(&data->fan_timer);
  309. }
  310. static inline void npcm7xx_fan_compute(struct npcm7xx_pwm_fan_data *data,
  311. u8 fan, u8 cmp, u8 fan_id, u8 flag_int,
  312. u8 flag_mode, u8 flag_clear)
  313. {
  314. u8 reg_int;
  315. u8 reg_mode;
  316. u16 fan_cap;
  317. if (cmp == NPCM7XX_FAN_CMPA)
  318. fan_cap = ioread16(NPCM7XX_FAN_REG_TCRA(data->fan_base, fan));
  319. else
  320. fan_cap = ioread16(NPCM7XX_FAN_REG_TCRB(data->fan_base, fan));
  321. /* clear capature flag, H/W will auto reset the NPCM7XX_FAN_TCNTx */
  322. iowrite8(flag_clear, NPCM7XX_FAN_REG_TICLR(data->fan_base, fan));
  323. if (data->fan_dev[fan_id].fan_st_flg == FAN_INIT) {
  324. /* First capture, drop it */
  325. data->fan_dev[fan_id].fan_st_flg =
  326. FAN_PREPARE_TO_GET_FIRST_CAPTURE;
  327. /* reset counter */
  328. data->fan_dev[fan_id].fan_cnt_tmp = 0;
  329. } else if (data->fan_dev[fan_id].fan_st_flg < FAN_ENOUGH_SAMPLE) {
  330. /*
  331. * collect the enough sample,
  332. * (ex: 2 pulse fan need to get 2 sample)
  333. */
  334. data->fan_dev[fan_id].fan_cnt_tmp +=
  335. (NPCM7XX_FAN_TCNT - fan_cap);
  336. data->fan_dev[fan_id].fan_st_flg++;
  337. } else {
  338. /* get enough sample or fan disable */
  339. if (data->fan_dev[fan_id].fan_st_flg == FAN_ENOUGH_SAMPLE) {
  340. data->fan_dev[fan_id].fan_cnt_tmp +=
  341. (NPCM7XX_FAN_TCNT - fan_cap);
  342. /* compute finial average cnt per pulse */
  343. data->fan_dev[fan_id].fan_cnt =
  344. data->fan_dev[fan_id].fan_cnt_tmp /
  345. FAN_ENOUGH_SAMPLE;
  346. data->fan_dev[fan_id].fan_st_flg = FAN_INIT;
  347. }
  348. reg_int = ioread8(NPCM7XX_FAN_REG_TIEN(data->fan_base, fan));
  349. /* disable interrupt */
  350. iowrite8((reg_int & ~flag_int),
  351. NPCM7XX_FAN_REG_TIEN(data->fan_base, fan));
  352. reg_mode = ioread8(NPCM7XX_FAN_REG_TCKC(data->fan_base, fan));
  353. /* stop capturing */
  354. iowrite8((reg_mode & ~flag_mode),
  355. NPCM7XX_FAN_REG_TCKC(data->fan_base, fan));
  356. }
  357. }
  358. static inline void npcm7xx_check_cmp(struct npcm7xx_pwm_fan_data *data,
  359. u8 fan, u8 cmp, u8 flag)
  360. {
  361. u8 reg_int;
  362. u8 reg_mode;
  363. u8 flag_timeout;
  364. u8 flag_cap;
  365. u8 flag_clear;
  366. u8 flag_int;
  367. u8 flag_mode;
  368. u8 fan_id;
  369. fan_id = NPCM7XX_FAN_INPUT(fan, cmp);
  370. if (cmp == NPCM7XX_FAN_CMPA) {
  371. flag_cap = NPCM7XX_FAN_TICTRL_TAPND;
  372. flag_timeout = NPCM7XX_FAN_TICTRL_TEPND;
  373. flag_int = NPCM7XX_FAN_TIEN_TAIEN | NPCM7XX_FAN_TIEN_TEIEN;
  374. flag_mode = NPCM7XX_FAN_TCKC_CLK1_APB;
  375. flag_clear = NPCM7XX_FAN_TICLR_TACLR | NPCM7XX_FAN_TICLR_TECLR;
  376. } else {
  377. flag_cap = NPCM7XX_FAN_TICTRL_TBPND;
  378. flag_timeout = NPCM7XX_FAN_TICTRL_TFPND;
  379. flag_int = NPCM7XX_FAN_TIEN_TBIEN | NPCM7XX_FAN_TIEN_TFIEN;
  380. flag_mode = NPCM7XX_FAN_TCKC_CLK2_APB;
  381. flag_clear = NPCM7XX_FAN_TICLR_TBCLR | NPCM7XX_FAN_TICLR_TFCLR;
  382. }
  383. if (flag & flag_timeout) {
  384. reg_int = ioread8(NPCM7XX_FAN_REG_TIEN(data->fan_base, fan));
  385. /* disable interrupt */
  386. iowrite8((reg_int & ~flag_int),
  387. NPCM7XX_FAN_REG_TIEN(data->fan_base, fan));
  388. /* clear interrupt flag */
  389. iowrite8(flag_clear,
  390. NPCM7XX_FAN_REG_TICLR(data->fan_base, fan));
  391. reg_mode = ioread8(NPCM7XX_FAN_REG_TCKC(data->fan_base, fan));
  392. /* stop capturing */
  393. iowrite8((reg_mode & ~flag_mode),
  394. NPCM7XX_FAN_REG_TCKC(data->fan_base, fan));
  395. /*
  396. * If timeout occurs (NPCM7XX_FAN_TIMEOUT), the fan doesn't
  397. * connect or speed is lower than 10.6Hz (320RPM/pulse2).
  398. * In these situation, the RPM output should be zero.
  399. */
  400. data->fan_dev[fan_id].fan_cnt = 0;
  401. } else {
  402. /* input capture is occurred */
  403. if (flag & flag_cap)
  404. npcm7xx_fan_compute(data, fan, cmp, fan_id, flag_int,
  405. flag_mode, flag_clear);
  406. }
  407. }
  408. static irqreturn_t npcm7xx_fan_isr(int irq, void *dev_id)
  409. {
  410. struct npcm7xx_pwm_fan_data *data = dev_id;
  411. unsigned long flags;
  412. int module;
  413. u8 flag;
  414. module = irq - data->fan_irq[0];
  415. spin_lock_irqsave(&data->fan_lock[module], flags);
  416. flag = ioread8(NPCM7XX_FAN_REG_TICTRL(data->fan_base, module));
  417. if (flag > 0) {
  418. npcm7xx_check_cmp(data, module, NPCM7XX_FAN_CMPA, flag);
  419. npcm7xx_check_cmp(data, module, NPCM7XX_FAN_CMPB, flag);
  420. spin_unlock_irqrestore(&data->fan_lock[module], flags);
  421. return IRQ_HANDLED;
  422. }
  423. spin_unlock_irqrestore(&data->fan_lock[module], flags);
  424. return IRQ_NONE;
  425. }
  426. static int npcm7xx_read_pwm(struct device *dev, u32 attr, int channel,
  427. long *val)
  428. {
  429. struct npcm7xx_pwm_fan_data *data = dev_get_drvdata(dev);
  430. u32 pmw_ch = (channel % NPCM7XX_PWM_MAX_CHN_NUM_IN_A_MODULE);
  431. u32 module = (channel / NPCM7XX_PWM_MAX_CHN_NUM_IN_A_MODULE);
  432. switch (attr) {
  433. case hwmon_pwm_input:
  434. *val = ioread32
  435. (NPCM7XX_PWM_REG_CMRx(data->pwm_base, module, pmw_ch));
  436. return 0;
  437. default:
  438. return -EOPNOTSUPP;
  439. }
  440. }
  441. static int npcm7xx_write_pwm(struct device *dev, u32 attr, int channel,
  442. long val)
  443. {
  444. struct npcm7xx_pwm_fan_data *data = dev_get_drvdata(dev);
  445. int err;
  446. switch (attr) {
  447. case hwmon_pwm_input:
  448. if (val < 0 || val > NPCM7XX_PWM_CMR_MAX)
  449. return -EINVAL;
  450. err = npcm7xx_pwm_config_set(data, channel, (u16)val);
  451. break;
  452. default:
  453. err = -EOPNOTSUPP;
  454. break;
  455. }
  456. return err;
  457. }
  458. static umode_t npcm7xx_pwm_is_visible(const void *_data, u32 attr, int channel)
  459. {
  460. const struct npcm7xx_pwm_fan_data *data = _data;
  461. if (!data->pwm_present[channel])
  462. return 0;
  463. switch (attr) {
  464. case hwmon_pwm_input:
  465. return 0644;
  466. default:
  467. return 0;
  468. }
  469. }
  470. static int npcm7xx_read_fan(struct device *dev, u32 attr, int channel,
  471. long *val)
  472. {
  473. struct npcm7xx_pwm_fan_data *data = dev_get_drvdata(dev);
  474. switch (attr) {
  475. case hwmon_fan_input:
  476. *val = 0;
  477. if (data->fan_dev[channel].fan_cnt <= 0)
  478. return data->fan_dev[channel].fan_cnt;
  479. /* Convert the raw reading to RPM */
  480. if (data->fan_dev[channel].fan_cnt > 0 &&
  481. data->fan_dev[channel].fan_pls_per_rev > 0)
  482. *val = ((data->input_clk_freq * 60) /
  483. (data->fan_dev[channel].fan_cnt *
  484. data->fan_dev[channel].fan_pls_per_rev));
  485. return 0;
  486. default:
  487. return -EOPNOTSUPP;
  488. }
  489. }
  490. static umode_t npcm7xx_fan_is_visible(const void *_data, u32 attr, int channel)
  491. {
  492. const struct npcm7xx_pwm_fan_data *data = _data;
  493. if (!data->fan_present[channel])
  494. return 0;
  495. switch (attr) {
  496. case hwmon_fan_input:
  497. return 0444;
  498. default:
  499. return 0;
  500. }
  501. }
  502. static int npcm7xx_read(struct device *dev, enum hwmon_sensor_types type,
  503. u32 attr, int channel, long *val)
  504. {
  505. switch (type) {
  506. case hwmon_pwm:
  507. return npcm7xx_read_pwm(dev, attr, channel, val);
  508. case hwmon_fan:
  509. return npcm7xx_read_fan(dev, attr, channel, val);
  510. default:
  511. return -EOPNOTSUPP;
  512. }
  513. }
  514. static int npcm7xx_write(struct device *dev, enum hwmon_sensor_types type,
  515. u32 attr, int channel, long val)
  516. {
  517. switch (type) {
  518. case hwmon_pwm:
  519. return npcm7xx_write_pwm(dev, attr, channel, val);
  520. default:
  521. return -EOPNOTSUPP;
  522. }
  523. }
  524. static umode_t npcm7xx_is_visible(const void *data,
  525. enum hwmon_sensor_types type,
  526. u32 attr, int channel)
  527. {
  528. switch (type) {
  529. case hwmon_pwm:
  530. return npcm7xx_pwm_is_visible(data, attr, channel);
  531. case hwmon_fan:
  532. return npcm7xx_fan_is_visible(data, attr, channel);
  533. default:
  534. return 0;
  535. }
  536. }
  537. static const u32 npcm7xx_pwm_config[] = {
  538. HWMON_PWM_INPUT,
  539. HWMON_PWM_INPUT,
  540. HWMON_PWM_INPUT,
  541. HWMON_PWM_INPUT,
  542. HWMON_PWM_INPUT,
  543. HWMON_PWM_INPUT,
  544. HWMON_PWM_INPUT,
  545. HWMON_PWM_INPUT,
  546. 0
  547. };
  548. static const struct hwmon_channel_info npcm7xx_pwm = {
  549. .type = hwmon_pwm,
  550. .config = npcm7xx_pwm_config,
  551. };
  552. static const u32 npcm7xx_fan_config[] = {
  553. HWMON_F_INPUT,
  554. HWMON_F_INPUT,
  555. HWMON_F_INPUT,
  556. HWMON_F_INPUT,
  557. HWMON_F_INPUT,
  558. HWMON_F_INPUT,
  559. HWMON_F_INPUT,
  560. HWMON_F_INPUT,
  561. HWMON_F_INPUT,
  562. HWMON_F_INPUT,
  563. HWMON_F_INPUT,
  564. HWMON_F_INPUT,
  565. HWMON_F_INPUT,
  566. HWMON_F_INPUT,
  567. HWMON_F_INPUT,
  568. HWMON_F_INPUT,
  569. 0
  570. };
  571. static const struct hwmon_channel_info npcm7xx_fan = {
  572. .type = hwmon_fan,
  573. .config = npcm7xx_fan_config,
  574. };
  575. static const struct hwmon_channel_info *npcm7xx_info[] = {
  576. &npcm7xx_pwm,
  577. &npcm7xx_fan,
  578. NULL
  579. };
  580. static const struct hwmon_ops npcm7xx_hwmon_ops = {
  581. .is_visible = npcm7xx_is_visible,
  582. .read = npcm7xx_read,
  583. .write = npcm7xx_write,
  584. };
  585. static const struct hwmon_chip_info npcm7xx_chip_info = {
  586. .ops = &npcm7xx_hwmon_ops,
  587. .info = npcm7xx_info,
  588. };
  589. static u32 npcm7xx_pwm_init(struct npcm7xx_pwm_fan_data *data)
  590. {
  591. int m, ch;
  592. u32 prescale_val, output_freq;
  593. data->pwm_clk_freq = clk_get_rate(data->pwm_clk);
  594. /* Adjust NPCM7xx PWMs output frequency to ~25Khz */
  595. output_freq = data->pwm_clk_freq / PWN_CNT_DEFAULT;
  596. prescale_val = DIV_ROUND_CLOSEST(output_freq, PWM_OUTPUT_FREQ_25KHZ);
  597. /* If prescale_val = 0, then the prescale output clock is stopped */
  598. if (prescale_val < MIN_PRESCALE1)
  599. prescale_val = MIN_PRESCALE1;
  600. /*
  601. * prescale_val need to decrement in one because in the PWM Prescale
  602. * register the Prescale value increment by one
  603. */
  604. prescale_val--;
  605. /* Setting PWM Prescale Register value register to both modules */
  606. prescale_val |= (prescale_val << NPCM7XX_PWM_PRESCALE_SHIFT_CH01);
  607. for (m = 0; m < NPCM7XX_PWM_MAX_MODULES ; m++) {
  608. iowrite32(prescale_val, NPCM7XX_PWM_REG_PR(data->pwm_base, m));
  609. iowrite32(NPCM7XX_PWM_PRESCALE2_DEFAULT,
  610. NPCM7XX_PWM_REG_CSR(data->pwm_base, m));
  611. iowrite32(NPCM7XX_PWM_CTRL_MODE_DEFAULT,
  612. NPCM7XX_PWM_REG_CR(data->pwm_base, m));
  613. for (ch = 0; ch < NPCM7XX_PWM_MAX_CHN_NUM_IN_A_MODULE; ch++) {
  614. iowrite32(NPCM7XX_PWM_COUNTER_DEFAULT_NUM,
  615. NPCM7XX_PWM_REG_CNRx(data->pwm_base, m, ch));
  616. }
  617. }
  618. return output_freq / ((prescale_val & 0xf) + 1);
  619. }
  620. static void npcm7xx_fan_init(struct npcm7xx_pwm_fan_data *data)
  621. {
  622. int md;
  623. int ch;
  624. int i;
  625. u32 apb_clk_freq;
  626. for (md = 0; md < NPCM7XX_FAN_MAX_MODULE; md++) {
  627. /* stop FAN0~7 clock */
  628. iowrite8(NPCM7XX_FAN_TCKC_CLKX_NONE,
  629. NPCM7XX_FAN_REG_TCKC(data->fan_base, md));
  630. /* disable all interrupt */
  631. iowrite8(0x00, NPCM7XX_FAN_REG_TIEN(data->fan_base, md));
  632. /* clear all interrupt */
  633. iowrite8(NPCM7XX_FAN_TICLR_CLEAR_ALL,
  634. NPCM7XX_FAN_REG_TICLR(data->fan_base, md));
  635. /* set FAN0~7 clock prescaler */
  636. iowrite8(NPCM7XX_FAN_CLK_PRESCALE,
  637. NPCM7XX_FAN_REG_TPRSC(data->fan_base, md));
  638. /* set FAN0~7 mode (high-to-low transition) */
  639. iowrite8((NPCM7XX_FAN_TMCTRL_MODE_5 | NPCM7XX_FAN_TMCTRL_TBEN |
  640. NPCM7XX_FAN_TMCTRL_TAEN),
  641. NPCM7XX_FAN_REG_TMCTRL(data->fan_base, md));
  642. /* set FAN0~7 Initial Count/Cap */
  643. iowrite16(NPCM7XX_FAN_TCNT,
  644. NPCM7XX_FAN_REG_TCNT1(data->fan_base, md));
  645. iowrite16(NPCM7XX_FAN_TCNT,
  646. NPCM7XX_FAN_REG_TCNT2(data->fan_base, md));
  647. /* set FAN0~7 compare (equal to count) */
  648. iowrite8((NPCM7XX_FAN_TCPCFG_EQAEN | NPCM7XX_FAN_TCPCFG_EQBEN),
  649. NPCM7XX_FAN_REG_TCPCFG(data->fan_base, md));
  650. /* set FAN0~7 compare value */
  651. iowrite16(NPCM7XX_FAN_TCPA,
  652. NPCM7XX_FAN_REG_TCPA(data->fan_base, md));
  653. iowrite16(NPCM7XX_FAN_TCPB,
  654. NPCM7XX_FAN_REG_TCPB(data->fan_base, md));
  655. /* set FAN0~7 fan input FANIN 0~15 */
  656. iowrite8(NPCM7XX_FAN_TINASEL_FANIN_DEFAULT,
  657. NPCM7XX_FAN_REG_TINASEL(data->fan_base, md));
  658. iowrite8(NPCM7XX_FAN_TINASEL_FANIN_DEFAULT,
  659. NPCM7XX_FAN_REG_TINBSEL(data->fan_base, md));
  660. for (i = 0; i < NPCM7XX_FAN_MAX_CHN_NUM_IN_A_MODULE; i++) {
  661. ch = md * NPCM7XX_FAN_MAX_CHN_NUM_IN_A_MODULE + i;
  662. data->fan_dev[ch].fan_st_flg = FAN_DISABLE;
  663. data->fan_dev[ch].fan_pls_per_rev =
  664. NPCM7XX_FAN_DEFAULT_PULSE_PER_REVOLUTION;
  665. data->fan_dev[ch].fan_cnt = 0;
  666. }
  667. }
  668. apb_clk_freq = clk_get_rate(data->fan_clk);
  669. /* Fan tach input clock = APB clock / prescalar, default is 255. */
  670. data->input_clk_freq = apb_clk_freq / (NPCM7XX_FAN_CLK_PRESCALE + 1);
  671. }
  672. static int
  673. npcm7xx_pwm_cz_get_max_state(struct thermal_cooling_device *tcdev,
  674. unsigned long *state)
  675. {
  676. struct npcm7xx_cooling_device *cdev = tcdev->devdata;
  677. *state = cdev->max_state;
  678. return 0;
  679. }
  680. static int
  681. npcm7xx_pwm_cz_get_cur_state(struct thermal_cooling_device *tcdev,
  682. unsigned long *state)
  683. {
  684. struct npcm7xx_cooling_device *cdev = tcdev->devdata;
  685. *state = cdev->cur_state;
  686. return 0;
  687. }
  688. static int
  689. npcm7xx_pwm_cz_set_cur_state(struct thermal_cooling_device *tcdev,
  690. unsigned long state)
  691. {
  692. struct npcm7xx_cooling_device *cdev = tcdev->devdata;
  693. int ret;
  694. if (state > cdev->max_state)
  695. return -EINVAL;
  696. cdev->cur_state = state;
  697. ret = npcm7xx_pwm_config_set(cdev->data, cdev->pwm_port,
  698. cdev->cooling_levels[cdev->cur_state]);
  699. return ret;
  700. }
  701. static const struct thermal_cooling_device_ops npcm7xx_pwm_cool_ops = {
  702. .get_max_state = npcm7xx_pwm_cz_get_max_state,
  703. .get_cur_state = npcm7xx_pwm_cz_get_cur_state,
  704. .set_cur_state = npcm7xx_pwm_cz_set_cur_state,
  705. };
  706. static int npcm7xx_create_pwm_cooling(struct device *dev,
  707. struct device_node *child,
  708. struct npcm7xx_pwm_fan_data *data,
  709. u32 pwm_port, u8 num_levels)
  710. {
  711. int ret;
  712. struct npcm7xx_cooling_device *cdev;
  713. cdev = devm_kzalloc(dev, sizeof(*cdev), GFP_KERNEL);
  714. if (!cdev)
  715. return -ENOMEM;
  716. cdev->cooling_levels = devm_kzalloc(dev, num_levels, GFP_KERNEL);
  717. if (!cdev->cooling_levels)
  718. return -ENOMEM;
  719. cdev->max_state = num_levels - 1;
  720. ret = of_property_read_u8_array(child, "cooling-levels",
  721. cdev->cooling_levels,
  722. num_levels);
  723. if (ret) {
  724. dev_err(dev, "Property 'cooling-levels' cannot be read.\n");
  725. return ret;
  726. }
  727. snprintf(cdev->name, THERMAL_NAME_LENGTH, "%s%d", child->name,
  728. pwm_port);
  729. cdev->tcdev = thermal_of_cooling_device_register(child,
  730. cdev->name,
  731. cdev,
  732. &npcm7xx_pwm_cool_ops);
  733. if (IS_ERR(cdev->tcdev))
  734. return PTR_ERR(cdev->tcdev);
  735. cdev->data = data;
  736. cdev->pwm_port = pwm_port;
  737. data->cdev[pwm_port] = cdev;
  738. return 0;
  739. }
  740. static int npcm7xx_en_pwm_fan(struct device *dev,
  741. struct device_node *child,
  742. struct npcm7xx_pwm_fan_data *data)
  743. {
  744. u8 *fan_ch;
  745. u32 pwm_port;
  746. int ret, fan_cnt;
  747. u8 index, ch;
  748. ret = of_property_read_u32(child, "reg", &pwm_port);
  749. if (ret)
  750. return ret;
  751. data->pwm_present[pwm_port] = true;
  752. ret = npcm7xx_pwm_config_set(data, pwm_port,
  753. NPCM7XX_PWM_CMR_DEFAULT_NUM);
  754. ret = of_property_count_u8_elems(child, "cooling-levels");
  755. if (ret > 0) {
  756. ret = npcm7xx_create_pwm_cooling(dev, child, data, pwm_port,
  757. ret);
  758. if (ret)
  759. return ret;
  760. }
  761. fan_cnt = of_property_count_u8_elems(child, "fan-tach-ch");
  762. if (fan_cnt < 1)
  763. return -EINVAL;
  764. fan_ch = devm_kcalloc(dev, fan_cnt, sizeof(*fan_ch), GFP_KERNEL);
  765. if (!fan_ch)
  766. return -ENOMEM;
  767. ret = of_property_read_u8_array(child, "fan-tach-ch", fan_ch, fan_cnt);
  768. if (ret)
  769. return ret;
  770. for (ch = 0; ch < fan_cnt; ch++) {
  771. index = fan_ch[ch];
  772. data->fan_present[index] = true;
  773. data->fan_dev[index].fan_st_flg = FAN_INIT;
  774. }
  775. return 0;
  776. }
  777. static int npcm7xx_pwm_fan_probe(struct platform_device *pdev)
  778. {
  779. struct device *dev = &pdev->dev;
  780. struct device_node *np, *child;
  781. struct npcm7xx_pwm_fan_data *data;
  782. struct resource *res;
  783. struct device *hwmon;
  784. char name[20];
  785. int ret, cnt;
  786. u32 output_freq;
  787. u32 i;
  788. np = dev->of_node;
  789. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  790. if (!data)
  791. return -ENOMEM;
  792. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwm");
  793. if (!res) {
  794. dev_err(dev, "pwm resource not found\n");
  795. return -ENODEV;
  796. }
  797. data->pwm_base = devm_ioremap_resource(dev, res);
  798. dev_dbg(dev, "pwm base resource is %pR\n", res);
  799. if (IS_ERR(data->pwm_base))
  800. return PTR_ERR(data->pwm_base);
  801. data->pwm_clk = devm_clk_get(dev, "pwm");
  802. if (IS_ERR(data->pwm_clk)) {
  803. dev_err(dev, "couldn't get pwm clock\n");
  804. return PTR_ERR(data->pwm_clk);
  805. }
  806. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fan");
  807. if (!res) {
  808. dev_err(dev, "fan resource not found\n");
  809. return -ENODEV;
  810. }
  811. data->fan_base = devm_ioremap_resource(dev, res);
  812. dev_dbg(dev, "fan base resource is %pR\n", res);
  813. if (IS_ERR(data->fan_base))
  814. return PTR_ERR(data->fan_base);
  815. data->fan_clk = devm_clk_get(dev, "fan");
  816. if (IS_ERR(data->fan_clk)) {
  817. dev_err(dev, "couldn't get fan clock\n");
  818. return PTR_ERR(data->fan_clk);
  819. }
  820. output_freq = npcm7xx_pwm_init(data);
  821. npcm7xx_fan_init(data);
  822. for (cnt = 0; cnt < NPCM7XX_PWM_MAX_MODULES ; cnt++)
  823. mutex_init(&data->pwm_lock[cnt]);
  824. for (i = 0; i < NPCM7XX_FAN_MAX_MODULE; i++) {
  825. spin_lock_init(&data->fan_lock[i]);
  826. data->fan_irq[i] = platform_get_irq(pdev, i);
  827. if (data->fan_irq[i] < 0) {
  828. dev_err(dev, "get IRQ fan%d failed\n", i);
  829. return data->fan_irq[i];
  830. }
  831. sprintf(name, "NPCM7XX-FAN-MD%d", i);
  832. ret = devm_request_irq(dev, data->fan_irq[i], npcm7xx_fan_isr,
  833. 0, name, (void *)data);
  834. if (ret) {
  835. dev_err(dev, "register IRQ fan%d failed\n", i);
  836. return ret;
  837. }
  838. }
  839. for_each_child_of_node(np, child) {
  840. ret = npcm7xx_en_pwm_fan(dev, child, data);
  841. if (ret) {
  842. dev_err(dev, "enable pwm and fan failed\n");
  843. of_node_put(child);
  844. return ret;
  845. }
  846. }
  847. hwmon = devm_hwmon_device_register_with_info(dev, "npcm7xx_pwm_fan",
  848. data, &npcm7xx_chip_info,
  849. NULL);
  850. if (IS_ERR(hwmon)) {
  851. dev_err(dev, "unable to register hwmon device\n");
  852. return PTR_ERR(hwmon);
  853. }
  854. for (i = 0; i < NPCM7XX_FAN_MAX_CHN_NUM; i++) {
  855. if (data->fan_present[i]) {
  856. /* fan timer initialization */
  857. data->fan_timer.expires = jiffies +
  858. msecs_to_jiffies(NPCM7XX_FAN_POLL_TIMER_200MS);
  859. timer_setup(&data->fan_timer,
  860. npcm7xx_fan_polling, 0);
  861. add_timer(&data->fan_timer);
  862. break;
  863. }
  864. }
  865. pr_info("NPCM7XX PWM-FAN Driver probed, output Freq %dHz[PWM], input Freq %dHz[FAN]\n",
  866. output_freq, data->input_clk_freq);
  867. return 0;
  868. }
  869. static const struct of_device_id of_pwm_fan_match_table[] = {
  870. { .compatible = "nuvoton,npcm750-pwm-fan", },
  871. {},
  872. };
  873. MODULE_DEVICE_TABLE(of, of_pwm_fan_match_table);
  874. static struct platform_driver npcm7xx_pwm_fan_driver = {
  875. .probe = npcm7xx_pwm_fan_probe,
  876. .driver = {
  877. .name = "npcm7xx_pwm_fan",
  878. .of_match_table = of_pwm_fan_match_table,
  879. },
  880. };
  881. module_platform_driver(npcm7xx_pwm_fan_driver);
  882. MODULE_DESCRIPTION("Nuvoton NPCM7XX PWM and Fan Tacho driver");
  883. MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>");
  884. MODULE_LICENSE("GPL v2");