ntc_thermistor.c 21 KB

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