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 
42 struct ntc_compensation {
43 	int		temp_c;
44 	unsigned int	ohm;
45 };
46 
47 /* Order matters, ntc_match references the entries by index */
48 static const struct platform_device_id ntc_thermistor_id[] = {
49 	{ "ncp15wb473", TYPE_NCPXXWB473 },
50 	{ "ncp18wb473", TYPE_NCPXXWB473 },
51 	{ "ncp21wb473", TYPE_NCPXXWB473 },
52 	{ "ncp03wb473", TYPE_NCPXXWB473 },
53 	{ "ncp15wl333", TYPE_NCPXXWL333 },
54 	{ },
55 };
56 
57 /*
58  * A compensation table should be sorted by the values of .ohm
59  * in descending order.
60  * The following compensation tables are from the specification of Murata NTC
61  * Thermistors Datasheet
62  */
63 static const struct ntc_compensation ncpXXwb473[] = {
64 	{ .temp_c	= -40, .ohm	= 1747920 },
65 	{ .temp_c	= -35, .ohm	= 1245428 },
66 	{ .temp_c	= -30, .ohm	= 898485 },
67 	{ .temp_c	= -25, .ohm	= 655802 },
68 	{ .temp_c	= -20, .ohm	= 483954 },
69 	{ .temp_c	= -15, .ohm	= 360850 },
70 	{ .temp_c	= -10, .ohm	= 271697 },
71 	{ .temp_c	= -5, .ohm	= 206463 },
72 	{ .temp_c	= 0, .ohm	= 158214 },
73 	{ .temp_c	= 5, .ohm	= 122259 },
74 	{ .temp_c	= 10, .ohm	= 95227 },
75 	{ .temp_c	= 15, .ohm	= 74730 },
76 	{ .temp_c	= 20, .ohm	= 59065 },
77 	{ .temp_c	= 25, .ohm	= 47000 },
78 	{ .temp_c	= 30, .ohm	= 37643 },
79 	{ .temp_c	= 35, .ohm	= 30334 },
80 	{ .temp_c	= 40, .ohm	= 24591 },
81 	{ .temp_c	= 45, .ohm	= 20048 },
82 	{ .temp_c	= 50, .ohm	= 16433 },
83 	{ .temp_c	= 55, .ohm	= 13539 },
84 	{ .temp_c	= 60, .ohm	= 11209 },
85 	{ .temp_c	= 65, .ohm	= 9328 },
86 	{ .temp_c	= 70, .ohm	= 7798 },
87 	{ .temp_c	= 75, .ohm	= 6544 },
88 	{ .temp_c	= 80, .ohm	= 5518 },
89 	{ .temp_c	= 85, .ohm	= 4674 },
90 	{ .temp_c	= 90, .ohm	= 3972 },
91 	{ .temp_c	= 95, .ohm	= 3388 },
92 	{ .temp_c	= 100, .ohm	= 2902 },
93 	{ .temp_c	= 105, .ohm	= 2494 },
94 	{ .temp_c	= 110, .ohm	= 2150 },
95 	{ .temp_c	= 115, .ohm	= 1860 },
96 	{ .temp_c	= 120, .ohm	= 1615 },
97 	{ .temp_c	= 125, .ohm	= 1406 },
98 };
99 static const struct ntc_compensation ncpXXwl333[] = {
100 	{ .temp_c	= -40, .ohm	= 1610154 },
101 	{ .temp_c	= -35, .ohm	= 1130850 },
102 	{ .temp_c	= -30, .ohm	= 802609 },
103 	{ .temp_c	= -25, .ohm	= 575385 },
104 	{ .temp_c	= -20, .ohm	= 416464 },
105 	{ .temp_c	= -15, .ohm	= 304219 },
106 	{ .temp_c	= -10, .ohm	= 224193 },
107 	{ .temp_c	= -5, .ohm	= 166623 },
108 	{ .temp_c	= 0, .ohm	= 124850 },
109 	{ .temp_c	= 5, .ohm	= 94287 },
110 	{ .temp_c	= 10, .ohm	= 71747 },
111 	{ .temp_c	= 15, .ohm	= 54996 },
112 	{ .temp_c	= 20, .ohm	= 42455 },
113 	{ .temp_c	= 25, .ohm	= 33000 },
114 	{ .temp_c	= 30, .ohm	= 25822 },
115 	{ .temp_c	= 35, .ohm	= 20335 },
116 	{ .temp_c	= 40, .ohm	= 16115 },
117 	{ .temp_c	= 45, .ohm	= 12849 },
118 	{ .temp_c	= 50, .ohm	= 10306 },
119 	{ .temp_c	= 55, .ohm	= 8314 },
120 	{ .temp_c	= 60, .ohm	= 6746 },
121 	{ .temp_c	= 65, .ohm	= 5503 },
122 	{ .temp_c	= 70, .ohm	= 4513 },
123 	{ .temp_c	= 75, .ohm	= 3721 },
124 	{ .temp_c	= 80, .ohm	= 3084 },
125 	{ .temp_c	= 85, .ohm	= 2569 },
126 	{ .temp_c	= 90, .ohm	= 2151 },
127 	{ .temp_c	= 95, .ohm	= 1809 },
128 	{ .temp_c	= 100, .ohm	= 1529 },
129 	{ .temp_c	= 105, .ohm	= 1299 },
130 	{ .temp_c	= 110, .ohm	= 1108 },
131 	{ .temp_c	= 115, .ohm	= 949 },
132 	{ .temp_c	= 120, .ohm	= 817 },
133 	{ .temp_c	= 125, .ohm	= 707 },
134 };
135 
136 struct ntc_data {
137 	struct device *hwmon_dev;
138 	struct ntc_thermistor_platform_data *pdata;
139 	const struct ntc_compensation *comp;
140 	struct device *dev;
141 	int n_comp;
142 	char name[PLATFORM_NAME_SIZE];
143 };
144 
145 #if defined(CONFIG_OF) && IS_ENABLED(CONFIG_IIO)
146 static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata)
147 {
148 	struct iio_channel *channel = pdata->chan;
149 	s64 result;
150 	int val, ret;
151 
152 	ret = iio_read_channel_raw(channel, &val);
153 	if (ret < 0) {
154 		pr_err("read channel() error: %d\n", ret);
155 		return ret;
156 	}
157 
158 	/* unit: mV */
159 	result = pdata->pullup_uv * (s64) val;
160 	result >>= 12;
161 
162 	return (int)result;
163 }
164 
165 static const struct of_device_id ntc_match[] = {
166 	{ .compatible = "ntc,ncp15wb473",
167 		.data = &ntc_thermistor_id[0] },
168 	{ .compatible = "ntc,ncp18wb473",
169 		.data = &ntc_thermistor_id[1] },
170 	{ .compatible = "ntc,ncp21wb473",
171 		.data = &ntc_thermistor_id[2] },
172 	{ .compatible = "ntc,ncp03wb473",
173 		.data = &ntc_thermistor_id[3] },
174 	{ .compatible = "ntc,ncp15wl333",
175 		.data = &ntc_thermistor_id[4] },
176 	{ },
177 };
178 MODULE_DEVICE_TABLE(of, ntc_match);
179 
180 static struct ntc_thermistor_platform_data *
181 ntc_thermistor_parse_dt(struct platform_device *pdev)
182 {
183 	struct iio_channel *chan;
184 	struct device_node *np = pdev->dev.of_node;
185 	struct ntc_thermistor_platform_data *pdata;
186 
187 	if (!np)
188 		return NULL;
189 
190 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
191 	if (!pdata)
192 		return ERR_PTR(-ENOMEM);
193 
194 	chan = iio_channel_get(&pdev->dev, NULL);
195 	if (IS_ERR(chan))
196 		return ERR_CAST(chan);
197 
198 	if (of_property_read_u32(np, "pullup-uv", &pdata->pullup_uv))
199 		return ERR_PTR(-ENODEV);
200 	if (of_property_read_u32(np, "pullup-ohm", &pdata->pullup_ohm))
201 		return ERR_PTR(-ENODEV);
202 	if (of_property_read_u32(np, "pulldown-ohm", &pdata->pulldown_ohm))
203 		return ERR_PTR(-ENODEV);
204 
205 	if (of_find_property(np, "connected-positive", NULL))
206 		pdata->connect = NTC_CONNECTED_POSITIVE;
207 	else /* status change should be possible if not always on. */
208 		pdata->connect = NTC_CONNECTED_GROUND;
209 
210 	pdata->chan = chan;
211 	pdata->read_uv = ntc_adc_iio_read;
212 
213 	return pdata;
214 }
215 static void ntc_iio_channel_release(struct ntc_thermistor_platform_data *pdata)
216 {
217 	if (pdata->chan)
218 		iio_channel_release(pdata->chan);
219 }
220 #else
221 static struct ntc_thermistor_platform_data *
222 ntc_thermistor_parse_dt(struct platform_device *pdev)
223 {
224 	return NULL;
225 }
226 
227 #define ntc_match	NULL
228 
229 static void ntc_iio_channel_release(struct ntc_thermistor_platform_data *pdata)
230 { }
231 #endif
232 
233 static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
234 {
235 	if (divisor == 0 && dividend == 0)
236 		return 0;
237 	if (divisor == 0)
238 		return UINT_MAX;
239 	return div64_u64(dividend, divisor);
240 }
241 
242 static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
243 {
244 	struct ntc_thermistor_platform_data *pdata = data->pdata;
245 	u64 mv = uv / 1000;
246 	u64 pmv = pdata->pullup_uv / 1000;
247 	u64 n, puo, pdo;
248 	puo = pdata->pullup_ohm;
249 	pdo = pdata->pulldown_ohm;
250 
251 	if (mv == 0) {
252 		if (pdata->connect == NTC_CONNECTED_POSITIVE)
253 			return INT_MAX;
254 		return 0;
255 	}
256 	if (mv >= pmv)
257 		return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
258 			0 : INT_MAX;
259 
260 	if (pdata->connect == NTC_CONNECTED_POSITIVE && puo == 0)
261 		n = div64_u64_safe(pdo * (pmv - mv), mv);
262 	else if (pdata->connect == NTC_CONNECTED_GROUND && pdo == 0)
263 		n = div64_u64_safe(puo * mv, pmv - mv);
264 	else if (pdata->connect == NTC_CONNECTED_POSITIVE)
265 		n = div64_u64_safe(pdo * puo * (pmv - mv),
266 				puo * mv - pdo * (pmv - mv));
267 	else
268 		n = div64_u64_safe(pdo * puo * mv, pdo * (pmv - mv) - puo * mv);
269 
270 	if (n > INT_MAX)
271 		n = INT_MAX;
272 	return n;
273 }
274 
275 static void lookup_comp(struct ntc_data *data, unsigned int ohm,
276 			int *i_low, int *i_high)
277 {
278 	int start, end, mid;
279 
280 	/*
281 	 * Handle special cases: Resistance is higher than or equal to
282 	 * resistance in first table entry, or resistance is lower or equal
283 	 * to resistance in last table entry.
284 	 * In these cases, return i_low == i_high, either pointing to the
285 	 * beginning or to the end of the table depending on the condition.
286 	 */
287 	if (ohm >= data->comp[0].ohm) {
288 		*i_low = 0;
289 		*i_high = 0;
290 		return;
291 	}
292 	if (ohm <= data->comp[data->n_comp - 1].ohm) {
293 		*i_low = data->n_comp - 1;
294 		*i_high = data->n_comp - 1;
295 		return;
296 	}
297 
298 	/* Do a binary search on compensation table */
299 	start = 0;
300 	end = data->n_comp;
301 	while (start < end) {
302 		mid = start + (end - start) / 2;
303 		/*
304 		 * start <= mid < end
305 		 * data->comp[start].ohm > ohm >= data->comp[end].ohm
306 		 *
307 		 * We could check for "ohm == data->comp[mid].ohm" here, but
308 		 * that is a quite unlikely condition, and we would have to
309 		 * check again after updating start. Check it at the end instead
310 		 * for simplicity.
311 		 */
312 		if (ohm >= data->comp[mid].ohm) {
313 			end = mid;
314 		} else {
315 			start = mid + 1;
316 			/*
317 			 * ohm >= data->comp[start].ohm might be true here,
318 			 * since we set start to mid + 1. In that case, we are
319 			 * done. We could keep going, but the condition is quite
320 			 * likely to occur, so it is worth checking for it.
321 			 */
322 			if (ohm >= data->comp[start].ohm)
323 				end = start;
324 		}
325 		/*
326 		 * start <= end
327 		 * data->comp[start].ohm >= ohm >= data->comp[end].ohm
328 		 */
329 	}
330 	/*
331 	 * start == end
332 	 * ohm >= data->comp[end].ohm
333 	 */
334 	*i_low = end;
335 	if (ohm == data->comp[end].ohm)
336 		*i_high = end;
337 	else
338 		*i_high = end - 1;
339 }
340 
341 static int get_temp_mc(struct ntc_data *data, unsigned int ohm)
342 {
343 	int low, high;
344 	int temp;
345 
346 	lookup_comp(data, ohm, &low, &high);
347 	if (low == high) {
348 		/* Unable to use linear approximation */
349 		temp = data->comp[low].temp_c * 1000;
350 	} else {
351 		temp = data->comp[low].temp_c * 1000 +
352 			((data->comp[high].temp_c - data->comp[low].temp_c) *
353 			 1000 * ((int)ohm - (int)data->comp[low].ohm)) /
354 			((int)data->comp[high].ohm - (int)data->comp[low].ohm);
355 	}
356 	return temp;
357 }
358 
359 static int ntc_thermistor_get_ohm(struct ntc_data *data)
360 {
361 	int read_uv;
362 
363 	if (data->pdata->read_ohm)
364 		return data->pdata->read_ohm();
365 
366 	if (data->pdata->read_uv) {
367 		read_uv = data->pdata->read_uv(data->pdata);
368 		if (read_uv < 0)
369 			return read_uv;
370 		return get_ohm_of_thermistor(data, read_uv);
371 	}
372 	return -EINVAL;
373 }
374 
375 static ssize_t ntc_show_name(struct device *dev,
376 		struct device_attribute *attr, char *buf)
377 {
378 	struct ntc_data *data = dev_get_drvdata(dev);
379 
380 	return sprintf(buf, "%s\n", data->name);
381 }
382 
383 static ssize_t ntc_show_type(struct device *dev,
384 		struct device_attribute *attr, char *buf)
385 {
386 	return sprintf(buf, "4\n");
387 }
388 
389 static ssize_t ntc_show_temp(struct device *dev,
390 		struct device_attribute *attr, char *buf)
391 {
392 	struct ntc_data *data = dev_get_drvdata(dev);
393 	int ohm;
394 
395 	ohm = ntc_thermistor_get_ohm(data);
396 	if (ohm < 0)
397 		return ohm;
398 
399 	return sprintf(buf, "%d\n", get_temp_mc(data, ohm));
400 }
401 
402 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, ntc_show_type, NULL, 0);
403 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ntc_show_temp, NULL, 0);
404 static DEVICE_ATTR(name, S_IRUGO, ntc_show_name, NULL);
405 
406 static struct attribute *ntc_attributes[] = {
407 	&dev_attr_name.attr,
408 	&sensor_dev_attr_temp1_type.dev_attr.attr,
409 	&sensor_dev_attr_temp1_input.dev_attr.attr,
410 	NULL,
411 };
412 
413 static const struct attribute_group ntc_attr_group = {
414 	.attrs = ntc_attributes,
415 };
416 
417 static int ntc_thermistor_probe(struct platform_device *pdev)
418 {
419 	const struct of_device_id *of_id =
420 			of_match_device(of_match_ptr(ntc_match), &pdev->dev);
421 	const struct platform_device_id *pdev_id;
422 	struct ntc_thermistor_platform_data *pdata;
423 	struct ntc_data *data;
424 	int ret;
425 
426 	pdata = ntc_thermistor_parse_dt(pdev);
427 	if (IS_ERR(pdata))
428 		return PTR_ERR(pdata);
429 	else if (pdata == NULL)
430 		pdata = dev_get_platdata(&pdev->dev);
431 
432 	if (!pdata) {
433 		dev_err(&pdev->dev, "No platform init data supplied.\n");
434 		return -ENODEV;
435 	}
436 
437 	/* Either one of the two is required. */
438 	if (!pdata->read_uv && !pdata->read_ohm) {
439 		dev_err(&pdev->dev,
440 			"Both read_uv and read_ohm missing. Need either one of the two.\n");
441 		return -EINVAL;
442 	}
443 
444 	if (pdata->read_uv && pdata->read_ohm) {
445 		dev_warn(&pdev->dev,
446 			 "Only one of read_uv and read_ohm is needed; ignoring read_uv.\n");
447 		pdata->read_uv = NULL;
448 	}
449 
450 	if (pdata->read_uv && (pdata->pullup_uv == 0 ||
451 				(pdata->pullup_ohm == 0 && pdata->connect ==
452 				 NTC_CONNECTED_GROUND) ||
453 				(pdata->pulldown_ohm == 0 && pdata->connect ==
454 				 NTC_CONNECTED_POSITIVE) ||
455 				(pdata->connect != NTC_CONNECTED_POSITIVE &&
456 				 pdata->connect != NTC_CONNECTED_GROUND))) {
457 		dev_err(&pdev->dev,
458 			"Required data to use read_uv not supplied.\n");
459 		return -EINVAL;
460 	}
461 
462 	data = devm_kzalloc(&pdev->dev, sizeof(struct ntc_data), GFP_KERNEL);
463 	if (!data)
464 		return -ENOMEM;
465 
466 	pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
467 
468 	data->dev = &pdev->dev;
469 	data->pdata = pdata;
470 	strlcpy(data->name, pdev_id->name, sizeof(data->name));
471 
472 	switch (pdev_id->driver_data) {
473 	case TYPE_NCPXXWB473:
474 		data->comp = ncpXXwb473;
475 		data->n_comp = ARRAY_SIZE(ncpXXwb473);
476 		break;
477 	case TYPE_NCPXXWL333:
478 		data->comp = ncpXXwl333;
479 		data->n_comp = ARRAY_SIZE(ncpXXwl333);
480 		break;
481 	default:
482 		dev_err(&pdev->dev, "Unknown device type: %lu(%s)\n",
483 				pdev_id->driver_data, pdev_id->name);
484 		return -EINVAL;
485 	}
486 
487 	platform_set_drvdata(pdev, data);
488 
489 	ret = sysfs_create_group(&data->dev->kobj, &ntc_attr_group);
490 	if (ret) {
491 		dev_err(data->dev, "unable to create sysfs files\n");
492 		return ret;
493 	}
494 
495 	data->hwmon_dev = hwmon_device_register(data->dev);
496 	if (IS_ERR(data->hwmon_dev)) {
497 		dev_err(data->dev, "unable to register as hwmon device.\n");
498 		ret = PTR_ERR(data->hwmon_dev);
499 		goto err_after_sysfs;
500 	}
501 
502 	dev_info(&pdev->dev, "Thermistor type: %s successfully probed.\n",
503 								pdev->name);
504 
505 	return 0;
506 err_after_sysfs:
507 	sysfs_remove_group(&data->dev->kobj, &ntc_attr_group);
508 	ntc_iio_channel_release(pdata);
509 	return ret;
510 }
511 
512 static int ntc_thermistor_remove(struct platform_device *pdev)
513 {
514 	struct ntc_data *data = platform_get_drvdata(pdev);
515 	struct ntc_thermistor_platform_data *pdata = data->pdata;
516 
517 	hwmon_device_unregister(data->hwmon_dev);
518 	sysfs_remove_group(&data->dev->kobj, &ntc_attr_group);
519 	ntc_iio_channel_release(pdata);
520 
521 	return 0;
522 }
523 
524 static struct platform_driver ntc_thermistor_driver = {
525 	.driver = {
526 		.name = "ntc-thermistor",
527 		.owner = THIS_MODULE,
528 		.of_match_table = of_match_ptr(ntc_match),
529 	},
530 	.probe = ntc_thermistor_probe,
531 	.remove = ntc_thermistor_remove,
532 	.id_table = ntc_thermistor_id,
533 };
534 
535 module_platform_driver(ntc_thermistor_driver);
536 
537 MODULE_DESCRIPTION("NTC Thermistor Driver");
538 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
539 MODULE_LICENSE("GPL");
540 MODULE_ALIAS("platform:ntc-thermistor");
541