thermal_mmio.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 = thermal_zone_device_priv(tz);
  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 thermal_mmio *sensor;
  34. int (*sensor_init_func)(struct platform_device *pdev,
  35. struct thermal_mmio *sensor);
  36. struct thermal_zone_device *thermal_zone;
  37. int ret;
  38. int temperature;
  39. sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
  40. if (!sensor)
  41. return -ENOMEM;
  42. sensor->mmio_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  43. if (IS_ERR(sensor->mmio_base))
  44. return PTR_ERR(sensor->mmio_base);
  45. sensor_init_func = device_get_match_data(&pdev->dev);
  46. if (sensor_init_func) {
  47. ret = sensor_init_func(pdev, sensor);
  48. if (ret) {
  49. dev_err(&pdev->dev,
  50. "failed to initialize sensor (%d)\n",
  51. ret);
  52. return ret;
  53. }
  54. }
  55. thermal_zone = devm_thermal_of_zone_register(&pdev->dev,
  56. 0,
  57. sensor,
  58. &thermal_mmio_ops);
  59. if (IS_ERR(thermal_zone)) {
  60. dev_err(&pdev->dev,
  61. "failed to register sensor (%ld)\n",
  62. PTR_ERR(thermal_zone));
  63. return PTR_ERR(thermal_zone);
  64. }
  65. thermal_mmio_get_temperature(thermal_zone, &temperature);
  66. dev_info(&pdev->dev,
  67. "thermal mmio sensor %s registered, current temperature: %d\n",
  68. pdev->name, temperature);
  69. return 0;
  70. }
  71. static int al_thermal_init(struct platform_device *pdev,
  72. struct thermal_mmio *sensor)
  73. {
  74. sensor->read_mmio = thermal_mmio_readb;
  75. sensor->mask = 0xff;
  76. sensor->factor = 1000;
  77. return 0;
  78. }
  79. static const struct of_device_id thermal_mmio_id_table[] = {
  80. { .compatible = "amazon,al-thermal", .data = al_thermal_init},
  81. {}
  82. };
  83. MODULE_DEVICE_TABLE(of, thermal_mmio_id_table);
  84. static struct platform_driver thermal_mmio_driver = {
  85. .probe = thermal_mmio_probe,
  86. .driver = {
  87. .name = "thermal-mmio",
  88. .of_match_table = thermal_mmio_id_table,
  89. },
  90. };
  91. module_platform_driver(thermal_mmio_driver);
  92. MODULE_AUTHOR("Talel Shenhar <talel@amazon.com>");
  93. MODULE_DESCRIPTION("Thermal MMIO Driver");
  94. MODULE_LICENSE("GPL v2");