1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright 2018-2020 NXP. 4 */ 5 6 #include <dt-bindings/firmware/imx/rsrc.h> 7 #include <linux/err.h> 8 #include <linux/firmware/imx/sci.h> 9 #include <linux/module.h> 10 #include <linux/of.h> 11 #include <linux/of_device.h> 12 #include <linux/platform_device.h> 13 #include <linux/slab.h> 14 #include <linux/thermal.h> 15 16 #include "thermal_hwmon.h" 17 18 #define IMX_SC_MISC_FUNC_GET_TEMP 13 19 20 static struct imx_sc_ipc *thermal_ipc_handle; 21 22 struct imx_sc_sensor { 23 struct thermal_zone_device *tzd; 24 u32 resource_id; 25 }; 26 27 struct req_get_temp { 28 u16 resource_id; 29 u8 type; 30 } __packed __aligned(4); 31 32 struct resp_get_temp { 33 s16 celsius; 34 s8 tenths; 35 } __packed __aligned(4); 36 37 struct imx_sc_msg_misc_get_temp { 38 struct imx_sc_rpc_msg hdr; 39 union { 40 struct req_get_temp req; 41 struct resp_get_temp resp; 42 } data; 43 } __packed __aligned(4); 44 45 static int imx_sc_thermal_get_temp(struct thermal_zone_device *tz, int *temp) 46 { 47 struct imx_sc_msg_misc_get_temp msg; 48 struct imx_sc_rpc_msg *hdr = &msg.hdr; 49 struct imx_sc_sensor *sensor = tz->devdata; 50 int ret; 51 52 msg.data.req.resource_id = sensor->resource_id; 53 msg.data.req.type = IMX_SC_C_TEMP; 54 55 hdr->ver = IMX_SC_RPC_VERSION; 56 hdr->svc = IMX_SC_RPC_SVC_MISC; 57 hdr->func = IMX_SC_MISC_FUNC_GET_TEMP; 58 hdr->size = 2; 59 60 ret = imx_scu_call_rpc(thermal_ipc_handle, &msg, true); 61 if (ret) { 62 dev_err(&sensor->tzd->device, "read temp sensor %d failed, ret %d\n", 63 sensor->resource_id, ret); 64 return ret; 65 } 66 67 *temp = msg.data.resp.celsius * 1000 + msg.data.resp.tenths * 100; 68 69 return 0; 70 } 71 72 static const struct thermal_zone_device_ops imx_sc_thermal_ops = { 73 .get_temp = imx_sc_thermal_get_temp, 74 }; 75 76 static int imx_sc_thermal_probe(struct platform_device *pdev) 77 { 78 struct imx_sc_sensor *sensor; 79 const int *resource_id; 80 int i, ret; 81 82 ret = imx_scu_get_handle(&thermal_ipc_handle); 83 if (ret) 84 return ret; 85 86 resource_id = of_device_get_match_data(&pdev->dev); 87 if (!resource_id) 88 return -EINVAL; 89 90 for (i = 0; resource_id[i] >= 0; i++) { 91 92 sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL); 93 if (!sensor) 94 return -ENOMEM; 95 96 sensor->resource_id = resource_id[i]; 97 98 sensor->tzd = devm_thermal_of_zone_register(&pdev->dev, sensor->resource_id, 99 sensor, &imx_sc_thermal_ops); 100 if (IS_ERR(sensor->tzd)) { 101 /* 102 * Save the error value before freeing the 103 * sensor pointer, otherwise we endup with a 104 * use-after-free error 105 */ 106 ret = PTR_ERR(sensor->tzd); 107 108 devm_kfree(&pdev->dev, sensor); 109 110 /* 111 * The thermal framework notifies us there is 112 * no thermal zone description for such a 113 * sensor id 114 */ 115 if (ret == -ENODEV) 116 continue; 117 118 dev_err(&pdev->dev, "failed to register thermal zone\n"); 119 return ret; 120 } 121 122 if (devm_thermal_add_hwmon_sysfs(sensor->tzd)) 123 dev_warn(&pdev->dev, "failed to add hwmon sysfs attributes\n"); 124 } 125 126 return 0; 127 } 128 129 static const int imx_sc_sensors[] = { 130 IMX_SC_R_SYSTEM, IMX_SC_R_PMIC_0, 131 IMX_SC_R_AP_0, IMX_SC_R_AP_1, 132 IMX_SC_R_GPU_0_PID0, IMX_SC_R_GPU_1_PID0, 133 IMX_SC_R_DRC_0, -1 }; 134 135 static const struct of_device_id imx_sc_thermal_table[] = { 136 { .compatible = "fsl,imx-sc-thermal", .data = imx_sc_sensors }, 137 {} 138 }; 139 MODULE_DEVICE_TABLE(of, imx_sc_thermal_table); 140 141 static struct platform_driver imx_sc_thermal_driver = { 142 .probe = imx_sc_thermal_probe, 143 .driver = { 144 .name = "imx-sc-thermal", 145 .of_match_table = imx_sc_thermal_table, 146 }, 147 }; 148 module_platform_driver(imx_sc_thermal_driver); 149 150 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>"); 151 MODULE_DESCRIPTION("Thermal driver for NXP i.MX SoCs with system controller"); 152 MODULE_LICENSE("GPL v2"); 153