ntc_thermistor.c 14 KB

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