Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Kirkwood thermal sensor driver |
| 3 | * |
| 4 | * Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org> |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/thermal.h> |
| 24 | |
| 25 | #define KIRKWOOD_THERMAL_VALID_OFFSET 9 |
| 26 | #define KIRKWOOD_THERMAL_VALID_MASK 0x1 |
| 27 | #define KIRKWOOD_THERMAL_TEMP_OFFSET 10 |
| 28 | #define KIRKWOOD_THERMAL_TEMP_MASK 0x1FF |
| 29 | |
| 30 | /* Kirkwood Thermal Sensor Dev Structure */ |
| 31 | struct kirkwood_thermal_priv { |
| 32 | void __iomem *sensor; |
| 33 | }; |
| 34 | |
| 35 | static int kirkwood_get_temp(struct thermal_zone_device *thermal, |
| 36 | unsigned long *temp) |
| 37 | { |
| 38 | unsigned long reg; |
| 39 | struct kirkwood_thermal_priv *priv = thermal->devdata; |
| 40 | |
| 41 | reg = readl_relaxed(priv->sensor); |
| 42 | |
| 43 | /* Valid check */ |
Ezequiel Garcia | 02519d3 | 2013-03-21 17:42:07 -0300 | [diff] [blame] | 44 | if (!((reg >> KIRKWOOD_THERMAL_VALID_OFFSET) & |
| 45 | KIRKWOOD_THERMAL_VALID_MASK)) { |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 46 | dev_err(&thermal->device, |
| 47 | "Temperature sensor reading not valid\n"); |
| 48 | return -EIO; |
| 49 | } |
| 50 | |
| 51 | /* |
Ezequiel Garcia | 696b607 | 2013-03-22 09:23:02 -0300 | [diff] [blame] | 52 | * Calculate temperature. According to Marvell internal |
| 53 | * documentation the formula for this is: |
| 54 | * Celsius = (322-reg)/1.3625 |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 55 | */ |
| 56 | reg = (reg >> KIRKWOOD_THERMAL_TEMP_OFFSET) & |
| 57 | KIRKWOOD_THERMAL_TEMP_MASK; |
Ezequiel Garcia | 696b607 | 2013-03-22 09:23:02 -0300 | [diff] [blame] | 58 | *temp = ((3220000000UL - (10000000UL * reg)) / 13625); |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static struct thermal_zone_device_ops ops = { |
| 64 | .get_temp = kirkwood_get_temp, |
| 65 | }; |
| 66 | |
| 67 | static const struct of_device_id kirkwood_thermal_id_table[] = { |
| 68 | { .compatible = "marvell,kirkwood-thermal" }, |
| 69 | {} |
| 70 | }; |
| 71 | |
| 72 | static int kirkwood_thermal_probe(struct platform_device *pdev) |
| 73 | { |
| 74 | struct thermal_zone_device *thermal = NULL; |
| 75 | struct kirkwood_thermal_priv *priv; |
| 76 | struct resource *res; |
| 77 | |
| 78 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 79 | if (!res) { |
| 80 | dev_err(&pdev->dev, "Failed to get platform resource\n"); |
| 81 | return -ENODEV; |
| 82 | } |
| 83 | |
| 84 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 85 | if (!priv) |
| 86 | return -ENOMEM; |
| 87 | |
Sachin Kamat | aa3b5d2 | 2013-03-04 06:45:34 +0000 | [diff] [blame] | 88 | priv->sensor = devm_ioremap_resource(&pdev->dev, res); |
| 89 | if (IS_ERR(priv->sensor)) |
| 90 | return PTR_ERR(priv->sensor); |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 91 | |
| 92 | thermal = thermal_zone_device_register("kirkwood_thermal", 0, 0, |
| 93 | priv, &ops, NULL, 0, 0); |
| 94 | if (IS_ERR(thermal)) { |
| 95 | dev_err(&pdev->dev, |
| 96 | "Failed to register thermal zone device\n"); |
| 97 | return PTR_ERR(thermal); |
| 98 | } |
| 99 | |
| 100 | platform_set_drvdata(pdev, thermal); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int kirkwood_thermal_exit(struct platform_device *pdev) |
| 106 | { |
| 107 | struct thermal_zone_device *kirkwood_thermal = |
| 108 | platform_get_drvdata(pdev); |
| 109 | |
| 110 | thermal_zone_device_unregister(kirkwood_thermal); |
| 111 | platform_set_drvdata(pdev, NULL); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | MODULE_DEVICE_TABLE(of, kirkwood_thermal_id_table); |
| 117 | |
| 118 | static struct platform_driver kirkwood_thermal_driver = { |
| 119 | .probe = kirkwood_thermal_probe, |
| 120 | .remove = kirkwood_thermal_exit, |
| 121 | .driver = { |
| 122 | .name = "kirkwood_thermal", |
| 123 | .owner = THIS_MODULE, |
Sachin Kamat | 90c3194 | 2013-05-16 10:28:10 +0000 | [diff] [blame^] | 124 | .of_match_table = kirkwood_thermal_id_table, |
Nobuhiro Iwamatsu | 7060aa3 | 2013-02-06 06:35:24 +0000 | [diff] [blame] | 125 | }, |
| 126 | }; |
| 127 | |
| 128 | module_platform_driver(kirkwood_thermal_driver); |
| 129 | |
| 130 | MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>"); |
| 131 | MODULE_DESCRIPTION("kirkwood thermal driver"); |
| 132 | MODULE_LICENSE("GPL"); |