ntc_thermistor.c 12 KB

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