1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 #include <linux/module.h>
7 #include <linux/of_address.h>
8 #include <linux/platform_device.h>
9 #include <linux/thermal.h>
12 void __iomem *mmio_base;
13 u32 (*read_mmio)(void __iomem *mmio_base);
18 static u32 thermal_mmio_readb(void __iomem *mmio_base)
20 return readb(mmio_base);
23 static int thermal_mmio_get_temperature(void *private, int *temp)
26 struct thermal_mmio *sensor =
27 (struct thermal_mmio *)private;
29 t = sensor->read_mmio(sensor->mmio_base) & sensor->mask;
37 static const struct thermal_zone_of_device_ops thermal_mmio_ops = {
38 .get_temp = thermal_mmio_get_temperature,
41 static int thermal_mmio_probe(struct platform_device *pdev)
43 struct resource *resource;
44 struct thermal_mmio *sensor;
45 int (*sensor_init_func)(struct platform_device *pdev,
46 struct thermal_mmio *sensor);
47 struct thermal_zone_device *thermal_zone;
51 sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
55 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
56 sensor->mmio_base = devm_ioremap_resource(&pdev->dev, resource);
57 if (IS_ERR(sensor->mmio_base))
58 return PTR_ERR(sensor->mmio_base);
60 sensor_init_func = device_get_match_data(&pdev->dev);
61 if (sensor_init_func) {
62 ret = sensor_init_func(pdev, sensor);
65 "failed to initialize sensor (%d)\n",
71 thermal_zone = devm_thermal_zone_of_sensor_register(&pdev->dev,
75 if (IS_ERR(thermal_zone)) {
77 "failed to register sensor (%ld)\n",
78 PTR_ERR(thermal_zone));
79 return PTR_ERR(thermal_zone);
82 thermal_mmio_get_temperature(sensor, &temperature);
84 "thermal mmio sensor %s registered, current temperature: %d\n",
85 pdev->name, temperature);
90 static int al_thermal_init(struct platform_device *pdev,
91 struct thermal_mmio *sensor)
93 sensor->read_mmio = thermal_mmio_readb;
95 sensor->factor = 1000;
100 static const struct of_device_id thermal_mmio_id_table[] = {
101 { .compatible = "amazon,al-thermal", .data = al_thermal_init},
104 MODULE_DEVICE_TABLE(of, thermal_mmio_id_table);
106 static struct platform_driver thermal_mmio_driver = {
107 .probe = thermal_mmio_probe,
109 .name = "thermal-mmio",
110 .of_match_table = of_match_ptr(thermal_mmio_id_table),
114 module_platform_driver(thermal_mmio_driver);
116 MODULE_AUTHOR("Talel Shenhar <talel@amazon.com>");
117 MODULE_DESCRIPTION("Thermal MMIO Driver");
118 MODULE_LICENSE("GPL v2");