1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 MaxLinear, Inc.
5 * This driver is a hardware monitoring driver for PVT controller
6 * (MR75203) which is used to configure & control Moortec embedded
7 * analog IP to enable multiple embedded temperature sensor(TS),
8 * voltage monitor(VM) & process detector(PD) modules.
10 #include <linux/bits.h>
11 #include <linux/clk.h>
12 #include <linux/hwmon.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/mutex.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/regmap.h>
19 #include <linux/reset.h>
20 #include <linux/units.h>
22 /* PVT Common register */
23 #define PVT_IP_CONFIG 0x04
24 #define TS_NUM_MSK GENMASK(4, 0)
26 #define PD_NUM_MSK GENMASK(12, 8)
28 #define VM_NUM_MSK GENMASK(20, 16)
30 #define CH_NUM_MSK GENMASK(31, 24)
33 /* Macro Common Register */
34 #define CLK_SYNTH 0x00
35 #define CLK_SYNTH_LO_SFT 0
36 #define CLK_SYNTH_HI_SFT 8
37 #define CLK_SYNTH_HOLD_SFT 16
38 #define CLK_SYNTH_EN BIT(24)
39 #define CLK_SYS_CYCLES_MAX 514
40 #define CLK_SYS_CYCLES_MIN 2
42 #define SDIF_DISABLE 0x04
44 #define SDIF_STAT 0x08
45 #define SDIF_BUSY BIT(0)
46 #define SDIF_LOCK BIT(1)
49 #define SDIF_PROG BIT(31)
50 #define SDIF_WRN_W BIT(27)
51 #define SDIF_WRN_R 0x00
52 #define SDIF_ADDR_SFT 24
54 #define SDIF_HALT 0x10
55 #define SDIF_CTRL 0x14
56 #define SDIF_SMPL_CTRL 0x20
58 /* TS & PD Individual Macro Register */
59 #define COM_REG_SIZE 0x40
61 #define SDIF_DONE(n) (COM_REG_SIZE + 0x14 + 0x40 * (n))
62 #define SDIF_SMPL_DONE BIT(0)
64 #define SDIF_DATA(n) (COM_REG_SIZE + 0x18 + 0x40 * (n))
65 #define SAMPLE_DATA_MSK GENMASK(15, 0)
67 #define HILO_RESET(n) (COM_REG_SIZE + 0x2c + 0x40 * (n))
69 /* VM Individual Macro Register */
70 #define VM_COM_REG_SIZE 0x200
71 #define VM_SDIF_DONE(n) (VM_COM_REG_SIZE + 0x34 + 0x200 * (n))
72 #define VM_SDIF_DATA(n) (VM_COM_REG_SIZE + 0x40 + 0x200 * (n))
74 /* SDA Slave Register */
76 #define IP_RST_REL BIT(1)
77 #define IP_RUN_CONT BIT(3)
78 #define IP_AUTO BIT(8)
79 #define IP_VM_MODE BIT(10)
82 #define CFG0_MODE_2 BIT(0)
83 #define CFG0_PARALLEL_OUT 0
85 #define CFG1_VOL_MEAS_MODE 0
86 #define CFG1_PARALLEL_OUT 0
92 #define VM_CH_INIT BIT(20)
93 #define VM_CH_REQ BIT(21)
96 #define POWER_DELAY_CYCLE_256 0x80
97 #define POWER_DELAY_CYCLE_64 0x40
99 #define PVT_POLL_DELAY_US 20
100 #define PVT_POLL_TIMEOUT_US 20000
101 #define PVT_H_CONST 100000
102 #define PVT_CAL5_CONST 2047
103 #define PVT_G_CONST 40000
104 #define PVT_CONV_BITS 10
105 #define PVT_N_CONST 90
106 #define PVT_R_CONST 245805
109 struct regmap *c_map;
110 struct regmap *t_map;
111 struct regmap *p_map;
112 struct regmap *v_map;
114 struct reset_control *rst;
122 static umode_t pvt_is_visible(const void *data, enum hwmon_sensor_types type,
123 u32 attr, int channel)
127 if (attr == hwmon_temp_input)
131 if (attr == hwmon_in_input)
140 static int pvt_read_temp(struct device *dev, u32 attr, int channel, long *val)
142 struct pvt_device *pvt = dev_get_drvdata(dev);
143 struct regmap *t_map = pvt->t_map;
149 case hwmon_temp_input:
150 ret = regmap_read_poll_timeout(t_map, SDIF_DONE(channel),
151 stat, stat & SDIF_SMPL_DONE,
153 PVT_POLL_TIMEOUT_US);
157 ret = regmap_read(t_map, SDIF_DATA(channel), &nbs);
161 nbs &= SAMPLE_DATA_MSK;
164 * Convert the register value to
165 * degrees centigrade temperature
167 tmp = nbs * PVT_H_CONST;
168 do_div(tmp, PVT_CAL5_CONST);
169 *val = tmp - PVT_G_CONST - pvt->ip_freq;
177 static int pvt_read_in(struct device *dev, u32 attr, int channel, long *val)
179 struct pvt_device *pvt = dev_get_drvdata(dev);
180 struct regmap *v_map = pvt->v_map;
185 if (channel >= pvt->v_num)
188 vm_idx = pvt->vm_idx[channel];
192 ret = regmap_read_poll_timeout(v_map, VM_SDIF_DONE(vm_idx),
193 stat, stat & SDIF_SMPL_DONE,
195 PVT_POLL_TIMEOUT_US);
199 ret = regmap_read(v_map, VM_SDIF_DATA(vm_idx), &n);
203 n &= SAMPLE_DATA_MSK;
204 /* Convert the N bitstream count into voltage */
205 *val = (PVT_N_CONST * n - PVT_R_CONST) >> PVT_CONV_BITS;
213 static int pvt_read(struct device *dev, enum hwmon_sensor_types type,
214 u32 attr, int channel, long *val)
218 return pvt_read_temp(dev, attr, channel, val);
220 return pvt_read_in(dev, attr, channel, val);
226 static const u32 pvt_chip_config[] = {
231 static const struct hwmon_channel_info pvt_chip = {
233 .config = pvt_chip_config,
236 static struct hwmon_channel_info pvt_temp = {
240 static struct hwmon_channel_info pvt_in = {
244 static const struct hwmon_ops pvt_hwmon_ops = {
245 .is_visible = pvt_is_visible,
249 static struct hwmon_chip_info pvt_chip_info = {
250 .ops = &pvt_hwmon_ops,
253 static int pvt_init(struct pvt_device *pvt)
255 u16 sys_freq, key, middle, low = 4, high = 8;
256 struct regmap *t_map = pvt->t_map;
257 struct regmap *p_map = pvt->p_map;
258 struct regmap *v_map = pvt->v_map;
259 u32 t_num = pvt->t_num;
260 u32 p_num = pvt->p_num;
261 u32 v_num = pvt->v_num;
265 sys_freq = clk_get_rate(pvt->clk) / HZ_PER_MHZ;
266 while (high >= low) {
267 middle = (low + high + 1) / 2;
268 key = DIV_ROUND_CLOSEST(sys_freq, middle);
269 if (key > CLK_SYS_CYCLES_MAX) {
272 } else if (key < CLK_SYS_CYCLES_MIN) {
281 * The system supports 'clk_sys' to 'clk_ip' frequency ratios
284 key = clamp_val(key, CLK_SYS_CYCLES_MIN, CLK_SYS_CYCLES_MAX) - 2;
286 clk_synth = ((key + 1) >> 1) << CLK_SYNTH_LO_SFT |
287 (key >> 1) << CLK_SYNTH_HI_SFT |
288 (key >> 1) << CLK_SYNTH_HOLD_SFT | CLK_SYNTH_EN;
290 pvt->ip_freq = sys_freq * 100 / (key + 2);
293 ret = regmap_write(t_map, SDIF_SMPL_CTRL, 0x0);
297 ret = regmap_write(t_map, SDIF_HALT, 0x0);
301 ret = regmap_write(t_map, CLK_SYNTH, clk_synth);
305 ret = regmap_write(t_map, SDIF_DISABLE, 0x0);
309 ret = regmap_read_poll_timeout(t_map, SDIF_STAT,
310 val, !(val & SDIF_BUSY),
312 PVT_POLL_TIMEOUT_US);
316 val = CFG0_MODE_2 | CFG0_PARALLEL_OUT | CFG0_12_BIT |
317 IP_CFG << SDIF_ADDR_SFT | SDIF_WRN_W | SDIF_PROG;
318 ret = regmap_write(t_map, SDIF_W, val);
322 ret = regmap_read_poll_timeout(t_map, SDIF_STAT,
323 val, !(val & SDIF_BUSY),
325 PVT_POLL_TIMEOUT_US);
329 val = POWER_DELAY_CYCLE_256 | IP_TMR << SDIF_ADDR_SFT |
330 SDIF_WRN_W | SDIF_PROG;
331 ret = regmap_write(t_map, SDIF_W, val);
335 ret = regmap_read_poll_timeout(t_map, SDIF_STAT,
336 val, !(val & SDIF_BUSY),
338 PVT_POLL_TIMEOUT_US);
342 val = IP_RST_REL | IP_RUN_CONT | IP_AUTO |
343 IP_CTRL << SDIF_ADDR_SFT |
344 SDIF_WRN_W | SDIF_PROG;
345 ret = regmap_write(t_map, SDIF_W, val);
351 ret = regmap_write(p_map, SDIF_HALT, 0x0);
355 ret = regmap_write(p_map, SDIF_DISABLE, BIT(p_num) - 1);
359 ret = regmap_write(p_map, CLK_SYNTH, clk_synth);
365 ret = regmap_write(v_map, SDIF_SMPL_CTRL, 0x0);
369 ret = regmap_write(v_map, SDIF_HALT, 0x0);
373 ret = regmap_write(v_map, CLK_SYNTH, clk_synth);
377 ret = regmap_write(v_map, SDIF_DISABLE, 0x0);
381 ret = regmap_read_poll_timeout(v_map, SDIF_STAT,
382 val, !(val & SDIF_BUSY),
384 PVT_POLL_TIMEOUT_US);
388 val = CFG1_VOL_MEAS_MODE | CFG1_PARALLEL_OUT |
389 CFG1_14_BIT | IP_CFG << SDIF_ADDR_SFT |
390 SDIF_WRN_W | SDIF_PROG;
391 ret = regmap_write(v_map, SDIF_W, val);
395 ret = regmap_read_poll_timeout(v_map, SDIF_STAT,
396 val, !(val & SDIF_BUSY),
398 PVT_POLL_TIMEOUT_US);
402 val = POWER_DELAY_CYCLE_64 | IP_TMR << SDIF_ADDR_SFT |
403 SDIF_WRN_W | SDIF_PROG;
404 ret = regmap_write(v_map, SDIF_W, val);
408 ret = regmap_read_poll_timeout(v_map, SDIF_STAT,
409 val, !(val & SDIF_BUSY),
411 PVT_POLL_TIMEOUT_US);
415 val = IP_RST_REL | IP_RUN_CONT | IP_AUTO | IP_VM_MODE |
416 IP_CTRL << SDIF_ADDR_SFT |
417 SDIF_WRN_W | SDIF_PROG;
418 ret = regmap_write(v_map, SDIF_W, val);
426 static struct regmap_config pvt_regmap_config = {
432 static int pvt_get_regmap(struct platform_device *pdev, char *reg_name,
433 struct pvt_device *pvt)
435 struct device *dev = &pdev->dev;
436 struct regmap **reg_map;
437 void __iomem *io_base;
439 if (!strcmp(reg_name, "common"))
440 reg_map = &pvt->c_map;
441 else if (!strcmp(reg_name, "ts"))
442 reg_map = &pvt->t_map;
443 else if (!strcmp(reg_name, "pd"))
444 reg_map = &pvt->p_map;
445 else if (!strcmp(reg_name, "vm"))
446 reg_map = &pvt->v_map;
450 io_base = devm_platform_ioremap_resource_byname(pdev, reg_name);
452 return PTR_ERR(io_base);
454 pvt_regmap_config.name = reg_name;
455 *reg_map = devm_regmap_init_mmio(dev, io_base, &pvt_regmap_config);
456 if (IS_ERR(*reg_map)) {
457 dev_err(dev, "failed to init register map\n");
458 return PTR_ERR(*reg_map);
464 static void pvt_clk_disable(void *data)
466 struct pvt_device *pvt = data;
468 clk_disable_unprepare(pvt->clk);
471 static int pvt_clk_enable(struct device *dev, struct pvt_device *pvt)
475 ret = clk_prepare_enable(pvt->clk);
479 return devm_add_action_or_reset(dev, pvt_clk_disable, pvt);
482 static void pvt_reset_control_assert(void *data)
484 struct pvt_device *pvt = data;
486 reset_control_assert(pvt->rst);
489 static int pvt_reset_control_deassert(struct device *dev, struct pvt_device *pvt)
493 ret = reset_control_deassert(pvt->rst);
497 return devm_add_action_or_reset(dev, pvt_reset_control_assert, pvt);
500 static int mr75203_probe(struct platform_device *pdev)
502 const struct hwmon_channel_info **pvt_info;
503 u32 ts_num, vm_num, pd_num, val, index, i;
504 struct device *dev = &pdev->dev;
505 u32 *temp_config, *in_config;
506 struct device *hwmon_dev;
507 struct pvt_device *pvt;
510 pvt = devm_kzalloc(dev, sizeof(*pvt), GFP_KERNEL);
514 ret = pvt_get_regmap(pdev, "common", pvt);
518 pvt->clk = devm_clk_get(dev, NULL);
519 if (IS_ERR(pvt->clk))
520 return dev_err_probe(dev, PTR_ERR(pvt->clk), "failed to get clock\n");
522 ret = pvt_clk_enable(dev, pvt);
524 dev_err(dev, "failed to enable clock\n");
528 pvt->rst = devm_reset_control_get_exclusive(dev, NULL);
529 if (IS_ERR(pvt->rst))
530 return dev_err_probe(dev, PTR_ERR(pvt->rst),
531 "failed to get reset control\n");
533 ret = pvt_reset_control_deassert(dev, pvt);
535 return dev_err_probe(dev, ret, "cannot deassert reset control\n");
537 ret = regmap_read(pvt->c_map, PVT_IP_CONFIG, &val);
541 ts_num = (val & TS_NUM_MSK) >> TS_NUM_SFT;
542 pd_num = (val & PD_NUM_MSK) >> PD_NUM_SFT;
543 vm_num = (val & VM_NUM_MSK) >> VM_NUM_SFT;
555 pvt_info = devm_kcalloc(dev, val + 2, sizeof(*pvt_info), GFP_KERNEL);
558 pvt_info[0] = &pvt_chip;
562 ret = pvt_get_regmap(pdev, "ts", pvt);
566 temp_config = devm_kcalloc(dev, ts_num + 1,
567 sizeof(*temp_config), GFP_KERNEL);
571 memset32(temp_config, HWMON_T_INPUT, ts_num);
572 pvt_temp.config = temp_config;
573 pvt_info[index++] = &pvt_temp;
577 ret = pvt_get_regmap(pdev, "pd", pvt);
585 ret = pvt_get_regmap(pdev, "vm", pvt);
589 pvt->vm_idx = devm_kcalloc(dev, vm_num, sizeof(*pvt->vm_idx),
594 ret = device_property_read_u8_array(dev, "intel,vm-map",
595 pvt->vm_idx, vm_num);
599 for (i = 0; i < vm_num; i++)
600 if (pvt->vm_idx[i] >= vm_num ||
601 pvt->vm_idx[i] == 0xff) {
608 * Incase intel,vm-map property is not defined, we assume
609 * incremental channel numbers.
611 for (i = num; i < vm_num; i++)
614 in_config = devm_kcalloc(dev, num + 1,
615 sizeof(*in_config), GFP_KERNEL);
619 memset32(in_config, HWMON_I_INPUT, num);
621 pvt_in.config = in_config;
623 pvt_info[index++] = &pvt_in;
628 dev_err(dev, "failed to init pvt: %d\n", ret);
632 pvt_chip_info.info = pvt_info;
633 hwmon_dev = devm_hwmon_device_register_with_info(dev, "pvt",
638 return PTR_ERR_OR_ZERO(hwmon_dev);
641 static const struct of_device_id moortec_pvt_of_match[] = {
642 { .compatible = "moortec,mr75203" },
645 MODULE_DEVICE_TABLE(of, moortec_pvt_of_match);
647 static struct platform_driver moortec_pvt_driver = {
649 .name = "moortec-pvt",
650 .of_match_table = moortec_pvt_of_match,
652 .probe = mr75203_probe,
654 module_platform_driver(moortec_pvt_driver);
656 MODULE_LICENSE("GPL v2");