thermal_mmio.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/of_address.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/thermal.h>
  9. struct thermal_mmio {
  10. void __iomem *mmio_base;
  11. u32 (*read_mmio)(void __iomem *mmio_base);
  12. u32 mask;
  13. int factor;
  14. };
  15. static u32 thermal_mmio_readb(void __iomem *mmio_base)
  16. {
  17. return readb(mmio_base);
  18. }
  19. static int thermal_mmio_get_temperature(struct thermal_zone_device *tz, int *temp)
  20. {
  21. int t;
  22. struct thermal_mmio *sensor = tz->devdata;
  23. t = sensor->read_mmio(sensor->mmio_base) & sensor->mask;
  24. t *= sensor->factor;
  25. *temp = t;
  26. return 0;
  27. }
  28. static const struct thermal_zone_device_ops thermal_mmio_ops = {
  29. .get_temp = thermal_mmio_get_temperature,
  30. };
  31. static int thermal_mmio_probe(struct platform_device *pdev)
  32. {
  33. struct resource *resource;
  34. struct thermal_mmio *sensor;
  35. int (*sensor_init_func)(struct platform_device *pdev,
  36. struct thermal_mmio *sensor);
  37. struct thermal_zone_device *thermal_zone;
  38. int ret;
  39. int temperature;
  40. sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
  41. if (!sensor)
  42. return -ENOMEM;
  43. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  44. sensor->mmio_base = devm_ioremap_resource(&pdev->dev, resource);
  45. if (IS_ERR(sensor->mmio_base))
  46. return PTR_ERR(sensor->mmio_base);
  47. sensor_init_func = device_get_match_data(&pdev->dev);
  48. if (sensor_init_func) {
  49. ret = sensor_init_func(pdev, sensor);
  50. if (ret) {
  51. dev_err(&pdev->dev,
  52. "failed to initialize sensor (%d)\n",
  53. ret);
  54. return ret;
  55. }
  56. }
  57. thermal_zone = devm_thermal_of_zone_register(&pdev->dev,
  58. 0,
  59. sensor,
  60. &thermal_mmio_ops);
  61. if (IS_ERR(thermal_zone)) {
  62. dev_err(&pdev->dev,
  63. "failed to register sensor (%ld)\n",
  64. PTR_ERR(thermal_zone));
  65. return PTR_ERR(thermal_zone);
  66. }
  67. thermal_mmio_get_temperature(thermal_zone, &temperature);
  68. dev_info(&pdev->dev,
  69. "thermal mmio sensor %s registered, current temperature: %d\n",
  70. pdev->name, temperature);
  71. return 0;
  72. }
  73. static int al_thermal_init(struct platform_device *pdev,
  74. struct thermal_mmio *sensor)
  75. {
  76. sensor->read_mmio = thermal_mmio_readb;
  77. sensor->mask = 0xff;
  78. sensor->factor = 1000;
  79. return 0;
  80. }
  81. static const struct of_device_id thermal_mmio_id_table[] = {
  82. { .compatible = "amazon,al-thermal", .data = al_thermal_init},
  83. {}
  84. };
  85. MODULE_DEVICE_TABLE(of, thermal_mmio_id_table);
  86. static struct platform_driver thermal_mmio_driver = {
  87. .probe = thermal_mmio_probe,
  88. .driver = {
  89. .name = "thermal-mmio",
  90. .of_match_table = thermal_mmio_id_table,
  91. },
  92. };
  93. module_platform_driver(thermal_mmio_driver);
  94. MODULE_AUTHOR("Talel Shenhar <[email protected]>");
  95. MODULE_DESCRIPTION("Thermal MMIO Driver");
  96. MODULE_LICENSE("GPL v2");