ntc_thermistor.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/platform_data/ntc_thermistor.h>
  31. #include <linux/iio/iio.h>
  32. #include <linux/iio/machine.h>
  33. #include <linux/iio/driver.h>
  34. #include <linux/iio/consumer.h>
  35. #include <linux/hwmon.h>
  36. #include <linux/hwmon-sysfs.h>
  37. #include <linux/thermal.h>
  38. struct ntc_compensation {
  39. int temp_c;
  40. unsigned int ohm;
  41. };
  42. /* Order matters, ntc_match references the entries by index */
  43. static const struct platform_device_id ntc_thermistor_id[] = {
  44. { "ncp15wb473", TYPE_NCPXXWB473 },
  45. { "ncp18wb473", TYPE_NCPXXWB473 },
  46. { "ncp21wb473", TYPE_NCPXXWB473 },
  47. { "ncp03wb473", TYPE_NCPXXWB473 },
  48. { "ncp15wl333", TYPE_NCPXXWL333 },
  49. { "b57330v2103", TYPE_B57330V2103},
  50. { "ncp03wf104", TYPE_NCPXXWF104 },
  51. { "ncp15xh103", TYPE_NCPXXXH103 },
  52. { },
  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 table is from the specification of EPCOS NTC
  206. * Thermistors Datasheet
  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. struct ntc_data {
  245. struct ntc_thermistor_platform_data *pdata;
  246. const struct ntc_compensation *comp;
  247. int n_comp;
  248. };
  249. #if defined(CONFIG_OF) && IS_ENABLED(CONFIG_IIO)
  250. static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata)
  251. {
  252. struct iio_channel *channel = pdata->chan;
  253. int raw, uv, ret;
  254. ret = iio_read_channel_raw(channel, &raw);
  255. if (ret < 0) {
  256. pr_err("read channel() error: %d\n", ret);
  257. return ret;
  258. }
  259. ret = iio_convert_raw_to_processed(channel, raw, &uv, 1000);
  260. if (ret < 0) {
  261. /* Assume 12 bit ADC with vref at pullup_uv */
  262. uv = (pdata->pullup_uv * (s64)raw) >> 12;
  263. }
  264. return uv;
  265. }
  266. static const struct of_device_id ntc_match[] = {
  267. { .compatible = "murata,ncp15wb473",
  268. .data = &ntc_thermistor_id[0] },
  269. { .compatible = "murata,ncp18wb473",
  270. .data = &ntc_thermistor_id[1] },
  271. { .compatible = "murata,ncp21wb473",
  272. .data = &ntc_thermistor_id[2] },
  273. { .compatible = "murata,ncp03wb473",
  274. .data = &ntc_thermistor_id[3] },
  275. { .compatible = "murata,ncp15wl333",
  276. .data = &ntc_thermistor_id[4] },
  277. { .compatible = "epcos,b57330v2103",
  278. .data = &ntc_thermistor_id[5]},
  279. { .compatible = "murata,ncp03wf104",
  280. .data = &ntc_thermistor_id[6] },
  281. { .compatible = "murata,ncp15xh103",
  282. .data = &ntc_thermistor_id[7] },
  283. /* Usage of vendor name "ntc" is deprecated */
  284. { .compatible = "ntc,ncp15wb473",
  285. .data = &ntc_thermistor_id[0] },
  286. { .compatible = "ntc,ncp18wb473",
  287. .data = &ntc_thermistor_id[1] },
  288. { .compatible = "ntc,ncp21wb473",
  289. .data = &ntc_thermistor_id[2] },
  290. { .compatible = "ntc,ncp03wb473",
  291. .data = &ntc_thermistor_id[3] },
  292. { .compatible = "ntc,ncp15wl333",
  293. .data = &ntc_thermistor_id[4] },
  294. { },
  295. };
  296. MODULE_DEVICE_TABLE(of, ntc_match);
  297. static struct ntc_thermistor_platform_data *
  298. ntc_thermistor_parse_dt(struct device *dev)
  299. {
  300. struct iio_channel *chan;
  301. enum iio_chan_type type;
  302. struct device_node *np = dev->of_node;
  303. struct ntc_thermistor_platform_data *pdata;
  304. int ret;
  305. if (!np)
  306. return NULL;
  307. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  308. if (!pdata)
  309. return ERR_PTR(-ENOMEM);
  310. chan = devm_iio_channel_get(dev, NULL);
  311. if (IS_ERR(chan))
  312. return ERR_CAST(chan);
  313. ret = iio_get_channel_type(chan, &type);
  314. if (ret < 0)
  315. return ERR_PTR(ret);
  316. if (type != IIO_VOLTAGE)
  317. return ERR_PTR(-EINVAL);
  318. if (of_property_read_u32(np, "pullup-uv", &pdata->pullup_uv))
  319. return ERR_PTR(-ENODEV);
  320. if (of_property_read_u32(np, "pullup-ohm", &pdata->pullup_ohm))
  321. return ERR_PTR(-ENODEV);
  322. if (of_property_read_u32(np, "pulldown-ohm", &pdata->pulldown_ohm))
  323. return ERR_PTR(-ENODEV);
  324. if (of_find_property(np, "connected-positive", NULL))
  325. pdata->connect = NTC_CONNECTED_POSITIVE;
  326. else /* status change should be possible if not always on. */
  327. pdata->connect = NTC_CONNECTED_GROUND;
  328. pdata->chan = chan;
  329. pdata->read_uv = ntc_adc_iio_read;
  330. return pdata;
  331. }
  332. #else
  333. static struct ntc_thermistor_platform_data *
  334. ntc_thermistor_parse_dt(struct device *dev)
  335. {
  336. return NULL;
  337. }
  338. #define ntc_match NULL
  339. #endif
  340. static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
  341. {
  342. if (divisor == 0 && dividend == 0)
  343. return 0;
  344. if (divisor == 0)
  345. return UINT_MAX;
  346. return div64_u64(dividend, divisor);
  347. }
  348. static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
  349. {
  350. struct ntc_thermistor_platform_data *pdata = data->pdata;
  351. u32 puv = pdata->pullup_uv;
  352. u64 n, puo, pdo;
  353. puo = pdata->pullup_ohm;
  354. pdo = pdata->pulldown_ohm;
  355. if (uv == 0)
  356. return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
  357. INT_MAX : 0;
  358. if (uv >= puv)
  359. return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
  360. 0 : INT_MAX;
  361. if (pdata->connect == NTC_CONNECTED_POSITIVE && puo == 0)
  362. n = div_u64(pdo * (puv - uv), uv);
  363. else if (pdata->connect == NTC_CONNECTED_GROUND && pdo == 0)
  364. n = div_u64(puo * uv, puv - uv);
  365. else if (pdata->connect == NTC_CONNECTED_POSITIVE)
  366. n = div64_u64_safe(pdo * puo * (puv - uv),
  367. puo * uv - pdo * (puv - uv));
  368. else
  369. n = div64_u64_safe(pdo * puo * uv, pdo * (puv - uv) - puo * uv);
  370. if (n > INT_MAX)
  371. n = INT_MAX;
  372. return n;
  373. }
  374. static void lookup_comp(struct ntc_data *data, unsigned int ohm,
  375. int *i_low, int *i_high)
  376. {
  377. int start, end, mid;
  378. /*
  379. * Handle special cases: Resistance is higher than or equal to
  380. * resistance in first table entry, or resistance is lower or equal
  381. * to resistance in last table entry.
  382. * In these cases, return i_low == i_high, either pointing to the
  383. * beginning or to the end of the table depending on the condition.
  384. */
  385. if (ohm >= data->comp[0].ohm) {
  386. *i_low = 0;
  387. *i_high = 0;
  388. return;
  389. }
  390. if (ohm <= data->comp[data->n_comp - 1].ohm) {
  391. *i_low = data->n_comp - 1;
  392. *i_high = data->n_comp - 1;
  393. return;
  394. }
  395. /* Do a binary search on compensation table */
  396. start = 0;
  397. end = data->n_comp;
  398. while (start < end) {
  399. mid = start + (end - start) / 2;
  400. /*
  401. * start <= mid < end
  402. * data->comp[start].ohm > ohm >= data->comp[end].ohm
  403. *
  404. * We could check for "ohm == data->comp[mid].ohm" here, but
  405. * that is a quite unlikely condition, and we would have to
  406. * check again after updating start. Check it at the end instead
  407. * for simplicity.
  408. */
  409. if (ohm >= data->comp[mid].ohm) {
  410. end = mid;
  411. } else {
  412. start = mid + 1;
  413. /*
  414. * ohm >= data->comp[start].ohm might be true here,
  415. * since we set start to mid + 1. In that case, we are
  416. * done. We could keep going, but the condition is quite
  417. * likely to occur, so it is worth checking for it.
  418. */
  419. if (ohm >= data->comp[start].ohm)
  420. end = start;
  421. }
  422. /*
  423. * start <= end
  424. * data->comp[start].ohm >= ohm >= data->comp[end].ohm
  425. */
  426. }
  427. /*
  428. * start == end
  429. * ohm >= data->comp[end].ohm
  430. */
  431. *i_low = end;
  432. if (ohm == data->comp[end].ohm)
  433. *i_high = end;
  434. else
  435. *i_high = end - 1;
  436. }
  437. static int get_temp_mc(struct ntc_data *data, unsigned int ohm)
  438. {
  439. int low, high;
  440. int temp;
  441. lookup_comp(data, ohm, &low, &high);
  442. if (low == high) {
  443. /* Unable to use linear approximation */
  444. temp = data->comp[low].temp_c * 1000;
  445. } else {
  446. temp = data->comp[low].temp_c * 1000 +
  447. ((data->comp[high].temp_c - data->comp[low].temp_c) *
  448. 1000 * ((int)ohm - (int)data->comp[low].ohm)) /
  449. ((int)data->comp[high].ohm - (int)data->comp[low].ohm);
  450. }
  451. return temp;
  452. }
  453. static int ntc_thermistor_get_ohm(struct ntc_data *data)
  454. {
  455. int read_uv;
  456. if (data->pdata->read_ohm)
  457. return data->pdata->read_ohm();
  458. if (data->pdata->read_uv) {
  459. read_uv = data->pdata->read_uv(data->pdata);
  460. if (read_uv < 0)
  461. return read_uv;
  462. return get_ohm_of_thermistor(data, read_uv);
  463. }
  464. return -EINVAL;
  465. }
  466. static int ntc_read_temp(void *data, int *temp)
  467. {
  468. int ohm;
  469. ohm = ntc_thermistor_get_ohm(data);
  470. if (ohm < 0)
  471. return ohm;
  472. *temp = get_temp_mc(data, ohm);
  473. return 0;
  474. }
  475. static ssize_t ntc_show_type(struct device *dev,
  476. struct device_attribute *attr, char *buf)
  477. {
  478. return sprintf(buf, "4\n");
  479. }
  480. static ssize_t ntc_show_temp(struct device *dev,
  481. struct device_attribute *attr, char *buf)
  482. {
  483. struct ntc_data *data = dev_get_drvdata(dev);
  484. int ohm;
  485. ohm = ntc_thermistor_get_ohm(data);
  486. if (ohm < 0)
  487. return ohm;
  488. return sprintf(buf, "%d\n", get_temp_mc(data, ohm));
  489. }
  490. static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, ntc_show_type, NULL, 0);
  491. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ntc_show_temp, NULL, 0);
  492. static struct attribute *ntc_attrs[] = {
  493. &sensor_dev_attr_temp1_type.dev_attr.attr,
  494. &sensor_dev_attr_temp1_input.dev_attr.attr,
  495. NULL,
  496. };
  497. ATTRIBUTE_GROUPS(ntc);
  498. static const struct thermal_zone_of_device_ops ntc_of_thermal_ops = {
  499. .get_temp = ntc_read_temp,
  500. };
  501. static int ntc_thermistor_probe(struct platform_device *pdev)
  502. {
  503. struct thermal_zone_device *tz;
  504. struct device *dev = &pdev->dev;
  505. const struct of_device_id *of_id =
  506. of_match_device(of_match_ptr(ntc_match), dev);
  507. const struct platform_device_id *pdev_id;
  508. struct ntc_thermistor_platform_data *pdata;
  509. struct device *hwmon_dev;
  510. struct ntc_data *data;
  511. pdata = ntc_thermistor_parse_dt(dev);
  512. if (IS_ERR(pdata))
  513. return PTR_ERR(pdata);
  514. else if (pdata == NULL)
  515. pdata = dev_get_platdata(dev);
  516. if (!pdata) {
  517. dev_err(dev, "No platform init data supplied.\n");
  518. return -ENODEV;
  519. }
  520. /* Either one of the two is required. */
  521. if (!pdata->read_uv && !pdata->read_ohm) {
  522. dev_err(dev,
  523. "Both read_uv and read_ohm missing. Need either one of the two.\n");
  524. return -EINVAL;
  525. }
  526. if (pdata->read_uv && pdata->read_ohm) {
  527. dev_warn(dev,
  528. "Only one of read_uv and read_ohm is needed; ignoring read_uv.\n");
  529. pdata->read_uv = NULL;
  530. }
  531. if (pdata->read_uv && (pdata->pullup_uv == 0 ||
  532. (pdata->pullup_ohm == 0 && pdata->connect ==
  533. NTC_CONNECTED_GROUND) ||
  534. (pdata->pulldown_ohm == 0 && pdata->connect ==
  535. NTC_CONNECTED_POSITIVE) ||
  536. (pdata->connect != NTC_CONNECTED_POSITIVE &&
  537. pdata->connect != NTC_CONNECTED_GROUND))) {
  538. dev_err(dev, "Required data to use read_uv not supplied.\n");
  539. return -EINVAL;
  540. }
  541. data = devm_kzalloc(dev, sizeof(struct ntc_data), GFP_KERNEL);
  542. if (!data)
  543. return -ENOMEM;
  544. pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
  545. data->pdata = pdata;
  546. switch (pdev_id->driver_data) {
  547. case TYPE_NCPXXWB473:
  548. data->comp = ncpXXwb473;
  549. data->n_comp = ARRAY_SIZE(ncpXXwb473);
  550. break;
  551. case TYPE_NCPXXWL333:
  552. data->comp = ncpXXwl333;
  553. data->n_comp = ARRAY_SIZE(ncpXXwl333);
  554. break;
  555. case TYPE_B57330V2103:
  556. data->comp = b57330v2103;
  557. data->n_comp = ARRAY_SIZE(b57330v2103);
  558. break;
  559. case TYPE_NCPXXWF104:
  560. data->comp = ncpXXwf104;
  561. data->n_comp = ARRAY_SIZE(ncpXXwf104);
  562. break;
  563. case TYPE_NCPXXXH103:
  564. data->comp = ncpXXxh103;
  565. data->n_comp = ARRAY_SIZE(ncpXXxh103);
  566. break;
  567. default:
  568. dev_err(dev, "Unknown device type: %lu(%s)\n",
  569. pdev_id->driver_data, pdev_id->name);
  570. return -EINVAL;
  571. }
  572. hwmon_dev = devm_hwmon_device_register_with_groups(dev, pdev_id->name,
  573. data, ntc_groups);
  574. if (IS_ERR(hwmon_dev)) {
  575. dev_err(dev, "unable to register as hwmon device.\n");
  576. return PTR_ERR(hwmon_dev);
  577. }
  578. dev_info(dev, "Thermistor type: %s successfully probed.\n",
  579. pdev_id->name);
  580. tz = devm_thermal_zone_of_sensor_register(dev, 0, data,
  581. &ntc_of_thermal_ops);
  582. if (IS_ERR(tz))
  583. dev_dbg(dev, "Failed to register to thermal fw.\n");
  584. return 0;
  585. }
  586. static struct platform_driver ntc_thermistor_driver = {
  587. .driver = {
  588. .name = "ntc-thermistor",
  589. .of_match_table = of_match_ptr(ntc_match),
  590. },
  591. .probe = ntc_thermistor_probe,
  592. .id_table = ntc_thermistor_id,
  593. };
  594. module_platform_driver(ntc_thermistor_driver);
  595. MODULE_DESCRIPTION("NTC Thermistor Driver");
  596. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  597. MODULE_LICENSE("GPL");
  598. MODULE_ALIAS("platform:ntc-thermistor");