Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
Siddartha Mohanadoss | cd8aa0b | 2017-04-06 15:17:46 -0700 | [diff] [blame] | 4 | * it under the terms of the GNU General Public License version 2 and |
Siddartha Mohanadoss | 41b4cd9 | 2017-02-21 14:34:23 -0800 | [diff] [blame] | 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/pm.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/thermal.h> |
| 23 | #include "tsens.h" |
| 24 | |
| 25 | LIST_HEAD(tsens_device_list); |
| 26 | |
| 27 | static int tsens_get_temp(struct tsens_sensor *s, int *temp) |
| 28 | { |
| 29 | struct tsens_device *tmdev = s->tmdev; |
| 30 | |
| 31 | return tmdev->ops->get_temp(s, temp); |
| 32 | } |
| 33 | |
| 34 | static int tsens_set_trip_temp(struct tsens_sensor *s, int trip, int temp) |
| 35 | { |
| 36 | struct tsens_device *tmdev = s->tmdev; |
| 37 | |
| 38 | if (tmdev->ops->set_trip_temp) |
| 39 | return tmdev->ops->set_trip_temp(s, trip, temp); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int tsens_init(struct tsens_device *tmdev) |
| 45 | { |
| 46 | if (tmdev->ops->hw_init) |
| 47 | return tmdev->ops->hw_init(tmdev); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int tsens_register_interrupts(struct tsens_device *tmdev) |
| 53 | { |
| 54 | if (tmdev->ops->interrupts_reg) |
| 55 | return tmdev->ops->interrupts_reg(tmdev); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static const struct of_device_id tsens_table[] = { |
| 61 | { .compatible = "qcom,msm8996-tsens", |
| 62 | .data = &data_tsens2xxx, |
| 63 | }, |
| 64 | { .compatible = "qcom,msm8953-tsens", |
| 65 | .data = &data_tsens2xxx, |
| 66 | }, |
| 67 | { .compatible = "qcom,msm8998-tsens", |
| 68 | .data = &data_tsens2xxx, |
| 69 | }, |
| 70 | { .compatible = "qcom,msmhamster-tsens", |
| 71 | .data = &data_tsens2xxx, |
| 72 | }, |
| 73 | { .compatible = "qcom,sdm660-tsens", |
| 74 | .data = &data_tsens23xx, |
| 75 | }, |
| 76 | { .compatible = "qcom,sdm630-tsens", |
| 77 | .data = &data_tsens23xx, |
| 78 | }, |
| 79 | { .compatible = "qcom,sdm845-tsens", |
| 80 | .data = &data_tsens24xx, |
| 81 | }, |
| 82 | {} |
| 83 | }; |
| 84 | MODULE_DEVICE_TABLE(of, tsens_table); |
| 85 | |
| 86 | static struct thermal_zone_of_device_ops tsens_tm_thermal_zone_ops = { |
| 87 | .get_temp = tsens_get_temp, |
| 88 | .set_trip_temp = tsens_set_trip_temp, |
| 89 | }; |
| 90 | |
| 91 | static int get_device_tree_data(struct platform_device *pdev, |
| 92 | struct tsens_device *tmdev) |
| 93 | { |
| 94 | struct device_node *of_node = pdev->dev.of_node; |
| 95 | u32 *hw_id, *client_id; |
| 96 | u32 rc = 0, i, tsens_num_sensors = 0; |
| 97 | int tsens_len; |
| 98 | const struct of_device_id *id; |
| 99 | const struct tsens_data *data; |
| 100 | struct resource *res_tsens_mem, *res_mem = NULL; |
| 101 | |
| 102 | if (!of_match_node(tsens_table, of_node)) { |
| 103 | pr_err("Need to read SoC specific fuse map\n"); |
| 104 | return -ENODEV; |
| 105 | } |
| 106 | |
| 107 | id = of_match_node(tsens_table, of_node); |
| 108 | if (id == NULL) { |
| 109 | pr_err("can not find tsens_table of_node\n"); |
| 110 | return -ENODEV; |
| 111 | } |
| 112 | |
| 113 | data = id->data; |
| 114 | hw_id = devm_kzalloc(&pdev->dev, |
| 115 | tsens_num_sensors * sizeof(u32), GFP_KERNEL); |
| 116 | if (!hw_id) |
| 117 | return -ENOMEM; |
| 118 | |
| 119 | client_id = devm_kzalloc(&pdev->dev, |
| 120 | tsens_num_sensors * sizeof(u32), GFP_KERNEL); |
| 121 | if (!client_id) |
| 122 | return -ENOMEM; |
| 123 | |
| 124 | tmdev->ops = data->ops; |
| 125 | tmdev->ctrl_data = data; |
| 126 | tmdev->pdev = pdev; |
| 127 | |
| 128 | if (!tmdev->ops || !tmdev->ops->hw_init || !tmdev->ops->get_temp) { |
| 129 | pr_err("Invalid ops\n"); |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
| 133 | /* TSENS register region */ |
| 134 | res_tsens_mem = platform_get_resource_byname(pdev, |
| 135 | IORESOURCE_MEM, "tsens_physical"); |
| 136 | if (!res_tsens_mem) { |
| 137 | pr_err("Could not get tsens physical address resource\n"); |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | |
| 141 | tsens_len = res_tsens_mem->end - res_tsens_mem->start + 1; |
| 142 | |
| 143 | res_mem = request_mem_region(res_tsens_mem->start, |
| 144 | tsens_len, res_tsens_mem->name); |
| 145 | if (!res_mem) { |
| 146 | pr_err("Request tsens physical memory region failed\n"); |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | |
| 150 | tmdev->tsens_addr = ioremap(res_mem->start, tsens_len); |
| 151 | if (!tmdev->tsens_addr) { |
| 152 | pr_err("Failed to IO map TSENS registers.\n"); |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | |
| 156 | rc = of_property_read_u32_array(of_node, |
| 157 | "qcom,sensor-id", hw_id, tsens_num_sensors); |
| 158 | if (rc) { |
| 159 | pr_err("Default sensor id mapping\n"); |
| 160 | for (i = 0; i < tsens_num_sensors; i++) |
| 161 | tmdev->sensor[i].hw_id = i; |
| 162 | } else { |
| 163 | pr_err("Use specified sensor id mapping\n"); |
| 164 | for (i = 0; i < tsens_num_sensors; i++) |
| 165 | tmdev->sensor[i].hw_id = hw_id[i]; |
| 166 | } |
| 167 | |
| 168 | rc = of_property_read_u32_array(of_node, |
| 169 | "qcom,client-id", client_id, tsens_num_sensors); |
| 170 | if (rc) { |
| 171 | for (i = 0; i < tsens_num_sensors; i++) |
| 172 | tmdev->sensor[i].id = i; |
| 173 | pr_debug("Default client id mapping\n"); |
| 174 | } else { |
| 175 | for (i = 0; i < tsens_num_sensors; i++) |
| 176 | tmdev->sensor[i].id = client_id[i]; |
| 177 | pr_debug("Use specified client id mapping\n"); |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int tsens_thermal_zone_register(struct tsens_device *tmdev) |
| 184 | { |
| 185 | int rc = 0, i = 0; |
| 186 | |
| 187 | for (i = 0; i < tmdev->num_sensors; i++) { |
| 188 | tmdev->sensor[i].tmdev = tmdev; |
| 189 | tmdev->sensor[i].tzd = devm_thermal_zone_of_sensor_register( |
| 190 | &tmdev->pdev->dev, i, &tmdev->sensor[i], |
| 191 | &tsens_tm_thermal_zone_ops); |
| 192 | if (IS_ERR(tmdev->sensor[i].tzd)) { |
| 193 | pr_err("Error registering sensor:%d\n", i); |
| 194 | continue; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return rc; |
| 199 | } |
| 200 | |
| 201 | static int tsens_tm_remove(struct platform_device *pdev) |
| 202 | { |
| 203 | platform_set_drvdata(pdev, NULL); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | int tsens_tm_probe(struct platform_device *pdev) |
| 209 | { |
| 210 | struct device_node *of_node = pdev->dev.of_node; |
| 211 | struct tsens_device *tmdev = NULL; |
| 212 | u32 tsens_num_sensors = 0; |
| 213 | int rc; |
| 214 | |
| 215 | if (!(pdev->dev.of_node)) |
| 216 | return -ENODEV; |
| 217 | |
| 218 | rc = of_property_read_u32(of_node, |
| 219 | "qcom,sensors", &tsens_num_sensors); |
| 220 | if (rc || (!tsens_num_sensors)) { |
| 221 | dev_err(&pdev->dev, "missing sensors\n"); |
| 222 | return -ENODEV; |
| 223 | } |
| 224 | |
| 225 | tmdev = devm_kzalloc(&pdev->dev, |
| 226 | sizeof(struct tsens_device) + |
| 227 | tsens_num_sensors * |
| 228 | sizeof(struct tsens_sensor), |
| 229 | GFP_KERNEL); |
| 230 | if (tmdev == NULL) { |
| 231 | pr_err("%s: kzalloc() failed.\n", __func__); |
| 232 | return -ENOMEM; |
| 233 | } |
| 234 | |
| 235 | tmdev->num_sensors = tsens_num_sensors; |
| 236 | |
| 237 | rc = get_device_tree_data(pdev, tmdev); |
| 238 | if (rc) { |
| 239 | pr_err("Error reading TSENS DT\n"); |
| 240 | return rc; |
| 241 | } |
| 242 | |
| 243 | rc = tsens_init(tmdev); |
| 244 | if (rc) |
| 245 | return rc; |
| 246 | |
| 247 | rc = tsens_thermal_zone_register(tmdev); |
| 248 | if (rc) { |
| 249 | pr_err("Error registering the thermal zone\n"); |
| 250 | return rc; |
| 251 | } |
| 252 | |
| 253 | rc = tsens_register_interrupts(tmdev); |
| 254 | if (rc < 0) { |
| 255 | pr_err("TSENS interrupt register failed:%d\n", rc); |
| 256 | return rc; |
| 257 | } |
| 258 | |
| 259 | list_add_tail(&tmdev->list, &tsens_device_list); |
| 260 | platform_set_drvdata(pdev, tmdev); |
| 261 | |
| 262 | return rc; |
| 263 | } |
| 264 | |
| 265 | static struct platform_driver tsens_tm_driver = { |
| 266 | .probe = tsens_tm_probe, |
| 267 | .remove = tsens_tm_remove, |
| 268 | .driver = { |
| 269 | .name = "msm-tsens", |
| 270 | .owner = THIS_MODULE, |
| 271 | .of_match_table = tsens_table, |
| 272 | }, |
| 273 | }; |
| 274 | |
| 275 | int __init tsens_tm_init_driver(void) |
| 276 | { |
| 277 | return platform_driver_register(&tsens_tm_driver); |
| 278 | } |
| 279 | subsys_initcall(tsens_tm_init_driver); |
| 280 | |
| 281 | static void __exit tsens_tm_deinit(void) |
| 282 | { |
| 283 | platform_driver_unregister(&tsens_tm_driver); |
| 284 | } |
| 285 | module_exit(tsens_tm_deinit); |
| 286 | |
| 287 | MODULE_ALIAS("platform:" TSENS_DRIVER_NAME); |
| 288 | MODULE_LICENSE("GPL v2"); |