defconfig: exynos9610: Re-add dropped Wi-Fi AP options lost
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / thermal / thermal-generic-adc.c
CommitLineData
b3aef78f
LD
1/*
2 * Generic ADC thermal driver
3 *
4 * Copyright (C) 2016 NVIDIA CORPORATION. All rights reserved.
5 *
6 * Author: Laxman Dewangan <ldewangan@nvidia.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#include <linux/iio/consumer.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17#include <linux/thermal.h>
18
19struct gadc_thermal_info {
20 struct device *dev;
21 struct thermal_zone_device *tz_dev;
22 struct iio_channel *channel;
23 s32 *lookup_table;
24 int nlookup_table;
25};
26
27static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val)
28{
919fc06b 29 int temp, temp_hi, temp_lo, adc_hi, adc_lo;
b3aef78f
LD
30 int i;
31
32 for (i = 0; i < gti->nlookup_table; i++) {
33 if (val >= gti->lookup_table[2 * i + 1])
34 break;
35 }
36
37 if (i == 0) {
38 temp = gti->lookup_table[0];
919fc06b 39 } else if (i >= gti->nlookup_table) {
b3aef78f
LD
40 temp = gti->lookup_table[2 * (gti->nlookup_table - 1)];
41 } else {
42 adc_hi = gti->lookup_table[2 * i - 1];
43 adc_lo = gti->lookup_table[2 * i + 1];
919fc06b
BA
44
45 temp_hi = gti->lookup_table[2 * i - 2];
46 temp_lo = gti->lookup_table[2 * i];
47
48 temp = temp_hi + mult_frac(temp_lo - temp_hi, val - adc_hi,
49 adc_lo - adc_hi);
b3aef78f
LD
50 }
51
52 return temp;
53}
54
55static int gadc_thermal_get_temp(void *data, int *temp)
56{
57 struct gadc_thermal_info *gti = data;
58 int val;
59 int ret;
60
61 ret = iio_read_channel_processed(gti->channel, &val);
62 if (ret < 0) {
63 dev_err(gti->dev, "IIO channel read failed %d\n", ret);
64 return ret;
65 }
66 *temp = gadc_thermal_adc_to_temp(gti, val);
67
68 return 0;
69}
70
71static const struct thermal_zone_of_device_ops gadc_thermal_ops = {
72 .get_temp = gadc_thermal_get_temp,
73};
74
75static int gadc_thermal_read_linear_lookup_table(struct device *dev,
76 struct gadc_thermal_info *gti)
77{
78 struct device_node *np = dev->of_node;
79 int ntable;
80 int ret;
81
82 ntable = of_property_count_elems_of_size(np, "temperature-lookup-table",
83 sizeof(u32));
84 if (ntable < 0) {
85 dev_err(dev, "Lookup table is not provided\n");
86 return ntable;
87 }
88
89 if (ntable % 2) {
90 dev_err(dev, "Pair of temperature vs ADC read value missing\n");
91 return -EINVAL;
92 }
93
94 gti->lookup_table = devm_kzalloc(dev, sizeof(*gti->lookup_table) *
95 ntable, GFP_KERNEL);
96 if (!gti->lookup_table)
97 return -ENOMEM;
98
99 ret = of_property_read_u32_array(np, "temperature-lookup-table",
100 (u32 *)gti->lookup_table, ntable);
101 if (ret < 0) {
102 dev_err(dev, "Failed to read temperature lookup table: %d\n",
103 ret);
104 return ret;
105 }
106
107 gti->nlookup_table = ntable / 2;
108
109 return 0;
110}
111
112static int gadc_thermal_probe(struct platform_device *pdev)
113{
114 struct gadc_thermal_info *gti;
115 int ret;
116
117 if (!pdev->dev.of_node) {
118 dev_err(&pdev->dev, "Only DT based supported\n");
119 return -ENODEV;
120 }
121
122 gti = devm_kzalloc(&pdev->dev, sizeof(*gti), GFP_KERNEL);
123 if (!gti)
124 return -ENOMEM;
125
126 ret = gadc_thermal_read_linear_lookup_table(&pdev->dev, gti);
127 if (ret < 0)
128 return ret;
129
130 gti->dev = &pdev->dev;
131 platform_set_drvdata(pdev, gti);
132
133 gti->channel = iio_channel_get(&pdev->dev, "sensor-channel");
134 if (IS_ERR(gti->channel)) {
135 ret = PTR_ERR(gti->channel);
136 dev_err(&pdev->dev, "IIO channel not found: %d\n", ret);
137 return ret;
138 }
139
140 gti->tz_dev = thermal_zone_of_sensor_register(&pdev->dev, 0,
141 gti, &gadc_thermal_ops);
142 if (IS_ERR(gti->tz_dev)) {
143 ret = PTR_ERR(gti->tz_dev);
144 dev_err(&pdev->dev, "Thermal zone sensor register failed: %d\n",
145 ret);
146 goto sensor_fail;
147 }
148
149 return 0;
150
151sensor_fail:
152 iio_channel_release(gti->channel);
153
154 return ret;
155}
156
157static int gadc_thermal_remove(struct platform_device *pdev)
158{
159 struct gadc_thermal_info *gti = platform_get_drvdata(pdev);
160
161 thermal_zone_of_sensor_unregister(&pdev->dev, gti->tz_dev);
162 iio_channel_release(gti->channel);
163
164 return 0;
165}
166
167static const struct of_device_id of_adc_thermal_match[] = {
168 { .compatible = "generic-adc-thermal", },
169 {},
170};
171MODULE_DEVICE_TABLE(of, of_adc_thermal_match);
172
173static struct platform_driver gadc_thermal_driver = {
174 .driver = {
175 .name = "generic-adc-thermal",
176 .of_match_table = of_adc_thermal_match,
177 },
178 .probe = gadc_thermal_probe,
179 .remove = gadc_thermal_remove,
180};
181
182module_platform_driver(gadc_thermal_driver);
183
184MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
185MODULE_DESCRIPTION("Generic ADC thermal driver using IIO framework with DT");
186MODULE_LICENSE("GPL v2");