1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  */
5 
6 #include <linux/module.h>
7 #include <linux/of_address.h>
8 #include <linux/platform_device.h>
9 #include <linux/thermal.h>
10 
11 struct thermal_mmio {
12 	void __iomem *mmio_base;
13 	u32 (*read_mmio)(void __iomem *mmio_base);
14 	u32 mask;
15 	int factor;
16 };
17 
18 static u32 thermal_mmio_readb(void __iomem *mmio_base)
19 {
20 	return readb(mmio_base);
21 }
22 
23 static int thermal_mmio_get_temperature(void *private, int *temp)
24 {
25 	int t;
26 	struct thermal_mmio *sensor =
27 		(struct thermal_mmio *)private;
28 
29 	t = sensor->read_mmio(sensor->mmio_base) & sensor->mask;
30 	t *= sensor->factor;
31 
32 	*temp = t;
33 
34 	return 0;
35 }
36 
37 static struct thermal_zone_of_device_ops thermal_mmio_ops = {
38 	.get_temp = thermal_mmio_get_temperature,
39 };
40 
41 static int thermal_mmio_probe(struct platform_device *pdev)
42 {
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;
48 	int ret;
49 	int temperature;
50 
51 	sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
52 	if (!sensor)
53 		return -ENOMEM;
54 
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 		dev_err(&pdev->dev, "failed to ioremap memory (%ld)\n",
59 			PTR_ERR(sensor->mmio_base));
60 		return PTR_ERR(sensor->mmio_base);
61 	}
62 
63 	sensor_init_func = device_get_match_data(&pdev->dev);
64 	if (sensor_init_func) {
65 		ret = sensor_init_func(pdev, sensor);
66 		if (ret) {
67 			dev_err(&pdev->dev,
68 				"failed to initialize sensor (%d)\n",
69 				ret);
70 			return ret;
71 		}
72 	}
73 
74 	thermal_zone = devm_thermal_zone_of_sensor_register(&pdev->dev,
75 							    0,
76 							    sensor,
77 							    &thermal_mmio_ops);
78 	if (IS_ERR(thermal_zone)) {
79 		dev_err(&pdev->dev,
80 			"failed to register sensor (%ld)\n",
81 			PTR_ERR(thermal_zone));
82 		return PTR_ERR(thermal_zone);
83 	}
84 
85 	thermal_mmio_get_temperature(sensor, &temperature);
86 	dev_info(&pdev->dev,
87 		 "thermal mmio sensor %s registered, current temperature: %d\n",
88 		 pdev->name, temperature);
89 
90 	return 0;
91 }
92 
93 static int al_thermal_init(struct platform_device *pdev,
94 			   struct thermal_mmio *sensor)
95 {
96 	sensor->read_mmio = thermal_mmio_readb;
97 	sensor->mask = 0xff;
98 	sensor->factor = 1000;
99 
100 	return 0;
101 }
102 
103 static const struct of_device_id thermal_mmio_id_table[] = {
104 	{ .compatible = "amazon,al-thermal", .data = al_thermal_init},
105 	{}
106 };
107 MODULE_DEVICE_TABLE(of, thermal_mmio_id_table);
108 
109 static struct platform_driver thermal_mmio_driver = {
110 	.probe = thermal_mmio_probe,
111 	.driver = {
112 		.name = "thermal-mmio",
113 		.of_match_table = of_match_ptr(thermal_mmio_id_table),
114 	},
115 };
116 
117 module_platform_driver(thermal_mmio_driver);
118 
119 MODULE_AUTHOR("Talel Shenhar <talel@amazon.com>");
120 MODULE_DESCRIPTION("Thermal MMIO Driver");
121 MODULE_LICENSE("GPL v2");
122