xref: /openbmc/linux/drivers/hwmon/mr75203.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
19d823351SRahul Tanwar // SPDX-License-Identifier: GPL-2.0
29d823351SRahul Tanwar /*
39d823351SRahul Tanwar  * Copyright (C) 2020 MaxLinear, Inc.
49d823351SRahul Tanwar  *
59d823351SRahul Tanwar  * This driver is a hardware monitoring driver for PVT controller
69d823351SRahul Tanwar  * (MR75203) which is used to configure & control Moortec embedded
79d823351SRahul Tanwar  * analog IP to enable multiple embedded temperature sensor(TS),
89d823351SRahul Tanwar  * voltage monitor(VM) & process detector(PD) modules.
99d823351SRahul Tanwar  */
109d823351SRahul Tanwar #include <linux/bits.h>
119d823351SRahul Tanwar #include <linux/clk.h>
12a4dd0b80SEliav Farber #include <linux/debugfs.h>
139d823351SRahul Tanwar #include <linux/hwmon.h>
14*25f98688SChristophe JAILLET #include <linux/kstrtox.h>
159d823351SRahul Tanwar #include <linux/module.h>
169d823351SRahul Tanwar #include <linux/mod_devicetable.h>
179d823351SRahul Tanwar #include <linux/mutex.h>
189d823351SRahul Tanwar #include <linux/platform_device.h>
199d823351SRahul Tanwar #include <linux/property.h>
209d823351SRahul Tanwar #include <linux/regmap.h>
219d823351SRahul Tanwar #include <linux/reset.h>
22430c0d7fSEliav Farber #include <linux/slab.h>
23d59eacaaSDaniel Lezcano #include <linux/units.h>
249d823351SRahul Tanwar 
259d823351SRahul Tanwar /* PVT Common register */
269d823351SRahul Tanwar #define PVT_IP_CONFIG	0x04
279d823351SRahul Tanwar #define TS_NUM_MSK	GENMASK(4, 0)
289d823351SRahul Tanwar #define TS_NUM_SFT	0
299d823351SRahul Tanwar #define PD_NUM_MSK	GENMASK(12, 8)
309d823351SRahul Tanwar #define PD_NUM_SFT	8
319d823351SRahul Tanwar #define VM_NUM_MSK	GENMASK(20, 16)
329d823351SRahul Tanwar #define VM_NUM_SFT	16
339d823351SRahul Tanwar #define CH_NUM_MSK	GENMASK(31, 24)
349d823351SRahul Tanwar #define CH_NUM_SFT	24
359d823351SRahul Tanwar 
36b7f5ac92SEliav Farber #define VM_NUM_MAX	(VM_NUM_MSK >> VM_NUM_SFT)
37b7f5ac92SEliav Farber 
389d823351SRahul Tanwar /* Macro Common Register */
399d823351SRahul Tanwar #define CLK_SYNTH		0x00
409d823351SRahul Tanwar #define CLK_SYNTH_LO_SFT	0
419d823351SRahul Tanwar #define CLK_SYNTH_HI_SFT	8
429d823351SRahul Tanwar #define CLK_SYNTH_HOLD_SFT	16
439d823351SRahul Tanwar #define CLK_SYNTH_EN		BIT(24)
449d823351SRahul Tanwar #define CLK_SYS_CYCLES_MAX	514
459d823351SRahul Tanwar #define CLK_SYS_CYCLES_MIN	2
469d823351SRahul Tanwar 
479d823351SRahul Tanwar #define SDIF_DISABLE	0x04
489d823351SRahul Tanwar 
499d823351SRahul Tanwar #define SDIF_STAT	0x08
509d823351SRahul Tanwar #define SDIF_BUSY	BIT(0)
519d823351SRahul Tanwar #define SDIF_LOCK	BIT(1)
529d823351SRahul Tanwar 
539d823351SRahul Tanwar #define SDIF_W		0x0c
549d823351SRahul Tanwar #define SDIF_PROG	BIT(31)
559d823351SRahul Tanwar #define SDIF_WRN_W	BIT(27)
569d823351SRahul Tanwar #define SDIF_WRN_R	0x00
579d823351SRahul Tanwar #define SDIF_ADDR_SFT	24
589d823351SRahul Tanwar 
599d823351SRahul Tanwar #define SDIF_HALT	0x10
609d823351SRahul Tanwar #define SDIF_CTRL	0x14
619d823351SRahul Tanwar #define SDIF_SMPL_CTRL	0x20
629d823351SRahul Tanwar 
639d823351SRahul Tanwar /* TS & PD Individual Macro Register */
649d823351SRahul Tanwar #define COM_REG_SIZE	0x40
659d823351SRahul Tanwar 
669d823351SRahul Tanwar #define SDIF_DONE(n)	(COM_REG_SIZE + 0x14 + 0x40 * (n))
679d823351SRahul Tanwar #define SDIF_SMPL_DONE	BIT(0)
689d823351SRahul Tanwar 
699d823351SRahul Tanwar #define SDIF_DATA(n)	(COM_REG_SIZE + 0x18 + 0x40 * (n))
709d823351SRahul Tanwar #define SAMPLE_DATA_MSK	GENMASK(15, 0)
719d823351SRahul Tanwar 
729d823351SRahul Tanwar #define HILO_RESET(n)	(COM_REG_SIZE + 0x2c + 0x40 * (n))
739d823351SRahul Tanwar 
749d823351SRahul Tanwar /* VM Individual Macro Register */
759d823351SRahul Tanwar #define VM_COM_REG_SIZE	0x200
7691a9e063SEliav Farber #define VM_SDIF_DONE(vm)	(VM_COM_REG_SIZE + 0x34 + 0x200 * (vm))
7791a9e063SEliav Farber #define VM_SDIF_DATA(vm, ch)	\
7891a9e063SEliav Farber 	(VM_COM_REG_SIZE + 0x40 + 0x200 * (vm) + 0x4 * (ch))
799d823351SRahul Tanwar 
809d823351SRahul Tanwar /* SDA Slave Register */
819d823351SRahul Tanwar #define IP_CTRL			0x00
829d823351SRahul Tanwar #define IP_RST_REL		BIT(1)
839d823351SRahul Tanwar #define IP_RUN_CONT		BIT(3)
849d823351SRahul Tanwar #define IP_AUTO			BIT(8)
859d823351SRahul Tanwar #define IP_VM_MODE		BIT(10)
869d823351SRahul Tanwar 
879d823351SRahul Tanwar #define IP_CFG			0x01
889d823351SRahul Tanwar #define CFG0_MODE_2		BIT(0)
899d823351SRahul Tanwar #define CFG0_PARALLEL_OUT	0
909d823351SRahul Tanwar #define CFG0_12_BIT		0
919d823351SRahul Tanwar #define CFG1_VOL_MEAS_MODE	0
929d823351SRahul Tanwar #define CFG1_PARALLEL_OUT	0
939d823351SRahul Tanwar #define CFG1_14_BIT		0
949d823351SRahul Tanwar 
959d823351SRahul Tanwar #define IP_DATA		0x03
969d823351SRahul Tanwar 
979d823351SRahul Tanwar #define IP_POLL		0x04
989d823351SRahul Tanwar #define VM_CH_INIT	BIT(20)
999d823351SRahul Tanwar #define VM_CH_REQ	BIT(21)
1009d823351SRahul Tanwar 
1019d823351SRahul Tanwar #define IP_TMR			0x05
102a8d6d499SArseny Demidov #define POWER_DELAY_CYCLE_256	0x100
1039d823351SRahul Tanwar #define POWER_DELAY_CYCLE_64	0x40
1049d823351SRahul Tanwar 
1059d823351SRahul Tanwar #define PVT_POLL_DELAY_US	20
1069d823351SRahul Tanwar #define PVT_POLL_TIMEOUT_US	20000
1079d823351SRahul Tanwar #define PVT_CONV_BITS		10
1089d823351SRahul Tanwar #define PVT_N_CONST		90
1099d823351SRahul Tanwar #define PVT_R_CONST		245805
1109d823351SRahul Tanwar 
11194c025b6SEliav Farber #define PVT_TEMP_MIN_mC		-40000
11294c025b6SEliav Farber #define PVT_TEMP_MAX_mC		125000
11394c025b6SEliav Farber 
11494c025b6SEliav Farber /* Temperature coefficients for series 5 */
11594c025b6SEliav Farber #define PVT_SERIES5_H_CONST	200000
11694c025b6SEliav Farber #define PVT_SERIES5_G_CONST	60000
11794c025b6SEliav Farber #define PVT_SERIES5_J_CONST	-100
11894c025b6SEliav Farber #define PVT_SERIES5_CAL5_CONST	4094
11994c025b6SEliav Farber 
1203b12ca79SEliav Farber /* Temperature coefficients for series 6 */
1213b12ca79SEliav Farber #define PVT_SERIES6_H_CONST	249400
1223b12ca79SEliav Farber #define PVT_SERIES6_G_CONST	57400
1233b12ca79SEliav Farber #define PVT_SERIES6_J_CONST	0
1243b12ca79SEliav Farber #define PVT_SERIES6_CAL5_CONST	4096
1253b12ca79SEliav Farber 
1263b12ca79SEliav Farber #define TEMPERATURE_SENSOR_SERIES_5	5
1273b12ca79SEliav Farber #define TEMPERATURE_SENSOR_SERIES_6	6
1283b12ca79SEliav Farber 
129430c0d7fSEliav Farber #define PRE_SCALER_X1	1
130430c0d7fSEliav Farber #define PRE_SCALER_X2	2
131430c0d7fSEliav Farber 
132b7f5ac92SEliav Farber /**
133b7f5ac92SEliav Farber  * struct voltage_device - VM single input parameters.
134b7f5ac92SEliav Farber  * @vm_map: Map channel number to VM index.
135b7f5ac92SEliav Farber  * @ch_map: Map channel number to channel index.
136430c0d7fSEliav Farber  * @pre_scaler: Pre scaler value (1 or 2) used to normalize the voltage output
137430c0d7fSEliav Farber  *              result.
138b7f5ac92SEliav Farber  *
139b7f5ac92SEliav Farber  * The structure provides mapping between channel-number (0..N-1) to VM-index
140b7f5ac92SEliav Farber  * (0..num_vm-1) and channel-index (0..ch_num-1) where N = num_vm * ch_num.
141430c0d7fSEliav Farber  * It also provides normalization factor for the VM equation.
142b7f5ac92SEliav Farber  */
143b7f5ac92SEliav Farber struct voltage_device {
144b7f5ac92SEliav Farber 	u32 vm_map;
145b7f5ac92SEliav Farber 	u32 ch_map;
146430c0d7fSEliav Farber 	u32 pre_scaler;
147b7f5ac92SEliav Farber };
148b7f5ac92SEliav Farber 
149b7f5ac92SEliav Farber /**
150b7f5ac92SEliav Farber  * struct voltage_channels - VM channel count.
151b7f5ac92SEliav Farber  * @total: Total number of channels in all VMs.
152b7f5ac92SEliav Farber  * @max: Maximum number of channels among all VMs.
153b7f5ac92SEliav Farber  *
154b7f5ac92SEliav Farber  * The structure provides channel count information across all VMs.
155b7f5ac92SEliav Farber  */
156b7f5ac92SEliav Farber struct voltage_channels {
157b7f5ac92SEliav Farber 	u32 total;
158b7f5ac92SEliav Farber 	u8 max;
159b7f5ac92SEliav Farber };
160b7f5ac92SEliav Farber 
1613b12ca79SEliav Farber struct temp_coeff {
1623b12ca79SEliav Farber 	u32 h;
1633b12ca79SEliav Farber 	u32 g;
1643b12ca79SEliav Farber 	u32 cal5;
1653b12ca79SEliav Farber 	s32 j;
1663b12ca79SEliav Farber };
1673b12ca79SEliav Farber 
1689d823351SRahul Tanwar struct pvt_device {
1699d823351SRahul Tanwar 	struct regmap		*c_map;
1709d823351SRahul Tanwar 	struct regmap		*t_map;
1719d823351SRahul Tanwar 	struct regmap		*p_map;
1729d823351SRahul Tanwar 	struct regmap		*v_map;
1739d823351SRahul Tanwar 	struct clk		*clk;
1749d823351SRahul Tanwar 	struct reset_control	*rst;
175a4dd0b80SEliav Farber 	struct dentry		*dbgfs_dir;
176b7f5ac92SEliav Farber 	struct voltage_device	*vd;
177b7f5ac92SEliav Farber 	struct voltage_channels	vm_channels;
1783b12ca79SEliav Farber 	struct temp_coeff	ts_coeff;
1799d823351SRahul Tanwar 	u32			t_num;
1809d823351SRahul Tanwar 	u32			p_num;
1819d823351SRahul Tanwar 	u32			v_num;
1829d823351SRahul Tanwar 	u32			ip_freq;
1839d823351SRahul Tanwar };
1849d823351SRahul Tanwar 
pvt_ts_coeff_j_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)185a4dd0b80SEliav Farber static ssize_t pvt_ts_coeff_j_read(struct file *file, char __user *user_buf,
186a4dd0b80SEliav Farber 				   size_t count, loff_t *ppos)
187a4dd0b80SEliav Farber {
188a4dd0b80SEliav Farber 	struct pvt_device *pvt = file->private_data;
189a4dd0b80SEliav Farber 	unsigned int len;
190a4dd0b80SEliav Farber 	char buf[13];
191a4dd0b80SEliav Farber 
192a4dd0b80SEliav Farber 	len = scnprintf(buf, sizeof(buf), "%d\n", pvt->ts_coeff.j);
193a4dd0b80SEliav Farber 
194a4dd0b80SEliav Farber 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
195a4dd0b80SEliav Farber }
196a4dd0b80SEliav Farber 
pvt_ts_coeff_j_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)197a4dd0b80SEliav Farber static ssize_t pvt_ts_coeff_j_write(struct file *file,
198a4dd0b80SEliav Farber 				    const char __user *user_buf,
199a4dd0b80SEliav Farber 				    size_t count, loff_t *ppos)
200a4dd0b80SEliav Farber {
201a4dd0b80SEliav Farber 	struct pvt_device *pvt = file->private_data;
202a4dd0b80SEliav Farber 	int ret;
203a4dd0b80SEliav Farber 
204a4dd0b80SEliav Farber 	ret = kstrtos32_from_user(user_buf, count, 0, &pvt->ts_coeff.j);
205a4dd0b80SEliav Farber 	if (ret)
206a4dd0b80SEliav Farber 		return ret;
207a4dd0b80SEliav Farber 
208a4dd0b80SEliav Farber 	return count;
209a4dd0b80SEliav Farber }
210a4dd0b80SEliav Farber 
211a4dd0b80SEliav Farber static const struct file_operations pvt_ts_coeff_j_fops = {
212a4dd0b80SEliav Farber 	.read = pvt_ts_coeff_j_read,
213a4dd0b80SEliav Farber 	.write = pvt_ts_coeff_j_write,
214a4dd0b80SEliav Farber 	.open = simple_open,
215a4dd0b80SEliav Farber 	.owner = THIS_MODULE,
216a4dd0b80SEliav Farber 	.llseek = default_llseek,
217a4dd0b80SEliav Farber };
218a4dd0b80SEliav Farber 
devm_pvt_ts_dbgfs_remove(void * data)219a4dd0b80SEliav Farber static void devm_pvt_ts_dbgfs_remove(void *data)
220a4dd0b80SEliav Farber {
221a4dd0b80SEliav Farber 	struct pvt_device *pvt = (struct pvt_device *)data;
222a4dd0b80SEliav Farber 
223a4dd0b80SEliav Farber 	debugfs_remove_recursive(pvt->dbgfs_dir);
224a4dd0b80SEliav Farber 	pvt->dbgfs_dir = NULL;
225a4dd0b80SEliav Farber }
226a4dd0b80SEliav Farber 
pvt_ts_dbgfs_create(struct pvt_device * pvt,struct device * dev)227a4dd0b80SEliav Farber static int pvt_ts_dbgfs_create(struct pvt_device *pvt, struct device *dev)
228a4dd0b80SEliav Farber {
229a4dd0b80SEliav Farber 	pvt->dbgfs_dir = debugfs_create_dir(dev_name(dev), NULL);
230a4dd0b80SEliav Farber 
231a4dd0b80SEliav Farber 	debugfs_create_u32("ts_coeff_h", 0644, pvt->dbgfs_dir,
232a4dd0b80SEliav Farber 			   &pvt->ts_coeff.h);
233a4dd0b80SEliav Farber 	debugfs_create_u32("ts_coeff_g", 0644, pvt->dbgfs_dir,
234a4dd0b80SEliav Farber 			   &pvt->ts_coeff.g);
235a4dd0b80SEliav Farber 	debugfs_create_u32("ts_coeff_cal5", 0644, pvt->dbgfs_dir,
236a4dd0b80SEliav Farber 			   &pvt->ts_coeff.cal5);
237a4dd0b80SEliav Farber 	debugfs_create_file("ts_coeff_j", 0644, pvt->dbgfs_dir, pvt,
238a4dd0b80SEliav Farber 			    &pvt_ts_coeff_j_fops);
239a4dd0b80SEliav Farber 
240a4dd0b80SEliav Farber 	return devm_add_action_or_reset(dev, devm_pvt_ts_dbgfs_remove, pvt);
241a4dd0b80SEliav Farber }
242a4dd0b80SEliav Farber 
pvt_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)2439d823351SRahul Tanwar static umode_t pvt_is_visible(const void *data, enum hwmon_sensor_types type,
2449d823351SRahul Tanwar 			      u32 attr, int channel)
2459d823351SRahul Tanwar {
2469d823351SRahul Tanwar 	switch (type) {
2479d823351SRahul Tanwar 	case hwmon_temp:
2489d823351SRahul Tanwar 		if (attr == hwmon_temp_input)
2499d823351SRahul Tanwar 			return 0444;
2509d823351SRahul Tanwar 		break;
2519d823351SRahul Tanwar 	case hwmon_in:
2529d823351SRahul Tanwar 		if (attr == hwmon_in_input)
2539d823351SRahul Tanwar 			return 0444;
2549d823351SRahul Tanwar 		break;
2559d823351SRahul Tanwar 	default:
2569d823351SRahul Tanwar 		break;
2579d823351SRahul Tanwar 	}
2589d823351SRahul Tanwar 	return 0;
2599d823351SRahul Tanwar }
2609d823351SRahul Tanwar 
pvt_calc_temp(struct pvt_device * pvt,u32 nbs)26194c025b6SEliav Farber static long pvt_calc_temp(struct pvt_device *pvt, u32 nbs)
26294c025b6SEliav Farber {
26394c025b6SEliav Farber 	/*
26494c025b6SEliav Farber 	 * Convert the register value to degrees centigrade temperature:
26594c025b6SEliav Farber 	 * T = G + H * (n / cal5 - 0.5) + J * F
26694c025b6SEliav Farber 	 */
2673b12ca79SEliav Farber 	struct temp_coeff *ts_coeff = &pvt->ts_coeff;
2683b12ca79SEliav Farber 
2693b12ca79SEliav Farber 	s64 tmp = ts_coeff->g +
270903882c7SEliav Farber 		div_s64(ts_coeff->h * (s64)nbs, ts_coeff->cal5) -
2713b12ca79SEliav Farber 		ts_coeff->h / 2 +
272903882c7SEliav Farber 		div_s64(ts_coeff->j * (s64)pvt->ip_freq, HZ_PER_MHZ);
27394c025b6SEliav Farber 
27494c025b6SEliav Farber 	return clamp_val(tmp, PVT_TEMP_MIN_mC, PVT_TEMP_MAX_mC);
27594c025b6SEliav Farber }
27694c025b6SEliav Farber 
pvt_read_temp(struct device * dev,u32 attr,int channel,long * val)2779d823351SRahul Tanwar static int pvt_read_temp(struct device *dev, u32 attr, int channel, long *val)
2789d823351SRahul Tanwar {
2799d823351SRahul Tanwar 	struct pvt_device *pvt = dev_get_drvdata(dev);
2809d823351SRahul Tanwar 	struct regmap *t_map = pvt->t_map;
2819d823351SRahul Tanwar 	u32 stat, nbs;
2829d823351SRahul Tanwar 	int ret;
2839d823351SRahul Tanwar 
2849d823351SRahul Tanwar 	switch (attr) {
2859d823351SRahul Tanwar 	case hwmon_temp_input:
2869d823351SRahul Tanwar 		ret = regmap_read_poll_timeout(t_map, SDIF_DONE(channel),
2879d823351SRahul Tanwar 					       stat, stat & SDIF_SMPL_DONE,
2889d823351SRahul Tanwar 					       PVT_POLL_DELAY_US,
2899d823351SRahul Tanwar 					       PVT_POLL_TIMEOUT_US);
2909d823351SRahul Tanwar 		if (ret)
2919d823351SRahul Tanwar 			return ret;
2929d823351SRahul Tanwar 
2939d823351SRahul Tanwar 		ret = regmap_read(t_map, SDIF_DATA(channel), &nbs);
2949d823351SRahul Tanwar 		if (ret < 0)
2959d823351SRahul Tanwar 			return ret;
2969d823351SRahul Tanwar 
2979d823351SRahul Tanwar 		nbs &= SAMPLE_DATA_MSK;
2989d823351SRahul Tanwar 
2999d823351SRahul Tanwar 		/*
3009d823351SRahul Tanwar 		 * Convert the register value to
3019d823351SRahul Tanwar 		 * degrees centigrade temperature
3029d823351SRahul Tanwar 		 */
30394c025b6SEliav Farber 		*val = pvt_calc_temp(pvt, nbs);
3049d823351SRahul Tanwar 
3059d823351SRahul Tanwar 		return 0;
3069d823351SRahul Tanwar 	default:
3079d823351SRahul Tanwar 		return -EOPNOTSUPP;
3089d823351SRahul Tanwar 	}
3099d823351SRahul Tanwar }
3109d823351SRahul Tanwar 
pvt_read_in(struct device * dev,u32 attr,int channel,long * val)3119d823351SRahul Tanwar static int pvt_read_in(struct device *dev, u32 attr, int channel, long *val)
3129d823351SRahul Tanwar {
3139d823351SRahul Tanwar 	struct pvt_device *pvt = dev_get_drvdata(dev);
3149d823351SRahul Tanwar 	struct regmap *v_map = pvt->v_map;
315430c0d7fSEliav Farber 	u32 n, stat, pre_scaler;
31691a9e063SEliav Farber 	u8 vm_idx, ch_idx;
3179d823351SRahul Tanwar 	int ret;
3189d823351SRahul Tanwar 
319b7f5ac92SEliav Farber 	if (channel >= pvt->vm_channels.total)
3209d823351SRahul Tanwar 		return -EINVAL;
3219d823351SRahul Tanwar 
322b7f5ac92SEliav Farber 	vm_idx = pvt->vd[channel].vm_map;
323b7f5ac92SEliav Farber 	ch_idx = pvt->vd[channel].ch_map;
3249d823351SRahul Tanwar 
3259d823351SRahul Tanwar 	switch (attr) {
3269d823351SRahul Tanwar 	case hwmon_in_input:
3279d823351SRahul Tanwar 		ret = regmap_read_poll_timeout(v_map, VM_SDIF_DONE(vm_idx),
3289d823351SRahul Tanwar 					       stat, stat & SDIF_SMPL_DONE,
3299d823351SRahul Tanwar 					       PVT_POLL_DELAY_US,
3309d823351SRahul Tanwar 					       PVT_POLL_TIMEOUT_US);
3319d823351SRahul Tanwar 		if (ret)
3329d823351SRahul Tanwar 			return ret;
3339d823351SRahul Tanwar 
33491a9e063SEliav Farber 		ret = regmap_read(v_map, VM_SDIF_DATA(vm_idx, ch_idx), &n);
3359d823351SRahul Tanwar 		if (ret < 0)
3369d823351SRahul Tanwar 			return ret;
3379d823351SRahul Tanwar 
3389d823351SRahul Tanwar 		n &= SAMPLE_DATA_MSK;
339430c0d7fSEliav Farber 		pre_scaler = pvt->vd[channel].pre_scaler;
340227a3a2fSEliav Farber 		/*
341227a3a2fSEliav Farber 		 * Convert the N bitstream count into voltage.
342227a3a2fSEliav Farber 		 * To support negative voltage calculation for 64bit machines
343227a3a2fSEliav Farber 		 * n must be cast to long, since n and *val differ both in
344227a3a2fSEliav Farber 		 * signedness and in size.
345227a3a2fSEliav Farber 		 * Division is used instead of right shift, because for signed
346227a3a2fSEliav Farber 		 * numbers, the sign bit is used to fill the vacated bit
347227a3a2fSEliav Farber 		 * positions, and if the number is negative, 1 is used.
348227a3a2fSEliav Farber 		 * BIT(x) may not be used instead of (1 << x) because it's
349227a3a2fSEliav Farber 		 * unsigned.
350227a3a2fSEliav Farber 		 */
351430c0d7fSEliav Farber 		*val = pre_scaler * (PVT_N_CONST * (long)n - PVT_R_CONST) /
352430c0d7fSEliav Farber 			(1 << PVT_CONV_BITS);
3539d823351SRahul Tanwar 
3549d823351SRahul Tanwar 		return 0;
3559d823351SRahul Tanwar 	default:
3569d823351SRahul Tanwar 		return -EOPNOTSUPP;
3579d823351SRahul Tanwar 	}
3589d823351SRahul Tanwar }
3599d823351SRahul Tanwar 
pvt_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)3609d823351SRahul Tanwar static int pvt_read(struct device *dev, enum hwmon_sensor_types type,
3619d823351SRahul Tanwar 		    u32 attr, int channel, long *val)
3629d823351SRahul Tanwar {
3639d823351SRahul Tanwar 	switch (type) {
3649d823351SRahul Tanwar 	case hwmon_temp:
3659d823351SRahul Tanwar 		return pvt_read_temp(dev, attr, channel, val);
3669d823351SRahul Tanwar 	case hwmon_in:
3679d823351SRahul Tanwar 		return pvt_read_in(dev, attr, channel, val);
3689d823351SRahul Tanwar 	default:
3699d823351SRahul Tanwar 		return -EOPNOTSUPP;
3709d823351SRahul Tanwar 	}
3719d823351SRahul Tanwar }
3729d823351SRahul Tanwar 
3739d823351SRahul Tanwar static struct hwmon_channel_info pvt_temp = {
3749d823351SRahul Tanwar 	.type = hwmon_temp,
3759d823351SRahul Tanwar };
3769d823351SRahul Tanwar 
3779d823351SRahul Tanwar static struct hwmon_channel_info pvt_in = {
3789d823351SRahul Tanwar 	.type = hwmon_in,
3799d823351SRahul Tanwar };
3809d823351SRahul Tanwar 
3819d823351SRahul Tanwar static const struct hwmon_ops pvt_hwmon_ops = {
3829d823351SRahul Tanwar 	.is_visible = pvt_is_visible,
3839d823351SRahul Tanwar 	.read = pvt_read,
3849d823351SRahul Tanwar };
3859d823351SRahul Tanwar 
3869d823351SRahul Tanwar static struct hwmon_chip_info pvt_chip_info = {
3879d823351SRahul Tanwar 	.ops = &pvt_hwmon_ops,
3889d823351SRahul Tanwar };
3899d823351SRahul Tanwar 
pvt_init(struct pvt_device * pvt)3909d823351SRahul Tanwar static int pvt_init(struct pvt_device *pvt)
3919d823351SRahul Tanwar {
3929d823351SRahul Tanwar 	u16 sys_freq, key, middle, low = 4, high = 8;
3939d823351SRahul Tanwar 	struct regmap *t_map = pvt->t_map;
3949d823351SRahul Tanwar 	struct regmap *p_map = pvt->p_map;
3959d823351SRahul Tanwar 	struct regmap *v_map = pvt->v_map;
3969d823351SRahul Tanwar 	u32 t_num = pvt->t_num;
3979d823351SRahul Tanwar 	u32 p_num = pvt->p_num;
3989d823351SRahul Tanwar 	u32 v_num = pvt->v_num;
3999d823351SRahul Tanwar 	u32 clk_synth, val;
4009d823351SRahul Tanwar 	int ret;
4019d823351SRahul Tanwar 
4029d823351SRahul Tanwar 	sys_freq = clk_get_rate(pvt->clk) / HZ_PER_MHZ;
4039d823351SRahul Tanwar 	while (high >= low) {
4049d823351SRahul Tanwar 		middle = (low + high + 1) / 2;
4059d823351SRahul Tanwar 		key = DIV_ROUND_CLOSEST(sys_freq, middle);
4069d823351SRahul Tanwar 		if (key > CLK_SYS_CYCLES_MAX) {
4079d823351SRahul Tanwar 			low = middle + 1;
4089d823351SRahul Tanwar 			continue;
4099d823351SRahul Tanwar 		} else if (key < CLK_SYS_CYCLES_MIN) {
4109d823351SRahul Tanwar 			high = middle - 1;
4119d823351SRahul Tanwar 			continue;
4129d823351SRahul Tanwar 		} else {
4139d823351SRahul Tanwar 			break;
4149d823351SRahul Tanwar 		}
4159d823351SRahul Tanwar 	}
4169d823351SRahul Tanwar 
4179d823351SRahul Tanwar 	/*
4189d823351SRahul Tanwar 	 * The system supports 'clk_sys' to 'clk_ip' frequency ratios
4199d823351SRahul Tanwar 	 * from 2:1 to 512:1
4209d823351SRahul Tanwar 	 */
4219d823351SRahul Tanwar 	key = clamp_val(key, CLK_SYS_CYCLES_MIN, CLK_SYS_CYCLES_MAX) - 2;
4229d823351SRahul Tanwar 
4239d823351SRahul Tanwar 	clk_synth = ((key + 1) >> 1) << CLK_SYNTH_LO_SFT |
4249d823351SRahul Tanwar 		    (key >> 1) << CLK_SYNTH_HI_SFT |
4259d823351SRahul Tanwar 		    (key >> 1) << CLK_SYNTH_HOLD_SFT | CLK_SYNTH_EN;
4269d823351SRahul Tanwar 
42794c025b6SEliav Farber 	pvt->ip_freq = clk_get_rate(pvt->clk) / (key + 2);
4289d823351SRahul Tanwar 
4299d823351SRahul Tanwar 	if (t_num) {
4309d823351SRahul Tanwar 		ret = regmap_write(t_map, SDIF_SMPL_CTRL, 0x0);
4319d823351SRahul Tanwar 		if (ret < 0)
4329d823351SRahul Tanwar 			return ret;
4339d823351SRahul Tanwar 
4349d823351SRahul Tanwar 		ret = regmap_write(t_map, SDIF_HALT, 0x0);
4359d823351SRahul Tanwar 		if (ret < 0)
4369d823351SRahul Tanwar 			return ret;
4379d823351SRahul Tanwar 
4389d823351SRahul Tanwar 		ret = regmap_write(t_map, CLK_SYNTH, clk_synth);
4399d823351SRahul Tanwar 		if (ret < 0)
4409d823351SRahul Tanwar 			return ret;
4419d823351SRahul Tanwar 
4429d823351SRahul Tanwar 		ret = regmap_write(t_map, SDIF_DISABLE, 0x0);
4439d823351SRahul Tanwar 		if (ret < 0)
4449d823351SRahul Tanwar 			return ret;
4459d823351SRahul Tanwar 
4469d823351SRahul Tanwar 		ret = regmap_read_poll_timeout(t_map, SDIF_STAT,
4479d823351SRahul Tanwar 					       val, !(val & SDIF_BUSY),
4489d823351SRahul Tanwar 					       PVT_POLL_DELAY_US,
4499d823351SRahul Tanwar 					       PVT_POLL_TIMEOUT_US);
4509d823351SRahul Tanwar 		if (ret)
4519d823351SRahul Tanwar 			return ret;
4529d823351SRahul Tanwar 
4539d823351SRahul Tanwar 		val = CFG0_MODE_2 | CFG0_PARALLEL_OUT | CFG0_12_BIT |
4549d823351SRahul Tanwar 		      IP_CFG << SDIF_ADDR_SFT | SDIF_WRN_W | SDIF_PROG;
4559d823351SRahul Tanwar 		ret = regmap_write(t_map, SDIF_W, val);
4569d823351SRahul Tanwar 		if (ret < 0)
4579d823351SRahul Tanwar 			return ret;
4589d823351SRahul Tanwar 
4599d823351SRahul Tanwar 		ret = regmap_read_poll_timeout(t_map, SDIF_STAT,
4609d823351SRahul Tanwar 					       val, !(val & SDIF_BUSY),
4619d823351SRahul Tanwar 					       PVT_POLL_DELAY_US,
4629d823351SRahul Tanwar 					       PVT_POLL_TIMEOUT_US);
4639d823351SRahul Tanwar 		if (ret)
4649d823351SRahul Tanwar 			return ret;
4659d823351SRahul Tanwar 
4669d823351SRahul Tanwar 		val = POWER_DELAY_CYCLE_256 | IP_TMR << SDIF_ADDR_SFT |
4679d823351SRahul Tanwar 			      SDIF_WRN_W | SDIF_PROG;
4689d823351SRahul Tanwar 		ret = regmap_write(t_map, SDIF_W, val);
4699d823351SRahul Tanwar 		if (ret < 0)
4709d823351SRahul Tanwar 			return ret;
4719d823351SRahul Tanwar 
4729d823351SRahul Tanwar 		ret = regmap_read_poll_timeout(t_map, SDIF_STAT,
4739d823351SRahul Tanwar 					       val, !(val & SDIF_BUSY),
4749d823351SRahul Tanwar 					       PVT_POLL_DELAY_US,
4759d823351SRahul Tanwar 					       PVT_POLL_TIMEOUT_US);
4769d823351SRahul Tanwar 		if (ret)
4779d823351SRahul Tanwar 			return ret;
4789d823351SRahul Tanwar 
4799d823351SRahul Tanwar 		val = IP_RST_REL | IP_RUN_CONT | IP_AUTO |
4809d823351SRahul Tanwar 		      IP_CTRL << SDIF_ADDR_SFT |
4819d823351SRahul Tanwar 		      SDIF_WRN_W | SDIF_PROG;
4829d823351SRahul Tanwar 		ret = regmap_write(t_map, SDIF_W, val);
4839d823351SRahul Tanwar 		if (ret < 0)
4849d823351SRahul Tanwar 			return ret;
4859d823351SRahul Tanwar 	}
4869d823351SRahul Tanwar 
4879d823351SRahul Tanwar 	if (p_num) {
4889d823351SRahul Tanwar 		ret = regmap_write(p_map, SDIF_HALT, 0x0);
4899d823351SRahul Tanwar 		if (ret < 0)
4909d823351SRahul Tanwar 			return ret;
4919d823351SRahul Tanwar 
4929d823351SRahul Tanwar 		ret = regmap_write(p_map, SDIF_DISABLE, BIT(p_num) - 1);
4939d823351SRahul Tanwar 		if (ret < 0)
4949d823351SRahul Tanwar 			return ret;
4959d823351SRahul Tanwar 
4969d823351SRahul Tanwar 		ret = regmap_write(p_map, CLK_SYNTH, clk_synth);
4979d823351SRahul Tanwar 		if (ret < 0)
4989d823351SRahul Tanwar 			return ret;
4999d823351SRahul Tanwar 	}
5009d823351SRahul Tanwar 
5019d823351SRahul Tanwar 	if (v_num) {
5029d823351SRahul Tanwar 		ret = regmap_write(v_map, SDIF_SMPL_CTRL, 0x0);
5039d823351SRahul Tanwar 		if (ret < 0)
5049d823351SRahul Tanwar 			return ret;
5059d823351SRahul Tanwar 
5069d823351SRahul Tanwar 		ret = regmap_write(v_map, SDIF_HALT, 0x0);
5079d823351SRahul Tanwar 		if (ret < 0)
5089d823351SRahul Tanwar 			return ret;
5099d823351SRahul Tanwar 
5109d823351SRahul Tanwar 		ret = regmap_write(v_map, CLK_SYNTH, clk_synth);
5119d823351SRahul Tanwar 		if (ret < 0)
5129d823351SRahul Tanwar 			return ret;
5139d823351SRahul Tanwar 
5149d823351SRahul Tanwar 		ret = regmap_write(v_map, SDIF_DISABLE, 0x0);
5159d823351SRahul Tanwar 		if (ret < 0)
5169d823351SRahul Tanwar 			return ret;
5179d823351SRahul Tanwar 
5189d823351SRahul Tanwar 		ret = regmap_read_poll_timeout(v_map, SDIF_STAT,
5199d823351SRahul Tanwar 					       val, !(val & SDIF_BUSY),
5209d823351SRahul Tanwar 					       PVT_POLL_DELAY_US,
5219d823351SRahul Tanwar 					       PVT_POLL_TIMEOUT_US);
5229d823351SRahul Tanwar 		if (ret)
5239d823351SRahul Tanwar 			return ret;
5249d823351SRahul Tanwar 
525b7f5ac92SEliav Farber 		val = (BIT(pvt->vm_channels.max) - 1) | VM_CH_INIT |
526e43212e0SEliav Farber 		      IP_POLL << SDIF_ADDR_SFT | SDIF_WRN_W | SDIF_PROG;
527e43212e0SEliav Farber 		ret = regmap_write(v_map, SDIF_W, val);
528e43212e0SEliav Farber 		if (ret < 0)
529e43212e0SEliav Farber 			return ret;
530e43212e0SEliav Farber 
531e43212e0SEliav Farber 		ret = regmap_read_poll_timeout(v_map, SDIF_STAT,
532e43212e0SEliav Farber 					       val, !(val & SDIF_BUSY),
533e43212e0SEliav Farber 					       PVT_POLL_DELAY_US,
534e43212e0SEliav Farber 					       PVT_POLL_TIMEOUT_US);
535e43212e0SEliav Farber 		if (ret)
536e43212e0SEliav Farber 			return ret;
537e43212e0SEliav Farber 
5389d823351SRahul Tanwar 		val = CFG1_VOL_MEAS_MODE | CFG1_PARALLEL_OUT |
5399d823351SRahul Tanwar 		      CFG1_14_BIT | IP_CFG << SDIF_ADDR_SFT |
5409d823351SRahul Tanwar 		      SDIF_WRN_W | SDIF_PROG;
5419d823351SRahul Tanwar 		ret = regmap_write(v_map, SDIF_W, val);
5429d823351SRahul Tanwar 		if (ret < 0)
5439d823351SRahul Tanwar 			return ret;
5449d823351SRahul Tanwar 
5459d823351SRahul Tanwar 		ret = regmap_read_poll_timeout(v_map, SDIF_STAT,
5469d823351SRahul Tanwar 					       val, !(val & SDIF_BUSY),
5479d823351SRahul Tanwar 					       PVT_POLL_DELAY_US,
5489d823351SRahul Tanwar 					       PVT_POLL_TIMEOUT_US);
5499d823351SRahul Tanwar 		if (ret)
5509d823351SRahul Tanwar 			return ret;
5519d823351SRahul Tanwar 
5529d823351SRahul Tanwar 		val = POWER_DELAY_CYCLE_64 | IP_TMR << SDIF_ADDR_SFT |
5539d823351SRahul Tanwar 		      SDIF_WRN_W | SDIF_PROG;
5549d823351SRahul Tanwar 		ret = regmap_write(v_map, SDIF_W, val);
5559d823351SRahul Tanwar 		if (ret < 0)
5569d823351SRahul Tanwar 			return ret;
5579d823351SRahul Tanwar 
5589d823351SRahul Tanwar 		ret = regmap_read_poll_timeout(v_map, SDIF_STAT,
5599d823351SRahul Tanwar 					       val, !(val & SDIF_BUSY),
5609d823351SRahul Tanwar 					       PVT_POLL_DELAY_US,
5619d823351SRahul Tanwar 					       PVT_POLL_TIMEOUT_US);
5629d823351SRahul Tanwar 		if (ret)
5639d823351SRahul Tanwar 			return ret;
5649d823351SRahul Tanwar 
5659d823351SRahul Tanwar 		val = IP_RST_REL | IP_RUN_CONT | IP_AUTO | IP_VM_MODE |
5669d823351SRahul Tanwar 		      IP_CTRL << SDIF_ADDR_SFT |
5679d823351SRahul Tanwar 		      SDIF_WRN_W | SDIF_PROG;
5689d823351SRahul Tanwar 		ret = regmap_write(v_map, SDIF_W, val);
5699d823351SRahul Tanwar 		if (ret < 0)
5709d823351SRahul Tanwar 			return ret;
5719d823351SRahul Tanwar 	}
5729d823351SRahul Tanwar 
5739d823351SRahul Tanwar 	return 0;
5749d823351SRahul Tanwar }
5759d823351SRahul Tanwar 
5769d823351SRahul Tanwar static struct regmap_config pvt_regmap_config = {
5779d823351SRahul Tanwar 	.reg_bits = 32,
5789d823351SRahul Tanwar 	.reg_stride = 4,
5799d823351SRahul Tanwar 	.val_bits = 32,
5809d823351SRahul Tanwar };
5819d823351SRahul Tanwar 
pvt_get_regmap(struct platform_device * pdev,char * reg_name,struct pvt_device * pvt)5829d823351SRahul Tanwar static int pvt_get_regmap(struct platform_device *pdev, char *reg_name,
5839d823351SRahul Tanwar 			  struct pvt_device *pvt)
5849d823351SRahul Tanwar {
5859d823351SRahul Tanwar 	struct device *dev = &pdev->dev;
5869d823351SRahul Tanwar 	struct regmap **reg_map;
5879d823351SRahul Tanwar 	void __iomem *io_base;
5889d823351SRahul Tanwar 
5899d823351SRahul Tanwar 	if (!strcmp(reg_name, "common"))
5909d823351SRahul Tanwar 		reg_map = &pvt->c_map;
5919d823351SRahul Tanwar 	else if (!strcmp(reg_name, "ts"))
5929d823351SRahul Tanwar 		reg_map = &pvt->t_map;
5939d823351SRahul Tanwar 	else if (!strcmp(reg_name, "pd"))
5949d823351SRahul Tanwar 		reg_map = &pvt->p_map;
5959d823351SRahul Tanwar 	else if (!strcmp(reg_name, "vm"))
5969d823351SRahul Tanwar 		reg_map = &pvt->v_map;
5979d823351SRahul Tanwar 	else
5989d823351SRahul Tanwar 		return -EINVAL;
5999d823351SRahul Tanwar 
6009d823351SRahul Tanwar 	io_base = devm_platform_ioremap_resource_byname(pdev, reg_name);
6019d823351SRahul Tanwar 	if (IS_ERR(io_base))
6029d823351SRahul Tanwar 		return PTR_ERR(io_base);
6039d823351SRahul Tanwar 
6049d823351SRahul Tanwar 	pvt_regmap_config.name = reg_name;
6059d823351SRahul Tanwar 	*reg_map = devm_regmap_init_mmio(dev, io_base, &pvt_regmap_config);
6069d823351SRahul Tanwar 	if (IS_ERR(*reg_map)) {
6079d823351SRahul Tanwar 		dev_err(dev, "failed to init register map\n");
6089d823351SRahul Tanwar 		return PTR_ERR(*reg_map);
6099d823351SRahul Tanwar 	}
6109d823351SRahul Tanwar 
6119d823351SRahul Tanwar 	return 0;
6129d823351SRahul Tanwar }
6139d823351SRahul Tanwar 
pvt_reset_control_assert(void * data)6149d823351SRahul Tanwar static void pvt_reset_control_assert(void *data)
6159d823351SRahul Tanwar {
6169d823351SRahul Tanwar 	struct pvt_device *pvt = data;
6179d823351SRahul Tanwar 
6189d823351SRahul Tanwar 	reset_control_assert(pvt->rst);
6199d823351SRahul Tanwar }
6209d823351SRahul Tanwar 
pvt_reset_control_deassert(struct device * dev,struct pvt_device * pvt)6219d823351SRahul Tanwar static int pvt_reset_control_deassert(struct device *dev, struct pvt_device *pvt)
6229d823351SRahul Tanwar {
6239d823351SRahul Tanwar 	int ret;
6249d823351SRahul Tanwar 
6259d823351SRahul Tanwar 	ret = reset_control_deassert(pvt->rst);
6269d823351SRahul Tanwar 	if (ret)
6279d823351SRahul Tanwar 		return ret;
6289d823351SRahul Tanwar 
6299d823351SRahul Tanwar 	return devm_add_action_or_reset(dev, pvt_reset_control_assert, pvt);
6309d823351SRahul Tanwar }
6319d823351SRahul Tanwar 
pvt_get_active_channel(struct device * dev,struct pvt_device * pvt,u32 vm_num,u32 ch_num,u8 * vm_idx)632b7f5ac92SEliav Farber static int pvt_get_active_channel(struct device *dev, struct pvt_device *pvt,
633b7f5ac92SEliav Farber 				  u32 vm_num, u32 ch_num, u8 *vm_idx)
634b7f5ac92SEliav Farber {
635b7f5ac92SEliav Farber 	u8 vm_active_ch[VM_NUM_MAX];
636b7f5ac92SEliav Farber 	int ret, i, j, k;
637b7f5ac92SEliav Farber 
638b7f5ac92SEliav Farber 	ret = device_property_read_u8_array(dev, "moortec,vm-active-channels",
639b7f5ac92SEliav Farber 					    vm_active_ch, vm_num);
640b7f5ac92SEliav Farber 	if (ret) {
641b7f5ac92SEliav Farber 		/*
642b7f5ac92SEliav Farber 		 * Incase "moortec,vm-active-channels" property is not defined,
643b7f5ac92SEliav Farber 		 * we assume each VM sensor has all of its channels active.
644b7f5ac92SEliav Farber 		 */
645b7f5ac92SEliav Farber 		memset(vm_active_ch, ch_num, vm_num);
646b7f5ac92SEliav Farber 		pvt->vm_channels.max = ch_num;
647b7f5ac92SEliav Farber 		pvt->vm_channels.total = ch_num * vm_num;
648b7f5ac92SEliav Farber 	} else {
649b7f5ac92SEliav Farber 		for (i = 0; i < vm_num; i++) {
650b7f5ac92SEliav Farber 			if (vm_active_ch[i] > ch_num) {
651b7f5ac92SEliav Farber 				dev_err(dev, "invalid active channels: %u\n",
652b7f5ac92SEliav Farber 					vm_active_ch[i]);
653b7f5ac92SEliav Farber 				return -EINVAL;
654b7f5ac92SEliav Farber 			}
655b7f5ac92SEliav Farber 
656b7f5ac92SEliav Farber 			pvt->vm_channels.total += vm_active_ch[i];
657b7f5ac92SEliav Farber 
658b7f5ac92SEliav Farber 			if (vm_active_ch[i] > pvt->vm_channels.max)
659b7f5ac92SEliav Farber 				pvt->vm_channels.max = vm_active_ch[i];
660b7f5ac92SEliav Farber 		}
661b7f5ac92SEliav Farber 	}
662b7f5ac92SEliav Farber 
663b7f5ac92SEliav Farber 	/*
664b7f5ac92SEliav Farber 	 * Map between the channel-number to VM-index and channel-index.
665b7f5ac92SEliav Farber 	 * Example - 3 VMs, "moortec,vm_active_ch" = <5 2 4>:
666b7f5ac92SEliav Farber 	 * vm_map = [0 0 0 0 0 1 1 2 2 2 2]
667b7f5ac92SEliav Farber 	 * ch_map = [0 1 2 3 4 0 1 0 1 2 3]
668b7f5ac92SEliav Farber 	 */
669b7f5ac92SEliav Farber 	pvt->vd = devm_kcalloc(dev, pvt->vm_channels.total, sizeof(*pvt->vd),
670b7f5ac92SEliav Farber 			       GFP_KERNEL);
671b7f5ac92SEliav Farber 	if (!pvt->vd)
672b7f5ac92SEliav Farber 		return -ENOMEM;
673b7f5ac92SEliav Farber 
674b7f5ac92SEliav Farber 	k = 0;
675b7f5ac92SEliav Farber 	for (i = 0; i < vm_num; i++) {
676b7f5ac92SEliav Farber 		for (j = 0; j < vm_active_ch[i]; j++) {
677b7f5ac92SEliav Farber 			pvt->vd[k].vm_map = vm_idx[i];
678b7f5ac92SEliav Farber 			pvt->vd[k].ch_map = j;
679b7f5ac92SEliav Farber 			k++;
680b7f5ac92SEliav Farber 		}
681b7f5ac92SEliav Farber 	}
682b7f5ac92SEliav Farber 
683b7f5ac92SEliav Farber 	return 0;
684b7f5ac92SEliav Farber }
685b7f5ac92SEliav Farber 
pvt_get_pre_scaler(struct device * dev,struct pvt_device * pvt)686430c0d7fSEliav Farber static int pvt_get_pre_scaler(struct device *dev, struct pvt_device *pvt)
687430c0d7fSEliav Farber {
688430c0d7fSEliav Farber 	u8 *pre_scaler_ch_list;
689430c0d7fSEliav Farber 	int i, ret, num_ch;
690430c0d7fSEliav Farber 	u32 channel;
691430c0d7fSEliav Farber 
692430c0d7fSEliav Farber 	/* Set default pre-scaler value to be 1. */
693430c0d7fSEliav Farber 	for (i = 0; i < pvt->vm_channels.total; i++)
694430c0d7fSEliav Farber 		pvt->vd[i].pre_scaler = PRE_SCALER_X1;
695430c0d7fSEliav Farber 
696430c0d7fSEliav Farber 	/* Get number of channels configured in "moortec,vm-pre-scaler-x2". */
697430c0d7fSEliav Farber 	num_ch = device_property_count_u8(dev, "moortec,vm-pre-scaler-x2");
698430c0d7fSEliav Farber 	if (num_ch <= 0)
699430c0d7fSEliav Farber 		return 0;
700430c0d7fSEliav Farber 
701430c0d7fSEliav Farber 	pre_scaler_ch_list = kcalloc(num_ch, sizeof(*pre_scaler_ch_list),
702430c0d7fSEliav Farber 				     GFP_KERNEL);
703430c0d7fSEliav Farber 	if (!pre_scaler_ch_list)
704430c0d7fSEliav Farber 		return -ENOMEM;
705430c0d7fSEliav Farber 
706430c0d7fSEliav Farber 	/* Get list of all channels that have pre-scaler of 2. */
707430c0d7fSEliav Farber 	ret = device_property_read_u8_array(dev, "moortec,vm-pre-scaler-x2",
708430c0d7fSEliav Farber 					    pre_scaler_ch_list, num_ch);
709430c0d7fSEliav Farber 	if (ret)
710430c0d7fSEliav Farber 		goto out;
711430c0d7fSEliav Farber 
712430c0d7fSEliav Farber 	for (i = 0; i < num_ch; i++) {
713430c0d7fSEliav Farber 		channel = pre_scaler_ch_list[i];
714430c0d7fSEliav Farber 		pvt->vd[channel].pre_scaler = PRE_SCALER_X2;
715430c0d7fSEliav Farber 	}
716430c0d7fSEliav Farber 
717430c0d7fSEliav Farber out:
718430c0d7fSEliav Farber 	kfree(pre_scaler_ch_list);
719430c0d7fSEliav Farber 
720430c0d7fSEliav Farber 	return ret;
721430c0d7fSEliav Farber }
722430c0d7fSEliav Farber 
pvt_set_temp_coeff(struct device * dev,struct pvt_device * pvt)7233b12ca79SEliav Farber static int pvt_set_temp_coeff(struct device *dev, struct pvt_device *pvt)
7243b12ca79SEliav Farber {
7253b12ca79SEliav Farber 	struct temp_coeff *ts_coeff = &pvt->ts_coeff;
7263b12ca79SEliav Farber 	u32 series;
7273b12ca79SEliav Farber 	int ret;
7283b12ca79SEliav Farber 
7293b12ca79SEliav Farber 	/* Incase ts-series property is not defined, use default 5. */
7303b12ca79SEliav Farber 	ret = device_property_read_u32(dev, "moortec,ts-series", &series);
7313b12ca79SEliav Farber 	if (ret)
7323b12ca79SEliav Farber 		series = TEMPERATURE_SENSOR_SERIES_5;
7333b12ca79SEliav Farber 
7343b12ca79SEliav Farber 	switch (series) {
7353b12ca79SEliav Farber 	case TEMPERATURE_SENSOR_SERIES_5:
7363b12ca79SEliav Farber 		ts_coeff->h = PVT_SERIES5_H_CONST;
7373b12ca79SEliav Farber 		ts_coeff->g = PVT_SERIES5_G_CONST;
7383b12ca79SEliav Farber 		ts_coeff->j = PVT_SERIES5_J_CONST;
7393b12ca79SEliav Farber 		ts_coeff->cal5 = PVT_SERIES5_CAL5_CONST;
7403b12ca79SEliav Farber 		break;
7413b12ca79SEliav Farber 	case TEMPERATURE_SENSOR_SERIES_6:
7423b12ca79SEliav Farber 		ts_coeff->h = PVT_SERIES6_H_CONST;
7433b12ca79SEliav Farber 		ts_coeff->g = PVT_SERIES6_G_CONST;
7443b12ca79SEliav Farber 		ts_coeff->j = PVT_SERIES6_J_CONST;
7453b12ca79SEliav Farber 		ts_coeff->cal5 = PVT_SERIES6_CAL5_CONST;
7463b12ca79SEliav Farber 		break;
7473b12ca79SEliav Farber 	default:
7483b12ca79SEliav Farber 		dev_err(dev, "invalid temperature sensor series (%u)\n",
7493b12ca79SEliav Farber 			series);
7503b12ca79SEliav Farber 		return -EINVAL;
7513b12ca79SEliav Farber 	}
7523b12ca79SEliav Farber 
7533b12ca79SEliav Farber 	dev_dbg(dev, "temperature sensor series = %u\n", series);
7543b12ca79SEliav Farber 
75527937d6fSEliav Farber 	/* Override ts-coeff-h/g/j/cal5 if they are defined. */
75627937d6fSEliav Farber 	device_property_read_u32(dev, "moortec,ts-coeff-h", &ts_coeff->h);
75727937d6fSEliav Farber 	device_property_read_u32(dev, "moortec,ts-coeff-g", &ts_coeff->g);
75827937d6fSEliav Farber 	device_property_read_u32(dev, "moortec,ts-coeff-j", &ts_coeff->j);
75927937d6fSEliav Farber 	device_property_read_u32(dev, "moortec,ts-coeff-cal5", &ts_coeff->cal5);
76027937d6fSEliav Farber 
76127937d6fSEliav Farber 	dev_dbg(dev, "ts-coeff: h = %u, g = %u, j = %d, cal5 = %u\n",
76227937d6fSEliav Farber 		ts_coeff->h, ts_coeff->g, ts_coeff->j, ts_coeff->cal5);
76327937d6fSEliav Farber 
7643b12ca79SEliav Farber 	return 0;
7653b12ca79SEliav Farber }
7663b12ca79SEliav Farber 
mr75203_probe(struct platform_device * pdev)7679d823351SRahul Tanwar static int mr75203_probe(struct platform_device *pdev)
7689d823351SRahul Tanwar {
76991a9e063SEliav Farber 	u32 ts_num, vm_num, pd_num, ch_num, val, index, i;
7709d823351SRahul Tanwar 	const struct hwmon_channel_info **pvt_info;
7719d823351SRahul Tanwar 	struct device *dev = &pdev->dev;
7729d823351SRahul Tanwar 	u32 *temp_config, *in_config;
7739d823351SRahul Tanwar 	struct device *hwmon_dev;
7749d823351SRahul Tanwar 	struct pvt_device *pvt;
7759d823351SRahul Tanwar 	int ret;
7769d823351SRahul Tanwar 
7779d823351SRahul Tanwar 	pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
7789d823351SRahul Tanwar 	if (!pvt)
7799d823351SRahul Tanwar 		return -ENOMEM;
7809d823351SRahul Tanwar 
7819d823351SRahul Tanwar 	ret = pvt_get_regmap(pdev, "common", pvt);
7829d823351SRahul Tanwar 	if (ret)
7839d823351SRahul Tanwar 		return ret;
7849d823351SRahul Tanwar 
7850dee25ebSUwe Kleine-König 	pvt->clk = devm_clk_get_enabled(dev, NULL);
7869d823351SRahul Tanwar 	if (IS_ERR(pvt->clk))
7879d823351SRahul Tanwar 		return dev_err_probe(dev, PTR_ERR(pvt->clk), "failed to get clock\n");
7889d823351SRahul Tanwar 
789493372f5SEliav Farber 	pvt->rst = devm_reset_control_get_optional_exclusive(dev, NULL);
7909d823351SRahul Tanwar 	if (IS_ERR(pvt->rst))
7919d823351SRahul Tanwar 		return dev_err_probe(dev, PTR_ERR(pvt->rst),
7929d823351SRahul Tanwar 				     "failed to get reset control\n");
7939d823351SRahul Tanwar 
794493372f5SEliav Farber 	if (pvt->rst) {
7959d823351SRahul Tanwar 		ret = pvt_reset_control_deassert(dev, pvt);
7969d823351SRahul Tanwar 		if (ret)
797493372f5SEliav Farber 			return dev_err_probe(dev, ret,
798493372f5SEliav Farber 					     "cannot deassert reset control\n");
799493372f5SEliav Farber 	}
8009d823351SRahul Tanwar 
8019d823351SRahul Tanwar 	ret = regmap_read(pvt->c_map, PVT_IP_CONFIG, &val);
8029d823351SRahul Tanwar 	if (ret < 0)
8039d823351SRahul Tanwar 		return ret;
8049d823351SRahul Tanwar 
8059d823351SRahul Tanwar 	ts_num = (val & TS_NUM_MSK) >> TS_NUM_SFT;
8069d823351SRahul Tanwar 	pd_num = (val & PD_NUM_MSK) >> PD_NUM_SFT;
8079d823351SRahul Tanwar 	vm_num = (val & VM_NUM_MSK) >> VM_NUM_SFT;
80891a9e063SEliav Farber 	ch_num = (val & CH_NUM_MSK) >> CH_NUM_SFT;
8099d823351SRahul Tanwar 	pvt->t_num = ts_num;
8109d823351SRahul Tanwar 	pvt->p_num = pd_num;
8119d823351SRahul Tanwar 	pvt->v_num = vm_num;
8129d823351SRahul Tanwar 	val = 0;
8139d823351SRahul Tanwar 	if (ts_num)
8149d823351SRahul Tanwar 		val++;
8159d823351SRahul Tanwar 	if (vm_num)
8169d823351SRahul Tanwar 		val++;
8179d823351SRahul Tanwar 	if (!val)
8189d823351SRahul Tanwar 		return -ENODEV;
8199d823351SRahul Tanwar 
8209d823351SRahul Tanwar 	pvt_info = devm_kcalloc(dev, val + 2, sizeof(*pvt_info), GFP_KERNEL);
8219d823351SRahul Tanwar 	if (!pvt_info)
8229d823351SRahul Tanwar 		return -ENOMEM;
8239070d861SGuenter Roeck 	pvt_info[0] = HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ);
8249d823351SRahul Tanwar 	index = 1;
8259d823351SRahul Tanwar 
8269d823351SRahul Tanwar 	if (ts_num) {
8279d823351SRahul Tanwar 		ret = pvt_get_regmap(pdev, "ts", pvt);
8289d823351SRahul Tanwar 		if (ret)
8299d823351SRahul Tanwar 			return ret;
8309d823351SRahul Tanwar 
8313b12ca79SEliav Farber 		ret = pvt_set_temp_coeff(dev, pvt);
8323b12ca79SEliav Farber 		if (ret)
8333b12ca79SEliav Farber 			return ret;
8343b12ca79SEliav Farber 
8359d823351SRahul Tanwar 		temp_config = devm_kcalloc(dev, ts_num + 1,
8369d823351SRahul Tanwar 					   sizeof(*temp_config), GFP_KERNEL);
8379d823351SRahul Tanwar 		if (!temp_config)
8389d823351SRahul Tanwar 			return -ENOMEM;
8399d823351SRahul Tanwar 
8409d823351SRahul Tanwar 		memset32(temp_config, HWMON_T_INPUT, ts_num);
8419d823351SRahul Tanwar 		pvt_temp.config = temp_config;
8429d823351SRahul Tanwar 		pvt_info[index++] = &pvt_temp;
843a4dd0b80SEliav Farber 
844a4dd0b80SEliav Farber 		pvt_ts_dbgfs_create(pvt, dev);
8459d823351SRahul Tanwar 	}
8469d823351SRahul Tanwar 
8479d823351SRahul Tanwar 	if (pd_num) {
8489d823351SRahul Tanwar 		ret = pvt_get_regmap(pdev, "pd", pvt);
8499d823351SRahul Tanwar 		if (ret)
8509d823351SRahul Tanwar 			return ret;
8519d823351SRahul Tanwar 	}
8529d823351SRahul Tanwar 
8539d823351SRahul Tanwar 	if (vm_num) {
854b7f5ac92SEliav Farber 		u8 vm_idx[VM_NUM_MAX];
8559d823351SRahul Tanwar 
8569d823351SRahul Tanwar 		ret = pvt_get_regmap(pdev, "vm", pvt);
8579d823351SRahul Tanwar 		if (ret)
8589d823351SRahul Tanwar 			return ret;
8599d823351SRahul Tanwar 
860b7f5ac92SEliav Farber 		ret = device_property_read_u8_array(dev, "intel,vm-map", vm_idx,
861b7f5ac92SEliav Farber 						    vm_num);
8629d823351SRahul Tanwar 		if (ret) {
86381114fc3SEliav Farber 			/*
86481114fc3SEliav Farber 			 * Incase intel,vm-map property is not defined, we
86581114fc3SEliav Farber 			 * assume incremental channel numbers.
86681114fc3SEliav Farber 			 */
86781114fc3SEliav Farber 			for (i = 0; i < vm_num; i++)
868b7f5ac92SEliav Farber 				vm_idx[i] = i;
8699d823351SRahul Tanwar 		} else {
8709d823351SRahul Tanwar 			for (i = 0; i < vm_num; i++)
871b7f5ac92SEliav Farber 				if (vm_idx[i] >= vm_num || vm_idx[i] == 0xff) {
872bb9195bdSEliav Farber 					pvt->v_num = i;
873bb9195bdSEliav Farber 					vm_num = i;
8749d823351SRahul Tanwar 					break;
8759d823351SRahul Tanwar 				}
8769d823351SRahul Tanwar 		}
8779d823351SRahul Tanwar 
878b7f5ac92SEliav Farber 		ret = pvt_get_active_channel(dev, pvt, vm_num, ch_num, vm_idx);
879b7f5ac92SEliav Farber 		if (ret)
880b7f5ac92SEliav Farber 			return ret;
881b7f5ac92SEliav Farber 
882430c0d7fSEliav Farber 		ret = pvt_get_pre_scaler(dev, pvt);
883430c0d7fSEliav Farber 		if (ret)
884430c0d7fSEliav Farber 			return ret;
885430c0d7fSEliav Farber 
886b7f5ac92SEliav Farber 		in_config = devm_kcalloc(dev, pvt->vm_channels.total + 1,
8879d823351SRahul Tanwar 					 sizeof(*in_config), GFP_KERNEL);
8889d823351SRahul Tanwar 		if (!in_config)
8899d823351SRahul Tanwar 			return -ENOMEM;
8909d823351SRahul Tanwar 
891b7f5ac92SEliav Farber 		memset32(in_config, HWMON_I_INPUT, pvt->vm_channels.total);
892b7f5ac92SEliav Farber 		in_config[pvt->vm_channels.total] = 0;
8939d823351SRahul Tanwar 		pvt_in.config = in_config;
8949d823351SRahul Tanwar 
8959d823351SRahul Tanwar 		pvt_info[index++] = &pvt_in;
8969d823351SRahul Tanwar 	}
8979d823351SRahul Tanwar 
8989d823351SRahul Tanwar 	ret = pvt_init(pvt);
8999d823351SRahul Tanwar 	if (ret) {
9009d823351SRahul Tanwar 		dev_err(dev, "failed to init pvt: %d\n", ret);
9019d823351SRahul Tanwar 		return ret;
9029d823351SRahul Tanwar 	}
9039d823351SRahul Tanwar 
9049d823351SRahul Tanwar 	pvt_chip_info.info = pvt_info;
9059d823351SRahul Tanwar 	hwmon_dev = devm_hwmon_device_register_with_info(dev, "pvt",
9069d823351SRahul Tanwar 							 pvt,
9079d823351SRahul Tanwar 							 &pvt_chip_info,
9089d823351SRahul Tanwar 							 NULL);
9099d823351SRahul Tanwar 
9109d823351SRahul Tanwar 	return PTR_ERR_OR_ZERO(hwmon_dev);
9119d823351SRahul Tanwar }
9129d823351SRahul Tanwar 
9139d823351SRahul Tanwar static const struct of_device_id moortec_pvt_of_match[] = {
9149d823351SRahul Tanwar 	{ .compatible = "moortec,mr75203" },
9159d823351SRahul Tanwar 	{ }
9169d823351SRahul Tanwar };
9179d823351SRahul Tanwar MODULE_DEVICE_TABLE(of, moortec_pvt_of_match);
9189d823351SRahul Tanwar 
9199d823351SRahul Tanwar static struct platform_driver moortec_pvt_driver = {
9209d823351SRahul Tanwar 	.driver = {
9219d823351SRahul Tanwar 		.name = "moortec-pvt",
9229d823351SRahul Tanwar 		.of_match_table = moortec_pvt_of_match,
9239d823351SRahul Tanwar 	},
9249d823351SRahul Tanwar 	.probe = mr75203_probe,
9259d823351SRahul Tanwar };
9269d823351SRahul Tanwar module_platform_driver(moortec_pvt_driver);
9279d823351SRahul Tanwar 
9289d823351SRahul Tanwar MODULE_LICENSE("GPL v2");
929