12dfef650SFabio Estevam // SPDX-License-Identifier: GPL-2.0 22dfef650SFabio Estevam // 32dfef650SFabio Estevam // Copyright 2016 Freescale Semiconductor, Inc. 443528445SJia Hongtao 543528445SJia Hongtao #include <linux/module.h> 643528445SJia Hongtao #include <linux/platform_device.h> 743528445SJia Hongtao #include <linux/err.h> 843528445SJia Hongtao #include <linux/io.h> 943528445SJia Hongtao #include <linux/of.h> 1043528445SJia Hongtao #include <linux/of_address.h> 1143528445SJia Hongtao #include <linux/thermal.h> 1243528445SJia Hongtao 1343528445SJia Hongtao #include "thermal_core.h" 1443528445SJia Hongtao 1543528445SJia Hongtao #define SITES_MAX 16 1643528445SJia Hongtao 1743528445SJia Hongtao /* 1843528445SJia Hongtao * QorIQ TMU Registers 1943528445SJia Hongtao */ 2043528445SJia Hongtao struct qoriq_tmu_site_regs { 2143528445SJia Hongtao u32 tritsr; /* Immediate Temperature Site Register */ 2243528445SJia Hongtao u32 tratsr; /* Average Temperature Site Register */ 2343528445SJia Hongtao u8 res0[0x8]; 2443528445SJia Hongtao }; 2543528445SJia Hongtao 2643528445SJia Hongtao struct qoriq_tmu_regs { 2743528445SJia Hongtao u32 tmr; /* Mode Register */ 2843528445SJia Hongtao #define TMR_DISABLE 0x0 2943528445SJia Hongtao #define TMR_ME 0x80000000 3043528445SJia Hongtao #define TMR_ALPF 0x0c000000 3143528445SJia Hongtao u32 tsr; /* Status Register */ 3243528445SJia Hongtao u32 tmtmir; /* Temperature measurement interval Register */ 3343528445SJia Hongtao #define TMTMIR_DEFAULT 0x0000000f 3443528445SJia Hongtao u8 res0[0x14]; 3543528445SJia Hongtao u32 tier; /* Interrupt Enable Register */ 3643528445SJia Hongtao #define TIER_DISABLE 0x0 3743528445SJia Hongtao u32 tidr; /* Interrupt Detect Register */ 3843528445SJia Hongtao u32 tiscr; /* Interrupt Site Capture Register */ 3943528445SJia Hongtao u32 ticscr; /* Interrupt Critical Site Capture Register */ 4043528445SJia Hongtao u8 res1[0x10]; 4143528445SJia Hongtao u32 tmhtcrh; /* High Temperature Capture Register */ 4243528445SJia Hongtao u32 tmhtcrl; /* Low Temperature Capture Register */ 4343528445SJia Hongtao u8 res2[0x8]; 4443528445SJia Hongtao u32 tmhtitr; /* High Temperature Immediate Threshold */ 4543528445SJia Hongtao u32 tmhtatr; /* High Temperature Average Threshold */ 4643528445SJia Hongtao u32 tmhtactr; /* High Temperature Average Crit Threshold */ 4743528445SJia Hongtao u8 res3[0x24]; 4843528445SJia Hongtao u32 ttcfgr; /* Temperature Configuration Register */ 4943528445SJia Hongtao u32 tscfgr; /* Sensor Configuration Register */ 5043528445SJia Hongtao u8 res4[0x78]; 5143528445SJia Hongtao struct qoriq_tmu_site_regs site[SITES_MAX]; 5243528445SJia Hongtao u8 res5[0x9f8]; 5343528445SJia Hongtao u32 ipbrr0; /* IP Block Revision Register 0 */ 5443528445SJia Hongtao u32 ipbrr1; /* IP Block Revision Register 1 */ 5543528445SJia Hongtao u8 res6[0x310]; 5643528445SJia Hongtao u32 ttr0cr; /* Temperature Range 0 Control Register */ 5743528445SJia Hongtao u32 ttr1cr; /* Temperature Range 1 Control Register */ 5843528445SJia Hongtao u32 ttr2cr; /* Temperature Range 2 Control Register */ 5943528445SJia Hongtao u32 ttr3cr; /* Temperature Range 3 Control Register */ 6043528445SJia Hongtao }; 6143528445SJia Hongtao 6243528445SJia Hongtao /* 6343528445SJia Hongtao * Thermal zone data 6443528445SJia Hongtao */ 6543528445SJia Hongtao struct qoriq_tmu_data { 6643528445SJia Hongtao struct thermal_zone_device *tz; 6743528445SJia Hongtao struct qoriq_tmu_regs __iomem *regs; 6843528445SJia Hongtao int sensor_id; 6943528445SJia Hongtao bool little_endian; 7043528445SJia Hongtao }; 7143528445SJia Hongtao 7243528445SJia Hongtao static void tmu_write(struct qoriq_tmu_data *p, u32 val, void __iomem *addr) 7343528445SJia Hongtao { 7443528445SJia Hongtao if (p->little_endian) 7543528445SJia Hongtao iowrite32(val, addr); 7643528445SJia Hongtao else 7743528445SJia Hongtao iowrite32be(val, addr); 7843528445SJia Hongtao } 7943528445SJia Hongtao 8043528445SJia Hongtao static u32 tmu_read(struct qoriq_tmu_data *p, void __iomem *addr) 8143528445SJia Hongtao { 8243528445SJia Hongtao if (p->little_endian) 8343528445SJia Hongtao return ioread32(addr); 8443528445SJia Hongtao else 8543528445SJia Hongtao return ioread32be(addr); 8643528445SJia Hongtao } 8743528445SJia Hongtao 8843528445SJia Hongtao static int tmu_get_temp(void *p, int *temp) 8943528445SJia Hongtao { 9043528445SJia Hongtao u32 val; 9143528445SJia Hongtao struct qoriq_tmu_data *data = p; 9243528445SJia Hongtao 9343528445SJia Hongtao val = tmu_read(data, &data->regs->site[data->sensor_id].tritsr); 9443528445SJia Hongtao *temp = (val & 0xff) * 1000; 9543528445SJia Hongtao 9643528445SJia Hongtao return 0; 9743528445SJia Hongtao } 9843528445SJia Hongtao 9943528445SJia Hongtao static int qoriq_tmu_get_sensor_id(void) 10043528445SJia Hongtao { 10143528445SJia Hongtao int ret, id; 10243528445SJia Hongtao struct of_phandle_args sensor_specs; 10343528445SJia Hongtao struct device_node *np, *sensor_np; 10443528445SJia Hongtao 10543528445SJia Hongtao np = of_find_node_by_name(NULL, "thermal-zones"); 10643528445SJia Hongtao if (!np) 10743528445SJia Hongtao return -ENODEV; 10843528445SJia Hongtao 10943528445SJia Hongtao sensor_np = of_get_next_child(np, NULL); 11043528445SJia Hongtao ret = of_parse_phandle_with_args(sensor_np, "thermal-sensors", 11143528445SJia Hongtao "#thermal-sensor-cells", 11243528445SJia Hongtao 0, &sensor_specs); 11343528445SJia Hongtao if (ret) { 11443528445SJia Hongtao of_node_put(np); 11543528445SJia Hongtao of_node_put(sensor_np); 11643528445SJia Hongtao return ret; 11743528445SJia Hongtao } 11843528445SJia Hongtao 11943528445SJia Hongtao if (sensor_specs.args_count >= 1) { 12043528445SJia Hongtao id = sensor_specs.args[0]; 12143528445SJia Hongtao WARN(sensor_specs.args_count > 1, 12243528445SJia Hongtao "%s: too many cells in sensor specifier %d\n", 12343528445SJia Hongtao sensor_specs.np->name, sensor_specs.args_count); 12443528445SJia Hongtao } else { 12543528445SJia Hongtao id = 0; 12643528445SJia Hongtao } 12743528445SJia Hongtao 12843528445SJia Hongtao of_node_put(np); 12943528445SJia Hongtao of_node_put(sensor_np); 13043528445SJia Hongtao 13143528445SJia Hongtao return id; 13243528445SJia Hongtao } 13343528445SJia Hongtao 13443528445SJia Hongtao static int qoriq_tmu_calibration(struct platform_device *pdev) 13543528445SJia Hongtao { 13643528445SJia Hongtao int i, val, len; 13743528445SJia Hongtao u32 range[4]; 13843528445SJia Hongtao const u32 *calibration; 13943528445SJia Hongtao struct device_node *np = pdev->dev.of_node; 14043528445SJia Hongtao struct qoriq_tmu_data *data = platform_get_drvdata(pdev); 14143528445SJia Hongtao 14243528445SJia Hongtao if (of_property_read_u32_array(np, "fsl,tmu-range", range, 4)) { 14343528445SJia Hongtao dev_err(&pdev->dev, "missing calibration range.\n"); 14443528445SJia Hongtao return -ENODEV; 14543528445SJia Hongtao } 14643528445SJia Hongtao 14743528445SJia Hongtao /* Init temperature range registers */ 14843528445SJia Hongtao tmu_write(data, range[0], &data->regs->ttr0cr); 14943528445SJia Hongtao tmu_write(data, range[1], &data->regs->ttr1cr); 15043528445SJia Hongtao tmu_write(data, range[2], &data->regs->ttr2cr); 15143528445SJia Hongtao tmu_write(data, range[3], &data->regs->ttr3cr); 15243528445SJia Hongtao 15343528445SJia Hongtao calibration = of_get_property(np, "fsl,tmu-calibration", &len); 15443528445SJia Hongtao if (calibration == NULL || len % 8) { 15543528445SJia Hongtao dev_err(&pdev->dev, "invalid calibration data.\n"); 15643528445SJia Hongtao return -ENODEV; 15743528445SJia Hongtao } 15843528445SJia Hongtao 15943528445SJia Hongtao for (i = 0; i < len; i += 8, calibration += 2) { 16043528445SJia Hongtao val = of_read_number(calibration, 1); 16143528445SJia Hongtao tmu_write(data, val, &data->regs->ttcfgr); 16243528445SJia Hongtao val = of_read_number(calibration + 1, 1); 16343528445SJia Hongtao tmu_write(data, val, &data->regs->tscfgr); 16443528445SJia Hongtao } 16543528445SJia Hongtao 16643528445SJia Hongtao return 0; 16743528445SJia Hongtao } 16843528445SJia Hongtao 16943528445SJia Hongtao static void qoriq_tmu_init_device(struct qoriq_tmu_data *data) 17043528445SJia Hongtao { 17143528445SJia Hongtao /* Disable interrupt, using polling instead */ 17243528445SJia Hongtao tmu_write(data, TIER_DISABLE, &data->regs->tier); 17343528445SJia Hongtao 17443528445SJia Hongtao /* Set update_interval */ 17543528445SJia Hongtao tmu_write(data, TMTMIR_DEFAULT, &data->regs->tmtmir); 17643528445SJia Hongtao 17743528445SJia Hongtao /* Disable monitoring */ 17843528445SJia Hongtao tmu_write(data, TMR_DISABLE, &data->regs->tmr); 17943528445SJia Hongtao } 18043528445SJia Hongtao 181c30d5d59SJulia Lawall static const struct thermal_zone_of_device_ops tmu_tz_ops = { 18243528445SJia Hongtao .get_temp = tmu_get_temp, 18343528445SJia Hongtao }; 18443528445SJia Hongtao 18543528445SJia Hongtao static int qoriq_tmu_probe(struct platform_device *pdev) 18643528445SJia Hongtao { 18743528445SJia Hongtao int ret; 18843528445SJia Hongtao struct qoriq_tmu_data *data; 18943528445SJia Hongtao struct device_node *np = pdev->dev.of_node; 1901a893a5aSFabio Estevam u32 site; 19143528445SJia Hongtao 19243528445SJia Hongtao if (!np) { 19343528445SJia Hongtao dev_err(&pdev->dev, "Device OF-Node is NULL"); 19443528445SJia Hongtao return -ENODEV; 19543528445SJia Hongtao } 19643528445SJia Hongtao 19743528445SJia Hongtao data = devm_kzalloc(&pdev->dev, sizeof(struct qoriq_tmu_data), 19843528445SJia Hongtao GFP_KERNEL); 19943528445SJia Hongtao if (!data) 20043528445SJia Hongtao return -ENOMEM; 20143528445SJia Hongtao 20243528445SJia Hongtao platform_set_drvdata(pdev, data); 20343528445SJia Hongtao 20443528445SJia Hongtao data->little_endian = of_property_read_bool(np, "little-endian"); 20543528445SJia Hongtao 20643528445SJia Hongtao data->sensor_id = qoriq_tmu_get_sensor_id(); 20743528445SJia Hongtao if (data->sensor_id < 0) { 20843528445SJia Hongtao dev_err(&pdev->dev, "Failed to get sensor id\n"); 20943528445SJia Hongtao ret = -ENODEV; 21043528445SJia Hongtao goto err_iomap; 21143528445SJia Hongtao } 21243528445SJia Hongtao 21343528445SJia Hongtao data->regs = of_iomap(np, 0); 21443528445SJia Hongtao if (!data->regs) { 21543528445SJia Hongtao dev_err(&pdev->dev, "Failed to get memory region\n"); 21643528445SJia Hongtao ret = -ENODEV; 21743528445SJia Hongtao goto err_iomap; 21843528445SJia Hongtao } 21943528445SJia Hongtao 22043528445SJia Hongtao qoriq_tmu_init_device(data); /* TMU initialization */ 22143528445SJia Hongtao 22243528445SJia Hongtao ret = qoriq_tmu_calibration(pdev); /* TMU calibration */ 22343528445SJia Hongtao if (ret < 0) 22443528445SJia Hongtao goto err_tmu; 22543528445SJia Hongtao 226f1506a69SFabio Estevam data->tz = devm_thermal_zone_of_sensor_register(&pdev->dev, 227f1506a69SFabio Estevam data->sensor_id, 22843528445SJia Hongtao data, &tmu_tz_ops); 22943528445SJia Hongtao if (IS_ERR(data->tz)) { 23043528445SJia Hongtao ret = PTR_ERR(data->tz); 23143528445SJia Hongtao dev_err(&pdev->dev, 23243528445SJia Hongtao "Failed to register thermal zone device %d\n", ret); 23343528445SJia Hongtao goto err_tmu; 23443528445SJia Hongtao } 23543528445SJia Hongtao 23643528445SJia Hongtao /* Enable monitoring */ 2371a893a5aSFabio Estevam site = 0x1 << (15 - data->sensor_id); 23843528445SJia Hongtao tmu_write(data, site | TMR_ME | TMR_ALPF, &data->regs->tmr); 23943528445SJia Hongtao 24043528445SJia Hongtao return 0; 24143528445SJia Hongtao 24243528445SJia Hongtao err_tmu: 24343528445SJia Hongtao iounmap(data->regs); 24443528445SJia Hongtao 24543528445SJia Hongtao err_iomap: 24643528445SJia Hongtao platform_set_drvdata(pdev, NULL); 24743528445SJia Hongtao 24843528445SJia Hongtao return ret; 24943528445SJia Hongtao } 25043528445SJia Hongtao 25143528445SJia Hongtao static int qoriq_tmu_remove(struct platform_device *pdev) 25243528445SJia Hongtao { 25343528445SJia Hongtao struct qoriq_tmu_data *data = platform_get_drvdata(pdev); 25443528445SJia Hongtao 25543528445SJia Hongtao /* Disable monitoring */ 25643528445SJia Hongtao tmu_write(data, TMR_DISABLE, &data->regs->tmr); 25743528445SJia Hongtao 25843528445SJia Hongtao iounmap(data->regs); 25943528445SJia Hongtao platform_set_drvdata(pdev, NULL); 26043528445SJia Hongtao 26143528445SJia Hongtao return 0; 26243528445SJia Hongtao } 26343528445SJia Hongtao 26443528445SJia Hongtao #ifdef CONFIG_PM_SLEEP 26543528445SJia Hongtao static int qoriq_tmu_suspend(struct device *dev) 26643528445SJia Hongtao { 26743528445SJia Hongtao u32 tmr; 26843528445SJia Hongtao struct qoriq_tmu_data *data = dev_get_drvdata(dev); 26943528445SJia Hongtao 27043528445SJia Hongtao /* Disable monitoring */ 27143528445SJia Hongtao tmr = tmu_read(data, &data->regs->tmr); 27243528445SJia Hongtao tmr &= ~TMR_ME; 27343528445SJia Hongtao tmu_write(data, tmr, &data->regs->tmr); 27443528445SJia Hongtao 27543528445SJia Hongtao return 0; 27643528445SJia Hongtao } 27743528445SJia Hongtao 27843528445SJia Hongtao static int qoriq_tmu_resume(struct device *dev) 27943528445SJia Hongtao { 28043528445SJia Hongtao u32 tmr; 28143528445SJia Hongtao struct qoriq_tmu_data *data = dev_get_drvdata(dev); 28243528445SJia Hongtao 28343528445SJia Hongtao /* Enable monitoring */ 28443528445SJia Hongtao tmr = tmu_read(data, &data->regs->tmr); 28543528445SJia Hongtao tmr |= TMR_ME; 28643528445SJia Hongtao tmu_write(data, tmr, &data->regs->tmr); 28743528445SJia Hongtao 28843528445SJia Hongtao return 0; 28943528445SJia Hongtao } 29043528445SJia Hongtao #endif 29143528445SJia Hongtao 29243528445SJia Hongtao static SIMPLE_DEV_PM_OPS(qoriq_tmu_pm_ops, 29343528445SJia Hongtao qoriq_tmu_suspend, qoriq_tmu_resume); 29443528445SJia Hongtao 29543528445SJia Hongtao static const struct of_device_id qoriq_tmu_match[] = { 29643528445SJia Hongtao { .compatible = "fsl,qoriq-tmu", }, 29743528445SJia Hongtao {}, 29843528445SJia Hongtao }; 29943528445SJia Hongtao MODULE_DEVICE_TABLE(of, qoriq_tmu_match); 30043528445SJia Hongtao 30143528445SJia Hongtao static struct platform_driver qoriq_tmu = { 30243528445SJia Hongtao .driver = { 30343528445SJia Hongtao .name = "qoriq_thermal", 30443528445SJia Hongtao .pm = &qoriq_tmu_pm_ops, 30543528445SJia Hongtao .of_match_table = qoriq_tmu_match, 30643528445SJia Hongtao }, 30743528445SJia Hongtao .probe = qoriq_tmu_probe, 30843528445SJia Hongtao .remove = qoriq_tmu_remove, 30943528445SJia Hongtao }; 31043528445SJia Hongtao module_platform_driver(qoriq_tmu); 31143528445SJia Hongtao 31243528445SJia Hongtao MODULE_AUTHOR("Jia Hongtao <hongtao.jia@nxp.com>"); 31343528445SJia Hongtao MODULE_DESCRIPTION("QorIQ Thermal Monitoring Unit driver"); 31443528445SJia Hongtao MODULE_LICENSE("GPL v2"); 315