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(struct thermal_zone_device *tz, int *temp) 24 { 25 int t; 26 struct thermal_mmio *sensor = tz->devdata; 27 28 t = sensor->read_mmio(sensor->mmio_base) & sensor->mask; 29 t *= sensor->factor; 30 31 *temp = t; 32 33 return 0; 34 } 35 36 static const struct thermal_zone_device_ops thermal_mmio_ops = { 37 .get_temp = thermal_mmio_get_temperature, 38 }; 39 40 static int thermal_mmio_probe(struct platform_device *pdev) 41 { 42 struct resource *resource; 43 struct thermal_mmio *sensor; 44 int (*sensor_init_func)(struct platform_device *pdev, 45 struct thermal_mmio *sensor); 46 struct thermal_zone_device *thermal_zone; 47 int ret; 48 int temperature; 49 50 sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL); 51 if (!sensor) 52 return -ENOMEM; 53 54 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0); 55 sensor->mmio_base = devm_ioremap_resource(&pdev->dev, resource); 56 if (IS_ERR(sensor->mmio_base)) 57 return PTR_ERR(sensor->mmio_base); 58 59 sensor_init_func = device_get_match_data(&pdev->dev); 60 if (sensor_init_func) { 61 ret = sensor_init_func(pdev, sensor); 62 if (ret) { 63 dev_err(&pdev->dev, 64 "failed to initialize sensor (%d)\n", 65 ret); 66 return ret; 67 } 68 } 69 70 thermal_zone = devm_thermal_of_zone_register(&pdev->dev, 71 0, 72 sensor, 73 &thermal_mmio_ops); 74 if (IS_ERR(thermal_zone)) { 75 dev_err(&pdev->dev, 76 "failed to register sensor (%ld)\n", 77 PTR_ERR(thermal_zone)); 78 return PTR_ERR(thermal_zone); 79 } 80 81 thermal_mmio_get_temperature(thermal_zone, &temperature); 82 dev_info(&pdev->dev, 83 "thermal mmio sensor %s registered, current temperature: %d\n", 84 pdev->name, temperature); 85 86 return 0; 87 } 88 89 static int al_thermal_init(struct platform_device *pdev, 90 struct thermal_mmio *sensor) 91 { 92 sensor->read_mmio = thermal_mmio_readb; 93 sensor->mask = 0xff; 94 sensor->factor = 1000; 95 96 return 0; 97 } 98 99 static const struct of_device_id thermal_mmio_id_table[] = { 100 { .compatible = "amazon,al-thermal", .data = al_thermal_init}, 101 {} 102 }; 103 MODULE_DEVICE_TABLE(of, thermal_mmio_id_table); 104 105 static struct platform_driver thermal_mmio_driver = { 106 .probe = thermal_mmio_probe, 107 .driver = { 108 .name = "thermal-mmio", 109 .of_match_table = thermal_mmio_id_table, 110 }, 111 }; 112 113 module_platform_driver(thermal_mmio_driver); 114 115 MODULE_AUTHOR("Talel Shenhar <talel@amazon.com>"); 116 MODULE_DESCRIPTION("Thermal MMIO Driver"); 117 MODULE_LICENSE("GPL v2"); 118