Lines Matching +full:temp +full:- +full:sensor

1 // SPDX-License-Identifier: GPL-2.0-only
3 * HiSilicon thermal sensor driver
5 * Copyright (c) 2014-2015 HiSilicon Limited.
6 * Copyright (c) 2014-2015 Linaro Limited.
40 #define HI6220_TEMP_BASE (-60000)
45 #define HI3660_TEMP_BASE (-63780)
68 int (*get_temp)(struct hisi_thermal_sensor *sensor);
69 int (*enable_sensor)(struct hisi_thermal_sensor *sensor);
70 int (*disable_sensor)(struct hisi_thermal_sensor *sensor);
71 int (*irq_handler)(struct hisi_thermal_sensor *sensor);
77 struct hisi_thermal_sensor *sensor; member
88 * Temperature base: -60°C
91 * millidegree and begins at -60 000 m°C
95 * Temp = TempBase + (steps x 785)
99 * steps = (Temp - TempBase) / 785
107 static inline int hi6220_thermal_temp_to_step(int temp) in hi6220_thermal_temp_to_step() argument
109 return DIV_ROUND_UP(temp - HI6220_TEMP_BASE, HI6220_TEMP_STEP); in hi6220_thermal_temp_to_step()
115 * Temperature base: -63.780°C
118 * millidegree and begins at -63 780 m°C
125 static inline int hi3660_thermal_temp_to_step(int temp) in hi3660_thermal_temp_to_step() argument
127 return DIV_ROUND_UP(temp - HI3660_TEMP_BASE, HI3660_TEMP_STEP); in hi3660_thermal_temp_to_step()
145 * temperature decrease to 65°C - 5°C another interrupt is raised.
177 static inline void hi6220_thermal_alarm_set(void __iomem *addr, int temp) in hi6220_thermal_alarm_set() argument
179 writel(hi6220_thermal_temp_to_step(temp) | 0x0FFFFFF00, in hi6220_thermal_alarm_set()
183 static inline void hi6220_thermal_reset_set(void __iomem *addr, int temp) in hi6220_thermal_reset_set() argument
185 writel(hi6220_thermal_temp_to_step(temp), addr + HI6220_TEMP0_RST_TH); in hi6220_thermal_reset_set()
243 * Temperature configuration register - Sensor selection
247 * 0x0: local sensor (default)
248 * 0x1: remote sensor 1 (ACPU cluster 1)
249 * 0x2: remote sensor 2 (ACPU cluster 0)
250 * 0x3: remote sensor 3 (G3D)
252 static inline void hi6220_thermal_sensor_select(void __iomem *addr, int sensor) in hi6220_thermal_sensor_select() argument
255 (sensor << 12), addr + HI6220_TEMP0_CFG); in hi6220_thermal_sensor_select()
259 * Temperature configuration register - Hdak conversion polling interval
274 static int hi6220_thermal_irq_handler(struct hisi_thermal_sensor *sensor) in hi6220_thermal_irq_handler() argument
276 struct hisi_thermal_data *data = sensor->data; in hi6220_thermal_irq_handler()
278 hi6220_thermal_alarm_clear(data->regs, 1); in hi6220_thermal_irq_handler()
282 static int hi3660_thermal_irq_handler(struct hisi_thermal_sensor *sensor) in hi3660_thermal_irq_handler() argument
284 struct hisi_thermal_data *data = sensor->data; in hi3660_thermal_irq_handler()
286 hi3660_thermal_alarm_clear(data->regs, sensor->id, 1); in hi3660_thermal_irq_handler()
290 static int hi6220_thermal_get_temp(struct hisi_thermal_sensor *sensor) in hi6220_thermal_get_temp() argument
292 struct hisi_thermal_data *data = sensor->data; in hi6220_thermal_get_temp()
294 return hi6220_thermal_get_temperature(data->regs); in hi6220_thermal_get_temp()
297 static int hi3660_thermal_get_temp(struct hisi_thermal_sensor *sensor) in hi3660_thermal_get_temp() argument
299 struct hisi_thermal_data *data = sensor->data; in hi3660_thermal_get_temp()
301 return hi3660_thermal_get_temperature(data->regs, sensor->id); in hi3660_thermal_get_temp()
304 static int hi6220_thermal_disable_sensor(struct hisi_thermal_sensor *sensor) in hi6220_thermal_disable_sensor() argument
306 struct hisi_thermal_data *data = sensor->data; in hi6220_thermal_disable_sensor()
308 /* disable sensor module */ in hi6220_thermal_disable_sensor()
309 hi6220_thermal_enable(data->regs, 0); in hi6220_thermal_disable_sensor()
310 hi6220_thermal_alarm_enable(data->regs, 0); in hi6220_thermal_disable_sensor()
311 hi6220_thermal_reset_enable(data->regs, 0); in hi6220_thermal_disable_sensor()
313 clk_disable_unprepare(data->clk); in hi6220_thermal_disable_sensor()
318 static int hi3660_thermal_disable_sensor(struct hisi_thermal_sensor *sensor) in hi3660_thermal_disable_sensor() argument
320 struct hisi_thermal_data *data = sensor->data; in hi3660_thermal_disable_sensor()
322 /* disable sensor module */ in hi3660_thermal_disable_sensor()
323 hi3660_thermal_alarm_enable(data->regs, sensor->id, 0); in hi3660_thermal_disable_sensor()
327 static int hi6220_thermal_enable_sensor(struct hisi_thermal_sensor *sensor) in hi6220_thermal_enable_sensor() argument
329 struct hisi_thermal_data *data = sensor->data; in hi6220_thermal_enable_sensor()
333 ret = clk_prepare_enable(data->clk); in hi6220_thermal_enable_sensor()
338 hi6220_thermal_reset_enable(data->regs, 0); in hi6220_thermal_enable_sensor()
339 hi6220_thermal_enable(data->regs, 0); in hi6220_thermal_enable_sensor()
341 /* select sensor id */ in hi6220_thermal_enable_sensor()
342 hi6220_thermal_sensor_select(data->regs, sensor->id); in hi6220_thermal_enable_sensor()
345 hi6220_thermal_hdak_set(data->regs, 0); in hi6220_thermal_enable_sensor()
347 /* setting lag value between current temp and the threshold */ in hi6220_thermal_enable_sensor()
348 hi6220_thermal_set_lag(data->regs, HI6220_TEMP_LAG); in hi6220_thermal_enable_sensor()
351 hi6220_thermal_alarm_set(data->regs, sensor->thres_temp); in hi6220_thermal_enable_sensor()
353 hi6220_thermal_reset_set(data->regs, HI6220_TEMP_RESET); in hi6220_thermal_enable_sensor()
356 hi6220_thermal_reset_enable(data->regs, 1); in hi6220_thermal_enable_sensor()
357 hi6220_thermal_enable(data->regs, 1); in hi6220_thermal_enable_sensor()
359 hi6220_thermal_alarm_clear(data->regs, 0); in hi6220_thermal_enable_sensor()
360 hi6220_thermal_alarm_enable(data->regs, 1); in hi6220_thermal_enable_sensor()
365 static int hi3660_thermal_enable_sensor(struct hisi_thermal_sensor *sensor) in hi3660_thermal_enable_sensor() argument
368 struct hisi_thermal_data *data = sensor->data; in hi3660_thermal_enable_sensor()
371 hi3660_thermal_alarm_enable(data->regs, sensor->id, 0); in hi3660_thermal_enable_sensor()
373 /* setting lag value between current temp and the threshold */ in hi3660_thermal_enable_sensor()
374 hi3660_thermal_set_lag(data->regs, sensor->id, HI3660_TEMP_LAG); in hi3660_thermal_enable_sensor()
377 value = hi3660_thermal_temp_to_step(sensor->thres_temp); in hi3660_thermal_enable_sensor()
378 hi3660_thermal_alarm_set(data->regs, sensor->id, value); in hi3660_thermal_enable_sensor()
381 hi3660_thermal_alarm_clear(data->regs, sensor->id, 1); in hi3660_thermal_enable_sensor()
382 hi3660_thermal_alarm_enable(data->regs, sensor->id, 1); in hi3660_thermal_enable_sensor()
389 struct platform_device *pdev = data->pdev; in hi6220_thermal_probe()
390 struct device *dev = &pdev->dev; in hi6220_thermal_probe()
392 data->clk = devm_clk_get(dev, "thermal_clk"); in hi6220_thermal_probe()
393 if (IS_ERR(data->clk)) in hi6220_thermal_probe()
394 return dev_err_probe(dev, PTR_ERR(data->clk), "failed to get thermal clk\n"); in hi6220_thermal_probe()
396 data->sensor = devm_kzalloc(dev, sizeof(*data->sensor), GFP_KERNEL); in hi6220_thermal_probe()
397 if (!data->sensor) in hi6220_thermal_probe()
398 return -ENOMEM; in hi6220_thermal_probe()
400 data->sensor[0].id = HI6220_CLUSTER0_SENSOR; in hi6220_thermal_probe()
401 data->sensor[0].irq_name = "tsensor_intr"; in hi6220_thermal_probe()
402 data->sensor[0].data = data; in hi6220_thermal_probe()
403 data->nr_sensors = 1; in hi6220_thermal_probe()
410 struct platform_device *pdev = data->pdev; in hi3660_thermal_probe()
411 struct device *dev = &pdev->dev; in hi3660_thermal_probe()
413 data->nr_sensors = 1; in hi3660_thermal_probe()
415 data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) * in hi3660_thermal_probe()
416 data->nr_sensors, GFP_KERNEL); in hi3660_thermal_probe()
417 if (!data->sensor) in hi3660_thermal_probe()
418 return -ENOMEM; in hi3660_thermal_probe()
420 data->sensor[0].id = HI3660_BIG_SENSOR; in hi3660_thermal_probe()
421 data->sensor[0].irq_name = "tsensor_a73"; in hi3660_thermal_probe()
422 data->sensor[0].data = data; in hi3660_thermal_probe()
427 static int hisi_thermal_get_temp(struct thermal_zone_device *tz, int *temp) in hisi_thermal_get_temp() argument
429 struct hisi_thermal_sensor *sensor = thermal_zone_device_priv(tz); in hisi_thermal_get_temp() local
430 struct hisi_thermal_data *data = sensor->data; in hisi_thermal_get_temp()
432 *temp = data->ops->get_temp(sensor); in hisi_thermal_get_temp()
443 struct hisi_thermal_sensor *sensor = dev; in hisi_thermal_alarm_irq_thread() local
444 struct hisi_thermal_data *data = sensor->data; in hisi_thermal_alarm_irq_thread()
445 int temp = 0; in hisi_thermal_alarm_irq_thread() local
447 data->ops->irq_handler(sensor); in hisi_thermal_alarm_irq_thread()
449 temp = data->ops->get_temp(sensor); in hisi_thermal_alarm_irq_thread()
451 if (temp >= sensor->thres_temp) { in hisi_thermal_alarm_irq_thread()
452 dev_crit(&data->pdev->dev, in hisi_thermal_alarm_irq_thread()
453 "sensor <%d> THERMAL ALARM: %d > %d\n", in hisi_thermal_alarm_irq_thread()
454 sensor->id, temp, sensor->thres_temp); in hisi_thermal_alarm_irq_thread()
456 thermal_zone_device_update(sensor->tzd, in hisi_thermal_alarm_irq_thread()
460 dev_crit(&data->pdev->dev, in hisi_thermal_alarm_irq_thread()
461 "sensor <%d> THERMAL ALARM stopped: %d < %d\n", in hisi_thermal_alarm_irq_thread()
462 sensor->id, temp, sensor->thres_temp); in hisi_thermal_alarm_irq_thread()
470 struct hisi_thermal_sensor *sensor = arg; in hisi_trip_walk_cb() local
472 if (trip->type != THERMAL_TRIP_PASSIVE) in hisi_trip_walk_cb()
475 sensor->thres_temp = trip->temperature; in hisi_trip_walk_cb()
481 struct hisi_thermal_sensor *sensor) in hisi_thermal_register_sensor() argument
485 sensor->tzd = devm_thermal_of_zone_register(&pdev->dev, in hisi_thermal_register_sensor()
486 sensor->id, sensor, in hisi_thermal_register_sensor()
488 if (IS_ERR(sensor->tzd)) { in hisi_thermal_register_sensor()
489 ret = PTR_ERR(sensor->tzd); in hisi_thermal_register_sensor()
490 sensor->tzd = NULL; in hisi_thermal_register_sensor()
491 dev_err(&pdev->dev, "failed to register sensor id %d: %d\n", in hisi_thermal_register_sensor()
492 sensor->id, ret); in hisi_thermal_register_sensor()
496 thermal_zone_for_each_trip(sensor->tzd, hisi_trip_walk_cb, sensor); in hisi_thermal_register_sensor()
523 .compatible = "hisilicon,hi3660-tsensor",
530 static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor, in hisi_thermal_toggle_sensor() argument
533 struct thermal_zone_device *tzd = sensor->tzd; in hisi_thermal_toggle_sensor()
544 struct device *dev = &pdev->dev; in hisi_thermal_probe()
549 return -ENOMEM; in hisi_thermal_probe()
551 data->pdev = pdev; in hisi_thermal_probe()
553 data->ops = of_device_get_match_data(dev); in hisi_thermal_probe()
555 data->regs = devm_platform_ioremap_resource(pdev, 0); in hisi_thermal_probe()
556 if (IS_ERR(data->regs)) in hisi_thermal_probe()
557 return PTR_ERR(data->regs); in hisi_thermal_probe()
559 ret = data->ops->probe(data); in hisi_thermal_probe()
563 for (i = 0; i < data->nr_sensors; i++) { in hisi_thermal_probe()
564 struct hisi_thermal_sensor *sensor = &data->sensor[i]; in hisi_thermal_probe() local
566 ret = hisi_thermal_register_sensor(pdev, sensor); in hisi_thermal_probe()
568 dev_err(dev, "failed to register thermal sensor: %d\n", in hisi_thermal_probe()
579 IRQF_ONESHOT, sensor->irq_name, in hisi_thermal_probe()
580 sensor); in hisi_thermal_probe()
586 ret = data->ops->enable_sensor(sensor); in hisi_thermal_probe()
588 dev_err(dev, "Failed to setup the sensor: %d\n", ret); in hisi_thermal_probe()
592 hisi_thermal_toggle_sensor(sensor, true); in hisi_thermal_probe()
603 for (i = 0; i < data->nr_sensors; i++) { in hisi_thermal_remove()
604 struct hisi_thermal_sensor *sensor = &data->sensor[i]; in hisi_thermal_remove() local
606 hisi_thermal_toggle_sensor(sensor, false); in hisi_thermal_remove()
607 data->ops->disable_sensor(sensor); in hisi_thermal_remove()
616 for (i = 0; i < data->nr_sensors; i++) in hisi_thermal_suspend()
617 data->ops->disable_sensor(&data->sensor[i]); in hisi_thermal_suspend()
627 for (i = 0; i < data->nr_sensors; i++) in hisi_thermal_resume()
628 ret |= data->ops->enable_sensor(&data->sensor[i]); in hisi_thermal_resume()