Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/pm.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/thermal.h> |
| 22 | #include "tsens.h" |
| 23 | |
| 24 | static int tsens_get_temp(void *data, int *temp) |
| 25 | { |
| 26 | const struct tsens_sensor *s = data; |
| 27 | struct tsens_device *tmdev = s->tmdev; |
| 28 | |
| 29 | return tmdev->ops->get_temp(tmdev, s->id, temp); |
| 30 | } |
| 31 | |
Sascha Hauer | e78eaf4 | 2016-06-22 16:42:03 +0800 | [diff] [blame] | 32 | static int tsens_get_trend(void *p, int trip, enum thermal_trend *trend) |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 33 | { |
Sascha Hauer | e78eaf4 | 2016-06-22 16:42:03 +0800 | [diff] [blame] | 34 | const struct tsens_sensor *s = p; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 35 | struct tsens_device *tmdev = s->tmdev; |
| 36 | |
| 37 | if (tmdev->ops->get_trend) |
Sascha Hauer | e78eaf4 | 2016-06-22 16:42:03 +0800 | [diff] [blame] | 38 | return tmdev->ops->get_trend(tmdev, s->id, trend); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 39 | |
| 40 | return -ENOTSUPP; |
| 41 | } |
| 42 | |
Arnd Bergmann | 5b97469 | 2016-07-04 15:12:28 +0200 | [diff] [blame] | 43 | static int __maybe_unused tsens_suspend(struct device *dev) |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 44 | { |
| 45 | struct tsens_device *tmdev = dev_get_drvdata(dev); |
| 46 | |
| 47 | if (tmdev->ops && tmdev->ops->suspend) |
| 48 | return tmdev->ops->suspend(tmdev); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
Arnd Bergmann | 5b97469 | 2016-07-04 15:12:28 +0200 | [diff] [blame] | 53 | static int __maybe_unused tsens_resume(struct device *dev) |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 54 | { |
| 55 | struct tsens_device *tmdev = dev_get_drvdata(dev); |
| 56 | |
| 57 | if (tmdev->ops && tmdev->ops->resume) |
| 58 | return tmdev->ops->resume(tmdev); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume); |
| 64 | |
| 65 | static const struct of_device_id tsens_table[] = { |
| 66 | { |
| 67 | .compatible = "qcom,msm8916-tsens", |
Rajendra Nayak | 840a5bd | 2016-05-05 14:21:40 +0530 | [diff] [blame] | 68 | .data = &data_8916, |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 69 | }, { |
| 70 | .compatible = "qcom,msm8974-tsens", |
Rajendra Nayak | 5e6703b | 2016-05-05 14:21:41 +0530 | [diff] [blame] | 71 | .data = &data_8974, |
Rajendra Nayak | d059c73 | 2016-05-05 14:21:44 +0530 | [diff] [blame] | 72 | }, { |
| 73 | .compatible = "qcom,msm8996-tsens", |
| 74 | .data = &data_8996, |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 75 | }, |
| 76 | {} |
| 77 | }; |
| 78 | MODULE_DEVICE_TABLE(of, tsens_table); |
| 79 | |
| 80 | static const struct thermal_zone_of_device_ops tsens_of_ops = { |
| 81 | .get_temp = tsens_get_temp, |
| 82 | .get_trend = tsens_get_trend, |
| 83 | }; |
| 84 | |
| 85 | static int tsens_register(struct tsens_device *tmdev) |
| 86 | { |
| 87 | int i; |
| 88 | struct thermal_zone_device *tzd; |
| 89 | u32 *hw_id, n = tmdev->num_sensors; |
| 90 | |
| 91 | hw_id = devm_kcalloc(tmdev->dev, n, sizeof(u32), GFP_KERNEL); |
| 92 | if (!hw_id) |
| 93 | return -ENOMEM; |
| 94 | |
| 95 | for (i = 0; i < tmdev->num_sensors; i++) { |
| 96 | tmdev->sensor[i].tmdev = tmdev; |
| 97 | tmdev->sensor[i].id = i; |
| 98 | tzd = devm_thermal_zone_of_sensor_register(tmdev->dev, i, |
| 99 | &tmdev->sensor[i], |
| 100 | &tsens_of_ops); |
| 101 | if (IS_ERR(tzd)) |
| 102 | continue; |
| 103 | tmdev->sensor[i].tzd = tzd; |
| 104 | if (tmdev->ops->enable) |
| 105 | tmdev->ops->enable(tmdev, i); |
| 106 | } |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int tsens_probe(struct platform_device *pdev) |
| 111 | { |
| 112 | int ret, i; |
| 113 | struct device *dev; |
| 114 | struct device_node *np; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 115 | struct tsens_device *tmdev; |
| 116 | const struct tsens_data *data; |
| 117 | const struct of_device_id *id; |
| 118 | |
| 119 | if (pdev->dev.of_node) |
| 120 | dev = &pdev->dev; |
| 121 | else |
| 122 | dev = pdev->dev.parent; |
| 123 | |
| 124 | np = dev->of_node; |
| 125 | |
| 126 | id = of_match_node(tsens_table, np); |
Rajendra Nayak | 20d4fd8 | 2016-05-05 14:21:43 +0530 | [diff] [blame] | 127 | if (id) |
| 128 | data = id->data; |
| 129 | else |
| 130 | data = &data_8960; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 131 | |
| 132 | if (data->num_sensors <= 0) { |
| 133 | dev_err(dev, "invalid number of sensors\n"); |
| 134 | return -EINVAL; |
| 135 | } |
| 136 | |
Kees Cook | 0ed2dd0 | 2018-05-08 16:08:53 -0700 | [diff] [blame] | 137 | tmdev = devm_kzalloc(dev, |
| 138 | struct_size(tmdev, sensor, data->num_sensors), |
| 139 | GFP_KERNEL); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 140 | if (!tmdev) |
| 141 | return -ENOMEM; |
| 142 | |
| 143 | tmdev->dev = dev; |
| 144 | tmdev->num_sensors = data->num_sensors; |
| 145 | tmdev->ops = data->ops; |
| 146 | for (i = 0; i < tmdev->num_sensors; i++) { |
| 147 | if (data->hw_ids) |
| 148 | tmdev->sensor[i].hw_id = data->hw_ids[i]; |
| 149 | else |
| 150 | tmdev->sensor[i].hw_id = i; |
| 151 | } |
| 152 | |
| 153 | if (!tmdev->ops || !tmdev->ops->init || !tmdev->ops->get_temp) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | ret = tmdev->ops->init(tmdev); |
| 157 | if (ret < 0) { |
| 158 | dev_err(dev, "tsens init failed\n"); |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | if (tmdev->ops->calibrate) { |
| 163 | ret = tmdev->ops->calibrate(tmdev); |
| 164 | if (ret < 0) { |
| 165 | dev_err(dev, "tsens calibration failed\n"); |
| 166 | return ret; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | ret = tsens_register(tmdev); |
| 171 | |
| 172 | platform_set_drvdata(pdev, tmdev); |
| 173 | |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | static int tsens_remove(struct platform_device *pdev) |
| 178 | { |
| 179 | struct tsens_device *tmdev = platform_get_drvdata(pdev); |
| 180 | |
| 181 | if (tmdev->ops->disable) |
| 182 | tmdev->ops->disable(tmdev); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static struct platform_driver tsens_driver = { |
| 188 | .probe = tsens_probe, |
| 189 | .remove = tsens_remove, |
| 190 | .driver = { |
| 191 | .name = "qcom-tsens", |
| 192 | .pm = &tsens_pm_ops, |
| 193 | .of_match_table = tsens_table, |
| 194 | }, |
| 195 | }; |
| 196 | module_platform_driver(tsens_driver); |
| 197 | |
| 198 | MODULE_LICENSE("GPL v2"); |
| 199 | MODULE_DESCRIPTION("QCOM Temperature Sensor driver"); |
| 200 | MODULE_ALIAS("platform:qcom-tsens"); |