xref: /linux/drivers/hwmon/aspeed-pwm-tacho.c (revision 5f348fa35a28b19cf7ee1eaa420757341a724c86)
12d7a548aSJaghathiswari Rankappagounder Natarajan /*
22d7a548aSJaghathiswari Rankappagounder Natarajan  * Copyright (c) 2016 Google, Inc
32d7a548aSJaghathiswari Rankappagounder Natarajan  *
42d7a548aSJaghathiswari Rankappagounder Natarajan  * This program is free software; you can redistribute it and/or modify
52d7a548aSJaghathiswari Rankappagounder Natarajan  * it under the terms of the GNU General Public License version 2 or later as
62d7a548aSJaghathiswari Rankappagounder Natarajan  * published by the Free Software Foundation.
72d7a548aSJaghathiswari Rankappagounder Natarajan  */
82d7a548aSJaghathiswari Rankappagounder Natarajan 
92d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/clk.h>
107ed1c5e5SPatrick Venture #include <linux/errno.h>
112d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/gpio/consumer.h>
122d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/delay.h>
132d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/hwmon.h>
142d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/hwmon-sysfs.h>
152d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/io.h>
162d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/kernel.h>
172d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/module.h>
182d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/of_platform.h>
192d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/of_device.h>
202d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/platform_device.h>
212d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/sysfs.h>
222d7a548aSJaghathiswari Rankappagounder Natarajan #include <linux/regmap.h>
232d7a548aSJaghathiswari Rankappagounder Natarajan 
242d7a548aSJaghathiswari Rankappagounder Natarajan /* ASPEED PWM & FAN Tach Register Definition */
252d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL		0x00
262d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL		0x04
272d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_DUTY0_CTRL		0x08
282d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_DUTY1_CTRL		0x0c
292d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TYPEM_CTRL		0x10
302d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TYPEM_CTRL1		0x14
312d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TYPEN_CTRL		0x18
322d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TYPEN_CTRL1		0x1c
332d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TACH_SOURCE		0x20
342d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TRIGGER		0x28
352d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_RESULT		0x2c
362d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_INTR_CTRL		0x30
372d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_INTR_STS		0x34
382d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TYPEM_LIMIT		0x38
392d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TYPEN_LIMIT		0x3C
402d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_EXT		0x40
412d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_EXT	0x44
422d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_DUTY2_CTRL		0x48
432d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_DUTY3_CTRL		0x4c
442d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TYPEO_CTRL		0x50
452d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TYPEO_CTRL1		0x54
462d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TACH_SOURCE_EXT	0x60
472d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_TYPEO_LIMIT		0x78
482d7a548aSJaghathiswari Rankappagounder Natarajan 
492d7a548aSJaghathiswari Rankappagounder Natarajan /* ASPEED_PTCR_CTRL : 0x00 - General Control Register */
502d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMD_TYPE_PART1	15
512d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMD_TYPE_PART2	6
522d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMD_TYPE_MASK	(BIT(7) | BIT(15))
532d7a548aSJaghathiswari Rankappagounder Natarajan 
542d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMC_TYPE_PART1	14
552d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMC_TYPE_PART2	5
562d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMC_TYPE_MASK	(BIT(6) | BIT(14))
572d7a548aSJaghathiswari Rankappagounder Natarajan 
582d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMB_TYPE_PART1	13
592d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMB_TYPE_PART2	4
602d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMB_TYPE_MASK	(BIT(5) | BIT(13))
612d7a548aSJaghathiswari Rankappagounder Natarajan 
622d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMA_TYPE_PART1	12
632d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMA_TYPE_PART2	3
642d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMA_TYPE_MASK	(BIT(4) | BIT(12))
652d7a548aSJaghathiswari Rankappagounder Natarajan 
662d7a548aSJaghathiswari Rankappagounder Natarajan #define	ASPEED_PTCR_CTRL_FAN_NUM_EN(x)	BIT(16 + (x))
672d7a548aSJaghathiswari Rankappagounder Natarajan 
682d7a548aSJaghathiswari Rankappagounder Natarajan #define	ASPEED_PTCR_CTRL_PWMD_EN	BIT(11)
692d7a548aSJaghathiswari Rankappagounder Natarajan #define	ASPEED_PTCR_CTRL_PWMC_EN	BIT(10)
702d7a548aSJaghathiswari Rankappagounder Natarajan #define	ASPEED_PTCR_CTRL_PWMB_EN	BIT(9)
712d7a548aSJaghathiswari Rankappagounder Natarajan #define	ASPEED_PTCR_CTRL_PWMA_EN	BIT(8)
722d7a548aSJaghathiswari Rankappagounder Natarajan 
732d7a548aSJaghathiswari Rankappagounder Natarajan #define	ASPEED_PTCR_CTRL_CLK_SRC	BIT(1)
742d7a548aSJaghathiswari Rankappagounder Natarajan #define	ASPEED_PTCR_CTRL_CLK_EN		BIT(0)
752d7a548aSJaghathiswari Rankappagounder Natarajan 
762d7a548aSJaghathiswari Rankappagounder Natarajan /* ASPEED_PTCR_CLK_CTRL : 0x04 - Clock Control Register */
772d7a548aSJaghathiswari Rankappagounder Natarajan /* TYPE N */
782d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEN_MASK		GENMASK(31, 16)
792d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEN_UNIT		24
802d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEN_H		20
812d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEN_L		16
822d7a548aSJaghathiswari Rankappagounder Natarajan /* TYPE M */
832d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEM_MASK         GENMASK(15, 0)
842d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEM_UNIT		8
852d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEM_H		4
862d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEM_L		0
872d7a548aSJaghathiswari Rankappagounder Natarajan 
882d7a548aSJaghathiswari Rankappagounder Natarajan /*
892d7a548aSJaghathiswari Rankappagounder Natarajan  * ASPEED_PTCR_DUTY_CTRL/1/2/3 : 0x08/0x0C/0x48/0x4C - PWM-FAN duty control
902d7a548aSJaghathiswari Rankappagounder Natarajan  * 0/1/2/3 register
912d7a548aSJaghathiswari Rankappagounder Natarajan  */
922d7a548aSJaghathiswari Rankappagounder Natarajan #define DUTY_CTRL_PWM2_FALL_POINT	24
932d7a548aSJaghathiswari Rankappagounder Natarajan #define DUTY_CTRL_PWM2_RISE_POINT	16
942d7a548aSJaghathiswari Rankappagounder Natarajan #define DUTY_CTRL_PWM2_RISE_FALL_MASK	GENMASK(31, 16)
952d7a548aSJaghathiswari Rankappagounder Natarajan #define DUTY_CTRL_PWM1_FALL_POINT	8
962d7a548aSJaghathiswari Rankappagounder Natarajan #define DUTY_CTRL_PWM1_RISE_POINT	0
972d7a548aSJaghathiswari Rankappagounder Natarajan #define DUTY_CTRL_PWM1_RISE_FALL_MASK   GENMASK(15, 0)
982d7a548aSJaghathiswari Rankappagounder Natarajan 
992d7a548aSJaghathiswari Rankappagounder Natarajan /* ASPEED_PTCR_TYPEM_CTRL : 0x10/0x18/0x50 - Type M/N/O Ctrl 0 Register */
1002d7a548aSJaghathiswari Rankappagounder Natarajan #define TYPE_CTRL_FAN_MASK		(GENMASK(5, 1) | GENMASK(31, 16))
1012d7a548aSJaghathiswari Rankappagounder Natarajan #define TYPE_CTRL_FAN1_MASK		GENMASK(31, 0)
1022d7a548aSJaghathiswari Rankappagounder Natarajan #define TYPE_CTRL_FAN_PERIOD		16
1032d7a548aSJaghathiswari Rankappagounder Natarajan #define TYPE_CTRL_FAN_MODE		4
1042d7a548aSJaghathiswari Rankappagounder Natarajan #define TYPE_CTRL_FAN_DIVISION		1
1052d7a548aSJaghathiswari Rankappagounder Natarajan #define TYPE_CTRL_FAN_TYPE_EN		1
1062d7a548aSJaghathiswari Rankappagounder Natarajan 
1072d7a548aSJaghathiswari Rankappagounder Natarajan /* ASPEED_PTCR_TACH_SOURCE : 0x20/0x60 - Tach Source Register */
1082d7a548aSJaghathiswari Rankappagounder Natarajan /* bit [0,1] at 0x20, bit [2] at 0x60 */
1092d7a548aSJaghathiswari Rankappagounder Natarajan #define TACH_PWM_SOURCE_BIT01(x)	((x) * 2)
1102d7a548aSJaghathiswari Rankappagounder Natarajan #define TACH_PWM_SOURCE_BIT2(x)		((x) * 2)
1112d7a548aSJaghathiswari Rankappagounder Natarajan #define TACH_PWM_SOURCE_MASK_BIT01(x)	(0x3 << ((x) * 2))
1122d7a548aSJaghathiswari Rankappagounder Natarajan #define TACH_PWM_SOURCE_MASK_BIT2(x)	BIT((x) * 2)
1132d7a548aSJaghathiswari Rankappagounder Natarajan 
1142d7a548aSJaghathiswari Rankappagounder Natarajan /* ASPEED_PTCR_RESULT : 0x2c - Result Register */
1152d7a548aSJaghathiswari Rankappagounder Natarajan #define RESULT_STATUS_MASK		BIT(31)
1162d7a548aSJaghathiswari Rankappagounder Natarajan #define RESULT_VALUE_MASK		0xfffff
1172d7a548aSJaghathiswari Rankappagounder Natarajan 
1182d7a548aSJaghathiswari Rankappagounder Natarajan /* ASPEED_PTCR_CTRL_EXT : 0x40 - General Control Extension #1 Register */
1192d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMH_TYPE_PART1	15
1202d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMH_TYPE_PART2	6
1212d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMH_TYPE_MASK	(BIT(7) | BIT(15))
1222d7a548aSJaghathiswari Rankappagounder Natarajan 
1232d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMG_TYPE_PART1	14
1242d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMG_TYPE_PART2	5
1252d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMG_TYPE_MASK	(BIT(6) | BIT(14))
1262d7a548aSJaghathiswari Rankappagounder Natarajan 
1272d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMF_TYPE_PART1	13
1282d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMF_TYPE_PART2	4
1292d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWMF_TYPE_MASK	(BIT(5) | BIT(13))
1302d7a548aSJaghathiswari Rankappagounder Natarajan 
1312d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWME_TYPE_PART1	12
1322d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWME_TYPE_PART2	3
1332d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CTRL_SET_PWME_TYPE_MASK	(BIT(4) | BIT(12))
1342d7a548aSJaghathiswari Rankappagounder Natarajan 
1352d7a548aSJaghathiswari Rankappagounder Natarajan #define	ASPEED_PTCR_CTRL_PWMH_EN	BIT(11)
1362d7a548aSJaghathiswari Rankappagounder Natarajan #define	ASPEED_PTCR_CTRL_PWMG_EN	BIT(10)
1372d7a548aSJaghathiswari Rankappagounder Natarajan #define	ASPEED_PTCR_CTRL_PWMF_EN	BIT(9)
1382d7a548aSJaghathiswari Rankappagounder Natarajan #define	ASPEED_PTCR_CTRL_PWME_EN	BIT(8)
1392d7a548aSJaghathiswari Rankappagounder Natarajan 
1402d7a548aSJaghathiswari Rankappagounder Natarajan /* ASPEED_PTCR_CLK_EXT_CTRL : 0x44 - Clock Control Extension #1 Register */
1412d7a548aSJaghathiswari Rankappagounder Natarajan /* TYPE O */
1422d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEO_MASK         GENMASK(15, 0)
1432d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEO_UNIT		8
1442d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEO_H		4
1452d7a548aSJaghathiswari Rankappagounder Natarajan #define ASPEED_PTCR_CLK_CTRL_TYPEO_L		0
1462d7a548aSJaghathiswari Rankappagounder Natarajan 
1472d7a548aSJaghathiswari Rankappagounder Natarajan #define PWM_MAX 255
1482d7a548aSJaghathiswari Rankappagounder Natarajan 
1492d7a548aSJaghathiswari Rankappagounder Natarajan #define M_PWM_DIV_H 0x00
1502d7a548aSJaghathiswari Rankappagounder Natarajan #define M_PWM_DIV_L 0x05
1512d7a548aSJaghathiswari Rankappagounder Natarajan #define M_PWM_PERIOD 0x5F
1522d7a548aSJaghathiswari Rankappagounder Natarajan #define M_TACH_CLK_DIV 0x00
1532d7a548aSJaghathiswari Rankappagounder Natarajan #define M_TACH_MODE 0x00
1542d7a548aSJaghathiswari Rankappagounder Natarajan #define M_TACH_UNIT 0x1000
1552d7a548aSJaghathiswari Rankappagounder Natarajan #define INIT_FAN_CTRL 0xFF
1562d7a548aSJaghathiswari Rankappagounder Natarajan 
1572d7a548aSJaghathiswari Rankappagounder Natarajan struct aspeed_pwm_tacho_data {
1582d7a548aSJaghathiswari Rankappagounder Natarajan 	struct regmap *regmap;
1592d7a548aSJaghathiswari Rankappagounder Natarajan 	unsigned long clk_freq;
1602d7a548aSJaghathiswari Rankappagounder Natarajan 	bool pwm_present[8];
1612d7a548aSJaghathiswari Rankappagounder Natarajan 	bool fan_tach_present[16];
1622d7a548aSJaghathiswari Rankappagounder Natarajan 	u8 type_pwm_clock_unit[3];
1632d7a548aSJaghathiswari Rankappagounder Natarajan 	u8 type_pwm_clock_division_h[3];
1642d7a548aSJaghathiswari Rankappagounder Natarajan 	u8 type_pwm_clock_division_l[3];
1652d7a548aSJaghathiswari Rankappagounder Natarajan 	u8 type_fan_tach_clock_division[3];
1662d7a548aSJaghathiswari Rankappagounder Natarajan 	u16 type_fan_tach_unit[3];
1672d7a548aSJaghathiswari Rankappagounder Natarajan 	u8 pwm_port_type[8];
1682d7a548aSJaghathiswari Rankappagounder Natarajan 	u8 pwm_port_fan_ctrl[8];
1692d7a548aSJaghathiswari Rankappagounder Natarajan 	u8 fan_tach_ch_source[16];
1702d7a548aSJaghathiswari Rankappagounder Natarajan 	const struct attribute_group *groups[3];
1712d7a548aSJaghathiswari Rankappagounder Natarajan };
1722d7a548aSJaghathiswari Rankappagounder Natarajan 
1732d7a548aSJaghathiswari Rankappagounder Natarajan enum type { TYPEM, TYPEN, TYPEO };
1742d7a548aSJaghathiswari Rankappagounder Natarajan 
1752d7a548aSJaghathiswari Rankappagounder Natarajan struct type_params {
1762d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 l_value;
1772d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 h_value;
1782d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 unit_value;
1792d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 clk_ctrl_mask;
1802d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 clk_ctrl_reg;
1812d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 ctrl_reg;
1822d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 ctrl_reg1;
1832d7a548aSJaghathiswari Rankappagounder Natarajan };
1842d7a548aSJaghathiswari Rankappagounder Natarajan 
1852d7a548aSJaghathiswari Rankappagounder Natarajan static const struct type_params type_params[] = {
1862d7a548aSJaghathiswari Rankappagounder Natarajan 	[TYPEM] = {
1872d7a548aSJaghathiswari Rankappagounder Natarajan 		.l_value = ASPEED_PTCR_CLK_CTRL_TYPEM_L,
1882d7a548aSJaghathiswari Rankappagounder Natarajan 		.h_value = ASPEED_PTCR_CLK_CTRL_TYPEM_H,
1892d7a548aSJaghathiswari Rankappagounder Natarajan 		.unit_value = ASPEED_PTCR_CLK_CTRL_TYPEM_UNIT,
1902d7a548aSJaghathiswari Rankappagounder Natarajan 		.clk_ctrl_mask = ASPEED_PTCR_CLK_CTRL_TYPEM_MASK,
1912d7a548aSJaghathiswari Rankappagounder Natarajan 		.clk_ctrl_reg = ASPEED_PTCR_CLK_CTRL,
1922d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg = ASPEED_PTCR_TYPEM_CTRL,
1932d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg1 = ASPEED_PTCR_TYPEM_CTRL1,
1942d7a548aSJaghathiswari Rankappagounder Natarajan 	},
1952d7a548aSJaghathiswari Rankappagounder Natarajan 	[TYPEN] = {
1962d7a548aSJaghathiswari Rankappagounder Natarajan 		.l_value = ASPEED_PTCR_CLK_CTRL_TYPEN_L,
1972d7a548aSJaghathiswari Rankappagounder Natarajan 		.h_value = ASPEED_PTCR_CLK_CTRL_TYPEN_H,
1982d7a548aSJaghathiswari Rankappagounder Natarajan 		.unit_value = ASPEED_PTCR_CLK_CTRL_TYPEN_UNIT,
1992d7a548aSJaghathiswari Rankappagounder Natarajan 		.clk_ctrl_mask = ASPEED_PTCR_CLK_CTRL_TYPEN_MASK,
2002d7a548aSJaghathiswari Rankappagounder Natarajan 		.clk_ctrl_reg = ASPEED_PTCR_CLK_CTRL,
2012d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg = ASPEED_PTCR_TYPEN_CTRL,
2022d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg1 = ASPEED_PTCR_TYPEN_CTRL1,
2032d7a548aSJaghathiswari Rankappagounder Natarajan 	},
2042d7a548aSJaghathiswari Rankappagounder Natarajan 	[TYPEO] = {
2052d7a548aSJaghathiswari Rankappagounder Natarajan 		.l_value = ASPEED_PTCR_CLK_CTRL_TYPEO_L,
2062d7a548aSJaghathiswari Rankappagounder Natarajan 		.h_value = ASPEED_PTCR_CLK_CTRL_TYPEO_H,
2072d7a548aSJaghathiswari Rankappagounder Natarajan 		.unit_value = ASPEED_PTCR_CLK_CTRL_TYPEO_UNIT,
2082d7a548aSJaghathiswari Rankappagounder Natarajan 		.clk_ctrl_mask = ASPEED_PTCR_CLK_CTRL_TYPEO_MASK,
2092d7a548aSJaghathiswari Rankappagounder Natarajan 		.clk_ctrl_reg = ASPEED_PTCR_CLK_CTRL_EXT,
2102d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg = ASPEED_PTCR_TYPEO_CTRL,
2112d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg1 = ASPEED_PTCR_TYPEO_CTRL1,
2122d7a548aSJaghathiswari Rankappagounder Natarajan 	}
2132d7a548aSJaghathiswari Rankappagounder Natarajan };
2142d7a548aSJaghathiswari Rankappagounder Natarajan 
2152d7a548aSJaghathiswari Rankappagounder Natarajan enum pwm_port { PWMA, PWMB, PWMC, PWMD, PWME, PWMF, PWMG, PWMH };
2162d7a548aSJaghathiswari Rankappagounder Natarajan 
2172d7a548aSJaghathiswari Rankappagounder Natarajan struct pwm_port_params {
2182d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 pwm_en;
2192d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 ctrl_reg;
2202d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 type_part1;
2212d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 type_part2;
2222d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 type_mask;
2232d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 duty_ctrl_rise_point;
2242d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 duty_ctrl_fall_point;
2252d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 duty_ctrl_reg;
2262d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 duty_ctrl_rise_fall_mask;
2272d7a548aSJaghathiswari Rankappagounder Natarajan };
2282d7a548aSJaghathiswari Rankappagounder Natarajan 
2292d7a548aSJaghathiswari Rankappagounder Natarajan static const struct pwm_port_params pwm_port_params[] = {
2302d7a548aSJaghathiswari Rankappagounder Natarajan 	[PWMA] = {
2312d7a548aSJaghathiswari Rankappagounder Natarajan 		.pwm_en = ASPEED_PTCR_CTRL_PWMA_EN,
2322d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg = ASPEED_PTCR_CTRL,
2332d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part1 = ASPEED_PTCR_CTRL_SET_PWMA_TYPE_PART1,
2342d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part2 = ASPEED_PTCR_CTRL_SET_PWMA_TYPE_PART2,
2352d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_mask = ASPEED_PTCR_CTRL_SET_PWMA_TYPE_MASK,
2362d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_point = DUTY_CTRL_PWM1_RISE_POINT,
2372d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_fall_point = DUTY_CTRL_PWM1_FALL_POINT,
2382d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_reg = ASPEED_PTCR_DUTY0_CTRL,
2392d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_fall_mask = DUTY_CTRL_PWM1_RISE_FALL_MASK,
2402d7a548aSJaghathiswari Rankappagounder Natarajan 	},
2412d7a548aSJaghathiswari Rankappagounder Natarajan 	[PWMB] = {
2422d7a548aSJaghathiswari Rankappagounder Natarajan 		.pwm_en = ASPEED_PTCR_CTRL_PWMB_EN,
2432d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg = ASPEED_PTCR_CTRL,
2442d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part1 = ASPEED_PTCR_CTRL_SET_PWMB_TYPE_PART1,
2452d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part2 = ASPEED_PTCR_CTRL_SET_PWMB_TYPE_PART2,
2462d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_mask = ASPEED_PTCR_CTRL_SET_PWMB_TYPE_MASK,
2472d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_point = DUTY_CTRL_PWM2_RISE_POINT,
2482d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_fall_point = DUTY_CTRL_PWM2_FALL_POINT,
2492d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_reg = ASPEED_PTCR_DUTY0_CTRL,
2502d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_fall_mask = DUTY_CTRL_PWM2_RISE_FALL_MASK,
2512d7a548aSJaghathiswari Rankappagounder Natarajan 	},
2522d7a548aSJaghathiswari Rankappagounder Natarajan 	[PWMC] = {
2532d7a548aSJaghathiswari Rankappagounder Natarajan 		.pwm_en = ASPEED_PTCR_CTRL_PWMC_EN,
2542d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg = ASPEED_PTCR_CTRL,
2552d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part1 = ASPEED_PTCR_CTRL_SET_PWMC_TYPE_PART1,
2562d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part2 = ASPEED_PTCR_CTRL_SET_PWMC_TYPE_PART2,
2572d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_mask = ASPEED_PTCR_CTRL_SET_PWMC_TYPE_MASK,
2582d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_point = DUTY_CTRL_PWM1_RISE_POINT,
2592d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_fall_point = DUTY_CTRL_PWM1_FALL_POINT,
2602d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_reg = ASPEED_PTCR_DUTY1_CTRL,
2612d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_fall_mask = DUTY_CTRL_PWM1_RISE_FALL_MASK,
2622d7a548aSJaghathiswari Rankappagounder Natarajan 	},
2632d7a548aSJaghathiswari Rankappagounder Natarajan 	[PWMD] = {
2642d7a548aSJaghathiswari Rankappagounder Natarajan 		.pwm_en = ASPEED_PTCR_CTRL_PWMD_EN,
2652d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg = ASPEED_PTCR_CTRL,
2662d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part1 = ASPEED_PTCR_CTRL_SET_PWMD_TYPE_PART1,
2672d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part2 = ASPEED_PTCR_CTRL_SET_PWMD_TYPE_PART2,
2682d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_mask = ASPEED_PTCR_CTRL_SET_PWMD_TYPE_MASK,
2692d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_point = DUTY_CTRL_PWM2_RISE_POINT,
2702d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_fall_point = DUTY_CTRL_PWM2_FALL_POINT,
2712d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_reg = ASPEED_PTCR_DUTY1_CTRL,
2722d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_fall_mask = DUTY_CTRL_PWM2_RISE_FALL_MASK,
2732d7a548aSJaghathiswari Rankappagounder Natarajan 	},
2742d7a548aSJaghathiswari Rankappagounder Natarajan 	[PWME] = {
2752d7a548aSJaghathiswari Rankappagounder Natarajan 		.pwm_en = ASPEED_PTCR_CTRL_PWME_EN,
2762d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg = ASPEED_PTCR_CTRL_EXT,
2772d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part1 = ASPEED_PTCR_CTRL_SET_PWME_TYPE_PART1,
2782d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part2 = ASPEED_PTCR_CTRL_SET_PWME_TYPE_PART2,
2792d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_mask = ASPEED_PTCR_CTRL_SET_PWME_TYPE_MASK,
2802d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_point = DUTY_CTRL_PWM1_RISE_POINT,
2812d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_fall_point = DUTY_CTRL_PWM1_FALL_POINT,
2822d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_reg = ASPEED_PTCR_DUTY2_CTRL,
2832d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_fall_mask = DUTY_CTRL_PWM1_RISE_FALL_MASK,
2842d7a548aSJaghathiswari Rankappagounder Natarajan 	},
2852d7a548aSJaghathiswari Rankappagounder Natarajan 	[PWMF] = {
2862d7a548aSJaghathiswari Rankappagounder Natarajan 		.pwm_en = ASPEED_PTCR_CTRL_PWMF_EN,
2872d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg = ASPEED_PTCR_CTRL_EXT,
2882d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part1 = ASPEED_PTCR_CTRL_SET_PWMF_TYPE_PART1,
2892d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part2 = ASPEED_PTCR_CTRL_SET_PWMF_TYPE_PART2,
2902d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_mask = ASPEED_PTCR_CTRL_SET_PWMF_TYPE_MASK,
2912d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_point = DUTY_CTRL_PWM2_RISE_POINT,
2922d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_fall_point = DUTY_CTRL_PWM2_FALL_POINT,
2932d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_reg = ASPEED_PTCR_DUTY2_CTRL,
2942d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_fall_mask = DUTY_CTRL_PWM2_RISE_FALL_MASK,
2952d7a548aSJaghathiswari Rankappagounder Natarajan 	},
2962d7a548aSJaghathiswari Rankappagounder Natarajan 	[PWMG] = {
2972d7a548aSJaghathiswari Rankappagounder Natarajan 		.pwm_en = ASPEED_PTCR_CTRL_PWMG_EN,
2982d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg = ASPEED_PTCR_CTRL_EXT,
2992d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part1 = ASPEED_PTCR_CTRL_SET_PWMG_TYPE_PART1,
3002d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part2 = ASPEED_PTCR_CTRL_SET_PWMG_TYPE_PART2,
3012d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_mask = ASPEED_PTCR_CTRL_SET_PWMG_TYPE_MASK,
3022d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_point = DUTY_CTRL_PWM1_RISE_POINT,
3032d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_fall_point = DUTY_CTRL_PWM1_FALL_POINT,
3042d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_reg = ASPEED_PTCR_DUTY3_CTRL,
3052d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_fall_mask = DUTY_CTRL_PWM1_RISE_FALL_MASK,
3062d7a548aSJaghathiswari Rankappagounder Natarajan 	},
3072d7a548aSJaghathiswari Rankappagounder Natarajan 	[PWMH] = {
3082d7a548aSJaghathiswari Rankappagounder Natarajan 		.pwm_en = ASPEED_PTCR_CTRL_PWMH_EN,
3092d7a548aSJaghathiswari Rankappagounder Natarajan 		.ctrl_reg = ASPEED_PTCR_CTRL_EXT,
3102d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part1 = ASPEED_PTCR_CTRL_SET_PWMH_TYPE_PART1,
3112d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_part2 = ASPEED_PTCR_CTRL_SET_PWMH_TYPE_PART2,
3122d7a548aSJaghathiswari Rankappagounder Natarajan 		.type_mask = ASPEED_PTCR_CTRL_SET_PWMH_TYPE_MASK,
3132d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_point = DUTY_CTRL_PWM2_RISE_POINT,
3142d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_fall_point = DUTY_CTRL_PWM2_FALL_POINT,
3152d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_reg = ASPEED_PTCR_DUTY3_CTRL,
3162d7a548aSJaghathiswari Rankappagounder Natarajan 		.duty_ctrl_rise_fall_mask = DUTY_CTRL_PWM2_RISE_FALL_MASK,
3172d7a548aSJaghathiswari Rankappagounder Natarajan 	}
3182d7a548aSJaghathiswari Rankappagounder Natarajan };
3192d7a548aSJaghathiswari Rankappagounder Natarajan 
3202d7a548aSJaghathiswari Rankappagounder Natarajan static int regmap_aspeed_pwm_tacho_reg_write(void *context, unsigned int reg,
3212d7a548aSJaghathiswari Rankappagounder Natarajan 					     unsigned int val)
3222d7a548aSJaghathiswari Rankappagounder Natarajan {
3232d7a548aSJaghathiswari Rankappagounder Natarajan 	void __iomem *regs = (void __iomem *)context;
3242d7a548aSJaghathiswari Rankappagounder Natarajan 
3252d7a548aSJaghathiswari Rankappagounder Natarajan 	writel(val, regs + reg);
3262d7a548aSJaghathiswari Rankappagounder Natarajan 	return 0;
3272d7a548aSJaghathiswari Rankappagounder Natarajan }
3282d7a548aSJaghathiswari Rankappagounder Natarajan 
3292d7a548aSJaghathiswari Rankappagounder Natarajan static int regmap_aspeed_pwm_tacho_reg_read(void *context, unsigned int reg,
3302d7a548aSJaghathiswari Rankappagounder Natarajan 					    unsigned int *val)
3312d7a548aSJaghathiswari Rankappagounder Natarajan {
3322d7a548aSJaghathiswari Rankappagounder Natarajan 	void __iomem *regs = (void __iomem *)context;
3332d7a548aSJaghathiswari Rankappagounder Natarajan 
3342d7a548aSJaghathiswari Rankappagounder Natarajan 	*val = readl(regs + reg);
3352d7a548aSJaghathiswari Rankappagounder Natarajan 	return 0;
3362d7a548aSJaghathiswari Rankappagounder Natarajan }
3372d7a548aSJaghathiswari Rankappagounder Natarajan 
3382d7a548aSJaghathiswari Rankappagounder Natarajan static const struct regmap_config aspeed_pwm_tacho_regmap_config = {
3392d7a548aSJaghathiswari Rankappagounder Natarajan 	.reg_bits = 32,
3402d7a548aSJaghathiswari Rankappagounder Natarajan 	.val_bits = 32,
3412d7a548aSJaghathiswari Rankappagounder Natarajan 	.reg_stride = 4,
3422d7a548aSJaghathiswari Rankappagounder Natarajan 	.max_register = ASPEED_PTCR_TYPEO_LIMIT,
3432d7a548aSJaghathiswari Rankappagounder Natarajan 	.reg_write = regmap_aspeed_pwm_tacho_reg_write,
3442d7a548aSJaghathiswari Rankappagounder Natarajan 	.reg_read = regmap_aspeed_pwm_tacho_reg_read,
3452d7a548aSJaghathiswari Rankappagounder Natarajan 	.fast_io = true,
3462d7a548aSJaghathiswari Rankappagounder Natarajan };
3472d7a548aSJaghathiswari Rankappagounder Natarajan 
3482d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_set_clock_enable(struct regmap *regmap, bool val)
3492d7a548aSJaghathiswari Rankappagounder Natarajan {
3502d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, ASPEED_PTCR_CTRL,
3512d7a548aSJaghathiswari Rankappagounder Natarajan 			   ASPEED_PTCR_CTRL_CLK_EN,
3522d7a548aSJaghathiswari Rankappagounder Natarajan 			   val ? ASPEED_PTCR_CTRL_CLK_EN : 0);
3532d7a548aSJaghathiswari Rankappagounder Natarajan }
3542d7a548aSJaghathiswari Rankappagounder Natarajan 
3552d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_set_clock_source(struct regmap *regmap, int val)
3562d7a548aSJaghathiswari Rankappagounder Natarajan {
3572d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, ASPEED_PTCR_CTRL,
3582d7a548aSJaghathiswari Rankappagounder Natarajan 			   ASPEED_PTCR_CTRL_CLK_SRC,
3592d7a548aSJaghathiswari Rankappagounder Natarajan 			   val ? ASPEED_PTCR_CTRL_CLK_SRC : 0);
3602d7a548aSJaghathiswari Rankappagounder Natarajan }
3612d7a548aSJaghathiswari Rankappagounder Natarajan 
3622d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_set_pwm_clock_values(struct regmap *regmap, u8 type,
3632d7a548aSJaghathiswari Rankappagounder Natarajan 					u8 div_high, u8 div_low, u8 unit)
3642d7a548aSJaghathiswari Rankappagounder Natarajan {
3652d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 reg_value = ((div_high << type_params[type].h_value) |
3662d7a548aSJaghathiswari Rankappagounder Natarajan 			 (div_low << type_params[type].l_value) |
3672d7a548aSJaghathiswari Rankappagounder Natarajan 			 (unit << type_params[type].unit_value));
3682d7a548aSJaghathiswari Rankappagounder Natarajan 
3692d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, type_params[type].clk_ctrl_reg,
3702d7a548aSJaghathiswari Rankappagounder Natarajan 			   type_params[type].clk_ctrl_mask, reg_value);
3712d7a548aSJaghathiswari Rankappagounder Natarajan }
3722d7a548aSJaghathiswari Rankappagounder Natarajan 
3732d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_set_pwm_port_enable(struct regmap *regmap, u8 pwm_port,
3742d7a548aSJaghathiswari Rankappagounder Natarajan 				       bool enable)
3752d7a548aSJaghathiswari Rankappagounder Natarajan {
3762d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, pwm_port_params[pwm_port].ctrl_reg,
3772d7a548aSJaghathiswari Rankappagounder Natarajan 			   pwm_port_params[pwm_port].pwm_en,
3782d7a548aSJaghathiswari Rankappagounder Natarajan 			   enable ? pwm_port_params[pwm_port].pwm_en : 0);
3792d7a548aSJaghathiswari Rankappagounder Natarajan }
3802d7a548aSJaghathiswari Rankappagounder Natarajan 
3812d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_set_pwm_port_type(struct regmap *regmap,
3822d7a548aSJaghathiswari Rankappagounder Natarajan 				     u8 pwm_port, u8 type)
3832d7a548aSJaghathiswari Rankappagounder Natarajan {
3842d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 reg_value = (type & 0x1) << pwm_port_params[pwm_port].type_part1;
3852d7a548aSJaghathiswari Rankappagounder Natarajan 
3862d7a548aSJaghathiswari Rankappagounder Natarajan 	reg_value |= (type & 0x2) << pwm_port_params[pwm_port].type_part2;
3872d7a548aSJaghathiswari Rankappagounder Natarajan 
3882d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, pwm_port_params[pwm_port].ctrl_reg,
3892d7a548aSJaghathiswari Rankappagounder Natarajan 			   pwm_port_params[pwm_port].type_mask, reg_value);
3902d7a548aSJaghathiswari Rankappagounder Natarajan }
3912d7a548aSJaghathiswari Rankappagounder Natarajan 
3922d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_set_pwm_port_duty_rising_falling(struct regmap *regmap,
3932d7a548aSJaghathiswari Rankappagounder Natarajan 						    u8 pwm_port, u8 rising,
3942d7a548aSJaghathiswari Rankappagounder Natarajan 						    u8 falling)
3952d7a548aSJaghathiswari Rankappagounder Natarajan {
3962d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 reg_value = (rising <<
3972d7a548aSJaghathiswari Rankappagounder Natarajan 			 pwm_port_params[pwm_port].duty_ctrl_rise_point);
3982d7a548aSJaghathiswari Rankappagounder Natarajan 	reg_value |= (falling <<
3992d7a548aSJaghathiswari Rankappagounder Natarajan 		      pwm_port_params[pwm_port].duty_ctrl_fall_point);
4002d7a548aSJaghathiswari Rankappagounder Natarajan 
4012d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, pwm_port_params[pwm_port].duty_ctrl_reg,
4022d7a548aSJaghathiswari Rankappagounder Natarajan 			   pwm_port_params[pwm_port].duty_ctrl_rise_fall_mask,
4032d7a548aSJaghathiswari Rankappagounder Natarajan 			   reg_value);
4042d7a548aSJaghathiswari Rankappagounder Natarajan }
4052d7a548aSJaghathiswari Rankappagounder Natarajan 
4062d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_set_tacho_type_enable(struct regmap *regmap, u8 type,
4072d7a548aSJaghathiswari Rankappagounder Natarajan 					 bool enable)
4082d7a548aSJaghathiswari Rankappagounder Natarajan {
4092d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, type_params[type].ctrl_reg,
4102d7a548aSJaghathiswari Rankappagounder Natarajan 			   TYPE_CTRL_FAN_TYPE_EN,
4112d7a548aSJaghathiswari Rankappagounder Natarajan 			   enable ? TYPE_CTRL_FAN_TYPE_EN : 0);
4122d7a548aSJaghathiswari Rankappagounder Natarajan }
4132d7a548aSJaghathiswari Rankappagounder Natarajan 
4142d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_set_tacho_type_values(struct regmap *regmap, u8 type,
4152d7a548aSJaghathiswari Rankappagounder Natarajan 					 u8 mode, u16 unit, u8 division)
4162d7a548aSJaghathiswari Rankappagounder Natarajan {
4172d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 reg_value = ((mode << TYPE_CTRL_FAN_MODE) |
4182d7a548aSJaghathiswari Rankappagounder Natarajan 			 (unit << TYPE_CTRL_FAN_PERIOD) |
4192d7a548aSJaghathiswari Rankappagounder Natarajan 			 (division << TYPE_CTRL_FAN_DIVISION));
4202d7a548aSJaghathiswari Rankappagounder Natarajan 
4212d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, type_params[type].ctrl_reg,
4222d7a548aSJaghathiswari Rankappagounder Natarajan 			   TYPE_CTRL_FAN_MASK, reg_value);
4232d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, type_params[type].ctrl_reg1,
4242d7a548aSJaghathiswari Rankappagounder Natarajan 			   TYPE_CTRL_FAN1_MASK, unit << 16);
4252d7a548aSJaghathiswari Rankappagounder Natarajan }
4262d7a548aSJaghathiswari Rankappagounder Natarajan 
4272d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_set_fan_tach_ch_enable(struct regmap *regmap, u8 fan_tach_ch,
4282d7a548aSJaghathiswari Rankappagounder Natarajan 					  bool enable)
4292d7a548aSJaghathiswari Rankappagounder Natarajan {
4302d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, ASPEED_PTCR_CTRL,
4312d7a548aSJaghathiswari Rankappagounder Natarajan 			   ASPEED_PTCR_CTRL_FAN_NUM_EN(fan_tach_ch),
4322d7a548aSJaghathiswari Rankappagounder Natarajan 			   enable ?
4332d7a548aSJaghathiswari Rankappagounder Natarajan 			   ASPEED_PTCR_CTRL_FAN_NUM_EN(fan_tach_ch) : 0);
4342d7a548aSJaghathiswari Rankappagounder Natarajan }
4352d7a548aSJaghathiswari Rankappagounder Natarajan 
4362d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_set_fan_tach_ch_source(struct regmap *regmap, u8 fan_tach_ch,
4372d7a548aSJaghathiswari Rankappagounder Natarajan 					  u8 fan_tach_ch_source)
4382d7a548aSJaghathiswari Rankappagounder Natarajan {
4392d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 reg_value1 = ((fan_tach_ch_source & 0x3) <<
4402d7a548aSJaghathiswari Rankappagounder Natarajan 			  TACH_PWM_SOURCE_BIT01(fan_tach_ch));
4412d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 reg_value2 = (((fan_tach_ch_source & 0x4) >> 2) <<
4422d7a548aSJaghathiswari Rankappagounder Natarajan 			  TACH_PWM_SOURCE_BIT2(fan_tach_ch));
4432d7a548aSJaghathiswari Rankappagounder Natarajan 
4442d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, ASPEED_PTCR_TACH_SOURCE,
4452d7a548aSJaghathiswari Rankappagounder Natarajan 			   TACH_PWM_SOURCE_MASK_BIT01(fan_tach_ch),
4462d7a548aSJaghathiswari Rankappagounder Natarajan 			   reg_value1);
4472d7a548aSJaghathiswari Rankappagounder Natarajan 
4482d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_update_bits(regmap, ASPEED_PTCR_TACH_SOURCE_EXT,
4492d7a548aSJaghathiswari Rankappagounder Natarajan 			   TACH_PWM_SOURCE_MASK_BIT2(fan_tach_ch),
4502d7a548aSJaghathiswari Rankappagounder Natarajan 			   reg_value2);
4512d7a548aSJaghathiswari Rankappagounder Natarajan }
4522d7a548aSJaghathiswari Rankappagounder Natarajan 
4532d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_set_pwm_port_fan_ctrl(struct aspeed_pwm_tacho_data *priv,
4542d7a548aSJaghathiswari Rankappagounder Natarajan 					 u8 index, u8 fan_ctrl)
4552d7a548aSJaghathiswari Rankappagounder Natarajan {
4562d7a548aSJaghathiswari Rankappagounder Natarajan 	u16 period, dc_time_on;
4572d7a548aSJaghathiswari Rankappagounder Natarajan 
4582d7a548aSJaghathiswari Rankappagounder Natarajan 	period = priv->type_pwm_clock_unit[priv->pwm_port_type[index]];
4592d7a548aSJaghathiswari Rankappagounder Natarajan 	period += 1;
4602d7a548aSJaghathiswari Rankappagounder Natarajan 	dc_time_on = (fan_ctrl * period) / PWM_MAX;
4612d7a548aSJaghathiswari Rankappagounder Natarajan 
4622d7a548aSJaghathiswari Rankappagounder Natarajan 	if (dc_time_on == 0) {
4632d7a548aSJaghathiswari Rankappagounder Natarajan 		aspeed_set_pwm_port_enable(priv->regmap, index, false);
4642d7a548aSJaghathiswari Rankappagounder Natarajan 	} else {
4652d7a548aSJaghathiswari Rankappagounder Natarajan 		if (dc_time_on == period)
4662d7a548aSJaghathiswari Rankappagounder Natarajan 			dc_time_on = 0;
4672d7a548aSJaghathiswari Rankappagounder Natarajan 
4682d7a548aSJaghathiswari Rankappagounder Natarajan 		aspeed_set_pwm_port_duty_rising_falling(priv->regmap, index, 0,
4692d7a548aSJaghathiswari Rankappagounder Natarajan 							dc_time_on);
4702d7a548aSJaghathiswari Rankappagounder Natarajan 		aspeed_set_pwm_port_enable(priv->regmap, index, true);
4712d7a548aSJaghathiswari Rankappagounder Natarajan 	}
4722d7a548aSJaghathiswari Rankappagounder Natarajan }
4732d7a548aSJaghathiswari Rankappagounder Natarajan 
4742d7a548aSJaghathiswari Rankappagounder Natarajan static u32 aspeed_get_fan_tach_ch_measure_period(struct aspeed_pwm_tacho_data
4752d7a548aSJaghathiswari Rankappagounder Natarajan 						 *priv, u8 type)
4762d7a548aSJaghathiswari Rankappagounder Natarajan {
4772d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 clk;
4782d7a548aSJaghathiswari Rankappagounder Natarajan 	u16 tacho_unit;
4792d7a548aSJaghathiswari Rankappagounder Natarajan 	u8 clk_unit, div_h, div_l, tacho_div;
4802d7a548aSJaghathiswari Rankappagounder Natarajan 
4812d7a548aSJaghathiswari Rankappagounder Natarajan 	clk = priv->clk_freq;
4822d7a548aSJaghathiswari Rankappagounder Natarajan 	clk_unit = priv->type_pwm_clock_unit[type];
4832d7a548aSJaghathiswari Rankappagounder Natarajan 	div_h = priv->type_pwm_clock_division_h[type];
4842d7a548aSJaghathiswari Rankappagounder Natarajan 	div_h = 0x1 << div_h;
4852d7a548aSJaghathiswari Rankappagounder Natarajan 	div_l = priv->type_pwm_clock_division_l[type];
4862d7a548aSJaghathiswari Rankappagounder Natarajan 	if (div_l == 0)
4872d7a548aSJaghathiswari Rankappagounder Natarajan 		div_l = 1;
4882d7a548aSJaghathiswari Rankappagounder Natarajan 	else
4892d7a548aSJaghathiswari Rankappagounder Natarajan 		div_l = div_l * 2;
4902d7a548aSJaghathiswari Rankappagounder Natarajan 
4912d7a548aSJaghathiswari Rankappagounder Natarajan 	tacho_unit = priv->type_fan_tach_unit[type];
4922d7a548aSJaghathiswari Rankappagounder Natarajan 	tacho_div = priv->type_fan_tach_clock_division[type];
4932d7a548aSJaghathiswari Rankappagounder Natarajan 
4942d7a548aSJaghathiswari Rankappagounder Natarajan 	tacho_div = 0x4 << (tacho_div * 2);
4952d7a548aSJaghathiswari Rankappagounder Natarajan 	return clk / (clk_unit * div_h * div_l * tacho_div * tacho_unit);
4962d7a548aSJaghathiswari Rankappagounder Natarajan }
4972d7a548aSJaghathiswari Rankappagounder Natarajan 
4987ed1c5e5SPatrick Venture static int aspeed_get_fan_tach_ch_rpm(struct aspeed_pwm_tacho_data *priv,
4992d7a548aSJaghathiswari Rankappagounder Natarajan 				      u8 fan_tach_ch)
5002d7a548aSJaghathiswari Rankappagounder Natarajan {
5012d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 raw_data, tach_div, clk_source, sec, val;
5022d7a548aSJaghathiswari Rankappagounder Natarajan 	u8 fan_tach_ch_source, type;
5032d7a548aSJaghathiswari Rankappagounder Natarajan 
5042d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_write(priv->regmap, ASPEED_PTCR_TRIGGER, 0);
5052d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_write(priv->regmap, ASPEED_PTCR_TRIGGER, 0x1 << fan_tach_ch);
5062d7a548aSJaghathiswari Rankappagounder Natarajan 
5072d7a548aSJaghathiswari Rankappagounder Natarajan 	fan_tach_ch_source = priv->fan_tach_ch_source[fan_tach_ch];
5082d7a548aSJaghathiswari Rankappagounder Natarajan 	type = priv->pwm_port_type[fan_tach_ch_source];
5092d7a548aSJaghathiswari Rankappagounder Natarajan 
5102d7a548aSJaghathiswari Rankappagounder Natarajan 	sec = (1000 / aspeed_get_fan_tach_ch_measure_period(priv, type));
5112d7a548aSJaghathiswari Rankappagounder Natarajan 	msleep(sec);
5122d7a548aSJaghathiswari Rankappagounder Natarajan 
5132d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_read(priv->regmap, ASPEED_PTCR_RESULT, &val);
5147ed1c5e5SPatrick Venture 	if (!(val & RESULT_STATUS_MASK))
5157ed1c5e5SPatrick Venture 		return -ETIMEDOUT;
5167ed1c5e5SPatrick Venture 
5172d7a548aSJaghathiswari Rankappagounder Natarajan 	raw_data = val & RESULT_VALUE_MASK;
5182d7a548aSJaghathiswari Rankappagounder Natarajan 	tach_div = priv->type_fan_tach_clock_division[type];
5192d7a548aSJaghathiswari Rankappagounder Natarajan 	tach_div = 0x4 << (tach_div * 2);
5202d7a548aSJaghathiswari Rankappagounder Natarajan 	clk_source = priv->clk_freq;
5212d7a548aSJaghathiswari Rankappagounder Natarajan 
5222d7a548aSJaghathiswari Rankappagounder Natarajan 	if (raw_data == 0)
5232d7a548aSJaghathiswari Rankappagounder Natarajan 		return 0;
5242d7a548aSJaghathiswari Rankappagounder Natarajan 
5252d7a548aSJaghathiswari Rankappagounder Natarajan 	return (clk_source * 60) / (2 * raw_data * tach_div);
5262d7a548aSJaghathiswari Rankappagounder Natarajan }
5272d7a548aSJaghathiswari Rankappagounder Natarajan 
5282d7a548aSJaghathiswari Rankappagounder Natarajan static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
5292d7a548aSJaghathiswari Rankappagounder Natarajan 		       const char *buf, size_t count)
5302d7a548aSJaghathiswari Rankappagounder Natarajan {
5312d7a548aSJaghathiswari Rankappagounder Natarajan 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
5322d7a548aSJaghathiswari Rankappagounder Natarajan 	int index = sensor_attr->index;
5332d7a548aSJaghathiswari Rankappagounder Natarajan 	int ret;
5342d7a548aSJaghathiswari Rankappagounder Natarajan 	struct aspeed_pwm_tacho_data *priv = dev_get_drvdata(dev);
5352d7a548aSJaghathiswari Rankappagounder Natarajan 	long fan_ctrl;
5362d7a548aSJaghathiswari Rankappagounder Natarajan 
5372d7a548aSJaghathiswari Rankappagounder Natarajan 	ret = kstrtol(buf, 10, &fan_ctrl);
5382d7a548aSJaghathiswari Rankappagounder Natarajan 	if (ret != 0)
5392d7a548aSJaghathiswari Rankappagounder Natarajan 		return ret;
5402d7a548aSJaghathiswari Rankappagounder Natarajan 
5412d7a548aSJaghathiswari Rankappagounder Natarajan 	if (fan_ctrl < 0 || fan_ctrl > PWM_MAX)
5422d7a548aSJaghathiswari Rankappagounder Natarajan 		return -EINVAL;
5432d7a548aSJaghathiswari Rankappagounder Natarajan 
5442d7a548aSJaghathiswari Rankappagounder Natarajan 	if (priv->pwm_port_fan_ctrl[index] == fan_ctrl)
5452d7a548aSJaghathiswari Rankappagounder Natarajan 		return count;
5462d7a548aSJaghathiswari Rankappagounder Natarajan 
5472d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->pwm_port_fan_ctrl[index] = fan_ctrl;
5482d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_set_pwm_port_fan_ctrl(priv, index, fan_ctrl);
5492d7a548aSJaghathiswari Rankappagounder Natarajan 
5502d7a548aSJaghathiswari Rankappagounder Natarajan 	return count;
5512d7a548aSJaghathiswari Rankappagounder Natarajan }
5522d7a548aSJaghathiswari Rankappagounder Natarajan 
5532d7a548aSJaghathiswari Rankappagounder Natarajan static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
5542d7a548aSJaghathiswari Rankappagounder Natarajan 			char *buf)
5552d7a548aSJaghathiswari Rankappagounder Natarajan {
5562d7a548aSJaghathiswari Rankappagounder Natarajan 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
5572d7a548aSJaghathiswari Rankappagounder Natarajan 	int index = sensor_attr->index;
5582d7a548aSJaghathiswari Rankappagounder Natarajan 	struct aspeed_pwm_tacho_data *priv = dev_get_drvdata(dev);
5592d7a548aSJaghathiswari Rankappagounder Natarajan 
5602d7a548aSJaghathiswari Rankappagounder Natarajan 	return sprintf(buf, "%u\n", priv->pwm_port_fan_ctrl[index]);
5612d7a548aSJaghathiswari Rankappagounder Natarajan }
5622d7a548aSJaghathiswari Rankappagounder Natarajan 
5632d7a548aSJaghathiswari Rankappagounder Natarajan static ssize_t show_rpm(struct device *dev, struct device_attribute *attr,
5642d7a548aSJaghathiswari Rankappagounder Natarajan 			char *buf)
5652d7a548aSJaghathiswari Rankappagounder Natarajan {
5662d7a548aSJaghathiswari Rankappagounder Natarajan 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
5672d7a548aSJaghathiswari Rankappagounder Natarajan 	int index = sensor_attr->index;
5687ed1c5e5SPatrick Venture 	int rpm;
5692d7a548aSJaghathiswari Rankappagounder Natarajan 	struct aspeed_pwm_tacho_data *priv = dev_get_drvdata(dev);
5702d7a548aSJaghathiswari Rankappagounder Natarajan 
5712d7a548aSJaghathiswari Rankappagounder Natarajan 	rpm = aspeed_get_fan_tach_ch_rpm(priv, index);
5727ed1c5e5SPatrick Venture 	if (rpm < 0)
5737ed1c5e5SPatrick Venture 		return rpm;
5742d7a548aSJaghathiswari Rankappagounder Natarajan 
5757ed1c5e5SPatrick Venture 	return sprintf(buf, "%d\n", rpm);
5762d7a548aSJaghathiswari Rankappagounder Natarajan }
5772d7a548aSJaghathiswari Rankappagounder Natarajan 
5782d7a548aSJaghathiswari Rankappagounder Natarajan static umode_t pwm_is_visible(struct kobject *kobj,
5792d7a548aSJaghathiswari Rankappagounder Natarajan 			      struct attribute *a, int index)
5802d7a548aSJaghathiswari Rankappagounder Natarajan {
5812d7a548aSJaghathiswari Rankappagounder Natarajan 	struct device *dev = container_of(kobj, struct device, kobj);
5822d7a548aSJaghathiswari Rankappagounder Natarajan 	struct aspeed_pwm_tacho_data *priv = dev_get_drvdata(dev);
5832d7a548aSJaghathiswari Rankappagounder Natarajan 
5842d7a548aSJaghathiswari Rankappagounder Natarajan 	if (!priv->pwm_present[index])
5852d7a548aSJaghathiswari Rankappagounder Natarajan 		return 0;
5862d7a548aSJaghathiswari Rankappagounder Natarajan 	return a->mode;
5872d7a548aSJaghathiswari Rankappagounder Natarajan }
5882d7a548aSJaghathiswari Rankappagounder Natarajan 
5892d7a548aSJaghathiswari Rankappagounder Natarajan static umode_t fan_dev_is_visible(struct kobject *kobj,
5902d7a548aSJaghathiswari Rankappagounder Natarajan 				  struct attribute *a, int index)
5912d7a548aSJaghathiswari Rankappagounder Natarajan {
5922d7a548aSJaghathiswari Rankappagounder Natarajan 	struct device *dev = container_of(kobj, struct device, kobj);
5932d7a548aSJaghathiswari Rankappagounder Natarajan 	struct aspeed_pwm_tacho_data *priv = dev_get_drvdata(dev);
5942d7a548aSJaghathiswari Rankappagounder Natarajan 
5952d7a548aSJaghathiswari Rankappagounder Natarajan 	if (!priv->fan_tach_present[index])
5962d7a548aSJaghathiswari Rankappagounder Natarajan 		return 0;
5972d7a548aSJaghathiswari Rankappagounder Natarajan 	return a->mode;
5982d7a548aSJaghathiswari Rankappagounder Natarajan }
5992d7a548aSJaghathiswari Rankappagounder Natarajan 
6002d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(pwm1, 0644,
601*5f348fa3SStefan Schaeckeler 			show_pwm, set_pwm, 0);
6022d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(pwm2, 0644,
603*5f348fa3SStefan Schaeckeler 			show_pwm, set_pwm, 1);
6042d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(pwm3, 0644,
605*5f348fa3SStefan Schaeckeler 			show_pwm, set_pwm, 2);
6062d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(pwm4, 0644,
607*5f348fa3SStefan Schaeckeler 			show_pwm, set_pwm, 3);
6082d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(pwm5, 0644,
609*5f348fa3SStefan Schaeckeler 			show_pwm, set_pwm, 4);
6102d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(pwm6, 0644,
611*5f348fa3SStefan Schaeckeler 			show_pwm, set_pwm, 5);
6122d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(pwm7, 0644,
613*5f348fa3SStefan Schaeckeler 			show_pwm, set_pwm, 6);
614*5f348fa3SStefan Schaeckeler static SENSOR_DEVICE_ATTR(pwm8, 0644,
6152d7a548aSJaghathiswari Rankappagounder Natarajan 			show_pwm, set_pwm, 7);
6162d7a548aSJaghathiswari Rankappagounder Natarajan static struct attribute *pwm_dev_attrs[] = {
6172d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_pwm1.dev_attr.attr,
6182d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_pwm2.dev_attr.attr,
6192d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_pwm3.dev_attr.attr,
6202d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_pwm4.dev_attr.attr,
6212d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_pwm5.dev_attr.attr,
6222d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_pwm6.dev_attr.attr,
6232d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_pwm7.dev_attr.attr,
624*5f348fa3SStefan Schaeckeler 	&sensor_dev_attr_pwm8.dev_attr.attr,
6252d7a548aSJaghathiswari Rankappagounder Natarajan 	NULL,
6262d7a548aSJaghathiswari Rankappagounder Natarajan };
6272d7a548aSJaghathiswari Rankappagounder Natarajan 
6282d7a548aSJaghathiswari Rankappagounder Natarajan static const struct attribute_group pwm_dev_group = {
6292d7a548aSJaghathiswari Rankappagounder Natarajan 	.attrs = pwm_dev_attrs,
6302d7a548aSJaghathiswari Rankappagounder Natarajan 	.is_visible = pwm_is_visible,
6312d7a548aSJaghathiswari Rankappagounder Natarajan };
6322d7a548aSJaghathiswari Rankappagounder Natarajan 
6332d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan1_input, 0444,
634*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 0);
6352d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan2_input, 0444,
636*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 1);
6372d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan3_input, 0444,
638*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 2);
6392d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan4_input, 0444,
640*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 3);
6412d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan5_input, 0444,
642*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 4);
6432d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan6_input, 0444,
644*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 5);
6452d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan7_input, 0444,
646*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 6);
6472d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan8_input, 0444,
648*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 7);
6492d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan9_input, 0444,
650*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 8);
6512d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan10_input, 0444,
652*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 9);
6532d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan11_input, 0444,
654*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 10);
6552d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan12_input, 0444,
656*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 11);
6572d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan13_input, 0444,
658*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 12);
6592d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan14_input, 0444,
660*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 13);
6612d7a548aSJaghathiswari Rankappagounder Natarajan static SENSOR_DEVICE_ATTR(fan15_input, 0444,
662*5f348fa3SStefan Schaeckeler 		show_rpm, NULL, 14);
663*5f348fa3SStefan Schaeckeler static SENSOR_DEVICE_ATTR(fan16_input, 0444,
6642d7a548aSJaghathiswari Rankappagounder Natarajan 		show_rpm, NULL, 15);
6652d7a548aSJaghathiswari Rankappagounder Natarajan static struct attribute *fan_dev_attrs[] = {
6662d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan1_input.dev_attr.attr,
6672d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan2_input.dev_attr.attr,
6682d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan3_input.dev_attr.attr,
6692d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan4_input.dev_attr.attr,
6702d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan5_input.dev_attr.attr,
6712d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan6_input.dev_attr.attr,
6722d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan7_input.dev_attr.attr,
6732d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan8_input.dev_attr.attr,
6742d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan9_input.dev_attr.attr,
6752d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan10_input.dev_attr.attr,
6762d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan11_input.dev_attr.attr,
6772d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan12_input.dev_attr.attr,
6782d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan13_input.dev_attr.attr,
6792d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan14_input.dev_attr.attr,
6802d7a548aSJaghathiswari Rankappagounder Natarajan 	&sensor_dev_attr_fan15_input.dev_attr.attr,
681*5f348fa3SStefan Schaeckeler 	&sensor_dev_attr_fan16_input.dev_attr.attr,
6822d7a548aSJaghathiswari Rankappagounder Natarajan 	NULL
6832d7a548aSJaghathiswari Rankappagounder Natarajan };
6842d7a548aSJaghathiswari Rankappagounder Natarajan 
6852d7a548aSJaghathiswari Rankappagounder Natarajan static const struct attribute_group fan_dev_group = {
6862d7a548aSJaghathiswari Rankappagounder Natarajan 	.attrs = fan_dev_attrs,
6872d7a548aSJaghathiswari Rankappagounder Natarajan 	.is_visible = fan_dev_is_visible,
6882d7a548aSJaghathiswari Rankappagounder Natarajan };
6892d7a548aSJaghathiswari Rankappagounder Natarajan 
6902d7a548aSJaghathiswari Rankappagounder Natarajan /*
6912d7a548aSJaghathiswari Rankappagounder Natarajan  * The clock type is type M :
6922d7a548aSJaghathiswari Rankappagounder Natarajan  * The PWM frequency = 24MHz / (type M clock division L bit *
6932d7a548aSJaghathiswari Rankappagounder Natarajan  * type M clock division H bit * (type M PWM period bit + 1))
6942d7a548aSJaghathiswari Rankappagounder Natarajan  */
6952d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_create_type(struct aspeed_pwm_tacho_data *priv)
6962d7a548aSJaghathiswari Rankappagounder Natarajan {
6972d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->type_pwm_clock_division_h[TYPEM] = M_PWM_DIV_H;
6982d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->type_pwm_clock_division_l[TYPEM] = M_PWM_DIV_L;
6992d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->type_pwm_clock_unit[TYPEM] = M_PWM_PERIOD;
7002d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_set_pwm_clock_values(priv->regmap, TYPEM, M_PWM_DIV_H,
7012d7a548aSJaghathiswari Rankappagounder Natarajan 				    M_PWM_DIV_L, M_PWM_PERIOD);
7022d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_set_tacho_type_enable(priv->regmap, TYPEM, true);
7032d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->type_fan_tach_clock_division[TYPEM] = M_TACH_CLK_DIV;
7042d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->type_fan_tach_unit[TYPEM] = M_TACH_UNIT;
7052d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_set_tacho_type_values(priv->regmap, TYPEM, M_TACH_MODE,
7062d7a548aSJaghathiswari Rankappagounder Natarajan 				     M_TACH_UNIT, M_TACH_CLK_DIV);
7072d7a548aSJaghathiswari Rankappagounder Natarajan }
7082d7a548aSJaghathiswari Rankappagounder Natarajan 
7092d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_create_pwm_port(struct aspeed_pwm_tacho_data *priv,
7102d7a548aSJaghathiswari Rankappagounder Natarajan 				   u8 pwm_port)
7112d7a548aSJaghathiswari Rankappagounder Natarajan {
7122d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_set_pwm_port_enable(priv->regmap, pwm_port, true);
7132d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->pwm_present[pwm_port] = true;
7142d7a548aSJaghathiswari Rankappagounder Natarajan 
7152d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->pwm_port_type[pwm_port] = TYPEM;
7162d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_set_pwm_port_type(priv->regmap, pwm_port, TYPEM);
7172d7a548aSJaghathiswari Rankappagounder Natarajan 
7182d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->pwm_port_fan_ctrl[pwm_port] = INIT_FAN_CTRL;
7192d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_set_pwm_port_fan_ctrl(priv, pwm_port, INIT_FAN_CTRL);
7202d7a548aSJaghathiswari Rankappagounder Natarajan }
7212d7a548aSJaghathiswari Rankappagounder Natarajan 
7222d7a548aSJaghathiswari Rankappagounder Natarajan static void aspeed_create_fan_tach_channel(struct aspeed_pwm_tacho_data *priv,
7232d7a548aSJaghathiswari Rankappagounder Natarajan 					   u8 *fan_tach_ch,
7242d7a548aSJaghathiswari Rankappagounder Natarajan 					   int count,
7252d7a548aSJaghathiswari Rankappagounder Natarajan 					   u8 pwm_source)
7262d7a548aSJaghathiswari Rankappagounder Natarajan {
7272d7a548aSJaghathiswari Rankappagounder Natarajan 	u8 val, index;
7282d7a548aSJaghathiswari Rankappagounder Natarajan 
7292d7a548aSJaghathiswari Rankappagounder Natarajan 	for (val = 0; val < count; val++) {
7302d7a548aSJaghathiswari Rankappagounder Natarajan 		index = fan_tach_ch[val];
7312d7a548aSJaghathiswari Rankappagounder Natarajan 		aspeed_set_fan_tach_ch_enable(priv->regmap, index, true);
7322d7a548aSJaghathiswari Rankappagounder Natarajan 		priv->fan_tach_present[index] = true;
7332d7a548aSJaghathiswari Rankappagounder Natarajan 		priv->fan_tach_ch_source[index] = pwm_source;
7342d7a548aSJaghathiswari Rankappagounder Natarajan 		aspeed_set_fan_tach_ch_source(priv->regmap, index, pwm_source);
7352d7a548aSJaghathiswari Rankappagounder Natarajan 	}
7362d7a548aSJaghathiswari Rankappagounder Natarajan }
7372d7a548aSJaghathiswari Rankappagounder Natarajan 
7382d7a548aSJaghathiswari Rankappagounder Natarajan static int aspeed_create_fan(struct device *dev,
7392d7a548aSJaghathiswari Rankappagounder Natarajan 			     struct device_node *child,
7402d7a548aSJaghathiswari Rankappagounder Natarajan 			     struct aspeed_pwm_tacho_data *priv)
7412d7a548aSJaghathiswari Rankappagounder Natarajan {
7422d7a548aSJaghathiswari Rankappagounder Natarajan 	u8 *fan_tach_ch;
7432d7a548aSJaghathiswari Rankappagounder Natarajan 	u32 pwm_port;
7442d7a548aSJaghathiswari Rankappagounder Natarajan 	int ret, count;
7452d7a548aSJaghathiswari Rankappagounder Natarajan 
7462d7a548aSJaghathiswari Rankappagounder Natarajan 	ret = of_property_read_u32(child, "reg", &pwm_port);
7472d7a548aSJaghathiswari Rankappagounder Natarajan 	if (ret)
7482d7a548aSJaghathiswari Rankappagounder Natarajan 		return ret;
7492d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_create_pwm_port(priv, (u8)pwm_port);
7502d7a548aSJaghathiswari Rankappagounder Natarajan 
7512d7a548aSJaghathiswari Rankappagounder Natarajan 	count = of_property_count_u8_elems(child, "aspeed,fan-tach-ch");
7522d7a548aSJaghathiswari Rankappagounder Natarajan 	if (count < 1)
7532d7a548aSJaghathiswari Rankappagounder Natarajan 		return -EINVAL;
7542d7a548aSJaghathiswari Rankappagounder Natarajan 	fan_tach_ch = devm_kzalloc(dev, sizeof(*fan_tach_ch) * count,
7552d7a548aSJaghathiswari Rankappagounder Natarajan 				   GFP_KERNEL);
7562d7a548aSJaghathiswari Rankappagounder Natarajan 	if (!fan_tach_ch)
7572d7a548aSJaghathiswari Rankappagounder Natarajan 		return -ENOMEM;
7582d7a548aSJaghathiswari Rankappagounder Natarajan 	ret = of_property_read_u8_array(child, "aspeed,fan-tach-ch",
7592d7a548aSJaghathiswari Rankappagounder Natarajan 					fan_tach_ch, count);
7602d7a548aSJaghathiswari Rankappagounder Natarajan 	if (ret)
7612d7a548aSJaghathiswari Rankappagounder Natarajan 		return ret;
7622d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_create_fan_tach_channel(priv, fan_tach_ch, count, pwm_port);
7632d7a548aSJaghathiswari Rankappagounder Natarajan 
7642d7a548aSJaghathiswari Rankappagounder Natarajan 	return 0;
7652d7a548aSJaghathiswari Rankappagounder Natarajan }
7662d7a548aSJaghathiswari Rankappagounder Natarajan 
7672d7a548aSJaghathiswari Rankappagounder Natarajan static int aspeed_pwm_tacho_probe(struct platform_device *pdev)
7682d7a548aSJaghathiswari Rankappagounder Natarajan {
7692d7a548aSJaghathiswari Rankappagounder Natarajan 	struct device *dev = &pdev->dev;
7702d7a548aSJaghathiswari Rankappagounder Natarajan 	struct device_node *np, *child;
7712d7a548aSJaghathiswari Rankappagounder Natarajan 	struct aspeed_pwm_tacho_data *priv;
7722d7a548aSJaghathiswari Rankappagounder Natarajan 	void __iomem *regs;
7732d7a548aSJaghathiswari Rankappagounder Natarajan 	struct resource *res;
7742d7a548aSJaghathiswari Rankappagounder Natarajan 	struct device *hwmon;
7752d7a548aSJaghathiswari Rankappagounder Natarajan 	struct clk *clk;
7762d7a548aSJaghathiswari Rankappagounder Natarajan 	int ret;
7772d7a548aSJaghathiswari Rankappagounder Natarajan 
7782d7a548aSJaghathiswari Rankappagounder Natarajan 	np = dev->of_node;
7792d7a548aSJaghathiswari Rankappagounder Natarajan 
7802d7a548aSJaghathiswari Rankappagounder Natarajan 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7812d7a548aSJaghathiswari Rankappagounder Natarajan 	if (!res)
7822d7a548aSJaghathiswari Rankappagounder Natarajan 		return -ENOENT;
7832d7a548aSJaghathiswari Rankappagounder Natarajan 	regs = devm_ioremap_resource(dev, res);
7842d7a548aSJaghathiswari Rankappagounder Natarajan 	if (IS_ERR(regs))
7852d7a548aSJaghathiswari Rankappagounder Natarajan 		return PTR_ERR(regs);
7862d7a548aSJaghathiswari Rankappagounder Natarajan 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
7872d7a548aSJaghathiswari Rankappagounder Natarajan 	if (!priv)
7882d7a548aSJaghathiswari Rankappagounder Natarajan 		return -ENOMEM;
7892d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->regmap = devm_regmap_init(dev, NULL, (__force void *)regs,
7902d7a548aSJaghathiswari Rankappagounder Natarajan 			&aspeed_pwm_tacho_regmap_config);
7912d7a548aSJaghathiswari Rankappagounder Natarajan 	if (IS_ERR(priv->regmap))
7922d7a548aSJaghathiswari Rankappagounder Natarajan 		return PTR_ERR(priv->regmap);
7932d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_write(priv->regmap, ASPEED_PTCR_TACH_SOURCE, 0);
7942d7a548aSJaghathiswari Rankappagounder Natarajan 	regmap_write(priv->regmap, ASPEED_PTCR_TACH_SOURCE_EXT, 0);
7952d7a548aSJaghathiswari Rankappagounder Natarajan 
7962d7a548aSJaghathiswari Rankappagounder Natarajan 	clk = devm_clk_get(dev, NULL);
7972d7a548aSJaghathiswari Rankappagounder Natarajan 	if (IS_ERR(clk))
7982d7a548aSJaghathiswari Rankappagounder Natarajan 		return -ENODEV;
7992d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->clk_freq = clk_get_rate(clk);
8002d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_set_clock_enable(priv->regmap, true);
8012d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_set_clock_source(priv->regmap, 0);
8022d7a548aSJaghathiswari Rankappagounder Natarajan 
8032d7a548aSJaghathiswari Rankappagounder Natarajan 	aspeed_create_type(priv);
8042d7a548aSJaghathiswari Rankappagounder Natarajan 
8052d7a548aSJaghathiswari Rankappagounder Natarajan 	for_each_child_of_node(np, child) {
8062d7a548aSJaghathiswari Rankappagounder Natarajan 		ret = aspeed_create_fan(dev, child, priv);
8072d7a548aSJaghathiswari Rankappagounder Natarajan 		of_node_put(child);
8082d7a548aSJaghathiswari Rankappagounder Natarajan 		if (ret)
8092d7a548aSJaghathiswari Rankappagounder Natarajan 			return ret;
8102d7a548aSJaghathiswari Rankappagounder Natarajan 	}
8112d7a548aSJaghathiswari Rankappagounder Natarajan 
8122d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->groups[0] = &pwm_dev_group;
8132d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->groups[1] = &fan_dev_group;
8142d7a548aSJaghathiswari Rankappagounder Natarajan 	priv->groups[2] = NULL;
8152d7a548aSJaghathiswari Rankappagounder Natarajan 	hwmon = devm_hwmon_device_register_with_groups(dev,
8162d7a548aSJaghathiswari Rankappagounder Natarajan 						       "aspeed_pwm_tacho",
8172d7a548aSJaghathiswari Rankappagounder Natarajan 						       priv, priv->groups);
8182d7a548aSJaghathiswari Rankappagounder Natarajan 	return PTR_ERR_OR_ZERO(hwmon);
8192d7a548aSJaghathiswari Rankappagounder Natarajan }
8202d7a548aSJaghathiswari Rankappagounder Natarajan 
8212d7a548aSJaghathiswari Rankappagounder Natarajan static const struct of_device_id of_pwm_tacho_match_table[] = {
8222d7a548aSJaghathiswari Rankappagounder Natarajan 	{ .compatible = "aspeed,ast2400-pwm-tacho", },
8232d7a548aSJaghathiswari Rankappagounder Natarajan 	{ .compatible = "aspeed,ast2500-pwm-tacho", },
8242d7a548aSJaghathiswari Rankappagounder Natarajan 	{},
8252d7a548aSJaghathiswari Rankappagounder Natarajan };
8262d7a548aSJaghathiswari Rankappagounder Natarajan MODULE_DEVICE_TABLE(of, of_pwm_tacho_match_table);
8272d7a548aSJaghathiswari Rankappagounder Natarajan 
8282d7a548aSJaghathiswari Rankappagounder Natarajan static struct platform_driver aspeed_pwm_tacho_driver = {
8292d7a548aSJaghathiswari Rankappagounder Natarajan 	.probe		= aspeed_pwm_tacho_probe,
8302d7a548aSJaghathiswari Rankappagounder Natarajan 	.driver		= {
8312d7a548aSJaghathiswari Rankappagounder Natarajan 		.name	= "aspeed_pwm_tacho",
8322d7a548aSJaghathiswari Rankappagounder Natarajan 		.of_match_table = of_pwm_tacho_match_table,
8332d7a548aSJaghathiswari Rankappagounder Natarajan 	},
8342d7a548aSJaghathiswari Rankappagounder Natarajan };
8352d7a548aSJaghathiswari Rankappagounder Natarajan 
8362d7a548aSJaghathiswari Rankappagounder Natarajan module_platform_driver(aspeed_pwm_tacho_driver);
8372d7a548aSJaghathiswari Rankappagounder Natarajan 
8382d7a548aSJaghathiswari Rankappagounder Natarajan MODULE_AUTHOR("Jaghathiswari Rankappagounder Natarajan <jaghu@google.com>");
8392d7a548aSJaghathiswari Rankappagounder Natarajan MODULE_DESCRIPTION("ASPEED PWM and Fan Tacho device driver");
8402d7a548aSJaghathiswari Rankappagounder Natarajan MODULE_LICENSE("GPL");
841