1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2015, The Linux Foundation. All rights reserved. 4 */ 5 6 #include <linux/err.h> 7 #include <linux/module.h> 8 #include <linux/of.h> 9 #include <linux/platform_device.h> 10 #include <linux/pm.h> 11 #include <linux/slab.h> 12 #include <linux/thermal.h> 13 #include "tsens.h" 14 15 static int tsens_get_temp(void *data, int *temp) 16 { 17 const struct tsens_sensor *s = data; 18 struct tsens_priv *priv = s->priv; 19 20 return priv->ops->get_temp(priv, s->id, temp); 21 } 22 23 static int tsens_get_trend(void *data, int trip, enum thermal_trend *trend) 24 { 25 const struct tsens_sensor *s = data; 26 struct tsens_priv *priv = s->priv; 27 28 if (priv->ops->get_trend) 29 return priv->ops->get_trend(priv, s->id, trend); 30 31 return -ENOTSUPP; 32 } 33 34 static int __maybe_unused tsens_suspend(struct device *dev) 35 { 36 struct tsens_priv *priv = dev_get_drvdata(dev); 37 38 if (priv->ops && priv->ops->suspend) 39 return priv->ops->suspend(priv); 40 41 return 0; 42 } 43 44 static int __maybe_unused tsens_resume(struct device *dev) 45 { 46 struct tsens_priv *priv = dev_get_drvdata(dev); 47 48 if (priv->ops && priv->ops->resume) 49 return priv->ops->resume(priv); 50 51 return 0; 52 } 53 54 static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume); 55 56 static const struct of_device_id tsens_table[] = { 57 { 58 .compatible = "qcom,msm8916-tsens", 59 .data = &data_8916, 60 }, { 61 .compatible = "qcom,msm8974-tsens", 62 .data = &data_8974, 63 }, { 64 .compatible = "qcom,msm8996-tsens", 65 .data = &data_8996, 66 }, { 67 .compatible = "qcom,tsens-v1", 68 .data = &data_tsens_v1, 69 }, { 70 .compatible = "qcom,tsens-v2", 71 .data = &data_tsens_v2, 72 }, 73 {} 74 }; 75 MODULE_DEVICE_TABLE(of, tsens_table); 76 77 static const struct thermal_zone_of_device_ops tsens_of_ops = { 78 .get_temp = tsens_get_temp, 79 .get_trend = tsens_get_trend, 80 }; 81 82 static int tsens_register(struct tsens_priv *priv) 83 { 84 int i; 85 struct thermal_zone_device *tzd; 86 87 for (i = 0; i < priv->num_sensors; i++) { 88 priv->sensor[i].priv = priv; 89 priv->sensor[i].id = i; 90 tzd = devm_thermal_zone_of_sensor_register(priv->dev, i, 91 &priv->sensor[i], 92 &tsens_of_ops); 93 if (IS_ERR(tzd)) 94 continue; 95 priv->sensor[i].tzd = tzd; 96 if (priv->ops->enable) 97 priv->ops->enable(priv, i); 98 } 99 return 0; 100 } 101 102 static int tsens_probe(struct platform_device *pdev) 103 { 104 int ret, i; 105 struct device *dev; 106 struct device_node *np; 107 struct tsens_priv *priv; 108 const struct tsens_plat_data *data; 109 const struct of_device_id *id; 110 u32 num_sensors; 111 112 if (pdev->dev.of_node) 113 dev = &pdev->dev; 114 else 115 dev = pdev->dev.parent; 116 117 np = dev->of_node; 118 119 id = of_match_node(tsens_table, np); 120 if (id) 121 data = id->data; 122 else 123 data = &data_8960; 124 125 num_sensors = data->num_sensors; 126 127 if (np) 128 of_property_read_u32(np, "#qcom,sensors", &num_sensors); 129 130 if (num_sensors <= 0) { 131 dev_err(dev, "invalid number of sensors\n"); 132 return -EINVAL; 133 } 134 135 priv = devm_kzalloc(dev, 136 struct_size(priv, sensor, num_sensors), 137 GFP_KERNEL); 138 if (!priv) 139 return -ENOMEM; 140 141 priv->dev = dev; 142 priv->num_sensors = num_sensors; 143 priv->ops = data->ops; 144 for (i = 0; i < priv->num_sensors; i++) { 145 if (data->hw_ids) 146 priv->sensor[i].hw_id = data->hw_ids[i]; 147 else 148 priv->sensor[i].hw_id = i; 149 } 150 priv->feat = data->feat; 151 priv->fields = data->fields; 152 153 if (!priv->ops || !priv->ops->init || !priv->ops->get_temp) 154 return -EINVAL; 155 156 ret = priv->ops->init(priv); 157 if (ret < 0) { 158 dev_err(dev, "tsens init failed\n"); 159 return ret; 160 } 161 162 if (priv->ops->calibrate) { 163 ret = priv->ops->calibrate(priv); 164 if (ret < 0) { 165 if (ret != -EPROBE_DEFER) 166 dev_err(dev, "tsens calibration failed\n"); 167 return ret; 168 } 169 } 170 171 ret = tsens_register(priv); 172 173 platform_set_drvdata(pdev, priv); 174 175 return ret; 176 } 177 178 static int tsens_remove(struct platform_device *pdev) 179 { 180 struct tsens_priv *priv = platform_get_drvdata(pdev); 181 182 if (priv->ops->disable) 183 priv->ops->disable(priv); 184 185 return 0; 186 } 187 188 static struct platform_driver tsens_driver = { 189 .probe = tsens_probe, 190 .remove = tsens_remove, 191 .driver = { 192 .name = "qcom-tsens", 193 .pm = &tsens_pm_ops, 194 .of_match_table = tsens_table, 195 }, 196 }; 197 module_platform_driver(tsens_driver); 198 199 MODULE_LICENSE("GPL v2"); 200 MODULE_DESCRIPTION("QCOM Temperature Sensor driver"); 201 MODULE_ALIAS("platform:qcom-tsens"); 202