1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Author: zhanghongchen <zhanghongchen@loongson.cn> 4 * Yinbo Zhu <zhuyinbo@loongson.cn> 5 * Copyright (C) 2022-2023 Loongson Technology Corporation Limited 6 */ 7 8 #include <linux/interrupt.h> 9 #include <linux/io.h> 10 #include <linux/minmax.h> 11 #include <linux/module.h> 12 #include <linux/of_device.h> 13 #include <linux/platform_device.h> 14 #include <linux/thermal.h> 15 #include <linux/units.h> 16 #include "thermal_hwmon.h" 17 18 #define LOONGSON2_MAX_SENSOR_SEL_NUM 3 19 20 #define LOONGSON2_THSENS_CTRL_HI_REG 0x0 21 #define LOONGSON2_THSENS_CTRL_LOW_REG 0x8 22 #define LOONGSON2_THSENS_STATUS_REG 0x10 23 #define LOONGSON2_THSENS_OUT_REG 0x14 24 25 #define LOONGSON2_THSENS_INT_LO BIT(0) 26 #define LOONGSON2_THSENS_INT_HIGH BIT(1) 27 #define LOONGSON2_THSENS_OUT_MASK 0xFF 28 29 struct loongson2_thermal_chip_data { 30 unsigned int thermal_sensor_sel; 31 }; 32 33 struct loongson2_thermal_data { 34 void __iomem *regs; 35 const struct loongson2_thermal_chip_data *chip_data; 36 }; 37 38 static int loongson2_thermal_set(struct loongson2_thermal_data *data, 39 int low, int high, bool enable) 40 { 41 u64 reg_ctrl = 0; 42 int reg_off = data->chip_data->thermal_sensor_sel * 2; 43 44 low = clamp(-40, low, high); 45 high = clamp(125, low, high); 46 47 low += HECTO; 48 high += HECTO; 49 50 reg_ctrl = low; 51 reg_ctrl |= enable ? 0x100 : 0; 52 writew(reg_ctrl, data->regs + LOONGSON2_THSENS_CTRL_LOW_REG + reg_off); 53 54 reg_ctrl = high; 55 reg_ctrl |= enable ? 0x100 : 0; 56 writew(reg_ctrl, data->regs + LOONGSON2_THSENS_CTRL_HI_REG + reg_off); 57 58 return 0; 59 } 60 61 static int loongson2_thermal_get_temp(struct thermal_zone_device *tz, int *temp) 62 { 63 u32 reg_val; 64 struct loongson2_thermal_data *data = thermal_zone_device_priv(tz); 65 66 reg_val = readl(data->regs + LOONGSON2_THSENS_OUT_REG); 67 *temp = ((reg_val & LOONGSON2_THSENS_OUT_MASK) - HECTO) * KILO; 68 69 return 0; 70 } 71 72 static irqreturn_t loongson2_thermal_irq_thread(int irq, void *dev) 73 { 74 struct thermal_zone_device *tzd = dev; 75 struct loongson2_thermal_data *data = thermal_zone_device_priv(tzd); 76 77 writeb(LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH, data->regs + 78 LOONGSON2_THSENS_STATUS_REG); 79 80 thermal_zone_device_update(tzd, THERMAL_EVENT_UNSPECIFIED); 81 82 return IRQ_HANDLED; 83 } 84 85 static int loongson2_thermal_set_trips(struct thermal_zone_device *tz, int low, int high) 86 { 87 struct loongson2_thermal_data *data = thermal_zone_device_priv(tz); 88 89 return loongson2_thermal_set(data, low/MILLI, high/MILLI, true); 90 } 91 92 static const struct thermal_zone_device_ops loongson2_of_thermal_ops = { 93 .get_temp = loongson2_thermal_get_temp, 94 .set_trips = loongson2_thermal_set_trips, 95 }; 96 97 static int loongson2_thermal_probe(struct platform_device *pdev) 98 { 99 struct device *dev = &pdev->dev; 100 struct loongson2_thermal_data *data; 101 struct thermal_zone_device *tzd; 102 int ret, irq, i; 103 104 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 105 if (!data) 106 return -ENOMEM; 107 108 data->chip_data = device_get_match_data(dev); 109 110 data->regs = devm_platform_ioremap_resource(pdev, 0); 111 if (IS_ERR(data->regs)) 112 return PTR_ERR(data->regs); 113 114 irq = platform_get_irq(pdev, 0); 115 if (irq < 0) 116 return irq; 117 118 writeb(LOONGSON2_THSENS_INT_LO | LOONGSON2_THSENS_INT_HIGH, data->regs + 119 LOONGSON2_THSENS_STATUS_REG); 120 121 loongson2_thermal_set(data, 0, 0, false); 122 123 for (i = 0; i <= LOONGSON2_MAX_SENSOR_SEL_NUM; i++) { 124 tzd = devm_thermal_of_zone_register(dev, i, data, 125 &loongson2_of_thermal_ops); 126 127 if (!IS_ERR(tzd)) 128 break; 129 130 if (PTR_ERR(tzd) != -ENODEV) 131 continue; 132 133 return dev_err_probe(dev, PTR_ERR(tzd), "failed to register"); 134 } 135 136 ret = devm_request_threaded_irq(dev, irq, NULL, loongson2_thermal_irq_thread, 137 IRQF_ONESHOT, "loongson2_thermal", tzd); 138 if (ret < 0) 139 return dev_err_probe(dev, ret, "failed to request alarm irq\n"); 140 141 devm_thermal_add_hwmon_sysfs(dev, tzd); 142 143 return 0; 144 } 145 146 static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k1000_data = { 147 .thermal_sensor_sel = 0, 148 }; 149 150 static const struct of_device_id of_loongson2_thermal_match[] = { 151 { 152 .compatible = "loongson,ls2k1000-thermal", 153 .data = &loongson2_thermal_ls2k1000_data, 154 }, 155 { /* end */ } 156 }; 157 MODULE_DEVICE_TABLE(of, of_loongson2_thermal_match); 158 159 static struct platform_driver loongson2_thermal_driver = { 160 .driver = { 161 .name = "loongson2_thermal", 162 .of_match_table = of_loongson2_thermal_match, 163 }, 164 .probe = loongson2_thermal_probe, 165 }; 166 module_platform_driver(loongson2_thermal_driver); 167 168 MODULE_DESCRIPTION("Loongson2 thermal driver"); 169 MODULE_LICENSE("GPL"); 170