ntc_thermistor.c 15 KB

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