5312444f105bd6aea92f35507f3a1811a980738c
[openwrt/staging/mkresin.git] / target / linux / layerscape / patches-4.14 / 825-tmu-support-layerscape.patch
1 From 2ddaec76dbe9b6e911e2a1442248ab103909cce3 Mon Sep 17 00:00:00 2001
2 From: Biwen Li <biwen.li@nxp.com>
3 Date: Wed, 17 Apr 2019 18:59:06 +0800
4 Subject: [PATCH] tmu: support layerscape
5
6 This is an integrated patch of tmu for layerscape
7
8 Signed-off-by: Biwen Li <biwen.li@nxp.com>
9 Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
10 Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
11 Signed-off-by: Yuantian Tang <andy.tang@nxp.com>
12 ---
13 drivers/thermal/qoriq_thermal.c | 102 ++++++++++++++------------------
14 1 file changed, 46 insertions(+), 56 deletions(-)
15
16 --- a/drivers/thermal/qoriq_thermal.c
17 +++ b/drivers/thermal/qoriq_thermal.c
18 @@ -69,14 +69,21 @@ struct qoriq_tmu_regs {
19 u32 ttr3cr; /* Temperature Range 3 Control Register */
20 };
21
22 +struct qoriq_tmu_data;
23 +
24 /*
25 * Thermal zone data
26 */
27 +struct qoriq_sensor {
28 + struct thermal_zone_device *tzd;
29 + struct qoriq_tmu_data *qdata;
30 + int id;
31 +};
32 +
33 struct qoriq_tmu_data {
34 - struct thermal_zone_device *tz;
35 struct qoriq_tmu_regs __iomem *regs;
36 - int sensor_id;
37 bool little_endian;
38 + struct qoriq_sensor *sensor[SITES_MAX];
39 };
40
41 static void tmu_write(struct qoriq_tmu_data *p, u32 val, void __iomem *addr)
42 @@ -97,48 +104,51 @@ static u32 tmu_read(struct qoriq_tmu_dat
43
44 static int tmu_get_temp(void *p, int *temp)
45 {
46 + struct qoriq_sensor *qsensor = p;
47 + struct qoriq_tmu_data *qdata = qsensor->qdata;
48 u32 val;
49 - struct qoriq_tmu_data *data = p;
50
51 - val = tmu_read(data, &data->regs->site[data->sensor_id].tritsr);
52 + val = tmu_read(qdata, &qdata->regs->site[qsensor->id].tritsr);
53 *temp = (val & 0xff) * 1000;
54
55 return 0;
56 }
57
58 -static int qoriq_tmu_get_sensor_id(void)
59 +static const struct thermal_zone_of_device_ops tmu_tz_ops = {
60 + .get_temp = tmu_get_temp,
61 +};
62 +
63 +static int qoriq_tmu_register_tmu_zone(struct platform_device *pdev)
64 {
65 - int ret, id;
66 - struct of_phandle_args sensor_specs;
67 - struct device_node *np, *sensor_np;
68 + struct qoriq_tmu_data *qdata = platform_get_drvdata(pdev);
69 + int id, sites = 0;
70
71 - np = of_find_node_by_name(NULL, "thermal-zones");
72 - if (!np)
73 - return -ENODEV;
74 + for (id = 0; id < SITES_MAX; id++) {
75 + qdata->sensor[id] = devm_kzalloc(&pdev->dev,
76 + sizeof(struct qoriq_sensor), GFP_KERNEL);
77 + if (!qdata->sensor[id])
78 + return -ENOMEM;
79 +
80 + qdata->sensor[id]->id = id;
81 + qdata->sensor[id]->qdata = qdata;
82 +
83 + qdata->sensor[id]->tzd = devm_thermal_zone_of_sensor_register(
84 + &pdev->dev, id, qdata->sensor[id], &tmu_tz_ops);
85 + if (IS_ERR(qdata->sensor[id]->tzd)) {
86 + if (PTR_ERR(qdata->sensor[id]->tzd) == -ENODEV)
87 + continue;
88 + else
89 + return PTR_ERR(qdata->sensor[id]->tzd);
90
91 - sensor_np = of_get_next_child(np, NULL);
92 - ret = of_parse_phandle_with_args(sensor_np, "thermal-sensors",
93 - "#thermal-sensor-cells",
94 - 0, &sensor_specs);
95 - if (ret) {
96 - of_node_put(np);
97 - of_node_put(sensor_np);
98 - return ret;
99 - }
100 -
101 - if (sensor_specs.args_count >= 1) {
102 - id = sensor_specs.args[0];
103 - WARN(sensor_specs.args_count > 1,
104 - "%s: too many cells in sensor specifier %d\n",
105 - sensor_specs.np->name, sensor_specs.args_count);
106 - } else {
107 - id = 0;
108 - }
109 + }
110
111 - of_node_put(np);
112 - of_node_put(sensor_np);
113 + sites |= 0x1 << (15 - id);
114 + }
115 + /* Enable monitoring */
116 + if (sites != 0)
117 + tmu_write(qdata, sites | TMR_ME | TMR_ALPF, &qdata->regs->tmr);
118
119 - return id;
120 + return 0;
121 }
122
123 static int qoriq_tmu_calibration(struct platform_device *pdev)
124 @@ -188,16 +198,11 @@ static void qoriq_tmu_init_device(struct
125 tmu_write(data, TMR_DISABLE, &data->regs->tmr);
126 }
127
128 -static const struct thermal_zone_of_device_ops tmu_tz_ops = {
129 - .get_temp = tmu_get_temp,
130 -};
131 -
132 static int qoriq_tmu_probe(struct platform_device *pdev)
133 {
134 int ret;
135 struct qoriq_tmu_data *data;
136 struct device_node *np = pdev->dev.of_node;
137 - u32 site = 0;
138
139 if (!np) {
140 dev_err(&pdev->dev, "Device OF-Node is NULL");
141 @@ -213,13 +218,6 @@ static int qoriq_tmu_probe(struct platfo
142
143 data->little_endian = of_property_read_bool(np, "little-endian");
144
145 - data->sensor_id = qoriq_tmu_get_sensor_id();
146 - if (data->sensor_id < 0) {
147 - dev_err(&pdev->dev, "Failed to get sensor id\n");
148 - ret = -ENODEV;
149 - goto err_iomap;
150 - }
151 -
152 data->regs = of_iomap(np, 0);
153 if (!data->regs) {
154 dev_err(&pdev->dev, "Failed to get memory region\n");
155 @@ -233,19 +231,13 @@ static int qoriq_tmu_probe(struct platfo
156 if (ret < 0)
157 goto err_tmu;
158
159 - data->tz = thermal_zone_of_sensor_register(&pdev->dev, data->sensor_id,
160 - data, &tmu_tz_ops);
161 - if (IS_ERR(data->tz)) {
162 - ret = PTR_ERR(data->tz);
163 - dev_err(&pdev->dev,
164 - "Failed to register thermal zone device %d\n", ret);
165 - goto err_tmu;
166 + ret = qoriq_tmu_register_tmu_zone(pdev);
167 + if (ret < 0) {
168 + dev_err(&pdev->dev, "Failed to register sensors\n");
169 + ret = -ENODEV;
170 + goto err_iomap;
171 }
172
173 - /* Enable monitoring */
174 - site |= 0x1 << (15 - data->sensor_id);
175 - tmu_write(data, site | TMR_ME | TMR_ALPF, &data->regs->tmr);
176 -
177 return 0;
178
179 err_tmu:
180 @@ -261,8 +253,6 @@ static int qoriq_tmu_remove(struct platf
181 {
182 struct qoriq_tmu_data *data = platform_get_drvdata(pdev);
183
184 - thermal_zone_of_sensor_unregister(&pdev->dev, data->tz);
185 -
186 /* Disable monitoring */
187 tmu_write(data, TMR_DISABLE, &data->regs->tmr);
188