blob: f9abeeeead9e966dd8c8df7d225b81788eb67e40 [file] [log] [blame]
Donggeun Kimf22aaaa72011-06-20 16:48:19 +09001/*
2 * ntc_thermistor.c - NTC Thermistors
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <linux/slab.h>
24#include <linux/module.h>
25#include <linux/pm_runtime.h>
26#include <linux/math64.h>
27#include <linux/platform_device.h>
28#include <linux/err.h>
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +053029#include <linux/of.h>
30#include <linux/of_device.h>
Donggeun Kimf22aaaa72011-06-20 16:48:19 +090031
32#include <linux/platform_data/ntc_thermistor.h>
33
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +053034#include <linux/iio/iio.h>
35#include <linux/iio/machine.h>
36#include <linux/iio/driver.h>
37#include <linux/iio/consumer.h>
38
Donggeun Kimf22aaaa72011-06-20 16:48:19 +090039#include <linux/hwmon.h>
Donggeun Kimf22aaaa72011-06-20 16:48:19 +090040
41struct ntc_compensation {
Guenter Roeck088ce2a2013-03-13 16:40:39 -070042 int temp_c;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +090043 unsigned int ohm;
44};
45
Peter Rosine056fe22018-11-21 16:03:39 +000046/*
47 * Used as index in a zero-terminated array, holes not allowed so
48 * that NTC_LAST is the first empty array entry.
49 */
50enum {
51 NTC_B57330V2103,
52 NTC_B57891S0103,
53 NTC_NCP03WB473,
54 NTC_NCP03WF104,
55 NTC_NCP15WB473,
56 NTC_NCP15WL333,
57 NTC_NCP15XH103,
58 NTC_NCP18WB473,
59 NTC_NCP21WB473,
60 NTC_LAST,
61};
62
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +053063static const struct platform_device_id ntc_thermistor_id[] = {
Peter Rosine056fe22018-11-21 16:03:39 +000064 [NTC_B57330V2103] = { "b57330v2103", TYPE_B57330V2103 },
65 [NTC_B57891S0103] = { "b57891s0103", TYPE_B57891S0103 },
66 [NTC_NCP03WB473] = { "ncp03wb473", TYPE_NCPXXWB473 },
67 [NTC_NCP03WF104] = { "ncp03wf104", TYPE_NCPXXWF104 },
68 [NTC_NCP15WB473] = { "ncp15wb473", TYPE_NCPXXWB473 },
69 [NTC_NCP15WL333] = { "ncp15wl333", TYPE_NCPXXWL333 },
70 [NTC_NCP15XH103] = { "ncp15xh103", TYPE_NCPXXXH103 },
71 [NTC_NCP18WB473] = { "ncp18wb473", TYPE_NCPXXWB473 },
72 [NTC_NCP21WB473] = { "ncp21wb473", TYPE_NCPXXWB473 },
73 [NTC_LAST] = { },
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +053074};
75
Donggeun Kimf22aaaa72011-06-20 16:48:19 +090076/*
77 * A compensation table should be sorted by the values of .ohm
78 * in descending order.
79 * The following compensation tables are from the specification of Murata NTC
80 * Thermistors Datasheet
81 */
Sachin Kamat4626dcf2013-02-19 15:07:34 +053082static const struct ntc_compensation ncpXXwb473[] = {
Guenter Roeck088ce2a2013-03-13 16:40:39 -070083 { .temp_c = -40, .ohm = 1747920 },
84 { .temp_c = -35, .ohm = 1245428 },
85 { .temp_c = -30, .ohm = 898485 },
86 { .temp_c = -25, .ohm = 655802 },
87 { .temp_c = -20, .ohm = 483954 },
88 { .temp_c = -15, .ohm = 360850 },
89 { .temp_c = -10, .ohm = 271697 },
90 { .temp_c = -5, .ohm = 206463 },
91 { .temp_c = 0, .ohm = 158214 },
92 { .temp_c = 5, .ohm = 122259 },
93 { .temp_c = 10, .ohm = 95227 },
94 { .temp_c = 15, .ohm = 74730 },
95 { .temp_c = 20, .ohm = 59065 },
96 { .temp_c = 25, .ohm = 47000 },
97 { .temp_c = 30, .ohm = 37643 },
98 { .temp_c = 35, .ohm = 30334 },
99 { .temp_c = 40, .ohm = 24591 },
100 { .temp_c = 45, .ohm = 20048 },
101 { .temp_c = 50, .ohm = 16433 },
102 { .temp_c = 55, .ohm = 13539 },
103 { .temp_c = 60, .ohm = 11209 },
104 { .temp_c = 65, .ohm = 9328 },
105 { .temp_c = 70, .ohm = 7798 },
106 { .temp_c = 75, .ohm = 6544 },
107 { .temp_c = 80, .ohm = 5518 },
108 { .temp_c = 85, .ohm = 4674 },
109 { .temp_c = 90, .ohm = 3972 },
110 { .temp_c = 95, .ohm = 3388 },
111 { .temp_c = 100, .ohm = 2902 },
112 { .temp_c = 105, .ohm = 2494 },
113 { .temp_c = 110, .ohm = 2150 },
114 { .temp_c = 115, .ohm = 1860 },
115 { .temp_c = 120, .ohm = 1615 },
116 { .temp_c = 125, .ohm = 1406 },
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900117};
Sachin Kamat4626dcf2013-02-19 15:07:34 +0530118static const struct ntc_compensation ncpXXwl333[] = {
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700119 { .temp_c = -40, .ohm = 1610154 },
120 { .temp_c = -35, .ohm = 1130850 },
121 { .temp_c = -30, .ohm = 802609 },
122 { .temp_c = -25, .ohm = 575385 },
123 { .temp_c = -20, .ohm = 416464 },
124 { .temp_c = -15, .ohm = 304219 },
125 { .temp_c = -10, .ohm = 224193 },
126 { .temp_c = -5, .ohm = 166623 },
127 { .temp_c = 0, .ohm = 124850 },
128 { .temp_c = 5, .ohm = 94287 },
129 { .temp_c = 10, .ohm = 71747 },
130 { .temp_c = 15, .ohm = 54996 },
131 { .temp_c = 20, .ohm = 42455 },
132 { .temp_c = 25, .ohm = 33000 },
133 { .temp_c = 30, .ohm = 25822 },
134 { .temp_c = 35, .ohm = 20335 },
135 { .temp_c = 40, .ohm = 16115 },
136 { .temp_c = 45, .ohm = 12849 },
137 { .temp_c = 50, .ohm = 10306 },
138 { .temp_c = 55, .ohm = 8314 },
139 { .temp_c = 60, .ohm = 6746 },
140 { .temp_c = 65, .ohm = 5503 },
141 { .temp_c = 70, .ohm = 4513 },
142 { .temp_c = 75, .ohm = 3721 },
143 { .temp_c = 80, .ohm = 3084 },
144 { .temp_c = 85, .ohm = 2569 },
145 { .temp_c = 90, .ohm = 2151 },
146 { .temp_c = 95, .ohm = 1809 },
147 { .temp_c = 100, .ohm = 1529 },
148 { .temp_c = 105, .ohm = 1299 },
149 { .temp_c = 110, .ohm = 1108 },
150 { .temp_c = 115, .ohm = 949 },
151 { .temp_c = 120, .ohm = 817 },
152 { .temp_c = 125, .ohm = 707 },
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900153};
154
Beomho Seo887ee432015-04-30 13:07:43 +0900155static const struct ntc_compensation ncpXXwf104[] = {
156 { .temp_c = -40, .ohm = 4397119 },
157 { .temp_c = -35, .ohm = 3088599 },
158 { .temp_c = -30, .ohm = 2197225 },
159 { .temp_c = -25, .ohm = 1581881 },
160 { .temp_c = -20, .ohm = 1151037 },
161 { .temp_c = -15, .ohm = 846579 },
162 { .temp_c = -10, .ohm = 628988 },
163 { .temp_c = -5, .ohm = 471632 },
164 { .temp_c = 0, .ohm = 357012 },
165 { .temp_c = 5, .ohm = 272500 },
166 { .temp_c = 10, .ohm = 209710 },
167 { .temp_c = 15, .ohm = 162651 },
168 { .temp_c = 20, .ohm = 127080 },
169 { .temp_c = 25, .ohm = 100000 },
170 { .temp_c = 30, .ohm = 79222 },
171 { .temp_c = 35, .ohm = 63167 },
172 { .temp_c = 40, .ohm = 50677 },
173 { .temp_c = 45, .ohm = 40904 },
174 { .temp_c = 50, .ohm = 33195 },
175 { .temp_c = 55, .ohm = 27091 },
176 { .temp_c = 60, .ohm = 22224 },
177 { .temp_c = 65, .ohm = 18323 },
178 { .temp_c = 70, .ohm = 15184 },
179 { .temp_c = 75, .ohm = 12635 },
180 { .temp_c = 80, .ohm = 10566 },
181 { .temp_c = 85, .ohm = 8873 },
182 { .temp_c = 90, .ohm = 7481 },
183 { .temp_c = 95, .ohm = 6337 },
184 { .temp_c = 100, .ohm = 5384 },
185 { .temp_c = 105, .ohm = 4594 },
186 { .temp_c = 110, .ohm = 3934 },
187 { .temp_c = 115, .ohm = 3380 },
188 { .temp_c = 120, .ohm = 2916 },
189 { .temp_c = 125, .ohm = 2522 },
190};
191
Joseph McNally54ce3a02016-02-28 22:31:23 +0000192static const struct ntc_compensation ncpXXxh103[] = {
193 { .temp_c = -40, .ohm = 247565 },
194 { .temp_c = -35, .ohm = 181742 },
195 { .temp_c = -30, .ohm = 135128 },
196 { .temp_c = -25, .ohm = 101678 },
197 { .temp_c = -20, .ohm = 77373 },
198 { .temp_c = -15, .ohm = 59504 },
199 { .temp_c = -10, .ohm = 46222 },
200 { .temp_c = -5, .ohm = 36244 },
201 { .temp_c = 0, .ohm = 28674 },
202 { .temp_c = 5, .ohm = 22878 },
203 { .temp_c = 10, .ohm = 18399 },
204 { .temp_c = 15, .ohm = 14910 },
205 { .temp_c = 20, .ohm = 12169 },
206 { .temp_c = 25, .ohm = 10000 },
207 { .temp_c = 30, .ohm = 8271 },
208 { .temp_c = 35, .ohm = 6883 },
209 { .temp_c = 40, .ohm = 5762 },
210 { .temp_c = 45, .ohm = 4851 },
211 { .temp_c = 50, .ohm = 4105 },
212 { .temp_c = 55, .ohm = 3492 },
213 { .temp_c = 60, .ohm = 2985 },
214 { .temp_c = 65, .ohm = 2563 },
215 { .temp_c = 70, .ohm = 2211 },
216 { .temp_c = 75, .ohm = 1915 },
217 { .temp_c = 80, .ohm = 1666 },
218 { .temp_c = 85, .ohm = 1454 },
219 { .temp_c = 90, .ohm = 1275 },
220 { .temp_c = 95, .ohm = 1121 },
221 { .temp_c = 100, .ohm = 990 },
222 { .temp_c = 105, .ohm = 876 },
223 { .temp_c = 110, .ohm = 779 },
224 { .temp_c = 115, .ohm = 694 },
225 { .temp_c = 120, .ohm = 620 },
226 { .temp_c = 125, .ohm = 556 },
227};
228
Johannes Pointnered67f082014-07-01 08:05:52 +0200229/*
Peter Rosine8fda2c2018-11-17 12:13:00 +0000230 * The following compensation tables are from the specifications in EPCOS NTC
231 * Thermistors Datasheets
Johannes Pointnered67f082014-07-01 08:05:52 +0200232 */
233static const struct ntc_compensation b57330v2103[] = {
234 { .temp_c = -40, .ohm = 190030 },
235 { .temp_c = -35, .ohm = 145360 },
236 { .temp_c = -30, .ohm = 112060 },
237 { .temp_c = -25, .ohm = 87041 },
238 { .temp_c = -20, .ohm = 68104 },
239 { .temp_c = -15, .ohm = 53665 },
240 { .temp_c = -10, .ohm = 42576 },
241 { .temp_c = -5, .ohm = 34001 },
242 { .temp_c = 0, .ohm = 27326 },
243 { .temp_c = 5, .ohm = 22096 },
244 { .temp_c = 10, .ohm = 17973 },
245 { .temp_c = 15, .ohm = 14703 },
246 { .temp_c = 20, .ohm = 12090 },
247 { .temp_c = 25, .ohm = 10000 },
248 { .temp_c = 30, .ohm = 8311 },
249 { .temp_c = 35, .ohm = 6941 },
250 { .temp_c = 40, .ohm = 5825 },
251 { .temp_c = 45, .ohm = 4911 },
252 { .temp_c = 50, .ohm = 4158 },
253 { .temp_c = 55, .ohm = 3536 },
254 { .temp_c = 60, .ohm = 3019 },
255 { .temp_c = 65, .ohm = 2588 },
256 { .temp_c = 70, .ohm = 2227 },
257 { .temp_c = 75, .ohm = 1924 },
258 { .temp_c = 80, .ohm = 1668 },
259 { .temp_c = 85, .ohm = 1451 },
260 { .temp_c = 90, .ohm = 1266 },
261 { .temp_c = 95, .ohm = 1108 },
262 { .temp_c = 100, .ohm = 973 },
263 { .temp_c = 105, .ohm = 857 },
264 { .temp_c = 110, .ohm = 757 },
265 { .temp_c = 115, .ohm = 671 },
266 { .temp_c = 120, .ohm = 596 },
267 { .temp_c = 125, .ohm = 531 },
268};
269
Peter Rosine8fda2c2018-11-17 12:13:00 +0000270static const struct ntc_compensation b57891s0103[] = {
271 { .temp_c = -55.0, .ohm = 878900 },
272 { .temp_c = -50.0, .ohm = 617590 },
273 { .temp_c = -45.0, .ohm = 439340 },
274 { .temp_c = -40.0, .ohm = 316180 },
275 { .temp_c = -35.0, .ohm = 230060 },
276 { .temp_c = -30.0, .ohm = 169150 },
277 { .temp_c = -25.0, .ohm = 125550 },
278 { .temp_c = -20.0, .ohm = 94143 },
279 { .temp_c = -15.0, .ohm = 71172 },
280 { .temp_c = -10.0, .ohm = 54308 },
281 { .temp_c = -5.0, .ohm = 41505 },
282 { .temp_c = 0.0, .ohm = 32014 },
283 { .temp_c = 5.0, .ohm = 25011 },
284 { .temp_c = 10.0, .ohm = 19691 },
285 { .temp_c = 15.0, .ohm = 15618 },
286 { .temp_c = 20.0, .ohm = 12474 },
287 { .temp_c = 25.0, .ohm = 10000 },
288 { .temp_c = 30.0, .ohm = 8080 },
289 { .temp_c = 35.0, .ohm = 6569 },
290 { .temp_c = 40.0, .ohm = 5372 },
291 { .temp_c = 45.0, .ohm = 4424 },
292 { .temp_c = 50.0, .ohm = 3661 },
293 { .temp_c = 55.0, .ohm = 3039 },
294 { .temp_c = 60.0, .ohm = 2536 },
295 { .temp_c = 65.0, .ohm = 2128 },
296 { .temp_c = 70.0, .ohm = 1794 },
297 { .temp_c = 75.0, .ohm = 1518 },
298 { .temp_c = 80.0, .ohm = 1290 },
299 { .temp_c = 85.0, .ohm = 1100 },
300 { .temp_c = 90.0, .ohm = 942 },
301 { .temp_c = 95.0, .ohm = 809 },
302 { .temp_c = 100.0, .ohm = 697 },
303 { .temp_c = 105.0, .ohm = 604 },
304 { .temp_c = 110.0, .ohm = 525 },
305 { .temp_c = 115.0, .ohm = 457 },
306 { .temp_c = 120.0, .ohm = 400 },
307 { .temp_c = 125.0, .ohm = 351 },
308 { .temp_c = 130.0, .ohm = 308 },
309 { .temp_c = 135.0, .ohm = 272 },
310 { .temp_c = 140.0, .ohm = 240 },
311 { .temp_c = 145.0, .ohm = 213 },
312 { .temp_c = 150.0, .ohm = 189 },
313 { .temp_c = 155.0, .ohm = 168 },
314};
315
Peter Rosin737c0862018-11-21 16:03:46 +0000316struct ntc_type {
317 const struct ntc_compensation *comp;
318 int n_comp;
319};
320
321#define NTC_TYPE(ntc, compensation) \
322[(ntc)] = { .comp = (compensation), .n_comp = ARRAY_SIZE(compensation) }
323
324static const struct ntc_type ntc_type[] = {
325 NTC_TYPE(TYPE_B57330V2103, b57330v2103),
326 NTC_TYPE(TYPE_B57891S0103, b57891s0103),
327 NTC_TYPE(TYPE_NCPXXWB473, ncpXXwb473),
328 NTC_TYPE(TYPE_NCPXXWF104, ncpXXwf104),
329 NTC_TYPE(TYPE_NCPXXWL333, ncpXXwl333),
330 NTC_TYPE(TYPE_NCPXXXH103, ncpXXxh103),
331};
332
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900333struct ntc_data {
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900334 struct ntc_thermistor_platform_data *pdata;
335 const struct ntc_compensation *comp;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900336 int n_comp;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900337};
338
Jean Delvare59cf4242014-05-25 17:23:08 +0200339#if defined(CONFIG_OF) && IS_ENABLED(CONFIG_IIO)
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530340static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata)
341{
342 struct iio_channel *channel = pdata->chan;
Chris Lesiak03152532015-06-01 11:27:37 -0500343 int raw, uv, ret;
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530344
Chris Lesiak03152532015-06-01 11:27:37 -0500345 ret = iio_read_channel_raw(channel, &raw);
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530346 if (ret < 0) {
347 pr_err("read channel() error: %d\n", ret);
348 return ret;
349 }
350
Chris Lesiak03152532015-06-01 11:27:37 -0500351 ret = iio_convert_raw_to_processed(channel, raw, &uv, 1000);
352 if (ret < 0) {
353 /* Assume 12 bit ADC with vref at pullup_uv */
354 uv = (pdata->pullup_uv * (s64)raw) >> 12;
355 }
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530356
Chris Lesiak03152532015-06-01 11:27:37 -0500357 return uv;
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530358}
359
360static const struct of_device_id ntc_match[] = {
Johannes Pointnered67f082014-07-01 08:05:52 +0200361 { .compatible = "epcos,b57330v2103",
Peter Rosine056fe22018-11-21 16:03:39 +0000362 .data = &ntc_thermistor_id[NTC_B57330V2103]},
Peter Rosine8fda2c2018-11-17 12:13:00 +0000363 { .compatible = "epcos,b57891s0103",
Peter Rosine056fe22018-11-21 16:03:39 +0000364 .data = &ntc_thermistor_id[NTC_B57891S0103] },
365 { .compatible = "murata,ncp03wb473",
366 .data = &ntc_thermistor_id[NTC_NCP03WB473] },
367 { .compatible = "murata,ncp03wf104",
368 .data = &ntc_thermistor_id[NTC_NCP03WF104] },
369 { .compatible = "murata,ncp15wb473",
370 .data = &ntc_thermistor_id[NTC_NCP15WB473] },
371 { .compatible = "murata,ncp15wl333",
372 .data = &ntc_thermistor_id[NTC_NCP15WL333] },
373 { .compatible = "murata,ncp15xh103",
374 .data = &ntc_thermistor_id[NTC_NCP15XH103] },
375 { .compatible = "murata,ncp18wb473",
376 .data = &ntc_thermistor_id[NTC_NCP18WB473] },
377 { .compatible = "murata,ncp21wb473",
378 .data = &ntc_thermistor_id[NTC_NCP21WB473] },
Naveen Krishna Chatradhi8b6f5e02014-06-25 11:59:31 +0530379
380 /* Usage of vendor name "ntc" is deprecated */
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530381 { .compatible = "ntc,ncp03wb473",
Peter Rosine056fe22018-11-21 16:03:39 +0000382 .data = &ntc_thermistor_id[NTC_NCP03WB473] },
383 { .compatible = "ntc,ncp15wb473",
384 .data = &ntc_thermistor_id[NTC_NCP15WB473] },
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530385 { .compatible = "ntc,ncp15wl333",
Peter Rosine056fe22018-11-21 16:03:39 +0000386 .data = &ntc_thermistor_id[NTC_NCP15WL333] },
387 { .compatible = "ntc,ncp18wb473",
388 .data = &ntc_thermistor_id[NTC_NCP18WB473] },
389 { .compatible = "ntc,ncp21wb473",
390 .data = &ntc_thermistor_id[NTC_NCP21WB473] },
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530391 { },
392};
393MODULE_DEVICE_TABLE(of, ntc_match);
394
395static struct ntc_thermistor_platform_data *
Guenter Roeck48001522016-07-25 13:37:56 -0700396ntc_thermistor_parse_dt(struct device *dev)
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530397{
398 struct iio_channel *chan;
Chris Lesiakadba6572015-05-26 15:40:44 -0500399 enum iio_chan_type type;
Guenter Roeck48001522016-07-25 13:37:56 -0700400 struct device_node *np = dev->of_node;
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530401 struct ntc_thermistor_platform_data *pdata;
Chris Lesiakadba6572015-05-26 15:40:44 -0500402 int ret;
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530403
404 if (!np)
405 return NULL;
406
Guenter Roeck48001522016-07-25 13:37:56 -0700407 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530408 if (!pdata)
409 return ERR_PTR(-ENOMEM);
410
Guenter Roeck48001522016-07-25 13:37:56 -0700411 chan = devm_iio_channel_get(dev, NULL);
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530412 if (IS_ERR(chan))
413 return ERR_CAST(chan);
414
Chris Lesiakadba6572015-05-26 15:40:44 -0500415 ret = iio_get_channel_type(chan, &type);
416 if (ret < 0)
417 return ERR_PTR(ret);
418
419 if (type != IIO_VOLTAGE)
420 return ERR_PTR(-EINVAL);
421
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700422 if (of_property_read_u32(np, "pullup-uv", &pdata->pullup_uv))
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530423 return ERR_PTR(-ENODEV);
424 if (of_property_read_u32(np, "pullup-ohm", &pdata->pullup_ohm))
425 return ERR_PTR(-ENODEV);
426 if (of_property_read_u32(np, "pulldown-ohm", &pdata->pulldown_ohm))
427 return ERR_PTR(-ENODEV);
428
429 if (of_find_property(np, "connected-positive", NULL))
430 pdata->connect = NTC_CONNECTED_POSITIVE;
431 else /* status change should be possible if not always on. */
432 pdata->connect = NTC_CONNECTED_GROUND;
433
434 pdata->chan = chan;
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700435 pdata->read_uv = ntc_adc_iio_read;
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530436
437 return pdata;
438}
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530439#else
440static struct ntc_thermistor_platform_data *
Guenter Roeck48001522016-07-25 13:37:56 -0700441ntc_thermistor_parse_dt(struct device *dev)
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530442{
443 return NULL;
444}
445
Jean Delvare59cf4242014-05-25 17:23:08 +0200446#define ntc_match NULL
447
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530448#endif
449
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900450static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
451{
452 if (divisor == 0 && dividend == 0)
453 return 0;
454 if (divisor == 0)
455 return UINT_MAX;
456 return div64_u64(dividend, divisor);
457}
458
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700459static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900460{
461 struct ntc_thermistor_platform_data *pdata = data->pdata;
Chris Lesiakf6725ae2015-06-02 15:57:58 -0500462 u32 puv = pdata->pullup_uv;
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700463 u64 n, puo, pdo;
464 puo = pdata->pullup_ohm;
465 pdo = pdata->pulldown_ohm;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900466
Chris Lesiakf6725ae2015-06-02 15:57:58 -0500467 if (uv == 0)
468 return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
469 INT_MAX : 0;
470 if (uv >= puv)
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900471 return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700472 0 : INT_MAX;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900473
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700474 if (pdata->connect == NTC_CONNECTED_POSITIVE && puo == 0)
Chris Lesiakf6725ae2015-06-02 15:57:58 -0500475 n = div_u64(pdo * (puv - uv), uv);
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700476 else if (pdata->connect == NTC_CONNECTED_GROUND && pdo == 0)
Chris Lesiakf6725ae2015-06-02 15:57:58 -0500477 n = div_u64(puo * uv, puv - uv);
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900478 else if (pdata->connect == NTC_CONNECTED_POSITIVE)
Chris Lesiakf6725ae2015-06-02 15:57:58 -0500479 n = div64_u64_safe(pdo * puo * (puv - uv),
480 puo * uv - pdo * (puv - uv));
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900481 else
Chris Lesiakf6725ae2015-06-02 15:57:58 -0500482 n = div64_u64_safe(pdo * puo * uv, pdo * (puv - uv) - puo * uv);
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900483
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700484 if (n > INT_MAX)
485 n = INT_MAX;
486 return n;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900487}
488
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700489static void lookup_comp(struct ntc_data *data, unsigned int ohm,
490 int *i_low, int *i_high)
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900491{
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700492 int start, end, mid;
493
494 /*
495 * Handle special cases: Resistance is higher than or equal to
496 * resistance in first table entry, or resistance is lower or equal
497 * to resistance in last table entry.
498 * In these cases, return i_low == i_high, either pointing to the
499 * beginning or to the end of the table depending on the condition.
500 */
501 if (ohm >= data->comp[0].ohm) {
502 *i_low = 0;
503 *i_high = 0;
504 return;
505 }
506 if (ohm <= data->comp[data->n_comp - 1].ohm) {
507 *i_low = data->n_comp - 1;
508 *i_high = data->n_comp - 1;
509 return;
510 }
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900511
512 /* Do a binary search on compensation table */
513 start = 0;
514 end = data->n_comp;
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700515 while (start < end) {
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900516 mid = start + (end - start) / 2;
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700517 /*
518 * start <= mid < end
519 * data->comp[start].ohm > ohm >= data->comp[end].ohm
520 *
521 * We could check for "ohm == data->comp[mid].ohm" here, but
522 * that is a quite unlikely condition, and we would have to
523 * check again after updating start. Check it at the end instead
524 * for simplicity.
525 */
526 if (ohm >= data->comp[mid].ohm) {
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900527 end = mid;
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700528 } else {
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900529 start = mid + 1;
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700530 /*
531 * ohm >= data->comp[start].ohm might be true here,
532 * since we set start to mid + 1. In that case, we are
533 * done. We could keep going, but the condition is quite
534 * likely to occur, so it is worth checking for it.
535 */
536 if (ohm >= data->comp[start].ohm)
537 end = start;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900538 }
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700539 /*
540 * start <= end
541 * data->comp[start].ohm >= ohm >= data->comp[end].ohm
542 */
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900543 }
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700544 /*
545 * start == end
546 * ohm >= data->comp[end].ohm
547 */
548 *i_low = end;
549 if (ohm == data->comp[end].ohm)
550 *i_high = end;
551 else
552 *i_high = end - 1;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900553}
554
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700555static int get_temp_mc(struct ntc_data *data, unsigned int ohm)
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900556{
557 int low, high;
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700558 int temp;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900559
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700560 lookup_comp(data, ohm, &low, &high);
561 if (low == high) {
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900562 /* Unable to use linear approximation */
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700563 temp = data->comp[low].temp_c * 1000;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900564 } else {
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700565 temp = data->comp[low].temp_c * 1000 +
566 ((data->comp[high].temp_c - data->comp[low].temp_c) *
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900567 1000 * ((int)ohm - (int)data->comp[low].ohm)) /
568 ((int)data->comp[high].ohm - (int)data->comp[low].ohm);
569 }
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700570 return temp;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900571}
572
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700573static int ntc_thermistor_get_ohm(struct ntc_data *data)
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900574{
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700575 int read_uv;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900576
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700577 if (data->pdata->read_ohm)
578 return data->pdata->read_ohm();
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900579
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700580 if (data->pdata->read_uv) {
581 read_uv = data->pdata->read_uv(data->pdata);
582 if (read_uv < 0)
583 return read_uv;
584 return get_ohm_of_thermistor(data, read_uv);
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900585 }
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700586 return -EINVAL;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900587}
588
Guenter Roeck7cc7de92019-02-12 09:55:16 -0800589static int ntc_read(struct device *dev, enum hwmon_sensor_types type,
590 u32 attr, int channel, long *val)
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900591{
592 struct ntc_data *data = dev_get_drvdata(dev);
Guenter Roeckdbe43a62012-04-22 20:58:51 -0700593 int ohm;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900594
Guenter Roeck7cc7de92019-02-12 09:55:16 -0800595 switch (type) {
596 case hwmon_temp:
597 switch (attr) {
598 case hwmon_temp_input:
599 ohm = ntc_thermistor_get_ohm(data);
600 if (ohm < 0)
601 return ohm;
602 *val = get_temp_mc(data, ohm);
603 return 0;
604 case hwmon_temp_type:
605 *val = 4;
606 return 0;
607 default:
608 break;
609 }
610 break;
611 default:
612 break;
613 }
614 return -EINVAL;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900615}
616
Guenter Roeck7cc7de92019-02-12 09:55:16 -0800617static umode_t ntc_is_visible(const void *data, enum hwmon_sensor_types type,
618 u32 attr, int channel)
619{
620 if (type == hwmon_temp) {
621 switch (attr) {
622 case hwmon_temp_input:
623 case hwmon_temp_type:
624 return 0444;
625 default:
626 break;
627 }
628 }
629 return 0;
630}
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900631
Guenter Roeck7cc7de92019-02-12 09:55:16 -0800632static const u32 ntc_chip_config[] = {
633 HWMON_C_REGISTER_TZ,
634 0
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900635};
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900636
Guenter Roeck7cc7de92019-02-12 09:55:16 -0800637static const struct hwmon_channel_info ntc_chip = {
638 .type = hwmon_chip,
639 .config = ntc_chip_config,
640};
641
642static const u32 ntc_temp_config[] = {
Guenter Roeck5fd43dd2019-03-20 10:32:58 -0700643 HWMON_T_INPUT | HWMON_T_TYPE,
Guenter Roeck7cc7de92019-02-12 09:55:16 -0800644 0
645};
646
647static const struct hwmon_channel_info ntc_temp = {
648 .type = hwmon_temp,
649 .config = ntc_temp_config,
650};
651
652static const struct hwmon_channel_info *ntc_info[] = {
653 &ntc_chip,
654 &ntc_temp,
655 NULL
656};
657
658static const struct hwmon_ops ntc_hwmon_ops = {
659 .is_visible = ntc_is_visible,
660 .read = ntc_read,
661};
662
663static const struct hwmon_chip_info ntc_chip_info = {
664 .ops = &ntc_hwmon_ops,
665 .info = ntc_info,
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400666};
667
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500668static int ntc_thermistor_probe(struct platform_device *pdev)
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900669{
Guenter Roeck48001522016-07-25 13:37:56 -0700670 struct device *dev = &pdev->dev;
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530671 const struct of_device_id *of_id =
Guenter Roeck48001522016-07-25 13:37:56 -0700672 of_match_device(of_match_ptr(ntc_match), dev);
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530673 const struct platform_device_id *pdev_id;
674 struct ntc_thermistor_platform_data *pdata;
Guenter Roeck5e7f5992016-07-25 13:45:30 -0700675 struct device *hwmon_dev;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900676 struct ntc_data *data;
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530677
Guenter Roeck48001522016-07-25 13:37:56 -0700678 pdata = ntc_thermistor_parse_dt(dev);
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530679 if (IS_ERR(pdata))
680 return PTR_ERR(pdata);
681 else if (pdata == NULL)
Guenter Roeck48001522016-07-25 13:37:56 -0700682 pdata = dev_get_platdata(dev);
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900683
684 if (!pdata) {
Guenter Roeck48001522016-07-25 13:37:56 -0700685 dev_err(dev, "No platform init data supplied.\n");
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900686 return -ENODEV;
687 }
688
689 /* Either one of the two is required. */
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700690 if (!pdata->read_uv && !pdata->read_ohm) {
Guenter Roeck48001522016-07-25 13:37:56 -0700691 dev_err(dev,
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700692 "Both read_uv and read_ohm missing. Need either one of the two.\n");
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900693 return -EINVAL;
694 }
695
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700696 if (pdata->read_uv && pdata->read_ohm) {
Guenter Roeck48001522016-07-25 13:37:56 -0700697 dev_warn(dev,
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700698 "Only one of read_uv and read_ohm is needed; ignoring read_uv.\n");
699 pdata->read_uv = NULL;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900700 }
701
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700702 if (pdata->read_uv && (pdata->pullup_uv == 0 ||
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900703 (pdata->pullup_ohm == 0 && pdata->connect ==
704 NTC_CONNECTED_GROUND) ||
705 (pdata->pulldown_ohm == 0 && pdata->connect ==
706 NTC_CONNECTED_POSITIVE) ||
707 (pdata->connect != NTC_CONNECTED_POSITIVE &&
708 pdata->connect != NTC_CONNECTED_GROUND))) {
Guenter Roeck48001522016-07-25 13:37:56 -0700709 dev_err(dev, "Required data to use read_uv not supplied.\n");
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900710 return -EINVAL;
711 }
712
Guenter Roeck48001522016-07-25 13:37:56 -0700713 data = devm_kzalloc(dev, sizeof(struct ntc_data), GFP_KERNEL);
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900714 if (!data)
715 return -ENOMEM;
716
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530717 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
718
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900719 data->pdata = pdata;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900720
Peter Rosin737c0862018-11-21 16:03:46 +0000721 if (pdev_id->driver_data >= ARRAY_SIZE(ntc_type)) {
Guenter Roeck48001522016-07-25 13:37:56 -0700722 dev_err(dev, "Unknown device type: %lu(%s)\n",
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530723 pdev_id->driver_data, pdev_id->name);
Guenter Roeck41141e62012-04-23 10:39:00 -0700724 return -EINVAL;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900725 }
726
Peter Rosin737c0862018-11-21 16:03:46 +0000727 data->comp = ntc_type[pdev_id->driver_data].comp;
728 data->n_comp = ntc_type[pdev_id->driver_data].n_comp;
729
Guenter Roeck7cc7de92019-02-12 09:55:16 -0800730 hwmon_dev = devm_hwmon_device_register_with_info(dev, pdev_id->name,
731 data, &ntc_chip_info,
732 NULL);
Guenter Roeck5e7f5992016-07-25 13:45:30 -0700733 if (IS_ERR(hwmon_dev)) {
Guenter Roeck48001522016-07-25 13:37:56 -0700734 dev_err(dev, "unable to register as hwmon device.\n");
Guenter Roeck5e7f5992016-07-25 13:45:30 -0700735 return PTR_ERR(hwmon_dev);
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900736 }
737
Guenter Roeck48001522016-07-25 13:37:56 -0700738 dev_info(dev, "Thermistor type: %s successfully probed.\n",
739 pdev_id->name);
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530740
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900741 return 0;
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900742}
743
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900744static struct platform_driver ntc_thermistor_driver = {
745 .driver = {
746 .name = "ntc-thermistor",
Naveen Krishna Chatradhi9e8269d2013-03-13 09:38:20 +0530747 .of_match_table = of_match_ptr(ntc_match),
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900748 },
749 .probe = ntc_thermistor_probe,
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900750 .id_table = ntc_thermistor_id,
751};
752
Axel Lin25a236a2011-11-25 02:31:00 -0500753module_platform_driver(ntc_thermistor_driver);
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900754
Johannes Pointnered67f082014-07-01 08:05:52 +0200755MODULE_DESCRIPTION("NTC Thermistor Driver");
Donggeun Kimf22aaaa72011-06-20 16:48:19 +0900756MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
757MODULE_LICENSE("GPL");
758MODULE_ALIAS("platform:ntc-thermistor");