Linux Audio

Check our new training course

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