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_device *tmdev = s->tmdev; 19 20 return tmdev->ops->get_temp(tmdev, s->id, temp); 21 } 22 23 static int tsens_get_trend(void *p, int trip, enum thermal_trend *trend) 24 { 25 const struct tsens_sensor *s = p; 26 struct tsens_device *tmdev = s->tmdev; 27 28 if (tmdev->ops->get_trend) 29 return tmdev->ops->get_trend(tmdev, s->id, trend); 30 31 return -ENOTSUPP; 32 } 33 34 static int __maybe_unused tsens_suspend(struct device *dev) 35 { 36 struct tsens_device *tmdev = dev_get_drvdata(dev); 37 38 if (tmdev->ops && tmdev->ops->suspend) 39 return tmdev->ops->suspend(tmdev); 40 41 return 0; 42 } 43 44 static int __maybe_unused tsens_resume(struct device *dev) 45 { 46 struct tsens_device *tmdev = dev_get_drvdata(dev); 47 48 if (tmdev->ops && tmdev->ops->resume) 49 return tmdev->ops->resume(tmdev); 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-v2", 68 .data = &data_tsens_v2, 69 }, 70 {} 71 }; 72 MODULE_DEVICE_TABLE(of, tsens_table); 73 74 static const struct thermal_zone_of_device_ops tsens_of_ops = { 75 .get_temp = tsens_get_temp, 76 .get_trend = tsens_get_trend, 77 }; 78 79 static int tsens_register(struct tsens_device *tmdev) 80 { 81 int i; 82 struct thermal_zone_device *tzd; 83 84 for (i = 0; i < tmdev->num_sensors; i++) { 85 tmdev->sensor[i].tmdev = tmdev; 86 tmdev->sensor[i].id = i; 87 tzd = devm_thermal_zone_of_sensor_register(tmdev->dev, i, 88 &tmdev->sensor[i], 89 &tsens_of_ops); 90 if (IS_ERR(tzd)) 91 continue; 92 tmdev->sensor[i].tzd = tzd; 93 if (tmdev->ops->enable) 94 tmdev->ops->enable(tmdev, i); 95 } 96 return 0; 97 } 98 99 static int tsens_probe(struct platform_device *pdev) 100 { 101 int ret, i; 102 struct device *dev; 103 struct device_node *np; 104 struct tsens_device *tmdev; 105 const struct tsens_data *data; 106 const struct of_device_id *id; 107 u32 num_sensors; 108 109 if (pdev->dev.of_node) 110 dev = &pdev->dev; 111 else 112 dev = pdev->dev.parent; 113 114 np = dev->of_node; 115 116 id = of_match_node(tsens_table, np); 117 if (id) 118 data = id->data; 119 else 120 data = &data_8960; 121 122 num_sensors = data->num_sensors; 123 124 if (np) 125 of_property_read_u32(np, "#qcom,sensors", &num_sensors); 126 127 if (num_sensors <= 0) { 128 dev_err(dev, "invalid number of sensors\n"); 129 return -EINVAL; 130 } 131 132 tmdev = devm_kzalloc(dev, 133 struct_size(tmdev, sensor, num_sensors), 134 GFP_KERNEL); 135 if (!tmdev) 136 return -ENOMEM; 137 138 tmdev->dev = dev; 139 tmdev->num_sensors = num_sensors; 140 tmdev->ops = data->ops; 141 for (i = 0; i < tmdev->num_sensors; i++) { 142 if (data->hw_ids) 143 tmdev->sensor[i].hw_id = data->hw_ids[i]; 144 else 145 tmdev->sensor[i].hw_id = i; 146 } 147 for (i = 0; i < REG_ARRAY_SIZE; i++) { 148 tmdev->reg_offsets[i] = data->reg_offsets[i]; 149 } 150 151 if (!tmdev->ops || !tmdev->ops->init || !tmdev->ops->get_temp) 152 return -EINVAL; 153 154 ret = tmdev->ops->init(tmdev); 155 if (ret < 0) { 156 dev_err(dev, "tsens init failed\n"); 157 return ret; 158 } 159 160 if (tmdev->ops->calibrate) { 161 ret = tmdev->ops->calibrate(tmdev); 162 if (ret < 0) { 163 dev_err(dev, "tsens calibration failed\n"); 164 return ret; 165 } 166 } 167 168 ret = tsens_register(tmdev); 169 170 platform_set_drvdata(pdev, tmdev); 171 172 return ret; 173 } 174 175 static int tsens_remove(struct platform_device *pdev) 176 { 177 struct tsens_device *tmdev = platform_get_drvdata(pdev); 178 179 if (tmdev->ops->disable) 180 tmdev->ops->disable(tmdev); 181 182 return 0; 183 } 184 185 static struct platform_driver tsens_driver = { 186 .probe = tsens_probe, 187 .remove = tsens_remove, 188 .driver = { 189 .name = "qcom-tsens", 190 .pm = &tsens_pm_ops, 191 .of_match_table = tsens_table, 192 }, 193 }; 194 module_platform_driver(tsens_driver); 195 196 MODULE_LICENSE("GPL v2"); 197 MODULE_DESCRIPTION("QCOM Temperature Sensor driver"); 198 MODULE_ALIAS("platform:qcom-tsens"); 199