]>
Commit | Line | Data |
---|---|---|
f22aaaa7 DK |
1 | /* |
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> | |
9e8269de NKC |
29 | #include <linux/of.h> |
30 | #include <linux/of_device.h> | |
f22aaaa7 DK |
31 | |
32 | #include <linux/platform_data/ntc_thermistor.h> | |
33 | ||
9e8269de NKC |
34 | #include <linux/iio/iio.h> |
35 | #include <linux/iio/machine.h> | |
36 | #include <linux/iio/driver.h> | |
37 | #include <linux/iio/consumer.h> | |
38 | ||
f22aaaa7 | 39 | #include <linux/hwmon.h> |
f22aaaa7 DK |
40 | |
41 | struct ntc_compensation { | |
088ce2ac | 42 | int temp_c; |
f22aaaa7 DK |
43 | unsigned int ohm; |
44 | }; | |
45 | ||
e056fe25 PR |
46 | /* |
47 | * Used as index in a zero-terminated array, holes not allowed so | |
48 | * that NTC_LAST is the first empty array entry. | |
49 | */ | |
50 | enum { | |
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 | ||
9e8269de | 63 | static const struct platform_device_id ntc_thermistor_id[] = { |
e056fe25 PR |
64 | [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] = { }, | |
9e8269de NKC |
74 | }; |
75 | ||
f22aaaa7 DK |
76 | /* |
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 | */ | |
4626dcff | 82 | static const struct ntc_compensation ncpXXwb473[] = { |
088ce2ac GR |
83 | { .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 }, | |
f22aaaa7 | 117 | }; |
4626dcff | 118 | static const struct ntc_compensation ncpXXwl333[] = { |
088ce2ac GR |
119 | { .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 }, | |
f22aaaa7 DK |
153 | }; |
154 | ||
887ee434 BS |
155 | static 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 | ||
54ce3a0d JM |
192 | static 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 | ||
ed67f087 | 229 | /* |
e8fda2c8 PR |
230 | * The following compensation tables are from the specifications in EPCOS NTC |
231 | * Thermistors Datasheets | |
ed67f087 JP |
232 | */ |
233 | static 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 | ||
e8fda2c8 PR |
270 | static 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 | ||
737c086e PR |
316 | struct 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 | ||
324 | static 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 | ||
f22aaaa7 | 333 | struct ntc_data { |
f22aaaa7 DK |
334 | struct ntc_thermistor_platform_data *pdata; |
335 | const struct ntc_compensation *comp; | |
f22aaaa7 | 336 | int n_comp; |
f22aaaa7 DK |
337 | }; |
338 | ||
59cf4243 | 339 | #if defined(CONFIG_OF) && IS_ENABLED(CONFIG_IIO) |
9e8269de NKC |
340 | static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata) |
341 | { | |
342 | struct iio_channel *channel = pdata->chan; | |
0315253b | 343 | int raw, uv, ret; |
9e8269de | 344 | |
0315253b | 345 | ret = iio_read_channel_raw(channel, &raw); |
9e8269de NKC |
346 | if (ret < 0) { |
347 | pr_err("read channel() error: %d\n", ret); | |
348 | return ret; | |
349 | } | |
350 | ||
0315253b CL |
351 | 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 | } | |
9e8269de | 356 | |
0315253b | 357 | return uv; |
9e8269de NKC |
358 | } |
359 | ||
360 | static const struct of_device_id ntc_match[] = { | |
ed67f087 | 361 | { .compatible = "epcos,b57330v2103", |
e056fe25 PR |
362 | .data = &ntc_thermistor_id[NTC_B57330V2103]}, |
363 | { .compatible = "epcos,b57891s0103", | |
364 | .data = &ntc_thermistor_id[NTC_B57891S0103] }, | |
365 | { .compatible = "murata,ncp03wb473", | |
366 | .data = &ntc_thermistor_id[NTC_NCP03WB473] }, | |
887ee434 | 367 | { .compatible = "murata,ncp03wf104", |
e056fe25 PR |
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] }, | |
54ce3a0d | 373 | { .compatible = "murata,ncp15xh103", |
e056fe25 PR |
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] }, | |
8b6f5e0f NKC |
379 | |
380 | /* Usage of vendor name "ntc" is deprecated */ | |
e056fe25 PR |
381 | { .compatible = "ntc,ncp03wb473", |
382 | .data = &ntc_thermistor_id[NTC_NCP03WB473] }, | |
9e8269de | 383 | { .compatible = "ntc,ncp15wb473", |
e056fe25 PR |
384 | .data = &ntc_thermistor_id[NTC_NCP15WB473] }, |
385 | { .compatible = "ntc,ncp15wl333", | |
386 | .data = &ntc_thermistor_id[NTC_NCP15WL333] }, | |
9e8269de | 387 | { .compatible = "ntc,ncp18wb473", |
e056fe25 | 388 | .data = &ntc_thermistor_id[NTC_NCP18WB473] }, |
9e8269de | 389 | { .compatible = "ntc,ncp21wb473", |
e056fe25 | 390 | .data = &ntc_thermistor_id[NTC_NCP21WB473] }, |
9e8269de NKC |
391 | { }, |
392 | }; | |
393 | MODULE_DEVICE_TABLE(of, ntc_match); | |
394 | ||
395 | static struct ntc_thermistor_platform_data * | |
48001525 | 396 | ntc_thermistor_parse_dt(struct device *dev) |
9e8269de NKC |
397 | { |
398 | struct iio_channel *chan; | |
adba6575 | 399 | enum iio_chan_type type; |
48001525 | 400 | struct device_node *np = dev->of_node; |
9e8269de | 401 | struct ntc_thermistor_platform_data *pdata; |
adba6575 | 402 | int ret; |
9e8269de NKC |
403 | |
404 | if (!np) | |
405 | return NULL; | |
406 | ||
48001525 | 407 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
9e8269de NKC |
408 | if (!pdata) |
409 | return ERR_PTR(-ENOMEM); | |
410 | ||
48001525 | 411 | chan = devm_iio_channel_get(dev, NULL); |
9e8269de NKC |
412 | if (IS_ERR(chan)) |
413 | return ERR_CAST(chan); | |
414 | ||
adba6575 CL |
415 | 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 | ||
088ce2ac | 422 | if (of_property_read_u32(np, "pullup-uv", &pdata->pullup_uv)) |
9e8269de NKC |
423 | 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; | |
088ce2ac | 435 | pdata->read_uv = ntc_adc_iio_read; |
9e8269de NKC |
436 | |
437 | return pdata; | |
438 | } | |
9e8269de NKC |
439 | #else |
440 | static struct ntc_thermistor_platform_data * | |
48001525 | 441 | ntc_thermistor_parse_dt(struct device *dev) |
9e8269de NKC |
442 | { |
443 | return NULL; | |
444 | } | |
445 | ||
59cf4243 JD |
446 | #define ntc_match NULL |
447 | ||
9e8269de NKC |
448 | #endif |
449 | ||
f22aaaa7 DK |
450 | static 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 | ||
088ce2ac | 459 | static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv) |
f22aaaa7 DK |
460 | { |
461 | struct ntc_thermistor_platform_data *pdata = data->pdata; | |
f6725ae2 | 462 | u32 puv = pdata->pullup_uv; |
088ce2ac GR |
463 | u64 n, puo, pdo; |
464 | puo = pdata->pullup_ohm; | |
465 | pdo = pdata->pulldown_ohm; | |
f22aaaa7 | 466 | |
f6725ae2 CL |
467 | if (uv == 0) |
468 | return (pdata->connect == NTC_CONNECTED_POSITIVE) ? | |
469 | INT_MAX : 0; | |
470 | if (uv >= puv) | |
f22aaaa7 | 471 | return (pdata->connect == NTC_CONNECTED_POSITIVE) ? |
dbe43a62 | 472 | 0 : INT_MAX; |
f22aaaa7 | 473 | |
088ce2ac | 474 | if (pdata->connect == NTC_CONNECTED_POSITIVE && puo == 0) |
f6725ae2 | 475 | n = div_u64(pdo * (puv - uv), uv); |
088ce2ac | 476 | else if (pdata->connect == NTC_CONNECTED_GROUND && pdo == 0) |
f6725ae2 | 477 | n = div_u64(puo * uv, puv - uv); |
f22aaaa7 | 478 | else if (pdata->connect == NTC_CONNECTED_POSITIVE) |
f6725ae2 CL |
479 | n = div64_u64_safe(pdo * puo * (puv - uv), |
480 | puo * uv - pdo * (puv - uv)); | |
f22aaaa7 | 481 | else |
f6725ae2 | 482 | n = div64_u64_safe(pdo * puo * uv, pdo * (puv - uv) - puo * uv); |
f22aaaa7 | 483 | |
088ce2ac GR |
484 | if (n > INT_MAX) |
485 | n = INT_MAX; | |
486 | return n; | |
f22aaaa7 DK |
487 | } |
488 | ||
dbe43a62 GR |
489 | static void lookup_comp(struct ntc_data *data, unsigned int ohm, |
490 | int *i_low, int *i_high) | |
f22aaaa7 | 491 | { |
dbe43a62 GR |
492 | 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 | } | |
f22aaaa7 DK |
511 | |
512 | /* Do a binary search on compensation table */ | |
513 | start = 0; | |
514 | end = data->n_comp; | |
dbe43a62 | 515 | while (start < end) { |
f22aaaa7 | 516 | mid = start + (end - start) / 2; |
dbe43a62 GR |
517 | /* |
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) { | |
f22aaaa7 | 527 | end = mid; |
f22aaaa7 | 528 | } else { |
dbe43a62 GR |
529 | start = mid + 1; |
530 | /* | |
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; | |
f22aaaa7 | 538 | } |
dbe43a62 GR |
539 | /* |
540 | * start <= end | |
541 | * data->comp[start].ohm >= ohm >= data->comp[end].ohm | |
542 | */ | |
f22aaaa7 | 543 | } |
dbe43a62 GR |
544 | /* |
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; | |
f22aaaa7 DK |
553 | } |
554 | ||
088ce2ac | 555 | static int get_temp_mc(struct ntc_data *data, unsigned int ohm) |
f22aaaa7 DK |
556 | { |
557 | int low, high; | |
dbe43a62 | 558 | int temp; |
f22aaaa7 | 559 | |
dbe43a62 GR |
560 | lookup_comp(data, ohm, &low, &high); |
561 | if (low == high) { | |
f22aaaa7 | 562 | /* Unable to use linear approximation */ |
088ce2ac | 563 | temp = data->comp[low].temp_c * 1000; |
f22aaaa7 | 564 | } else { |
088ce2ac GR |
565 | temp = data->comp[low].temp_c * 1000 + |
566 | ((data->comp[high].temp_c - data->comp[low].temp_c) * | |
f22aaaa7 DK |
567 | 1000 * ((int)ohm - (int)data->comp[low].ohm)) / |
568 | ((int)data->comp[high].ohm - (int)data->comp[low].ohm); | |
569 | } | |
dbe43a62 | 570 | return temp; |
f22aaaa7 DK |
571 | } |
572 | ||
dbe43a62 | 573 | static int ntc_thermistor_get_ohm(struct ntc_data *data) |
f22aaaa7 | 574 | { |
088ce2ac | 575 | int read_uv; |
f22aaaa7 | 576 | |
dbe43a62 GR |
577 | if (data->pdata->read_ohm) |
578 | return data->pdata->read_ohm(); | |
f22aaaa7 | 579 | |
088ce2ac GR |
580 | 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); | |
f22aaaa7 | 585 | } |
dbe43a62 | 586 | return -EINVAL; |
f22aaaa7 DK |
587 | } |
588 | ||
7cc7de93 GR |
589 | static int ntc_read(struct device *dev, enum hwmon_sensor_types type, |
590 | u32 attr, int channel, long *val) | |
c08860ff | 591 | { |
7cc7de93 | 592 | struct ntc_data *data = dev_get_drvdata(dev); |
c08860ff JL |
593 | int ohm; |
594 | ||
7cc7de93 GR |
595 | 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; | |
c08860ff JL |
615 | } |
616 | ||
7cc7de93 GR |
617 | static umode_t ntc_is_visible(const void *data, enum hwmon_sensor_types type, |
618 | u32 attr, int channel) | |
f22aaaa7 | 619 | { |
7cc7de93 GR |
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; | |
f22aaaa7 DK |
630 | } |
631 | ||
7cc7de93 | 632 | static const struct hwmon_channel_info *ntc_info[] = { |
0ddca577 GR |
633 | HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ), |
634 | HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_TYPE), | |
7cc7de93 | 635 | NULL |
f22aaaa7 | 636 | }; |
f22aaaa7 | 637 | |
7cc7de93 GR |
638 | static const struct hwmon_ops ntc_hwmon_ops = { |
639 | .is_visible = ntc_is_visible, | |
640 | .read = ntc_read, | |
641 | }; | |
642 | ||
643 | static const struct hwmon_chip_info ntc_chip_info = { | |
644 | .ops = &ntc_hwmon_ops, | |
645 | .info = ntc_info, | |
2251aef6 EV |
646 | }; |
647 | ||
6c931ae1 | 648 | static int ntc_thermistor_probe(struct platform_device *pdev) |
f22aaaa7 | 649 | { |
48001525 | 650 | struct device *dev = &pdev->dev; |
9e8269de | 651 | const struct of_device_id *of_id = |
48001525 | 652 | of_match_device(of_match_ptr(ntc_match), dev); |
9e8269de NKC |
653 | const struct platform_device_id *pdev_id; |
654 | struct ntc_thermistor_platform_data *pdata; | |
5e7f5994 | 655 | struct device *hwmon_dev; |
f22aaaa7 | 656 | struct ntc_data *data; |
9e8269de | 657 | |
48001525 | 658 | pdata = ntc_thermistor_parse_dt(dev); |
9e8269de NKC |
659 | if (IS_ERR(pdata)) |
660 | return PTR_ERR(pdata); | |
661 | else if (pdata == NULL) | |
48001525 | 662 | pdata = dev_get_platdata(dev); |
f22aaaa7 DK |
663 | |
664 | if (!pdata) { | |
48001525 | 665 | dev_err(dev, "No platform init data supplied.\n"); |
f22aaaa7 DK |
666 | return -ENODEV; |
667 | } | |
668 | ||
669 | /* Either one of the two is required. */ | |
088ce2ac | 670 | if (!pdata->read_uv && !pdata->read_ohm) { |
48001525 | 671 | dev_err(dev, |
088ce2ac | 672 | "Both read_uv and read_ohm missing. Need either one of the two.\n"); |
f22aaaa7 DK |
673 | return -EINVAL; |
674 | } | |
675 | ||
088ce2ac | 676 | if (pdata->read_uv && pdata->read_ohm) { |
48001525 | 677 | dev_warn(dev, |
088ce2ac GR |
678 | "Only one of read_uv and read_ohm is needed; ignoring read_uv.\n"); |
679 | pdata->read_uv = NULL; | |
f22aaaa7 DK |
680 | } |
681 | ||
088ce2ac | 682 | if (pdata->read_uv && (pdata->pullup_uv == 0 || |
f22aaaa7 DK |
683 | (pdata->pullup_ohm == 0 && pdata->connect == |
684 | NTC_CONNECTED_GROUND) || | |
685 | (pdata->pulldown_ohm == 0 && pdata->connect == | |
686 | NTC_CONNECTED_POSITIVE) || | |
687 | (pdata->connect != NTC_CONNECTED_POSITIVE && | |
688 | pdata->connect != NTC_CONNECTED_GROUND))) { | |
48001525 | 689 | dev_err(dev, "Required data to use read_uv not supplied.\n"); |
f22aaaa7 DK |
690 | return -EINVAL; |
691 | } | |
692 | ||
48001525 | 693 | data = devm_kzalloc(dev, sizeof(struct ntc_data), GFP_KERNEL); |
f22aaaa7 DK |
694 | if (!data) |
695 | return -ENOMEM; | |
696 | ||
9e8269de NKC |
697 | pdev_id = of_id ? of_id->data : platform_get_device_id(pdev); |
698 | ||
f22aaaa7 | 699 | data->pdata = pdata; |
f22aaaa7 | 700 | |
737c086e | 701 | if (pdev_id->driver_data >= ARRAY_SIZE(ntc_type)) { |
48001525 | 702 | dev_err(dev, "Unknown device type: %lu(%s)\n", |
9e8269de | 703 | pdev_id->driver_data, pdev_id->name); |
41141e64 | 704 | return -EINVAL; |
f22aaaa7 DK |
705 | } |
706 | ||
737c086e PR |
707 | data->comp = ntc_type[pdev_id->driver_data].comp; |
708 | data->n_comp = ntc_type[pdev_id->driver_data].n_comp; | |
709 | ||
7cc7de93 GR |
710 | hwmon_dev = devm_hwmon_device_register_with_info(dev, pdev_id->name, |
711 | data, &ntc_chip_info, | |
712 | NULL); | |
5e7f5994 | 713 | if (IS_ERR(hwmon_dev)) { |
48001525 | 714 | dev_err(dev, "unable to register as hwmon device.\n"); |
5e7f5994 | 715 | return PTR_ERR(hwmon_dev); |
f22aaaa7 DK |
716 | } |
717 | ||
48001525 GR |
718 | dev_info(dev, "Thermistor type: %s successfully probed.\n", |
719 | pdev_id->name); | |
9e8269de | 720 | |
f22aaaa7 DK |
721 | return 0; |
722 | } | |
723 | ||
f22aaaa7 DK |
724 | static struct platform_driver ntc_thermistor_driver = { |
725 | .driver = { | |
726 | .name = "ntc-thermistor", | |
9e8269de | 727 | .of_match_table = of_match_ptr(ntc_match), |
f22aaaa7 DK |
728 | }, |
729 | .probe = ntc_thermistor_probe, | |
f22aaaa7 DK |
730 | .id_table = ntc_thermistor_id, |
731 | }; | |
732 | ||
25a236a5 | 733 | module_platform_driver(ntc_thermistor_driver); |
f22aaaa7 | 734 | |
ed67f087 | 735 | MODULE_DESCRIPTION("NTC Thermistor Driver"); |
f22aaaa7 DK |
736 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
737 | MODULE_LICENSE("GPL"); | |
738 | MODULE_ALIAS("platform:ntc-thermistor"); |