ntc_thermistor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. } else {
  199. *i_low = mid + 1;
  200. *i_high = mid;
  201. }
  202. return 0;
  203. }
  204. static int get_temp_mC(struct ntc_data *data, unsigned int ohm, int *temp)
  205. {
  206. int low, high;
  207. int ret;
  208. ret = lookup_comp(data, ohm, &low, &high);
  209. if (ret) {
  210. /* Unable to use linear approximation */
  211. if (low != -1)
  212. *temp = data->comp[low].temp_C * 1000;
  213. else if (high != -1)
  214. *temp = data->comp[high].temp_C * 1000;
  215. else
  216. return ret;
  217. } else {
  218. *temp = data->comp[low].temp_C * 1000 +
  219. ((data->comp[high].temp_C - data->comp[low].temp_C) *
  220. 1000 * ((int)ohm - (int)data->comp[low].ohm)) /
  221. ((int)data->comp[high].ohm - (int)data->comp[low].ohm);
  222. }
  223. return 0;
  224. }
  225. static int ntc_thermistor_read(struct ntc_data *data, int *temp)
  226. {
  227. int ret;
  228. int read_ohm, read_uV;
  229. unsigned int ohm = 0;
  230. if (data->pdata->read_ohm) {
  231. read_ohm = data->pdata->read_ohm();
  232. if (read_ohm < 0)
  233. return read_ohm;
  234. ohm = (unsigned int)read_ohm;
  235. }
  236. if (data->pdata->read_uV) {
  237. read_uV = data->pdata->read_uV();
  238. if (read_uV < 0)
  239. return read_uV;
  240. ohm = get_ohm_of_thermistor(data, (unsigned int)read_uV);
  241. }
  242. ret = get_temp_mC(data, ohm, temp);
  243. if (ret) {
  244. dev_dbg(data->dev, "Sensor reading function not available.\n");
  245. return ret;
  246. }
  247. return 0;
  248. }
  249. static ssize_t ntc_show_name(struct device *dev,
  250. struct device_attribute *attr, char *buf)
  251. {
  252. struct ntc_data *data = dev_get_drvdata(dev);
  253. return sprintf(buf, "%s\n", data->name);
  254. }
  255. static ssize_t ntc_show_type(struct device *dev,
  256. struct device_attribute *attr, char *buf)
  257. {
  258. return sprintf(buf, "4\n");
  259. }
  260. static ssize_t ntc_show_temp(struct device *dev,
  261. struct device_attribute *attr, char *buf)
  262. {
  263. struct ntc_data *data = dev_get_drvdata(dev);
  264. int temp, ret;
  265. ret = ntc_thermistor_read(data, &temp);
  266. if (ret)
  267. return ret;
  268. return sprintf(buf, "%d\n", temp);
  269. }
  270. static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, ntc_show_type, NULL, 0);
  271. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ntc_show_temp, NULL, 0);
  272. static DEVICE_ATTR(name, S_IRUGO, ntc_show_name, NULL);
  273. static struct attribute *ntc_attributes[] = {
  274. &dev_attr_name.attr,
  275. &sensor_dev_attr_temp1_type.dev_attr.attr,
  276. &sensor_dev_attr_temp1_input.dev_attr.attr,
  277. NULL,
  278. };
  279. static const struct attribute_group ntc_attr_group = {
  280. .attrs = ntc_attributes,
  281. };
  282. static int __devinit ntc_thermistor_probe(struct platform_device *pdev)
  283. {
  284. struct ntc_data *data;
  285. struct ntc_thermistor_platform_data *pdata = pdev->dev.platform_data;
  286. int ret = 0;
  287. if (!pdata) {
  288. dev_err(&pdev->dev, "No platform init data supplied.\n");
  289. return -ENODEV;
  290. }
  291. /* Either one of the two is required. */
  292. if (!pdata->read_uV && !pdata->read_ohm) {
  293. dev_err(&pdev->dev, "Both read_uV and read_ohm missing."
  294. "Need either one of the two.\n");
  295. return -EINVAL;
  296. }
  297. if (pdata->read_uV && pdata->read_ohm) {
  298. dev_warn(&pdev->dev, "Only one of read_uV and read_ohm "
  299. "is needed; ignoring read_uV.\n");
  300. pdata->read_uV = NULL;
  301. }
  302. if (pdata->read_uV && (pdata->pullup_uV == 0 ||
  303. (pdata->pullup_ohm == 0 && pdata->connect ==
  304. NTC_CONNECTED_GROUND) ||
  305. (pdata->pulldown_ohm == 0 && pdata->connect ==
  306. NTC_CONNECTED_POSITIVE) ||
  307. (pdata->connect != NTC_CONNECTED_POSITIVE &&
  308. pdata->connect != NTC_CONNECTED_GROUND))) {
  309. dev_err(&pdev->dev, "Required data to use read_uV not "
  310. "supplied.\n");
  311. return -EINVAL;
  312. }
  313. data = kzalloc(sizeof(struct ntc_data), GFP_KERNEL);
  314. if (!data)
  315. return -ENOMEM;
  316. data->dev = &pdev->dev;
  317. data->pdata = pdata;
  318. strncpy(data->name, pdev->id_entry->name, PLATFORM_NAME_SIZE);
  319. switch (pdev->id_entry->driver_data) {
  320. case TYPE_NCPXXWB473:
  321. data->comp = ncpXXwb473;
  322. data->n_comp = ARRAY_SIZE(ncpXXwb473);
  323. break;
  324. case TYPE_NCPXXWL333:
  325. data->comp = ncpXXwl333;
  326. data->n_comp = ARRAY_SIZE(ncpXXwl333);
  327. break;
  328. default:
  329. dev_err(&pdev->dev, "Unknown device type: %lu(%s)\n",
  330. pdev->id_entry->driver_data,
  331. pdev->id_entry->name);
  332. ret = -EINVAL;
  333. goto err;
  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. goto err;
  340. }
  341. data->hwmon_dev = hwmon_device_register(data->dev);
  342. if (IS_ERR_OR_NULL(data->hwmon_dev)) {
  343. dev_err(data->dev, "unable to register as hwmon device.\n");
  344. ret = -EINVAL;
  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. err:
  354. kfree(data);
  355. return ret;
  356. }
  357. static int __devexit ntc_thermistor_remove(struct platform_device *pdev)
  358. {
  359. struct ntc_data *data = platform_get_drvdata(pdev);
  360. hwmon_device_unregister(data->hwmon_dev);
  361. sysfs_remove_group(&data->dev->kobj, &ntc_attr_group);
  362. platform_set_drvdata(pdev, NULL);
  363. kfree(data);
  364. return 0;
  365. }
  366. static const struct platform_device_id ntc_thermistor_id[] = {
  367. { "ncp15wb473", TYPE_NCPXXWB473 },
  368. { "ncp18wb473", TYPE_NCPXXWB473 },
  369. { "ncp21wb473", TYPE_NCPXXWB473 },
  370. { "ncp03wb473", TYPE_NCPXXWB473 },
  371. { "ncp15wl333", TYPE_NCPXXWL333 },
  372. { },
  373. };
  374. static struct platform_driver ntc_thermistor_driver = {
  375. .driver = {
  376. .name = "ntc-thermistor",
  377. .owner = THIS_MODULE,
  378. },
  379. .probe = ntc_thermistor_probe,
  380. .remove = __devexit_p(ntc_thermistor_remove),
  381. .id_table = ntc_thermistor_id,
  382. };
  383. module_platform_driver(ntc_thermistor_driver);
  384. MODULE_DESCRIPTION("NTC Thermistor Driver");
  385. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  386. MODULE_LICENSE("GPL");
  387. MODULE_ALIAS("platform:ntc-thermistor");