]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/hwmon/ntc_thermistor.c
Merge tag 'fbdev-v5.0-rc3' of git://github.com/bzolnier/linux
[linux.git] / drivers / hwmon / ntc_thermistor.c
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>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31
32 #include <linux/platform_data/ntc_thermistor.h>
33
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
39 #include <linux/hwmon.h>
40 #include <linux/hwmon-sysfs.h>
41 #include <linux/thermal.h>
42
43 struct ntc_compensation {
44         int             temp_c;
45         unsigned int    ohm;
46 };
47
48 /*
49  * Used as index in a zero-terminated array, holes not allowed so
50  * that NTC_LAST is the first empty array entry.
51  */
52 enum {
53         NTC_B57330V2103,
54         NTC_B57891S0103,
55         NTC_NCP03WB473,
56         NTC_NCP03WF104,
57         NTC_NCP15WB473,
58         NTC_NCP15WL333,
59         NTC_NCP15XH103,
60         NTC_NCP18WB473,
61         NTC_NCP21WB473,
62         NTC_LAST,
63 };
64
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 },
75         [NTC_LAST]            = { },
76 };
77
78 /*
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
83  */
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 },
119 };
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 },
155 };
156
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 },
192 };
193
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 },
229 };
230
231 /*
232  * The following compensation tables are from the specifications in EPCOS NTC
233  * Thermistors Datasheets
234  */
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 },
270 };
271
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 },
316 };
317
318 struct ntc_type {
319         const struct ntc_compensation *comp;
320         int n_comp;
321 };
322
323 #define NTC_TYPE(ntc, compensation) \
324 [(ntc)] = { .comp = (compensation), .n_comp = ARRAY_SIZE(compensation) }
325
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),
333 };
334
335 struct ntc_data {
336         struct ntc_thermistor_platform_data *pdata;
337         const struct ntc_compensation *comp;
338         int n_comp;
339 };
340
341 #if defined(CONFIG_OF) && IS_ENABLED(CONFIG_IIO)
342 static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata)
343 {
344         struct iio_channel *channel = pdata->chan;
345         int raw, uv, ret;
346
347         ret = iio_read_channel_raw(channel, &raw);
348         if (ret < 0) {
349                 pr_err("read channel() error: %d\n", ret);
350                 return ret;
351         }
352
353         ret = iio_convert_raw_to_processed(channel, raw, &uv, 1000);
354         if (ret < 0) {
355                 /* Assume 12 bit ADC with vref at pullup_uv */
356                 uv = (pdata->pullup_uv * (s64)raw) >> 12;
357         }
358
359         return uv;
360 }
361
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] },
381
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] },
393         { },
394 };
395 MODULE_DEVICE_TABLE(of, ntc_match);
396
397 static struct ntc_thermistor_platform_data *
398 ntc_thermistor_parse_dt(struct device *dev)
399 {
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;
404         int ret;
405
406         if (!np)
407                 return NULL;
408
409         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
410         if (!pdata)
411                 return ERR_PTR(-ENOMEM);
412
413         chan = devm_iio_channel_get(dev, NULL);
414         if (IS_ERR(chan))
415                 return ERR_CAST(chan);
416
417         ret = iio_get_channel_type(chan, &type);
418         if (ret < 0)
419                 return ERR_PTR(ret);
420
421         if (type != IIO_VOLTAGE)
422                 return ERR_PTR(-EINVAL);
423
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);
430
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;
435
436         pdata->chan = chan;
437         pdata->read_uv = ntc_adc_iio_read;
438
439         return pdata;
440 }
441 #else
442 static struct ntc_thermistor_platform_data *
443 ntc_thermistor_parse_dt(struct device *dev)
444 {
445         return NULL;
446 }
447
448 #define ntc_match       NULL
449
450 #endif
451
452 static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
453 {
454         if (divisor == 0 && dividend == 0)
455                 return 0;
456         if (divisor == 0)
457                 return UINT_MAX;
458         return div64_u64(dividend, divisor);
459 }
460
461 static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
462 {
463         struct ntc_thermistor_platform_data *pdata = data->pdata;
464         u32 puv = pdata->pullup_uv;
465         u64 n, puo, pdo;
466         puo = pdata->pullup_ohm;
467         pdo = pdata->pulldown_ohm;
468
469         if (uv == 0)
470                 return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
471                         INT_MAX : 0;
472         if (uv >= puv)
473                 return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
474                         0 : INT_MAX;
475
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));
483         else
484                 n = div64_u64_safe(pdo * puo * uv, pdo * (puv - uv) - puo * uv);
485
486         if (n > INT_MAX)
487                 n = INT_MAX;
488         return n;
489 }
490
491 static void lookup_comp(struct ntc_data *data, unsigned int ohm,
492                         int *i_low, int *i_high)
493 {
494         int start, end, mid;
495
496         /*
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.
502          */
503         if (ohm >= data->comp[0].ohm) {
504                 *i_low = 0;
505                 *i_high = 0;
506                 return;
507         }
508         if (ohm <= data->comp[data->n_comp - 1].ohm) {
509                 *i_low = data->n_comp - 1;
510                 *i_high = data->n_comp - 1;
511                 return;
512         }
513
514         /* Do a binary search on compensation table */
515         start = 0;
516         end = data->n_comp;
517         while (start < end) {
518                 mid = start + (end - start) / 2;
519                 /*
520                  * start <= mid < end
521                  * data->comp[start].ohm > ohm >= data->comp[end].ohm
522                  *
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
526                  * for simplicity.
527                  */
528                 if (ohm >= data->comp[mid].ohm) {
529                         end = mid;
530                 } else {
531                         start = mid + 1;
532                         /*
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.
537                          */
538                         if (ohm >= data->comp[start].ohm)
539                                 end = start;
540                 }
541                 /*
542                  * start <= end
543                  * data->comp[start].ohm >= ohm >= data->comp[end].ohm
544                  */
545         }
546         /*
547          * start == end
548          * ohm >= data->comp[end].ohm
549          */
550         *i_low = end;
551         if (ohm == data->comp[end].ohm)
552                 *i_high = end;
553         else
554                 *i_high = end - 1;
555 }
556
557 static int get_temp_mc(struct ntc_data *data, unsigned int ohm)
558 {
559         int low, high;
560         int temp;
561
562         lookup_comp(data, ohm, &low, &high);
563         if (low == high) {
564                 /* Unable to use linear approximation */
565                 temp = data->comp[low].temp_c * 1000;
566         } else {
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);
571         }
572         return temp;
573 }
574
575 static int ntc_thermistor_get_ohm(struct ntc_data *data)
576 {
577         int read_uv;
578
579         if (data->pdata->read_ohm)
580                 return data->pdata->read_ohm();
581
582         if (data->pdata->read_uv) {
583                 read_uv = data->pdata->read_uv(data->pdata);
584                 if (read_uv < 0)
585                         return read_uv;
586                 return get_ohm_of_thermistor(data, read_uv);
587         }
588         return -EINVAL;
589 }
590
591 static int ntc_read_temp(void *data, int *temp)
592 {
593         int ohm;
594
595         ohm = ntc_thermistor_get_ohm(data);
596         if (ohm < 0)
597                 return ohm;
598
599         *temp = get_temp_mc(data, ohm);
600
601         return 0;
602 }
603
604 static ssize_t ntc_type_show(struct device *dev,
605                              struct device_attribute *attr, char *buf)
606 {
607         return sprintf(buf, "4\n");
608 }
609
610 static ssize_t ntc_temp_show(struct device *dev,
611                              struct device_attribute *attr, char *buf)
612 {
613         struct ntc_data *data = dev_get_drvdata(dev);
614         int ohm;
615
616         ohm = ntc_thermistor_get_ohm(data);
617         if (ohm < 0)
618                 return ohm;
619
620         return sprintf(buf, "%d\n", get_temp_mc(data, ohm));
621 }
622
623 static SENSOR_DEVICE_ATTR_RO(temp1_type, ntc_type, 0);
624 static SENSOR_DEVICE_ATTR_RO(temp1_input, ntc_temp, 0);
625
626 static struct attribute *ntc_attrs[] = {
627         &sensor_dev_attr_temp1_type.dev_attr.attr,
628         &sensor_dev_attr_temp1_input.dev_attr.attr,
629         NULL,
630 };
631 ATTRIBUTE_GROUPS(ntc);
632
633 static const struct thermal_zone_of_device_ops ntc_of_thermal_ops = {
634         .get_temp = ntc_read_temp,
635 };
636
637 static int ntc_thermistor_probe(struct platform_device *pdev)
638 {
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;
647
648         pdata = ntc_thermistor_parse_dt(dev);
649         if (IS_ERR(pdata))
650                 return PTR_ERR(pdata);
651         else if (pdata == NULL)
652                 pdata = dev_get_platdata(dev);
653
654         if (!pdata) {
655                 dev_err(dev, "No platform init data supplied.\n");
656                 return -ENODEV;
657         }
658
659         /* Either one of the two is required. */
660         if (!pdata->read_uv && !pdata->read_ohm) {
661                 dev_err(dev,
662                         "Both read_uv and read_ohm missing. Need either one of the two.\n");
663                 return -EINVAL;
664         }
665
666         if (pdata->read_uv && pdata->read_ohm) {
667                 dev_warn(dev,
668                          "Only one of read_uv and read_ohm is needed; ignoring read_uv.\n");
669                 pdata->read_uv = NULL;
670         }
671
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");
680                 return -EINVAL;
681         }
682
683         data = devm_kzalloc(dev, sizeof(struct ntc_data), GFP_KERNEL);
684         if (!data)
685                 return -ENOMEM;
686
687         pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
688
689         data->pdata = pdata;
690
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);
694                 return -EINVAL;
695         }
696
697         data->comp   = ntc_type[pdev_id->driver_data].comp;
698         data->n_comp = ntc_type[pdev_id->driver_data].n_comp;
699
700         hwmon_dev = devm_hwmon_device_register_with_groups(dev, pdev_id->name,
701                                                            data, ntc_groups);
702         if (IS_ERR(hwmon_dev)) {
703                 dev_err(dev, "unable to register as hwmon device.\n");
704                 return PTR_ERR(hwmon_dev);
705         }
706
707         dev_info(dev, "Thermistor type: %s successfully probed.\n",
708                  pdev_id->name);
709
710         tz = devm_thermal_zone_of_sensor_register(dev, 0, data,
711                                                   &ntc_of_thermal_ops);
712         if (IS_ERR(tz))
713                 dev_dbg(dev, "Failed to register to thermal fw.\n");
714
715         return 0;
716 }
717
718 static struct platform_driver ntc_thermistor_driver = {
719         .driver = {
720                 .name = "ntc-thermistor",
721                 .of_match_table = of_match_ptr(ntc_match),
722         },
723         .probe = ntc_thermistor_probe,
724         .id_table = ntc_thermistor_id,
725 };
726
727 module_platform_driver(ntc_thermistor_driver);
728
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");