Amit Kucheria | 2d71d8d | 2018-09-12 15:22:47 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 4 | */ |
| 5 | |
Amit Kucheria | 7c938f4 | 2019-11-01 00:07:28 +0530 | [diff] [blame] | 6 | #include <linux/debugfs.h> |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 7 | #include <linux/err.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/of.h> |
Amit Kucheria | 634e11d | 2019-11-01 00:07:39 +0530 | [diff] [blame] | 10 | #include <linux/of_platform.h> |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/pm.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/thermal.h> |
| 15 | #include "tsens.h" |
| 16 | |
| 17 | static int tsens_get_temp(void *data, int *temp) |
| 18 | { |
Amit Kucheria | 8b71bce | 2019-11-01 00:07:25 +0530 | [diff] [blame] | 19 | struct tsens_sensor *s = data; |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 20 | struct tsens_priv *priv = s->priv; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 21 | |
Amit Kucheria | 8b71bce | 2019-11-01 00:07:25 +0530 | [diff] [blame] | 22 | return priv->ops->get_temp(s, temp); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 23 | } |
| 24 | |
Amit Kucheria | 2cbcd2e | 2019-03-20 18:47:45 +0530 | [diff] [blame] | 25 | static int tsens_get_trend(void *data, int trip, enum thermal_trend *trend) |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 26 | { |
Amit Kucheria | 8b71bce | 2019-11-01 00:07:25 +0530 | [diff] [blame] | 27 | struct tsens_sensor *s = data; |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 28 | struct tsens_priv *priv = s->priv; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 29 | |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 30 | if (priv->ops->get_trend) |
Amit Kucheria | 8b71bce | 2019-11-01 00:07:25 +0530 | [diff] [blame] | 31 | return priv->ops->get_trend(s, trend); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 32 | |
| 33 | return -ENOTSUPP; |
| 34 | } |
| 35 | |
Arnd Bergmann | 5b97469 | 2016-07-04 15:12:28 +0200 | [diff] [blame] | 36 | static int __maybe_unused tsens_suspend(struct device *dev) |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 37 | { |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 38 | struct tsens_priv *priv = dev_get_drvdata(dev); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 39 | |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 40 | if (priv->ops && priv->ops->suspend) |
| 41 | return priv->ops->suspend(priv); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Arnd Bergmann | 5b97469 | 2016-07-04 15:12:28 +0200 | [diff] [blame] | 46 | static int __maybe_unused tsens_resume(struct device *dev) |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 47 | { |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 48 | struct tsens_priv *priv = dev_get_drvdata(dev); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 49 | |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 50 | if (priv->ops && priv->ops->resume) |
| 51 | return priv->ops->resume(priv); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume); |
| 57 | |
| 58 | static const struct of_device_id tsens_table[] = { |
| 59 | { |
| 60 | .compatible = "qcom,msm8916-tsens", |
Rajendra Nayak | 840a5bd | 2016-05-05 14:21:40 +0530 | [diff] [blame] | 61 | .data = &data_8916, |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 62 | }, { |
| 63 | .compatible = "qcom,msm8974-tsens", |
Rajendra Nayak | 5e6703b | 2016-05-05 14:21:41 +0530 | [diff] [blame] | 64 | .data = &data_8974, |
Rajendra Nayak | d059c73 | 2016-05-05 14:21:44 +0530 | [diff] [blame] | 65 | }, { |
AngeloGioacchino Del Regno | 0e58029 | 2019-10-05 12:41:31 +0200 | [diff] [blame] | 66 | .compatible = "qcom,msm8976-tsens", |
| 67 | .data = &data_8976, |
| 68 | }, { |
Rajendra Nayak | d059c73 | 2016-05-05 14:21:44 +0530 | [diff] [blame] | 69 | .compatible = "qcom,msm8996-tsens", |
| 70 | .data = &data_8996, |
Amit Kucheria | 191dc74 | 2018-07-18 12:13:12 +0530 | [diff] [blame] | 71 | }, { |
Amit Kucheria | e8c24c6f | 2019-03-20 18:48:00 +0530 | [diff] [blame] | 72 | .compatible = "qcom,tsens-v1", |
| 73 | .data = &data_tsens_v1, |
| 74 | }, { |
Amit Kucheria | 191dc74 | 2018-07-18 12:13:12 +0530 | [diff] [blame] | 75 | .compatible = "qcom,tsens-v2", |
| 76 | .data = &data_tsens_v2, |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 77 | }, |
| 78 | {} |
| 79 | }; |
| 80 | MODULE_DEVICE_TABLE(of, tsens_table); |
| 81 | |
| 82 | static const struct thermal_zone_of_device_ops tsens_of_ops = { |
| 83 | .get_temp = tsens_get_temp, |
| 84 | .get_trend = tsens_get_trend, |
Amit Kucheria | 634e11d | 2019-11-01 00:07:39 +0530 | [diff] [blame] | 85 | .set_trips = tsens_set_trips, |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 86 | }; |
| 87 | |
Amit Kucheria | 79125e0 | 2020-03-12 18:07:02 +0530 | [diff] [blame] | 88 | static int tsens_register_irq(struct tsens_priv *priv, char *irqname, |
| 89 | irq_handler_t thread_fn) |
| 90 | { |
| 91 | struct platform_device *pdev; |
| 92 | int ret, irq; |
| 93 | |
| 94 | pdev = of_find_device_by_node(priv->dev->of_node); |
| 95 | if (!pdev) |
| 96 | return -ENODEV; |
| 97 | |
| 98 | irq = platform_get_irq_byname(pdev, irqname); |
| 99 | if (irq < 0) { |
| 100 | ret = irq; |
| 101 | /* For old DTs with no IRQ defined */ |
| 102 | if (irq == -ENXIO) |
| 103 | ret = 0; |
| 104 | } else { |
| 105 | ret = devm_request_threaded_irq(&pdev->dev, irq, |
| 106 | NULL, thread_fn, |
Amit Kucheria | 8d3a6d4 | 2020-03-12 18:07:05 +0530 | [diff] [blame] | 107 | IRQF_ONESHOT, |
Amit Kucheria | 79125e0 | 2020-03-12 18:07:02 +0530 | [diff] [blame] | 108 | dev_name(&pdev->dev), priv); |
| 109 | if (ret) |
| 110 | dev_err(&pdev->dev, "%s: failed to get irq\n", |
| 111 | __func__); |
| 112 | else |
| 113 | enable_irq_wake(irq); |
| 114 | } |
| 115 | |
| 116 | put_device(&pdev->dev); |
| 117 | return ret; |
| 118 | } |
| 119 | |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 120 | static int tsens_register(struct tsens_priv *priv) |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 121 | { |
Amit Kucheria | 79125e0 | 2020-03-12 18:07:02 +0530 | [diff] [blame] | 122 | int i, ret; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 123 | struct thermal_zone_device *tzd; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 124 | |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 125 | for (i = 0; i < priv->num_sensors; i++) { |
| 126 | priv->sensor[i].priv = priv; |
Amit Kucheria | 8b71bce | 2019-11-01 00:07:25 +0530 | [diff] [blame] | 127 | tzd = devm_thermal_zone_of_sensor_register(priv->dev, priv->sensor[i].hw_id, |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 128 | &priv->sensor[i], |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 129 | &tsens_of_ops); |
| 130 | if (IS_ERR(tzd)) |
| 131 | continue; |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 132 | priv->sensor[i].tzd = tzd; |
| 133 | if (priv->ops->enable) |
| 134 | priv->ops->enable(priv, i); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 135 | } |
Amit Kucheria | 634e11d | 2019-11-01 00:07:39 +0530 | [diff] [blame] | 136 | |
Amit Kucheria | 79125e0 | 2020-03-12 18:07:02 +0530 | [diff] [blame] | 137 | ret = tsens_register_irq(priv, "uplow", tsens_irq_thread); |
| 138 | if (ret < 0) |
| 139 | return ret; |
Amit Kucheria | 634e11d | 2019-11-01 00:07:39 +0530 | [diff] [blame] | 140 | |
Amit Kucheria | 79125e0 | 2020-03-12 18:07:02 +0530 | [diff] [blame] | 141 | if (priv->feat->crit_int) |
| 142 | ret = tsens_register_irq(priv, "critical", |
| 143 | tsens_critical_irq_thread); |
Amit Kucheria | 634e11d | 2019-11-01 00:07:39 +0530 | [diff] [blame] | 144 | |
Amit Kucheria | 634e11d | 2019-11-01 00:07:39 +0530 | [diff] [blame] | 145 | return ret; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static int tsens_probe(struct platform_device *pdev) |
| 149 | { |
| 150 | int ret, i; |
| 151 | struct device *dev; |
| 152 | struct device_node *np; |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 153 | struct tsens_priv *priv; |
Amit Kucheria | 3c040ce | 2019-03-20 18:47:42 +0530 | [diff] [blame] | 154 | const struct tsens_plat_data *data; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 155 | const struct of_device_id *id; |
Bjorn Andersson | 6d7c70d | 2018-05-07 16:53:39 -0700 | [diff] [blame] | 156 | u32 num_sensors; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 157 | |
| 158 | if (pdev->dev.of_node) |
| 159 | dev = &pdev->dev; |
| 160 | else |
| 161 | dev = pdev->dev.parent; |
| 162 | |
| 163 | np = dev->of_node; |
| 164 | |
| 165 | id = of_match_node(tsens_table, np); |
Rajendra Nayak | 20d4fd8 | 2016-05-05 14:21:43 +0530 | [diff] [blame] | 166 | if (id) |
| 167 | data = id->data; |
| 168 | else |
| 169 | data = &data_8960; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 170 | |
Bjorn Andersson | 6d7c70d | 2018-05-07 16:53:39 -0700 | [diff] [blame] | 171 | num_sensors = data->num_sensors; |
| 172 | |
| 173 | if (np) |
| 174 | of_property_read_u32(np, "#qcom,sensors", &num_sensors); |
| 175 | |
| 176 | if (num_sensors <= 0) { |
Amit Kucheria | 3795ad5 | 2019-11-01 00:07:27 +0530 | [diff] [blame] | 177 | dev_err(dev, "%s: invalid number of sensors\n", __func__); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 178 | return -EINVAL; |
| 179 | } |
| 180 | |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 181 | priv = devm_kzalloc(dev, |
| 182 | struct_size(priv, sensor, num_sensors), |
Kees Cook | 0ed2dd0 | 2018-05-08 16:08:53 -0700 | [diff] [blame] | 183 | GFP_KERNEL); |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 184 | if (!priv) |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 185 | return -ENOMEM; |
| 186 | |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 187 | priv->dev = dev; |
| 188 | priv->num_sensors = num_sensors; |
| 189 | priv->ops = data->ops; |
| 190 | for (i = 0; i < priv->num_sensors; i++) { |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 191 | if (data->hw_ids) |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 192 | priv->sensor[i].hw_id = data->hw_ids[i]; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 193 | else |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 194 | priv->sensor[i].hw_id = i; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 195 | } |
Amit Kucheria | c199705 | 2019-03-20 18:47:50 +0530 | [diff] [blame] | 196 | priv->feat = data->feat; |
| 197 | priv->fields = data->fields; |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 198 | |
Amit Kucheria | 0e9c0bc | 2019-11-01 00:07:26 +0530 | [diff] [blame] | 199 | platform_set_drvdata(pdev, priv); |
| 200 | |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 201 | if (!priv->ops || !priv->ops->init || !priv->ops->get_temp) |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 202 | return -EINVAL; |
| 203 | |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 204 | ret = priv->ops->init(priv); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 205 | if (ret < 0) { |
Amit Kucheria | 3795ad5 | 2019-11-01 00:07:27 +0530 | [diff] [blame] | 206 | dev_err(dev, "%s: init failed\n", __func__); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 207 | return ret; |
| 208 | } |
| 209 | |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 210 | if (priv->ops->calibrate) { |
| 211 | ret = priv->ops->calibrate(priv); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 212 | if (ret < 0) { |
Amit Kucheria | fc7d18c | 2019-03-20 18:47:52 +0530 | [diff] [blame] | 213 | if (ret != -EPROBE_DEFER) |
Amit Kucheria | 3795ad5 | 2019-11-01 00:07:27 +0530 | [diff] [blame] | 214 | dev_err(dev, "%s: calibration failed\n", __func__); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 215 | return ret; |
| 216 | } |
| 217 | } |
| 218 | |
Amit Kucheria | 0e9c0bc | 2019-11-01 00:07:26 +0530 | [diff] [blame] | 219 | return tsens_register(priv); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static int tsens_remove(struct platform_device *pdev) |
| 223 | { |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 224 | struct tsens_priv *priv = platform_get_drvdata(pdev); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 225 | |
Amit Kucheria | 7c938f4 | 2019-11-01 00:07:28 +0530 | [diff] [blame] | 226 | debugfs_remove_recursive(priv->debug_root); |
Amit Kucheria | 634e11d | 2019-11-01 00:07:39 +0530 | [diff] [blame] | 227 | tsens_disable_irq(priv); |
Amit Kucheria | 69b628a | 2019-03-20 18:47:44 +0530 | [diff] [blame] | 228 | if (priv->ops->disable) |
| 229 | priv->ops->disable(priv); |
Rajendra Nayak | 9066073 | 2016-05-05 14:21:39 +0530 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static struct platform_driver tsens_driver = { |
| 235 | .probe = tsens_probe, |
| 236 | .remove = tsens_remove, |
| 237 | .driver = { |
| 238 | .name = "qcom-tsens", |
| 239 | .pm = &tsens_pm_ops, |
| 240 | .of_match_table = tsens_table, |
| 241 | }, |
| 242 | }; |
| 243 | module_platform_driver(tsens_driver); |
| 244 | |
| 245 | MODULE_LICENSE("GPL v2"); |
| 246 | MODULE_DESCRIPTION("QCOM Temperature Sensor driver"); |
| 247 | MODULE_ALIAS("platform:qcom-tsens"); |