ntc_thermistor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. 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. 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 unsigned int get_ohm_of_thermistor(struct ntc_data *data,
  130. unsigned int uV)
  131. {
  132. struct ntc_thermistor_platform_data *pdata = data->pdata;
  133. u64 mV = uV / 1000;
  134. u64 pmV = pdata->pullup_uV / 1000;
  135. u64 N, puO, pdO;
  136. puO = pdata->pullup_ohm;
  137. pdO = pdata->pulldown_ohm;
  138. if (mV == 0) {
  139. if (pdata->connect == NTC_CONNECTED_POSITIVE)
  140. return UINT_MAX;
  141. return 0;
  142. }
  143. if (mV >= pmV)
  144. return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
  145. 0 : UINT_MAX;
  146. if (pdata->connect == NTC_CONNECTED_POSITIVE && puO == 0)
  147. N = div64_u64_safe(pdO * (pmV - mV), mV);
  148. else if (pdata->connect == NTC_CONNECTED_GROUND && pdO == 0)
  149. N = div64_u64_safe(puO * mV, pmV - mV);
  150. else if (pdata->connect == NTC_CONNECTED_POSITIVE)
  151. N = div64_u64_safe(pdO * puO * (pmV - mV),
  152. puO * mV - pdO * (pmV - mV));
  153. else
  154. N = div64_u64_safe(pdO * puO * mV, pdO * (pmV - mV) - puO * mV);
  155. return (unsigned int) N;
  156. }
  157. static int lookup_comp(struct ntc_data *data,
  158. unsigned int ohm, int *i_low, int *i_high)
  159. {
  160. int start, end, mid = -1;
  161. /* Do a binary search on compensation table */
  162. start = 0;
  163. end = data->n_comp;
  164. while (end > start) {
  165. mid = start + (end - start) / 2;
  166. if (data->comp[mid].ohm < ohm)
  167. end = mid;
  168. else if (data->comp[mid].ohm > ohm)
  169. start = mid + 1;
  170. else
  171. break;
  172. }
  173. if (mid == 0) {
  174. if (data->comp[mid].ohm > ohm) {
  175. *i_high = mid;
  176. *i_low = mid + 1;
  177. return 0;
  178. } else {
  179. *i_low = mid;
  180. *i_high = -1;
  181. return -EINVAL;
  182. }
  183. }
  184. if (mid == (data->n_comp - 1)) {
  185. if (data->comp[mid].ohm <= ohm) {
  186. *i_low = mid;
  187. *i_high = mid - 1;
  188. return 0;
  189. } else {
  190. *i_low = -1;
  191. *i_high = mid;
  192. return -EINVAL;
  193. }
  194. }
  195. if (data->comp[mid].ohm <= ohm) {
  196. *i_low = mid;
  197. *i_high = mid - 1;
  198. }
  199. if (data->comp[mid].ohm > ohm) {
  200. *i_low = mid + 1;
  201. *i_high = mid;
  202. }
  203. return 0;
  204. }
  205. static int get_temp_mC(struct ntc_data *data, unsigned int ohm, int *temp)
  206. {
  207. int low, high;
  208. int ret;
  209. ret = lookup_comp(data, ohm, &low, &high);
  210. if (ret) {
  211. /* Unable to use linear approximation */
  212. if (low != -1)
  213. *temp = data->comp[low].temp_C * 1000;
  214. else if (high != -1)
  215. *temp = data->comp[high].temp_C * 1000;
  216. else
  217. return ret;
  218. } else {
  219. *temp = data->comp[low].temp_C * 1000 +
  220. ((data->comp[high].temp_C - data->comp[low].temp_C) *
  221. 1000 * ((int)ohm - (int)data->comp[low].ohm)) /
  222. ((int)data->comp[high].ohm - (int)data->comp[low].ohm);
  223. }
  224. return 0;
  225. }
  226. static int ntc_thermistor_read(struct ntc_data *data, int *temp)
  227. {
  228. int ret;
  229. int read_ohm, read_uV;
  230. unsigned int ohm = 0;
  231. if (data->pdata->read_ohm) {
  232. read_ohm = data->pdata->read_ohm();
  233. if (read_ohm < 0)
  234. return read_ohm;
  235. ohm = (unsigned int)read_ohm;
  236. }
  237. if (data->pdata->read_uV) {
  238. read_uV = data->pdata->read_uV();
  239. if (read_uV < 0)
  240. return read_uV;
  241. ohm = get_ohm_of_thermistor(data, (unsigned int)read_uV);
  242. }
  243. ret = get_temp_mC(data, ohm, temp);
  244. if (ret) {
  245. dev_dbg(data->dev, "Sensor reading function not available.\n");
  246. return ret;
  247. }
  248. return 0;
  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 temp, ret;
  266. ret = ntc_thermistor_read(data, &temp);
  267. if (ret)
  268. return ret;
  269. return sprintf(buf, "%d\n", temp);
  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 __devinit 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, "Both read_uV and read_ohm missing."
  295. "Need either one of the two.\n");
  296. return -EINVAL;
  297. }
  298. if (pdata->read_uV && pdata->read_ohm) {
  299. dev_warn(&pdev->dev, "Only one of read_uV and read_ohm "
  300. "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, "Required data to use read_uV not "
  311. "supplied.\n");
  312. return -EINVAL;
  313. }
  314. data = kzalloc(sizeof(struct ntc_data), GFP_KERNEL);
  315. if (!data)
  316. return -ENOMEM;
  317. data->dev = &pdev->dev;
  318. data->pdata = pdata;
  319. strncpy(data->name, pdev->id_entry->name, PLATFORM_NAME_SIZE);
  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. ret = -EINVAL;
  334. goto err;
  335. }
  336. platform_set_drvdata(pdev, data);
  337. ret = sysfs_create_group(&data->dev->kobj, &ntc_attr_group);
  338. if (ret) {
  339. dev_err(data->dev, "unable to create sysfs files\n");
  340. goto err;
  341. }
  342. data->hwmon_dev = hwmon_device_register(data->dev);
  343. if (IS_ERR_OR_NULL(data->hwmon_dev)) {
  344. dev_err(data->dev, "unable to register as hwmon device.\n");
  345. ret = -EINVAL;
  346. goto err_after_sysfs;
  347. }
  348. dev_info(&pdev->dev, "Thermistor %s:%d (type: %s/%lu) successfully probed.\n",
  349. pdev->name, pdev->id, pdev->id_entry->name,
  350. pdev->id_entry->driver_data);
  351. return 0;
  352. err_after_sysfs:
  353. sysfs_remove_group(&data->dev->kobj, &ntc_attr_group);
  354. err:
  355. kfree(data);
  356. return ret;
  357. }
  358. static int __devexit ntc_thermistor_remove(struct platform_device *pdev)
  359. {
  360. struct ntc_data *data = platform_get_drvdata(pdev);
  361. hwmon_device_unregister(data->hwmon_dev);
  362. sysfs_remove_group(&data->dev->kobj, &ntc_attr_group);
  363. platform_set_drvdata(pdev, NULL);
  364. kfree(data);
  365. return 0;
  366. }
  367. static const struct platform_device_id ntc_thermistor_id[] = {
  368. { "ncp15wb473", TYPE_NCPXXWB473 },
  369. { "ncp18wb473", TYPE_NCPXXWB473 },
  370. { "ncp21wb473", TYPE_NCPXXWB473 },
  371. { "ncp03wb473", TYPE_NCPXXWB473 },
  372. { "ncp15wl333", TYPE_NCPXXWL333 },
  373. { },
  374. };
  375. static struct platform_driver ntc_thermistor_driver = {
  376. .driver = {
  377. .name = "ntc-thermistor",
  378. .owner = THIS_MODULE,
  379. },
  380. .probe = ntc_thermistor_probe,
  381. .remove = __devexit_p(ntc_thermistor_remove),
  382. .id_table = ntc_thermistor_id,
  383. };
  384. static int __init ntc_thermistor_init(void)
  385. {
  386. return platform_driver_register(&ntc_thermistor_driver);
  387. }
  388. module_init(ntc_thermistor_init);
  389. static void __exit ntc_thermistor_cleanup(void)
  390. {
  391. platform_driver_unregister(&ntc_thermistor_driver);
  392. }
  393. module_exit(ntc_thermistor_cleanup);
  394. MODULE_DESCRIPTION("NTC Thermistor Driver");
  395. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  396. MODULE_LICENSE("GPL");
  397. MODULE_ALIAS("platform:ntc-thermistor");