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(struct thermal_zone_device
*tz
, int *temp
)
26 struct thermal_mmio
*sensor
= thermal_zone_device_priv(tz
);
28 t
= sensor
->read_mmio(sensor
->mmio_base
) & sensor
->mask
;
36 static const struct thermal_zone_device_ops thermal_mmio_ops
= {
37 .get_temp
= thermal_mmio_get_temperature
,
40 static int thermal_mmio_probe(struct platform_device
*pdev
)
42 struct thermal_mmio
*sensor
;
43 int (*sensor_init_func
)(struct platform_device
*pdev
,
44 struct thermal_mmio
*sensor
);
45 struct thermal_zone_device
*thermal_zone
;
49 sensor
= devm_kzalloc(&pdev
->dev
, sizeof(*sensor
), GFP_KERNEL
);
53 sensor
->mmio_base
= devm_platform_get_and_ioremap_resource(pdev
, 0, NULL
);
54 if (IS_ERR(sensor
->mmio_base
))
55 return PTR_ERR(sensor
->mmio_base
);
57 sensor_init_func
= device_get_match_data(&pdev
->dev
);
58 if (sensor_init_func
) {
59 ret
= sensor_init_func(pdev
, sensor
);
62 "failed to initialize sensor (%d)\n",
68 thermal_zone
= devm_thermal_of_zone_register(&pdev
->dev
,
72 if (IS_ERR(thermal_zone
)) {
74 "failed to register sensor (%ld)\n",
75 PTR_ERR(thermal_zone
));
76 return PTR_ERR(thermal_zone
);
79 thermal_mmio_get_temperature(thermal_zone
, &temperature
);
81 "thermal mmio sensor %s registered, current temperature: %d\n",
82 pdev
->name
, temperature
);
87 static int al_thermal_init(struct platform_device
*pdev
,
88 struct thermal_mmio
*sensor
)
90 sensor
->read_mmio
= thermal_mmio_readb
;
92 sensor
->factor
= 1000;
97 static const struct of_device_id thermal_mmio_id_table
[] = {
98 { .compatible
= "amazon,al-thermal", .data
= al_thermal_init
},
101 MODULE_DEVICE_TABLE(of
, thermal_mmio_id_table
);
103 static struct platform_driver thermal_mmio_driver
= {
104 .probe
= thermal_mmio_probe
,
106 .name
= "thermal-mmio",
107 .of_match_table
= thermal_mmio_id_table
,
111 module_platform_driver(thermal_mmio_driver
);
113 MODULE_AUTHOR("Talel Shenhar <talel@amazon.com>");
114 MODULE_DESCRIPTION("Thermal MMIO Driver");
115 MODULE_LICENSE("GPL v2");