ntc_thermistor.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ntc_thermistor.c - NTC Thermistors
  4. *
  5. * Copyright (C) 2010 Samsung Electronics
  6. * MyungJoo Ham <myungjoo.ham@samsung.com>
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/math64.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/err.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_data/ntc_thermistor.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/machine.h>
  19. #include <linux/iio/driver.h>
  20. #include <linux/iio/consumer.h>
  21. #include <linux/hwmon.h>
  22. struct ntc_compensation {
  23. int temp_c;
  24. unsigned int ohm;
  25. };
  26. /*
  27. * Used as index in a zero-terminated array, holes not allowed so
  28. * that NTC_LAST is the first empty array entry.
  29. */
  30. enum {
  31. NTC_B57330V2103,
  32. NTC_B57891S0103,
  33. NTC_NCP03WB473,
  34. NTC_NCP03WF104,
  35. NTC_NCP15WB473,
  36. NTC_NCP15WL333,
  37. NTC_NCP15XH103,
  38. NTC_NCP18WB473,
  39. NTC_NCP21WB473,
  40. NTC_LAST,
  41. };
  42. static const struct platform_device_id ntc_thermistor_id[] = {
  43. [NTC_B57330V2103] = { "b57330v2103", TYPE_B57330V2103 },
  44. [NTC_B57891S0103] = { "b57891s0103", TYPE_B57891S0103 },
  45. [NTC_NCP03WB473] = { "ncp03wb473", TYPE_NCPXXWB473 },
  46. [NTC_NCP03WF104] = { "ncp03wf104", TYPE_NCPXXWF104 },
  47. [NTC_NCP15WB473] = { "ncp15wb473", TYPE_NCPXXWB473 },
  48. [NTC_NCP15WL333] = { "ncp15wl333", TYPE_NCPXXWL333 },
  49. [NTC_NCP15XH103] = { "ncp15xh103", TYPE_NCPXXXH103 },
  50. [NTC_NCP18WB473] = { "ncp18wb473", TYPE_NCPXXWB473 },
  51. [NTC_NCP21WB473] = { "ncp21wb473", TYPE_NCPXXWB473 },
  52. [NTC_LAST] = { },
  53. };
  54. /*
  55. * A compensation table should be sorted by the values of .ohm
  56. * in descending order.
  57. * The following compensation tables are from the specification of Murata NTC
  58. * Thermistors Datasheet
  59. */
  60. static const struct ntc_compensation ncpXXwb473[] = {
  61. { .temp_c = -40, .ohm = 1747920 },
  62. { .temp_c = -35, .ohm = 1245428 },
  63. { .temp_c = -30, .ohm = 898485 },
  64. { .temp_c = -25, .ohm = 655802 },
  65. { .temp_c = -20, .ohm = 483954 },
  66. { .temp_c = -15, .ohm = 360850 },
  67. { .temp_c = -10, .ohm = 271697 },
  68. { .temp_c = -5, .ohm = 206463 },
  69. { .temp_c = 0, .ohm = 158214 },
  70. { .temp_c = 5, .ohm = 122259 },
  71. { .temp_c = 10, .ohm = 95227 },
  72. { .temp_c = 15, .ohm = 74730 },
  73. { .temp_c = 20, .ohm = 59065 },
  74. { .temp_c = 25, .ohm = 47000 },
  75. { .temp_c = 30, .ohm = 37643 },
  76. { .temp_c = 35, .ohm = 30334 },
  77. { .temp_c = 40, .ohm = 24591 },
  78. { .temp_c = 45, .ohm = 20048 },
  79. { .temp_c = 50, .ohm = 16433 },
  80. { .temp_c = 55, .ohm = 13539 },
  81. { .temp_c = 60, .ohm = 11209 },
  82. { .temp_c = 65, .ohm = 9328 },
  83. { .temp_c = 70, .ohm = 7798 },
  84. { .temp_c = 75, .ohm = 6544 },
  85. { .temp_c = 80, .ohm = 5518 },
  86. { .temp_c = 85, .ohm = 4674 },
  87. { .temp_c = 90, .ohm = 3972 },
  88. { .temp_c = 95, .ohm = 3388 },
  89. { .temp_c = 100, .ohm = 2902 },
  90. { .temp_c = 105, .ohm = 2494 },
  91. { .temp_c = 110, .ohm = 2150 },
  92. { .temp_c = 115, .ohm = 1860 },
  93. { .temp_c = 120, .ohm = 1615 },
  94. { .temp_c = 125, .ohm = 1406 },
  95. };
  96. static const struct ntc_compensation ncpXXwl333[] = {
  97. { .temp_c = -40, .ohm = 1610154 },
  98. { .temp_c = -35, .ohm = 1130850 },
  99. { .temp_c = -30, .ohm = 802609 },
  100. { .temp_c = -25, .ohm = 575385 },
  101. { .temp_c = -20, .ohm = 416464 },
  102. { .temp_c = -15, .ohm = 304219 },
  103. { .temp_c = -10, .ohm = 224193 },
  104. { .temp_c = -5, .ohm = 166623 },
  105. { .temp_c = 0, .ohm = 124850 },
  106. { .temp_c = 5, .ohm = 94287 },
  107. { .temp_c = 10, .ohm = 71747 },
  108. { .temp_c = 15, .ohm = 54996 },
  109. { .temp_c = 20, .ohm = 42455 },
  110. { .temp_c = 25, .ohm = 33000 },
  111. { .temp_c = 30, .ohm = 25822 },
  112. { .temp_c = 35, .ohm = 20335 },
  113. { .temp_c = 40, .ohm = 16115 },
  114. { .temp_c = 45, .ohm = 12849 },
  115. { .temp_c = 50, .ohm = 10306 },
  116. { .temp_c = 55, .ohm = 8314 },
  117. { .temp_c = 60, .ohm = 6746 },
  118. { .temp_c = 65, .ohm = 5503 },
  119. { .temp_c = 70, .ohm = 4513 },
  120. { .temp_c = 75, .ohm = 3721 },
  121. { .temp_c = 80, .ohm = 3084 },
  122. { .temp_c = 85, .ohm = 2569 },
  123. { .temp_c = 90, .ohm = 2151 },
  124. { .temp_c = 95, .ohm = 1809 },
  125. { .temp_c = 100, .ohm = 1529 },
  126. { .temp_c = 105, .ohm = 1299 },
  127. { .temp_c = 110, .ohm = 1108 },
  128. { .temp_c = 115, .ohm = 949 },
  129. { .temp_c = 120, .ohm = 817 },
  130. { .temp_c = 125, .ohm = 707 },
  131. };
  132. static const struct ntc_compensation ncpXXwf104[] = {
  133. { .temp_c = -40, .ohm = 4397119 },
  134. { .temp_c = -35, .ohm = 3088599 },
  135. { .temp_c = -30, .ohm = 2197225 },
  136. { .temp_c = -25, .ohm = 1581881 },
  137. { .temp_c = -20, .ohm = 1151037 },
  138. { .temp_c = -15, .ohm = 846579 },
  139. { .temp_c = -10, .ohm = 628988 },
  140. { .temp_c = -5, .ohm = 471632 },
  141. { .temp_c = 0, .ohm = 357012 },
  142. { .temp_c = 5, .ohm = 272500 },
  143. { .temp_c = 10, .ohm = 209710 },
  144. { .temp_c = 15, .ohm = 162651 },
  145. { .temp_c = 20, .ohm = 127080 },
  146. { .temp_c = 25, .ohm = 100000 },
  147. { .temp_c = 30, .ohm = 79222 },
  148. { .temp_c = 35, .ohm = 63167 },
  149. { .temp_c = 40, .ohm = 50677 },
  150. { .temp_c = 45, .ohm = 40904 },
  151. { .temp_c = 50, .ohm = 33195 },
  152. { .temp_c = 55, .ohm = 27091 },
  153. { .temp_c = 60, .ohm = 22224 },
  154. { .temp_c = 65, .ohm = 18323 },
  155. { .temp_c = 70, .ohm = 15184 },
  156. { .temp_c = 75, .ohm = 12635 },
  157. { .temp_c = 80, .ohm = 10566 },
  158. { .temp_c = 85, .ohm = 8873 },
  159. { .temp_c = 90, .ohm = 7481 },
  160. { .temp_c = 95, .ohm = 6337 },
  161. { .temp_c = 100, .ohm = 5384 },
  162. { .temp_c = 105, .ohm = 4594 },
  163. { .temp_c = 110, .ohm = 3934 },
  164. { .temp_c = 115, .ohm = 3380 },
  165. { .temp_c = 120, .ohm = 2916 },
  166. { .temp_c = 125, .ohm = 2522 },
  167. };
  168. static const struct ntc_compensation ncpXXxh103[] = {
  169. { .temp_c = -40, .ohm = 247565 },
  170. { .temp_c = -35, .ohm = 181742 },
  171. { .temp_c = -30, .ohm = 135128 },
  172. { .temp_c = -25, .ohm = 101678 },
  173. { .temp_c = -20, .ohm = 77373 },
  174. { .temp_c = -15, .ohm = 59504 },
  175. { .temp_c = -10, .ohm = 46222 },
  176. { .temp_c = -5, .ohm = 36244 },
  177. { .temp_c = 0, .ohm = 28674 },
  178. { .temp_c = 5, .ohm = 22878 },
  179. { .temp_c = 10, .ohm = 18399 },
  180. { .temp_c = 15, .ohm = 14910 },
  181. { .temp_c = 20, .ohm = 12169 },
  182. { .temp_c = 25, .ohm = 10000 },
  183. { .temp_c = 30, .ohm = 8271 },
  184. { .temp_c = 35, .ohm = 6883 },
  185. { .temp_c = 40, .ohm = 5762 },
  186. { .temp_c = 45, .ohm = 4851 },
  187. { .temp_c = 50, .ohm = 4105 },
  188. { .temp_c = 55, .ohm = 3492 },
  189. { .temp_c = 60, .ohm = 2985 },
  190. { .temp_c = 65, .ohm = 2563 },
  191. { .temp_c = 70, .ohm = 2211 },
  192. { .temp_c = 75, .ohm = 1915 },
  193. { .temp_c = 80, .ohm = 1666 },
  194. { .temp_c = 85, .ohm = 1454 },
  195. { .temp_c = 90, .ohm = 1275 },
  196. { .temp_c = 95, .ohm = 1121 },
  197. { .temp_c = 100, .ohm = 990 },
  198. { .temp_c = 105, .ohm = 876 },
  199. { .temp_c = 110, .ohm = 779 },
  200. { .temp_c = 115, .ohm = 694 },
  201. { .temp_c = 120, .ohm = 620 },
  202. { .temp_c = 125, .ohm = 556 },
  203. };
  204. /*
  205. * The following compensation tables are from the specifications in EPCOS NTC
  206. * Thermistors Datasheets
  207. */
  208. static const struct ntc_compensation b57330v2103[] = {
  209. { .temp_c = -40, .ohm = 190030 },
  210. { .temp_c = -35, .ohm = 145360 },
  211. { .temp_c = -30, .ohm = 112060 },
  212. { .temp_c = -25, .ohm = 87041 },
  213. { .temp_c = -20, .ohm = 68104 },
  214. { .temp_c = -15, .ohm = 53665 },
  215. { .temp_c = -10, .ohm = 42576 },
  216. { .temp_c = -5, .ohm = 34001 },
  217. { .temp_c = 0, .ohm = 27326 },
  218. { .temp_c = 5, .ohm = 22096 },
  219. { .temp_c = 10, .ohm = 17973 },
  220. { .temp_c = 15, .ohm = 14703 },
  221. { .temp_c = 20, .ohm = 12090 },
  222. { .temp_c = 25, .ohm = 10000 },
  223. { .temp_c = 30, .ohm = 8311 },
  224. { .temp_c = 35, .ohm = 6941 },
  225. { .temp_c = 40, .ohm = 5825 },
  226. { .temp_c = 45, .ohm = 4911 },
  227. { .temp_c = 50, .ohm = 4158 },
  228. { .temp_c = 55, .ohm = 3536 },
  229. { .temp_c = 60, .ohm = 3019 },
  230. { .temp_c = 65, .ohm = 2588 },
  231. { .temp_c = 70, .ohm = 2227 },
  232. { .temp_c = 75, .ohm = 1924 },
  233. { .temp_c = 80, .ohm = 1668 },
  234. { .temp_c = 85, .ohm = 1451 },
  235. { .temp_c = 90, .ohm = 1266 },
  236. { .temp_c = 95, .ohm = 1108 },
  237. { .temp_c = 100, .ohm = 973 },
  238. { .temp_c = 105, .ohm = 857 },
  239. { .temp_c = 110, .ohm = 757 },
  240. { .temp_c = 115, .ohm = 671 },
  241. { .temp_c = 120, .ohm = 596 },
  242. { .temp_c = 125, .ohm = 531 },
  243. };
  244. static const struct ntc_compensation b57891s0103[] = {
  245. { .temp_c = -55.0, .ohm = 878900 },
  246. { .temp_c = -50.0, .ohm = 617590 },
  247. { .temp_c = -45.0, .ohm = 439340 },
  248. { .temp_c = -40.0, .ohm = 316180 },
  249. { .temp_c = -35.0, .ohm = 230060 },
  250. { .temp_c = -30.0, .ohm = 169150 },
  251. { .temp_c = -25.0, .ohm = 125550 },
  252. { .temp_c = -20.0, .ohm = 94143 },
  253. { .temp_c = -15.0, .ohm = 71172 },
  254. { .temp_c = -10.0, .ohm = 54308 },
  255. { .temp_c = -5.0, .ohm = 41505 },
  256. { .temp_c = 0.0, .ohm = 32014 },
  257. { .temp_c = 5.0, .ohm = 25011 },
  258. { .temp_c = 10.0, .ohm = 19691 },
  259. { .temp_c = 15.0, .ohm = 15618 },
  260. { .temp_c = 20.0, .ohm = 12474 },
  261. { .temp_c = 25.0, .ohm = 10000 },
  262. { .temp_c = 30.0, .ohm = 8080 },
  263. { .temp_c = 35.0, .ohm = 6569 },
  264. { .temp_c = 40.0, .ohm = 5372 },
  265. { .temp_c = 45.0, .ohm = 4424 },
  266. { .temp_c = 50.0, .ohm = 3661 },
  267. { .temp_c = 55.0, .ohm = 3039 },
  268. { .temp_c = 60.0, .ohm = 2536 },
  269. { .temp_c = 65.0, .ohm = 2128 },
  270. { .temp_c = 70.0, .ohm = 1794 },
  271. { .temp_c = 75.0, .ohm = 1518 },
  272. { .temp_c = 80.0, .ohm = 1290 },
  273. { .temp_c = 85.0, .ohm = 1100 },
  274. { .temp_c = 90.0, .ohm = 942 },
  275. { .temp_c = 95.0, .ohm = 809 },
  276. { .temp_c = 100.0, .ohm = 697 },
  277. { .temp_c = 105.0, .ohm = 604 },
  278. { .temp_c = 110.0, .ohm = 525 },
  279. { .temp_c = 115.0, .ohm = 457 },
  280. { .temp_c = 120.0, .ohm = 400 },
  281. { .temp_c = 125.0, .ohm = 351 },
  282. { .temp_c = 130.0, .ohm = 308 },
  283. { .temp_c = 135.0, .ohm = 272 },
  284. { .temp_c = 140.0, .ohm = 240 },
  285. { .temp_c = 145.0, .ohm = 213 },
  286. { .temp_c = 150.0, .ohm = 189 },
  287. { .temp_c = 155.0, .ohm = 168 },
  288. };
  289. struct ntc_type {
  290. const struct ntc_compensation *comp;
  291. int n_comp;
  292. };
  293. #define NTC_TYPE(ntc, compensation) \
  294. [(ntc)] = { .comp = (compensation), .n_comp = ARRAY_SIZE(compensation) }
  295. static const struct ntc_type ntc_type[] = {
  296. NTC_TYPE(TYPE_B57330V2103, b57330v2103),
  297. NTC_TYPE(TYPE_B57891S0103, b57891s0103),
  298. NTC_TYPE(TYPE_NCPXXWB473, ncpXXwb473),
  299. NTC_TYPE(TYPE_NCPXXWF104, ncpXXwf104),
  300. NTC_TYPE(TYPE_NCPXXWL333, ncpXXwl333),
  301. NTC_TYPE(TYPE_NCPXXXH103, ncpXXxh103),
  302. };
  303. struct ntc_data {
  304. struct ntc_thermistor_platform_data *pdata;
  305. const struct ntc_compensation *comp;
  306. int n_comp;
  307. };
  308. #if defined(CONFIG_OF) && IS_ENABLED(CONFIG_IIO)
  309. static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata)
  310. {
  311. struct iio_channel *channel = pdata->chan;
  312. int raw, uv, ret;
  313. ret = iio_read_channel_raw(channel, &raw);
  314. if (ret < 0) {
  315. pr_err("read channel() error: %d\n", ret);
  316. return ret;
  317. }
  318. ret = iio_convert_raw_to_processed(channel, raw, &uv, 1000);
  319. if (ret < 0) {
  320. /* Assume 12 bit ADC with vref at pullup_uv */
  321. uv = (pdata->pullup_uv * (s64)raw) >> 12;
  322. }
  323. return uv;
  324. }
  325. static const struct of_device_id ntc_match[] = {
  326. { .compatible = "epcos,b57330v2103",
  327. .data = &ntc_thermistor_id[NTC_B57330V2103]},
  328. { .compatible = "epcos,b57891s0103",
  329. .data = &ntc_thermistor_id[NTC_B57891S0103] },
  330. { .compatible = "murata,ncp03wb473",
  331. .data = &ntc_thermistor_id[NTC_NCP03WB473] },
  332. { .compatible = "murata,ncp03wf104",
  333. .data = &ntc_thermistor_id[NTC_NCP03WF104] },
  334. { .compatible = "murata,ncp15wb473",
  335. .data = &ntc_thermistor_id[NTC_NCP15WB473] },
  336. { .compatible = "murata,ncp15wl333",
  337. .data = &ntc_thermistor_id[NTC_NCP15WL333] },
  338. { .compatible = "murata,ncp15xh103",
  339. .data = &ntc_thermistor_id[NTC_NCP15XH103] },
  340. { .compatible = "murata,ncp18wb473",
  341. .data = &ntc_thermistor_id[NTC_NCP18WB473] },
  342. { .compatible = "murata,ncp21wb473",
  343. .data = &ntc_thermistor_id[NTC_NCP21WB473] },
  344. /* Usage of vendor name "ntc" is deprecated */
  345. { .compatible = "ntc,ncp03wb473",
  346. .data = &ntc_thermistor_id[NTC_NCP03WB473] },
  347. { .compatible = "ntc,ncp15wb473",
  348. .data = &ntc_thermistor_id[NTC_NCP15WB473] },
  349. { .compatible = "ntc,ncp15wl333",
  350. .data = &ntc_thermistor_id[NTC_NCP15WL333] },
  351. { .compatible = "ntc,ncp18wb473",
  352. .data = &ntc_thermistor_id[NTC_NCP18WB473] },
  353. { .compatible = "ntc,ncp21wb473",
  354. .data = &ntc_thermistor_id[NTC_NCP21WB473] },
  355. { },
  356. };
  357. MODULE_DEVICE_TABLE(of, ntc_match);
  358. static struct ntc_thermistor_platform_data *
  359. ntc_thermistor_parse_dt(struct device *dev)
  360. {
  361. struct iio_channel *chan;
  362. enum iio_chan_type type;
  363. struct device_node *np = dev->of_node;
  364. struct ntc_thermistor_platform_data *pdata;
  365. int ret;
  366. if (!np)
  367. return NULL;
  368. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  369. if (!pdata)
  370. return ERR_PTR(-ENOMEM);
  371. chan = devm_iio_channel_get(dev, NULL);
  372. if (IS_ERR(chan))
  373. return ERR_CAST(chan);
  374. ret = iio_get_channel_type(chan, &type);
  375. if (ret < 0)
  376. return ERR_PTR(ret);
  377. if (type != IIO_VOLTAGE)
  378. return ERR_PTR(-EINVAL);
  379. if (of_property_read_u32(np, "pullup-uv", &pdata->pullup_uv))
  380. return ERR_PTR(-ENODEV);
  381. if (of_property_read_u32(np, "pullup-ohm", &pdata->pullup_ohm))
  382. return ERR_PTR(-ENODEV);
  383. if (of_property_read_u32(np, "pulldown-ohm", &pdata->pulldown_ohm))
  384. return ERR_PTR(-ENODEV);
  385. if (of_find_property(np, "connected-positive", NULL))
  386. pdata->connect = NTC_CONNECTED_POSITIVE;
  387. else /* status change should be possible if not always on. */
  388. pdata->connect = NTC_CONNECTED_GROUND;
  389. pdata->chan = chan;
  390. pdata->read_uv = ntc_adc_iio_read;
  391. return pdata;
  392. }
  393. #else
  394. static struct ntc_thermistor_platform_data *
  395. ntc_thermistor_parse_dt(struct device *dev)
  396. {
  397. return NULL;
  398. }
  399. #define ntc_match NULL
  400. #endif
  401. static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
  402. {
  403. if (divisor == 0 && dividend == 0)
  404. return 0;
  405. if (divisor == 0)
  406. return UINT_MAX;
  407. return div64_u64(dividend, divisor);
  408. }
  409. static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
  410. {
  411. struct ntc_thermistor_platform_data *pdata = data->pdata;
  412. u32 puv = pdata->pullup_uv;
  413. u64 n, puo, pdo;
  414. puo = pdata->pullup_ohm;
  415. pdo = pdata->pulldown_ohm;
  416. if (uv == 0)
  417. return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
  418. INT_MAX : 0;
  419. if (uv >= puv)
  420. return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
  421. 0 : INT_MAX;
  422. if (pdata->connect == NTC_CONNECTED_POSITIVE && puo == 0)
  423. n = div_u64(pdo * (puv - uv), uv);
  424. else if (pdata->connect == NTC_CONNECTED_GROUND && pdo == 0)
  425. n = div_u64(puo * uv, puv - uv);
  426. else if (pdata->connect == NTC_CONNECTED_POSITIVE)
  427. n = div64_u64_safe(pdo * puo * (puv - uv),
  428. puo * uv - pdo * (puv - uv));
  429. else
  430. n = div64_u64_safe(pdo * puo * uv, pdo * (puv - uv) - puo * uv);
  431. if (n > INT_MAX)
  432. n = INT_MAX;
  433. return n;
  434. }
  435. static void lookup_comp(struct ntc_data *data, unsigned int ohm,
  436. int *i_low, int *i_high)
  437. {
  438. int start, end, mid;
  439. /*
  440. * Handle special cases: Resistance is higher than or equal to
  441. * resistance in first table entry, or resistance is lower or equal
  442. * to resistance in last table entry.
  443. * In these cases, return i_low == i_high, either pointing to the
  444. * beginning or to the end of the table depending on the condition.
  445. */
  446. if (ohm >= data->comp[0].ohm) {
  447. *i_low = 0;
  448. *i_high = 0;
  449. return;
  450. }
  451. if (ohm <= data->comp[data->n_comp - 1].ohm) {
  452. *i_low = data->n_comp - 1;
  453. *i_high = data->n_comp - 1;
  454. return;
  455. }
  456. /* Do a binary search on compensation table */
  457. start = 0;
  458. end = data->n_comp;
  459. while (start < end) {
  460. mid = start + (end - start) / 2;
  461. /*
  462. * start <= mid < end
  463. * data->comp[start].ohm > ohm >= data->comp[end].ohm
  464. *
  465. * We could check for "ohm == data->comp[mid].ohm" here, but
  466. * that is a quite unlikely condition, and we would have to
  467. * check again after updating start. Check it at the end instead
  468. * for simplicity.
  469. */
  470. if (ohm >= data->comp[mid].ohm) {
  471. end = mid;
  472. } else {
  473. start = mid + 1;
  474. /*
  475. * ohm >= data->comp[start].ohm might be true here,
  476. * since we set start to mid + 1. In that case, we are
  477. * done. We could keep going, but the condition is quite
  478. * likely to occur, so it is worth checking for it.
  479. */
  480. if (ohm >= data->comp[start].ohm)
  481. end = start;
  482. }
  483. /*
  484. * start <= end
  485. * data->comp[start].ohm >= ohm >= data->comp[end].ohm
  486. */
  487. }
  488. /*
  489. * start == end
  490. * ohm >= data->comp[end].ohm
  491. */
  492. *i_low = end;
  493. if (ohm == data->comp[end].ohm)
  494. *i_high = end;
  495. else
  496. *i_high = end - 1;
  497. }
  498. static int get_temp_mc(struct ntc_data *data, unsigned int ohm)
  499. {
  500. int low, high;
  501. int temp;
  502. lookup_comp(data, ohm, &low, &high);
  503. if (low == high) {
  504. /* Unable to use linear approximation */
  505. temp = data->comp[low].temp_c * 1000;
  506. } else {
  507. temp = data->comp[low].temp_c * 1000 +
  508. ((data->comp[high].temp_c - data->comp[low].temp_c) *
  509. 1000 * ((int)ohm - (int)data->comp[low].ohm)) /
  510. ((int)data->comp[high].ohm - (int)data->comp[low].ohm);
  511. }
  512. return temp;
  513. }
  514. static int ntc_thermistor_get_ohm(struct ntc_data *data)
  515. {
  516. int read_uv;
  517. if (data->pdata->read_ohm)
  518. return data->pdata->read_ohm();
  519. if (data->pdata->read_uv) {
  520. read_uv = data->pdata->read_uv(data->pdata);
  521. if (read_uv < 0)
  522. return read_uv;
  523. return get_ohm_of_thermistor(data, read_uv);
  524. }
  525. return -EINVAL;
  526. }
  527. static int ntc_read(struct device *dev, enum hwmon_sensor_types type,
  528. u32 attr, int channel, long *val)
  529. {
  530. struct ntc_data *data = dev_get_drvdata(dev);
  531. int ohm;
  532. switch (type) {
  533. case hwmon_temp:
  534. switch (attr) {
  535. case hwmon_temp_input:
  536. ohm = ntc_thermistor_get_ohm(data);
  537. if (ohm < 0)
  538. return ohm;
  539. *val = get_temp_mc(data, ohm);
  540. return 0;
  541. case hwmon_temp_type:
  542. *val = 4;
  543. return 0;
  544. default:
  545. break;
  546. }
  547. break;
  548. default:
  549. break;
  550. }
  551. return -EINVAL;
  552. }
  553. static umode_t ntc_is_visible(const void *data, enum hwmon_sensor_types type,
  554. u32 attr, int channel)
  555. {
  556. if (type == hwmon_temp) {
  557. switch (attr) {
  558. case hwmon_temp_input:
  559. case hwmon_temp_type:
  560. return 0444;
  561. default:
  562. break;
  563. }
  564. }
  565. return 0;
  566. }
  567. static const struct hwmon_channel_info *ntc_info[] = {
  568. HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
  569. HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_TYPE),
  570. NULL
  571. };
  572. static const struct hwmon_ops ntc_hwmon_ops = {
  573. .is_visible = ntc_is_visible,
  574. .read = ntc_read,
  575. };
  576. static const struct hwmon_chip_info ntc_chip_info = {
  577. .ops = &ntc_hwmon_ops,
  578. .info = ntc_info,
  579. };
  580. static int ntc_thermistor_probe(struct platform_device *pdev)
  581. {
  582. struct device *dev = &pdev->dev;
  583. const struct of_device_id *of_id =
  584. of_match_device(of_match_ptr(ntc_match), dev);
  585. const struct platform_device_id *pdev_id;
  586. struct ntc_thermistor_platform_data *pdata;
  587. struct device *hwmon_dev;
  588. struct ntc_data *data;
  589. pdata = ntc_thermistor_parse_dt(dev);
  590. if (IS_ERR(pdata))
  591. return PTR_ERR(pdata);
  592. else if (pdata == NULL)
  593. pdata = dev_get_platdata(dev);
  594. if (!pdata) {
  595. dev_err(dev, "No platform init data supplied.\n");
  596. return -ENODEV;
  597. }
  598. /* Either one of the two is required. */
  599. if (!pdata->read_uv && !pdata->read_ohm) {
  600. dev_err(dev,
  601. "Both read_uv and read_ohm missing. Need either one of the two.\n");
  602. return -EINVAL;
  603. }
  604. if (pdata->read_uv && pdata->read_ohm) {
  605. dev_warn(dev,
  606. "Only one of read_uv and read_ohm is needed; ignoring read_uv.\n");
  607. pdata->read_uv = NULL;
  608. }
  609. if (pdata->read_uv && (pdata->pullup_uv == 0 ||
  610. (pdata->pullup_ohm == 0 && pdata->connect ==
  611. NTC_CONNECTED_GROUND) ||
  612. (pdata->pulldown_ohm == 0 && pdata->connect ==
  613. NTC_CONNECTED_POSITIVE) ||
  614. (pdata->connect != NTC_CONNECTED_POSITIVE &&
  615. pdata->connect != NTC_CONNECTED_GROUND))) {
  616. dev_err(dev, "Required data to use read_uv not supplied.\n");
  617. return -EINVAL;
  618. }
  619. data = devm_kzalloc(dev, sizeof(struct ntc_data), GFP_KERNEL);
  620. if (!data)
  621. return -ENOMEM;
  622. pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
  623. data->pdata = pdata;
  624. if (pdev_id->driver_data >= ARRAY_SIZE(ntc_type)) {
  625. dev_err(dev, "Unknown device type: %lu(%s)\n",
  626. pdev_id->driver_data, pdev_id->name);
  627. return -EINVAL;
  628. }
  629. data->comp = ntc_type[pdev_id->driver_data].comp;
  630. data->n_comp = ntc_type[pdev_id->driver_data].n_comp;
  631. hwmon_dev = devm_hwmon_device_register_with_info(dev, pdev_id->name,
  632. data, &ntc_chip_info,
  633. NULL);
  634. if (IS_ERR(hwmon_dev)) {
  635. dev_err(dev, "unable to register as hwmon device.\n");
  636. return PTR_ERR(hwmon_dev);
  637. }
  638. dev_info(dev, "Thermistor type: %s successfully probed.\n",
  639. pdev_id->name);
  640. return 0;
  641. }
  642. static struct platform_driver ntc_thermistor_driver = {
  643. .driver = {
  644. .name = "ntc-thermistor",
  645. .of_match_table = of_match_ptr(ntc_match),
  646. },
  647. .probe = ntc_thermistor_probe,
  648. .id_table = ntc_thermistor_id,
  649. };
  650. module_platform_driver(ntc_thermistor_driver);
  651. MODULE_DESCRIPTION("NTC Thermistor Driver");
  652. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  653. MODULE_LICENSE("GPL");
  654. MODULE_ALIAS("platform:ntc-thermistor");