2 * ntc_thermistor.c - NTC Thermistors
4 * Copyright (C) 2010 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
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.
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.
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
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>
30 #include <linux/of_device.h>
32 #include <linux/platform_data/ntc_thermistor.h>
34 #include <linux/iio/iio.h>
35 #include <linux/iio/machine.h>
36 #include <linux/iio/driver.h>
37 #include <linux/iio/consumer.h>
39 #include <linux/hwmon.h>
40 #include <linux/hwmon-sysfs.h>
41 #include <linux/thermal.h>
43 struct ntc_compensation {
49 * Used as index in a zero-terminated array, holes not allowed so
50 * that NTC_LAST is the first empty array entry.
65 static const struct platform_device_id ntc_thermistor_id[] = {
66 [NTC_B57330V2103] = { "b57330v2103", TYPE_B57330V2103 },
67 [NTC_B57891S0103] = { "b57891s0103", TYPE_B57891S0103 },
68 [NTC_NCP03WB473] = { "ncp03wb473", TYPE_NCPXXWB473 },
69 [NTC_NCP03WF104] = { "ncp03wf104", TYPE_NCPXXWF104 },
70 [NTC_NCP15WB473] = { "ncp15wb473", TYPE_NCPXXWB473 },
71 [NTC_NCP15WL333] = { "ncp15wl333", TYPE_NCPXXWL333 },
72 [NTC_NCP15XH103] = { "ncp15xh103", TYPE_NCPXXXH103 },
73 [NTC_NCP18WB473] = { "ncp18wb473", TYPE_NCPXXWB473 },
74 [NTC_NCP21WB473] = { "ncp21wb473", TYPE_NCPXXWB473 },
79 * A compensation table should be sorted by the values of .ohm
80 * in descending order.
81 * The following compensation tables are from the specification of Murata NTC
82 * Thermistors Datasheet
84 static const struct ntc_compensation ncpXXwb473[] = {
85 { .temp_c = -40, .ohm = 1747920 },
86 { .temp_c = -35, .ohm = 1245428 },
87 { .temp_c = -30, .ohm = 898485 },
88 { .temp_c = -25, .ohm = 655802 },
89 { .temp_c = -20, .ohm = 483954 },
90 { .temp_c = -15, .ohm = 360850 },
91 { .temp_c = -10, .ohm = 271697 },
92 { .temp_c = -5, .ohm = 206463 },
93 { .temp_c = 0, .ohm = 158214 },
94 { .temp_c = 5, .ohm = 122259 },
95 { .temp_c = 10, .ohm = 95227 },
96 { .temp_c = 15, .ohm = 74730 },
97 { .temp_c = 20, .ohm = 59065 },
98 { .temp_c = 25, .ohm = 47000 },
99 { .temp_c = 30, .ohm = 37643 },
100 { .temp_c = 35, .ohm = 30334 },
101 { .temp_c = 40, .ohm = 24591 },
102 { .temp_c = 45, .ohm = 20048 },
103 { .temp_c = 50, .ohm = 16433 },
104 { .temp_c = 55, .ohm = 13539 },
105 { .temp_c = 60, .ohm = 11209 },
106 { .temp_c = 65, .ohm = 9328 },
107 { .temp_c = 70, .ohm = 7798 },
108 { .temp_c = 75, .ohm = 6544 },
109 { .temp_c = 80, .ohm = 5518 },
110 { .temp_c = 85, .ohm = 4674 },
111 { .temp_c = 90, .ohm = 3972 },
112 { .temp_c = 95, .ohm = 3388 },
113 { .temp_c = 100, .ohm = 2902 },
114 { .temp_c = 105, .ohm = 2494 },
115 { .temp_c = 110, .ohm = 2150 },
116 { .temp_c = 115, .ohm = 1860 },
117 { .temp_c = 120, .ohm = 1615 },
118 { .temp_c = 125, .ohm = 1406 },
120 static const struct ntc_compensation ncpXXwl333[] = {
121 { .temp_c = -40, .ohm = 1610154 },
122 { .temp_c = -35, .ohm = 1130850 },
123 { .temp_c = -30, .ohm = 802609 },
124 { .temp_c = -25, .ohm = 575385 },
125 { .temp_c = -20, .ohm = 416464 },
126 { .temp_c = -15, .ohm = 304219 },
127 { .temp_c = -10, .ohm = 224193 },
128 { .temp_c = -5, .ohm = 166623 },
129 { .temp_c = 0, .ohm = 124850 },
130 { .temp_c = 5, .ohm = 94287 },
131 { .temp_c = 10, .ohm = 71747 },
132 { .temp_c = 15, .ohm = 54996 },
133 { .temp_c = 20, .ohm = 42455 },
134 { .temp_c = 25, .ohm = 33000 },
135 { .temp_c = 30, .ohm = 25822 },
136 { .temp_c = 35, .ohm = 20335 },
137 { .temp_c = 40, .ohm = 16115 },
138 { .temp_c = 45, .ohm = 12849 },
139 { .temp_c = 50, .ohm = 10306 },
140 { .temp_c = 55, .ohm = 8314 },
141 { .temp_c = 60, .ohm = 6746 },
142 { .temp_c = 65, .ohm = 5503 },
143 { .temp_c = 70, .ohm = 4513 },
144 { .temp_c = 75, .ohm = 3721 },
145 { .temp_c = 80, .ohm = 3084 },
146 { .temp_c = 85, .ohm = 2569 },
147 { .temp_c = 90, .ohm = 2151 },
148 { .temp_c = 95, .ohm = 1809 },
149 { .temp_c = 100, .ohm = 1529 },
150 { .temp_c = 105, .ohm = 1299 },
151 { .temp_c = 110, .ohm = 1108 },
152 { .temp_c = 115, .ohm = 949 },
153 { .temp_c = 120, .ohm = 817 },
154 { .temp_c = 125, .ohm = 707 },
157 static const struct ntc_compensation ncpXXwf104[] = {
158 { .temp_c = -40, .ohm = 4397119 },
159 { .temp_c = -35, .ohm = 3088599 },
160 { .temp_c = -30, .ohm = 2197225 },
161 { .temp_c = -25, .ohm = 1581881 },
162 { .temp_c = -20, .ohm = 1151037 },
163 { .temp_c = -15, .ohm = 846579 },
164 { .temp_c = -10, .ohm = 628988 },
165 { .temp_c = -5, .ohm = 471632 },
166 { .temp_c = 0, .ohm = 357012 },
167 { .temp_c = 5, .ohm = 272500 },
168 { .temp_c = 10, .ohm = 209710 },
169 { .temp_c = 15, .ohm = 162651 },
170 { .temp_c = 20, .ohm = 127080 },
171 { .temp_c = 25, .ohm = 100000 },
172 { .temp_c = 30, .ohm = 79222 },
173 { .temp_c = 35, .ohm = 63167 },
174 { .temp_c = 40, .ohm = 50677 },
175 { .temp_c = 45, .ohm = 40904 },
176 { .temp_c = 50, .ohm = 33195 },
177 { .temp_c = 55, .ohm = 27091 },
178 { .temp_c = 60, .ohm = 22224 },
179 { .temp_c = 65, .ohm = 18323 },
180 { .temp_c = 70, .ohm = 15184 },
181 { .temp_c = 75, .ohm = 12635 },
182 { .temp_c = 80, .ohm = 10566 },
183 { .temp_c = 85, .ohm = 8873 },
184 { .temp_c = 90, .ohm = 7481 },
185 { .temp_c = 95, .ohm = 6337 },
186 { .temp_c = 100, .ohm = 5384 },
187 { .temp_c = 105, .ohm = 4594 },
188 { .temp_c = 110, .ohm = 3934 },
189 { .temp_c = 115, .ohm = 3380 },
190 { .temp_c = 120, .ohm = 2916 },
191 { .temp_c = 125, .ohm = 2522 },
194 static const struct ntc_compensation ncpXXxh103[] = {
195 { .temp_c = -40, .ohm = 247565 },
196 { .temp_c = -35, .ohm = 181742 },
197 { .temp_c = -30, .ohm = 135128 },
198 { .temp_c = -25, .ohm = 101678 },
199 { .temp_c = -20, .ohm = 77373 },
200 { .temp_c = -15, .ohm = 59504 },
201 { .temp_c = -10, .ohm = 46222 },
202 { .temp_c = -5, .ohm = 36244 },
203 { .temp_c = 0, .ohm = 28674 },
204 { .temp_c = 5, .ohm = 22878 },
205 { .temp_c = 10, .ohm = 18399 },
206 { .temp_c = 15, .ohm = 14910 },
207 { .temp_c = 20, .ohm = 12169 },
208 { .temp_c = 25, .ohm = 10000 },
209 { .temp_c = 30, .ohm = 8271 },
210 { .temp_c = 35, .ohm = 6883 },
211 { .temp_c = 40, .ohm = 5762 },
212 { .temp_c = 45, .ohm = 4851 },
213 { .temp_c = 50, .ohm = 4105 },
214 { .temp_c = 55, .ohm = 3492 },
215 { .temp_c = 60, .ohm = 2985 },
216 { .temp_c = 65, .ohm = 2563 },
217 { .temp_c = 70, .ohm = 2211 },
218 { .temp_c = 75, .ohm = 1915 },
219 { .temp_c = 80, .ohm = 1666 },
220 { .temp_c = 85, .ohm = 1454 },
221 { .temp_c = 90, .ohm = 1275 },
222 { .temp_c = 95, .ohm = 1121 },
223 { .temp_c = 100, .ohm = 990 },
224 { .temp_c = 105, .ohm = 876 },
225 { .temp_c = 110, .ohm = 779 },
226 { .temp_c = 115, .ohm = 694 },
227 { .temp_c = 120, .ohm = 620 },
228 { .temp_c = 125, .ohm = 556 },
232 * The following compensation tables are from the specifications in EPCOS NTC
233 * Thermistors Datasheets
235 static const struct ntc_compensation b57330v2103[] = {
236 { .temp_c = -40, .ohm = 190030 },
237 { .temp_c = -35, .ohm = 145360 },
238 { .temp_c = -30, .ohm = 112060 },
239 { .temp_c = -25, .ohm = 87041 },
240 { .temp_c = -20, .ohm = 68104 },
241 { .temp_c = -15, .ohm = 53665 },
242 { .temp_c = -10, .ohm = 42576 },
243 { .temp_c = -5, .ohm = 34001 },
244 { .temp_c = 0, .ohm = 27326 },
245 { .temp_c = 5, .ohm = 22096 },
246 { .temp_c = 10, .ohm = 17973 },
247 { .temp_c = 15, .ohm = 14703 },
248 { .temp_c = 20, .ohm = 12090 },
249 { .temp_c = 25, .ohm = 10000 },
250 { .temp_c = 30, .ohm = 8311 },
251 { .temp_c = 35, .ohm = 6941 },
252 { .temp_c = 40, .ohm = 5825 },
253 { .temp_c = 45, .ohm = 4911 },
254 { .temp_c = 50, .ohm = 4158 },
255 { .temp_c = 55, .ohm = 3536 },
256 { .temp_c = 60, .ohm = 3019 },
257 { .temp_c = 65, .ohm = 2588 },
258 { .temp_c = 70, .ohm = 2227 },
259 { .temp_c = 75, .ohm = 1924 },
260 { .temp_c = 80, .ohm = 1668 },
261 { .temp_c = 85, .ohm = 1451 },
262 { .temp_c = 90, .ohm = 1266 },
263 { .temp_c = 95, .ohm = 1108 },
264 { .temp_c = 100, .ohm = 973 },
265 { .temp_c = 105, .ohm = 857 },
266 { .temp_c = 110, .ohm = 757 },
267 { .temp_c = 115, .ohm = 671 },
268 { .temp_c = 120, .ohm = 596 },
269 { .temp_c = 125, .ohm = 531 },
272 static const struct ntc_compensation b57891s0103[] = {
273 { .temp_c = -55.0, .ohm = 878900 },
274 { .temp_c = -50.0, .ohm = 617590 },
275 { .temp_c = -45.0, .ohm = 439340 },
276 { .temp_c = -40.0, .ohm = 316180 },
277 { .temp_c = -35.0, .ohm = 230060 },
278 { .temp_c = -30.0, .ohm = 169150 },
279 { .temp_c = -25.0, .ohm = 125550 },
280 { .temp_c = -20.0, .ohm = 94143 },
281 { .temp_c = -15.0, .ohm = 71172 },
282 { .temp_c = -10.0, .ohm = 54308 },
283 { .temp_c = -5.0, .ohm = 41505 },
284 { .temp_c = 0.0, .ohm = 32014 },
285 { .temp_c = 5.0, .ohm = 25011 },
286 { .temp_c = 10.0, .ohm = 19691 },
287 { .temp_c = 15.0, .ohm = 15618 },
288 { .temp_c = 20.0, .ohm = 12474 },
289 { .temp_c = 25.0, .ohm = 10000 },
290 { .temp_c = 30.0, .ohm = 8080 },
291 { .temp_c = 35.0, .ohm = 6569 },
292 { .temp_c = 40.0, .ohm = 5372 },
293 { .temp_c = 45.0, .ohm = 4424 },
294 { .temp_c = 50.0, .ohm = 3661 },
295 { .temp_c = 55.0, .ohm = 3039 },
296 { .temp_c = 60.0, .ohm = 2536 },
297 { .temp_c = 65.0, .ohm = 2128 },
298 { .temp_c = 70.0, .ohm = 1794 },
299 { .temp_c = 75.0, .ohm = 1518 },
300 { .temp_c = 80.0, .ohm = 1290 },
301 { .temp_c = 85.0, .ohm = 1100 },
302 { .temp_c = 90.0, .ohm = 942 },
303 { .temp_c = 95.0, .ohm = 809 },
304 { .temp_c = 100.0, .ohm = 697 },
305 { .temp_c = 105.0, .ohm = 604 },
306 { .temp_c = 110.0, .ohm = 525 },
307 { .temp_c = 115.0, .ohm = 457 },
308 { .temp_c = 120.0, .ohm = 400 },
309 { .temp_c = 125.0, .ohm = 351 },
310 { .temp_c = 130.0, .ohm = 308 },
311 { .temp_c = 135.0, .ohm = 272 },
312 { .temp_c = 140.0, .ohm = 240 },
313 { .temp_c = 145.0, .ohm = 213 },
314 { .temp_c = 150.0, .ohm = 189 },
315 { .temp_c = 155.0, .ohm = 168 },
319 const struct ntc_compensation *comp;
323 #define NTC_TYPE(ntc, compensation) \
324 [(ntc)] = { .comp = (compensation), .n_comp = ARRAY_SIZE(compensation) }
326 static const struct ntc_type ntc_type[] = {
327 NTC_TYPE(TYPE_B57330V2103, b57330v2103),
328 NTC_TYPE(TYPE_B57891S0103, b57891s0103),
329 NTC_TYPE(TYPE_NCPXXWB473, ncpXXwb473),
330 NTC_TYPE(TYPE_NCPXXWF104, ncpXXwf104),
331 NTC_TYPE(TYPE_NCPXXWL333, ncpXXwl333),
332 NTC_TYPE(TYPE_NCPXXXH103, ncpXXxh103),
336 struct ntc_thermistor_platform_data *pdata;
337 const struct ntc_compensation *comp;
341 #if defined(CONFIG_OF) && IS_ENABLED(CONFIG_IIO)
342 static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata)
344 struct iio_channel *channel = pdata->chan;
347 ret = iio_read_channel_raw(channel, &raw);
349 pr_err("read channel() error: %d\n", ret);
353 ret = iio_convert_raw_to_processed(channel, raw, &uv, 1000);
355 /* Assume 12 bit ADC with vref at pullup_uv */
356 uv = (pdata->pullup_uv * (s64)raw) >> 12;
362 static const struct of_device_id ntc_match[] = {
363 { .compatible = "epcos,b57330v2103",
364 .data = &ntc_thermistor_id[NTC_B57330V2103]},
365 { .compatible = "epcos,b57891s0103",
366 .data = &ntc_thermistor_id[NTC_B57891S0103] },
367 { .compatible = "murata,ncp03wb473",
368 .data = &ntc_thermistor_id[NTC_NCP03WB473] },
369 { .compatible = "murata,ncp03wf104",
370 .data = &ntc_thermistor_id[NTC_NCP03WF104] },
371 { .compatible = "murata,ncp15wb473",
372 .data = &ntc_thermistor_id[NTC_NCP15WB473] },
373 { .compatible = "murata,ncp15wl333",
374 .data = &ntc_thermistor_id[NTC_NCP15WL333] },
375 { .compatible = "murata,ncp15xh103",
376 .data = &ntc_thermistor_id[NTC_NCP15XH103] },
377 { .compatible = "murata,ncp18wb473",
378 .data = &ntc_thermistor_id[NTC_NCP18WB473] },
379 { .compatible = "murata,ncp21wb473",
380 .data = &ntc_thermistor_id[NTC_NCP21WB473] },
382 /* Usage of vendor name "ntc" is deprecated */
383 { .compatible = "ntc,ncp03wb473",
384 .data = &ntc_thermistor_id[NTC_NCP03WB473] },
385 { .compatible = "ntc,ncp15wb473",
386 .data = &ntc_thermistor_id[NTC_NCP15WB473] },
387 { .compatible = "ntc,ncp15wl333",
388 .data = &ntc_thermistor_id[NTC_NCP15WL333] },
389 { .compatible = "ntc,ncp18wb473",
390 .data = &ntc_thermistor_id[NTC_NCP18WB473] },
391 { .compatible = "ntc,ncp21wb473",
392 .data = &ntc_thermistor_id[NTC_NCP21WB473] },
395 MODULE_DEVICE_TABLE(of, ntc_match);
397 static struct ntc_thermistor_platform_data *
398 ntc_thermistor_parse_dt(struct device *dev)
400 struct iio_channel *chan;
401 enum iio_chan_type type;
402 struct device_node *np = dev->of_node;
403 struct ntc_thermistor_platform_data *pdata;
409 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
411 return ERR_PTR(-ENOMEM);
413 chan = devm_iio_channel_get(dev, NULL);
415 return ERR_CAST(chan);
417 ret = iio_get_channel_type(chan, &type);
421 if (type != IIO_VOLTAGE)
422 return ERR_PTR(-EINVAL);
424 if (of_property_read_u32(np, "pullup-uv", &pdata->pullup_uv))
425 return ERR_PTR(-ENODEV);
426 if (of_property_read_u32(np, "pullup-ohm", &pdata->pullup_ohm))
427 return ERR_PTR(-ENODEV);
428 if (of_property_read_u32(np, "pulldown-ohm", &pdata->pulldown_ohm))
429 return ERR_PTR(-ENODEV);
431 if (of_find_property(np, "connected-positive", NULL))
432 pdata->connect = NTC_CONNECTED_POSITIVE;
433 else /* status change should be possible if not always on. */
434 pdata->connect = NTC_CONNECTED_GROUND;
437 pdata->read_uv = ntc_adc_iio_read;
442 static struct ntc_thermistor_platform_data *
443 ntc_thermistor_parse_dt(struct device *dev)
448 #define ntc_match NULL
452 static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
454 if (divisor == 0 && dividend == 0)
458 return div64_u64(dividend, divisor);
461 static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
463 struct ntc_thermistor_platform_data *pdata = data->pdata;
464 u32 puv = pdata->pullup_uv;
466 puo = pdata->pullup_ohm;
467 pdo = pdata->pulldown_ohm;
470 return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
473 return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
476 if (pdata->connect == NTC_CONNECTED_POSITIVE && puo == 0)
477 n = div_u64(pdo * (puv - uv), uv);
478 else if (pdata->connect == NTC_CONNECTED_GROUND && pdo == 0)
479 n = div_u64(puo * uv, puv - uv);
480 else if (pdata->connect == NTC_CONNECTED_POSITIVE)
481 n = div64_u64_safe(pdo * puo * (puv - uv),
482 puo * uv - pdo * (puv - uv));
484 n = div64_u64_safe(pdo * puo * uv, pdo * (puv - uv) - puo * uv);
491 static void lookup_comp(struct ntc_data *data, unsigned int ohm,
492 int *i_low, int *i_high)
497 * Handle special cases: Resistance is higher than or equal to
498 * resistance in first table entry, or resistance is lower or equal
499 * to resistance in last table entry.
500 * In these cases, return i_low == i_high, either pointing to the
501 * beginning or to the end of the table depending on the condition.
503 if (ohm >= data->comp[0].ohm) {
508 if (ohm <= data->comp[data->n_comp - 1].ohm) {
509 *i_low = data->n_comp - 1;
510 *i_high = data->n_comp - 1;
514 /* Do a binary search on compensation table */
517 while (start < end) {
518 mid = start + (end - start) / 2;
521 * data->comp[start].ohm > ohm >= data->comp[end].ohm
523 * We could check for "ohm == data->comp[mid].ohm" here, but
524 * that is a quite unlikely condition, and we would have to
525 * check again after updating start. Check it at the end instead
528 if (ohm >= data->comp[mid].ohm) {
533 * ohm >= data->comp[start].ohm might be true here,
534 * since we set start to mid + 1. In that case, we are
535 * done. We could keep going, but the condition is quite
536 * likely to occur, so it is worth checking for it.
538 if (ohm >= data->comp[start].ohm)
543 * data->comp[start].ohm >= ohm >= data->comp[end].ohm
548 * ohm >= data->comp[end].ohm
551 if (ohm == data->comp[end].ohm)
557 static int get_temp_mc(struct ntc_data *data, unsigned int ohm)
562 lookup_comp(data, ohm, &low, &high);
564 /* Unable to use linear approximation */
565 temp = data->comp[low].temp_c * 1000;
567 temp = data->comp[low].temp_c * 1000 +
568 ((data->comp[high].temp_c - data->comp[low].temp_c) *
569 1000 * ((int)ohm - (int)data->comp[low].ohm)) /
570 ((int)data->comp[high].ohm - (int)data->comp[low].ohm);
575 static int ntc_thermistor_get_ohm(struct ntc_data *data)
579 if (data->pdata->read_ohm)
580 return data->pdata->read_ohm();
582 if (data->pdata->read_uv) {
583 read_uv = data->pdata->read_uv(data->pdata);
586 return get_ohm_of_thermistor(data, read_uv);
591 static int ntc_read_temp(void *data, int *temp)
595 ohm = ntc_thermistor_get_ohm(data);
599 *temp = get_temp_mc(data, ohm);
604 static ssize_t ntc_type_show(struct device *dev,
605 struct device_attribute *attr, char *buf)
607 return sprintf(buf, "4\n");
610 static ssize_t ntc_temp_show(struct device *dev,
611 struct device_attribute *attr, char *buf)
613 struct ntc_data *data = dev_get_drvdata(dev);
616 ohm = ntc_thermistor_get_ohm(data);
620 return sprintf(buf, "%d\n", get_temp_mc(data, ohm));
623 static SENSOR_DEVICE_ATTR_RO(temp1_type, ntc_type, 0);
624 static SENSOR_DEVICE_ATTR_RO(temp1_input, ntc_temp, 0);
626 static struct attribute *ntc_attrs[] = {
627 &sensor_dev_attr_temp1_type.dev_attr.attr,
628 &sensor_dev_attr_temp1_input.dev_attr.attr,
631 ATTRIBUTE_GROUPS(ntc);
633 static const struct thermal_zone_of_device_ops ntc_of_thermal_ops = {
634 .get_temp = ntc_read_temp,
637 static int ntc_thermistor_probe(struct platform_device *pdev)
639 struct thermal_zone_device *tz;
640 struct device *dev = &pdev->dev;
641 const struct of_device_id *of_id =
642 of_match_device(of_match_ptr(ntc_match), dev);
643 const struct platform_device_id *pdev_id;
644 struct ntc_thermistor_platform_data *pdata;
645 struct device *hwmon_dev;
646 struct ntc_data *data;
648 pdata = ntc_thermistor_parse_dt(dev);
650 return PTR_ERR(pdata);
651 else if (pdata == NULL)
652 pdata = dev_get_platdata(dev);
655 dev_err(dev, "No platform init data supplied.\n");
659 /* Either one of the two is required. */
660 if (!pdata->read_uv && !pdata->read_ohm) {
662 "Both read_uv and read_ohm missing. Need either one of the two.\n");
666 if (pdata->read_uv && pdata->read_ohm) {
668 "Only one of read_uv and read_ohm is needed; ignoring read_uv.\n");
669 pdata->read_uv = NULL;
672 if (pdata->read_uv && (pdata->pullup_uv == 0 ||
673 (pdata->pullup_ohm == 0 && pdata->connect ==
674 NTC_CONNECTED_GROUND) ||
675 (pdata->pulldown_ohm == 0 && pdata->connect ==
676 NTC_CONNECTED_POSITIVE) ||
677 (pdata->connect != NTC_CONNECTED_POSITIVE &&
678 pdata->connect != NTC_CONNECTED_GROUND))) {
679 dev_err(dev, "Required data to use read_uv not supplied.\n");
683 data = devm_kzalloc(dev, sizeof(struct ntc_data), GFP_KERNEL);
687 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
691 if (pdev_id->driver_data >= ARRAY_SIZE(ntc_type)) {
692 dev_err(dev, "Unknown device type: %lu(%s)\n",
693 pdev_id->driver_data, pdev_id->name);
697 data->comp = ntc_type[pdev_id->driver_data].comp;
698 data->n_comp = ntc_type[pdev_id->driver_data].n_comp;
700 hwmon_dev = devm_hwmon_device_register_with_groups(dev, pdev_id->name,
702 if (IS_ERR(hwmon_dev)) {
703 dev_err(dev, "unable to register as hwmon device.\n");
704 return PTR_ERR(hwmon_dev);
707 dev_info(dev, "Thermistor type: %s successfully probed.\n",
710 tz = devm_thermal_zone_of_sensor_register(dev, 0, data,
711 &ntc_of_thermal_ops);
713 dev_dbg(dev, "Failed to register to thermal fw.\n");
718 static struct platform_driver ntc_thermistor_driver = {
720 .name = "ntc-thermistor",
721 .of_match_table = of_match_ptr(ntc_match),
723 .probe = ntc_thermistor_probe,
724 .id_table = ntc_thermistor_id,
727 module_platform_driver(ntc_thermistor_driver);
729 MODULE_DESCRIPTION("NTC Thermistor Driver");
730 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
731 MODULE_LICENSE("GPL");
732 MODULE_ALIAS("platform:ntc-thermistor");