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