Lines Matching +full:tempmon +full:- +full:data

1 // SPDX-License-Identifier: GPL-2.0
17 #include <linux/nvmem-consumer.h>
202 struct regmap *tempmon; member
216 static void imx_set_panic_temp(struct imx_thermal_data *data, in imx_set_panic_temp() argument
219 const struct thermal_soc_data *soc_data = data->socdata; in imx_set_panic_temp()
220 struct regmap *map = data->tempmon; in imx_set_panic_temp()
223 critical_value = (data->c2 - panic_temp) / data->c1; in imx_set_panic_temp()
225 regmap_write(map, soc_data->panic_alarm_ctrl + REG_CLR, in imx_set_panic_temp()
226 soc_data->panic_alarm_mask); in imx_set_panic_temp()
227 regmap_write(map, soc_data->panic_alarm_ctrl + REG_SET, in imx_set_panic_temp()
228 critical_value << soc_data->panic_alarm_shift); in imx_set_panic_temp()
231 static void imx_set_alarm_temp(struct imx_thermal_data *data, in imx_set_alarm_temp() argument
234 struct regmap *map = data->tempmon; in imx_set_alarm_temp()
235 const struct thermal_soc_data *soc_data = data->socdata; in imx_set_alarm_temp()
238 data->alarm_temp = alarm_temp; in imx_set_alarm_temp()
240 if (data->socdata->version == TEMPMON_IMX7D) in imx_set_alarm_temp()
241 alarm_value = alarm_temp / 1000 + data->c1 - 25; in imx_set_alarm_temp()
243 alarm_value = (data->c2 - alarm_temp) / data->c1; in imx_set_alarm_temp()
245 regmap_write(map, soc_data->high_alarm_ctrl + REG_CLR, in imx_set_alarm_temp()
246 soc_data->high_alarm_mask); in imx_set_alarm_temp()
247 regmap_write(map, soc_data->high_alarm_ctrl + REG_SET, in imx_set_alarm_temp()
248 alarm_value << soc_data->high_alarm_shift); in imx_set_alarm_temp()
253 struct imx_thermal_data *data = tz->devdata; in imx_get_temp() local
254 const struct thermal_soc_data *soc_data = data->socdata; in imx_get_temp()
255 struct regmap *map = data->tempmon; in imx_get_temp()
260 ret = pm_runtime_resume_and_get(data->dev); in imx_get_temp()
264 regmap_read(map, soc_data->temp_data, &val); in imx_get_temp()
266 if ((val & soc_data->temp_valid_mask) == 0) { in imx_get_temp()
267 dev_dbg(&tz->device, "temp measurement never finished\n"); in imx_get_temp()
268 return -EAGAIN; in imx_get_temp()
271 n_meas = (val & soc_data->temp_value_mask) in imx_get_temp()
272 >> soc_data->temp_value_shift; in imx_get_temp()
275 if (data->socdata->version == TEMPMON_IMX7D) in imx_get_temp()
276 *temp = (n_meas - data->c1 + 25) * 1000; in imx_get_temp()
278 *temp = data->c2 - n_meas * data->c1; in imx_get_temp()
281 if (data->socdata->version == TEMPMON_IMX6Q) { in imx_get_temp()
282 if (data->alarm_temp == data->temp_passive && in imx_get_temp()
283 *temp >= data->temp_passive) in imx_get_temp()
284 imx_set_alarm_temp(data, data->temp_critical); in imx_get_temp()
285 if (data->alarm_temp == data->temp_critical && in imx_get_temp()
286 *temp < data->temp_passive) { in imx_get_temp()
287 imx_set_alarm_temp(data, data->temp_passive); in imx_get_temp()
288 dev_dbg(&tz->device, "thermal alarm off: T < %d\n", in imx_get_temp()
289 data->alarm_temp / 1000); in imx_get_temp()
293 if (*temp != data->last_temp) { in imx_get_temp()
294 dev_dbg(&tz->device, "millicelsius: %d\n", *temp); in imx_get_temp()
295 data->last_temp = *temp; in imx_get_temp()
299 if (!data->irq_enabled && *temp < data->alarm_temp) { in imx_get_temp()
300 data->irq_enabled = true; in imx_get_temp()
301 enable_irq(data->irq); in imx_get_temp()
304 pm_runtime_put(data->dev); in imx_get_temp()
312 struct imx_thermal_data *data = tz->devdata; in imx_change_mode() local
315 pm_runtime_get(data->dev); in imx_change_mode()
317 if (!data->irq_enabled) { in imx_change_mode()
318 data->irq_enabled = true; in imx_change_mode()
319 enable_irq(data->irq); in imx_change_mode()
322 pm_runtime_put(data->dev); in imx_change_mode()
324 if (data->irq_enabled) { in imx_change_mode()
325 disable_irq(data->irq); in imx_change_mode()
326 data->irq_enabled = false; in imx_change_mode()
343 struct imx_thermal_data *data = tz->devdata; in imx_get_crit_temp() local
345 *temp = data->temp_critical; in imx_get_crit_temp()
352 struct imx_thermal_data *data = tz->devdata; in imx_get_trip_temp() local
354 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive : in imx_get_trip_temp()
355 data->temp_critical; in imx_get_trip_temp()
362 struct imx_thermal_data *data = tz->devdata; in imx_set_trip_temp() local
365 ret = pm_runtime_resume_and_get(data->dev); in imx_set_trip_temp()
371 return -EPERM; in imx_set_trip_temp()
374 if (temp < 0 || temp > data->temp_critical) in imx_set_trip_temp()
375 return -EINVAL; in imx_set_trip_temp()
377 data->temp_passive = temp; in imx_set_trip_temp()
379 imx_set_alarm_temp(data, temp); in imx_set_trip_temp()
381 pm_runtime_put(data->dev); in imx_set_trip_temp()
396 dev_err(&tz->device, in imx_bind()
398 tz->type, cdev->type, ret); in imx_bind()
412 dev_err(&tz->device, in imx_unbind()
414 tz->type, cdev->type, ret); in imx_unbind()
434 struct imx_thermal_data *data = platform_get_drvdata(pdev); in imx_init_calib() local
439 dev_err(&pdev->dev, "invalid sensor calibration data\n"); in imx_init_calib()
440 return -EINVAL; in imx_init_calib()
444 * On i.MX7D, we only use the calibration data at 25C to get the temp, in imx_init_calib()
445 * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C. in imx_init_calib()
447 if (data->socdata->version == TEMPMON_IMX7D) { in imx_init_calib()
448 data->c1 = (ocotp_ana1 >> 9) & 0x1ff; in imx_init_calib()
459 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C in imx_init_calib()
460 * = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C in imx_init_calib()
461 * = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C in imx_init_calib()
462 * = c2 - c1 * N in imx_init_calib()
467 * c1 = 1 / (0.0015423 * N1 - 0.4297157) °C in imx_init_calib()
468 * c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C in imx_init_calib()
475 do_div(temp64, 15423 * n1 - 4148468); in imx_init_calib()
476 data->c1 = temp64; in imx_init_calib()
477 data->c2 = n1 * data->c1 + 28581; in imx_init_calib()
484 struct imx_thermal_data *data = platform_get_drvdata(pdev); in imx_init_temp_grade() local
489 data->temp_grade = "Commercial"; in imx_init_temp_grade()
490 data->temp_max = 95000; in imx_init_temp_grade()
492 case 1: /* Extended Commercial (-20 °C to 105 °C) */ in imx_init_temp_grade()
493 data->temp_grade = "Extended Commercial"; in imx_init_temp_grade()
494 data->temp_max = 105000; in imx_init_temp_grade()
496 case 2: /* Industrial (-40 °C to 105 °C) */ in imx_init_temp_grade()
497 data->temp_grade = "Industrial"; in imx_init_temp_grade()
498 data->temp_max = 105000; in imx_init_temp_grade()
500 case 3: /* Automotive (-40 °C to 125 °C) */ in imx_init_temp_grade()
501 data->temp_grade = "Automotive"; in imx_init_temp_grade()
502 data->temp_max = 125000; in imx_init_temp_grade()
510 data->temp_critical = data->temp_max - (1000 * 5); in imx_init_temp_grade()
511 data->temp_passive = data->temp_max - (1000 * 10); in imx_init_temp_grade()
520 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, in imx_init_from_tempmon_data()
521 "fsl,tempmon-data"); in imx_init_from_tempmon_data()
524 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret); in imx_init_from_tempmon_data()
530 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret); in imx_init_from_tempmon_data()
539 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret); in imx_init_from_tempmon_data()
552 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val); in imx_init_from_nvmem_cells()
560 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val); in imx_init_from_nvmem_cells()
570 struct imx_thermal_data *data = dev; in imx_thermal_alarm_irq() local
573 data->irq_enabled = false; in imx_thermal_alarm_irq()
580 struct imx_thermal_data *data = dev; in imx_thermal_alarm_irq_thread() local
582 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n", in imx_thermal_alarm_irq_thread()
583 data->alarm_temp / 1000); in imx_thermal_alarm_irq_thread()
585 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED); in imx_thermal_alarm_irq_thread()
591 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
592 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
593 { .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
600 * Create cooling device in case no #cooling-cells property is available in
603 static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data) in imx_thermal_register_legacy_cooling() argument
608 data->policy = cpufreq_cpu_get(0); in imx_thermal_register_legacy_cooling()
609 if (!data->policy) { in imx_thermal_register_legacy_cooling()
611 return -EPROBE_DEFER; in imx_thermal_register_legacy_cooling()
614 np = of_get_cpu_node(data->policy->cpu, NULL); in imx_thermal_register_legacy_cooling()
616 if (!np || !of_find_property(np, "#cooling-cells", NULL)) { in imx_thermal_register_legacy_cooling()
617 data->cdev = cpufreq_cooling_register(data->policy); in imx_thermal_register_legacy_cooling()
618 if (IS_ERR(data->cdev)) { in imx_thermal_register_legacy_cooling()
619 ret = PTR_ERR(data->cdev); in imx_thermal_register_legacy_cooling()
620 cpufreq_cpu_put(data->policy); in imx_thermal_register_legacy_cooling()
629 static void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data) in imx_thermal_unregister_legacy_cooling() argument
631 cpufreq_cooling_unregister(data->cdev); in imx_thermal_unregister_legacy_cooling()
632 cpufreq_cpu_put(data->policy); in imx_thermal_unregister_legacy_cooling()
637 static inline int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data) in imx_thermal_register_legacy_cooling() argument
642 static inline void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data) in imx_thermal_unregister_legacy_cooling() argument
649 struct imx_thermal_data *data; in imx_thermal_probe() local
654 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); in imx_thermal_probe()
655 if (!data) in imx_thermal_probe()
656 return -ENOMEM; in imx_thermal_probe()
658 data->dev = &pdev->dev; in imx_thermal_probe()
660 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon"); in imx_thermal_probe()
663 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret); in imx_thermal_probe()
666 data->tempmon = map; in imx_thermal_probe()
668 data->socdata = of_device_get_match_data(&pdev->dev); in imx_thermal_probe()
669 if (!data->socdata) { in imx_thermal_probe()
670 dev_err(&pdev->dev, "no device match found\n"); in imx_thermal_probe()
671 return -ENODEV; in imx_thermal_probe()
675 if (data->socdata->version == TEMPMON_IMX6SX) { in imx_thermal_probe()
683 regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET, in imx_thermal_probe()
684 data->socdata->low_alarm_mask); in imx_thermal_probe()
687 data->irq = platform_get_irq(pdev, 0); in imx_thermal_probe()
688 if (data->irq < 0) in imx_thermal_probe()
689 return data->irq; in imx_thermal_probe()
691 platform_set_drvdata(pdev, data); in imx_thermal_probe()
693 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) { in imx_thermal_probe()
696 return dev_err_probe(&pdev->dev, ret, in imx_thermal_probe()
701 dev_err(&pdev->dev, "failed to init from fsl,tempmon-data\n"); in imx_thermal_probe()
707 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, in imx_thermal_probe()
708 data->socdata->power_down_mask); in imx_thermal_probe()
709 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, in imx_thermal_probe()
710 data->socdata->measure_temp_mask); in imx_thermal_probe()
711 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR, in imx_thermal_probe()
712 data->socdata->measure_freq_mask); in imx_thermal_probe()
713 if (data->socdata->version != TEMPMON_IMX7D) in imx_thermal_probe()
716 regmap_write(map, data->socdata->sensor_ctrl + REG_SET, in imx_thermal_probe()
717 data->socdata->power_down_mask); in imx_thermal_probe()
719 ret = imx_thermal_register_legacy_cooling(data); in imx_thermal_probe()
721 return dev_err_probe(&pdev->dev, ret, in imx_thermal_probe()
724 data->thermal_clk = devm_clk_get(&pdev->dev, NULL); in imx_thermal_probe()
725 if (IS_ERR(data->thermal_clk)) { in imx_thermal_probe()
726 ret = PTR_ERR(data->thermal_clk); in imx_thermal_probe()
727 if (ret != -EPROBE_DEFER) in imx_thermal_probe()
728 dev_err(&pdev->dev, in imx_thermal_probe()
740 ret = clk_prepare_enable(data->thermal_clk); in imx_thermal_probe()
742 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret); in imx_thermal_probe()
746 data->tz = thermal_zone_device_register("imx_thermal_zone", in imx_thermal_probe()
748 BIT(IMX_TRIP_PASSIVE), data, in imx_thermal_probe()
752 if (IS_ERR(data->tz)) { in imx_thermal_probe()
753 ret = PTR_ERR(data->tz); in imx_thermal_probe()
754 dev_err(&pdev->dev, in imx_thermal_probe()
759 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC" in imx_thermal_probe()
760 " critical:%dC passive:%dC\n", data->temp_grade, in imx_thermal_probe()
761 data->temp_max / 1000, data->temp_critical / 1000, in imx_thermal_probe()
762 data->temp_passive / 1000); in imx_thermal_probe()
765 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR, in imx_thermal_probe()
766 data->socdata->measure_freq_mask); in imx_thermal_probe()
768 regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET, in imx_thermal_probe()
769 measure_freq << data->socdata->measure_freq_shift); in imx_thermal_probe()
770 imx_set_alarm_temp(data, data->temp_passive); in imx_thermal_probe()
772 if (data->socdata->version == TEMPMON_IMX6SX) in imx_thermal_probe()
773 imx_set_panic_temp(data, data->temp_critical); in imx_thermal_probe()
775 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, in imx_thermal_probe()
776 data->socdata->power_down_mask); in imx_thermal_probe()
777 regmap_write(map, data->socdata->sensor_ctrl + REG_SET, in imx_thermal_probe()
778 data->socdata->measure_temp_mask); in imx_thermal_probe()
783 pm_runtime_set_active(&pdev->dev); in imx_thermal_probe()
784 pm_runtime_enable(data->dev); in imx_thermal_probe()
786 ret = pm_runtime_resume_and_get(data->dev); in imx_thermal_probe()
790 data->irq_enabled = true; in imx_thermal_probe()
791 ret = thermal_zone_device_enable(data->tz); in imx_thermal_probe()
795 ret = devm_request_threaded_irq(&pdev->dev, data->irq, in imx_thermal_probe()
797 0, "imx_thermal", data); in imx_thermal_probe()
799 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret); in imx_thermal_probe()
803 pm_runtime_put(data->dev); in imx_thermal_probe()
808 thermal_zone_device_unregister(data->tz); in imx_thermal_probe()
810 pm_runtime_put_noidle(data->dev); in imx_thermal_probe()
811 pm_runtime_disable(data->dev); in imx_thermal_probe()
813 clk_disable_unprepare(data->thermal_clk); in imx_thermal_probe()
815 imx_thermal_unregister_legacy_cooling(data); in imx_thermal_probe()
822 struct imx_thermal_data *data = platform_get_drvdata(pdev); in imx_thermal_remove() local
824 pm_runtime_put_noidle(data->dev); in imx_thermal_remove()
825 pm_runtime_disable(data->dev); in imx_thermal_remove()
827 thermal_zone_device_unregister(data->tz); in imx_thermal_remove()
828 imx_thermal_unregister_legacy_cooling(data); in imx_thermal_remove()
835 struct imx_thermal_data *data = dev_get_drvdata(dev); in imx_thermal_suspend() local
845 ret = thermal_zone_device_disable(data->tz); in imx_thermal_suspend()
849 return pm_runtime_force_suspend(data->dev); in imx_thermal_suspend()
854 struct imx_thermal_data *data = dev_get_drvdata(dev); in imx_thermal_resume() local
857 ret = pm_runtime_force_resume(data->dev); in imx_thermal_resume()
861 return thermal_zone_device_enable(data->tz); in imx_thermal_resume()
866 struct imx_thermal_data *data = dev_get_drvdata(dev); in imx_thermal_runtime_suspend() local
867 const struct thermal_soc_data *socdata = data->socdata; in imx_thermal_runtime_suspend()
868 struct regmap *map = data->tempmon; in imx_thermal_runtime_suspend()
871 ret = regmap_write(map, socdata->sensor_ctrl + REG_CLR, in imx_thermal_runtime_suspend()
872 socdata->measure_temp_mask); in imx_thermal_runtime_suspend()
876 ret = regmap_write(map, socdata->sensor_ctrl + REG_SET, in imx_thermal_runtime_suspend()
877 socdata->power_down_mask); in imx_thermal_runtime_suspend()
881 clk_disable_unprepare(data->thermal_clk); in imx_thermal_runtime_suspend()
888 struct imx_thermal_data *data = dev_get_drvdata(dev); in imx_thermal_runtime_resume() local
889 const struct thermal_soc_data *socdata = data->socdata; in imx_thermal_runtime_resume()
890 struct regmap *map = data->tempmon; in imx_thermal_runtime_resume()
893 ret = clk_prepare_enable(data->thermal_clk); in imx_thermal_runtime_resume()
897 ret = regmap_write(map, socdata->sensor_ctrl + REG_CLR, in imx_thermal_runtime_resume()
898 socdata->power_down_mask); in imx_thermal_runtime_resume()
902 ret = regmap_write(map, socdata->sensor_ctrl + REG_SET, in imx_thermal_runtime_resume()
903 socdata->measure_temp_mask); in imx_thermal_runtime_resume()
936 MODULE_ALIAS("platform:imx-thermal");