Merge tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
[sfrench/cifs-2.6.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
41 struct ntc_compensation {
42         int             temp_c;
43         unsigned int    ohm;
44 };
45
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
63 static const struct platform_device_id ntc_thermistor_id[] = {
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]            = { },
74 };
75
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  */
82 static const struct ntc_compensation ncpXXwb473[] = {
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 },
117 };
118 static const struct ntc_compensation ncpXXwl333[] = {
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 },
153 };
154
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
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
229 /*
230  * The following compensation tables are from the specifications in EPCOS NTC
231  * Thermistors Datasheets
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
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
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
333 struct ntc_data {
334         struct ntc_thermistor_platform_data *pdata;
335         const struct ntc_compensation *comp;
336         int n_comp;
337 };
338
339 #if defined(CONFIG_OF) && IS_ENABLED(CONFIG_IIO)
340 static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata)
341 {
342         struct iio_channel *channel = pdata->chan;
343         int raw, uv, ret;
344
345         ret = iio_read_channel_raw(channel, &raw);
346         if (ret < 0) {
347                 pr_err("read channel() error: %d\n", ret);
348                 return ret;
349         }
350
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         }
356
357         return uv;
358 }
359
360 static const struct of_device_id ntc_match[] = {
361         { .compatible = "epcos,b57330v2103",
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] },
367         { .compatible = "murata,ncp03wf104",
368                 .data = &ntc_thermistor_id[NTC_NCP03WF104] },
369         { .compatible = "murata,ncp15wb473",
370                 .data = &ntc_thermistor_id[NTC_NCP15WB473] },
371         { .compatible = "murata,ncp15wl333",
372                 .data = &ntc_thermistor_id[NTC_NCP15WL333] },
373         { .compatible = "murata,ncp15xh103",
374                 .data = &ntc_thermistor_id[NTC_NCP15XH103] },
375         { .compatible = "murata,ncp18wb473",
376                 .data = &ntc_thermistor_id[NTC_NCP18WB473] },
377         { .compatible = "murata,ncp21wb473",
378                 .data = &ntc_thermistor_id[NTC_NCP21WB473] },
379
380         /* Usage of vendor name "ntc" is deprecated */
381         { .compatible = "ntc,ncp03wb473",
382                 .data = &ntc_thermistor_id[NTC_NCP03WB473] },
383         { .compatible = "ntc,ncp15wb473",
384                 .data = &ntc_thermistor_id[NTC_NCP15WB473] },
385         { .compatible = "ntc,ncp15wl333",
386                 .data = &ntc_thermistor_id[NTC_NCP15WL333] },
387         { .compatible = "ntc,ncp18wb473",
388                 .data = &ntc_thermistor_id[NTC_NCP18WB473] },
389         { .compatible = "ntc,ncp21wb473",
390                 .data = &ntc_thermistor_id[NTC_NCP21WB473] },
391         { },
392 };
393 MODULE_DEVICE_TABLE(of, ntc_match);
394
395 static struct ntc_thermistor_platform_data *
396 ntc_thermistor_parse_dt(struct device *dev)
397 {
398         struct iio_channel *chan;
399         enum iio_chan_type type;
400         struct device_node *np = dev->of_node;
401         struct ntc_thermistor_platform_data *pdata;
402         int ret;
403
404         if (!np)
405                 return NULL;
406
407         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
408         if (!pdata)
409                 return ERR_PTR(-ENOMEM);
410
411         chan = devm_iio_channel_get(dev, NULL);
412         if (IS_ERR(chan))
413                 return ERR_CAST(chan);
414
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
422         if (of_property_read_u32(np, "pullup-uv", &pdata->pullup_uv))
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;
435         pdata->read_uv = ntc_adc_iio_read;
436
437         return pdata;
438 }
439 #else
440 static struct ntc_thermistor_platform_data *
441 ntc_thermistor_parse_dt(struct device *dev)
442 {
443         return NULL;
444 }
445
446 #define ntc_match       NULL
447
448 #endif
449
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
459 static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
460 {
461         struct ntc_thermistor_platform_data *pdata = data->pdata;
462         u32 puv = pdata->pullup_uv;
463         u64 n, puo, pdo;
464         puo = pdata->pullup_ohm;
465         pdo = pdata->pulldown_ohm;
466
467         if (uv == 0)
468                 return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
469                         INT_MAX : 0;
470         if (uv >= puv)
471                 return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
472                         0 : INT_MAX;
473
474         if (pdata->connect == NTC_CONNECTED_POSITIVE && puo == 0)
475                 n = div_u64(pdo * (puv - uv), uv);
476         else if (pdata->connect == NTC_CONNECTED_GROUND && pdo == 0)
477                 n = div_u64(puo * uv, puv - uv);
478         else if (pdata->connect == NTC_CONNECTED_POSITIVE)
479                 n = div64_u64_safe(pdo * puo * (puv - uv),
480                                 puo * uv - pdo * (puv - uv));
481         else
482                 n = div64_u64_safe(pdo * puo * uv, pdo * (puv - uv) - puo * uv);
483
484         if (n > INT_MAX)
485                 n = INT_MAX;
486         return n;
487 }
488
489 static void lookup_comp(struct ntc_data *data, unsigned int ohm,
490                         int *i_low, int *i_high)
491 {
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         }
511
512         /* Do a binary search on compensation table */
513         start = 0;
514         end = data->n_comp;
515         while (start < end) {
516                 mid = start + (end - start) / 2;
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) {
527                         end = mid;
528                 } else {
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;
538                 }
539                 /*
540                  * start <= end
541                  * data->comp[start].ohm >= ohm >= data->comp[end].ohm
542                  */
543         }
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;
553 }
554
555 static int get_temp_mc(struct ntc_data *data, unsigned int ohm)
556 {
557         int low, high;
558         int temp;
559
560         lookup_comp(data, ohm, &low, &high);
561         if (low == high) {
562                 /* Unable to use linear approximation */
563                 temp = data->comp[low].temp_c * 1000;
564         } else {
565                 temp = data->comp[low].temp_c * 1000 +
566                         ((data->comp[high].temp_c - data->comp[low].temp_c) *
567                          1000 * ((int)ohm - (int)data->comp[low].ohm)) /
568                         ((int)data->comp[high].ohm - (int)data->comp[low].ohm);
569         }
570         return temp;
571 }
572
573 static int ntc_thermistor_get_ohm(struct ntc_data *data)
574 {
575         int read_uv;
576
577         if (data->pdata->read_ohm)
578                 return data->pdata->read_ohm();
579
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);
585         }
586         return -EINVAL;
587 }
588
589 static int ntc_read(struct device *dev, enum hwmon_sensor_types type,
590                     u32 attr, int channel, long *val)
591 {
592         struct ntc_data *data = dev_get_drvdata(dev);
593         int ohm;
594
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;
615 }
616
617 static umode_t ntc_is_visible(const void *data, enum hwmon_sensor_types type,
618                               u32 attr, int channel)
619 {
620         if (type == hwmon_temp) {
621                 switch (attr) {
622                 case hwmon_temp_input:
623                 case hwmon_temp_type:
624                         return 0444;
625                 default:
626                         break;
627                 }
628         }
629         return 0;
630 }
631
632 static const struct hwmon_channel_info *ntc_info[] = {
633         HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
634         HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_TYPE),
635         NULL
636 };
637
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,
646 };
647
648 static int ntc_thermistor_probe(struct platform_device *pdev)
649 {
650         struct device *dev = &pdev->dev;
651         const struct of_device_id *of_id =
652                         of_match_device(of_match_ptr(ntc_match), dev);
653         const struct platform_device_id *pdev_id;
654         struct ntc_thermistor_platform_data *pdata;
655         struct device *hwmon_dev;
656         struct ntc_data *data;
657
658         pdata = ntc_thermistor_parse_dt(dev);
659         if (IS_ERR(pdata))
660                 return PTR_ERR(pdata);
661         else if (pdata == NULL)
662                 pdata = dev_get_platdata(dev);
663
664         if (!pdata) {
665                 dev_err(dev, "No platform init data supplied.\n");
666                 return -ENODEV;
667         }
668
669         /* Either one of the two is required. */
670         if (!pdata->read_uv && !pdata->read_ohm) {
671                 dev_err(dev,
672                         "Both read_uv and read_ohm missing. Need either one of the two.\n");
673                 return -EINVAL;
674         }
675
676         if (pdata->read_uv && pdata->read_ohm) {
677                 dev_warn(dev,
678                          "Only one of read_uv and read_ohm is needed; ignoring read_uv.\n");
679                 pdata->read_uv = NULL;
680         }
681
682         if (pdata->read_uv && (pdata->pullup_uv == 0 ||
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))) {
689                 dev_err(dev, "Required data to use read_uv not supplied.\n");
690                 return -EINVAL;
691         }
692
693         data = devm_kzalloc(dev, sizeof(struct ntc_data), GFP_KERNEL);
694         if (!data)
695                 return -ENOMEM;
696
697         pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
698
699         data->pdata = pdata;
700
701         if (pdev_id->driver_data >= ARRAY_SIZE(ntc_type)) {
702                 dev_err(dev, "Unknown device type: %lu(%s)\n",
703                                 pdev_id->driver_data, pdev_id->name);
704                 return -EINVAL;
705         }
706
707         data->comp   = ntc_type[pdev_id->driver_data].comp;
708         data->n_comp = ntc_type[pdev_id->driver_data].n_comp;
709
710         hwmon_dev = devm_hwmon_device_register_with_info(dev, pdev_id->name,
711                                                          data, &ntc_chip_info,
712                                                          NULL);
713         if (IS_ERR(hwmon_dev)) {
714                 dev_err(dev, "unable to register as hwmon device.\n");
715                 return PTR_ERR(hwmon_dev);
716         }
717
718         dev_info(dev, "Thermistor type: %s successfully probed.\n",
719                  pdev_id->name);
720
721         return 0;
722 }
723
724 static struct platform_driver ntc_thermistor_driver = {
725         .driver = {
726                 .name = "ntc-thermistor",
727                 .of_match_table = of_match_ptr(ntc_match),
728         },
729         .probe = ntc_thermistor_probe,
730         .id_table = ntc_thermistor_id,
731 };
732
733 module_platform_driver(ntc_thermistor_driver);
734
735 MODULE_DESCRIPTION("NTC Thermistor Driver");
736 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
737 MODULE_LICENSE("GPL");
738 MODULE_ALIAS("platform:ntc-thermistor");