Linux Audio

Check our new training course

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