Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Kirkwood thermal sensor driver |
| 4 | * |
| 5 | * Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org> |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/device.h> |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/io.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/of.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/thermal.h> |
| 15 | |
| 16 | #define KIRKWOOD_THERMAL_VALID_OFFSET 9 |
| 17 | #define KIRKWOOD_THERMAL_VALID_MASK 0x1 |
| 18 | #define KIRKWOOD_THERMAL_TEMP_OFFSET 10 |
| 19 | #define KIRKWOOD_THERMAL_TEMP_MASK 0x1FF |
| 20 | |
| 21 | /* Kirkwood Thermal Sensor Dev Structure */ |
| 22 | struct kirkwood_thermal_priv { |
| 23 | void __iomem *sensor; |
| 24 | }; |
| 25 | |
| 26 | static int kirkwood_get_temp(struct thermal_zone_device *thermal, |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 27 | int *temp) |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 28 | { |
| 29 | unsigned long reg; |
| 30 | struct kirkwood_thermal_priv *priv = thermal->devdata; |
| 31 | |
| 32 | reg = readl_relaxed(priv->sensor); |
| 33 | |
| 34 | /* Valid check */ |
Ezequiel Garcia | 02519d3 | 2013-03-21 17:42:07 -0300 | [diff] [blame] | 35 | if (!((reg >> KIRKWOOD_THERMAL_VALID_OFFSET) & |
| 36 | KIRKWOOD_THERMAL_VALID_MASK)) { |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 37 | dev_err(&thermal->device, |
| 38 | "Temperature sensor reading not valid\n"); |
| 39 | return -EIO; |
| 40 | } |
| 41 | |
| 42 | /* |
Ezequiel Garcia | 696b607 | 2013-03-22 09:23:02 -0300 | [diff] [blame] | 43 | * Calculate temperature. According to Marvell internal |
| 44 | * documentation the formula for this is: |
| 45 | * Celsius = (322-reg)/1.3625 |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 46 | */ |
| 47 | reg = (reg >> KIRKWOOD_THERMAL_TEMP_OFFSET) & |
| 48 | KIRKWOOD_THERMAL_TEMP_MASK; |
Ezequiel Garcia | 696b607 | 2013-03-22 09:23:02 -0300 | [diff] [blame] | 49 | *temp = ((3220000000UL - (10000000UL * reg)) / 13625); |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static struct thermal_zone_device_ops ops = { |
| 55 | .get_temp = kirkwood_get_temp, |
| 56 | }; |
| 57 | |
| 58 | static const struct of_device_id kirkwood_thermal_id_table[] = { |
| 59 | { .compatible = "marvell,kirkwood-thermal" }, |
| 60 | {} |
| 61 | }; |
| 62 | |
| 63 | static int kirkwood_thermal_probe(struct platform_device *pdev) |
| 64 | { |
| 65 | struct thermal_zone_device *thermal = NULL; |
| 66 | struct kirkwood_thermal_priv *priv; |
| 67 | struct resource *res; |
Andrzej Pietrasiewicz | bbcf90c | 2020-06-29 14:29:22 +0200 | [diff] [blame] | 68 | int ret; |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 69 | |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 70 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 71 | if (!priv) |
| 72 | return -ENOMEM; |
| 73 | |
Wolfram Sang | c28f692 | 2013-05-10 08:17:11 +0000 | [diff] [blame] | 74 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Sachin Kamat | aa3b5d2 | 2013-03-04 06:45:34 +0000 | [diff] [blame] | 75 | priv->sensor = devm_ioremap_resource(&pdev->dev, res); |
| 76 | if (IS_ERR(priv->sensor)) |
| 77 | return PTR_ERR(priv->sensor); |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 78 | |
| 79 | thermal = thermal_zone_device_register("kirkwood_thermal", 0, 0, |
| 80 | priv, &ops, NULL, 0, 0); |
| 81 | if (IS_ERR(thermal)) { |
| 82 | dev_err(&pdev->dev, |
| 83 | "Failed to register thermal zone device\n"); |
| 84 | return PTR_ERR(thermal); |
| 85 | } |
Andrzej Pietrasiewicz | bbcf90c | 2020-06-29 14:29:22 +0200 | [diff] [blame] | 86 | ret = thermal_zone_device_enable(thermal); |
| 87 | if (ret) { |
| 88 | thermal_zone_device_unregister(thermal); |
| 89 | dev_err(&pdev->dev, "Failed to enable thermal zone device\n"); |
| 90 | return ret; |
| 91 | } |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 92 | |
| 93 | platform_set_drvdata(pdev, thermal); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int kirkwood_thermal_exit(struct platform_device *pdev) |
| 99 | { |
| 100 | struct thermal_zone_device *kirkwood_thermal = |
| 101 | platform_get_drvdata(pdev); |
| 102 | |
| 103 | thermal_zone_device_unregister(kirkwood_thermal); |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | MODULE_DEVICE_TABLE(of, kirkwood_thermal_id_table); |
| 109 | |
| 110 | static struct platform_driver kirkwood_thermal_driver = { |
| 111 | .probe = kirkwood_thermal_probe, |
| 112 | .remove = kirkwood_thermal_exit, |
| 113 | .driver = { |
| 114 | .name = "kirkwood_thermal", |
Sachin Kamat | 90c3194 | 2013-05-16 10:28:10 +0000 | [diff] [blame] | 115 | .of_match_table = kirkwood_thermal_id_table, |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 116 | }, |
| 117 | }; |
| 118 | |
| 119 | module_platform_driver(kirkwood_thermal_driver); |
| 120 | |
| 121 | MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>"); |
| 122 | MODULE_DESCRIPTION("kirkwood thermal driver"); |
| 123 | MODULE_LICENSE("GPL"); |