axi-fan-control.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Fan Control HDL CORE driver
  4. *
  5. * Copyright 2019 Analog Devices Inc.
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/clk.h>
  9. #include <linux/fpga/adi-axi-common.h>
  10. #include <linux/hwmon.h>
  11. #include <linux/hwmon-sysfs.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. /* register map */
  19. #define ADI_REG_RSTN 0x0080
  20. #define ADI_REG_PWM_WIDTH 0x0084
  21. #define ADI_REG_TACH_PERIOD 0x0088
  22. #define ADI_REG_TACH_TOLERANCE 0x008c
  23. #define ADI_REG_PWM_PERIOD 0x00c0
  24. #define ADI_REG_TACH_MEASUR 0x00c4
  25. #define ADI_REG_TEMPERATURE 0x00c8
  26. #define ADI_REG_TEMP_00_H 0x0100
  27. #define ADI_REG_TEMP_25_L 0x0104
  28. #define ADI_REG_TEMP_25_H 0x0108
  29. #define ADI_REG_TEMP_50_L 0x010c
  30. #define ADI_REG_TEMP_50_H 0x0110
  31. #define ADI_REG_TEMP_75_L 0x0114
  32. #define ADI_REG_TEMP_75_H 0x0118
  33. #define ADI_REG_TEMP_100_L 0x011c
  34. #define ADI_REG_IRQ_MASK 0x0040
  35. #define ADI_REG_IRQ_PENDING 0x0044
  36. #define ADI_REG_IRQ_SRC 0x0048
  37. /* IRQ sources */
  38. #define ADI_IRQ_SRC_PWM_CHANGED BIT(0)
  39. #define ADI_IRQ_SRC_TACH_ERR BIT(1)
  40. #define ADI_IRQ_SRC_TEMP_INCREASE BIT(2)
  41. #define ADI_IRQ_SRC_NEW_MEASUR BIT(3)
  42. #define ADI_IRQ_SRC_MASK GENMASK(3, 0)
  43. #define ADI_IRQ_MASK_OUT_ALL 0xFFFFFFFFU
  44. #define SYSFS_PWM_MAX 255
  45. struct axi_fan_control_data {
  46. void __iomem *base;
  47. struct device *hdev;
  48. unsigned long clk_rate;
  49. int irq;
  50. /* pulses per revolution */
  51. u32 ppr;
  52. bool hw_pwm_req;
  53. bool update_tacho_params;
  54. u8 fan_fault;
  55. };
  56. static inline void axi_iowrite(const u32 val, const u32 reg,
  57. const struct axi_fan_control_data *ctl)
  58. {
  59. iowrite32(val, ctl->base + reg);
  60. }
  61. static inline u32 axi_ioread(const u32 reg,
  62. const struct axi_fan_control_data *ctl)
  63. {
  64. return ioread32(ctl->base + reg);
  65. }
  66. /*
  67. * The core calculates the temperature as:
  68. * T = /raw * 509.3140064 / 65535) - 280.2308787
  69. */
  70. static ssize_t axi_fan_control_show(struct device *dev, struct device_attribute *da, char *buf)
  71. {
  72. struct axi_fan_control_data *ctl = dev_get_drvdata(dev);
  73. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  74. u32 temp = axi_ioread(attr->index, ctl);
  75. temp = DIV_ROUND_CLOSEST_ULL(temp * 509314ULL, 65535) - 280230;
  76. return sprintf(buf, "%u\n", temp);
  77. }
  78. static ssize_t axi_fan_control_store(struct device *dev, struct device_attribute *da,
  79. const char *buf, size_t count)
  80. {
  81. struct axi_fan_control_data *ctl = dev_get_drvdata(dev);
  82. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  83. u32 temp;
  84. int ret;
  85. ret = kstrtou32(buf, 10, &temp);
  86. if (ret)
  87. return ret;
  88. temp = DIV_ROUND_CLOSEST_ULL((temp + 280230) * 65535ULL, 509314);
  89. axi_iowrite(temp, attr->index, ctl);
  90. return count;
  91. }
  92. static long axi_fan_control_get_pwm_duty(const struct axi_fan_control_data *ctl)
  93. {
  94. u32 pwm_width = axi_ioread(ADI_REG_PWM_WIDTH, ctl);
  95. u32 pwm_period = axi_ioread(ADI_REG_PWM_PERIOD, ctl);
  96. /*
  97. * PWM_PERIOD is a RO register set by the core. It should never be 0.
  98. * For now we are trusting the HW...
  99. */
  100. return DIV_ROUND_CLOSEST(pwm_width * SYSFS_PWM_MAX, pwm_period);
  101. }
  102. static int axi_fan_control_set_pwm_duty(const long val,
  103. struct axi_fan_control_data *ctl)
  104. {
  105. u32 pwm_period = axi_ioread(ADI_REG_PWM_PERIOD, ctl);
  106. u32 new_width;
  107. long __val = clamp_val(val, 0, SYSFS_PWM_MAX);
  108. new_width = DIV_ROUND_CLOSEST(__val * pwm_period, SYSFS_PWM_MAX);
  109. axi_iowrite(new_width, ADI_REG_PWM_WIDTH, ctl);
  110. return 0;
  111. }
  112. static long axi_fan_control_get_fan_rpm(const struct axi_fan_control_data *ctl)
  113. {
  114. const u32 tach = axi_ioread(ADI_REG_TACH_MEASUR, ctl);
  115. if (tach == 0)
  116. /* should we return error, EAGAIN maybe? */
  117. return 0;
  118. /*
  119. * The tacho period should be:
  120. * TACH = 60/(ppr * rpm), where rpm is revolutions per second
  121. * and ppr is pulses per revolution.
  122. * Given the tacho period, we can multiply it by the input clock
  123. * so that we know how many clocks we need to have this period.
  124. * From this, we can derive the RPM value.
  125. */
  126. return DIV_ROUND_CLOSEST(60 * ctl->clk_rate, ctl->ppr * tach);
  127. }
  128. static int axi_fan_control_read_temp(struct device *dev, u32 attr, long *val)
  129. {
  130. struct axi_fan_control_data *ctl = dev_get_drvdata(dev);
  131. long raw_temp;
  132. switch (attr) {
  133. case hwmon_temp_input:
  134. raw_temp = axi_ioread(ADI_REG_TEMPERATURE, ctl);
  135. /*
  136. * The formula for the temperature is:
  137. * T = (ADC * 501.3743 / 2^bits) - 273.6777
  138. * It's multiplied by 1000 to have millidegrees as
  139. * specified by the hwmon sysfs interface.
  140. */
  141. *val = ((raw_temp * 501374) >> 16) - 273677;
  142. return 0;
  143. default:
  144. return -ENOTSUPP;
  145. }
  146. }
  147. static int axi_fan_control_read_fan(struct device *dev, u32 attr, long *val)
  148. {
  149. struct axi_fan_control_data *ctl = dev_get_drvdata(dev);
  150. switch (attr) {
  151. case hwmon_fan_fault:
  152. *val = ctl->fan_fault;
  153. /* clear it now */
  154. ctl->fan_fault = 0;
  155. return 0;
  156. case hwmon_fan_input:
  157. *val = axi_fan_control_get_fan_rpm(ctl);
  158. return 0;
  159. default:
  160. return -ENOTSUPP;
  161. }
  162. }
  163. static int axi_fan_control_read_pwm(struct device *dev, u32 attr, long *val)
  164. {
  165. struct axi_fan_control_data *ctl = dev_get_drvdata(dev);
  166. switch (attr) {
  167. case hwmon_pwm_input:
  168. *val = axi_fan_control_get_pwm_duty(ctl);
  169. return 0;
  170. default:
  171. return -ENOTSUPP;
  172. }
  173. }
  174. static int axi_fan_control_write_pwm(struct device *dev, u32 attr, long val)
  175. {
  176. struct axi_fan_control_data *ctl = dev_get_drvdata(dev);
  177. switch (attr) {
  178. case hwmon_pwm_input:
  179. return axi_fan_control_set_pwm_duty(val, ctl);
  180. default:
  181. return -ENOTSUPP;
  182. }
  183. }
  184. static int axi_fan_control_read_labels(struct device *dev,
  185. enum hwmon_sensor_types type,
  186. u32 attr, int channel, const char **str)
  187. {
  188. switch (type) {
  189. case hwmon_fan:
  190. *str = "FAN";
  191. return 0;
  192. case hwmon_temp:
  193. *str = "SYSMON4";
  194. return 0;
  195. default:
  196. return -ENOTSUPP;
  197. }
  198. }
  199. static int axi_fan_control_read(struct device *dev,
  200. enum hwmon_sensor_types type,
  201. u32 attr, int channel, long *val)
  202. {
  203. switch (type) {
  204. case hwmon_fan:
  205. return axi_fan_control_read_fan(dev, attr, val);
  206. case hwmon_pwm:
  207. return axi_fan_control_read_pwm(dev, attr, val);
  208. case hwmon_temp:
  209. return axi_fan_control_read_temp(dev, attr, val);
  210. default:
  211. return -ENOTSUPP;
  212. }
  213. }
  214. static int axi_fan_control_write(struct device *dev,
  215. enum hwmon_sensor_types type,
  216. u32 attr, int channel, long val)
  217. {
  218. switch (type) {
  219. case hwmon_pwm:
  220. return axi_fan_control_write_pwm(dev, attr, val);
  221. default:
  222. return -ENOTSUPP;
  223. }
  224. }
  225. static umode_t axi_fan_control_fan_is_visible(const u32 attr)
  226. {
  227. switch (attr) {
  228. case hwmon_fan_input:
  229. case hwmon_fan_fault:
  230. case hwmon_fan_label:
  231. return 0444;
  232. default:
  233. return 0;
  234. }
  235. }
  236. static umode_t axi_fan_control_pwm_is_visible(const u32 attr)
  237. {
  238. switch (attr) {
  239. case hwmon_pwm_input:
  240. return 0644;
  241. default:
  242. return 0;
  243. }
  244. }
  245. static umode_t axi_fan_control_temp_is_visible(const u32 attr)
  246. {
  247. switch (attr) {
  248. case hwmon_temp_input:
  249. case hwmon_temp_label:
  250. return 0444;
  251. default:
  252. return 0;
  253. }
  254. }
  255. static umode_t axi_fan_control_is_visible(const void *data,
  256. enum hwmon_sensor_types type,
  257. u32 attr, int channel)
  258. {
  259. switch (type) {
  260. case hwmon_fan:
  261. return axi_fan_control_fan_is_visible(attr);
  262. case hwmon_pwm:
  263. return axi_fan_control_pwm_is_visible(attr);
  264. case hwmon_temp:
  265. return axi_fan_control_temp_is_visible(attr);
  266. default:
  267. return 0;
  268. }
  269. }
  270. /*
  271. * This core has two main ways of changing the PWM duty cycle. It is done,
  272. * either by a request from userspace (writing on pwm1_input) or by the
  273. * core itself. When the change is done by the core, it will use predefined
  274. * parameters to evaluate the tach signal and, on that case we cannot set them.
  275. * On the other hand, when the request is done by the user, with some arbitrary
  276. * value that the core does not now about, we have to provide the tach
  277. * parameters so that, the core can evaluate the signal. On the IRQ handler we
  278. * distinguish this by using the ADI_IRQ_SRC_TEMP_INCREASE interrupt. This tell
  279. * us that the CORE requested a new duty cycle. After this, there is 5s delay
  280. * on which the core waits for the fan rotation speed to stabilize. After this
  281. * we get ADI_IRQ_SRC_PWM_CHANGED irq where we will decide if we need to set
  282. * the tach parameters or not on the next tach measurement cycle (corresponding
  283. * already to the ney duty cycle) based on the %ctl->hw_pwm_req flag.
  284. */
  285. static irqreturn_t axi_fan_control_irq_handler(int irq, void *data)
  286. {
  287. struct axi_fan_control_data *ctl = (struct axi_fan_control_data *)data;
  288. u32 irq_pending = axi_ioread(ADI_REG_IRQ_PENDING, ctl);
  289. u32 clear_mask;
  290. if (irq_pending & ADI_IRQ_SRC_TEMP_INCREASE)
  291. /* hardware requested a new pwm */
  292. ctl->hw_pwm_req = true;
  293. if (irq_pending & ADI_IRQ_SRC_PWM_CHANGED) {
  294. /*
  295. * if the pwm changes on behalf of software,
  296. * we need to provide new tacho parameters to the core.
  297. * Wait for the next measurement for that...
  298. */
  299. if (!ctl->hw_pwm_req) {
  300. ctl->update_tacho_params = true;
  301. } else {
  302. ctl->hw_pwm_req = false;
  303. hwmon_notify_event(ctl->hdev, hwmon_pwm,
  304. hwmon_pwm_input, 0);
  305. }
  306. }
  307. if (irq_pending & ADI_IRQ_SRC_NEW_MEASUR) {
  308. if (ctl->update_tacho_params) {
  309. u32 new_tach = axi_ioread(ADI_REG_TACH_MEASUR, ctl);
  310. /* get 25% tolerance */
  311. u32 tach_tol = DIV_ROUND_CLOSEST(new_tach * 25, 100);
  312. /* set new tacho parameters */
  313. axi_iowrite(new_tach, ADI_REG_TACH_PERIOD, ctl);
  314. axi_iowrite(tach_tol, ADI_REG_TACH_TOLERANCE, ctl);
  315. ctl->update_tacho_params = false;
  316. }
  317. }
  318. if (irq_pending & ADI_IRQ_SRC_TACH_ERR)
  319. ctl->fan_fault = 1;
  320. /* clear all interrupts */
  321. clear_mask = irq_pending & ADI_IRQ_SRC_MASK;
  322. axi_iowrite(clear_mask, ADI_REG_IRQ_PENDING, ctl);
  323. return IRQ_HANDLED;
  324. }
  325. static int axi_fan_control_init(struct axi_fan_control_data *ctl,
  326. const struct device_node *np)
  327. {
  328. int ret;
  329. /* get fan pulses per revolution */
  330. ret = of_property_read_u32(np, "pulses-per-revolution", &ctl->ppr);
  331. if (ret)
  332. return ret;
  333. /* 1, 2 and 4 are the typical and accepted values */
  334. if (ctl->ppr != 1 && ctl->ppr != 2 && ctl->ppr != 4)
  335. return -EINVAL;
  336. /*
  337. * Enable all IRQs
  338. */
  339. axi_iowrite(ADI_IRQ_MASK_OUT_ALL &
  340. ~(ADI_IRQ_SRC_NEW_MEASUR | ADI_IRQ_SRC_TACH_ERR |
  341. ADI_IRQ_SRC_PWM_CHANGED | ADI_IRQ_SRC_TEMP_INCREASE),
  342. ADI_REG_IRQ_MASK, ctl);
  343. /* bring the device out of reset */
  344. axi_iowrite(0x01, ADI_REG_RSTN, ctl);
  345. return ret;
  346. }
  347. static const struct hwmon_channel_info *axi_fan_control_info[] = {
  348. HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT),
  349. HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_FAULT | HWMON_F_LABEL),
  350. HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
  351. NULL
  352. };
  353. static const struct hwmon_ops axi_fan_control_hwmon_ops = {
  354. .is_visible = axi_fan_control_is_visible,
  355. .read = axi_fan_control_read,
  356. .write = axi_fan_control_write,
  357. .read_string = axi_fan_control_read_labels,
  358. };
  359. static const struct hwmon_chip_info axi_chip_info = {
  360. .ops = &axi_fan_control_hwmon_ops,
  361. .info = axi_fan_control_info,
  362. };
  363. /* temperature threshold below which PWM should be 0% */
  364. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp_hyst, axi_fan_control, ADI_REG_TEMP_00_H);
  365. /* temperature threshold above which PWM should be 25% */
  366. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, axi_fan_control, ADI_REG_TEMP_25_L);
  367. /* temperature threshold below which PWM should be 25% */
  368. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_temp_hyst, axi_fan_control, ADI_REG_TEMP_25_H);
  369. /* temperature threshold above which PWM should be 50% */
  370. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_temp, axi_fan_control, ADI_REG_TEMP_50_L);
  371. /* temperature threshold below which PWM should be 50% */
  372. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_temp_hyst, axi_fan_control, ADI_REG_TEMP_50_H);
  373. /* temperature threshold above which PWM should be 75% */
  374. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_temp, axi_fan_control, ADI_REG_TEMP_75_L);
  375. /* temperature threshold below which PWM should be 75% */
  376. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_temp_hyst, axi_fan_control, ADI_REG_TEMP_75_H);
  377. /* temperature threshold above which PWM should be 100% */
  378. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_temp, axi_fan_control, ADI_REG_TEMP_100_L);
  379. static struct attribute *axi_fan_control_attrs[] = {
  380. &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
  381. &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
  382. &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
  383. &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
  384. &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
  385. &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
  386. &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
  387. &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
  388. NULL,
  389. };
  390. ATTRIBUTE_GROUPS(axi_fan_control);
  391. static const u32 version_1_0_0 = ADI_AXI_PCORE_VER(1, 0, 'a');
  392. static const struct of_device_id axi_fan_control_of_match[] = {
  393. { .compatible = "adi,axi-fan-control-1.00.a",
  394. .data = (void *)&version_1_0_0},
  395. {},
  396. };
  397. MODULE_DEVICE_TABLE(of, axi_fan_control_of_match);
  398. static int axi_fan_control_probe(struct platform_device *pdev)
  399. {
  400. struct axi_fan_control_data *ctl;
  401. struct clk *clk;
  402. const struct of_device_id *id;
  403. const char *name = "axi_fan_control";
  404. u32 version;
  405. int ret;
  406. id = of_match_node(axi_fan_control_of_match, pdev->dev.of_node);
  407. if (!id)
  408. return -EINVAL;
  409. ctl = devm_kzalloc(&pdev->dev, sizeof(*ctl), GFP_KERNEL);
  410. if (!ctl)
  411. return -ENOMEM;
  412. ctl->base = devm_platform_ioremap_resource(pdev, 0);
  413. if (IS_ERR(ctl->base))
  414. return PTR_ERR(ctl->base);
  415. clk = devm_clk_get_enabled(&pdev->dev, NULL);
  416. if (IS_ERR(clk)) {
  417. dev_err(&pdev->dev, "clk_get failed with %ld\n", PTR_ERR(clk));
  418. return PTR_ERR(clk);
  419. }
  420. ctl->clk_rate = clk_get_rate(clk);
  421. if (!ctl->clk_rate)
  422. return -EINVAL;
  423. version = axi_ioread(ADI_AXI_REG_VERSION, ctl);
  424. if (ADI_AXI_PCORE_VER_MAJOR(version) !=
  425. ADI_AXI_PCORE_VER_MAJOR((*(u32 *)id->data))) {
  426. dev_err(&pdev->dev, "Major version mismatch. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n",
  427. ADI_AXI_PCORE_VER_MAJOR((*(u32 *)id->data)),
  428. ADI_AXI_PCORE_VER_MINOR((*(u32 *)id->data)),
  429. ADI_AXI_PCORE_VER_PATCH((*(u32 *)id->data)),
  430. ADI_AXI_PCORE_VER_MAJOR(version),
  431. ADI_AXI_PCORE_VER_MINOR(version),
  432. ADI_AXI_PCORE_VER_PATCH(version));
  433. return -ENODEV;
  434. }
  435. ret = axi_fan_control_init(ctl, pdev->dev.of_node);
  436. if (ret) {
  437. dev_err(&pdev->dev, "Failed to initialize device\n");
  438. return ret;
  439. }
  440. ctl->hdev = devm_hwmon_device_register_with_info(&pdev->dev,
  441. name,
  442. ctl,
  443. &axi_chip_info,
  444. axi_fan_control_groups);
  445. if (IS_ERR(ctl->hdev))
  446. return PTR_ERR(ctl->hdev);
  447. ctl->irq = platform_get_irq(pdev, 0);
  448. if (ctl->irq < 0)
  449. return ctl->irq;
  450. ret = devm_request_threaded_irq(&pdev->dev, ctl->irq, NULL,
  451. axi_fan_control_irq_handler,
  452. IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
  453. pdev->driver_override, ctl);
  454. if (ret) {
  455. dev_err(&pdev->dev, "failed to request an irq, %d", ret);
  456. return ret;
  457. }
  458. return 0;
  459. }
  460. static struct platform_driver axi_fan_control_driver = {
  461. .driver = {
  462. .name = "axi_fan_control_driver",
  463. .of_match_table = axi_fan_control_of_match,
  464. },
  465. .probe = axi_fan_control_probe,
  466. };
  467. module_platform_driver(axi_fan_control_driver);
  468. MODULE_AUTHOR("Nuno Sa <[email protected]>");
  469. MODULE_DESCRIPTION("Analog Devices Fan Control HDL CORE driver");
  470. MODULE_LICENSE("GPL");