1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ntc_thermistor.c - NTC Thermistors
4 *
5 * Copyright (C) 2010 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 */
8
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/math64.h>
12 #include <linux/platform_device.h>
13 #include <linux/property.h>
14 #include <linux/err.h>
15 #include <linux/fixp-arith.h>
16 #include <linux/iio/consumer.h>
17 #include <linux/hwmon.h>
18
19 enum ntc_thermistor_type {
20 TYPE_B57330V2103,
21 TYPE_B57891S0103,
22 TYPE_NCPXXWB473,
23 TYPE_NCPXXWF104,
24 TYPE_NCPXXWL333,
25 TYPE_NCPXXXH103,
26 TYPE_NCPXXWM474,
27 };
28
29 struct ntc_compensation {
30 int temp_c;
31 unsigned int ohm;
32 };
33
34 /*
35 * Used as index in a zero-terminated array, holes not allowed so
36 * that NTC_LAST is the first empty array entry.
37 */
38 enum {
39 NTC_B57330V2103,
40 NTC_B57891S0103,
41 NTC_NCP03WB473,
42 NTC_NCP03WF104,
43 NTC_NCP15WB473,
44 NTC_NCP15WL333,
45 NTC_NCP15XH103,
46 NTC_NCP18WB473,
47 NTC_NCP21WB473,
48 NTC_SSG1404001221,
49 NTC_NCP18WM474,
50 NTC_LAST,
51 };
52
53 static const struct platform_device_id ntc_thermistor_id[] = {
54 [NTC_B57330V2103] = { .name = "b57330v2103", .driver_data = TYPE_B57330V2103 },
55 [NTC_B57891S0103] = { .name = "b57891s0103", .driver_data = TYPE_B57891S0103 },
56 [NTC_NCP03WB473] = { .name = "ncp03wb473", .driver_data = TYPE_NCPXXWB473 },
57 [NTC_NCP03WF104] = { .name = "ncp03wf104", .driver_data = TYPE_NCPXXWF104 },
58 [NTC_NCP15WB473] = { .name = "ncp15wb473", .driver_data = TYPE_NCPXXWB473 },
59 [NTC_NCP15WL333] = { .name = "ncp15wl333", .driver_data = TYPE_NCPXXWL333 },
60 [NTC_NCP15XH103] = { .name = "ncp15xh103", .driver_data = TYPE_NCPXXXH103 },
61 [NTC_NCP18WB473] = { .name = "ncp18wb473", .driver_data = TYPE_NCPXXWB473 },
62 [NTC_NCP21WB473] = { .name = "ncp21wb473", .driver_data = TYPE_NCPXXWB473 },
63 [NTC_SSG1404001221] = { .name = "ssg1404_001221", .driver_data = TYPE_NCPXXWB473 },
64 [NTC_NCP18WM474] = { .name = "ncp18wm474", .driver_data = TYPE_NCPXXWM474 },
65 [NTC_LAST] = { }
66 };
67 MODULE_DEVICE_TABLE(platform, ntc_thermistor_id);
68
69 /*
70 * A compensation table should be sorted by the values of .ohm
71 * in descending order.
72 * The following compensation tables are from the specification of Murata NTC
73 * Thermistors Datasheet
74 */
75 static const struct ntc_compensation ncpXXwb473[] = {
76 { .temp_c = -40, .ohm = 1747920 },
77 { .temp_c = -35, .ohm = 1245428 },
78 { .temp_c = -30, .ohm = 898485 },
79 { .temp_c = -25, .ohm = 655802 },
80 { .temp_c = -20, .ohm = 483954 },
81 { .temp_c = -15, .ohm = 360850 },
82 { .temp_c = -10, .ohm = 271697 },
83 { .temp_c = -5, .ohm = 206463 },
84 { .temp_c = 0, .ohm = 158214 },
85 { .temp_c = 5, .ohm = 122259 },
86 { .temp_c = 10, .ohm = 95227 },
87 { .temp_c = 15, .ohm = 74730 },
88 { .temp_c = 20, .ohm = 59065 },
89 { .temp_c = 25, .ohm = 47000 },
90 { .temp_c = 30, .ohm = 37643 },
91 { .temp_c = 35, .ohm = 30334 },
92 { .temp_c = 40, .ohm = 24591 },
93 { .temp_c = 45, .ohm = 20048 },
94 { .temp_c = 50, .ohm = 16433 },
95 { .temp_c = 55, .ohm = 13539 },
96 { .temp_c = 60, .ohm = 11209 },
97 { .temp_c = 65, .ohm = 9328 },
98 { .temp_c = 70, .ohm = 7798 },
99 { .temp_c = 75, .ohm = 6544 },
100 { .temp_c = 80, .ohm = 5518 },
101 { .temp_c = 85, .ohm = 4674 },
102 { .temp_c = 90, .ohm = 3972 },
103 { .temp_c = 95, .ohm = 3388 },
104 { .temp_c = 100, .ohm = 2902 },
105 { .temp_c = 105, .ohm = 2494 },
106 { .temp_c = 110, .ohm = 2150 },
107 { .temp_c = 115, .ohm = 1860 },
108 { .temp_c = 120, .ohm = 1615 },
109 { .temp_c = 125, .ohm = 1406 },
110 };
111 static const struct ntc_compensation ncpXXwl333[] = {
112 { .temp_c = -40, .ohm = 1610154 },
113 { .temp_c = -35, .ohm = 1130850 },
114 { .temp_c = -30, .ohm = 802609 },
115 { .temp_c = -25, .ohm = 575385 },
116 { .temp_c = -20, .ohm = 416464 },
117 { .temp_c = -15, .ohm = 304219 },
118 { .temp_c = -10, .ohm = 224193 },
119 { .temp_c = -5, .ohm = 166623 },
120 { .temp_c = 0, .ohm = 124850 },
121 { .temp_c = 5, .ohm = 94287 },
122 { .temp_c = 10, .ohm = 71747 },
123 { .temp_c = 15, .ohm = 54996 },
124 { .temp_c = 20, .ohm = 42455 },
125 { .temp_c = 25, .ohm = 33000 },
126 { .temp_c = 30, .ohm = 25822 },
127 { .temp_c = 35, .ohm = 20335 },
128 { .temp_c = 40, .ohm = 16115 },
129 { .temp_c = 45, .ohm = 12849 },
130 { .temp_c = 50, .ohm = 10306 },
131 { .temp_c = 55, .ohm = 8314 },
132 { .temp_c = 60, .ohm = 6746 },
133 { .temp_c = 65, .ohm = 5503 },
134 { .temp_c = 70, .ohm = 4513 },
135 { .temp_c = 75, .ohm = 3721 },
136 { .temp_c = 80, .ohm = 3084 },
137 { .temp_c = 85, .ohm = 2569 },
138 { .temp_c = 90, .ohm = 2151 },
139 { .temp_c = 95, .ohm = 1809 },
140 { .temp_c = 100, .ohm = 1529 },
141 { .temp_c = 105, .ohm = 1299 },
142 { .temp_c = 110, .ohm = 1108 },
143 { .temp_c = 115, .ohm = 949 },
144 { .temp_c = 120, .ohm = 817 },
145 { .temp_c = 125, .ohm = 707 },
146 };
147
148 static const struct ntc_compensation ncpXXwf104[] = {
149 { .temp_c = -40, .ohm = 4397119 },
150 { .temp_c = -35, .ohm = 3088599 },
151 { .temp_c = -30, .ohm = 2197225 },
152 { .temp_c = -25, .ohm = 1581881 },
153 { .temp_c = -20, .ohm = 1151037 },
154 { .temp_c = -15, .ohm = 846579 },
155 { .temp_c = -10, .ohm = 628988 },
156 { .temp_c = -5, .ohm = 471632 },
157 { .temp_c = 0, .ohm = 357012 },
158 { .temp_c = 5, .ohm = 272500 },
159 { .temp_c = 10, .ohm = 209710 },
160 { .temp_c = 15, .ohm = 162651 },
161 { .temp_c = 20, .ohm = 127080 },
162 { .temp_c = 25, .ohm = 100000 },
163 { .temp_c = 30, .ohm = 79222 },
164 { .temp_c = 35, .ohm = 63167 },
165 { .temp_c = 40, .ohm = 50677 },
166 { .temp_c = 45, .ohm = 40904 },
167 { .temp_c = 50, .ohm = 33195 },
168 { .temp_c = 55, .ohm = 27091 },
169 { .temp_c = 60, .ohm = 22224 },
170 { .temp_c = 65, .ohm = 18323 },
171 { .temp_c = 70, .ohm = 15184 },
172 { .temp_c = 75, .ohm = 12635 },
173 { .temp_c = 80, .ohm = 10566 },
174 { .temp_c = 85, .ohm = 8873 },
175 { .temp_c = 90, .ohm = 7481 },
176 { .temp_c = 95, .ohm = 6337 },
177 { .temp_c = 100, .ohm = 5384 },
178 { .temp_c = 105, .ohm = 4594 },
179 { .temp_c = 110, .ohm = 3934 },
180 { .temp_c = 115, .ohm = 3380 },
181 { .temp_c = 120, .ohm = 2916 },
182 { .temp_c = 125, .ohm = 2522 },
183 };
184
185 static const struct ntc_compensation ncpXXxh103[] = {
186 { .temp_c = -40, .ohm = 195652 },
187 { .temp_c = -35, .ohm = 148171 },
188 { .temp_c = -30, .ohm = 113347 },
189 { .temp_c = -25, .ohm = 87559 },
190 { .temp_c = -20, .ohm = 68237 },
191 { .temp_c = -15, .ohm = 53650 },
192 { .temp_c = -10, .ohm = 42506 },
193 { .temp_c = -5, .ohm = 33892 },
194 { .temp_c = 0, .ohm = 27219 },
195 { .temp_c = 5, .ohm = 22021 },
196 { .temp_c = 10, .ohm = 17926 },
197 { .temp_c = 15, .ohm = 14674 },
198 { .temp_c = 20, .ohm = 12081 },
199 { .temp_c = 25, .ohm = 10000 },
200 { .temp_c = 30, .ohm = 8315 },
201 { .temp_c = 35, .ohm = 6948 },
202 { .temp_c = 40, .ohm = 5834 },
203 { .temp_c = 45, .ohm = 4917 },
204 { .temp_c = 50, .ohm = 4161 },
205 { .temp_c = 55, .ohm = 3535 },
206 { .temp_c = 60, .ohm = 3014 },
207 { .temp_c = 65, .ohm = 2586 },
208 { .temp_c = 70, .ohm = 2228 },
209 { .temp_c = 75, .ohm = 1925 },
210 { .temp_c = 80, .ohm = 1669 },
211 { .temp_c = 85, .ohm = 1452 },
212 { .temp_c = 90, .ohm = 1268 },
213 { .temp_c = 95, .ohm = 1110 },
214 { .temp_c = 100, .ohm = 974 },
215 { .temp_c = 105, .ohm = 858 },
216 { .temp_c = 110, .ohm = 758 },
217 { .temp_c = 115, .ohm = 672 },
218 { .temp_c = 120, .ohm = 596 },
219 { .temp_c = 125, .ohm = 531 },
220 };
221
222 static const struct ntc_compensation ncpXXwm474[] = {
223 { .temp_c = -40, .ohm = 10900000 },
224 { .temp_c = -35, .ohm = 9600000 },
225 { .temp_c = -30, .ohm = 8300000 },
226 { .temp_c = -25, .ohm = 7000000 },
227 { .temp_c = -20, .ohm = 5980000 },
228 { .temp_c = -15, .ohm = 4960000 },
229 { .temp_c = -10, .ohm = 3940000 },
230 { .temp_c = -5, .ohm = 2920000 },
231 { .temp_c = 0, .ohm = 1900000 },
232 { .temp_c = 5, .ohm = 1614000 },
233 { .temp_c = 10, .ohm = 1328000 },
234 { .temp_c = 15, .ohm = 1042000 },
235 { .temp_c = 20, .ohm = 756000 },
236 { .temp_c = 25, .ohm = 470000 },
237 { .temp_c = 30, .ohm = 404000 },
238 { .temp_c = 35, .ohm = 338000 },
239 { .temp_c = 40, .ohm = 272000 },
240 { .temp_c = 45, .ohm = 206000 },
241 { .temp_c = 50, .ohm = 140000 },
242 { .temp_c = 55, .ohm = 122000 },
243 { .temp_c = 60, .ohm = 104000 },
244 { .temp_c = 65, .ohm = 86000 },
245 { .temp_c = 70, .ohm = 68000 },
246 { .temp_c = 75, .ohm = 50000 },
247 { .temp_c = 80, .ohm = 44200 },
248 { .temp_c = 85, .ohm = 38400 },
249 { .temp_c = 90, .ohm = 32600 },
250 { .temp_c = 95, .ohm = 26800 },
251 { .temp_c = 100, .ohm = 21000 },
252 { .temp_c = 105, .ohm = 18600 },
253 { .temp_c = 110, .ohm = 16200 },
254 { .temp_c = 115, .ohm = 13800 },
255 { .temp_c = 120, .ohm = 11400 },
256 { .temp_c = 125, .ohm = 9000 },
257 };
258
259 /*
260 * The following compensation tables are from the specifications in EPCOS NTC
261 * Thermistors Datasheets
262 */
263 static const struct ntc_compensation b57330v2103[] = {
264 { .temp_c = -40, .ohm = 190030 },
265 { .temp_c = -35, .ohm = 145360 },
266 { .temp_c = -30, .ohm = 112060 },
267 { .temp_c = -25, .ohm = 87041 },
268 { .temp_c = -20, .ohm = 68104 },
269 { .temp_c = -15, .ohm = 53665 },
270 { .temp_c = -10, .ohm = 42576 },
271 { .temp_c = -5, .ohm = 34001 },
272 { .temp_c = 0, .ohm = 27326 },
273 { .temp_c = 5, .ohm = 22096 },
274 { .temp_c = 10, .ohm = 17973 },
275 { .temp_c = 15, .ohm = 14703 },
276 { .temp_c = 20, .ohm = 12090 },
277 { .temp_c = 25, .ohm = 10000 },
278 { .temp_c = 30, .ohm = 8311 },
279 { .temp_c = 35, .ohm = 6941 },
280 { .temp_c = 40, .ohm = 5825 },
281 { .temp_c = 45, .ohm = 4911 },
282 { .temp_c = 50, .ohm = 4158 },
283 { .temp_c = 55, .ohm = 3536 },
284 { .temp_c = 60, .ohm = 3019 },
285 { .temp_c = 65, .ohm = 2588 },
286 { .temp_c = 70, .ohm = 2227 },
287 { .temp_c = 75, .ohm = 1924 },
288 { .temp_c = 80, .ohm = 1668 },
289 { .temp_c = 85, .ohm = 1451 },
290 { .temp_c = 90, .ohm = 1266 },
291 { .temp_c = 95, .ohm = 1108 },
292 { .temp_c = 100, .ohm = 973 },
293 { .temp_c = 105, .ohm = 857 },
294 { .temp_c = 110, .ohm = 757 },
295 { .temp_c = 115, .ohm = 671 },
296 { .temp_c = 120, .ohm = 596 },
297 { .temp_c = 125, .ohm = 531 },
298 };
299
300 static const struct ntc_compensation b57891s0103[] = {
301 { .temp_c = -55.0, .ohm = 878900 },
302 { .temp_c = -50.0, .ohm = 617590 },
303 { .temp_c = -45.0, .ohm = 439340 },
304 { .temp_c = -40.0, .ohm = 316180 },
305 { .temp_c = -35.0, .ohm = 230060 },
306 { .temp_c = -30.0, .ohm = 169150 },
307 { .temp_c = -25.0, .ohm = 125550 },
308 { .temp_c = -20.0, .ohm = 94143 },
309 { .temp_c = -15.0, .ohm = 71172 },
310 { .temp_c = -10.0, .ohm = 54308 },
311 { .temp_c = -5.0, .ohm = 41505 },
312 { .temp_c = 0.0, .ohm = 32014 },
313 { .temp_c = 5.0, .ohm = 25011 },
314 { .temp_c = 10.0, .ohm = 19691 },
315 { .temp_c = 15.0, .ohm = 15618 },
316 { .temp_c = 20.0, .ohm = 12474 },
317 { .temp_c = 25.0, .ohm = 10000 },
318 { .temp_c = 30.0, .ohm = 8080 },
319 { .temp_c = 35.0, .ohm = 6569 },
320 { .temp_c = 40.0, .ohm = 5372 },
321 { .temp_c = 45.0, .ohm = 4424 },
322 { .temp_c = 50.0, .ohm = 3661 },
323 { .temp_c = 55.0, .ohm = 3039 },
324 { .temp_c = 60.0, .ohm = 2536 },
325 { .temp_c = 65.0, .ohm = 2128 },
326 { .temp_c = 70.0, .ohm = 1794 },
327 { .temp_c = 75.0, .ohm = 1518 },
328 { .temp_c = 80.0, .ohm = 1290 },
329 { .temp_c = 85.0, .ohm = 1100 },
330 { .temp_c = 90.0, .ohm = 942 },
331 { .temp_c = 95.0, .ohm = 809 },
332 { .temp_c = 100.0, .ohm = 697 },
333 { .temp_c = 105.0, .ohm = 604 },
334 { .temp_c = 110.0, .ohm = 525 },
335 { .temp_c = 115.0, .ohm = 457 },
336 { .temp_c = 120.0, .ohm = 400 },
337 { .temp_c = 125.0, .ohm = 351 },
338 { .temp_c = 130.0, .ohm = 308 },
339 { .temp_c = 135.0, .ohm = 272 },
340 { .temp_c = 140.0, .ohm = 240 },
341 { .temp_c = 145.0, .ohm = 213 },
342 { .temp_c = 150.0, .ohm = 189 },
343 { .temp_c = 155.0, .ohm = 168 },
344 };
345
346 struct ntc_type {
347 const struct ntc_compensation *comp;
348 int n_comp;
349 };
350
351 #define NTC_TYPE(ntc, compensation) \
352 [(ntc)] = { .comp = (compensation), .n_comp = ARRAY_SIZE(compensation) }
353
354 static const struct ntc_type ntc_type[] = {
355 NTC_TYPE(TYPE_B57330V2103, b57330v2103),
356 NTC_TYPE(TYPE_B57891S0103, b57891s0103),
357 NTC_TYPE(TYPE_NCPXXWB473, ncpXXwb473),
358 NTC_TYPE(TYPE_NCPXXWF104, ncpXXwf104),
359 NTC_TYPE(TYPE_NCPXXWL333, ncpXXwl333),
360 NTC_TYPE(TYPE_NCPXXXH103, ncpXXxh103),
361 NTC_TYPE(TYPE_NCPXXWM474, ncpXXwm474),
362 };
363
364 /*
365 * pullup_uV, pullup_ohm, pulldown_ohm, and connect are required.
366 *
367 * How to setup pullup_ohm, pulldown_ohm, and connect is
368 * described at Documentation/hwmon/ntc_thermistor.rst
369 *
370 * pullup/down_ohm: 0 for infinite / not-connected
371 *
372 * chan: iio_channel pointer to communicate with the ADC which the
373 * thermistor is using for conversion of the analog values.
374 */
375 struct ntc_data {
376 const struct ntc_compensation *comp;
377 int n_comp;
378 unsigned int pullup_uv;
379 unsigned int pullup_ohm;
380 unsigned int pulldown_ohm;
381 enum { NTC_CONNECTED_POSITIVE, NTC_CONNECTED_GROUND } connect;
382 struct iio_channel *chan;
383 };
384
ntc_adc_iio_read(struct ntc_data * data)385 static int ntc_adc_iio_read(struct ntc_data *data)
386 {
387 struct iio_channel *channel = data->chan;
388 int uv, ret;
389
390 ret = iio_read_channel_processed_scale(channel, &uv, 1000);
391 if (ret < 0) {
392 int raw;
393
394 /*
395 * This fallback uses a raw read and then
396 * assumes the ADC is 12 bits, scaling with
397 * a factor 1000 to get to microvolts.
398 */
399 ret = iio_read_channel_raw(channel, &raw);
400 if (ret < 0) {
401 pr_err("read channel() error: %d\n", ret);
402 return ret;
403 }
404 ret = iio_convert_raw_to_processed(channel, raw, &uv, 1000);
405 if (ret < 0) {
406 /* Assume 12 bit ADC with vref at pullup_uv */
407 uv = (data->pullup_uv * (s64)raw) >> 12;
408 }
409 }
410
411 return uv;
412 }
413
div64_u64_safe(u64 dividend,u64 divisor)414 static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
415 {
416 if (divisor == 0 && dividend == 0)
417 return 0;
418 if (divisor == 0)
419 return UINT_MAX;
420 return div64_u64(dividend, divisor);
421 }
422
get_ohm_of_thermistor(struct ntc_data * data,unsigned int uv)423 static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
424 {
425 u32 puv = data->pullup_uv;
426 u64 n, puo, pdo;
427 puo = data->pullup_ohm;
428 pdo = data->pulldown_ohm;
429
430 /* faulty adc value */
431 if (uv == 0 || uv >= puv)
432 return -ENODATA;
433
434 if (data->connect == NTC_CONNECTED_POSITIVE && puo == 0)
435 n = div_u64(pdo * (puv - uv), uv);
436 else if (data->connect == NTC_CONNECTED_GROUND && pdo == 0)
437 n = div_u64(puo * uv, puv - uv);
438 else if (data->connect == NTC_CONNECTED_POSITIVE)
439 n = div64_u64_safe(pdo * puo * (puv - uv),
440 puo * uv - pdo * (puv - uv));
441 else
442 n = div64_u64_safe(pdo * puo * uv, pdo * (puv - uv) - puo * uv);
443
444 /* sensor out of bounds */
445 if (n > data->comp[0].ohm || n < data->comp[data->n_comp - 1].ohm)
446 return -ENODATA;
447
448 return n;
449 }
450
lookup_comp(struct ntc_data * data,unsigned int ohm,int * i_low,int * i_high)451 static void lookup_comp(struct ntc_data *data, unsigned int ohm,
452 int *i_low, int *i_high)
453 {
454 int start, end, mid;
455
456 /*
457 * Handle special cases: Resistance is higher than or equal to
458 * resistance in first table entry, or resistance is lower or equal
459 * to resistance in last table entry.
460 * In these cases, return i_low == i_high, either pointing to the
461 * beginning or to the end of the table depending on the condition.
462 */
463 if (ohm >= data->comp[0].ohm) {
464 *i_low = 0;
465 *i_high = 0;
466 return;
467 }
468 if (ohm <= data->comp[data->n_comp - 1].ohm) {
469 *i_low = data->n_comp - 1;
470 *i_high = data->n_comp - 1;
471 return;
472 }
473
474 /* Do a binary search on compensation table */
475 start = 0;
476 end = data->n_comp;
477 while (start < end) {
478 mid = start + (end - start) / 2;
479 /*
480 * start <= mid < end
481 * data->comp[start].ohm > ohm >= data->comp[end].ohm
482 *
483 * We could check for "ohm == data->comp[mid].ohm" here, but
484 * that is a quite unlikely condition, and we would have to
485 * check again after updating start. Check it at the end instead
486 * for simplicity.
487 */
488 if (ohm >= data->comp[mid].ohm) {
489 end = mid;
490 } else {
491 start = mid + 1;
492 /*
493 * ohm >= data->comp[start].ohm might be true here,
494 * since we set start to mid + 1. In that case, we are
495 * done. We could keep going, but the condition is quite
496 * likely to occur, so it is worth checking for it.
497 */
498 if (ohm >= data->comp[start].ohm)
499 end = start;
500 }
501 /*
502 * start <= end
503 * data->comp[start].ohm >= ohm >= data->comp[end].ohm
504 */
505 }
506 /*
507 * start == end
508 * ohm >= data->comp[end].ohm
509 */
510 *i_low = end;
511 if (ohm == data->comp[end].ohm)
512 *i_high = end;
513 else
514 *i_high = end - 1;
515 }
516
get_temp_mc(struct ntc_data * data,unsigned int ohm)517 static int get_temp_mc(struct ntc_data *data, unsigned int ohm)
518 {
519 int low, high;
520 int temp;
521
522 lookup_comp(data, ohm, &low, &high);
523 /*
524 * First multiplying the table temperatures with 1000 to get to
525 * millicentigrades (which is what we want) and then interpolating
526 * will give the best precision.
527 */
528 temp = fixp_linear_interpolate(data->comp[low].ohm,
529 data->comp[low].temp_c * 1000,
530 data->comp[high].ohm,
531 data->comp[high].temp_c * 1000,
532 ohm);
533 return temp;
534 }
535
ntc_thermistor_get_ohm(struct ntc_data * data)536 static int ntc_thermistor_get_ohm(struct ntc_data *data)
537 {
538 int read_uv;
539
540 read_uv = ntc_adc_iio_read(data);
541 if (read_uv < 0)
542 return read_uv;
543 return get_ohm_of_thermistor(data, read_uv);
544 }
545
ntc_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)546 static int ntc_read(struct device *dev, enum hwmon_sensor_types type,
547 u32 attr, int channel, long *val)
548 {
549 struct ntc_data *data = dev_get_drvdata(dev);
550 int ohm;
551
552 switch (type) {
553 case hwmon_temp:
554 switch (attr) {
555 case hwmon_temp_input:
556 ohm = ntc_thermistor_get_ohm(data);
557 if (ohm < 0)
558 return ohm;
559 *val = get_temp_mc(data, ohm);
560 return 0;
561 case hwmon_temp_type:
562 *val = 4;
563 return 0;
564 default:
565 break;
566 }
567 break;
568 default:
569 break;
570 }
571 return -EINVAL;
572 }
573
ntc_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)574 static umode_t ntc_is_visible(const void *data, enum hwmon_sensor_types type,
575 u32 attr, int channel)
576 {
577 if (type == hwmon_temp) {
578 switch (attr) {
579 case hwmon_temp_input:
580 case hwmon_temp_type:
581 return 0444;
582 default:
583 break;
584 }
585 }
586 return 0;
587 }
588
589 static const struct hwmon_channel_info * const ntc_info[] = {
590 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
591 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_TYPE),
592 NULL
593 };
594
595 static const struct hwmon_ops ntc_hwmon_ops = {
596 .is_visible = ntc_is_visible,
597 .read = ntc_read,
598 };
599
600 static const struct hwmon_chip_info ntc_chip_info = {
601 .ops = &ntc_hwmon_ops,
602 .info = ntc_info,
603 };
604
ntc_thermistor_parse_props(struct device * dev,struct ntc_data * data)605 static int ntc_thermistor_parse_props(struct device *dev,
606 struct ntc_data *data)
607 {
608 struct iio_channel *chan;
609 enum iio_chan_type type;
610 int ret;
611
612 chan = devm_iio_channel_get(dev, NULL);
613 if (IS_ERR(chan))
614 return PTR_ERR(chan);
615
616 ret = iio_get_channel_type(chan, &type);
617 if (ret < 0)
618 return ret;
619
620 if (type != IIO_VOLTAGE)
621 return -EINVAL;
622
623 ret = device_property_read_u32(dev, "pullup-uv", &data->pullup_uv);
624 if (ret)
625 return dev_err_probe(dev, ret, "pullup-uv not specified\n");
626
627 ret = device_property_read_u32(dev, "pullup-ohm", &data->pullup_ohm);
628 if (ret)
629 return dev_err_probe(dev, ret, "pullup-ohm not specified\n");
630
631 ret = device_property_read_u32(dev, "pulldown-ohm", &data->pulldown_ohm);
632 if (ret)
633 return dev_err_probe(dev, ret, "pulldown-ohm not specified\n");
634
635 if (device_property_read_bool(dev, "connected-positive"))
636 data->connect = NTC_CONNECTED_POSITIVE;
637 else /* status change should be possible if not always on. */
638 data->connect = NTC_CONNECTED_GROUND;
639
640 data->chan = chan;
641
642 return 0;
643 }
644
ntc_thermistor_probe(struct platform_device * pdev)645 static int ntc_thermistor_probe(struct platform_device *pdev)
646 {
647 struct device *dev = &pdev->dev;
648 const struct platform_device_id *pdev_id;
649 struct device *hwmon_dev;
650 struct ntc_data *data;
651 int ret;
652
653 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
654 if (!data)
655 return -ENOMEM;
656
657 ret = ntc_thermistor_parse_props(dev, data);
658 if (ret)
659 return ret;
660
661 if (data->pullup_uv == 0 ||
662 (data->pullup_ohm == 0 && data->connect ==
663 NTC_CONNECTED_GROUND) ||
664 (data->pulldown_ohm == 0 && data->connect ==
665 NTC_CONNECTED_POSITIVE) ||
666 (data->connect != NTC_CONNECTED_POSITIVE &&
667 data->connect != NTC_CONNECTED_GROUND)) {
668 dev_err(dev, "Required data to use NTC driver not supplied.\n");
669 return -EINVAL;
670 }
671
672 pdev_id = device_get_match_data(dev);
673
674 if (pdev_id->driver_data >= ARRAY_SIZE(ntc_type)) {
675 dev_err(dev, "Unknown device type: %lu(%s)\n",
676 pdev_id->driver_data, pdev_id->name);
677 return -EINVAL;
678 }
679
680 data->comp = ntc_type[pdev_id->driver_data].comp;
681 data->n_comp = ntc_type[pdev_id->driver_data].n_comp;
682
683 hwmon_dev = devm_hwmon_device_register_with_info(dev, pdev_id->name,
684 data, &ntc_chip_info,
685 NULL);
686 if (IS_ERR(hwmon_dev)) {
687 dev_err(dev, "unable to register as hwmon device.\n");
688 return PTR_ERR(hwmon_dev);
689 }
690
691 dev_info(dev, "Thermistor type: %s successfully probed.\n",
692 pdev_id->name);
693
694 return 0;
695 }
696
697 static const struct of_device_id ntc_match[] = {
698 { .compatible = "epcos,b57330v2103",
699 .data = &ntc_thermistor_id[NTC_B57330V2103]},
700 { .compatible = "epcos,b57891s0103",
701 .data = &ntc_thermistor_id[NTC_B57891S0103] },
702 { .compatible = "murata,ncp03wb473",
703 .data = &ntc_thermistor_id[NTC_NCP03WB473] },
704 { .compatible = "murata,ncp03wf104",
705 .data = &ntc_thermistor_id[NTC_NCP03WF104] },
706 { .compatible = "murata,ncp15wb473",
707 .data = &ntc_thermistor_id[NTC_NCP15WB473] },
708 { .compatible = "murata,ncp15wl333",
709 .data = &ntc_thermistor_id[NTC_NCP15WL333] },
710 { .compatible = "murata,ncp15xh103",
711 .data = &ntc_thermistor_id[NTC_NCP15XH103] },
712 { .compatible = "murata,ncp18wb473",
713 .data = &ntc_thermistor_id[NTC_NCP18WB473] },
714 { .compatible = "murata,ncp21wb473",
715 .data = &ntc_thermistor_id[NTC_NCP21WB473] },
716 { .compatible = "samsung,1404-001221",
717 .data = &ntc_thermistor_id[NTC_SSG1404001221] },
718 { .compatible = "murata,ncp18wm474",
719 .data = &ntc_thermistor_id[NTC_NCP18WM474] },
720
721 /* Usage of vendor name "ntc" is deprecated */
722 { .compatible = "ntc,ncp03wb473",
723 .data = &ntc_thermistor_id[NTC_NCP03WB473] },
724 { .compatible = "ntc,ncp15wb473",
725 .data = &ntc_thermistor_id[NTC_NCP15WB473] },
726 { .compatible = "ntc,ncp15wl333",
727 .data = &ntc_thermistor_id[NTC_NCP15WL333] },
728 { .compatible = "ntc,ncp18wb473",
729 .data = &ntc_thermistor_id[NTC_NCP18WB473] },
730 { .compatible = "ntc,ncp21wb473",
731 .data = &ntc_thermistor_id[NTC_NCP21WB473] },
732 { },
733 };
734 MODULE_DEVICE_TABLE(of, ntc_match);
735
736 static struct platform_driver ntc_thermistor_driver = {
737 .driver = {
738 .name = "ntc-thermistor",
739 .of_match_table = ntc_match,
740 },
741 .probe = ntc_thermistor_probe,
742 .id_table = ntc_thermistor_id,
743 };
744
745 module_platform_driver(ntc_thermistor_driver);
746
747 MODULE_DESCRIPTION("NTC Thermistor Driver");
748 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
749 MODULE_LICENSE("GPL");
750 MODULE_ALIAS("platform:ntc-thermistor");
751 MODULE_IMPORT_NS("IIO_CONSUMER");
752