ntc_thermistor.c 20 KB

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